use of org.sql2o.converters.ConverterException in project sql2o by aaberg.
the class Pojo method setProperty.
@SuppressWarnings("unchecked")
public void setProperty(String propertyPath, Object value, Quirks quirks) {
// String.split uses RegularExpression
// this is overkill for every column for every row
int index = propertyPath.indexOf('.');
Setter setter;
if (index > 0) {
final String substring = propertyPath.substring(0, index);
setter = metadata.getPropertySetter(substring);
String newPath = propertyPath.substring(index + 1);
Object subValue = this.metadata.getValueOfProperty(substring, this.object);
if (subValue == null) {
try {
subValue = setter.getType().newInstance();
} catch (InstantiationException e) {
throw new Sql2oException("Could not instantiate a new instance of class " + setter.getType().toString(), e);
} catch (IllegalAccessException e) {
throw new Sql2oException("Could not instantiate a new instance of class " + setter.getType().toString(), e);
}
setter.setProperty(this.object, subValue);
}
PojoMetadata subMetadata = new PojoMetadata(setter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure);
Pojo subPojo = new Pojo(subMetadata, this.caseSensitive, subValue);
subPojo.setProperty(newPath, value, quirks);
} else {
setter = metadata.getPropertySetter(propertyPath);
Converter converter;
try {
converter = throwIfNull(setter.getType(), quirks.converterOf(setter.getType()));
} catch (ConverterException e) {
throw new Sql2oException("Cannot convert column " + propertyPath + " to type " + setter.getType(), e);
}
try {
setter.setProperty(this.object, converter.convert(value));
} catch (ConverterException e) {
throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + setter.getType(), e);
}
}
}
use of org.sql2o.converters.ConverterException in project sql2o by aaberg.
the class Connection method getKeys.
// need to change Convert
@SuppressWarnings("unchecked")
public <V> List<V> getKeys(Class<V> returnType) {
final Quirks quirks = sql2o.getQuirks();
if (!this.canGetKeys) {
throw new Sql2oException("Keys where not fetched from database. Please set the returnGeneratedKeys parameter in the createQuery() method to enable fetching of generated keys.");
}
if (this.keys != null) {
try {
Converter<V> converter = throwIfNull(returnType, quirks.converterOf(returnType));
List<V> convertedKeys = new ArrayList<V>(this.keys.size());
for (Object key : this.keys) {
convertedKeys.add(converter.convert(key));
}
return convertedKeys;
} catch (ConverterException e) {
throw new Sql2oException("Exception occurred while converting value from database to type " + returnType.toString(), e);
}
}
return null;
}
use of org.sql2o.converters.ConverterException 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.converters.ConverterException in project sql2o by aaberg.
the class Pojo method getProperty.
@SuppressWarnings("unchecked")
public Object getProperty(String propertyPath, Quirks quirks) {
// String.split uses RegularExpression
// this is overkill for every column for every row
int index = propertyPath.indexOf('.');
Getter getter;
if (index > 0) {
final String substring = propertyPath.substring(0, index);
getter = metadata.getPropertyGetter(substring);
String newPath = propertyPath.substring(index + 1);
Object subValue = this.metadata.getValueOfProperty(substring, this.object);
if (subValue == null) {
try {
subValue = getter.getType().newInstance();
} catch (InstantiationException e) {
throw new Sql2oException("Could not instantiate a new instance of class " + getter.getType().toString(), e);
} catch (IllegalAccessException e) {
throw new Sql2oException("Could not instantiate a new instance of class " + getter.getType().toString(), e);
}
return getter.getProperty(this.object);
}
PojoMetadata subMetadata = new PojoMetadata(getter.getType(), this.caseSensitive, this.metadata.isAutoDeriveColumnNames(), this.metadata.getColumnMappings(), this.metadata.throwOnMappingFailure);
Pojo subPojo = new Pojo(subMetadata, this.caseSensitive, subValue);
return subPojo.getProperty(newPath, quirks);
} else {
getter = metadata.getPropertyGetter(propertyPath);
Converter converter;
try {
converter = throwIfNull(getter.getType(), quirks.converterOf(getter.getType()));
} catch (ConverterException e) {
throw new Sql2oException("Cannot convert column " + propertyPath + " to type " + getter.getType(), e);
}
try {
return converter.convert(getter.getProperty(this.object));
} catch (ConverterException e) {
throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + getter.getType(), e);
}
}
}
use of org.sql2o.converters.ConverterException in project sql2o by aaberg.
the class Connection method getKey.
// need to change Convert
@SuppressWarnings("unchecked")
public <V> V getKey(Class returnType) {
final Quirks quirks = this.sql2o.getQuirks();
Object key = getKey();
try {
Converter<V> converter = throwIfNull(returnType, quirks.converterOf(returnType));
return converter.convert(key);
} catch (ConverterException e) {
throw new Sql2oException("Exception occurred while converting value from database to type " + returnType.toString(), e);
}
}
Aggregations