Skip to content

Commit

Permalink
Merge pull request #408 from RoyMontoya/SchemaGenerator
Browse files Browse the repository at this point in the history
small changes to clean Schema generator
  • Loading branch information
sibelius committed Nov 4, 2015
2 parents ef0445f + bd1ba7b commit 8279d8b
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions library/src/main/java/com/orm/SchemaGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class SchemaGenerator {

private Context context;

public static final String NULL = " NULL";
public static final String NOT_NULL = " NOT NULL";
public static final String UNIQUE = " UNIQUE";
public static final String SUGAR = "Sugar";

public SchemaGenerator(Context context) {
this.context = context;
}
Expand All @@ -44,8 +49,8 @@ public void doUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVers
List<Class> domainClasses = getDomainClasses(context);
String sql = "select count(*) from sqlite_master where type='table' and name='%s';";
for (Class domain : domainClasses) {
Cursor c = sqLiteDatabase.rawQuery(String.format(sql, NamingHelper.toSQLName(domain)), null);
if (c.moveToFirst() && c.getInt(0) == 0) {
Cursor cursor = sqLiteDatabase.rawQuery(String.format(sql, NamingHelper.toSQLName(domain)), null);
if (cursor.moveToFirst() && cursor.getInt(0) == 0) {
createTable(domain, sqLiteDatabase);
}
}
Expand All @@ -66,7 +71,7 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe
List<String> files = Arrays.asList(this.context.getAssets().list("sugar_upgrades"));
Collections.sort(files, new NumberComparator());
for (String file : files) {
Log.i("Sugar", "filename : " + file);
Log.i(SUGAR, "filename : " + file);

try {
int version = Integer.valueOf(file.replace(".sql", ""));
Expand All @@ -76,12 +81,12 @@ private boolean executeSugarUpgrade(SQLiteDatabase db, int oldVersion, int newVe
isSuccess = true;
}
} catch (NumberFormatException e) {
Log.i("Sugar", "not a sugar script. ignored." + file);
Log.i(SUGAR, "not a sugar script. ignored." + file);
}

}
} catch (IOException e) {
Log.e("Sugar", e.getMessage());
Log.e(SUGAR, e.getMessage());
}

return isSuccess;
Expand All @@ -94,17 +99,17 @@ private void executeScript(SQLiteDatabase db, String file) {
String line;
while ((line = reader.readLine()) != null) {
Log.i("Sugar script", line);
db.execSQL(line.toString());
db.execSQL(line);
}
} catch (IOException e) {
Log.e("Sugar", e.getMessage());
Log.e(SUGAR, e.getMessage());
}

Log.i("Sugar", "Script executed");
Log.i(SUGAR, "Script executed");
}

private void createTable(Class<?> table, SQLiteDatabase sqLiteDatabase) {
Log.i("Sugar", "Create table if not exists");
Log.i(SUGAR, "Create table if not exists");
List<Field> fields = ReflectionUtil.getTableFields(table);
String tableName = NamingHelper.toSQLName(table);
StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
Expand All @@ -126,37 +131,37 @@ private void createTable(Class<?> table, SQLiteDatabase sqLiteDatabase) {
sb.append(", ").append(columnName).append(" ").append(columnType);

if (columnAnnotation.notNull()) {
if (columnType.endsWith(" NULL")) {
if (columnType.endsWith(NULL)) {
sb.delete(sb.length() - 5, sb.length());
}
sb.append(" NOT NULL");
sb.append(NOT_NULL);
}

if (columnAnnotation.unique()) {
sb.append(" UNIQUE");
sb.append(UNIQUE);
}

} else {
sb.append(", ").append(columnName).append(" ").append(columnType);

if (column.isAnnotationPresent(NotNull.class)) {
if (columnType.endsWith(" NULL")) {
if (columnType.endsWith(NULL)) {
sb.delete(sb.length() - 5, sb.length());
}
sb.append(" NOT NULL");
sb.append(NOT_NULL);
}

if (column.isAnnotationPresent(Unique.class)) {
sb.append(" UNIQUE");
sb.append(UNIQUE);
}
}
}
}

sb.append(" ) ");
Log.i("Sugar", "Creating table " + tableName);
Log.i(SUGAR, "Creating table " + tableName);

if (!"".equals(sb.toString())) {
if (!sb.toString().isEmpty()) {
try {
sqLiteDatabase.execSQL(sb.toString());
} catch (SQLException e) {
Expand Down

0 comments on commit 8279d8b

Please sign in to comment.