forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cs
More file actions
234 lines (200 loc) · 8.3 KB
/
Table.cs
File metadata and controls
234 lines (200 loc) · 8.3 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
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Simple.Data.Extensions;
namespace Simple.Data.Ado.Schema
{
public class Table : IEquatable<Table>
{
private readonly string _actualName;
private readonly string _schema;
private readonly TableType _type;
private readonly DatabaseSchema _databaseSchema;
private readonly Lazy<ColumnCollection> _lazyColumns;
private readonly Lazy<Key> _lazyPrimaryKey;
private readonly Lazy<ForeignKeyCollection> _lazyForeignKeys;
public Table(string name, string schema, TableType type)
{
_actualName = name;
_schema = string.IsNullOrWhiteSpace(schema) ? null : schema;
_type = type;
}
internal Table(string name, string schema, TableType type, DatabaseSchema databaseSchema)
{
_actualName = name;
_databaseSchema = databaseSchema;
_schema = schema;
_type = type;
_lazyColumns = new Lazy<ColumnCollection>(GetColumns);
_lazyPrimaryKey = new Lazy<Key>(GetPrimaryKey);
_lazyForeignKeys = new Lazy<ForeignKeyCollection>(GetForeignKeys);
}
public TableType Type
{
get { return _type; }
}
internal string HomogenizedName
{
get { return ActualName.Homogenize(); }
}
internal DatabaseSchema DatabaseSchema
{
get { return _databaseSchema; }
}
public string Schema
{
get { return _schema; }
}
public string ActualName
{
get { return _actualName; }
}
internal string QuotedName
{
get { return _databaseSchema.QuoteObjectName(_actualName); }
}
public string QualifiedName
{
get
{
return _schema == null
? _databaseSchema.QuoteObjectName(_actualName)
: _databaseSchema.QuoteObjectName(_schema) + '.' + _databaseSchema.QuoteObjectName(_actualName);
}
}
public IEnumerable<Column> Columns
{
get { return _lazyColumns.Value.AsEnumerable(); }
}
public Column FindColumn(string columnName)
{
var columns = _lazyColumns.Value;
try
{
return columns.Find(columnName);
}
catch (UnresolvableObjectException ex)
{
string fullColumnName = _actualName + "." + ex.ObjectName;
throw new UnresolvableObjectException(fullColumnName, string.Format("Column '{0}' not found.", fullColumnName), ex);
}
}
public bool TryFindColumn(string columnName, out Column column)
{
var columns = _lazyColumns.Value;
if (columns.Contains(columnName))
{
column = columns.Find(columnName);
return true;
}
column = null;
return false;
}
public bool HasColumn(string columnName)
{
return _lazyColumns.Value.Contains(columnName);
}
public Key PrimaryKey
{
get { return _lazyPrimaryKey.Value; }
}
public ForeignKeyCollection ForeignKeys
{
get { return _lazyForeignKeys.Value; }
}
private ColumnCollection GetColumns()
{
return new ColumnCollection(_databaseSchema.SchemaProvider.GetColumns(this));
}
private Key GetPrimaryKey()
{
return _databaseSchema.SchemaProvider.GetPrimaryKey(this);
//var columns = _databaseSchema.SchemaProvider.GetSchema("PRIMARY_KEYS", ActualName).AsEnumerable()
// .OrderBy(row => (int) row["ORDINAL_POSITION"])
// .Select(row => row["COLUMN_NAME"].ToString())
// .ToArray();
//return new Key(columns);
}
private ForeignKeyCollection GetForeignKeys()
{
var collection = new ForeignKeyCollection();
//var keys = _databaseSchema.SchemaProvider.GetSchema("FOREIGN_KEYS", ActualName).AsEnumerable()
// .GroupBy(row => row["UNIQUE_TABLE_NAME"].ToString());
foreach (var key in _databaseSchema.SchemaProvider.GetForeignKeys(this))
{
//var columns = key.OrderBy(row => (int)row["ORDINAL_POSITION"]).Select(row => row["COLUMN_NAME"].ToString()).ToArray();
//var uniqueColumns = key.OrderBy(row => (int)row["ORDINAL_POSITION"]).Select(row => row["UNIQUE_COLUMN_NAME"].ToString()).ToArray();
collection.Add(key);
}
return collection;
}
internal TableJoin GetMaster(string name)
{
var table = _databaseSchema.FindTable(name);
var foreignKey =
this.ForeignKeys.FirstOrDefault(fk => fk.MasterTable.Schema == table.Schema && fk.MasterTable.Name == table.ActualName);
if (foreignKey == null) return null;
return new TableJoin(table, table.FindColumn(foreignKey.UniqueColumns[0]), this, this.FindColumn(foreignKey.Columns[0]));
}
private string GetCommonColumnName(Table other)
{
return other.Columns
.Select(c => c.HomogenizedName)
.Intersect(Columns.Select(c => c.HomogenizedName))
.SingleOrDefault();
}
internal TableJoin GetDetail(string name)
{
var table = DatabaseSchema.FindTable(name);
var foreignKey =
table.ForeignKeys.FirstOrDefault(fk => fk.MasterTable.Schema == this.Schema && fk.MasterTable.Name == this.ActualName);
if (foreignKey == null) return null;
return new TableJoin(this, this.FindColumn(foreignKey.UniqueColumns[0]), table, table.FindColumn(foreignKey.Columns[0]));
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(Table other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._actualName, _actualName) && Equals(other._schema, _schema) && Equals(other._type, _type);
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Table)) return false;
return Equals((Table) obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
unchecked
{
int result = (_actualName != null ? _actualName.GetHashCode() : 0);
result = (result*397) ^ (_schema != null ? _schema.GetHashCode() : 0);
result = (result*397) ^ _type.GetHashCode();
return result;
}
}
}
}