Search in sources :

Example 51 with TranslatorException

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

the class InfinispanUpdateExecution method insertRow.

private void insertRow(RemoteCache<Object, Object> cache, Object rowKey, InfinispanDocument row, boolean upsert) throws TranslatorException {
    // this is always single row; putIfAbsent is not working correctly.
    InfinispanDocument previous = (InfinispanDocument) cache.get(rowKey);
    if (previous != null && !upsert) {
        throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25005, previous.getName(), rowKey));
    }
    if (upsert && previous != null) {
        previous.merge(row);
        previous = (InfinispanDocument) cache.replace(rowKey, previous);
    } else {
        previous = row;
        previous = (InfinispanDocument) cache.put(rowKey, previous);
    }
}
Also used : InfinispanDocument(org.teiid.infinispan.api.InfinispanDocument) TranslatorException(org.teiid.translator.TranslatorException)

Example 52 with TranslatorException

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

the class JDBCProcedureExecution method execute.

@Override
public void execute() throws TranslatorException {
    Call procedure = (Call) command;
    columnDataTypes = procedure.getResultSetColumnTypes();
    // translate command
    TranslatedCommand translatedComm = translateCommand(procedure);
    // create statement or CallableStatement and execute
    String sql = translatedComm.getSql();
    try {
        // create parameter index map
        CallableStatement cstmt = getCallableStatement(sql);
        this.results = this.executionFactory.executeStoredProcedure(cstmt, translatedComm.getPreparedValues(), procedure.getReturnType());
        addStatementWarnings();
    } catch (SQLException e) {
        throw new TranslatorException(JDBCPlugin.Event.TEIID11004, e, JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID11004, sql));
    }
}
Also used : Call(org.teiid.language.Call) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) TranslatorException(org.teiid.translator.TranslatorException)

Example 53 with TranslatorException

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

the class JDBCQueryExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    try {
        if (results.next()) {
            // New row for result set
            List<Object> vals = new ArrayList<Object>(columnDataTypes.length);
            for (int i = 0; i < columnDataTypes.length; i++) {
                // Convert from 0-based to 1-based
                Object value = this.executionFactory.retrieveValue(results, i + 1, columnDataTypes[i]);
                vals.add(value);
            }
            return vals;
        }
    } catch (SQLException e) {
        throw new TranslatorException(e, // $NON-NLS-1$
        JDBCPlugin.Util.getString("JDBCTranslator.Unexpected_exception_translating_results___8", e.getMessage()));
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException)

Example 54 with TranslatorException

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

the class JDBCUpdateExecution method execute.

public int[] execute(BatchedUpdates batchedCommand) throws TranslatorException {
    boolean succeeded = false;
    boolean commitType = false;
    if (batchedCommand.isSingleResult()) {
        commitType = getAutoCommit(null);
    }
    Command[] commands = batchedCommand.getUpdateCommands().toArray(new Command[batchedCommand.getUpdateCommands().size()]);
    result = new int[commands.length];
    TranslatedCommand tCommand = null;
    int batchStart = 0;
    int i = 0;
    try {
        // before at the end of the command execution.
        if (commitType) {
            connection.setAutoCommit(false);
        }
        List<TranslatedCommand> executedCmds = new ArrayList<TranslatedCommand>();
        TranslatedCommand previousCommand = null;
        for (; i < commands.length; i++) {
            tCommand = translateCommand(commands[i]);
            if (tCommand.isPrepared()) {
                PreparedStatement pstmt = null;
                if (previousCommand != null && previousCommand.isPrepared() && previousCommand.getSql().equals(tCommand.getSql())) {
                    pstmt = (PreparedStatement) statement;
                } else {
                    if (!executedCmds.isEmpty()) {
                        executeBatch(i, result, executedCmds);
                        batchStart = i;
                    }
                    pstmt = getPreparedStatement(tCommand.getSql());
                }
                bind(pstmt, tCommand.getPreparedValues(), null);
                pstmt.addBatch();
            } else {
                if (previousCommand != null && previousCommand.isPrepared()) {
                    executeBatch(i, result, executedCmds);
                    batchStart = i;
                    getStatement();
                }
                if (statement == null) {
                    getStatement();
                }
                statement.addBatch(tCommand.getSql());
            }
            executedCmds.add(tCommand);
            previousCommand = tCommand;
        }
        if (!executedCmds.isEmpty()) {
            executeBatch(commands.length, result, executedCmds);
        }
        succeeded = true;
    } catch (TranslatorException e) {
        if (batchedCommand.isSingleResult()) {
            throw e;
        }
        int size = i + 1;
        // if there is a BatchUpdateException, there are more update counts to accumulate
        if (e.getCause() instanceof BatchUpdateException) {
            BatchUpdateException bue = (BatchUpdateException) e.getCause();
            int[] batchResults = bue.getUpdateCounts();
            for (int j = 0; j < batchResults.length; j++) {
                result[batchStart + j] = batchResults[j];
            }
            size = batchStart + batchResults.length;
        } else {
            size = batchStart;
        }
        // resize the result and throw exception
        throw new TranslatorBatchException(e, Arrays.copyOf(result, size));
    } catch (SQLException e) {
        if (batchedCommand.isSingleResult()) {
            throw new JDBCExecutionException(JDBCPlugin.Event.TEIID11011, e, tCommand);
        }
        // resize the result and throw exception
        throw new TranslatorBatchException(e, Arrays.copyOf(result, batchStart));
    } finally {
        if (commitType) {
            restoreAutoCommit(!succeeded, null);
        }
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) Command(org.teiid.language.Command) BulkCommand(org.teiid.language.BulkCommand) TranslatorException(org.teiid.translator.TranslatorException) BatchUpdateException(java.sql.BatchUpdateException)

Example 55 with TranslatorException

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

the class TestEmbeddedServer method testImportExcept.

@Ignore("limit to/exclude not yet implemented")
@Test
public void testImportExcept() throws Exception {
    es.start(new EmbeddedConfiguration());
    es.addMetadataRepository("x", new MetadataRepository() {

        @Override
        public void loadMetadata(MetadataFactory factory, ExecutionFactory executionFactory, Object connectionFactory, String text) throws TranslatorException {
            assertEquals("helloworld1,other", factory.getModelProperties().get("importer.excludeTables"));
            Table t = factory.addTable("helloworld");
            t.setVirtual(true);
            factory.addColumn("col", "string", t);
            t.setSelectTransformation("select 'HELLO WORLD'");
        }
    });
    String externalDDL = "CREATE DATABASE test VERSION '1';" + "USE DATABASE test VERSION '1';" + "CREATE VIRTUAL SCHEMA test2;" + "IMPORT FOREIGN SCHEMA public except (helloworld1, other) FROM REPOSITORY x INTO test2;";
    es.deployVDB(new ByteArrayInputStream(externalDDL.getBytes(Charset.forName("UTF-8"))), true);
    ResultSet rs = es.getDriver().connect("jdbc:teiid:test", null).createStatement().executeQuery("select * from helloworld");
    rs.next();
    assertEquals("HELLO WORLD", rs.getString(1));
}
Also used : MetadataRepository(org.teiid.metadata.MetadataRepository) Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionFactory(org.teiid.translator.ExecutionFactory) TranslatorException(org.teiid.translator.TranslatorException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TranslatorException (org.teiid.translator.TranslatorException)227 ArrayList (java.util.ArrayList)51 Column (org.teiid.metadata.Column)47 List (java.util.List)32 Table (org.teiid.metadata.Table)30 IOException (java.io.IOException)26 SQLException (java.sql.SQLException)26 ResourceException (javax.resource.ResourceException)26 Test (org.junit.Test)16 Expression (org.teiid.language.Expression)16 Literal (org.teiid.language.Literal)16 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 Blob (java.sql.Blob)15 Argument (org.teiid.language.Argument)13 DBObject (com.mongodb.DBObject)11 HashMap (java.util.HashMap)11 ColumnReference (org.teiid.language.ColumnReference)11 ExecutionContext (org.teiid.translator.ExecutionContext)11 BasicDBObject (com.mongodb.BasicDBObject)10 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10