diff options
author | James Moger <james.moger@gmail.com> | 2011-08-12 15:41:42 -0400 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2011-08-12 15:41:42 -0400 |
commit | 3622ae0026718ac8466e13e8011f8db309d89106 (patch) | |
tree | 39ff193be51b2970fd2b51efcaf73c669288005b | |
parent | 373a5c74f4c5e8cc19812a63893f6090d8f14b95 (diff) | |
download | iciql-3622ae0026718ac8466e13e8011f8db309d89106.tar.gz iciql-3622ae0026718ac8466e13e8011f8db309d89106.zip |
Fixed bug in delete() and update() where primary key is a primitive.v0.6.5
-rw-r--r-- | docs/05_releases.mkd | 8 | ||||
-rw-r--r-- | src/com/iciql/Constants.java | 2 | ||||
-rw-r--r-- | src/com/iciql/Db.java | 8 | ||||
-rw-r--r-- | src/com/iciql/TableDefinition.java | 24 | ||||
-rw-r--r-- | tests/com/iciql/test/PrimitivesTest.java | 5 |
5 files changed, 31 insertions, 16 deletions
diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd index a837a31..c6d84e1 100644 --- a/docs/05_releases.mkd +++ b/docs/05_releases.mkd @@ -6,6 +6,12 @@ **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%)) *released %BUILDDATE%*
+- fixed failure of db.delete(PrimitiveModel) and db.update(PrimitiveModel)
+
+### Older Releases
+
+**0.6.4** *released 2011-08-12*
+
- api change release (API v4)
- @IQTable.createIfRequired -> @IQTable.create
- don't INSERT primitive autoIncrement fields, let database assign value
@@ -39,8 +45,6 @@ Date myDate = new Date(100, 1, 1); @IQTable( primaryKey = {"name", "nickname"})
%ENDCODE%
-### Older Releases
-
**0.6.3** *released 2011-08-08*
- api change release (API v3)
diff --git a/src/com/iciql/Constants.java b/src/com/iciql/Constants.java index 23f42d2..e8ac70a 100644 --- a/src/com/iciql/Constants.java +++ b/src/com/iciql/Constants.java @@ -25,7 +25,7 @@ public class Constants { // The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
- public static final String VERSION = "0.6.4";
+ public static final String VERSION = "0.6.5";
// The build script extracts this exact line so be careful editing it
// and only use A-Z a-z 0-9 .-_ in the string.
diff --git a/src/com/iciql/Db.java b/src/com/iciql/Db.java index a8c0b12..cbc1ed4 100644 --- a/src/com/iciql/Db.java +++ b/src/com/iciql/Db.java @@ -201,14 +201,14 @@ public class Db { define(clazz).createTableIfRequired(this).merge(this, t);
}
- public <T> void update(T t) {
+ public <T> int update(T t) {
Class<?> clazz = t.getClass();
- define(clazz).createTableIfRequired(this).update(this, t);
+ return define(clazz).createTableIfRequired(this).update(this, t);
}
- public <T> void delete(T t) {
+ public <T> int delete(T t) {
Class<?> clazz = t.getClass();
- define(clazz).createTableIfRequired(this).delete(this, t);
+ return define(clazz).createTableIfRequired(this).delete(this, t);
}
public <T extends Object> Query<T> from(T alias) {
diff --git a/src/com/iciql/TableDefinition.java b/src/com/iciql/TableDefinition.java index 03345d4..ec79756 100644 --- a/src/com/iciql/TableDefinition.java +++ b/src/com/iciql/TableDefinition.java @@ -468,7 +468,7 @@ public class TableDefinition<T> { stat.executeUpdate();
}
- void update(Db db, Object obj) {
+ int update(Db db, Object obj) {
if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass()
+ " - no update possible");
@@ -492,24 +492,27 @@ public class TableDefinition<T> { boolean firstCondition = true;
for (FieldDefinition field : fields) {
if (field.isPrimaryKey) {
- Object aliasValue = field.getValue(alias);
+ Object fieldAlias = field.getValue(alias);
Object value = field.getValue(obj);
+ if (field.isPrimitive) {
+ fieldAlias = query.getPrimitiveAliasByValue(fieldAlias);
+ }
if (!firstCondition) {
query.addConditionToken(ConditionAndOr.AND);
}
firstCondition = false;
- query.addConditionToken(new Condition<Object>(aliasValue, value, CompareType.EQUAL));
+ query.addConditionToken(new Condition<Object>(fieldAlias, value, CompareType.EQUAL));
}
}
stat.setSQL(buff.toString());
query.appendWhere(stat);
StatementLogger.update(stat.getSQL());
- stat.executeUpdate();
+ return stat.executeUpdate();
}
- void delete(Db db, Object obj) {
+ int delete(Db db, Object obj) {
if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
- throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass()
+ throw new IllegalStateException("No primary key columns defined for table " + obj.getClass()
+ " - no update possible");
}
SQLStatement stat = new SQLStatement(db);
@@ -521,19 +524,22 @@ public class TableDefinition<T> { boolean firstCondition = true;
for (FieldDefinition field : fields) {
if (field.isPrimaryKey) {
- Object aliasValue = field.getValue(alias);
+ Object fieldAlias = field.getValue(alias);
Object value = field.getValue(obj);
+ if (field.isPrimitive) {
+ fieldAlias = query.getPrimitiveAliasByValue(fieldAlias);
+ }
if (!firstCondition) {
query.addConditionToken(ConditionAndOr.AND);
}
firstCondition = false;
- query.addConditionToken(new Condition<Object>(aliasValue, value, CompareType.EQUAL));
+ query.addConditionToken(new Condition<Object>(fieldAlias, value, CompareType.EQUAL));
}
}
stat.setSQL(buff.toString());
query.appendWhere(stat);
StatementLogger.delete(stat.getSQL());
- stat.executeUpdate();
+ return stat.executeUpdate();
}
TableDefinition<T> createTableIfRequired(Db db) {
diff --git a/tests/com/iciql/test/PrimitivesTest.java b/tests/com/iciql/test/PrimitivesTest.java index a200b57..9c214b1 100644 --- a/tests/com/iciql/test/PrimitivesTest.java +++ b/tests/com/iciql/test/PrimitivesTest.java @@ -74,6 +74,11 @@ public class PrimitivesTest { List<PrimitivesModel> list = db.from(p).orderBy(p.myLong).select();
assertEquals(models.size(), list.size());
assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", list.toString());
+
+ // test model update
+ retrievedModel.myInteger = 1337;
+ assertEquals(1, db.update(retrievedModel));
+ assertEquals(1, db.delete(retrievedModel));
db.close();
}
|