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