-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableInfo.cs
272 lines (249 loc) · 11.1 KB
/
TableInfo.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
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SyncInstaller
{
internal class TableInfo //: ITableInfo
{
public string ConnString { get; private set; }
public TableInfo(string connString)
{
if (string.IsNullOrEmpty(connString)) throw new ArgumentNullException(nameof(connString));
ConnString = connString;
}
public DataTable GetTables()
{
DataTable data = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(GetTablesQuery(), conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
data.Load(reader);
}
}
return data;
}
}
public IEnumerable<string> GetTableNames()
{
List<string> tables = new List<string>();
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(GetTablesQuery(), conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
tables.Add(reader["TableName"].ToString());
}
}
}
return tables;
}
}
private string GetTablesQuery()
{
return @"
IF EXISTS(SELECT 1 FROM sys.tables where name = '_clnt') --AND EXISTS (SELECT 1 from _clnt)
BEGIN
IF EXISTS(SELECT 1 FROM _clnt)
SELECT [TableID] AS 'TableID'
,[ParentID] AS 'ParentID'
,[TableName] AS 'TableName'
,[Description] AS 'Description'
FROM [_tableData] td
INNER JOIN [SYS].[TABLES] st
ON td.[TableName] = st.[name]
UNION
SELECT st.[object_id] AS 'TableID'
,-1 AS 'ParentID'
,lut.[TableName] AS 'TableName'
,lut.[Description] AS 'Description'
FROM [_lut_tbls] lut
INNER JOIN [SYS].[TABLES] st
ON lut.[TableName] = st.[name]
UNION
SELECT [object_id] AS 'TableID'
,-1 AS 'ParentID'
,[Name] AS 'TableName'
, '' AS 'Description'
FROM [SYS].[TABLES]
WHERE [Name] LIKE '[_]%'
AND [Name] <> '_db'
AND [Name] NOT LIKE '%[_]tracking'
UNION
SELECT st.[object_id] AS 'TableID'
,-1 AS 'ParentID'
,g.[TableName] AS 'TableName'
,g.[Description] AS 'Description'
FROM [_gph] g
INNER JOIN [SYS].[TABLES] st
ON g.[TableName]=st.[name]
ORDER BY TableName,TableID
ELSE
SELECT [object_id] AS 'TableID'
,-1 AS 'ParentID'
,[name] AS 'TableName'
,'' AS 'Description'
FROM [sys].[tables]
WHERE [name] NOT LIKE '%[_]tracking'
AND [Name] <> '_db'
AND [name] not in ('dtproperties', 'sysdiagrams', 'schema_info', 'scope_config', 'scope_info', 'scope_parameters', 'scope_templates')
END
ELSE
SELECT [object_id] AS 'TableID'
,-1 AS 'ParentID'
,[name] AS 'TableName'
,'' AS 'Description'
FROM [sys].[tables]
WHERE [name] NOT LIKE '%[_]tracking'
AND [Name] <> '_db'
AND [name] not in ('dtproperties', 'sysdiagrams', 'schema_info', 'scope_config', 'scope_info', 'scope_parameters', 'scope_templates')
";
}
//public DbSyncTableDescription GetDescription(string tableName)
//{
// return GetDescription(tableName, null);
//}
public Dictionary<string, List<string>> GetTablesAndColumns(IEnumerable<string> tableNames, out string problem)
{
Dictionary<string, List<string>> tablesAndColumns = GetTablesAndColumns(tableNames);
//List<DbSyncTableDescription> result = new List<DbSyncTableDescription>();
//foreach (KeyValuePair<string, List<string>> table in tablesAndColumns)
//{
// DbSyncTableDescription desc = GetDescriptionForTable(table.Key, table.Value, out problem);
// if (problem != null)
// {
// return new List<DbSyncTableDescription>(0);
// }
// result.Add(desc);
//}
problem = null;
return tablesAndColumns;
}
//public IEnumerable<DbSyncTableDescription> GetDescriptions(IEnumerable<string> tableNames, out string problem)
//{
// Dictionary<string, List<string>> tablesAndColumns = GetTablesAndColumns(tableNames);
// List<DbSyncTableDescription> result = new List<DbSyncTableDescription>();
// foreach (KeyValuePair<string, List<string>> table in tablesAndColumns)
// {
// DbSyncTableDescription desc = GetDescriptionForTable(table.Key, table.Value, out problem);
// if (problem != null)
// {
// return new List<DbSyncTableDescription>(0);
// }
// result.Add(desc);
// }
// problem = null;
// return result;
//}
/// <summary>
/// Returns the table description that only contains columns from the list provided (may contain fewer if they were not valid for Sync use)
/// Returns the table description wilth all columns if no column filter
/// No check is made on the tableName however.
/// </summary>
/// <param name="tableName"></param>
/// <param name="columnNames"></param>
/// <returns></returns>
//public DbSyncTableDescription GetDescription(string tableName, IEnumerable<string> columnNames)
//{
// KeyValuePair<string, List<string>> tablesAndColumns = GetTablesAndColumns(new string[] { tableName }).First();
// IEnumerable<string> columns = columnNames == null || columnNames.Count() == 0 ? tablesAndColumns.Value
// : columnNames.Intersect(tablesAndColumns.Value);
// return GetDescriptionForTable(tablesAndColumns.Key, columns.ToList(), out string problem);
//}
//private DbSyncTableDescription GetDescriptionForTable(string tableName, List<string> columnNames, out string problem)
//{
// problem = null;
// if (string.IsNullOrEmpty(tableName))
// {
// problem = string.Format("No table data exists for '{0}'.", tableName);
// }
// if (columnNames == null || columnNames.Count == 0)
// {
// problem = string.Format("No column data exists for '{0}'.", tableName);
// }
// if (problem != null)
// {
// return new DbSyncTableDescription(tableName);
// }
// using (SqlConnection conn = new SqlConnection(ConnString))
// {
// conn.Open();
// DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable(tableName, new Collection<string>(columnNames), conn);
// if (columnNames.Contains("GEO_ID"))
// {
// foreach (var column in tableDesc.Columns)
// {
// column.IsPrimaryKey = column.UnquotedName == "GEO_ID";
// }
// }
// return tableDesc;
// }
//}
private Dictionary<string, List<string>> GetTablesAndColumns(IEnumerable<string> tables)
{
Dictionary<string, List<string>> result = new Dictionary<string, List<string>>();
if (tables == null || tables.Count() == 0)
{
return result;
}
StringBuilder script = new StringBuilder();
//script.Append(@"
// SELECT
// OBJECT_NAME([OBJECT_ID]) AS 'TableName',
// [NAME] AS 'ColumnName'
// FROM [SYS].[COLUMNS]
// WHERE [NAME] NOT IN ('rts','Commands')
// AND TYPE_NAME(user_type_id) NOT IN ('geography','geometry')
// AND [is_computed] = 0
// AND [is_identity] = 0
// ");
script.Append(@"
SELECT
OBJECT_NAME([OBJECT_ID]) AS 'TableName',
[NAME] AS 'ColumnName'
FROM [SYS].[COLUMNS]
WHERE [NAME] NOT IN ('rts','Commands')
AND TYPE_NAME(user_type_id) NOT IN ('geography','geometry')
AND system_type_id NOT IN(241)
AND [is_computed] = 0
AND [is_identity] = 0
");
;
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
using (SqlCommand command = new SqlCommand(script.ToString(), conn))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string tableName = reader["TableName"].ToString();
if (tables.Contains(tableName))
{
if (!result.ContainsKey(tableName))
{
result.Add(tableName, new List<string>());
}
result[tableName].Add(reader["ColumnName"].ToString());
}
}
}
}
}
return result;
}
}
}