@Test public void synonyms() throws Exception { final String[] classes = { "MutableTable", "MutableTable", "MutableTable", "", "", "", }; final String[] synonymNames = { "AUTHORS", "BOOKAUTHORS", "BOOKS", "\"Global Counts\"", "No_Columns", "PUBLICATIONS", }; final InformationSchemaViews informationSchemaViews = new InformationSchemaViews(); informationSchemaViews.setSynonymSql( "SELECT LIMIT 1 3 \n" + " TABLE_CATALOG AS SYNONYM_CATALOG, \n" + " TABLE_SCHEMA AS SYNONYM_SCHEMA, \n" + " TABLE_NAME AS SYNONYM_NAME, \n" + " TABLE_CATALOG AS REFERENCED_OBJECT_CATALOG, \n" + " TABLE_SCHEMA AS REFERENCED_OBJECT_SCHEMA, \n" + " TABLE_NAME AS REFERENCED_OBJECT_NAME \n" + "FROM \n" + " INFORMATION_SCHEMA.TABLES \n" + "WHERE \n" + " TABLE_SCHEMA = 'BOOKS' \n" + "UNION \n" + "SELECT LIMIT 1 3 \n" + " 'PUBLIC' AS SYNONYM_CATALOG, \n" + " 'BOOKS' AS SYNONYM_SCHEMA, \n" + " TABLE_NAME AS SYNONYM_NAME, \n" + " TABLE_CATALOG AS REFERENCED_OBJECT_CATALOG, \n" + " TABLE_SCHEMA AS REFERENCED_OBJECT_SCHEMA, \n" + " TABLE_NAME + '1' AS REFERENCED_OBJECT_NAME \n" + "FROM \n" + " INFORMATION_SCHEMA.TABLES \n" + "WHERE \n" + " TABLE_SCHEMA != 'BOOKS' "); final SchemaInfoLevel minimum = SchemaInfoLevel.minimum(); minimum.setRetrieveSynonymInformation(true); final SchemaCrawlerOptions schemaCrawlerOptions = new SchemaCrawlerOptions(); schemaCrawlerOptions.setSchemaInfoLevel(minimum); schemaCrawlerOptions.setInformationSchemaViews(informationSchemaViews); schemaCrawlerOptions.setSynonymInclusionRule(InclusionRule.INCLUDE_ALL); final Database database = getDatabase(schemaCrawlerOptions); final Schema schema = database.getSchema("PUBLIC.BOOKS"); assertNotNull("BOOKS Schema not found", schema); final Synonym[] synonyms = database.getSynonyms(schema).toArray(new Synonym[0]); assertEquals("Synonym count does not match", 6, synonyms.length); for (int i = 0; i < synonyms.length; i++) { final Synonym synonym = synonyms[i]; assertNotNull(synonym); assertEquals( "Wrong referenced object class - " + synonym.getReferencedObject().getClass(), classes[i], synonym.getReferencedObject().getClass().getSimpleName()); assertEquals("", synonymNames[i], synonym.getName()); } }
@Test public void schemaCounts() throws Exception { final String[] dataSources = { "MicrosoftSQLServer", "MySQL", "Oracle", "PostgreSQL", "SQLite", }; final int[] catalogCounts = { 4, 4, 1, 1, 1, }; final int[][] schemaCounts = { { 5, 5, 5, 5, }, { 1, 1, 1, 1, }, { 14, }, { 5, }, { 1, }, }; final SchemaCrawlerOptions schemaCrawlerOptions = createOptions(".*", ".*"); final SchemaInfoLevel infoLevel = SchemaInfoLevel.minimum(); infoLevel.setRetrieveTables(false); infoLevel.setRetrieveProcedures(false); schemaCrawlerOptions.setSchemaInfoLevel(infoLevel); for (int i = 0; i < dataSources.length; i++) { final String dataSource = dataSources[i]; final Database database = retrieveDatabase(dataSource, schemaCrawlerOptions); final Catalog[] catalogs = database.getCatalogs(); assertEquals( "Incorrect number of catalogs for " + dataSource, catalogCounts[i], catalogs.length); for (int j = 0; j < catalogs.length; j++) { final Catalog catalog = catalogs[j]; final Schema[] schemas = catalog.getSchemas(); assertEquals( "Incorrect number of schemas for " + dataSource + " catalog #" + j, schemaCounts[i][j], schemas.length); } } }