Beispiel #1
0
 /** Shallow copy, the value is not copied */
 protected Object clone() {
   Column copy = new Column();
   copy.setLength(length);
   copy.setScale(scale);
   copy.setValue(value);
   copy.setTypeIndex(typeIndex);
   copy.setName(getQuotedName());
   copy.setNullable(nullable);
   copy.setPrecision(precision);
   copy.setUnique(unique);
   copy.setSqlType(sqlType);
   copy.setSqlTypeCode(sqlTypeCode);
   copy.uniqueInteger = uniqueInteger; // usually useless
   copy.setCheckConstraint(checkConstraint);
   copy.setComment(comment);
   copy.setDefaultValue(defaultValue);
   copy.setCustomRead(customRead);
   copy.setCustomWrite(customWrite);
   return copy;
 }
  public static void processBasicColumns(
      MetaDataDialect metaDataDialect,
      ReverseEngineeringStrategy revengStrategy,
      String defaultSchema,
      String defaultCatalog,
      Table table,
      ProgressListener progress) {

    String qualify =
        TableNameQualifier.qualify(table.getCatalog(), table.getSchema(), table.getName());
    Iterator<?> columnIterator = null;

    try {
      Map<?, ?> columnRs = null;
      log.debug("Finding columns for " + qualify);
      progress.startSubTask("Finding columns for " + qualify);
      columnIterator =
          metaDataDialect.getColumns(
              getCatalogForDBLookup(table.getCatalog(), defaultCatalog),
              getSchemaForDBLookup(table.getSchema(), defaultSchema),
              table.getName(),
              null);
      // dumpHeader(columnRs);
      while (columnIterator.hasNext()) {
        // dumpRow(columnRs);
        columnRs = (Map<?, ?>) columnIterator.next();
        String tableName = (String) columnRs.get("TABLE_NAME");
        int sqlType = ((Integer) columnRs.get("DATA_TYPE")).intValue();
        // String sqlTypeName = (String) columnRs.get("TYPE_NAME");
        String columnName = (String) columnRs.get("COLUMN_NAME");
        String comment = (String) columnRs.get("REMARKS");

        TableIdentifier ti =
            RevEngUtils.createTableIdentifier(table, defaultCatalog, defaultSchema);
        if (revengStrategy.excludeColumn(ti, columnName)) {
          log.debug("Column " + ti + "." + columnName + " excluded by strategy");
          continue;
        }
        if (!tableName.equals(table.getName())) {
          log.debug(
              "Table name "
                  + tableName
                  + " does not match requested "
                  + table.getName()
                  + ". Ignoring column "
                  + columnName
                  + " since it either is invalid or a duplicate");
          continue;
        }

        // String columnDefaultValue = columnRs.getString("COLUMN_DEF"); TODO: only read if have a
        // way to avoid issues with clobs/lobs and similar
        int dbNullability = ((Integer) columnRs.get("NULLABLE")).intValue();
        boolean isNullable = true;
        switch (dbNullability) {
          case DatabaseMetaData.columnNullable:
          case DatabaseMetaData.columnNullableUnknown:
            isNullable = true;
            break;
          case DatabaseMetaData.columnNoNulls:
            isNullable = false;
            break;
          default:
            isNullable = true;
        }

        int size = ((Integer) columnRs.get("COLUMN_SIZE")).intValue();
        int decimalDigits = ((Integer) columnRs.get("DECIMAL_DIGITS")).intValue();

        Column column = new Column();
        column.setName(quote(columnName, metaDataDialect));
        Column existing = table.getColumn(column);
        if (existing != null) {
          // TODO: should we just pick it up and fill it up with whatever we get from the db instead
          // ?
          throw new JDBCBinderException(column + " already exists in " + qualify);
        }

        // TODO: column.setSqlType(sqlTypeName); //this does not work 'cos the
        // precision/scale/length are not retured in TYPE_NAME
        // column.setSqlType(sqlTypeName);
        column.setComment(comment);
        column.setSqlTypeCode(new Integer(sqlType));
        if (intBounds(size)) {
          if (JDBCToHibernateTypeHelper.typeHasLength(sqlType)) {
            column.setLength(size);
          }
          if (JDBCToHibernateTypeHelper.typeHasScaleAndPrecision(sqlType)) {
            column.setPrecision(size);
          }
        }
        if (intBounds(decimalDigits)) {
          if (JDBCToHibernateTypeHelper.typeHasScaleAndPrecision(sqlType)) {
            column.setScale(decimalDigits);
          }
        }

        column.setNullable(isNullable);

        // columnDefaultValue is useless for Hibernate
        // isIndexed  (available via Indexes)
        // unique - detected when getting indexes
        // isPk - detected when finding primary keys

        table.addColumn(column);
      }
    } finally {

      if (columnIterator != null) {
        try {
          metaDataDialect.close(columnIterator);
        } catch (JDBCException se) {
          log.warn("Exception while closing iterator for column meta data", se);
        }
      }
    }
  }
Beispiel #3
0
  /**
   * @param column
   * @param generatedIdentifier
   * @return
   */
  private String guessAndAlignType(
      Table table, Column column, Mapping mapping, boolean generatedIdentifier) {
    // TODO: this method mutates the column if the types does not match...not good.
    // maybe we should copy the column instead before calling this method.
    Integer sqlTypeCode = column.getSqlTypeCode();
    String location =
        "Table: "
            + Table.qualify(table.getCatalog(), table.getSchema(), table.getQuotedName())
            + " column: "
            + column.getQuotedName();
    if (sqlTypeCode == null) {
      throw new JDBCBinderException("sqltype is null for " + location);
    }

    String preferredHibernateType =
        revengStrategy.columnToHibernateTypeName(
            TableIdentifier.create(table),
            column.getName(),
            sqlTypeCode.intValue(),
            column.getLength(),
            column.getPrecision(),
            column.getScale(),
            column.isNullable(),
            generatedIdentifier);

    Type wantedType = TypeFactory.heuristicType(preferredHibernateType);

    if (wantedType != null) {
      int[] wantedSqlTypes = wantedType.sqlTypes(mapping);

      if (wantedSqlTypes.length > 1) {
        throw new JDBCBinderException(
            "The type "
                + preferredHibernateType
                + " found on "
                + location
                + " spans multiple columns. Only single column types allowed.");
      }

      int wantedSqlType = wantedSqlTypes[0];
      if (wantedSqlType != sqlTypeCode.intValue()) {
        log.debug(
            "Sql type mismatch for "
                + location
                + " between DB and wanted hibernate type. Sql type set to "
                + typeCodeName(sqlTypeCode.intValue())
                + " instead of "
                + typeCodeName(wantedSqlType));
        column.setSqlTypeCode(new Integer(wantedSqlType));
      }
    } else {
      log.debug(
          "No Hibernate type found for "
              + preferredHibernateType
              + ". Most likely cause is a missing UserType class.");
    }

    if (preferredHibernateType == null) {
      throw new JDBCBinderException(
          "Could not find javatype for " + typeCodeName(sqlTypeCode.intValue()));
    }

    return preferredHibernateType;
  }