Search in sources :

Example 1 with QueryRunner

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

the class MysqlUserMetadataService method getDescendantDataUris.

@Override
public List<String> getDescendantDataUris(@Nonnull final String uri) {
    List<String> result;
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        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;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 2 with QueryRunner

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

the class MysqlUserMetadataService method _deleteDataMetadatas.

@SuppressWarnings("checkstyle:methodname")
private Void _deleteDataMetadatas(final Connection conn, @Nullable final List<String> uris, final boolean removeDataMetadata) 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);
            if (removeDataMetadata) {
                new QueryRunner().update(conn, String.format(SQL.DELETE_DATA_METADATA, idParamString), (Object[]) aIds);
            }
        }
    }
    return null;
}
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) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 3 with QueryRunner

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

the class MysqlUserMetadataService method _deleteDefinitionMetadatas.

@SuppressWarnings("checkstyle:methodname")
private Void _deleteDefinitionMetadatas(final Connection conn, @Nullable final List<QualifiedName> names) throws SQLException {
    if (names != null && !names.isEmpty()) {
        final List<String> paramVariables = names.stream().map(s -> "?").collect(Collectors.toList());
        final String[] aNames = names.stream().map(QualifiedName::toString).toArray(String[]::new);
        new QueryRunner().update(conn, String.format(SQL.DELETE_DEFINITION_METADATA, Joiner.on(",").skipNulls().join(paramVariables)), (Object[]) aNames);
    }
    return null;
}
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) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 4 with QueryRunner

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

the class MysqlUserMetadataService method getDescendantDefinitionNames.

@Override
public List<QualifiedName> getDescendantDefinitionNames(@Nonnull final QualifiedName name) {
    List<String> result;
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        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());
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 5 with QueryRunner

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

the class MysqlUserMetadataService method getDeletedDataMetadataUris.

@Override
public List<String> getDeletedDataMetadataUris(final Date deletedPriorTo, final Integer offset, final Integer limit) {
    List<String> result;
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        final ColumnListHandler<String> handler = new ColumnListHandler<>("uri");
        result = new QueryRunner().query(connection, String.format(SQL.GET_DELETED_DATA_METADATA_URI, offset, limit), handler, deletedPriorTo);
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed to get deleted data metadata uris deleted prior to %s", deletedPriorTo), e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

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