DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
UpdateStatementNode.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 
23 
24 namespace Deveel.Data.Sql.Parser {
26  public SimpleUpdateNode SimpleUpdate { get; private set; }
27 
28  public QueryUpdateNode QueryUpdate { get; private set; }
29 
30  protected override ISqlNode OnChildNode(ISqlNode node) {
31  if (node is SimpleUpdateNode) {
32  SimpleUpdate = (SimpleUpdateNode) node;
33  } else if (node is QueryUpdateNode) {
34  QueryUpdate = (QueryUpdateNode) node;
35  }
36 
37  return base.OnChildNode(node);
38  }
39 
40  protected override void BuildStatement(StatementBuilder builder) {
41  if (SimpleUpdate != null) {
42  BuildSimpleUpdate(builder, SimpleUpdate);
43  } else if (QueryUpdate != null) {
44  BuildQueryUpdate(builder, QueryUpdate);
45  }
46  }
47 
48  private void BuildSimpleUpdate(StatementBuilder builder, SimpleUpdateNode node) {
49  var whereExpression = ExpressionBuilder.Build(node.WhereExpression);
50  var assignments = UpdateAssignments(node.Columns);
51  builder.Statements.Add(new UpdateStatement(node.TableName, whereExpression, assignments));
52  }
53 
54  private IEnumerable<SqlColumnAssignment> UpdateAssignments(IEnumerable<UpdateColumnNode> columns) {
55  if (columns == null)
56  return null;
57 
58  return columns.Select(column => new SqlColumnAssignment(column.ColumnName, ExpressionBuilder.Build(column.Expression)));
59  }
60 
61  private void BuildQueryUpdate(StatementBuilder builder, QueryUpdateNode node) {
62  throw new NotImplementedException();
63  }
64  }
65 }
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
void BuildQueryUpdate(StatementBuilder builder, QueryUpdateNode node)
static SqlExpression Build(IExpressionNode node)
override ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
ICollection< IStatement > Statements
IEnumerable< UpdateColumnNode > Columns
override void BuildStatement(StatementBuilder builder)
void BuildSimpleUpdate(StatementBuilder builder, SimpleUpdateNode node)
IEnumerable< SqlColumnAssignment > UpdateAssignments(IEnumerable< UpdateColumnNode > columns)