From a3e01d6998005db012530be3456e8d06b5d91c5b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 19 Jun 2015 18:51:33 +1200 Subject: Handle null database queries --- src/org/traccar/database/QueryBuilder.java | 228 +++++++++++++++-------------- 1 file changed, 117 insertions(+), 111 deletions(-) (limited to 'src/org') 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> 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>(); 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 Collection executeQuery(T prototype) throws SQLException { List result = new LinkedList(); - try { + if (query != null) { - ResultSet resultSet = statement.executeQuery(); - try { - - ResultSetMetaData resultMetaData = resultSet.getMetaData(); - List> processors = new LinkedList>(); + 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> processors = new LinkedList>(); - 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() { - @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() { + @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() { - @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() { + @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() { - @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() { + @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() { - @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() { + @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() { - @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() { + @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() { - @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() { + @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 processor : processors) { - processor.process(object, resultSet); + while (resultSet.next()) { + T object = (T) prototype.create(); + for (ResultSetProcessor 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; } -- cgit v1.2.3