DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlTableConstraint.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.IO;
19 
22 using Deveel.Data.Sql.Tables;
23 
24 namespace Deveel.Data.Sql.Statements {
25  [Serializable]
26  public sealed class SqlTableConstraint : IPreparable, ISerializable {
27  public SqlTableConstraint(ConstraintType constraintType, string[] columns)
28  : this(null, constraintType, columns) {
29  }
30 
31  public SqlTableConstraint(string constraintName, ConstraintType constraintType, string[] columns) {
32  ConstraintName = constraintName;
33  ConstraintType = constraintType;
34  Columns = columns;
35  }
36 
38  ConstraintName = data.GetString("Name");
39  ConstraintType = (ConstraintType) data.GetInt32("Type");
40  Columns = data.GetValue<string[]>("Columns");
41  CheckExpression = data.GetValue<SqlExpression>("Check");
42  ReferenceTable = data.GetString("ReferenceTable");
43  ReferenceColumns = data.GetValue<string[]>("ReferenceColumns");
44  OnDelete = (ForeignKeyAction) data.GetInt32("OnDelete");
45  OnUpdate = (ForeignKeyAction) data.GetInt32("OnUpdate");
46  }
47 
48  public string ConstraintName { get; private set; }
49 
50  public ConstraintType ConstraintType { get; private set; }
51 
52  public string[] Columns { get; private set; }
53 
54  public SqlExpression CheckExpression { get; set; }
55 
56  public string ReferenceTable { get; set; }
57 
58  public string[] ReferenceColumns { get; set; }
59 
60  public ForeignKeyAction OnDelete { get; set; }
61 
62  public ForeignKeyAction OnUpdate { get; set; }
63 
65  var checkExpression = CheckExpression;
66  if (checkExpression != null)
67  checkExpression = checkExpression.Prepare(preparer);
68 
69  return new SqlTableConstraint(ConstraintName, ConstraintType, Columns) {
70  CheckExpression = checkExpression,
71  ReferenceTable = ReferenceTable,
72  ReferenceColumns = ReferenceColumns
73  };
74  }
75 
76  public static SqlTableConstraint UniqueKey(string constraintName, string[] columns) {
77  return new SqlTableConstraint(constraintName, ConstraintType.Unique, columns);
78  }
79 
80  public static SqlTableConstraint PrimaryKey(string constraintName, string[] columns) {
81  return new SqlTableConstraint(constraintName, ConstraintType.PrimaryKey, columns);
82  }
83 
84  public static SqlTableConstraint Check(string constraintName, SqlExpression expression) {
85  return new SqlTableConstraint(constraintName, ConstraintType.Check, null) {
86  CheckExpression = expression
87  };
88  }
89 
90  public static SqlTableConstraint ForeignKey(string constraintName, string[] columns, string refTable,
91  string[] refcolumns, ForeignKeyAction onDelete, ForeignKeyAction onUpdate) {
92  return new SqlTableConstraint(constraintName, ConstraintType.ForeignKey, columns) {
93  ReferenceTable = refTable,
94  ReferenceColumns = refcolumns,
95  OnDelete = onDelete,
96  OnUpdate = onUpdate
97  };
98  }
99 
100  public static void Serialize(SqlTableConstraint constraint, BinaryWriter writer) {
101  throw new NotImplementedException();
102  }
103 
104  public static SqlTableConstraint Deserialize(BinaryReader reader) {
105  throw new NotImplementedException();
106  }
107 
109  data.SetValue("Name", ConstraintName);
110  data.SetValue("Type", (int)ConstraintType);
111  data.SetValue("Columns", Columns);
112  data.SetValue("ReferenceTable", ReferenceTable);
113  data.SetValue("ReferenceColumns", ReferenceColumns);
114  data.SetValue("Check", CheckExpression);
115  data.SetValue("OnDelete", (int)OnDelete);
116  data.SetValue("OnUpdate", (int)OnUpdate);
117  }
118  }
119 }
void GetData(SerializeData data)
SqlTableConstraint(string constraintName, ConstraintType constraintType, string[] columns)
void SetValue(string key, Type type, object value)
static SqlTableConstraint Deserialize(BinaryReader reader)
static SqlTableConstraint PrimaryKey(string constraintName, string[] columns)
static void Serialize(SqlTableConstraint constraint, BinaryWriter writer)
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
static SqlTableConstraint UniqueKey(string constraintName, string[] columns)
An interface used to prepare a SqlExpression object.
SqlTableConstraint(ConstraintType constraintType, string[] columns)
object Prepare(IExpressionPreparer preparer)
Converts the underlying value of this instance into an object that can be evaluated by an expression...
static SqlTableConstraint Check(string constraintName, SqlExpression expression)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlTableConstraint ForeignKey(string constraintName, string[] columns, string refTable, string[] refcolumns, ForeignKeyAction onDelete, ForeignKeyAction onUpdate)
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...
Definition: IPreparable.cs:30