From: James Moger Date: Wed, 13 May 2015 16:08:55 +0000 (-0400) Subject: Add support for upcoming Postgres 9.5 UPSERT syntax X-Git-Tag: v1.6.3~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=75c995b50cf8f00655d578de7cb88106c9c27918;p=iciql.git Add support for upcoming Postgres 9.5 UPSERT syntax --- 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>, DataTypeAdapter> typeAdapters; + protected float databaseVersion; + protected int databaseMajorVersion; + protected int databaseMinorVersion; + protected String databaseName; + protected String productVersion; + protected Mode mode; + protected Map>, DataTypeAdapter> typeAdapters; public SQLDialectDefault() { typeAdapters = new ConcurrentHashMap>, 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 void prepareMerge(SQLStatement stat, String schemaName, String tableName, + TableDefinition 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