Search in sources :

Example 11 with ResultSetHandler

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

the class MysqlUserMetadataService method searchByOwners.

@Override
public List<QualifiedName> searchByOwners(final Set<String> owners) {
    final List<QualifiedName> result = Lists.newArrayList();
    final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATA_NAMES);
    final List<Object> paramList = Lists.newArrayList();
    query.append(" where 1=0");
    owners.forEach(s -> {
        query.append(" or data like ?");
        paramList.add("%\"userId\":\"" + s.trim() + "\"%");
    });
    final Object[] params = new Object[paramList.size()];
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        // Handler for reading the result set
        final ResultSetHandler<Void> handler = rs -> {
            while (rs.next()) {
                final String definitionName = rs.getString("name");
                result.add(QualifiedName.fromString(definitionName, false));
            }
            return null;
        };
        new QueryRunner().query(connection, query.toString(), handler, paramList.toArray(params));
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException("Failed to get definition data", e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) URL(java.net.URL) Date(java.util.Date) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ResultSet(java.sql.ResultSet) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) DataSource(javax.sql.DataSource) Config(com.netflix.metacat.common.server.properties.Config) BaseUserMetadataService(com.netflix.metacat.common.server.usermetadata.BaseUserMetadataService) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) Properties(java.util.Properties) QueryRunner(org.apache.commons.dbutils.QueryRunner) MetacatJson(com.netflix.metacat.common.json.MetacatJson) Files(java.nio.file.Files) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Maps(com.google.common.collect.Maps) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Collectors(java.util.stream.Collectors) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DBUtil(com.netflix.metacat.common.server.util.DBUtil) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) Collections(java.util.Collections) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) FileSystems(java.nio.file.FileSystems) Joiner(com.google.common.base.Joiner) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) QualifiedName(com.netflix.metacat.common.QualifiedName) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 12 with ResultSetHandler

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

the class JDBCDriverWithPoolTest method testMySQLWithConnectionPoolUsingSameContainer.

@Test
public void testMySQLWithConnectionPoolUsingSameContainer() throws SQLException, InterruptedException {
    // Populate the database with some data in multiple threads, so that multiple connections from the pool will be used
    for (int i = 0; i < 100; i++) {
        executorService.submit(() -> {
            try {
                new QueryRunner(dataSource).insert("INSERT INTO my_counter (n) VALUES (5)", (ResultSetHandler<Object>) rs -> true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
    }
    // Complete population of the database
    executorService.shutdown();
    executorService.awaitTermination(5, TimeUnit.MINUTES);
    // compare to expected results
    int count = new QueryRunner(dataSource).query("SELECT COUNT(1) FROM my_counter", rs -> {
        rs.next();
        return rs.getInt(1);
    });
    assertEquals("Reuse of a datasource points to the same DB container", 100, count);
    int sum = new QueryRunner(dataSource).query("SELECT SUM(n) FROM my_counter", rs -> {
        rs.next();
        return rs.getInt(1);
    });
    // 100 records * 5 = 500 expected
    assertEquals("Reuse of a datasource points to the same DB container", 500, sum);
}
Also used : Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner) VisibleAssertions.assertEquals(org.rnorth.visibleassertions.VisibleAssertions.assertEquals) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Supplier(java.util.function.Supplier) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Executors(java.util.concurrent.Executors) ParallelParameterized(com.googlecode.junittoolbox.ParallelParameterized) TimeUnit(java.util.concurrent.TimeUnit) HikariConfig(com.zaxxer.hikari.HikariConfig) SQLException(java.sql.SQLException) Arrays.asList(java.util.Arrays.asList) HikariDataSource(com.zaxxer.hikari.HikariDataSource) ViburDBCPDataSource(org.vibur.dbcp.ViburDBCPDataSource) DataSource(javax.sql.DataSource) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) Parameterized(org.junit.runners.Parameterized) ExecutorService(java.util.concurrent.ExecutorService) SQLException(java.sql.SQLException) QueryRunner(org.apache.commons.dbutils.QueryRunner) Test(org.junit.Test)

Example 13 with ResultSetHandler

use of org.apache.commons.dbutils.ResultSetHandler in project jSqlBox by drinkjava2.

the class ActiveRecordUtils method doGuess.

/**
 * Execute operation to access database, based on current method @Sql annotated
 * String or Text String and parameters, guess a best fit
 * query/update/delete/execute method to run
 *
 * @param ac
 * @param params
 * @return <T> T
 */
@SuppressWarnings("all")
protected static <T> T doGuess(ActiveRecord ac, Object... params) {
    // NOSONAR
    int callerPos = 0;
    StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
    for (StackTraceElement stack : stacks) {
        callerPos++;
        if ("com.github.drinkjava2.jsqlbox.ActiveRecord".equals(stack.getClassName()) && "guess".equals(stack.getMethodName()))
            break;
    }
    String callerClassName = stacks[callerPos].getClassName();
    String callerMethodName = stacks[callerPos].getMethodName();
    Class<?> callerClass = ClassCacheUtils.checkClassExist(callerClassName);
    if (callerClass == null)
        throw new SqlBoxException("Can not find class '" + callerClassName + "'");
    Method callerMethod = ClassCacheUtils.checkMethodExist(callerClass, callerMethodName);
    if (callerMethod == null)
        throw new SqlBoxException("Can not find method '" + callerMethodName + "' in '" + callerClassName + "'");
    PreparedSQL sp = getPreparedSQLNoParamsFromSrcCode(callerClassName, callerMethodName, callerMethod);
    String sql = sp.getSql().trim();
    Class<?>[] handlerClass = sp.getHandlerClasses();
    char dotype;
    if (StrUtils.startsWithIgnoreCase(sql, "select"))
        dotype = 's';
    else if (StrUtils.startsWithIgnoreCase(sql, "delete"))
        dotype = 'u';
    else if (StrUtils.startsWithIgnoreCase(sql, "update"))
        dotype = 'u';
    else if (StrUtils.startsWithIgnoreCase(sql, "insert"))
        dotype = 'u';
    else
        // execute
        dotype = 'e';
    boolean hasQuestionMark = sql.indexOf('?') > -1;
    boolean useTemplate = sql.indexOf(':') > -1 || sql.indexOf("#{") > -1;
    if (useTemplate && hasQuestionMark)
        throw new SqlBoxException("guess() method can not determine use template or normal style for SQL '" + sql + "'");
    boolean isEntityQuery = sql.indexOf(".**") > -1;
    if (isEntityQuery && 's' != dotype)
        throw new SqlBoxException("'.**' entity query style SQL can only start with 'select'");
    Map<String, Object> map = null;
    if (useTemplate)
        map = buildParamMap(callerClassName, callerMethodName, params);
    Object o = null;
    switch(dotype) {
        case 's':
            {
                ResultSetHandler<T> resultSetHandler = buildResultHandler(handlerClass);
                if (isEntityQuery) {
                    if (useTemplate)
                        return (T) ac.ctx().tQuery(new EntityListHandler(ac.getClass()), sql, map);
                    else
                        return (T) ac.ctx().nQuery(new EntityListHandler(ac.getClass()), sql, params);
                } else {
                    if (useTemplate)
                        return ac.ctx().tQuery(resultSetHandler, sql, map);
                    else
                        return ac.ctx().nQuery(resultSetHandler, sql, params);
                }
            }
        case 'u':
            {
                if (useTemplate)
                    o = ac.ctx().tUpdate(sql, map);
                else
                    o = ac.ctx().nUpdate(sql, params);
                return (T) o;
            }
        case 'e':
            {
                if (handlerClass == null) {
                    if (useTemplate)
                        o = ac.ctx().tExecute(sql, map);
                    else
                        o = ac.ctx().nExecute(sql, params);
                    return (T) o;
                }
                ResultSetHandler<T> resultSetHandler = buildResultHandler(handlerClass);
                if (useTemplate)
                    o = ac.ctx().tExecute(resultSetHandler, sql, map);
                else
                    o = ac.ctx().nExecute(resultSetHandler, sql, params);
                return (T) o;
            }
        default:
            return null;
    }
}
Also used : PreparedSQL(com.github.drinkjava2.jdbpro.inline.PreparedSQL) Method(java.lang.reflect.Method) EntityListHandler(com.github.drinkjava2.jsqlbox.handler.EntityListHandler) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler)

Example 14 with ResultSetHandler

use of org.apache.commons.dbutils.ResultSetHandler in project jSqlBox by drinkjava2.

the class ImprovedQueryRunner method readCache.

/**
 * Explain SQL to cached result object, if have
 */
private Object[] readCache(ResultSetHandler<?> rsh, String sql, Object... params) {
    Object[] result = new Object[2];
    String key = null;
    if (handlers != null)
        for (ResultSetHandler handler : handlers) if (handler instanceof CacheSqlHandler) {
            if (key == null)
                key = createKey(sql, params);
            result[0] = key;
            Object value = ((CacheSqlHandler) handler).readFromCache(key);
            if (value != null) {
                result[1] = value;
                return result;
            }
        }
    for (ResultSetHandler handler : getThreadedHandlers()) if (handler instanceof CacheSqlHandler) {
        if (key == null)
            key = createKey(sql, params);
        result[0] = key;
        Object value = ((CacheSqlHandler) handler).readFromCache(key);
        if (value != null) {
            result[1] = value;
            return result;
        }
    }
    if (rsh != null && rsh instanceof CacheSqlHandler) {
        if (key == null)
            key = createKey(sql, params);
        result[0] = key;
        Object value = ((CacheSqlHandler) rsh).readFromCache(key);
        if (value != null)
            result[1] = value;
    }
    return result;
}
Also used : CacheSqlHandler(com.github.drinkjava2.jdbpro.handler.CacheSqlHandler) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler)

Aggregations

ResultSetHandler (org.apache.commons.dbutils.ResultSetHandler)14 Connection (java.sql.Connection)12 SQLException (java.sql.SQLException)11 Lists (com.google.common.collect.Lists)10 Maps (com.google.common.collect.Maps)10 QualifiedName (com.netflix.metacat.common.QualifiedName)10 DataSourceManager (com.netflix.metacat.common.server.util.DataSourceManager)10 List (java.util.List)10 Map (java.util.Map)10 Joiner (com.google.common.base.Joiner)9 Strings (com.google.common.base.Strings)9 Date (java.util.Date)9 DataSource (javax.sql.DataSource)9 QueryRunner (org.apache.commons.dbutils.QueryRunner)9 Nonnull (javax.annotation.Nonnull)8 Slf4j (lombok.extern.slf4j.Slf4j)8 Throwables (com.google.common.base.Throwables)7 ConnectorContext (com.netflix.metacat.common.server.connectors.ConnectorContext)7 ThreadServiceManager (com.netflix.metacat.common.server.util.ThreadServiceManager)7 HiveConnectorInfoConverter (com.netflix.metacat.connector.hive.converters.HiveConnectorInfoConverter)7