Search in sources :

Example 16 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project atlasdb by palantir.

the class InDbTimestampBoundStore method readLimit.

private Long readLimit(Connection connection) throws SQLException {
    String sql = "SELECT last_allocated FROM " + prefixedTimestampTableName() + " FOR UPDATE";
    QueryRunner run = new QueryRunner();
    return run.query(connection, sql, rs -> {
        if (rs.next()) {
            return rs.getLong("last_allocated");
        } else {
            return null;
        }
    });
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 17 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project atlasdb by palantir.

the class InDbTimestampBoundStore method createLimit.

private void createLimit(Connection connection, long limit) throws SQLException {
    QueryRunner run = new QueryRunner();
    run.update(connection, String.format("INSERT INTO %s (last_allocated) VALUES (?)", prefixedTimestampTableName()), limit);
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 18 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project tutorials by eugenp.

the class EmployeeHandler method handle.

@Override
public List<Employee> handle(ResultSet rs) throws SQLException {
    List<Employee> employees = super.handle(rs);
    QueryRunner runner = new QueryRunner();
    BeanListHandler<Email> handler = new BeanListHandler<>(Email.class);
    String query = "SELECT * FROM email WHERE employeeid = ?";
    for (Employee employee : employees) {
        List<Email> emails = runner.query(connection, query, handler, employee.getId());
        employee.setEmails(emails);
    }
    return employees;
}
Also used : BeanListHandler(org.apache.commons.dbutils.handlers.BeanListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 19 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project tutorials by eugenp.

the class DbUtilsUnitTest method givenHandler_whenInserting_thenExpectedId.

@Test
public void givenHandler_whenInserting_thenExpectedId() throws SQLException {
    ScalarHandler<Integer> scalarHandler = new ScalarHandler<>();
    QueryRunner runner = new QueryRunner();
    String insertSQL = "INSERT INTO employee (firstname,lastname,salary, hireddate) VALUES (?, ?, ?, ?)";
    int newId = runner.insert(connection, insertSQL, scalarHandler, "Jenny", "Medici", 60000.60, new Date());
    assertEquals(newId, 6);
}
Also used : ScalarHandler(org.apache.commons.dbutils.handlers.ScalarHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Date(java.util.Date) Test(org.junit.Test)

Example 20 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project tutorials by eugenp.

the class DbUtilsUnitTest method givenResultHandler_whenExecutingQuery_thenAllPropertiesSetted.

@Test
public void givenResultHandler_whenExecutingQuery_thenAllPropertiesSetted() throws SQLException {
    EmployeeHandler employeeHandler = new EmployeeHandler(connection);
    QueryRunner runner = new QueryRunner();
    String query = "SELECT * FROM employee_legacy";
    List<Employee> employees = runner.query(connection, query, employeeHandler);
    assertEquals((int) employees.get(0).getId(), 1);
    assertEquals(employees.get(0).getFirstName(), "John");
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner) AsyncQueryRunner(org.apache.commons.dbutils.AsyncQueryRunner) Test(org.junit.Test)

Aggregations

QueryRunner (org.apache.commons.dbutils.QueryRunner)48 SQLException (java.sql.SQLException)26 Connection (java.sql.Connection)25 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)17 DataSource (javax.sql.DataSource)15 QualifiedName (com.netflix.metacat.common.QualifiedName)14 List (java.util.List)13 Map (java.util.Map)13 ResultSetHandler (org.apache.commons.dbutils.ResultSetHandler)13 Maps (com.google.common.collect.Maps)12 DataSourceManager (com.netflix.metacat.common.server.util.DataSourceManager)12 Date (java.util.Date)12 Slf4j (lombok.extern.slf4j.Slf4j)12 Joiner (com.google.common.base.Joiner)11 Lists (com.google.common.collect.Lists)11 Collectors (java.util.stream.Collectors)11 Nonnull (javax.annotation.Nonnull)11 Strings (com.google.common.base.Strings)10 Nullable (javax.annotation.Nullable)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7