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

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

Go to download

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

There is a newer version: 9.0.2
Show newest version
package org.simpleflatmapper.jdbc.impl;

import org.simpleflatmapper.jdbc.Crud;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
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 MysqlCrudFactory {

    public static  Crud newInstance(ClassMeta target, ClassMeta  keyTarget, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory, DefaultCrud defaultCrud) throws SQLException {
        return new MultiRowsBatchInsertCrud(
                defaultCrud,
                buildBatchInsert(target, crudMeta, jdbcMapperFactory, false),
                buildBatchInsert(target, crudMeta, jdbcMapperFactory, true));

    }

    private static  BatchQueryExecutor buildBatchInsert(
            ClassMeta target,
            CrudMeta crudMeta,
            JdbcMapperFactory jdbcMapperFactory,
            boolean onDuplicateKeyUpdate) throws SQLException {

        List generatedKeys = new ArrayList();
        List insertColumns = new ArrayList();
        List updateColumns = new ArrayList();

        PreparedStatementMapperBuilder statementMapperBuilder = jdbcMapperFactory.from(target);
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            String columnName = cm.getColumn();
            if (cm.isGenerated()) {
                generatedKeys.add(columnName);
            } else {
                insertColumns.add(columnName);
                statementMapperBuilder.addColumn(columnName);
            }
            if (!cm.isKey()) {
                updateColumns.add(columnName);
            }
        }

        MysqlBatchInsertQueryExecutor queryExecutor = new MysqlBatchInsertQueryExecutor(
                crudMeta.getTable(),
                insertColumns.toArray(new String[0]),
                onDuplicateKeyUpdate ? updateColumns.toArray(new String[0]) : null,
                generatedKeys.toArray(new String[0]),
                statementMapperBuilder.buildIndexFieldMappers());

        return
                new SizeAdjusterBatchQueryExecutor(queryExecutor);
    }

    public static  QueryPreparer buildUpsert(ClassMeta target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) {
        List generatedKeys = new ArrayList();
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ");
        sb.append(crudMeta.getTable()).append("(");

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

        sb.append(") VALUES(");

        first = true;
        for(ColumnMeta cm : crudMeta.getColumnMetas()) {
            if (!cm.isGenerated()) {
                if (!first) {
                    sb.append(", ");
                }
                sb.append("?");
                first = false;
            }
        }
        sb.append(") ON DUPLICATE KEY UPDATE ");

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

        return jdbcMapperFactory.from(target).to(NamedSqlQuery.parse(sb), generatedKeys.toArray(new String[0]));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy