Search in sources :

Example 26 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project notes by KevinBlandy.

the class UtilsDemo method find.

public static void find() throws SQLException {
    // ����QueryRunner���󴫵����ӳض���
    QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
    // ����SQLģ��
    String sql = "select * from users where userName=?";
    // ��������
    Object[] params = { "Kevin" };
    // ����SQLģ��Ͳ���,ִ�в�ѯ��䷵�ض���/���߼��ϸ��ݵڶ�����������
    User user = qr.query(sql, new BeanHandler(User.class), params);
}
Also used : BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 27 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project testcontainers-java by testcontainers.

the class JDBCDriverTest method performSimpleTestWithCharacterSet.

private void performSimpleTestWithCharacterSet(String jdbcUrl) throws SQLException {
    try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
        boolean result = new QueryRunner(dataSource).query("SHOW VARIABLES LIKE 'character\\_set\\_connection'", rs -> {
            rs.next();
            String resultSetInt = rs.getString(2);
            assertEquals("Passing query parameters to set DB connection encoding is successful", "utf8", resultSetInt);
            return true;
        });
        assertTrue("The database returned a record as expected", result);
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 28 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project testcontainers-java by testcontainers.

the class JDBCDriverTest method performSimpleTest.

private void performSimpleTest(String jdbcUrl) throws SQLException {
    try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
        boolean result = new QueryRunner(dataSource).query("SELECT 1", rs -> {
            rs.next();
            int resultSetInt = rs.getInt(1);
            assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
            return true;
        });
        assertTrue("The database returned a record as expected", result);
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 29 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.

the class HiveConnectorFastPartitionService method getsubhandlerresults.

private <T> List<T> getsubhandlerresults(final String databaseName, final String tableName, final String filterExpression, final List<String> partitionIds, final String sql, final ResultSetHandler<List<T>> resultSetHandler, final String joinSql, final String filterSql, final List<Object> filterParams, final Sort sort, final Pageable pageable) {
    // Get data source
    final DataSource dataSource = DataSourceManager.get().get(catalogName);
    // Create the sql
    final StringBuilder queryBuilder = new StringBuilder(sql);
    if (joinSql != null) {
        queryBuilder.append(joinSql);
    }
    queryBuilder.append(" where d.NAME = ? and t.TBL_NAME = ?");
    if (filterSql != null) {
        queryBuilder.append(filterSql);
    }
    if (partitionIds != null && !partitionIds.isEmpty()) {
        queryBuilder.append(" and p.PART_NAME in ('").append(Joiner.on("','").skipNulls().join(partitionIds)).append("')");
    }
    if (sort != null && sort.hasSort()) {
        queryBuilder.append(" order by ").append(sort.getSortBy()).append(" ").append(sort.getOrder().name());
    }
    if (pageable != null && pageable.isPageable() && Strings.isNullOrEmpty(filterExpression)) {
        if (sort == null || !sort.hasSort()) {
            queryBuilder.append(" order by p.part_id");
        }
        queryBuilder.append(" limit ").append(pageable.getOffset()).append(',').append(pageable.getLimit());
    }
    List<T> partitions = Lists.newArrayList();
    try (Connection conn = dataSource.getConnection()) {
        final List<Object> params = Lists.newArrayList(databaseName, tableName);
        if (filterSql != null) {
            params.addAll(filterParams);
        }
        final Object[] oParams = new Object[params.size()];
        partitions = new QueryRunner().query(conn, queryBuilder.toString(), resultSetHandler, params.toArray(oParams));
    } catch (SQLException e) {
        Throwables.propagate(e);
    }
    //
    if (pageable != null && pageable.isPageable() && !Strings.isNullOrEmpty(filterExpression)) {
        int limit = pageable.getOffset() + pageable.getLimit();
        if (partitions.size() < limit) {
            limit = partitions.size();
        }
        if (pageable.getOffset() > limit) {
            partitions = Lists.newArrayList();
        } else {
            partitions = partitions.subList(pageable.getOffset(), limit);
        }
    }
    return partitions;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner) DataSource(javax.sql.DataSource)

Example 30 with QueryRunner

use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.

the class HiveConnectorFastPartitionService method getparameters.

private Map<Long, Map<String, String>> getparameters(final List<Long> ids, final String sql, final String idName) {
    // Get data source
    final DataSource dataSource = DataSourceManager.get().get(catalogName);
    // Create the sql
    final StringBuilder queryBuilder = new StringBuilder(sql);
    if (!ids.isEmpty()) {
        queryBuilder.append(" and ").append(idName).append(" in ('").append(Joiner.on("','").skipNulls().join(ids)).append("')");
    }
    final ResultSetHandler<Map<Long, Map<String, String>>> handler = rs -> {
        final Map<Long, Map<String, String>> result = Maps.newHashMap();
        while (rs.next()) {
            final Long id = rs.getLong(idName);
            final String key = rs.getString("param_key");
            final String value = rs.getString("param_value");
            Map<String, String> parameters = result.get(id);
            if (parameters == null) {
                parameters = Maps.newHashMap();
                result.put(id, parameters);
            }
            parameters.put(key, value);
        }
        return result;
    };
    try (Connection conn = dataSource.getConnection()) {
        return new QueryRunner().query(conn, queryBuilder.toString(), handler);
    } catch (SQLException e) {
        Throwables.propagate(e);
    }
    return Maps.newHashMap();
}
Also used : Connection(java.sql.Connection) PartitionKeyParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionKeyParserEval) Date(java.util.Date) PartitionFilterGenerator(com.netflix.metacat.connector.hive.util.PartitionFilterGenerator) PartitionParamParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionParamParserEval) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) PartitionInfo(com.netflix.metacat.common.server.connectors.model.PartitionInfo) Map(java.util.Map) ConnectorContext(com.netflix.metacat.common.server.connectors.ConnectorContext) StorageInfo(com.netflix.metacat.common.server.connectors.model.StorageInfo) QueryRunner(org.apache.commons.dbutils.QueryRunner) NonNull(lombok.NonNull) Collection(java.util.Collection) Pageable(com.netflix.metacat.common.dto.Pageable) QualifiedName(com.netflix.metacat.common.QualifiedName) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) HiveMetrics(com.netflix.metacat.connector.hive.monitoring.HiveMetrics) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) Joiner(com.google.common.base.Joiner) Sort(com.netflix.metacat.common.dto.Sort) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AuditInfo(com.netflix.metacat.common.server.connectors.model.AuditInfo) HashMap(java.util.HashMap) Id(com.netflix.spectator.api.Id) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ThreadServiceManager(com.netflix.metacat.common.server.util.ThreadServiceManager) DataSource(javax.sql.DataSource) PartitionParser(com.netflix.metacat.common.server.partition.parser.PartitionParser) Named(javax.inject.Named) HiveConnectorInfoConverter(com.netflix.metacat.connector.hive.converters.HiveConnectorInfoConverter) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PartitionDetail(com.netflix.metacat.connector.hive.util.PartitionDetail) Functions(com.google.common.base.Functions) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) Throwables(com.google.common.base.Throwables) Maps(com.google.common.collect.Maps) FilterPartition(com.netflix.metacat.common.server.partition.util.FilterPartition) TimeUnit(java.util.concurrent.TimeUnit) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) Futures(com.google.common.util.concurrent.Futures) StringReader(java.io.StringReader) Registry(com.netflix.spectator.api.Registry) PartitionListRequest(com.netflix.metacat.common.server.connectors.model.PartitionListRequest) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Map(java.util.Map) HashMap(java.util.HashMap) QueryRunner(org.apache.commons.dbutils.QueryRunner) DataSource(javax.sql.DataSource)

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