DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryExtensions.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;
22 using Deveel.Data.Sql.Query;
24 
25 namespace Deveel.Data.Sql.Views {
26  public static class QueryExtensions {
27  public static bool ViewExists(this IQuery context, ObjectName viewName) {
28  return context.ObjectExists(DbObjectType.View, viewName);
29  }
30 
31  public static void DefineView(this IQuery context, ViewInfo viewInfo, bool replaceIfExists) {
32  var tablesInPlan = viewInfo.QueryPlan.DiscoverTableNames();
33  foreach (var tableName in tablesInPlan) {
34  if (!context.UserCanSelectFromTable(tableName))
35  throw new InvalidAccessException(context.UserName(), tableName);
36  }
37 
38  if (context.ViewExists(viewInfo.ViewName)) {
39  if (!replaceIfExists)
40  throw new InvalidOperationException(
41  String.Format("The view {0} already exists and the REPLCE clause was not specified.", viewInfo.ViewName));
42 
43  context.DropObject(DbObjectType.View, viewInfo.ViewName);
44  }
45 
46  context.CreateObject(viewInfo);
47 
48  // The initial grants for a view is to give the user who created it
49  // full access.
50  using (var systemContext = context.Direct()) {
51  systemContext.GrantToUserOnTable(viewInfo.ViewName, context.UserName(), Privileges.TableAll);
52  }
53  }
54 
55  public static void DefineView(this IQuery context, ObjectName viewName, IQueryPlanNode queryPlan, bool replaceIfExists) {
56  // We have to execute the plan to get the TableInfo that represents the
57  // result of the view execution.
58  var table = queryPlan.Evaluate(context);
59  var tableInfo = table.TableInfo.Alias(viewName);
60 
61  var viewInfo = new ViewInfo(tableInfo, null, queryPlan);
62  context.DefineView(viewInfo, replaceIfExists);
63  }
64 
65  public static void DropView(this IQuery context, ObjectName viewName) {
66  DropView(context, viewName, false);
67  }
68 
69  public static void DropView(this IQuery context, ObjectName viewName, bool ifExists) {
70  context.DropViews(new[] { viewName }, ifExists);
71  }
72 
73  public static void DropViews(this IQuery context, IEnumerable<ObjectName> viewNames) {
74  DropViews(context, viewNames, false);
75  }
76 
77  public static void DropViews(this IQuery context, IEnumerable<ObjectName> viewNames, bool onlyIfExists) {
78  var viewNameList = viewNames.ToList();
79  foreach (var tableName in viewNameList) {
80  if (!context.UserCanDropObject(DbObjectType.View, tableName))
81  throw new MissingPrivilegesException(context.UserName(), tableName, Privileges.Drop);
82  }
83 
84  // If the 'only if exists' flag is false, we need to check tables to drop
85  // exist first.
86  if (!onlyIfExists) {
87  // For each table to drop.
88  foreach (var viewName in viewNameList) {
89  // If view doesn't exist, throw an error
90  if (!context.ViewExists(viewName)) {
91  throw new ObjectNotFoundException(viewName, String.Format("The view '{0}' does not exist and cannot be dropped.", viewName));
92  }
93  }
94  }
95 
96  foreach (var viewName in viewNameList) {
97  // Does the table already exist?
98  if (context.ViewExists(viewName)) {
99  // Drop table in the transaction
100  context.DropObject(DbObjectType.Table, viewName);
101 
102  // Revoke all the grants on the table
103  context.RevokeAllGrantsOnView(viewName);
104  }
105  }
106  }
107 
108  public static View GetView(this IQuery context, ObjectName viewName) {
109  return context.GetObject(DbObjectType.View, viewName, AccessType.Read) as View;
110  }
111 
112  public static IQueryPlanNode GetViewQueryPlan(this IQuery context, ObjectName viewName) {
113  var view = context.GetView(viewName);
114  return view == null ? null : view.QueryPlan;
115  }
116  }
117 }
ITable Evaluate(IRequest context)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static void DropView(this IQuery context, ObjectName viewName, bool ifExists)
static void DefineView(this IQuery context, ViewInfo viewInfo, bool replaceIfExists)
A node element of a query plan tree. /summary>
static void DropViews(this IQuery context, IEnumerable< ObjectName > viewNames)
static void DropView(this IQuery context, ObjectName viewName)
static void DefineView(this IQuery context, ObjectName viewName, IQueryPlanNode queryPlan, bool replaceIfExists)
static View GetView(this IQuery context, ObjectName viewName)
static IQueryPlanNode GetViewQueryPlan(this IQuery context, ObjectName viewName)
static void DropViews(this IQuery context, IEnumerable< ObjectName > viewNames, bool onlyIfExists)
static bool ViewExists(this IQuery context, ObjectName viewName)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
IQueryPlanNode QueryPlan
Definition: ViewInfo.cs:56