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