Search in sources :

Example 26 with Handle

use of jnc.platform.win32.Handle in project providence by morimekta.

the class MessageInserter method execute.

public int execute(Handle handle, Collection<M> items) {
    if (items.isEmpty()) {
        throw new IllegalArgumentException("Nothing to insert");
    }
    String query = queryPrefix + items.stream().map(item -> valueMarkers).collect(Collectors.joining(", ")) + querySuffix;
    Update update = handle.createStatement(query);
    int offset = 0;
    for (M item : items) {
        for (String column : columnOrder) {
            F field = columnToFieldMap.get(column);
            int type = columnTypeMap.get(column);
            update.bind(offset++, new MessageFieldArgument<>(item, field, type));
        }
    }
    return update.execute();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) PMessage(net.morimekta.providence.PMessage) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PField(net.morimekta.providence.descriptor.PField) Update(org.skife.jdbi.v2.Update) List(java.util.List) Handle(org.skife.jdbi.v2.Handle) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Collections(java.util.Collections) MessageFieldArgument.getDefaultColumnType(net.morimekta.providence.jdbi.v2.MessageFieldArgument.getDefaultColumnType) Update(org.skife.jdbi.v2.Update)

Example 27 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class LatestMysqlSchemaStore method getTableDDL.

String getTableDDL(@NotNull final String database, @NotNull final String table) {
    // and handle.createQuery() needs to escape colon(:)
    try (Handle handle = jdbi.open()) {
        Statement statement = handle.getConnection().createStatement();
        statement.execute(String.format(TABLE_DDL_QUERY, MysqlSchemaUtil.escapeBackQuote(database), MysqlSchemaUtil.escapeBackQuote(table)));
        ResultSet resultSet = statement.getResultSet();
        resultSet.first();
        return resultSet.getString(2);
    } catch (SQLException ex) {
        log.error(String.format("Failed to get DDL for database: %s table: %s.", database, table), ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Handle(org.skife.jdbi.v2.Handle)

Example 28 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaDatabase method applyDDLStatement.

void applyDDLStatement(@NotNull final String database, @NotNull final String ddl) {
    log.info(String.format("Applying DDL statement: %s (Database selected: %s)", ddl, database));
    try (Handle handle = jdbi.open()) {
        disableForeignKeyChecks(handle);
        MysqlSchemaUtil.VOID_RETRYER.call(() -> {
            MysqlSchemaUtil.executeSQL(handle, database.isEmpty() ? null : getSchemaDatabaseName(source, database), addSourcePrefix(ddl));
            return null;
        });
        metrics.schemaDatabaseApplyDDLSuccess(database);
    } catch (Exception ex) {
        log.error(String.format("Failed to apply DDL Statement to source: %s database: %s. (SQL: %s. Exception: %s)", source, database, ddl, ex));
        metrics.schemaDatabaseApplyDDLFailure(database, ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Example 29 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaDatabase method fetchTableSchema.

Map<String, MysqlTableSchema> fetchTableSchema(@NotNull final String database) {
    List<ColumnInfo> allColumnInfo;
    try (Handle handle = jdbi.open()) {
        disableForeignKeyChecks(handle);
        allColumnInfo = MysqlSchemaUtil.LIST_COLUMNINFO_RETRYER.call(() -> handle.createQuery(ALL_TABLE_SCHEMA_QUERY).bind("db", getSchemaDatabaseName(source, database)).map(MysqlSchemaUtil.COLUMN_MAPPER).list());
    } catch (Exception ex) {
        log.error(String.format("Failed to fetch schema for database: %s", database), ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
    Map<String, MysqlTableSchema> allTableSchemaMap = Maps.newHashMap();
    allColumnInfo.forEach(columnInfo -> {
        String table = columnInfo.getTable();
        allTableSchemaMap.computeIfAbsent(table, __ -> MysqlSchemaUtil.createTableSchema(source, database, table, "", Lists.newArrayList())).getColumnInfo().add(columnInfo);
    });
    return allTableSchemaMap;
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Example 30 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaStore method get.

@Override
public MysqlTableSchema get(@NotNull final String database, @NotNull final String table, final int version) {
    try (Handle handle = jdbi.open()) {
        String schemaInfo = MysqlSchemaUtil.STRING_RETRYER.call(() -> handle.createQuery(String.format(GET_SCHEMA_BY_VERSION_QUERY, source)).bind("database", database).bind("table", table).bind("version", version).map(StringColumnMapper.INSTANCE).first());
        metrics.schemaStoreGetSuccess(database, table);
        return deserializeSchemaInfo(schemaInfo);
    } catch (Exception ex) {
        log.error(String.format("Failed to get schema of database: %s table: %s version: %d. Does it exist?", database, table, version), ex);
        metrics.schemaStoreGetFailure(database, table, ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Aggregations

Handle (org.skife.jdbi.v2.Handle)103 DBI (org.skife.jdbi.v2.DBI)28 Before (org.junit.Before)21 IOException (java.io.IOException)18 List (java.util.List)17 DataSourceFactory (io.dropwizard.db.DataSourceFactory)15 DBIFactory (io.dropwizard.jdbi.DBIFactory)15 SQLException (java.sql.SQLException)15 Map (java.util.Map)14 Test (org.junit.Test)14 Test (org.testng.annotations.Test)14 DateTime (org.joda.time.DateTime)13 ArrayList (java.util.ArrayList)11 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)11 ResultSet (java.sql.ResultSet)10 ImmutableList (com.google.common.collect.ImmutableList)8 UUID (java.util.UUID)8 CallbackFailedException (org.skife.jdbi.v2.exceptions.CallbackFailedException)7 ImmutableSet (com.google.common.collect.ImmutableSet)6 Set (java.util.Set)6