DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SelectStatementNode.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.Collections.Generic;
19 using System.Linq;
20 using System.Linq.Expressions;
21 
24 
25 namespace Deveel.Data.Sql.Parser {
27  public SqlQueryExpressionNode QueryExpression { get; private set; }
28 
34  public IEnumerable<OrderByNode> OrderBy { get; private set; }
35 
36  public LimitNode Limit { get; private set; }
37 
38  protected override ISqlNode OnChildNode(ISqlNode node) {
39  if (node.NodeName == "sql_query_expression") {
40  QueryExpression = node as SqlQueryExpressionNode;
41  } else if (node.NodeName == "order_opt") {
42  GetOrderBy(node);
43  } else if (node.NodeName == "limit_opt") {
44  GetLimit(node);
45  }
46 
47  return base.OnChildNode(node);
48  }
49 
50  private void GetOrderBy(ISqlNode node) {
51  var listNode = node.FindByName("sorted_def_list");
52  if (listNode != null) {
53  OrderBy = listNode.ChildNodes.Cast<OrderByNode>();
54  }
55  }
56 
57  private void GetLimit(ISqlNode node) {
58  var child = node.ChildNodes.FirstOrDefault();
59  if (child != null)
60  Limit = (LimitNode) child;
61  }
62 
63  protected override void BuildStatement(StatementBuilder builder) {
64  var queryExpression = (SqlQueryExpression) ExpressionBuilder.Build(QueryExpression);
65  if (QueryExpression.IntoClause != null) {
66  var refExp = ExpressionBuilder.Build(QueryExpression.IntoClause);
67  builder.Statements.Add(new SelectIntoStatement(queryExpression, refExp));
68  } else {
69  var orderBy = BuildOrderBy(OrderBy);
70  var statement = new SelectStatement(queryExpression, orderBy);
71  statement.Limit = BuildLimit(Limit);
72  builder.Statements.Add(statement);
73  }
74  }
75 
76  private IEnumerable<SortColumn> BuildOrderBy(IEnumerable<OrderByNode> nodes) {
77  if (nodes == null)
78  return null;
79 
80  return nodes.Select(node => new SortColumn(ExpressionBuilder.Build(node.Expression), node.Ascending));
81  }
82 
84  if (node == null)
85  return null;
86 
87  return new QueryLimit(node.Offset, node.Count);
88  }
89  }
90 }
Within an SQL query node, this describes the ordering criteria that will be applied when returning th...
Definition: OrderByNode.cs:25
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
string NodeName
Gets the name of the node analyzed from the parser.
Definition: ISqlNode.cs:29
static SqlExpression Build(IExpressionNode node)
ICollection< IStatement > Statements
The root node of an expression used to select a set of items from a set of sources defined...
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...
Object used to represent a column in the ORDER BY clauses of a select statement.
Definition: SortColumn.cs:26
IEnumerable< SortColumn > BuildOrderBy(IEnumerable< OrderByNode > nodes)
override void BuildStatement(StatementBuilder builder)