forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.cs
1115 lines (992 loc) · 29.7 KB
/
Table.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Dialect.Schema;
using NHibernate.Engine;
using NHibernate.Util;
namespace NHibernate.Mapping
{
[Flags]
public enum SchemaAction
{
None = 0,
Drop = 1,
Update = 2,
Export = 4,
Validate = 8,
All = Drop | Update | Export | Validate
}
/// <summary>
/// Represents a Table in a database that an object gets mapped against.
/// </summary>
[Serializable]
public class Table : IRelationalModel
{
private readonly List<string> checkConstraints = new List<string>();
private readonly LinkedHashMap<string, Column> columns = new LinkedHashMap<string, Column>();
private readonly Dictionary<ForeignKeyKey, ForeignKey> foreignKeys = new Dictionary<ForeignKeyKey, ForeignKey>();
private readonly Dictionary<string, Index> indexes = new Dictionary<string, Index>();
private int? uniqueInteger;
private readonly Dictionary<string, UniqueKey> uniqueKeys = new Dictionary<string, UniqueKey>();
private string catalog;
private string comment;
private bool hasDenormalizedTables;
private IKeyValue idValue;
private bool isAbstract;
private bool isSchemaQuoted;
private bool isCatalogQuoted;
private string name;
private bool quoted;
private string schema;
private SchemaAction schemaActions = SchemaAction.All;
private string subselect;
/// <summary>
/// Initializes a new instance of <see cref="Table"/>.
/// </summary>
public Table()
{
}
public Table(string name) : this()
{
Name = name;
}
/// <summary>
/// Gets or sets the name of the Table in the database.
/// </summary>
/// <value>
/// The name of the Table in the database. The get does
/// not return a Quoted Table name.
/// </value>
/// <remarks>
/// <p>
/// If a value is passed in that is wrapped by <c>`</c> then
/// NHibernate will Quote the Table whenever SQL is generated
/// for it. How the Table 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;
}
}
}
/// <summary>
/// Gets the number of columns that this Table contains.
/// </summary>
/// <value>
/// The number of columns that this Table contains.
/// </value>
public int ColumnSpan
{
get { return columns.Count; }
}
/// <summary>
/// Gets an <see cref="IEnumerable"/> of <see cref="Column"/> objects that
/// are part of the Table.
/// </summary>
/// <value>
/// An <see cref="IEnumerable"/> of <see cref="Column"/> objects that are
/// part of the Table.
/// </value>
public virtual IEnumerable<Column> ColumnIterator
{
get { return columns.Values; }
}
/// <summary>
/// Gets an <see cref="ICollection"/> of <see cref="Index"/> objects that
/// are part of the Table.
/// </summary>
/// <value>
/// An <see cref="ICollection"/> of <see cref="Index"/> objects that are
/// part of the Table.
/// </value>
public virtual IEnumerable<Index> IndexIterator
{
get { return indexes.Values; }
}
/// <summary>
/// Gets an <see cref="IEnumerable"/> of <see cref="ForeignKey"/> objects that
/// are part of the Table.
/// </summary>
/// <value>
/// An <see cref="IEnumerable"/> of <see cref="ForeignKey"/> objects that are
/// part of the Table.
/// </value>
public IEnumerable<ForeignKey> ForeignKeyIterator
{
get { return foreignKeys.Values; }
}
/// <summary>
/// Gets an <see cref="IEnumerable"/> of <see cref="UniqueKey"/> objects that
/// are part of the Table.
/// </summary>
/// <value>
/// An <see cref="IEnumerable"/> of <see cref="UniqueKey"/> objects that are
/// part of the Table.
/// </value>
public virtual IEnumerable<UniqueKey> UniqueKeyIterator
{
get { return uniqueKeys.Values; }
}
/// <summary>
/// Gets or sets the <see cref="PrimaryKey"/> of the Table.
/// </summary>
/// <value>The <see cref="PrimaryKey"/> of the Table.</value>
public virtual PrimaryKey PrimaryKey { get; set; }
/// <summary>
/// Gets or sets the schema the table is in.
/// </summary>
/// <value>
/// The schema the table is in or <see langword="null" /> if no schema is specified.
/// </value>
public string Schema
{
get { return schema; }
set
{
if (value != null && value[0] == '`')
{
isSchemaQuoted = true;
schema = value.Substring(1, value.Length - 2);
}
else
{
schema = value;
}
}
}
/// <summary>
/// Gets the unique number of the Table.
/// Used for SQL alias generation
/// </summary>
/// <value>The unique number of the Table.</value>
public int UniqueInteger
{
get { return uniqueInteger ?? throw new InvalidOperationException(nameof(UniqueInteger) + " has not been supplied"); }
set { uniqueInteger = 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; }
}
public IEnumerable<string> CheckConstraintsIterator
{
get { return checkConstraints; }
}
public bool IsAbstractUnionTable
{
get { return HasDenormalizedTables && isAbstract; }
}
public bool HasDenormalizedTables
{
get { return hasDenormalizedTables; }
}
public bool IsAbstract
{
get { return isAbstract; }
set { isAbstract = value; }
}
internal IDictionary<string, UniqueKey> UniqueKeys
{
get
{
if (uniqueKeys.Count > 1)
{
//deduplicate unique constraints sharing the same columns
//this is needed by Hibernate Annotations since it creates automagically
// unique constraints for the user
var finalUniqueKeys = new Dictionary<string, UniqueKey>(uniqueKeys.Count);
foreach (var entry in uniqueKeys)
{
UniqueKey uk = entry.Value;
IList<Column> _columns = uk.Columns;
bool skip = false;
var tempUks = new Dictionary<string, UniqueKey>(finalUniqueKeys);
foreach (var tUk in tempUks)
{
UniqueKey currentUk = tUk.Value;
if (AreSameColumns(currentUk.Columns, _columns))
{
skip = true;
break;
}
}
if (!skip)
{
finalUniqueKeys[entry.Key] = uk;
}
}
return finalUniqueKeys;
}
else
{
return uniqueKeys;
}
}
}
public bool HasPrimaryKey
{
get { return PrimaryKey != null; }
}
public string Catalog
{
get { return catalog; }
set
{
if (value != null && value[0] == '`')
{
isCatalogQuoted = true;
catalog = value.Substring(1, value.Length - 2);
}
else
{
catalog = value;
}
}
}
public string Comment
{
get { return comment; }
set { comment = value; }
}
public string Subselect
{
get { return subselect; }
set { subselect = value; }
}
public IKeyValue IdentifierValue
{
get { return idValue; }
set { idValue = value; }
}
public bool IsSubselect
{
get { return !string.IsNullOrEmpty(subselect); }
}
public bool IsPhysicalTable
{
get { return !IsSubselect && !IsAbstractUnionTable; }
}
public SchemaAction SchemaActions
{
get { return schemaActions; }
set { schemaActions = value; }
}
public string RowId { get; set; }
public bool IsSchemaQuoted
{
get { return isSchemaQuoted; }
}
public bool IsCatalogQuoted
{
get { return isCatalogQuoted; }
}
#region IRelationalModel Members
/// <summary>
/// Generates the SQL string to create this Table in the database.
/// </summary>
/// <param name="dialect">The <see cref="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 Table, Primary Key Constraints
/// , and Unique Key Constraints.
/// </returns>
public string SqlCreateString(Dialect.Dialect dialect, IMapping p, string defaultCatalog, string defaultSchema)
{
StringBuilder buf =
new StringBuilder(HasPrimaryKey ? dialect.CreateTableString : dialect.CreateMultisetTableString).Append(' ').Append(
GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(" (");
bool identityColumn = idValue != null && idValue.IsIdentityColumn(dialect);
// try to find out the name of the pk to create it as identity if the
// identitygenerator is used
string pkname = null;
if (HasPrimaryKey && identityColumn)
{
foreach (Column col in PrimaryKey.ColumnIterator)
{
pkname = col.GetQuotedName(dialect); //should only go through this loop once
}
}
bool commaNeeded = false;
foreach (Column col in ColumnIterator)
{
if (commaNeeded)
{
buf.Append(StringHelper.CommaSpace);
}
commaNeeded = true;
buf.Append(col.GetQuotedName(dialect)).Append(' ');
if (identityColumn && col.GetQuotedName(dialect).Equals(pkname))
{
// to support dialects that have their own identity data type
if (dialect.HasDataTypeInIdentityColumn)
{
buf.Append(col.GetSqlType(dialect, p));
}
buf.Append(' ').Append(dialect.GetIdentityColumnString(col.GetSqlTypeCode(p).DbType));
}
else
{
buf.Append(col.GetSqlType(dialect, p));
if (!string.IsNullOrEmpty(col.DefaultValue))
{
buf.Append(" default ").Append(col.DefaultValue).Append(" ");
}
if (col.IsNullable)
{
buf.Append(dialect.NullColumnString);
}
else
{
buf.Append(" not null");
}
}
if (col.IsUnique)
{
if (dialect.SupportsUnique && (!col.IsNullable || dialect.SupportsNullInUnique))
{
buf.Append(" unique");
}
else
{
UniqueKey uk = GetOrCreateUniqueKey(col.GetQuotedName(dialect) + "_");
uk.AddColumn(col);
}
}
if (col.HasCheckConstraint && dialect.SupportsColumnCheck)
{
buf.Append(" check( ").Append(col.CheckConstraint).Append(") ");
}
if (string.IsNullOrEmpty(col.Comment) == false)
{
buf.Append(dialect.GetColumnComment(col.Comment));
}
}
if (HasPrimaryKey && (dialect.GenerateTablePrimaryKeyConstraintForIdentityColumn || !identityColumn))
{
buf.Append(StringHelper.CommaSpace).Append(PrimaryKey.SqlConstraintString(dialect, defaultSchema));
}
foreach (UniqueKey uk in UniqueKeyIterator)
{
string ukSql = uk.SqlConstraintString(dialect);
if (!string.IsNullOrEmpty(ukSql))
{
buf.Append(',').Append(ukSql);
}
}
if (dialect.SupportsTableCheck)
{
foreach (string checkConstraint in checkConstraints)
{
buf.Append(", check (").Append(checkConstraint).Append(") ");
}
}
if (!dialect.SupportsForeignKeyConstraintInAlterTable)
{
foreach (ForeignKey foreignKey in ForeignKeyIterator)
{
if (foreignKey.IsGenerated(dialect))
{
buf.Append(",").Append(foreignKey.SqlConstraintString(dialect, foreignKey.Name, defaultCatalog, defaultSchema));
}
}
}
buf.Append(StringHelper.ClosedParen);
if (string.IsNullOrEmpty(comment) == false)
{
buf.Append(dialect.GetTableComment(comment));
}
buf.Append(dialect.TableTypeString);
return buf.ToString();
}
/// <summary>
/// Generates the SQL string to drop this Table in the database.
/// </summary>
/// <param name="dialect">The <see cref="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 Table and to cascade the drop to
/// the constraints if the database supports it.
/// </returns>
public string SqlDropString(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
{
return dialect.GetDropTableString(GetQualifiedName(dialect, defaultCatalog, defaultSchema));
}
#endregion
/// <summary>
/// Gets the schema qualified name of the Table.
/// </summary>
/// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
/// <returns>The name of the table qualified with the schema if one is specified.</returns>
public string GetQualifiedName(Dialect.Dialect dialect)
{
return GetQualifiedName(dialect, null, null);
}
/// <summary>
/// Gets the schema qualified name of the Table using the specified qualifier
/// </summary>
/// <param name="dialect">The <see cref="Dialect"/> that knows how to Quote the Table name.</param>
/// <param name="defaultCatalog">The catalog name.</param>
/// <param name="defaultSchema">The schema name.</param>
/// <returns>A String representing the Qualified name.</returns>
/// <remarks>If this were used with MSSQL it would return a dbo.table_name.</remarks>
public virtual string GetQualifiedName(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
{
if (!string.IsNullOrEmpty(subselect))
{
return "( " + subselect + " )";
}
var quotedName = GetQuotedName(dialect);
var usedSchema = GetQuotedSchema(dialect, defaultSchema);
var usedCatalog = GetQuotedCatalog(dialect, defaultCatalog);
return dialect.Qualify(usedCatalog, usedSchema, quotedName);
}
/// <summary> returns quoted name as it would be in the mapping file.</summary>
public string GetQuotedName()
{
return quoted ? "`" + name + "`" : name;
}
/// <summary>
/// Gets the name of this Table in quoted form if it is necessary.
/// </summary>
/// <param name="dialect">
/// The <see cref="Dialect.Dialect"/> that knows how to quote the Table name.
/// </param>
/// <returns>
/// The Table 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 dialect)
{
return IsQuoted ? dialect.QuoteForTableName(name) : name;
}
/// <summary> returns quoted name as it is in the mapping file.</summary>
public string GetQuotedSchema()
{
return IsSchemaQuoted ? "`" + schema + "`" : schema;
}
public string GetQuotedSchema(Dialect.Dialect dialect)
{
return IsSchemaQuoted ? dialect.QuoteForSchemaName(schema) : schema;
}
public string GetQuotedSchema(Dialect.Dialect dialect, string defaultQuotedSchema)
{
return schema == null ? defaultQuotedSchema :
GetQuotedSchema(dialect);
}
/// <summary> returns quoted name as it is in the mapping file.</summary>
public string GetQuotedCatalog()
{
return IsCatalogQuoted ? "`" + catalog + "`" : catalog;
}
public string GetQuotedCatalog(Dialect.Dialect dialect)
{
return IsCatalogQuoted ? dialect.QuoteForCatalogName(catalog) : catalog;
}
public string GetQuotedCatalog(Dialect.Dialect dialect, string defaultQuotedCatalog)
{
return catalog == null ? defaultQuotedCatalog :
GetQuotedCatalog(dialect);
}
/// <summary>
/// Gets the schema for this table in quoted form if it is necessary.
/// </summary>
/// <param name="dialect">
/// The <see cref="Dialect.Dialect" /> that knows how to quote the schema name.
/// </param>
/// <returns>
/// The schema name for this table 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>
// Since v5.1; back-tilts are removed when storing schema: this thing is non-sens.
[Obsolete("This method is no-op and has no usages")]
public string GetQuotedSchemaName(Dialect.Dialect dialect)
{
if (schema == null)
{
return null;
}
if (schema.StartsWith('`'))
{
return dialect.QuoteForSchemaName(schema.Substring(1, schema.Length - 2));
}
return schema;
}
/// <summary>
/// Gets the <see cref="Column"/> at the specified index.
/// </summary>
/// <param name="n">The index of the Column to get.</param>
/// <returns>
/// The <see cref="Column"/> at the specified index.
/// </returns>
public Column GetColumn(int n)
{
return columns.Values.Skip(n).First();
}
/// <summary>
/// Adds the <see cref="Column"/> to the <see cref="ICollection"/> of
/// Columns that are part of the Table.
/// </summary>
/// <param name="column">The <see cref="Column"/> to include in the Table.</param>
public void AddColumn(Column column)
{
Column old = GetColumn(column);
if (old == null)
{
columns[column.CanonicalName] = column;
column.UniqueInteger = columns.Count;
}
else
{
column.UniqueInteger = old.UniqueInteger;
}
}
public string[] SqlAlterStrings(Dialect.Dialect dialect, IMapping p, ITableMetadata tableInfo, string defaultCatalog,
string defaultSchema)
{
StringBuilder root =
new StringBuilder("alter table ").Append(GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(' ').
Append(dialect.AddColumnString);
var results = new List<string>(ColumnSpan);
foreach (Column column in ColumnIterator)
{
IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name);
if (columnInfo != null)
{
continue;
}
// the column doesnt exist at all.
StringBuilder alter =
new StringBuilder(root.ToString()).Append(' ').Append(column.GetQuotedName(dialect)).Append(' ').Append(
column.GetSqlType(dialect, p));
string defaultValue = column.DefaultValue;
if (!string.IsNullOrEmpty(defaultValue))
{
alter.Append(" default ").Append(defaultValue);
if (column.IsNullable)
{
alter.Append(dialect.NullColumnString);
}
else
{
alter.Append(" not null");
}
}
bool useUniqueConstraint = column.Unique && dialect.SupportsUnique
&& (!column.IsNullable || dialect.SupportsNullInUnique);
if (useUniqueConstraint)
{
alter.Append(" unique");
}
if (column.HasCheckConstraint && dialect.SupportsColumnCheck)
{
alter.Append(" check(").Append(column.CheckConstraint).Append(") ");
}
string columnComment = column.Comment;
if (columnComment != null)
{
alter.Append(dialect.GetColumnComment(columnComment));
}
alter.Append(dialect.AddColumnSuffixString);
results.Add(alter.ToString());
}
return results.ToArray();
}
public Index GetIndex(string indexName)
{
Index result;
indexes.TryGetValue(indexName, out result);
return result;
}
public Index AddIndex(Index index)
{
Index current = GetIndex(index.Name);
if (current != null)
{
throw new MappingException("Index " + index.Name + " already exists!");
}
indexes[index.Name] = index;
return index;
}
/// <summary>
/// Gets the <see cref="Index"/> identified by the name.
/// </summary>
/// <param name="indexName">The name of the <see cref="Index"/> to get.</param>
/// <returns>
/// The <see cref="Index"/> identified by the name. If the <see cref="Index"/>
/// identified by the name does not exist then it is created.
/// </returns>
public Index GetOrCreateIndex(string indexName)
{
Index index = GetIndex(indexName);
if (index == null)
{
index = new Index();
index.Name = indexName;
index.Table = this;
indexes[indexName] = index;
}
return index;
}
public UniqueKey GetUniqueKey(string keyName)
{
UniqueKey result;
uniqueKeys.TryGetValue(keyName, out result);
return result;
}
public UniqueKey AddUniqueKey(UniqueKey uniqueKey)
{
UniqueKey current = GetUniqueKey(uniqueKey.Name);
if (current != null)
{
throw new MappingException("UniqueKey " + uniqueKey.Name + " already exists!");
}
uniqueKeys[uniqueKey.Name] = uniqueKey;
return uniqueKey;
}
/// <summary>
/// Gets the <see cref="UniqueKey"/> identified by the name.
/// </summary>
/// <param name="keyName">The name of the <see cref="UniqueKey"/> to get.</param>
/// <returns>
/// The <see cref="UniqueKey"/> identified by the name. If the <see cref="UniqueKey"/>
/// identified by the name does not exist then it is created.
/// </returns>
public UniqueKey GetOrCreateUniqueKey(string keyName)
{
UniqueKey uk = GetUniqueKey(keyName);
if (uk == null)
{
uk = new UniqueKey();
uk.Name = keyName;
uk.Table = this;
uniqueKeys[keyName] = uk;
}
return uk;
}
public virtual void CreateForeignKeys() { }
public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable<Column> keyColumns, string referencedEntityName)
{
return CreateForeignKey(keyName, keyColumns, referencedEntityName, null);
}
/// <summary>
/// Create a <see cref="ForeignKey"/> for the columns in the Table.
/// </summary>
/// <param name="keyName"></param>
/// <param name="keyColumns">An <see cref="IList"/> of <see cref="Column"/> objects.</param>
/// <param name="referencedEntityName"></param>
/// <param name="referencedColumns"></param>
/// <returns>
/// A <see cref="ForeignKey"/> for the columns in the Table.
/// </returns>
/// <remarks>
/// This does not necessarily create a <see cref="ForeignKey"/>, if
/// one already exists for the columns then it will return an
/// existing <see cref="ForeignKey"/>.
/// </remarks>
public virtual ForeignKey CreateForeignKey(string keyName, IEnumerable<Column> keyColumns, string referencedEntityName,
IEnumerable<Column> referencedColumns)
{
IEnumerable<Column> kCols = keyColumns;
IEnumerable<Column> refCols = referencedColumns;
var key = new ForeignKeyKey(kCols, referencedEntityName, refCols);
ForeignKey fk;
foreignKeys.TryGetValue(key, out fk);
if (fk == null)
{
fk = new ForeignKey();
// NOTE : if the name is null, we will generate an implicit name during second pass processing
// after we know the referenced table name (which might not be resolved yet).
fk.Name = keyName;
fk.Table = this;
foreignKeys.Add(key, fk);
fk.ReferencedEntityName = referencedEntityName;
fk.AddColumns(kCols);
if (referencedColumns != null)
{
fk.AddReferencedColumns(refCols);
}
}
if (!string.IsNullOrEmpty(keyName))
{
fk.Name = keyName;
}
return fk;
}
public virtual UniqueKey CreateUniqueKey(IList<Column> keyColumns)
{
var keyName = Constraint.GenerateName( "UK_", this, null, keyColumns);
var uk = GetOrCreateUniqueKey(keyName);
uk.AddColumns(keyColumns);
return uk;
}
/// <summary>
/// Generates a unique string for an <see cref="ICollection"/> of
/// <see cref="Column"/> objects.
/// </summary>
/// <param name="uniqueColumns">An <see cref="ICollection"/> of <see cref="Column"/> objects.</param>
/// <returns>
/// An unique string for the <see cref="Column"/> objects.
/// </returns>
// Since v5.2
[Obsolete("Use Constraint.GenerateName instead.")]
public string UniqueColumnString(IEnumerable uniqueColumns)
{
return UniqueColumnString(uniqueColumns, null);
}
// Since v5.2
[Obsolete("Use Constraint.GenerateName instead.")]
public string UniqueColumnString(IEnumerable iterator, string referencedEntityName)
{
// NH Different implementation (NH-1399)
int result = 37;
if (referencedEntityName != null)
{
result ^= referencedEntityName.GetHashCode();
}
foreach (object o in iterator)
{
result ^= o.GetHashCode();
}
return (name.GetHashCode().ToString("X") + result.GetHashCode().ToString("X"));
}
/// <summary>
/// Sets the Identifier of the Table.
/// </summary>
/// <param name="identifierValue">The <see cref="SimpleValue"/> that represents the Identifier.</param>
public void SetIdentifierValue(SimpleValue identifierValue)
{
idValue = identifierValue;
}
/// <summary>
///
/// </summary>
/// <param name="constraint"></param>
public void AddCheckConstraint(string constraint)
{
if (!string.IsNullOrEmpty(constraint))
{
checkConstraints.Add(constraint);
}
}
internal void SetHasDenormalizedTables()
{
hasDenormalizedTables = true;
}
public virtual bool ContainsColumn(Column column)
{
return columns.ContainsValue(column);
}
/// <summary> Return the column which is identified by column provided as argument. </summary>
/// <param name="column">column with at least a name. </param>
/// <returns>
/// The underlying column or null if not inside this table.
/// Note: the instance *can* be different than the input parameter, but the name will be the same.
/// </returns>
public virtual Column GetColumn(Column column)
{
if (column == null)
{
return null;
}
Column result;
columns.TryGetValue(column.CanonicalName, out result);
return column.Equals(result) ? result : null;
}
private static bool AreSameColumns(ICollection<Column> col1, ICollection<Column> col2)
{
if (col1.Count != col2.Count)
{
return false;
}
foreach (Column column in col1)
{
if (!col2.Contains(column))
{
return false;
}
}
foreach (Column column in col2)
{
if (!col1.Contains(column))
{
return false;
}
}
return true;
}
public virtual string[] SqlCommentStrings(Dialect.Dialect dialect, string defaultCatalog, string defaultSchema)
{
var comments = new List<string>();
if (dialect.SupportsCommentOn)
{
string tableName = GetQualifiedName(dialect, defaultCatalog, defaultSchema);
if (!string.IsNullOrEmpty(comment))
{
StringBuilder buf =
new StringBuilder().Append("comment on table ").Append(tableName).Append(" is '").Append(comment).Append("'");
comments.Add(buf.ToString());
}
foreach (Column column in ColumnIterator)
{
string columnComment = column.Comment;
if (columnComment != null)
{
StringBuilder buf =
new StringBuilder().Append("comment on column ").Append(tableName).Append('.').Append(
column.GetQuotedName(dialect)).Append(" is '").Append(columnComment).Append("'");
comments.Add(buf.ToString());
}
}
}
return comments.ToArray();
}
public virtual string SqlTemporaryTableCreateString(Dialect.Dialect dialect, IMapping mapping)
{
StringBuilder buffer = new StringBuilder(dialect.CreateTemporaryTableString).Append(' ').Append(name).Append(" (");
bool commaNeeded = false;
foreach (Column column in ColumnIterator)
{
buffer.Append(column.GetQuotedName(dialect)).Append(' ');
buffer.Append(column.GetSqlType(dialect, mapping));
if (commaNeeded)
{
buffer.Append(StringHelper.CommaSpace);
}
commaNeeded = true;
if (column.IsNullable)
{
buffer.Append(dialect.NullColumnString);
}
else
{
buffer.Append(" not null");
}
}
buffer.Append(") ");
buffer.Append(dialect.CreateTemporaryTablePostfix);
return buffer.ToString();