15
15
#endregion
16
16
17
17
using System ;
18
+ using System . Collections . Generic ;
18
19
using System . IO ;
19
- using System . Text . RegularExpressions ;
20
+ using System . Text ;
20
21
21
22
using FluentMigrator . BatchParser ;
23
+ using FluentMigrator . BatchParser . RangeSearchers ;
22
24
using FluentMigrator . BatchParser . Sources ;
23
25
using FluentMigrator . BatchParser . SpecialTokenSearchers ;
24
26
25
27
using McMaster . Extensions . CommandLineUtils ;
26
28
27
29
namespace TestSqlServerBatchParser
28
30
{
29
- class Program
31
+ internal static class Program
30
32
{
31
- private static string _sqlText ;
33
+ private static StringBuilder _sqlText ;
34
+ private static string _sqlStatement ;
35
+ private static bool _outputEverySqlStatement ;
32
36
33
37
static int Main ( string [ ] args )
34
38
{
@@ -37,12 +41,38 @@ static int Main(string[] args)
37
41
38
42
var scriptFileName = app . Argument < string > ( "script" , "SQL script file name" )
39
43
. IsRequired ( ) ;
40
- var stripComments = app . Option ( "-s|--strip" , "Strip comments" , CommandOptionType . NoValue ) ;
44
+ var stripComments = app . Option ( "--strip" , "Strip comments" , CommandOptionType . NoValue ) ;
45
+ var singleStatement = app . Option ( "-s|--single" , "Output single statements" , CommandOptionType . NoValue ) ;
41
46
42
47
app . OnExecute (
43
48
( ) =>
44
49
{
45
- var batchParser = new SqlServerBatchParser ( ) ;
50
+ // The default range searchers
51
+ var rangeSearchers = new List < IRangeSearcher >
52
+ {
53
+ new MultiLineComment ( ) ,
54
+ new DoubleDashSingleLineComment ( ) ,
55
+ new PoundSignSingleLineComment ( ) ,
56
+ new SqlString ( ) ,
57
+ } ;
58
+
59
+ // The special token searchers
60
+ var specialTokenSearchers = new List < ISpecialTokenSearcher > ( ) ;
61
+
62
+ // Add SQL server specific range searcher
63
+ rangeSearchers . Add ( new SqlServerIdentifier ( ) ) ;
64
+
65
+ // Add SQL server specific token searcher
66
+ specialTokenSearchers . Add ( new GoSearcher ( ) ) ;
67
+
68
+ // We want every single SQL statement
69
+ _outputEverySqlStatement = singleStatement . HasValue ( ) ;
70
+ if ( _outputEverySqlStatement )
71
+ {
72
+ specialTokenSearchers . Add ( new SemicolonSearcher ( ) ) ;
73
+ }
74
+
75
+ var batchParser = new SqlBatchParser ( rangeSearchers , specialTokenSearchers ) ;
46
76
batchParser . SpecialToken += BatchParserOnSpecialToken ;
47
77
batchParser . SqlText += BatchParserOnSqlText ;
48
78
@@ -61,24 +91,43 @@ static int Main(string[] args)
61
91
62
92
private static void BatchParserOnSqlText ( object sender , SqlTextEventArgs sqlTextEventArgs )
63
93
{
64
- _sqlText = sqlTextEventArgs . SqlText . Trim ( ) ;
94
+ var content = sqlTextEventArgs . SqlText . Trim ( ) ;
95
+ if ( _outputEverySqlStatement )
96
+ {
97
+ if ( _sqlText == null )
98
+ _sqlText = new StringBuilder ( ) ;
99
+ if ( ! string . IsNullOrEmpty ( content ) )
100
+ {
101
+ _sqlText . Append ( content ) . Append ( ';' ) . AppendLine ( ) ;
102
+ _sqlStatement = content ;
103
+ }
104
+ }
105
+ else
106
+ {
107
+ _sqlText = new StringBuilder ( sqlTextEventArgs . SqlText . Trim ( ) ) ;
108
+ }
65
109
}
66
110
67
111
private static void BatchParserOnSpecialToken ( object sender , SpecialTokenEventArgs specialTokenEventArgs )
68
112
{
69
113
if ( specialTokenEventArgs . Opaque is GoSearcher . GoSearcherParameters goParameters )
70
114
{
71
115
RunSql ( goParameters . Count ) ;
116
+ _sqlText = null ;
117
+ }
118
+ else if ( ! string . IsNullOrEmpty ( _sqlStatement ) )
119
+ {
120
+ Console . Out . WriteLine ( "Statement: {0};" , _sqlStatement ) ;
121
+ _sqlStatement = null ;
72
122
}
73
-
74
- _sqlText = null ;
75
123
}
76
124
77
125
private static void RunSql ( int count = 1 )
78
126
{
79
- if ( string . IsNullOrEmpty ( _sqlText ) )
127
+ if ( ( _sqlText ? . Length ?? 0 ) == 0 )
80
128
return ;
81
129
130
+ Console . Out . WriteLine ( "Executing batch:" ) ;
82
131
for ( var i = 0 ; i != count ; ++ i )
83
132
{
84
133
Console . WriteLine ( _sqlText ) ;
0 commit comments