forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeignKey.cs
116 lines (101 loc) · 3.45 KB
/
ForeignKey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System.Collections;
namespace NHibernate.Mapping
{
/// <summary>
/// A Foreign Key constraint in the database.
/// </summary>
public class ForeignKey : Constraint
{
private Table referencedTable;
private System.Type referencedClass;
/// <summary>
/// Initializes a new instance of the <see cref="ForeignKey"/> class.
/// </summary>
public ForeignKey()
{
}
/// <summary>
/// Generates the SQL string to create the named Foreign Key Constraint in the database.
/// </summary>
/// <param name="d">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param>
/// <param name="constraintName">The name to use as the identifier of the constraint in the database.</param>
/// <param name="defaultSchema"></param>
/// <returns>
/// A string that contains the SQL to create the named Foreign Key Constraint.
/// </returns>
public override string SqlConstraintString(Dialect.Dialect d, string constraintName, string defaultSchema)
{
string[] cols = new string[ColumnSpan];
string[] refcols = new string[ColumnSpan];
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, defaultSchema), refcols);
}
/// <summary>
/// Gets or sets the <see cref="Table"/> that the Foreign Key is referencing.
/// </summary>
/// <value>The <see cref="Table"/> the Foreign Key is referencing.</value>
/// <exception cref="MappingException">
/// Thrown when the number of columns in this Foreign Key is not the same
/// amount of columns as the Primary Key in the ReferencedTable.
/// </exception>
public Table ReferencedTable
{
get { return referencedTable; }
set
{
if (value.PrimaryKey.ColumnSpan != ColumnSpan)
{
string message = "Foreign key in table {0} must have same number of columns as referenced primary key in table {1}";
throw new MappingException(string.Format(message, this.Table.Name, value.Name));
}
IEnumerator fkCols = ColumnCollection.GetEnumerator();
IEnumerator pkCols = value.PrimaryKey.ColumnCollection.GetEnumerator();
while (fkCols.MoveNext() && pkCols.MoveNext())
{
((Column) fkCols.Current).Length = ((Column) pkCols.Current).Length;
}
this.referencedTable = value;
}
}
/// <summary>
/// Gets or sets the <see cref="System.Type"/> that this Foreign Key is referencing.
/// </summary>
/// <value>
/// The <see cref="System.Type"/> that this Foreign Key is referencing.
/// </value>
public System.Type ReferencedClass
{
get { return referencedClass; }
set { referencedClass = value; }
}
#region IRelationalModel Memebers
/// <summary>
/// Get the SQL string to drop this Constraint in the database.
/// </summary>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param>
/// <param name="defaultSchema"></param>
/// <returns>
/// A string that contains the SQL to drop this Constraint.
/// </returns>
public override string SqlDropString(Dialect.Dialect dialect, string defaultSchema)
{
// TODO: NH-421
return
string.Format("alter table {0} {1}", Table.GetQualifiedName(dialect, defaultSchema),
dialect.GetDropForeignKeyConstraintString(Name));
}
#endregion
}
}