Skip to content

Commit 5dd154e

Browse files
committed
Fix the creation of a table containing a PRIMARY KEY AUTOINCREMENT field
Fixes foxykeep#11
1 parent 90f05f6 commit 5dd154e

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

generator/example/format.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
"is_id":true, // Optional. Whether the field is the identifier field of the
3535
class. The column named will be changed to BaseColumns._ID in the
3636
database. Only one field should have this value set to true by table.
37-
Default is false.
38-
"is_autoincrement":false // Optional. Can only be set on the field flagged with
37+
Default is false. Can only be set on a field flagged with
38+
"is_primary_key".
39+
"is_autoincrement":false // Optional. Can only be set on a field flagged with
3940
"is_id". Whether or not the field should be autoincremented.
4041
"is_index":true, // Optional. Whether the field needs to have an index added to
4142
it in the database. Should be set to true for text field which will

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static void generateContentClass(final String fileName, final String cla
112112
final StringBuilder sbBulkParams = new StringBuilder();
113113
final StringBuilder sbBulkValues = new StringBuilder();
114114

115-
boolean hasPreviousPrimaryKey, hasPreviousInsertFields;
115+
boolean hasPreviousPrimaryKey, hasAutoIncrementPrimaryKey, hasPreviousInsertFields;
116116
boolean hasPreviousInsertDefaultValues, hasTextField;
117117
boolean hasPreviousUpgradeElements;
118118
int maxUpgradeVersion, minUpgradeWithoutChanges;
@@ -129,6 +129,7 @@ private static void generateContentClass(final String fileName, final String cla
129129
sbBulkParams.setLength(0);
130130
sbBulkValues.setLength(0);
131131
hasPreviousPrimaryKey = false;
132+
hasAutoIncrementPrimaryKey = false;
132133
hasTextField = false;
133134

134135
for (int i = 0, n = tableData.fieldList.size(); i < n; i++) {
@@ -158,16 +159,28 @@ private static void generateContentClass(final String fileName, final String cla
158159
.append(fieldData.dbConstantName).append(".getName() + \" \" + ")
159160
.append("Columns.")
160161
.append(fieldData.dbConstantName).append(".getType()");
161-
if (fieldData.dbIsAutoincrement) {
162-
sbCreateTable.append("+ \" AUTOINCREMENT\"");
163-
}
164162
if (fieldData.dbIsPrimaryKey) {
165-
if (hasPreviousPrimaryKey) {
166-
sbCreateTablePrimaryKey.append(" + \", \" + ");
163+
if (fieldData.dbIsAutoincrement) {
164+
if (hasPreviousPrimaryKey) {
165+
throw new IllegalArgumentException("Not possible to have multiple" +
166+
" primary key fields if one of them is an autoincrement " +
167+
"field");
168+
} else {
169+
hasAutoIncrementPrimaryKey = true;
170+
sbCreateTable.append("+ \" PRIMARY KEY AUTOINCREMENT\"");
171+
}
172+
} else if (hasAutoIncrementPrimaryKey) {
173+
throw new IllegalArgumentException("Not possible to have multiple" +
174+
" primary key fields if one of them is an autoincrement " +
175+
"field");
176+
} else {
177+
if (hasPreviousPrimaryKey) {
178+
sbCreateTablePrimaryKey.append(" + \", \" + ");
179+
}
180+
hasPreviousPrimaryKey = true;
181+
sbCreateTablePrimaryKey.append("Columns.")
182+
.append(fieldData.dbConstantName).append(".getName()");
167183
}
168-
hasPreviousPrimaryKey = true;
169-
sbCreateTablePrimaryKey.append("Columns.")
170-
.append(fieldData.dbConstantName).append(".getName()");
171184
}
172185

173186
if (fieldData.dbHasIndex) {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@ public FieldData(final JSONObject json) throws JSONException {
3030
dbConstantName = NameUtils.createConstantName(name);
3131
dbIsPrimaryKey = json.optBoolean("is_primary_key", false);
3232
dbIsId = json.optBoolean("is_id", false);
33+
dbIsAutoincrement = json.optBoolean("is_autoincrement", false);
3334
if (dbIsId) {
35+
if (!dbIsPrimaryKey) {
36+
throw new IllegalArgumentException("Field \"" + name + "\" | is_id can only be "
37+
+ "used on a field flagged with is_primary_key");
38+
}
3439
dbName = "_id";
35-
dbIsAutoincrement = json.optBoolean("is_autoincrement", false);
3640
if (dbIsAutoincrement && !type.equals("integer")) {
37-
throw new IllegalArgumentException("is_autoincrement can only be used on an " +
38-
"integer type field");
41+
throw new IllegalArgumentException("Field \"" + name + "\" | is_autoincrement can "
42+
+ "only be used on an integer type field");
3943
}
4044
} else {
45+
if (dbIsAutoincrement) {
46+
throw new IllegalArgumentException("Field \"" + name + "\" | id_autoincrement can "
47+
+ "only be used on a field flagged with is_id");
48+
}
4149
dbName = name;
4250
}
4351
dbHasIndex = !dbIsPrimaryKey && json.optBoolean("is_index", false);

0 commit comments

Comments
 (0)