Search in sources :

Example 11 with QueryRunner

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

the class MySqlTagService method rename.

@Override
public Void rename(final QualifiedName name, final String newTableName) {
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            final QualifiedName newName = QualifiedName.ofTable(name.getCatalogName(), name.getDatabaseName(), newTableName);
            new QueryRunner().update(conn, SQL_UPDATE_TAG_ITEM, newName.toString(), name.toString());
            conn.commit();
        } catch (Exception e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        final String message = String.format("Failed to rename item name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
    return null;
}
Also used : 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) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 12 with QueryRunner

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

the class MySqlTagService method search.

/**
     * Returns the list of <code>QualifiedName</code> of items that have tags containing the given tag text.
     *
     * @param tag          partial text of a tag
     * @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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 13 with QueryRunner

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

the class MySqlTagService method remove.

private void remove(final Connection conn, final QualifiedName name, final Set<String> tags, final boolean updateUserMetadata) throws SQLException {
    new QueryRunner().update(conn, String.format(SQL_DELETE_TAG_ITEM_TAGS_BY_NAME_TAGS, "'" + Joiner.on("','").skipNulls().join(tags) + "'"), name.toString());
    if (updateUserMetadata) {
        final TagItem tagItem = get(name);
        tagItem.getValues().removeAll(tags);
        final Map<String, Set<String>> data = Maps.newHashMap();
        data.put(NAME_TAGS, tagItem.getValues());
        userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
    }
}
Also used : TagItem(com.netflix.metacat.common.server.model.TagItem) Set(java.util.Set) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 14 with QueryRunner

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

the class MySqlLookupService method insertLookupValues.

private void insertLookupValues(final Long id, final Set<String> inserts, final Connection conn) throws SQLException {
    final Object[][] params = new Object[inserts.size()][];
    final Iterator<String> iter = inserts.iterator();
    int index = 0;
    while (iter.hasNext()) {
        params[index++] = ImmutableList.of(id, iter.next()).toArray();
    }
    new QueryRunner().batch(conn, SQL_INSERT_LOOKUP_VALUES, params);
}
Also used : QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 15 with QueryRunner

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

the class MySqlLookupService method findOrCreateLookupByName.

private Lookup findOrCreateLookupByName(final String name, final Connection conn) throws SQLException {
    Lookup lookup = get(name);
    if (lookup == null) {
        final Object[] params = { name, STRING_TYPE, config.getLookupServiceUserAdmin(), config.getLookupServiceUserAdmin() };
        final Long lookupId = new QueryRunner().insert(conn, SQL_INSERT_LOOKUP, new ScalarHandler<>(1), params);
        lookup = new Lookup();
        lookup.setName(name);
        lookup.setId(lookupId);
    }
    return lookup;
}
Also used : Lookup(com.netflix.metacat.common.server.model.Lookup) 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