-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathWhereClauseExample.cs
More file actions
53 lines (48 loc) · 2.07 KB
/
Copy pathWhereClauseExample.cs
File metadata and controls
53 lines (48 loc) · 2.07 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
using Antlr4.Runtime.Misc;
using SQLParser.Parsers.TSql;
namespace SQLParser.Example
{
/// <summary>
/// This is an example of how to use the parser to parse out the where clause of a select statement.
/// </summary>
public class WhereClauseExample
{
/// <summary>
/// Runs this instance.
/// </summary>
public void Run()
{
// Create a printer to store the results of the parse
var ExamplePrinter = new Printer();
// Parse a statement and pass the printer to the parser
Parser.Parse("SELECT * FROM Somewhere WHERE MyColumn = @parameter1 AND MySecondColumn = @parameter2", ExamplePrinter, Enums.SQLType.TSql);
// ExamplePrinter.WhereClause is now the where clause of the statement
Console.WriteLine(ExamplePrinter.WhereClause);
}
/// <summary>
/// This is an example of a printer that can be used to parse a statement.
/// </summary>
/// <seealso cref="TSqlParserBaseListener"/>
internal class Printer : TSqlParserBaseListener
{
/// <summary>
/// The final where clause found in the statement.
/// </summary>
public string? WhereClause { get; set; }
/// <summary>
/// The select statement is found in the query specification. For our purposes, we only
/// need to check if there is a where clause and if so, store it.
/// </summary>
/// <param name="context">The query context object</param>
public override void EnterQuery_specification([NotNull] TSqlParser.Query_specificationContext context)
{
if (context.where is null)
return;
var StartIndex = context.where.Start.StartIndex;
var StopIndex = context.where.Stop.StopIndex;
var WhereClauseInterval = new Interval(StartIndex, StopIndex);
WhereClause = context.Start.InputStream.GetText(WhereClauseInterval);
}
}
}
}