DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
AlterTableNode.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 
21 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql.Parser {
25  public string TableName { get; private set; }
26 
27  public IEnumerable<IAlterActionNode> Actions { get; private set; }
28 
29  public CreateTableNode CreateTable { get; private set; }
30 
31  protected override ISqlNode OnChildNode(ISqlNode node) {
32  if (node is ObjectNameNode) {
33  TableName = ((ObjectNameNode) node).Name;
34  } else if (node is CreateTableNode) {
35  CreateTable = (CreateTableNode) node;
36  } else if (node.NodeName == "alter_actions") {
37  Actions = node.FindNodes<IAlterActionNode>();
38  }
39 
40  return base.OnChildNode(node);
41  }
42 
43  protected override void BuildStatement(StatementBuilder builder) {
44  if (CreateTable != null) {
45  var statements = new List<IStatement>();
46  CreateTable.Build(builder.TypeResolver, statements);
47 
48  foreach (var statement in statements) {
49  if (statement is CreateTableStatement)
50  ((CreateTableStatement) statement).IfNotExists = true;
51  }
52 
53  foreach (var statement in statements) {
54  builder.Statements.Add(statement);
55  }
56  } else if (Actions != null) {
57  foreach (var action in Actions) {
58  BuildAction(builder.TypeResolver, ObjectName.Parse(TableName), action, builder.Statements);
59  }
60  }
61  }
62 
63  private static void BuildAction(ITypeResolver typeResolver, ObjectName tableName, IAlterActionNode action,
64  ICollection<IStatement> statements) {
65  if (action is AddColumnNode) {
66  var column = ((AddColumnNode) action).Column;
67  var constraints = new List<SqlTableConstraint>();
68  var columnInfo = column.BuildColumn(typeResolver, tableName.FullName, constraints);
69 
70  statements.Add(new AlterTableStatement(tableName, new AddColumnAction(columnInfo)));
71 
72  foreach (var constraint in constraints) {
73  statements.Add(new AlterTableStatement(tableName, new AddConstraintAction(constraint)));
74  }
75  } else if (action is AddConstraintNode) {
76  var constraint = ((AddConstraintNode) action).Constraint;
77 
78  var constraintInfo = constraint.BuildConstraint();
79  statements.Add(new AlterTableStatement(tableName, new AddConstraintAction(constraintInfo)));
80  } else if (action is DropColumnNode) {
81  var columnName = ((DropColumnNode) action).ColumnName;
82  statements.Add(new AlterTableStatement(tableName, new DropColumnAction(columnName)));
83  } else if (action is DropConstraintNode) {
84  var constraintName = ((DropConstraintNode) action).ConstraintName;
85  statements.Add(new AlterTableStatement(tableName, new DropConstraintAction(constraintName)));
86  } else if (action is SetDefaultNode) {
87  var actionNode = ((SetDefaultNode) action);
88  var columnName = actionNode.ColumnName;
89  var expression = ExpressionBuilder.Build(actionNode.Expression);
90  statements.Add(new AlterTableStatement(tableName, new SetDefaultAction(columnName, expression)));
91  } else if (action is DropDefaultNode) {
92  var columnName = ((DropDefaultNode) action).ColumnName;
93  statements.Add(new AlterTableStatement(tableName, new DropDefaultAction(columnName)));
94  } else if (action is AlterColumnNode) {
95  var column = ((AlterColumnNode) action).Column;
96  var constraints = new List<SqlTableConstraint>();
97  var columnInfo = column.BuildColumn(typeResolver, tableName.FullName, constraints);
98 
99  // CHECK: Here we do a drop and add column: is there a better way on the back-end?
100  statements.Add(new AlterTableStatement(tableName, new DropColumnAction(columnInfo.ColumnName)));
101 
102  statements.Add(new AlterTableStatement(tableName, new AddColumnAction(columnInfo)));
103 
104  foreach (var constraint in constraints) {
105  statements.Add(new AlterTableStatement(tableName, new AddConstraintAction(constraint)));
106  }
107  }
108  }
109  }
110 }
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
The statement object used to create a table in a database.
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)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static void BuildAction(ITypeResolver typeResolver, ObjectName tableName, IAlterActionNode action, ICollection< IStatement > statements)
ICollection< IStatement > Statements
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...
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114