Search in sources :

Example 1 with DBCLogicalOperator

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

the class SQLUtils method getConstraintCondition.

@Nullable
public static String getConstraintCondition(@NotNull DBPDataSource dataSource, @NotNull DBDAttributeConstraint constraint, boolean inlineCriteria) {
    String criteria = constraint.getCriteria();
    if (!CommonUtils.isEmpty(criteria)) {
        final char firstChar = criteria.trim().charAt(0);
        if (!Character.isLetter(firstChar) && firstChar != '=' && firstChar != '>' && firstChar != '<' && firstChar != '!') {
            return '=' + criteria;
        } else {
            return criteria;
        }
    } else if (constraint.getOperator() != null) {
        DBCLogicalOperator operator = constraint.getOperator();
        StringBuilder conString = new StringBuilder();
        Object value = constraint.getValue();
        if (DBUtils.isNullValue(value)) {
            if (operator.getArgumentCount() == 0) {
                return operator.getStringValue();
            }
            conString.append("IS ");
            if (constraint.isReverseOperator()) {
                conString.append("NOT ");
            }
            conString.append("NULL");
            return conString.toString();
        }
        if (constraint.isReverseOperator()) {
            conString.append("NOT ");
        }
        if (operator.getArgumentCount() > 0) {
            conString.append(operator.getStringValue());
            for (int i = 0; i < operator.getArgumentCount(); i++) {
                if (i > 0) {
                    conString.append(" AND");
                }
                if (inlineCriteria) {
                    conString.append(' ').append(convertValueToSQL(dataSource, constraint.getAttribute(), value));
                } else {
                    conString.append(" ?");
                }
            }
        } else if (operator.getArgumentCount() < 0) {
            // Multiple arguments
            int valueCount = Array.getLength(value);
            boolean hasNull = false, hasNotNull = false;
            for (int i = 0; i < valueCount; i++) {
                final boolean isNull = DBUtils.isNullValue(Array.get(value, i));
                if (isNull && !hasNull) {
                    hasNull = true;
                }
                if (!isNull && !hasNotNull) {
                    hasNotNull = true;
                }
            }
            if (!hasNotNull) {
                return "IS NULL";
            }
            if (hasNull) {
                conString.append("IS NULL OR ").append(DBUtils.getObjectFullName(dataSource, constraint.getAttribute(), DBPEvaluationContext.DML)).append(" ");
            }
            conString.append(operator.getStringValue());
            conString.append(" (");
            if (!value.getClass().isArray()) {
                value = new Object[] { value };
            }
            boolean hasValue = false;
            for (int i = 0; i < valueCount; i++) {
                Object itemValue = Array.get(value, i);
                if (DBUtils.isNullValue(itemValue)) {
                    continue;
                }
                if (hasValue) {
                    conString.append(",");
                }
                hasValue = true;
                if (inlineCriteria) {
                    conString.append(convertValueToSQL(dataSource, constraint.getAttribute(), itemValue));
                } else {
                    conString.append("?");
                }
            }
            conString.append(")");
        }
        return conString.toString();
    } else {
        return null;
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject) DBCLogicalOperator(org.jkiss.dbeaver.model.exec.DBCLogicalOperator) Nullable(org.jkiss.code.Nullable)

Aggregations

Nullable (org.jkiss.code.Nullable)1 DBCLogicalOperator (org.jkiss.dbeaver.model.exec.DBCLogicalOperator)1 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)1 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)1