From 1f9a3c81b5ddd6218e2c6b62fe0478ba147ca6e0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 May 2015 18:49:30 +1200 Subject: Ignore field if no query column --- src/org/traccar/database/QueryBuilder.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index d05f48c09..a35b07fee 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -19,6 +19,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; @@ -203,6 +204,9 @@ public class QueryBuilder { public Collection executeQuery(T prototype) throws SQLException { List result = new LinkedList(); + ResultSet resultSet = statement.executeQuery(); + ResultSetMetaData resultMetaData = resultSet.getMetaData(); + List> processors = new LinkedList>(); Method[] methods = prototype.getClass().getMethods(); @@ -211,6 +215,19 @@ public class QueryBuilder { 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.getColumnName(i))) { + column = true; + break; + } + } + if (!column) { + continue; + } + Class parameterType = method.getParameterTypes()[0]; if (parameterType.equals(boolean.class)) { @@ -282,8 +299,6 @@ public class QueryBuilder { } } } - - ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { T object = (T) prototype.create(); -- cgit v1.2.3