use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class MysqlMultiRowsBatchInsertCrudTest method testBatchUpsertOnDb.
@Test
public void testBatchUpsertOnDb() 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");
DbObject object1 = DbObject.newInstance();
DbObject object2 = DbObject.newInstance();
objectCrud.create(connection, object1);
object1.setName("BatchUpdate");
object2.setName("BatchUpdate");
objectCrud.createOrUpdate(connection, Arrays.<DbObject>asList(object1, object2));
ResultSet resultSet = connection.createStatement().executeQuery("SELECT count(*) FROM TEST_DB_OBJECT WHERE name = 'BatchUpdate'");
assertTrue(resultSet.next());
assertEquals(2, resultSet.getInt(1));
assertEquals(object1, objectCrud.read(connection, object1.getId()));
assertEquals(object2, objectCrud.read(connection, object2.getId()));
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudTest method testManualAutogeneratedFromSeq.
@Test
public void testManualAutogeneratedFromSeq() 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_seq");
st.execute("DROP SEQUENCE IF EXISTS test_seq_seq");
st.execute("CREATE SEQUENCE test_seq_seq");
st.execute("CREATE TABLE test_db_object_seq( id bigint 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().addColumnProperty("id", KeyProperty.DEFAULT, AutoGeneratedProperty.of("nextval('test_seq_seq')")).<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "test_db_object_seq");
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));
object = DbObject.newInstance();
object.setId(-22225);
// create batch
key = objectCrud.create(connection, Arrays.asList(object), new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
if (key != null)
throw new IllegalArgumentException();
key = aLong;
}
}).key;
assertFalse(key.equals(object.getId()));
object.setId(key);
// read
assertEquals(object, objectCrud.read(connection, key));
object = DbObject.newInstance();
object.setId(-22225);
try {
// createOrUpdate
key = objectCrud.createOrUpdate(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));
// createOrUpdate batch
key = objectCrud.createOrUpdate(connection, Arrays.asList(object), new CheckedConsumer<Long>() {
Long key;
@Override
public void accept(Long aLong) throws Exception {
if (key != null)
throw new IllegalArgumentException();
key = aLong;
}
}).key;
assertFalse(key.equals(object.getId()));
object.setId(key);
// read
assertEquals(object, objectCrud.read(connection, key));
} catch (UnsupportedOperationException e) {
// wrong postgres version
}
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class PostgresqlCrudTest method testUpsert.
@Test
public void testUpsert() throws SQLException {
Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
if (connection == null) {
System.err.println("Db MySQL not available");
return;
}
if (connection.getMetaData().getDatabaseMajorVersion() != 9 || connection.getMetaData().getDatabaseMinorVersion() < 5) {
System.err.println("Db Postgresql 9.5 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();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class CrudTest method testDbObjectCrudAutoInc.
@Test
public void testDbObjectCrudAutoInc() throws SQLException {
Connection connection = DbHelper.getDbConnection(targetDB);
if (connection == null) {
System.err.println("Db " + targetDB + " not available");
return;
}
try {
Crud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(connection, "TEST_DB_OBJECT_AUTOINC");
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));
object.setName("Udpdated");
// update
objectCrud.update(connection, object);
assertEquals(object, objectCrud.read(connection, key));
// delete
objectCrud.delete(connection, key);
assertNull(objectCrud.read(connection, key));
objectCrud.create(connection, DbObject.newInstance());
// batch
final List<Long> keys = new ArrayList<Long>();
final List<DbObject> values = Arrays.asList(DbObject.newInstance(), DbObject.newInstance());
objectCrud.create(connection, values, new CheckedConsumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
values.get(keys.size()).setId(aLong);
keys.add(aLong);
}
});
assertCollectionEquals(values, objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
int i = 333;
for (DbObject value : values) {
value.setEmail(Integer.toHexString(i));
i++;
}
objectCrud.update(connection, values);
assertCollectionEquals(values, objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
objectCrud.delete(connection, keys);
assertCollectionEquals(Collections.<DbObject>emptyList(), objectCrud.read(connection, keys, new ListCollector<DbObject>()).getList());
} finally {
connection.close();
}
}
use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperFactoryTest method testFieldErrorHandlingOnResultSet.
@Test
public void testFieldErrorHandlingOnResultSet() throws SQLException, Exception, ParseException {
@SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
ResultSet rs = mock(ResultSet.class);
final Exception exception = new SQLException("Error!");
JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).newBuilder(DbObject.class).addMapping("id").mapper();
when(rs.next()).thenReturn(true, false);
when(rs.getLong(1)).thenThrow(exception);
List<DbObject> list = mapper.forEach(rs, new ListCollector<DbObject>()).getList();
assertNotNull(list.get(0));
verify(fieldMapperErrorHandler).errorMappingField(eq(new JdbcColumnKey("id", 1)), any(), same(list.get(0)), same(exception));
}
Aggregations