Java Code Examples for org.jdbi.v3.core.Jdbi#open()

The following examples show how to use org.jdbi.v3.core.Jdbi#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBIRunner.java    From jdit with MIT License 6 votes vote down vote up
@Override
protected Statement classBlock(RunNotifier notifier) {
    final Statement statement = super.classBlock(notifier);
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            // Open a new handle for every test
            // It affords to avoid creating a static state which makes tests more independent
            JditProperties jditProperties = klass.getAnnotation(JditProperties.class);
            Jdbi dbi = jditProperties != null ? DBIContextFactory.getDBI(jditProperties.value()) : DBIContextFactory.getDBI();
            try (Handle handle = dbi.open()) {
                injector = new TestObjectsInjector(dbi, handle);
                databaseMaintenance = DatabaseMaintenanceFactory.create(handle);
                dataSetInjector = new DataSetInjector(new DataMigration(handle));
                statement.evaluate();
            }
        }
    };
}
 
Example 2
Source File: TestVerifierRewriteQueries.java    From presto with Apache License 2.0 5 votes vote down vote up
public TestVerifierRewriteQueries()
{
    handle = Jdbi.open(URL);
    handle.execute("CREATE TABLE \"test_table\" (a BIGINT, b DOUBLE, c VARCHAR)");
    parser = new SqlParser();

    config = new VerifierConfig();
    config.setTestGateway(URL);
    config.setTestTimeout(new Duration(10, TimeUnit.SECONDS));
    config.setControlTimeout(new Duration(10, TimeUnit.SECONDS));
    config.setShadowTestTablePrefix("tmp_verifier_test_");
    config.setShadowControlTablePrefix("tmp_verifier_control_");

    ImmutableList.Builder<QueryPair> builder = ImmutableList.builder();
    for (String queryString : QUERY_STRINGS) {
        Query query = new Query(
                CATALOG,
                SCHEMA,
                ImmutableList.of(),
                queryString,
                ImmutableList.of(),
                null,
                null,
                ImmutableMap.of());
        builder.add(new QueryPair(QUERY_SUITE, QUERY_NAME, query, query));
    }
    queryPairs = builder.build();
}
 
Example 3
Source File: Jdbi3ITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) {
  Driver.load();
  final Jdbi dbi = Jdbi.create("jdbc:h2:mem:dbi", "sa", "");
  try (final Handle handle = dbi.open()) {
    handle.execute("CREATE TABLE employer (id INTEGER)");
  }

  TestUtil.checkSpan(new ComponentSpanCount("java-jdbc", 2));
}
 
Example 4
Source File: RowMapperFactoryTest.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
@Test
public void testMapToDbObject() throws Exception {
    Jdbi dbi = Jdbi.create(DbHelper.getHsqlDataSource());
    dbi.installPlugins();
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).findFirst().get();
        DbHelper.assertDbObjectMapping(dbObject);
    } finally {
        handle.close();
    }
}
 
Example 5
Source File: TestShadowing.java    From presto with Apache License 2.0 4 votes vote down vote up
public TestShadowing()
{
    handle = Jdbi.open(URL);
}
 
Example 6
Source File: H2QueryRunner.java    From presto with Apache License 2.0 4 votes vote down vote up
public H2QueryRunner()
{
    handle = Jdbi.open("jdbc:h2:mem:test" + System.nanoTime() + ThreadLocalRandom.current().nextLong());
    TpchMetadata tpchMetadata = new TpchMetadata();

    handle.execute("CREATE TABLE orders (\n" +
            "  orderkey BIGINT PRIMARY KEY,\n" +
            "  custkey BIGINT NOT NULL,\n" +
            "  orderstatus CHAR(1) NOT NULL,\n" +
            "  totalprice DOUBLE NOT NULL,\n" +
            "  orderdate DATE NOT NULL,\n" +
            "  orderpriority CHAR(15) NOT NULL,\n" +
            "  clerk CHAR(15) NOT NULL,\n" +
            "  shippriority INTEGER NOT NULL,\n" +
            "  comment VARCHAR(79) NOT NULL\n" +
            ")");
    handle.execute("CREATE INDEX custkey_index ON orders (custkey)");
    insertRows(tpchMetadata, ORDERS);

    handle.execute("CREATE TABLE lineitem (\n" +
            "  orderkey BIGINT,\n" +
            "  partkey BIGINT NOT NULL,\n" +
            "  suppkey BIGINT NOT NULL,\n" +
            "  linenumber INTEGER,\n" +
            "  quantity DOUBLE NOT NULL,\n" +
            "  extendedprice DOUBLE NOT NULL,\n" +
            "  discount DOUBLE NOT NULL,\n" +
            "  tax DOUBLE NOT NULL,\n" +
            "  returnflag CHAR(1) NOT NULL,\n" +
            "  linestatus CHAR(1) NOT NULL,\n" +
            "  shipdate DATE NOT NULL,\n" +
            "  commitdate DATE NOT NULL,\n" +
            "  receiptdate DATE NOT NULL,\n" +
            "  shipinstruct VARCHAR(25) NOT NULL,\n" +
            "  shipmode VARCHAR(10) NOT NULL,\n" +
            "  comment VARCHAR(44) NOT NULL,\n" +
            "  PRIMARY KEY (orderkey, linenumber)" +
            ")");
    insertRows(tpchMetadata, LINE_ITEM);

    handle.execute("CREATE TABLE nation (\n" +
            "  nationkey BIGINT PRIMARY KEY,\n" +
            "  name VARCHAR(25) NOT NULL,\n" +
            "  regionkey BIGINT NOT NULL,\n" +
            "  comment VARCHAR(114) NOT NULL\n" +
            ")");
    insertRows(tpchMetadata, NATION);

    handle.execute("CREATE TABLE region(\n" +
            "  regionkey BIGINT PRIMARY KEY,\n" +
            "  name VARCHAR(25) NOT NULL,\n" +
            "  comment VARCHAR(115) NOT NULL\n" +
            ")");
    insertRows(tpchMetadata, REGION);

    handle.execute("CREATE TABLE part(\n" +
            "  partkey BIGINT PRIMARY KEY,\n" +
            "  name VARCHAR(55) NOT NULL,\n" +
            "  mfgr VARCHAR(25) NOT NULL,\n" +
            "  brand VARCHAR(10) NOT NULL,\n" +
            "  type VARCHAR(25) NOT NULL,\n" +
            "  size INTEGER NOT NULL,\n" +
            "  container VARCHAR(10) NOT NULL,\n" +
            "  retailprice DOUBLE NOT NULL,\n" +
            "  comment VARCHAR(23) NOT NULL\n" +
            ")");
    insertRows(tpchMetadata, PART);

    handle.execute("CREATE TABLE customer (\n" +
            "  custkey BIGINT NOT NULL,\n" +
            "  name VARCHAR(25) NOT NULL,\n" +
            "  address VARCHAR(40) NOT NULL,\n" +
            "  nationkey BIGINT NOT NULL,\n" +
            "  phone VARCHAR(15) NOT NULL,\n" +
            "  acctbal DOUBLE NOT NULL,\n" +
            "  mktsegment VARCHAR(10) NOT NULL,\n" +
            "  comment VARCHAR(117) NOT NULL,\n" +
            "  PRIMARY KEY (custkey)" +
            ")");
    insertRows(tpchMetadata, CUSTOMER);
}
 
Example 7
Source File: DBTeststepRunner.java    From irontest with Apache License 2.0 4 votes vote down vote up
public BasicTeststepRun run() throws Exception {
    Teststep teststep = getTeststep();
    BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
    DBAPIResponse response = new DBAPIResponse();
    String request = (String) teststep.getRequest();

    List<String> statements = IronTestUtils.getStatements(request);
    sanityCheckTheStatements(statements);

    Endpoint endpoint = teststep.getEndpoint();
    Jdbi jdbi;
    if (endpoint.getUsername() == null) {
        jdbi = Jdbi.create(endpoint.getUrl());
    } else {
        jdbi = Jdbi.create(endpoint.getUrl(), endpoint.getUsername(), getDecryptedEndpointPassword());
    }
    Handle handle = jdbi.open();
    if (SQLStatementType.isSelectStatement(statements.get(0))) {    //  the request is a select statement
        RetainingColumnOrderResultSetMapper resultSetMapper = new RetainingColumnOrderResultSetMapper();
        //  use statements.get(0) instead of the raw request, as Oracle does not support trailing semicolon in select statement
        Query query = handle.createQuery(statements.get(0))
                .setMaxRows(5000);          //  limit the number of returned rows
        //  obtain columnNames in case the query returns no row
        final List<String> columnNames = new ArrayList<String>();
        query.addCustomizer(new StatementCustomizer() {
            public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException {
                ResultSetMetaData metaData = stmt.getMetaData();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    columnNames.add(metaData.getColumnLabel(i));
                }
            }
        });

        List<Map<String, Object>> rows = query.map(resultSetMapper).list();
        response.setColumnNames(columnNames);
        response.setRowsJSON(jacksonObjectMapper.writeValueAsString(rows));
    } else {                                          //  the request is one or more non-select statements
        Script script = handle.createScript(request);
        int[] returnValues = script.execute();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < returnValues.length; i++) {
            String statementType = SQLStatementType.getByStatement(statements.get(i)).toString();
            sb.append(returnValues[i]).append(" row(s) ").append(statementType.toLowerCase())
                .append(statementType.endsWith("E") ? "d" : "ed").append("\n");
            response.setStatementExecutionResults(sb.toString());
        }
    }

    handle.close();

    basicTeststepRun.setResponse(response);
    return basicTeststepRun;
}