Commit 711a60b8f8b46622d58f81f63c2e2fca5bed0bf1
1 parent
bcd995f5
数据库版块集成
Showing
15 changed files
with
973 additions
and
0 deletions
core/database/.gitignore
0 → 100644
| 1 | +/build | ... | ... |
core/database/build.gradle
0 → 100644
| 1 | +apply plugin: 'com.android.library' | |
| 2 | +apply plugin: 'org.greenrobot.greendao' | |
| 3 | + | |
| 4 | +android { | |
| 5 | +// compileSdkVersion 29 | |
| 6 | +// buildToolsVersion "29.0.2" | |
| 7 | +// | |
| 8 | +// | |
| 9 | +// defaultConfig { | |
| 10 | +// minSdkVersion 16 | |
| 11 | +// targetSdkVersion 29 | |
| 12 | +// versionCode 1 | |
| 13 | +// versionName "1.0" | |
| 14 | +// | |
| 15 | +// testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |
| 16 | +// consumerProguardFiles 'consumer-rules.pro' | |
| 17 | +// } | |
| 18 | +// | |
| 19 | +// buildTypes { | |
| 20 | +// release { | |
| 21 | +// minifyEnabled false | |
| 22 | +// proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |
| 23 | +// } | |
| 24 | +// } | |
| 25 | + | |
| 26 | +} | |
| 27 | + | |
| 28 | +greendao { | |
| 29 | + schemaVersion 1 //数据库版本号 | |
| 30 | + daoPackage 'com.cnlive.database.dao' | |
| 31 | + targetGenDir 'src/main/java' | |
| 32 | + generateTests false //设置为true以自动生成单元测试。 | |
| 33 | + targetGenDirTests 'src/main/java' | |
| 34 | +} | |
| 35 | + | |
| 36 | +dependencies { | |
| 37 | + implementation fileTree(dir: 'libs', include: ['*.jar']) | |
| 38 | + | |
| 39 | + //数据库加密 | |
| 40 | + implementation 'net.zetetic:android-database-sqlcipher:3.5.6' | |
| 41 | +} | ... | ... |
core/database/consumer-rules.pro
0 → 100644
core/database/proguard-rules.pro
0 → 100644
| 1 | +# Add project specific ProGuard rules here. | |
| 2 | +# You can control the set of applied configuration files using the | |
| 3 | +# proguardFiles setting in build.gradle. | |
| 4 | +# | |
| 5 | +# For more details, see | |
| 6 | +# http://developer.android.com/guide/developing/tools/proguard.html | |
| 7 | + | |
| 8 | +# If your project uses WebView with JS, uncomment the following | |
| 9 | +# and specify the fully qualified class name to the JavaScript interface | |
| 10 | +# class: | |
| 11 | +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | |
| 12 | +# public *; | |
| 13 | +#} | |
| 14 | + | |
| 15 | +# Uncomment this to preserve the line number information for | |
| 16 | +# debugging stack traces. | |
| 17 | +#-keepattributes SourceFile,LineNumberTable | |
| 18 | + | |
| 19 | +# If you keep the line number information, uncomment this to | |
| 20 | +# hide the original source file name. | |
| 21 | +#-renamesourcefileattribute SourceFile | ... | ... |
core/database/src/androidTest/java/com/cnlive/database/ExampleInstrumentedTest.java
0 → 100644
| 1 | +package com.cnlive.database; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | + | |
| 5 | +import androidx.test.platform.app.InstrumentationRegistry; | |
| 6 | +import androidx.test.ext.junit.runners.AndroidJUnit4; | |
| 7 | + | |
| 8 | +import org.junit.Test; | |
| 9 | +import org.junit.runner.RunWith; | |
| 10 | + | |
| 11 | +import static org.junit.Assert.*; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Instrumented test, which will execute on an Android device. | |
| 15 | + * | |
| 16 | + * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | |
| 17 | + */ | |
| 18 | +@RunWith(AndroidJUnit4.class) | |
| 19 | +public class ExampleInstrumentedTest { | |
| 20 | + @Test | |
| 21 | + public void useAppContext() { | |
| 22 | + // Context of the app under test. | |
| 23 | + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | |
| 24 | + | |
| 25 | + assertEquals("com.cnlive.database.test", appContext.getPackageName()); | |
| 26 | + } | |
| 27 | +} | ... | ... |
core/database/src/main/AndroidManifest.xml
0 → 100644
core/database/src/main/java/com/cnlive/database/bean/User.java
0 → 100644
| 1 | +package com.cnlive.database.bean; | |
| 2 | + | |
| 3 | +import org.greenrobot.greendao.annotation.Entity; | |
| 4 | +import org.greenrobot.greendao.annotation.Id; | |
| 5 | +import org.greenrobot.greendao.annotation.Unique; | |
| 6 | +import org.greenrobot.greendao.annotation.Generated; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * created by hanyayun | |
| 10 | + * on 2019/12/12 | |
| 11 | + */ | |
| 12 | + | |
| 13 | +@Entity | |
| 14 | +public class User { | |
| 15 | + @Id(autoincrement = true) | |
| 16 | + Long id; | |
| 17 | + @Unique | |
| 18 | + int studentNo;//学号 | |
| 19 | + int age; //年龄 | |
| 20 | + String telPhone;//手机号 | |
| 21 | + String sex; //性别 | |
| 22 | + String name;//姓名 | |
| 23 | + String address;//家庭住址 | |
| 24 | + String schoolName;//学校名字 | |
| 25 | + String grade;//几年级 | |
| 26 | + @Generated(hash = 473708544) | |
| 27 | + public User(Long id, int studentNo, int age, String telPhone, String sex, | |
| 28 | + String name, String address, String schoolName, String grade) { | |
| 29 | + this.id = id; | |
| 30 | + this.studentNo = studentNo; | |
| 31 | + this.age = age; | |
| 32 | + this.telPhone = telPhone; | |
| 33 | + this.sex = sex; | |
| 34 | + this.name = name; | |
| 35 | + this.address = address; | |
| 36 | + this.schoolName = schoolName; | |
| 37 | + this.grade = grade; | |
| 38 | + } | |
| 39 | + @Generated(hash = 586692638) | |
| 40 | + public User() { | |
| 41 | + } | |
| 42 | + public Long getId() { | |
| 43 | + return this.id; | |
| 44 | + } | |
| 45 | + public void setId(Long id) { | |
| 46 | + this.id = id; | |
| 47 | + } | |
| 48 | + public int getStudentNo() { | |
| 49 | + return this.studentNo; | |
| 50 | + } | |
| 51 | + public void setStudentNo(int studentNo) { | |
| 52 | + this.studentNo = studentNo; | |
| 53 | + } | |
| 54 | + public int getAge() { | |
| 55 | + return this.age; | |
| 56 | + } | |
| 57 | + public void setAge(int age) { | |
| 58 | + this.age = age; | |
| 59 | + } | |
| 60 | + public String getTelPhone() { | |
| 61 | + return this.telPhone; | |
| 62 | + } | |
| 63 | + public void setTelPhone(String telPhone) { | |
| 64 | + this.telPhone = telPhone; | |
| 65 | + } | |
| 66 | + public String getSex() { | |
| 67 | + return this.sex; | |
| 68 | + } | |
| 69 | + public void setSex(String sex) { | |
| 70 | + this.sex = sex; | |
| 71 | + } | |
| 72 | + public String getName() { | |
| 73 | + return this.name; | |
| 74 | + } | |
| 75 | + public void setName(String name) { | |
| 76 | + this.name = name; | |
| 77 | + } | |
| 78 | + public String getAddress() { | |
| 79 | + return this.address; | |
| 80 | + } | |
| 81 | + public void setAddress(String address) { | |
| 82 | + this.address = address; | |
| 83 | + } | |
| 84 | + public String getSchoolName() { | |
| 85 | + return this.schoolName; | |
| 86 | + } | |
| 87 | + public void setSchoolName(String schoolName) { | |
| 88 | + this.schoolName = schoolName; | |
| 89 | + } | |
| 90 | + public String getGrade() { | |
| 91 | + return this.grade; | |
| 92 | + } | |
| 93 | + public void setGrade(String grade) { | |
| 94 | + this.grade = grade; | |
| 95 | + } | |
| 96 | +} | ... | ... |
core/database/src/main/java/com/cnlive/database/dao/DaoMaster.java
0 → 100644
| 1 | +package com.cnlive.database.dao; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.database.sqlite.SQLiteDatabase; | |
| 5 | +import android.database.sqlite.SQLiteDatabase.CursorFactory; | |
| 6 | +import android.util.Log; | |
| 7 | + | |
| 8 | +import org.greenrobot.greendao.AbstractDaoMaster; | |
| 9 | +import org.greenrobot.greendao.database.StandardDatabase; | |
| 10 | +import org.greenrobot.greendao.database.Database; | |
| 11 | +import org.greenrobot.greendao.database.DatabaseOpenHelper; | |
| 12 | +import org.greenrobot.greendao.identityscope.IdentityScopeType; | |
| 13 | + | |
| 14 | + | |
| 15 | +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. | |
| 16 | +/** | |
| 17 | + * Master of DAO (schema version 1): knows all DAOs. | |
| 18 | + */ | |
| 19 | +public class DaoMaster extends AbstractDaoMaster { | |
| 20 | + public static final int SCHEMA_VERSION = 1; | |
| 21 | + | |
| 22 | + /** Creates underlying database table using DAOs. */ | |
| 23 | + public static void createAllTables(Database db, boolean ifNotExists) { | |
| 24 | + UserDao.createTable(db, ifNotExists); | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** Drops underlying database table using DAOs. */ | |
| 28 | + public static void dropAllTables(Database db, boolean ifExists) { | |
| 29 | + UserDao.dropTable(db, ifExists); | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * WARNING: Drops all table on Upgrade! Use only during development. | |
| 34 | + * Convenience method using a {@link DevOpenHelper}. | |
| 35 | + */ | |
| 36 | + public static DaoSession newDevSession(Context context, String name) { | |
| 37 | + Database db = new DevOpenHelper(context, name).getWritableDb(); | |
| 38 | + DaoMaster daoMaster = new DaoMaster(db); | |
| 39 | + return daoMaster.newSession(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + public DaoMaster(SQLiteDatabase db) { | |
| 43 | + this(new StandardDatabase(db)); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public DaoMaster(Database db) { | |
| 47 | + super(db, SCHEMA_VERSION); | |
| 48 | + registerDaoClass(UserDao.class); | |
| 49 | + } | |
| 50 | + | |
| 51 | + public DaoSession newSession() { | |
| 52 | + return new DaoSession(db, IdentityScopeType.Session, daoConfigMap); | |
| 53 | + } | |
| 54 | + | |
| 55 | + public DaoSession newSession(IdentityScopeType type) { | |
| 56 | + return new DaoSession(db, type, daoConfigMap); | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Calls {@link #createAllTables(Database, boolean)} in {@link #onCreate(Database)} - | |
| 61 | + */ | |
| 62 | + public static abstract class OpenHelper extends DatabaseOpenHelper { | |
| 63 | + public OpenHelper(Context context, String name) { | |
| 64 | + super(context, name, SCHEMA_VERSION); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public OpenHelper(Context context, String name, CursorFactory factory) { | |
| 68 | + super(context, name, factory, SCHEMA_VERSION); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Override | |
| 72 | + public void onCreate(Database db) { | |
| 73 | + Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION); | |
| 74 | + createAllTables(db, false); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** WARNING: Drops all table on Upgrade! Use only during development. */ | |
| 79 | + public static class DevOpenHelper extends OpenHelper { | |
| 80 | + public DevOpenHelper(Context context, String name) { | |
| 81 | + super(context, name); | |
| 82 | + } | |
| 83 | + | |
| 84 | + public DevOpenHelper(Context context, String name, CursorFactory factory) { | |
| 85 | + super(context, name, factory); | |
| 86 | + } | |
| 87 | + | |
| 88 | + @Override | |
| 89 | + public void onUpgrade(Database db, int oldVersion, int newVersion) { | |
| 90 | + Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables"); | |
| 91 | + dropAllTables(db, true); | |
| 92 | + onCreate(db); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | +} | ... | ... |
core/database/src/main/java/com/cnlive/database/dao/DaoSession.java
0 → 100644
| 1 | +package com.cnlive.database.dao; | |
| 2 | + | |
| 3 | +import java.util.Map; | |
| 4 | + | |
| 5 | +import org.greenrobot.greendao.AbstractDao; | |
| 6 | +import org.greenrobot.greendao.AbstractDaoSession; | |
| 7 | +import org.greenrobot.greendao.database.Database; | |
| 8 | +import org.greenrobot.greendao.identityscope.IdentityScopeType; | |
| 9 | +import org.greenrobot.greendao.internal.DaoConfig; | |
| 10 | + | |
| 11 | +import com.cnlive.database.bean.User; | |
| 12 | + | |
| 13 | +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * {@inheritDoc} | |
| 17 | + * | |
| 18 | + * @see org.greenrobot.greendao.AbstractDaoSession | |
| 19 | + */ | |
| 20 | +public class DaoSession extends AbstractDaoSession { | |
| 21 | + | |
| 22 | + private final DaoConfig userDaoConfig; | |
| 23 | + | |
| 24 | + private final UserDao userDao; | |
| 25 | + | |
| 26 | + public DaoSession(Database db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig> | |
| 27 | + daoConfigMap) { | |
| 28 | + super(db); | |
| 29 | + | |
| 30 | + userDaoConfig = daoConfigMap.get(UserDao.class).clone(); | |
| 31 | + userDaoConfig.initIdentityScope(type); | |
| 32 | + | |
| 33 | + userDao = new UserDao(userDaoConfig, this); | |
| 34 | + | |
| 35 | + registerDao(User.class, userDao); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public void clear() { | |
| 39 | + userDaoConfig.clearIdentityScope(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + public UserDao getUserDao() { | |
| 43 | + return userDao; | |
| 44 | + } | |
| 45 | + | |
| 46 | +} | ... | ... |
core/database/src/main/java/com/cnlive/database/dao/UserDao.java
0 → 100644
| 1 | +package com.cnlive.database.dao; | |
| 2 | + | |
| 3 | +import android.database.Cursor; | |
| 4 | +import android.database.sqlite.SQLiteStatement; | |
| 5 | + | |
| 6 | +import org.greenrobot.greendao.AbstractDao; | |
| 7 | +import org.greenrobot.greendao.Property; | |
| 8 | +import org.greenrobot.greendao.internal.DaoConfig; | |
| 9 | +import org.greenrobot.greendao.database.Database; | |
| 10 | +import org.greenrobot.greendao.database.DatabaseStatement; | |
| 11 | + | |
| 12 | +import com.cnlive.database.bean.User; | |
| 13 | + | |
| 14 | +// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. | |
| 15 | +/** | |
| 16 | + * DAO for table "USER". | |
| 17 | +*/ | |
| 18 | +public class UserDao extends AbstractDao<User, Long> { | |
| 19 | + | |
| 20 | + public static final String TABLENAME = "USER"; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Properties of entity User.<br/> | |
| 24 | + * Can be used for QueryBuilder and for referencing column names. | |
| 25 | + */ | |
| 26 | + public static class Properties { | |
| 27 | + public final static Property Id = new Property(0, Long.class, "id", true, "_id"); | |
| 28 | + public final static Property StudentNo = new Property(1, int.class, "studentNo", false, "STUDENT_NO"); | |
| 29 | + public final static Property Age = new Property(2, int.class, "age", false, "AGE"); | |
| 30 | + public final static Property TelPhone = new Property(3, String.class, "telPhone", false, "TEL_PHONE"); | |
| 31 | + public final static Property Sex = new Property(4, String.class, "sex", false, "SEX"); | |
| 32 | + public final static Property Name = new Property(5, String.class, "name", false, "NAME"); | |
| 33 | + public final static Property Address = new Property(6, String.class, "address", false, "ADDRESS"); | |
| 34 | + public final static Property SchoolName = new Property(7, String.class, "schoolName", false, "SCHOOL_NAME"); | |
| 35 | + public final static Property Grade = new Property(8, String.class, "grade", false, "GRADE"); | |
| 36 | + } | |
| 37 | + | |
| 38 | + | |
| 39 | + public UserDao(DaoConfig config) { | |
| 40 | + super(config); | |
| 41 | + } | |
| 42 | + | |
| 43 | + public UserDao(DaoConfig config, DaoSession daoSession) { | |
| 44 | + super(config, daoSession); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** Creates the underlying database table. */ | |
| 48 | + public static void createTable(Database db, boolean ifNotExists) { | |
| 49 | + String constraint = ifNotExists? "IF NOT EXISTS ": ""; | |
| 50 | + db.execSQL("CREATE TABLE " + constraint + "\"USER\" (" + // | |
| 51 | + "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id | |
| 52 | + "\"STUDENT_NO\" INTEGER NOT NULL UNIQUE ," + // 1: studentNo | |
| 53 | + "\"AGE\" INTEGER NOT NULL ," + // 2: age | |
| 54 | + "\"TEL_PHONE\" TEXT," + // 3: telPhone | |
| 55 | + "\"SEX\" TEXT," + // 4: sex | |
| 56 | + "\"NAME\" TEXT," + // 5: name | |
| 57 | + "\"ADDRESS\" TEXT," + // 6: address | |
| 58 | + "\"SCHOOL_NAME\" TEXT," + // 7: schoolName | |
| 59 | + "\"GRADE\" TEXT);"); // 8: grade | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** Drops the underlying database table. */ | |
| 63 | + public static void dropTable(Database db, boolean ifExists) { | |
| 64 | + String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"USER\""; | |
| 65 | + db.execSQL(sql); | |
| 66 | + } | |
| 67 | + | |
| 68 | + @Override | |
| 69 | + protected final void bindValues(DatabaseStatement stmt, User entity) { | |
| 70 | + stmt.clearBindings(); | |
| 71 | + | |
| 72 | + Long id = entity.getId(); | |
| 73 | + if (id != null) { | |
| 74 | + stmt.bindLong(1, id); | |
| 75 | + } | |
| 76 | + stmt.bindLong(2, entity.getStudentNo()); | |
| 77 | + stmt.bindLong(3, entity.getAge()); | |
| 78 | + | |
| 79 | + String telPhone = entity.getTelPhone(); | |
| 80 | + if (telPhone != null) { | |
| 81 | + stmt.bindString(4, telPhone); | |
| 82 | + } | |
| 83 | + | |
| 84 | + String sex = entity.getSex(); | |
| 85 | + if (sex != null) { | |
| 86 | + stmt.bindString(5, sex); | |
| 87 | + } | |
| 88 | + | |
| 89 | + String name = entity.getName(); | |
| 90 | + if (name != null) { | |
| 91 | + stmt.bindString(6, name); | |
| 92 | + } | |
| 93 | + | |
| 94 | + String address = entity.getAddress(); | |
| 95 | + if (address != null) { | |
| 96 | + stmt.bindString(7, address); | |
| 97 | + } | |
| 98 | + | |
| 99 | + String schoolName = entity.getSchoolName(); | |
| 100 | + if (schoolName != null) { | |
| 101 | + stmt.bindString(8, schoolName); | |
| 102 | + } | |
| 103 | + | |
| 104 | + String grade = entity.getGrade(); | |
| 105 | + if (grade != null) { | |
| 106 | + stmt.bindString(9, grade); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Override | |
| 111 | + protected final void bindValues(SQLiteStatement stmt, User entity) { | |
| 112 | + stmt.clearBindings(); | |
| 113 | + | |
| 114 | + Long id = entity.getId(); | |
| 115 | + if (id != null) { | |
| 116 | + stmt.bindLong(1, id); | |
| 117 | + } | |
| 118 | + stmt.bindLong(2, entity.getStudentNo()); | |
| 119 | + stmt.bindLong(3, entity.getAge()); | |
| 120 | + | |
| 121 | + String telPhone = entity.getTelPhone(); | |
| 122 | + if (telPhone != null) { | |
| 123 | + stmt.bindString(4, telPhone); | |
| 124 | + } | |
| 125 | + | |
| 126 | + String sex = entity.getSex(); | |
| 127 | + if (sex != null) { | |
| 128 | + stmt.bindString(5, sex); | |
| 129 | + } | |
| 130 | + | |
| 131 | + String name = entity.getName(); | |
| 132 | + if (name != null) { | |
| 133 | + stmt.bindString(6, name); | |
| 134 | + } | |
| 135 | + | |
| 136 | + String address = entity.getAddress(); | |
| 137 | + if (address != null) { | |
| 138 | + stmt.bindString(7, address); | |
| 139 | + } | |
| 140 | + | |
| 141 | + String schoolName = entity.getSchoolName(); | |
| 142 | + if (schoolName != null) { | |
| 143 | + stmt.bindString(8, schoolName); | |
| 144 | + } | |
| 145 | + | |
| 146 | + String grade = entity.getGrade(); | |
| 147 | + if (grade != null) { | |
| 148 | + stmt.bindString(9, grade); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + @Override | |
| 153 | + public Long readKey(Cursor cursor, int offset) { | |
| 154 | + return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0); | |
| 155 | + } | |
| 156 | + | |
| 157 | + @Override | |
| 158 | + public User readEntity(Cursor cursor, int offset) { | |
| 159 | + User entity = new User( // | |
| 160 | + cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id | |
| 161 | + cursor.getInt(offset + 1), // studentNo | |
| 162 | + cursor.getInt(offset + 2), // age | |
| 163 | + cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // telPhone | |
| 164 | + cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // sex | |
| 165 | + cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // name | |
| 166 | + cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // address | |
| 167 | + cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // schoolName | |
| 168 | + cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8) // grade | |
| 169 | + ); | |
| 170 | + return entity; | |
| 171 | + } | |
| 172 | + | |
| 173 | + @Override | |
| 174 | + public void readEntity(Cursor cursor, User entity, int offset) { | |
| 175 | + entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); | |
| 176 | + entity.setStudentNo(cursor.getInt(offset + 1)); | |
| 177 | + entity.setAge(cursor.getInt(offset + 2)); | |
| 178 | + entity.setTelPhone(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3)); | |
| 179 | + entity.setSex(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); | |
| 180 | + entity.setName(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5)); | |
| 181 | + entity.setAddress(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6)); | |
| 182 | + entity.setSchoolName(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7)); | |
| 183 | + entity.setGrade(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8)); | |
| 184 | + } | |
| 185 | + | |
| 186 | + @Override | |
| 187 | + protected final Long updateKeyAfterInsert(User entity, long rowId) { | |
| 188 | + entity.setId(rowId); | |
| 189 | + return rowId; | |
| 190 | + } | |
| 191 | + | |
| 192 | + @Override | |
| 193 | + public Long getKey(User entity) { | |
| 194 | + if(entity != null) { | |
| 195 | + return entity.getId(); | |
| 196 | + } else { | |
| 197 | + return null; | |
| 198 | + } | |
| 199 | + } | |
| 200 | + | |
| 201 | + @Override | |
| 202 | + public boolean hasKey(User entity) { | |
| 203 | + return entity.getId() != null; | |
| 204 | + } | |
| 205 | + | |
| 206 | + @Override | |
| 207 | + protected final boolean isEntityUpdateable() { | |
| 208 | + return true; | |
| 209 | + } | |
| 210 | + | |
| 211 | +} | ... | ... |
core/database/src/main/java/com/cnlive/database/dbUtil/DaoHelper.java
0 → 100644
| 1 | +package com.cnlive.database.dbUtil; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | + | |
| 5 | + | |
| 6 | +import com.cnlive.database.dao.DaoMaster; | |
| 7 | +import com.cnlive.database.dao.DaoSession; | |
| 8 | + | |
| 9 | +import org.greenrobot.greendao.database.Database; | |
| 10 | + | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Created by xiansong on 2017/9/8. | |
| 14 | + */ | |
| 15 | + | |
| 16 | +public class DaoHelper { | |
| 17 | + private static char[] databasePassword = "com.cnlive.strike_Passw0rd".toCharArray(); | |
| 18 | + private static DaoHelper instance; | |
| 19 | + private Database db; | |
| 20 | + private DaoMaster daoMaster; | |
| 21 | + private DaoSession daoSession; | |
| 22 | + | |
| 23 | + public DaoHelper(Context context) { | |
| 24 | +// CNLiveUserInfo userInfo = UserService.getInstance(context).getAppAccount(); | |
| 25 | +// DaoMaster.OpenHelper helper = new MigrationHelper(context, (userInfo == null ? "" : userInfo.getUid()) + "-strike-db", null); | |
| 26 | +// db = helper.getEncryptedWritableDb(databasePassword); | |
| 27 | +// daoMaster = new DaoMaster(db); | |
| 28 | +// daoSession = daoMaster.newSession(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static synchronized DaoHelper getInstance(Context ctx) { | |
| 32 | + if (instance == null) | |
| 33 | + instance = new DaoHelper(ctx.getApplicationContext()); | |
| 34 | + return instance; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public static synchronized DaoHelper updateInstance(Context ctx) { | |
| 38 | + instance = new DaoHelper(ctx.getApplicationContext()); | |
| 39 | + return instance; | |
| 40 | + } | |
| 41 | + | |
| 42 | +// public ExceptionInfoDao getExceptionInfoDao() { | |
| 43 | +// return daoSession.getExceptionInfoDao(); | |
| 44 | +// } | |
| 45 | +// | |
| 46 | +// public IMGroupMembersDao getIMGroupMembersDao() { | |
| 47 | +// return daoSession.getIMGroupMembersDao(); | |
| 48 | +// } | |
| 49 | +// | |
| 50 | +// public IMGroupMembersListDao getIMGroupMembersListDao() { | |
| 51 | +// return daoSession.getIMGroupMembersListDao(); | |
| 52 | +// } | |
| 53 | +// | |
| 54 | +// public IMUserInfoDao getIMUserInfoDao() { | |
| 55 | +// return daoSession.getIMUserInfoDao(); | |
| 56 | +// } | |
| 57 | +// | |
| 58 | +// public IMConversationInfoDao getIMConversationInfoDao() { | |
| 59 | +// return daoSession.getIMConversationInfoDao(); | |
| 60 | +// } | |
| 61 | +// | |
| 62 | +// public SysNumReadableDao getSysNumReadableDao() { | |
| 63 | +// return daoSession.getSysNumReadableDao(); | |
| 64 | +// } | |
| 65 | +// | |
| 66 | +// public DBFriendCircleMessageDao getFriendCircleMessageDao() { | |
| 67 | +// return daoSession.getDBFriendCircleMessageDao(); | |
| 68 | +// } | |
| 69 | +// | |
| 70 | +// public DBMomentDetailDao getDBMomentDetailDao() { | |
| 71 | +// return daoSession.getDBMomentDetailDao(); | |
| 72 | +// } | |
| 73 | +// | |
| 74 | +// public DBMomentItemDao getDBMomentItemDao() { | |
| 75 | +// return daoSession.getDBMomentItemDao(); | |
| 76 | +// } | |
| 77 | +// | |
| 78 | +// public ChatMediaDao getChatMediaDao() { | |
| 79 | +// return daoSession.getChatMediaDao(); | |
| 80 | +// } | |
| 81 | +// | |
| 82 | +// public SearchHistoryDao getSearchHistoryDao() { | |
| 83 | +// return daoSession.getSearchHistoryDao(); | |
| 84 | +// } | |
| 85 | +// | |
| 86 | +// | |
| 87 | +// public ChatImageAndVideoMsgDao getChatImageAndVideoMsgDao() { | |
| 88 | +// return daoSession.getChatImageAndVideoMsgDao(); | |
| 89 | +// } | |
| 90 | +// | |
| 91 | +// public WitnessDao getWitnessDao() { | |
| 92 | +// return daoSession.getWitnessDao(); | |
| 93 | +// } | |
| 94 | +// public SearchContentDao getSearchContentDao(){ | |
| 95 | +// return daoSession.getSearchContentDao(); | |
| 96 | +// } | |
| 97 | +// public DarenDetailMsgDao getDarenDetailDao() { | |
| 98 | +// return daoSession.getDarenDetailMsgDao(); | |
| 99 | +// } | |
| 100 | +// | |
| 101 | +// public DarenListMsgDao getDarenListDao() { | |
| 102 | +// return daoSession.getDarenListMsgDao(); | |
| 103 | +// } | |
| 104 | +// | |
| 105 | +// public ChatFileMsgDao getChatFileMsgDao() { | |
| 106 | +// return daoSession.getChatFileMsgDao(); | |
| 107 | +// } | |
| 108 | +// | |
| 109 | +// public AudioPlayHistoricalRecordDao getAudioHistoryRecordDao() { | |
| 110 | +// return daoSession.getAudioPlayHistoricalRecordDao(); | |
| 111 | +// } | |
| 112 | +// | |
| 113 | +// public AudioPlayLastInfoDao getAudioPlayLastInfoDao() { | |
| 114 | +// return daoSession.getAudioPlayLastInfoDao(); | |
| 115 | +// } | |
| 116 | +// | |
| 117 | +// public DownloadInfoDao getDownloadInfoDao() { | |
| 118 | +// return daoSession.getDownloadInfoDao(); | |
| 119 | +// } | |
| 120 | +// | |
| 121 | +// public NewsNumberDao getNewsNumberDao() { | |
| 122 | +// return daoSession.getNewsNumberDao(); | |
| 123 | +// } | |
| 124 | +// | |
| 125 | +// /** | |
| 126 | +// * 新的朋友 | |
| 127 | +// * | |
| 128 | +// * @return | |
| 129 | +// */ | |
| 130 | +// public NewContactFriendEntityDao getNewContactFriendEntityDao() { | |
| 131 | +// return daoSession.getNewContactFriendEntityDao(); | |
| 132 | +// } | |
| 133 | +// | |
| 134 | +// /** | |
| 135 | +// * 新的朋友 | |
| 136 | +// * | |
| 137 | +// * @return | |
| 138 | +// */ | |
| 139 | +// public NewPhoneContactEntityDao getNewPhoneContactEntityDao() { | |
| 140 | +// return daoSession.getNewPhoneContactEntityDao(); | |
| 141 | +// } | |
| 142 | +// | |
| 143 | +// public IMGroupFaceMemberIdsDao getGroupFaceMemberIdsDao() { | |
| 144 | +// return daoSession.getIMGroupFaceMemberIdsDao(); | |
| 145 | +// } | |
| 146 | +// | |
| 147 | +// /** | |
| 148 | +// * 联系人-群聊 | |
| 149 | +// * | |
| 150 | +// * @return | |
| 151 | +// */ | |
| 152 | +// public ContanctGroupChatEntityDao getContanctGroupChatEntityDao() { | |
| 153 | +// return daoSession.getContanctGroupChatEntityDao(); | |
| 154 | +// } | |
| 155 | +// | |
| 156 | +// /** | |
| 157 | +// * 联系人-达人列表 | |
| 158 | +// * | |
| 159 | +// * @return | |
| 160 | +// */ | |
| 161 | +// public ContanctDaRenEntityDao getContanctDaRenEntityDao() { | |
| 162 | +// return daoSession.getContanctDaRenEntityDao(); | |
| 163 | +// } | |
| 164 | +// | |
| 165 | +// public IMGroupMembersManagerListDao getGroupMemberManagerIdsDao() { | |
| 166 | +// return daoSession.getIMGroupMembersManagerListDao(); | |
| 167 | +// } | |
| 168 | +// | |
| 169 | +// public ContactListDao getContactListDao() { | |
| 170 | +// return daoSession.getContactListDao(); | |
| 171 | +// } | |
| 172 | +// | |
| 173 | +// public LaunchAdInfoDao getLaunchAdInfo() { | |
| 174 | +// return daoSession.getLaunchAdInfoDao(); | |
| 175 | +// } | |
| 176 | +// | |
| 177 | +// public HolidayThemeInfoDao getHolidayThemeInfoDao() { | |
| 178 | +// return daoSession.getHolidayThemeInfoDao(); | |
| 179 | +// } | |
| 180 | +} | ... | ... |
core/database/src/main/java/com/cnlive/database/dbUtil/MigrationHelper.java
0 → 100644
| 1 | +package com.cnlive.database.dbUtil; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.database.sqlite.SQLiteDatabase; | |
| 5 | + | |
| 6 | +import com.cnlive.database.dao.DaoMaster; | |
| 7 | +import com.cnlive.database.dao.UserDao; | |
| 8 | + | |
| 9 | +import org.greenrobot.greendao.database.Database; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * created by hanyayun | |
| 13 | + * on 2019/12/12 | |
| 14 | + */ | |
| 15 | +public class MigrationHelper extends DaoMaster.OpenHelper { | |
| 16 | + | |
| 17 | + public MigrationHelper(Context context, String name) { | |
| 18 | + super(context, name); | |
| 19 | + } | |
| 20 | + | |
| 21 | + public MigrationHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) { | |
| 22 | + super(context, name, factory); | |
| 23 | + } | |
| 24 | + | |
| 25 | + @Override | |
| 26 | + public void onUpgrade(Database db, int oldVersion, int newVersion) { | |
| 27 | + new UpgradeHelper().migrate(db, UserDao.class | |
| 28 | +// ExceptionInfoDao.class, | |
| 29 | +// IMGroupMembersListDao.class, | |
| 30 | +// IMGroupMembersDao.class, | |
| 31 | +// IMUserInfoDao.class, | |
| 32 | +// IMConversationInfoDao.class, | |
| 33 | +// SysNumReadableDao.class, | |
| 34 | +// DBFriendCircleMessageDao.class, | |
| 35 | +// ChatMediaDao.class, | |
| 36 | +// SearchHistoryDao.class, | |
| 37 | +// ChatImageAndVideoMsgDao.class, | |
| 38 | +// DBMomentDetailDao.class, | |
| 39 | +// DBMomentItemDao.class, | |
| 40 | +// DarenDetailMsgDao.class, | |
| 41 | +// DarenListMsgDao.class, | |
| 42 | +// ChatFileMsgDao.class, | |
| 43 | +// WitnessDao.class, | |
| 44 | +// AudioPlayHistoricalRecordDao.class, | |
| 45 | +// DownloadInfoDao.class, | |
| 46 | +// NewsNumberDao.class, | |
| 47 | +// NewContactFriendEntityDao.class, | |
| 48 | +// NewPhoneContactEntityDao.class, | |
| 49 | +// AudioPlayLastInfoDao.class, | |
| 50 | +// IMGroupFaceMemberIdsDao.class, | |
| 51 | +// IMGroupMembersManagerListDao.class, | |
| 52 | +// ContactListDao.class, | |
| 53 | +// SearchContentDao.class, | |
| 54 | +// IMGroupMembersManagerListDao.class, | |
| 55 | +// LaunchAdInfoDao.class, | |
| 56 | +// HolidayThemeInfoDao.class | |
| 57 | + ); | |
| 58 | + } | |
| 59 | +} | ... | ... |
core/database/src/main/java/com/cnlive/database/dbUtil/UpgradeHelper.java
0 → 100644
| 1 | +package com.cnlive.database.dbUtil; | |
| 2 | + | |
| 3 | +import android.database.Cursor; | |
| 4 | +import android.text.TextUtils; | |
| 5 | + | |
| 6 | +import com.cnlive.database.dao.DaoMaster; | |
| 7 | + | |
| 8 | +import org.greenrobot.greendao.AbstractDao; | |
| 9 | +import org.greenrobot.greendao.database.Database; | |
| 10 | +import org.greenrobot.greendao.internal.DaoConfig; | |
| 11 | + | |
| 12 | +import java.util.ArrayList; | |
| 13 | +import java.util.Arrays; | |
| 14 | +import java.util.List; | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * created by hanyayun | |
| 18 | + * on 2019/12/12 | |
| 19 | + */ | |
| 20 | +public class UpgradeHelper { | |
| 21 | + | |
| 22 | + private static final String CONVERSION_CLASS_NOT_FOUND_EXCEPTION = "MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS"; | |
| 23 | + | |
| 24 | + public void migrate(final Database db, final Class<? extends AbstractDao<?, ?>>... daoClasses) { | |
| 25 | + generateTempTables(db, daoClasses); | |
| 26 | + DaoMaster.dropAllTables(db, true); | |
| 27 | + DaoMaster.createAllTables(db, false); | |
| 28 | + restoreData(db, daoClasses); | |
| 29 | + } | |
| 30 | + | |
| 31 | + private void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { | |
| 32 | + for (int i = 0; i < daoClasses.length; i++) { | |
| 33 | + try { | |
| 34 | + DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]); | |
| 35 | + String divider = ""; | |
| 36 | + String tableName = daoConfig.tablename; | |
| 37 | + String tempTableName = daoConfig.tablename.concat("_TEMP"); | |
| 38 | + ArrayList<String> properties = new ArrayList<>(); | |
| 39 | + | |
| 40 | + StringBuilder createTableStringBuilder = new StringBuilder(); | |
| 41 | + | |
| 42 | + createTableStringBuilder.append("CREATE TABLE ").append(tempTableName).append(" ("); | |
| 43 | + | |
| 44 | + for (int j = 0; j < daoConfig.properties.length; j++) { | |
| 45 | + String columnName = daoConfig.properties[j].columnName; | |
| 46 | + | |
| 47 | + if (getColumns(db, tableName).contains(columnName)) { | |
| 48 | + properties.add(columnName); | |
| 49 | + | |
| 50 | + String type = null; | |
| 51 | + | |
| 52 | + try { | |
| 53 | + type = getTypeByClass(daoConfig.properties[j].type); | |
| 54 | + } catch (Exception exception) { | |
| 55 | + exception.printStackTrace(); | |
| 56 | + } | |
| 57 | + | |
| 58 | + createTableStringBuilder.append(divider).append(columnName).append(" ").append(type); | |
| 59 | + | |
| 60 | + if (daoConfig.properties[j].primaryKey) { | |
| 61 | + createTableStringBuilder.append(" PRIMARY KEY"); | |
| 62 | + } | |
| 63 | + | |
| 64 | + divider = ","; | |
| 65 | + } | |
| 66 | + } | |
| 67 | + createTableStringBuilder.append(");"); | |
| 68 | + | |
| 69 | + db.execSQL(createTableStringBuilder.toString()); | |
| 70 | + | |
| 71 | + StringBuilder insertTableStringBuilder = new StringBuilder(); | |
| 72 | + | |
| 73 | + insertTableStringBuilder.append("INSERT INTO ").append(tempTableName).append(" ("); | |
| 74 | + insertTableStringBuilder.append(TextUtils.join(",", properties)); | |
| 75 | + insertTableStringBuilder.append(") SELECT "); | |
| 76 | + insertTableStringBuilder.append(TextUtils.join(",", properties)); | |
| 77 | + insertTableStringBuilder.append(" FROM ").append(tableName).append(";"); | |
| 78 | + | |
| 79 | + db.execSQL(insertTableStringBuilder.toString()); | |
| 80 | + } catch (Exception e) { | |
| 81 | + e.printStackTrace(); | |
| 82 | + } finally { | |
| 83 | + continue; | |
| 84 | + } | |
| 85 | + } | |
| 86 | + } | |
| 87 | + | |
| 88 | + private void restoreData(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { | |
| 89 | + for (int i = 0; i < daoClasses.length; i++) { | |
| 90 | + try { | |
| 91 | + DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]); | |
| 92 | + String tableName = daoConfig.tablename; | |
| 93 | + String tempTableName = daoConfig.tablename.concat("_TEMP"); | |
| 94 | + ArrayList<String> properties = new ArrayList(); | |
| 95 | + | |
| 96 | + for (int j = 0; j < daoConfig.properties.length; j++) { | |
| 97 | + String columnName = daoConfig.properties[j].columnName; | |
| 98 | + | |
| 99 | + if (getColumns(db, tempTableName).contains(columnName)) { | |
| 100 | + properties.add(columnName); | |
| 101 | + } | |
| 102 | + } | |
| 103 | + | |
| 104 | + StringBuilder insertTableStringBuilder = new StringBuilder(); | |
| 105 | + | |
| 106 | + insertTableStringBuilder.append("INSERT INTO ").append(tableName).append(" ("); | |
| 107 | + insertTableStringBuilder.append(TextUtils.join(",", properties)); | |
| 108 | + insertTableStringBuilder.append(") SELECT "); | |
| 109 | + insertTableStringBuilder.append(TextUtils.join(",", properties)); | |
| 110 | + insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";"); | |
| 111 | + | |
| 112 | + StringBuilder dropTableStringBuilder = new StringBuilder(); | |
| 113 | + | |
| 114 | + dropTableStringBuilder.append("DROP TABLE ").append(tempTableName); | |
| 115 | + | |
| 116 | + db.execSQL(insertTableStringBuilder.toString()); | |
| 117 | + db.execSQL(dropTableStringBuilder.toString()); | |
| 118 | + } catch (Exception e) { | |
| 119 | + e.printStackTrace(); | |
| 120 | + } finally { | |
| 121 | + continue; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + private String getTypeByClass(Class<?> type) throws Exception { | |
| 127 | + if (type.equals(String.class)) { | |
| 128 | + return " TEXT"; | |
| 129 | + } | |
| 130 | + if (type.equals(Integer.class) || type.equals(int.class) || | |
| 131 | + type.equals(byte.class) || type.equals(Byte.class) || | |
| 132 | + type.equals(short.class) || type.equals(Short.class)) { | |
| 133 | + return " INTEGER DEFAULT 0"; | |
| 134 | + } | |
| 135 | + | |
| 136 | + if (type.equals(Double.class) || type.equals(double.class)) { | |
| 137 | + return "DOUBLE DEFAULT 0"; | |
| 138 | + } | |
| 139 | + | |
| 140 | + if(type.equals(float.class) || type.equals(Float.class)){ | |
| 141 | + return "FLOAT DEFAULT 0"; | |
| 142 | + } | |
| 143 | + | |
| 144 | + if (type.equals(Long.class) || type.equals(long.class)) { | |
| 145 | + return " Long DEFAULT 0"; | |
| 146 | + } | |
| 147 | + if (type.equals(Boolean.class) || type.equals(boolean.class)) { | |
| 148 | + return " NUMERIC DEFAULT 0"; | |
| 149 | + } | |
| 150 | + | |
| 151 | + Exception exception = new Exception(CONVERSION_CLASS_NOT_FOUND_EXCEPTION.concat(" - Class: ").concat(type.toString())); | |
| 152 | + exception.printStackTrace(); | |
| 153 | + throw exception; | |
| 154 | + } | |
| 155 | + | |
| 156 | + private static List<String> getColumns(Database db, String tableName) { | |
| 157 | + | |
| 158 | + List<String> columns = new ArrayList<>(); | |
| 159 | + Cursor cursor = null; | |
| 160 | + try { | |
| 161 | + cursor = db.rawQuery("SELECT * FROM " + tableName + " limit 1", null); | |
| 162 | + if (cursor != null) { | |
| 163 | + columns = new ArrayList<>(Arrays.asList(cursor.getColumnNames())); | |
| 164 | + } | |
| 165 | + } catch (Exception e) { | |
| 166 | + e.printStackTrace(); | |
| 167 | + } finally { | |
| 168 | + if (cursor != null) | |
| 169 | + cursor.close(); | |
| 170 | + } | |
| 171 | + return columns; | |
| 172 | + } | |
| 173 | +} | ... | ... |
core/database/src/main/res/values/strings.xml
0 → 100644
core/database/src/test/java/com/cnlive/database/ExampleUnitTest.java
0 → 100644
| 1 | +package com.cnlive.database; | |
| 2 | + | |
| 3 | +import org.junit.Test; | |
| 4 | + | |
| 5 | +import static org.junit.Assert.*; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Example local unit test, which will execute on the development machine (host). | |
| 9 | + * | |
| 10 | + * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | |
| 11 | + */ | |
| 12 | +public class ExampleUnitTest { | |
| 13 | + @Test | |
| 14 | + public void addition_isCorrect() { | |
| 15 | + assertEquals(4, 2 + 2); | |
| 16 | + } | |
| 17 | +} | |
| 0 | 18 | \ No newline at end of file | ... | ... |