TableSpaceMapper { public String getTableSpace(Statement statement) throws ScriptException { String tableName = null; if (statement instanceof Insert) { Insert st = (Insert) statement; tableName = st.getTable().getFullyQualifiedName(); } else if (statement instanceof Update) { Update st = (Update) statement; tableName = st.getTable().getFullyQualifiedName(); } else if (statement instanceof Delete) { Delete st = (Delete) statement; tableName = st.getTables().get(0).getFullyQualifiedName(); } else if (statement instanceof Truncate) { Truncate st = (Truncate) statement; tableName = st.getTable().getFullyQualifiedName(); } else if (statement instanceof Drop) { Drop st = (Drop) statement; tableName = st.getName().getFullyQualifiedName(); } else if (statement instanceof CreateTable) { CreateTable st = (CreateTable) statement; tableName = st.getTable().getFullyQualifiedName(); } else if (statement instanceof Alter) { Alter st = (Alter) statement; tableName = st.getTable().getFullyQualifiedName(); } else { return null; } return mapTableNameToSchema(tableName); } TableSpaceMapper(String groovyScript); String getTableSpace(Statement statement); String getTableSpace(String tableName);  }
@Test public void testGetTableSpace() throws Exception { String script = "if (tableName.startsWith('test')) return 'ns1' else return 'ns2';"; TableSpaceMapper mapper = new TableSpaceMapper(script); { Insert insert = new Insert(); Table table = new Table("test1"); insert.setTable(table); assertEquals("ns1", mapper.getTableSpace(insert)); } { Insert insert = new Insert(); Table table = new Table("notest1"); insert.setTable(table); assertEquals("ns2", mapper.getTableSpace(insert)); } }