Search in sources :

Example 6 with ParamSetException

use of com.cubrid.common.ui.spi.util.paramSetter.ParamSetException 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;
}
Also used : HashMap(java.util.HashMap) ParamSetException(com.cubrid.common.ui.spi.util.paramSetter.ParamSetException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DBConnection(com.cubrid.cubridmanager.core.common.jdbc.DBConnection) ArrayList(java.util.ArrayList) ParamSetter(com.cubrid.common.ui.spi.util.paramSetter.ParamSetter) PreparedStatement(java.sql.PreparedStatement) Point(org.eclipse.swt.graphics.Point) PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) CellValue(com.cubrid.common.ui.spi.table.CellValue) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)6 SQLException (java.sql.SQLException)6 CellValue (com.cubrid.common.ui.spi.table.CellValue)5 Map (java.util.Map)5 Point (org.eclipse.swt.graphics.Point)5 PstmtParameter (com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ParamSetter (com.cubrid.common.ui.spi.util.paramSetter.ParamSetter)3 DBConnection (com.cubrid.cubridmanager.core.common.jdbc.DBConnection)3 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 TableItem (org.eclipse.swt.widgets.TableItem)2 ImportColumnData (com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportColumnData)1 ImportRowData (com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData)1 ExportQueryResultDialog (com.cubrid.common.ui.query.dialog.ExportQueryResultDialog)1 QueryResultTableCalcInfo (com.cubrid.common.ui.query.editor.QueryResultTableCalcInfo)1 QueryTableTabItem (com.cubrid.common.ui.query.editor.QueryTableTabItem)1 StatusLineContrItem (com.cubrid.common.ui.spi.contribution.StatusLineContrItem)1