Search in sources :

Example 66 with DSLContext

use of org.jooq.DSLContext in project keywhiz by square.

the class MigrationsRule method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            File yamlFile = new File(Resources.getResource("keywhiz-test.yaml").getFile());
            Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            ObjectMapper objectMapper = KeywhizService.customizeObjectMapper(Jackson.newObjectMapper());
            KeywhizConfig config = new ConfigurationFactory<>(KeywhizConfig.class, validator, objectMapper, "dw").build(yamlFile);
            DataSource dataSource = config.getDataSourceFactory().build(new MetricRegistry(), "db-migrations");
            Flyway flyway = new Flyway();
            flyway.setDataSource(dataSource);
            flyway.setLocations(config.getMigrationsDir());
            flyway.clean();
            flyway.migrate();
            DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
            DbSeedCommand.doImport(dslContext);
            base.evaluate();
        }
    };
}
Also used : Flyway(org.flywaydb.core.Flyway) Statement(org.junit.runners.model.Statement) MetricRegistry(com.codahale.metrics.MetricRegistry) DSLContext(org.jooq.DSLContext) File(java.io.File) Validator(javax.validation.Validator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(javax.sql.DataSource)

Example 67 with DSLContext

use of org.jooq.DSLContext in project zipkin by openzipkin.

the class HasIpv6 method test.

static boolean test(DataSource datasource, DSLContexts context) {
    try (Connection conn = datasource.getConnection()) {
        DSLContext dsl = context.get(conn);
        dsl.select(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV6).from(ZIPKIN_ANNOTATIONS).limit(1).fetchAny();
        return true;
    } catch (DataAccessException e) {
        if (e.sqlState().equals("42S22")) {
            LOG.warning("zipkin_annotations.ipv6 doesn't exist, so Endpoint.ipv6 is not supported. " + "Execute: alter table zipkin_annotations add `endpoint_ipv6` BINARY(16)");
            return false;
        }
        problemReading(e);
    } catch (SQLException | RuntimeException e) {
        problemReading(e);
    }
    return false;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) DataAccessException(org.jooq.exception.DataAccessException)

Example 68 with DSLContext

use of org.jooq.DSLContext in project zipkin by openzipkin.

the class HasTraceIdHigh method test.

static boolean test(DataSource datasource, DSLContexts context) {
    try (Connection conn = datasource.getConnection()) {
        DSLContext dsl = context.get(conn);
        dsl.select(ZIPKIN_SPANS.TRACE_ID_HIGH).from(ZIPKIN_SPANS).limit(1).fetchAny();
        return true;
    } catch (DataAccessException e) {
        if (e.sqlState().equals("42S22")) {
            LOG.warning(MESSAGE);
            return false;
        }
        problemReading(e);
    } catch (SQLException | RuntimeException e) {
        problemReading(e);
    }
    return false;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) DataAccessException(org.jooq.exception.DataAccessException)

Example 69 with DSLContext

use of org.jooq.DSLContext in project zipkin by openzipkin.

the class MySQLSpanConsumer method accept.

/** Blocking version of {@link AsyncSpanConsumer#accept} */
@Override
public void accept(List<Span> spans) {
    if (spans.isEmpty())
        return;
    try (Connection conn = datasource.getConnection()) {
        DSLContext create = context.get(conn);
        List<Query> inserts = new ArrayList<>();
        for (Span span : spans) {
            Long overridingTimestamp = authoritativeTimestamp(span);
            Long timestamp = overridingTimestamp != null ? overridingTimestamp : guessTimestamp(span);
            Map<TableField<Record, ?>, Object> updateFields = new LinkedHashMap<>();
            if (!span.name.equals("") && !span.name.equals("unknown")) {
                updateFields.put(ZIPKIN_SPANS.NAME, span.name);
            }
            // replace any tentative timestamp with the authoritative one.
            if (overridingTimestamp != null) {
                updateFields.put(ZIPKIN_SPANS.START_TS, overridingTimestamp);
            }
            if (span.duration != null) {
                updateFields.put(ZIPKIN_SPANS.DURATION, span.duration);
            }
            InsertSetMoreStep<Record> insertSpan = create.insertInto(ZIPKIN_SPANS).set(ZIPKIN_SPANS.TRACE_ID, span.traceId).set(ZIPKIN_SPANS.ID, span.id).set(ZIPKIN_SPANS.PARENT_ID, span.parentId).set(ZIPKIN_SPANS.NAME, span.name).set(ZIPKIN_SPANS.DEBUG, span.debug).set(ZIPKIN_SPANS.START_TS, timestamp).set(ZIPKIN_SPANS.DURATION, span.duration);
            if (span.traceIdHigh != 0 && schema.hasTraceIdHigh) {
                insertSpan.set(ZIPKIN_SPANS.TRACE_ID_HIGH, span.traceIdHigh);
            }
            inserts.add(updateFields.isEmpty() ? insertSpan.onDuplicateKeyIgnore() : insertSpan.onDuplicateKeyUpdate().set(updateFields));
            for (Annotation annotation : span.annotations) {
                InsertSetMoreStep<Record> insert = create.insertInto(ZIPKIN_ANNOTATIONS).set(ZIPKIN_ANNOTATIONS.TRACE_ID, span.traceId).set(ZIPKIN_ANNOTATIONS.SPAN_ID, span.id).set(ZIPKIN_ANNOTATIONS.A_KEY, annotation.value).set(ZIPKIN_ANNOTATIONS.A_TYPE, -1).set(ZIPKIN_ANNOTATIONS.A_TIMESTAMP, annotation.timestamp);
                if (span.traceIdHigh != 0 && schema.hasTraceIdHigh) {
                    insert.set(ZIPKIN_ANNOTATIONS.TRACE_ID_HIGH, span.traceIdHigh);
                }
                if (annotation.endpoint != null) {
                    insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME, annotation.endpoint.serviceName);
                    insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV4, annotation.endpoint.ipv4);
                    if (annotation.endpoint.ipv6 != null && schema.hasIpv6) {
                        insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV6, annotation.endpoint.ipv6);
                    }
                    insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_PORT, annotation.endpoint.port);
                }
                inserts.add(insert.onDuplicateKeyIgnore());
            }
            for (BinaryAnnotation annotation : span.binaryAnnotations) {
                InsertSetMoreStep<Record> insert = create.insertInto(ZIPKIN_ANNOTATIONS).set(ZIPKIN_ANNOTATIONS.TRACE_ID, span.traceId).set(ZIPKIN_ANNOTATIONS.SPAN_ID, span.id).set(ZIPKIN_ANNOTATIONS.A_KEY, annotation.key).set(ZIPKIN_ANNOTATIONS.A_VALUE, annotation.value).set(ZIPKIN_ANNOTATIONS.A_TYPE, annotation.type.value).set(ZIPKIN_ANNOTATIONS.A_TIMESTAMP, timestamp);
                if (span.traceIdHigh != 0 && schema.hasTraceIdHigh) {
                    insert.set(ZIPKIN_ANNOTATIONS.TRACE_ID_HIGH, span.traceIdHigh);
                }
                if (annotation.endpoint != null) {
                    insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME, annotation.endpoint.serviceName);
                    insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV4, annotation.endpoint.ipv4);
                    if (annotation.endpoint.ipv6 != null && schema.hasIpv6) {
                        insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV6, annotation.endpoint.ipv6);
                    }
                    insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_PORT, annotation.endpoint.port);
                }
                inserts.add(insert.onDuplicateKeyIgnore());
            }
        }
        create.batch(inserts).execute();
    } catch (SQLException e) {
        // TODO
        throw new RuntimeException(e);
    }
}
Also used : Query(org.jooq.Query) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DSLContext(org.jooq.DSLContext) TableField(org.jooq.TableField) Span(zipkin.Span) Annotation(zipkin.Annotation) BinaryAnnotation(zipkin.BinaryAnnotation) LinkedHashMap(java.util.LinkedHashMap) BinaryAnnotation(zipkin.BinaryAnnotation) Record(org.jooq.Record)

Example 70 with DSLContext

use of org.jooq.DSLContext in project spring-boot by spring-projects.

the class JooqAutoConfigurationTests method customProvidersArePickedUp.

@Test
public void customProvidersArePickedUp() {
    registerAndRefresh(JooqDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class, TxManagerConfiguration.class, TestRecordMapperProvider.class, TestRecordListenerProvider.class, TestExecuteListenerProvider.class, TestVisitListenerProvider.class, JooqAutoConfiguration.class);
    DSLContext dsl = this.context.getBean(DSLContext.class);
    assertThat(dsl.configuration().recordMapperProvider().getClass()).isEqualTo(TestRecordMapperProvider.class);
    assertThat(dsl.configuration().recordListenerProviders().length).isEqualTo(1);
    assertThat(dsl.configuration().executeListenerProviders().length).isEqualTo(2);
    assertThat(dsl.configuration().visitListenerProviders().length).isEqualTo(1);
}
Also used : DSLContext(org.jooq.DSLContext) Test(org.junit.Test)

Aggregations

DSLContext (org.jooq.DSLContext)109 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)55 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)17 List (java.util.List)17 DIConfiguration (com.khartec.waltz.service.DIConfiguration)14 Collectors (java.util.stream.Collectors)14 EntityKind (com.khartec.waltz.model.EntityKind)11 ArrayList (java.util.ArrayList)9 DSL (org.jooq.impl.DSL)9 EntityReference (com.khartec.waltz.model.EntityReference)8 Timestamp (java.sql.Timestamp)8 Random (java.util.Random)8 IntStream (java.util.stream.IntStream)8 Application (com.khartec.waltz.model.application.Application)7 LogicalFlowDao (com.khartec.waltz.data.logical_flow.LogicalFlowDao)6 OrganisationalUnit (com.khartec.waltz.model.orgunit.OrganisationalUnit)6 Field (org.jooq.Field)6 Record1 (org.jooq.Record1)6 Test (org.junit.Test)6