use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.
the class PstmtOneDataDialog method validate.
/**
*
* Validate data
*
* @param data The String
* @param item The TableItem
* @return boolean
*/
private boolean validate(String data, TableItem item) {
setErrorMessage(null);
String paraName = item.getText(0);
String paraType = item.getText(1);
if (!validateType(paraName, paraType)) {
return false;
}
if (DBAttrTypeFormatter.isMuchValueType(paraType) && DBAttrTypeFormatter.isFilePath(data)) {
return true;
}
/*For bug TOOLS-3119*/
int index = parameterTable.indexOf(item);
if (data.length() > 0) {
FormatDataResult formatDataResult = DBAttrTypeFormatter.format(DataType.getRealType(paraType), DataType.NULL_EXPORT_FORMAT.equals(data) ? null : data, false, database.getDatabaseInfo().getCharSet(), true);
if (!formatDataResult.isSuccess()) {
setErrorMessage(Messages.bind(Messages.errParaTypeValueMapping, new String[] { String.valueOf(index + 1), item.getText(0), DataType.getRealType(paraType) }));
return false;
}
} else {
setErrorMessage(Messages.bind(Messages.msgParaValue, new String[] { paraName, String.valueOf(index + 1) }));
return false;
}
return true;
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.
the class BLOBCellPopupDialog method validate.
private boolean validate() {
setErrorMessage(null);
String content = "";
if (isEditable && columnValueText != null && !columnValueText.isDisposed()) {
content = columnValueText.getText();
if (DataType.isBitDataType(columnInfo.getType()) || DataType.isBitVaryingDataType(columnInfo.getType())) {
String dataType = DataType.makeType(columnInfo.getType(), columnInfo.getChildElementType(), columnInfo.getPrecision(), columnInfo.getScale());
FormatDataResult result = DBAttrTypeFormatter.format(dataType, content, null, false, fileCharsetCombo.getText(), true);
if (!result.isSuccess()) {
String msg = Messages.bind(com.cubrid.common.ui.query.Messages.errTextTypeNotMatch, dataType);
setErrorMessage(msg);
return false;
}
}
}
return true;
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult 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;
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult 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;
}
use of com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult in project cubrid-manager by CUBRID.
the class EditVirtualTableDialog method checkValid.
/**
* Where info is right, if not valid, pop-up dialog.
*
* @return if all are correct
*/
private boolean checkValid() {
if (!verifyTableName()) {
return false;
}
if (columnsTable.getItemCount() == 0) {
CommonUITool.openErrorBox(Messages.noAttributes);
return false;
}
//auto increment
SchemaInfo newSchemaInfo = getNewSchemaInfo();
DBAttribute attr = newSchemaInfo.getAutoIncrementColumn();
if (attr != null && StringUtil.isNotEmpty(attr.getDefault())) {
CommonUITool.openErrorBox(Messages.errCanNotSetAIOnDefault);
return false;
}
//default value
List<DBAttribute> attrList = newSchemaInfo.getAttributes();
for (DBAttribute attribute : attrList) {
String defaultValue = attribute.getDefault();
if (StringUtil.isEmpty(defaultValue)) {
continue;
}
String revisedType = DataType.reviseDataType(attribute.getType());
FormatDataResult formatDataResult = DBAttrTypeFormatter.format(revisedType, defaultValue, true, database.getDatabaseInfo().getCharSet(), false);
if (!formatDataResult.isSuccess()) {
CommonUITool.openErrorBox(Messages.bind(com.cubrid.common.ui.er.Messages.errMatchDefaultValue, defaultValue, attribute.getType()));
return false;
}
}
return true;
}
Aggregations