DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryContextExtensions.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 
19 using Deveel.Data;
20 using Deveel.Data.Security;
21 using Deveel.Data.Services;
22 using Deveel.Data.Sql;
24 
25 namespace Deveel.Data.Routines {
26  public static class QueryContextExtensions {
27  public static bool IsSystemFunction(this IQueryContext context, Invoke invoke) {
28  var info = context.ResolveFunctionInfo(invoke);
29  if (info == null)
30  return false;
31 
32  return info.FunctionType != FunctionType.External &&
33  info.FunctionType != FunctionType.UserDefined;
34  }
35 
36  public static bool IsAggregateFunction(this IQueryContext context, Invoke invoke) {
37  var function = context.ResolveFunction(invoke);
38  return function != null && function.FunctionType == FunctionType.Aggregate;
39  }
40 
41  public static IRoutine ResolveRoutine(this IQueryContext context, Invoke invoke) {
42  var routine = context.ResolveSystemRoutine(invoke);
43  if (routine == null)
44  routine = context.ResolveUserRoutine(invoke);
45 
46  return routine;
47  }
48 
49  public static IRoutine ResolveSystemRoutine(this IQueryContext context, Invoke invoke) {
50  // return context.SystemContext().ResolveRoutine(invoke, context);
51 
52  var resolvers = context.ResolveAllServices<IRoutineResolver>();
53  foreach (var resolver in resolvers) {
54  var routine = resolver.ResolveRoutine(invoke, context);
55  if (routine != null)
56  return routine;
57  }
58 
59  return null;
60  }
61 
62  public static IRoutine ResolveUserRoutine(this IQueryContext context, Invoke invoke) {
63  var routine = context.Session().ResolveRoutine(invoke);
64  if (routine != null &&
65  !context.UserCanExecute(routine.Type, invoke))
66  throw new InvalidOperationException();
67 
68  return routine;
69  }
70 
71  public static IFunction ResolveFunction(this IQueryContext context, Invoke invoke) {
72  return context.ResolveRoutine(invoke) as IFunction;
73  }
74 
75  public static IFunction ResolveFunction(this IQueryContext context, ObjectName functionName, params SqlExpression[] args) {
76  var invoke = new Invoke(functionName, args);
77  return context.ResolveFunction(invoke);
78  }
79 
80  public static FunctionInfo ResolveFunctionInfo(this IQueryContext context, Invoke invoke) {
81  return context.ResolveRoutineInfo(invoke) as FunctionInfo;
82  }
83 
84  public static RoutineInfo ResolveRoutineInfo(this IQueryContext context, Invoke invoke) {
85  var routine = context.ResolveRoutine(invoke);
86  if (routine == null)
87  return null;
88 
89  return routine.RoutineInfo;
90  }
91 
92  public static DataObject InvokeSystemFunction(this IQueryContext context, string functionName,
93  params SqlExpression[] args) {
94  var resolvedName = new ObjectName(SystemSchema.SchemaName, functionName);
95  var invoke = new Invoke(resolvedName, args);
96  return context.InvokeFunction(invoke);
97  }
98 
99  public static DataObject InvokeFunction(this IQueryContext context, Invoke invoke) {
100  var result = invoke.Execute(context);
101  return result.ReturnValue;
102  }
103 
104  public static DataObject InvokeFunction(this IQueryContext context, ObjectName functionName,
105  params SqlExpression[] args) {
106  return context.InvokeFunction(new Invoke(functionName, args));
107  }
108 
109  public static void SetReturn(this IQueryContext context, SqlExpression expression) {
110  var functionContext = context as IFunctionQueryContext;
111  if (functionContext == null)
112  throw new InvalidOperationException("Not in a FUNCTION scope.");
113 
114  functionContext.SetReturn(expression);
115  }
116  }
117 }
The system uses instances of this interface to resolve routines given a user invocation.
static bool IsAggregateFunction(this IQueryContext context, Invoke invoke)
Defines a routine that is a function, that means it returns a value after its execution.
Definition: IFunction.cs:26
static bool IsSystemFunction(this IQueryContext context, Invoke invoke)
static DataObject InvokeFunction(this IQueryContext context, Invoke invoke)
static IFunction ResolveFunction(this IQueryContext context, Invoke invoke)
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
The contract to define a program routine that can interact with database objects. ...
Definition: IRoutine.cs:26
static RoutineInfo ResolveRoutineInfo(this IQueryContext context, Invoke invoke)
static DataObject InvokeSystemFunction(this IQueryContext context, string functionName, params SqlExpression[] args)
static readonly ObjectName SchemaName
The name of the system schema as ObjectName.
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
static IFunction ResolveFunction(this IQueryContext context, ObjectName functionName, params SqlExpression[] args)
The information about the invocation of a routine, including the full name and arguments (as SqlExpre...
Definition: Invoke.cs:30
DataObject ReturnValue
If the context of the result is a function, gets the return value of the function.
Definition: InvokeResult.cs:59
FunctionType
The different type of a function.
Definition: FunctionType.cs:25
Provides utilities and properties for handling the SYSTEN schema of a database.
Definition: SystemSchema.cs:37
static IRoutine ResolveUserRoutine(this IQueryContext context, Invoke invoke)
The function signature information that are used to resolve a function within a context.
Definition: FunctionInfo.cs:30
static FunctionInfo ResolveFunctionInfo(this IQueryContext context, Invoke invoke)
static DataObject InvokeFunction(this IQueryContext context, ObjectName functionName, params SqlExpression[] args)
static IRoutine ResolveSystemRoutine(this IQueryContext context, Invoke invoke)
InvokeResult Execute()
Definition: Invoke.cs:151
IRoutine ResolveRoutine(Invoke request, IQuery query)
Resolves a routine that matches the given invocation within the context provided. ...
Defines the metadata for a routine that are used to resolve within a context.
Definition: RoutineInfo.cs:28
static IRoutine ResolveRoutine(this IQueryContext context, Invoke invoke)
Defines the base class for instances that represent SQL expression tree nodes.
static void SetReturn(this IQueryContext context, SqlExpression expression)