DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryContext.Objects.cs
Go to the documentation of this file.
1 using System;
2 
3 using Deveel.Data.Security;
4 using Deveel.Data.Sql.Cursors;
7 
8 namespace Deveel.Data.Sql {
9  public static class QueryContextExtensions {
10  public static bool ObjectExists(this IQueryContext context, ObjectName objectName) {
11  // Special types for these database objects that can be
12  // declared in a limited context
13  if (context.CursorExists(objectName.Name))
14  return true;
15  if (context.VariableExists(objectName.Name))
16  return true;
17 
18  // We haven't found it neither in this context nor in the parent:
19  // fallback to the transaction scope
20  return context.Session().ObjectExists(objectName);
21  }
22 
23  public static bool ObjectExists(this IQueryContext context, DbObjectType objectType, ObjectName objectName) {
24  // Special types for these database objects that can be
25  // declared in a limited context
26  if (objectType == DbObjectType.Cursor &&
27  context.CursorExists(objectName.Name))
28  return true;
29 
30  if (objectType == DbObjectType.Variable &&
31  context.VariableExists(objectName.Name))
32  return true;
33 
34  // We haven't found it neither in this context nor in the parent:
35  // fallback to the transaction scope
36  return context.Session().ObjectExists(objectType, objectName);
37  }
38 
39  public static IDbObject GetObject(this IQueryContext context, DbObjectType objType, ObjectName objName) {
40  return GetObject(context, objType, objName, AccessType.ReadWrite);
41  }
42 
43  public static IDbObject GetObject(this IQueryContext context, DbObjectType objType, ObjectName objName, AccessType accessType) {
44  // First handle the special cases of cursors and variable, that can be declared
45  // in a query context
46  // If they are declared in the context, the user owns them and we don't need
47  // to verify the ownership
48  if (objType == DbObjectType.Cursor) {
49  var obj = context.FindCursor(objName.Name);
50  if (obj != null)
51  return obj;
52  } else if (objType == DbObjectType.Variable) {
53  var obj = context.FindVariable(objName.Name);
54  if (obj != null)
55  return obj;
56  }
57 
58  // TODO: throw a specialized exception
59  if (!context.UserCanAccessObject(objType, objName))
60  throw new InvalidOperationException();
61 
62  return context.Session().GetObject(objType, objName, accessType);
63  }
64 
65  internal static void CreateObject(this IQueryContext context, IObjectInfo objectInfo) {
66  // TODO: throw a specialized exception
67  if (!context.UserCanCreateObject(objectInfo.ObjectType, objectInfo.FullName))
68  throw new InvalidOperationException();
69 
70  context.Session().CreateObject(objectInfo);
71  }
72 
73  public static bool DropObject(this IQueryContext context, DbObjectType objectType, ObjectName objectName) {
74  if (objectType == DbObjectType.Variable &&
75  context.DropVariable(objectName.Name)) {
76  return true;
77  }
78  if (objectType == DbObjectType.Cursor &&
79  context.DropCursor(objectName.Name)) {
80  return true;
81  }
82 
83  if (!context.UserCanDropObject(objectType, objectName))
84  throw new MissingPrivilegesException(context.UserName(), objectName, Privileges.Drop);
85 
86  context.Session().DropObject(objectType, objectName);
87  return true;
88  }
89 
90  public static void AlterObject(this IQueryContext context, IObjectInfo objectInfo) {
91  if (objectInfo == null)
92  throw new ArgumentNullException("objectInfo");
93 
94  if (!context.UserCanAlterObject(objectInfo.ObjectType, objectInfo.FullName))
95  throw new MissingPrivilegesException(context.UserName(), objectInfo.FullName, Privileges.Alter);
96 
97  context.Session().AlterObject(objectInfo);
98  }
99 
100  public static ObjectName ResolveObjectName(this IQueryContext context, string name) {
101  if (context.VariableExists(name) ||
102  context.CursorExists(name))
103  return new ObjectName(name);
104 
105  return context.Session().ResolveObjectName(name);
106  }
107 
108  public static ObjectName ResolveObjectName(this IQueryContext context, DbObjectType objectType, ObjectName objectName) {
109  if (objectType == DbObjectType.Variable &&
110  context.VariableExists(objectName.Name))
111  return new ObjectName(objectName.Name);
112  if (objectType == DbObjectType.Cursor &&
113  context.CursorExists(objectName.Name))
114  return new ObjectName(objectName.Name);
115 
116  return context.Session().ResolveObjectName(objectType, objectName);
117  }
118 
119  public static IDbObject FindObject(this IQueryContext context, ObjectName objectName) {
120  return context.Session().FindObject(objectName);
121  }
122  }
123 }
static bool DropObject(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static ObjectName ResolveObjectName(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
static IDbObject GetObject(this IQueryContext context, DbObjectType objType, ObjectName objName)
static bool ObjectExists(this IQueryContext context, DbObjectType objectType, ObjectName objectName)
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
static void CreateObject(this IQueryContext context, IObjectInfo objectInfo)
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 bool ObjectExists(this IQueryContext context, ObjectName objectName)
static IDbObject GetObject(this IQueryContext context, DbObjectType objType, ObjectName objName, AccessType accessType)
static void AlterObject(this IQueryContext context, IObjectInfo objectInfo)
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
static ObjectName ResolveObjectName(this IQueryContext context, string name)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
static IDbObject FindObject(this IQueryContext context, ObjectName objectName)