@Override public CountResponse executeCount(Query query, List<SQLParam> parameters) { ConnectionImpl connection = null; try { String sql = query.toString(); LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$ connection = getConnection(); final PreparedStatementImpl stmt = connection.prepareStatement(sql); if (!parameters.isEmpty()) { for (int i = 0; i < parameters.size(); i++) { stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType); } } ResultSet rs = stmt.executeQuery(); rs.next(); int count = rs.getInt(1); rs.close(); stmt.close(); return Responses.count(count); } catch (Exception e) { throw new ServerErrorException(e.getMessage(), e); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { } } }
@Test public void testWarnings() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); DQP dqp = Mockito.mock(DQP.class); ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>(); Mockito.stub(dqp.executeRequest(Mockito.anyLong(), (RequestMessage) Mockito.anyObject())) .toReturn(results); ResultsMessage rm = new ResultsMessage(); rm.setResults(new List<?>[] {Arrays.asList(1)}); rm.setWarnings(Arrays.asList(new Throwable())); rm.setColumnNames(new String[] {"expr1"}); rm.setDataTypes(new String[] {"string"}); results.getResultsReceiver().receiveResults(rm); Mockito.stub(conn.getDQP()).toReturn(dqp); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) { @Override protected java.util.TimeZone getServerTimeZone() throws java.sql.SQLException { return null; } }; statement.execute("select 'a'"); assertNotNull(statement.getResultSet()); SQLWarning warning = statement.getWarnings(); assertNotNull(warning); assertNull(warning.getNextWarning()); }
@Override public VDBMetaData getVDB() { ConnectionImpl connection = null; if (this.vdb == null) { try { connection = getConnection(); LocalServerConnection lsc = (LocalServerConnection) connection.getServerConnection(); VDBMetaData vdb = lsc.getWorkContext().getVDB(); if (vdb == null) { throw new NotFoundException( ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16001, this.vdbName, this.vdbVersion)); } this.vdb = vdb; } catch (SQLException e) { throw new ServerErrorException(e.getMessage(), e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } } return this.vdb; }
@Test public void testSetPayloadStatement() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Properties p = new Properties(); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertFalse(statement.execute("set payload foo bar")); // $NON-NLS-1$ }
@Test public void testTimeoutProperty() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Properties p = new Properties(); p.setProperty(ExecutionProperties.QUERYTIMEOUT, "2"); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertEquals(2, statement.getQueryTimeout()); }
@Test public void testSetAuthorizationStatement() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Properties p = new Properties(); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertFalse(statement.execute("set session authorization bar")); // $NON-NLS-1$ Mockito.verify(conn).changeUser("bar", null); }
@Test public void testUseJDBC4ColumnNameAndLabelSemantics() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Properties p = new Properties(); p.setProperty(ExecutionProperties.JDBC4COLUMNNAMEANDLABELSEMANTICS, "false"); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertEquals( Boolean.FALSE.toString(), statement.getExecutionProperty(ExecutionProperties.JDBC4COLUMNNAMEANDLABELSEMANTICS)); }
@Test public void testTransactionStatements() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Properties p = new Properties(); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertFalse(statement.execute("start transaction")); // $NON-NLS-1$ Mockito.verify(conn).setAutoCommit(false); assertFalse(statement.execute("commit")); // $NON-NLS-1$ Mockito.verify(conn).setAutoCommit(true); assertFalse(statement.execute("start transaction")); // $NON-NLS-1$ assertFalse(statement.execute("rollback")); // $NON-NLS-1$ Mockito.verify(conn).rollback(false); }
@SuppressWarnings("unchecked") @Test public void testTransactionStatementsAsynch() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Mockito.stub(conn.submitSetAutoCommitTrue(Mockito.anyBoolean())) .toReturn((ResultsFuture) ResultsFuture.NULL_FUTURE); Properties p = new Properties(); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); statement.submitExecute("start transaction", null); // $NON-NLS-1$ Mockito.verify(conn).setAutoCommit(false); statement.submitExecute("commit", null); // $NON-NLS-1$ Mockito.verify(conn).submitSetAutoCommitTrue(true); statement.submitExecute("start transaction", null); // $NON-NLS-1$ statement.submitExecute("rollback", null); // $NON-NLS-1$ Mockito.verify(conn).submitSetAutoCommitTrue(false); }
@Override public UpdateResponse executeUpdate(Command query, List<SQLParam> parameters) { ConnectionImpl connection = null; try { String sql = query.toString(); LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$ connection = getConnection(); final PreparedStatementImpl stmt = connection.prepareStatement( sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, Statement.RETURN_GENERATED_KEYS); if (!parameters.isEmpty()) { for (int i = 0; i < parameters.size(); i++) { stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType); } } final int count = stmt.executeUpdate(); final Map<String, Object> keys = getGeneratedKeys(stmt.getGeneratedKeys()); stmt.close(); return new UpdateResponse() { @Override public Map<String, Object> getGeneratedKeys() { return keys; } @Override public int getUpdateCount() { return count; } }; } catch (Exception e) { throw new ServerErrorException(e.getMessage(), e); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { } } }
@Test public void testBatchExecution() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); DQP dqp = Mockito.mock(DQP.class); ResultsFuture<ResultsMessage> results = new ResultsFuture<ResultsMessage>(); Mockito.stub(dqp.executeRequest(Mockito.anyLong(), (RequestMessage) Mockito.anyObject())) .toReturn(results); ResultsMessage rm = new ResultsMessage(); rm.setResults(new List<?>[] {Arrays.asList(1), Arrays.asList(2)}); rm.setUpdateResult(true); results.getResultsReceiver().receiveResults(rm); Mockito.stub(conn.getDQP()).toReturn(dqp); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); statement.clearBatch(); // previously caused npe statement.addBatch("delete from table"); // $NON-NLS-1$ statement.addBatch("delete from table1"); // $NON-NLS-1$ assertTrue(Arrays.equals(new int[] {1, 2}, statement.executeBatch())); }
@Test public void testPropertiesOverride() throws Exception { ConnectionImpl conn = Mockito.mock(ConnectionImpl.class); Properties p = new Properties(); p.setProperty(ExecutionProperties.ANSI_QUOTED_IDENTIFIERS, Boolean.TRUE.toString()); Mockito.stub(conn.getExecutionProperties()).toReturn(p); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertEquals( Boolean.TRUE.toString(), statement.getExecutionProperty(ExecutionProperties.ANSI_QUOTED_IDENTIFIERS)); statement.setExecutionProperty( ExecutionProperties.ANSI_QUOTED_IDENTIFIERS, Boolean.FALSE.toString()); assertEquals( Boolean.FALSE.toString(), statement.getExecutionProperty(ExecutionProperties.ANSI_QUOTED_IDENTIFIERS)); assertEquals( Boolean.TRUE.toString(), p.getProperty(ExecutionProperties.ANSI_QUOTED_IDENTIFIERS)); }
@Test public void testDisableLocalTransations() throws Exception { ServerConnection mock = Mockito.mock(ServerConnection.class); DQP dqp = Mockito.mock(DQP.class); Mockito.stub(mock.getService(DQP.class)).toReturn(dqp); ConnectionImpl conn = new ConnectionImpl(mock, new Properties(), "x"); StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); assertTrue(conn.getAutoCommit()); statement.execute("set disablelocaltxn true"); assertFalse(statement.execute("start transaction")); // $NON-NLS-1$ conn.beginLocalTxnIfNeeded(); assertFalse(conn.isInLocalTxn()); statement.execute("set disablelocaltxn false"); assertFalse(statement.execute("start transaction")); // $NON-NLS-1$ conn.beginLocalTxnIfNeeded(); assertTrue(conn.isInLocalTxn()); }
@Override public EntityList executeSQL( Query query, List<SQLParam> parameters, EdmEntitySet entitySet, LinkedHashMap<String, Boolean> projectedColumns, QueryInfo queryInfo) { ConnectionImpl connection = null; try { boolean cache = queryInfo != null && this.batchSize > 0; if (cache) { CacheHint hint = new CacheHint(); hint.setTtl(this.cacheTime); hint.setScope(CacheDirective.Scope.USER); hint.setMinRows(Long.valueOf(this.batchSize)); query.setCacheHint(hint); } boolean getCount = false; if (queryInfo != null) { getCount = queryInfo.inlineCount == InlineCount.ALLPAGES; if (!getCount && (queryInfo.top != null || queryInfo.skip != null)) { if (queryInfo.top != null && queryInfo.skip != null) { query.setLimit(new Limit(new Constant(queryInfo.skip), new Constant(queryInfo.top))); } else if (queryInfo.top != null) { query.setLimit(new Limit(new Constant(0), new Constant(queryInfo.top))); } } } connection = getConnection(); String sessionId = connection.getServerConnection().getLogonResult().getSessionID(); String skipToken = null; if (queryInfo != null && queryInfo.skipToken != null) { skipToken = queryInfo.skipToken; if (cache) { int idx = queryInfo.skipToken.indexOf(DELIMITER); sessionId = queryInfo.skipToken.substring(0, idx); skipToken = queryInfo.skipToken.substring(idx + 2); } } String sql = query.toString(); if (cache) { sql += " /* " + sessionId + " */"; // $NON-NLS-1$ //$NON-NLS-2$ } LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$ final PreparedStatement stmt = connection.prepareStatement( sql, cache ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); if (parameters != null && !parameters.isEmpty()) { for (int i = 0; i < parameters.size(); i++) { stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType); } } final ResultSet rs = stmt.executeQuery(); if (projectedColumns == null) { projectedColumns = new LinkedHashMap<String, Boolean>(); for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { projectedColumns.put(rs.getMetaData().getColumnLabel(i + 1), Boolean.TRUE); } } EntityList result = new EntityList(invalidCharacterReplacement); HashMap<String, EdmProperty> propertyTypes = new HashMap<String, EdmProperty>(); EdmEntityType entityType = entitySet.getType(); Iterator<EdmProperty> propIter = entityType.getProperties().iterator(); while (propIter.hasNext()) { EdmProperty prop = propIter.next(); propertyTypes.put(prop.getName(), prop); } // skip to the initial position int count = 0; int skipSize = 0; // skip based upon the skip value if (getCount && queryInfo.skip != null) { skipSize = queryInfo.skip; } // skip based upon the skipToken if (skipToken != null) { skipSize += Integer.parseInt(skipToken); } if (skipSize > 0) { count += skip(cache, rs, skipSize); } // determine the number of records to return int size = batchSize; int top = Integer.MAX_VALUE; if (getCount && queryInfo.top != null) { top = queryInfo.top; size = top; if (batchSize > 0) { size = Math.min(batchSize, size); } } else if (size < 1) { size = Integer.MAX_VALUE; } // build the results for (int i = 0; i < size; i++) { if (!rs.next()) { break; } count++; result.addEntity(rs, propertyTypes, projectedColumns, entitySet); } // set the count if (getCount) { if (!cache) { while (rs.next()) { count++; } } else { rs.last(); count = rs.getRow(); } } result.setCount(count); // set the skipToken if needed if (cache && result.size() == this.batchSize) { int end = skipSize + result.size(); if (getCount) { if (end < Math.min(top, count)) { result.setNextToken(nextToken(cache, sessionId, end)); } } else if (rs.next()) { result.setNextToken(nextToken(cache, sessionId, end)); // will force the entry to cache or is effectively a no-op when already cached rs.last(); } } return result; } catch (Exception e) { throw new ServerErrorException(e.getMessage(), e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } }
@Override public BaseResponse executeCall(String sql, List<SQLParam> parameters, EdmType returnType) { ConnectionImpl connection = null; try { LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$ connection = getConnection(); final CallableStatementImpl stmt = connection.prepareCall(sql); int i = 1; if (returnType != null && returnType.isSimple()) { stmt.registerOutParameter( i++, JDBCSQLTypeInfo.getSQLType( ODataTypeManager.teiidType(returnType.getFullyQualifiedTypeName()))); } if (!parameters.isEmpty()) { for (SQLParam param : parameters) { stmt.setObject(i++, param.value, param.sqlType); } } boolean results = stmt.execute(); if (results) { final ResultSet rs = stmt.getResultSet(); OCollection.Builder resultRows = OCollections.newBuilder( (EdmComplexType) ((EdmCollectionType) returnType).getItemType()); while (rs.next()) { int idx = 1; List<OProperty<?>> row = new ArrayList<OProperty<?>>(); Iterator<EdmProperty> props = ((EdmComplexType) ((EdmCollectionType) returnType).getItemType()) .getProperties() .iterator(); while (props.hasNext()) { EdmProperty prop = props.next(); row.add( buildPropery( prop.getName(), prop.getType(), rs.getObject(idx++), invalidCharacterReplacement)); } OComplexObject erow = OComplexObjects.create( (EdmComplexType) ((EdmCollectionType) returnType).getItemType(), row); resultRows.add(erow); } String collectionName = returnType.getFullyQualifiedTypeName(); collectionName = collectionName.replace("(", "_"); // $NON-NLS-1$ //$NON-NLS-2$ collectionName = collectionName.replace(")", "_"); // $NON-NLS-1$ //$NON-NLS-2$ return Responses.collection(resultRows.build(), null, null, null, collectionName); } if (returnType != null) { Object result = stmt.getObject(1); OProperty prop = buildPropery("return", returnType, result, invalidCharacterReplacement); // $NON-NLS-1$ return Responses.property(prop); } return null; } catch (Exception e) { throw new ServerErrorException(e.getMessage(), e); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { } } } }