Skip to content

Commit

Permalink
version 1.3.0,fix issue #12
Browse files Browse the repository at this point in the history
  • Loading branch information
yuweiguocn committed Dec 2, 2016
1 parent 1010159 commit 4be21bd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GreenDaoUpgradeHelper is a database upgrade helper for greenDao.Use GreenDaoUpgr
```
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0'
}
```
or (greendao 3.0 below)
Expand Down
2 changes: 1 addition & 1 deletion README_CH.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ GreenDaoUpgradeHelper是一个greenDao的数据库升级帮助类。使用它可
```
dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.2.0'
compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.3.0'
}
```
如果你使用的greendao是3.0以前的版本,请使用下面的依赖:
Expand Down
35 changes: 2 additions & 33 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
versionCode 120
versionName "1.2"
versionCode 130
versionName "1.3"
}
buildTypes {
release {
Expand All @@ -22,34 +22,3 @@ dependencies {
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.android.support:support-annotations:24.2.1'
}


//dependsOn 可根据实际需要增加或更改
task buildJar(type: Jar) {

appendix = ""
baseName = "greendao-upgrade"
version = "0.0.5"
classifier = "release"

//后缀名
extension = "jar"
//最终的 Jar 包名,如果没设置,默认为 [baseName]-[appendix]-[version]-[classifier].[extension]
// archiveName = "AndroidJarDemo.jar"

//需打包的资源所在的路径集
def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"];
//初始化资源路径集
from srcClassDir

//去除路径集下部分的资源
exclude "com/github/yuweiguocn/library/greendao/BuildConfig.class"
exclude "com/github/yuweiguocn/library/greendao/BuildConfig\$*.class"
exclude "**/R.class"
exclude "**/R\$*.class"

//只导入资源路径集下的部分资源
include "com/github/yuweiguocn/library/greendao/**/*.class"

//注: exclude include 支持可变长参数
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,37 @@ public final class MigrationHelper {

public static boolean DEBUG = false;
private static String TAG = "MigrationHelper";
private static final String SQLITE_MASTER = "sqlite_master";
private static final String SQLITE_TEMP_MASTER = "sqlite_temp_master";

public static void migrate(SQLiteDatabase db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
Database database = new StandardDatabase(db);
if (DEBUG) {
Log.d(TAG, "【Database Version】" + db.getVersion());
Log.d(TAG, "【Generate temp table】start");
}

printLog("【The Old Database Version】" + db.getVersion());
printLog("【Generate temp table】start");
generateTempTables(database, daoClasses);
if (DEBUG) {
Log.d(TAG, "【Generate temp table】complete");
}
printLog("【Generate temp table】complete");

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

if (DEBUG) {
Log.d(TAG, "【Restore data】start");
}
printLog("【Restore data】start");
restoreData(database, daoClasses);
if (DEBUG) {
Log.d(TAG, "【Restore data】complete");
}
printLog("【Restore data】complete");
}

private static void generateTempTables(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
for (int i = 0; i < daoClasses.length; i++) {
String tempTableName = null;

DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String tableName = daoConfig.tablename;
if (!isTableExists(db, false, tableName)) {
printLog("【New Table】" + tableName);
continue;
}
try {
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String tableName = daoConfig.tablename;
tempTableName = daoConfig.tablename.concat("_TEMP");

StringBuilder dropTableStringBuilder = new StringBuilder();
dropTableStringBuilder.append("DROP TABLE IF EXISTS ").append(tempTableName).append(";");
db.execSQL(dropTableStringBuilder.toString());
Expand All @@ -67,16 +66,38 @@ private static void generateTempTables(Database db, Class<? extends AbstractDao<
insertTableStringBuilder.append("CREATE TEMPORARY TABLE ").append(tempTableName);
insertTableStringBuilder.append(" AS SELECT * FROM ").append(tableName).append(";");
db.execSQL(insertTableStringBuilder.toString());
if (DEBUG) {
Log.d(TAG, "【Table】" + tableName +"\n ---Columns-->"+getColumnsStr(daoConfig));
Log.d(TAG, "【Generate temp table】" + tempTableName);
}
printLog("【Table】" + tableName +"\n ---Columns-->"+getColumnsStr(daoConfig));
printLog("【Generate temp table】" + tempTableName);
} catch (SQLException e) {
Log.e(TAG, "【Failed to generate temp table】" + tempTableName, e);
}
}
}

private static boolean isTableExists(Database db, boolean isTemp, String tableName) {
if (db == null || TextUtils.isEmpty(tableName)) {
return false;
}
String dbName = isTemp ? SQLITE_TEMP_MASTER : SQLITE_MASTER;
String sql = "SELECT COUNT(*) FROM " + dbName + " WHERE type = ? AND name = ?";
Cursor cursor=null;
int count = 0;
try {
cursor = db.rawQuery(sql, new String[]{"table", tableName});
if (cursor == null || !cursor.moveToFirst()) {
return false;
}
count = cursor.getInt(0);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
return count > 0;
}


private static String getColumnsStr(DaoConfig daoConfig) {
if (daoConfig == null) {
return "no columns";
Expand All @@ -95,16 +116,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);
if (DEBUG) {
Log.d(TAG, "【Drop all table】");
}
printLog("【Drop all table】");
}

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

/**
Expand All @@ -130,12 +147,15 @@ private static void reflectMethod(Database db, String methodName, boolean isExis

private static void restoreData(Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) {
for (int i = 0; i < daoClasses.length; i++) {
String tempTableName = null;
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String tableName = daoConfig.tablename;
String tempTableName = daoConfig.tablename.concat("_TEMP");

if (!isTableExists(db, true, tempTableName)) {
continue;
}

try {
DaoConfig daoConfig = new DaoConfig(db, daoClasses[i]);
String tableName = daoConfig.tablename;
tempTableName = daoConfig.tablename.concat("_TEMP");
// get all columns from tempTable, take careful to use the columns list
List<String> columns = getColumns(db, tempTableName);
ArrayList<String> properties = new ArrayList<>(columns.size());
Expand All @@ -155,18 +175,14 @@ private static void restoreData(Database db, Class<? extends AbstractDao<?, ?>>.
insertTableStringBuilder.append(columnSQL);
insertTableStringBuilder.append(" FROM ").append(tempTableName).append(";");
db.execSQL(insertTableStringBuilder.toString());
if (DEBUG) {
Log.d(TAG, "【Restore data】 to " + tableName);
}
printLog("【Restore data】 to " + tableName);
}
StringBuilder dropTableStringBuilder = new StringBuilder();
dropTableStringBuilder.append("DROP TABLE ").append(tempTableName);
db.execSQL(dropTableStringBuilder.toString());
if (DEBUG) {
Log.d(TAG, "【Drop temp table】" + tempTableName);
}
printLog("【Drop temp table】" + tempTableName);
} catch (SQLException e) {
Log.e(TAG, "【Failed to restore data from temp table (probably new table)】" + tempTableName, e);
Log.e(TAG, "【Failed to restore data from temp table 】" + tempTableName, e);
}
}
}
Expand All @@ -190,4 +206,10 @@ private static List<String> getColumns(Database db, String tableName) {
return columns;
}

private static void printLog(String info){
if(DEBUG){
Log.d(TAG, info);
}
}

}

0 comments on commit 4be21bd

Please sign in to comment.