Search in sources :

Example 1 with PreparedStatementImpl

use of org.teiid.jdbc.PreparedStatementImpl in project teiid by teiid.

the class LocalClient method executeUpdate.

@Override
public UpdateResponse executeUpdate(Command query, List<SQLParameter> parameters) throws SQLException {
    String sql = query.toString();
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql);
    final PreparedStatementImpl stmt = getConnection().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).getValue(), parameters.get(i).getSqlType());
        }
    }
    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;
        }
    };
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl) CacheHint(org.teiid.query.sql.lang.CacheHint)

Example 2 with PreparedStatementImpl

use of org.teiid.jdbc.PreparedStatementImpl in project teiid by teiid.

the class ODBCServerRemoteImpl method sendPortalResults.

private void sendPortalResults(final int maxRows, final Portal query) {
    if (query.rs != null) {
        // this is a suspended portal
        sendCursorResults(query, maxRows);
        return;
    }
    final PreparedStatementImpl stmt = query.stmt;
    try {
        this.executionFuture = stmt.submitExecute(ResultsMode.EITHER, null);
        executionFuture.addCompletionListener(new ResultsFuture.CompletionListener<Boolean>() {

            @Override
            public void onCompletion(ResultsFuture<Boolean> future) {
                executionFuture = null;
                try {
                    if (future.get()) {
                        query.rs = stmt.getResultSet();
                        sendCursorResults(query, maxRows);
                    } else {
                        client.sendUpdateCount(query.prepared.sql, stmt.getUpdateCount());
                        updateSessionProperties();
                        doneExecuting();
                    }
                } catch (ExecutionException e) {
                    if (e.getCause() != null) {
                        errorOccurred(e.getCause());
                    } else {
                        errorOccurred(e);
                    }
                } catch (Throwable e) {
                    errorOccurred(e);
                }
            }
        });
    } catch (SQLException e) {
        errorOccurred(e);
    }
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with PreparedStatementImpl

use of org.teiid.jdbc.PreparedStatementImpl in project teiid by teiid.

the class ODBCServerRemoteImpl method prepare.

@Override
public void prepare(String prepareName, String sql, int[] paramType) {
    if (prepareName == null || prepareName.length() == 0) {
        prepareName = UNNAMED;
    }
    if (sql != null) {
        PreparedStatementImpl stmt = null;
        try {
            // stmt is alive until session ends.
            if (prepareName.equals(UNNAMED)) {
                this.preparedMap.remove(prepareName);
            } else {
                Prepared previous = this.preparedMap.get(prepareName);
                if (previous != null) {
                    errorOccurred(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40110, prepareName));
                    return;
                }
            }
            // just pull the initial information - leave statement formation until binding
            String modfiedSQL = fixSQL(sql);
            stmt = this.connection.prepareStatement(modfiedSQL);
            Prepared prepared = new Prepared(prepareName, sql, modfiedSQL, paramType, getPgColInfo(stmt.getMetaData()));
            this.preparedMap.put(prepareName, prepared);
            this.client.prepareCompleted(prepareName);
        } catch (SQLException e) {
            if (e.getCause() instanceof TeiidProcessingException) {
                LogManager.logWarning(LogConstants.CTX_ODBC, e.getCause(), RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40020));
            }
            errorOccurred(e);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
    }
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 4 with PreparedStatementImpl

use of org.teiid.jdbc.PreparedStatementImpl in project teiid by teiid.

the class ODBCServerRemoteImpl method cursorExecute.

private void cursorExecute(String cursorName, final String sql, final ResultsFuture<Integer> completion, boolean scroll, final boolean binary) {
    try {
        // close if the name is already used or the unnamed prepare; otherwise
        // stmt is alive until session ends.
        this.preparedMap.remove(UNNAMED);
        Portal p = this.portalMap.remove(UNNAMED);
        if (p != null) {
            closePortal(p);
        }
        if (cursorName == null || cursorName.length() == 0) {
            cursorName = UNNAMED;
        }
        Cursor cursor = cursorMap.get(cursorName);
        if (cursor != null) {
            errorOccurred(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40111, cursorName));
            return;
        }
        final PreparedStatementImpl stmt = this.connection.prepareStatement(sql, scroll ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        this.executionFuture = stmt.submitExecute(ResultsMode.RESULTSET, null);
        final String name = cursorName;
        this.executionFuture.addCompletionListener(new ResultsFuture.CompletionListener<Boolean>() {

            @Override
            public void onCompletion(ResultsFuture<Boolean> future) {
                executionFuture = null;
                try {
                    if (future.get()) {
                        List<PgColInfo> cols = getPgColInfo(stmt.getResultSet().getMetaData());
                        cursorMap.put(name, new Cursor(name, sql, stmt, stmt.getResultSet(), cols, binary));
                        // $NON-NLS-1$
                        client.sendCommandComplete("DECLARE CURSOR", null);
                        completion.getResultsReceiver().receiveResults(0);
                    }
                } catch (Throwable e) {
                    completion.getResultsReceiver().exceptionOccurred(e);
                }
            }
        });
    } catch (SQLException e) {
        completion.getResultsReceiver().exceptionOccurred(e);
    }
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with PreparedStatementImpl

use of org.teiid.jdbc.PreparedStatementImpl in project teiid by teiid.

the class ODBCServerRemoteImpl method bindParameters.

@Override
public void bindParameters(String bindName, String prepareName, Object[] params, int resultCodeCount, short[] resultColumnFormat, Charset encoding) {
    // the next Bind statement specifying the unnamed portal as destination is issued.
    if (bindName == null || bindName.length() == 0) {
        Portal p = this.portalMap.remove(UNNAMED);
        if (p != null) {
            closePortal(p);
        }
        bindName = UNNAMED;
    } else if (this.portalMap.get(bindName) != null || this.cursorMap.get(bindName) != null) {
        errorOccurred(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40111, bindName));
        return;
    }
    if (prepareName == null || prepareName.length() == 0) {
        prepareName = UNNAMED;
    }
    Prepared prepared = this.preparedMap.get(prepareName);
    if (prepared == null) {
        errorOccurred(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40077, prepareName));
        return;
    }
    PreparedStatementImpl stmt = null;
    try {
        stmt = this.connection.prepareStatement(prepared.modifiedSql);
        for (int i = 0; i < params.length; i++) {
            Object param = params[i];
            if (param instanceof byte[] && prepared.paramType.length > i) {
                int oid = prepared.paramType[i];
                switch(oid) {
                    case PGUtil.PG_TYPE_UNSPECIFIED:
                        // TODO: should infer type from the parameter metadata from the parse message
                        break;
                    case PGUtil.PG_TYPE_BYTEA:
                        break;
                    case PGUtil.PG_TYPE_INT2:
                        param = (short) readLong((byte[]) param, 2);
                        break;
                    case PGUtil.PG_TYPE_INT4:
                        param = (int) readLong((byte[]) param, 4);
                        break;
                    case PGUtil.PG_TYPE_INT8:
                        param = readLong((byte[]) param, 8);
                        break;
                    case PGUtil.PG_TYPE_FLOAT4:
                        param = Float.intBitsToFloat((int) readLong((byte[]) param, 4));
                        break;
                    case PGUtil.PG_TYPE_FLOAT8:
                        param = Double.longBitsToDouble(readLong((byte[]) param, 8));
                        break;
                    case PGUtil.PG_TYPE_DATE:
                        param = TimestampUtils.toDate(TimestampWithTimezone.getCalendar().getTimeZone(), (int) readLong((byte[]) param, 4));
                        break;
                    default:
                        // start with the string conversion
                        param = new String((byte[]) param, encoding);
                        break;
                }
            }
            stmt.setObject(i + 1, param);
        }
        this.portalMap.put(bindName, new Portal(bindName, prepared, resultColumnFormat, stmt));
        this.client.bindComplete();
        stmt = null;
    } catch (SQLException e) {
        errorOccurred(e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) PreparedStatementImpl(org.teiid.jdbc.PreparedStatementImpl)

Aggregations

PreparedStatementImpl (org.teiid.jdbc.PreparedStatementImpl)6 SQLException (java.sql.SQLException)4 TeiidSQLException (org.teiid.jdbc.TeiidSQLException)4 ArrayList (java.util.ArrayList)2 ResultsFuture (org.teiid.client.util.ResultsFuture)2 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 UpdateResponse (org.teiid.odata.api.UpdateResponse)1 PgColInfo (org.teiid.odbc.PGUtil.PgColInfo)1 CacheHint (org.teiid.query.sql.lang.CacheHint)1