Search in sources :

Example 6 with PstmtParameter

use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.

the class AbsImportRunnable method processRowData.

protected ImportRowData processRowData(String[] columnArray, String[] columnPattern, int currentRow, File parentFile) throws StopPerformException {
    // FIXME move this logic to core module
    ImportRowData rowData = new ImportRowData(currentRow);
    ImportColumnData columnData = null;
    boolean isSuccess = false;
    try {
        for (int j = 0; j < tableConfig.getPstmList().size(); j++) {
            PstmtParameter pstmtParameter = tableConfig.getPstmList().get(j);
            int column = Integer.parseInt(pstmtParameter.getStringParamValue());
            String content = null;
            String pattern = null;
            if (columnArray.length > column) {
                content = columnArray[column];
            }
            if (columnPattern != null && columnPattern.length > column) {
                pattern = columnPattern[column];
            }
            /*Recored the origin data*/
            columnData = new ImportColumnData(content);
            rowData.getColumnList().add(columnData);
            String dataType = DataType.getRealType(pstmtParameter.getDataType());
            Object value = getRealValueForImport(dataType, content, parentFile);
            try {
                PstmtParameter parameter = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), value);
                parameter.setCharSet(importConfig.getFilesCharset());
                if (StringUtil.isNotEmpty(pattern)) {
                    parameter.setDatePattern(pattern);
                }
                if (value != null && value instanceof File) {
                    parameter.setFileValue(true);
                }
                setPreparedStatementValue(pStmt, parameter, dbCharset);
                columnData.setStatus(ImportStatus.STATUS_FORMAT_SUCCESS);
                isSuccess = true;
            } catch (ParamSetException ex) {
                isSuccess = false;
                LOGGER.debug(ex.getMessage());
            } catch (SQLException ex) {
                isSuccess = false;
                LOGGER.debug(ex.getMessage());
            } finally {
                if (!isSuccess) {
                    columnData.setStatus(ImportStatus.STATUS_FORMAT_FAILED);
                    dataTypeErrorHandling(getErrorMsg(currentRow, column, dataType));
                    PstmtParameter parameter = new PstmtParameter(pstmtParameter.getParamName(), pstmtParameter.getParamIndex(), pstmtParameter.getDataType(), null);
                    parameter.setCharSet(importConfig.getFilesCharset());
                    try {
                        setPreparedStatementNULL(pStmt, parameter);
                    } catch (SQLException e) {
                        LOGGER.debug(e.getMessage());
                    }
                }
            }
        }
    } catch (OutOfMemoryError error) {
        throw new RuntimeException(error);
    }
    return rowData;
}
Also used : ImportRowData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportRowData) ImportColumnData(com.cubrid.common.ui.cubrid.table.dialog.imp.model.ImportColumnData) PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) ParamSetException(com.cubrid.common.ui.spi.util.paramSetter.ParamSetException) SQLException(java.sql.SQLException) File(java.io.File)

Example 7 with PstmtParameter

use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.

the class AbsExportDataHandler method getStatement.

protected CUBRIDPreparedStatementProxy getStatement(Connection conn, String sql, String tableName) throws SQLException {
    // FIXME move this logic to core module
    CUBRIDPreparedStatementProxy stmt = QueryExecuter.getStatement(conn, sql, false, true);
    List<PstmtParameter> pstmList = exportConfig.getParameterList(tableName);
    if (pstmList != null) {
        String charset = dbInfo.getCharSet();
        for (PstmtParameter pstmtParameter : pstmList) {
            FieldHandlerUtils.setPreparedStatementValue(pstmtParameter, stmt, charset);
        }
    }
    return stmt;
}
Also used : PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) CUBRIDPreparedStatementProxy(com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy)

Example 8 with PstmtParameter

use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.

the class AddTableFileDialog method okPressed.

protected void okPressed() {
    if (isOpenedFile) {
        // FIXME move this logic to core module
        int fromListCount = fromList.getItemCount();
        String[] fromListItems = fromList.getItems();
        String[] toListItems = new String[fromListCount];
        for (int i = 0; i < fromListCount; i++) {
            toListItems[i] = toList.getItem(i);
        }
        List<PstmtParameter> parameterList = new ArrayList<PstmtParameter>();
        for (int i = 0; i < fromListCount; i++) {
            String name = toListItems[i];
            if (name.trim().length() == 0) {
                continue;
            }
            String type = colTypeList.get(colNameList.indexOf(name));
            String excelColumn = fromListItems[i];
            String value = "0";
            for (int j = 0; j < fileColumnList.size(); j++) {
                if (excelColumn.equals(fileColumnList.get(j))) {
                    value = j + "";
                    break;
                }
            }
            PstmtParameter pstmtParameter = new PstmtParameter(name, i + 1, type, value);
            pstmtParameter.setFileColumnName(excelColumn);
            parameterList.add(pstmtParameter);
        }
        ICubridNode classNode = mappingComposite.getTableNode(tableName);
        classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
        File file = new File(fileNameTxt.getText());
        classNode.setData(ImportObjectLabelProvider.FILE_PAHT, file.getName());
        classNode.setData(ImportObjectLabelProvider.IS_MAPPED, true);
        mappingComposite.refreshTable();
        TableConfig tableConfig = configModel.getTableConfig(classNode.getName());
        if (tableConfig == null) {
            tableConfig = new TableConfig(classNode.getName());
        }
        tableConfig.setPstmList(parameterList);
        tableConfig.setFilePath(fileNameTxt.getText());
        tableConfig.setInsertDML(getInsertDML());
        tableConfig.setLineCount(totalLine);
        tableConfig.setFirstRowAsColumn(firstRowAsColumnBtn.getSelection());
        tableConfig.setMapped(true);
        Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
        if (isNew != null && (Boolean) isNew) {
            tableConfig.setCreateDDL(classNode.getData(ImportObjectLabelProvider.CREATE_DDL).toString());
        }
        configModel.addTableConfig(tableConfig);
    }
    super.okPressed();
}
Also used : PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) ArrayList(java.util.ArrayList) ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) File(java.io.File)

Example 9 with PstmtParameter

use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.

the class TableConfig method clone.

/**
	 * Clone a object
	 */
public TableConfig clone() {
    TableConfig tableConfig = null;
    try {
        tableConfig = (TableConfig) super.clone();
    } catch (CloneNotSupportedException e) {
    }
    List<PstmtParameter> pstmList = new ArrayList<PstmtParameter>();
    for (PstmtParameter pstm : tableConfig.getPstmList()) {
        pstmList.add(pstm.clone());
    }
    tableConfig.setPstmList(pstmList);
    return tableConfig;
}
Also used : PstmtParameter(com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter) ArrayList(java.util.ArrayList)

Example 10 with PstmtParameter

use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.

the class QueryExecuter method insertValues.

/**
	 * Insert values on the query result editor.
	 *
	 * @return
	 * @throws SQLException
	 */
private Map<String, Map<String, CellValue>> insertValues() throws SQLException, ParamSetException {
    Map<String, Map<String, CellValue>> successedMap = new HashMap<String, Map<String, CellValue>>();
    if (insValues == null || insValues.size() == 0) {
        return successedMap;
    }
    ParamSetter paramSetter = new ParamSetter();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(3);
    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 : insValues.keySet()) {
            Map<String, CellValue> valuesMap = insValues.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("INSERT INTO ").append(escapedTable).append(" (");
                int paramCount = 0;
                for (ColumnInfo columnInfo : colInfoList) {
                    if (paramCount > 0) {
                        sqlBuffer.append(",");
                    }
                    sqlBuffer.append(QuerySyntax.escapeKeyword(columnInfo.getName()));
                    paramCount++;
                }
                for (ColumnInfo columnInfo : unPColInfoList) {
                    if (paramCount > 0) {
                        sqlBuffer.append(",");
                    }
                    sqlBuffer.append(QuerySyntax.escapeKeyword(columnInfo.getName()));
                    paramCount++;
                }
                sqlBuffer.append(") VALUES (");
                int dataIndex = 1;
                List<PstmtParameter> pstmtParaList = new ArrayList<PstmtParameter>();
                for (ColumnInfo columnInfo : colInfoList) {
                    if (dataIndex > 1) {
                        sqlBuffer.append(",");
                    }
                    sqlBuffer.append("?");
                    CellValue value = valuesMap.get(columnInfo.getIndex());
                    PstmtParameter pstmtParameter = new PstmtParameter(columnInfo.getName(), dataIndex, columnInfo.getComleteType(), value.getValue());
                    pstmtParaList.add(pstmtParameter);
                    dataIndex++;
                }
                String charset = getDatabaseInfo() != null ? getDatabaseInfo().getCharSet() : null;
                for (ColumnInfo columnInfo : unPColInfoList) {
                    if (dataIndex > 1) {
                        sqlBuffer.append(",");
                    }
                    CellValue value = valuesMap.get(columnInfo.getIndex());
                    String dataType = DataType.makeType(columnInfo.getType(), columnInfo.getChildElementType(), columnInfo.getPrecision(), columnInfo.getScale());
                    FormatDataResult result = DBAttrTypeFormatter.format(dataType, value.getStringValue(), null, false, charset, false);
                    if (result.isSuccess()) {
                        sqlBuffer.append(result.getFormatedString());
                    } else {
                        throw new ParamSetException("Format data \"" + value.getStringValue() + "\"error for data type " + dataType);
                    }
                }
                sqlBuffer.append(")");
                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 e) {
                if (successedMap.containsKey(key)) {
                    successedMap.remove(key);
                }
                LOGGER.error("", e);
                logMessageText.setText(e.getLocalizedMessage());
                throw e;
            } 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) NumberFormat(java.text.NumberFormat)

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