DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Public Member Functions | Protected Member Functions | Properties | Private Member Functions | Static Private Member Functions | List of all members
Deveel.Data.Sql.Statements.AlterTableStatement Class Reference
Inheritance diagram for Deveel.Data.Sql.Statements.AlterTableStatement:
Deveel.Data.Sql.Statements.SqlStatement Deveel.Data.Sql.Statements.IPreparableStatement Deveel.Data.Sql.Expressions.IPreparable Deveel.Data.Sql.Statements.IStatement Deveel.Data.Serialization.ISerializable Deveel.Data.Sql.Statements.IStatement Deveel.Data.Sql.IExecutable Deveel.Data.Sql.IExecutable

Public Member Functions

 AlterTableStatement (ObjectName tableName, IAlterTableAction action)
 
- Public Member Functions inherited from Deveel.Data.Sql.Statements.SqlStatement
ITable Execute (IRequest context)
 Prepares and evaluates this statement into a tabular result. More...
 

Protected Member Functions

override void GetData (SerializeData data)
 
override void ExecuteStatement (ExecutionContext context)
 
- Protected Member Functions inherited from Deveel.Data.Sql.Statements.SqlStatement
 SqlStatement ()
 
 SqlStatement (ObjectData data)
 

Properties

ObjectName TableName [get, private set]
 
IAlterTableAction Action [get, private set]
 
- Properties inherited from Deveel.Data.Sql.Statements.SqlStatement
SqlQuery SourceQuery [get, set]
 Gets the SqlQuery that is the origin of this statement. More...
 
bool IsFromQuery [get, private set]
 Gets a boolean value indicating if this object was formed from the parsing of a SqlQuery or if it was manually created. More...
 
- Properties inherited from Deveel.Data.Sql.Statements.IStatement
SqlQuery SourceQuery [get]
 

Private Member Functions

 AlterTableStatement (ObjectData data)
 
IStatement IPreparableStatement. Prepare (IRequest context)
 
object IPreparable. Prepare (IExpressionPreparer preparer)
 Converts the underlying value of this instance into an object that can be evaluated by an expression. More...
 
bool CheckColumnNamesMatch (IRequest context, String col1, String col2)
 

Static Private Member Functions

static void CheckColumnConstraint (string columnName, string[] columns, ObjectName table, string constraintName)
 

Additional Inherited Members

- Static Public Member Functions inherited from Deveel.Data.Sql.Statements.SqlStatement
static IEnumerable< SqlStatementParse (string sqlSource)
 Parses a given string into one of more statements. More...
 
static IEnumerable< SqlStatementParse (IContext context, string sqlSource)
 Parses a given string into one of more statements. More...
 
static IEnumerable< SqlStatementParse (IContext context, SqlQuery query)
 
- Package Functions inherited from Deveel.Data.Sql.Statements.SqlStatement
void SetSource (SqlQuery query)
 

Detailed Description

Definition at line 28 of file AlterTableStatement.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Statements.AlterTableStatement.AlterTableStatement ( ObjectName  tableName,
IAlterTableAction  action 
)
inline

Definition at line 29 of file AlterTableStatement.cs.

29  {
30  if (tableName == null)
31  throw new ArgumentNullException("tableName");
32  if (action == null)
33  throw new ArgumentNullException("action");
34 
35  TableName = tableName;
36  Action = action;
37  }
Deveel.Data.Sql.Statements.AlterTableStatement.AlterTableStatement ( ObjectData  data)
inlineprivate

Definition at line 39 of file AlterTableStatement.cs.

39  {
40  TableName = data.GetValue<ObjectName>("TableName");
41  Action = data.GetValue<IAlterTableAction>("Action");
42  }

Member Function Documentation

static void Deveel.Data.Sql.Statements.AlterTableStatement.CheckColumnConstraint ( string  columnName,
string[]  columns,
ObjectName  table,
string  constraintName 
)
inlinestaticprivate

Definition at line 61 of file AlterTableStatement.cs.

61  {
62  foreach (string column in columns) {
63  if (columnName.Equals(column)) {
64  throw new ConstraintViolationException(SqlModelErrorCodes.DropColumnViolation,
65  "Constraint violation (" + constraintName +
66  ") dropping column " + columnName + " because of " +
67  "referential constraint in " + table);
68  }
69  }
70 
71  }
A database exception that represents a constraint violation.
bool Deveel.Data.Sql.Statements.AlterTableStatement.CheckColumnNamesMatch ( IRequest  context,
String  col1,
String  col2 
)
inlineprivate

Definition at line 73 of file AlterTableStatement.cs.

73  {
74  var comparison = context.Query.IgnoreIdentifiersCase() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
75  return col1.Equals(col2, comparison);
76  }
override void Deveel.Data.Sql.Statements.AlterTableStatement.ExecuteStatement ( ExecutionContext  context)
inlineprotectedvirtual

Reimplemented from Deveel.Data.Sql.Statements.SqlStatement.

Definition at line 83 of file AlterTableStatement.cs.

83  {
84  if (!context.Request.Query.UserCanAlterTable(TableName))
85  throw new InvalidAccessException(context.Request.Query.UserName(), TableName);
86 
87  var table = context.Request.Query.GetTable(TableName);
88  if (table == null)
89  throw new ObjectNotFoundException(TableName);
90 
91  var tableInfo = table.TableInfo;
92  var newTableInfo = new TableInfo(tableInfo.TableName);
93 
94  var checker = ColumnChecker.Default(context.Request, TableName);
95 
96  bool tableAltered = false;
97  bool markDropped = false;
98 
99  for (int n = 0; n < tableInfo.ColumnCount; ++n) {
100  var column = tableInfo[n];
101 
102  string columnName = column.ColumnName;
103  var columnType = column.ColumnType;
104  var defaultExpression = column.DefaultExpression;
105 
106  if (Action.ActionType == AlterTableActionType.SetDefault &&
107  CheckColumnNamesMatch(context.Request, ((SetDefaultAction)Action).ColumnName, columnName)) {
108  var exp = ((SetDefaultAction)Action).DefaultExpression;
109  exp = checker.CheckExpression(exp);
110  defaultExpression = exp;
111  tableAltered = true;
112  } else if (Action.ActionType == AlterTableActionType.DropDefault &&
113  CheckColumnNamesMatch(context.Request, ((DropDefaultAction)Action).ColumnName, columnName)) {
114  defaultExpression = null;
115  tableAltered = true;
116  } else if (Action.ActionType == AlterTableActionType.DropColumn &&
117  CheckColumnNamesMatch(context.Request, ((DropColumnAction)Action).ColumnName, columnName)) {
118  // Check there are no referential links to this column
119  var refs = context.Request.Query.GetTableImportedForeignKeys(TableName);
120  foreach (var reference in refs) {
121  CheckColumnConstraint(columnName, reference.ForeignColumnNames, reference.ForeignTable, reference.ConstraintName);
122  }
123 
124  // Or from it
125  refs = context.Request.Query.GetTableForeignKeys(TableName);
126  foreach (var reference in refs) {
127  CheckColumnConstraint(columnName, reference.ColumnNames, reference.TableName, reference.ConstraintName);
128  }
129 
130  // Or that it's part of a primary key
131  var primaryKey = context.Request.Query.GetTablePrimaryKey(TableName);
132  if (primaryKey != null)
133  CheckColumnConstraint(columnName, primaryKey.ColumnNames, TableName, primaryKey.ConstraintName);
134 
135  // Or that it's part of a unique set
136  var uniques = context.Request.Query.GetTableUniqueKeys(TableName);
137  foreach (var unique in uniques) {
138  CheckColumnConstraint(columnName, unique.ColumnNames, TableName, unique.ConstraintName);
139  }
140 
141  markDropped = true;
142  tableAltered = true;
143  }
144 
145  var newColumn = new ColumnInfo(columnName, columnType);
146  if (defaultExpression != null)
147  newColumn.DefaultExpression = defaultExpression;
148 
149  newColumn.IndexType = column.IndexType;
150  newColumn.IsNotNull = column.IsNotNull;
151 
152  // If not dropped then add to the new table definition.
153  if (!markDropped) {
154  newTableInfo.AddColumn(newColumn);
155  }
156  }
157 
158  if (Action.ActionType == AlterTableActionType.AddColumn) {
159  var col = ((AddColumnAction)Action).Column;
160 
161  checker.CheckExpression(col.DefaultExpression);
162  var columnName = col.ColumnName;
163  var columnType = col.ColumnType;
164 
165  // If column name starts with [table_name]. then strip it off
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 + "'.");
169 
170  var newColumn = new ColumnInfo(columnName, columnType) {
171  IsNotNull = col.IsNotNull,
172  DefaultExpression = col.DefaultExpression
173  };
174 
175  newTableInfo.AddColumn(newColumn);
176  tableAltered = true;
177  }
178 
179  if (Action.ActionType == AlterTableActionType.DropConstraint) {
180  string constraintName = ((DropConstraintAction)Action).ConstraintName;
181  int dropCount = context.Request.Query.DropConstraint(TableName, constraintName);
182  if (dropCount == 0)
183  throw new InvalidOperationException("Named constraint to drop on table " + TableName + " was not found: " + constraintName);
184  } else if (Action.ActionType == AlterTableActionType.DropPrimaryKey) {
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);
188  }
189 
190  if (Action.ActionType == AlterTableActionType.AddConstraint) {
191  var constraint = ((AddConstraintAction)Action).Constraint;
192  bool foreignConstraint = (constraint.ConstraintType == ConstraintType.ForeignKey);
193 
194  ObjectName refTname = null;
195  if (foreignConstraint) {
196  refTname = context.Request.Query.ResolveTableName(constraint.ReferenceTable);
197  }
198 
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);
203 
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);
208  }
209 
210  var newConstraint = new ConstraintInfo(constraint.ConstraintType, TableName, columnNames.ToArray());
211  if (foreignConstraint) {
212  newConstraint.ForeignTable = refTname;
213  newConstraint.ForeignColumnNames = refCols.ToArray();
214  }
215 
216  if (constraint.ConstraintType == ConstraintType.Check)
217  newConstraint.CheckExpression = expression;
218 
219  context.Request.Query.AddConstraint(TableName, newConstraint);
220  }
221 
222  // Alter the existing table to the new format...
223  if (tableAltered) {
224  if (newTableInfo.ColumnCount == 0)
225  throw new InvalidOperationException("Can not ALTER table to have 0 columns.");
226 
227  context.Request.Query.AlterTable(newTableInfo);
228  } else {
229  // If the table wasn't physically altered, check the constraints.
230  // Calling this method will also make the transaction check all
231  // deferred constraints during the next commit.
232  context.Request.Query.CheckConstraints(TableName);
233  }
234  }
Defines the metadata properties of a column within a table of a database.
Definition: ColumnInfo.cs:36
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...
Definition: ColumnInfo.cs:144
ConstraintType
An enumeration of all the supported kinds of constraints within a table or a schema.
string FullName
Gets the full reference name formatted.
Definition: ObjectName.cs:114
AlterTableActionType
The possible types of actions in a AlterTableAction expression.
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
bool CheckColumnNamesMatch(IRequest context, String col1, String col2)
override void Deveel.Data.Sql.Statements.AlterTableStatement.GetData ( SerializeData  data)
inlineprotectedvirtual

Reimplemented from Deveel.Data.Sql.Statements.SqlStatement.

Definition at line 78 of file AlterTableStatement.cs.

78  {
79  data.SetValue("TableName", TableName);
80  data.SetValue("Action", Action);
81  }
void SetValue(string key, Type type, object value)
IStatement IPreparableStatement. Deveel.Data.Sql.Statements.AlterTableStatement.Prepare ( IRequest  context)
inlineprivate

Implements Deveel.Data.Sql.Statements.IPreparableStatement.

Definition at line 48 of file AlterTableStatement.cs.

48  {
49  var tableName = context.Query.ResolveTableName(TableName);
50  return new AlterTableStatement(tableName, Action);
51  }
AlterTableStatement(ObjectName tableName, IAlterTableAction action)
object IPreparable. Deveel.Data.Sql.Statements.AlterTableStatement.Prepare ( IExpressionPreparer  preparer)
inlineprivate

Converts the underlying value of this instance into an object that can be evaluated by an expression.

Parameters
preparerThe context used to prepare this object.
Returns
Returns an object that can be evaluated by an expression.

Implements Deveel.Data.Sql.Expressions.IPreparable.

Definition at line 53 of file AlterTableStatement.cs.

53  {
54  var action = Action;
55  if (action is IPreparable)
56  action = (IAlterTableAction)(action as IPreparable).Prepare(preparer);
57 
58  return new AlterTableStatement(TableName, action);
59  }
AlterTableStatement(ObjectName tableName, IAlterTableAction action)
A contract for objects that participate to a SqlExpression.Prepare phase of an expression evaluation...
Definition: IPreparable.cs:30

Property Documentation

IAlterTableAction Deveel.Data.Sql.Statements.AlterTableStatement.Action
getprivate set

Definition at line 46 of file AlterTableStatement.cs.

ObjectName Deveel.Data.Sql.Statements.AlterTableStatement.TableName
getprivate set

Definition at line 44 of file AlterTableStatement.cs.


The documentation for this class was generated from the following file: