18 using System.Collections.Generic;
26 namespace Deveel.Data.Sql.Statements {
30 if (tableName == null)
31 throw new ArgumentNullException(
"tableName");
33 throw new ArgumentNullException(
"action");
35 TableName = tableName;
49 var tableName = context.
Query.ResolveTableName(TableName);
62 foreach (
string column
in columns) {
63 if (columnName.Equals(column)) {
65 "Constraint violation (" + constraintName +
66 ") dropping column " + columnName +
" because of " +
67 "referential constraint in " + table);
74 var comparison = context.
Query.IgnoreIdentifiersCase() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
75 return col1.Equals(col2, comparison);
79 data.
SetValue(
"TableName", TableName);
91 var tableInfo = table.TableInfo;
92 var newTableInfo =
new TableInfo(tableInfo.TableName);
96 bool tableAltered =
false;
97 bool markDropped =
false;
99 for (
int n = 0; n < tableInfo.ColumnCount; ++n) {
100 var column = tableInfo[n];
102 string columnName = column.ColumnName;
103 var columnType = column.ColumnType;
104 var defaultExpression = column.DefaultExpression;
110 defaultExpression = exp;
114 defaultExpression = null;
119 var refs = context.
Request.
Query.GetTableImportedForeignKeys(TableName);
120 foreach (var reference
in refs) {
121 CheckColumnConstraint(columnName, reference.ForeignColumnNames, reference.ForeignTable, reference.ConstraintName);
125 refs = context.
Request.
Query.GetTableForeignKeys(TableName);
126 foreach (var reference
in refs) {
127 CheckColumnConstraint(columnName, reference.ColumnNames, reference.TableName, reference.ConstraintName);
131 var primaryKey = context.
Request.
Query.GetTablePrimaryKey(TableName);
132 if (primaryKey != null)
133 CheckColumnConstraint(columnName, primaryKey.ColumnNames, TableName, primaryKey.ConstraintName);
136 var uniques = context.
Request.
Query.GetTableUniqueKeys(TableName);
137 foreach (var unique
in uniques) {
138 CheckColumnConstraint(columnName, unique.ColumnNames, TableName, unique.ConstraintName);
145 var newColumn =
new ColumnInfo(columnName, columnType);
146 if (defaultExpression != null)
147 newColumn.DefaultExpression = defaultExpression;
149 newColumn.IndexType = column.IndexType;
150 newColumn.IsNotNull = column.IsNotNull;
154 newTableInfo.AddColumn(newColumn);
161 checker.CheckExpression(col.DefaultExpression);
162 var columnName = col.ColumnName;
163 var columnType = col.ColumnType;
166 columnName = checker.StripTableName(TableName.Name, columnName);
167 if (tableInfo.IndexOfColumn(col.ColumnName) != -1)
168 throw new InvalidOperationException(
"The column '" + col.ColumnName +
"' is already in the table '" + tableInfo.TableName +
"'.");
170 var newColumn =
new ColumnInfo(columnName, columnType) {
172 DefaultExpression = col.DefaultExpression
175 newTableInfo.AddColumn(newColumn);
181 int dropCount = context.
Request.
Query.DropConstraint(TableName, constraintName);
183 throw new InvalidOperationException(
"Named constraint to drop on table " + TableName +
" was not found: " + constraintName);
186 if (!context.
Request.
Query.DropPrimaryKey(TableName, constraintName))
187 throw new InvalidOperationException(
"No primary key to delete on table " + TableName);
192 bool foreignConstraint = (constraint.ConstraintType ==
ConstraintType.ForeignKey);
195 if (foreignConstraint) {
196 refTname = context.
Request.
Query.ResolveTableName(constraint.ReferenceTable);
199 var columnNames = checker.StripColumnList(TableName.FullName, constraint.Columns);
200 columnNames = checker.StripColumnList(constraint.ReferenceTable, columnNames);
201 var expression = checker.CheckExpression(constraint.CheckExpression);
202 columnNames = checker.CheckColumns(columnNames);
204 IEnumerable<string> refCols = null;
205 if (foreignConstraint && constraint.ReferenceColumns != null) {
207 refCols = referencedChecker.
CheckColumns(constraint.ReferenceColumns);
210 var newConstraint =
new ConstraintInfo(constraint.ConstraintType, TableName, columnNames.ToArray());
211 if (foreignConstraint) {
213 newConstraint.ForeignColumnNames = refCols.ToArray();
217 newConstraint.CheckExpression = expression;
219 context.
Request.
Query.AddConstraint(TableName, newConstraint);
224 if (newTableInfo.ColumnCount == 0)
225 throw new InvalidOperationException(
"Can not ALTER table to have 0 columns.");
236 #region PreparedSerializer
Defines the metadata properties of a column within a table of a database.
Enumerates a known set of codes in a SQL Model
A database exception that represents a constraint violation.
static void CheckColumnConstraint(string columnName, string[] columns, ObjectName table, string constraintName)
IEnumerable< string > CheckColumns(IEnumerable< string > columnNames)
bool IsNotNull
Gets or sets a boolean value indicating if the column values are constrained to be ony NOT NULL...
AlterTableStatement(ObjectData data)
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
override void ExecuteStatement(ExecutionContext context)
Represents the foundation class of SQL statements to be executed.
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
AlterTableStatement(ObjectName tableName, IAlterTableAction action)
An interface used to prepare a SqlExpression object.
override void GetData(SerializeData data)
const int DropColumnViolation
Column can't be dropped before of an reference to it.
AlterTableActionType IAlterTableAction. ActionType
static ColumnChecker Default(IRequest context, ObjectName tableName)
AlterTableActionType
The possible types of actions in a AlterTableAction expression.
object Prepare(IExpressionPreparer preparer)
Converts the underlying value of this instance into an object that can be evaluated by an expression...
SqlExpression CheckExpression(SqlExpression expression)
object GetValue(string key)
IStatement Prepare(IRequest request)
Defines the metadata properties of a table existing within a database.
AlterTableActionType IAlterTableAction. ActionType
bool CheckColumnNamesMatch(IRequest context, String col1, String col2)
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...