/**
   * 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());
  }
Пример #2
0
  private PrimaryKeyInfo bindPrimaryKeyToProperties(
      Table table, RootClass rc, Set processed, Mapping mapping, DatabaseCollector collector) {
    SimpleValue id = null;
    String idPropertyname = null;

    PrimaryKeyInfo pki = new PrimaryKeyInfo();

    List keyColumns = null;
    if (table.getPrimaryKey() != null) {
      keyColumns = table.getPrimaryKey().getColumns();
    } else {
      log.debug("No primary key found for " + table + ", using all properties as the identifier.");
      keyColumns = new ArrayList();
      Iterator iter = table.getColumnIterator();
      while (iter.hasNext()) {
        Column col = (Column) iter.next();
        keyColumns.add(col);
      }
    }

    final TableIdentifier tableIdentifier = TableIdentifier.create(table);

    String tableIdentifierStrategyName = "assigned";

    boolean naturalId;

    if (keyColumns.size() > 1) {
      log.debug(
          "id strategy for " + rc.getEntityName() + " since it has a multiple column primary key");
      naturalId = true;

      id = handleCompositeKey(rc, processed, keyColumns, mapping);
      idPropertyname = revengStrategy.tableToIdentifierPropertyName(tableIdentifier);
      if (idPropertyname == null) {
        idPropertyname = "id";
      }
    } else {
      pki.suggestedStrategy = revengStrategy.getTableIdentifierStrategyName(tableIdentifier);
      String suggestedStrategy = pki.suggestedStrategy;
      if (suggestedStrategy == null) {
        suggestedStrategy =
            collector.getSuggestedIdentifierStrategy(
                tableIdentifier.getCatalog(),
                tableIdentifier.getSchema(),
                tableIdentifier.getName());
        if (suggestedStrategy == null) {
          suggestedStrategy = "assigned";
        }
        tableIdentifierStrategyName = suggestedStrategy;
      } else {
        tableIdentifierStrategyName = suggestedStrategy;
      }

      naturalId = "assigned".equals(tableIdentifierStrategyName);
      Column pkc = (Column) keyColumns.get(0);
      checkColumn(pkc);

      id = bindColumnToSimpleValue(table, pkc, mapping, !naturalId);

      idPropertyname = revengStrategy.tableToIdentifierPropertyName(tableIdentifier);
      if (idPropertyname == null) {
        idPropertyname = revengStrategy.columnToPropertyName(tableIdentifier, pkc.getName());
      }

      processed.add(pkc);
    }
    id.setIdentifierGeneratorStrategy(tableIdentifierStrategyName);
    pki.suggestedProperties = revengStrategy.getTableIdentifierProperties(tableIdentifier);
    id.setIdentifierGeneratorProperties(pki.suggestedProperties);
    if (naturalId) {
      id.setNullValue("undefined");
    }

    Property property =
        makeProperty(
            tableIdentifier, makeUnique(rc, idPropertyname), id, true, true, false, null, null);
    rc.setIdentifierProperty(property);
    rc.setIdentifier(id);

    return pki;
  }
Пример #3
0
  /**
   * @param manyToOneCandidates
   * @param mappings2
   */
  private void createPersistentClasses(DatabaseCollector collector, Mapping mapping) {
    Map manyToOneCandidates = collector.getOneToManyCandidates();

    for (Iterator iter = mappings.iterateTables(); iter.hasNext(); ) {
      Table table = (Table) iter.next();
      if (table.getColumnSpan() == 0) {
        log.warn("Cannot create persistent class for " + table + " as no columns were found.");
        continue;
      }
      // TODO: this naively just create an entity per table
      // should have an opt-out option to mark some as helper tables, subclasses etc.
      /*if(table.getPrimaryKey()==null || table.getPrimaryKey().getColumnSpan()==0) {
      log.warn("Cannot create persistent class for " + table + " as no primary key was found.");
               continue;
               // TODO: just create one big embedded composite id instead.
           }*/

      if (revengStrategy.isManyToManyTable(table)) {
        log.debug("Ignoring " + table + " as class since rev.eng. says it is a many-to-many");
        continue;
      }

      RootClass rc = new RootClass();
      TableIdentifier tableIdentifier = TableIdentifier.create(table);
      String className = revengStrategy.tableToClassName(tableIdentifier);
      log.debug("Building entity " + className + " based on " + tableIdentifier);
      rc.setEntityName(className);
      rc.setClassName(className);
      rc.setProxyInterfaceName(rc.getEntityName()); // TODO: configurable ?
      rc.setLazy(true);

      rc.setMetaAttributes(safeMeta(revengStrategy.tableToMetaAttributes(tableIdentifier)));

      rc.setDiscriminatorValue(rc.getEntityName());
      rc.setTable(table);
      try {
        mappings.addClass(rc);
      } catch (DuplicateMappingException dme) {
        // TODO: detect this and generate a "permutation" of it ?
        PersistentClass class1 = mappings.getClass(dme.getName());
        Table table2 = class1.getTable();
        throw new JDBCBinderException(
            "Duplicate class name '"
                + rc.getEntityName()
                + "' generated for '"
                + table
                + "'. Same name where generated for '"
                + table2
                + "'");
      }
      mappings.addImport(rc.getEntityName(), rc.getEntityName());

      Set processed = new HashSet();

      PrimaryKeyInfo pki = bindPrimaryKeyToProperties(table, rc, processed, mapping, collector);
      bindColumnsToVersioning(table, rc, processed, mapping);
      bindOutgoingForeignKeys(table, rc, processed);
      bindColumnsToProperties(table, rc, processed, mapping);
      List incomingForeignKeys = (List) manyToOneCandidates.get(rc.getEntityName());
      bindIncomingForeignKeys(rc, processed, incomingForeignKeys, mapping);
      updatePrimaryKey(rc, pki);
    }
  }