Search in sources :

Example 41 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class Query method getParameterValues.

public final Value[] getParameterValues() {
    ArrayList<Parameter> list = getParameters();
    if (list == null) {
        list = New.arrayList();
    }
    int size = list.size();
    Value[] params = new Value[size];
    for (int i = 0; i < size; i++) {
        Value v = list.get(i).getParamValue();
        params[i] = v;
    }
    return params;
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 42 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class Comparison method optimize.

@Override
public Expression optimize(Session session) {
    left = left.optimize(session);
    if (right != null) {
        right = right.optimize(session);
        if (right instanceof ExpressionColumn) {
            if (left.isConstant() || left instanceof Parameter) {
                Expression temp = left;
                left = right;
                right = temp;
                compareType = getReversedCompareType(compareType);
            }
        }
        if (left instanceof ExpressionColumn) {
            if (right.isConstant()) {
                Value r = right.getValue(session);
                if (r == ValueNull.INSTANCE) {
                    if ((compareType & NULL_SAFE) == 0) {
                        return ValueExpression.getNull();
                    }
                }
            } else if (right instanceof Parameter) {
                ((Parameter) right).setColumn(((ExpressionColumn) left).getColumn());
            }
        }
    }
    if (compareType == IS_NULL || compareType == IS_NOT_NULL) {
        if (left.isConstant()) {
            return ValueExpression.get(getValue(session));
        }
    } else {
        if (SysProperties.CHECK && (left == null || right == null)) {
            DbException.throwInternalError();
        }
        if (left == ValueExpression.getNull() || right == ValueExpression.getNull()) {
            // a NULL constants
            if ((compareType & NULL_SAFE) == 0) {
                return ValueExpression.getNull();
            }
        }
        if (left.isConstant() && right.isConstant()) {
            return ValueExpression.get(getValue(session));
        }
    }
    return this;
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 43 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class ConditionAndOr method optimize.

@Override
public Expression optimize(Session session) {
    // NULL handling: see wikipedia,
    // http://www-cs-students.stanford.edu/~wlam/compsci/sqlnulls
    left = left.optimize(session);
    right = right.optimize(session);
    int lc = left.getCost(), rc = right.getCost();
    if (rc < lc) {
        Expression t = left;
        left = right;
        right = t;
    }
    // SELECT * FROM TEST WHERE NOT (B=A AND B=0 AND A=0); // 1, NULL
    if (session.getDatabase().getSettings().optimizeTwoEquals && andOrType == AND) {
        // try to add conditions (A=B AND B=1: add A=1)
        if (left instanceof Comparison && right instanceof Comparison) {
            Comparison compLeft = (Comparison) left;
            Comparison compRight = (Comparison) right;
            Expression added = compLeft.getAdditional(session, compRight, true);
            if (added != null) {
                added = added.optimize(session);
                ConditionAndOr a = new ConditionAndOr(AND, this, added);
                return a;
            }
        }
    }
    // (A=1 AND (B=2 OR B=3))
    if (andOrType == OR && session.getDatabase().getSettings().optimizeOr) {
        // try to add conditions (A=B AND B=1: add A=1)
        if (left instanceof Comparison && right instanceof Comparison) {
            Comparison compLeft = (Comparison) left;
            Comparison compRight = (Comparison) right;
            Expression added = compLeft.getAdditional(session, compRight, false);
            if (added != null) {
                return added.optimize(session);
            }
        } else if (left instanceof ConditionIn && right instanceof Comparison) {
            Expression added = ((ConditionIn) left).getAdditional((Comparison) right);
            if (added != null) {
                return added.optimize(session);
            }
        } else if (right instanceof ConditionIn && left instanceof Comparison) {
            Expression added = ((ConditionIn) right).getAdditional((Comparison) left);
            if (added != null) {
                return added.optimize(session);
            }
        } else if (left instanceof ConditionInConstantSet && right instanceof Comparison) {
            Expression added = ((ConditionInConstantSet) left).getAdditional(session, (Comparison) right);
            if (added != null) {
                return added.optimize(session);
            }
        } else if (right instanceof ConditionInConstantSet && left instanceof Comparison) {
            Expression added = ((ConditionInConstantSet) right).getAdditional(session, (Comparison) left);
            if (added != null) {
                return added.optimize(session);
            }
        }
    }
    // TODO optimization: convert .. OR .. to UNION if the cost is lower
    Value l = left.isConstant() ? left.getValue(session) : null;
    Value r = right.isConstant() ? right.getValue(session) : null;
    if (l == null && r == null) {
        return this;
    }
    if (l != null && r != null) {
        return ValueExpression.get(getValue(session));
    }
    switch(andOrType) {
        case AND:
            if (l != null) {
                if (Boolean.FALSE.equals(l.getBoolean())) {
                    return ValueExpression.get(l);
                } else if (Boolean.TRUE.equals(l.getBoolean())) {
                    return right;
                }
            } else if (r != null) {
                if (Boolean.FALSE.equals(r.getBoolean())) {
                    return ValueExpression.get(r);
                } else if (Boolean.TRUE.equals(r.getBoolean())) {
                    return left;
                }
            }
            break;
        case OR:
            if (l != null) {
                if (Boolean.TRUE.equals(l.getBoolean())) {
                    return ValueExpression.get(l);
                } else if (Boolean.FALSE.equals(l.getBoolean())) {
                    return right;
                }
            } else if (r != null) {
                if (Boolean.TRUE.equals(r.getBoolean())) {
                    return ValueExpression.get(r);
                } else if (Boolean.FALSE.equals(r.getBoolean())) {
                    return left;
                }
            }
            break;
        default:
            DbException.throwInternalError("type=" + andOrType);
    }
    return this;
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 44 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class ConditionIn method getValue.

@Override
public Value getValue(Session session) {
    Value l = left.getValue(session);
    if (l == ValueNull.INSTANCE) {
        return l;
    }
    boolean result = false;
    boolean hasNull = false;
    for (Expression e : valueList) {
        Value r = e.getValue(session);
        if (r == ValueNull.INSTANCE) {
            hasNull = true;
        } else {
            r = r.convertTo(l.getType());
            result = Comparison.compareNotNull(database, l, r, Comparison.EQUAL);
            if (result) {
                break;
            }
        }
    }
    if (!result && hasNull) {
        return ValueNull.INSTANCE;
    }
    return ValueBoolean.get(result);
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 45 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class ConditionInSelect method getValueSlow.

private Value getValueSlow(LocalResult rows, Value l) {
    // this only returns the correct result if the result has at least one
    // row, and if l is not null
    boolean hasNull = false;
    boolean result = all;
    while (rows.next()) {
        boolean value;
        Value r = rows.currentRow()[0];
        if (r == ValueNull.INSTANCE) {
            value = false;
            hasNull = true;
        } else {
            value = Comparison.compareNotNull(database, l, r, compareType);
        }
        if (!value && all) {
            result = false;
            break;
        } else if (value && !all) {
            result = true;
            break;
        }
    }
    if (!result && hasNull) {
        return ValueNull.INSTANCE;
    }
    return ValueBoolean.get(result);
}
Also used : Value(com.wplatform.ddal.value.Value)

Aggregations

Value (com.wplatform.ddal.value.Value)84 Expression (com.wplatform.ddal.command.expression.Expression)14 Column (com.wplatform.ddal.dbobject.table.Column)14 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)13 DbException (com.wplatform.ddal.message.DbException)9 SQLException (java.sql.SQLException)8 Row (com.wplatform.ddal.result.Row)7 SearchRow (com.wplatform.ddal.result.SearchRow)7 PreparedStatement (java.sql.PreparedStatement)7 TableMate (com.wplatform.ddal.dbobject.table.TableMate)6 LocalResult (com.wplatform.ddal.result.LocalResult)6 ResultInterface (com.wplatform.ddal.result.ResultInterface)5 Connection (java.sql.Connection)5 Parameter (com.wplatform.ddal.command.expression.Parameter)4 List (java.util.List)4 DataSource (javax.sql.DataSource)4 Query (com.wplatform.ddal.command.dml.Query)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3