From add55d83f367c0001deadadac4b83cc6542eb234 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 14 Jul 2016 16:50:28 +0500 Subject: Set long as NULL only for *Id fields --- src/org/traccar/database/QueryBuilder.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index b91218a13..7d8731933 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -175,7 +175,20 @@ public final class QueryBuilder { public QueryBuilder setLong(String name, long value) throws SQLException { for (int i : indexes(name)) { try { - if (value == 0) { + statement.setLong(i, value); + } catch (SQLException error) { + statement.close(); + connection.close(); + throw error; + } + } + return this; + } + + public QueryBuilder setLong(String name, long value, boolean nullIfZero) throws SQLException { + for (int i : indexes(name)) { + try { + if (value == 0 && nullIfZero) { statement.setNull(i, Types.INTEGER); } else { statement.setLong(i, value); @@ -249,7 +262,11 @@ public final class QueryBuilder { } else if (method.getReturnType().equals(int.class)) { setInteger(name, (Integer) method.invoke(object)); } else if (method.getReturnType().equals(long.class)) { - setLong(name, (Long) method.invoke(object)); + if (name.endsWith("Id")) { + setLong(name, (Long) method.invoke(object), true); + } else { + setLong(name, (Long) method.invoke(object)); + } } else if (method.getReturnType().equals(double.class)) { setDouble(name, (Double) method.invoke(object)); } else if (method.getReturnType().equals(String.class)) { -- cgit v1.2.3