diff --git a/generator/example/format.json b/generator/example/format.json index c09ef9f..4a0a315 100644 --- a/generator/example/format.json +++ b/generator/example/format.json @@ -43,8 +43,12 @@ have a where clause used on them. Default is false. "version":1, // Optional. Version of the database where this field is added to the table. Default is the table creation version. - "skip_bulk_insert":false // Optional. Whether to skip this field in the bulk + "skip_bulk_insert":false, // Optional. Whether to skip this field in the bulk insert methods. Default is false. + "default":"75", // Optional. Default value for the field. + "unique":true // Optional. Flag the column as unique (not possible to have + duplicate data in this column). Multiple columns can be flagged as + unique. }, {...} ] diff --git a/generator/res/content_subclass.txt b/generator/res/content_subclass.txt index 6b10b07..2527d4e 100644 --- a/generator/res/content_subclass.txt +++ b/generator/res/content_subclass.txt @@ -1,6 +1,6 @@ /** - * Created in version %15$d + * Created in version %16$d */ public static final class %1$s extends %2$sContent { @@ -51,20 +51,20 @@ if (%2$sProvider.ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "%1$s | createTable start"); } - db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + %8$s%9$s + ");"); + db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + %8$s%9$s%10$s + ");"); -%10$s if (%2$sProvider.ACTIVATE_ALL_LOGS) { +%11$s if (%2$sProvider.ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "%1$s | createTable end"); } } - // Version %15$d : Creation of the table -%16$s public static void upgradeTable(SQLiteDatabase db, int oldVersion, int newVersion) { + // Version %16$d : Creation of the table +%17$s public static void upgradeTable(SQLiteDatabase db, int oldVersion, int newVersion) { if (%2$sProvider.ACTIVATE_ALL_LOGS) { Log.d(LOG_TAG, "%1$s | upgradeTable start"); } - if (oldVersion < %15$d) { + if (oldVersion < %16$d) { Log.i(LOG_TAG, "Upgrading from version " + oldVersion + " to " + newVersion + ", data will be lost!"); @@ -72,7 +72,7 @@ createTable(db); return; } -%17$s +%18$s if (oldVersion != newVersion) { throw new IllegalStateException("Error upgrading the database to version " @@ -85,10 +85,10 @@ } static String getBulkInsertString() { - return new StringBuilder("INSERT INTO ").append(TABLE_NAME).append(" ( ")%11$s.append(" ) VALUES (%12$s)").toString(); + return new StringBuilder("INSERT INTO ").append(TABLE_NAME).append(" ( ")%12$s.append(" ) VALUES (%13$s)").toString(); } static void bindValuesInBulkInsert(SQLiteStatement stmt, ContentValues values) { int i = 1; -%13$s%14$s } +%14$s%15$s } } diff --git a/generator/res/content_subclass_upgrade.txt b/generator/res/content_subclass_upgrade.txt index 90816d8..428815a 100644 --- a/generator/res/content_subclass_upgrade.txt +++ b/generator/res/content_subclass_upgrade.txt @@ -1,8 +1,8 @@ if (oldVersion <= %1$d) { - db.execSQL("CREATE TABLE " + TABLE_NAME + "_tmp (" + %2$s%3$s + ");"); + db.execSQL("CREATE TABLE " + TABLE_NAME + "_tmp (" + %2$s%3$s%4$s + ");"); - db.execSQL("INSERT INTO " + TABLE_NAME + "_tmp SELECT " + %4$s + ", %5$s FROM " + TABLE_NAME + ";"); + db.execSQL("INSERT INTO " + TABLE_NAME + "_tmp SELECT " + %5$s + ", %6$s FROM " + TABLE_NAME + ";"); db.execSQL("DROP TABLE " + TABLE_NAME + ";"); db.execSQL("ALTER TABLE " + TABLE_NAME + "_tmp RENAME TO " + TABLE_NAME + ";"); diff --git a/generator/src/com/foxykeep/cpcodegenerator/generator/DatabaseGenerator.java b/generator/src/com/foxykeep/cpcodegenerator/generator/DatabaseGenerator.java index 2096096..d6663eb 100644 --- a/generator/src/com/foxykeep/cpcodegenerator/generator/DatabaseGenerator.java +++ b/generator/src/com/foxykeep/cpcodegenerator/generator/DatabaseGenerator.java @@ -23,6 +23,7 @@ public class DatabaseGenerator { private static final String BULK_STRING_VALUE = " String value;\n"; private static final String PRIMARY_KEY_FORMAT = " + \", PRIMARY KEY (\" + %1$s + \")\""; + private static final String UNIQUE_FORMAT = " + \", UNIQUE (\" + %1$s + \")\""; private static final String URI_TYPE_FORMAT = " %1$s%2$s(%3$s.TABLE_NAME%4$s, %3$s.TABLE_NAME, %3$s.%5$s)%6$s\n"; @@ -100,11 +101,13 @@ private static void generateContentClass(final String fileName, final String cla final StringBuilder sbProjection = new StringBuilder(); final StringBuilder sbCreateTable = new StringBuilder(); final StringBuilder sbCreateTablePrimaryKey = new StringBuilder(); + final StringBuilder sbCreateTableUnique = new StringBuilder(); final StringBuilder sbUpgradeTableComment = new StringBuilder(); final StringBuilder sbUpgradeTableCommentNewFields = new StringBuilder(); final StringBuilder sbUpgradeTable = new StringBuilder(); final StringBuilder sbUpgradeTableCreateTmpTable = new StringBuilder(); final StringBuilder sbUpgradeTableCreateTmpTablePrimaryKey = new StringBuilder(); + final StringBuilder sbUpgradeTableCreateTmpTableUnique = new StringBuilder(); final StringBuilder sbUpgradeTableInsertFields = new StringBuilder(); final StringBuilder sbUpgradeTableInsertDefaultValues = new StringBuilder(); final StringBuilder sbIndexes = new StringBuilder(); @@ -113,6 +116,7 @@ private static void generateContentClass(final String fileName, final String cla final StringBuilder sbBulkValues = new StringBuilder(); boolean hasPreviousPrimaryKey, hasAutoIncrementPrimaryKey, hasPreviousInsertFields; + boolean hasPreviousUnique; boolean hasPreviousInsertDefaultValues, hasTextField; boolean hasPreviousUpgradeElements; int maxUpgradeVersion, minUpgradeWithoutChanges; @@ -129,11 +133,12 @@ private static void generateContentClass(final String fileName, final String cla sbBulkParams.setLength(0); sbBulkValues.setLength(0); hasPreviousPrimaryKey = false; + hasPreviousUnique = false; hasAutoIncrementPrimaryKey = false; hasTextField = false; for (int i = 0, n = tableData.fieldList.size(); i < n; i++) { - final FieldData fieldData = tableData.fieldList.get(i); + FieldData fieldData = tableData.fieldList.get(i); final boolean isNotLast = i != n - 1; @@ -159,6 +164,10 @@ private static void generateContentClass(final String fileName, final String cla .append(fieldData.dbConstantName).append(".getName() + \" \" + ") .append("Columns.") .append(fieldData.dbConstantName).append(".getType()"); + if (fieldData.dbDefaultValue != null) { + sbCreateTable.append(" + \" DEFAULT ").append(fieldData.dbDefaultValue) + .append("\""); + } if (fieldData.dbIsPrimaryKey) { if (fieldData.dbIsAutoincrement) { if (hasPreviousPrimaryKey) { @@ -183,6 +192,15 @@ private static void generateContentClass(final String fileName, final String cla } } + if (fieldData.dbIsUnique) { + if (hasPreviousUnique) { + sbCreateTableUnique.append(" + \", \" + "); + } + hasPreviousUnique = true; + sbCreateTableUnique.append("Columns.") + .append(fieldData.dbConstantName).append(".getName()"); + } + if (fieldData.dbHasIndex) { sbIndexes.append(TAB3) .append("db.execSQL(\"CREATE INDEX ") @@ -233,8 +251,8 @@ private static void generateContentClass(final String fileName, final String cla maxUpgradeVersion = tableData.version; minUpgradeWithoutChanges = -1; for (int curVers = tableData.version + 1; curVers <= dbVersion; curVers++) { - final List upgradeFieldDataList = tableData.upgradeFieldMap - .get(curVers); + List upgradeFieldDataList = + tableData.upgradeFieldMap.get(curVers); if (upgradeFieldDataList == null) { if (minUpgradeWithoutChanges == -1) { minUpgradeWithoutChanges = curVers; @@ -282,12 +300,41 @@ private static void generateContentClass(final String fileName, final String cla .append(fieldData.dbConstantName).append(".getName() + \" \" + ") .append("Columns.").append(fieldData.dbConstantName) .append(".getType()"); + if (fieldData.dbDefaultValue != null) { + sbUpgradeTableCreateTmpTable.append(" + \" DEFAULT ") + .append(fieldData.dbDefaultValue).append("\""); + } if (fieldData.dbIsPrimaryKey) { - if (hasPreviousPrimaryKey) { - sbUpgradeTableCreateTmpTablePrimaryKey.append(" + \", \" + "); + if (fieldData.dbIsAutoincrement) { + if (hasPreviousPrimaryKey) { + throw new IllegalArgumentException("Not possible to have " + + "multiple primary key fields if one of them is an " + + "autoincrement field"); + } else { + hasAutoIncrementPrimaryKey = true; + sbUpgradeTableCreateTmpTable + .append("+ \" PRIMARY KEY AUTOINCREMENT\""); + } + } else if (hasAutoIncrementPrimaryKey) { + throw new IllegalArgumentException("Not possible to have multiple" + + " primary key fields if one of them is an autoincrement " + + "field"); + } else { + if (hasPreviousPrimaryKey) { + sbUpgradeTableCreateTmpTablePrimaryKey.append(" + \", \" + "); + } + hasPreviousPrimaryKey = true; + sbUpgradeTableCreateTmpTablePrimaryKey.append("Columns.") + .append(fieldData.dbConstantName).append(".getName()"); } - hasPreviousPrimaryKey = true; - sbUpgradeTableCreateTmpTablePrimaryKey.append("Columns.") + } + + if (fieldData.dbIsUnique) { + if (hasPreviousUnique) { + sbUpgradeTableCreateTmpTableUnique.append(" + \", \" + "); + } + hasPreviousUnique = true; + sbUpgradeTableCreateTmpTableUnique.append("Columns.") .append(fieldData.dbConstantName).append(".getName()"); } @@ -315,11 +362,12 @@ private static void generateContentClass(final String fileName, final String cla } sbUpgradeTable.append(String.format( - contentSubClassUpgrade, - curVers, + contentSubClassUpgrade, curVers, sbUpgradeTableCreateTmpTable.toString(), hasPreviousPrimaryKey ? String.format(PRIMARY_KEY_FORMAT, sbUpgradeTableCreateTmpTablePrimaryKey.toString()) : "", + hasPreviousUnique ? String.format(UNIQUE_FORMAT, + sbUpgradeTableCreateTmpTableUnique.toString()) : "", sbUpgradeTableInsertFields.toString(), sbUpgradeTableInsertDefaultValues.toString())); @@ -366,7 +414,9 @@ private static void generateContentClass(final String fileName, final String cla sbProjection.toString(), sbCreateTable.toString(), hasPreviousPrimaryKey ? String.format(PRIMARY_KEY_FORMAT, - sbCreateTablePrimaryKey.toString()) : "", sbIndexes.toString(), + sbCreateTablePrimaryKey.toString()) : "", + hasPreviousUnique ? String.format(UNIQUE_FORMAT, + sbCreateTableUnique.toString()) : "", sbIndexes.toString(), sbBulkFields.toString(), sbBulkParams.toString(), hasTextField ? BULK_STRING_VALUE : "", sbBulkValues.toString(), tableData.version, sbUpgradeTableComment.toString(), sbUpgradeTable diff --git a/generator/src/com/foxykeep/cpcodegenerator/model/FieldData.java b/generator/src/com/foxykeep/cpcodegenerator/model/FieldData.java index 5e19d5b..141253a 100644 --- a/generator/src/com/foxykeep/cpcodegenerator/model/FieldData.java +++ b/generator/src/com/foxykeep/cpcodegenerator/model/FieldData.java @@ -1,5 +1,6 @@ package com.foxykeep.cpcodegenerator.model; +import com.foxykeep.cpcodegenerator.util.JsonUtils; import com.foxykeep.cpcodegenerator.util.NameUtils; import org.json.JSONException; @@ -20,6 +21,8 @@ public class FieldData { public boolean dbIsAutoincrement = false; public boolean dbHasIndex; public boolean dbSkipBulkInsert; + public String dbDefaultValue; + public boolean dbIsUnique; public FieldData(final JSONObject json) throws JSONException { name = json.getString("name"); @@ -51,6 +54,9 @@ public FieldData(final JSONObject json) throws JSONException { dbHasIndex = !dbIsPrimaryKey && json.optBoolean("is_index", false); dbSkipBulkInsert = json.optBoolean("skip_bulk_insert", false); + + dbDefaultValue = JsonUtils.getStringFixFalseNull(json, "default"); + dbIsUnique = json.optBoolean("unique", false); } private void setType(final String type) { @@ -67,7 +73,6 @@ private void setType(final String type) { } public static String getDefaultValue(final String type) { - if (type.equals("string") || type.equals("text") || type.equals("String")) { return "''"; } else { diff --git a/generator/src/com/foxykeep/cpcodegenerator/util/JsonUtils.java b/generator/src/com/foxykeep/cpcodegenerator/util/JsonUtils.java new file mode 100644 index 0000000..0a378b4 --- /dev/null +++ b/generator/src/com/foxykeep/cpcodegenerator/util/JsonUtils.java @@ -0,0 +1,19 @@ +package com.foxykeep.cpcodegenerator.util; + +import org.json.JSONObject; + +public class JsonUtils { + + private static final String JSON_NULL = "null"; + + private JsonUtils() {} + + public static String getStringFixFalseNull(final JSONObject jsonObject, final String key) { + if (jsonObject.has(key)) { + final String value = jsonObject.optString(key); + return JSON_NULL.equals(value) ? null : value; + } else { + return null; + } + } +}