Search in sources :

Example 36 with Value

use of com.wplatform.ddal.value.Value 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 37 with Value

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

the class Query method prepareOrder.

/**
 * Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
 * objects. The expression list is extended if necessary.
 *
 * @param orderList       a list of {@link SelectOrderBy} elements
 * @param expressionCount the number of columns in the query
 * @return the {@link SortOrder} object
 */
public SortOrder prepareOrder(ArrayList<SelectOrderBy> orderList, int expressionCount) {
    int size = orderList.size();
    int[] index = new int[size];
    int[] sortType = new int[size];
    for (int i = 0; i < size; i++) {
        SelectOrderBy o = orderList.get(i);
        int idx;
        boolean reverse = false;
        Expression expr = o.columnIndexExpr;
        Value v = expr.getValue(null);
        if (v == ValueNull.INSTANCE) {
            // parameter not yet set - order by first column
            idx = 0;
        } else {
            idx = v.getInt();
            if (idx < 0) {
                reverse = true;
                idx = -idx;
            }
            idx -= 1;
            if (idx < 0 || idx >= expressionCount) {
                throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, "" + (idx + 1));
            }
        }
        index[i] = idx;
        boolean desc = o.descending;
        if (reverse) {
            desc = !desc;
        }
        int type = desc ? SortOrder.DESCENDING : SortOrder.ASCENDING;
        if (o.nullsFirst) {
            type += SortOrder.NULLS_FIRST;
        } else if (o.nullsLast) {
            type += SortOrder.NULLS_LAST;
        }
        sortType[i] = type;
    }
    return new SortOrder(session.getDatabase(), index, sortType, orderList);
}
Also used : Value(com.wplatform.ddal.value.Value) SortOrder(com.wplatform.ddal.result.SortOrder)

Example 38 with Value

use of com.wplatform.ddal.value.Value 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 39 with Value

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

the class JdbcConnection method createClob.

/**
 * Create a new empty Clob object.
 *
 * @return the object
 */
@Override
public Clob createClob() throws SQLException {
    try {
        int id = getNextId(TraceObject.CLOB);
        debugCodeAssign("Clob", TraceObject.CLOB, id, "createClob()");
        checkClosedForWrite();
        try {
            Value v = ValueLobDb.createTempClob(new InputStreamReader(new ByteArrayInputStream(Utils.EMPTY_BYTES)), 0);
            session.addTemporaryLob(v);
            return new JdbcClob(this, v, id);
        } finally {
            afterWriting();
        }
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Value(com.wplatform.ddal.value.Value) Savepoint(java.sql.Savepoint) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) DbException(com.wplatform.ddal.message.DbException)

Example 40 with Value

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

the class SelectUnion method queryWithoutCache.

@Override
protected LocalResult queryWithoutCache(int maxRows, ResultTarget target) {
    if (maxRows != 0) {
        // maxRows is set (maxRows 0 means no limit)
        int l;
        if (limitExpr == null) {
            l = -1;
        } else {
            Value v = limitExpr.getValue(session);
            l = v == ValueNull.INSTANCE ? -1 : v.getInt();
        }
        if (l < 0) {
            // for limitExpr, 0 means no rows, and -1 means no limit
            l = maxRows;
        } else {
            l = Math.min(l, maxRows);
        }
        limitExpr = ValueExpression.get(ValueInt.get(l));
    }
    if (session.getDatabase().getSettings().optimizeInsertFromSelect) {
        if (unionType == UNION_ALL && target != null) {
            if (sort == null && !distinct && maxRows == 0 && offsetExpr == null && limitExpr == null) {
                left.query(0, target);
                right.query(0, target);
                return null;
            }
        }
    }
    int columnCount = left.getColumnCount();
    LocalResult result = new LocalResult(session, expressionArray, columnCount);
    if (sort != null) {
        result.setSortOrder(sort);
    }
    if (distinct) {
        left.setDistinct(true);
        right.setDistinct(true);
        result.setDistinct();
    }
    if (randomAccessResult) {
        result.setRandomAccess();
    }
    switch(unionType) {
        case UNION:
        case EXCEPT:
            left.setDistinct(true);
            right.setDistinct(true);
            result.setDistinct();
            break;
        case UNION_ALL:
            break;
        case INTERSECT:
            left.setDistinct(true);
            right.setDistinct(true);
            break;
        default:
            DbException.throwInternalError("type=" + unionType);
    }
    LocalResult l = left.query(0);
    LocalResult r = right.query(0);
    l.reset();
    r.reset();
    switch(unionType) {
        case UNION_ALL:
        case UNION:
            {
                while (l.next()) {
                    result.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    result.addRow(convert(r.currentRow(), columnCount));
                }
                break;
            }
        case EXCEPT:
            {
                while (l.next()) {
                    result.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    result.removeDistinct(convert(r.currentRow(), columnCount));
                }
                break;
            }
        case INTERSECT:
            {
                LocalResult temp = new LocalResult(session, expressionArray, columnCount);
                temp.setDistinct();
                temp.setRandomAccess();
                while (l.next()) {
                    temp.addRow(convert(l.currentRow(), columnCount));
                }
                while (r.next()) {
                    Value[] values = convert(r.currentRow(), columnCount);
                    if (temp.containsDistinct(values)) {
                        result.addRow(values);
                    }
                }
                break;
            }
        default:
            DbException.throwInternalError("type=" + unionType);
    }
    if (offsetExpr != null) {
        result.setOffset(offsetExpr.getValue(session).getInt());
    }
    if (limitExpr != null) {
        Value v = limitExpr.getValue(session);
        if (v != ValueNull.INSTANCE) {
            result.setLimit(v.getInt());
        }
    }
    l.close();
    r.close();
    result.done();
    if (target != null) {
        while (result.next()) {
            target.addRow(result.currentRow());
        }
        result.close();
        return null;
    }
    return result;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) 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