DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CreateViewNode.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 ObjectNameNode ViewName { get; private set; }
28 
29  public bool ReplaceIfExists { get; private set; }
30 
31  public IEnumerable<string> ColumnNames { get; private set; }
32 
33  public SqlQueryExpressionNode QueryExpression { get; private set; }
34 
35  protected override ISqlNode OnChildNode(ISqlNode node) {
36  if (node.NodeName == "column_list_opt") {
37  GetColumnList(node);
38  } else if (node.NodeName == "or_replace_opt") {
39  if (node.ChildNodes.Any())
40  ReplaceIfExists = true;
41  } else if (node is ObjectNameNode) {
42  ViewName = (ObjectNameNode) node;
43  } else if (node is SqlQueryExpressionNode) {
44  QueryExpression = (SqlQueryExpressionNode) node;
45  }
46 
47  return base.OnChildNode(node);
48  }
49 
50  private void GetColumnList(ISqlNode node) {
51  var columnListNode = node.ChildNodes.FirstOrDefault();
52  if (columnListNode == null)
53  return;
54 
55  var columnNames = (columnListNode.ChildNodes.Where(childNode => childNode.NodeName.Equals("column_name"))
56  .Select(childNode => childNode.ChildNodes.FirstOrDefault())
57  .Where(columnName => columnName != null && columnName is IdentifierNode)
58  .Select(columnName => ((IdentifierNode) columnName).Text)).ToList();
59 
60  ColumnNames = columnNames.AsEnumerable();
61  }
62 
63  protected override void BuildStatement(StatementBuilder builder) {
64  var queryExpression = (SqlQueryExpression)ExpressionBuilder.Build(QueryExpression);
65  var statement = new CreateViewStatement(ViewName.Name, ColumnNames, queryExpression);
66  builder.Statements.Add(statement);
67  }
68  }
69 }
This is a simple identifier within a SQL grammar.
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
Represents a composed name for an object within the system.
string NodeName
Gets the name of the node analyzed from the parser.
Definition: ISqlNode.cs:29
static SqlExpression Build(IExpressionNode node)
override void BuildStatement(StatementBuilder builder)
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...