Fixed create query

This commit is contained in:
Luca Frosini 2021-04-08 12:03:33 +02:00
parent 1deb586924
commit 5ecaf67020
1 changed files with 22 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.accounting.utility.postgresql; package org.gcube.accounting.utility.postgresql;
import java.io.Serializable; import java.io.Serializable;
import java.sql.SQLException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.SortedSet; import java.util.SortedSet;
@ -8,9 +9,14 @@ import java.util.TimeZone;
import java.util.TreeSet; import java.util.TreeSet;
import org.gcube.documentstore.records.Record; import org.gcube.documentstore.records.Record;
import org.postgresql.core.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PostgreSQLQuery { public class PostgreSQLQuery {
protected static final Logger logger = LoggerFactory.getLogger(PostgreSQLQuery.class);
private static final String UTC_TIME_ZONE = "UTC"; private static final String UTC_TIME_ZONE = "UTC";
public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone(UTC_TIME_ZONE); public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone(UTC_TIME_ZONE);
@ -19,12 +25,12 @@ public class PostgreSQLQuery {
protected StringBuffer stringBuffer; protected StringBuffer stringBuffer;
protected String getQuotedString(String string) { protected String getQuotedString(String string) throws SQLException {
StringBuffer buffer = new StringBuffer(); StringBuilder builder = new StringBuilder();
buffer.append("'"); builder.append("'");
buffer.append(string); Utils.escapeLiteral(builder, string, false);
buffer.append("'"); builder.append("'");
return buffer.toString(); return builder.toString();
} }
protected void appendString(String string) { protected void appendString(String string) {
@ -33,11 +39,11 @@ public class PostgreSQLQuery {
stringBuffer.append("'"); stringBuffer.append("'");
} }
protected void appendValue(Serializable serializable) { protected void appendValue(Serializable serializable) throws SQLException {
stringBuffer.append(getValue(serializable)); stringBuffer.append(getValue(serializable));
} }
protected String getValue(Serializable serializable) { protected String getValue(Serializable serializable) throws SQLException {
if(serializable instanceof Number) { if(serializable instanceof Number) {
return serializable.toString(); return serializable.toString();
} }
@ -94,8 +100,14 @@ public class PostgreSQLQuery {
} }
stringBuffer.append(") VALUES"); stringBuffer.append(") VALUES");
stringBuffer.append(values); stringBuffer.append(values);
stringBuffer.append(");"); stringBuffer.append(")");
return stringBuffer.toString(); stringBuffer.append(" ON CONFLICT (");
String dbField = recordToDBFields.getTableField(Record.ID);
stringBuffer.append(dbField);
stringBuffer.append(") DO NOTHING;");
String ret = stringBuffer.toString();
logger.trace("Created query {}", ret);
return ret;
} }
} }