Search in sources :

Example 11 with PstmtParameter

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

PstmtParameter (com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter)11 ArrayList (java.util.ArrayList)7 ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)5 SQLException (java.sql.SQLException)5 Point (org.eclipse.swt.graphics.Point)4 CellValue (com.cubrid.common.ui.spi.table.CellValue)3 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 File (java.io.File)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)2 IXMLMemento (com.cubrid.cubridmanager.core.common.xml.IXMLMemento)2 CUBRIDPreparedStatementProxy (com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)2 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)1 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)1 ImportColumnData (com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportColumnData)1