forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.cs
163 lines (146 loc) · 4.83 KB
/
Index.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// An Index in the database.
/// </summary>
[Serializable]
public class Index : IRelationalModel
{
private Table table;
private readonly List<Column> columns = new List<Column>();
private string name;
public static string BuildSqlCreateIndexString(Dialect.Dialect dialect, string name, Table table,
IEnumerable<Column> columns, bool unique, string defaultCatalog, string defaultSchema)
{
//TODO handle supportsNotNullUnique=false, but such a case does not exist in the wild so far
StringBuilder buf = new StringBuilder("create")
.Append(unique ? " unique" : "")
.Append(" index ")
.Append(dialect.QualifyIndexName ? name : StringHelper.Unqualify(name))
.Append(" on ")
.Append(table.GetQualifiedName(dialect, defaultCatalog, defaultSchema))
.Append(" (");
bool commaNeeded = false;
foreach (Column column in columns)
{
if (commaNeeded)
buf.Append(StringHelper.CommaSpace);
commaNeeded = true;
buf.Append(column.GetQuotedName(dialect));
}
buf.Append(")");
return buf.ToString();
}
public static string BuildSqlDropIndexString(Dialect.Dialect dialect, Table table, string name, string defaultCatalog, string defaultSchema)
{
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("drop index ")
.AppendLine(StringHelper.Qualify(table.GetQualifiedName(dialect, defaultCatalog, defaultSchema), name))
.Append(dialect.GetIfExistsDropConstraintEnd(catalog, schema, tableName, name))
.ToString();
}
/// <summary>
/// Generates the SQL string to create this Index in the database.
/// </summary>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to use for SQL rules.</param>
/// <param name="p"></param>
/// <param name="defaultCatalog"></param>
/// <param name="defaultSchema"></param>
/// <returns>
/// A string that contains the SQL to create this Index.
/// </returns>
public string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema)
{
var indexName = IsInherited ? Table.Name + Name : Name;
return BuildSqlCreateIndexString(dialect, indexName, Table, ColumnIterator, false, defaultCatalog, defaultSchema);
}
/// <summary>
/// Generates the SQL string to drop this Index 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 Index.
/// </returns>
public string SqlDropString(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
{
return BuildSqlDropIndexString(dialect, Table, Name, defaultCatalog, defaultSchema);
}
/// <summary>
/// Gets or sets the <see cref="Table"/> this Index is in.
/// </summary>
/// <value>
/// The <see cref="Table"/> this Index is in.
/// </value>
public Table Table
{
get { return table; }
set { table = value; }
}
/// <summary>
/// Gets an <see cref="ICollection"/> of <see cref="Column"/> objects that are
/// part of the Index.
/// </summary>
/// <value>
/// An <see cref="ICollection"/> of <see cref="Column"/> objects that are
/// part of the Index.
/// </value>
public IEnumerable<Column> ColumnIterator
{
get { return columns; }
}
public int ColumnSpan
{
get { return columns.Count; }
}
/// <summary>
/// Adds the <see cref="Column"/> to the <see cref="ICollection"/> of
/// Columns that are part of the Index.
/// </summary>
/// <param name="column">The <see cref="Column"/> to include in the Index.</param>
public void AddColumn(Column column)
{
if (!columns.Contains(column))
{
columns.Add(column);
}
}
public void AddColumns(IEnumerable<Column> extraColumns)
{
foreach (Column column in extraColumns)
AddColumn(column);
}
/// <summary>
/// Gets or sets the Name used to identify the Index in the database.
/// </summary>
/// <value>The Name used to identify the Index in the database.</value>
public string Name
{
get { return name; }
set { name = value; }
}
/// <summary>
/// Is this index inherited from the base class mapping
/// </summary>
public bool IsInherited { get; set; }
public bool ContainsColumn(Column column)
{
return columns.Contains(column);
}
public override string ToString()
{
return GetType().FullName + "(" + Name + ")";
}
}
}