use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.
the class FileToTableMappingComposite method parseData.
@SuppressWarnings("unchecked")
private void parseData(final Map<String, String> tableToFile) throws Exception {
allTableList = (List<ICubridNode>) treeViewer.getInput();
final List<ICubridNode> newMappedNodeList = new ArrayList<ICubridNode>();
final ImportConfig importConfig = importSettingPage.getImportDataWizard().getImportConfig();
ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
progress.setCancelable(true);
progress.run(true, true, new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException {
monitor.beginTask(Messages.taskParsingData, tableToFile.size());
for (Entry<String, String> entry : tableToFile.entrySet()) {
// FIXME move this logic to core module
String tableName = entry.getKey();
String filePath = entry.getValue();
File file = new File(filePath);
String fileName = file.getName();
monitor.subTask(Messages.bind(Messages.taskParsingFile, fileName));
List<String> fileColumnList = new ArrayList<String>();
List<String> firstRowColsLst = new ArrayList<String>();
List<String> colNameList = new ArrayList<String>();
List<String> colTypeList = new ArrayList<String>();
int totalLine = 0;
List<Integer> itemsNumberOfSheets = null;
/*Find the class node*/
ICubridNode classNode = getTableNode(tableName);
/*Process file*/
ImportFileHandler importFileHandler = ImportFileHandlerFactory.getHandler(file.getAbsolutePath(), importConfig);
ImportFileDescription ifd = getFileDescription(importFileHandler);
firstRowColsLst.addAll(ifd.getFirstRowCols());
totalLine = ifd.getTotalCount();
itemsNumberOfSheets = ifd.getItemsNumberOfSheets();
boolean isFirstLineAsColumnName = isFirstLineAsColumnName(tableName);
fillInFromList(fileColumnList, firstRowColsLst, isFirstLineAsColumnName);
if (isFirstLineAsColumnName) {
handleSelectEventForFirstRowAsColBtn(itemsNumberOfSheets, fileColumnList, firstRowColsLst, totalLine, isFirstLineAsColumnName);
}
if (classNode != null) {
Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
if (isNew == null || !(Boolean) isNew) {
SchemaInfo schemaInfo = database.getDatabaseInfo().getSchemaInfo(tableName);
List<DBAttribute> attributes = schemaInfo == null ? null : schemaInfo.getAttributes();
if (attributes == null) {
return;
}
for (DBAttribute attr : attributes) {
String column = attr.getName();
String dataType = attr.getType();
colNameList.add(column);
colTypeList.add(dataType);
}
} else {
removeClassNode(classNode);
classNode = createClassNode(tableName, isFirstLineAsColumnName(tableName), firstRowColsLst, colNameList, colTypeList);
}
} else if (importConfig.isCreateTableAccordingData()) {
classNode = createClassNode(tableName, isFirstLineAsColumnName(tableName), firstRowColsLst, colNameList, colTypeList);
} else {
failedFileList.add(file.getAbsolutePath());
continue;
}
if (fileColumnList.size() == colNameList.size()) {
classNode.setData(ImportObjectLabelProvider.IS_MAPPED, true);
classNode.setData(ImportObjectLabelProvider.FILE_PAHT, fileName);
classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
List<PstmtParameter> parameterList = new ArrayList<PstmtParameter>();
for (int i = 0; i < fileColumnList.size(); i++) {
PstmtParameter pstmtParameter = new PstmtParameter(colNameList.get(i), i + 1, colTypeList.get(i), String.valueOf(i));
pstmtParameter.setFileColumnName(fileColumnList.get(i));
parameterList.add(pstmtParameter);
}
TableConfig tableConfig = new TableConfig(classNode.getName());
tableConfig.setPstmList(parameterList);
tableConfig.setFilePath(file.getAbsolutePath());
tableConfig.setInsertDML(getInsertDML(classNode, colNameList, colTypeList));
tableConfig.setLineCount(totalLine);
tableConfig.setMapped(true);
Object isNew = classNode.getData(ImportObjectLabelProvider.IS_NEW);
if (isNew != null && (Boolean) isNew) {
tableConfig.setCreateDDL(classNode.getData(ImportObjectLabelProvider.CREATE_DDL).toString());
}
importConfig.addTableConfig(tableConfig);
newMappedNodeList.add(classNode);
} else {
classNode.setData(ImportObjectLabelProvider.IS_MAPPED, false);
classNode.setData(ImportObjectLabelProvider.FILE_PAHT, fileName);
classNode.setData(ImportObjectLabelProvider.ROW_COUNT, totalLine);
TableConfig tableConfig = importConfig.getSelectedMap().get(classNode.getName());
if (tableConfig != null) {
tableConfig.setMapped(false);
tableConfig.setFilePath(filePath);
tableConfig.setLineCount(totalLine);
tableConfig.getPstmList().clear();
tableConfig.setInsertDML("");
}
failedFileList.add(fileName);
}
monitor.worked(1);
if (monitor.isCanceled()) {
return;
}
}
}
});
for (ICubridNode node : allTableList) {
if (newMappedNodeList.contains(node)) {
treeViewer.setChecked(node, true);
}
}
treeViewer.refresh();
}
use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.
the class ImportConfigManager method saveConfigs.
/**
*
* Save the import configs to plug-in preference
*
*/
public void saveConfigs() {
synchronized (this) {
XMLMemento memento = XMLMemento.createWriteRoot("databases");
Iterator<ImportConfig> iterator = importConfigList.iterator();
while (iterator.hasNext()) {
ImportConfig model = (ImportConfig) iterator.next();
model.setHistory(true);
IXMLMemento configMemento = memento.createChild("config");
configMemento.putString("name", model.getName());
configMemento.putString("id", model.getId());
configMemento.putInteger("importType", model.getImportType());
configMemento.putInteger("errorHandle", model.getErrorHandle());
configMemento.putString("fileCharset", model.getFilesCharset());
configMemento.putBoolean("isCreateTable", model.isCreateTableAccordingData());
configMemento.putInteger("threadCount", model.getThreadCount());
configMemento.putInteger("commitCount", model.getCommitLine());
configMemento.putString("rowDelimiter", model.getRowDelimiter());
configMemento.putString("columnDelimiter", model.getColumnDelimiter());
configMemento.putBoolean("isHaMode", model.isHaMode());
IXMLMemento nullValuesMemento = configMemento.createChild("nullValues");
for (String nullValue : model.getNullValueList()) {
IXMLMemento nullValueMemento = nullValuesMemento.createChild("nullValue");
nullValueMemento.putTextData(nullValue);
}
IXMLMemento tableConfigsMemento = configMemento.createChild("tableConfigs");
for (TableConfig tableConfig : model.getSelectedMap().values()) {
IXMLMemento tableConfigMemento = tableConfigsMemento.createChild("tableConfig");
for (PstmtParameter pstm : tableConfig.getPstmList()) {
IXMLMemento pstmMemento = tableConfigMemento.createChild("pstm");
pstmMemento.putString("paramName", pstm.getParamName());
pstmMemento.putString("paramType", pstm.getDataType());
pstmMemento.putString("paramValue", pstm.getStringParamValue());
pstmMemento.putInteger("paramIndex", pstm.getParamIndex());
pstmMemento.putString("fileColumnName", pstm.getFileColumnName());
}
tableConfigMemento.putString("name", tableConfig.getName());
tableConfigMemento.putString("filePath", tableConfig.getFilePath());
tableConfigMemento.putString("fileType", tableConfig.getFileType() == null ? "" : tableConfig.getFileType());
tableConfigMemento.putInteger("lineCount", tableConfig.getLineCount());
tableConfigMemento.putString("createDDL", tableConfig.getCreateDDL() == null ? "" : tableConfig.getCreateDDL());
tableConfigMemento.putString("insertDML", tableConfig.getInsertDML() == null ? "" : tableConfig.getInsertDML());
tableConfigMemento.putBoolean("isFirstRowAsColumn", tableConfig.isFirstRowAsColumn());
}
}
PersistUtils.saveXMLMemento(CommonUIPlugin.PLUGIN_ID, COM_CUBRID_IMPORT_SETTING, memento);
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.
the class ImportConfigManager method loadImportConfigs.
/**
*
* Load the added import configs from plug-in preference
*
*/
protected void loadImportConfigs() {
synchronized (this) {
importConfigList.clear();
IXMLMemento memento = PersistUtils.getXMLMemento(CommonUIPlugin.PLUGIN_ID, COM_CUBRID_IMPORT_SETTING);
IXMLMemento[] children = memento == null ? null : memento.getChildren("config");
for (int i = 0; children != null && i < children.length; i++) {
String name = children[i].getString("name");
String id = children[i].getString("id");
int importType = children[i].getInteger("importType");
int errorHandle = children[i].getInteger("errorHandle");
String fileCharset = children[i].getString("fileCharset");
boolean isCreateTable = children[i].getBoolean("isCreateTable");
int threadCount = children[i].getInteger("threadCount");
int commitCount = children[i].getInteger("commitCount");
String rowDelimiter = children[i].getString("rowDelimiter");
String columnDelimiter = children[i].getString("columnDelimiter");
boolean isHaMode = children[i].getBoolean("isHaMode");
List<String> nullValues = new ArrayList<String>();
IXMLMemento nullValuesMemento = children[i].getChild("nullValues");
IXMLMemento[] nullValueArray = nullValuesMemento.getChildren("nullValue");
for (IXMLMemento nullValueMemento : nullValueArray) {
if (nullValueMemento.getTextData() != null) {
nullValues.add(nullValueMemento.getTextData());
}
}
ImportConfig importConfig = new ImportConfig();
importConfig.setName(name);
importConfig.setId(id);
importConfig.setType(importType);
importConfig.setErrorHandle(errorHandle);
importConfig.setFilesCharset(fileCharset);
importConfig.setCreateTableAccordingData(isCreateTable);
importConfig.setThreadCount(threadCount);
importConfig.setCommitLine(commitCount);
importConfig.setRowDelimiter(rowDelimiter);
importConfig.setColumnDelimiter(columnDelimiter);
importConfig.setNullValueList(nullValues);
importConfig.setHistory(true);
importConfig.setHaMode(isHaMode);
IXMLMemento tableConfigsMemento = children[i].getChild("tableConfigs");
IXMLMemento[] tableConfigArray = tableConfigsMemento.getChildren("tableConfig");
for (IXMLMemento configMemento : tableConfigArray) {
String tableName = configMemento.getString("name");
String filePath = configMemento.getString("filePath");
String fileType = configMemento.getString("fileType");
String createDDL = configMemento.getString("createDDL");
String insertDML = configMemento.getString("insertDML");
Integer lineCount = configMemento.getInteger("lineCount");
boolean isFirstRowAsColumn = configMemento.getBoolean("isFirstRowAsColumn");
List<PstmtParameter> pstmList = new ArrayList<PstmtParameter>();
IXMLMemento[] pstmArray = configMemento.getChildren("pstm");
for (IXMLMemento pstmMemento : pstmArray) {
String paramName = pstmMemento.getString("paramName");
String paramType = pstmMemento.getString("paramType");
String paramValue = pstmMemento.getString("paramValue");
int paramIndex = pstmMemento.getInteger("paramIndex");
String fileColumnName = pstmMemento.getString("fileColumnName");
PstmtParameter pstm = new PstmtParameter(paramName, paramIndex, paramType, paramValue);
pstm.setFileColumnName(fileColumnName);
pstmList.add(pstm);
}
TableConfig tableConfig = new TableConfig(tableName);
tableConfig.setFilePath(filePath);
tableConfig.setFileType(fileType);
tableConfig.setCreateDDL(createDDL);
tableConfig.setInsertDML(insertDML);
tableConfig.setFirstRowAsColumn(isFirstRowAsColumn);
tableConfig.setPstmList(pstmList);
if (lineCount != null) {
tableConfig.setLineCount(lineCount);
}
importConfig.addTableConfig(tableConfig);
}
importConfigList.add(importConfig);
}
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.
the class QueryExecuter method reloadQuery.
public void reloadQuery() {
CUBRIDPreparedStatementProxy pstmt = null;
CUBRIDResultSetProxy prs = null;
try {
pstmt = QueryExecuter.getStatement(connection.checkAndConnectQuietly(), query, false, false);
if (parameterList != null) {
for (PstmtParameter pstmtParameter : parameterList) {
FieldHandlerUtils.setPreparedStatementValue(pstmtParameter, pstmt, charset);
}
}
if (pstmt.hasResultSet()) {
pstmt.setQueryInfo(false);
pstmt.setOnlyQueryPlan(false);
pstmt.executeQuery();
prs = (CUBRIDResultSetProxy) pstmt.getResultSet();
if (allColumnList != null) {
allColumnList.clear();
}
if (allDataList != null) {
allDataList.clear();
}
int page = queryInfo.getCurrentPage();
makeResult(prs);
queryInfo.setCurrentPage(page);
makeItem();
updateActions();
}
} catch (final Exception ee) {
LOGGER.error("execute SQL failed sql at query editor : " + query + " error message: " + ee);
ee.printStackTrace();
} finally {
QueryUtil.freeQuery(pstmt, prs);
pstmt = null;
prs = null;
}
}
use of com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter in project cubrid-manager by CUBRID.
the class QueryExecuter method updateValues.
/**
* Update values on the query result editor.
*
* @param queryConn Connection
* @return
* @throws SQLException
* @throws ParamSetException
*/
private Map<String, Map<String, CellValue>> updateValues() throws SQLException, ParamSetException {
Map<String, Map<String, CellValue>> successedMap = new HashMap<String, Map<String, CellValue>>();
ParamSetter paramSetter = new ParamSetter();
if (oldValues == null || oldValues.size() == 0) {
return successedMap;
}
if (newValues == null || newValues.size() == 0) {
return successedMap;
}
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;
}
List<ColumnInfo> colInfoList = getAllColumnList();
PreparedStatement pstmt = null;
for (String key : oldValues.keySet()) {
try {
Map<String, CellValue> oldValueMap = oldValues.get(key);
Map<String, CellValue> newValueMap = newValues.get(key);
if (oldValueMap == null || oldValueMap.size() == 0 || newValueMap == null || newValueMap.size() == 0) {
continue;
}
StringBuilder updateSQLBuffer = new StringBuilder();
List<ColumnInfo> updatedColInfoList = new ArrayList<ColumnInfo>();
List<CellValue> newValueList = new ArrayList<CellValue>();
for (int i = 0; i < colInfoList.size(); i++) {
ColumnInfo colInfo = colInfoList.get(i);
CellValue newValue = newValueMap.get(colInfo.getIndex());
CellValue oldValue = oldValueMap.get(colInfo.getIndex());
if ((oldValue == null && newValue != null) || (newValue == null && oldValue != null)) {
newValueList.add(newValue);
updatedColInfoList.add(colInfo);
} else if (oldValue != null && newValue != null && !oldValue.equals(newValue)) {
newValueList.add(newValue);
updatedColInfoList.add(colInfo);
}
}
if (updatedColInfoList.isEmpty()) {
continue;
}
updateSQLBuffer.append("UPDATE ").append(escapedTable).append(" SET ");
StringBuilder setSQLBf = new StringBuilder();
List<PstmtParameter> pstmtParaList = new ArrayList<PstmtParameter>();
int valueParamIndex = 1;
for (int i = 0; i < updatedColInfoList.size(); i++) {
ColumnInfo colInfo = updatedColInfoList.get(i);
CellValue newValue = newValueMap.get(colInfo.getIndex());
String colName = colInfo.getName();
if (queryEditor.isIgnoreType(colInfo.getType())) {
continue;
}
if (setSQLBf.length() > 0) {
setSQLBf.append(", ");
}
CellValue cellValue = newValueMap.get(colInfo.getIndex());
if (DataType.DATATYPE_NATIONAL_CHARACTER.equalsIgnoreCase(colInfo.getType()) || DataType.DATATYPE_NCHAR_VARYING.equalsIgnoreCase(colInfo.getType()) || DataType.DATATYPE_NCHAR.equalsIgnoreCase(colInfo.getType())) {
String dataType = DataType.makeType(colInfo.getType(), colInfo.getChildElementType(), colInfo.getPrecision(), colInfo.getScale());
String charset = getDatabaseInfo() != null ? getDatabaseInfo().getCharSet() : null;
FormatDataResult result = DBAttrTypeFormatter.format(dataType, cellValue.getStringValue(), null, false, charset, false);
if (result.isSuccess()) {
setSQLBf.append(QuerySyntax.escapeKeyword(colName));
setSQLBf.append(" = ").append(result.getFormatedString());
} else {
throw new ParamSetException("Format data \"" + cellValue.getStringValue() + "\"error for data type " + dataType);
}
} else if ((DataType.DATATYPE_BIT.equalsIgnoreCase(colInfo.getType()) || DataType.DATATYPE_BIT_VARYING.equalsIgnoreCase(colInfo.getType())) && newValue.getValue() instanceof String) {
String dataType = DataType.makeType(colInfo.getType(), colInfo.getChildElementType(), colInfo.getPrecision(), colInfo.getScale());
String charset = getDatabaseInfo() != null ? getDatabaseInfo().getCharSet() : null;
FormatDataResult result = DBAttrTypeFormatter.format(dataType, cellValue.getStringValue(), null, false, charset, false);
setSQLBf.append(QuerySyntax.escapeKeyword(colName));
setSQLBf.append(" = ").append(result.getFormatedString());
} else {
setSQLBf.append(QuerySyntax.escapeKeyword(colName)).append(" = ?");
PstmtParameter pstmtParameter = new PstmtParameter(colInfo.getName(), valueParamIndex++, colInfo.getComleteType(), cellValue.getValue());
pstmtParaList.add(pstmtParameter);
}
}
if (setSQLBf.length() < 1) {
continue;
}
updateSQLBuffer.append(setSQLBf);
updateSQLBuffer.append(" WHERE ");
List<String> pkList = UIQueryUtil.getPkList(getDatabaseInfo(), tableName);
int pkParamIndex = 0;
for (int i = 0; i < newValueMap.size(); i++) {
ColumnInfo colInfo = ((ColumnInfo) getAllColumnList().get(i));
String col = colInfo.getName();
if (!pkList.contains(col)) {
continue;
}
if (queryEditor.isIgnoreType(colInfo.getType())) {
continue;
}
if (pkParamIndex > 0) {
updateSQLBuffer.append(" AND ");
}
updateSQLBuffer.append(QuerySyntax.escapeKeyword(col)).append(" = ?");
CellValue object = oldValueMap.get(colInfo.getIndex());
PstmtParameter pstmtParameter = new PstmtParameter(colInfo.getName(), valueParamIndex++, colInfo.getComleteType(), object.getValue());
pstmtParaList.add(pstmtParameter);
pkParamIndex++;
}
pstmt = conn.prepareStatement(updateSQLBuffer.toString());
for (PstmtParameter pstmtParameter : pstmtParaList) {
paramSetter.handle(pstmt, pstmtParameter);
}
pstmt.executeUpdate();
successedMap.put(key, newValueMap);
if (!connection.isAutoCommit() && queryEditor.getConnection() == connection) {
queryEditor.setHaveActiveTransaction(true);
}
for (ColumnInfo colInfo : updatedColInfoList) {
CellValue newValue = newValueMap.get(colInfo.getIndex());
CellValue oldValue = oldValueMap.get(colInfo.getIndex());
if (newValue != null && oldValue != null) {
oldValue.setValue(newValue.getValue());
}
}
} 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