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();
}
}
}
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();
}
}
}
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());
}
}
};
}
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());
}
}
};
}
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);
}
}
}
};
}
Aggregations