Search in sources :

Example 41 with Reference

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

Example 42 with Reference

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());
}
Also used : Reference(org.teiid.query.sql.symbol.Reference) UpdateExecution(org.teiid.translator.UpdateExecution) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) Literal(org.teiid.language.Literal) Collection(java.util.Collection) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Test(org.junit.Test)

Aggregations

Reference (org.teiid.query.sql.symbol.Reference)42 Test (org.junit.Test)15 Constant (org.teiid.query.sql.symbol.Constant)14 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)14 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)12 Expression (org.teiid.query.sql.symbol.Expression)11 Limit (org.teiid.query.sql.lang.Limit)7 Query (org.teiid.query.sql.lang.Query)7 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)7 ArrayList (java.util.ArrayList)6 Criteria (org.teiid.query.sql.lang.Criteria)6 LinkedList (java.util.LinkedList)5 From (org.teiid.query.sql.lang.From)5 Select (org.teiid.query.sql.lang.Select)5 SetQuery (org.teiid.query.sql.lang.SetQuery)5 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)5 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)5 Column (org.teiid.metadata.Column)4 Command (org.teiid.query.sql.lang.Command)4 SPParameter (org.teiid.query.sql.lang.SPParameter)4