From 4cb53c8d9942430a935138c07f5916846559ec78 Mon Sep 17 00:00:00 2001 From: Vertex Integration Date: Thu, 6 Apr 2017 14:53:00 -0400 Subject: [PATCH] Add Query.ignoreColumn() --- .../sql2o/DefaultResultSetHandlerFactory.java | 8 ++++++- ...DefaultResultSetHandlerFactoryBuilder.java | 9 +++++++- core/src/main/java/org/sql2o/Query.java | 7 +++++++ .../sql2o/ResultSetHandlerFactoryBuilder.java | 3 +++ .../main/java/org/sql2o/reflection/Pojo.java | 4 ++-- .../org/sql2o/reflection/PojoMetadata.java | 21 ++++++++++++------- .../java/org/sql2o/issues/IssuesTest.java | 20 +++++++++++++++++- .../reflect/ReadColumnAnnotationTest.java | 11 +++++----- 8 files changed, 65 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactory.java b/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactory.java index bbd0d053..a0b6602d 100644 --- a/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactory.java +++ b/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactory.java @@ -190,6 +190,10 @@ private ResultSetHandler newResultSetHandler0(final ResultSetMetaData meta) t getters = new Getter[columnCount + 1]; // getters[0] is always null for (int i = 1; i <= columnCount; i++) { String colName = quirks.getColumnName(meta, i); + if( metadata.getIgnoredColumns().contains( colName.toLowerCase() ) ) { + continue; + } + // behavior change: do not throw if POJO contains less properties getters[i] = getGetter(quirks, colName, metadata); @@ -203,7 +207,9 @@ private ResultSetHandler newResultSetHandler0(final ResultSetMetaData meta) t setters = new Setter[columnCount + 1]; // setters[0] is always null for (int i = 1; i <= columnCount; i++) { String colName = quirks.getColumnName(meta, i); - + if( metadata.getIgnoredColumns().contains( colName.toLowerCase() ) ) { + continue; + } setters[i] = getSetter(quirks, colName, metadata); // If more than 1 column is fetched (we cannot fall back to executeScalar), diff --git a/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactoryBuilder.java b/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactoryBuilder.java index 5f97fd9e..599f0cc1 100644 --- a/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactoryBuilder.java +++ b/core/src/main/java/org/sql2o/DefaultResultSetHandlerFactoryBuilder.java @@ -4,6 +4,7 @@ import org.sql2o.reflection.PojoMetadata; import java.util.Map; +import java.util.Set; public class DefaultResultSetHandlerFactoryBuilder implements ResultSetHandlerFactoryBuilder { private boolean caseSensitive; @@ -11,6 +12,12 @@ public class DefaultResultSetHandlerFactoryBuilder implements ResultSetHandlerFa private boolean throwOnMappingError; private Map columnMappings; private Quirks quirks; + private Set ignoredColumns; + + @Override + public void setIgnoredColumns( Set ignoredColumns ) { + this.ignoredColumns = ignoredColumns; + } public boolean isCaseSensitive() { return caseSensitive; @@ -58,7 +65,7 @@ public void setQuirks(Quirks quirks) { @SuppressWarnings("unchecked") public ResultSetHandlerFactory newFactory(Class clazz) { - PojoMetadata pojoMetadata = new PojoMetadata(clazz, caseSensitive, autoDeriveColumnNames, columnMappings, throwOnMappingError); + PojoMetadata pojoMetadata = new PojoMetadata(clazz, caseSensitive, autoDeriveColumnNames, columnMappings, throwOnMappingError, ignoredColumns); return new DefaultResultSetHandlerFactory(pojoMetadata, quirks); } diff --git a/core/src/main/java/org/sql2o/Query.java b/core/src/main/java/org/sql2o/Query.java index f60f62a1..2de5f5b6 100644 --- a/core/src/main/java/org/sql2o/Query.java +++ b/core/src/main/java/org/sql2o/Query.java @@ -27,6 +27,7 @@ public class Query implements AutoCloseable { private final static Logger logger = LocalLoggerFactory.getLogger(Query.class); private Connection connection; + private Set ignoredColumns = new HashSet<>(); private Map caseSensitiveColumnMappings; private Map columnMappings; private PreparedStatement preparedStatement = null; @@ -315,6 +316,11 @@ public void setParameter(int paramIdx, PreparedStatement statement) throws SQLEx return this; } + public Query ignoreColumn( String colName ) { + ignoredColumns.add( colName.toLowerCase() ); + return this; + } + /** * Set an array parameter.
* For example: @@ -529,6 +535,7 @@ private ResultSetHandlerFactory newResultSetHandlerFactory(Class retur builder.setColumnMappings(this.getColumnMappings()); builder.setQuirks(quirks); builder.throwOnMappingError(this.throwOnMappingFailure); + builder.setIgnoredColumns( ignoredColumns ); return builder.newFactory(returnType); } diff --git a/core/src/main/java/org/sql2o/ResultSetHandlerFactoryBuilder.java b/core/src/main/java/org/sql2o/ResultSetHandlerFactoryBuilder.java index 9d07ad54..f97784a4 100644 --- a/core/src/main/java/org/sql2o/ResultSetHandlerFactoryBuilder.java +++ b/core/src/main/java/org/sql2o/ResultSetHandlerFactoryBuilder.java @@ -3,6 +3,7 @@ import org.sql2o.quirks.Quirks; import java.util.Map; +import java.util.Set; /** * Created with IntelliJ IDEA. @@ -33,4 +34,6 @@ public interface ResultSetHandlerFactoryBuilder { void setQuirks(Quirks quirksMode); ResultSetHandlerFactory newFactory(Class clazz); + + void setIgnoredColumns( Set ignoredColumns ); } diff --git a/core/src/main/java/org/sql2o/reflection/Pojo.java b/core/src/main/java/org/sql2o/reflection/Pojo.java index f9ba76a1..66fb6a5d 100644 --- a/core/src/main/java/org/sql2o/reflection/Pojo.java +++ b/core/src/main/java/org/sql2o/reflection/Pojo.java @@ -58,7 +58,7 @@ public Object getProperty(String propertyPath, Quirks quirks){ return getter.getProperty(this.object); } - PojoMetadata subMetadata = new PojoMetadata(getter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure); + PojoMetadata subMetadata = new PojoMetadata(getter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure, this.metadata.getIgnoredColumns()); Pojo subPojo = new Pojo(subMetadata, this.caseSensitive, subValue); return subPojo.getProperty(newPath, quirks); @@ -105,7 +105,7 @@ public void setProperty(String propertyPath, Object value, Quirks quirks){ setter.setProperty(this.object, subValue); } - PojoMetadata subMetadata = new PojoMetadata(setter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure); + PojoMetadata subMetadata = new PojoMetadata(setter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure, this.metadata.getIgnoredColumns()); Pojo subPojo = new Pojo(subMetadata, this.caseSensitive, subValue); subPojo.setProperty(newPath, value, quirks); } diff --git a/core/src/main/java/org/sql2o/reflection/PojoMetadata.java b/core/src/main/java/org/sql2o/reflection/PojoMetadata.java index 250f7dbd..9449da50 100644 --- a/core/src/main/java/org/sql2o/reflection/PojoMetadata.java +++ b/core/src/main/java/org/sql2o/reflection/PojoMetadata.java @@ -1,17 +1,17 @@ package org.sql2o.reflection; +import org.sql2o.Sql2oException; +import org.sql2o.tools.AbstractCache; +import org.sql2o.tools.UnderscoreToCamelCase; + +import javax.persistence.Column; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Collections; import java.util.HashMap; import java.util.Map; - -import javax.persistence.Column; - -import org.sql2o.Sql2oException; -import org.sql2o.tools.AbstractCache; -import org.sql2o.tools.UnderscoreToCamelCase; +import java.util.Set; /** * Stores metadata for a POJO. @@ -29,6 +29,8 @@ public class PojoMetadata { public final boolean throwOnMappingFailure; private Class clazz; + private final Set ignoredColumns; + public boolean isCaseSensitive() { return caseSensitive; } @@ -59,11 +61,12 @@ public int hashCode() { return result; } - public PojoMetadata(Class clazz, boolean caseSensitive, boolean autoDeriveColumnNames, Map columnMappings, boolean throwOnMappingError) { + public PojoMetadata(Class clazz, boolean caseSensitive, boolean autoDeriveColumnNames, Map columnMappings, boolean throwOnMappingError, Set ignoredColumns ) { this.caseSensitive = caseSensitive; this.autoDeriveColumnNames = autoDeriveColumnNames; this.clazz = clazz; this.columnMappings = columnMappings == null ? Collections.emptyMap() : columnMappings; + this.ignoredColumns = ignoredColumns == null ? Collections.emptySet() : ignoredColumns; this.propertyInfo = getPropertyInfoThroughCache(); this.throwOnMappingFailure = throwOnMappingError; @@ -74,6 +77,10 @@ public ObjectConstructor getObjectConstructor() { return propertyInfo.objectConstructor; } + public Set getIgnoredColumns() { + return ignoredColumns; + } + private PropertyAndFieldInfo getPropertyInfoThroughCache() { return (caseSensitive ? caseSensitiveTrue diff --git a/core/src/test/java/org/sql2o/issues/IssuesTest.java b/core/src/test/java/org/sql2o/issues/IssuesTest.java index 73cdfca8..9e8df533 100644 --- a/core/src/test/java/org/sql2o/issues/IssuesTest.java +++ b/core/src/test/java/org/sql2o/issues/IssuesTest.java @@ -361,7 +361,7 @@ class Pojo{ try { Pojo pojo = connection.createQuery(sql).executeAndFetchFirst(Pojo.class); - fail("Expeced an exception to be thrown"); + fail("Expected an exception to be thrown"); } catch(Sql2oException e) { assertEquals("Could not map VAL2 to any property.", e.getMessage()); } @@ -373,6 +373,24 @@ class Pojo{ } } + @Test + public void testIgnoreSpecificColumnMapping() { + String sql = "select 1 id, 'foo' val1, 'bar' val2 from (values(0))"; + + class Pojo{ + public int id; + public String val1; + } + + try (Connection connection = sql2o.open()) { + + Pojo pojo = connection.createQuery(sql).ignoreColumn("val2").executeAndFetchFirst(Pojo.class); + + assertEquals(1, pojo.id); + assertEquals("foo", pojo.val1); + } + } + @Test public void testIssue166OneCharacterParameterFail() { try (Connection connection = sql2o.open()) { diff --git a/core/src/test/java/org/sql2o/reflect/ReadColumnAnnotationTest.java b/core/src/test/java/org/sql2o/reflect/ReadColumnAnnotationTest.java index ad59cf47..63fe5564 100644 --- a/core/src/test/java/org/sql2o/reflect/ReadColumnAnnotationTest.java +++ b/core/src/test/java/org/sql2o/reflect/ReadColumnAnnotationTest.java @@ -1,13 +1,12 @@ package org.sql2o.reflect; -import javax.persistence.Column; - +import com.google.common.collect.ImmutableMap; +import junit.framework.TestCase; import org.junit.Test; import org.sql2o.reflection.PojoMetadata; -import com.google.common.collect.ImmutableMap; - -import junit.framework.TestCase; +import javax.persistence.Column; +import java.util.Collections; @SuppressWarnings("unused") public class ReadColumnAnnotationTest extends TestCase { @@ -53,7 +52,7 @@ public void testUppercaseAnnotationFieldPojo() { } private PojoMetadata newPojoMetadata(Class clazz) { - return new PojoMetadata(clazz, false, false, ImmutableMap. of(), true); + return new PojoMetadata(clazz, false, false, ImmutableMap. of(), true, Collections.emptySet()); } private static class NoAnnotation {