DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ConstraintInfo.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 
20 
21 namespace Deveel.Data.Sql.Tables {
22  public sealed class ConstraintInfo {
23  public ConstraintInfo(ConstraintType constraintType, ObjectName tableName, string[] columnNames)
24  : this(null, constraintType, tableName, columnNames) {
25  }
26 
27  public ConstraintInfo(string constraintName, ConstraintType constraintType, ObjectName tableName, string[] columnNames) {
28  if (tableName == null)
29  throw new ArgumentNullException("tableName");
30  if (columnNames == null)
31  throw new ArgumentNullException("columnNames");
32 
33  if (columnNames.Length == 0)
34  throw new ArgumentException("The provided column names for the constraint is empty.", "columnNames");
35 
36  ConstraintName = constraintName;
37  ColumnNames = columnNames;
38  TableName = tableName;
39  ConstraintType = constraintType;
40  }
41 
42  public ConstraintType ConstraintType { get; private set; }
43 
44  public ObjectName TableName { get; private set; }
45 
46  public string ConstraintName { get; set; }
47 
48  public string[] ColumnNames { get; private set; }
49 
50  public SqlExpression CheckExpression { get; set; }
51 
52  public ObjectName ForeignTable { get; set; }
53 
54  public string[] ForeignColumnNames { get; set; }
55 
56  public ForeignKeyAction OnDelete { get; set; }
57 
58  public ForeignKeyAction OnUpdate { get; set; }
59 
60  public ConstraintDeferrability Deferred { get; set; }
61 
62  public static ConstraintInfo Unique(ObjectName tableName, params string[] columnNames) {
63  return Unique(null, tableName, columnNames);
64  }
65 
66  public static ConstraintInfo Unique(string constraintName, ObjectName tableName, string[] columnNames) {
67  return new ConstraintInfo(constraintName, ConstraintType.Unique, tableName, columnNames);
68  }
69 
70  public static ConstraintInfo Check(ObjectName tableName, SqlExpression expression, params string[] columnNames) {
71  return Check(null, tableName, expression, columnNames);
72  }
73 
74  public static ConstraintInfo Check(string constraintName, ObjectName tableName, SqlExpression expression, params string[] columnNames) {
75  return new ConstraintInfo(constraintName, ConstraintType.Check, tableName, columnNames) {
76  CheckExpression = expression
77  };
78  }
79 
80  public static ConstraintInfo PrimaryKey(ObjectName tableName, params string[] columnNames) {
81  return PrimaryKey(null, tableName, columnNames);
82  }
83 
84  public static ConstraintInfo PrimaryKey(string constraintName, ObjectName tableName, params string[] columnNames) {
85  return new ConstraintInfo(constraintName, ConstraintType.PrimaryKey, tableName, columnNames);
86  }
87 
88  public static ConstraintInfo ForeignKey(ObjectName tableName, string columnName, ObjectName refTable, string refColumn) {
89  return ForeignKey(tableName, new[] {columnName}, refTable, new[] {refColumn});
90  }
91 
92  public static ConstraintInfo ForeignKey(ObjectName tableName, string[] columnNames, ObjectName refTable, string[] refColumns) {
93  return ForeignKey(null, tableName, columnNames, refTable, refColumns);
94  }
95 
96  public static ConstraintInfo ForeignKey(string constraintName, ObjectName tableName, string[] columnNames,
97  ObjectName refTable, string[] refColumns) {
98  if (tableName == null)
99  throw new ArgumentNullException("tableName");
100  if (refTable == null)
101  throw new ArgumentNullException("refTable");
102  if (columnNames == null || columnNames.Length == 0)
103  throw new ArgumentException("At least one column is required", "columnNames");
104  if (refColumns == null || refColumns.Length == 0)
105  throw new ArgumentException("At least one referenced column is required.", "refColumns");
106 
107  if (columnNames.Length != refColumns.Length)
108  throw new ArgumentException("The number of columns in the constraint must match the number of columns referenced.");
109 
110  var constraint = new ConstraintInfo(constraintName, ConstraintType.ForeignKey, tableName, columnNames);
111  constraint.ForeignTable = refTable;
112  constraint.ForeignColumnNames = refColumns;
113  return constraint;
114  }
115  }
116 }
static ConstraintInfo PrimaryKey(ObjectName tableName, params string[] columnNames)
static ConstraintInfo Unique(string constraintName, ObjectName tableName, string[] columnNames)
ConstraintInfo(ConstraintType constraintType, ObjectName tableName, string[] columnNames)
static ConstraintInfo Check(ObjectName tableName, SqlExpression expression, params string[] columnNames)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
ConstraintDeferrability
The type of deferrance of a constraint.
static ConstraintInfo ForeignKey(ObjectName tableName, string[] columnNames, ObjectName refTable, string[] refColumns)
ConstraintInfo(string constraintName, ConstraintType constraintType, ObjectName tableName, string[] columnNames)
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
static ConstraintInfo Check(string constraintName, ObjectName tableName, SqlExpression expression, params string[] columnNames)
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
static ConstraintInfo ForeignKey(string constraintName, ObjectName tableName, string[] columnNames, ObjectName refTable, string[] refColumns)
static ConstraintInfo PrimaryKey(string constraintName, ObjectName tableName, params string[] columnNames)
static ConstraintInfo ForeignKey(ObjectName tableName, string columnName, ObjectName refTable, string refColumn)
Defines the base class for instances that represent SQL expression tree nodes.
static ConstraintInfo Unique(ObjectName tableName, params string[] columnNames)