public static Set<org.talend.core.model.metadata.builder.connection.MetadataTable> getTablesFromCurrentCatalogOrSchema(String dbsid, String schema, DatabaseConnection dbconn) { Set<org.talend.core.model.metadata.builder.connection.MetadataTable> allTables = new HashSet<org.talend.core.model.metadata.builder.connection.MetadataTable>(); /* context model show all tables */ if (dbconn.isContextMode()) { allTables = ConnectionHelper.getTables(dbconn); } else { boolean hasSchemaInCatalog = false; Catalog c = (Catalog) ConnectionHelper.getPackage(dbsid, dbconn, Catalog.class); Schema s = (Schema) ConnectionHelper.getPackage(schema, dbconn, Schema.class); List<Schema> subschemas = new ArrayList<Schema>(); if (c != null) { subschemas = CatalogHelper.getSchemas(c); hasSchemaInCatalog = subschemas.size() > 0; } if (c != null && s == null && !hasSchemaInCatalog) { // only catalog PackageHelper.getAllTables(c, allTables); // PackageHelper.addMetadataTable(dbtable, c); } else if (s != null && !hasSchemaInCatalog && c == null) { // only schema PackageHelper.getAllTables(s, allTables); // PackageHelper.addMetadataTable(dbtable, s); } else if (c != null && hasSchemaInCatalog) { // both schema and catalog subschemas = CatalogHelper.getSchemas(c); hasSchemaInCatalog = subschemas.size() > 0; if (subschemas.size() > 0) { for (Schema current : subschemas) { if (current.getName() == null) { /* if the current schema no name should set an empty string for name, bug 17244 */ current.setName(""); } if (current.getName().equals(schema)) { s = current; break; } } /** * if dont specifc a schema because of getUiSchema() is null,show all cataogs table by * default,or it will cause bug 0016578 */ if (s == null || "".equals(s)) { // allTables = ConnectionHelper.getTables(dbconn); PackageHelper.getAllTables(c, allTables); } else { PackageHelper.getAllTables(s, allTables); } // PackageHelper.addMetadataTable(dbtable, s); } } else { // return nothing } } return allTables; }
public static void removeTablesFromCurrentCatalogOrSchema( String dbsid, String schema, DatabaseConnection dbconn, Collection<? extends MetadataTable> tablesToDelete) { boolean hasSchemaInCatalog = false; Catalog c = (Catalog) ConnectionHelper.getPackage(dbsid, dbconn, Catalog.class); Schema s = (Schema) ConnectionHelper.getPackage(schema, dbconn, Schema.class); List<Schema> subschemas = new ArrayList<Schema>(); if (c != null) { subschemas = CatalogHelper.getSchemas(c); hasSchemaInCatalog = subschemas.size() > 0; } if (c != null && s == null && !hasSchemaInCatalog) { // only catalog c.getOwnedElement().removeAll(tablesToDelete); } else if (s != null && !hasSchemaInCatalog && c == null) { // only schema s.getOwnedElement().removeAll(tablesToDelete); // PackageHelper.addMetadataTable(dbtable, s); } else if (c != null && hasSchemaInCatalog) { // both schema and catalog subschemas = CatalogHelper.getSchemas(c); hasSchemaInCatalog = subschemas.size() > 0; if (subschemas.size() > 0) { for (Schema current : subschemas) { if (current.getName().equals(schema)) { s = current; break; } } /** * if dont specifc a schema because of getUiSchema() is null,show all cataogs table by * default,or it will cause bug 0016578 */ if (s == null || "".equals(s)) { // allTables = ConnectionHelper.getTables(dbconn); // added for bug 17467 // set db connection's schema as null, and retrieve schema again, to remove some tables EList<ModelElement> ownedElement = c.getOwnedElement(); if (ownedElement instanceof EObjectContainmentWithInverseEList) { EObjectContainmentWithInverseEList elist = (EObjectContainmentWithInverseEList) ownedElement; if (!elist.isEmpty() && elist.size() > 0) { for (int i = 0; i < elist.size(); i++) { Object object = elist.get(i); if (object instanceof SchemaImpl) { SchemaImpl schemaImpl = (SchemaImpl) object; EList<ModelElement> ownedElement2 = schemaImpl.getOwnedElement(); ownedElement2.removeAll(tablesToDelete); } } } } ownedElement.removeAll(tablesToDelete); } else { s.getOwnedElement().removeAll(tablesToDelete); } // PackageHelper.addMetadataTable(dbtable, s); } } else { // return nothing } }
private static void addTableForTemCatalogOrSchema( String dbsid, String schema, DatabaseConnection connection, MetadataTable dbtable, IMetadataConnection iMetadataConnection, int stackCount) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { if (3 < stackCount) { // if loop count is more than 3 times, seems it will never end this loop, and will never get // the dbsid and // schema return; } boolean hasSchemaInCatalog = false; boolean isAccess = EDatabaseTypeName.ACCESS.getDisplayName().equals(iMetadataConnection.getDbType()); Catalog c = (Catalog) ConnectionHelper.getPackage(dbsid, connection, Catalog.class); Schema s = (Schema) ConnectionHelper.getPackage(schema, connection, Schema.class); List<Schema> subschemas = new ArrayList<Schema>(); if (c != null) { subschemas = CatalogHelper.getSchemas(c); hasSchemaInCatalog = subschemas.size() > 0; } if (c != null && s == null && !hasSchemaInCatalog) { // only catalog PackageHelper.addMetadataTable(dbtable, c); } else if (s != null && !hasSchemaInCatalog && c == null) { // only schema PackageHelper.addMetadataTable(dbtable, s); } else if (c != null && hasSchemaInCatalog) { // both schema and catalog subschemas = CatalogHelper.getSchemas(c); hasSchemaInCatalog = subschemas.size() > 0; if (subschemas.size() > 0) { for (Schema current : subschemas) { if (current.getName().equals(schema)) { s = current; break; } } if (s != null) { // for bug 16794 if (s instanceof SchemaImpl) { SchemaImpl schemaElement = (SchemaImpl) s; EList<ModelElement> ownedElement = schemaElement.getOwnedElement(); ownedElement.add(dbtable); } } else if (subschemas.size() > 0) { // added for bug 17467 // set db connection's schema as null, and retrieve schema again, to add some tables for (int i = 0; i < subschemas.size(); i++) { SchemaImpl schemaElement = (SchemaImpl) subschemas.get(i); EList<ModelElement> ownedElement = schemaElement.getOwnedElement(); ownedElement.add(dbtable); } } // PackageHelper.addMetadataTable(dbtable, s); } } else if (s == null && c == null && !isAccess && stackCount == 1) { // TDI-20584:after migration from 4.0 to // 4.2,lost all catalogs // and schemas for database // in case after migration connetion from the version before 4.0,there is no any db structure // on // temConnection,it will casue pbs,so sychronize with imetadataConnection fillCatalogAndSchemas(iMetadataConnection, connection); addTableForTemCatalogOrSchema( dbsid, schema, connection, dbtable, iMetadataConnection, stackCount + 1); } else { /* * if there is no catalog or schema,create the structure correctly rather than always create a catalog,found * this issue when fixing bug 16636 */ ProjectNodeHelper.addCatalogOrSchema(iMetadataConnection, connection); if (isAccess) { addTableForTemCatalogOrSchema( dbsid, connection.getName(), connection, dbtable, iMetadataConnection, stackCount + 1); } else { addTableForTemCatalogOrSchema( dbsid, schema, connection, dbtable, iMetadataConnection, stackCount + 1); } } }
private static void fillValuesForSchemaOrCatalog( EDatabaseSchemaOrCatalogMapping catalog, EDatabaseSchemaOrCatalogMapping schema, IMetadataConnection metadataConnection, DatabaseConnection dbconn) { Schema s = null; Catalog c = null; List<Schema> schemas = new ArrayList<Schema>(); String user = metadataConnection.getUsername(); String defaultname = dbconn.getName(); String dbsid = metadataConnection.getDatabase(); String dbuischema = metadataConnection.getSchema(); if (schema != null && catalog != null) { if (schema.equals(EDatabaseSchemaOrCatalogMapping.None) && !catalog.equals(EDatabaseSchemaOrCatalogMapping.None)) { // only // catalog if (catalog.equals(EDatabaseSchemaOrCatalogMapping.Sid)) { c = CatalogHelper.createCatalog(dbsid); c.getDataManager().add(dbconn); ConnectionHelper.addCatalog(c, dbconn); } } else if (!schema.equals(EDatabaseSchemaOrCatalogMapping.None) // only schema && catalog.equals(EDatabaseSchemaOrCatalogMapping.None)) { if (schema.equals(EDatabaseSchemaOrCatalogMapping.Schema)) { s = SchemaHelper.createSchema(dbuischema); s.getDataManager().add(dbconn); ConnectionHelper.addSchema(s, dbconn); } if (schema.equals(EDatabaseSchemaOrCatalogMapping.Login)) { s = SchemaHelper.createSchema(user); s.getDataManager().add(dbconn); ConnectionHelper.addSchema(s, dbconn); } if (schema.equals( EDatabaseSchemaOrCatalogMapping.Default_Name)) { // for databases like access s = SchemaHelper.createSchema(defaultname); s.getDataManager().add(dbconn); ConnectionHelper.addSchema(s, dbconn); } } else { // both schema and catalog String cvalue = dbsid; String svalue = null; cvalue = dbsid; switch (schema) { case Sid: svalue = dbsid; break; case Schema: svalue = dbuischema; break; case Login: svalue = user; break; } c = CatalogHelper.createCatalog(cvalue); s = SchemaHelper.createSchema(svalue); schemas.add(s); CatalogHelper.addSchemas(schemas, c); c.getDataManager().add(dbconn); ConnectionHelper.addCatalog(c, dbconn); } } }
/** * wzhang Comment method "addDefaultTableForSpecifiedDataPackage". this function only for add * metadataTable. * * @param dbconn * @param dbtable * @throws SQLException * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */ public static void addDefaultTableForSpecifiedDataPackage( DatabaseConnection dbconn, MetadataTable dbtable) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { // if the database connection is contextmodel, need to get the original value of every parameter ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance(); IMetadataConnection imetadataConnection = ConvertionHelper.convert(dbconn); String schema = imetadataConnection.getSchema(); String catalog = imetadataConnection.getDatabase(); String databaseType = imetadataConnection.getDbType(); EDatabaseTypeName currentType = EDatabaseTypeName.getTypeFromDbType(databaseType); // IDBMetadataProvider extractor = // ExtractMetaDataFromDataBase.getProviderByDbType(databaseType); // if (extractor != null && currentType.isUseProvider()) { // catalog = extractor.getDefaultCatalogName(); // } EDatabaseSchemaOrCatalogMapping curCatalog = currentType.getCatalogMappingField(); EDatabaseSchemaOrCatalogMapping curSchema = currentType.getSchemaMappingField(); if (curCatalog != null && curSchema != null) { switch (curCatalog) { case Login: catalog = imetadataConnection.getUsername(); break; case None: catalog = ""; break; } switch (curSchema) { case Login: schema = imetadataConnection.getUsername(); break; case Schema: schema = imetadataConnection.getSchema(); break; case None: schema = ""; break; case Default_Name: schema = dbconn.getName(); // label for default name for // access or such kind of // non-catalogs databases break; } } boolean isAccess = EDatabaseTypeName.ACCESS.getDisplayName().equals(imetadataConnection.getDbType()); if (!isAccess) { schema = extractMeta.getMeataConnectionSchema(imetadataConnection); } // for olap connection boolean isOlap = extractMeta.isOLAPConnection(dbconn); if (isOlap) { List<Catalog> catalogs = ConnectionHelper.getCatalogs(dbconn); if (!catalogs.isEmpty()) { Catalog c = catalogs.get(0); catalog = c.getName(); if (!CatalogHelper.getSchemas(c).isEmpty()) { Schema s = CatalogHelper.getSchemas(c).get(0); schema = s.getName(); } } } addTableForTemCatalogOrSchema(catalog, schema, dbconn, dbtable, imetadataConnection); }