public String sqlCreateString( Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) throws HibernateException { StringBuffer buf = new StringBuffer("create table ") .append(getQualifiedName(dialect, defaultCatalog, defaultSchema)) .append(" ("); boolean identityColumn = idValue != null && idValue.isIdentityColumn(dialect); // Try to find out the name of the primary key to create it as identity if the IdentityGenerator // is used String pkname = null; if (hasPrimaryKey() && identityColumn) { pkname = ((Column) getPrimaryKey().getColumnIterator().next()).getQuotedName(dialect); } Iterator iter = getColumnIterator(); while (iter.hasNext()) { Column col = (Column) iter.next(); buf.append(col.getQuotedName(dialect)).append(' '); if (identityColumn && col.getQuotedName(dialect).equals(pkname)) { // to support dialects that have their own identity data type if (dialect.hasDataTypeInIdentityColumn()) { buf.append(col.getSqlType(dialect, p)); } buf.append(' ').append(dialect.getIdentityColumnString(col.getSqlTypeCode(p))); } else { buf.append(col.getSqlType(dialect, p)); String defaultValue = col.getDefaultValue(); if (defaultValue != null) { buf.append(" default ").append(defaultValue); } if (col.isNullable()) { buf.append(dialect.getNullColumnString()); } else { buf.append(" not null"); } } boolean useUniqueConstraint = col.isUnique() && (!col.isNullable() || dialect.supportsNotNullUnique()); if (useUniqueConstraint) { if (dialect.supportsUnique()) { buf.append(" unique"); } else { UniqueKey uk = getOrCreateUniqueKey(col.getQuotedName(dialect) + '_'); uk.addColumn(col); } } if (col.hasCheckConstraint() && dialect.supportsColumnCheck()) { buf.append(" check (").append(col.getCheckConstraint()).append(")"); } String columnComment = col.getComment(); if (columnComment != null) { buf.append(dialect.getColumnComment(columnComment)); } if (iter.hasNext()) buf.append(", "); } if (hasPrimaryKey()) { buf.append(", ").append(getPrimaryKey().sqlConstraintString(dialect)); } if (dialect.supportsUniqueConstraintInCreateAlterTable()) { Iterator ukiter = getUniqueKeyIterator(); while (ukiter.hasNext()) { UniqueKey uk = (UniqueKey) ukiter.next(); buf.append(", ").append(uk.sqlConstraintString(dialect)); } } /*Iterator idxiter = getIndexIterator(); while ( idxiter.hasNext() ) { Index idx = (Index) idxiter.next(); buf.append(',').append( idx.sqlConstraintString(dialect) ); }*/ if (dialect.supportsTableCheck()) { Iterator chiter = checkConstraints.iterator(); while (chiter.hasNext()) { buf.append(", check (").append(chiter.next()).append(')'); } } buf.append(')'); if (comment != null) buf.append(dialect.getTableComment(comment)); return buf.append(dialect.getTableTypeString()).toString(); }
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(); }