forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraint.cs
168 lines (152 loc) · 4.9 KB
/
Constraint.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// Base class for relational constraints in the database.
/// </summary>
public abstract class Constraint : IRelationalModel
{
private string name;
private readonly List<Column> columns = new List<Column>();
private Table table;
/// <summary>
/// Gets or sets the Name used to identify the constraint in the database.
/// </summary>
/// <value>The Name used to identify the constraint in the database.</value>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// Gets an <see cref="IEnumerable{Column}"/> of <see cref="Column"/> objects that are part of the constraint.
/// </summary>
/// <value>
/// An <see cref="IEnumerable{Column}"/> of <see cref="Column"/> objects that are part of the constraint.
/// </value>
public IEnumerable<Column> ColumnIterator
{
get { return columns; }
}
/// <summary>
/// Adds the <see cref="Column"/> to the <see cref="ICollection"/> of
/// Columns that are part of the constraint.
/// </summary>
/// <param name="column">The <see cref="Column"/> to include in the Constraint.</param>
public void AddColumn(Column column)
{
if (!columns.Contains(column))
columns.Add(column);
}
public void AddColumns(IEnumerable<Column> columnIterator)
{
foreach (Column col in columnIterator)
{
if (!col.IsFormula)
AddColumn(col);
}
}
/// <summary>
/// Gets the number of columns that this Constraint contains.
/// </summary>
/// <value>
/// The number of columns that this Constraint contains.
/// </value>
public int ColumnSpan
{
get { return columns.Count; }
}
public IList<Column> Columns
{
get{return columns;}
}
/// <summary>
/// Gets or sets the <see cref="Table"/> this Constraint is in.
/// </summary>
/// <value>
/// The <see cref="Table"/> this Constraint is in.
/// </value>
public Table Table
{
get { return table; }
set { table = value; }
}
#region IRelationModel Members
/// <summary>
/// Generates 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>
/// <param name="defaultCatalog"></param>
/// <returns>
/// A string that contains the SQL to drop this Constraint.
/// </returns>
public virtual string SqlDropString(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
{
if (IsGenerated(dialect))
{
string ifExists = dialect.GetIfExistsDropConstraint(Table, Name);
string drop =
string.Format("alter table {0} drop constraint {1}", Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), Name);
string end = dialect.GetIfExistsDropConstraintEnd(Table, Name);
return ifExists + System.Environment.NewLine + drop + System.Environment.NewLine + end;
}
else
{
return null;
}
}
/// <summary>
/// Generates the SQL string to create this Constraint 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>
/// <param name="defaultCatalog"></param>
/// <returns>
/// A string that contains the SQL to create this Constraint.
/// </returns>
public virtual string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema)
{
if (IsGenerated(dialect))
{
string constraintString = SqlConstraintString(dialect, Name, defaultCatalog, defaultSchema);
StringBuilder buf = new StringBuilder("alter table ")
.Append(Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema))
.Append(constraintString);
return buf.ToString();
}
else
{
return null;
}
}
#endregion
/// <summary>
/// When implemented by a class, generates the SQL string to create the named
/// 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>
/// <param name="defaultCatalog"></param>
/// <returns>
/// A string that contains the SQL to create the named Constraint.
/// </returns>
public abstract string SqlConstraintString(Dialect.Dialect d, string constraintName, string defaultCatalog, string defaultSchema);
public virtual bool IsGenerated(Dialect.Dialect dialect)
{
return true;
}
public override System.String ToString()
{
return
string.Format("{0}({1}{2}) as {3}", GetType().FullName, Table.Name,
StringHelper.CollectionToString((ICollection) Columns), name);
}
}
}