@Test public void testGlobalTemp() throws Exception { String ddl = "CREATE GLOBAL TEMPORARY TABLE T (col string);"; Schema s = helpParse(ddl, "model").getSchema(); Table t = s.getTable("T"); assertEquals(1, t.getColumns().size()); }
@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 testInsteadOfTriggerIsDistinct() throws Exception { String ddl = "CREATE VIEW G1( e1 integer, e2 varchar) AS select * from foo;" + "CREATE TRIGGER ON G1 INSTEAD OF UPDATE AS " + "FOR EACH ROW \n" + "BEGIN ATOMIC \n" + "if (\"new\" is not distinct from \"old\") raise sqlexception 'error';\n" + "END;"; Schema s = helpParse(ddl, "model").getSchema(); assertEquals( "FOR EACH ROW\nBEGIN ATOMIC\nIF(\"new\" IS NOT DISTINCT FROM \"old\")\nBEGIN\nRAISE SQLEXCEPTION 'error';\nEND\nEND", s.getTable("G1").getUpdatePlan()); }
@Test public void testInsteadOfTrigger() throws Exception { String ddl = "CREATE VIEW G1( e1 integer, e2 varchar) AS select * from foo;" + "CREATE TRIGGER ON G1 INSTEAD OF INSERT AS " + "FOR EACH ROW \n" + "BEGIN ATOMIC \n" + "insert into g1 (e1, e2) values (1, 'trig');\n" + "END;" + "CREATE View G2( e1 integer, e2 varchar) AS select * from foo;"; Schema s = helpParse(ddl, "model").getSchema(); Map<String, Table> tableMap = s.getTables(); assertTrue("Table not found", tableMap.containsKey("G1")); assertTrue("Table not found", tableMap.containsKey("G2")); assertEquals( "FOR EACH ROW\nBEGIN ATOMIC\nINSERT INTO g1 (e1, e2) VALUES (1, 'trig');\nEND", s.getTable("G1").getInsertPlan()); }
@Override public void execute( VDBMetaData vdb, MetadataStore store, ValidatorReport report, MetadataValidator metadataValidator) { for (Schema schema : store.getSchemaList()) { if (vdb.getImportedModels().contains(schema.getName())) { continue; } ModelMetaData model = vdb.getModel(schema.getName()); for (Table t : schema.getTables().values()) { if (t.isVirtual()) { if (t.isMaterialized() && t.getMaterializedTable() != null) { String matTableName = t.getMaterializedTable().getFullName(); int index = matTableName.indexOf(Table.NAME_DELIM_CHAR); if (index == -1) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31088, matTableName, t.getFullName())); } else { String schemaName = matTableName.substring(0, index); Schema matSchema = store.getSchema(schemaName); if (matSchema == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31089, schemaName, matTableName, t.getFullName())); } else { Table matTable = matSchema.getTable(matTableName.substring(index + 1)); if (matTable == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31090, matTableName.substring(index + 1), schemaName, t.getFullName())); } else { t.setMaterializedTable(matTable); } } } } } for (KeyRecord record : t.getAllKeys()) { if (record.getColumns() == null || record.getColumns().isEmpty()) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31149, t.getFullName(), record.getName())); } } List<ForeignKey> fks = t.getForeignKeys(); if (fks == null || fks.isEmpty()) { continue; } for (ForeignKey fk : fks) { // Only applicable to older teiid releases than 8.9 if (fk.getReferenceKey() != null && !metadataValidator.isTeiidOrGreater(Version.TEIID_8_9)) { // ensure derived fields are set fk.setReferenceKey(fk.getReferenceKey()); continue; } String referenceTableName = fk.getReferenceTableName(); Table referenceTable = null; if (fk.getReferenceKey() == null) { if (referenceTableName == null) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31091, t.getFullName())); continue; } // TODO there is an ambiguity here because we don't properly track the name parts // so we have to first check for a table name that may contain . referenceTable = schema.getTable(referenceTableName); } else { referenceTableName = fk.getReferenceKey().getParent().getFullName(); } String referenceSchemaName = schema.getName(); int index = referenceTableName.indexOf(Table.NAME_DELIM_CHAR); if (referenceTable == null) { if (index != -1) { referenceSchemaName = referenceTableName.substring(0, index); Schema referenceSchema = store.getSchema(referenceSchemaName); if (referenceSchema == null) { metadataValidator.log( report, model, Messages.gs(Messages.TEIID.TEIID31093, referenceSchemaName, t.getFullName())); continue; } referenceTable = referenceSchema.getTable(referenceTableName.substring(index + 1)); } if (referenceTable == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31092, t.getFullName(), referenceTableName.substring(index + 1), referenceSchemaName)); continue; } } KeyRecord uniqueKey = null; List<String> referenceColumns = fk.getReferenceColumns(); if (fk.getReferenceKey() != null) { // index metadata logic sets the key prior to having the column names List<Column> cols = fk.getReferenceKey().getColumns(); referenceColumns = new ArrayList<String>(); for (Column col : cols) { referenceColumns.add(col.getName()); } } if (referenceColumns == null || referenceColumns.isEmpty()) { if (referenceTable.getPrimaryKey() == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31094, t.getFullName(), referenceTableName.substring(index + 1), referenceSchemaName)); } else { uniqueKey = referenceTable.getPrimaryKey(); } } else { for (KeyRecord record : referenceTable.getUniqueKeys()) { if (keyMatches(fk.getReferenceColumns(), record)) { uniqueKey = record; break; } } if (uniqueKey == null && referenceTable.getPrimaryKey() != null && keyMatches(fk.getReferenceColumns(), referenceTable.getPrimaryKey())) { uniqueKey = referenceTable.getPrimaryKey(); } } if (uniqueKey == null) { metadataValidator.log( report, model, Messages.gs( Messages.TEIID.TEIID31095, t.getFullName(), referenceTableName.substring(index + 1), referenceSchemaName, fk.getReferenceColumns())); } else { fk.setReferenceKey(uniqueKey); } } } } }