DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Query.Objects.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 using System.Text;
21 
22 using Deveel.Data.Security;
23 using Deveel.Data.Sql.Cursors;
24 using Deveel.Data.Sql.Variables;
26 
27 namespace Deveel.Data.Sql {
28  public static class QueryExtensions {
29  public static bool ObjectExists(this IQuery query, ObjectName objectName) {
30  if (query.Context.VariableExists(objectName.Name))
31  return true;
32  if (query.Context.VariableExists(objectName.Name))
33  return true;
34 
35  return query.Session.ObjectExists(objectName);
36  }
37 
38  public static bool ObjectExists(this IQuery query, DbObjectType objectType, ObjectName objectName) {
39  if (objectType == DbObjectType.Cursor &&
40  query.Context.CursorExists(objectName.Name))
41  return true;
42  if (objectType == DbObjectType.Variable &&
43  query.Context.VariableExists(objectName.Name))
44  return true;
45 
46  return query.Session.ObjectExists(objectType, objectName);
47  }
48 
49  public static IDbObject GetObject(this IQuery query, DbObjectType objType, ObjectName objName) {
50  return GetObject(query, objType, objName, AccessType.ReadWrite);
51  }
52 
53  public static IDbObject GetObject(this IQuery query, DbObjectType objType, ObjectName objName, AccessType accessType) {
54  if (objType == DbObjectType.Cursor)
55  return query.Context.FindCursor(objName.Name);
56  if (objType == DbObjectType.Variable)
57  return query.Context.FindVariable(objName.Name);
58 
59  // TODO: throw a specialized exception
60  if (!query.UserCanAccessObject(objType, objName))
61  throw new InvalidOperationException();
62 
63  return query.Session.GetObject(objType, objName, accessType);
64  }
65 
66  public static void CreateObject(this IQuery query, IObjectInfo objectInfo) {
67  // TODO: throw a specialized exception
68  if (!query.UserCanCreateObject(objectInfo.ObjectType, objectInfo.FullName))
69  throw new InvalidOperationException();
70 
71  query.Session.CreateObject(objectInfo);
72  }
73 
74  public static bool DropObject(this IQuery query, DbObjectType objectType, ObjectName objectName) {
75  if (objectType == DbObjectType.Cursor)
76  return query.Context.DropCursor(objectName.Name);
77  if (objectType == DbObjectType.Variable)
78  return query.Context.DropVariable(objectName.Name);
79 
80  if (!query.UserCanDropObject(objectType, objectName))
81  throw new MissingPrivilegesException(query.UserName(), objectName, Privileges.Drop);
82 
83  query.Session.DropObject(objectType, objectName);
84  return true;
85  }
86 
87  public static void AlterObject(this IQuery query, IObjectInfo objectInfo) {
88  if (objectInfo == null)
89  throw new ArgumentNullException("objectInfo");
90 
91  if (objectInfo.ObjectType == DbObjectType.Cursor ||
92  objectInfo.ObjectType == DbObjectType.Variable) {
93  throw new NotSupportedException();
94  }
95 
96  if (!query.UserCanAlterObject(objectInfo.ObjectType, objectInfo.FullName))
97  throw new MissingPrivilegesException(query.UserName(), objectInfo.FullName, Privileges.Alter);
98 
99  query.Session.AlterObject(objectInfo);
100  }
101 
102  public static ObjectName ResolveObjectName(this IQuery query, string name) {
103  if (query.Context.CursorExists(name))
104  return new ObjectName(name);
105  if (query.Context.VariableExists(name))
106  return new ObjectName(name);
107 
108  return query.Session.ResolveObjectName(name);
109  }
110 
111  public static ObjectName ResolveObjectName(this IQuery query, DbObjectType objectType, ObjectName objectName) {
112  if (objectType == DbObjectType.Variable &&
113  query.Context.VariableExists(objectName.Name))
114  return new ObjectName(objectName.Name);
115  if (objectType == DbObjectType.Cursor &&
116  query.Context.VariableExists(objectName.Name))
117  return new ObjectName(objectName.Name);
118 
119  return query.Session.ResolveObjectName(objectType, objectName);
120  }
121 
122  public static IDbObject FindObject(this IQuery query, ObjectName objectName) {
123  if (query.Context.CursorExists(objectName.Name))
124  return query.Context.FindCursor(objectName.Name);
125  if (query.Context.VariableExists(objectName.Name))
126  return query.Context.FindVariable(objectName.Name);
127 
128  return query.Session.FindObject(objectName);
129  }
130  }
131 }
static bool DropObject(this IQuery query, DbObjectType objectType, ObjectName objectName)
static ObjectName ResolveObjectName(this IQuery query, DbObjectType objectType, ObjectName objectName)
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
static bool ObjectExists(this IQuery query, DbObjectType objectType, ObjectName objectName)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static IDbObject GetObject(this IQuery query, DbObjectType objType, ObjectName objName, AccessType accessType)
static bool ObjectExists(this IQuery query, ObjectName objectName)
ISession Session
Definition: IQuery.cs:23
static ObjectName ResolveObjectName(this IQuery query, string name)
static void AlterObject(this IQuery query, IObjectInfo objectInfo)
static IDbObject GetObject(this IQuery query, DbObjectType objType, ObjectName objName)
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
static void CreateObject(this IQuery query, IObjectInfo objectInfo)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
static IDbObject FindObject(this IQuery query, ObjectName objectName)
new IQueryContext Context
Definition: IQuery.cs:21