]> source.dussan.org Git - iciql.git/commitdiff
Fix #23: Postgres 9.5 UPSERT syntax was incorrect
authorJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 17:50:18 +0000 (13:50 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 4 Apr 2016 17:50:18 +0000 (13:50 -0400)
releases.moxie
src/main/java/com/iciql/SQLDialectPostgreSQL.java

index c19c81da7d1a3a5b85865c370e4d56f3fb162f74..538540d8d75987b8a25aa8c1d2c92f47945d9e92 100644 (file)
@@ -11,11 +11,13 @@ r30: {
     security: ~
     fixes:
     - Fixed setting null column values (issue-22)
+    - Fixed Postgres 9.5 Upsert syntax (issue-23)
     changes: ~
     additions:
     - Added groupBy methods to QueryWhere (issue-20)
     dependencyChanges: ~
-    contributors: ~
+    contributors:
+    - James Moger
 }
 
 #
index 382c435bc680095be502423b6e96ae7b07bd87df..54e47b0aa253cd642f8d15ef7850067dfce8a589 100644 (file)
@@ -106,7 +106,14 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault {
        public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName,\r
                        TableDefinition<T> def, Object obj) {\r
 \r
-               if (databaseVersion < 9.5f) {\r
+               FieldDefinition primaryKey = null;\r
+               for (FieldDefinition field : def.fields) {\r
+                       if (field.isPrimaryKey) {\r
+                               primaryKey = field;\r
+                       }\r
+               }\r
+\r
+               if (primaryKey == null || databaseVersion < 9.5f) {\r
                        // simulated UPSERT for <= 9.4 release\r
                        super.prepareMerge(stat, schemaName, tableName, def, obj);\r
                        return;\r
@@ -129,7 +136,25 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault {
                        Object parameter = serialize(value, field.typeAdapter);\r
                        stat.addParameter(parameter);\r
                }\r
-               buff.append(") ON CONFLICT DO UPDATE SET");\r
+\r
+               buff.append(") ON CONFLICT (");\r
+               buff.resetCount();\r
+               for (FieldDefinition field : def.fields) {\r
+                       if (field.isPrimaryKey) {\r
+                               buff.appendExceptFirst(", ");\r
+                               buff.append(field.columnName);\r
+                       }\r
+               }\r
+               buff.append(") DO UPDATE SET ");\r
+               buff.resetCount();\r
+               for (FieldDefinition field : def.fields) {\r
+                       buff.appendExceptFirst(", ");\r
+                       buff.append(field.columnName);\r
+                       buff.append("=?");\r
+                       Object value = def.getValue(obj, field);\r
+                       Object parameter = serialize(value, field.typeAdapter);\r
+                       stat.addParameter(parameter);\r
+               }\r
                stat.setSQL(buff.toString());\r
        }\r
 \r