Search in sources :

Example 1 with FieldType

use of io.openmessaging.connector.api.data.FieldType in project rocketmq-externals by apache.

the class Updater method updateRow.

private Boolean updateRow(String dbName, String tableName, Map<Field, Object[]> fieldMap, Integer id) {
    int count = 0;
    PreparedStatement stmt;
    boolean finishUpdate = false;
    String update = "replace into " + dbName + "." + tableName + " set ";
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[1];
        if ("id".equals(fieldName)) {
            if (id == 0) {
                if (count == fieldMap.size())
                    update = update.substring(0, update.length() - 1);
                continue;
            } else {
                fieldValue = id;
            }
        }
        if (fieldValue == null) {
            update += fieldName + " = NULL";
        } else {
            update = typeParser(fieldType, fieldName, fieldValue, update);
        }
        if (count < fieldMap.size()) {
            update += ",";
        }
    }
    try {
        while (!connection.isClosed() && !finishUpdate) {
            stmt = connection.prepareStatement(update);
            int result = stmt.executeUpdate();
            if (result > 0) {
                log.info("replace into table success");
                return true;
            }
            finishUpdate = true;
            stmt.close();
        }
    } catch (SQLException e) {
        log.error("update table error,{}", e);
    }
    return false;
}
Also used : Field(io.openmessaging.connector.api.data.Field) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) FieldType(io.openmessaging.connector.api.data.FieldType)

Example 2 with FieldType

use of io.openmessaging.connector.api.data.FieldType in project rocketmq-externals by apache.

the class Updater method queryAfterUpdateRowId.

private Integer queryAfterUpdateRowId(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    int count = 0, id = 0;
    ResultSet rs;
    PreparedStatement stmt;
    Boolean finishQuery = false;
    String query = "select id from " + dbName + "." + tableName + " where 1=1";
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[1];
        if ("id".equals(fieldName))
            continue;
        if (count <= fieldMap.size()) {
            query += " and ";
        }
        if (fieldValue == null) {
            query += fieldName + " is NULL";
        } else {
            query = typeParser(fieldType, fieldName, fieldValue, query);
        }
    }
    try {
        while (!connection.isClosed() && !finishQuery) {
            stmt = connection.prepareStatement(query);
            rs = stmt.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    id = rs.getInt("id");
                }
                finishQuery = true;
                rs.close();
            }
        }
    } catch (SQLException e) {
        log.error("query table error,{}", e);
    }
    return id;
}
Also used : Field(io.openmessaging.connector.api.data.Field) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) FieldType(io.openmessaging.connector.api.data.FieldType)

Example 3 with FieldType

use of io.openmessaging.connector.api.data.FieldType in project rocketmq-externals by apache.

the class Updater method queryBeforeUpdateRowId.

private Integer queryBeforeUpdateRowId(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    int count = 0, id = 0;
    ResultSet rs;
    PreparedStatement stmt;
    Boolean finishQuery = false;
    String query = "select id from " + dbName + "." + tableName + " where 1=1";
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[0];
        if ("id".equals(fieldName))
            continue;
        if (count <= fieldMap.size()) {
            query += " and ";
        }
        if (fieldValue == null) {
            query += fieldName + " is NULL";
        } else {
            query = typeParser(fieldType, fieldName, fieldValue, query);
        }
    }
    try {
        while (!connection.isClosed() && !finishQuery) {
            stmt = connection.prepareStatement(query);
            rs = stmt.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    id = rs.getInt("id");
                }
                finishQuery = true;
                rs.close();
            }
        }
    } catch (SQLException e) {
        log.error("query table error,{}", e);
    }
    return id;
}
Also used : Field(io.openmessaging.connector.api.data.Field) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) FieldType(io.openmessaging.connector.api.data.FieldType)

Example 4 with FieldType

use of io.openmessaging.connector.api.data.FieldType in project rocketmq-externals by apache.

the class Updater method deleteRow.

private boolean deleteRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    DeleteSelection deleteSelection = QueryBuilder.deleteFrom(dbName, tableName);
    Delete delete = null;
    int count = 0;
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[1];
        if (count == 1) {
            delete = deleteSelection.whereColumn(fieldName).isEqualTo(buildTerm(fieldType, fieldValue));
        } else {
            delete = delete.whereColumn(fieldName).isEqualTo(buildTerm(fieldType, fieldValue));
        }
    }
    boolean finishDelete = false;
    SimpleStatement stmt = delete.build();
    try {
        while (!cqlSession.isClosed() && !finishDelete) {
            ResultSet result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                log.info("delete from table success, executed query {}", delete);
                return true;
            }
            finishDelete = true;
        }
    } catch (Exception e) {
        log.error("delete from table error,{}", e);
    }
    return false;
}
Also used : Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) FieldType(io.openmessaging.connector.api.data.FieldType) Field(io.openmessaging.connector.api.data.Field) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Map(java.util.Map)

Example 5 with FieldType

use of io.openmessaging.connector.api.data.FieldType in project rocketmq-externals by apache.

the class Updater method updateRow.

/**
 * Since we have no way of getting the id of a record, and we cannot get the primary key list of a table,
 * even we can it is not extensible. So we the result sql sentense would be like
 * UPDATE dbName.tableName SET afterUpdateValues WHERE beforeUpdateValues.
 */
private Boolean updateRow(String dbName, String tableName, Map<Field, Object[]> fieldMap) {
    log.info("Updater.updateRow() get called ");
    int count = 0;
    InsertInto insert = QueryBuilder.insertInto(dbName, tableName);
    RegularInsert regularInsert = null;
    for (Map.Entry<Field, Object[]> entry : fieldMap.entrySet()) {
        count++;
        String fieldName = entry.getKey().getName();
        FieldType fieldType = entry.getKey().getType();
        Object fieldValue = entry.getValue()[1];
        if (count == 1) {
            regularInsert = insert.value(fieldName, buildTerm(fieldType, fieldValue));
        } else {
            regularInsert = regularInsert.value(fieldName, buildTerm(fieldType, fieldValue));
        }
    }
    SimpleStatement stmt;
    boolean finishUpdate = false;
    log.info("trying to execute sql query,{}", regularInsert.asCql());
    try {
        while (!cqlSession.isClosed() && !finishUpdate) {
            stmt = regularInsert.build();
            ResultSet result = cqlSession.execute(stmt);
            if (result.wasApplied()) {
                log.info("update table success, executed cql query {}", regularInsert.asCql());
                return true;
            }
            finishUpdate = true;
        }
    } catch (Exception e) {
        log.error("update table error,{}", e);
    }
    return false;
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) FieldType(io.openmessaging.connector.api.data.FieldType) Field(io.openmessaging.connector.api.data.Field) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) InsertInto(com.datastax.oss.driver.api.querybuilder.insert.InsertInto) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Map(java.util.Map)

Aggregations

Field (io.openmessaging.connector.api.data.Field)6 FieldType (io.openmessaging.connector.api.data.FieldType)6 Map (java.util.Map)5 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 ResultSet (java.sql.ResultSet)2 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)1 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)1 InsertInto (com.datastax.oss.driver.api.querybuilder.insert.InsertInto)1 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)1 Schema (io.openmessaging.connector.api.data.Schema)1 SinkDataEntry (io.openmessaging.connector.api.data.SinkDataEntry)1