Search in sources :

Example 1 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class Query method query.

/**
     * Execute the query, writing the result to the target result.
     *
     * @param limit  the maximum number of rows to return
     * @param target the target result (null will return the result)
     * @return the result set (if the target is not set).
     */
public LocalResult query(int limit, ResultTarget target) {
    if (noCache) {
        return queryWithoutCache(limit, target);
    }
    Value[] params = getParameterValues();
    if (isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) {
        if (lastResult != null && !lastResult.isClosed() && limit == lastLimit) {
            if (sameResultAsLast(session, params, lastParameters)) {
                lastResult = lastResult.createShallowCopy(session);
                if (lastResult != null) {
                    lastResult.reset();
                    return lastResult;
                }
            }
        }
    }
    lastParameters = params;
    closeLastResult();
    LocalResult r = queryWithoutCache(limit, target);
    lastResult = r;
    lastLimit = limit;
    return r;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Value(com.wplatform.ddal.value.Value)

Example 2 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class ConditionInSelect method exportParameters.

@Override
public String exportParameters(TableFilter filter, List<Value> container) {
    Session session = filter.getSession();
    LocalResult rows = query(session);
    if (rows.getRowCount() > 0) {
        StatementBuilder buff = new StatementBuilder();
        buff.append('(').append(left.exportParameters(filter, container)).append(' ');
        if (all) {
            //由于all代表全部,所以<all表示小于子查询中返回全部值中的最小值;
            //>all表示大于子查询中返回全部值中的最大值。
            buff.append(Comparison.getCompareOperator(compareType)).append(" ALL");
        } else {
            if (compareType == Comparison.EQUAL) {
                buff.append("IN");
            } else {
                //<any可以理解为小于子查询中返回的任意一个值,因此只要小于最大值即可
                //>any可以理解为大于子查询中返回的任意一个值,因此只要大于最小值即可
                buff.append(Comparison.getCompareOperator(compareType)).append(" ANY");
            }
        }
        buff.append("(");
        while (rows.next()) {
            buff.appendExceptFirst(",");
            buff.append("?");
            Value r = rows.currentRow()[0];
            container.add(r);
        }
        buff.append("))");
        return buff.toString();
    } else {
        return "1 = 0";
    }
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) Session(com.wplatform.ddal.engine.Session)

Example 3 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class ConditionInSelect method getValue.

@Override
public Value getValue(Session session) {
    LocalResult rows = query(session);
    try {
        Value l = left.getValue(session);
        if (rows.getRowCount() == 0) {
            return ValueBoolean.get(all);
        } else if (l == ValueNull.INSTANCE) {
            return l;
        }
        if (!session.getDatabase().getSettings().optimizeInSelect) {
            return getValueSlow(rows, l);
        }
        if (all || (compareType != Comparison.EQUAL && compareType != Comparison.EQUAL_NULL_SAFE)) {
            return getValueSlow(rows, l);
        }
        int dataType = rows.getColumnType(0);
        if (dataType == Value.NULL) {
            return ValueBoolean.get(false);
        }
        l = l.convertTo(dataType);
        if (rows.containsDistinct(new Value[] { l })) {
            return ValueBoolean.get(true);
        }
        if (rows.containsDistinct(new Value[] { ValueNull.INSTANCE })) {
            return ValueNull.INSTANCE;
        }
        return ValueBoolean.get(false);
    } finally {
        rows.close();
    }
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Value(com.wplatform.ddal.value.Value)

Example 4 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class ConditionExists method getValue.

@Override
public Value getValue(Session session) {
    query.setSession(session);
    LocalResult result = query.query(1);
    session.addTemporaryResult(result);
    boolean r = result.getRowCount() > 0;
    return ValueBoolean.get(r);
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult)

Example 5 with LocalResult

use of com.wplatform.ddal.result.LocalResult in project jdbc-shards by wplatform.

the class Call method query.

@Override
public ResultInterface query(int maxrows) {
    setCurrentRowNumber(1);
    Value v = expression.getValue(session);
    if (isResultSet) {
        v = v.convertTo(Value.RESULT_SET);
        ResultSet rs = v.getResultSet();
        return LocalResult.read(session, rs, maxrows);
    }
    LocalResult result = new LocalResult(session, expressions, 1);
    Value[] row = { v };
    result.addRow(row);
    result.done();
    return result;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Value(com.wplatform.ddal.value.Value) ResultSet(java.sql.ResultSet)

Aggregations

LocalResult (com.wplatform.ddal.result.LocalResult)14 Value (com.wplatform.ddal.value.Value)6 Expression (com.wplatform.ddal.command.expression.Expression)2 Column (com.wplatform.ddal.dbobject.table.Column)2 Database (com.wplatform.ddal.engine.Database)2 ExpressionColumn (com.wplatform.ddal.command.expression.ExpressionColumn)1 Session (com.wplatform.ddal.engine.Session)1 ResultTarget (com.wplatform.ddal.result.ResultTarget)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1 ValueResultSet (com.wplatform.ddal.value.ValueResultSet)1 ValueString (com.wplatform.ddal.value.ValueString)1 ResultSet (java.sql.ResultSet)1