use of org.simpleflatmapper.jdbc.MultiIndexFieldMapper in project SimpleFlatMapper by arnaudroger.
the class CrudFactory method buildKeyTupleQueryPreparer.
private static <T, K> KeyTupleQueryPreparer<K> buildKeyTupleQueryPreparer(ClassMeta<K> keyTarget, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) {
PreparedStatementMapperBuilder<K> builder = jdbcMapperFactory.from(keyTarget);
List<String> primaryKeys = new ArrayList<String>();
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<K>[] multiIndexFieldMappers = builder.buildIndexFieldMappers(contextFactoryBuilder);
return new KeyTupleQueryPreparer<K>(multiIndexFieldMappers, contextFactoryBuilder.build(), primaryKeys.toArray(new String[0]));
}
use of org.simpleflatmapper.jdbc.MultiIndexFieldMapper in project SimpleFlatMapper by arnaudroger.
the class MysqlCrudFactory method buildBatchInsert.
private static <T, K> BatchQueryExecutor<T> buildBatchInsert(ClassMeta<T> target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory, boolean onDuplicateKeyUpdate) {
List<String> generatedKeys = new ArrayList<String>();
List<String> insertColumns = new ArrayList<String>();
List<String> insertColumnExpressions = new ArrayList<String>();
List<String> updateColumns = new ArrayList<String>();
PreparedStatementMapperBuilder<T> statementMapperBuilder = jdbcMapperFactory.<T>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);
}
}
DefaultContextFactoryBuilder builder = new DefaultContextFactoryBuilder();
MultiIndexFieldMapper<T>[] multiIndexFieldMappers = statementMapperBuilder.buildIndexFieldMappers(builder);
MysqlBatchInsertQueryExecutor<T> queryExecutor = new MysqlBatchInsertQueryExecutor<T>(crudMeta, insertColumns.toArray(new String[0]), insertColumnExpressions.toArray(new String[0]), onDuplicateKeyUpdate ? updateColumns.toArray(new String[0]) : null, generatedKeys.toArray(new String[0]), multiIndexFieldMappers, builder.build());
return new SizeAdjusterBatchQueryExecutor<T>(queryExecutor);
}
use of org.simpleflatmapper.jdbc.MultiIndexFieldMapper in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudFactory method buildBatchInsert.
private static <T, K> BatchQueryExecutor<T> buildBatchInsert(ClassMeta<T> target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory, boolean onDuplicateKeyUpdate) throws SQLException {
List<String> generatedKeys = new ArrayList<String>();
List<String> insertColumns = new ArrayList<String>();
List<String> insertColumnExpressions = new ArrayList<String>();
List<String> updateColumns = new ArrayList<String>();
List<String> keys = new ArrayList<String>();
PreparedStatementMapperBuilder<T> statementMapperBuilder = jdbcMapperFactory.<T>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<T>[] multiIndexFieldMappers = statementMapperBuilder.buildIndexFieldMappers(defaultContextFactoryBuilder);
PostgresqlBatchInsertQueryExecutor<T> queryExecutor = new PostgresqlBatchInsertQueryExecutor<T>(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;
}
use of org.simpleflatmapper.jdbc.MultiIndexFieldMapper in project SimpleFlatMapper by arnaudroger.
the class PsSetterNotFoundTest method jdbcMapperExtrapolateGetterFromConstructor.
@Test
public void jdbcMapperExtrapolateGetterFromConstructor() throws Exception {
final MultiIndexFieldMapper<Foo>[] fieldMappers = JdbcMapperFactory.newInstance().buildFrom(Foo.class).addColumn("bar").buildIndexFieldMappers(EmptyContextFactoryBuilder.INSTANCE);
assertEquals(1, fieldMappers.length);
PreparedStatement ps = mock(PreparedStatement.class);
fieldMappers[0].map(ps, new Foo(new BarOneProp("val")), 0, EmptyContext.INSTANCE);
verify(ps).setString(1, "val");
JdbcMapperFactory.newInstance().buildFrom(Crux.class).addColumn("foo").buildIndexFieldMappers(EmptyContextFactoryBuilder.INSTANCE);
}
Aggregations