Created
March 1, 2017 07:04
-
-
Save umesh0492/9e8888cbfab3c29bdabefe9d9650e737 to your computer and use it in GitHub Desktop.
Green DAO Migrate Helper class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by umesh0492 on 15/02/16. | |
*/ | |
public class GreenDAOMigrateHelper { | |
private static final String CONVERSION_CLASS_NOT_FOUND_EXCEPTION = "MIGRATION HELPER - CLASS DOESN'T MATCH WITH THE CURRENT PARAMETERS"; | |
private static GreenDAOMigrateHelper instance; | |
public static GreenDAOMigrateHelper getInstance () { | |
if (instance == null) { | |
instance = new GreenDAOMigrateHelper (); | |
} | |
return instance; | |
} | |
public void migrate (Database db, Context context) { | |
DaoMaster.dropAllTables (db, true); | |
DaoMaster.createAllTables (db, false); | |
SharedPreferences prefs = | |
PreferenceManager.getDefaultSharedPreferences (context); | |
prefs.edit ().clear ().commit (); | |
} | |
public void migrateLatest (Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { | |
generateTempTables (db, daoClasses); | |
DaoMaster.dropAllTables (db, true); | |
DaoMaster.createAllTables (db, false); | |
restoreData (db, daoClasses); | |
} | |
//generate AND copy values | |
private void generateTempTables (Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { | |
for (int i = 0; i < daoClasses.length; i++) { | |
DaoConfig daoConfig = new DaoConfig (db, daoClasses[i]); | |
String divider = ""; | |
String tableName = daoConfig.tablename; | |
String tempTableName = daoConfig.tablename.concat ("_TEMP"); | |
ArrayList<String> properties = new ArrayList<> (); | |
StringBuilder createTableStringBuilder = new StringBuilder (); | |
createTableStringBuilder.append ("CREATE TABLE ").append (tempTableName).append (" ("); | |
for (int j = 0; j < daoConfig.properties.length; j++) { | |
String columnName = daoConfig.properties[j].columnName; | |
if (getColumns (db, tableName).contains (columnName)) { | |
properties.add (columnName); | |
String type = null; | |
try { | |
type = getTypeByClass (daoConfig.properties[j].type); | |
} catch (Exception exception) { | |
//Sentry.captureException (exception); | |
} | |
createTableStringBuilder.append (divider).append (columnName).append (" ").append (type); | |
if (daoConfig.properties[j].primaryKey) { | |
createTableStringBuilder.append (" PRIMARY KEY"); | |
} | |
divider = ","; | |
} | |
} | |
createTableStringBuilder.append (");"); | |
Log.e ("Table statement", createTableStringBuilder.toString ()); | |
db.execSQL (createTableStringBuilder.toString ()); | |
StringBuilder insertTableStringBuilder = new StringBuilder (); | |
insertTableStringBuilder.append ("INSERT INTO ").append (tempTableName).append (" ("); | |
insertTableStringBuilder.append (TextUtils.join (",", properties)); | |
insertTableStringBuilder.append (") SELECT "); | |
insertTableStringBuilder.append (TextUtils.join (",", properties)); | |
insertTableStringBuilder.append (" FROM ").append (tableName).append (";"); | |
db.execSQL (insertTableStringBuilder.toString ()); | |
} | |
} | |
private void restoreData (Database db, Class<? extends AbstractDao<?, ?>>... daoClasses) { | |
for (int i = 0; i < daoClasses.length; i++) { | |
DaoConfig daoConfig = new DaoConfig (db, daoClasses[i]); | |
String tableName = daoConfig.tablename; | |
String tempTableName = daoConfig.tablename.concat ("_TEMP"); | |
ArrayList<String> properties = new ArrayList (); | |
for (int j = 0; j < daoConfig.properties.length; j++) { | |
String columnName = daoConfig.properties[j].columnName; | |
if (getColumns (db, tempTableName).contains (columnName)) { | |
properties.add (columnName); | |
} | |
} | |
StringBuilder insertTableStringBuilder = new StringBuilder (); | |
insertTableStringBuilder.append ("INSERT INTO ").append (tableName).append (" ("); | |
insertTableStringBuilder.append (TextUtils.join (",", properties)); | |
insertTableStringBuilder.append (") SELECT "); | |
insertTableStringBuilder.append (TextUtils.join (",", properties)); | |
insertTableStringBuilder.append (" FROM ").append (tempTableName).append (";"); | |
StringBuilder dropTableStringBuilder = new StringBuilder (); | |
dropTableStringBuilder.append ("DROP TABLE ").append (tempTableName); | |
db.execSQL (insertTableStringBuilder.toString ()); | |
db.execSQL (dropTableStringBuilder.toString ()); | |
} | |
} | |
private static List<String> getColumns (Database db, String tableName) { | |
List<String> columns = new ArrayList<> (); | |
Cursor cursor = null; | |
try { | |
cursor = db.rawQuery ("SELECT * FROM " + tableName + " limit 1", null); | |
if (cursor != null) { | |
columns = new ArrayList<> (Arrays.asList (cursor.getColumnNames ())); | |
} | |
} catch (Exception e) { | |
Log.v (tableName, e.getMessage (), e); | |
//Sentry.captureException (e); | |
e.printStackTrace (); | |
} finally { | |
if (cursor != null) { | |
cursor.close (); | |
} | |
} | |
return columns; | |
} | |
private String getTypeByClass (Class<?> type) throws Exception { | |
if (type.equals (String.class)) { | |
return "TEXT"; | |
} | |
if (type.equals (Long.class) || type.equals (Integer.class) | |
|| type.equals (int.class) || type.equals (long.class)) { | |
return "INTEGER"; | |
} | |
if (type.equals (Boolean.class) || type.equals (boolean.class)) { | |
return "BOOLEAN"; | |
} | |
Exception exception = new Exception (CONVERSION_CLASS_NOT_FOUND_EXCEPTION.concat (" - Class: ").concat (type.toString ())); | |
//Sentry.captureException (exception); | |
throw exception; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment