DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
UpdateStatement.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.Collections.Generic;
19 
22 using Deveel.Data.Sql.Query;
23 using Deveel.Data.Sql.Tables;
24 
25 namespace Deveel.Data.Sql.Statements {
27  public UpdateStatement(string tableName, SqlExpression wherExpression, IEnumerable<SqlColumnAssignment> assignments) {
28  if (wherExpression == null)
29  throw new ArgumentNullException("wherExpression");
30  if (assignments == null)
31  throw new ArgumentNullException("assignments");
32  if (String.IsNullOrEmpty(tableName))
33  throw new ArgumentNullException("tableName");
34 
35  TableName = tableName;
36  WherExpression = wherExpression;
37  Assignments = assignments;
38  }
39 
40  public string TableName { get; private set; }
41 
42  public SqlExpression WherExpression { get; private set; }
43 
44  public int Limit { get; set; }
45 
46  public IEnumerable<SqlColumnAssignment> Assignments { get; private set; }
47 
49  var tableName = context.Query.ResolveTableName(TableName);
50  if (!context.Query.TableExists(tableName))
51  throw new ObjectNotFoundException(tableName);
52 
53  var queryExpression = new SqlQueryExpression(new[]{SelectColumn.Glob("*") });
54  queryExpression.FromClause.AddTable(tableName.FullName);
55  queryExpression.WhereExpression = WherExpression;
56 
57  var queryFrom = QueryExpressionFrom.Create(context, queryExpression);
58  var queryPlan = context.Query.Context.QueryPlanner().PlanQuery(new QueryInfo(context, queryExpression));
59 
60  var columns = new List<SqlAssignExpression>();
61  foreach (var assignment in Assignments) {
62  var columnName = ObjectName.Parse(assignment.ColumnName);
63 
64  var refName = queryFrom.ResolveReference(columnName);
65  var expression = assignment.Expression.Prepare(queryFrom.ExpressionPreparer);
66 
67  var assign = SqlExpression.Assign(SqlExpression.Reference(refName), expression);
68  columns.Add(assign);
69  }
70 
71  return new Prepared(tableName, queryPlan, columns.ToArray(), Limit);
72  }
73 
74  #region Prepared
75 
76  [Serializable]
77  sealed class Prepared : SqlStatement {
78  internal Prepared(ObjectName tableName, IQueryPlanNode queryPlan, SqlAssignExpression[] columns, int limit) {
79  TableName = tableName;
80  QueryPlan = queryPlan;
81  Columns = columns;
82  Limit = limit;
83  }
84 
85  private Prepared(ObjectData data) {
86  TableName = data.GetValue<ObjectName>("TableName");
87  QueryPlan = data.GetValue<IQueryPlanNode>("QueryPlan");
88  Columns = data.GetValue<SqlAssignExpression[]>("Columns");
89  Limit = data.GetInt32("Limit");
90  }
91 
92  public ObjectName TableName { get; private set; }
93 
94  public IQueryPlanNode QueryPlan { get; private set; }
95 
96  public SqlAssignExpression[] Columns { get; private set; }
97 
98  public int Limit { get; private set; }
99 
100  protected override void ExecuteStatement(ExecutionContext context) {
101  var updateCount = context.Request.Query.UpdateTable(TableName, QueryPlan, Columns, Limit);
102  context.SetResult(updateCount);
103  }
104 
105  protected override void GetData(SerializeData data) {
106  data.SetValue("TableName", TableName);
107  data.SetValue("QueryPlan", QueryPlan);
108  data.SetValue("Columns", Columns);
109  data.SetValue("Limit", Limit);
110  }
111  }
112 
113  #endregion
114  }
115 }
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
void AddTable(string alias, FromTable table)
Adds a table as source to the query with a given alias.
Definition: FromClause.cs:93
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static SqlAssignExpression Assign(SqlExpression reference, SqlExpression valueExpression)
override void ExecuteStatement(ExecutionContext context)
A node element of a query plan tree. /summary>
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
static QueryExpressionFrom Create(IRequest context, SqlQueryExpression expression)
static SqlBinaryExpression Add(SqlExpression left, SqlExpression right)
Represents a column selected to be in the output of a select statement.
Definition: SelectColumn.cs:31
UpdateStatement(string tableName, SqlExpression wherExpression, IEnumerable< SqlColumnAssignment > assignments)
static SqlReferenceExpression Reference(ObjectName objectName)
Prepared(ObjectName tableName, IQueryPlanNode queryPlan, SqlAssignExpression[] columns, int limit)
Defines the base class for instances that represent SQL expression tree nodes.
static SelectColumn Glob(string glob)
Creates a special SelectColumn that is used to select all the columns in a table. ...
new IQueryContext Context
Definition: IQuery.cs:21