DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlBinaryExpressionNode.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Linq;
19 using System.Text;
20 
21 namespace Deveel.Data.Sql.Parser {
26  private bool leftSeen;
27 
31  public IExpressionNode Left { get; private set; }
32 
36  public IExpressionNode Right { get; private set; }
37 
42  public bool IsAll { get; private set; }
43 
48  public bool IsAny { get; private set; }
49 
54  public string Operator { get; private set; }
55 
57  protected override ISqlNode OnChildNode(ISqlNode node) {
58  if (node is IExpressionNode) {
59  if (!leftSeen) {
60  Left = (IExpressionNode) node;
61  leftSeen = true;
62  } else {
63  Right = (IExpressionNode) node;
64  leftSeen = false;
65  }
66  } else if (node.NodeName == "binary_op") {
67  GetOperator(node);
68  }
69 
70  return base.OnChildNode(node);
71  }
72 
73  private void GetOperator(ISqlNode node) {
74  var childNode = node.ChildNodes.First();
75  if (childNode.NodeName == "binary_op_simple") {
76  var op = childNode.ChildNodes.First();
77  Operator = ((SqlKeyNode) op).Text;
78  } else if (childNode.NodeName == "logical_op") {
79  GetLogicalOp(childNode);
80  } else if (node.NodeName == "any_op" ||
81  node.NodeName == "all_op") {
82  GetAnyAllOp(childNode);
83  } else if (childNode.NodeName == "subquery_op") {
84  GetLogicalOp(childNode);
85  }
86  }
87 
88  private void GetLogicalOp(ISqlNode node) {
89  var sb = new StringBuilder();
90  foreach (var childNode in node.ChildNodes) {
91  if (childNode is SqlKeyNode) {
92  sb.Append(((SqlKeyNode) childNode).Text);
93  sb.Append(" ");
94  }
95  }
96 
97  Operator = sb.ToString().Trim();
98  }
99 
100  private void GetAnyAllOp(ISqlNode node) {
101  var sb = new StringBuilder();
102  foreach (var childNode in node.ChildNodes) {
103  if (childNode is SqlKeyNode) {
104  var anyOrAll = ((SqlKeyNode) childNode).Text;
105  if (String.Equals(anyOrAll, "ALL", StringComparison.OrdinalIgnoreCase)) {
106  IsAll = true;
107  } else if (String.Equals(anyOrAll, "ANY", StringComparison.OrdinalIgnoreCase)) {
108  IsAny = true;
109  }
110  } else if (childNode.NodeName == "binary_op_simple") {
111  var op = childNode.ChildNodes.First();
112  sb.Append(((SqlKeyNode) op).Text);
113  }
114  }
115 
116  Operator = sb.ToString();
117  }
118  }
119 }
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
Represents a keyword found during the compilation of a source text.
Definition: SqlKeyNode.cs:25
string NodeName
Gets the name of the node analyzed from the parser.
Definition: ISqlNode.cs:29
IEnumerable< ISqlNode > ChildNodes
Gets a read-only enumeration of the children nodes, if any.
Definition: ISqlNode.cs:39
override ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
This interface acts like a marker that indicates if a ISqlNode represents a SQL expression.
Represents an expression that evaluates between two other expressions.
The default implementation of ISqlNode, that is a node in the text analysis parsing of SQL commands...
Definition: SqlNode.cs:32