From 3622ae0026718ac8466e13e8011f8db309d89106 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 12 Aug 2011 15:41:42 -0400 Subject: [PATCH] Fixed bug in delete() and update() where primary key is a primitive. --- docs/05_releases.mkd | 8 ++++++-- src/com/iciql/Constants.java | 2 +- src/com/iciql/Db.java | 8 ++++---- src/com/iciql/TableDefinition.java | 24 +++++++++++++++--------- 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 void update(T t) { + public int update(T t) { Class clazz = t.getClass(); - define(clazz).createTableIfRequired(this).update(this, t); + return define(clazz).createTableIfRequired(this).update(this, t); } - public void delete(T t) { + public int delete(T t) { Class clazz = t.getClass(); - define(clazz).createTableIfRequired(this).delete(this, t); + return define(clazz).createTableIfRequired(this).delete(this, t); } public Query 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 { 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 { 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(aliasValue, value, CompareType.EQUAL)); + query.addConditionToken(new Condition(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 { 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(aliasValue, value, CompareType.EQUAL)); + query.addConditionToken(new Condition(fieldAlias, value, CompareType.EQUAL)); } } stat.setSQL(buff.toString()); query.appendWhere(stat); StatementLogger.delete(stat.getSQL()); - stat.executeUpdate(); + return stat.executeUpdate(); } TableDefinition 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 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(); } -- 2.39.5