Esempio n. 1
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;
  }
  public synchronized void create(String... primaryKeyNames) {
    PrimaryKeyInfo<B> primaryKeyInfo =
        PrimaryKeyInfo.getInstance(beanClass, primaryKeyNames, referencedFactories);
    Property[] primaryKeyProperties = primaryKeyInfo.getProperties();
    Property[] properties =
        Property.deriveProperties(beanClass, primaryKeyInfo, referencedFactories);

    String primaryKeyColumnNamesCommaSeparated =
        appendColumnNamesCommaSeparated(new StringBuffer(), primaryKeyInfo.getProperties())
            .toString();

    // Make main table
    StringBuffer b = new StringBuffer();
    b.append("create table ");
    b.append(tableName);
    b.append(" (");
    for (int i = 0; i < properties.length; i++) {
      if (i > 0) b.append(", ");
      Property prop = properties[i];
      if (prop.isPrimaryKeyProperty()
          && prop.getType() == int.class
          && primaryKeyInfo.getProperties().length == 1) {
        b.append(prop.getName());
        b.append(" INT NOT NULL AUTO_INCREMENT");
      } else if (prop.isPrimaryKeyProperty()
          && prop.getType() == long.class
          && primaryKeyInfo.getProperties().length == 1) {
        b.append(prop.getName());
        b.append(" BIGINT NOT NULL AUTO_INCREMENT");
      } else if (prop.isArray()) {
        b.append(prop.getName());
        b.append(" BOOLEAN NOT NULL DEFAULT 0");
      } else {
        String[] columnNames = prop.getColumnNames();
        Class<?>[] columnTypes = prop.getColumnTypes();
        int[] columnStrLens = prop.getColumnMaxStrLens();
        for (int j = 0; j < columnNames.length; j++) {
          if (j > 0) b.append(", ");
          b.append(columnNames[j]);
          b.append(' ');
          b.append(javaToSql(columnTypes[j], prop, columnStrLens[j]));
        }
      }
    }

    if (primaryKeyProperties.length > 0) {
      b.append(", PRIMARY KEY(");
      b.append(primaryKeyColumnNamesCommaSeparated);
      b.append(')');
    }

    b.append(')');

    Connection con;
    try {
      con = connectionPool.getConnection();
    } catch (ConnectionException e) {
      throw new BeanFactoryException(e);
    }

    try {
      Statement stmt = con.createStatement();
      if (printSQL != null) printDebug("createTable: " + b);
      stmt.executeUpdate(b.toString());
      stmt.close();
    } catch (SQLException e) {
      try {
        con.close();
      } catch (SQLException e2) {
        /* ignore */
      }
      throw new BeanFactoryException(
          "Error creating table \"" + tableName + "\": " + e.getMessage());
    }

    // If there are any array columns, make auxiliary tables to hold their data
    for (Property prop : properties) {
      if (prop.isArray()) {
        try {
          Statement stmt = con.createStatement();
          StringBuffer sql = new StringBuffer();
          sql.append("CREATE TABLE ").append(tableName).append('_');
          sql.append(prop.getName().toLowerCase()).append(" (");
          for (int i = 0; i < primaryKeyProperties.length; i++) {
            if (i > 0) sql.append(", ");
            String[] priKeyColumnNames = primaryKeyProperties[i].getColumnNames();
            Class<?>[] priKeyColumnTypes = primaryKeyProperties[i].getColumnTypes();
            int[] priKeyColStrLens = primaryKeyProperties[i].getColumnMaxStrLens();
            for (int j = 0; j < priKeyColumnNames.length; j++) {
              if (j > 0) sql.append(", ");
              sql.append(priKeyColumnNames[j]);
              sql.append(' ');
              sql.append(
                  javaToSql(priKeyColumnTypes[j], primaryKeyProperties[i], priKeyColStrLens[j]));
            }
          }
          sql.append(", ");
          sql.append(ARRAY_POS_COLUMN_NAME);
          sql.append(" INTEGER NOT NULL");
          String[] columnNames = prop.getColumnNames();
          Class<?>[] columnTypes = prop.getColumnTypes();
          int[] columnStrLens = prop.getColumnMaxStrLens();
          for (int j = 0; j < columnNames.length; j++) {
            sql.append(", ");
            sql.append(columnNames[j]);
            sql.append(' ');
            sql.append(javaToSql(columnTypes[j], prop, columnStrLens[j]));
          }
          sql.append(", PRIMARY KEY(");
          sql.append(primaryKeyColumnNamesCommaSeparated);
          sql.append(',');
          sql.append(ARRAY_POS_COLUMN_NAME);
          sql.append("))");
          if (printSQL != null) printDebug("createTable: " + sql);
          stmt.executeUpdate(sql.toString());
          stmt.close();
        } catch (SQLException e) {
          try {
            con.close();
          } catch (SQLException e2) {
            /* ignore */
          }
          throw new BeanFactoryException(
              "Error creating auxiliary table \""
                  + tableName
                  + '_'
                  + prop.getName()
                  + "\": "
                  + e.getMessage());
        }
      }
    }
    connectionPool.releaseConnection(con);
  }