use of org.teiid.query.sql.symbol.Reference 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();
}
}
}
use of org.teiid.query.sql.symbol.Reference in project teiid by teiid.
the class TestEmbeddedServer method testDynamicUpdate.
@Test
public void testDynamicUpdate() throws Exception {
EmbeddedConfiguration ec = new EmbeddedConfiguration();
MockTransactionManager tm = new MockTransactionManager();
ec.setTransactionManager(tm);
ec.setUseDisk(false);
es.start(ec);
es.addTranslator("t", new ExecutionFactory<Void, Void>() {
@Override
public boolean supportsCompareCriteriaEquals() {
return true;
}
@Override
public boolean isSourceRequired() {
return false;
}
@Override
public UpdateExecution createUpdateExecution(Command command, ExecutionContext executionContext, RuntimeMetadata metadata, Void connection) throws TranslatorException {
Collection<Literal> values = CollectorVisitor.collectObjects(Literal.class, command);
assertEquals(2, values.size());
for (Literal literal : values) {
assertFalse(literal.getValue() instanceof Reference);
}
return new UpdateExecution() {
@Override
public void execute() throws TranslatorException {
}
@Override
public void close() {
}
@Override
public void cancel() throws TranslatorException {
}
@Override
public int[] getUpdateCounts() throws DataNotAvailableException, TranslatorException {
return new int[] { 1 };
}
};
}
});
ModelMetaData mmd1 = new ModelMetaData();
mmd1.setName("accounts");
mmd1.setSchemaSourceType("ddl");
mmd1.setSchemaText(ObjectConverterUtil.convertFileToString(UnitTestUtil.getTestDataFile("dynamic_update.sql")));
mmd1.addSourceMapping("y", "t", null);
es.deployVDB("vdb", mmd1);
Connection c = es.getDriver().connect("jdbc:teiid:vdb", null);
PreparedStatement ps = c.prepareStatement("update hello1 set SchemaName=? where Name=?");
ps.setString(1, "test1223");
ps.setString(2, "Columns");
assertEquals(1, ps.executeUpdate());
}
Aggregations