use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.
the class QueryExecuter method deleteValues.
/**
* Delete values on the query result editor.
*
* @param queryConn Connection
* @throws SQLException the exception
*/
private Map<String, Map<String, CellValue>> deleteValues() throws SQLException, ParamSetException {
ParamSetter paramSetter = new ParamSetter();
Map<String, Map<String, CellValue>> successedMap = new HashMap<String, Map<String, CellValue>>();
Connection conn = connection.checkAndConnect();
try {
String tableName = UIQueryUtil.getTableNameFromQuery(conn, query);
String escapedTable = QuerySyntax.escapeKeyword(tableName);
if (tableName == null) {
CommonUITool.openErrorBox(Messages.errModifiedOneTable);
return successedMap;
}
PreparedStatement pstmt = null;
List<ColumnInfo> allColumnList = getAllColumnList();
for (String key : delValues.keySet()) {
Map<String, CellValue> valuesMap = delValues.get(key);
if (valuesMap == null) {
continue;
}
try {
List<ColumnInfo> colInfoList = new ArrayList<ColumnInfo>();
List<ColumnInfo> unPColInfoList = new ArrayList<ColumnInfo>();
for (int i = 0; i < allColumnList.size(); i++) {
ColumnInfo colInfo = allColumnList.get(i);
if (queryEditor.isIgnoreType(colInfo.getType())) {
continue;
}
CellValue value = valuesMap.get(colInfo.getIndex());
if (value == null || value.getValue() == null) {
continue;
}
if (DataType.DATATYPE_NATIONAL_CHARACTER.equalsIgnoreCase(colInfo.getType()) || DataType.DATATYPE_NCHAR_VARYING.equalsIgnoreCase(colInfo.getType()) || DataType.DATATYPE_NCHAR.equalsIgnoreCase(colInfo.getType())) {
unPColInfoList.add(colInfo);
continue;
}
if ((DataType.DATATYPE_BIT.equalsIgnoreCase(colInfo.getType()) || DataType.DATATYPE_BIT_VARYING.equalsIgnoreCase(colInfo.getType())) && value.getValue() instanceof String) {
unPColInfoList.add(colInfo);
continue;
}
colInfoList.add(colInfo);
}
StringBuilder sqlBuffer = new StringBuilder();
sqlBuffer.append("DELETE FROM ").append(QuerySyntax.escapeKeyword(escapedTable)).append(" WHERE ");
List<PstmtParameter> pstmtParaList = new ArrayList<PstmtParameter>();
int paramCount = 1;
for (ColumnInfo columnInfo : colInfoList) {
if (paramCount > 1) {
sqlBuffer.append(" AND ");
}
sqlBuffer.append(QuerySyntax.escapeKeyword(columnInfo.getName())).append(" = ? ");
CellValue value = valuesMap.get(columnInfo.getIndex());
PstmtParameter pstmtParameter = new PstmtParameter(columnInfo.getName(), paramCount, columnInfo.getComleteType(), value.getValue());
pstmtParaList.add(pstmtParameter);
paramCount++;
}
for (ColumnInfo columnInfo : unPColInfoList) {
if (paramCount > 1) {
sqlBuffer.append(" AND ");
}
sqlBuffer.append(QuerySyntax.escapeKeyword(columnInfo.getName())).append("=");
CellValue cellValue = valuesMap.get(columnInfo.getIndex());
String dataType = DataType.makeType(columnInfo.getType(), columnInfo.getChildElementType(), columnInfo.getPrecision(), columnInfo.getScale());
FormatDataResult result = DBAttrTypeFormatter.format(dataType, cellValue.getStringValue(), null, false, charset, false);
if (result.isSuccess()) {
sqlBuffer.append(result.getFormatedString());
} else {
throw new ParamSetException("Format data \"" + cellValue.getStringValue() + "\"error for data type " + dataType);
}
paramCount++;
}
pstmt = conn.prepareStatement(sqlBuffer.toString());
for (PstmtParameter pstmtParameter : pstmtParaList) {
paramSetter.handle(pstmt, pstmtParameter);
}
pstmt.executeUpdate();
successedMap.put(key, valuesMap);
if (!connection.isAutoCommit() && queryEditor.getConnection() == connection) {
queryEditor.setHaveActiveTransaction(true);
}
} catch (SQLException ex) {
if (successedMap.containsKey(key)) {
successedMap.remove(key);
}
LOGGER.error("", ex);
throw ex;
} finally {
QueryUtil.freeQuery(pstmt);
}
}
} finally {
if (connection != null && connection.isAutoClosable()) {
connection.commit();
connection.close();
}
}
return successedMap;
}
Aggregations