2
2
using System . Text ;
3
3
using NHibernate . Util ;
4
4
using System ;
5
+ using System . Linq ;
5
6
6
7
namespace NHibernate . Mapping
7
8
{
@@ -14,7 +15,7 @@ public class ForeignKey : Constraint
14
15
private Table referencedTable ;
15
16
private string referencedEntityName ;
16
17
private bool cascadeDeleteEnabled ;
17
- private readonly List < Column > referencedColumns = new List < Column > ( ) ;
18
+ private List < Column > referencedColumns ;
18
19
19
20
/// <summary>
20
21
/// Generates the SQL string to create the named Foreign Key Constraint in the database.
@@ -26,29 +27,26 @@ public class ForeignKey : Constraint
26
27
/// <returns>
27
28
/// A string that contains the SQL to create the named Foreign Key Constraint.
28
29
/// </returns>
29
- public override string SqlConstraintString ( Dialect . Dialect d , string constraintName , string defaultCatalog , string defaultSchema )
30
- {
31
- string [ ] cols = new string [ ColumnSpan ] ;
32
- string [ ] refcols = new string [ ColumnSpan ] ;
33
- int i = 0 ;
34
- IEnumerable < Column > refiter ;
35
- if ( IsReferenceToPrimaryKey )
36
- refiter = referencedTable . PrimaryKey . ColumnIterator ;
37
- else
38
- refiter = referencedColumns ;
39
- foreach ( Column column in ColumnIterator )
40
- {
41
- cols [ i ] = column . GetQuotedName ( d ) ;
42
- i ++ ;
43
- }
30
+ public override string SqlConstraintString (
31
+ Dialect . Dialect d ,
32
+ string constraintName ,
33
+ string defaultCatalog ,
34
+ string defaultSchema )
35
+ {
36
+ var refiter = IsReferenceToPrimaryKey
37
+ ? referencedTable . PrimaryKey . Columns
38
+ : referencedColumns ;
39
+
40
+ var cols = Columns . ToArray ( column => column . GetQuotedName ( d ) ) ;
41
+ var refcols = refiter . ToArray ( column => column . GetQuotedName ( d ) ) ;
42
+
43
+ string result = d . GetAddForeignKeyConstraintString (
44
+ constraintName ,
45
+ cols ,
46
+ referencedTable . GetQualifiedName ( d , defaultCatalog , defaultSchema ) ,
47
+ refcols ,
48
+ IsReferenceToPrimaryKey ) ;
44
49
45
- i = 0 ;
46
- foreach ( Column column in refiter )
47
- {
48
- refcols [ i ] = column . GetQuotedName ( d ) ;
49
- i ++ ;
50
- }
51
- string result = d . GetAddForeignKeyConstraintString ( constraintName , cols , referencedTable . GetQualifiedName ( d , defaultCatalog , defaultSchema ) , refcols , IsReferenceToPrimaryKey ) ;
52
50
return cascadeDeleteEnabled && d . SupportsCascadeDelete ? result + " on delete cascade" : result ;
53
51
}
54
52
@@ -172,40 +170,24 @@ public virtual void AddReferencedColumns(IEnumerable<Column> referencedColumnsIt
172
170
173
171
private void AddReferencedColumn ( Column column )
174
172
{
173
+ referencedColumns ??= new List < Column > ( 1 ) ;
175
174
if ( ! referencedColumns . Contains ( column ) )
176
175
referencedColumns . Add ( column ) ;
177
176
}
178
177
179
178
internal void AddReferencedTable ( PersistentClass referencedClass )
180
179
{
181
- if ( referencedColumns . Count > 0 )
182
- {
183
- referencedTable = referencedColumns [ 0 ] . Value . Table ;
184
- }
185
- else
186
- {
187
- referencedTable = referencedClass . Table ;
188
- }
180
+ referencedTable = IsReferenceToPrimaryKey ? referencedClass . Table : referencedColumns [ 0 ] . Value . Table ;
189
181
}
190
182
191
183
public override string ToString ( )
192
184
{
193
- if ( ! IsReferenceToPrimaryKey )
194
- {
195
- var result = new StringBuilder ( ) ;
196
- result . Append ( GetType ( ) . FullName )
197
- . Append ( '(' )
198
- . Append ( Table . Name )
199
- . Append ( string . Join ( ", " , Columns ) )
200
- . Append ( " ref-columns:" )
201
- . Append ( '(' )
202
- . Append ( string . Join ( ", " , ReferencedColumns ) )
203
- . Append ( ") as " )
204
- . Append ( Name ) ;
205
- return result . ToString ( ) ;
206
- }
185
+ if ( IsReferenceToPrimaryKey )
186
+ return base . ToString ( ) ;
207
187
208
- return base . ToString ( ) ;
188
+ var columns = string . Join ( ", " , Columns ) ;
189
+ var refColumns = string . Join ( ", " , referencedColumns ) ;
190
+ return $ "{ GetType ( ) . FullName } ({ Table . Name } { columns } ref-columns:({ refColumns } ) as { Name } ";
209
191
}
210
192
211
193
public bool HasPhysicalConstraint
@@ -218,7 +200,11 @@ public bool HasPhysicalConstraint
218
200
219
201
public IList < Column > ReferencedColumns
220
202
{
221
- get { return referencedColumns ; }
203
+ get
204
+ {
205
+ referencedColumns ??= new List < Column > ( 1 ) ;
206
+ return referencedColumns ;
207
+ }
222
208
}
223
209
224
210
public string ReferencedEntityName
@@ -228,10 +214,7 @@ public string ReferencedEntityName
228
214
}
229
215
230
216
/// <summary>Does this foreignkey reference the primary key of the reference table </summary>
231
- public bool IsReferenceToPrimaryKey
232
- {
233
- get { return referencedColumns . Count == 0 ; }
234
- }
217
+ public bool IsReferenceToPrimaryKey => referencedColumns == null || referencedColumns . Count == 0 ;
235
218
236
219
public string GeneratedConstraintNamePrefix => "FK_" ;
237
220
@@ -242,12 +225,7 @@ public override bool IsGenerated(Dialect.Dialect dialect)
242
225
if ( dialect . SupportsNullInUnique || IsReferenceToPrimaryKey )
243
226
return true ;
244
227
245
- foreach ( var column in ReferencedColumns )
246
- {
247
- if ( column . IsNullable )
248
- return false ;
249
- }
250
- return true ;
228
+ return referencedColumns . All ( column => ! column . IsNullable ) ;
251
229
}
252
230
}
253
231
}
0 commit comments