@Override public Crud<T, K> doInConnection(Connection connection) throws SQLException, DataAccessException { return factory.<T, K>crud(target, keyTarget).table(connection, table); } });
@Override public Crud<T, K> doInConnection(Connection connection) throws SQLException, DataAccessException { return factory.<T, K>crud(target, keyTarget).to(connection); } });
public JdbcTemplateCrud<T, K> lazilyTo(JdbcOperations jdbcOperations) { final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory); Crud<T, K> crud = factory.<T, K>crud(target, keyTarget).crud(); return new JdbcTemplateCrud<T, K>(jdbcOperations, crud); } }
/** * 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)); }
public <T, K> CrudDSL<T, K> crud(final ClassMeta<T> target, final ClassMeta<K> keyTarget) { return new CrudDSL<T, K>(target, keyTarget, JdbcMapperFactory.newInstance(this)); }
private String getTable(Connection connection, ClassMeta<T> target) throws SQLException { final Class<Object> targetClass = TypeHelper.toClass(target.getType()); Table table = AliasProviderService.getAliasProvider().getTable(targetClass); StringBuilder sb = new StringBuilder(); if (table.schema() != null && table.schema().length() > 0) { sb.append(table.schema()).append("."); } if (table.table() == null) { DatabaseMetaData metaData = connection.getMetaData(); final ResultSet tables = getTables(connection, metaData); final String className = TypeHelper.toClass(targetClass).getSimpleName(); try { while(tables.next()) { String tableName = tables.getString("TABLE_NAME"); if (DefaultPropertyNameMatcher.of(tableName).matches(className)) { sb.append(tableName); return sb.toString(); } } } finally { tables.close(); } throw new IllegalArgumentException("Type " + target.getType() + " has no table mapping"); } sb.append(table.table()); return sb.toString(); }
/** * 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)); }
public <T, K> CrudDSL<T, K> crud(final ClassMeta<T> target, final ClassMeta<K> keyTarget) { return new CrudDSL<T, K>(target, keyTarget, JdbcMapperFactory.newInstance(this)); }
private String getTable(Connection connection, ClassMeta<T> target) throws SQLException { final Class<Object> targetClass = TypeHelper.toClass(target.getType()); Table table = AliasProviderService.getAliasProvider().getTable(targetClass); StringBuilder sb = new StringBuilder(); if (table.schema() != null && table.schema().length() > 0) { sb.append(table.schema()).append("."); } if (table.table() == null) { DatabaseMetaData metaData = connection.getMetaData(); final ResultSet tables = getTables(connection, metaData); final String className = TypeHelper.toClass(targetClass).getSimpleName(); try { while(tables.next()) { String tableName = tables.getString("TABLE_NAME"); if (DefaultPropertyNameMatcher.of(tableName).matches(className)) { sb.append(tableName); return sb.toString(); } } } finally { tables.close(); } throw new IllegalArgumentException("Type " + target.getType() + " has no table mapping"); } sb.append(table.table()); return sb.toString(); }
/** * 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 validating it against the specified datasource. * The table name is derived from the jpa annotation or from the class name. * @param dataSource the datasource * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> to(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), to(connection)); } finally { connection.close(); } }
@Test public void testDbObjectLazyCrudTable() throws SQLException { Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<DbObjectTable, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObjectTable, Long>crud(DbObjectTable.class, Long.class).crud(); checkCrudDbObject(connection, objectCrud, DbObject.newInstance(new DbObjectTable())); } 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(); } }
/** * Create a connected crud validating it against the specified datasource. * The table name is derived from the jpa annotation or from the class name. * @param dataSource the datasource * @return a new crud instance * @throws SQLException if an error occurred */ public ConnectedCrud<T, K> to(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); try { return new ConnectedCrud<T, K>(new DataSourceTransactionTemplate(dataSource), to(connection)); } 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 testDbObjectCrudTable() throws SQLException { Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<DbObjectTable, Long> objectCrud = JdbcMapperFactory.newInstance().<DbObjectTable, Long>crud(DbObjectTable.class, Long.class).to(connection); checkCrudDbObject(connection, objectCrud, DbObject.newInstance(new DbObjectTable())); } finally { connection.close(); } }
@Test public void testBatchInsertSizeReducer() 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 preparedStatementFail = mock(PreparedStatement.class); PreparedStatement preparedStatementSucceed = mock(PreparedStatement.class); List<DbObject> values = new ArrayList<DbObject>(); for(int i = 0; i < 101; i++) { values.add(DbObject.newInstance()); } when(mockConnection.prepareStatement(anyString())) .thenReturn(preparedStatementFail, preparedStatementFail, preparedStatementSucceed); when(preparedStatementFail.executeUpdate()).thenThrow(MysqlCrudTest.getPacketTooBigException()); objectCrud.create(mockConnection, values); verify(preparedStatementFail, times(2)).executeUpdate(); verify(preparedStatementSucceed, times(5)).executeUpdate(); } finally { connection.close(); } }
@Test public void testDbObjectCrudTestDbObject() throws SQLException { Connection connection = DbHelper.getDbConnection(targetDB); if (connection == null) { System.err.println("Db " + targetDB + " not available"); return; } try { Crud<TestDbObject, Long> objectCrud = JdbcMapperFactory.newInstance().<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection); checkCrudDbObject(connection, objectCrud, DbObject.newInstance(new TestDbObject())); } finally { connection.close(); } }