Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Query.ignoreColumn() #273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ private ResultSetHandler<T> 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);

Expand All @@ -203,7 +207,9 @@ private ResultSetHandler<T> 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import org.sql2o.reflection.PojoMetadata;

import java.util.Map;
import java.util.Set;

public class DefaultResultSetHandlerFactoryBuilder implements ResultSetHandlerFactoryBuilder {
private boolean caseSensitive;
private boolean autoDeriveColumnNames;
private boolean throwOnMappingError;
private Map<String, String> columnMappings;
private Quirks quirks;
private Set<String> ignoredColumns;

@Override
public void setIgnoredColumns( Set<String> ignoredColumns ) {
this.ignoredColumns = ignoredColumns;
}

public boolean isCaseSensitive() {
return caseSensitive;
Expand Down Expand Up @@ -58,7 +65,7 @@ public void setQuirks(Quirks quirks) {

@SuppressWarnings("unchecked")
public <T> ResultSetHandlerFactory<T> newFactory(Class<T> 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);
}

Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/sql2o/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Query implements AutoCloseable {
private final static Logger logger = LocalLoggerFactory.getLogger(Query.class);

private Connection connection;
private Set<String> ignoredColumns = new HashSet<>();
private Map<String, String> caseSensitiveColumnMappings;
private Map<String, String> columnMappings;
private PreparedStatement preparedStatement = null;
Expand Down Expand Up @@ -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.<br>
* For example:
Expand Down Expand Up @@ -529,6 +535,7 @@ private <T> ResultSetHandlerFactory<T> newResultSetHandlerFactory(Class<T> retur
builder.setColumnMappings(this.getColumnMappings());
builder.setQuirks(quirks);
builder.throwOnMappingError(this.throwOnMappingFailure);
builder.setIgnoredColumns( ignoredColumns );
return builder.newFactory(returnType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.sql2o.quirks.Quirks;

import java.util.Map;
import java.util.Set;

/**
* Created with IntelliJ IDEA.
Expand Down Expand Up @@ -33,4 +34,6 @@ public interface ResultSetHandlerFactoryBuilder {
void setQuirks(Quirks quirksMode);

<E> ResultSetHandlerFactory<E> newFactory(Class<E> clazz);

void setIgnoredColumns( Set<String> ignoredColumns );
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/sql2o/reflection/Pojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 14 additions & 7 deletions core/src/main/java/org/sql2o/reflection/PojoMetadata.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -29,6 +29,8 @@ public class PojoMetadata {
public final boolean throwOnMappingFailure;
private Class clazz;

private final Set<String> ignoredColumns;

public boolean isCaseSensitive() {
return caseSensitive;
}
Expand Down Expand Up @@ -59,11 +61,12 @@ public int hashCode() {
return result;
}

public PojoMetadata(Class clazz, boolean caseSensitive, boolean autoDeriveColumnNames, Map<String, String> columnMappings, boolean throwOnMappingError) {
public PojoMetadata(Class clazz, boolean caseSensitive, boolean autoDeriveColumnNames, Map<String, String> columnMappings, boolean throwOnMappingError, Set<String> ignoredColumns ) {
this.caseSensitive = caseSensitive;
this.autoDeriveColumnNames = autoDeriveColumnNames;
this.clazz = clazz;
this.columnMappings = columnMappings == null ? Collections.<String,String>emptyMap() : columnMappings;
this.ignoredColumns = ignoredColumns == null ? Collections.<String>emptySet() : ignoredColumns;

this.propertyInfo = getPropertyInfoThroughCache();
this.throwOnMappingFailure = throwOnMappingError;
Expand All @@ -74,6 +77,10 @@ public ObjectConstructor getObjectConstructor() {
return propertyInfo.objectConstructor;
}

public Set<String> getIgnoredColumns() {
return ignoredColumns;
}

private PropertyAndFieldInfo getPropertyInfoThroughCache() {
return (caseSensitive
? caseSensitiveTrue
Expand Down
20 changes: 19 additions & 1 deletion core/src/test/java/org/sql2o/issues/IssuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -53,7 +52,7 @@ public void testUppercaseAnnotationFieldPojo() {
}

private PojoMetadata newPojoMetadata(Class<?> clazz) {
return new PojoMetadata(clazz, false, false, ImmutableMap.<String, String> of(), true);
return new PojoMetadata(clazz, false, false, ImmutableMap.<String, String> of(), true, Collections.<String>emptySet());
}

private static class NoAnnotation {
Expand Down