Skip to content

Commit

Permalink
Add "unique" and "default" fields in the format
Browse files Browse the repository at this point in the history
"Unique" flags the column as unique (not possible to have duplicate data
in this column)

"Default" allows the developer to set a default value for this column
   It must be the same type as the type of the column.
  • Loading branch information
foxykeep committed May 12, 2013
1 parent 38c36de commit 7c81dc4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 23 deletions.
6 changes: 5 additions & 1 deletion generator/example/format.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.
},
{...}
]
Expand Down
18 changes: 9 additions & 9 deletions generator/res/content_subclass.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/**
* Created in version %15$d
* Created in version %16$d
*/
public static final class %1$s extends %2$sContent {

Expand Down Expand Up @@ -51,28 +51,28 @@
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!");

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
createTable(db);
return;
}
%17$s
%18$s

if (oldVersion != newVersion) {
throw new IllegalStateException("Error upgrading the database to version "
Expand All @@ -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 }
}
4 changes: 2 additions & 2 deletions generator/res/content_subclass_upgrade.txt
Original file line number Diff line number Diff line change
@@ -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 + ";");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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 ")
Expand Down Expand Up @@ -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<FieldData> upgradeFieldDataList = tableData.upgradeFieldMap
.get(curVers);
List<FieldData> upgradeFieldDataList =
tableData.upgradeFieldMap.get(curVers);
if (upgradeFieldDataList == null) {
if (minUpgradeWithoutChanges == -1) {
minUpgradeWithoutChanges = curVers;
Expand Down Expand Up @@ -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()");
}

Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions generator/src/com/foxykeep/cpcodegenerator/util/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 7c81dc4

Please sign in to comment.