org.simpleflatmapper.jdbc.impl.MysqlCrudFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-jdbc Show documentation
Show all versions of sfm-jdbc Show documentation
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
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) {
List generatedKeys = new ArrayList();
List insertColumns = new ArrayList();
List insertColumnExpressions = 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);
}
if (cm.isInsertable()){
insertColumns.add(columnName);
if (!cm.isGenerated()) {
statementMapperBuilder.addColumn(columnName);
}
insertColumnExpressions.add(cm.getInsertExpression());
}
if (!cm.isKey()) {
updateColumns.add(columnName);
}
}
MysqlBatchInsertQueryExecutor queryExecutor = new MysqlBatchInsertQueryExecutor(
crudMeta,
insertColumns.toArray(new String[0]),
insertColumnExpressions.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 ");
if (crudMeta.hasNoUpdatableFields()) {
sb.append("IGNORE ");
}
sb.append("INTO ");
crudMeta.appendTableName(sb);
sb.append("(");
boolean first = true;
for(ColumnMeta cm : crudMeta.getColumnMetas()) {
if (cm.isGenerated()) {
generatedKeys.add(cm.getColumn());
}
if (cm.isInsertable()) {
if (!first) {
sb.append(", ");
}
sb.append(cm.getColumn());
first = false;
}
}
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(") ");
if (!crudMeta.hasNoUpdatableFields()) {
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]));
}
}