DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TableColumnNode.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 
22 using Deveel.Data.Sql.Tables;
23 using Deveel.Data.Types;
24 
25 namespace Deveel.Data.Sql.Parser {
27  public IdentifierNode ColumnName { get; private set; }
28 
29  public DataTypeNode DataType { get; private set; }
30 
31  public IEnumerable<ColumnConstraintNode> Constraints { get; private set; }
32 
33  public IExpressionNode Default { get; private set; }
34 
35  public bool IsIdentity { get; private set; }
36 
37  protected override void OnNodeInit() {
38  ColumnName = this.FindNode<IdentifierNode>();
39  DataType = this.FindNode<DataTypeNode>();
40  Default = this.FindNode<IExpressionNode>();
41  IsIdentity = this.HasOptionalNode("column_identity_opt");
42 
43  Constraints = this.FindNodes<ColumnConstraintNode>();
44  }
45 
46  public SqlTableColumn BuildColumn(ITypeResolver typeResolver, string tableName, IList<SqlTableConstraint> constraints) {
47  var dataType = DataTypeBuilder.Build(typeResolver, DataType);
48 
49  var columnInfo = new SqlTableColumn(ColumnName.Text, dataType);
50 
51  if (Default != null)
52  columnInfo.DefaultExpression = ExpressionBuilder.Build(Default);
53 
54  if (IsIdentity) {
55  columnInfo.DefaultExpression = SqlExpression.FunctionCall("UNIQUEKEY",
56  new[] { SqlExpression.Constant(tableName) });
57  columnInfo.IsIdentity = true;
58  }
59 
60  foreach (var constraint in Constraints) {
61  if (String.Equals(ConstraintTypeNames.Check, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase)) {
62  var exp = ExpressionBuilder.Build(constraint.CheckExpression);
63  constraints.Add(SqlTableConstraint.Check(null, exp));
64  } else if (String.Equals(ConstraintTypeNames.ForeignKey, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase)) {
65  var fTable = constraint.ReferencedTable.Name;
66  var fColumn = constraint.ReferencedColumn.Text;
67  var onDelete = ForeignKeyAction.NoAction;
68  var onUpdate = ForeignKeyAction.NoAction;
69 
70  if (!String.IsNullOrEmpty(constraint.OnDeleteAction))
71  onDelete = StatementBuilder.GetForeignKeyAction(constraint.OnDeleteAction);
72  if (!String.IsNullOrEmpty(constraint.OnUpdateAction))
73  onUpdate = StatementBuilder.GetForeignKeyAction(constraint.OnUpdateAction);
74 
75  constraints.Add(SqlTableConstraint.ForeignKey(null, new[]{ColumnName.Text}, fTable, new[]{fColumn}, onDelete, onUpdate));
76  } else if (String.Equals(ConstraintTypeNames.PrimaryKey, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase)) {
77  constraints.Add(SqlTableConstraint.PrimaryKey(null, new[]{ColumnName.Text}));
78  } else if (String.Equals(ConstraintTypeNames.UniqueKey, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase)) {
79  constraints.Add(SqlTableConstraint.UniqueKey(null, new[]{ColumnName.Text}));
80  } else if (String.Equals(ConstraintTypeNames.NotNull, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase)) {
81  columnInfo.IsNotNull = true;
82  } else if (String.Equals(ConstraintTypeNames.Null, constraint.ConstraintType, StringComparison.OrdinalIgnoreCase)) {
83  columnInfo.IsNotNull = false;
84  }
85  }
86 
87  return columnInfo;
88  }
89  }
90 }
This is a simple identifier within a SQL grammar.
override void OnNodeInit()
After the initialization of the node from the parser, this method is invoked to let the specific init...
A long string in the system.
static SqlExpression Build(IExpressionNode node)
static SqlType Build(ITypeResolver resolver, ISqlNode sqlNode)
static SqlTableConstraint PrimaryKey(string constraintName, string[] columns)
static ForeignKeyAction GetForeignKeyAction(string actionName)
No specific form of the parameter was given: this default to the system default parameter style confi...
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
static SqlTableConstraint UniqueKey(string constraintName, string[] columns)
SqlTableColumn BuildColumn(ITypeResolver typeResolver, string tableName, IList< SqlTableConstraint > constraints)
static SqlBinaryExpression Add(SqlExpression left, SqlExpression right)
static SqlTableConstraint Check(string constraintName, SqlExpression expression)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static SqlFunctionCallExpression FunctionCall(ObjectName functionName)
Describes the information of a data type as found in a SQL string.
Definition: DataTypeNode.cs:28
This interface acts like a marker that indicates if a ISqlNode represents a SQL expression.
static SqlTableConstraint ForeignKey(string constraintName, string[] columns, string refTable, string[] refcolumns, ForeignKeyAction onDelete, ForeignKeyAction onUpdate)
The default implementation of ISqlNode, that is a node in the text analysis parsing of SQL commands...
Definition: SqlNode.cs:32