Search in sources :

Example 6 with ConnectionFactory

use of org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory in project infinispan by infinispan.

the class AbstractSQLStoreFunctionalTest method createTable.

protected void createTable(String cacheName, String tableName, ConnectionFactoryConfigurationBuilder<ConnectionFactoryConfiguration> builder) {
    String tableCreation;
    String upperCaseCacheName = cacheName.toUpperCase();
    if (cacheName.equalsIgnoreCase("testPreloadStoredAsBinary")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "picture " + binaryType() + ", " + "accepted_tos " + booleanType() + ", " + "sex VARCHAR(255), " + "birthdate " + dateTimeType() + ", " + "PRIMARY KEY (keycolumn))";
    } else if (cacheName.equalsIgnoreCase("testStoreByteArrays")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn " + binaryType() + " NOT NULL, " + "value1 " + binaryType() + " NOT NULL, " + "PRIMARY KEY (keycolumn))";
    } else if (upperCaseCacheName.startsWith("TESTDBHASMOREVALUECOLUMNS")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "picture " + binaryType() + ", " + "sex VARCHAR(255), " + "birthdate " + dateTimeType() + ", " + "value2 VARCHAR(255), " + "value3 VARCHAR(255), " + "PRIMARY KEY (keycolumn))";
    } else if (upperCaseCacheName.startsWith("TESTDBHASMOREKEYCOLUMNS")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + // The name of the field for the Key schema is "value"
        "value VARCHAR(255) NOT NULL, " + "keycolumn2 VARCHAR(255) NOT NULL," + "value1 VARCHAR(255) NOT NULL, " + "PRIMARY KEY (value, keycolumn2))";
    } else if (upperCaseCacheName.startsWith("TESTDBHASLESSVALUECOLUMNS")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "PRIMARY KEY (keycolumn))";
    } else if (upperCaseCacheName.startsWith("TESTEMBEDDED")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "NAME VARCHAR(255) NOT NULL, " + "street VARCHAR(255), " + "city VARCHAR(255), " + "zip INT, " + "picture " + binaryType() + ", " + "sex VARCHAR(255), " + "birthdate " + dateTimeType() + ", " + "PRIMARY KEY (name))";
    } else if (upperCaseCacheName.startsWith("TESTENUMFORVALUE")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "NAME VARCHAR(255) NOT NULL, " + "sex VARCHAR(255), " + "PRIMARY KEY (name))";
    } else if (upperCaseCacheName.startsWith("TESTENUMFORKEY")) {
        tableCreation = "CREATE TABLE " + tableName + " (" + "sex VARCHAR(255) NOT NULL, " + "name VARCHAR(255), " + "PRIMARY KEY (sex))";
    } else {
        tableCreation = "CREATE TABLE " + tableName + " (" + "keycolumn VARCHAR(255) NOT NULL, " + "value1 VARCHAR(255) NOT NULL, " + "PRIMARY KEY (keycolumn))";
    }
    ConnectionFactoryConfiguration config = builder.create();
    ConnectionFactory factory = ConnectionFactory.getConnectionFactory(config.connectionFactoryClass());
    factory.start(config, getClass().getClassLoader());
    Connection connection = null;
    try {
        connection = factory.getConnection();
        String modifiedTableName = tableToSearch(tableName);
        try (ResultSet rs = connection.getMetaData().getTables(null, null, modifiedTableName, new String[] { "TABLE" })) {
            if (!rs.next()) {
                try (Statement stmt = connection.createStatement()) {
                    log.debugf("Table: %s doesn't exist, creating via %s%n", modifiedTableName, tableCreation);
                    stmt.execute(tableCreation);
                }
            }
        }
    } catch (SQLException t) {
        throw new AssertionError(t);
    } finally {
        factory.releaseConnection(connection);
        factory.stop();
    }
}
Also used : ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(util.JdbcConnection) ResultSet(java.sql.ResultSet) ConnectionFactoryConfiguration(org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration)

Aggregations

ConnectionFactory (org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory)6 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 ConnectionFactoryConfiguration (org.infinispan.persistence.jdbc.common.configuration.ConnectionFactoryConfiguration)2 TableName (org.infinispan.persistence.jdbc.impl.table.TableName)2 InternalDataContainer (org.infinispan.container.impl.InternalDataContainer)1 UnitTestDatabaseManager.configureUniqueConnectionFactory (org.infinispan.persistence.jdbc.UnitTestDatabaseManager.configureUniqueConnectionFactory)1 PooledConnectionFactoryConfiguration (org.infinispan.persistence.jdbc.common.configuration.PooledConnectionFactoryConfiguration)1 PooledConnectionFactory (org.infinispan.persistence.jdbc.common.impl.connectionfactory.PooledConnectionFactory)1 JdbcStringBasedStore (org.infinispan.persistence.jdbc.stringbased.JdbcStringBasedStore)1 PersistenceManager (org.infinispan.persistence.manager.PersistenceManager)1 PersistenceException (org.infinispan.persistence.spi.PersistenceException)1 JdbcConnection (util.JdbcConnection)1