DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryExtensions.Commands.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 using System.Linq;
20 
21 using Deveel.Data.Security;
23 using Deveel.Data.Sql.Objects;
24 using Deveel.Data.Sql.Query;
25 using Deveel.Data.Types;
26 
27 namespace Deveel.Data.Sql.Tables {
28  public static partial class QueryExtensions {
29  public static int DeleteFrom(this IQuery context, ObjectName tableName, SqlQueryExpression query) {
30  return DeleteFrom(context, tableName, query, -1);
31  }
32 
33  public static int DeleteFrom(this IQuery context, ObjectName tableName, SqlExpression expression) {
34  return DeleteFrom(context, tableName, expression, -1);
35  }
36 
37  public static int DeleteFrom(this IQuery context, ObjectName tableName, SqlExpression expression, int limit) {
38  if (expression is SqlQueryExpression)
39  return context.DeleteFrom(tableName, (SqlQueryExpression)expression, limit);
40 
41  var table = context.GetMutableTable(tableName);
42  if (table == null)
43  throw new ObjectNotFoundException(tableName);
44 
45  var queryExpression = new SqlQueryExpression(new List<SelectColumn> { SelectColumn.Glob("*") });
46  queryExpression.FromClause.AddTable(tableName.Name);
47  queryExpression.WhereExpression = expression;
48 
49  var planExpression = queryExpression.Evaluate(context, null);
50  var plan = (SqlQueryObject)((SqlConstantExpression)planExpression).Value.Value;
51  var deleteSet = plan.QueryPlan.Evaluate(context);
52 
53  return context.DeleteFrom(tableName, deleteSet, limit);
54  }
55 
56  public static int DeleteFrom(this IQuery context, ObjectName tableName, SqlQueryExpression query, int limit) {
57  IQueryPlanNode plan;
58 
59  try {
60  var planValue = query.EvaluateToConstant(context, null);
61  if (planValue == null)
62  throw new InvalidOperationException();
63 
64  if (!(planValue.Type is QueryType))
65  throw new InvalidOperationException();
66 
67  plan = ((SqlQueryObject)planValue.Value).QueryPlan;
68  } catch (QueryException) {
69  throw;
70  } catch (SecurityException) {
71  throw;
72  } catch (Exception ex) {
73  throw new InvalidOperationException(String.Format("Could not delete from table '{0}': unable to form the delete set.", tableName), ex);
74  }
75 
76  var deleteSet = plan.Evaluate(context);
77  return context.DeleteFrom(tableName, deleteSet, limit);
78  }
79 
80  public static int DeleteFrom(this IQuery context, ObjectName tableName, ITable deleteSet, int limit) {
81  if (!context.UserCanDeleteFromTable(tableName))
82  throw new MissingPrivilegesException(context.UserName(), tableName, Privileges.Delete);
83 
84  var table = context.GetMutableTable(tableName);
85  if (table == null)
86  throw new ObjectNotFoundException(tableName);
87 
88  return table.Delete(deleteSet, limit);
89  }
90 
91  public static int UpdateTable(this IQuery context, ObjectName tableName, IQueryPlanNode queryPlan,
92  IEnumerable<SqlAssignExpression> assignments, int limit) {
93  var columnNames = assignments.Select(x => x.ReferenceExpression)
94  .Cast<SqlReferenceExpression>()
95  .Select(x => x.ReferenceName.Name).ToArray();
96 
97  if (!context.UserCanUpdateTable(tableName, columnNames))
98  throw new MissingPrivilegesException(context.UserName(), tableName, Privileges.Update);
99 
100  if (!context.UserCanSelectFromPlan(queryPlan))
101  throw new InvalidOperationException();
102 
103  var table = context.GetMutableTable(tableName);
104  if (table == null)
105  throw new ObjectNotFoundException(tableName);
106 
107  var updateSet = queryPlan.Evaluate(context);
108  return table.Update(context, updateSet, assignments, limit);
109  }
110 
111  public static void InsertIntoTable(this IQuery context, ObjectName tableName, IEnumerable<SqlAssignExpression> assignments) {
112  var columnNames =
113  assignments.Select(x => x.ReferenceExpression)
114  .Cast<SqlReferenceExpression>()
115  .Select(x => x.ReferenceName.Name).ToArray();
116  if (!context.UserCanInsertIntoTable(tableName, columnNames))
117  throw new MissingPrivilegesException(context.UserName(), tableName, Privileges.Insert);
118 
119  var table = context.GetMutableTable(tableName);
120  if (table == null)
121  throw new ObjectNotFoundException(tableName);
122 
123  var row = table.NewRow();
124  foreach (var expression in assignments) {
125  row.EvaluateAssignment(expression, context);
126  }
127 
128  table.AddRow(row);
129  }
130 
131  public static int InsertIntoTable(this IQuery context, ObjectName tableName, IEnumerable<SqlAssignExpression[]> assignments) {
132  int insertCount = 0;
133 
134  foreach (var assignment in assignments) {
135  context.InsertIntoTable(tableName, assignment);
136  insertCount++;
137  }
138 
139  return insertCount;
140  }
141 
142  }
143 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
An expression that references an object within a context.
A long string in the system.
ITable Evaluate(IRequest context)
static void InsertIntoTable(this IQuery context, ObjectName tableName, IEnumerable< SqlAssignExpression > assignments)
static int DeleteFrom(this IQuery context, ObjectName tableName, SqlExpression expression)
static int InsertIntoTable(this IQuery context, ObjectName tableName, IEnumerable< SqlAssignExpression[]> assignments)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static int DeleteFrom(this IQuery context, ObjectName tableName, SqlQueryExpression query)
static int DeleteFrom(this IQuery context, ObjectName tableName, SqlQueryExpression query, int limit)
A node element of a query plan tree. /summary>
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
static int DeleteFrom(this IQuery context, ObjectName tableName, SqlExpression expression, int limit)
Represents a column selected to be in the output of a select statement.
Definition: SelectColumn.cs:31
static int UpdateTable(this IQuery context, ObjectName tableName, IQueryPlanNode queryPlan, IEnumerable< SqlAssignExpression > assignments, int limit)
An expression that holds a constant value.
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
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. ...
static int DeleteFrom(this IQuery context, ObjectName tableName, ITable deleteSet, int limit)