Search in sources :

Example 1 with CaseAgnosticStringSet

use of com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet in project registry by hortonworks.

the class JdbcStorageManager method buildStorableKey.

// private helper methods
/**
 * Query parameters are typically specified for a column or key in a database table or storage namespace. Therefore, we build
 * the {@link StorableKey} from the list of query parameters, and then can use {@link SqlSelectQuery} builder to generate the query using
 * the query parameters in the where clause
 *
 * @return {@link StorableKey} with all query parameters that match database columns <br/>
 * null if none of the query parameters specified matches a column in the DB
 */
private StorableKey buildStorableKey(String namespace, List<QueryParam> queryParams) throws Exception {
    final Map<Schema.Field, Object> fieldsToVal = new HashMap<>();
    StorableKey storableKey = null;
    try {
        CaseAgnosticStringSet columnNames = queryExecutor.getColumnNames(namespace);
        for (QueryParam qp : queryParams) {
            if (!columnNames.contains(qp.getName())) {
                log.warn("Query parameter [{}] does not exist for namespace [{}]. Query parameter ignored.", qp.getName(), namespace);
            } else {
                final String val = qp.getValue();
                final Schema.Type typeOfVal = Schema.Type.getTypeOfVal(val);
                fieldsToVal.put(new Schema.Field(qp.getName(), typeOfVal), // instantiates object of the appropriate type
                typeOfVal.getJavaType().getConstructor(String.class).newInstance(val));
            }
        }
        // it is empty when none of the query parameters specified matches a column in the DB
        if (!fieldsToVal.isEmpty()) {
            final PrimaryKey primaryKey = new PrimaryKey(fieldsToVal);
            storableKey = new StorableKey(namespace, primaryKey);
        }
        log.debug("Building StorableKey from QueryParam: \n\tnamespace = [{}]\n\t queryParams = [{}]\n\t StorableKey = [{}]", namespace, queryParams, storableKey);
    } catch (Exception e) {
        log.debug("Exception occurred when attempting to generate StorableKey from QueryParam", e);
        throw new IllegalQueryParameterException(e);
    }
    return storableKey;
}
Also used : HashMap(java.util.HashMap) Schema(com.hortonworks.registries.common.Schema) PrimaryKey(com.hortonworks.registries.storage.PrimaryKey) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) StorageException(com.hortonworks.registries.storage.exception.StorageException) IllegalQueryParameterException(com.hortonworks.registries.storage.exception.IllegalQueryParameterException) OrderByField(com.hortonworks.registries.storage.OrderByField) CaseAgnosticStringSet(com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet) QueryParam(com.hortonworks.registries.common.QueryParam) StorableKey(com.hortonworks.registries.storage.StorableKey) IllegalQueryParameterException(com.hortonworks.registries.storage.exception.IllegalQueryParameterException)

Example 2 with CaseAgnosticStringSet

use of com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet in project registry by hortonworks.

the class AbstractQueryExecutor method getColumnNames.

@Override
public CaseAgnosticStringSet getColumnNames(String namespace) throws SQLException {
    CaseAgnosticStringSet columns = new CaseAgnosticStringSet();
    Connection connection = null;
    try {
        connection = getConnection();
        final ResultSetMetaData rsMetadata = PreparedStatementBuilder.of(connection, new ExecutionConfig(queryTimeoutSecs), storageDataTypeContext, new SqlSelectQuery(namespace)).getMetaData();
        for (int i = 1; i <= rsMetadata.getColumnCount(); i++) {
            columns.add(rsMetadata.getColumnName(i));
        }
        return columns;
    } catch (SQLException e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } finally {
        if (!transactionBookKeeper.hasActiveTransaction(Thread.currentThread().getId())) {
            closeConnection(connection);
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) CaseAgnosticStringSet(com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet) SqlSelectQuery(com.hortonworks.registries.storage.impl.jdbc.provider.sql.query.SqlSelectQuery) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecutionConfig(com.hortonworks.registries.storage.impl.jdbc.config.ExecutionConfig)

Example 3 with CaseAgnosticStringSet

use of com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet in project registry by hortonworks.

the class OracleExecutor method getColumnNames.

@Override
public CaseAgnosticStringSet getColumnNames(String namespace) throws SQLException {
    CaseAgnosticStringSet columns = new CaseAgnosticStringSet();
    Connection connection = null;
    try {
        connection = getConnection();
        final ResultSetMetaData rsMetadata = PreparedStatementBuilder.of(connection, new ExecutionConfig(queryTimeoutSecs), ORACLE_DATA_TYPE_CONTEXT, new OracleSelectQuery(namespace)).getMetaData();
        for (int i = 1; i <= rsMetadata.getColumnCount(); i++) {
            columns.add(rsMetadata.getColumnName(i));
        }
        return columns;
    } catch (SQLException e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } finally {
        if (!transactionBookKeeper.hasActiveTransaction(Thread.currentThread().getId())) {
            closeConnection(connection);
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) CaseAgnosticStringSet(com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ExecutionConfig(com.hortonworks.registries.storage.impl.jdbc.config.ExecutionConfig) OracleSelectQuery(com.hortonworks.registries.storage.impl.jdbc.provider.oracle.query.OracleSelectQuery)

Aggregations

CaseAgnosticStringSet (com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet)3 ExecutionConfig (com.hortonworks.registries.storage.impl.jdbc.config.ExecutionConfig)2 Connection (java.sql.Connection)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 QueryParam (com.hortonworks.registries.common.QueryParam)1 Schema (com.hortonworks.registries.common.Schema)1 OrderByField (com.hortonworks.registries.storage.OrderByField)1 PrimaryKey (com.hortonworks.registries.storage.PrimaryKey)1 StorableKey (com.hortonworks.registries.storage.StorableKey)1 AlreadyExistsException (com.hortonworks.registries.storage.exception.AlreadyExistsException)1 IllegalQueryParameterException (com.hortonworks.registries.storage.exception.IllegalQueryParameterException)1 StorageException (com.hortonworks.registries.storage.exception.StorageException)1 OracleSelectQuery (com.hortonworks.registries.storage.impl.jdbc.provider.oracle.query.OracleSelectQuery)1 SqlSelectQuery (com.hortonworks.registries.storage.impl.jdbc.provider.sql.query.SqlSelectQuery)1 HashMap (java.util.HashMap)1