All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.simpleflatmapper.jdbc.impl.PostgresqlCrudFactory Maven / Gradle / Ivy
package org.simpleflatmapper.jdbc.impl;
import org.simpleflatmapper.converter.DefaultContextFactoryBuilder;
import org.simpleflatmapper.jdbc.Crud;
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 PostgresqlCrudFactory {
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 insertColumnExpressions = new ArrayList();
List updateColumns = new ArrayList();
List keys = 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);
insertColumnExpressions.add(cm.getInsertExpression());
if (!cm.isGenerated()) {
statementMapperBuilder.addColumn(columnName);
}
}
if (!cm.isKey()) {
updateColumns.add(columnName);
} else {
keys.add(columnName);
}
}
DefaultContextFactoryBuilder defaultContextFactoryBuilder = new DefaultContextFactoryBuilder();
MultiIndexFieldMapper[] multiIndexFieldMappers = statementMapperBuilder.buildIndexFieldMappers(defaultContextFactoryBuilder);
PostgresqlBatchInsertQueryExecutor queryExecutor = new PostgresqlBatchInsertQueryExecutor(
crudMeta,
insertColumns.toArray(new String[0]),
insertColumnExpressions.toArray(new String[0]),
onDuplicateKeyUpdate ? updateColumns.toArray(new String[0]) : null,
generatedKeys.toArray(new String[0]),
keys.toArray(new String[0]),
multiIndexFieldMappers,
defaultContextFactoryBuilder.build());
return queryExecutor;
}
public static QueryPreparer buildUpsert(ClassMeta target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) {
List generatedKeys = new ArrayList();
StringBuilder sb = new StringBuilder();
sb.append("INSERT 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(", ");
}
crudMeta.appendProtectedField(sb, 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(") ON CONFLICT (");
first = true;
for(ColumnMeta cm : crudMeta.getColumnMetas()) {
if (cm.isKey()) {
if (!first) {
sb.append(", ");
}
crudMeta.appendProtectedField(sb, cm.getColumn());
first = false;
}
}
sb.append(") DO ");
if (crudMeta.hasNoUpdatableFields()) {
sb.append("NOTHING");
} else {
sb.append("UPDATE SET ");
first = true;
for (ColumnMeta cm : crudMeta.getColumnMetas()) {
if (!cm.isKey()) {
if (!first) {
sb.append(", ");
}
crudMeta.appendProtectedField(sb, cm.getColumn());
sb.append(" = EXCLUDED.");
crudMeta.appendProtectedField(sb, cm.getColumn());
first = false;
}
}
}
return jdbcMapperFactory.from(target).to(NamedSqlQuery.parse(sb), generatedKeys.toArray(new String[0]));
}
}