aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-19 18:51:33 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-19 18:51:33 +1200
commita3e01d6998005db012530be3456e8d06b5d91c5b (patch)
tree3b2f34ec7b6b0546a28daf1c32aa899350732ae6 /src/org
parent4aa2838cd7a71ee1d0c819ce6a37bd2aaddd233d (diff)
downloadtrackermap-server-a3e01d6998005db012530be3456e8d06b5d91c5b.tar.gz
trackermap-server-a3e01d6998005db012530be3456e8d06b5d91c5b.tar.bz2
trackermap-server-a3e01d6998005db012530be3456e8d06b5d91c5b.zip
Handle null database queries
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/database/QueryBuilder.java228
1 files changed, 117 insertions, 111 deletions
diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java
index 05ec3e35c..23d8c6bfa 100644
--- a/src/org/traccar/database/QueryBuilder.java
+++ b/src/org/traccar/database/QueryBuilder.java
@@ -15,7 +15,6 @@
*/
package org.traccar.database;
-import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
@@ -32,26 +31,28 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-
import javax.sql.DataSource;
-
import org.traccar.model.Factory;
public class QueryBuilder {
private final Map<String, List<Integer>> indexMap;
private final Connection connection;
- private final PreparedStatement statement;
+ private PreparedStatement statement;
+ private final String query;
private QueryBuilder(DataSource dataSource, String query) throws SQLException {
indexMap = new HashMap<String, List<Integer>>();
connection = dataSource.getConnection();
- String parsedQuery = parse(query, indexMap);
- try {
- statement = connection.prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS);
- } catch (SQLException error) {
- connection.close();
- throw error;
+ this.query = query;
+ if (query != null) {
+ String parsedQuery = parse(query, indexMap);
+ try {
+ statement = connection.prepareStatement(parsedQuery, Statement.RETURN_GENERATED_KEYS);
+ } catch (SQLException error) {
+ connection.close();
+ throw error;
+ }
}
}
@@ -279,122 +280,125 @@ public class QueryBuilder {
public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException {
List<T> result = new LinkedList<T>();
- try {
+ if (query != null) {
- ResultSet resultSet = statement.executeQuery();
-
try {
-
- ResultSetMetaData resultMetaData = resultSet.getMetaData();
- List<ResultSetProcessor<T>> processors = new LinkedList<ResultSetProcessor<T>>();
+ ResultSet resultSet = statement.executeQuery();
+
+ try {
- Method[] methods = prototype.getClass().getMethods();
+ ResultSetMetaData resultMetaData = resultSet.getMetaData();
- for (final Method method : methods) {
- if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {
+ List<ResultSetProcessor<T>> processors = new LinkedList<ResultSetProcessor<T>>();
- final String name = method.getName().substring(3);
+ Method[] methods = prototype.getClass().getMethods();
- // Check if column exists
- boolean column = false;
- for (int i = 1; i <= resultMetaData.getColumnCount(); i++) {
- if (name.equalsIgnoreCase(resultMetaData.getColumnLabel(i))) {
- column = true;
- break;
+ for (final Method method : methods) {
+ if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {
+
+ final String name = method.getName().substring(3);
+
+ // Check if column exists
+ boolean column = false;
+ for (int i = 1; i <= resultMetaData.getColumnCount(); i++) {
+ if (name.equalsIgnoreCase(resultMetaData.getColumnLabel(i))) {
+ column = true;
+ break;
+ }
+ }
+ if (!column) {
+ continue;
}
- }
- if (!column) {
- continue;
- }
- Class<?> parameterType = method.getParameterTypes()[0];
+ Class<?> parameterType = method.getParameterTypes()[0];
- if (parameterType.equals(boolean.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getBoolean(name));
- } catch (IllegalAccessException error) {
- } catch (InvocationTargetException error) {
+ if (parameterType.equals(boolean.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getBoolean(name));
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
}
- }
- });
- } else if (parameterType.equals(int.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getInt(name));
- } catch (IllegalAccessException error) {
- } catch (InvocationTargetException error) {
+ });
+ } else if (parameterType.equals(int.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getInt(name));
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
}
- }
- });
- } else if (parameterType.equals(long.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getLong(name));
- } catch (IllegalAccessException error) {
- } catch (InvocationTargetException error) {
+ });
+ } else if (parameterType.equals(long.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getLong(name));
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
}
- }
- });
- } else if (parameterType.equals(double.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getDouble(name));
- } catch (IllegalAccessException error) {
- } catch (InvocationTargetException error) {
+ });
+ } else if (parameterType.equals(double.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getDouble(name));
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
}
- }
- });
- } else if (parameterType.equals(String.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getString(name));
- } catch (IllegalAccessException error) {
- } catch (InvocationTargetException error) {
+ });
+ } else if (parameterType.equals(String.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getString(name));
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
}
- }
- });
- } else if (parameterType.equals(Date.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, new Date(resultSet.getTimestamp(name).getTime()));
- } catch (IllegalAccessException error) {
- } catch (InvocationTargetException error) {
+ });
+ } else if (parameterType.equals(Date.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, new Date(resultSet.getTimestamp(name).getTime()));
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
}
- }
- });
+ });
+ }
}
}
- }
- while (resultSet.next()) {
- T object = (T) prototype.create();
- for (ResultSetProcessor<T> processor : processors) {
- processor.process(object, resultSet);
+ while (resultSet.next()) {
+ T object = (T) prototype.create();
+ for (ResultSetProcessor<T> processor : processors) {
+ processor.process(object, resultSet);
+ }
+ result.add(object);
}
- result.add(object);
+
+ } finally {
+ resultSet.close();
}
-
+
} finally {
- resultSet.close();
+ statement.close();
+ connection.close();
}
-
- } finally {
- statement.close();
- connection.close();
}
return result;
@@ -402,15 +406,17 @@ public class QueryBuilder {
public long executeUpdate() throws SQLException {
- try {
- statement.executeUpdate();
- ResultSet resultSet = statement.getGeneratedKeys();
- if (resultSet.next()) {
- return resultSet.getLong(1);
+ if (query != null) {
+ try {
+ statement.executeUpdate();
+ ResultSet resultSet = statement.getGeneratedKeys();
+ if (resultSet.next()) {
+ return resultSet.getLong(1);
+ }
+ } finally {
+ statement.close();
+ connection.close();
}
- } finally {
- statement.close();
- connection.close();
}
return 0;
}