Search in sources :

Example 1 with JDBC4PreparedStatement

use of com.mysql.jdbc.JDBC4PreparedStatement in project DataX by alibaba.

the class AdsInsertProxy method generateDmlSql.

private String generateDmlSql(Connection connection, Record record, String mode) throws SQLException {
    String sql = null;
    StringBuilder sqlSb = new StringBuilder();
    if (mode.equalsIgnoreCase(Constant.INSERTMODE)) {
        sqlSb.append(this.insertSqlPrefix);
        sqlSb.append("(");
        int columnsSize = this.columns.size();
        for (int i = 0; i < columnsSize; i++) {
            if ((i + 1) != columnsSize) {
                sqlSb.append("?,");
            } else {
                sqlSb.append("?");
            }
        }
        sqlSb.append(")");
        //mysql impl warn: if a database access error occurs or this method is called on a closed connection
        PreparedStatement statement = connection.prepareStatement(sqlSb.toString());
        for (int i = 0; i < this.columns.size(); i++) {
            int preparedParamsIndex = i;
            if (Constant.STREAMMODE.equalsIgnoreCase(this.writeMode)) {
                if (preparedParamsIndex >= this.opColumnIndex) {
                    preparedParamsIndex = i + 1;
                }
            }
            String columnName = this.columns.get(i);
            int columnSqltype = this.userConfigColumnsMetaData.get(columnName).getLeft();
            prepareColumnTypeValue(statement, columnSqltype, record.getColumn(preparedParamsIndex), i, columnName);
        }
        sql = ((JDBC4PreparedStatement) statement).asSql();
        DBUtil.closeDBResources(statement, null);
    } else {
        sqlSb.append(this.deleteSqlPrefix);
        sqlSb.append("(");
        Set<Entry<String, Integer>> primaryEntrySet = this.primaryKeyNameIndexMap.entrySet();
        int entrySetSize = primaryEntrySet.size();
        int i = 0;
        for (Entry<String, Integer> eachEntry : primaryEntrySet) {
            if ((i + 1) != entrySetSize) {
                sqlSb.append(String.format(" (%s = ?) and ", eachEntry.getKey()));
            } else {
                sqlSb.append(String.format(" (%s = ?) ", eachEntry.getKey()));
            }
            i++;
        }
        sqlSb.append(")");
        //mysql impl warn: if a database access error occurs or this method is called on a closed connection
        PreparedStatement statement = connection.prepareStatement(sqlSb.toString());
        i = 0;
        //ads的real time表只能是1级分区、且分区列类型是long, 但是这里是需要主键删除的
        for (Entry<String, Integer> each : primaryEntrySet) {
            String columnName = each.getKey();
            int columnSqlType = this.userConfigColumnsMetaData.get(columnName).getLeft();
            int primaryKeyInUserConfigIndex = this.primaryKeyNameIndexMap.get(columnName);
            if (primaryKeyInUserConfigIndex >= this.opColumnIndex) {
                primaryKeyInUserConfigIndex++;
            }
            prepareColumnTypeValue(statement, columnSqlType, record.getColumn(primaryKeyInUserConfigIndex), i, columnName);
            i++;
        }
        sql = ((JDBC4PreparedStatement) statement).asSql();
        DBUtil.closeDBResources(statement, null);
    }
    return sql;
}
Also used : Entry(java.util.Map.Entry) JDBC4PreparedStatement(com.mysql.jdbc.JDBC4PreparedStatement)

Aggregations

JDBC4PreparedStatement (com.mysql.jdbc.JDBC4PreparedStatement)1 Entry (java.util.Map.Entry)1