Beispiel #1
0
  public void validateColumns(Dialect dialect, Mapping mapping, TableMetadata tableInfo) {
    Iterator iter = getColumnIterator();
    while (iter.hasNext()) {
      Column col = (Column) iter.next();

      ColumnMetadata columnInfo = tableInfo.getColumnMetadata(col.getName());

      if (columnInfo == null) {
        throw new HibernateException(
            "Missing column: "
                + col.getName()
                + " in "
                + Table.qualify(
                    tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()));
      } else {
        final boolean typesMatch =
            col.getSqlType(dialect, mapping)
                    .toLowerCase(Locale.ROOT)
                    .startsWith(columnInfo.getTypeName().toLowerCase(Locale.ROOT))
                || columnInfo.getTypeCode() == col.getSqlTypeCode(mapping);
        if (!typesMatch) {
          throw new HibernateException(
              "Wrong column type in "
                  + Table.qualify(
                      tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName())
                  + " for column "
                  + col.getName()
                  + ". Found: "
                  + columnInfo.getTypeName().toLowerCase(Locale.ROOT)
                  + ", expected: "
                  + col.getSqlType(dialect, mapping));
        }
      }
    }
  }
Beispiel #2
0
  public Iterator sqlAlterStrings(
      Dialect dialect,
      Mapping p,
      TableMetadata tableInfo,
      String defaultCatalog,
      String defaultSchema)
      throws HibernateException {

    StringBuffer root =
        new StringBuffer("alter table ")
            .append(getQualifiedName(dialect, defaultCatalog, defaultSchema))
            .append(' ')
            .append(dialect.getAddColumnString());

    Iterator iter = getColumnIterator();
    List results = new ArrayList();
    while (iter.hasNext()) {
      Column column = (Column) iter.next();

      ColumnMetadata columnInfo = tableInfo.getColumnMetadata(column.getName());

      if (columnInfo == null) {
        // the column doesnt exist at all.
        StringBuffer alter =
            new StringBuffer(root.toString())
                .append(' ')
                .append(column.getQuotedName(dialect))
                .append(' ')
                .append(column.getSqlType(dialect, p));

        String defaultValue = column.getDefaultValue();
        if (defaultValue != null) {
          alter.append(" default ").append(defaultValue);

          if (column.isNullable()) {
            alter.append(dialect.getNullColumnString());
          } else {
            alter.append(" not null");
          }
        }

        boolean useUniqueConstraint =
            column.isUnique()
                && dialect.supportsUnique()
                && (!column.isNullable() || dialect.supportsNotNullUnique());
        if (useUniqueConstraint) {
          alter.append(" unique");
        }

        if (column.hasCheckConstraint() && dialect.supportsColumnCheck()) {
          alter.append(" check(").append(column.getCheckConstraint()).append(")");
        }

        String columnComment = column.getComment();
        if (columnComment != null) alter.append(dialect.getColumnComment(columnComment));

        results.add(alter.toString());
      }
    }

    return results.iterator();
  }