From: Michael D. <mik...@us...> - 2004-04-10 05:19:35
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28379/NHibernate/Mapping Modified Files: Column.cs Constraint.cs ForeignKey.cs Index.cs PrimaryKey.cs Table.cs UniqueKey.cs Log Message: Fixed to support Quoted Tables and Columns - ie in MsSql the use of [ and ] to surround table names/columns. Index: Index.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/Index.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Index.cs 27 Feb 2003 23:33:28 -0000 1.2 --- Index.cs 10 Apr 2004 05:06:02 -0000 1.3 *************** *** 17,24 **** .Append( dialect.QualifyIndexName ? name : StringHelper.Unqualify(name) ) .Append(" on ") ! .Append( table.QualifiedName ) .Append(" ("); for(int i=0; i<columns.Count; i++) { ! buf.Append( ((Column)columns[i]).Name); if (i<columns.Count-1) buf.Append(StringHelper.CommaSpace); } --- 17,24 ---- .Append( dialect.QualifyIndexName ? name : StringHelper.Unqualify(name) ) .Append(" on ") ! .Append( table.GetQualifiedName(dialect)) .Append(" ("); for(int i=0; i<columns.Count; i++) { ! buf.Append( ((Column)columns[i]).GetQuotedName(dialect) ); if (i<columns.Count-1) buf.Append(StringHelper.CommaSpace); } *************** *** 28,32 **** public string SqlDropString(Dialect.Dialect dialect) { ! return "drop index " + table.QualifiedName + StringHelper.Dot + name; } --- 28,32 ---- public string SqlDropString(Dialect.Dialect dialect) { ! return "drop index " + table.GetQualifiedName(dialect) + StringHelper.Dot + name; } Index: ForeignKey.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/ForeignKey.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ForeignKey.cs 21 Mar 2003 22:30:19 -0000 1.3 --- ForeignKey.cs 10 Apr 2004 05:06:02 -0000 1.4 *************** *** 14,26 **** int i=0; foreach(Column col in referencedTable.PrimaryKey.ColumnCollection) { ! refcols[i] = col.Name; i++; } i=0; foreach(Column col in ColumnCollection) { ! cols[i] = col.Name; i++; } ! return d.GetAddForeignKeyConstraintString(constraintName, cols, referencedTable.QualifiedName, refcols); } --- 14,26 ---- int i=0; foreach(Column col in referencedTable.PrimaryKey.ColumnCollection) { ! refcols[i] = col.GetQuotedName(d); i++; } i=0; foreach(Column col in ColumnCollection) { ! cols[i] = col.GetQuotedName(d); i++; } ! return d.GetAddForeignKeyConstraintString(constraintName, cols, referencedTable.GetQualifiedName(d), refcols); } Index: Constraint.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/Constraint.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Constraint.cs 27 Feb 2003 23:33:28 -0000 1.2 --- Constraint.cs 10 Apr 2004 05:06:02 -0000 1.3 *************** *** 36,45 **** public string SqlDropString(Dialect.Dialect dialect) { ! return "alter table " + Table.QualifiedName + " drop constraint " + Name; } public string SqlCreateString(Dialect.Dialect dialect, IMapping p) { StringBuilder buf = new StringBuilder("alter table ") ! .Append( Table.QualifiedName ) .Append( SqlConstraintString( dialect, Name ) ); return buf.ToString(); --- 36,45 ---- public string SqlDropString(Dialect.Dialect dialect) { ! return "alter table " + Table.GetQualifiedName(dialect)+ " drop constraint " + Name; } public string SqlCreateString(Dialect.Dialect dialect, IMapping p) { StringBuilder buf = new StringBuilder("alter table ") ! .Append( Table.GetQualifiedName(dialect) ) .Append( SqlConstraintString( dialect, Name ) ); return buf.ToString(); Index: Column.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/Column.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Column.cs 10 Feb 2004 18:35:30 -0000 1.4 --- Column.cs 10 Apr 2004 05:06:02 -0000 1.5 *************** *** 21,24 **** --- 21,25 ---- private bool unique = false; private string sqlType; + private bool quoted = false; internal int uniqueInteger; *************** *** 35,41 **** public string Name { get { return name; } ! set { name = value; } } public string Alias { get { --- 36,59 ---- public string Name { get { return name; } ! set ! { ! if (value[0] == '`') ! { ! quoted = true; ! name = value.Substring(1, value.Length - 2); ! } ! else ! { ! name = value; ! } ! } } + public string GetQuotedName(Dialect.Dialect d) + { + return quoted ? + d.OpenQuote + name + d.CloseQuote : + name; + } public string Alias { get { Index: PrimaryKey.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/PrimaryKey.cs,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** PrimaryKey.cs 10 Feb 2004 18:35:30 -0000 1.3 --- PrimaryKey.cs 10 Apr 2004 05:06:02 -0000 1.4 *************** *** 13,17 **** int i=0; foreach(Column col in ColumnCollection) { ! buf.Append(col.Name); if (i < ColumnCollection.Count-1) buf.Append(StringHelper.CommaSpace); i++; --- 13,17 ---- int i=0; foreach(Column col in ColumnCollection) { ! buf.Append(col.GetQuotedName(d)); if (i < ColumnCollection.Count-1) buf.Append(StringHelper.CommaSpace); i++; *************** *** 26,30 **** int i=0; foreach(Column col in ColumnCollection) { ! buf.Append( col.Name); if (i < ColumnCollection.Count - 1) buf.Append(StringHelper.CommaSpace); i++; --- 26,30 ---- int i=0; foreach(Column col in ColumnCollection) { ! buf.Append( col.GetQuotedName(d) ); if (i < ColumnCollection.Count - 1) buf.Append(StringHelper.CommaSpace); i++; Index: Table.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/Table.cs,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Table.cs 10 Feb 2004 18:35:30 -0000 1.6 --- Table.cs 10 Apr 2004 05:06:02 -0000 1.7 *************** *** 16,19 **** --- 16,20 ---- private string name; + private bool quoted; private string schema; private SequencedHashMap columns = new SequencedHashMap(); *************** *** 30,40 **** } ! /// <summary> ! /// Returns the QualifiedName for the table by combining the Schema and Name property. ! /// </summary> ! public string QualifiedName { ! get { return (schema == null) ? name : schema + StringHelper.Dot + name; } } /// <summary> /// Returns the QualifiedName for the table using the specified Qualifier --- 31,48 ---- } ! public string GetQualifiedName(Dialect.Dialect dialect) ! { ! string quotedName = GetQuotedName(dialect); ! return schema==null ? quotedName : schema + StringHelper.Dot + quotedName; } + // /// <summary> + // /// Returns the QualifiedName for the table by combining the Schema and Name property. + // /// </summary> + // public string QualifiedName { + // get { return (schema == null) ? name : schema + StringHelper.Dot + name; } + // } + + /// <summary> /// Returns the QualifiedName for the table using the specified Qualifier *************** *** 43,55 **** /// <returns>A String representing the Qualified name.</returns> /// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks> ! public string GetQualifiedName(string defaultQualifier) { ! return (schema==null) ? ( (defaultQualifier==null) ? name : defaultQualifier + StringHelper.Dot + name ) : QualifiedName; } public string Name { get { return name; } ! set { name = value; } } public Column GetColumn(int n) { IEnumerator iter = columns.Values.GetEnumerator(); --- 51,81 ---- /// <returns>A String representing the Qualified name.</returns> /// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks> ! public string GetQualifiedName(Dialect.Dialect dialect, string defaultQualifier) { ! string quotedName = GetQuotedName(dialect); ! return (schema==null) ? ( (defaultQualifier==null) ? quotedName : defaultQualifier + StringHelper.Dot + name ) : GetQualifiedName(dialect); } public string Name { get { return name; } ! set ! { ! if (value[0]=='`') ! { ! quoted = true; ! name = value.Substring(1, value.Length-2); ! } ! else ! { ! name = value; ! } ! } } + public string GetQuotedName(Dialect.Dialect dialect) + { + return quoted ? + dialect.OpenQuote + name + dialect.CloseQuote : + name; + } public Column GetColumn(int n) { IEnumerator iter = columns.Values.GetEnumerator(); *************** *** 92,96 **** if (buf.Length != 0) buf.Append(StringHelper.CommaSpace); ! buf.Append(col.Name).Append(' ').Append(col.GetSqlType(dialect, p)); if (col.IsUnique && dialect.SupportsUnique) { buf.Append(" unique"); --- 118,124 ---- if (buf.Length != 0) buf.Append(StringHelper.CommaSpace); ! buf.Append(col.GetQuotedName(dialect)) ! .Append(' ') ! .Append(col.GetSqlType(dialect, p)); if (col.IsUnique && dialect.SupportsUnique) { buf.Append(" unique"); *************** *** 102,106 **** return null; ! return new StringBuilder("alter table ").Append(QualifiedName).Append(" add ").Append(buf).ToString(); } --- 130,134 ---- return null; ! return new StringBuilder("alter table ").Append(GetQualifiedName(dialect)).Append(" add ").Append(buf).ToString(); } *************** *** 108,112 **** public string SqlCreateString(Dialect.Dialect dialect, IMapping p) { StringBuilder buf = new StringBuilder("create table ") ! .Append( QualifiedName ) .Append( " ("); --- 136,140 ---- public string SqlCreateString(Dialect.Dialect dialect, IMapping p) { StringBuilder buf = new StringBuilder("create table ") ! .Append( GetQualifiedName(dialect) ) .Append( " ("); *************** *** 117,121 **** if (primaryKey != null && identityColumn ) { foreach(Column col in primaryKey.ColumnCollection) { ! pkname = col.Name; //should only go through this loop once } } --- 145,149 ---- if (primaryKey != null && identityColumn ) { foreach(Column col in primaryKey.ColumnCollection) { ! pkname = col.GetQuotedName(dialect); //should only go through this loop once } } *************** *** 123,131 **** foreach(Column col in ColumnCollection) { i++; ! buf.Append( col.Name) .Append(' ') .Append( col.GetSqlType(dialect, p) ); ! if ( identityColumn && col.Name.Equals(pkname) ) { buf.Append(' ') .Append( dialect.IdentityColumnString); --- 151,159 ---- 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); *************** *** 161,165 **** public string SqlDropString(Dialect.Dialect dialect) { ! return "drop table " + QualifiedName + dialect.CascadeConstraintsString; } --- 189,193 ---- public string SqlDropString(Dialect.Dialect dialect) { ! return "drop table " + GetQualifiedName(dialect) + dialect.CascadeConstraintsString; } *************** *** 211,234 **** public string UniqueColumnString(ICollection col) { ! string uniqueName; int result = 0; foreach(object obj in col) { ! result += obj.GetHashCode(); ! } ! /* ! * I was running into the problem of FKs being genrated with negative numbers. ! * So the sql being sent to generate the FK was "FK-1234" - SqlServer2000 does ! * not like that. So I'll change the "-" to an "_" to try and workaround that ! * problem ! */ ! //return ( Integer.toHexString( name.hashCode() ) + Integer.toHexString(result) ).toUpperCase(); ! if(result < 0) { ! result = result * -1; ! uniqueName = name + "_" + result.ToString(); ! } ! else { ! uniqueName = name + result.ToString(); } ! return uniqueName; //name + result.ToString(); } --- 239,254 ---- public string UniqueColumnString(ICollection col) { ! int result = 0; + foreach(object obj in col) { ! ! // this is marked as unchecked because the GetHashCode could potentially ! // cause an integer overflow. This way if there is an overflow it will ! // just roll back over. ! unchecked{ result += obj.GetHashCode(); } } ! ! return ( name.GetHashCode().ToString("X") + result.GetHashCode().ToString("X") ); } Index: UniqueKey.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Mapping/UniqueKey.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** UniqueKey.cs 27 Feb 2003 23:33:28 -0000 1.2 --- UniqueKey.cs 10 Apr 2004 05:06:02 -0000 1.3 *************** *** 5,32 **** using NHibernate.Dialect; ! ! namespace NHibernate.Mapping { ! public class UniqueKey : Constraint { ! public string SqlConstraintString(Dialect.Dialect d) { StringBuilder buf = new StringBuilder(" unique ("); ! int i=0; ! foreach(Column col in ColumnCollection) { ! buf.Append( col.Name); ! if (i < ColumnCollection.Count-1) buf.Append(StringHelper.CommaSpace); } return buf.Append(StringHelper.ClosedParen).ToString(); } ! public override string SqlConstraintString(Dialect.Dialect d, string constraintName) { StringBuilder buf = new StringBuilder( d.GetAddPrimaryKeyConstraintString(constraintName)) .Append('('); ! int i=0; ! foreach(Column col in ColumnCollection) { ! buf.Append( col.Name); ! if (i < ColumnCollection.Count - 1) buf.Append(StringHelper.CommaSpace); } return StringHelper.Replace( buf.Append(StringHelper.ClosedParen).ToString(), "primary key", "unique" ); } --- 5,48 ---- using NHibernate.Dialect; ! namespace NHibernate.Mapping ! { ! public class UniqueKey : Constraint ! { ! public string SqlConstraintString(Dialect.Dialect d) ! { StringBuilder buf = new StringBuilder(" unique ("); ! bool commaNeeded = false; ! ! foreach(Column col in ColumnCollection) ! { ! if(commaNeeded) buf.Append( StringHelper.CommaSpace ); ! commaNeeded = true; ! ! buf.Append( col.GetQuotedName(d) ); ! } + return buf.Append(StringHelper.ClosedParen).ToString(); } ! public override string SqlConstraintString(Dialect.Dialect d, string constraintName) ! { StringBuilder buf = new StringBuilder( d.GetAddPrimaryKeyConstraintString(constraintName)) .Append('('); ! ! bool commaNeeded = false; ! ! foreach(Column col in ColumnCollection) ! { ! if(commaNeeded) buf.Append( StringHelper.CommaSpace ); ! commaNeeded = true; ! ! buf.Append( col.GetQuotedName(d) ); ! } + return StringHelper.Replace( buf.Append(StringHelper.ClosedParen).ToString(), "primary key", "unique" ); } |