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

org.simpleflatmapper.jdbc.impl.CrudFactory Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

The newest version!
package org.simpleflatmapper.jdbc.impl;

import org.simpleflatmapper.converter.DefaultContextFactoryBuilder;
import org.simpleflatmapper.jdbc.Crud;
import org.simpleflatmapper.jdbc.JdbcMapper;
import org.simpleflatmapper.jdbc.JdbcMapperBuilder;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
import org.simpleflatmapper.jdbc.MultiIndexFieldMapper;
import org.simpleflatmapper.jdbc.PreparedStatementMapperBuilder;
import org.simpleflatmapper.jdbc.QueryPreparer;
import org.simpleflatmapper.jdbc.named.NamedSqlQuery;
import org.simpleflatmapper.reflect.meta.ClassMeta;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class CrudFactory {
    public static  Crud newInstance(
            ClassMeta target,
            ClassMeta  keyTarget,
            CrudMeta crudMeta,
            JdbcMapperFactory jdbcMapperFactory) throws SQLException {
        JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance(jdbcMapperFactory);
        return createCrud(target, keyTarget, crudMeta, mapperFactory);

    }

    private static  Crud createCrud(ClassMeta target, ClassMeta keyTarget, CrudMeta crudMeta, JdbcMapperFactory mapperFactory) throws SQLException {
        crudMeta.addColumnProperties(mapperFactory);

        QueryPreparer insert = buildInsert(target, crudMeta, mapperFactory);
        QueryPreparer update = buildUpdate(target, crudMeta, mapperFactory);
        QueryPreparer select = buildSelect(keyTarget, crudMeta, mapperFactory);
        QueryPreparer delete = buildDelete(keyTarget, crudMeta, mapperFactory);
        QueryPreparer upsert = buildUpsert(target, crudMeta, mapperFactory);

        KeyTupleQueryPreparer keyTupleQueryPreparer = buildKeyTupleQueryPreparer(keyTarget, crudMeta, mapperFactory);

        JdbcMapper selectMapper = buildSelectMapper(target, crudMeta, mapperFactory);
        JdbcMapper keyMapper = buildKeyMapper(keyTarget, crudMeta, mapperFactory);

        boolean hasGeneratedKeys = crudMeta.hasGeneratedKeys();

        DefaultCrud defaultCrud = new DefaultCrud(
                insert,
                update,
                select,
                upsert,
                keyTupleQueryPreparer,
                selectMapper,
                delete,
                keyMapper,
                crudMeta,
                hasGeneratedKeys,
                new SelectQueryWhereFactory(crudMeta, selectMapper, mapperFactory));

        if (crudMeta.getDatabaseMeta().isMysql()) {
            return MysqlCrudFactory.newInstance(target, keyTarget, crudMeta, mapperFactory, defaultCrud);
        } else if (crudMeta.getDatabaseMeta().isPostgresSql()) {
            return PostgresqlCrudFactory.newInstance(target, keyTarget, crudMeta, mapperFactory, defaultCrud);
        }

        return defaultCrud;
    }

    private static  QueryPreparer buildUpsert(ClassMeta  target, CrudMeta crudMeta, JdbcMapperFactory mapperFactory) {
        if (crudMeta.getDatabaseMeta().isMysql()) {
            return MysqlCrudFactory.buildUpsert(target, crudMeta, mapperFactory);
        } else if (crudMeta.getDatabaseMeta().isPostgresSql() && crudMeta.getDatabaseMeta().isVersionMet(9, 5)) {
            return PostgresqlCrudFactory.buildUpsert(target, crudMeta, mapperFactory);
        }
        return new UnsupportedQueryPreparer("Upsert Not Supported on " + crudMeta.getDatabaseMeta());
    }


    private static  KeyTupleQueryPreparer buildKeyTupleQueryPreparer(ClassMeta  keyTarget, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) {
        PreparedStatementMapperBuilder builder = jdbcMapperFactory.from(keyTarget);
        List primaryKeys = new ArrayList();

        int i = 1;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (cm.isKey()) {
                primaryKeys.add(cm.getColumn());
                builder.addColumn(cm.toJdbcColumnKey(i));
                i++;
            }
        }
        DefaultContextFactoryBuilder contextFactoryBuilder = new DefaultContextFactoryBuilder();
        MultiIndexFieldMapper[] multiIndexFieldMappers = builder.buildIndexFieldMappers(contextFactoryBuilder);
        return new KeyTupleQueryPreparer(multiIndexFieldMappers, contextFactoryBuilder.build(), primaryKeys.toArray(new String[0]));
    }

    private static JdbcMapper buildKeyMapper(ClassMeta  keyTarget, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) {
        JdbcMapperBuilder mapperBuilder = jdbcMapperFactory.newBuilder(keyTarget);

        int i = 1;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (cm.isKey()) {
                mapperBuilder.addMapping(cm.toJdbcColumnKey(i));
                i++;
            }
        }
        if (i == 1) {
            throw new IllegalArgumentException("No key defined to map to " + keyTarget.getType() + ", specify key using DSL or add a primary key to the table");
        }
        return mapperBuilder.mapper();
    }

    private static  JdbcMapper buildSelectMapper(ClassMeta  target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) throws SQLException {
        JdbcMapperBuilder mapperBuilder = jdbcMapperFactory.newBuilder(target);

        int i = 1;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            mapperBuilder.addMapping(cm.toJdbcColumnKey(i));
            i++;
        }
        return mapperBuilder.mapper();
    }

    private static  QueryPreparer buildInsert(ClassMeta target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) throws SQLException {
        List generatedKeys = new ArrayList();

        StringBuilder sb = new StringBuilder("INSERT INTO ");
        appendTableName(sb, crudMeta);
        sb.append("(");
        boolean first = true;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (cm.isInsertable()) {
                if (!first) {
                    sb.append(", ");
                }
                crudMeta.appendProtectedField(sb, cm.getColumn());
                first = false;
            } 
            
            if (cm.isGenerated()) {
                generatedKeys.add(cm.getColumn());
            }
        }
        sb.append(") VALUES(");
        first = true;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (cm.isInsertable()) {
                if (!first) {
                    sb.append(", ");
                }
                sb.append(cm.getInsertExpression());
                first = false;
            }
        }
        sb.append(")");
        return jdbcMapperFactory.from(target).to(NamedSqlQuery.parse(sb), generatedKeys.isEmpty() ? null :  generatedKeys.toArray(new String[0]));
    }

    private static  QueryPreparer buildUpdate(ClassMeta target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) throws SQLException {
        StringBuilder sb = new StringBuilder("UPDATE ");
        appendTableName(sb, crudMeta);
        sb.append(" SET ");
        boolean first = true;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            String columnName = cm.getColumn();
            if (!cm.isKey()) {
                if (!first) {
                    sb.append(", ");
                }
                crudMeta.appendProtectedField(sb, columnName);
                sb.append(" = ?");
                first = false;
            }
        }
        if (first) {
            // no field to update
            return null;
        }
        addWhereOnPrimaryKeys(crudMeta, sb);
        return jdbcMapperFactory.from(target).to(NamedSqlQuery.parse(sb));
    }

    private static  QueryPreparer buildSelect(ClassMeta keyTarget, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) throws SQLException {
        StringBuilder sb = new StringBuilder("SELECT ");

        boolean first = true;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (!first) {
                sb.append(", ");
            }
            crudMeta.appendProtectedField(sb, cm.getColumn());
            first = false;
        }

        sb.append(" FROM ");
        appendTableName(sb, crudMeta);
        addWhereOnPrimaryKeys(crudMeta, sb);
        return jdbcMapperFactory.from(keyTarget).to(NamedSqlQuery.parse(sb));
    }

    private static void appendTableName(StringBuilder sb, CrudMeta crudMeta) {
        crudMeta.appendTableName(sb);
    }

    private static  QueryPreparer buildDelete(ClassMeta keyTarget, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) throws SQLException {
        StringBuilder sb = new StringBuilder("DELETE FROM ");
        appendTableName(sb, crudMeta);
        addWhereOnPrimaryKeys(crudMeta, sb);
        return jdbcMapperFactory.from(keyTarget).to(NamedSqlQuery.parse(sb));
    }

    private static  void addWhereOnPrimaryKeys(CrudMeta crudMeta, StringBuilder sb) {
        sb.append(" WHERE ");
        boolean first = true;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (cm.isKey()) {
                if (!first) {
                    sb.append("AND ");
                }
                crudMeta.appendProtectedField(sb, cm.getColumn());
                sb.append(" = ? ");
                first = false;
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy