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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations