use of org.jkiss.dbeaver.model.data.DBDAttributeValue 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();
}
}
}
Aggregations