Search in sources :

Example 1 with DatabaseMeta

use of org.simpleflatmapper.jdbc.impl.DatabaseMeta 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 2 with DatabaseMeta

use of org.simpleflatmapper.jdbc.impl.DatabaseMeta in project SimpleFlatMapper by arnaudroger.

the class DatabaseMetaTest method isVersionMet.

@Test
public void isVersionMet() throws Exception {
    DatabaseMeta databaseMeta = new DatabaseMeta("foo", 3, 4);
    assertTrue(databaseMeta.isVersionMet(2, 9));
    assertTrue(databaseMeta.isVersionMet(3, 3));
    assertTrue(databaseMeta.isVersionMet(3, 4));
    assertFalse(databaseMeta.isVersionMet(3, 5));
    assertFalse(databaseMeta.isVersionMet(4, 1));
}
Also used : DatabaseMeta(org.simpleflatmapper.jdbc.impl.DatabaseMeta) Test(org.junit.Test)

Example 3 with DatabaseMeta

use of org.simpleflatmapper.jdbc.impl.DatabaseMeta 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)

Aggregations

Test (org.junit.Test)3 DatabaseMeta (org.simpleflatmapper.jdbc.impl.DatabaseMeta)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 ColumnMeta (org.simpleflatmapper.jdbc.impl.ColumnMeta)2 CrudMeta (org.simpleflatmapper.jdbc.impl.CrudMeta)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2