static FunctionMethod replaceProcedureWithFunction(MetadataFactory factory, Procedure proc) throws MetadataException { if (proc.isFunction() && proc.getQueryPlan() != null) { return null; } FunctionMethod method = createFunctionMethod(proc); //remove the old proc factory.getSchema().getResolvingOrder().remove(factory.getSchema().getResolvingOrder().size() - 1); factory.getSchema().getProcedures().remove(proc.getName()); factory.getSchema().addFunction(method); return method; }
static FunctionMethod replaceProcedureWithFunction(MetadataFactory factory, Procedure proc) throws MetadataException { if (proc.isFunction() && proc.getQueryPlan() != null) { return null; } FunctionMethod method = createFunctionMethod(proc); //remove the old proc factory.getSchema().getResolvingOrder().remove(factory.getSchema().getResolvingOrder().size() - 1); factory.getSchema().getProcedures().remove(proc.getName()); factory.getSchema().addFunction(method); return method; }
static FunctionMethod replaceProcedureWithFunction(MetadataFactory factory, Procedure proc) throws MetadataException { if (proc.isFunction() && proc.getQueryPlan() != null) { return null; } FunctionMethod method = createFunctionMethod(proc); //remove the old proc factory.getSchema().getResolvingOrder().remove(factory.getSchema().getResolvingOrder().size() - 1); factory.getSchema().getProcedures().remove(proc.getName()); factory.getSchema().addFunction(method); return method; }
@Test(expected=MetadataException.class) public void testTableDefinition() throws Exception { String ddl = "CREATE FOREIGN TABLE G1(e4 string); alter view g1 as select 'a';"; helpParse(ddl, "model").getSchema(); }
@Test(expected=MetadataException.class) public void testAfterTriggerNameRequired() throws Exception { String ddl = "CREATE FOREIGN TABLE T ( e1 integer, e2 varchar);" + "CREATE TRIGGER ON T AFTER UPDATE AS " + "FOR EACH ROW \n" + "BEGIN ATOMIC \n" + "if (\"new\" is not distinct from \"old\") raise sqlexception 'error';\n" + "END;"; helpParse(ddl, "model").getSchema(); }
private Table addEntity(MetadataFactory mf, Metamodel model, EntityType<?> entity) throws TranslatorException { Table table = mf.getSchema().getTable(entity.getName()); if (table == null) { table = mf.addTable(entity.getName()); table.setSupportsUpdate(true); table.setProperty(ENTITYCLASS, entity.getJavaType().getCanonicalName()); addPrimaryKey(mf, model, entity, table); addSingularAttributes(mf, model, entity, table, Collections.EMPTY_LIST); } return table; }
private void helpTest(String ddl, String expected) { Schema s = TestDDLParser.helpParse(ddl, "model").getSchema(); String metadataDDL = DDLStringVisitor.getDDLString(s, null, null); assertEquals(expected, metadataDDL); }
@Test public void testOptionsKey() throws Exception { String ddl = "CREATE FOREIGN TABLE G1( e1 integer, e2 varchar, e3 date, UNIQUE (e1) OPTIONS (CUSTOM_PROP 'VALUE'))"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); Table table = tableMap.get("G1"); KeyRecord record = table.getAllKeys().iterator().next(); assertEquals("VALUE", record.getProperty("CUSTOM_PROP", false)); }
@Test public void testMixedCaseTypes() throws Exception { String ddl = "CREATE FUNCTION SourceFunc(flag Boolean) RETURNS varchaR options (UUID 'z')"; Schema s = helpParse(ddl, "model").getSchema(); FunctionMethod fm = s.getFunction("z"); assertEquals("boolean", fm.getInputParameters().get(0).getRuntimeType()); }
@Test public void testVarArgs() throws Exception { String ddl = "CREATE FUNCTION SourceFunc(flag boolean) RETURNS varchar options (varargs 'true', UUID 'z')"; Schema s = helpParse(ddl, "model").getSchema(); FunctionMethod fm = s.getFunction("z"); assertTrue( fm.getInputParameters().get(0).isVarArg()); }
@Test public void testAlterTableDropOptions() throws Exception { String ddl = "CREATE FOREIGN TABLE G1( e1 integer, e2 varchar, e3 date) OPTIONS(CARDINALITY 12, FOO 'BAR');" + "ALTER FOREIGN TABLE G1 OPTIONS(DROP CARDINALITY);" + "ALTER FOREIGN TABLE G1 OPTIONS(DROP FOO);"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); Table table = tableMap.get("G1"); assertEquals(-1, table.getCardinality()); assertNull(table.getProperty("FOO", false)); }
@Test public void testAlterTableRenameColumn() throws Exception { String ddl = "CREATE FOREIGN TABLE G1( e1 integer, e2 varchar, e3 date);" + "ALTER FOREIGN TABLE G1 RENAME COLUMN e3 TO e3renamed;"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); Table table = tableMap.get("G1"); Column e3 = table.getColumnByName("e3renamed"); assertNotNull(e3); assertEquals("date", e3.getRuntimeType()); }
@Test public void testAlterTableModifyOptions() throws Exception { String ddl = "CREATE FOREIGN TABLE G1( e1 integer, e2 varchar, e3 date) OPTIONS(CARDINALITY 12, FOO 'BAR');" + "ALTER FOREIGN TABLE G1 OPTIONS(SET CARDINALITY 24);" + "ALTER FOREIGN TABLE G1 OPTIONS(SET FOO 'BARBAR');"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); Table table = tableMap.get("G1"); assertEquals(24, table.getCardinality()); assertEquals("BARBAR", table.getProperty("FOO", false)); }
protected MetadataFactory createMetadataFactory(VDBMetaData vdb, MetadataStore store, ModelMetaData model, Map<String, ? extends VDBResource> vdbResources) { Map<String, Datatype> datatypes = store.getDatatypes(); MetadataFactory factory = new MetadataFactory(vdb.getName(), vdb.getVersion(), datatypes, model); factory.getSchema().setPhysical(model.isSource()); factory.setParser(new QueryParser()); //for thread safety each factory gets it's own instance. factory.setVdbResources(vdbResources); factory.setVDBClassLoader(vdb.getAttachment(ClassLoader.class)); return factory; }
@Test public void testView() throws Exception { String ddl = "CREATE View G1( e1 integer, e2 varchar) OPTIONS (CARDINALITY 12) AS select e1, e2 from foo.bar"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); Table table = tableMap.get("G1"); assertEquals("SELECT e1, e2 FROM foo.bar", table.getSelectTransformation()); assertEquals(12, table.getCardinality()); }
@Test public void testAlterProcedureColumnRename() throws Exception { String ddl = "CREATE FOREIGN PROCEDURE myProc(OUT p1 boolean, p2 varchar, INOUT p3 decimal) " + "RETURNS (r1 varchar, r2 decimal)" + "OPTIONS(RANDOM 'any', UUID 'uuid', NAMEINSOURCE 'nis', ANNOTATION 'desc', UPDATECOUNT '2');" + "ALTER FOREIGN PROCEDURE myProc RENAME PARAMETER p2 TO p2renamed;"; Schema s = helpParse(ddl, "model").getSchema(); Procedure proc = s.getProcedure("myProc"); assertNotNull(proc); assertEquals("p2renamed", proc.getParameters().get(1).getName()); }
@Test public void testArrayType() throws Exception { String ddl = "CREATE VIEW V (col string[]) as select ('a','b');"; Schema s = helpParse(ddl, "model").getSchema(); Table t = s.getTable("V"); assertEquals(1, t.getColumns().size()); assertEquals("string[]", t.getColumns().get(0).getRuntimeType()); assertEquals(String[].class, t.getColumns().get(0).getJavaType()); }
@Test public void testAlterProcedureColumnType() throws Exception { String ddl = "CREATE FOREIGN PROCEDURE myProc(OUT p1 boolean, p2 varchar, INOUT p3 decimal) " + "RETURNS (r1 varchar, r2 decimal)" + "OPTIONS(RANDOM 'any', UUID 'uuid', NAMEINSOURCE 'nis', ANNOTATION 'desc', UPDATECOUNT '2');" + "ALTER FOREIGN PROCEDURE myProc ALTER PARAMETER p2 TYPE integer;"; Schema s = helpParse(ddl, "model").getSchema(); Procedure proc = s.getProcedure("myProc"); assertNotNull(proc); assertEquals("p2", proc.getParameters().get(1).getName()); assertEquals("integer", proc.getParameters().get(1).getRuntimeType()); }
@Test public void testSchemaProperties() { ModelMetaData mmd = new ModelMetaData(); mmd.setName("foo"); mmd.addProperty("teiid_rel:data-ttl", "1"); MetadataFactory mf = new MetadataFactory("x", 1, Collections.EMPTY_MAP, mmd); Schema s = mf.getSchema(); assertEquals("foo", s.getName()); String val = s.getProperty(DataModifiable.DATA_TTL, false); assertEquals("1", val); }
@Test public void testPushdownFunctionNoArgs() throws Exception { String ddl = "CREATE FOREIGN FUNCTION SourceFunc() RETURNS integer OPTIONS (UUID 'hello world')"; Schema s = helpParse(ddl, "model").getSchema(); FunctionMethod fm = s.getFunction("hello world"); assertNotNull(fm); assertEquals("integer", fm.getOutputParameter().getRuntimeType()); assertEquals(FunctionMethod.PushDown.MUST_PUSHDOWN, fm.getPushdown()); }