Search in sources :

Example 1 with DBCStatement

use of org.jkiss.dbeaver.model.exec.DBCStatement in project dbeaver by serge-rider.

the class JDBCTableColumn method getValueEnumeration.

@NotNull
@Override
public Collection<DBDLabelValuePair> getValueEnumeration(@NotNull DBCSession session, @Nullable Object valuePattern, int maxResults) throws DBException {
    DBDValueHandler valueHandler = DBUtils.findValueHandler(session, this);
    StringBuilder query = new StringBuilder();
    query.append("SELECT ").append(DBUtils.getQuotedIdentifier(this)).append(", count(*)");
    // Do not use description columns because they duplicate distinct value
    //        String descColumns = DBVUtils.getDictionaryDescriptionColumns(session.getProgressMonitor(), this);
    //        if (descColumns != null) {
    //            query.append(", ").append(descColumns);
    //        }
    query.append("\nFROM ").append(DBUtils.getObjectFullName(getTable(), DBPEvaluationContext.DML));
    if (valuePattern instanceof String) {
        query.append("\nWHERE ").append(DBUtils.getQuotedIdentifier(this)).append(" LIKE ?");
    }
    query.append("\nGROUP BY ").append(DBUtils.getQuotedIdentifier(this));
    try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false)) {
        if (valuePattern instanceof String) {
            valueHandler.bindValueObject(session, dbStat, this, 0, "%" + valuePattern + "%");
        }
        dbStat.setLimit(0, maxResults);
        if (dbStat.executeStatement()) {
            try (DBCResultSet dbResult = dbStat.openResultSet()) {
                return DBVUtils.readDictionaryRows(session, this, valueHandler, dbResult);
            }
        } else {
            return Collections.emptyList();
        }
    }
}
Also used : DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) NotNull(org.jkiss.code.NotNull)

Example 2 with DBCStatement

use of org.jkiss.dbeaver.model.exec.DBCStatement in project dbeaver by serge-rider.

the class JDBCTableConstraint method readKeyEnumeration.

private Collection<DBDLabelValuePair> readKeyEnumeration(DBCSession session, DBSEntityAttribute keyColumn, Object keyPattern, List<DBDAttributeValue> preceedingKeys, int maxResults) throws DBException {
    final TABLE table = getParentObject();
    assert table != null;
    DBDValueHandler keyValueHandler = DBUtils.findValueHandler(session, keyColumn);
    if (keyPattern != null) {
        if (keyPattern instanceof CharSequence) {
            if (((CharSequence) keyPattern).length() > 0) {
                keyPattern = "%" + keyPattern.toString() + "%";
            } else {
                keyPattern = null;
            }
        } else if (keyPattern instanceof Number) {
            // Subtract gap value to see some values before specified
            int gapSize = maxResults / 2;
            if (keyPattern instanceof Integer) {
                keyPattern = (Integer) keyPattern - gapSize;
            } else if (keyPattern instanceof Short) {
                keyPattern = (Short) keyPattern - gapSize;
            } else if (keyPattern instanceof Long) {
                keyPattern = (Long) keyPattern - gapSize;
            } else if (keyPattern instanceof Float) {
                keyPattern = (Float) keyPattern - gapSize;
            } else if (keyPattern instanceof Double) {
                keyPattern = (Double) keyPattern - gapSize;
            } else if (keyPattern instanceof BigInteger) {
                keyPattern = ((BigInteger) keyPattern).subtract(BigInteger.valueOf(gapSize));
            } else if (keyPattern instanceof BigDecimal) {
                keyPattern = ((BigDecimal) keyPattern).subtract(new BigDecimal(gapSize));
            }
        } else {
            // not supported
            keyPattern = null;
        }
    }
    StringBuilder query = new StringBuilder();
    query.append("SELECT ").append(DBUtils.getQuotedIdentifier(keyColumn));
    String descColumns = DBVUtils.getDictionaryDescriptionColumns(session.getProgressMonitor(), keyColumn);
    Collection<DBSEntityAttribute> descAttributes = null;
    if (descColumns != null) {
        descAttributes = DBVEntity.getDescriptionColumns(session.getProgressMonitor(), table, descColumns);
        query.append(", ").append(descColumns);
    }
    query.append(" FROM ").append(DBUtils.getObjectFullName(table, DBPEvaluationContext.DML));
    if (!CommonUtils.isEmpty(preceedingKeys) || keyPattern != null) {
        query.append(" WHERE ");
    }
    boolean hasCond = false;
    // Preceeding keys
    if (preceedingKeys != null && !preceedingKeys.isEmpty()) {
        for (int i = 0; i < preceedingKeys.size(); i++) {
            if (hasCond)
                query.append(" AND ");
            query.append(DBUtils.getQuotedIdentifier(getDataSource(), preceedingKeys.get(i).getAttribute().getName())).append(" = ?");
            hasCond = true;
        }
    }
    if (keyPattern != null) {
        if (hasCond)
            query.append(" AND (");
        query.append(DBUtils.getQuotedIdentifier(keyColumn));
        if (keyPattern instanceof CharSequence) {
            query.append(" LIKE ?");
        } else {
            query.append(" >= ?");
        }
        // Add desc columns conditions
        if (keyPattern instanceof CharSequence && descAttributes != null) {
            for (DBSEntityAttribute descAttr : descAttributes) {
                if (descAttr.getDataKind() == DBPDataKind.STRING) {
                    query.append(" OR ").append(DBUtils.getQuotedIdentifier(descAttr)).append(" LIKE ?");
                }
            }
        }
        if (hasCond)
            query.append(")");
        query.append(" ORDER BY ").append(DBUtils.getQuotedIdentifier(keyColumn));
    }
    try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false)) {
        int paramPos = 0;
        if (preceedingKeys != null && !preceedingKeys.isEmpty()) {
            for (DBDAttributeValue precAttribute : preceedingKeys) {
                DBDValueHandler precValueHandler = DBUtils.findValueHandler(session, precAttribute.getAttribute());
                precValueHandler.bindValueObject(session, dbStat, precAttribute.getAttribute(), paramPos++, precAttribute.getValue());
            }
        }
        if (keyPattern != null) {
            keyValueHandler.bindValueObject(session, dbStat, keyColumn, paramPos++, keyPattern);
        }
        if (keyPattern instanceof CharSequence && descAttributes != null) {
            for (DBSEntityAttribute descAttr : descAttributes) {
                if (descAttr.getDataKind() == DBPDataKind.STRING) {
                    final DBDValueHandler valueHandler = DBUtils.findValueHandler(session, descAttr);
                    valueHandler.bindValueObject(session, dbStat, keyColumn, paramPos++, keyPattern);
                }
            }
        }
        dbStat.setLimit(0, maxResults);
        if (dbStat.executeStatement()) {
            try (DBCResultSet dbResult = dbStat.openResultSet()) {
                return DBVUtils.readDictionaryRows(session, keyColumn, keyValueHandler, dbResult);
            }
        } else {
            return Collections.emptyList();
        }
    }
}
Also used : DBDAttributeValue(org.jkiss.dbeaver.model.data.DBDAttributeValue) DBDValueHandler(org.jkiss.dbeaver.model.data.DBDValueHandler) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) BigDecimal(java.math.BigDecimal) DBSEntityConstraint(org.jkiss.dbeaver.model.struct.DBSEntityConstraint) AbstractTableConstraint(org.jkiss.dbeaver.model.impl.struct.AbstractTableConstraint) BigInteger(java.math.BigInteger) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) BigInteger(java.math.BigInteger) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet)

Example 3 with DBCStatement

use of org.jkiss.dbeaver.model.exec.DBCStatement in project dbeaver by serge-rider.

the class OracleMaintenanceDialog method getScriptListener.

@Override
protected SQLScriptProgressListener<T> getScriptListener() {
    return new SQLScriptStatusDialog<T>(getShell(), getTitle() + " progress", null) {

        @Override
        protected void createStatusColumns(Tree objectTree) {
            TreeColumn msgColumn = new TreeColumn(objectTree, SWT.NONE);
            msgColumn.setText("Message");
        }

        @Override
        public void processObjectResults(@NotNull T object, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
        }

        @Override
        public void endObjectProcessing(@NotNull T object, Exception error) {
            super.endObjectProcessing(object, error);
            TreeItem treeItem = getTreeItem(object);
            if (treeItem != null) {
                treeItem.setText(1, error == null ? "Done" : error.getMessage());
            }
        }
    };
}
Also used : SWT(org.eclipse.swt.SWT) TreeItem(org.eclipse.swt.widgets.TreeItem) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) NotNull(org.jkiss.code.NotNull) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) Nullable(org.jkiss.code.Nullable) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 4 with DBCStatement

use of org.jkiss.dbeaver.model.exec.DBCStatement in project dbeaver by serge-rider.

the class DB2BaseTableToolDialog method getScriptListener.

@Override
protected SQLScriptProgressListener<DB2Table> getScriptListener() {
    final int nbExtraColumns = getNumberExtraResultingColumns();
    return new SQLScriptStatusDialog<DB2Table>(getShell(), getTitle() + " " + DB2Messages.dialog_table_tools_progress, null) {

        @Override
        protected void createStatusColumns(Tree objectTree) {
            TreeColumn msgColumn = new TreeColumn(objectTree, SWT.NONE);
            msgColumn.setText(DB2Messages.dialog_table_tools_result);
            for (int i = 0; i < nbExtraColumns; i++) {
                new TreeColumn(objectTree, SWT.NONE);
            }
        }

        @Override
        public void endObjectProcessing(@NotNull DB2Table db2Table, Exception exception) {
            TreeItem treeItem = getTreeItem(db2Table);
            if (exception == null) {
                treeItem.setText(1, DB2Messages.dialog_table_tools_success_title);
            } else {
                treeItem.setText(1, exception.getMessage());
            }
            UIUtils.packColumns(treeItem.getParent(), false, null);
        }

        // DF: This method is for tools that return resultsets
        @Override
        public void processObjectResults(@NotNull DB2Table db2Table, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
            if (resultSet == null) {
                return;
            }
            // Retrive column names
            JDBCResultSetMetaDataImpl rsMetaData = (JDBCResultSetMetaDataImpl) resultSet.getMeta();
            try {
                TreeItem treeItem = getTreeItem(db2Table);
                Font f = UIUtils.makeBoldFont(treeItem.getFont());
                if (treeItem != null) {
                    // Display the column names
                    TreeItem subItem = null;
                    subItem = new TreeItem(treeItem, SWT.NONE);
                    subItem.setFont(f);
                    for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
                        subItem.setText(i, rsMetaData.getColumnName(i + 1));
                        subItem.setGrayed(true);
                    }
                    // Display the data for each row
                    while (resultSet.nextRow()) {
                        subItem = new TreeItem(treeItem, SWT.NONE);
                        for (int i = 0; i < rsMetaData.getColumnCount(); i++) {
                            subItem.setText(i, CommonUtils.toString(resultSet.getAttributeValue(i)));
                        }
                    }
                    treeItem.setExpanded(true);
                }
            } catch (SQLException e) {
                throw new DBCException(e.getMessage());
            }
        }
    };
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) SQLException(java.sql.SQLException) DBCException(org.jkiss.dbeaver.model.exec.DBCException) NotNull(org.jkiss.code.NotNull) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) JDBCResultSetMetaDataImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCResultSetMetaDataImpl) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) Font(org.eclipse.swt.graphics.Font) DB2Table(org.jkiss.dbeaver.ext.db2.model.DB2Table) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) Nullable(org.jkiss.code.Nullable)

Example 5 with DBCStatement

use of org.jkiss.dbeaver.model.exec.DBCStatement in project dbeaver by serge-rider.

the class TableToolDialog method getScriptListener.

@Override
protected SQLScriptProgressListener<MySQLTable> getScriptListener() {
    return new SQLScriptStatusDialog<MySQLTable>(getShell(), getTitle() + " progress", null) {

        @Override
        protected void createStatusColumns(Tree objectTree) {
            TreeColumn msgColumn = new TreeColumn(objectTree, SWT.NONE);
            msgColumn.setText("Message");
        }

        @Override
        public void processObjectResults(@NotNull MySQLTable object, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
            if (resultSet == null) {
                return;
            }
            Map<String, String> statusMap = new LinkedHashMap<>();
            while (resultSet.nextRow()) {
                statusMap.put(CommonUtils.toString(resultSet.getAttributeValue("Msg_type")), CommonUtils.toString(resultSet.getAttributeValue("Msg_text")));
            }
            TreeItem treeItem = getTreeItem(object);
            if (treeItem != null && !statusMap.isEmpty()) {
                if (statusMap.size() == 1) {
                    treeItem.setText(1, statusMap.values().iterator().next());
                } else {
                    String statusText = statusMap.get("status");
                    if (!CommonUtils.isEmpty(statusText)) {
                        treeItem.setText(1, statusText);
                    }
                    for (Map.Entry<String, String> status : statusMap.entrySet()) {
                        if (!status.getKey().equals("status")) {
                            TreeItem subItem = new TreeItem(treeItem, SWT.NONE);
                            subItem.setText(0, status.getKey());
                            subItem.setText(1, status.getValue());
                        }
                    }
                    treeItem.setExpanded(true);
                }
            }
        }
    };
}
Also used : TreeItem(org.eclipse.swt.widgets.TreeItem) MySQLTable(org.jkiss.dbeaver.ext.mysql.model.MySQLTable) NotNull(org.jkiss.code.NotNull) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) LinkedHashMap(java.util.LinkedHashMap) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Nullable(org.jkiss.code.Nullable)

Aggregations

DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)8 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)6 NotNull (org.jkiss.code.NotNull)5 DBCException (org.jkiss.dbeaver.model.exec.DBCException)5 Tree (org.eclipse.swt.widgets.Tree)4 TreeColumn (org.eclipse.swt.widgets.TreeColumn)4 TreeItem (org.eclipse.swt.widgets.TreeItem)4 Nullable (org.jkiss.code.Nullable)4 SQLScriptStatusDialog (org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog)4 SQLException (java.sql.SQLException)2 DBDValueHandler (org.jkiss.dbeaver.model.data.DBDValueHandler)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 SQLWarning (java.sql.SQLWarning)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 SWT (org.eclipse.swt.SWT)1 Font (org.eclipse.swt.graphics.Font)1 DB2Table (org.jkiss.dbeaver.ext.db2.model.DB2Table)1 MySQLTable (org.jkiss.dbeaver.ext.mysql.model.MySQLTable)1