Search in sources :

Example 16 with CellValue

use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.

the class DateTimeCellPopuoDialog method buttonPressed.

/**
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 * @param buttonId the id of the button that was pressed (see
	 *        <code>IDialogConstants.*_ID</code> constants)
	 */
protected void buttonPressed(int buttonId) {
    if (buttonId == IDialogConstants.OK_ID) {
        String stringValue = NULL_VALUE;
        Date dateValue = null;
        Calendar calendar = null;
        if (!setNullBtn.getSelection()) {
            int year = dateComposite.getYear();
            int month = dateComposite.getMonth();
            int day = dateComposite.getDay();
            int hour = timeComposite.getHours();
            int minute = timeComposite.getMinutes();
            int second = timeComposite.getSeconds();
            calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);
            if (millSpinner != null) {
                calendar.set(Calendar.MILLISECOND, millSpinner.getSelection());
                dateValue = new Date(calendar.getTimeInMillis());
                stringValue = DateUtil.getDatetimeString(dateValue, DateUtil.DATETIME_FORMAT);
            } else {
                dateValue = new Date(calendar.getTimeInMillis());
                stringValue = DateUtil.getDatetimeString(dateValue, DateUtil.DATETIME_FORMAT);
            }
        }
        newValue = new CellValue(dateValue);
        newValue.setShowValue(stringValue);
    }
    super.buttonPressed(buttonId);
}
Also used : Calendar(java.util.Calendar) CellValue(com.cubrid.common.ui.spi.table.CellValue) Date(java.util.Date)

Example 17 with CellValue

use of com.cubrid.common.ui.spi.table.CellValue 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)

Example 18 with CellValue

use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.

the class QueryExecuter method saveInsertedUpdatedDeletedRecords.

/**
	 * Save all new inserted/updated records to database
	 * @throws ParamSetException
	 */
@SuppressWarnings("unchecked")
public boolean saveInsertedUpdatedDeletedRecords() throws SQLException, ParamSetException {
    logMessageText.setText("");
    // delete
    if (delValues.size() > 0) {
        Map<String, Map<String, CellValue>> deleteValuesMap = deleteValues();
        if (deleteValuesMap.size() == 0) {
            return false;
        }
    }
    // insert
    int insertCounts = 0;
    if (insValues.size() > 0) {
        Map<String, TableItem> insertedTableItems = new HashMap<String, TableItem>();
        for (TableItem recordItem : tblResult.getItems()) {
            String key = "" + recordItem.hashCode();
            Map<String, CellValue> newValue = (Map<String, CellValue>) recordItem.getData(LASTEST_DATA_FLAG);
            if (insValues.containsKey(key) && newValue != null) {
                Map<String, CellValue> insertValue = new HashMap<String, CellValue>();
                insertValue.putAll(newValue);
                insValues.put(key, insertValue);
                insertedTableItems.put(key, recordItem);
            }
            insertCounts++;
        }
        Map<String, Map<String, CellValue>> insertValuesMap = insertValues();
        if (insertValuesMap.size() == 0) {
            return false;
        }
    }
    // update
    if (oldValues.size() > 0 && newValues.size() > 0) {
        Map<String, Map<String, CellValue>> updateValuesMap = updateValues();
        if (updateValuesMap.size() == 0) {
            return false;
        }
    }
    clearModifiedLog();
    if (insertCounts > 0) {
        reloadQuery();
    }
    try {
        for (CombinedQueryEditorComposite combinedQueryEditorComposite : getQueryEditor().getAllCombinedQueryEditorComposite()) {
            combinedQueryEditorComposite.getRecentlyUsedSQLComposite().refreshRecentlyUsedSQLList();
        }
    } catch (Exception ignored) {
    }
    return true;
}
Also used : HashMap(java.util.HashMap) TableItem(org.eclipse.swt.widgets.TableItem) CellValue(com.cubrid.common.ui.spi.table.CellValue) Map(java.util.Map) HashMap(java.util.HashMap) Point(org.eclipse.swt.graphics.Point) ParamSetException(com.cubrid.common.ui.spi.util.paramSetter.ParamSetException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 19 with CellValue

use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.

the class QueryExecuter method addTableItemData.

/**
	 * Add a record to data set.
	 *
	 * @param rs result set
	 * @return Map<String, CellValue> map type data of a table item
	 * @throws SQLException
	 */
public Map<String, CellValue> addTableItemData(CUBRIDResultSetProxy rs, int idxInDataList) throws SQLException {
    Map<String, CellValue> map = new HashMap<String, CellValue>();
    int columnPos = 0, columnCount = allColumnList == null ? 0 : allColumnList.size();
    if (allColumnList != null) {
        for (int j = 1; j <= columnCount; j++) {
            ColumnInfo columnInfo = (ColumnInfo) allColumnList.get(columnPos);
            String columnType = columnInfo.getType();
            String index = columnInfo.getIndex();
            String showValue = null;
            Object value = rs.getObject(j);
            CellValue cellValue = new CellValue();
            if (value != null) {
                if (DataType.DATATYPE_SET.equals(columnType) || DataType.DATATYPE_MULTISET.equals(columnType) || DataType.DATATYPE_SEQUENCE.equals(columnType)) {
                    StringBuffer data = new StringBuffer();
                    Object[] set = (Object[]) rs.getCollection(j);
                    data.append("{");
                    for (int i = 0; i < set.length; i++) {
                        Object setI = set[i];
                        if (setI == null) {
                            data.append(DataType.VALUE_NULL);
                        } else if (setI.getClass() == CUBRIDOIDProxy.getCUBRIDOIDClass(rs.getJdbcVersion())) {
                            data.append((new CUBRIDOIDProxy(setI)).getOidString());
                        } else {
                            data.append(setI);
                        }
                        if (i < set.length - 1) {
                            data.append(",");
                        }
                    }
                    data.append("}");
                    showValue = data.toString();
                    cellValue.setShowValue(showValue);
                    cellValue.setValue(showValue);
                } else if (DataType.DATATYPE_DATETIME.equalsIgnoreCase(columnType)) {
                    showValue = CommonUITool.formatDate(rs.getTimestamp(j), FieldHandlerUtils.FORMAT_DATETIME);
                    cellValue.setValue(rs.getTimestamp(j));
                    cellValue.setShowValue(showValue);
                } else if (DataType.DATATYPE_BIT_VARYING.equalsIgnoreCase(columnType) || DataType.DATATYPE_BIT.equalsIgnoreCase(columnType)) {
                    byte[] dataTmp = rs.getBytes(j);
                    if (dataTmp.length > FieldHandlerUtils.BIT_TYPE_MUCH_VALUE_LENGTH) {
                        showValue = DataType.BIT_EXPORT_FORMAT;
                    } else {
                        showValue = "X'" + DBAttrTypeFormatter.getHexString(dataTmp, columnInfo.getPrecision()) + "'";
                    }
                    cellValue.setValue(dataTmp);
                    cellValue.setShowValue(showValue);
                } else if (DataType.DATATYPE_FLOAT.equalsIgnoreCase(columnType)) {
                    float floatValue = rs.getFloat(j);
                    showValue = formater4Float.format(floatValue);
                    cellValue.setValue(floatValue);
                    cellValue.setShowValue(showValue);
                } else if (DataType.DATATYPE_DOUBLE.equalsIgnoreCase(columnType)) {
                    double doubleValue = rs.getDouble(j);
                    showValue = formater4Double.format(doubleValue);
                    cellValue.setValue(doubleValue);
                    cellValue.setShowValue(showValue);
                } else if (DataType.DATATYPE_BLOB.equalsIgnoreCase(columnType) || value instanceof Blob) {
                    columnInfo.setType(DataType.DATATYPE_BLOB);
                    loadBlobData(rs, j, cellValue);
                } else if (DataType.DATATYPE_CLOB.equalsIgnoreCase(columnType) || value instanceof Clob) {
                    columnInfo.setType(DataType.DATATYPE_CLOB);
                    loadClobData(rs, j, cellValue);
                } else if (DataType.DATATYPE_NCHAR.equalsIgnoreCase(columnType)) {
                    columnInfo.setType(DataType.DATATYPE_NCHAR);
                    String strValue = rs.getString(j);
                    showValue = "N'" + strValue + "'";
                    cellValue.setValue(strValue);
                    cellValue.setShowValue(showValue);
                } else if (DataType.DATATYPE_NCHAR_VARYING.equalsIgnoreCase(columnType)) {
                    columnInfo.setType(DataType.DATATYPE_NCHAR_VARYING);
                    String strValue = rs.getString(j);
                    showValue = "N'" + strValue + "'";
                    cellValue.setValue(strValue);
                    cellValue.setShowValue(showValue);
                } else {
                    showValue = rs.getString(j);
                    cellValue.setValue(value);
                    cellValue.setShowValue(showValue);
                }
            }
            map.put(index, cellValue);
            columnPos++;
        }
    }
    if (allDataList != null) {
        if (idxInDataList < 0 || idxInDataList >= allDataList.size() - 1) {
            allDataList.add(map);
        } else {
            allDataList.add(idxInDataList, map);
        }
    }
    return map;
}
Also used : CUBRIDOIDProxy(com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy) Blob(java.sql.Blob) HashMap(java.util.HashMap) Point(org.eclipse.swt.graphics.Point) CellValue(com.cubrid.common.ui.spi.table.CellValue) Clob(java.sql.Clob)

Example 20 with CellValue

use of com.cubrid.common.ui.spi.table.CellValue in project cubrid-manager by CUBRID.

the class QueryExecuter method makeItem.

/**
	 * make table item by the data in allDataList
	 */
public void makeItem() {
    isSingleTableQuery = false;
    if (!queryEditor.isCollectExecStats()) {
        String tableName = null;
        if (columnTableNames != null && columnTableNames.size() > 0) {
            tableName = columnTableNames.get(0);
            if (tableName != null) {
                isSingleTableQuery = true;
                for (int i = 1, len = columnTableNames.size(); i < len; i++) {
                    if (!tableName.equals(columnTableNames.get(i))) {
                        isSingleTableQuery = false;
                        break;
                    }
                }
            }
        }
        if (isSingleTableQuery) {
            int matchedCnt = 0;
            List<String> pkList = UIQueryUtil.getPkList(getDatabaseInfo(), tableName);
            for (String pk : pkList) {
                for (int j = 0; allColumnList != null && j < allColumnList.size(); j++) {
                    String columnName = allColumnList.get(j).getName();
                    if (pk.equalsIgnoreCase(columnName)) {
                        matchedCnt++;
                    }
                }
            }
            if (matchedCnt > 0 && matchedCnt == pkList.size()) {
                isContainPrimayKey = true;
            } else {
                isContainPrimayKey = false;
            }
        }
    }
    if (insertRecordItem != null && !insertRecordItem.isDisposed()) {
        insertRecordItem.setEnabled(getEditable() && isEditMode());
    }
    disableActions();
    clearModifiedLog();
    tblResult.removeAll();
    rsToItemMap.clear();
    int begin = (queryInfo.getCurrentPage() - 1) * queryInfo.getPageSize();
    int last = begin + queryInfo.getPageSize();
    List<Point> matchedPointList = new ArrayList<Point>();
    int itemNo = 0;
    int index = (queryInfo.getCurrentPage() - 1) * queryInfo.getPageSize() + 1;
    for (int i = 0; allDataList != null && i < last && i < queryInfo.getTotalRs(); i++) {
        Map<String, CellValue> dataMap = allDataList.get(i);
        // filter the data
        boolean isAccepted = filterResultContrItem.select(dataMap, filterSetting);
        if (!isAccepted) {
            continue;
        }
        TableItem item = new TableItem(tblResult, SWT.MULTI);
        rsToItemMap.put("" + item.hashCode(), "" + i);
        item.setText(0, String.valueOf(index + i - begin));
        item.setData(dataMap);
        makeItemValue(item, dataMap);
        item.setBackground(0, Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
        int columnNum = 1;
        for (int j = 0; allColumnList != null && j < allColumnList.size(); j++, columnNum++) {
            ColumnInfo columnInfo = allColumnList.get(j);
            String columnIndex = columnInfo.getIndex();
            // display compare data for multiple queries
            if (multiResultsCompare == true && baseQueryExecuter != null) {
                compareTableItemData(item, i, columnIndex);
            }
            Object colValue = dataMap.get(columnIndex);
            String showValue = null;
            if (colValue instanceof String) {
                showValue = (String) colValue;
            } else if (colValue instanceof CellValue) {
                showValue = ((CellValue) colValue).getShowValue();
            }
            if (showValue == null) {
                item.setText(columnNum, DataType.NULL_EXPORT_FORMAT);
                item.setData((columnNum) + "", DataType.VALUE_NULL);
            } else {
                item.setText(columnNum, showValue);
            }
            if (DataType.isSelfDefinedData(showValue)) {
                item.setBackground(columnNum, Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
            }
            // Select the matched data
            if (showValue != null && filterResultContrItem.isMatch(filterSetting, showValue, columnInfo)) {
                Point ponit = new Point(j + 1, itemNo);
                matchedPointList.add(ponit);
            }
        }
        itemNo++;
    }
    if (filterResultContrItem.isUseFilter()) {
        selectableSupport.setSelection(matchedPointList);
    }
    tblResult.setTopIndex(begin);
    if (delRecordItem != null && !delRecordItem.isDisposed()) {
        delRecordItem.setEnabled(false);
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) CellValue(com.cubrid.common.ui.spi.table.CellValue) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Aggregations

CellValue (com.cubrid.common.ui.spi.table.CellValue)24 Point (org.eclipse.swt.graphics.Point)11 HashMap (java.util.HashMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 SQLException (java.sql.SQLException)6 TableItem (org.eclipse.swt.widgets.TableItem)6 ParamSetException (com.cubrid.common.ui.spi.util.paramSetter.ParamSetException)5 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)4 PstmtParameter (com.cubrid.common.ui.cubrid.table.dialog.PstmtParameter)3 ParamSetter (com.cubrid.common.ui.spi.util.paramSetter.ParamSetter)3 DBConnection (com.cubrid.cubridmanager.core.common.jdbc.DBConnection)3 File (java.io.File)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 Calendar (java.util.Calendar)3 Date (java.util.Date)3 ColumnInfo (com.cubrid.common.ui.query.control.ColumnInfo)2 IOException (java.io.IOException)2 DateTimeComponent (com.cubrid.common.ui.query.control.DateTimeComponent)1