Search in sources :

Example 11 with Limit

use of org.teiid.query.sql.lang.Limit in project teiid by teiid.

the class TestLimitParsing method testLimitWithReferences1.

@Test
public void testLimitWithReferences1() {
    Query query = new Query();
    Select select = new Select(Arrays.asList(new MultipleElementSymbol()));
    // $NON-NLS-1$
    From from = new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("a"))));
    query.setSelect(select);
    query.setFrom(from);
    query.setLimit(new Limit(new Reference(0), new Constant(new Integer(100))));
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTest("Select * from a limit ?,100", "SELECT * FROM a LIMIT ?, 100", query);
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Query(org.teiid.query.sql.lang.Query) SetQuery(org.teiid.query.sql.lang.SetQuery) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) Select(org.teiid.query.sql.lang.Select) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) From(org.teiid.query.sql.lang.From) Limit(org.teiid.query.sql.lang.Limit) Test(org.junit.Test)

Example 12 with Limit

use of org.teiid.query.sql.lang.Limit in project teiid by teiid.

the class TestLimitParsing method testFetchFirst.

@Test
public void testFetchFirst() {
    Query query = new Query();
    Select select = new Select(Arrays.asList(new MultipleElementSymbol()));
    // $NON-NLS-1$
    From from = new From(Arrays.asList(new UnaryFromClause(new GroupSymbol("a"))));
    query.setSelect(select);
    query.setFrom(from);
    query.setLimit(new Limit(null, new Constant(2)));
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTest("Select * from a fetch first 2 rows only", "SELECT * FROM a LIMIT 2", query);
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Query(org.teiid.query.sql.lang.Query) SetQuery(org.teiid.query.sql.lang.SetQuery) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) Constant(org.teiid.query.sql.symbol.Constant) Select(org.teiid.query.sql.lang.Select) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) From(org.teiid.query.sql.lang.From) Limit(org.teiid.query.sql.lang.Limit) Test(org.junit.Test)

Example 13 with Limit

use of org.teiid.query.sql.lang.Limit in project teiid by teiid.

the class LocalClient method executeSQL.

@Override
public void executeSQL(Query query, List<SQLParameter> parameters, boolean calculateTotalSize, Integer skipOption, Integer topOption, String nextOption, int pageSize, final QueryResponse response) throws SQLException {
    boolean cache = pageSize > 0;
    if (cache) {
        CacheHint hint = new CacheHint();
        hint.setTtl(getCacheTime());
        hint.setScope(CacheDirective.Scope.USER);
        query.setCacheHint(hint);
    }
    boolean getCount = false;
    getCount = calculateTotalSize;
    boolean skipAndTopApplied = false;
    if (!getCount && (topOption != null || skipOption != null)) {
        query.setLimit(new Limit(skipOption != null ? new Constant(skipOption) : null, topOption != null ? new Constant(topOption) : null));
        skipAndTopApplied = true;
    }
    String sessionId = getConnection().getServerConnection().getLogonResult().getSessionID();
    String nextToken = null;
    Integer savedEntityCount = null;
    if (nextOption != null) {
        nextToken = nextOption;
        if (cache) {
            StringTokenizer st = new StringTokenizer(nextOption, DELIMITER);
            sessionId = st.nextToken();
            nextToken = st.nextToken();
            if (st.hasMoreTokens()) {
                savedEntityCount = Integer.parseInt(st.nextToken());
            }
        }
        // the URL might have $count=true, but ignore it.
        getCount = false;
    }
    String sql = query.toString();
    if (cache) {
        // $NON-NLS-1$ //$NON-NLS-2$
        sql += " /* " + sessionId + " */";
    }
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql);
    final PreparedStatement stmt = getConnection().prepareStatement(sql, cache ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    if (parameters != null && !parameters.isEmpty()) {
        List<Reference> references = ReferenceCollectorVisitor.getReferences(query);
        for (int i = 0; i < references.size(); i++) {
            int index = references.get(i).getIndex();
            stmt.setObject(i + 1, parameters.get(index).getValue(), parameters.get(index).getSqlType());
        }
    }
    final ResultSet rs = stmt.executeQuery();
    // skip to the initial position
    int count = 0;
    int entityCount = 0;
    int skipSize = 0;
    // skip based upon the skip value
    if (nextToken == null && skipOption != null && !skipAndTopApplied) {
        if (skipOption > 0) {
            int s = skipEntities(rs, skipOption);
            count += s;
            entityCount = s;
            skipSize = count;
        }
    }
    // skip based upon the skipToken
    if (nextToken != null) {
        skipSize += Integer.parseInt(nextToken);
        if (skipSize > 0) {
            count += skip(cache, rs, skipSize);
        }
    }
    // determine the number of records to return
    int size = pageSize;
    int top = Integer.MAX_VALUE;
    if (getCount && topOption != null) {
        top = topOption;
        size = top;
        if (pageSize > 0) {
            size = Math.min(pageSize, size);
        }
    } else if (size < 1) {
        size = Integer.MAX_VALUE;
    }
    // build the results
    int i = 0;
    int nextCount = count;
    while (true) {
        if (!rs.next()) {
            break;
        }
        count++;
        i++;
        entityCount++;
        if (i > size) {
            break;
        }
        nextCount++;
        response.addRow(rs);
    }
    // set the count
    if (getCount) {
        while (rs.next()) {
            count++;
            entityCount++;
        }
    }
    if (savedEntityCount != null) {
        response.setCount(savedEntityCount);
    } else {
        response.setCount(entityCount);
    }
    // set the skipToken if needed
    if (cache && response.size() == pageSize) {
        long end = nextCount;
        if (getCount) {
            if (end < Math.min(top, count)) {
                response.setNextToken(nextToken(cache, sessionId, end, entityCount));
            }
        } else if (count != nextCount) {
            response.setNextToken(nextToken(cache, sessionId, end, null));
            // will force the entry to cache or is effectively a no-op when already cached
            rs.last();
        }
    }
}
Also used : CacheHint(org.teiid.query.sql.lang.CacheHint) Constant(org.teiid.query.sql.symbol.Constant) Reference(org.teiid.query.sql.symbol.Reference) PreparedStatement(java.sql.PreparedStatement) CacheHint(org.teiid.query.sql.lang.CacheHint) StringTokenizer(java.util.StringTokenizer) ResultSet(java.sql.ResultSet) Limit(org.teiid.query.sql.lang.Limit)

Aggregations

Limit (org.teiid.query.sql.lang.Limit)13 Test (org.junit.Test)11 SetQuery (org.teiid.query.sql.lang.SetQuery)11 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)11 From (org.teiid.query.sql.lang.From)10 Query (org.teiid.query.sql.lang.Query)10 Select (org.teiid.query.sql.lang.Select)10 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)10 Constant (org.teiid.query.sql.symbol.Constant)10 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)10 Reference (org.teiid.query.sql.symbol.Reference)7 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 StringTokenizer (java.util.StringTokenizer)1 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1 Command (org.teiid.query.sql.lang.Command)1 QueryCommand (org.teiid.query.sql.lang.QueryCommand)1 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)1