Search in sources :

Example 6 with PreparedStatementCallback

use of org.springframework.jdbc.core.PreparedStatementCallback in project otter by alibaba.

the class BitTableIntegration method test_mysql.

@Test
public void test_mysql() throws UnsupportedEncodingException {
    DbMediaSource dbMediaSource = new DbMediaSource();
    dbMediaSource.setId(10L);
    dbMediaSource.setDriver("com.mysql.jdbc.Driver");
    dbMediaSource.setUsername("xxxxx");
    dbMediaSource.setPassword("xxxxx");
    dbMediaSource.setUrl("jdbc:mysql://127.0.0.1:3306");
    dbMediaSource.setEncode("UTF-8");
    dbMediaSource.setType(DataMediaType.MYSQL);
    final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, dbMediaSource);
    want.object(dbDialect).clazIs(MysqlDialect.class);
    Table table = dbDialect.findTable(SCHEMA_NAME, TABLE_NAME);
    System.out.println(table);
    final SqlTemplate sqlTemplate = dbDialect.getSqlTemplate();
    final JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate();
    final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate();
    final int[] pkColumnTypes = { Types.INTEGER };
    final int[] columnTypes = { Types.BIT, Types.BIT };
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            int affect = 0;
            String sql = null;
            // 执行insert
            sql = sqlTemplate.getInsertSql(SCHEMA_NAME, TABLE_NAME, pkColumns, columns);
            System.out.println(sql);
            affect = (Integer) jdbcTemplate.execute(sql, new PreparedStatementCallback() {

                public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    doPreparedStatement(ps, dbDialect, toTypes(columnTypes, pkColumnTypes), toValues(columnValues, pkColumnValues));
                    return ps.executeUpdate();
                }
            });
            want.number(affect).isEqualTo(1);
            return null;
        // throw new RuntimeException("rollback");
        }
    });
}
Also used : Table(org.apache.ddlutils.model.Table) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) PreparedStatement(java.sql.PreparedStatement) SqlTemplate(com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) TransactionCallback(org.springframework.transaction.support.TransactionCallback) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) DbMediaSource(com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource) Test(org.testng.annotations.Test) BaseDbTest(com.alibaba.otter.node.etl.BaseDbTest)

Example 7 with PreparedStatementCallback

use of org.springframework.jdbc.core.PreparedStatementCallback in project otter by alibaba.

the class DdlUtils method getShardKeyByDRDS.

/**
     * 获取DRDS下表的拆分字段, 返回格式为 id,name
     * 
     * @param dataSource
     * @param schemaName
     * @param tableName
     * @return
     */
public static String getShardKeyByDRDS(final JdbcTemplate jdbcTemplate, final String catalogName, final String schemaName, final String tableName) {
    try {
        return (String) jdbcTemplate.execute("show partitions from ?", new PreparedStatementCallback() {

            public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                DatabaseMetaData metaData = ps.getConnection().getMetaData();
                // String sName = getIdentifierName(schemaName, metaData);
                String convertTableName = tableName;
                if (metaData.storesUpperCaseIdentifiers()) {
                    convertTableName = tableName.toUpperCase();
                }
                if (metaData.storesLowerCaseIdentifiers()) {
                    convertTableName = tableName.toLowerCase();
                }
                String tName = convertTableName;
                ps.setString(1, tName);
                ResultSet rs = ps.executeQuery();
                String log = null;
                if (rs.next()) {
                    log = rs.getString("KEYS");
                }
                rs.close();
                return log;
            }
        });
    } catch (DataAccessException e) {
        // 兼容下oracle源库和目标库DRDS表名不一致的情况,识别一下表名不存在
        Throwable cause = e.getRootCause();
        if (cause instanceof SQLException) {
            // ER_NO_SUCH_TABLE
            if (((SQLException) cause).getErrorCode() == 1146) {
                return null;
            }
        }
        throw e;
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

PreparedStatement (java.sql.PreparedStatement)7 PreparedStatementCallback (org.springframework.jdbc.core.PreparedStatementCallback)7 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)5 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)5 SqlTemplate (com.alibaba.otter.node.etl.common.db.dialect.SqlTemplate)5 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 TransactionStatus (org.springframework.transaction.TransactionStatus)5 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)5 Test (org.testng.annotations.Test)5 DbMediaSource (com.alibaba.otter.shared.common.model.config.data.db.DbMediaSource)3 Table (org.apache.ddlutils.model.Table)3 DbDataMedia (com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 DataAccessException (org.springframework.dao.DataAccessException)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 RollbackExchangeException (org.apache.camel.RollbackExchangeException)1