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;
}
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);
}
}
}
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);
}
}
}
Aggregations