Search in sources :

Example 1 with TableOperations

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());
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) QueriesJdbcConfiguration(org.infinispan.persistence.sql.configuration.QueriesJdbcConfiguration) TableOperations(org.infinispan.persistence.jdbc.common.TableOperations) QueriesJdbcConfigurationBuilder(org.infinispan.persistence.sql.configuration.QueriesJdbcConfigurationBuilder) HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) IntSet(org.infinispan.commons.util.IntSet) SQLException(java.sql.SQLException) ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) ResultSet(java.sql.ResultSet) QueriesJdbcStoreConfigurationBuilder(org.infinispan.persistence.sql.configuration.QueriesJdbcStoreConfigurationBuilder) Map(java.util.Map) QueriesJdbcStoreConfiguration(org.infinispan.persistence.sql.configuration.QueriesJdbcStoreConfiguration) ConfiguredBy(org.infinispan.commons.configuration.ConfiguredBy) ResultSetMetaData(java.sql.ResultSetMetaData) QueriesJdbcConfiguration(org.infinispan.persistence.sql.configuration.QueriesJdbcConfiguration) HashMap(java.util.HashMap) QueriesJdbcConfigurationBuilder(org.infinispan.persistence.sql.configuration.QueriesJdbcConfigurationBuilder)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConfiguredBy (org.infinispan.commons.configuration.ConfiguredBy)1 IntSet (org.infinispan.commons.util.IntSet)1 TableOperations (org.infinispan.persistence.jdbc.common.TableOperations)1 ConnectionFactory (org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory)1 QueriesJdbcConfiguration (org.infinispan.persistence.sql.configuration.QueriesJdbcConfiguration)1 QueriesJdbcConfigurationBuilder (org.infinispan.persistence.sql.configuration.QueriesJdbcConfigurationBuilder)1 QueriesJdbcStoreConfiguration (org.infinispan.persistence.sql.configuration.QueriesJdbcStoreConfiguration)1 QueriesJdbcStoreConfigurationBuilder (org.infinispan.persistence.sql.configuration.QueriesJdbcStoreConfigurationBuilder)1