Package org.sql2o.converters

Examples of org.sql2o.converters.Converter


    @SuppressWarnings("unchecked")
    private Object convertParameter(Object value) {
        if (value == null) {
            return null;
        }
        Converter converter = getQuirks().converterOf(value.getClass());
        if (converter == null) {
            // let's try to add parameter AS IS
            return value;
        }
        return converter.toDatabaseParam(value);
    }
View Full Code Here


    @SuppressWarnings("unchecked")
    public <E> Converter<E> converterOf(Class<E> ofClass) {
        // if nobody change this collection outside constructor
        // it's thread-safe
        Converter c =  converters.get(ofClass);
        // if no "local" converter let's look in global
        return c!=null?c:Convert.getConverterIfExists(ofClass);

    }
View Full Code Here

            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);
            }
        }
    }
View Full Code Here

            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);
            }
        }
       
View Full Code Here

        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);
                    }
                }
View Full Code Here

        if (index <= 0) {
            // Simple path - fast way
            final Setter setter = metadata.getPropertySetterIfExists(propertyPath);
            // behavior change: do not throw if POJO contains less properties
            if (setter == null) return null;
            final Converter converter = quirks.converterOf(setter.getType());
            // setter without converter
            if (converter == null) return setter;
            return new Setter() {
                public void setProperty(Object obj, Object value) {
                    try {
                        setter.setProperty(obj, converter.convert(value));
                    } catch (ConverterException e) {
                        throw new Sql2oException("Error trying to convert column " + propertyPath + " to type " + setter.getType(), e);
                    }
                }
View Full Code Here

    @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 = new Getter[columnCount + 1];   // getters[0] is always null
        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);

            // If more than 1 column is fetched (we cannot fall back to executeScalar),
            // and the getter doesn't exist, throw exception.
            if (getters[i] == null && columnCount > 1) {
                throw new Sql2oException("Could not map " + colName + " to any property.");
            }
        }

        setters = new Setter[columnCount + 1];   // setters[0] is always null
        for (int i = 1; i <= columnCount; i++) {
            String colName = quirks.getColumnName(meta, i);
            // behavior change: do not throw if POJO contains less properties
            setters[i] = getSetter(quirks, colName, metadata);

            // If more than 1 column is fetched (we cannot fall back to executeScalar),
            // and the setter doesn't exist, throw exception.
            if (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);
                    }
                }

View Full Code Here

TOP

Related Classes of org.sql2o.converters.Converter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.