Permalink
Browse files

ISPN-2155 JDBC cache store should quote generated table name

  • Loading branch information...
tristantarrant authored and galderz committed Jul 23, 2012
1 parent 1001d9a commit 7cae612edd501665fdf09b8c089860710f203bd5
@@ -82,6 +82,7 @@
public DatabaseType databaseType;
private String loadAllKeysBinarySql;
private String loadAllKeysStringSql;
+ private String identifierQuoteString;
public TableManipulation(String idColumnName, String idColumnType, String tableNamePrefix, String dataColumnName,
String dataColumnType, String timestampColumnName, String timestampColumnType) {
@@ -355,11 +356,25 @@ public String getTableName() {
if (tableNamePrefix == null || cacheName == null) {
throw new IllegalStateException("Both tableNamePrefix and cacheName must be non null at this point!");
}
- tableName = tableNamePrefix + "_" + cacheName.replace(".", "_");
+ tableName = getIdentifierQuoteString() + tableNamePrefix + "_" + cacheName.replace(".", "_") + getIdentifierQuoteString();
}
return tableName;
}
+ public String getIdentifierQuoteString() {
+ if (identifierQuoteString == null) {
+ switch (getDatabaseType()) {
+ case MYSQL:
+ identifierQuoteString = "`";
+ break;
+ default:
+ identifierQuoteString = "\"";
+ break;
+ }
+ }
+ return identifierQuoteString;
+ }
+
public String getTableNamePrefix() {
return tableNamePrefix;
}
@@ -158,6 +158,13 @@ public void testDrop() throws Exception {
assert !tableManipulation.tableExists(connection);
}
+ public void testTableQuoting() throws Exception {
+ tableManipulation.setCacheName("my.cache");
+ assert !existsTable(connection, tableManipulation.getTableName());
+ tableManipulation.createTable(connection);
+ assert existsTable(connection, tableManipulation.getTableName());
+ }
+
static boolean existsTable(Connection connection, String tableName) throws Exception {
Statement st = connection.createStatement();
ResultSet rs = null;
@@ -65,7 +65,8 @@ public void testForJdbcStringBasedCacheStore() throws Exception {
JdbcStringBasedCacheStore firstCs = (JdbcStringBasedCacheStore) TestingUtil.extractComponent(first, CacheLoaderManager.class).getCacheLoader();
JdbcStringBasedCacheStore secondCs = (JdbcStringBasedCacheStore) TestingUtil.extractComponent(second, CacheLoaderManager.class).getCacheLoader();
- asserTableExistance(firstCs.getConnectionFactory().getConnection(), "ISPN_STRING_TABLE_second", "ISPN_STRING_TABLE_first", "ISPN_STRING_TABLE");
+ String quote = firstCs.getTableManipulation().getIdentifierQuoteString();
+ assertTableExistence(firstCs.getConnectionFactory().getConnection(), quote, "ISPN_STRING_TABLE_second", "ISPN_STRING_TABLE_first", "ISPN_STRING_TABLE");
assertNoOverlapingState(first, second, firstCs, secondCs);
} finally {
@@ -83,7 +84,8 @@ public void testForJdbcBinaryCacheStore() throws Exception {
JdbcBinaryCacheStore firstCs = (JdbcBinaryCacheStore) TestingUtil.extractComponent(first, CacheLoaderManager.class).getCacheLoader();
JdbcBinaryCacheStore secondCs = (JdbcBinaryCacheStore) TestingUtil.extractComponent(second, CacheLoaderManager.class).getCacheLoader();
- asserTableExistance(firstCs.getConnectionFactory().getConnection(), "ISPN_BUCKET_TABLE_second", "ISPN_BUCKET_TABLE_first", "IISPN_BUCKET_TABLE");
+ String quote = firstCs.getTableManipulation().getIdentifierQuoteString();
+ assertTableExistence(firstCs.getConnectionFactory().getConnection(), quote, "ISPN_BUCKET_TABLE_second", "ISPN_BUCKET_TABLE_first", "IISPN_BUCKET_TABLE");
assertNoOverlapingState(first, second, firstCs, secondCs);
} finally {
@@ -103,8 +105,9 @@ public void testForMixedCacheStore() throws Exception {
JdbcMixedCacheStore firstCs = (JdbcMixedCacheStore) TestingUtil.extractComponent(first, CacheLoaderManager.class).getCacheLoader();
JdbcMixedCacheStore secondCs = (JdbcMixedCacheStore) TestingUtil.extractComponent(second, CacheLoaderManager.class).getCacheLoader();
- asserTableExistance(firstCs.getConnectionFactory().getConnection(), "ISPN_MIXED_STR_TABLE_second", "ISPN_MIXED_STR_TABLE_first", "ISPN_MIXED_STR_TABLE");
- asserTableExistance(firstCs.getConnectionFactory().getConnection(), "ISPN_MIXED_BINARY_TABLE_second", "ISPN_MIXED_BINARY_TABLE_first", "ISPN_MIXED_BINARY_TABLE");
+ String quote = firstCs.getStringBasedCacheStore().getTableManipulation().getIdentifierQuoteString();
+ assertTableExistence(firstCs.getConnectionFactory().getConnection(), quote, "ISPN_MIXED_STR_TABLE_second", "ISPN_MIXED_STR_TABLE_first", "ISPN_MIXED_STR_TABLE");
+ assertTableExistence(firstCs.getConnectionFactory().getConnection(), quote, "ISPN_MIXED_BINARY_TABLE_second", "ISPN_MIXED_BINARY_TABLE_first", "ISPN_MIXED_BINARY_TABLE");
assertNoOverlapingState(first, second, firstCs, secondCs);
@@ -159,13 +162,17 @@ public int hashCode() {
}
}
- private void asserTableExistance(Connection connection, String secondTable, String firstTable, String tablePrefix) throws Exception {
- assert !TableManipulationTest.existsTable(connection, tablePrefix) : "this table should not exist!";
- assert TableManipulationTest.existsTable(connection, firstTable);
- assert TableManipulationTest.existsTable(connection, secondTable);
+ private void assertTableExistence(Connection connection, String quote, String secondTable, String firstTable, String tablePrefix) throws Exception {
+ assert !TableManipulationTest.existsTable(connection, quoteTableName(quote, tablePrefix)) : "this table should not exist!";
+ assert TableManipulationTest.existsTable(connection, quoteTableName(quote, firstTable));
+ assert TableManipulationTest.existsTable(connection, quoteTableName(quote, secondTable));
connection.close();
}
+ private String quoteTableName(String quote, String tableName) {
+ return quote + tableName + quote;
+ }
+
private void assertNoOverlapingState(Cache<String, String> first, Cache<String, String> second, CacheStore firstCs, CacheStore secondCs) throws CacheLoaderException {
first.put("k", "v");
assert firstCs.containsKey("k");
@@ -56,18 +56,17 @@
private static final String DB_TYPE = System.getProperty("infinispan.test.jdbc.db", "H2");
private static final String H2_DRIVER = org.h2.Driver.class.getName();
private static final String NON_EXISTENT_DRIVER = "non.existent.Driver";
+ private static final DatabaseType dt;
static {
String driver = "";
- DatabaseType dt = DatabaseType.H2;
try {
- if (!DB_TYPE.equalsIgnoreCase("H2")) {
- if (DB_TYPE.equalsIgnoreCase("mysql")) {
- driver = Driver.class.getName();
- dt = DatabaseType.MYSQL;
- } else {
- driver = H2_DRIVER;
- }
+ if (DB_TYPE.equalsIgnoreCase("mysql")) {
+ driver = Driver.class.getName();
+ dt = DatabaseType.MYSQL;
+ } else {
+ driver = H2_DRIVER;
+ dt = DatabaseType.H2;
}
try {
Class.forName(driver);
@@ -153,17 +152,17 @@ private static String extractTestName() {
public static TableManipulation buildStringTableManipulation() {
-
- return new TableManipulation("ID_COLUMN", "VARCHAR(255)", "ISPN_JDBC", "DATA_COLUMN",
+ TableManipulation tableManipulation = new TableManipulation("ID_COLUMN", "VARCHAR(255)", "ISPN_JDBC", "DATA_COLUMN",
"BLOB", "TIMESTAMP_COLUMN", "BIGINT");
-
+ tableManipulation.databaseType = dt;
+ return tableManipulation;
}
public static TableManipulation buildBinaryTableManipulation() {
-
- return new TableManipulation("ID_COLUMN", "INT", "ISPN_JDBC", "DATA_COLUMN",
+ TableManipulation tableManipulation = new TableManipulation("ID_COLUMN", "INT", "ISPN_JDBC", "DATA_COLUMN",
"BLOB", "TIMESTAMP_COLUMN", "BIGINT");
-
+ tableManipulation.databaseType = dt;
+ return tableManipulation;
}
/**

0 comments on commit 7cae612

Please sign in to comment.