Example usage for org.apache.commons.dbutils.handlers ColumnListHandler ColumnListHandler

List of usage examples for org.apache.commons.dbutils.handlers ColumnListHandler ColumnListHandler

Introduction

In this page you can find the example usage for org.apache.commons.dbutils.handlers ColumnListHandler ColumnListHandler.

Prototype

public ColumnListHandler(String columnName) 

Source Link

Document

Creates a new instance of ColumnListHandler.

Usage

From source file:com.gs.obevo.db.impl.platforms.sybasease.AseEnvironmentInfraSetup.java

private void setupGroups(Connection conn, PhysicalSchema schema, boolean failOnSetupException) {
    MutableSet<String> existingGroups;
    try {/*  ww  w .  j  ava  2 s .  co  m*/
        existingGroups = ListAdapter.adapt(jdbc.query(conn, schema.getPhysicalName() + "..sp_helpgroup",
                new ColumnListHandler<String>("Group_name"))).toSet();
    } catch (DataAccessException e) {
        if (failOnSetupException) {
            throw e;
        } else {
            LOG.warn("Group validation query failed; continuing w/ deployment per configuration", e);
            deployMetricsCollector.addMetric(DeployMetrics.WARNINGS_PREFIX + ".aseGroupValidationQueryFailure",
                    true);
            return;
        }
    }

    ImmutableList<Group> missingGroups = env.getGroups()
            .select(Predicates.attributeNotIn(Group.TO_NAME, existingGroups));
    MutableList<String> failedGroups = Lists.mutable.empty();
    for (Group group : missingGroups) {
        LOG.info("Group " + group.getName() + " doesn't exist in database " + schema.getPhysicalName()
                + "; creating it now");
        try {
            jdbc.update(conn, schema.getPhysicalName() + "..sp_addgroup " + group.getName());
        } catch (DataAccessException sqlExc) {
            if (failOnSetupException) {
                throw new DeployerRuntimeException("Failed to create group " + group.getName() + " in database "
                        + schema.getPhysicalName() + " during setup; exiting the deploy", sqlExc);
            } else {
                LOG.warn(
                        "Group creation failed for group {} in database {}; continuing w/ deployment per configuration",
                        group, schema, sqlExc);
                failedGroups.add(group.getName());
            }
        }
    }

    if (failedGroups.notEmpty()) {
        deployMetricsCollector.addMetric(DeployMetrics.WARNINGS_PREFIX + ".aseGroupCreationFailure",
                "Failed creating groups " + failedGroups + " in database " + schema);
    }
}

From source file:de.tu_berlin.dima.oligos.db.oracle.OracleColumnConnector.java

@Override
public Set<Constraint> getConstraints() throws SQLException {
    if (constraints == null) {
        constraints = Sets.newHashSet();
        ResultSetHandler<List<String>> handler = new ColumnListHandler<>("TYPE");
        QueryRunner runner = new QueryRunner(true);
        List<String> cons = runner.query(connector.getConnection(), CONSTRAINT_QUERY, handler, schema, table,
                column);//w  w  w.j  a v a2 s. co m

        if (cons.contains("U")) {
            constraints.add(Constraint.UNIQUE);
        } else if (cons.contains("P")) {
            constraints.add(Constraint.PRIMARY_KEY);
        } else if (cons.contains("R")) {
            constraints.add(Constraint.FOREIGN_KEY);
        }

    }
    return constraints;
}

From source file:com.gs.obevo.db.impl.platforms.sybasease.AseEnvironmentInfraSetup.java

private void setupUsers(Connection conn, PhysicalSchema schema, boolean failOnSetupException) {
    MutableSet<String> existingUsers;
    try {/*from   ww  w .  j  av a  2 s .c o m*/
        existingUsers = ListAdapter.adapt(jdbc.query(conn, schema.getPhysicalName() + "..sp_helpuser",
                new ColumnListHandler<String>("Users_name"))).toSet();
    } catch (DataAccessException e) {
        if (failOnSetupException) {
            throw e;
        } else {
            LOG.warn("User validation query failed; continuing w/ deployment per configuration", e);
            deployMetricsCollector.addMetric(DeployMetrics.WARNINGS_PREFIX + ".aseUserValidationQueryFailure",
                    true);
            return;
        }
    }

    ImmutableList<User> missingUsers = env.getUsers()
            .select(Predicates.attributeNotIn(User.TO_NAME, existingUsers));
    if (missingUsers.notEmpty()) {
        String errorMessage = "Specified users " + missingUsers.collect(User.TO_NAME).makeString("[", ",", "]")
                + " do not exist in database " + schema.getPhysicalName()
                + "; please create the users or remove from your configuration (or rely on groups instead for permissions)";
        if (failOnSetupException) {
            throw new IllegalArgumentException(errorMessage);
        } else {
            LOG.warn(errorMessage);
            LOG.warn("Will proceed with deployment as you have configured this to just be a warning");
            deployMetricsCollector.addMetric(DeployMetrics.WARNINGS_PREFIX + ".usersInConfigButNotInDb",
                    errorMessage);
        }
    }
}

From source file:com.netflix.metacat.usermetadata.mysql.MysqlUserMetadataService.java

@SuppressWarnings("checkstyle:methodname")
private Void _softDeleteDataMetadatas(final Connection conn, final String userId, final List<String> uris)
        throws SQLException {
    if (uris != null && !uris.isEmpty()) {
        final List<String> paramVariables = uris.stream().map(s -> "?").collect(Collectors.toList());
        final String[] aUris = uris.stream().toArray(String[]::new);
        final String paramString = Joiner.on(",").skipNulls().join(paramVariables);
        final ColumnListHandler<Long> handler = new ColumnListHandler<>("id");
        final List<Long> ids = new QueryRunner().query(conn,
                String.format(SQL.GET_DATA_METADATA_IDS, paramString), handler, (Object[]) aUris);
        if (!ids.isEmpty()) {
            final List<String> idParamVariables = ids.stream().map(s -> "?").collect(Collectors.toList());
            final Long[] aIds = ids.stream().toArray(Long[]::new);
            final String idParamString = Joiner.on(",").skipNulls().join(idParamVariables);
            final List<Long> dupIds = new QueryRunner().query(conn,
                    String.format(SQL.GET_DATA_METADATA_DELETE_BY_IDS, idParamString), handler,
                    (Object[]) aIds);
            if (!dupIds.isEmpty()) {
                ids.removeAll(dupIds);/*from   w  w w.ja  v a  2s  . co  m*/
            }
            final List<Object[]> deleteDataMetadatas = Lists.newArrayList();
            ids.forEach(id -> deleteDataMetadatas.add(new Object[] { id, userId }));
            new QueryRunner().batch(conn, SQL.SOFT_DELETE_DATA_METADATA,
                    deleteDataMetadatas.toArray(new Object[deleteDataMetadatas.size()][2]));
        }
    }
    return null;
}

From source file:com.netflix.metacat.usermetadata.mysql.MysqlUserMetadataService.java

@SuppressWarnings("checkstyle:methodname")
private Void _deleteDataMetadatas(final Connection conn, final List<String> uris) throws SQLException {
    if (uris != null && !uris.isEmpty()) {
        final List<String> paramVariables = uris.stream().map(s -> "?").collect(Collectors.toList());
        final String[] aUris = uris.stream().toArray(String[]::new);
        final String paramString = Joiner.on(",").skipNulls().join(paramVariables);
        final ColumnListHandler<Long> handler = new ColumnListHandler<>("id");
        final List<Long> ids = new QueryRunner().query(conn,
                String.format(SQL.GET_DATA_METADATA_IDS, paramString), handler, (Object[]) aUris);
        if (!ids.isEmpty()) {
            final List<String> idParamVariables = ids.stream().map(s -> "?").collect(Collectors.toList());
            final Long[] aIds = ids.stream().toArray(Long[]::new);
            final String idParamString = Joiner.on(",").skipNulls().join(idParamVariables);
            new QueryRunner().update(conn, String.format(SQL.DELETE_DATA_METADATA_DELETE, idParamString),
                    (Object[]) aIds);
            new QueryRunner().update(conn, String.format(SQL.DELETE_DATA_METADATA, idParamString),
                    (Object[]) aIds);
        }// ww w .  j a v a2 s.  c  o  m
    }
    return null;
}

From source file:com.xeiam.yank.Yank.java

/**
 * Return a List of Objects from a single table column given an SQL statement
 *
 * @param <T>/*www . j a va2s .co m*/
 * @param sql The SQL statement
 * @param params The replacement parameters
 * @param columnType The Class of the desired return Objects matching the table
 * @return The Column as a List
 */
public static <T> List<T> queryColumn(String sql, String columnName, Class<T> columnType, Object[] params) {

    List<T> returnList = null;

    try {

        ColumnListHandler<T> resultSetHandler = new ColumnListHandler<T>(columnName);

        returnList = new QueryRunner(YANK_POOL_MANAGER.getDataSource()).query(sql, resultSetHandler, params);

    } catch (SQLException e) {
        handleSQLException(e);
    }

    return returnList;
}

From source file:com.netflix.metacat.usermetadata.mysql.MysqlUserMetadataService.java

@Override
public List<QualifiedName> getDescendantDefinitionNames(@Nonnull final QualifiedName name) {
    List<String> result = null;
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {//from   ww  w  .j a v  a2 s .  co  m
        final ColumnListHandler<String> handler = new ColumnListHandler<>("name");
        result = new QueryRunner().query(connection, SQL.GET_DESCENDANT_DEFINITION_NAMES, handler,
                name.toString() + "/%");
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed to get descendant names for %s", name), e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result.stream().map(QualifiedName::fromString).collect(Collectors.toList());
}

From source file:com.netflix.metacat.usermetadata.mysql.MySqlTagService.java

/**
 * Returns the list of <code>QualifiedName</code> of items that are tagged by the
 * given <code>includeTags</code> and do not contain the given <code>excludeTags</code>.
 * @param includeTags include items that contain tags
 * @param excludeTags include items that do not contain tags
 * @param sourceName catalog/source name
 * @param databaseName database name//ww  w .jav  a  2  s.  c om
 * @param tableName table name
 * @return list of qualified names of the items
 */
@Override
public List<QualifiedName> list(final Set<String> includeTags, final Set<String> excludeTags,
        final String sourceName, final String databaseName, final String tableName) {
    Set<String> includedNames = Sets.newHashSet();
    final Set<String> excludedNames = Sets.newHashSet();
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final QueryRunner runner = new QueryRunner();
        final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
        //Includes
        String query = String.format(QUERY_SEARCH,
                "in ('" + Joiner.on("','").skipNulls().join(includeTags) + "')");
        final Object[] params = { includeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
        includedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), params));
        if (excludeTags != null && !excludeTags.isEmpty()) {
            //Excludes
            query = String.format(QUERY_SEARCH,
                    "in ('" + Joiner.on("','").skipNulls().join(excludeTags) + "')");
            final Object[] eParams = { excludeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0,
                    wildCardName };
            excludedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), eParams));
        }
    } catch (SQLException e) {
        final String message = String.format("Failed getting the list of qualified names for tags %s",
                includeTags);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }

    if (excludeTags != null && !excludeTags.isEmpty()) {
        includedNames = Sets.difference(includedNames, excludedNames);
    }

    return includedNames.stream().map(s -> QualifiedName.fromString(s, false)).collect(Collectors.toList());
}

From source file:com.netflix.metacat.usermetadata.mysql.MysqlUserMetadataService.java

@Override
public List<String> getDescendantDataUris(@Nonnull final String uri) {
    List<String> result = null;
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {/*from   w  w  w .ja  va  2  s.  c  o m*/
        final ColumnListHandler<String> handler = new ColumnListHandler<>("uri");
        result = new QueryRunner().query(connection, SQL.GET_DESCENDANT_DATA_URIS, handler, uri + "/%");
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed to get descendant uris for %s", uri), e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}

From source file:com.netflix.metacat.usermetadata.mysql.MySqlTagService.java

/**
 * Returns the list of <code>QualifiedName</code> of items that have tags containing the given tag text.
 * @param tag partial text of a tag//from   ww  w .j  av  a 2  s.  c o  m
 * @param sourceName source/catalog name
 * @param databaseName database name
 * @param tableName table name
 * @return list of qualified names of the items
 */
@Override
public List<QualifiedName> search(final String tag, final String sourceName, final String databaseName,
        final String tableName) {
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
        //Includes
        final String query = String.format(QUERY_SEARCH, "like ?");
        final Object[] params = { tag == null ? 1 : 0, tag + "%", wildCardName == null ? 1 : 0, wildCardName };
        return new QueryRunner().query(connection, query, new ColumnListHandler<>("name"), params);
    } catch (SQLException e) {
        final String message = String.format("Failed getting the list of qualified names for tag %s", tag);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
}