-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cs
455 lines (433 loc) · 13.6 KB
/
Database.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
namespace D00B
{
public class DBTableKey : IComparable<DBTableKey>, IEquatable<DBTableKey>, IComparable
{
private string m_strSchema = string.Empty;
private string m_strTable = string.Empty;
private string m_strColumn = string.Empty;
private string m_strJoinTag = string.Empty;
public DBTableKey()
{
}
public DBTableKey(string strSchema, string strTable, string strColumn) : this()
{
Schema = strSchema;
Table = strTable;
Column = strColumn;
}
public override string ToString()
{
return Schema + "." + Table + "." + Column;
}
public string Schema
{
get { return m_strSchema; }
set { if (!string.IsNullOrEmpty(value)) m_strSchema = value; }
}
public string Table
{
get { return m_strTable; }
set { if (!string.IsNullOrEmpty(value)) m_strTable = value; }
}
public string Column
{
get { return m_strColumn; }
set { if (!string.IsNullOrEmpty(value)) m_strColumn = value; }
}
public string JoinTag
{
get { return m_strJoinTag; }
set { if (!string.IsNullOrEmpty(value)) m_strJoinTag = value; }
}
public override int GetHashCode()
{
return Utility.GetHashCode(ToString());
}
public int CompareTo(DBTableKey rhs)
{
if (Equals(rhs))
return 0;
return ToString().CompareTo(rhs.ToString());
}
int IComparable.CompareTo(object rhs)
{
if (!(rhs is DBTableKey))
throw new InvalidOperationException("CompareTo: Not a DB Table Key");
return CompareTo((DBTableKey)rhs);
}
public static bool operator <(DBTableKey lhs, DBTableKey rhs) => lhs.CompareTo(rhs) < 0;
public static bool operator >(DBTableKey lhs, DBTableKey rhs) => lhs.CompareTo(rhs) > 0;
public bool Equals(DBTableKey rhs) => ToString() == rhs.ToString();
public override bool Equals(object rhs)
{
if (!(rhs is DBTableKey))
return false;
return Equals((DBTableKey)rhs);
}
public static bool operator ==(DBTableKey lhs, DBTableKey rhs) => lhs.Equals(rhs);
public static bool operator !=(DBTableKey lhs, DBTableKey rhs) => !(lhs == rhs);
}
public class DBJoinKey : DBTableKey
{
private readonly Utility.Join m_Join;
public DBJoinKey()
{
m_Join = Utility.Join.Inner;
}
public override string ToString()
{
string strJoinType;
switch (m_Join)
{
case Utility.Join.Inner:
strJoinType = "inner";
break;
case Utility.Join.Left:
strJoinType = "left";
break;
case Utility.Join.Right:
strJoinType = "right";
break;
case Utility.Join.Full:
strJoinType = "full outer";
break;
default:
strJoinType = "self";
break;
}
return strJoinType;
}
public DBJoinKey(string strK1, string strK2, string strK3, Utility.Join Join) : this()
{
Schema = strK1;
Table = strK2;
Column = strK3;
m_Join = Join;
}
public Utility.Join JoinType
{
get { return m_Join; }
}
}
public class DBColumn : IComparable<DBColumn>, IEquatable<DBColumn>, IComparable
{
private string m_strName;
private bool m_bInclude;
private string m_strFormatString;
private int m_iAlignment;
string m_strCultureName;
IFormatProvider m_FormatProvider;
private TypeCode m_TypeCode;
private bool m_bIsKey;
private SortOrder m_SortOrder;
private List<DBTableKey> m_Tables = new List<DBTableKey>();
public DBColumn()
{
m_strFormatString = "G";
m_iAlignment = 0;
m_strCultureName = CultureInfo.CurrentCulture.Name;
m_FormatProvider = new CultureInfo(m_strCultureName);
m_bInclude = true;
m_bIsKey = false;
m_SortOrder = SortOrder.Ascending;
}
public DBColumn(string strName) : this()
{
if (!string.IsNullOrEmpty(strName))
m_strName = strName;
}
public DBColumn(string strName, bool bIsKey) : this(strName)
{
m_bIsKey = bIsKey;
}
public override string ToString()
{
return m_strName;
}
public override int GetHashCode()
{
return Utility.GetHashCode(ToString());
}
public int CompareTo(DBColumn rhs)
{
if (Equals(rhs))
return 0;
return ToString().CompareTo(rhs.ToString());
}
int IComparable.CompareTo(object rhs)
{
if (!(rhs is DBColumn))
throw new InvalidOperationException("CompareTo: Not a DBColumn");
return CompareTo((DBColumn)rhs);
}
public static bool operator <(DBColumn lhs, DBColumn rhs) => lhs.CompareTo(rhs) < 0;
public static bool operator >(DBColumn lhs, DBColumn rhs) => lhs.CompareTo(rhs) > 0;
public bool Equals(DBColumn rhs) => ToString() == rhs.ToString();
public override bool Equals(object rhs)
{
if (!(rhs is DBColumn))
return false;
return Equals((DBColumn)rhs);
}
public static bool operator ==(DBColumn lhs, DBColumn rhs) => lhs.Equals(rhs);
public static bool operator !=(DBColumn lhs, DBColumn rhs) => !(lhs == rhs);
public bool IsPrimaryKey
{
get { return m_bIsKey; }
set { m_bIsKey = value; }
}
public List<DBTableKey> Tables
{
get { return m_Tables; }
set { m_Tables = value; }
}
public string Name
{
get { return m_strName; }
set { m_strName = string.IsNullOrEmpty(value) ? value : string.Empty; }
}
public bool Include
{
get
{
return m_bInclude;
}
set
{
m_bInclude = value;
}
}
public string FormatString
{
get
{
return m_strFormatString;
}
set
{
m_strFormatString = value;
}
}
public IFormatProvider FormatProvider
{
get
{
return m_FormatProvider;
}
set
{
}
}
public int Alignment
{
get
{
return m_iAlignment;
}
set
{
m_iAlignment = value;
}
}
public string CultureName
{
get
{
return m_strCultureName;
}
set
{
m_strCultureName = value;
m_FormatProvider = new CultureInfo(m_strCultureName);
}
}
public TypeCode TypeCode
{
get
{
return m_TypeCode;
}
set
{
m_TypeCode = value;
}
}
public SortOrder SortOrder
{
get
{
return m_SortOrder;
}
set
{
m_SortOrder = value;
}
}
}
public class DBTable : IComparable<DBTable>, IEquatable<DBTable>, IComparable
{
DBTableKey m_TableKey = new DBTableKey();
private int m_nRows = 0;
private int m_iSelectedIndex = 0;
private bool m_bVisited = false;
readonly List<DBTableKey> m_PKeys;
List<DBColumn> m_Cols;
readonly Dictionary<DBTableKey, List<DBTableKey>> m_MapPKtoFK;
readonly Dictionary<DBTableKey, List<DBTableKey>> m_MapFKtoPK;
bool m_bView = false;
public DBTable()
{
m_Cols = new List<DBColumn>();
m_PKeys = new List<DBTableKey>();
m_MapPKtoFK = new Dictionary<DBTableKey, List<DBTableKey>>();
m_MapFKtoPK = new Dictionary<DBTableKey, List<DBTableKey>>();
}
public DBTable(DBTableKey TableKey) : this()
{
m_TableKey = TableKey;
}
public void AddPK(DBTableKey PK)
{
m_PKeys.Add(PK);
}
public bool ContainsPK(string strOwner, string strTable, string strColumn)
{
return m_MapPKtoFK.ContainsKey(new DBTableKey(strOwner, strTable, strColumn));
}
public bool ContainsPK(DBTableKey PK)
{
return m_MapPKtoFK.ContainsKey(PK);
}
public bool ContainsFK(string strOwner, string strTable, string strColumn)
{
return m_MapFKtoPK.ContainsKey(new DBTableKey(strOwner, strTable, strColumn));
}
public bool ContainsFK(DBTableKey FK)
{
return m_MapFKtoPK.ContainsKey(FK);
}
public List<DBTableKey> PKKeyList(string strOwner, string strTable, string strColumn)
{
if (m_MapFKtoPK != null)
{
DBTableKey FK = new DBTableKey(strOwner, strTable, strColumn);
if (m_MapFKtoPK.ContainsKey(FK))
return m_MapFKtoPK[FK];
}
return null;
}
public List<DBTableKey> FKKeyList(string strOwner, string strTable, string strColumn)
{
if (m_MapPKtoFK != null)
{
DBTableKey PK = new DBTableKey(strOwner, strTable, strColumn);
if (m_MapPKtoFK.ContainsKey(PK))
return m_MapPKtoFK[PK];
}
return null;
}
public void AddKeyMap(string strPKOwn, string strPKTab, string strPKCol, string strFKOwn, string strFKTab, string strFKCol)
{
// Primary Key
DBTableKey PK = new DBTableKey(strPKOwn, strPKTab, strPKCol);
List<DBTableKey> FKeyList;
if (!m_MapPKtoFK.ContainsKey(PK))
{
FKeyList = new List<DBTableKey>();
m_MapPKtoFK[PK] = FKeyList;
}
DBTableKey FK = new DBTableKey(strFKOwn, strFKTab, strFKCol);
FKeyList = m_MapPKtoFK[PK];
FKeyList.Add(FK);
m_MapPKtoFK[PK] = FKeyList;
// Foreign Key
List<DBTableKey> PKeyList;
if (!m_MapFKtoPK.ContainsKey(FK))
{
PKeyList = new List<DBTableKey>();
m_MapFKtoPK[FK] = PKeyList;
}
PKeyList = m_MapFKtoPK[FK];
PKeyList.Add(PK);
m_MapFKtoPK[FK] = PKeyList;
}
public override string ToString()
{
return m_TableKey.ToString();
}
public override int GetHashCode()
{
return Utility.GetHashCode(ToString());
}
public int CompareTo(DBTable rhs)
{
if (Equals(rhs))
return 0;
return ToString().CompareTo(rhs.ToString());
}
int IComparable.CompareTo(object rhs)
{
if (!(rhs is DBTable))
throw new InvalidOperationException("CompareTo: Not a DBTable");
return CompareTo((DBTable)rhs);
}
public static bool operator <(DBTable lhs, DBTable rhs) => lhs.CompareTo(rhs) < 0;
public static bool operator >(DBTable lhs, DBTable rhs) => lhs.CompareTo(rhs) > 0;
public bool Equals(DBTable rhs) => ToString() == rhs.ToString();
public override bool Equals(object rhs)
{
if (!(rhs is DBTable))
return false;
return Equals((DBTable)rhs);
}
public static bool operator ==(DBTable lhs, DBTable rhs) => lhs.Equals(rhs);
public static bool operator !=(DBTable lhs, DBTable rhs) => !(lhs == rhs);
public string TableSchema
{
get { return m_TableKey.Schema; }
set { m_TableKey.Schema = string.IsNullOrEmpty(value) ? value : string.Empty; }
}
public string TableName
{
get { return m_TableKey.Table; }
set { m_TableKey.Table = string.IsNullOrEmpty(value) ? value : string.Empty; }
}
public List<DBColumn> Columns
{
get { return m_Cols; }
set { m_Cols = value; }
}
public List<DBTableKey> Keys
{
get { return m_PKeys; }
}
public bool HasKey(DBTableKey PK)
{
return Keys.Contains(PK);
}
public bool Visited
{
get { return m_bVisited; }
set { m_bVisited = value; }
}
public int Rows
{
get { return m_nRows; }
set { m_nRows = value; }
}
public int SelectedIndex
{
get { return m_iSelectedIndex; }
set { m_iSelectedIndex = value; }
}
public bool View
{
get { return m_bView; }
set { m_bView = value; }
}
}
}