]> source.dussan.org Git - iciql.git/commitdiff
Add support for upcoming Postgres 9.5 UPSERT syntax
authorJames Moger <james.moger@gitblit.com>
Wed, 13 May 2015 16:08:55 +0000 (12:08 -0400)
committerJames Moger <james.moger@gitblit.com>
Wed, 13 May 2015 16:08:55 +0000 (12:08 -0400)
releases.moxie
src/main/java/com/iciql/SQLDialectDefault.java
src/main/java/com/iciql/SQLDialectPostgreSQL.java

index 4a7bbbe70ea6148bad52a22811090aa6f6cd00a8..156c86dfdace2a7db296bbeacd6d7e05d73eafd9 100644 (file)
@@ -10,10 +10,12 @@ r27: {
     text: ~
     security: ~
     fixes: ~
-    changes: ~
+    changes:
+    - Add support for PostgreSQL 9.5 UPSERT syntax
     additions: ~
     dependencyChanges: ~
-    contributors: ~
+    contributors:
+    - James Moger
 }
 
 #
@@ -32,7 +34,8 @@ r26: {
     changes: ~
     additions: ~
     dependencyChanges: ~
-    contributors: ~
+    contributors:
+    - James Moger
 }
 
 #
index e5b6ca60b5fd6ff0c72bda38a895e98f4b42beed..371ee31a998f8f7b05a6d7781ef52e46f226c0bd 100644 (file)
@@ -49,13 +49,13 @@ public class SQLDialectDefault implements SQLDialect {
 
        final String LITERAL = "'";
 
-       float databaseVersion;
-       int databaseMajorVersion;
-       int databaseMinorVersion;
-       String databaseName;
-       String productVersion;
-       Mode mode;
-       Map<Class<? extends DataTypeAdapter<?>>, DataTypeAdapter<?>> typeAdapters;
+       protected float databaseVersion;
+       protected int databaseMajorVersion;
+       protected int databaseMinorVersion;
+       protected String databaseName;
+       protected String productVersion;
+       protected Mode mode;
+       protected Map<Class<? extends DataTypeAdapter<?>>, DataTypeAdapter<?>> typeAdapters;
 
        public SQLDialectDefault() {
                typeAdapters = new ConcurrentHashMap<Class<? extends DataTypeAdapter<?>>, DataTypeAdapter<?>>();
index 6998c24556fb1f33a5a9067423d20b2a704e18e4..382c435bc680095be502423b6e96ae7b07bd87df 100644 (file)
@@ -16,6 +16,7 @@
 \r
 package com.iciql;\r
 \r
+import com.iciql.TableDefinition.FieldDefinition;\r
 import com.iciql.TableDefinition.IndexDefinition;\r
 import com.iciql.util.StatementBuilder;\r
 \r
@@ -101,4 +102,35 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault {
                stat.setSQL(buff.toString().trim());\r
        }\r
 \r
+       @Override\r
+       public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName,\r
+                       TableDefinition<T> def, Object obj) {\r
+\r
+               if (databaseVersion < 9.5f) {\r
+                       // simulated UPSERT for <= 9.4 release\r
+                       super.prepareMerge(stat, schemaName, tableName, def, obj);\r
+                       return;\r
+               }\r
+\r
+               // official UPSERT added in 9.5 release\r
+               StatementBuilder buff = new StatementBuilder("INSERT INTO ");\r
+               buff.append(prepareTableName(schemaName, tableName)).append(" (");\r
+               buff.resetCount();\r
+               for (FieldDefinition field : def.fields) {\r
+                       buff.appendExceptFirst(", ");\r
+                       buff.append(field.columnName);\r
+               }\r
+               buff.resetCount();\r
+               buff.append(") VALUES (");\r
+               for (FieldDefinition field : def.fields) {\r
+                       buff.appendExceptFirst(", ");\r
+                       buff.append('?');\r
+                       Object value = def.getValue(obj, field);\r
+                       Object parameter = serialize(value, field.typeAdapter);\r
+                       stat.addParameter(parameter);\r
+               }\r
+               buff.append(") ON CONFLICT DO UPDATE SET");\r
+               stat.setSQL(buff.toString());\r
+       }\r
+\r
 }
\ No newline at end of file