forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTsQuery.cs
More file actions
512 lines (471 loc) · 16.4 KB
/
NpgsqlTsQuery.cs
File metadata and controls
512 lines (471 loc) · 16.4 KB
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#region License
// The PostgreSQL License
//
// Copyright (C) 2017 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using JetBrains.Annotations;
#pragma warning disable CA1034
// ReSharper disable once CheckNamespace
namespace NpgsqlTypes
{
/// <summary>
/// Represents a PostgreSQL tsquery. This is the base class for lexeme, not, and and or nodes.
/// </summary>
public abstract class NpgsqlTsQuery
{
/// <summary>
/// Node kind
/// </summary>
public NodeKind Kind { get; protected set; }
/// <summary>
/// NodeKind
/// </summary>
public enum NodeKind
{
/// <summary>
/// Lexeme
/// </summary>
Lexeme,
/// <summary>
/// Not operator
/// </summary>
Not,
/// <summary>
/// And operator
/// </summary>
And,
/// <summary>
/// Or operator
/// </summary>
Or,
/// <summary>
/// Represents the empty tsquery. Should only be used at top level.
/// </summary>
Empty
}
internal abstract void Write(StringBuilder sb, bool first = false);
/// <summary>
/// Writes the tsquery in PostgreSQL's text format.
/// </summary>
/// <returns></returns>
public override string ToString()
{
var sb = new StringBuilder();
Write(sb, true);
return sb.ToString();
}
/// <summary>
/// Parses a tsquery in PostgreSQL's text format.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static NpgsqlTsQuery Parse(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
var valStack = new Stack<NpgsqlTsQuery>();
var opStack = new Stack<char>();
var sb = new StringBuilder();
var pos = 0;
var expectingBinOp = false;
NextToken:
if (pos >= value.Length)
goto Finish;
var ch = value[pos++];
if (ch == '\'')
goto WaitEndComplex;
if ((ch == ')' || ch == '|' || ch == '&') && !expectingBinOp || (ch == '(' || ch == '!') && expectingBinOp)
throw new FormatException("Syntax error in tsquery. Unexpected token.");
if (ch == '(' || ch == '!' || ch == '&')
{
opStack.Push(ch);
expectingBinOp = false;
goto NextToken;
}
if (ch == '|')
{
if (opStack.Count > 0 && opStack.Peek() == '|')
{
if (valStack.Count < 2)
throw new FormatException("Syntax error in tsquery");
var right = valStack.Pop();
var left = valStack.Pop();
valStack.Push(new NpgsqlTsQueryOr(left, right));
// Implicit pop and repush |
}
else
opStack.Push('|');
expectingBinOp = false;
goto NextToken;
}
if (ch == ')')
{
while (opStack.Count > 0 && opStack.Peek() != '(')
{
if (valStack.Count < 2 || opStack.Peek() == '!')
throw new FormatException("Syntax error in tsquery");
var right = valStack.Pop();
var left = valStack.Pop();
valStack.Push(opStack.Pop() == '&' ? (NpgsqlTsQuery)new NpgsqlTsQueryAnd(left, right) : new NpgsqlTsQueryOr(left, right));
}
if (opStack.Count == 0)
throw new FormatException("Syntax error in tsquery: closing parenthesis without an opening parenthesis");
opStack.Pop();
goto PushedVal;
}
if (ch == ':')
throw new FormatException("Unexpected : while parsing tsquery");
if (char.IsWhiteSpace(ch))
goto NextToken;
pos--;
if (expectingBinOp)
throw new FormatException("Unexpected lexeme while parsing tsquery");
// Proceed to WaitEnd
WaitEnd:
if (pos >= value.Length || char.IsWhiteSpace(ch = value[pos]) || ch == '!' || ch == '&' || ch == '|' || ch == '(' || ch == ')')
{
valStack.Push(new NpgsqlTsQueryLexeme(sb.ToString()));
goto PushedVal;
}
pos++;
if (ch == ':')
{
valStack.Push(new NpgsqlTsQueryLexeme(sb.ToString()));
sb.Clear();
goto InWeightInfo;
}
if (ch == '\\')
{
if (pos >= value.Length)
throw new FormatException(@"Unexpected \ in end of value");
ch = value[pos++];
}
sb.Append(ch);
goto WaitEnd;
WaitEndComplex:
if (pos >= value.Length)
throw new FormatException("Missing terminating ' in string literal");
ch = value[pos++];
if (ch == '\'')
{
if (pos < value.Length && value[pos] == '\'')
{
ch = '\'';
pos++;
}
else
{
valStack.Push(new NpgsqlTsQueryLexeme(sb.ToString()));
if (pos < value.Length && value[pos] == ':')
{
pos++;
goto InWeightInfo;
}
goto PushedVal;
}
}
if (ch == '\\')
{
if (pos >= value.Length)
throw new FormatException(@"Unexpected \ in end of value");
ch = value[pos++];
}
sb.Append(ch);
goto WaitEndComplex;
InWeightInfo:
if (pos >= value.Length)
goto Finish;
ch = value[pos];
if (ch == '*')
((NpgsqlTsQueryLexeme)valStack.Peek()).IsPrefixSearch = true;
else if (ch == 'a' || ch == 'A')
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.A;
else if (ch == 'b' || ch == 'B')
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.B;
else if (ch == 'c' || ch == 'C')
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.C;
else if (ch == 'd' || ch == 'D')
((NpgsqlTsQueryLexeme)valStack.Peek()).Weights |= NpgsqlTsQueryLexeme.Weight.D;
else
goto PushedVal;
pos++;
goto InWeightInfo;
PushedVal:
sb.Clear();
while (opStack.Count > 0 && (opStack.Peek() == '&' || opStack.Peek() == '!'))
{
if (opStack.Peek() == '&')
{
if (valStack.Count < 2)
throw new FormatException("Syntax error in tsquery");
var right = valStack.Pop();
var left = valStack.Pop();
valStack.Push(new NpgsqlTsQueryAnd(left, right));
}
else if (opStack.Peek() == '!')
{
if (valStack.Count == 0)
throw new FormatException("Syntax error in tsquery");
valStack.Push(new NpgsqlTsQueryNot(valStack.Pop()));
}
opStack.Pop();
}
expectingBinOp = true;
goto NextToken;
Finish:
while (opStack.Count > 0)
{
if (valStack.Count < 2 || (opStack.Peek() != '|' && opStack.Peek() != '&'))
throw new FormatException("Syntax error in tsquery");
var right = valStack.Pop();
var left = valStack.Pop();
valStack.Push(opStack.Pop() == '&' ? (NpgsqlTsQuery)new NpgsqlTsQueryAnd(left, right) : new NpgsqlTsQueryOr(left, right));
}
if (valStack.Count != 1)
throw new FormatException("Syntax error in tsquery");
return valStack.Pop();
}
}
/// <summary>
/// TsQuery Lexeme node.
/// </summary>
public sealed class NpgsqlTsQueryLexeme : NpgsqlTsQuery
{
string _text;
/// <summary>
/// Lexeme text.
/// </summary>
public string Text
{
get => _text;
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("Text is null or empty string", nameof(value));
_text = value;
}
}
Weight _weights;
/// <summary>
/// Weights is a bitmask of the Weight enum.
/// </summary>
public Weight Weights
{
get => _weights;
set
{
if (((byte)value >> 4) != 0)
throw new ArgumentOutOfRangeException(nameof(value), "Illegal weights");
_weights = value;
}
}
/// <summary>
/// Prefix search.
/// </summary>
public bool IsPrefixSearch { get; set; }
/// <summary>
/// Creates a tsquery lexeme with only lexeme text.
/// </summary>
/// <param name="text">Lexeme text.</param>
public NpgsqlTsQueryLexeme(string text) : this(text, Weight.None, false) { }
/// <summary>
/// Creates a tsquery lexeme with lexeme text and weights.
/// </summary>
/// <param name="text">Lexeme text.</param>
/// <param name="weights">Bitmask of enum Weight.</param>
public NpgsqlTsQueryLexeme(string text, Weight weights) : this(text, weights, false) { }
/// <summary>
/// Creates a tsquery lexeme with lexeme text, weights and prefix search flag.
/// </summary>
/// <param name="text">Lexeme text.</param>
/// <param name="weights">Bitmask of enum Weight.</param>
/// <param name="isPrefixSearch">Is prefix search?</param>
public NpgsqlTsQueryLexeme(string text, Weight weights, bool isPrefixSearch)
{
Kind = NodeKind.Lexeme;
Text = text;
Weights = weights;
IsPrefixSearch = isPrefixSearch;
}
/// <summary>
/// Weight enum, can be OR'ed together.
/// </summary>
#pragma warning disable CA1714
[Flags]
public enum Weight
#pragma warning restore CA1714
{
/// <summary>
/// None
/// </summary>
None = 0,
/// <summary>
/// D
/// </summary>
D = 1,
/// <summary>
/// C
/// </summary>
C = 2,
/// <summary>
/// B
/// </summary>
B = 4,
/// <summary>
/// A
/// </summary>
A = 8
}
internal override void Write(StringBuilder sb, bool first = false)
{
sb.Append('\'').Append(Text.Replace(@"\", @"\\").Replace("'", "''")).Append('\'');
if (IsPrefixSearch || Weights != Weight.None)
sb.Append(':');
if (IsPrefixSearch)
sb.Append('*');
if ((Weights & Weight.A) != Weight.None)
sb.Append('A');
if ((Weights & Weight.B) != Weight.None)
sb.Append('B');
if ((Weights & Weight.C) != Weight.None)
sb.Append('C');
if ((Weights & Weight.D) != Weight.None)
sb.Append('D');
}
}
/// <summary>
/// TsQuery Not node.
/// </summary>
public sealed class NpgsqlTsQueryNot : NpgsqlTsQuery
{
/// <summary>
/// Child node
/// </summary>
public NpgsqlTsQuery Child { get; set; }
/// <summary>
/// Creates a not operator, with a given child node.
/// </summary>
/// <param name="child"></param>
public NpgsqlTsQueryNot([CanBeNull] NpgsqlTsQuery child)
{
Kind = NodeKind.Not;
Child = child;
}
internal override void Write(StringBuilder sb, bool first = false)
{
sb.Append('!');
if (Child == null)
{
sb.Append("''");
}
else
{
if (Child.Kind != NodeKind.Lexeme)
sb.Append("( ");
Child.Write(sb, true);
if (Child.Kind != NodeKind.Lexeme)
sb.Append(" )");
}
}
}
/// <summary>
/// Base class for TsQuery binary operators (& and |).
/// </summary>
public abstract class NpgsqlTsQueryBinOp : NpgsqlTsQuery
{
/// <summary>
/// Left child
/// </summary>
public NpgsqlTsQuery Left { get; set; }
/// <summary>
/// Right child
/// </summary>
public NpgsqlTsQuery Right { get; set; }
}
/// <summary>
/// TsQuery And node.
/// </summary>
public sealed class NpgsqlTsQueryAnd : NpgsqlTsQueryBinOp
{
/// <summary>
/// Creates an and operator, with two given child nodes.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
public NpgsqlTsQueryAnd([CanBeNull] NpgsqlTsQuery left, [CanBeNull] NpgsqlTsQuery right)
{
Kind = NodeKind.And;
Left = left;
Right = right;
}
internal override void Write(StringBuilder sb, bool first = false)
{
Left.Write(sb);
sb.Append(" & ");
Right.Write(sb);
}
}
/// <summary>
/// TsQuery Or Node.
/// </summary>
public sealed class NpgsqlTsQueryOr : NpgsqlTsQueryBinOp
{
/// <summary>
/// Creates an or operator, with two given child nodes.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
public NpgsqlTsQueryOr([CanBeNull] NpgsqlTsQuery left, [CanBeNull] NpgsqlTsQuery right)
{
Kind = NodeKind.Or;
Left = left;
Right = right;
}
internal override void Write(StringBuilder sb, bool first = false)
{
if (!first)
sb.Append("( ");
Left.Write(sb);
sb.Append(" | ");
Right.Write(sb);
if (!first)
sb.Append(" )");
}
}
/// <summary>
/// Represents an empty tsquery. Shold only be used as top node.
/// </summary>
public sealed class NpgsqlTsQueryEmpty : NpgsqlTsQuery
{
/// <summary>
/// Creates a tsquery that represents an empty query. Should not be used as child node.
/// </summary>
public NpgsqlTsQueryEmpty()
{
Kind = NodeKind.Empty;
}
internal override void Write(StringBuilder sb, bool first = false)
{
}
}
}