Skip to content

Commit

Permalink
v2.0 add delete and create table listener
Browse files Browse the repository at this point in the history
  • Loading branch information
yuweiguocn committed Aug 7, 2017
1 parent 611f4be commit bccc897
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 16 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The original code is from [stackoverflow](http://stackoverflow.com/a/30334668/71
```
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.0'
}
```
or (greendao 3.0 below)
Expand All @@ -38,7 +38,16 @@ or (greendao 3.0 below)
3.Add a new class that extends **DaoMaster.OpenHelper**,add a constructor,implement **onUpgrade** method and called **migrate** method,arguments is all of generated Dao class:

```
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
},TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
```


Expand All @@ -50,7 +59,16 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
},TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
}
}
Expand Down
28 changes: 25 additions & 3 deletions README_CH.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GreenDaoUpgradeHelper是一个greenDao的数据库升级帮助类。使用它可
```
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.4.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.0'
}
```
如果你使用的greendao是3.0以前的版本,请使用下面的依赖:
Expand All @@ -37,7 +37,18 @@ GreenDaoUpgradeHelper是一个greenDao的数据库升级帮助类。使用它可

3.添加一个新类继承**DaoMaster.OpenHelper**,添加构造函数并实现**onUpgrade**方法,在onUpgrade方法添加如下代码即可,参数为所有的Dao类:
```
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
},TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
```
完整代码:
```
Expand All @@ -47,7 +58,18 @@ public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
},TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFac

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db,TestDataDao.class,TestData2Dao.class,TestData3Dao.class);
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
},TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.novoda:bintray-release:0.4.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 130
versionName "1.3"
versionCode 200
versionName "2.0"
}
buildTypes {
release {
Expand All @@ -32,7 +32,7 @@ publish {
userOrg = 'yuweiguocn'//用户名
groupId = 'io.github.yuweiguocn'//包名
artifactId = 'GreenDaoUpgradeHelper'//类库名
publishVersion = '1.4.0'//版本号
publishVersion = '2.0.0'//版本号
desc = 'To solve database upgrade of greenDao and just need one line code.'//描述
website = 'https://github.com/yuweiguocn/GreenDaoUpgradeHelper'//对应的github网址
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.greenrobot.greendao.database.StandardDatabase;
import org.greenrobot.greendao.internal.DaoConfig;

import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand All @@ -30,20 +31,44 @@ public final class MigrationHelper {
private static final String SQLITE_MASTER = "sqlite_master";
private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master";

private static WeakReference<ReCreateAllTableListener> weakListener;

public interface ReCreateAllTableListener{
void onCreateAllTables(Database db, boolean ifNotExists);
void onDropAllTables(Database db, boolean ifExists);
}

public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
printLog("【The Old Database Version】" + db.getVersion());
Database database = new StandardDatabase(db);
migrate(database, daoClasses);
}

public static void migrate(SQLiteDatabase db, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) {
weakListener = new WeakReference<>(listener);
migrate(db, daoClasses);
}

public static void migrate(Database database, ReCreateAllTableListener listener, Class<? extends AbstractDao<?, ?>>... daoClasses) {
weakListener = new WeakReference<>(listener);
migrate(database, daoClasses);
}

public static void migrate(Database database, Class<? extends AbstractDao<?, ?>>... daoClasses) {
printLog("【Generate temp table】start");
generateTempTables(database, daoClasses);
printLog("【Generate temp table】complete");

dropAllTables(database, true, daoClasses);
createAllTables(database, false, daoClasses);

ReCreateAllTableListener listener = weakListener.get();
if (listener != null) {
listener.onDropAllTables(database, true);
printLog("【Drop all table by listener】");
listener.onCreateAllTables(database, false);
printLog("【Create all table by listener】");
} else {
dropAllTables(database, true, daoClasses);
createAllTables(database, false, daoClasses);
}
printLog("【Restore data】start");
restoreData(database, daoClasses);
printLog("【Restore data】complete");
Expand Down Expand Up @@ -119,12 +144,12 @@ private static String getColumnsStr(DaoConfig daoConfig) {

private static void dropAllTables(Database db, boolean ifExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {
reflectMethod(db, "dropTable", ifExists, daoClasses);
printLog("【Drop all table】");
printLog("【Drop all table by reflect】");
}

private static void createAllTables(Database db, boolean ifNotExists, @NonNull Class<? extends AbstractDao<?, ?>>... daoClasses) {
reflectMethod(db, "createTable", ifNotExists, daoClasses);
printLog("【Create all table】");
printLog("【Create all table by reflect】");
}

/**
Expand Down

0 comments on commit bccc897

Please sign in to comment.