private ModelMetaData buildSourceTableWithPK() { ModelMetaData model = new ModelMetaData(); model.setName("source"); model.setModelType(Model.Type.PHYSICAL); MetadataFactory target = new MetadataFactory(VDBNAME, VDBVERSION, SystemMetadata.getInstance().getRuntimeTypeMap(), model); Table t = target.addTable("Person"); target.addColumn("id", "integer", t); target.addColumn("name", "string", t); target.addColumn("dob", "date", t); target.addPrimaryKey("PK", Arrays.asList("id"), t); Table addr = target.addTable("address"); target.addColumn("id", "integer", addr); target.addColumn("street", "string", addr); target.addColumn("pid", "integer", addr); target.addPrimaryKey("PK", Arrays.asList("id"), addr); target.addForeignKey("FK", Arrays.asList("pid"), Arrays.asList("id"), "Person", addr); String ddl = DDLStringVisitor.getDDLString(target.getSchema(), null, null); model.addSourceMetadata("ddl", ddl); model.addAttchment(MetadataFactory.class, target); return model; }
@Override public void loadMetadata(MetadataFactory factory, ExecutionFactory executionFactory, Object connectionFactory) throws TranslatorException { if (executionFactory != null && executionFactory.supportsDirectQueryProcedure() && factory.getSchema().getProcedure(executionFactory.getDirectQueryProcedureName()) == null) { Procedure p = factory.addProcedure(executionFactory.getDirectQueryProcedureName()); p.setAnnotation("Invokes translator with a native query that returns results in an array of values"); //$NON-NLS-1$ ProcedureParameter param = factory.addProcedureParameter("request", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$ param.setAnnotation("The native query to execute"); //$NON-NLS-1$ param.setNullType(NullType.No_Nulls); param = factory.addProcedureParameter("variable", TypeFacility.RUNTIME_NAMES.OBJECT, Type.In, p); //$NON-NLS-1$ param.setAnnotation("Any number of varaibles; usage will vary by translator"); //$NON-NLS-1$ param.setNullType(NullType.Nullable); param.setVarArg(true); factory.addProcedureResultSetColumn("tuple", DataTypeManager.getDataTypeName(DataTypeManager.getArrayType(TypeFacility.RUNTIME_TYPES.OBJECT)), p); //$NON-NLS-1$ } } }
@Override public void loadMetadata(MetadataFactory factory, ExecutionFactory executionFactory, Object connectionFactory, String text) throws TranslatorException { String ddlFile = factory.getModelProperties().getProperty("ddl-file"); if (ddlFile != null) { text = ddlFile; } if (text != null) { VDBResource resource = factory.getVDBResources().get(text); if (resource == null) { throw new MetadataException(Event.TEIID31137, QueryPlugin.Util.gs(Event.TEIID31137, text)); } InputStream is; try { is = resource.openStream(); } catch (IOException e1) { throw new MetadataException(e1); } try { //TODO: could allow for a property driven encoding factory.parse(new InputStreamReader(is, Charset.forName("UTF-8"))); //$NON-NLS-1$ } finally { try { is.close(); } catch (IOException e) { } } } }
@Override public void loadMetadata(MetadataFactory factory, ExecutionFactory executionFactory, Object connectionFactory, String text) throws TranslatorException { String ddl = factory.getModelProperties().getProperty("ddl"); if (ddl != null) { text = ddl; } if (text != null) { factory.parse(new StringReader(text)); } }
@Override public void getMetadata(MetadataFactory metadataFactory, VirtualFileConnection connection) throws TranslatorException { Procedure p = metadataFactory.addProcedure(GETTEXTFILES); p.setAnnotation("Returns text files that match the given path and pattern as CLOBs"); //$NON-NLS-1$ ProcedureParameter param = metadataFactory.addProcedureParameter("pathAndPattern", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p); //$NON-NLS-1$ param.setAnnotation("The path and pattern of what files to return. Currently the only pattern supported is *.<ext>, which returns only the files matching the given extension at the given path."); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.CLOB, p); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("filePath", TypeFacility.RUNTIME_NAMES.STRING, p); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("lastModified", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("created", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("size", TypeFacility.RUNTIME_NAMES.LONG, p); //$NON-NLS-1$ Procedure p1 = metadataFactory.addProcedure(GETFILES); p1.setAnnotation("Returns files that match the given path and pattern as BLOBs"); //$NON-NLS-1$ param = metadataFactory.addProcedureParameter("pathAndPattern", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p1); //$NON-NLS-1$ param.setAnnotation("The path and pattern of what files to return. Currently the only pattern supported is *.<ext>, which returns only the files matching the given extension at the given path."); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("file", TypeFacility.RUNTIME_NAMES.BLOB, p1); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("filePath", TypeFacility.RUNTIME_NAMES.STRING, p1); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("lastModified", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p1); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("created", TypeFacility.RUNTIME_NAMES.TIMESTAMP, p1); //$NON-NLS-1$ metadataFactory.addProcedureResultSetColumn("size", TypeFacility.RUNTIME_NAMES.LONG, p1); //$NON-NLS-1$ Procedure p2 = metadataFactory.addProcedure(SAVEFILE); p2.setAnnotation("Saves the given value to the given path. Any existing file will be overriden."); //$NON-NLS-1$ metadataFactory.addProcedureParameter("filePath", TypeFacility.RUNTIME_NAMES.STRING, Type.In, p2); //$NON-NLS-1$ param = metadataFactory.addProcedureParameter("file", TypeFacility.RUNTIME_NAMES.OBJECT, Type.In, p2); //$NON-NLS-1$ param.setAnnotation("The contents to save. Can be one of CLOB, BLOB, or XML"); //$NON-NLS-1$ Procedure p3 = metadataFactory.addProcedure(DELETEFILE); p3.setAnnotation("Delete the given file path. "); //$NON-NLS-1$
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 testTypeCorrection() throws Exception { MetadataFactory mf = new MetadataFactory(null, 1, "x", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null); //$NON-NLS-1$ Table t = mf.addTable("y"); //$NON-NLS-1$ mf.addColumn("test", "string", t); mf.addColumn("array", "string[]", t); Datatype unknown = new Datatype(); unknown.setName("unknown"); Column col = mf.addColumn("arg", "string", t); col.setDatatype(unknown, false, 0); MetadataFactory mf1 = UnitTestUtil.helpSerialize(mf); Column column = mf1.getSchema().getTable("y").getColumns().get(0); Datatype dt = column.getDatatype(); assertNotSame(mf.getDataTypes().get(dt.getName()), column.getDatatype()); assertEquals(1, mf1.getSchema().getTable("y").getColumns().get(1).getArrayDimensions()); mf1.correctDatatypes(mf.getDataTypes()); assertSame(mf.getDataTypes().get(dt.getName()), column.getDatatype()); assertEquals(1, mf1.getSchema().getTable("y").getColumns().get(1).getArrayDimensions()); }
ArrayList<String> pkColumns = new ArrayList<String>(); for (Column column:record.getColumns()) { Column c = metadataFactory.getSchema().getTable(childTable.getName()).getColumnByName(column.getName()); if (c == null) { c = metadataFactory.addColumn(column.getName(), column.getRuntimeType(), childTable); metadataFactory.addPrimaryKey("PK0", pkColumns, childTable); //$NON-NLS-1$ metadataFactory.addForeignKey("FK0", pkColumns, table.getName(), childTable); //$NON-NLS-1$ ArrayList<String> pkColumns = new ArrayList<String>(); for (Column column:record.getColumns()) { Column c = metadataFactory.getSchema().getTable(childTable.getName()).getColumnByName(table.getName()+"_"+column.getName()); //$NON-NLS-1$ if (c == null) { c = metadataFactory.addColumn(table.getName()+"_"+column.getName(), column.getRuntimeType(), childTable); //$NON-NLS-1$ metadataFactory.addForeignKey("FK0", pkColumns, table.getName(), childTable); //$NON-NLS-1$
dt.setJavaClassName(String.class.getCanonicalName()); datatypes.put(DataTypeManager.DefaultDataTypes.STRING, dt); MetadataFactory mf = new MetadataFactory(null, 1, "x", datatypes, new Properties(), null); //$NON-NLS-1$ mf.addProcedure("y"); //$NON-NLS-1$ Table t = mf.addTable("foo"); mf.addColumn("col", DataTypeManager.DefaultDataTypes.STRING, t); MetadataFactory mf1 = new MetadataFactory(null, 1, "x1", datatypes, new Properties(), null); //$NON-NLS-1$ mf1.addProcedure("y"); //$NON-NLS-1$ Table table = mf1.addTable("doc"); table.setSchemaPaths(Arrays.asList("../../x.xsd")); table.setResourcePath("/a/b/doc.xmi"); resources.put("/x.xsd", new VDBResources.Resource(VFS.getRootVirtualFile())); CompositeMetadataStore cms = new CompositeMetadataStore(Arrays.asList(mf.asMetadataStore(), mf1.asMetadataStore()));
void addNavigationRelations(MetadataFactory mf, String tableName, EdmEntityType fromEntity) throws TranslatorException { Table fromTable = mf.getSchema().getTable(tableName); Table toTable = mf.getSchema().getTable(nav.getName()); if (toTable == null) { if (mf.getSchema().getTable(association.getName()) == null) { Table linkTable = mf.addTable(association.getName()); linkTable.setProperty(ENTITY_TYPE, "LinkTable"); //$NON-NLS-1$ linkTable.setProperty(LINK_TABLES, fromTable.getName()+","+toTable.getName()); //$NON-NLS-1$ allKeys.add(c.getName()); mf.addPrimaryKey("PK", allKeys, linkTable); //$NON-NLS-1$ mf.addForeignKey(fromTable.getName() + "_FK", leftNames, fromTable.getName(), linkTable); //$NON-NLS-1$ mf.addForeignKey(toTable.getName() + "_FK", rightNames, toTable.getName(), linkTable); // //$NON-NLS-1$
List<String> domains = connection.getDomains(); for (String domain : domains) { Table table = metadataFactory.addTable(domain); table.setSupportsUpdate(true); Column itemName = metadataFactory.addColumn(DISPLAY_ITEM_NAME, TypeFacility.RUNTIME_NAMES.STRING, table); itemName.setUpdatable(true); itemName.setNameInSource(ITEM_NAME); itemName.setNullType(NullType.No_Nulls); metadataFactory.addPrimaryKey("PK0", Arrays.asList(DISPLAY_ITEM_NAME), table); //$NON-NLS-1$ column = metadataFactory.addColumn(attribute.getName(), TypeFacility.RUNTIME_NAMES.STRING+"[]", table); //$NON-NLS-1$ column = metadataFactory.addColumn(attribute.getName(), TypeFacility.RUNTIME_NAMES.STRING, table);
@Test public void testCorrectName() { ModelMetaData mmd = new ModelMetaData(); mmd.setName("foo"); HashMap<String, Datatype> types = new HashMap<String, Datatype>(); Datatype value = new Datatype(); value.setName("string"); types.put("string", value); MetadataFactory factory = new MetadataFactory("x", 1, types, mmd); Table x = factory.addTable("x"); Column c = factory.addColumn("a.b", "string", x); assertEquals("a_b", c.getName()); }
@Override public void loadMetadata(MetadataFactory factory, ExecutionFactory<Object, Object> executionFactory, Object connectionFactory) throws TranslatorException { Schema s = factory.getSchema(); for (Table t : s.getTables().values()) { if (!t.isPhysical()) { continue; } Column c = t.getColumnByName(multiSourceColumnName); if (c == null) { c = factory.addColumn(multiSourceColumnName, DataTypeManager.DefaultDataTypes.STRING, t); MultiSourceMetadataWrapper.setMultiSourceElementMetadata(c); } } outer: for (Procedure p : s.getProcedures().values()) { if (p.isVirtual()) { continue; } for (ProcedureParameter pp : p.getParameters()) { if (multiSourceColumnName.equalsIgnoreCase(pp.getName())) { continue outer; } } ProcedureParameter pp = factory.addProcedureParameter(multiSourceColumnName, DataTypeManager.DefaultDataTypes.STRING, Type.In, p); pp.setNullType(NullType.Nullable); } }
private Column addColumn(MetadataFactory mf, String name, String type, Table entityTable) throws TranslatorException { if (!columnExists(name, entityTable)) { Column c = mf.addColumn(name, type, entityTable); c.setUpdatable(true); return c; } return entityTable.getColumnByName(name); }
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; }
BaseColumn addProcColumn(MetadataFactory factory, Procedure proc, String name, ParsedDataType type, boolean rs) throws MetadataException { BaseColumn column = null; if (rs) { column = factory.addProcedureResultSetColumn(name, type.type, proc); } else { boolean added = false; for (ProcedureParameter pp : proc.getParameters()) { if (pp.getType() == Type.ReturnValue) { added = true; if (pp.getDatatype() != factory.getDataTypes().get(type.type)) { throw new MetadataException(QueryPlugin.Util.getString("SQLParser.proc_type_conflict", proc.getName(), pp.getDatatype(), type.type)); //$NON-NLS-1$ } } } if (!added) { column = factory.addProcedureParameter(name, type.type, ProcedureParameter.Type.ReturnValue, proc); } } setTypeInfo(type, column); return column; }
@Test public void testNameFormat() { ModelMetaData mmd = new ModelMetaData(); mmd.setName("foo"); mmd.addProperty("importer.nameFormat", "x_%s_y"); HashMap<String, Datatype> types = new HashMap<String, Datatype>(); MetadataFactory factory = new MetadataFactory("x", 1, types, mmd); Table x = factory.addTable("x"); assertEquals("x_x_y", x.getName()); Procedure p = factory.addProcedure("a%b.c"); assertEquals("x_a%b.c_y", p.getName()); }
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 getPrimaryKeys(MetadataFactory metadataFactory, DatabaseMetaData metadata, Collection<TableInfo> tables) throws SQLException { LogManager.logDetail(LogConstants.CTX_CONNECTOR, "JDBCMetadataProcessor - Importing primary keys"); //$NON-NLS-1$ for (TableInfo tableInfo : tables) { ResultSet pks = metadata.getPrimaryKeys(tableInfo.catalog, tableInfo.schema, tableInfo.name); TreeMap<Short, String> keyColumns = null; String pkName = null; while (pks.next()) { String columnName = pks.getString(4); short seqNum = safeGetShort(pks, 5); if (keyColumns == null) { keyColumns = new TreeMap<Short, String>(); } keyColumns.put(seqNum, columnName); if (pkName == null) { pkName = pks.getString(6); if (pkName == null) { pkName = "PK_" + tableInfo.table.getName().toUpperCase(); //$NON-NLS-1$ } } } if (keyColumns != null) { metadataFactory.addPrimaryKey(pkName, new ArrayList<String>(keyColumns.values()), tableInfo.table); } pks.close(); } }
private Table addNavigationAsTable(MetadataFactory mf, XMLMetadata metadata, Table fromTable, CsdlNavigationProperty property) throws TranslatorException { String name = join(fromTable.getName(), NAME_SEPARATOR, property.getName()); Table toTable = addTable(mf, name, property.getType(), property.isCollection()?ODataType.NAVIGATION_COLLECTION:ODataType.NAVIGATION, metadata); toTable.setNameInSource(property.getName()); KeyRecord pk = fromTable.getPrimaryKey(); List<String> columnNames = new ArrayList<String>(); for (Column c : pk.getColumns()) { String columnName = join(fromTable.getName(), NAME_SEPARATOR, c.getName()); Column column = mf.addColumn(columnName, c.getRuntimeType(), toTable); column.setProperty(PSEUDO, String.valueOf(Boolean.TRUE)); columnNames.add(columnName); } mf.addForeignKey("FK0", columnNames, getColumnNames(pk.getColumns()), fromTable.getFullName(), toTable); return toTable; }