Search in sources :

Example 16 with DBCResultSet

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

the class JDBCTableColumn method getValueEnumeration.

@NotNull
@Override
public List<DBDLabelValuePair> getValueEnumeration(@NotNull DBCSession session, @Nullable Object valuePattern, int maxResults, boolean formatValues) 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));
        if (getDataKind() == DBPDataKind.STRING) {
            query.append(" LIKE ?");
        } else {
            query.append(" = ?");
        }
    }
    query.append("\nGROUP BY ").append(DBUtils.getQuotedIdentifier(this));
    try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.toString(), false, false, false)) {
        if (valuePattern instanceof String) {
            if (getDataKind() == DBPDataKind.STRING) {
                valueHandler.bindValueObject(session, dbStat, this, 0, "%" + valuePattern + "%");
            } else {
                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, formatValues);
            }
        } 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)

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