@Override public Crud<T, K> doInConnection(Connection connection) throws SQLException, DataAccessException { return factory.<T, K>crud(target, keyTarget).table(connection, table); } });
private Crud<T, K> instantiateCrud(Connection connection) throws SQLException { if (table == null) { return crudDSL.to(connection); } else { return crudDSL.table(connection, table); } }
/** * Create a connected crud validating it against the specified connection. * The table name is derived from the jpa annotation or from the class name. * @param connection the connection * @return a new crud instance * @throws SQLException if an error occurred */ public Crud<T, K> to(Connection connection) throws SQLException { return table(connection, getTable(connection, target)); }
private Crud<T, K> instantiateCrud(Connection connection) throws SQLException { if (table == null) { return crudDSL.to(connection); } else { return crudDSL.table(connection, table); } }
/** * Create a connected crud validating it against the specified connection. * The table name is derived from the jpa annotation or from the class name. * @param connection the connection * @return a new crud instance * @throws SQLException if an error occurred */ public Crud<T, K> to(Connection connection) throws SQLException { return table(connection, getTable(connection, target)); }
/** * Create a connected crud against the specified table validating it against the specified datasource. * @param dataSource the datasource * @param table the table * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> table(DataSource dataSource, String table) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), table(connection, table)); } finally { connection.close(); } }
/** * Create a connected crud against the specified table validating it against the specified datasource. * @param dataSource the datasource * @param table the table * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> table(DataSource dataSource, String table) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), table(connection, table)); } finally { connection.close(); } }
public JdbcTemplateCrud<T, K> lazilyTo(JdbcOperations jdbcOperations, final String table) { final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory); Crud<T, K> crud = factory.<T, K>crud(target, keyTarget).table(table); return new JdbcTemplateCrud<T, K>(jdbcOperations, crud); }
@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(); } } @Test
@Test public void testFailOnNoPrimaryKey() throws SQLException { try { JdbcMapperFactory.newInstance().crud(MyObject.class, long.class).table(connection, "CRUDNOKEY"); fail(); } catch (IllegalArgumentException e) { System.out.println("e = " + e); } } @Test
public static void main(String[] args) throws SQLException { Connection connection = MysqlDbHelper.objectDb(); Crud<DbObject, Long> crud = JdbcMapperFactory .newInstance() .crud(DbObject.class, Long.class) .table(connection, "TEST_DB_OBJECT"); connection.createStatement().execute("TRUNCATE TABLE TEST_DB_OBJECT"); List<DbObject> objects = new ArrayList<DbObject>(10000); for(int i = 0; i < 65001; i++) { DbObject e = DbObject.newInstance(); e.setId(i + 1); objects.add(e); } crud.create(connection, objects); }
@Test public void testDbObjectCrud() 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"); checkCrudDbObject(connection, objectCrud, DbObject.newInstance()); } finally { connection.close(); } }
@Test public void testDbObjectLazyCrud() 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("TEST_DB_OBJECT"); checkCrudDbObject(connection, objectCrud, DbObject.newInstance()); } finally { connection.close(); } }
@Test public void testUpsertHsqlBatch() throws Exception { testDefaultCrudCreateOrUpdate(DbHelper.getDbConnection(DbHelper.TargetDB.HSQLDB), JdbcMapperFactory.newInstance().crud(DbObject.class, Long.class).table(DbHelper.getDbConnection(DbHelper.TargetDB.HSQLDB), "TEST_DB_OBJECT")); testDefaultCrudCreateOrUpdate(DbHelper.getDbConnection(DbHelper.TargetDB.HSQLDB), JdbcMapperFactory.newInstance().crud(DbObject.class, Long.class).table("TEST_DB_OBJECT")); testConnectedCrudCreateOrUpdate(JdbcMapperFactory.newInstance().crud(DbObject.class, Long.class).table(DbHelper.getHsqlDataSource(), "TEST_DB_OBJECT")); }
@Test public void testDbObjectCrud() throws Exception { DataSource dataSource = DbHelper.getHsqlDataSource(); ConnectedCrud<DbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObject, Long>crud(DbObject.class, Long.class).table(dataSource, "TEST_DB_OBJECT"); checkCrudDbObject(objectCrud, DbObject.newInstance()); Connection connection = dataSource.getConnection(); try { CrudTest.checkCrudDbObject(connection, objectCrud.crud(), DbObject.newInstance()); } 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 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 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(); } }