-
Notifications
You must be signed in to change notification settings - Fork 2
/
VendorSpecific.cs
478 lines (434 loc) · 14.8 KB
/
VendorSpecific.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
470
471
472
473
474
475
476
477
478
/*
* Copyright 2017 Stanislav Muhametsin. All rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using UtilPack;
using TReader = UtilPack.PeekablePotentiallyAsyncReader<System.Char?>;
namespace CBAM.SQL.PostgreSQL.Implementation
{
internal static class Parser
{
// Helper class to keep track of how many chars has been read from the underlying reader
private sealed class TextReaderWrapper : TextReader
{
private readonly TextReader _reader;
private Int32 _charsRead;
internal TextReaderWrapper( TextReader reader )
{
ArgumentValidator.ValidateNotNull( "Reader", reader );
this._reader = reader;
this._charsRead = 0;
}
public Int32 CharsRead
{
get
{
return this._charsRead;
}
}
public override Int32 Read()
{
var retVal = this._reader.Read();
if ( retVal != -1 )
{
++this._charsRead;
}
return retVal;
}
public override Int32 Peek()
{
return this._reader.Peek();
}
protected override void Dispose( bool disposing )
{
// Do nothing - we don't want to close underlying reader.
}
}
// Returns amount of characters read
internal static async ValueTask<Int32[]> ParseStringForNextSQLStatement(
TReader reader,
Boolean standardConformingStrings,
Func<Int32> onParameter
)
{
var parenthesisLevel = 0;
List<Int32> paramIndicesList = null;
var queryEndEncountered = false;
Char? prev1 = null, prev2 = null;
Char? c;
while ( !queryEndEncountered && ( c = await reader.TryReadNextAsync() ).HasValue )
{
switch ( c )
{
case '\'':
await ParseSingleQuotes( reader, standardConformingStrings, prev1, prev2 );
break;
case '"':
await ParseDoubleQuotes( reader );
break;
case '-':
await ParseLineComment( reader );
break;
case '/':
await ParseBlockComment( reader );
break;
case '$':
await ParseDollarQuotes( reader, prev1 );
break;
case '(':
++parenthesisLevel;
break;
case ')':
--parenthesisLevel;
break;
case '?':
if ( onParameter != null )
{
if ( paramIndicesList == null )
{
paramIndicesList = new List<Int32>();
}
paramIndicesList.Add( onParameter() );
}
break;
case ';':
if ( parenthesisLevel == 0 )
{
queryEndEncountered = true;
}
break;
}
prev2 = prev1;
prev1 = c;
}
return paramIndicesList == null ? null : paramIndicesList.ToArray();
}
// See http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html for String Constants with C-style Escapes
// Returns index of the single quote character ending this single quote sequence
internal static async ValueTask<Boolean> ParseSingleQuotes(
TReader reader,
Boolean standardConformingStrings,
Char? prev1,
Char? prev2
)
{
Char? c;
if ( !standardConformingStrings
&& prev1.HasValue
&& prev2.HasValue
&& ( prev1 == 'e' || prev1 == 'E' )
&& CharTerminatesIdentifier( prev2.Value )
)
{
// C-Style escaping
// Treat backslashes as escape character
Char prev = '\0';
while ( ( c = await reader.TryReadNextAsync() ).HasValue )
{
if ( c != '\\' && prev != '\\' && await CheckSingleQuote( reader, c.Value ) )
{
break;
}
prev = c.Value;
}
}
else
{
// Don't treat backslashes as escape character
while ( ( c = await reader.TryReadNextAsync() ).HasValue && !await CheckSingleQuote( reader, c.Value ) ) ;
}
return true;
}
internal static async ValueTask<Boolean> ParseDoubleQuotes(
TReader reader
)
{
Char? c;
while ( ( c = await reader.TryReadNextAsync() ).HasValue )
{
if ( c == '"' )
{
// Check for double-doublequote
if ( ( await reader.TryPeekAsync() ).IsOfValue( '"' ) )
{
await reader.ReadNextAsync();
}
else
{
break;
}
}
}
return true;
}
internal static async ValueTask<Boolean> ParseLineComment(
TReader reader
)
{
if ( ( await reader.TryPeekAsync() ).IsOfValue( '-' ) )
{
// Line comment starting
Char? c;
while ( ( c = await reader.TryReadNextAsync() ).HasValue && c != '\r' && c != '\n' ) ;
}
return true;
}
internal static async ValueTask<Boolean> ParseBlockComment(
TReader reader
)
{
if ( ( await reader.TryPeekAsync() ).IsOfValue( '*' ) )
{
// Block comment starting
// SQL spec says block comments nest
var level = 1;
await reader.ReadNextAsync();
Char? prev = null;
Char? cur = null;
var levelChanged = false;
while ( level != 0 && ( cur = await reader.ReadNextAsync() ).HasValue )
{
var oldLevel = level;
if ( !levelChanged ) // Don't process '*/*' or '/*/' twice
{
if ( prev.HasValue )
{
if ( prev == '*' && cur == '/' )
{
// Block comment ending
--level;
}
else if ( prev == '/' && cur == '*' )
{
// Nested block comment
++level;
}
}
}
levelChanged = level != oldLevel;
prev = cur;
}
}
return true;
}
// See http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html for dollar quote spec
internal static async ValueTask<Boolean> ParseDollarQuotes(
TReader reader,
Char? prev
)
{
var c = await reader.TryPeekAsync();
if ( c.HasValue && ( !prev.HasValue || !IsIdentifierContinuationCharacter( prev.Value ) ) )
{
Char[] tag = null;
if ( c == '$' )
{
tag = Empty<Char>.Array;
}
else if ( IsDollarQuoteTagStartCharacter( c.Value ) )
{
var list = new List<Char>();
while ( ( c = await reader.TryPeekAsync() ).HasValue )
{
if ( c == '$' )
{
tag = list.ToArray();
break;
}
else if ( !IsDollarQuoteTagContinuationCharacter( c.Value ) )
{
break;
}
else
{
list.Add( await reader.ReadNextAsync() );
}
}
}
if ( tag != null )
{
// Read the tag-ending dollar sign
await reader.ReadNextAsync();
var tagLen = tag.Length;
var isEmptyTag = tagLen == 0;
var array = isEmptyTag ? null : new Char[tagLen];
var arrayIdx = tagLen - 1;
while ( ( c = await reader.TryReadNextAsync() ).HasValue )
{
if ( c == '$' )
{
// Check if this is double-dollar-sign for empty tag, or that previous characters are same as tag
if ( isEmptyTag && prev == '$' )
{
break;
}
else if ( !isEmptyTag && CheckForCircularlyFilledArray( tag, tagLen, array, arrayIdx ) )
{
break;
}
}
if ( !isEmptyTag )
{
if ( tag.Length > 1 )
{
if ( arrayIdx == tag.Length - 1 )
{
arrayIdx = 0;
}
else
{
++arrayIdx;
}
}
array[arrayIdx] = (Char) c;
}
prev = c;
}
}
}
return true;
}
// Returns true if this character ends string literal
private static async ValueTask<Boolean> CheckSingleQuote(
TReader reader,
Char prevChar
)
{
var retVal = prevChar == '\'';
if ( retVal )
{
Char? peek;
if ( ( peek = await reader.TryPeekAsync() ).HasValue )
{
// Check for double quotes
if ( peek == '\'' )
{
await reader.ReadNextAsync();
retVal = false;
}
else if ( peek == '\n' || peek == '\r' )
{
// Check for newline-separated string literal ( http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html )
while ( peek.HasValue && peek == '\n' || peek == '\r' )
{
peek = await reader.ReadNextAsync();
}
if ( peek.HasValue && peek == '\'' )
{
retVal = false;
}
}
}
}
return retVal;
}
// Returns true if character terminates identifier in backend parser
private static Boolean CharTerminatesIdentifier( Char c )
{
return c == '"' || IsSpace( c ) || IsOperatorChar( c );
}
// The functions below must be kept in sync with logic of pgsql/src/backend/parser/scan.l
// Returns true if character is treated as space character in backend parser
internal static Boolean IsSpace( Char c )
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
}
// Returns true if the given character is a valid character for an operator in backend parser
private static Boolean IsOperatorChar( Char c )
{
/*
* Extracted from operators defined by {self} and {op_chars}
* in pgsql/src/backend/parser/scan.l.
*/
return ",()[].;:+-*/%^<>=~!@#&|`?".IndexOf( c ) != -1;
}
// Checks wehether character is valid as second or later character of an identifier
private static Boolean IsIdentifierContinuationCharacter( Char c )
{
return ( c >= 'a' && c <= 'z' )
|| ( c >= 'A' && c <= 'Z' )
|| c == '_'
|| c > 127
|| ( c >= '0' && c <= '9' )
|| c == '$';
}
// Checks wthether character is valid as first character of dollar quote tag
private static Boolean IsDollarQuoteTagStartCharacter( Char c )
{
return ( c >= 'a' && c <= 'z' )
|| ( c >= 'A' && c <= 'Z' )
|| c == '_'
|| c > 127;
}
// Checks whether character is valid as second or later character of dollar quote tag
private static Boolean IsDollarQuoteTagContinuationCharacter( Char c )
{
return ( c >= 'a' && c <= 'z' )
|| ( c >= 'A' && c <= 'Z' )
|| c == '_'
|| ( c >= '0' && c <= '9' )
|| c > 127;
}
// auxArrayIndex = index of last set character in auxArray
internal static Boolean CheckForCircularlyFilledArray( Char[] referenceDataArray, Int32 refLen, Char[] auxArray, Int32 auxArrayIndex )
{
var min = auxArrayIndex + 1 - refLen;
var i = refLen - 1;
if ( min >= 0 )
{
// Enough to check that last auxLen chars are same (do check backwards)
for ( var j = auxArrayIndex; i >= 0; --i, --j )
{
if ( referenceDataArray[i] != auxArray[j] )
{
return false;
}
}
}
else
{
var j = auxArrayIndex;
for ( ; j >= 0; --j, --i )
{
if ( referenceDataArray[i] != auxArray[j] )
{
return false;
}
}
for ( j = auxArray.Length - 1; i >= 0; --i, --j )
{
if ( referenceDataArray[i] != auxArray[j] )
{
return false;
}
}
}
return true;
}
}
}
public static partial class E_CBAM
{
internal static Boolean IsOfValue( this Char? nullable, Char value )
{
return nullable.HasValue && nullable.Value == value;
}
}