Search in sources :

Example 1 with Converter

use of org.sql2o.converters.Converter 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);
        }
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) ConverterException(org.sql2o.converters.ConverterException) Converter(org.sql2o.converters.Converter)

Example 2 with Converter

use of org.sql2o.converters.Converter 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;
        }
    };
}
Also used : ConverterException(org.sql2o.converters.ConverterException) Pojo(org.sql2o.reflection.Pojo) Getter(org.sql2o.reflection.Getter) Converter(org.sql2o.converters.Converter)

Example 3 with Converter

use of org.sql2o.converters.Converter in project runelite by runelite.

the class SpringBootWebApplication method cacheSql2o.

@Bean("Runelite Cache SQL2O")
Sql2o cacheSql2o() throws NamingException {
    DataSource dataSource = (DataSource) getContext().lookup("jdbc/runelite-cache2");
    Map<Class, Converter> converters = new HashMap<>();
    converters.put(Instant.class, new InstantConverter());
    return new Sql2o(dataSource, new NoQuirks(converters));
}
Also used : HashMap(java.util.HashMap) InstantConverter(net.runelite.http.service.util.InstantConverter) Converter(org.sql2o.converters.Converter) InstantConverter(net.runelite.http.service.util.InstantConverter) NoQuirks(org.sql2o.quirks.NoQuirks) Sql2o(org.sql2o.Sql2o) DataSource(javax.sql.DataSource) Bean(org.springframework.context.annotation.Bean)

Example 4 with Converter

use of org.sql2o.converters.Converter in project runelite by runelite.

the class SpringBootWebApplication method trackerSql2o.

@Bean("Runelite XP Tracker SQL2O")
Sql2o trackerSql2o() throws NamingException {
    DataSource dataSource = (DataSource) getContext().lookup("jdbc/runelite-tracker");
    Map<Class, Converter> converters = new HashMap<>();
    converters.put(Instant.class, new InstantConverter());
    return new Sql2o(dataSource, new NoQuirks(converters));
}
Also used : HashMap(java.util.HashMap) InstantConverter(net.runelite.http.service.util.InstantConverter) Converter(org.sql2o.converters.Converter) InstantConverter(net.runelite.http.service.util.InstantConverter) NoQuirks(org.sql2o.quirks.NoQuirks) Sql2o(org.sql2o.Sql2o) DataSource(javax.sql.DataSource) Bean(org.springframework.context.annotation.Bean)

Example 5 with Converter

use of org.sql2o.converters.Converter 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);
        }
    }
}
Also used : Sql2oException(org.sql2o.Sql2oException) ConverterException(org.sql2o.converters.ConverterException) Converter(org.sql2o.converters.Converter)

Aggregations

Converter (org.sql2o.converters.Converter)12 HashMap (java.util.HashMap)7 Bean (org.springframework.context.annotation.Bean)7 Sql2o (org.sql2o.Sql2o)7 NoQuirks (org.sql2o.quirks.NoQuirks)7 InstantConverter (net.runelite.http.service.util.InstantConverter)6 ConverterException (org.sql2o.converters.ConverterException)5 DataSource (javax.sql.DataSource)3 Sql2oException (org.sql2o.Sql2oException)2 Getter (org.sql2o.reflection.Getter)2 Pojo (org.sql2o.reflection.Pojo)2 Setter (org.sql2o.reflection.Setter)2 ResultSet (java.sql.ResultSet)1 Qualifier (org.springframework.beans.factory.annotation.Qualifier)1