From 88b3f00f3855aa408c7859051efdcb5b37fe559f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 29 Jun 2015 09:41:29 +1200 Subject: Fix postgres generated keys problem (fix #480) --- src/org/traccar/database/DataManager.java | 6 +++--- src/org/traccar/database/QueryBuilder.java | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 8b1ff2c2e..496a5a1b6 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -227,7 +227,7 @@ public class DataManager { } public void addUser(User user) throws SQLException { - user.setId(QueryBuilder.create(dataSource, getQuery("database.insertUser")) + user.setId(QueryBuilder.create(dataSource, getQuery("database.insertUser"), true) .setObject(user) .executeUpdate()); Context.getPermissionsManager().refresh(); @@ -269,7 +269,7 @@ public class DataManager { } public void addDevice(Device device) throws SQLException { - device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice")) + device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice"), true) .setObject(device) .executeUpdate()); } @@ -306,7 +306,7 @@ public class DataManager { } public void addPosition(Position position) throws SQLException { - position.setId(QueryBuilder.create(dataSource, getQuery("database.insertPosition")) + position.setId(QueryBuilder.create(dataSource, getQuery("database.insertPosition"), true) .setObject(position) .setDate("time", position.getFixTime()) // tmp .setLong("device_id", position.getDeviceId()) // tmp diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index 23d8c6bfa..5e5c163a3 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -41,14 +41,18 @@ public class QueryBuilder { private PreparedStatement statement; private final String query; - private QueryBuilder(DataSource dataSource, String query) throws SQLException { + private QueryBuilder(DataSource dataSource, String query, boolean returnGeneratedKeys) throws SQLException { indexMap = new HashMap>(); connection = dataSource.getConnection(); this.query = query; if (query != null) { - String parsedQuery = parse(query, indexMap); + String parsedQuery = parse(query.trim(), indexMap); try { - statement = connection.prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS); + if (returnGeneratedKeys) { + statement = connection.prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS); + } else { + statement = connection.prepareStatement(parsedQuery); + } } catch (SQLException error) { connection.close(); throw error; @@ -115,9 +119,13 @@ public class QueryBuilder { return parsedQuery.toString(); } - + public static QueryBuilder create(DataSource dataSource, String query) throws SQLException { - return new QueryBuilder(dataSource, query); + return new QueryBuilder(dataSource, query, false); + } + + public static QueryBuilder create(DataSource dataSource, String query, boolean returnGeneratedKeys) throws SQLException { + return new QueryBuilder(dataSource, query, returnGeneratedKeys); } private List indexes(String name) { -- cgit v1.2.3