/**
   * There are 2 solutions: 1. DatabaseCollector#addTable()/getTable() should be called for not
   * quoted parameters - I think it is preferable way. 2. DatabaseCollector#addTable()/getTable()
   * should be called for quoted parameters - here users should use the same quotes as JDBCReader.
   * Because of this there are 2 opposite methods(and they are both failed as addTable uses quoted
   * names but getTable uses non-quoted names )
   */
  public void testQuotedNamesAndDefaultDatabaseCollector() {
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    ServiceRegistry serviceRegistry = builder.build();

    MetaDataDialect realMetaData =
        MetaDataDialectFactory.createMetaDataDialect(
            serviceRegistry.getService(JdbcServices.class).getDialect(), cfg.getProperties());

    JDBCReader reader =
        JDBCReaderFactory.newJDBCReader(
            cfg.getProperties(),
            new DefaultReverseEngineeringStrategy(),
            realMetaData,
            serviceRegistry);

    DatabaseCollector dc = new DefaultDatabaseCollector(reader.getMetaDataDialect());
    reader.readDatabaseSchema(dc, null, SCHEMA);

    assertNotNull("The table should be found", dc.getTable(SCHEMA, "PUBLIC", TABLE1));
    assertNotNull("The table should be found", dc.getTable(SCHEMA, "PUBLIC", TABLE2));
    assertNull(
        "Quoted names should not return the table", dc.getTable(quote(SCHEMA), "PUBLIC", QTABLE1));
    assertNull(
        "Quoted names should not return the table", dc.getTable(quote(SCHEMA), "PUBLIC", QTABLE2));

    assertEquals("Foreign key 'masterref' was filtered!", 1, dc.getOneToManyCandidates().size());
  }
Exemple #2
0
  /**
   * Read JDBC Metadata from the database. Does not create any classes or other ORM releated
   * structures.
   *
   * @param catalog
   * @param schema
   * @return
   * @throws SQLException
   */
  public DatabaseCollector readDatabaseSchema(String catalog, String schema) throws SQLException {
    // use default from settings if nothing else specified.
    catalog = catalog != null ? catalog : settings.getDefaultCatalogName();
    schema = schema != null ? schema : settings.getDefaultSchemaName();

    JDBCReader reader =
        JDBCReaderFactory.newJDBCReader(cfg.getProperties(), settings, revengStrategy);
    DatabaseCollector dbs = new MappingsDatabaseCollector(mappings, reader.getMetaDataDialect());
    reader.readDatabaseSchema(dbs, catalog, schema);
    return dbs;
  }
  public void visit(Configuration cfg, Table table, IssueCollector pc) {

    if (table.isPhysicalTable()) {
      setSchemaSelection(table);

      List list = reader.readDatabaseSchema(dbc, null, null);

      if (list.isEmpty()) {
        pc.reportIssue(
            new Issue(
                "SCHEMA_TABLE_MISSING",
                Issue.HIGH_PRIORITY,
                "Missing table "
                    + Table.qualify(table.getCatalog(), table.getSchema(), table.getName())));
        return;
      } else if (list.size() > 1) {
        pc.reportIssue(
            new Issue(
                "SCHEMA_TABLE_MISSING",
                Issue.NORMAL_PRIORITY,
                "Found "
                    + list.size()
                    + " tables for "
                    + Table.qualify(table.getCatalog(), table.getSchema(), table.getName())));
        return;
      } else {
        currentDbTable = (Table) list.get(0);
        visitColumns(cfg, table, pc);
      }
    } else {
      // log?
    }
  }
  public void initialize(Configuration cfg, ServiceRegistry serviceRegistry, Settings settings) {

    super.initialize(cfg, serviceRegistry, settings);
    JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
    dialect = jdbcServices.getDialect();

    tableSelector = new TableSelectorStrategy(new DefaultReverseEngineeringStrategy());
    reader = JDBCReaderFactory.newJDBCReader(serviceRegistry, settings, tableSelector);
    dbc = new DefaultDatabaseCollector(reader.getMetaDataDialect());
    mapping = cfg.buildMapping();
  }
 private boolean isTable(Object key) throws HibernateException {
   // BIG HACK - should probably utilize the table cache before going to the jdbcreader :(
   if (key instanceof String) {
     String[] strings = StringHelper.split(".", (String) key);
     if (strings.length == 1) {
       tableSelector.clearSchemaSelections();
       tableSelector.addSchemaSelection(new SchemaSelection(null, null, strings[0]));
       List list = reader.readDatabaseSchema(dbc, null, null);
       return !list.isEmpty();
     } else if (strings.length == 3) {
       tableSelector.clearSchemaSelections();
       tableSelector.addSchemaSelection(new SchemaSelection(strings[0], strings[1], strings[2]));
       List list = reader.readDatabaseSchema(dbc, null, null);
       return !list.isEmpty();
     } else if (strings.length == 2) {
       tableSelector.clearSchemaSelections();
       tableSelector.addSchemaSelection(new SchemaSelection(null, strings[0], strings[1]));
       List list = reader.readDatabaseSchema(dbc, null, null);
       return !list.isEmpty();
     }
   }
   return false;
 }
  public void visitGenerators(Configuration cfg, IssueCollector collector) {
    Iterator iter = iterateGenerators(cfg);

    Set sequences = Collections.EMPTY_SET;
    if (dialect.supportsSequences()) {
      sequences = reader.readSequences(dialect.getQuerySequencesString());
    }

    // TODO: move this check into something that could check per class or collection instead.
    while (iter.hasNext()) {
      PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next();
      Object key = generator.generatorKey();
      if (!isSequence(key, sequences) && !isTable(key)) {
        collector.reportIssue(
            new Issue(
                "MISSING_ID_GENERATOR", Issue.HIGH_PRIORITY, "Missing sequence or table: " + key));
      }
    }
  }