diff options
-rw-r--r-- | releases.moxie | 9 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectDefault.java | 14 | ||||
-rw-r--r-- | src/main/java/com/iciql/SQLDialectPostgreSQL.java | 32 |
3 files changed, 45 insertions, 10 deletions
diff --git a/releases.moxie b/releases.moxie index 4a7bbbe..156c86d 100644 --- a/releases.moxie +++ b/releases.moxie @@ -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 } # diff --git a/src/main/java/com/iciql/SQLDialectDefault.java b/src/main/java/com/iciql/SQLDialectDefault.java index e5b6ca6..371ee31 100644 --- a/src/main/java/com/iciql/SQLDialectDefault.java +++ b/src/main/java/com/iciql/SQLDialectDefault.java @@ -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<?>>(); diff --git a/src/main/java/com/iciql/SQLDialectPostgreSQL.java b/src/main/java/com/iciql/SQLDialectPostgreSQL.java index 6998c24..382c435 100644 --- a/src/main/java/com/iciql/SQLDialectPostgreSQL.java +++ b/src/main/java/com/iciql/SQLDialectPostgreSQL.java @@ -16,6 +16,7 @@ package com.iciql;
+import com.iciql.TableDefinition.FieldDefinition;
import com.iciql.TableDefinition.IndexDefinition;
import com.iciql.util.StatementBuilder;
@@ -101,4 +102,35 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault { stat.setSQL(buff.toString().trim());
}
+ @Override
+ public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName,
+ TableDefinition<T> def, Object obj) {
+
+ if (databaseVersion < 9.5f) {
+ // simulated UPSERT for <= 9.4 release
+ super.prepareMerge(stat, schemaName, tableName, def, obj);
+ return;
+ }
+
+ // official UPSERT added in 9.5 release
+ StatementBuilder buff = new StatementBuilder("INSERT INTO ");
+ buff.append(prepareTableName(schemaName, tableName)).append(" (");
+ buff.resetCount();
+ for (FieldDefinition field : def.fields) {
+ buff.appendExceptFirst(", ");
+ buff.append(field.columnName);
+ }
+ buff.resetCount();
+ buff.append(") VALUES (");
+ for (FieldDefinition field : def.fields) {
+ buff.appendExceptFirst(", ");
+ buff.append('?');
+ Object value = def.getValue(obj, field);
+ Object parameter = serialize(value, field.typeAdapter);
+ stat.addParameter(parameter);
+ }
+ buff.append(") ON CONFLICT DO UPDATE SET");
+ stat.setSQL(buff.toString());
+ }
+
}
\ No newline at end of file |