]> source.dussan.org Git - iciql.git/commitdiff
Fixed bug in delete() and update() where primary key is a primitive. v0.6.5
authorJames Moger <james.moger@gmail.com>
Fri, 12 Aug 2011 19:41:42 +0000 (15:41 -0400)
committerJames Moger <james.moger@gmail.com>
Fri, 12 Aug 2011 19:41:42 +0000 (15:41 -0400)
docs/05_releases.mkd
src/com/iciql/Constants.java
src/com/iciql/Db.java
src/com/iciql/TableDefinition.java
tests/com/iciql/test/PrimitivesTest.java

index a837a31882e2db74bd45ddd26323de225ea5a2d0..c6d84e1af2f3d71af27499fb4fd2cadbccb22b23 100644 (file)
@@ -6,6 +6,12 @@
 \r
 **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%)) &nbsp; *released %BUILDDATE%*\r
 \r
+- fixed failure of db.delete(PrimitiveModel) and db.update(PrimitiveModel)\r
+\r
+### Older Releases\r
+\r
+**0.6.4** &nbsp; *released 2011-08-12*\r
+\r
 - api change release (API v4)\r
 - @IQTable.createIfRequired -> @IQTable.create\r
 - don't INSERT primitive autoIncrement fields, let database assign value\r
@@ -39,8 +45,6 @@ Date myDate = new Date(100, 1, 1);
 @IQTable( primaryKey = {"name", "nickname"})\r
 %ENDCODE%\r
 \r
-### Older Releases\r
-\r
 **0.6.3** &nbsp; *released 2011-08-08*\r
 \r
 - api change release (API v3)\r
index 23f42d20854d873e1faa2d077d65ee38cce4dd10..e8ac70aeeeaed60c0008041bbb86e415fc2a32f1 100644 (file)
@@ -25,7 +25,7 @@ public class Constants {
 \r
        // The build script extracts this exact line so be careful editing it\r
        // and only use A-Z a-z 0-9 .-_ in the string.\r
-       public static final String VERSION = "0.6.4";\r
+       public static final String VERSION = "0.6.5";\r
 \r
        // The build script extracts this exact line so be careful editing it\r
        // and only use A-Z a-z 0-9 .-_ in the string.\r
index a8c0b127a3ed597f6ae792c7e0bc45d1a07a3832..cbc1ed4b1d43536e6ff5f3148d2b40d261cda4f4 100644 (file)
@@ -201,14 +201,14 @@ public class Db {
                define(clazz).createTableIfRequired(this).merge(this, t);\r
        }\r
 \r
-       public <T> void update(T t) {\r
+       public <T> int update(T t) {\r
                Class<?> clazz = t.getClass();\r
-               define(clazz).createTableIfRequired(this).update(this, t);\r
+               return define(clazz).createTableIfRequired(this).update(this, t);\r
        }\r
 \r
-       public <T> void delete(T t) {\r
+       public <T> int delete(T t) {\r
                Class<?> clazz = t.getClass();\r
-               define(clazz).createTableIfRequired(this).delete(this, t);\r
+               return define(clazz).createTableIfRequired(this).delete(this, t);\r
        }\r
 \r
        public <T extends Object> Query<T> from(T alias) {\r
index 03345d4ce9c61990292c657834cc41266ec6559e..ec797560c73381e14d1ac34db18daeb89a050935 100644 (file)
@@ -468,7 +468,7 @@ public class TableDefinition<T> {
                stat.executeUpdate();\r
        }\r
 \r
-       void update(Db db, Object obj) {\r
+       int update(Db db, Object obj) {\r
                if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {\r
                        throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass()\r
                                        + " - no update possible");\r
@@ -492,24 +492,27 @@ public class TableDefinition<T> {
                boolean firstCondition = true;\r
                for (FieldDefinition field : fields) {\r
                        if (field.isPrimaryKey) {\r
-                               Object aliasValue = field.getValue(alias);\r
+                               Object fieldAlias = field.getValue(alias);\r
                                Object value = field.getValue(obj);\r
+                               if (field.isPrimitive) {\r
+                                       fieldAlias = query.getPrimitiveAliasByValue(fieldAlias);\r
+                               }\r
                                if (!firstCondition) {\r
                                        query.addConditionToken(ConditionAndOr.AND);\r
                                }\r
                                firstCondition = false;\r
-                               query.addConditionToken(new Condition<Object>(aliasValue, value, CompareType.EQUAL));\r
+                               query.addConditionToken(new Condition<Object>(fieldAlias, value, CompareType.EQUAL));\r
                        }\r
                }\r
                stat.setSQL(buff.toString());\r
                query.appendWhere(stat);\r
                StatementLogger.update(stat.getSQL());\r
-               stat.executeUpdate();\r
+               return stat.executeUpdate();\r
        }\r
 \r
-       void delete(Db db, Object obj) {\r
+       int delete(Db db, Object obj) {\r
                if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {\r
-                       throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass()\r
+                       throw new IllegalStateException("No primary key columns defined for table " + obj.getClass()\r
                                        + " - no update possible");\r
                }\r
                SQLStatement stat = new SQLStatement(db);\r
@@ -521,19 +524,22 @@ public class TableDefinition<T> {
                boolean firstCondition = true;\r
                for (FieldDefinition field : fields) {\r
                        if (field.isPrimaryKey) {\r
-                               Object aliasValue = field.getValue(alias);\r
+                               Object fieldAlias = field.getValue(alias);\r
                                Object value = field.getValue(obj);\r
+                               if (field.isPrimitive) {\r
+                                       fieldAlias = query.getPrimitiveAliasByValue(fieldAlias);\r
+                               }\r
                                if (!firstCondition) {\r
                                        query.addConditionToken(ConditionAndOr.AND);\r
                                }\r
                                firstCondition = false;\r
-                               query.addConditionToken(new Condition<Object>(aliasValue, value, CompareType.EQUAL));\r
+                               query.addConditionToken(new Condition<Object>(fieldAlias, value, CompareType.EQUAL));\r
                        }\r
                }\r
                stat.setSQL(buff.toString());\r
                query.appendWhere(stat);\r
                StatementLogger.delete(stat.getSQL());\r
-               stat.executeUpdate();\r
+               return stat.executeUpdate();\r
        }\r
 \r
        TableDefinition<T> createTableIfRequired(Db db) {\r
index a200b57ab139a914782c807f7f15efe681b6f769..9c214b18e739f2e1ea7d413ec021209320616a29 100644 (file)
@@ -74,6 +74,11 @@ public class PrimitivesTest {
                List<PrimitivesModel> list = db.from(p).orderBy(p.myLong).select();\r
                assertEquals(models.size(), list.size());\r
                assertEquals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]", list.toString());\r
+               \r
+               // test model update\r
+               retrievedModel.myInteger = 1337;\r
+               assertEquals(1, db.update(retrievedModel));\r
+               assertEquals(1, db.delete(retrievedModel));             \r
 \r
                db.close();\r
        }\r