List of usage examples for org.springframework.jdbc.core StatementCallback StatementCallback
StatementCallback
From source file:net.sourceforge.vulcan.spring.jdbc.JdbcSchemaMigrator.java
boolean isTablePresent(final String tableName) { final Boolean b = (Boolean) jdbcTemplate.execute(new StatementCallback() { public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { ResultSet rs = stmt.getConnection().getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (rs.next()) { if (tableName.equalsIgnoreCase(rs.getString("TABLE_NAME"))) { return true; }// w w w. j a va2 s. co m } return false; } }); return b.booleanValue(); }
From source file:net.sourceforge.vulcan.spring.jdbc.JdbcSchemaMigrator.java
private void executeSql(final Resource resource) throws IOException { final String text = loadResource(resource); log.info("Running migration script " + resource.getFilename()); final String[] commands = text.split(";"); for (String command : commands) { final String trimmed = command.trim(); if (StringUtils.isBlank(trimmed)) { continue; }/* w ww . j a v a2s.co m*/ jdbcTemplate.execute(new StatementCallback() { public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { stmt.execute(trimmed); final Connection conn = stmt.getConnection(); if (!conn.getAutoCommit()) { conn.commit(); } return null; } }); } }
From source file:com.alibaba.cobar.manager.dao.delegate.CobarAdapter.java
@Override public Pair<Long, Long> getCurrentTimeMillis() { return (Pair<Long, Long>) getJdbcTemplate().execute(new StatementCallback() { @Override/*from w w w .j av a 2s . co m*/ public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { ResultSet rs = null; try { long time1 = System.currentTimeMillis(); rs = stmt.executeQuery("show @@status.time"); long time2 = System.currentTimeMillis(); if (rs.next()) { return new Pair<Long, Long>(time1 + (time2 - time1) / 2, rs.getLong(1)); } else { throw new IncorrectResultSizeDataAccessException(1, 0); } } finally { if (rs != null) { rs.close(); } } } }); }
From source file:com.alibaba.otter.node.etl.load.loader.db.DbLoadAction.java
/** * ddl??: /*from w w w.j av a 2 s . co m*/ * * @param context * @param eventDatas */ private void doDdl(DbLoadContext context, List<EventData> eventDatas) { for (final EventData data : eventDatas) { DataMedia dataMedia = ConfigHelper.findDataMedia(context.getPipeline(), data.getTableId()); final DbDialect dbDialect = dbDialectFactory.getDbDialect(context.getIdentity().getPipelineId(), (DbMediaSource) dataMedia.getSource()); Boolean skipDdlException = context.getPipeline().getParameters().getSkipDdlException(); try { Boolean result = dbDialect.getJdbcTemplate().execute(new StatementCallback<Boolean>() { public Boolean doInStatement(Statement stmt) throws SQLException, DataAccessException { Boolean result = false; if (dbDialect instanceof MysqlDialect && StringUtils.isNotEmpty(data.getDdlSchemaName())) { // mysqlddl??schema result &= stmt.execute("use " + data.getDdlSchemaName()); } result &= stmt.execute(data.getSql()); return result; } }); if (result) { context.getProcessedDatas().add(data); // ??sql } else { context.getFailedDatas().add(data); } } catch (Throwable e) { if (skipDdlException) { // do skip logger.warn("skip exception for ddl : {} , caused by {}", data, ExceptionUtils.getFullStackTrace(e)); } else { throw new LoadException(e); } } } }
From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.ParametersSetterBlockServiceImpl.java
private Map<String, Integer> createTypes(JdbcTemplate jdbcTemplate, final String tableName) { return jdbcTemplate.execute(new StatementCallback<Map<String, Integer>>() { public Map<String, Integer> doInStatement(Statement stmt) throws SQLException, DataAccessException { ResultSet rs = stmt.executeQuery("select * from " + tableName); try { ResultSetMetaData meta = rs.getMetaData(); Map<String, Integer> types = new HashMap<String, Integer>(); int count = meta.getColumnCount(); for (int i = 1; i <= count; i++) { String name = meta.getColumnName(i); int type = meta.getColumnType(i); types.put(name, type); }/*from w ww . ja v a2 s. c om*/ return types; } finally { rs.close(); } } }); }
From source file:org.kuali.kpme.core.util.ClearDatabaseLifecycle.java
protected void clearTables(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) { LOG.info("Clearing tables for schema " + schemaName); Assert.assertNotNull("DataSource could not be located.", dataSource); if (schemaName == null || schemaName.equals("")) { Assert.fail("Empty schema name given"); }/*from www . j ava 2 s. co m*/ new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() { public Object doInStatement(Statement statement) throws SQLException { final List<String> reEnableConstraints = new ArrayList<String>(); List<List<String>> tableLists = new ArrayList<List<String>>(2); tableLists.add(TABLES_TO_CLEAR); tableLists.add(alternativeTablesToClear); for (List<String> list : tableLists) { for (String tableName : list) { //if there is an id name that doesnt follow convention check and limit accordingly String idName = TABLE_TO_ID_MAP.get(tableName); String deleteStatement = null; Integer clearId = TABLE_START_CLEAR_ID.get(tableName) != null ? TABLE_START_CLEAR_ID.get(tableName) : START_CLEAR_ID; if (idName == null) { deleteStatement = "DELETE FROM " + tableName + " WHERE " + StringUtils.removeEnd(tableName, "_T") + "_ID" + " >= " + clearId; } else { deleteStatement = "DELETE FROM " + tableName + " WHERE " + idName + " >= " + clearId; } LOG.debug("Clearing contents using statement ->" + deleteStatement + "<-"); statement.addBatch(deleteStatement); } } for (final String constraint : reEnableConstraints) { LOG.debug("Enabling constraints using statement ->" + constraint + "<-"); statement.addBatch(constraint); } statement.executeBatch(); return null; } }); } }); LOG.info("Tables successfully cleared for schema " + schemaName); }
From source file:org.kuali.kpme.core.util.DatabaseCleanupDataLifecycle.java
public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) { Assert.assertNotNull("DataSource could not be located.", dataSource); if (schemaName == null || schemaName.equals("")) { Assert.fail("Empty schema name given"); }/* www .j av a 2s. com*/ new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback() { public Object doInStatement(Statement statement) throws SQLException { List<String> sqlStatements = new ArrayList<String>(); // // djunk - add a per-class special test data loader, // loads <testclassname>.sql from the same directory // as the other SQL loaded. if (callingTestClass != null) { sqlStatements.addAll(getTestDataSQLStatements( "src/test/config/sql/" + callingTestClass.getSimpleName() + "-cleanup.sql")); } for (String sql : sqlStatements) { if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) { // ignore comment lines in our sql reader. statement.addBatch(sql); } } statement.executeBatch(); return null; } }); } }); }
From source file:org.kuali.kpme.core.util.LoadDatabaseDataLifeCycle.java
public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) { LOG.info("Populating tables for schema " + schemaName); Assert.assertNotNull("DataSource could not be located.", dataSource); if (schemaName == null || schemaName.equals("")) { Assert.fail("Empty schema name given"); }/*from w ww . ja va 2 s. c o m*/ new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() { public Object doInStatement(Statement statement) throws SQLException { if (callingTestClass != null) { List<String> sqlStatements = getTestDataSQLStatements( "src/test/config/sql/" + callingTestClass.getSimpleName() + ".sql"); for (String sql : sqlStatements) { if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) { // ignore comment lines in our sql reader. statement.addBatch(sql); } } } statement.executeBatch(); return null; } }); } }); }
From source file:org.kuali.kpme.core.util.SQLDataLifeCycle.java
public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) { LOG.info("Clearing tables for schema " + schemaName); Assert.assertNotNull("DataSource could not be located.", dataSource); if (schemaName == null || schemaName.equals("")) { Assert.fail("Empty schema name given"); }//from w w w . j a va2 s .c o m new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() { public Object doInStatement(Statement statement) throws SQLException { if (callingTestClass != null) { List<String> sqlStatements = getTestDataSQLStatements( "src/test/config/sql/" + callingTestClass.getSimpleName() + ".sql"); for (String sql : sqlStatements) { if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) { // ignore comment lines in our sql reader. statement.addBatch(sql); } } } statement.executeBatch(); return null; } }); } }); }
From source file:org.kuali.kra.infrastructure.TestUtilities.java
License:asdf
public static void clearTables(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String edenSchemaName, final List<String> dontClear) { LOG.info("Clearing tables for schema " + edenSchemaName); if (dataSource == null) { Assert.fail("Null data source given"); }//from w w w.j a v a 2 s. c om if (edenSchemaName == null || edenSchemaName.equals("")) { Assert.fail("Empty eden schema name given"); } new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { verifyTestEnvironment(dataSource); JdbcTemplate template = new JdbcTemplate(dataSource); return template.execute(new StatementCallback() { public Object doInStatement(Statement statement) throws SQLException { List<String> reEnableConstraints = new ArrayList<String>(); ResultSet resultSet = statement.getConnection().getMetaData().getTables(null, edenSchemaName, null, new String[] { "TABLE" }); while (resultSet.next()) { String tableName = resultSet.getString("TABLE_NAME"); if (tableName.startsWith("EN_") && !dontClear.contains(tableName)) { ResultSet keyResultSet = statement.getConnection().getMetaData() .getExportedKeys(null, edenSchemaName, tableName); while (keyResultSet.next()) { String fkName = keyResultSet.getString("FK_NAME"); String fkTableName = keyResultSet.getString("FKTABLE_NAME"); statement.addBatch( "ALTER TABLE " + fkTableName + " DISABLE CONSTRAINT " + fkName); reEnableConstraints .add("ALTER TABLE " + fkTableName + " ENABLE CONSTRAINT " + fkName); } keyResultSet.close(); statement.addBatch("DELETE FROM " + tableName.toUpperCase()); } } for (String constraint : reEnableConstraints) { statement.addBatch(constraint); } statement.executeBatch(); resultSet.close(); return null; } }); } }); LOG.info("Tables successfully cleared for schema " + edenSchemaName); }