Skip to content

Commit 7c81dc4

Browse files
committed
Add "unique" and "default" fields in the format
"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.
1 parent 38c36de commit 7c81dc4

6 files changed

Lines changed: 101 additions & 23 deletions

File tree

generator/example/format.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
have a where clause used on them. Default is false.
4444
"version":1, // Optional. Version of the database where this field is added to
4545
the table. Default is the table creation version.
46-
"skip_bulk_insert":false // Optional. Whether to skip this field in the bulk
46+
"skip_bulk_insert":false, // Optional. Whether to skip this field in the bulk
4747
insert methods. Default is false.
48+
"default":"75", // Optional. Default value for the field.
49+
"unique":true // Optional. Flag the column as unique (not possible to have
50+
duplicate data in this column). Multiple columns can be flagged as
51+
unique.
4852
},
4953
{...}
5054
]

generator/res/content_subclass.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/**
3-
* Created in version %15$d
3+
* Created in version %16$d
44
*/
55
public static final class %1$s extends %2$sContent {
66

@@ -51,28 +51,28 @@
5151
if (%2$sProvider.ACTIVATE_ALL_LOGS) {
5252
Log.d(LOG_TAG, "%1$s | createTable start");
5353
}
54-
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + %8$s%9$s + ");");
54+
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + %8$s%9$s%10$s + ");");
5555

56-
%10$s if (%2$sProvider.ACTIVATE_ALL_LOGS) {
56+
%11$s if (%2$sProvider.ACTIVATE_ALL_LOGS) {
5757
Log.d(LOG_TAG, "%1$s | createTable end");
5858
}
5959
}
6060

61-
// Version %15$d : Creation of the table
62-
%16$s public static void upgradeTable(SQLiteDatabase db, int oldVersion, int newVersion) {
61+
// Version %16$d : Creation of the table
62+
%17$s public static void upgradeTable(SQLiteDatabase db, int oldVersion, int newVersion) {
6363
if (%2$sProvider.ACTIVATE_ALL_LOGS) {
6464
Log.d(LOG_TAG, "%1$s | upgradeTable start");
6565
}
6666

67-
if (oldVersion < %15$d) {
67+
if (oldVersion < %16$d) {
6868
Log.i(LOG_TAG, "Upgrading from version " + oldVersion + " to " + newVersion
6969
+ ", data will be lost!");
7070

7171
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
7272
createTable(db);
7373
return;
7474
}
75-
%17$s
75+
%18$s
7676

7777
if (oldVersion != newVersion) {
7878
throw new IllegalStateException("Error upgrading the database to version "
@@ -85,10 +85,10 @@
8585
}
8686

8787
static String getBulkInsertString() {
88-
return new StringBuilder("INSERT INTO ").append(TABLE_NAME).append(" ( ")%11$s.append(" ) VALUES (%12$s)").toString();
88+
return new StringBuilder("INSERT INTO ").append(TABLE_NAME).append(" ( ")%12$s.append(" ) VALUES (%13$s)").toString();
8989
}
9090

9191
static void bindValuesInBulkInsert(SQLiteStatement stmt, ContentValues values) {
9292
int i = 1;
93-
%13$s%14$s }
93+
%14$s%15$s }
9494
}

generator/res/content_subclass_upgrade.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
if (oldVersion <= %1$d) {
3-
db.execSQL("CREATE TABLE " + TABLE_NAME + "_tmp (" + %2$s%3$s + ");");
3+
db.execSQL("CREATE TABLE " + TABLE_NAME + "_tmp (" + %2$s%3$s%4$s + ");");
44

5-
db.execSQL("INSERT INTO " + TABLE_NAME + "_tmp SELECT " + %4$s + ", %5$s FROM " + TABLE_NAME + ";");
5+
db.execSQL("INSERT INTO " + TABLE_NAME + "_tmp SELECT " + %5$s + ", %6$s FROM " + TABLE_NAME + ";");
66

77
db.execSQL("DROP TABLE " + TABLE_NAME + ";");
88
db.execSQL("ALTER TABLE " + TABLE_NAME + "_tmp RENAME TO " + TABLE_NAME + ";");

generator/src/com/foxykeep/cpcodegenerator/generator/DatabaseGenerator.java

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class DatabaseGenerator {
2323

2424
private static final String BULK_STRING_VALUE = " String value;\n";
2525
private static final String PRIMARY_KEY_FORMAT = " + \", PRIMARY KEY (\" + %1$s + \")\"";
26+
private static final String UNIQUE_FORMAT = " + \", UNIQUE (\" + %1$s + \")\"";
2627

2728
private static final String URI_TYPE_FORMAT =
2829
" %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
100101
final StringBuilder sbProjection = new StringBuilder();
101102
final StringBuilder sbCreateTable = new StringBuilder();
102103
final StringBuilder sbCreateTablePrimaryKey = new StringBuilder();
104+
final StringBuilder sbCreateTableUnique = new StringBuilder();
103105
final StringBuilder sbUpgradeTableComment = new StringBuilder();
104106
final StringBuilder sbUpgradeTableCommentNewFields = new StringBuilder();
105107
final StringBuilder sbUpgradeTable = new StringBuilder();
106108
final StringBuilder sbUpgradeTableCreateTmpTable = new StringBuilder();
107109
final StringBuilder sbUpgradeTableCreateTmpTablePrimaryKey = new StringBuilder();
110+
final StringBuilder sbUpgradeTableCreateTmpTableUnique = new StringBuilder();
108111
final StringBuilder sbUpgradeTableInsertFields = new StringBuilder();
109112
final StringBuilder sbUpgradeTableInsertDefaultValues = new StringBuilder();
110113
final StringBuilder sbIndexes = new StringBuilder();
@@ -113,6 +116,7 @@ private static void generateContentClass(final String fileName, final String cla
113116
final StringBuilder sbBulkValues = new StringBuilder();
114117

115118
boolean hasPreviousPrimaryKey, hasAutoIncrementPrimaryKey, hasPreviousInsertFields;
119+
boolean hasPreviousUnique;
116120
boolean hasPreviousInsertDefaultValues, hasTextField;
117121
boolean hasPreviousUpgradeElements;
118122
int maxUpgradeVersion, minUpgradeWithoutChanges;
@@ -129,11 +133,12 @@ private static void generateContentClass(final String fileName, final String cla
129133
sbBulkParams.setLength(0);
130134
sbBulkValues.setLength(0);
131135
hasPreviousPrimaryKey = false;
136+
hasPreviousUnique = false;
132137
hasAutoIncrementPrimaryKey = false;
133138
hasTextField = false;
134139

135140
for (int i = 0, n = tableData.fieldList.size(); i < n; i++) {
136-
final FieldData fieldData = tableData.fieldList.get(i);
141+
FieldData fieldData = tableData.fieldList.get(i);
137142

138143
final boolean isNotLast = i != n - 1;
139144

@@ -159,6 +164,10 @@ private static void generateContentClass(final String fileName, final String cla
159164
.append(fieldData.dbConstantName).append(".getName() + \" \" + ")
160165
.append("Columns.")
161166
.append(fieldData.dbConstantName).append(".getType()");
167+
if (fieldData.dbDefaultValue != null) {
168+
sbCreateTable.append(" + \" DEFAULT ").append(fieldData.dbDefaultValue)
169+
.append("\"");
170+
}
162171
if (fieldData.dbIsPrimaryKey) {
163172
if (fieldData.dbIsAutoincrement) {
164173
if (hasPreviousPrimaryKey) {
@@ -183,6 +192,15 @@ private static void generateContentClass(final String fileName, final String cla
183192
}
184193
}
185194

195+
if (fieldData.dbIsUnique) {
196+
if (hasPreviousUnique) {
197+
sbCreateTableUnique.append(" + \", \" + ");
198+
}
199+
hasPreviousUnique = true;
200+
sbCreateTableUnique.append("Columns.")
201+
.append(fieldData.dbConstantName).append(".getName()");
202+
}
203+
186204
if (fieldData.dbHasIndex) {
187205
sbIndexes.append(TAB3)
188206
.append("db.execSQL(\"CREATE INDEX ")
@@ -233,8 +251,8 @@ private static void generateContentClass(final String fileName, final String cla
233251
maxUpgradeVersion = tableData.version;
234252
minUpgradeWithoutChanges = -1;
235253
for (int curVers = tableData.version + 1; curVers <= dbVersion; curVers++) {
236-
final List<FieldData> upgradeFieldDataList = tableData.upgradeFieldMap
237-
.get(curVers);
254+
List<FieldData> upgradeFieldDataList =
255+
tableData.upgradeFieldMap.get(curVers);
238256
if (upgradeFieldDataList == null) {
239257
if (minUpgradeWithoutChanges == -1) {
240258
minUpgradeWithoutChanges = curVers;
@@ -282,12 +300,41 @@ private static void generateContentClass(final String fileName, final String cla
282300
.append(fieldData.dbConstantName).append(".getName() + \" \" + ")
283301
.append("Columns.").append(fieldData.dbConstantName)
284302
.append(".getType()");
303+
if (fieldData.dbDefaultValue != null) {
304+
sbUpgradeTableCreateTmpTable.append(" + \" DEFAULT ")
305+
.append(fieldData.dbDefaultValue).append("\"");
306+
}
285307
if (fieldData.dbIsPrimaryKey) {
286-
if (hasPreviousPrimaryKey) {
287-
sbUpgradeTableCreateTmpTablePrimaryKey.append(" + \", \" + ");
308+
if (fieldData.dbIsAutoincrement) {
309+
if (hasPreviousPrimaryKey) {
310+
throw new IllegalArgumentException("Not possible to have " +
311+
"multiple primary key fields if one of them is an " +
312+
"autoincrement field");
313+
} else {
314+
hasAutoIncrementPrimaryKey = true;
315+
sbUpgradeTableCreateTmpTable
316+
.append("+ \" PRIMARY KEY AUTOINCREMENT\"");
317+
}
318+
} else if (hasAutoIncrementPrimaryKey) {
319+
throw new IllegalArgumentException("Not possible to have multiple" +
320+
" primary key fields if one of them is an autoincrement " +
321+
"field");
322+
} else {
323+
if (hasPreviousPrimaryKey) {
324+
sbUpgradeTableCreateTmpTablePrimaryKey.append(" + \", \" + ");
325+
}
326+
hasPreviousPrimaryKey = true;
327+
sbUpgradeTableCreateTmpTablePrimaryKey.append("Columns.")
328+
.append(fieldData.dbConstantName).append(".getName()");
288329
}
289-
hasPreviousPrimaryKey = true;
290-
sbUpgradeTableCreateTmpTablePrimaryKey.append("Columns.")
330+
}
331+
332+
if (fieldData.dbIsUnique) {
333+
if (hasPreviousUnique) {
334+
sbUpgradeTableCreateTmpTableUnique.append(" + \", \" + ");
335+
}
336+
hasPreviousUnique = true;
337+
sbUpgradeTableCreateTmpTableUnique.append("Columns.")
291338
.append(fieldData.dbConstantName).append(".getName()");
292339
}
293340

@@ -315,11 +362,12 @@ private static void generateContentClass(final String fileName, final String cla
315362
}
316363

317364
sbUpgradeTable.append(String.format(
318-
contentSubClassUpgrade,
319-
curVers,
365+
contentSubClassUpgrade, curVers,
320366
sbUpgradeTableCreateTmpTable.toString(),
321367
hasPreviousPrimaryKey ? String.format(PRIMARY_KEY_FORMAT,
322368
sbUpgradeTableCreateTmpTablePrimaryKey.toString()) : "",
369+
hasPreviousUnique ? String.format(UNIQUE_FORMAT,
370+
sbUpgradeTableCreateTmpTableUnique.toString()) : "",
323371
sbUpgradeTableInsertFields.toString(),
324372
sbUpgradeTableInsertDefaultValues.toString()));
325373

@@ -366,7 +414,9 @@ private static void generateContentClass(final String fileName, final String cla
366414
sbProjection.toString(),
367415
sbCreateTable.toString(),
368416
hasPreviousPrimaryKey ? String.format(PRIMARY_KEY_FORMAT,
369-
sbCreateTablePrimaryKey.toString()) : "", sbIndexes.toString(),
417+
sbCreateTablePrimaryKey.toString()) : "",
418+
hasPreviousUnique ? String.format(UNIQUE_FORMAT,
419+
sbCreateTableUnique.toString()) : "", sbIndexes.toString(),
370420
sbBulkFields.toString(), sbBulkParams.toString(),
371421
hasTextField ? BULK_STRING_VALUE : "", sbBulkValues.toString(),
372422
tableData.version, sbUpgradeTableComment.toString(), sbUpgradeTable

generator/src/com/foxykeep/cpcodegenerator/model/FieldData.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.foxykeep.cpcodegenerator.model;
22

3+
import com.foxykeep.cpcodegenerator.util.JsonUtils;
34
import com.foxykeep.cpcodegenerator.util.NameUtils;
45

56
import org.json.JSONException;
@@ -20,6 +21,8 @@ public class FieldData {
2021
public boolean dbIsAutoincrement = false;
2122
public boolean dbHasIndex;
2223
public boolean dbSkipBulkInsert;
24+
public String dbDefaultValue;
25+
public boolean dbIsUnique;
2326

2427
public FieldData(final JSONObject json) throws JSONException {
2528
name = json.getString("name");
@@ -51,6 +54,9 @@ public FieldData(final JSONObject json) throws JSONException {
5154
dbHasIndex = !dbIsPrimaryKey && json.optBoolean("is_index", false);
5255

5356
dbSkipBulkInsert = json.optBoolean("skip_bulk_insert", false);
57+
58+
dbDefaultValue = JsonUtils.getStringFixFalseNull(json, "default");
59+
dbIsUnique = json.optBoolean("unique", false);
5460
}
5561

5662
private void setType(final String type) {
@@ -67,7 +73,6 @@ private void setType(final String type) {
6773
}
6874

6975
public static String getDefaultValue(final String type) {
70-
7176
if (type.equals("string") || type.equals("text") || type.equals("String")) {
7277
return "''";
7378
} else {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.foxykeep.cpcodegenerator.util;
2+
3+
import org.json.JSONObject;
4+
5+
public class JsonUtils {
6+
7+
private static final String JSON_NULL = "null";
8+
9+
private JsonUtils() {}
10+
11+
public static String getStringFixFalseNull(final JSONObject jsonObject, final String key) {
12+
if (jsonObject.has(key)) {
13+
final String value = jsonObject.optString(key);
14+
return JSON_NULL.equals(value) ? null : value;
15+
} else {
16+
return null;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)