Search in sources :

Example 71 with DbObject

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

the class MysqlCrudTest method testBatch.

@Test
public void testBatch() throws SQLException {
    Connection connection = mock(Connection.class);
    PreparedStatement ps = mock(PreparedStatement.class);
    when(connection.prepareStatement("INSERT INTO TEST(id) VALUES(?), (?), (?), (?), (?), (?), (?), (?), (?), (?)")).thenReturn(ps);
    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 List<DbObject> values = new ArrayList<DbObject>();
    for (int i = 0; i < 10; i++) {
        values.add(DbObject.newInstance());
    }
    objectCrud.create(connection, values);
    for (int i = 0; i < 10; i++) {
        verify(ps).setLong(i + 1, values.get(i).getId());
    }
    verify(ps).executeUpdate();
}
Also used : CrudMeta(org.simpleflatmapper.jdbc.impl.CrudMeta) ColumnMeta(org.simpleflatmapper.jdbc.impl.ColumnMeta) DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) DatabaseMeta(org.simpleflatmapper.jdbc.impl.DatabaseMeta) Test(org.junit.Test)

Example 72 with DbObject

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

the class MysqlMultiRowsBatchInsertCrudTest method testUpsertAutoIncUniqueIndexOnIndex.

@Test
public void testUpsertAutoIncUniqueIndexOnIndex() 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_AUTOINC_NAMEINDEX");
        final DbObject object = DbObject.newInstance();
        Long key = objectCrud.createOrUpdate(connection, object, new KeyCapture<Long>()).getKey();
        assertNotNull(key);
        object.setId(key);
        assertEquals(object, objectCrud.read(connection, object.getId()));
        object.setEmail("Updated Email " + key);
        key = objectCrud.createOrUpdate(connection, object, new KeyCapture<Long>()).getKey();
        System.out.println("key = " + key + " current id " + object.getId());
        ResultSet resultSet = connection.createStatement().executeQuery("SELECT count(*) FROM TEST_DB_OBJECT_AUTOINC_NAMEINDEX WHERE email = '" + object.getEmail() + "'");
        assertTrue(resultSet.next());
        assertEquals(1, resultSet.getInt(1));
        assertEquals(object, objectCrud.read(connection, object.getId()));
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 73 with DbObject

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

the class MysqlMultiRowsBatchInsertCrudTest method testBatchInsertCreateOneQuery.

@Test
public void testBatchInsertCreateOneQuery() 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 preparedStatement = mock(PreparedStatement.class);
        ArgumentCaptor<String> queryCapture = ArgumentCaptor.forClass(String.class);
        when(mockConnection.prepareStatement(queryCapture.capture())).thenReturn(preparedStatement);
        objectCrud.create(mockConnection, Arrays.asList(DbObject.newInstance(), DbObject.newInstance()));
        assertEquals("INSERT INTO test_db_object(id, name, email, creation_Time, type_ordinal, type_name) VALUES(?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)".toLowerCase(), queryCapture.getValue().toLowerCase());
        verify(preparedStatement, never()).addBatch();
        verify(preparedStatement, never()).executeBatch();
        verify(preparedStatement).executeUpdate();
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 74 with DbObject

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

the class MysqlMultiRowsBatchInsertCrudTest method testBatchUpsertCreateOneQuery.

@Test
public void testBatchUpsertCreateOneQuery() 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 preparedStatement = mock(PreparedStatement.class);
        ArgumentCaptor<String> queryCapture = ArgumentCaptor.forClass(String.class);
        when(mockConnection.prepareStatement(queryCapture.capture())).thenReturn(preparedStatement);
        objectCrud.createOrUpdate(mockConnection, Arrays.asList(DbObject.newInstance(), DbObject.newInstance()));
        assertEquals(("INSERT INTO test_db_object(id, name, email, creation_Time, type_ordinal, type_name) " + "VALUES(?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?) " + "ON DUPLICATE KEY UPDATE name = VALUES(name), " + "email = VALUES(email)," + " creation_Time = VALUES(creation_Time), " + "type_ordinal = VALUES(type_ordinal), " + "type_name = VALUES(type_name)").toLowerCase(), queryCapture.getValue().toLowerCase());
        verify(preparedStatement, never()).addBatch();
        verify(preparedStatement, never()).executeBatch();
        verify(preparedStatement).executeUpdate();
    } finally {
        connection.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 75 with DbObject

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

the class PostgresqlCrudTest method testSerial.

@Test
public void testSerial() 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("CREATE TABLE IF NOT EXISTS test_db_object_serial(  id bigserial primary key," + " 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().addKeys("id").<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_serial");
        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)

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