Datei: NDOEnhancer/NDOEnhancer/Enhancer/Generator/IndexDefinition.cs

Last Commit (ab290a4)
1 -- File didn't exist --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
New Commit (f5f4fd8)
1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Linq;
5 using NDO.Mapping;
6
7 namespace NDOEnhancer
8 {
9 ····/// <summary>
10 ····/// Describes an index, which NDO creates for a table.
11 ····/// </summary>
12 ····/// <remarks>
13 ····/// Without these indexes the database has to scan the whole table for each access via a foreign key.
14 ····/// Databases which use locks for ReadCommitted (e.g. SqlServer) lock all rows they read during the scan.
15 ····/// So concurrent transactions block each other and cause deadlocks, even if they don't access the same rows.
16 ····/// The index on both sides of a mapping table is unique, because NDO doesn't allow to add the same object twice to a relation.
17 ····/// </remarks>
18 ····internal class IndexDefinition
19 ····{
20 ········public IndexDefinition( string name, DataColumn[] columns, bool isUnique )
21 ········{
22 ············Name = name;
23 ············Columns = columns;
24 ············IsUnique = isUnique;
25 ········}
26
27 ········/// <summary>
28 ········/// The unquoted name of the index.
29 ········/// </summary>
30 ········public string Name { get; }
31
32 ········public DataColumn[] Columns { get; }
33
34 ········/// <summary>
35 ········/// True, if the database should reject two rows with the same values in the columns of the index.
36 ········/// </summary>
37 ········public bool IsUnique { get; }
38
39 ········/// <summary>
40 ········/// Determines the indexes of a table.
41 ········/// </summary>
42 ········/// <remarks>
43 ········/// Tables of classes get an index for the foreign key columns of each relation.
44 ········/// Mapping tables get a unique index for the columns of both sides (used for the access from the own side and the deletion
45 ········/// of single entries) and an index for the columns of the other side.
46 ········/// </remarks>
47 ········/// <param name="dt">The table</param>
48 ········/// <param name="mappings">The mapping information</param>
49 ········/// <returns>The indexes in a stable order, so that the names don't change between two runs of the enhancer.</returns>
50 ········public static IEnumerable<IndexDefinition> ForTable( DataTable dt, NDOMapping mappings )
51 ········{
52 ············var columnLists = new List<(DataColumn[] Columns, bool IsUnique)>();
53
54 ············Relation r = mappings.Classes
55 ················.SelectMany( c => c.Relations )
56 ················.FirstOrDefault( rel => rel.MappingTable != null && rel.MappingTable.TableName == dt.TableName );
57
58 ············if (r != null)
59 ············{
60 ················DataColumn[] ownColumns = GetColumns( dt, r.ForeignKeyColumns.Select( c => c.Name ), r.ForeignKeyTypeColumnName );
61 ················DataColumn[] otherColumns = GetColumns( dt, r.MappingTable.ChildForeignKeyColumns.Select( c => c.Name ), r.MappingTable.ChildForeignKeyTypeColumnName );
62 ················if (ownColumns != null && otherColumns != null)
63 ················{
64 ····················columnLists.Add( (ownColumns.Concat( otherColumns ).ToArray(), true) );
65 ····················columnLists.Add( (otherColumns, false) );
66 ················}
67 ············}
68 ············else
69 ············{
70 ················// The SchemaGenerator creates a DataRelation for each foreign key in a table of a class.
71 ················foreach (DataRelation dr in dt.ParentRelations)
72 ····················columnLists.Add( (dr.ChildColumns, false) );
73 ············}
74
75 ············// Short names, because some databases limit the length of identifiers
76 ············string tableName = dt.TableName.Substring( dt.TableName.LastIndexOf( '.' ) + 1 );
77 ············var created = new HashSet<string>();
78 ············int n = 0;
79 ············foreach (var (columns, isUnique) in columnLists)
80 ············{
81 ················// Two DataRelations might use the same columns
82 ················if (!created.Add( String.Join( ",", columns.Select( c => c.ColumnName ) ) ))
83 ····················continue;
84 ················yield return new IndexDefinition( $"IX_{tableName}_{++n}", columns, isUnique );
85 ············}
86 ········}
87
88 ········static DataColumn[] GetColumns( DataTable dt, IEnumerable<string> columnNames, string typeColumnName )
89 ········{
90 ············var names = columnNames.ToList();
91 ············if (typeColumnName != null)
92 ················names.Add( typeColumnName );
93 ············if (names.Count == 0 || names.Any( name => !dt.Columns.Contains( name ) ))
94 ················return null;
95 ············return names.Select( name => dt.Columns[name] ).ToArray();
96 ········}
97 ····}
98 }
99