use of org.sql2o.reflection.Getter in project sql2o by aaberg.
the class AbstractFieldGetterFactoryTest method testAllTypes.
public void testAllTypes() throws IllegalAccessException {
POJO1 pojo = new POJO1();
pojo._boolean = true;
pojo._byte = 17;
pojo._short = 87;
pojo._int = Integer.MIN_VALUE;
pojo._long = 1337;
pojo._char = 'a';
pojo._double = Math.PI;
pojo._float = (float) Math.log(93);
pojo._obj = pojo;
Field[] fields = pojo.getClass().getDeclaredFields();
for (Field field : fields) {
Getter getter = fgf.newGetter(field);
assertSame(field.getType(), getter.getType());
Object val1 = field.get(pojo);
assertEquals(val1, getter.getProperty(pojo));
}
}
use of org.sql2o.reflection.Getter in project sql2o by aaberg.
the class AbstractMethodGetterFactoryTest method testAllTypes.
public void testAllTypes() throws IllegalAccessException, NoSuchFieldException {
POJO1 pojo = new POJO1();
pojo._boolean = true;
pojo._byte = 17;
pojo._short = 87;
pojo._int = Integer.MIN_VALUE;
pojo._long = 1337;
pojo._char = 'a';
pojo._double = Math.PI;
pojo._float = (float) Math.log(93);
pojo._obj = pojo;
Method[] methods = pojo.getClass().getDeclaredMethods();
for (Method method : methods) {
if (!method.getName().startsWith("get_"))
continue;
Field field = pojo.getClass().getDeclaredField(method.getName().substring(3));
Getter getter = mgf.newGetter(method);
assertSame(field.getType(), getter.getType());
Object val1 = field.get(pojo);
assertEquals(val1, getter.getProperty(pojo));
}
}
use of org.sql2o.reflection.Getter in project sql2o by aaberg.
the class DefaultResultSetHandlerFactory method getGetter.
@SuppressWarnings("unchecked")
private static Getter getGetter(final Quirks quirks, final String propertyPath, final PojoMetadata metadata) {
int index = propertyPath.indexOf('.');
if (index <= 0) {
// Simple path - fast way
final Getter getter = metadata.getPropertyGetterIfExists(propertyPath);
// behavior change: do not throw if POJO contains less properties
if (getter == null)
return null;
final Converter converter = quirks.converterOf(getter.getType());
// getter without converter
if (converter == null)
return getter;
return new Getter() {
public Object getProperty(Object obj) {
try {
return converter.convert(getter.getProperty(obj));
} catch (ConverterException e) {
throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + getter.getType(), e);
}
}
public Class getType() {
return getter.getType();
}
};
}
// TODO: rewrite, get rid of POJO class
return new Getter() {
public Object getProperty(Object obj) {
Pojo pojo = new Pojo(metadata, metadata.isCaseSensitive(), obj);
return pojo.getProperty(propertyPath, quirks);
}
public Class getType() {
// doesn't used anyway
return Object.class;
}
};
}
use of org.sql2o.reflection.Getter in project sql2o by aaberg.
the class DefaultResultSetHandlerFactory method newResultSetHandler0.
@SuppressWarnings("unchecked")
private ResultSetHandler<T> newResultSetHandler0(final ResultSetMetaData meta) throws SQLException {
final Getter[] getters;
final Setter[] setters;
final Converter converter;
final boolean useExecuteScalar;
// TODO: it's possible to cache converter/setters/getters
// cache key is ResultSetMetadata + Bean type
converter = quirks.converterOf(metadata.getType());
final int columnCount = meta.getColumnCount();
// getters[0] is always null
getters = new Getter[columnCount + 1];
for (int i = 1; i <= columnCount; i++) {
String colName = quirks.getColumnName(meta, i);
// behavior change: do not throw if POJO contains less properties
getters[i] = getGetter(quirks, colName, metadata);
// and the getter doesn't exist, throw exception.
if (this.metadata.throwOnMappingFailure && getters[i] == null && columnCount > 1) {
throw new Sql2oException("Could not map " + colName + " to any property.");
}
}
// setters[0] is always null
setters = new Setter[columnCount + 1];
for (int i = 1; i <= columnCount; i++) {
String colName = quirks.getColumnName(meta, i);
setters[i] = getSetter(quirks, colName, metadata);
// and the setter doesn't exist, throw exception.
if (this.metadata.throwOnMappingFailure && setters[i] == null && columnCount > 1) {
throw new Sql2oException("Could not map " + colName + " to any property.");
}
}
/**
* Fallback to executeScalar if converter exists,
* we're selecting 1 column, and no property setter exists for the column.
*/
useExecuteScalar = converter != null && columnCount == 1 && setters[1] == null;
return new ResultSetHandler<T>() {
@SuppressWarnings("unchecked")
public T handle(ResultSet resultSet) throws SQLException {
if (useExecuteScalar) {
try {
return (T) converter.convert(quirks.getRSVal(resultSet, 1));
} catch (ConverterException e) {
throw new Sql2oException("Error occurred while converting value from database to type " + metadata.getType(), e);
}
}
// otherwise we want executeAndFetch with object mapping
Object pojo = metadata.getObjectConstructor().newInstance();
for (int colIdx = 1; colIdx <= columnCount; colIdx++) {
Setter setter = setters[colIdx];
if (setter == null)
continue;
setter.setProperty(pojo, quirks.getRSVal(resultSet, colIdx));
}
return (T) pojo;
}
};
}
Aggregations