All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.db.helper.JdbcResultFactories Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package play.db.helper;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JdbcResultFactories {

    private JdbcResultFactories() {
    }


    public static  JdbcResultFactory build(Class objectClass) {
        return build(objectClass, (List)null);
    }

    public static  JdbcResultFactory build(Class objectClass, String ... fields) {
        return build(objectClass, Arrays.asList(fields));
    }

    public static  JdbcResultFactory build(Class objectClass, List fields) {
        return objectClass == Boolean.class
            || objectClass == Character.class
            || objectClass == Byte.class
            || objectClass == Short.class
            || objectClass == Integer.class
            || objectClass == Long.class
            || objectClass == Float.class
            || objectClass == Double.class
                ? new PrimitiveFactory(objectClass, fields)
                : new ClassFactory(objectClass, fields);
    }



    public static  JdbcResultFactory buildPrimitive(Class objectClass) {
        return buildPrimitive(objectClass, 1);
    }

    public static  JdbcResultFactory buildPrimitive(Class objectClass, int columnIndex) {
        return new PrimitiveFactory(objectClass, columnIndex);
    }

    public static  JdbcResultFactory buildPrimitive(Class objectClass, String field) {
        return new PrimitiveFactory(objectClass, field);
    }



    public static  JdbcResultFactory buildClass(Class objectClass) {
        return buildClass(objectClass, (List)null);
    }

    public static  JdbcResultFactory buildClass(Class objectClass, String ... fields) {
        return buildClass(objectClass, Arrays.asList(fields));
    }

    public static  JdbcResultFactory buildClass(Class objectClass, List fields) {
        return new ClassFactory(objectClass, fields);
    }



    public static class PrimitiveFactory implements JdbcResultFactory {

        private final Class objectClass;
        private final String field;
        private int columnIndex;

        public PrimitiveFactory(Class objectClass, int columnIndex) {
            this.objectClass = objectClass;
            this.field = null;
            this.columnIndex = columnIndex;
        }

        public PrimitiveFactory(Class objectClass, String field) {
            this.objectClass = objectClass;
            this.field = field;
            this.columnIndex = 1;
        }

        public PrimitiveFactory(Class objectClass, List fields) {
            this.objectClass = objectClass;
            this.field = fields == null || fields.isEmpty() ? null : fields.get(0);
            this.columnIndex = 1;
        }

        public void init(ResultSet result) throws SQLException {
            if (field != null) {
                ResultSetMetaData meta = result.getMetaData();
                int count = meta.getColumnCount();
                for (int i = 1; i <= count; i++) {
                    String label = meta.getColumnLabel(i);
                    if (label.equals(field)) {
                        columnIndex = i;
                        break;
                    }
                }
            }
        }

        @SuppressWarnings("unchecked")
        public T create(ResultSet result) throws SQLException {
            Object value = result.getObject(columnIndex);
            if (value instanceof BigDecimal) value = new Long(((BigDecimal)value).longValue());
            if (!objectClass.isInstance(value)) throw new IllegalArgumentException();
            return (T) value;
        }

    }

    public static class ClassFactory implements JdbcResultFactory {

        private final Class objectClass;
        private List fields;

        public ClassFactory(Class objectClass, List fields) {
            this.objectClass = objectClass;
            this.fields = fields;
        }

        public void init(ResultSet result) throws SQLException {
            if (fields == null) {
                fields = new ArrayList();
                ResultSetMetaData meta = result.getMetaData();
                int count = meta.getColumnCount();
                for (int i = 1; i <= count; i++) {
                    String label = meta.getColumnLabel(i);
                    if (label.length()>0) fields.add(label);
                }
            }
        }

        public T create(ResultSet result) throws SQLException {
            try {
                T obj = objectClass.newInstance();
                for (String field : fields) {
                    Object value = result.getObject(field);
                    if (value instanceof BigDecimal) value = new Long(((BigDecimal)value).longValue());
                    objectClass.getDeclaredField(field).set(obj, value);
                }
                return obj;
            } catch (InstantiationException ex) {
                throw new RuntimeException(ex);
            } catch (NoSuchFieldException ex) {
                throw new RuntimeException(ex);
            } catch (IllegalAccessException ex) {
                throw new RuntimeException(ex);
            }
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy