Search in sources :

Example 1 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestConnectorWorkItem method testProcedureBatching.

@Test
public void testProcedureBatching() throws Exception {
    ProcedureExecution exec = new FakeProcedureExecution(2, 1);
    // this has two result set columns and 1 out parameter
    int total_columns = 3;
    // $NON-NLS-1$
    StoredProcedure command = (StoredProcedure) helpGetCommand("{call pm2.spTest8(?)}", EXAMPLE_BQT);
    command.getInputParameters().get(0).setExpression(new Constant(1));
    Call proc = new LanguageBridgeFactory(EXAMPLE_BQT).translate(command);
    ProcedureBatchHandler pbh = new ProcedureBatchHandler(proc, exec);
    assertEquals(total_columns, pbh.padRow(Arrays.asList(null, null)).size());
    List params = pbh.getParameterRow();
    assertEquals(total_columns, params.size());
    // check the parameter value
    assertEquals(Integer.valueOf(0), params.get(2));
    try {
        pbh.padRow(Arrays.asList(1));
        // $NON-NLS-1$
        fail("Expected exception from resultset mismatch");
    } catch (TranslatorException err) {
        assertEquals("TEIID30479 Could not process stored procedure results for EXEC spTest8(1).  Expected 2 result set columns, but was 1.  Please update your models to allow for stored procedure results batching.", // $NON-NLS-1$
        err.getMessage());
    }
}
Also used : Call(org.teiid.language.Call) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) ProcedureExecution(org.teiid.translator.ProcedureExecution) Constant(org.teiid.query.sql.symbol.Constant) List(java.util.List) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException) SourceHint(org.teiid.query.sql.lang.SourceHint) Test(org.junit.Test)

Example 2 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestODataQueryExecution method testActionReturnsComplexCollection.

@Test
public void testActionReturnsComplexCollection() throws Exception {
    String query = "exec invoke(1, 'foo')";
    String expectedURL = "invoke";
    String response = "{\"value\":[{\n" + "            \"street\":\"United States\",\n" + "            \"city\":\"Boise\",\n" + "            \"state\":\"ID\"\n" + "           }," + "           {" + "            \"street\":\"China\",\n" + "            \"city\":\"Newyork\",\n" + "            \"state\":\"NY\"\n" + "         }]}";
    CsdlComplexType complex = TestODataMetadataProcessor.complexType("Address");
    CsdlReturnType returnType = new CsdlReturnType();
    returnType.setType("namespace.Address");
    MetadataFactory mf = TestODataMetadataProcessor.actionMetadata("invoke", returnType, complex);
    ProcedureExecution excution = helpProcedureExecute(mf, query, response, expectedURL, 200);
    assertArrayEquals(new Object[] { "United States", "Boise", "ID" }, excution.next().toArray(new Object[3]));
    assertArrayEquals(new Object[] { "China", "Newyork", "NY" }, excution.next().toArray(new Object[3]));
    assertNull(excution.next());
}
Also used : MetadataFactory(org.teiid.metadata.MetadataFactory) ProcedureExecution(org.teiid.translator.ProcedureExecution) CsdlComplexType(org.apache.olingo.commons.api.edm.provider.CsdlComplexType) CsdlReturnType(org.apache.olingo.commons.api.edm.provider.CsdlReturnType) Test(org.junit.Test)

Example 3 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class FileExecutionFactory method createProcedureExecution.

// @Override
public ProcedureExecution createProcedureExecution(final Call command, final ExecutionContext executionContext, final RuntimeMetadata metadata, final Connection conn) throws TranslatorException {
    if (conn instanceof VirtualFileConnection) {
        return new VirtualFileProcedureExecution(command, (VirtualFileConnection) conn);
    }
    final FileConnection fc = (FileConnection) conn;
    if (command.getProcedureName().equalsIgnoreCase(SAVEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                Object file = command.getArguments().get(1).getArgumentValue().getValue();
                if (file == null || filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Saving", filePath);
                InputStream is = null;
                try {
                    if (file instanceof SQLXML) {
                        is = ((SQLXML) file).getBinaryStream();
                    } else if (file instanceof Clob) {
                        is = new ReaderInputStream(((Clob) file).getCharacterStream(), encoding);
                    } else if (file instanceof Blob) {
                        is = ((Blob) file).getBinaryStream();
                    } else {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("unknown_type"));
                    }
                    ObjectConverterUtil.write(is, fc.getFile(filePath));
                } catch (IOException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (SQLException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_writing"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    } else if (command.getProcedureName().equalsIgnoreCase(DELETEFILE)) {
        return new ProcedureExecution() {

            @Override
            public void execute() throws TranslatorException {
                String filePath = (String) command.getArguments().get(0).getArgumentValue().getValue();
                if (filePath == null) {
                    // $NON-NLS-1$
                    throw new TranslatorException(UTIL.getString("non_null"));
                }
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Deleting", filePath);
                try {
                    File f = fc.getFile(filePath);
                    if (!f.exists()) {
                        if (exceptionIfFileNotFound) {
                            // $NON-NLS-1$
                            throw new TranslatorException(DataPlugin.Util.gs("file_not_found", filePath));
                        }
                    } else if (!f.delete()) {
                        // $NON-NLS-1$
                        throw new TranslatorException(UTIL.getString("error_deleting"));
                    }
                } catch (ResourceException e) {
                    // $NON-NLS-1$
                    throw new TranslatorException(e, UTIL.getString("error_deleting"));
                }
            }

            @Override
            public void close() {
            }

            @Override
            public void cancel() throws TranslatorException {
            }

            @Override
            public List<?> next() throws TranslatorException, DataNotAvailableException {
                return null;
            }

            @Override
            public List<?> getOutputParameterValues() throws TranslatorException {
                return Collections.emptyList();
            }
        };
    }
    return new FileProcedureExecution(command, fc);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) SQLXML(java.sql.SQLXML) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) TranslatorException(org.teiid.translator.TranslatorException) ResourceException(javax.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) Clob(java.sql.Clob) VirtualFile(org.jboss.vfs.VirtualFile) File(java.io.File) VirtualFileConnection(org.teiid.file.VirtualFileConnection) VirtualFileConnection(org.teiid.file.VirtualFileConnection) FileConnection(org.teiid.translator.FileConnection)

Example 4 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestOlapTranslator method testCannedProcedure.

@Test
public void testCannedProcedure() throws Exception {
    String ddl = "create foreign procedure proc(arg integer, arg1 date) returns table (x string) options (\"teiid_rel:native-query\" '$2 $1 something')";
    String query = "exec proc(2, {d'1970-01-01'})";
    TransformationMetadata tm = RealMetadataFactory.fromDDL(ddl, "x", "phy");
    CommandBuilder commandBuilder = new CommandBuilder(tm);
    Command obj = commandBuilder.getCommand(query);
    OlapExecutionFactory oef = new OlapExecutionFactory();
    Connection mock = Mockito.mock(java.sql.Connection.class);
    OlapWrapper mock2 = Mockito.mock(OlapWrapper.class);
    OlapConnection mock3 = Mockito.mock(OlapConnection.class);
    OlapStatement mock4 = Mockito.mock(OlapStatement.class);
    Mockito.stub(mock4.executeOlapQuery(Mockito.anyString())).toThrow(new TeiidRuntimeException());
    Mockito.stub(mock3.createStatement()).toReturn(mock4);
    Mockito.stub(mock2.unwrap(OlapConnection.class)).toReturn(mock3);
    Mockito.stub(mock.unwrap(OlapWrapper.class)).toReturn(mock2);
    ProcedureExecution pe = oef.createProcedureExecution((Call) obj, Mockito.mock(ExecutionContext.class), new RuntimeMetadataImpl(tm), mock);
    try {
        pe.execute();
        fail();
    } catch (TeiidRuntimeException e) {
        Mockito.verify(mock4).executeOlapQuery("'1970-01-01' 2 something");
    }
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) OlapConnection(org.olap4j.OlapConnection) Connection(java.sql.Connection) OlapConnection(org.olap4j.OlapConnection) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) OlapStatement(org.olap4j.OlapStatement) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) OlapWrapper(org.olap4j.OlapWrapper) ProcedureExecution(org.teiid.translator.ProcedureExecution) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Example 5 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestSwaggerQueryExecution method testParameterInPath.

@Test
public void testParameterInPath() throws Exception {
    String query = "exec getPetById(petId=>687789);";
    String expectedURL = "http://petstore.swagger.io/v2/pet/687789";
    String response = "{\n" + "  \"id\": 687789,\n" + "  \"category\": {\n" + "    \"id\": 0,\n" + "    \"name\": \"Lions\"\n" + "  },\n" + "  \"name\": \"nikky\",\n" + "  \"photoUrls\": [\n" + "    \"url1\"\n" + "  ],\n" + "  \"tags\": [\n" + "    {\n" + "      \"id\": 1,\n" + "      \"name\": \"tag1\"\n" + "    }\n" + "  ],\n" + "  \"status\": \"sold\"\n" + "}";
    ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "GET", null, getHeaders());
    assertArrayEquals(new Object[] { 687789L, 0L, "Lions", "nikky", new String[] { "url1" }, 1L, "tag1", "sold" }, excution.next().toArray(new Object[8]));
    assertNull(excution.next());
}
Also used : ProcedureExecution(org.teiid.translator.ProcedureExecution) Test(org.junit.Test)

Aggregations

ProcedureExecution (org.teiid.translator.ProcedureExecution)24 Test (org.junit.Test)20 MetadataFactory (org.teiid.metadata.MetadataFactory)7 CsdlReturnType (org.apache.olingo.commons.api.edm.provider.CsdlReturnType)6 List (java.util.List)5 ExecutionContext (org.teiid.translator.ExecutionContext)4 CsdlComplexType (org.apache.olingo.commons.api.edm.provider.CsdlComplexType)3 Call (org.teiid.language.Call)3 Command (org.teiid.language.Command)3 TranslatorException (org.teiid.translator.TranslatorException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 Connection (java.sql.Connection)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 DataSource (javax.activation.DataSource)2 TranslationUtility (org.teiid.cdk.api.TranslationUtility)2 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)2