DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
StatementBuilder.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 
22 using Deveel.Data.Sql.Tables;
23 using Deveel.Data.Types;
24 
25 namespace Deveel.Data.Sql.Parser {
26  public sealed class StatementBuilder : ISqlNodeVisitor {
27  public StatementBuilder(ITypeResolver typeResolver) {
28  TypeResolver = typeResolver;
29  Statements = new List<IStatement>();
30  }
31 
32  public ITypeResolver TypeResolver { get; private set; }
33 
34  public ICollection<IStatement> Statements { get; private set; }
35 
37  Visit(node);
38  }
39 
40  private void Visit(ISqlNode node) {
41  if (node is ISqlVisitableNode) {
42  (node as ISqlVisitableNode).Accept(this);
43  return;
44  }
45 
46  if (node is SequenceOfStatementsNode)
47  VisitSequenceOfStatements((SequenceOfStatementsNode) node);
48  }
49 
51  return DataTypeBuilder.Build(TypeResolver, node);
52  }
53 
55  foreach (var statementNode in node.Statements) {
56  Visit(statementNode);
57  }
58  }
59 
60  public IEnumerable<IStatement> Build(ISqlNode rootNode) {
61  Visit(rootNode);
62  return Statements.ToArray();
63  }
64 
65  internal static ForeignKeyAction GetForeignKeyAction(string actionName) {
66  if (String.Equals("NO ACTION", actionName, StringComparison.OrdinalIgnoreCase) ||
67  String.Equals("NOACTION", actionName, StringComparison.OrdinalIgnoreCase))
68  return ForeignKeyAction.NoAction;
69  if (String.Equals("CASCADE", actionName, StringComparison.OrdinalIgnoreCase))
70  return ForeignKeyAction.Cascade;
71  if (String.Equals("SET DEFAULT", actionName, StringComparison.OrdinalIgnoreCase) ||
72  String.Equals("SETDEFAULT", actionName, StringComparison.OrdinalIgnoreCase))
73  return ForeignKeyAction.SetDefault;
74  if (String.Equals("SET NULL", actionName, StringComparison.OrdinalIgnoreCase) ||
75  String.Equals("SETNULL", actionName, StringComparison.OrdinalIgnoreCase))
76  return ForeignKeyAction.SetNull;
77 
78  throw new NotSupportedException();
79  }
80  }
81 }
An interface that allows implementations of the visitor pattern on SQL compiled nodes.
A long string in the system.
void Visit(ISqlNode node)
Invoked to access the given ISqlNode.
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
IEnumerable< IStatement > Build(ISqlNode rootNode)
static SqlType Build(ITypeResolver resolver, ISqlNode sqlNode)
static ForeignKeyAction GetForeignKeyAction(string actionName)
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
void VisitSequenceOfStatements(SequenceOfStatementsNode node)
An implementation of ISqlNode that accepts visits from a ISqlNodeVisitor
StatementBuilder(ITypeResolver typeResolver)
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
void Visit(ISqlNode node)
Invoked to access the given ISqlNode.
Describes the information of a data type as found in a SQL string.
Definition: DataTypeNode.cs:28
SqlType BuildDataType(DataTypeNode node)