DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CreateTableNode.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.Types;
23 
24 namespace Deveel.Data.Sql.Parser {
26  public ObjectNameNode TableName { get; private set; }
27 
28  public bool IfNotExists { get; private set; }
29 
30  public bool Temporary { get; private set; }
31 
32  public IEnumerable<TableColumnNode> Columns { get; private set; }
33 
34  public IEnumerable<TableConstraintNode> Constraints { get; private set; }
35 
36  protected override void OnNodeInit() {
37  TableName = this.FindNode<ObjectNameNode>();
38  IfNotExists = this.HasOptionalNode("if_not_exists_opt");
39  Temporary = this.HasOptionalNode("temporary_opt");
40 
41  var elements = this.FindNodes<ITableElementNode>().ToList();
42  Columns = elements.OfType<TableColumnNode>();
43  Constraints = elements.OfType<TableConstraintNode>();
44  }
45 
46  protected override void BuildStatement(StatementBuilder builder) {
47  Build(builder.TypeResolver, builder.Statements);
48  }
49 
50  public void Build(ITypeResolver typeResolver, ICollection<IStatement> statements) {
51  string idColumn = null;
52 
53  var tableName = TableName;
54  var constraints = new List<SqlTableConstraint>();
55  var columns = new List<SqlTableColumn>();
56 
57  foreach (var column in Columns) {
58  if (column.IsIdentity) {
59  if (!String.IsNullOrEmpty(idColumn))
60  throw new InvalidOperationException(String.Format("Table {0} defines already {1} as identity column.", TableName,
61  idColumn));
62 
63  if (column.Default != null)
64  throw new InvalidOperationException(String.Format("The identity column {0} cannot have a DEFAULT constraint.",
65  idColumn));
66 
67  idColumn = column.ColumnName.Text;
68  }
69 
70  var columnInfo = column.BuildColumn(typeResolver, tableName.Name, constraints);
71 
72  columns.Add(columnInfo);
73  }
74 
75  //TODO: Optimization: merge same constraints
76 
77  statements.Add(MakeCreateTable(tableName.Name, columns, IfNotExists, Temporary));
78 
79  foreach (var constraint in Constraints) {
80  var constraintInfo = constraint.BuildConstraint();
81  statements.Add(new AlterTableStatement(ObjectName.Parse(tableName.Name), new AddConstraintAction(constraintInfo)));
82  }
83 
84  foreach (var constraint in constraints) {
85  statements.Add(MakeAlterTableAddConstraint(tableName.Name, constraint));
86  }
87  }
88 
89  private static SqlStatement MakeAlterTableAddConstraint(string tableName, SqlTableConstraint constraint) {
90  var action = new AddConstraintAction(constraint);
91 
92  return new AlterTableStatement(ObjectName.Parse(tableName), action);
93  }
94 
95  private static SqlStatement MakeCreateTable(string tableName, IEnumerable<SqlTableColumn> columns, bool ifNotExists,
96  bool temporary) {
97  var tree = new CreateTableStatement(tableName, columns.ToList());
98  tree.IfNotExists = ifNotExists;
99  tree.Temporary = temporary;
100  return tree;
101  }
102  }
103 }
override void BuildStatement(StatementBuilder builder)
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
A long string in the system.
The statement object used to create a table in a database.
Represents a composed name for an object within the system.
void Build(ITypeResolver typeResolver, ICollection< IStatement > statements)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
ICollection< IStatement > Statements
override void OnNodeInit()
After the initialization of the node from the parser, this method is invoked to let the specific init...
static SqlStatement MakeAlterTableAddConstraint(string tableName, SqlTableConstraint constraint)
static SqlStatement MakeCreateTable(string tableName, IEnumerable< SqlTableColumn > columns, bool ifNotExists, bool temporary)