forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraint.cs
211 lines (189 loc) · 6.86 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// Base class for relational constraints in the database.
/// </summary>
[Serializable]
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>
/// Generate a name hopefully unique using the table and column names.
/// Static so the name can be generated prior to creating the Constraint.
/// They're cached, keyed by name, in multiple locations.
/// </summary>
/// <param name="prefix">A name prefix for the generated name.</param>
/// <param name="table">The table for which the name is generated.</param>
/// <param name="referencedTable">The referenced table, if any.</param>
/// <param name="columns">The columns for which the name is generated.</param>
/// <returns>The generated name.</returns>
/// <remarks>Hybrid of Hibernate <c>Constraint.generateName</c> and
/// <c>NamingHelper.generateHashedFkName</c>.</remarks>
public static string GenerateName(
string prefix, Table table, Table referencedTable, IEnumerable<Column> columns)
{
// Use a concatenation that guarantees uniqueness, even if identical names
// exist between all table and column identifiers.
var sb = new StringBuilder("table`").Append(table.Name).Append("`");
if (referencedTable != null)
sb.Append("references`").Append(referencedTable.Name).Append("`");
// Ensure a consistent ordering of columns, regardless of the order
// they were bound.
foreach (var column in columns.OrderBy(c => c.CanonicalName))
{
sb.Append("column`").Append(column.CanonicalName).Append("`");
}
// Hash the generated name for avoiding collisions with user choosen names.
// This is not 100% reliable, as hashing may still have a chance of generating
// collisions.
// Hibernate uses MD5 here, which .Net standrad implementation is rejected by
// FIPS enabled machine. Better use a non-cryptographic hash.
var name = prefix + Hasher.HashToString(sb.ToString());
return name;
}
/// <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)
{
AddColumn(col);
}
}
public void AddColumns(IEnumerable<ISelectable> columnIterator)
{
AddColumns(columnIterator.OfType<Column>());
}
/// <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))
{
return null;
}
var catalog = Table.GetQuotedCatalog(dialect, defaultCatalog);
var schema = Table.GetQuotedSchema(dialect, defaultSchema);
var tableName = Table.GetQuotedName(dialect);
return new StringBuilder()
.AppendLine(dialect.GetIfExistsDropConstraint(catalog, schema, tableName, Name))
.Append("alter table ")
.Append(Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema))
.Append(" drop constraint ")
.AppendLine(Name)
.Append(dialect.GetIfExistsDropConstraintEnd(catalog, schema, tableName, Name))
.ToString();
}
/// <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))
{
return null;
}
StringBuilder buf = new StringBuilder("alter table ")
.Append(Table.GetQualifiedName(dialect, defaultCatalog, defaultSchema))
.Append(SqlConstraintString(dialect, Name, defaultCatalog, defaultSchema));
return buf.ToString();
}
#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 String ToString()
{
return string.Format("{0}({1}{2}) as {3}", GetType().FullName, Table.Name, StringHelper.CollectionToString(Columns), name);
}
}
}