Search in sources :

Example 11 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class PostgresqlCrudTest method testManualAutogenerated.

@Test
public void testManualAutogenerated() throws SQLException {
    Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    if (connection == null) {
        System.err.println("Db POSTGRESQL not available");
        return;
    }
    try {
        Statement st = connection.createStatement();
        try {
            st.execute("DROP TABLE IF EXISTS test_db_object_dv");
            st.execute("CREATE TABLE test_db_object_dv(  id bigint DEFAULT 3," + " name varchar(100), " + " email varchar(100)," + " creation_Time timestamp, type_ordinal int, type_name varchar(10)  )");
        } finally {
            st.close();
        }
        Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().addColumnProperty("id", KeyProperty.DEFAULT, AutoGeneratedProperty.DEFAULT).<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_dv");
        DbObject object = DbObject.newInstance();
        object.setId(-22225);
        // create
        Long key = objectCrud.create(connection, object, new CheckedConsumer<Long>() {

            Long key;

            @Override
            public void accept(Long aLong) throws Exception {
                key = aLong;
            }
        }).key;
        assertFalse(key.equals(object.getId()));
        object.setId(key);
        // read
        assertEquals(object, objectCrud.read(connection, key));
    } finally {
        connection.close();
    }
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 12 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class ResultSetFieldMapperFactoryTest method testPrimitiveField.

@Test
public void testPrimitiveField() {
    ClassMeta<DbObject> classMeta = ReflectionService.newInstance(false).getClassMeta(DbObject.class);
    PropertyMeta<DbObject, Long> id = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).<Long>findProperty(new DefaultPropertyNameMatcher("id", 0, false, false), new Object[0]);
    FieldMapperColumnDefinition<JdbcColumnKey> identity = FieldMapperColumnDefinition.identity();
    PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 1), identity);
    FieldMapper<ResultSet, DbObject> fieldMapper = factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
    assertTrue(fieldMapper instanceof LongFieldMapper);
    PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping1 = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 0), identity);
    fieldMapper = factory.newFieldMapper(propertyMapping1, null, RethrowMapperBuilderErrorHandler.INSTANCE);
    assertTrue(fieldMapper instanceof LongFieldMapper);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) LongFieldMapper(org.simpleflatmapper.map.fieldmapper.LongFieldMapper) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Test(org.junit.Test)

Example 13 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class MysqlCrudTest method testSplitBatch.

@Test
public void testSplitBatch() throws SQLException {
    Connection connection = mock(Connection.class);
    PreparedStatement ps = mock(PreparedStatement.class);
    PreparedStatement ps2 = mock(PreparedStatement.class);
    when(connection.prepareStatement("INSERT INTO TEST(id) VALUES(?), (?), (?), (?), (?), (?), (?), (?), (?), (?)")).thenReturn(ps);
    when(connection.prepareStatement("INSERT INTO TEST(id) VALUES(?), (?), (?), (?), (?)")).thenReturn(ps2);
    when(ps.executeUpdate()).thenThrow(getPacketTooBigException());
    Crud<DbObject, Long> objectCrud = CrudFactory.<DbObject, Long>newInstance(ReflectionService.newInstance().getClassMeta(DbObject.class), ReflectionService.newInstance().getClassMeta(Long.class), new CrudMeta(new DatabaseMeta("MySQL", 5, 5), "TEST", new ColumnMeta[] { new ColumnMeta("id", Types.INTEGER, true, null) }), JdbcMapperFactory.newInstance());
    final int batchsize = 10;
    final List<DbObject> values = new ArrayList<DbObject>();
    for (int i = 0; i < batchsize; i++) {
        values.add(DbObject.newInstance());
    }
    objectCrud.create(connection, values);
    for (int i = 0; i < batchsize; i++) {
        verify(ps).setLong(i + 1, values.get(i).getId());
    }
    verify(ps).executeUpdate();
    for (int i = 0; i < batchsize / 2; i++) {
        ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class);
        verify(ps2, times(2)).setLong(eq(i + 1), captor.capture());
        final List<Long> allValues = captor.getAllValues();
        assertEquals(values.get(i).getId(), allValues.get(0).longValue());
        assertEquals(values.get(i + batchsize / 2).getId(), allValues.get(1).longValue());
    }
    verify(ps).executeUpdate();
}
Also used : CrudMeta(org.simpleflatmapper.jdbc.impl.CrudMeta) DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DatabaseMeta(org.simpleflatmapper.jdbc.impl.DatabaseMeta) ColumnMeta(org.simpleflatmapper.jdbc.impl.ColumnMeta) Test(org.junit.Test)

Example 14 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class MysqlMultiRowsBatchInsertCrudTest method testUpsert.

@Test
public void testUpsert() throws SQLException {
    Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.MYSQL);
    if (connection == null) {
        System.err.println("Db MySQL not available");
        return;
    }
    try {
        Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
        final DbObject object = DbObject.newInstance();
        objectCrud.createOrUpdate(connection, object);
        assertEquals(object, objectCrud.read(connection, object.getId()));
        object.setEmail("Updated Email");
        objectCrud.createOrUpdate(connection, object);
        assertEquals(object, objectCrud.read(connection, object.getId()));
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) Test(org.junit.Test)

Example 15 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class MysqlMultiRowsBatchInsertCrudTest method testBatchInsertSizeReducer.

@Test
public void testBatchInsertSizeReducer() throws SQLException {
    Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.MYSQL);
    if (connection == null) {
        System.err.println("Db MySQL not available");
        return;
    }
    try {
        Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT");
        Connection mockConnection = mock(Connection.class);
        PreparedStatement preparedStatementFail = mock(PreparedStatement.class);
        PreparedStatement preparedStatementSucceed = mock(PreparedStatement.class);
        List<DbObject> values = new ArrayList<DbObject>();
        for (int i = 0; i < 101; i++) {
            values.add(DbObject.newInstance());
        }
        when(mockConnection.prepareStatement(anyString())).thenReturn(preparedStatementFail, preparedStatementFail, preparedStatementSucceed);
        when(preparedStatementFail.executeUpdate()).thenThrow(MysqlCrudTest.getPacketTooBigException());
        objectCrud.create(mockConnection, values);
        verify(preparedStatementFail, times(2)).executeUpdate();
        verify(preparedStatementSucceed, times(5)).executeUpdate();
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3