summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2016-04-04 13:50:18 -0400
committerJames Moger <james.moger@gitblit.com>2016-04-04 13:50:18 -0400
commit85a2499da3dcda11ca086aa3be180b17f0ddd2c3 (patch)
treeb418763d49ebf2916dd4e45fcae29b573c4a1c58
parent3902340cc24caa443fe2326a3374f4b329706cd7 (diff)
downloadiciql-85a2499da3dcda11ca086aa3be180b17f0ddd2c3.tar.gz
iciql-85a2499da3dcda11ca086aa3be180b17f0ddd2c3.zip
Fix #23: Postgres 9.5 UPSERT syntax was incorrect
-rw-r--r--releases.moxie4
-rw-r--r--src/main/java/com/iciql/SQLDialectPostgreSQL.java29
2 files changed, 30 insertions, 3 deletions
diff --git a/releases.moxie b/releases.moxie
index c19c81d..538540d 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -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
}
#
diff --git a/src/main/java/com/iciql/SQLDialectPostgreSQL.java b/src/main/java/com/iciql/SQLDialectPostgreSQL.java
index 382c435..54e47b0 100644
--- a/src/main/java/com/iciql/SQLDialectPostgreSQL.java
+++ b/src/main/java/com/iciql/SQLDialectPostgreSQL.java
@@ -106,7 +106,14 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault {
public <T> void prepareMerge(SQLStatement stat, String schemaName, String tableName,
TableDefinition<T> def, Object obj) {
- if (databaseVersion < 9.5f) {
+ FieldDefinition primaryKey = null;
+ for (FieldDefinition field : def.fields) {
+ if (field.isPrimaryKey) {
+ primaryKey = field;
+ }
+ }
+
+ if (primaryKey == null || databaseVersion < 9.5f) {
// simulated UPSERT for <= 9.4 release
super.prepareMerge(stat, schemaName, tableName, def, obj);
return;
@@ -129,7 +136,25 @@ public class SQLDialectPostgreSQL extends SQLDialectDefault {
Object parameter = serialize(value, field.typeAdapter);
stat.addParameter(parameter);
}
- buff.append(") ON CONFLICT DO UPDATE SET");
+
+ buff.append(") ON CONFLICT (");
+ buff.resetCount();
+ for (FieldDefinition field : def.fields) {
+ if (field.isPrimaryKey) {
+ buff.appendExceptFirst(", ");
+ buff.append(field.columnName);
+ }
+ }
+ buff.append(") DO UPDATE SET ");
+ buff.resetCount();
+ for (FieldDefinition field : def.fields) {
+ buff.appendExceptFirst(", ");
+ buff.append(field.columnName);
+ buff.append("=?");
+ Object value = def.getValue(obj, field);
+ Object parameter = serialize(value, field.typeAdapter);
+ stat.addParameter(parameter);
+ }
stat.setSQL(buff.toString());
}