Search in sources :

Example 1 with IllegalQueryParameterException

use of com.hortonworks.registries.storage.exception.IllegalQueryParameterException 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)

Aggregations

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 CaseAgnosticStringSet (com.hortonworks.registries.storage.impl.jdbc.util.CaseAgnosticStringSet)1 HashMap (java.util.HashMap)1