Search in sources :

Example 1 with DBType

use of com.palantir.nexus.db.DBType in project atlasdb by palantir.

the class BasicSQL method selectLightResultSetSpecifyingDBType.

protected AgnosticLightResultSet selectLightResultSetSpecifyingDBType(final Connection c, final FinalSQLString sql, Object[] vs, final DBType dbType, @Nullable Integer fetchSize) throws PalantirSqlException, PalantirInterruptedException {
    if (SqlLoggers.LOGGER.isTraceEnabled()) {
        SqlLoggers.LOGGER.trace("SQL light result set selection query: {}", sql.getQuery());
    }
    // $NON-NLS-1$
    final ResourceCreationLocation alrsCreationException = new ResourceCreationLocation("This is where the AgnosticLightResultSet wrapper was created");
    PreparedStatementVisitor<AgnosticLightResultSet> preparedStatementVisitor = ps -> {
        // $NON-NLS-1$
        final ResourceCreationLocation creationException = new ResourceCreationLocation("This is where the ResultsSet was created", alrsCreationException);
        final ResultSetVisitor<AgnosticLightResultSet> resultSetVisitor = rs -> {
            try {
                return new AgnosticLightResultSetImpl(rs, dbType, rs.getMetaData(), ps, // $NON-NLS-1$
                "selectList", sql, getSqlTimer(), creationException);
            } catch (Exception e) {
                closeSilently(rs);
                BasicSQLUtils.throwUncheckedIfSQLException(e);
                throw Throwables.throwUncheckedException(e);
            }
        };
        try {
            return runCancellably(ps, resultSetVisitor, sql, AutoClose.FALSE, fetchSize);
        } catch (Exception e) {
            closeSilently(ps);
            BasicSQLUtils.throwUncheckedIfSQLException(e);
            throw Throwables.throwUncheckedException(e);
        }
    };
    return wrapPreparedStatement(c, sql, vs, preparedStatementVisitor, // $NON-NLS-1$
    "selectList", // don't close the ps
    AutoClose.FALSE);
}
Also used : PalantirSqlException(com.palantir.exception.PalantirSqlException) FinalSQLString(com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString) Connection(java.sql.Connection) Throwables(com.palantir.common.base.Throwables) ObjectInputStream(java.io.ObjectInputStream) BigDecimal(java.math.BigDecimal) Future(java.util.concurrent.Future) ByteArrayInputStream(java.io.ByteArrayInputStream) PTExecutors(com.palantir.common.concurrent.PTExecutors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(java.sql.ResultSet) Map(java.util.Map) ResultSets(com.palantir.sql.ResultSets) Method(java.lang.reflect.Method) DateTimeFormat(org.joda.time.format.DateTimeFormat) NamedThreadFactory(com.palantir.common.concurrent.NamedThreadFactory) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) JdbcHandler(com.palantir.db.oracle.JdbcHandler) BlobHandler(com.palantir.db.oracle.JdbcHandler.BlobHandler) PreparedStatement(java.sql.PreparedStatement) VerboseSQLException(com.palantir.util.sql.VerboseSQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) ResourceCreationLocation(com.palantir.nexus.db.ResourceCreationLocation) PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException) SqlLoggers(com.palantir.nexus.db.sql.monitoring.logger.SqlLoggers) ResultSetMetaData(java.sql.ResultSetMetaData) Proxy(java.lang.reflect.Proxy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Callable(java.util.concurrent.Callable) ThreadNamingCallable(com.palantir.common.concurrent.ThreadNamingCallable) ArrayList(java.util.ArrayList) Connections(com.palantir.sql.Connections) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) PreparedStatements(com.palantir.sql.PreparedStatements) ObjectOutputStream(java.io.ObjectOutputStream) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) DBType(com.palantir.nexus.db.DBType) Iterator(java.util.Iterator) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Validate(org.apache.commons.lang3.Validate) SqlTimer(com.palantir.nexus.db.monitoring.timer.SqlTimer) Statement(java.sql.Statement) InvocationHandler(java.lang.reflect.InvocationHandler) InputStream(java.io.InputStream) ResourceCreationLocation(com.palantir.nexus.db.ResourceCreationLocation) PalantirSqlException(com.palantir.exception.PalantirSqlException) VerboseSQLException(com.palantir.util.sql.VerboseSQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with DBType

use of com.palantir.nexus.db.DBType in project atlasdb by palantir.

the class SQL method handlePtInputStream.

private BlobHandler handlePtInputStream(Connection c, PreparedStatement ps, int i, PTInputStream is) {
    if (is.getLength() <= ORACLE_BYTE_LOWER_LIMIT) {
        try {
            byte[] bytes = IOUtils.toByteArray(is, is.getLength());
            Preconditions.checkArgument(bytes.length == is.getLength(), // $NON-NLS-1$
            "incorrect length - bytes: %s, input stream: %s", bytes.length, is.getLength());
            PreparedStatements.setBytes(ps, i, bytes);
        } catch (IOException e) {
            // $NON-NLS-1$
            throw Throwables.chain(PalantirSqlException.createForChaining(), Throwables.chain(new SQLException("Internal IOException"), e));
        } finally {
            IOUtils.closeQuietly(is);
        }
        return null;
    } else if (is.getLength() <= Integer.MAX_VALUE) {
        if (DBType.getTypeFromConnection(c) == DBType.POSTGRESQL && is.getLength() > SQL.POSTGRES_BLOB_WRITE_LIMIT) {
            // $NON-NLS-1$
            Validate.isTrue(false, "Postgres only supports blobs up to 1G");
        }
        PreparedStatements.setBinaryStream(ps, i, is, (int) is.getLength());
        return null;
    } else {
        DBType dbType = DBType.getTypeFromConnection(c);
        // $NON-NLS-1$
        Validate.isTrue(dbType == DBType.ORACLE, "We only support blobs over 2GB on oracle (postgres only supports blobs up to 1G)");
        BlobHandler blobHandler;
        try {
            blobHandler = getJdbcHandler().createBlob(c);
        } catch (SQLException e) {
            // $NON-NLS-1$
            sqlExceptionlog.debug("Caught SQLException", e);
            throw PalantirSqlException.create(e);
        }
        OutputStream os = null;
        try {
            os = blobHandler.setBinaryStream(0);
            PTStreams.copy(is, os);
            os.close();
            ps.setBlob(i, blobHandler.getBlob());
        } catch (Exception e) {
            try {
                blobHandler.freeTemporary();
            } catch (Exception e1) {
                // $NON-NLS-1$
                SqlLoggers.LOGGER.error("failed to free temp blob", e1);
            }
            // $NON-NLS-1$
            throw Throwables.chain(PalantirSqlException.createForChaining(), Throwables.chain(new SQLException("failed to transfer blob over 2GB to the DB"), e));
        } finally {
            IOUtils.closeQuietly(os);
        }
        return blobHandler;
    }
}
Also used : SQLException(java.sql.SQLException) OutputStream(java.io.OutputStream) BlobHandler(com.palantir.db.oracle.JdbcHandler.BlobHandler) IOException(java.io.IOException) DBType(com.palantir.nexus.db.DBType) PalantirSqlException(com.palantir.exception.PalantirSqlException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 3 with DBType

use of com.palantir.nexus.db.DBType in project atlasdb by palantir.

the class SQLString method registerQuery.

/**
 * Same as the overloaded registerQuery, but overrides the query for a specific DBType.
 * @param key Unique identifier representing this query
 * @param sql The query that will be stored
 * @param dbTypes Override the query for this list of DBTypes.  These are not allowed to be null.
 */
public static void registerQuery(String key, String sql, DBType... dbTypes) {
    // $NON-NLS-1$
    Validate.notEmpty(dbTypes, "DbType list may not be empty");
    for (DBType type : dbTypes) {
        // $NON-NLS-1$
        Validate.notNull(type, "dbType must not be null");
        registerQuery(key, sql, type);
    }
}
Also used : DBType(com.palantir.nexus.db.DBType)

Aggregations

DBType (com.palantir.nexus.db.DBType)3 BlobHandler (com.palantir.db.oracle.JdbcHandler.BlobHandler)2 PalantirSqlException (com.palantir.exception.PalantirSqlException)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Throwables (com.palantir.common.base.Throwables)1 NamedThreadFactory (com.palantir.common.concurrent.NamedThreadFactory)1 PTExecutors (com.palantir.common.concurrent.PTExecutors)1 ThreadNamingCallable (com.palantir.common.concurrent.ThreadNamingCallable)1 JdbcHandler (com.palantir.db.oracle.JdbcHandler)1 PalantirInterruptedException (com.palantir.exception.PalantirInterruptedException)1 ResourceCreationLocation (com.palantir.nexus.db.ResourceCreationLocation)1 SqlTimer (com.palantir.nexus.db.monitoring.timer.SqlTimer)1 FinalSQLString (com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString)1 SqlLoggers (com.palantir.nexus.db.sql.monitoring.logger.SqlLoggers)1 Connections (com.palantir.sql.Connections)1 PreparedStatements (com.palantir.sql.PreparedStatements)1 ResultSets (com.palantir.sql.ResultSets)1