-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathUniqueKey.cs
115 lines (105 loc) · 3.71 KB
/
UniqueKey.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
using System;
using System.Text;
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// An Unique Key constraint in the database.
/// </summary>
[Serializable]
public class UniqueKey : Constraint
{
/// <summary>
/// Generates the SQL string to create the Unique Key Constraint in the database.
/// </summary>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param>
/// <returns> A string that contains the SQL to create the Unique Key Constraint. </returns>
public string SqlConstraintString(Dialect.Dialect dialect)
{
StringBuilder buf = new StringBuilder("unique (");
bool commaNeeded = false;
bool nullable = false;
foreach (Column column in ColumnIterator)
{
if (!nullable && column.IsNullable)
nullable = true;
if (commaNeeded)
buf.Append(StringHelper.CommaSpace);
commaNeeded = true;
buf.Append(column.GetQuotedName(dialect));
}
//do not add unique constraint on DB not supporting unique and nullable columns
return !nullable || dialect.SupportsNullInUnique ? buf.Append(StringHelper.ClosedParen).ToString() : null;
}
/// <summary>
/// Generates the SQL string to create the Unique Key Constraint in the database.
/// </summary>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param>
/// <param name="constraintName"></param>
/// <param name="defaultCatalog"></param>
/// <param name="defaultSchema"></param>
/// <returns>
/// A string that contains the SQL to create the Unique Key Constraint.
/// </returns>
public override string SqlConstraintString(Dialect.Dialect dialect, string constraintName, string defaultCatalog, string defaultSchema)
{
StringBuilder buf = new StringBuilder(dialect.GetAddPrimaryKeyConstraintString(constraintName))
.Append(StringHelper.OpenParen);
bool commaNeeded = false;
bool nullable = false;
foreach (Column column in ColumnIterator)
{
if (!nullable && column.IsNullable)
nullable = true;
if (commaNeeded)
buf.Append(StringHelper.CommaSpace);
commaNeeded = true;
buf.Append(column.GetQuotedName(dialect));
}
return !nullable || dialect.SupportsNullInUnique
? buf.Append(StringHelper.ClosedParen).Replace("primary key", "unique").ToString()
: null;
}
public override string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema)
{
if (dialect.SupportsUniqueConstraintInCreateAlterTable)
{
return base.SqlCreateString(dialect, p, defaultCatalog, defaultSchema);
}
else
{
return Index.BuildSqlCreateIndexString(dialect, Name, Table, ColumnIterator, true, defaultCatalog, defaultSchema);
}
}
#region IRelationalModel Members
/// <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="defaultCatalog"></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 defaultCatalog, string defaultSchema)
{
if (dialect.SupportsUniqueConstraintInCreateAlterTable)
return base.SqlDropString(dialect, defaultCatalog, defaultSchema);
else
return Index.BuildSqlDropIndexString(dialect, Table, Name, defaultCatalog, defaultSchema);
}
#endregion
public override bool IsGenerated(Dialect.Dialect dialect)
{
if (dialect.SupportsNullInUnique)
return true;
foreach (Column column in ColumnIterator)
{
if (column.IsNullable)
return false;
}
return true;
}
}
}