Search in sources :

Example 1 with DBCResultSet

use of org.jkiss.dbeaver.model.exec.DBCResultSet 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 DBCResultSet

use of org.jkiss.dbeaver.model.exec.DBCResultSet 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 DBCResultSet

use of org.jkiss.dbeaver.model.exec.DBCResultSet 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 DBCResultSet

use of org.jkiss.dbeaver.model.exec.DBCResultSet in project dbeaver by dbeaver.

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 5 with DBCResultSet

use of org.jkiss.dbeaver.model.exec.DBCResultSet in project dbeaver by dbeaver.

the class TableToolDialog method getScriptListener.

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

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

        @Override
        public void processObjectResults(@NotNull PostgreObject object, @Nullable DBCStatement statement, @Nullable DBCResultSet resultSet) throws DBCException {
            if (statement == null) {
                return;
            }
            TreeItem treeItem = getTreeItem(object);
            if (treeItem != null) {
                try {
                    int warnNum = 0;
                    SQLWarning warning = ((JDBCStatement) statement).getWarnings();
                    while (warning != null) {
                        if (warnNum == 0) {
                            treeItem.setText(1, warning.getMessage());
                        } else {
                            TreeItem warnItem = new TreeItem(treeItem, SWT.NONE);
                            warnItem.setText(0, "");
                            warnItem.setText(1, warning.getMessage());
                        }
                        warnNum++;
                        warning = warning.getNextWarning();
                    }
                    if (warnNum == 0) {
                        treeItem.setText(1, "Done");
                    }
                } catch (SQLException e) {
                // ignore
                }
                treeItem.setExpanded(true);
            }
        }

        @Override
        public void endObjectProcessing(@NotNull PostgreObject object, Exception error) {
            super.endObjectProcessing(object, error);
            if (error != null) {
                TreeItem treeItem = getTreeItem(object);
                if (treeItem != null) {
                    treeItem.setText(1, error.getMessage());
                }
            }
        }
    };
}
Also used : SQLWarning(java.sql.SQLWarning) TreeItem(org.eclipse.swt.widgets.TreeItem) SQLException(java.sql.SQLException) NotNull(org.jkiss.code.NotNull) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLScriptStatusDialog(org.jkiss.dbeaver.ui.dialogs.sql.SQLScriptStatusDialog) DBCException(org.jkiss.dbeaver.model.exec.DBCException) SQLException(java.sql.SQLException) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Tree(org.eclipse.swt.widgets.Tree) PostgreObject(org.jkiss.dbeaver.ext.postgresql.model.PostgreObject) DBCResultSet(org.jkiss.dbeaver.model.exec.DBCResultSet) Nullable(org.jkiss.code.Nullable)

Aggregations

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