|
From: <fab...@us...> - 2008-12-16 20:44:25
|
Revision: 3960
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3960&view=rev
Author: fabiomaulo
Date: 2008-12-16 20:44:19 +0000 (Tue, 16 Dec 2008)
Log Message:
-----------
- Minor refactoring
- bugfix in exception message
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs
trunk/nhibernate/src/NHibernate/Mapping/Table.cs
Modified: trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs 2008-12-16 20:16:16 UTC (rev 3959)
+++ trunk/nhibernate/src/NHibernate/Cfg/Configuration.cs 2008-12-16 20:44:19 UTC (rev 3960)
@@ -2031,7 +2031,7 @@
string key = generator.GeneratorKey();
if (!databaseMetadata.IsSequence(key) && !databaseMetadata.IsTable(key))
{
- throw new HibernateException(string.Format("Missing sequence or table: ", key));
+ throw new HibernateException(string.Format("Missing sequence or table: "+ key));
}
}
}
Modified: trunk/nhibernate/src/NHibernate/Mapping/Table.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Mapping/Table.cs 2008-12-16 20:16:16 UTC (rev 3959)
+++ trunk/nhibernate/src/NHibernate/Mapping/Table.cs 2008-12-16 20:44:19 UTC (rev 3960)
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.Data;
using System.Text;
using NHibernate.Dialect.Schema;
using NHibernate.Engine;
@@ -14,9 +13,9 @@
{
None = 0,
Drop = 1,
- Update= 2,
- Export= 4,
- Validate= 8,
+ Update = 2,
+ Export = 4,
+ Validate = 8,
All = Drop | Update | Export | Validate
}
@@ -26,78 +25,25 @@
[Serializable]
public class Table : IRelationalModel
{
- internal class ForeignKeyKey: IEqualityComparer<ForeignKeyKey>
- {
- internal string referencedClassName;
- internal List<Column> columns;
- internal List<Column> referencedColumns;
-
- internal ForeignKeyKey(IEnumerable<Column> columns, string referencedClassName, IEnumerable<Column> referencedColumns)
- {
- this.referencedClassName = referencedClassName;
- this.columns = new List<Column>(columns);
- if (referencedColumns != null)
- this.referencedColumns = new List<Column>(referencedColumns);
- else
- this.referencedColumns = new List<Column>();
- }
-
- public override int GetHashCode()
- {
- return GetHashCode(this);
- }
-
- public override bool Equals(object other)
- {
- ForeignKeyKey that = other as ForeignKeyKey;
- if (that != null)
- return Equals(this, that);
- else
- return false;
- }
-
- #region IEqualityComparer<ForeignKeyKey> Members
-
- public bool Equals(ForeignKeyKey x, ForeignKeyKey y)
- {
- // NH : Different implementation to prevent NH930 (look test)
- return //y.referencedClassName.Equals(x.referencedClassName) &&
- CollectionHelper.CollectionEquals<Column>(y.columns, x.columns) &&
- CollectionHelper.CollectionEquals<Column>(y.referencedColumns, x.referencedColumns);
- }
-
- public int GetHashCode(ForeignKeyKey obj)
- {
- int result = CollectionHelper.GetHashCode(obj.columns) ^ CollectionHelper.GetHashCode(obj.referencedColumns);
- return result;
- }
-
- #endregion
- }
-
- private string name;
- private string schema;
- private string catalog;
-
+ private static int tableCounter;
+ private readonly List<string> checkConstraints = new List<string>();
private readonly LinkedHashMap<string, Column> columns = new LinkedHashMap<string, Column>();
- private IKeyValue idValue;
- private PrimaryKey primaryKey;
- private readonly Dictionary<string, Index> indexes = new Dictionary<string, Index>();
private readonly Dictionary<ForeignKeyKey, ForeignKey> foreignKeys = new Dictionary<ForeignKeyKey, ForeignKey>();
- private readonly Dictionary<string, UniqueKey> uniqueKeys = new Dictionary<string, UniqueKey>();
+ private readonly Dictionary<string, Index> indexes = new Dictionary<string, Index>();
private readonly int uniqueInteger;
- private bool quoted;
- private static int tableCounter = 0;
- private readonly List<string> checkConstraints = new List<string>();
- private bool isAbstract;
- private bool hasDenormalizedTables = false;
+ private readonly Dictionary<string, UniqueKey> uniqueKeys = new Dictionary<string, UniqueKey>();
+ private string catalog;
private string comment;
- private string subselect;
- private string rowId;
+ private bool hasDenormalizedTables;
+ private IKeyValue idValue;
+ private bool isAbstract;
private bool isSchemaQuoted;
+ private string name;
+ private bool quoted;
+ private string schema;
private SchemaAction schemaActions = SchemaAction.All;
+ private string subselect;
-
/// <summary>
/// Initializes a new instance of <see cref="Table"/>.
/// </summary>
@@ -106,43 +52,12 @@
uniqueInteger = tableCounter++;
}
- public Table(string name)
- : this()
+ public Table(string name) : this()
{
Name = name;
}
/// <summary>
- /// Gets the schema qualified name of the Table.
- /// </summary>
- /// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
- /// <returns>The name of the table qualified with the schema if one is specified.</returns>
- public string GetQualifiedName(Dialect.Dialect dialect)
- {
- return GetQualifiedName(dialect, null, null);
- }
-
- /// <summary>
- /// Gets the schema qualified name of the Table using the specified qualifier
- /// </summary>
- /// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
- /// <param name="defaultCatalog">The catalog name.</param>
- /// <param name="defaultSchema">The schema name.</param>
- /// <returns>A String representing the Qualified name.</returns>
- /// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks>
- public virtual string GetQualifiedName(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
- {
- if (!string.IsNullOrEmpty(subselect))
- {
- return "( " + subselect + " )";
- }
- string quotedName = GetQuotedName(dialect);
- string usedSchema = schema == null ? defaultSchema : GetQuotedSchema(dialect);
- string usedCatalog = catalog ?? defaultCatalog;
- return dialect.Qualify(usedCatalog, usedSchema, quotedName);
- }
-
- /// <summary>
/// Gets or sets the name of the Table in the database.
/// </summary>
/// <value>
@@ -177,102 +92,7 @@
}
}
- /// <summary> returns quoted name as it would be in the mapping file.</summary>
- public string GetQuotedName()
- {
- return quoted ? "`" + name + "`" : name;
- }
-
/// <summary>
- /// Gets the name of this Table in quoted form if it is necessary.
- /// </summary>
- /// <param name="dialect">
- /// The <see cref="Dialect.Dialect"/> that knows how to quote the Table name.
- /// </param>
- /// <returns>
- /// The Table name in a form that is safe to use inside of a SQL statement.
- /// Quoted if it needs to be, not quoted if it does not need to be.
- /// </returns>
- public string GetQuotedName(Dialect.Dialect dialect)
- {
- return IsQuoted ?
- dialect.QuoteForTableName(name) :
- name;
- }
-
- /// <summary> returns quoted name as it is in the mapping file.</summary>
- public string GetQuotedSchema()
- {
- return IsSchemaQuoted ? "`" + schema + "`" : schema;
- }
-
- public string GetQuotedSchema(Dialect.Dialect dialect)
- {
- return IsSchemaQuoted ? dialect.OpenQuote + schema + dialect.CloseQuote : schema;
- }
-
- /// <summary>
- /// Gets the schema for this table in quoted form if it is necessary.
- /// </summary>
- /// <param name="dialect">
- /// The <see cref="Dialect.Dialect" /> that knows how to quote the table name.
- /// </param>
- /// <returns>
- /// The schema name for this table in a form that is safe to use inside
- /// of a SQL statement. Quoted if it needs to be, not quoted if it does not need to be.
- /// </returns>
- public string GetQuotedSchemaName(Dialect.Dialect dialect)
- {
- if (schema == null)
- {
- return null;
- }
-
- if (schema.StartsWith("`"))
- {
- return dialect.QuoteForSchemaName(schema.Substring(1, schema.Length - 2));
- }
-
- return schema;
- }
-
- /// <summary>
- /// Gets the <see cref="Column"/> at the specified index.
- /// </summary>
- /// <param name="n">The index of the Column to get.</param>
- /// <returns>
- /// The <see cref="Column"/> at the specified index.
- /// </returns>
- public Column GetColumn(int n)
- {
- IEnumerator<Column> iter = columns.Values.GetEnumerator();
- for (int i = 0; i < n; i++)
- {
- iter.MoveNext();
- }
- return iter.Current;
- }
-
- /// <summary>
- /// Adds the <see cref="Column"/> to the <see cref="ICollection"/> of
- /// Columns that are part of the Table.
- /// </summary>
- /// <param name="column">The <see cref="Column"/> to include in the Table.</param>
- public void AddColumn(Column column)
- {
- Column old = GetColumn(column);
- if (old == null)
- {
- columns[column.CanonicalName] = column;
- column.uniqueInteger = columns.Count;
- }
- else
- {
- column.uniqueInteger = old.uniqueInteger;
- }
- }
-
- /// <summary>
/// Gets the number of columns that this Table contains.
/// </summary>
/// <value>
@@ -335,67 +155,168 @@
get { return uniqueKeys.Values; }
}
- public string[] SqlAlterStrings(Dialect.Dialect dialect, IMapping p, ITableMetadata tableInfo, string defaultCatalog, string defaultSchema)
+ /// <summary>
+ /// Gets or sets the <see cref="PrimaryKey"/> of the Table.
+ /// </summary>
+ /// <value>The <see cref="PrimaryKey"/> of the Table.</value>
+ public virtual PrimaryKey PrimaryKey { get; set; }
+
+ /// <summary>
+ /// Gets or sets the schema the table is in.
+ /// </summary>
+ /// <value>
+ /// The schema the table is in or <see langword="null" /> if no schema is specified.
+ /// </value>
+ public string Schema
{
- StringBuilder root = new StringBuilder("alter table ")
- .Append(GetQualifiedName(dialect, defaultCatalog, defaultSchema))
- .Append(' ')
- .Append(dialect.AddColumnString);
+ get { return schema; }
+ set
+ {
+ if (value != null && value[0] == '`')
+ {
+ isSchemaQuoted = true;
+ schema = value.Substring(1, value.Length - 2);
+ }
+ else
+ {
+ schema = value;
+ }
+ }
+ }
- List<string> results = new List<string>(ColumnSpan);
+ /// <summary>
+ /// Gets the unique number of the Table.
+ /// </summary>
+ /// <value>The unique number of the Table.</value>
+ public int UniqueInteger
+ {
+ get { return uniqueInteger; }
+ }
- foreach (Column column in ColumnIterator)
+ /// <summary>
+ /// Gets or sets if the column needs to be quoted in SQL statements.
+ /// </summary>
+ /// <value><see langword="true" /> if the column is quoted.</value>
+ public bool IsQuoted
+ {
+ get { return quoted; }
+ set { quoted = value; }
+ }
+
+ public IEnumerable<string> CheckConstraintsIterator
+ {
+ get { return checkConstraints; }
+ }
+
+ public bool IsAbstractUnionTable
+ {
+ get { return HasDenormalizedTables && isAbstract; }
+ }
+
+ public bool HasDenormalizedTables
+ {
+ get { return hasDenormalizedTables; }
+ }
+
+ public bool IsAbstract
+ {
+ get { return isAbstract; }
+ set { isAbstract = value; }
+ }
+
+ internal IDictionary<string, UniqueKey> UniqueKeys
+ {
+ get
{
- IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name);
- if (columnInfo != null)
- continue;
-
- // the column doesnt exist at all.
- StringBuilder alter = new StringBuilder(root.ToString())
- .Append(' ')
- .Append(column.GetQuotedName(dialect))
- .Append(' ')
- .Append(column.GetSqlType(dialect, p));
-
- string defaultValue = column.DefaultValue;
- if (!string.IsNullOrEmpty(defaultValue))
+ if (uniqueKeys.Count > 1)
+ {
+ //deduplicate unique constraints sharing the same columns
+ //this is needed by Hibernate Annotations since it creates automagically
+ // unique constraints for the user
+ var finalUniqueKeys = new Dictionary<string, UniqueKey>(uniqueKeys.Count);
+ foreach (var entry in uniqueKeys)
{
- alter.Append(" default ").Append(defaultValue);
-
- if (column.IsNullable)
+ UniqueKey uk = entry.Value;
+ IList<Column> _columns = uk.Columns;
+ bool skip = false;
+ var tempUks = new Dictionary<string, UniqueKey>(finalUniqueKeys);
+ foreach (var tUk in tempUks)
{
- alter.Append(dialect.NullColumnString);
+ UniqueKey currentUk = tUk.Value;
+ if (AreSameColumns(currentUk.Columns, _columns))
+ {
+ skip = true;
+ break;
+ }
}
- else
+ if (!skip)
{
- alter.Append(" not null");
+ finalUniqueKeys[entry.Key] = uk;
}
}
-
- bool useUniqueConstraint = column.Unique && dialect.SupportsUnique
- && (!column.IsNullable || dialect.SupportsNotNullUnique);
- if (useUniqueConstraint)
- {
- alter.Append(" unique");
+ return finalUniqueKeys;
}
-
- if (column.HasCheckConstraint && dialect.SupportsColumnCheck)
+ else
{
- alter.Append(" check(").Append(column.CheckConstraint).Append(") ");
+ return uniqueKeys;
}
+ }
+ }
- string columnComment = column.Comment;
- if (columnComment != null)
- {
- alter.Append(dialect.GetColumnComment(columnComment));
- }
+ public bool HasPrimaryKey
+ {
+ get { return PrimaryKey != null; }
+ }
- results.Add(alter.ToString());
- }
+ public string Catalog
+ {
+ get { return catalog; }
+ set { catalog = value; }
+ }
- return results.ToArray();
+ public string Comment
+ {
+ get { return comment; }
+ set { comment = value; }
}
+ public string Subselect
+ {
+ get { return subselect; }
+ set { subselect = value; }
+ }
+
+ public IKeyValue IdentifierValue
+ {
+ get { return idValue; }
+ set { idValue = value; }
+ }
+
+ public bool IsSubselect
+ {
+ get { return !string.IsNullOrEmpty(subselect); }
+ }
+
+ public bool IsPhysicalTable
+ {
+ get { return !IsSubselect && !IsAbstractUnionTable; }
+ }
+
+ public SchemaAction SchemaActions
+ {
+ get { return schemaActions; }
+ set { schemaActions = value; }
+ }
+
+ public string RowId { get; set; }
+
+ public bool IsSchemaQuoted
+ {
+ get { return isSchemaQuoted; }
+ }
+
+ #region IRelationalModel Members
+
/// <summary>
/// Generates the SQL string to create this Table in the database.
/// </summary>
@@ -409,13 +330,9 @@
/// </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(" (");
+ StringBuilder buf =
+ new StringBuilder(HasPrimaryKey ? dialect.CreateTableString : dialect.CreateMultisetTableString).Append(' ').Append(
+ GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(" (");
bool identityColumn = idValue != null && idValue.IsIdentityColumn(dialect);
@@ -439,8 +356,7 @@
}
commaNeeded = true;
- buf.Append(col.GetQuotedName(dialect))
- .Append(' ');
+ buf.Append(col.GetQuotedName(dialect)).Append(' ');
if (identityColumn && col.GetQuotedName(dialect).Equals(pkname))
{
@@ -449,14 +365,13 @@
{
buf.Append(col.GetSqlType(dialect, p));
}
- buf.Append(' ')
- .Append(dialect.GetIdentityColumnString(col.GetSqlTypeCode(p).DbType));
+ buf.Append(' ').Append(dialect.GetIdentityColumnString(col.GetSqlTypeCode(p).DbType));
}
else
{
buf.Append(col.GetSqlType(dialect, p));
- if(string.IsNullOrEmpty(col.DefaultValue)==false)
+ if (!string.IsNullOrEmpty(col.DefaultValue))
{
buf.Append(" default ").Append(col.DefaultValue).Append(" ");
}
@@ -484,14 +399,12 @@
}
}
- if(col.HasCheckConstraint && dialect.SupportsColumnCheck)
+ if (col.HasCheckConstraint && dialect.SupportsColumnCheck)
{
- buf.Append(" check( ")
- .Append(col.CheckConstraint)
- .Append(") ");
+ buf.Append(" check( ").Append(col.CheckConstraint).Append(") ");
}
- if(string.IsNullOrEmpty(col.Comment)==false)
+ if (string.IsNullOrEmpty(col.Comment) == false)
{
buf.Append(dialect.GetColumnComment(col.Comment));
}
@@ -505,20 +418,18 @@
{
buf.Append(',').Append(uk.SqlConstraintString(dialect));
}
-
- if(dialect.SupportsTableCheck)
+
+ if (dialect.SupportsTableCheck)
{
foreach (string checkConstraint in checkConstraints)
{
- buf.Append(", check (")
- .Append(checkConstraint)
- .Append(") ");
+ buf.Append(", check (").Append(checkConstraint).Append(") ");
}
}
buf.Append(StringHelper.ClosedParen);
- if(string.IsNullOrEmpty(comment)==false)
+ if (string.IsNullOrEmpty(comment) == false)
{
buf.Append(dialect.GetTableComment(comment));
}
@@ -541,28 +452,204 @@
return dialect.GetDropTableString(GetQualifiedName(dialect, defaultCatalog, defaultSchema));
}
+ #endregion
+
/// <summary>
- /// Gets or sets the <see cref="PrimaryKey"/> of the Table.
+ /// Gets the schema qualified name of the Table.
/// </summary>
- /// <value>The <see cref="PrimaryKey"/> of the Table.</value>
- public virtual PrimaryKey PrimaryKey
+ /// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
+ /// <returns>The name of the table qualified with the schema if one is specified.</returns>
+ public string GetQualifiedName(Dialect.Dialect dialect)
{
- get { return primaryKey; }
- set { primaryKey = value; }
+ return GetQualifiedName(dialect, null, null);
}
/// <summary>
+ /// Gets the schema qualified name of the Table using the specified qualifier
+ /// </summary>
+ /// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
+ /// <param name="defaultCatalog">The catalog name.</param>
+ /// <param name="defaultSchema">The schema name.</param>
+ /// <returns>A String representing the Qualified name.</returns>
+ /// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks>
+ public virtual string GetQualifiedName(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
+ {
+ if (!string.IsNullOrEmpty(subselect))
+ {
+ return "( " + subselect + " )";
+ }
+ string quotedName = GetQuotedName(dialect);
+ string usedSchema = schema == null ? defaultSchema : GetQuotedSchema(dialect);
+ string usedCatalog = catalog ?? defaultCatalog;
+ return dialect.Qualify(usedCatalog, usedSchema, quotedName);
+ }
+
+ /// <summary> returns quoted name as it would be in the mapping file.</summary>
+ public string GetQuotedName()
+ {
+ return quoted ? "`" + name + "`" : name;
+ }
+
+ /// <summary>
+ /// Gets the name of this Table in quoted form if it is necessary.
+ /// </summary>
+ /// <param name="dialect">
+ /// The <see cref="Dialect.Dialect"/> that knows how to quote the Table name.
+ /// </param>
+ /// <returns>
+ /// The Table name in a form that is safe to use inside of a SQL statement.
+ /// Quoted if it needs to be, not quoted if it does not need to be.
+ /// </returns>
+ public string GetQuotedName(Dialect.Dialect dialect)
+ {
+ return IsQuoted ? dialect.QuoteForTableName(name) : name;
+ }
+
+ /// <summary> returns quoted name as it is in the mapping file.</summary>
+ public string GetQuotedSchema()
+ {
+ return IsSchemaQuoted ? "`" + schema + "`" : schema;
+ }
+
+ public string GetQuotedSchema(Dialect.Dialect dialect)
+ {
+ return IsSchemaQuoted ? dialect.OpenQuote + schema + dialect.CloseQuote : schema;
+ }
+
+ /// <summary>
+ /// Gets the schema for this table in quoted form if it is necessary.
+ /// </summary>
+ /// <param name="dialect">
+ /// The <see cref="Dialect.Dialect" /> that knows how to quote the table name.
+ /// </param>
+ /// <returns>
+ /// The schema name for this table in a form that is safe to use inside
+ /// of a SQL statement. Quoted if it needs to be, not quoted if it does not need to be.
+ /// </returns>
+ public string GetQuotedSchemaName(Dialect.Dialect dialect)
+ {
+ if (schema == null)
+ {
+ return null;
+ }
+
+ if (schema.StartsWith("`"))
+ {
+ return dialect.QuoteForSchemaName(schema.Substring(1, schema.Length - 2));
+ }
+
+ return schema;
+ }
+
+ /// <summary>
+ /// Gets the <see cref="Column"/> at the specified index.
+ /// </summary>
+ /// <param name="n">The index of the Column to get.</param>
+ /// <returns>
+ /// The <see cref="Column"/> at the specified index.
+ /// </returns>
+ public Column GetColumn(int n)
+ {
+ IEnumerator<Column> iter = columns.Values.GetEnumerator();
+ for (int i = 0; i < n; i++)
+ {
+ iter.MoveNext();
+ }
+ return iter.Current;
+ }
+
+ /// <summary>
+ /// Adds the <see cref="Column"/> to the <see cref="ICollection"/> of
+ /// Columns that are part of the Table.
+ /// </summary>
+ /// <param name="column">The <see cref="Column"/> to include in the Table.</param>
+ public void AddColumn(Column column)
+ {
+ Column old = GetColumn(column);
+ if (old == null)
+ {
+ columns[column.CanonicalName] = column;
+ column.uniqueInteger = columns.Count;
+ }
+ else
+ {
+ column.uniqueInteger = old.uniqueInteger;
+ }
+ }
+
+ public string[] SqlAlterStrings(Dialect.Dialect dialect, IMapping p, ITableMetadata tableInfo, string defaultCatalog,
+ string defaultSchema)
+ {
+ StringBuilder root =
+ new StringBuilder("alter table ").Append(GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(' ').
+ Append(dialect.AddColumnString);
+
+ var results = new List<string>(ColumnSpan);
+
+ foreach (Column column in ColumnIterator)
+ {
+ IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name);
+ if (columnInfo != null)
+ {
+ continue;
+ }
+
+ // the column doesnt exist at all.
+ StringBuilder alter =
+ new StringBuilder(root.ToString()).Append(' ').Append(column.GetQuotedName(dialect)).Append(' ').Append(
+ column.GetSqlType(dialect, p));
+
+ string defaultValue = column.DefaultValue;
+ if (!string.IsNullOrEmpty(defaultValue))
+ {
+ alter.Append(" default ").Append(defaultValue);
+
+ if (column.IsNullable)
+ {
+ alter.Append(dialect.NullColumnString);
+ }
+ else
+ {
+ alter.Append(" not null");
+ }
+ }
+
+ bool useUniqueConstraint = column.Unique && dialect.SupportsUnique
+ && (!column.IsNullable || dialect.SupportsNotNullUnique);
+ if (useUniqueConstraint)
+ {
+ alter.Append(" unique");
+ }
+
+ if (column.HasCheckConstraint && dialect.SupportsColumnCheck)
+ {
+ alter.Append(" check(").Append(column.CheckConstraint).Append(") ");
+ }
+
+ string columnComment = column.Comment;
+ if (columnComment != null)
+ {
+ alter.Append(dialect.GetColumnComment(columnComment));
+ }
+
+ results.Add(alter.ToString());
+ }
+
+ return results.ToArray();
+ }
+
+ /// <summary>
/// Gets the <see cref="Index"/> identified by the name.
/// </summary>
- /// <param name="name">The name of the <see cref="Index"/> to get.</param>
+ /// <param name="indexName">The name of the <see cref="Index"/> to get.</param>
/// <returns>
/// The <see cref="Index"/> identified by the name. If the <see cref="Index"/>
/// identified by the name does not exist then it is created.
/// </returns>
- public Index GetIndex(string name)
+ public Index GetIndex(string indexName)
{
Index result;
- indexes.TryGetValue(name, out result);
+ indexes.TryGetValue(indexName, out result);
return result;
}
@@ -593,15 +680,15 @@
/// <summary>
/// Gets the <see cref="UniqueKey"/> identified by the name.
/// </summary>
- /// <param name="name">The name of the <see cref="UniqueKey"/> to get.</param>
+ /// <param name="keyName">The name of the <see cref="UniqueKey"/> to get.</param>
/// <returns>
/// The <see cref="UniqueKey"/> identified by the name. If the <see cref="UniqueKey"/>
/// identified by the name does not exist then it is created.
/// </returns>
- public UniqueKey GetUniqueKey(string name)
+ public UniqueKey GetUniqueKey(string keyName)
{
UniqueKey result;
- uniqueKeys.TryGetValue(name, out result);
+ uniqueKeys.TryGetValue(keyName, out result);
return result;
}
@@ -630,9 +717,7 @@
return uk;
}
- public virtual void CreateForeignKeys()
- {
- }
+ public virtual void CreateForeignKeys() {}
public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable<Column> keyColumns, string referencedEntityName)
{
@@ -654,13 +739,13 @@
/// one already exists for the columns then it will return an
/// existing <see cref="ForeignKey"/>.
/// </remarks>
- public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable<Column> keyColumns,
- string referencedEntityName, IEnumerable<Column> referencedColumns)
+ public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable<Column> keyColumns, string referencedEntityName,
+ IEnumerable<Column> referencedColumns)
{
IEnumerable<Column> kCols = keyColumns;
IEnumerable<Column> refCols = referencedColumns;
- ForeignKeyKey key = new ForeignKeyKey(kCols, referencedEntityName, refCols);
+ var key = new ForeignKeyKey(kCols, referencedEntityName, refCols);
ForeignKey fk;
foreignKeys.TryGetValue(key, out fk);
@@ -707,13 +792,13 @@
/// Generates a unique string for an <see cref="ICollection"/> of
/// <see cref="Column"/> objects.
/// </summary>
- /// <param name="columns">An <see cref="ICollection"/> of <see cref="Column"/> objects.</param>
+ /// <param name="uniqueColumns">An <see cref="ICollection"/> of <see cref="Column"/> objects.</param>
/// <returns>
/// An unique string for the <see cref="Column"/> objects.
/// </returns>
- public string UniqueColumnString(IEnumerable columns)
+ public string UniqueColumnString(IEnumerable uniqueColumns)
{
- return UniqueColumnString(columns, null);
+ return UniqueColumnString(uniqueColumns, null);
}
public string UniqueColumnString(IEnumerable iterator, string referencedEntityName)
@@ -721,7 +806,9 @@
// NH Different implementation (NH-1339)
int result = 37;
if (referencedEntityName != null)
+ {
result ^= referencedEntityName.GetHashCode();
+ }
foreach (object o in iterator)
{
@@ -731,57 +818,15 @@
}
/// <summary>
- /// Gets or sets the schema the table is in.
- /// </summary>
- /// <value>
- /// The schema the table is in or <see langword="null" /> if no schema is specified.
- /// </value>
- public string Schema
- {
- get{return schema;}
- set
- {
- if (value != null && value[0] == '`')
- {
- isSchemaQuoted = true;
- schema = value.Substring(1, value.Length - 2);
- }
- else
- {
- schema = value;
- }
- }
- }
-
- /// <summary>
- /// Gets the unique number of the Table.
- /// </summary>
- /// <value>The unique number of the Table.</value>
- public int UniqueInteger
- {
- get { return uniqueInteger; }
- }
-
- /// <summary>
/// Sets the Identifier of the Table.
/// </summary>
- /// <param name="idValue">The <see cref="SimpleValue"/> that represents the Identifier.</param>
- public void SetIdentifierValue(SimpleValue idValue)
+ /// <param name="identifierValue">The <see cref="SimpleValue"/> that represents the Identifier.</param>
+ public void SetIdentifierValue(SimpleValue identifierValue)
{
- this.idValue = idValue;
+ idValue = identifierValue;
}
/// <summary>
- /// Gets or sets if the column needs to be quoted in SQL statements.
- /// </summary>
- /// <value><see langword="true" /> if the column is quoted.</value>
- public bool IsQuoted
- {
- get { return quoted; }
- set { quoted = value; }
- }
-
- /// <summary>
///
/// </summary>
/// <param name="constraint"></param>
@@ -790,32 +835,11 @@
checkConstraints.Add(constraint);
}
- public IEnumerable<string> CheckConstraintsIterator
- {
- get { return checkConstraints; }
- }
-
- public bool IsAbstractUnionTable
- {
- get { return HasDenormalizedTables && isAbstract; }
- }
-
- public bool HasDenormalizedTables
- {
- get { return hasDenormalizedTables; }
- }
-
internal void SetHasDenormalizedTables()
{
hasDenormalizedTables = true;
}
- public bool IsAbstract
- {
- get { return isAbstract; }
- set { isAbstract = value; }
- }
-
public virtual bool ContainsColumn(Column column)
{
return columns.ContainsValue(column);
@@ -830,7 +854,9 @@
public virtual Column GetColumn(Column column)
{
if (column == null)
+ {
return null;
+ }
Column result;
columns.TryGetValue(column.CanonicalName, out result);
@@ -838,131 +864,39 @@
return column.Equals(result) ? result : null;
}
- internal IDictionary<string, UniqueKey> UniqueKeys
- {
- get
- {
- if (uniqueKeys.Count > 1)
- {
- //deduplicate unique constraints sharing the same columns
- //this is needed by Hibernate Annotations since it creates automagically
- // unique constraints for the user
- Dictionary<string, UniqueKey> finalUniqueKeys = new Dictionary<string, UniqueKey>(uniqueKeys.Count);
- foreach (KeyValuePair<string, UniqueKey> entry in uniqueKeys)
- {
- UniqueKey uk = entry.Value;
- IList<Column> _columns = uk.Columns;
- bool skip = false;
- Dictionary<string, UniqueKey> tempUks = new Dictionary<string, UniqueKey>(finalUniqueKeys);
- foreach (KeyValuePair<string, UniqueKey> tUk in tempUks)
- {
- UniqueKey currentUk = tUk.Value;
- if (AreSameColumns(currentUk.Columns, _columns))
- {
- skip = true;
- break;
- }
- }
- if (!skip)
- finalUniqueKeys[entry.Key] = uk;
- }
- return finalUniqueKeys;
- }
- else
- {
- return uniqueKeys;
- }
- }
- }
-
private static bool AreSameColumns(ICollection<Column> col1, ICollection<Column> col2)
{
- if(col1.Count!=col2.Count)
+ if (col1.Count != col2.Count)
+ {
return false;
- bool result= true;
+ }
foreach (Column column in col1)
{
- if(!col2.Contains(column))
+ if (!col2.Contains(column))
+ {
return false;
+ }
}
foreach (Column column in col2)
{
if (!col1.Contains(column))
+ {
return false;
+ }
}
- return result;
+ return true;
}
- public bool HasPrimaryKey
- {
- get { return PrimaryKey != null; }
- }
-
- public string Catalog
- {
- get { return catalog; }
- set { catalog = value; }
- }
-
- public string Comment
- {
- get { return comment; }
- set { comment = value; }
- }
-
- public string Subselect
- {
- get { return subselect; }
- set { subselect = value; }
- }
-
- public IKeyValue IdentifierValue
- {
- get { return idValue; }
- set { idValue = value; }
- }
-
- public bool IsSubselect
- {
- get { return !string.IsNullOrEmpty(subselect); }
- }
-
- public bool IsPhysicalTable
- {
- get { return !IsSubselect && !IsAbstractUnionTable; }
- }
-
- public SchemaAction SchemaActions
- {
- get { return schemaActions; }
- set { schemaActions = value; }
- }
-
- public string RowId
- {
- get { return rowId; }
- set { rowId = value; }
- }
-
- public bool IsSchemaQuoted
- {
- get { return isSchemaQuoted; }
- }
-
public virtual string[] SqlCommentStrings(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
{
- List<string> comments = new List<string>();
+ var comments = new List<string>();
if (dialect.SupportsCommentOn)
{
string tableName = GetQualifiedName(dialect, defaultCatalog, defaultSchema);
if (!string.IsNullOrEmpty(comment))
{
- StringBuilder buf = new StringBuilder()
- .Append("comment on table ")
- .Append(tableName)
- .Append(" is '")
- .Append(comment)
- .Append("'");
+ StringBuilder buf =
+ new StringBuilder().Append("comment on table ").Append(tableName).Append(" is '").Append(comment).Append("'");
comments.Add(buf.ToString());
}
foreach (Column column in ColumnIterator)
@@ -970,13 +904,9 @@
string columnComment = column.Comment;
if (columnComment != null)
{
- StringBuilder buf = new StringBuilder()
- .Append("comment on column ")
- .Append(tableName)
- .Append('.')
- .Append(column.GetQuotedName(dialect))
- .Append(" is '")
- .Append(columnComment).Append("'");
+ StringBuilder buf =
+ new StringBuilder().Append("comment on column ").Append(tableName).Append('.').Append(
+ column.GetQuotedName(dialect)).Append(" is '").Append(columnComment).Append("'");
comments.Add(buf.ToString());
}
}
@@ -986,19 +916,17 @@
public virtual string SqlTemporaryTableCreateString(Dialect.Dialect dialect, IMapping mapping)
{
- StringBuilder buffer = new StringBuilder(dialect.CreateTemporaryTableString)
- .Append(' ')
- .Append(name)
- .Append(" (");
+ StringBuilder buffer = new StringBuilder(dialect.CreateTemporaryTableString).Append(' ').Append(name).Append(" (");
bool commaNeeded = false;
foreach (Column column in ColumnIterator)
{
- buffer.Append(column.GetQuotedName(dialect))
- .Append(' ');
+ buffer.Append(column.GetQuotedName(dialect)).Append(' ');
buffer.Append(column.GetSqlType(dialect, mapping));
if (commaNeeded)
+ {
buffer.Append(StringHelper.CommaSpace);
+ }
commaNeeded = true;
if (column.IsNullable)
@@ -1018,9 +946,7 @@
public override string ToString()
{
- StringBuilder buf = new StringBuilder()
- .Append(GetType().FullName)
- .Append('(');
+ StringBuilder buf = new StringBuilder().Append(GetType().FullName).Append('(');
if (Catalog != null)
{
buf.Append(Catalog + ".");
@@ -1035,37 +961,36 @@
public void ValidateColumns(Dialect.Dialect dialect, IMapping mapping, ITableMetadata tableInfo)
{
- var iter = this.ColumnIterator;
- foreach (var column in iter)
+ IEnumerable<Column> iter = ColumnIterator;
+ foreach (Column column in iter)
{
+ IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name);
- var columnInfo = tableInfo.GetColumnMetadata(column.Name);
-
if (columnInfo == null)
+ {
throw new HibernateException(string.Format("Missing column: {0} in {1}", column.Name,
- Table.Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name)));
+ Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name)));
+ }
else
{
//TODO: Add new method to ColumnMetadata :getTypeCode
- bool typesMatch = column.GetSqlType(dialect, mapping).ToLower()
- .StartsWith(columnInfo.TypeName.ToLower())
- ; //|| columnInfo.get() == column.GetSqlTypeCode(mapping);
+ bool typesMatch = column.GetSqlType(dialect, mapping).ToLower().StartsWith(columnInfo.TypeName.ToLower());
+ //|| columnInfo.get() == column.GetSqlTypeCode(mapping);
if (!typesMatch)
{
- throw new HibernateException(
- string.Format("Wrong column type in {0} for column {1}. Found: {2}, Expected {3}",
- Table.Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name),
- column.Name, columnInfo.TypeName.ToLower(), column.GetSqlType(dialect, mapping)));
+ throw new HibernateException(string.Format("Wrong column type in {0} for column {1}. Found: {2}, Expected {3}",
+ Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name),
+ column.Name, columnInfo.TypeName.ToLower(),
+ column.GetSqlType(dialect, mapping)));
}
}
}
-
}
public static string Qualify(string catalog, string schema, string table)
{
- StringBuilder qualifiedName = new StringBuilder(100);
+ var qualifiedName = new StringBuilder(100);
if (catalog != null)
{
qualifiedName.Append(catalog).Append('.');
@@ -1076,5 +1001,66 @@
}
return qualifiedName.Append(table).ToString();
}
+
+ #region Nested type: ForeignKeyKey
+
+ internal class ForeignKeyKey : IEqualityComparer<ForeignKeyKey>
+ {
+ internal List<Column> columns;
+ internal string referencedClassName;
+ internal List<Column> referencedColumns;
+
+ internal ForeignKeyKey(IEnumerable<Column> columns, string referencedClassName, IEnumerable<Column> referencedColumns)
+ {
+ this.referencedClassName = referencedClassName;
+ this.columns = new List<Column>(columns);
+ if (referencedColumns != null)
+ {
+ this.referencedColumns = new List<Column>(referencedColumns);
+ }
+ else
+ {
+ this.referencedColumns = new List<Column>();
+ }
+ }
+
+ #region IEqualityComparer<ForeignKeyKey> Members
+
+ public bool Equals(ForeignKeyKey x, ForeignKeyKey y)
+ {
+ // NH : Different implementation to prevent NH930 (look test)
+ return //y.referencedClassName.Equals(x.referencedClassName) &&
+ CollectionHelper.CollectionEquals<Column>(y.columns, x.columns)
+ && CollectionHelper.CollectionEquals<Column>(y.referencedColumns, x.referencedColumns);
+ }
+
+ public int GetHashCode(ForeignKeyKey obj)
+ {
+ int result = CollectionHelper.GetHashCode(obj.columns) ^ CollectionHelper.GetHashCode(obj.referencedColumns);
+ return result;
+ }
+
+ #endregion
+
+ public override int GetHashCode()
+ {
+ return GetHashCode(this);
+ }
+
+ public override bool Equals(object other)
+ {
+ var that = other as ForeignKeyKey;
+ if (that != null)
+ {
+ return Equals(this, that);
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+
+ #endregion
}
-}
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|