DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
InsertStatementNode.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 
23 
24 namespace Deveel.Data.Sql.Parser {
26  public string TableName { get; private set; }
27 
28  public IEnumerable<string> ColumnNames { get; private set; }
29 
30  public ValuesInsertNode ValuesInsert { get; private set; }
31 
32  public SetInsertNode SetInsert { get; private set; }
33 
34  public QueryInsertNode QueryInsert { get; private set; }
35 
36  protected override ISqlNode OnChildNode(ISqlNode node) {
37  if (node is ObjectNameNode) {
38  TableName = ((ObjectNameNode) node).Name;
39  } else if (node.NodeName.Equals("insert_source")) {
40  var colNode = node.FindByName("column_list_opt");
41  if (colNode != null)
42  ColumnNames = colNode.FindNodes<IdentifierNode>().Select(x => x.Text);
43 
44  ValuesInsert = node.FindNode<ValuesInsertNode>();
45  SetInsert = node.FindNode<SetInsertNode>();
46  QueryInsert = node.FindNode<QueryInsertNode>();
47  }
48 
49  return base.OnChildNode(node);
50  }
51 
52  protected override void BuildStatement(StatementBuilder builder) {
53  if (ValuesInsert != null) {
54  var valueInsert = ValuesInsert;
55  var values =
56  valueInsert.Values.Select(setNode => setNode.Values.Select(ExpressionBuilder.Build).ToArray()).ToList();
57  builder.Statements.Add(new InsertStatement(TableName, ColumnNames, values));
58  } else if (SetInsert != null) {
59  var assignments = SetInsert.Assignments;
60 
61  var columnNames = new List<string>();
62  var values = new List<SqlExpression>();
63  foreach (var assignment in assignments) {
64  var columnName = assignment.ColumnName;
65  var value = ExpressionBuilder.Build(assignment.Value);
66 
67  columnNames.Add(columnName);
68  values.Add(value);
69  }
70 
71  builder.Statements.Add(new InsertStatement(TableName, columnNames.ToArray(), new[] {values.ToArray()}));
72  } else if (QueryInsert != null) {
73  var queryInsert = QueryInsert;
74  var queryExpression = ExpressionBuilder.Build(queryInsert.QueryExpression) as SqlQueryExpression;
75  if (queryExpression == null)
76  throw new SqlParseException();
77 
78  builder.Statements.Add(new InsertSelectStatement(TableName, ColumnNames, queryExpression));
79  }
80  }
81  }
82 }
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)
ICollection< IStatement > Statements
static SqlBinaryExpression Add(SqlExpression left, SqlExpression right)
override void BuildStatement(StatementBuilder builder)
override ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
override ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
An error that occurs when compiling a input string into a SQL object.