@Override public T doInConnection(Connection connection) throws SQLException, DataAccessException { return crud.read(connection, key); } });
@Override public RH doInConnection(Connection connection) throws SQLException, DataAccessException { return crud.read(connection, keys, consumer); } });
@Override public T apply(Connection connection) throws SQLException { return delegate.read(connection, key); } });
@Override public Object apply(Connection connection) throws SQLException { delegate.read(connection, keys, consumer); return null; } });
@Override public T apply(Connection connection) throws SQLException { return delegate.read(connection, key); } });
@Override public Object apply(Connection connection) throws SQLException { delegate.read(connection, keys, consumer); return null; } });
@Override public <RH extends CheckedConsumer<? super T>> RH read(Connection connection, Collection<K> keys, RH consumer) throws SQLException { return getDelegate(connection).read(connection, keys, consumer); }
@Override public T read(Connection connection, K key) throws SQLException { return getDelegate(connection).read(connection, key); }
@Override public T read(Connection connection, K key) throws SQLException { return getDelegate(connection).read(connection, key); }
@Override public <RH extends CheckedConsumer<? super T>> RH read(Connection connection, Collection<K> keys, RH consumer) throws SQLException { return getDelegate(connection).read(connection, keys, consumer); }
@Test public void testBatchUpsertOnDb() throws SQLException { Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL); if (connection == null) { System.err.println("Db Postgresql not available"); return; } System.out.println("connection = " + connection.getMetaData().getDatabaseMajorVersion() + "." + connection.getMetaData().getDatabaseMinorVersion()); 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"); 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(); } }
@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(); } }
@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(); } }
@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(); } }
@Test public void testWithManualKey() throws SQLException { Crud<MyObject, Long> table = JdbcMapperFactory .newInstance() .addKeys("id") .<MyObject, Long>crud(MyObject.class, Long.class) .table(connection, "CRUDNOKEY"); assertEquals(new MyObject(1l, "name"), table.read(connection, 1l)); }
@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(); } }
@Test public void testUUID() throws SQLException { Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL); if (connection == null) { System.err.println("Db POSTGRESQL not available"); return; } try { Crud<MyEntity, UUID> objectCrud = JdbcMapperFactory .newInstance() .addKeys("uid") .<MyEntity, UUID>crud(MyEntity.class, UUID.class) .table(connection, "TEST_UUID"); final MyEntity object = new MyEntity(); object.setId(1); object.setUid(UUID.randomUUID()); object.setName("n1"); objectCrud.create(connection, object); assertEquals(object, objectCrud.read(connection, object.getUid())); object.setName("Updated Email"); objectCrud.update(connection, object); assertEquals(object, objectCrud.read(connection, object.getUid())); } finally { connection.close(); } }
@Test public void testDbObjectCrudWithCustomGetter() throws Exception { JdbcMapperFactory mapperFactory = JdbcMapperFactory .newInstance() .addColumnProperty("name", new GetterProperty(new Getter<ResultSet, String>() { @Override public String get(ResultSet target) throws Exception { return "customname"; } })); Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection); TestDbObject testDbObject = DbObject.newInstance(new TestDbObject()); objectCrud.create(connection, testDbObject); TestDbObject read = objectCrud.read(connection, testDbObject.getId()); assertEquals("customname", read.getName()); assertEquals(testDbObject.getEmail(), read.getEmail()); } finally { connection.close(); } }
@Test public void testDbObjectCrudWithCustomSetter() throws Exception { JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new IndexedSetterProperty(new IndexedSetter<PreparedStatement, Object>() { @Override public void set(PreparedStatement target, Object value, int index) throws Exception { target.setString(index, "customname"); } })); Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection); TestDbObject testDbObject = DbObject.newInstance(new TestDbObject()); assertNotEquals("customname", testDbObject); objectCrud.create(connection, testDbObject); TestDbObject read = objectCrud.read(connection, testDbObject.getId()); assertEquals("customname", read.getName()); assertEquals(testDbObject.getEmail(), read.getEmail()); } finally { connection.close(); } }