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