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