forked from tloten/bulk-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryRunner.cs
224 lines (200 loc) · 8.64 KB
/
QueryRunner.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace BulkQuery
{
public static class QueryRunner
{
public static List<DatabaseDefinition> GetDatabasesForServer(ServerDefinition server)
{
var databases = new List<DatabaseDefinition>();
const string query = "SELECT name FROM master.dbo.sysdatabases";
var builder = new SqlConnectionStringBuilder(server.ConnectionString)
{
ConnectTimeout = 5
};
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
var dbName = reader["name"] as string;
databases.Add(new DatabaseDefinition(dbName, server));
}
}
finally
{
// Always call Close when done reading.
reader.Close();
}
}
return databases;
}
private static bool AreColumnsIdentical(DataColumnCollection columnsA, DataColumnCollection columnsB)
{
if (columnsA.Count != columnsB.Count)
return false;
for (int i = 0; i < columnsA.Count; i++)
{
var colA = columnsA[i];
var colB = columnsB[i];
if (colA.DataType != colB.DataType || colA.ColumnName != colB.ColumnName || colA.Ordinal != colB.Ordinal)
return false;
}
return true;
}
public class QueryResult
{
public List<string> Messages { get; set; }
public DataTable ResultTable { get; set; }
}
public static Task<QueryResult> BulkQuery(IList<DatabaseDefinition> databases, string query)
{
// Needed to push all async code to thread pool thread (otherwise UI thread attempts to keep thread affinity
// for each async continuation).
// http://stackoverflow.com/a/14485163/505457
return Task.Run(() => BulkQueryInternal(databases, query));
}
private static async Task<QueryResult> BulkQueryInternal(IList<DatabaseDefinition> databases, string query)
{
var resultsTasks = databases
.Select(db => SingleQuery(db, query))
.ToList();
await Task.WhenAll(resultsTasks);
var results = resultsTasks.Select(t => t.Result);
DataTable aggregateResultTable = null;
var messages = new List<string>();
foreach (var result in results)
{
messages.AddRange(result.Messages);
if (result.ResultTable == null)
continue;
if (aggregateResultTable == null)
{
// First set of results.. we'll use this as the 'schema' and all subsequent results must match it.
aggregateResultTable = result.ResultTable;
}
else
{
// Check that the 'schema' of this db's result matches the initial schema.
if (!AreColumnsIdentical(aggregateResultTable.Columns, result.ResultTable.Columns))
{
messages.Add($"Columns returned by {result.ResultTable.TableName} does not match those of {aggregateResultTable.TableName}.");
}
else
{
// Copy the rows over to the existing datatable.
foreach (var row in result.ResultTable.Rows.Cast<DataRow>())
{
aggregateResultTable.Rows.Add(row.ItemArray);
}
}
}
}
return new QueryResult
{
Messages = messages,
ResultTable = aggregateResultTable
};
}
private static async Task<QueryResult> SingleQuery(DatabaseDefinition db, string query)
{
var result = new QueryResult
{
Messages = new List<string>()
};
// var isDbNameUnique = databases.Count(d => d.DatabaseName == db.DatabaseName) == 1;
// var friendlyDbName = db.DatabaseName;
// if (!isDbNameUnique)
// {
// friendlyDbName += " - " + db.Server.DisplayName;
// }
var friendlyDbName = $"{db.DatabaseName} - {db.Server.DisplayName}";
var builder = new SqlConnectionStringBuilder(db.Server.ConnectionString)
{
InitialCatalog = db.DatabaseName
};
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
try
{
Debug.WriteLine($"connecting to {friendlyDbName}");
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = query;
Debug.WriteLine("querying " + friendlyDbName);
using (var dataReader = await command.ExecuteReaderAsync())
{
Debug.WriteLine($"reading results from {friendlyDbName}");
result.ResultTable = ReadTable(dataReader);
result.ResultTable.TableName = friendlyDbName;
Debug.WriteLine($"finished reading results from {friendlyDbName}");
// Add a column that shows which DB it came from.
var sourceCol = new DataColumn("Source Database", typeof(string));
result.ResultTable.Columns.Add(sourceCol);
foreach (var col in result.ResultTable.Columns.Cast<DataColumn>().ToList())
{
if (col != sourceCol)
col.SetOrdinal(col.Ordinal + 1);
}
sourceCol.SetOrdinal(0);
foreach (var row in result.ResultTable.Rows.Cast<DataRow>())
{
row[sourceCol] = friendlyDbName;
}
}
}
catch (Exception ex)
{
result.Messages.Add($"Error on {friendlyDbName}: {ex.Message}");
}
}
return result;
}
// The built in dataTable.Load(dataReader) method is very slow for large data sets.
// This one is taken from http://stackoverflow.com/questions/18961938/populate-data-table-from-data-reader
// and seems to be significantly faster.
private static DataTable ReadTable(SqlDataReader dataReader)
{
var dtSchema = dataReader.GetSchemaTable();
var dt = new DataTable();
var listCols = new List<DataColumn>();
if (dtSchema != null)
{
foreach (DataRow drow in dtSchema.Rows)
{
string columnName = Convert.ToString(drow["ColumnName"]);
DataColumn column = new DataColumn(columnName, (Type)(drow["DataType"]))
{
AllowDBNull = (bool)drow["AllowDBNull"]
};
listCols.Add(column);
dt.Columns.Add(column);
}
}
// Read rows from DataReader and populate the DataTable
while (dataReader.Read())
// this can also be made async, but it doesn't seem to result in a perf gain, and can sometimes
// make the connections flaky (intermittent read failures with lots of parallel queries), so leaving
// it as a blocking read for now.
//while (await dataReader.ReadAsync())
{
DataRow dataRow = dt.NewRow();
for (var i = 0; i < listCols.Count; i++)
{
dataRow[listCols[i]] = dataReader[i];
}
dt.Rows.Add(dataRow);
}
return dt;
}
}
}