84 if (!context.Request.Query.UserCanAlterTable(
TableName))
87 var table = context.Request.Query.GetTable(
TableName);
89 throw new ObjectNotFoundException(
TableName);
91 var tableInfo = table.TableInfo;
92 var newTableInfo =
new TableInfo(tableInfo.TableName);
94 var checker = ColumnChecker.Default(context.Request,
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;
108 var exp = ((SetDefaultAction)Action).DefaultExpression;
109 exp = checker.CheckExpression(exp);
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)
136 var uniques = context.Request.Query.GetTableUniqueKeys(
TableName);
137 foreach (var unique
in uniques) {
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);
159 var col = ((AddColumnAction)Action).Column;
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);
180 string constraintName = ((DropConstraintAction)Action).ConstraintName;
181 int dropCount = context.Request.Query.DropConstraint(
TableName, constraintName);
183 throw new InvalidOperationException(
"Named constraint to drop on table " +
TableName +
" was not found: " + constraintName);
185 string constraintName = ((DropConstraintAction)Action).ConstraintName;
186 if (!context.Request.Query.DropPrimaryKey(
TableName, constraintName))
187 throw new InvalidOperationException(
"No primary key to delete on table " +
TableName);
191 var constraint = ((AddConstraintAction)Action).Constraint;
192 bool foreignConstraint = (constraint.ConstraintType ==
ConstraintType.ForeignKey);
194 ObjectName refTname = null;
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) {
206 var referencedChecker = ColumnChecker.Default(context.Request, refTname);
207 refCols = referencedChecker.CheckColumns(constraint.ReferenceColumns);
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.");
227 context.Request.Query.AlterTable(newTableInfo);
232 context.Request.Query.CheckConstraints(
TableName);
Defines the metadata properties of a column within a table of a database.
static void CheckColumnConstraint(string columnName, string[] columns, ObjectName table, string constraintName)
bool IsNotNull
Gets or sets a boolean value indicating if the column values are constrained to be ony NOT NULL...
AlterTableActionType ActionType
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
string FullName
Gets the full reference name formatted.
AlterTableActionType
The possible types of actions in a AlterTableAction expression.
string Name
Gets the name of the object being referenced.
Defines the metadata properties of a table existing within a database.
bool CheckColumnNamesMatch(IRequest context, String col1, String col2)