Search in sources :

Example 11 with SqlConnection

use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.

the class OracleOverflowWriteTable method delete.

@Override
public void delete(List<Entry<Cell, Long>> entries) {
    List<Object[]> args = Lists.newArrayListWithCapacity(entries.size());
    for (Map.Entry<Cell, Long> entry : entries) {
        Cell cell = entry.getKey();
        args.add(new Object[] { cell.getRowName(), cell.getColumnName(), entry.getValue() });
    }
    switch(config.overflowMigrationState()) {
        case UNSTARTED:
            deleteOverflow(config.singleOverflowTable(), args);
            break;
        case IN_PROGRESS:
            deleteOverflow(config.singleOverflowTable(), args);
            deleteOverflow(getShortOverflowTableName(), args);
            break;
        // fall through
        case FINISHING:
        case FINISHED:
            deleteOverflow(getShortOverflowTableName(), args);
            break;
        default:
            throw new EnumConstantNotPresentException(OverflowMigrationState.class, config.overflowMigrationState().name());
    }
    String shortTableName = oraclePrefixedTableNames.get(tableRef, conns);
    SqlConnection conn = conns.get();
    try {
        log.info("Got connection for delete on table {}: {}, autocommit={}", shortTableName, conn.getUnderlyingConnection(), conn.getUnderlyingConnection().getAutoCommit());
    } catch (PalantirSqlException | SQLException e) {
    // 
    }
    conn.updateManyUnregisteredQuery(" /* DELETE_ONE (" + shortTableName + ") */ " + " DELETE /*+ INDEX(m " + PrimaryKeyConstraintNames.get(shortTableName) + ") */ " + " FROM " + shortTableName + " m " + " WHERE m.row_name = ? " + "  AND m.col_name = ? " + "  AND m.ts = ?", args);
}
Also used : SQLException(java.sql.SQLException) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) PalantirSqlException(com.palantir.exception.PalantirSqlException) Map(java.util.Map) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 12 with SqlConnection

use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.

the class TableValueStyleCache method getTableType.

public TableValueStyle getTableType(final ConnectionSupplier connectionSupplier, final TableReference tableRef, TableReference metadataTable) {
    try {
        return valueStyleByTableRef.get(tableRef, () -> {
            SqlConnection conn = connectionSupplier.get();
            AgnosticResultSet results = conn.selectResultSetUnregisteredQuery(String.format("SELECT table_size FROM %s WHERE table_name = ?", metadataTable.getQualifiedName()), tableRef.getQualifiedName());
            Preconditions.checkArgument(!results.rows().isEmpty(), "table %s not found", tableRef.getQualifiedName());
            return TableValueStyle.byId(Iterables.getOnlyElement(results.rows()).getInteger("table_size"));
        });
    } catch (ExecutionException e) {
        log.error("TableValueStyle for the table {} could not be retrieved.", tableRef.getQualifiedName());
        throw Throwables.propagate(e);
    }
}
Also used : AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with SqlConnection

use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.

the class OracleTableNameUnmapper method getShortTableNameFromMappingTable.

@SuppressWarnings("checkstyle:NestedTryDepth")
public String getShortTableNameFromMappingTable(ConnectionSupplier connectionSupplier, String tablePrefix, TableReference tableRef) throws TableMappingNotFoundException {
    String fullTableName = tablePrefix + DbKvs.internalTableName(tableRef);
    try {
        return unmappingCache.get(fullTableName, () -> {
            SqlConnection conn = connectionSupplier.get();
            AgnosticResultSet results = conn.selectResultSetUnregisteredQuery("SELECT short_table_name " + "FROM " + AtlasDbConstants.ORACLE_NAME_MAPPING_TABLE + " WHERE table_name = ?", fullTableName);
            if (results.size() == 0) {
                throw new TableMappingNotFoundException("The table " + fullTableName + " does not have a mapping." + "This might be because the table does not exist.");
            }
            return Iterables.getOnlyElement(results.rows()).getString("short_table_name");
        });
    } catch (ExecutionException e) {
        throw new TableMappingNotFoundException(e.getCause());
    }
}
Also used : AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) TableMappingNotFoundException(com.palantir.atlasdb.keyvalue.impl.TableMappingNotFoundException) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with SqlConnection

use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.

the class ConnectionManagerAwareDbKvs method getSimpleTimedSqlConnectionSupplier.

private static SqlConnectionSupplier getSimpleTimedSqlConnectionSupplier(ReentrantManagedConnectionSupplier connectionSupplier) {
    Supplier<Connection> supplier = () -> connectionSupplier.get();
    SQL sql = new SQL() {

        @Override
        protected SqlConfig getSqlConfig() {
            return new SqlConfig() {

                @Override
                public boolean isSqlCancellationDisabled() {
                    return false;
                }

                protected Iterable<SqlTimer> getSqlTimers() {
                    return ImmutableList.of(SqlTimers.createDurationSqlTimer(), SqlTimers.createSqlStatsSqlTimer());
                }

                @Override
                public SqlTimer getSqlTimer() {
                    return SqlTimers.createCombinedSqlTimer(getSqlTimers());
                }
            };
        }
    };
    return new SqlConnectionSupplier() {

        @Override
        public SqlConnection get() {
            return new ConnectionBackedSqlConnectionImpl(supplier.get(), () -> {
                throw new UnsupportedOperationException("This SQL connection does not provide reliable timestamp.");
            }, new SqlConnectionHelper(sql));
        }

        @Override
        public void close() {
            connectionSupplier.close();
        }
    };
}
Also used : Connection(java.sql.Connection) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) SqlTimer(com.palantir.nexus.db.monitoring.timer.SqlTimer) ConnectionBackedSqlConnectionImpl(com.palantir.nexus.db.sql.ConnectionBackedSqlConnectionImpl) SqlConnectionHelper(com.palantir.nexus.db.sql.SqlConnectionHelper) SQL(com.palantir.nexus.db.sql.SQL)

Example 15 with SqlConnection

use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.

the class DbKvs method runWriteForceAutocommit.

private <T> T runWriteForceAutocommit(TableReference tableRef, Function<DbWriteTable, T> runner) {
    ConnectionSupplier conns = new ConnectionSupplier(connections);
    try {
        SqlConnection conn = conns.get();
        boolean autocommit;
        try {
            autocommit = conn.getUnderlyingConnection().getAutoCommit();
        } catch (PalantirSqlException e1) {
            throw Throwables.rewrapAndThrowUncheckedException(e1);
        } catch (SQLException e1) {
            throw Throwables.rewrapAndThrowUncheckedException(e1);
        }
        if (!autocommit) {
            return runWriteFreshConnection(conns, tableRef, runner);
        } else {
            return runner.apply(dbTables.createWrite(tableRef, conns));
        }
    } finally {
        conns.close();
    }
}
Also used : SQLException(java.sql.SQLException) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) PalantirSqlException(com.palantir.exception.PalantirSqlException)

Aggregations

SqlConnection (com.palantir.nexus.db.sql.SqlConnection)23 Test (org.junit.Test)11 AgnosticResultSet (com.palantir.nexus.db.sql.AgnosticResultSet)6 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 Before (org.junit.Before)3 ConnectionSupplier (com.palantir.atlasdb.keyvalue.dbkvs.impl.ConnectionSupplier)2 OverflowSequenceSupplier (com.palantir.atlasdb.keyvalue.dbkvs.impl.oracle.OverflowSequenceSupplier)2 PalantirSqlException (com.palantir.exception.PalantirSqlException)2 AgnosticResultRow (com.palantir.nexus.db.sql.AgnosticResultRow)2 ExecutionException (java.util.concurrent.ExecutionException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Cell (com.palantir.atlasdb.keyvalue.api.Cell)1 TableMappingNotFoundException (com.palantir.atlasdb.keyvalue.impl.TableMappingNotFoundException)1 SqlTimer (com.palantir.nexus.db.monitoring.timer.SqlTimer)1 AgnosticResultSetImpl (com.palantir.nexus.db.sql.AgnosticResultSetImpl)1 ConnectionBackedSqlConnectionImpl (com.palantir.nexus.db.sql.ConnectionBackedSqlConnectionImpl)1 SQL (com.palantir.nexus.db.sql.SQL)1 SqlConnectionHelper (com.palantir.nexus.db.sql.SqlConnectionHelper)1 ArrayList (java.util.ArrayList)1