-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqlTableHelper.cs
More file actions
178 lines (152 loc) · 5.87 KB
/
SqlTableHelper.cs
File metadata and controls
178 lines (152 loc) · 5.87 KB
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
using System.Linq;
using System.Reflection;
using FizzCode.DbTools.Common;
using FizzCode.DbTools.DataDefinition;
using FizzCode.DbTools.DataDefinition.Base;
namespace FizzCode.DbTools.QueryBuilder;
public static class SqlTableHelper
{
internal static void SetAliasProperty(this SqlTable sqlTable, string alias)
{
var aliasProperty = sqlTable.Properties.OfType<AliasTableProperty>().FirstOrDefault();
if (aliasProperty is null)
{
aliasProperty = new AliasTableProperty(alias);
sqlTable.Properties.Add(aliasProperty);
}
else
{
aliasProperty.Alias = alias;
}
}
internal static void SetAliasProperty(this SqlView sqlView, string alias)
{
var aliasProperty = sqlView.Properties.OfType<AliasViewProperty>().FirstOrDefault();
if (aliasProperty is null)
{
aliasProperty = new AliasViewProperty(alias);
sqlView.Properties.Add(aliasProperty);
}
else
{
aliasProperty.Alias = alias;
}
}
public static string? GetAlias(this SqlTableOrView sqlTableOrView)
{
return sqlTableOrView switch
{
SqlTable sqlTable => GetAlias(sqlTable),
SqlView sqlView => GetAlias(sqlView),
_ => throw new System.ArgumentException("Unknown SqlTableOrView Type.")
};
}
public static string? GetAlias(this SqlTable sqlTable)
{
var aliasTableProperty = sqlTable.Properties.OfType<AliasTableProperty>().FirstOrDefault();
return aliasTableProperty?.Alias;
}
public static string? GetAlias(this SqlView sqlTable)
{
var aliasProperty = sqlTable.Properties.OfType<AliasViewProperty>().FirstOrDefault();
return aliasProperty?.Alias;
}
internal static void SetAlias(SqlTable table, string? alias)
{
if (!string.IsNullOrEmpty(alias))
{
table.SetAliasProperty(alias);
return;
}
var tableName = table.SchemaAndTableNameSafe.TableName;
var capitals = new string(tableName.Where(c => char.IsUpper(c)).ToArray());
#pragma warning disable CA1308 // Normalize strings to uppercase
table.SetAliasProperty(capitals.Length > 0 ? capitals.ToLowerInvariant()
: alias ?? tableName.Substring(0, 1).ToLowerInvariant());
#pragma warning restore CA1308 // Normalize strings to uppercase
}
internal static void SetAlias(SqlView table, string? alias)
{
if (!string.IsNullOrEmpty(alias))
{
table.SetAliasProperty(alias);
return;
}
var tableName = table.SchemaAndTableNameSafe.TableName;
var capitals = new string(tableName.Where(c => char.IsUpper(c)).ToArray());
#pragma warning disable CA1308 // Normalize strings to uppercase
table.SetAliasProperty(capitals.Length > 0 ? capitals.ToLowerInvariant()
: alias ?? tableName.Substring(0, 1).ToLowerInvariant());
#pragma warning restore CA1308 // Normalize strings to uppercase
}
internal static void SetupDeclaredTable(SqlTable table)
{
AddDeclaredColumns(table);
AddDeclaredForeignKeys(table);
UpdateDeclaredIndexes(table);
UpdateDeclaredCustomProperties(table);
}
private static void AddDeclaredColumns(SqlTable table)
{
var properties = table.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(pi => typeof(SqlColumn).IsAssignableFrom(pi.PropertyType) && pi.GetIndexParameters().Length == 0);
foreach (var property in properties)
{
var column = property.GetValueSafe<SqlColumn>(table);
column.Name = property.Name;
column.Table = table;
}
}
private static void AddDeclaredForeignKeys(SqlTable table)
{
var properties = table.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(pi =>
typeof(ForeignKey).IsAssignableFrom(pi.PropertyType)
&& pi.GetIndexParameters().Length == 0);
foreach (var property in properties)
{
var fk = property.GetValueSafe<ForeignKey>(table);
if (!property.Name.StartsWith('_'))
fk.Name = property.Name;
fk.SqlTableOrView = table;
}
}
private static void UpdateDeclaredIndexes(SqlTable table)
{
var properties = table.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(pi =>
(typeof(Index).IsAssignableFrom(pi.PropertyType)
|| typeof(UniqueConstraint).IsAssignableFrom(pi.PropertyType))
&& pi.GetIndexParameters().Length == 0);
foreach (var property in properties)
{
var index = property.GetValueSafe<Index>(table);
if (!property.Name.StartsWith('_'))
index.Name = property.Name;
index.SqlTableOrView = table;
var registeredIdexes = index.SqlColumnRegistrations.ToList();
foreach (var cr in registeredIdexes)
{
index.SqlColumnRegistrations.Remove(cr);
index.SqlColumns.Add(new ColumnAndOrder(table.Columns[cr.ColumnName], cr.Order));
}
}
}
private static void UpdateDeclaredCustomProperties(SqlTable table)
{
var properties = table.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(pi =>
typeof(SqlTableCustomProperty).IsAssignableFrom(pi.PropertyType)
&& pi.GetIndexParameters().Length == 0);
// TODO ?
foreach (var property in properties)
{
var customProperty = property.GetValueSafe<SqlTableCustomProperty>(table);
customProperty.SqlTableOrView = table;
}
}
}