use of org.infinispan.persistence.jdbc.common.TableOperations in project infinispan by infinispan.
the class QueriesJdbcStore method actualCreateTableOperations.
@Override
protected TableOperations<K, V> actualCreateTableOperations(ProtoSchemaOptions<K, V, QueriesJdbcStoreConfiguration> options) {
QueriesJdbcConfigurationBuilder builder = new QueriesJdbcConfigurationBuilder();
QueriesJdbcConfiguration originalConfig = config.getQueriesJdbcConfiguration();
builder.read(originalConfig);
QueryNamedParameterParser.ParserResults selectResults = QueryNamedParameterParser.parseSqlStatement(originalConfig.select());
builder.select(selectResults.getSqlToUse());
if (config.ignoreModifications()) {
return new QueryTableOperations(options, null, builder.create());
}
QueryNamedParameterParser.ParserResults deleteResults = QueryNamedParameterParser.parseSqlStatement(originalConfig.delete());
builder.delete(deleteResults.getSqlToUse());
// Delete all should not have any parameters
if (QueryNamedParameterParser.parseSqlStatement(originalConfig.deleteAll()).getOrderedParameters().size() > 0) {
throw log.deleteAllCannotHaveParameters(config.getQueriesJdbcConfiguration().selectAll());
}
// Size should not have any parameters
if (QueryNamedParameterParser.parseSqlStatement(originalConfig.size()).getOrderedParameters().size() > 0) {
throw log.sizeCannotHaveParameters(config.getQueriesJdbcConfiguration().selectAll());
}
// This ensures that delete and select parameters match, so we only need on instance of key parameters for both
if (!deleteResults.getOrderedParameters().equals(selectResults.getOrderedParameters())) {
throw log.deleteAndSelectQueryMismatchArguments(deleteResults.getOrderedParameters(), selectResults.getOrderedParameters());
}
// (e.g.) INSERT INTO books (isbn, title) VALUES (:key, :value) ON CONFLICT (isbn) DO UPDATE SET title = :value
QueryNamedParameterParser.ParserResults upsertResults = QueryNamedParameterParser.parseSqlStatement(originalConfig.upsert());
builder.upsert(upsertResults.getSqlToUse());
Map<String, Parameter> parameterMap = new HashMap<>();
// This includes all the keys as well
for (Parameter parameter : options.valueParameters) {
parameterMap.put(parameter.getName().toUpperCase(), parameter);
}
Parameter[] upsertParameters = upsertResults.getOrderedParameters().stream().map(name -> {
Parameter param = parameterMap.get(name.toUpperCase());
if (param == null) {
throw log.deleteAndSelectQueryMismatchArguments(name, originalConfig.upsert(), originalConfig.selectAll());
}
return param;
}).toArray(Parameter[]::new);
return new QueryTableOperations(options, upsertParameters, builder.create());
}
Aggregations