-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySqlScript.cs
469 lines (419 loc) · 15.1 KB
/
MySqlScript.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright � 2004, 2014, Oracle and/or its affiliates. All rights reserved.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System.Collections.Generic;
using System;
using System.Data;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using MySqlConnector;
namespace MySql.Data.MySqlClient
{
/// <summary>
/// Provides a class capable of executing a SQL script containing
/// multiple SQL statements including CREATE PROCEDURE statements
/// that require changing the delimiter
/// </summary>
public class MySqlScript
{
private MySqlConnection connection;
private string query;
private string delimiter;
public event MySqlStatementExecutedEventHandler StatementExecuted;
public event MySqlScriptErrorEventHandler Error;
public event EventHandler ScriptCompleted;
#region Constructors
/// <summary>
/// Initializes a new instance of the
/// <see cref="MySqlScript"/> class.
/// </summary>
public MySqlScript()
{
Delimiter = ";";
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="MySqlScript"/> class.
/// </summary>
/// <param name="connection">The connection.</param>
public MySqlScript(MySqlConnection connection)
: this()
{
this.connection = connection;
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="MySqlScript"/> class.
/// </summary>
/// <param name="query">The query.</param>
public MySqlScript(string query)
: this()
{
this.query = query;
}
/// <summary>
/// Initializes a new instance of the
/// <see cref="MySqlScript"/> class.
/// </summary>
/// <param name="connection">The connection.</param>
/// <param name="query">The query.</param>
public MySqlScript(MySqlConnection connection, string query)
: this()
{
this.connection = connection;
this.query = query;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the connection.
/// </summary>
/// <value>The connection.</value>
public MySqlConnection Connection
{
get { return connection; }
set { connection = value; }
}
/// <summary>
/// Gets or sets the query.
/// </summary>
/// <value>The query.</value>
public string Query
{
get { return query; }
set { query = value; }
}
/// <summary>
/// Gets or sets the delimiter.
/// </summary>
/// <value>The delimiter.</value>
public string Delimiter
{
get { return delimiter; }
set { delimiter = value; }
}
public bool AnsiQuotes { get; set; } = true;
public bool NoBackslashEscapes { get; set; } = true;
#endregion
#region Public Methods
/// <summary>
/// Executes this instance.
/// </summary>
/// <returns>The number of statements executed as part of the script.</returns>
public int Execute()
{
bool openedConnection = false;
if (connection == null)
throw new InvalidOperationException("Connection not set");
if (query == null || query.Length == 0)
return 0;
// next we open up the connetion if it is not already open
if (connection.State != ConnectionState.Open)
{
openedConnection = true;
connection.Open();
}
// since we don't allow setting of parameters on a script we can
// therefore safely allow the use of user variables. no one should be using
// this connection while we are using it so we can temporarily tell it
// to allow the use of user variables
bool allowUserVars = true;
try
{
// first we break the query up into smaller queries
List<ScriptStatement> statements = BreakIntoStatements();
int count = 0;
MySqlCommand cmd = new MySqlCommand(null, connection);
foreach (ScriptStatement statement in statements)
{
if (String.IsNullOrEmpty(statement.text)) continue;
cmd.CommandText = statement.text;
try
{
cmd.ExecuteNonQuery();
count++;
OnQueryExecuted(statement);
}
catch (Exception ex)
{
if (Error == null)
throw;
if (!OnScriptError(ex))
break;
}
}
OnScriptCompleted();
return count;
}
finally
{
if (openedConnection)
{
connection.Close();
}
}
}
#endregion
private void OnQueryExecuted(ScriptStatement statement)
{
if (StatementExecuted != null)
{
MySqlScriptEventArgs args = new MySqlScriptEventArgs();
args.Statement = statement;
StatementExecuted(this, args);
}
}
private void OnScriptCompleted()
{
ScriptCompleted?.Invoke(this, EventArgs.Empty);
}
private bool OnScriptError(Exception ex)
{
if (Error != null)
{
MySqlScriptErrorEventArgs args = new MySqlScriptErrorEventArgs(ex);
Error(this, args);
return args.Ignore;
}
return false;
}
private List<int> BreakScriptIntoLines()
{
List<int> lineNumbers = new List<int>();
StringReader sr = new StringReader(query);
string line = sr.ReadLine();
int pos = 0;
while (line != null)
{
lineNumbers.Add(pos);
pos += line.Length;
line = sr.ReadLine();
}
return lineNumbers;
}
private static int FindLineNumber(int position, List<int> lineNumbers)
{
int i = 0;
while (i < lineNumbers.Count && position < lineNumbers[i])
i++;
return i;
}
private List<ScriptStatement> BreakIntoStatements()
{
string currentDelimiter = Delimiter;
int startPos = 0;
List<ScriptStatement> statements = new List<ScriptStatement>();
List<int> lineNumbers = BreakScriptIntoLines();
MySqlTokenizer tokenizer = new MySqlTokenizer(query);
tokenizer.AnsiQuotes = AnsiQuotes;
tokenizer.BackslashEscapes = !NoBackslashEscapes;
string token = tokenizer.NextToken();
while (token != null)
{
if (!tokenizer.Quoted)
{
if (token.ToLower(CultureInfo.InvariantCulture) == "delimiter")
{
tokenizer.NextToken();
AdjustDelimiterEnd(tokenizer);
currentDelimiter = query.Substring(tokenizer.StartIndex,
tokenizer.StopIndex - tokenizer.StartIndex).Trim();
startPos = tokenizer.StopIndex;
}
else
{
// this handles the case where our tokenizer reads part of the
// delimiter
if (currentDelimiter.StartsWith(token, StringComparison.OrdinalIgnoreCase))
{
if ((tokenizer.StartIndex + currentDelimiter.Length) <= query.Length)
{
if (query.Substring(tokenizer.StartIndex, currentDelimiter.Length) == currentDelimiter)
{
token = currentDelimiter;
tokenizer.Position = tokenizer.StartIndex + currentDelimiter.Length;
tokenizer.StopIndex = tokenizer.Position;
}
}
}
int delimiterPos = token.IndexOf(currentDelimiter, StringComparison.OrdinalIgnoreCase);
if (delimiterPos != -1)
{
int endPos = tokenizer.StopIndex - token.Length + delimiterPos;
if (tokenizer.StopIndex == query.Length - 1)
endPos++;
string currentQuery = query.Substring(startPos, endPos - startPos);
ScriptStatement statement = new ScriptStatement();
statement.text = currentQuery.Trim();
statement.line = FindLineNumber(startPos, lineNumbers);
statement.position = startPos - lineNumbers[statement.line];
statements.Add(statement);
startPos = endPos + currentDelimiter.Length;
}
}
}
token = tokenizer.NextToken();
}
// now clean up the last statement
if (startPos < query.Length - 1)
{
string sqlLeftOver = query.Substring(startPos).Trim();
if (!String.IsNullOrEmpty(sqlLeftOver))
{
ScriptStatement statement = new ScriptStatement();
statement.text = sqlLeftOver;
statement.line = FindLineNumber(startPos, lineNumbers);
statement.position = startPos - lineNumbers[statement.line];
statements.Add(statement);
}
}
return statements;
}
private void AdjustDelimiterEnd(MySqlTokenizer tokenizer)
{
if (tokenizer.StopIndex < query.Length)
{
int pos = tokenizer.StopIndex;
char c = query[pos];
while (!Char.IsWhiteSpace(c) && pos < (query.Length - 1))
{
c = query[++pos];
}
tokenizer.StopIndex = pos;
tokenizer.Position = pos;
}
}
/// <summary>
/// Async version of Execute
/// </summary>
/// <returns>The number of statements executed as part of the script inside.</returns>
public Task<int> ExecuteAsync()
{
return ExecuteAsync(CancellationToken.None);
}
public Task<int> ExecuteAsync(CancellationToken cancellationToken)
{
var result = new TaskCompletionSource<int>();
if (cancellationToken == CancellationToken.None || !cancellationToken.IsCancellationRequested)
{
try
{
var executeResult = Execute();
result.SetResult(executeResult);
}
catch (Exception ex)
{
result.SetException(ex);
}
}
else
{
result.SetCanceled();
}
return result.Task;
}
}
/// <summary>
///
/// </summary>
public delegate void MySqlStatementExecutedEventHandler(object sender, MySqlScriptEventArgs args);
/// <summary>
///
/// </summary>
public delegate void MySqlScriptErrorEventHandler(object sender, MySqlScriptErrorEventArgs args);
/// <summary>
///
/// </summary>
public class MySqlScriptEventArgs : EventArgs
{
private ScriptStatement statement;
internal ScriptStatement Statement
{
set { this.statement = value; }
}
/// <summary>
/// Gets the statement text.
/// </summary>
/// <value>The statement text.</value>
public string StatementText
{
get { return statement.text; }
}
/// <summary>
/// Gets the line.
/// </summary>
/// <value>The line.</value>
public int Line
{
get { return statement.line; }
}
/// <summary>
/// Gets the position.
/// </summary>
/// <value>The position.</value>
public int Position
{
get { return statement.position; }
}
}
/// <summary>
///
/// </summary>
public class MySqlScriptErrorEventArgs : MySqlScriptEventArgs
{
private Exception exception;
private bool ignore;
/// <summary>
/// Initializes a new instance of the <see cref="MySqlScriptErrorEventArgs"/> class.
/// </summary>
/// <param name="exception">The exception.</param>
public MySqlScriptErrorEventArgs(Exception exception)
: base()
{
this.exception = exception;
}
/// <summary>
/// Gets the exception.
/// </summary>
/// <value>The exception.</value>
public Exception Exception
{
get { return exception; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MySqlScriptErrorEventArgs"/> is ignore.
/// </summary>
/// <value><c>true</c> if ignore; otherwise, <c>false</c>.</value>
public bool Ignore
{
get { return ignore; }
set { ignore = value; }
}
}
struct ScriptStatement
{
public string text;
public int line;
public int position;
}
}