Search in sources :

Example 36 with QueryRunner

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

the class MySqlLookupService method get.

/**
     * Returns the lookup for the given <code>name</code>.
     *
     * @param name lookup name
     * @return lookup
     */
@Override
public Lookup get(final String name) {
    Lookup result;
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final ResultSetHandler<Lookup> handler = new BeanHandler<>(Lookup.class);
        result = new QueryRunner().query(connection, SQL_GET_LOOKUP, handler, name);
        if (result != null) {
            result.setValues(getValues(result.getId()));
        }
    } catch (Exception e) {
        final String message = String.format("Failed to get the lookup for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Connection(java.sql.Connection) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) Lookup(com.netflix.metacat.common.server.model.Lookup) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 37 with QueryRunner

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

the class MySqlTagService method get.

/**
     * Returns the TagItem for the given <code>name</code>.
     *
     * @param name tag name
     * @return TagItem
     */
public TagItem get(final String name) {
    TagItem result = null;
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final ResultSetHandler<TagItem> handler = new BeanHandler<>(TagItem.class);
        result = new QueryRunner().query(connection, SQL_GET_TAG_ITEM, handler, name);
        if (result != null) {
            result.setValues(getValues(result.getId()));
        }
    } catch (Exception e) {
        final String message = String.format("Failed to get the tag item for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) Connection(java.sql.Connection) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 38 with QueryRunner

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

the class MySqlTagService method findOrCreateTagItemByName.

private TagItem findOrCreateTagItemByName(final String name, final Connection conn) throws SQLException {
    TagItem result = get(name);
    if (result == null) {
        final Object[] params = { name, config.getTagServiceUserAdmin(), config.getTagServiceUserAdmin() };
        final Long id = new QueryRunner().insert(conn, SQL_INSERT_TAG_ITEM, new ScalarHandler<>(1), params);
        result = new TagItem();
        result.setName(name);
        result.setId(id);
    }
    return result;
}
Also used : TagItem(com.netflix.metacat.common.server.model.TagItem) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 39 with QueryRunner

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

the class MysqlUserMetadataService method _getMetadataMap.

@SuppressWarnings("checkstyle:methodname")
private Map<String, ObjectNode> _getMetadataMap(@Nullable final List<?> keys, final String sql) {
    final Map<String, ObjectNode> result = Maps.newHashMap();
    if (keys == null || keys.isEmpty()) {
        return result;
    }
    final List<String> paramVariables = keys.stream().map(s -> "?").collect(Collectors.toList());
    final String[] aKeys = keys.stream().map(Object::toString).toArray(String[]::new);
    final String query = String.format(sql, Joiner.on(",").join(paramVariables));
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try {
        final ResultSetHandler<Void> handler = resultSet -> {
            while (resultSet.next()) {
                final String json = resultSet.getString("data");
                final String name = resultSet.getString("name");
                if (json != null) {
                    try {
                        result.put(name, metacatJson.parseJsonObject(json));
                    } catch (MetacatJsonException e) {
                        log.error("Invalid json '{}' for name '{}'", json, name);
                        throw new UserMetadataServiceException(String.format("Invalid json %s for name %s", json, name), e);
                    }
                }
            }
            return null;
        };
        new QueryRunner().query(connection, query, handler, (Object[]) aKeys);
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed to get data for %s", keys), 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) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SQLException(java.sql.SQLException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 40 with QueryRunner

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

the class MysqlUserMetadataService method _softDeleteDataMetadatas.

@SuppressWarnings("checkstyle:methodname")
private Void _softDeleteDataMetadatas(final Connection conn, final String userId, @Nullable 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);
            }
            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;
}
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)

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