Search in sources :

Example 1 with DefaultContextFactoryBuilder

use of org.simpleflatmapper.converter.DefaultContextFactoryBuilder 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]));
}
Also used : MultiIndexFieldMapper(org.simpleflatmapper.jdbc.MultiIndexFieldMapper) ArrayList(java.util.ArrayList) DefaultContextFactoryBuilder(org.simpleflatmapper.converter.DefaultContextFactoryBuilder)

Example 2 with DefaultContextFactoryBuilder

use of org.simpleflatmapper.converter.DefaultContextFactoryBuilder 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);
}
Also used : MultiIndexFieldMapper(org.simpleflatmapper.jdbc.MultiIndexFieldMapper) ArrayList(java.util.ArrayList) DefaultContextFactoryBuilder(org.simpleflatmapper.converter.DefaultContextFactoryBuilder)

Example 3 with DefaultContextFactoryBuilder

use of org.simpleflatmapper.converter.DefaultContextFactoryBuilder in project SimpleFlatMapper by arnaudroger.

the class SqlParameterSourceBuilder method build.

private <P> PlaceHolderValueGetter<T> build(JdbcColumnKey key, PropertyMapping<T, P, JdbcColumnKey> pm) {
    if (pm != null) {
        int parameterType = getParameterType(pm);
        ContextualGetter<T, ? extends P> getter = ContextualGetterAdapter.of(pm.getPropertyMeta().getGetter());
        // need conversion ?
        final DefaultContextFactoryBuilder contextFactoryBuilder = new DefaultContextFactoryBuilder();
        Type propertyType = pm.getPropertyMeta().getPropertyType();
        Class<?> sqlType = JdbcTypeHelper.toJavaType(parameterType, propertyType);
        boolean findConverter = false;
        for (ConverterProperty cp : pm.getColumnDefinition().lookForAll(ConverterProperty.class)) {
            if (TypeHelper.isAssignable(cp.inType, propertyType)) {
                getter = new FieldMapperGetterWithConverter(cp.function, getter);
                findConverter = true;
                break;
            }
        }
        if (!findConverter && !TypeHelper.isAssignable(sqlType, propertyType)) {
            ContextualConverter<? super Object, ?> converter = ConverterService.getInstance().findConverter(propertyType, sqlType, contextFactoryBuilder);
            if (converter != null) {
                getter = new FieldMapperGetterWithConverter(converter, getter);
            }
        }
        return new PlaceHolderValueGetter<T>(pm.getColumnKey().getOrginalName(), parameterType, null, getter, contextFactoryBuilder.build());
    } else {
        return new PlaceHolderValueGetter<T>(key.getOrginalName(), key.getSqlType(null), null, NullContextualGetter.<T, P>getter(), EmptyContextFactory.INSTANCE);
    }
}
Also used : Type(java.lang.reflect.Type) ConverterProperty(org.simpleflatmapper.map.property.ConverterProperty) FieldMapperGetterWithConverter(org.simpleflatmapper.map.fieldmapper.FieldMapperGetterWithConverter) DefaultContextFactoryBuilder(org.simpleflatmapper.converter.DefaultContextFactoryBuilder)

Example 4 with DefaultContextFactoryBuilder

use of org.simpleflatmapper.converter.DefaultContextFactoryBuilder 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;
}
Also used : MultiIndexFieldMapper(org.simpleflatmapper.jdbc.MultiIndexFieldMapper) ArrayList(java.util.ArrayList) DefaultContextFactoryBuilder(org.simpleflatmapper.converter.DefaultContextFactoryBuilder)

Example 5 with DefaultContextFactoryBuilder

use of org.simpleflatmapper.converter.DefaultContextFactoryBuilder in project SimpleFlatMapper by arnaudroger.

the class ConverterServiceTestHelper method testConverter.

public static <I, O> void testConverter(I i, O o, Class<I> classi, Class<O> classo, Object... params) throws Exception {
    if (hasZoneId(params)) {
        params = Arrays.copyOf(params, params.length + 1);
        params[params.length - 1] = TimeZone.getTimeZone("UTC");
    }
    DefaultContextFactoryBuilder defaultContextFactoryBuilder = new DefaultContextFactoryBuilder();
    final ContextualConverter<? super I, ? extends O> converter = ConverterService.getInstance().findConverter(classi, classo, defaultContextFactoryBuilder, params);
    assertNotNull("Converter not null", converter);
    ContextFactory contextFactory = defaultContextFactoryBuilder.build();
    assertEquals(o, converter.convert(i, contextFactory.newContext()));
    assertNotNull(converter.toString());
}
Also used : ContextFactory(org.simpleflatmapper.converter.ContextFactory) DefaultContextFactoryBuilder(org.simpleflatmapper.converter.DefaultContextFactoryBuilder)

Aggregations

DefaultContextFactoryBuilder (org.simpleflatmapper.converter.DefaultContextFactoryBuilder)5 ArrayList (java.util.ArrayList)3 MultiIndexFieldMapper (org.simpleflatmapper.jdbc.MultiIndexFieldMapper)3 Type (java.lang.reflect.Type)1 ContextFactory (org.simpleflatmapper.converter.ContextFactory)1 FieldMapperGetterWithConverter (org.simpleflatmapper.map.fieldmapper.FieldMapperGetterWithConverter)1 ConverterProperty (org.simpleflatmapper.map.property.ConverterProperty)1