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);
}
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);
}
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();
}
}
}
Aggregations