use of com.cubrid.common.ui.spi.table.CellValue 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;
}
use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.
the class QueryExecuter method handleButtonEvent.
/**
* Open detail dialog
*
* @param event MouseEvent
*/
@SuppressWarnings("unchecked")
public void handleButtonEvent(int rowIndex, int columnIndex) {
// Get columnInfo
ColumnInfo columnInfo = (ColumnInfo) tblResult.getColumn(columnIndex).getData();
// Get data
final TableItem item = selectableSupport.getTableCursor().getRow();
Map<String, CellValue> dataMap = (Map<String, CellValue>) item.getData();
Map<String, CellValue> newValueMap = (Map<String, CellValue>) item.getData(LASTEST_DATA_FLAG);
if (newValueMap == null) {
newValueMap = new HashMap<String, CellValue>();
newValueMap.putAll(dataMap);
item.setData(LASTEST_DATA_FLAG, newValueMap);
}
String dataIndex = String.valueOf(columnIndex);
CellValue cellValue = newValueMap.get(dataIndex);
if (cellValue == null) {
cellValue = new CellValue();
newValueMap.put(dataIndex, cellValue);
}
String charset = getDatabaseInfo() != null ? getDatabaseInfo().getCharSet() : null;
String dataType = DataType.makeType(columnInfo.getType(), columnInfo.getChildElementType(), columnInfo.getPrecision(), columnInfo.getScale());
cellValue.setFileCharset(charset);
CellViewer cellViewer = new CellViewer(columnInfo, editMode, charset);
if (IDialogConstants.OK_ID == cellViewer.openCellViewer(tblResult.getShell(), cellValue)) {
CellValue newValue = cellViewer.getValue();
if (!CellViewer.isCellValueEqual(cellValue, newValue)) {
String showValue = null;
if (newValue.getValue() == null) {
showValue = DataType.NULL_EXPORT_FORMAT;
item.setText(columnIndex, showValue);
newValueMap.put(dataIndex, newValue);
updateValue(item, dataMap, newValueMap);
} else if (newValue.getValue() instanceof String) {
String strValue = newValue.getValue().toString();
FormatDataResult result = DBAttrTypeFormatter.format(dataType, strValue, null, false, charset, false);
if (result.isSuccess()) {
// Update the data
showValue = newValue.getShowValue();
item.setText(columnIndex, showValue);
newValueMap.put(dataIndex, newValue);
updateValue(item, dataMap, newValueMap);
} else {
CommonUITool.openErrorBox(Messages.bind(Messages.errTextTypeNotMatch, dataType));
return;
}
} else if (newValue.getValue() instanceof byte[]) {
if (DataType.DATATYPE_BIT.equalsIgnoreCase(columnInfo.getType()) || DataType.DATATYPE_BIT_VARYING.equalsIgnoreCase(columnInfo.getType())) {
byte[] bValues = (byte[]) newValue.getValue();
if (bValues.length * 8 > columnInfo.getPrecision() + 7) {
String msg = Messages.bind(Messages.errTextTypeNotMatch, dataType);
CommonUITool.openErrorBox(msg);
return;
}
}
showValue = newValue.getShowValue();
item.setText(columnIndex, showValue);
newValueMap.put(dataIndex, newValue);
updateValue(item, dataMap, newValueMap);
} else {
showValue = newValue.getShowValue();
item.setText(columnIndex, showValue);
newValueMap.put(dataIndex, newValue);
updateValue(item, dataMap, newValueMap);
}
selectableSupport.getTableCursor().redraw();
}
}
}
use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.
the class Export method init.
/**
*
* Initial the exported column information and data information
*
* @param columnInfoList List<ColumnInfo>
* @param exportedDataList List<Map<String, Object>>
* @param hasOid boolean
*/
private void init(List<ColumnInfo> columnInfoList, List<Map<String, CellValue>> exportedDataList, boolean hasOid) {
if (columnInfoList == null || columnInfoList.isEmpty()) {
return;
}
int colCount = columnInfoList.size();
int itemCount = exportedDataList.size();
int start = 0;
if (hasOid) {
start++;
}
List<ColumnInfo> columnList = new ArrayList<ColumnInfo>();
List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
for (int j = start; j < colCount; j++) {
ColumnInfo columnInfo = columnInfoList.get(j);
columnList.add(columnInfo);
}
for (int i = 0; i < itemCount; i++) {
Map<String, String> value = new HashMap<String, String>();
for (int j = start; j < colCount; j++) {
ColumnInfo columnInfo = columnInfoList.get(j);
String type = columnInfo.getType();
CellValue dataValue = exportedDataList.get(i).get((hasOid ? j : j + 1) + "");
value.put(columnInfo.getIndex(), FieldHandlerUtils.getValueForExport(type, dataValue.getShowValue()));
}
dataList.add(value);
}
resultColsList.add(columnList);
resultDataList.add(dataList);
}
use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.
the class FilterResultContrItem method select.
/**
*
* Returns whether the given element makes it through this filter.
*
* @param dataMap Map<String, Object>
* @param filterSetting QueryResultFilterSetting
* @return boolean
*/
public boolean select(Map<String, CellValue> dataMap, QueryResultFilterSetting filterSetting) {
if (filterSetting == null || filterSetting.getContent() == null || filterSetting.getContent().trim().length() == 0) {
return true;
}
List<ColumnInfo> colInfoList = filterSetting.getFilterColumnInfoList();
if (colInfoList == null || colInfoList.isEmpty()) {
return true;
}
for (ColumnInfo colInfo : colInfoList) {
String columnIndex = colInfo.getIndex();
Object colValue = dataMap.get(columnIndex);
String colStringValue = null;
if (colValue instanceof String) {
colStringValue = (String) colValue;
} else if (colValue instanceof CellValue) {
colStringValue = ((CellValue) colValue).getShowValue();
}
if (isMatch(filterSetting, colStringValue, null)) {
return true;
}
}
return false;
}
use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.
the class CellViewer method isCellValueEqual.
/**
* Check whether these two objects are equal
*
* @param oldObj Object
* @param newObj Object
* @return boolean
*/
public static boolean isCellValueEqual(Object oldObj, Object newObj) {
// Judge the cell value is null
if (oldObj == null || newObj == null) {
if (oldObj == null && newObj == null) {
return true;
}
return false;
}
if (oldObj instanceof CellValue) {
oldObj = ((CellValue) oldObj).getValue();
}
if (newObj instanceof CellValue) {
newObj = ((CellValue) newObj).getValue();
}
// Judge the data is null
if (oldObj == null || newObj == null) {
if (oldObj == null && newObj == null) {
return true;
}
return false;
}
if (!oldObj.getClass().equals(newObj.getClass())) {
return false;
}
if (oldObj instanceof File && newObj instanceof File) {
File oldFile = (File) oldObj;
File newFile = (File) newObj;
return oldFile.getAbsolutePath().equals(newFile.getAbsolutePath()) && oldFile.lastModified() == newFile.lastModified();
} else if (oldObj instanceof byte[] && newObj instanceof byte[]) {
byte[] oldBytes = (byte[]) oldObj;
byte[] newBytes = (byte[]) newObj;
if (oldBytes.length != newBytes.length) {
return false;
} else {
for (int i = 0; i < oldBytes.length; i++) {
if (oldBytes[i] != newBytes[i]) {
return false;
}
}
return true;
}
}
return oldObj.toString().equals(newObj.toString());
}
Aggregations