QueryUtils { public static String discoverTablespace(String defaultTableSpace, String query) { if (query == null) { return defaultTableSpace; } final Matcher matcher = TABLE_SPACE_NAME_PATTERN.matcher(query); if (matcher.find()) { final String tableSpace = matcher.group("tablespace"); if (tableSpace != null) { return tableSpace.substring(0, tableSpace.length() - 1); } } return defaultTableSpace; }  static String discoverTablespace(String defaultTableSpace, String query);  }
@Test public void testDiscoverTablespaceFromSelect() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "select * from test")); assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "select a from myts.test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "select a.b from myts.test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "select a.b from myts.test where foo='bar'")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "select * from myts.test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "SELECT COUNT(M.id) FROM myts.a M WHERE 1=1 AND (M.id IN (SELECT H.id FROM myts.b AS H WHERE H.FIELD_NAME='From' AND H.FIELD_VALUE LIKE 'aaa'))")); }
@Test public void testDiscoverTablespaceFromUpdate() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "update test")); assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "update myts.test set a='b'")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "update myts.test")); }
@Test public void testDiscoverTablespaceFromInsert() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "insert into test(a,b) values(?,?,?)")); assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "insert into test values(?,?,?)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "insert into myts.test(a,b) values(?,?,?,?)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "insert into myts.test values(?,?,?,?)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "INSERT INTO myts.a SELECT * FROM myts.c M WHERE 1=1 AND (M.id IN (SELECT H.id FROM myts.b AS H WHERE H.FIELD_NAME='Into' AND H.FIELD_VALUE LIKE 'aaa'))")); }
@Test public void testDiscoverTablespaceFromUpsert() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "upsert into test(a,b) values(?,?,?)")); assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "upsert into test values(?,?,?)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "upsert into myts.test(a,b) values(?,?,?,?)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "upsert into myts.test values(?,?,?,?)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "UPSERT INTO myts.a SELECT * FROM myts.c M WHERE 1=1 AND (M.id IN (SELECT H.id FROM myts.b AS H WHERE H.FIELD_NAME='Into' AND H.FIELD_VALUE LIKE 'aaa'))")); }
@Test public void testDiscoverTablespaceFromDelete() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "delete * from test")); assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "delete a from myts.test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "delete a.b from myts.test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "delete a.b from myts.test where foo='bar'")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "delete * from myts.test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "DELTE FROM myts.a M WHERE 1=1 AND (M.id IN (SELECT H.id FROM myts.b AS H WHERE H.FIELD_NAME='From' AND H.FIELD_VALUE LIKE 'aaa'))")); }
@Test public void testDiscoverTablespaceFromCreateTable() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "create table test(a,b)")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "create table myts.test(a,b)")); }
@Test public void testDiscoverTablespaceFromDropTable() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "drop table test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "drop table myts.test")); }
@Test public void testDiscoverTablespaceFromTruncateTable() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "truncate table test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "truncate table myts.test")); }
@Test public void testDiscoverTablespaceFromAlterTable() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "alter table test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "alter table myts.test")); }
@Test public void testDiscoverTablespaceFromCreateIndex() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "create index test on test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "create index test on myts.test")); assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "create brin index test on test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "create brin index test on myts.test")); }
@Test public void testDiscoverTablespaceFromDropIndex() { String defaultTableSpace = TableSpace.DEFAULT; String theTableSpace = "myts"; assertEquals(defaultTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "drop index test")); assertEquals(theTableSpace, QueryUtils.discoverTablespace(defaultTableSpace, "drop index myts.test")); }