-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathColumn.cs
488 lines (438 loc) · 13.1 KB
/
Column.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System;
using NHibernate.Dialect.Function;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// Represents the mapping to a column in a database.
/// </summary>
[Serializable]
public class Column : ISelectable, ICloneable
{
public const int DefaultLength = 255;
public const int DefaultPrecision = 19;
public const int DefaultScale = 2;
private int? _length;
private int? _precision;
private int? _scale;
private IValue _value;
private int _typeIndex;
private string _name;
private bool _nullable = true;
private bool _unique;
private string _sqlType;
private SqlType _sqlTypeCode;
private bool _quoted;
internal int UniqueInteger;
private string _checkConstraint;
private string _comment;
private string _defaultValue;
/// <summary>
/// Initializes a new instance of <see cref="Column"/>.
/// </summary>
public Column()
{
}
/// <summary>
/// Initializes a new instance of <see cref="Column"/>.
/// </summary>
/// <param name="columnName">The name of the column.</param>
public Column(string columnName)
{
Name = columnName;
}
/// <summary>
/// Gets or sets the length of the datatype in the database.
/// </summary>
/// <value>The length of the datatype in the database.</value>
public int Length
{
get { return _length.GetValueOrDefault(DefaultLength); }
set { _length = value; }
}
/// <summary>
/// Gets or sets the name of the column in the database.
/// </summary>
/// <value>
/// The name of the column in the database. The get does
/// not return a Quoted column name.
/// </value>
/// <remarks>
/// <p>
/// If a value is passed in that is wrapped by <c>`</c> then
/// NHibernate will Quote the column whenever SQL is generated
/// for it. How the column is quoted depends on the Dialect.
/// </p>
/// <p>
/// The value returned by the getter is not Quoted. To get the
/// column name in quoted form use <see cref="GetQuotedName(Dialect.Dialect)"/>.
/// </p>
/// </remarks>
public string Name
{
get { return _name; }
set
{
if (value[0] == '`')
{
_quoted = true;
_name = value.Substring(1, value.Length - 2);
}
else
{
_name = value;
}
}
}
public string CanonicalName
{
get { return _quoted ? _name : _name.ToLowerInvariant(); }
}
/// <summary>
/// Gets the name of this Column in quoted form if it is necessary.
/// </summary>
/// <param name="d">
/// The <see cref="Dialect.Dialect"/> that knows how to quote
/// the column name.
/// </param>
/// <returns>
/// The column name in a form that is safe to use inside of a SQL statement.
/// Quoted if it needs to be, not quoted if it does not need to be.
/// </returns>
public string GetQuotedName(Dialect.Dialect d)
{
return IsQuoted ? d.QuoteForColumnName(_name) : _name;
}
// Accommodate the one character suffix appended in AbstractCollectionPersister and
// the SelectFragment suffix up to 99 joins.
private const int _charactersLeftCount = 4;
/// <summary>
/// For any column name, generate an alias that is unique to that
/// column name, and also take Dialect.MaxAliasLength into account.
/// It keeps four characters left for accommodating additional suffixes.
/// </summary>
public string GetAlias(Dialect.Dialect dialect)
{
return GetAlias(dialect.MaxAliasLength);
}
private string GetAlias(int maxAliasLength)
{
var usableLength = maxAliasLength - _charactersLeftCount;
var name = CanonicalName;
string alias = name;
string suffix = UniqueInteger.ToString() + StringHelper.Underscore;
int lastLetter = StringHelper.LastIndexOfLetter(name);
if (lastLetter == -1)
{
alias = "column";
}
else if (lastLetter < name.Length - 1)
{
alias = name.Substring(0, lastLetter + 1);
}
// Updated logic ported from Hibernate's fix for HHH-8073.
// https://github.com/hibernate/hibernate-orm/commit/79073a98f0e4ed225fe4608b67594196f86d48d7
// To my mind it is weird - since the suffix is now always used, it
// seems "useRawName" is a misleading choice of variable name. For the same
// reason, the checks for "_quoted" and "rowid" looks redundant. If you remove
// those checks, then the double checks for total length can be reduced to one.
// But I will leave it like this for now to make it look similar. /Oskar 2016-08-20
bool useRawName = name.Length + suffix.Length <= usableLength &&
!_quoted &&
!"rowid".Equals(name, StringComparison.OrdinalIgnoreCase);
if (!useRawName)
{
if (suffix.Length >= usableLength)
{
throw new MappingException(
$"Unique suffix {suffix} length must be less than maximum {usableLength} characters.");
}
if (alias.Length + suffix.Length > usableLength)
alias = alias.Substring(0, usableLength - suffix.Length);
}
return alias + suffix;
}
/// <summary>
/// For any column name, generate an alias that is unique to that
/// column name and table, and also take Dialect.MaxAliasLength into account.
/// It keeps four characters left for accommodating additional suffixes.
/// </summary>
public string GetAlias(Dialect.Dialect dialect, Table table)
{
string suffix = table.UniqueInteger.ToString() + StringHelper.Underscore;
int maxAliasLength = dialect.MaxAliasLength - suffix.Length;
return GetAlias(maxAliasLength) + suffix;
}
/// <summary>
/// Gets or sets if the column can have null values in it.
/// </summary>
/// <value><see langword="true" /> if the column can have a null value in it.</value>
public bool IsNullable
{
get { return _nullable; }
set { _nullable = value; }
}
/// <summary>
/// Gets or sets the index of the column in the <see cref="IType"/>.
/// </summary>
/// <value>
/// The index of the column in the <see cref="IType"/>.
/// </value>
public int TypeIndex
{
get { return _typeIndex; }
set { _typeIndex = value; }
}
/// <summary>
/// Gets or sets if the column contains unique values.
/// </summary>
/// <value><see langword="true" /> if the column contains unique values.</value>
public bool IsUnique
{
get { return _unique; }
set { _unique = value; }
}
/// <summary>
/// Gets the name of the data type for the column.
/// </summary>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to use to get the valid data types.</param>
/// <param name="mapping"></param>
/// <returns>
/// The name of the data type for the column.
/// </returns>
/// <remarks>
/// If the mapping file contains a value of the attribute <c>sql-type</c> this will
/// return the string contained in that attribute. Otherwise it will use the
/// typename from the <see cref="Dialect.Dialect"/> of the <see cref="SqlType"/> object.
/// </remarks>
public string GetSqlType(Dialect.Dialect dialect, IMapping mapping)
{
return _sqlType ?? GetDialectTypeName(dialect, mapping);
}
private string GetDialectTypeName(Dialect.Dialect dialect, IMapping mapping)
{
if (IsCaracteristicsDefined())
{
// NH-1070: the size should be 0 if the precision or scale is defined.
return dialect.GetTypeName(
GetSqlTypeCode(mapping),
IsPrecisionDefined() || IsScaleDefined() ? 0 : Length,
Precision, Scale);
}
else
return dialect.GetTypeName(GetSqlTypeCode(mapping));
}
#region System.Object Members
/// <summary>
/// Determines if this instance of <see cref="Column"/> and a specified object,
/// which must be a <b>Column</b> can be considered the same.
/// </summary>
/// <param name="obj">An <see cref="Object"/> that should be a <see cref="Column"/>.</param>
/// <returns>
/// <see langword="true" /> if the name of this Column and the other Column are the same,
/// otherwise <see langword="false" />.
/// </returns>
public override bool Equals(object obj)
{
Column columnObj = obj as Column;
return columnObj != null && Equals(columnObj);
}
/// <summary>
/// Determines if this instance of <see cref="Column"/> and the specified Column
/// can be considered the same.
/// </summary>
/// <param name="column">A <see cref="Column"/> to compare to this Column.</param>
/// <returns>
/// <see langword="true" /> if the name of this Column and the other Column are the same,
/// otherwise <see langword="false" />.
/// </returns>
public bool Equals(Column column)
{
if (null == column)
return false;
if (ReferenceEquals(this, column))
return true;
return IsQuoted ? _name.Equals(column._name) : _name.Equals(column._name, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
public override int GetHashCode()
{
return IsQuoted ? _name.GetHashCode() : _name.ToLowerInvariant().GetHashCode();
}
#endregion
/// <summary>
/// Gets or sets the sql data type name of the column.
/// </summary>
/// <value>
/// The sql data type name of the column.
/// </value>
/// <remarks>
/// This is usually read from the <c>sql-type</c> attribute.
/// </remarks>
public string SqlType
{
get { return _sqlType; }
set { _sqlType = value; }
}
/// <summary>
/// Gets or sets if the column needs to be quoted in SQL statements.
/// </summary>
/// <value><see langword="true" /> if the column is quoted.</value>
public bool IsQuoted
{
get { return _quoted; }
set { _quoted = value; }
}
/// <summary>
/// Gets or sets whether the column is unique.
/// </summary>
public bool Unique
{
get { return _unique; }
set { _unique = value; }
}
/// <summary>
/// Gets or sets a check constraint on the column
/// </summary>
public string CheckConstraint
{
get { return _checkConstraint; }
set { _checkConstraint = value; }
}
/// <summary>
/// Do we have a check constraint?
/// </summary>
public bool HasCheckConstraint
{
get { return !string.IsNullOrEmpty(_checkConstraint); }
}
public string Text
{
get { return Name; }
}
public string GetText(Dialect.Dialect dialect)
{
return GetQuotedName(dialect);
}
public bool IsFormula
{
get { return false; }
}
public int Precision
{
get { return _precision.GetValueOrDefault(DefaultPrecision); }
set { _precision = value; }
}
public int Scale
{
get { return _scale.GetValueOrDefault(DefaultScale); }
set { _scale = value; }
}
public IValue Value
{
get { return _value; }
set { _value = value; }
}
/// <summary>
/// The underlying columns SqlType.
/// </summary>
/// <remarks>
/// If null, it is because the sqltype code is unknown.
///
/// Use <see cref="GetSqlTypeCode(IMapping)"/> to retreive the sqltypecode used
/// for the columns associated Value/Type.
/// </remarks>
public SqlType SqlTypeCode
{
get { return _sqlTypeCode; }
set { _sqlTypeCode = value; }
}
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
public string DefaultValue
{
get { return _defaultValue; }
set { _defaultValue = value; }
}
public string GetTemplate(Dialect.Dialect dialect, SQLFunctionRegistry functionRegistry)
{
return GetQuotedName(dialect);
}
public override string ToString()
{
return string.Format("{0}({1})", GetType().FullName, _name);
}
public SqlType GetSqlTypeCode(IMapping mapping)
{
IType type = Value.Type;
try
{
SqlType sqltc = type.SqlTypes(mapping)[TypeIndex];
if (SqlTypeCode != null && !ReferenceEquals(SqlTypeCode, sqltc))
{
throw new MappingException(string.Format("SQLType code's does not match. mapped as {0} but is {1}", sqltc, SqlTypeCode));
}
return sqltc;
}
catch (Exception e)
{
throw new MappingException(string.Format("Could not determine type for column {0} of type {1}: {2}",
_name, type.GetType().FullName, e.GetType().FullName), e);
}
}
/// <summary>returns quoted name as it would be in the mapping file. </summary>
public string GetQuotedName()
{
return _quoted ? '`' + _name + '`' : _name;
}
public bool IsCaracteristicsDefined()
{
return IsLengthDefined() || IsPrecisionDefined() || IsScaleDefined();
}
public bool IsPrecisionDefined()
{
return _precision.HasValue;
}
public bool IsScaleDefined()
{
return _scale.HasValue;
}
public bool IsLengthDefined()
{
return _length.HasValue;
}
#region ICloneable Members
/// <summary> Shallow copy, the value is not copied</summary>
public object Clone()
{
Column copy = new Column();
if (_length.HasValue)
copy.Length = Length;
if (_precision.HasValue)
copy.Precision = Precision;
if (_scale.HasValue)
copy.Scale = Scale;
copy.Value = _value;
copy.TypeIndex = _typeIndex;
copy.Name = GetQuotedName();
copy.IsNullable = _nullable;
copy.Unique = _unique;
copy.SqlType = _sqlType;
copy.SqlTypeCode = _sqlTypeCode;
copy.UniqueInteger = UniqueInteger; //usually useless
copy.CheckConstraint = _checkConstraint;
copy.Comment = _comment;
copy.DefaultValue = _defaultValue;
return copy;
}
#endregion
}
}