summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2015-05-13 12:08:55 -0400
committerJames Moger <james.moger@gitblit.com>2015-05-13 12:08:55 -0400
commit75c995b50cf8f00655d578de7cb88106c9c27918 (patch)
tree7724cd97ffebbcbd86567bc5a3b13acbae64fb52 /src
parent0bdd8009079a1501a0e4511f26607616862de608 (diff)
downloadiciql-75c995b50cf8f00655d578de7cb88106c9c27918.tar.gz
iciql-75c995b50cf8f00655d578de7cb88106c9c27918.zip
Add support for upcoming Postgres 9.5 UPSERT syntax
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/iciql/SQLDialectDefault.java14
-rw-r--r--src/main/java/com/iciql/SQLDialectPostgreSQL.java32
2 files changed, 39 insertions, 7 deletions
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