/// <summary> /// Generates the SQL string to create this Table in the database. /// </summary> /// <param name="dialect">The <see cref="Dialect"/> to use for SQL rules.</param> /// <param name="p"></param> /// <param name="defaultCatalog"></param> /// <param name="defaultSchema"></param> /// <returns> /// A string that contains the SQL to create this Table, Primary Key Constraints /// , and Unique Key Constraints. /// </returns> public string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema) { StringBuilder buf = new StringBuilder(HasPrimaryKey ? dialect.CreateTableString : dialect.CreateMultisetTableString).Append(' ').Append( GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(" ("); bool identityColumn = idValue != null && idValue.IsIdentityColumn(dialect); // try to find out the name of the pk to create it as identity if the // identitygenerator is used string pkname = null; if (HasPrimaryKey && identityColumn) { foreach (Column col in PrimaryKey.ColumnIterator) { pkname = col.GetQuotedName(dialect); //should only go through this loop once } } bool commaNeeded = false; foreach (Column col in ColumnIterator) { if (commaNeeded) { buf.Append(StringHelper.CommaSpace); } commaNeeded = true; 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).DbType)); } else { buf.Append(col.GetSqlType(dialect, p)); if (!string.IsNullOrEmpty(col.DefaultValue)) { buf.Append(" default ").Append(col.DefaultValue).Append(" "); } if (col.IsNullable) { buf.Append(dialect.NullColumnString); } else { buf.Append(" not null"); } } if (col.IsUnique) { if (dialect.SupportsUnique) { buf.Append(" unique"); } else { UniqueKey uk = GetUniqueKey(col.GetQuotedName(dialect) + "_"); uk.AddColumn(col); } } if (col.HasCheckConstraint && dialect.SupportsColumnCheck) { buf.Append(" check( ").Append(col.CheckConstraint).Append(") "); } if (string.IsNullOrEmpty(col.Comment) == false) { buf.Append(dialect.GetColumnComment(col.Comment)); } } if (HasPrimaryKey && (dialect.GenerateTablePrimaryKeyConstraintForIdentityColumn || !identityColumn)) { buf.Append(StringHelper.CommaSpace).Append(PrimaryKey.SqlConstraintString(dialect, defaultSchema)); } foreach (UniqueKey uk in UniqueKeyIterator) { buf.Append(',').Append(uk.SqlConstraintString(dialect)); } if (dialect.SupportsTableCheck) { foreach (string checkConstraint in checkConstraints) { buf.Append(", check (").Append(checkConstraint).Append(") "); } } if (!dialect.SupportsForeignKeyConstraintInAlterTable) { foreach (ForeignKey foreignKey in ForeignKeyIterator) { if (foreignKey.HasPhysicalConstraint) { buf.Append(",").Append(foreignKey.SqlConstraintString(dialect, foreignKey.Name, defaultCatalog, defaultSchema)); } } } buf.Append(StringHelper.ClosedParen); if (string.IsNullOrEmpty(comment) == false) { buf.Append(dialect.GetTableComment(comment)); } buf.Append(dialect.TableTypeString); return(buf.ToString()); }
/// <summary> /// Generates the SQL string to create this Table in the database. /// </summary> /// <param name="dialect">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param> /// <param name="p"></param> /// <param name="defaultSchema"></param> /// <returns> /// A string that contains the SQL to create this Table, Primary Key Constraints /// , and Unique Key Constraints. /// </returns> public string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultSchema) { StringBuilder buf = new StringBuilder("create table ") .Append(GetQualifiedName(dialect, defaultSchema)) .Append(" ("); bool identityColumn = idValue != null && idValue.CreateIdentifierGenerator(dialect) is IdentityGenerator; // try to find out the name of the pk to create it as identity if the // identitygenerator is used string pkname = null; if (primaryKey != null && identityColumn) { foreach (Column col in primaryKey.ColumnCollection) { pkname = col.GetQuotedName(dialect); //should only go through this loop once } } int i = 0; foreach (Column col in ColumnCollection) { i++; buf.Append(col.GetQuotedName(dialect)) .Append(' ') .Append(col.GetSqlType(dialect, p)); if (identityColumn && col.GetQuotedName(dialect).Equals(pkname)) { buf.Append(' ') .Append(dialect.IdentityColumnString); } else { if (col.IsNullable) { buf.Append(dialect.NullColumnString); } else { buf.Append(" not null"); } } if (col.IsUnique) { if (dialect.SupportsUnique) { buf.Append(" unique"); } else { UniqueKey uk = GetUniqueKey(col.GetQuotedName(dialect) + "_"); uk.AddColumn(col); } } if (i < ColumnCollection.Count) { buf.Append(StringHelper.CommaSpace); } } if (primaryKey != null) { //if ( dialect is HSQLDialect && identityColumn ) { // // skip the primary key definition // //ugly hack... //} else { buf.Append(',').Append(primaryKey.SqlConstraintString(dialect, defaultSchema)); //} } foreach (UniqueKey uk in UniqueKeyCollection) { buf.Append(',').Append(uk.SqlConstraintString(dialect)); } buf.Append(StringHelper.ClosedParen); return(buf.ToString()); }