DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Query.Routines.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.Security;
20 using Deveel.Data.Services;
21 using Deveel.Data.Sql;
23 
24 namespace Deveel.Data.Routines {
25  public static class Query {
26  public static bool IsSystemFunction(this IQuery query, Invoke invoke) {
27  var info = query.ResolveFunctionInfo(invoke);
28  if (info == null)
29  return false;
30 
31  return info.FunctionType != FunctionType.External &&
32  info.FunctionType != FunctionType.UserDefined;
33  }
34 
35  public static bool IsAggregateFunction(this IQuery query, Invoke invoke) {
36  var function = query.ResolveFunction(invoke);
37  return function != null && function.FunctionType == FunctionType.Aggregate;
38  }
39 
40  public static IRoutine ResolveRoutine(this IQuery query, Invoke invoke) {
41  var routine = query.ResolveSystemRoutine(invoke);
42  if (routine == null)
43  routine = query.ResolveUserRoutine(invoke);
44 
45  return routine;
46  }
47 
48  public static IRoutine ResolveSystemRoutine(this IQuery query, Invoke invoke) {
49  // return query.SystemContext().ResolveRoutine(invoke, query);
50 
51  var resolvers = query.Context.ResolveAllServices<IRoutineResolver>();
52  foreach (var resolver in resolvers) {
53  var routine = resolver.ResolveRoutine(invoke, query);
54  if (routine != null)
55  return routine;
56  }
57 
58  return null;
59  }
60 
61  public static IRoutine ResolveUserRoutine(this IQuery query, Invoke invoke) {
62  var routine = query.Session.ResolveRoutine(invoke);
63  if (routine != null &&
64  !query.UserCanExecute(routine.Type, invoke))
65  throw new InvalidOperationException();
66 
67  return routine;
68  }
69 
70  public static IFunction ResolveFunction(this IQuery query, Invoke invoke) {
71  return query.ResolveRoutine(invoke) as IFunction;
72  }
73 
74  public static IFunction ResolveFunction(this IQuery query, ObjectName functionName, params SqlExpression[] args) {
75  var invoke = new Invoke(functionName, args);
76  return query.ResolveFunction(invoke);
77  }
78 
79  public static FunctionInfo ResolveFunctionInfo(this IQuery query, Invoke invoke) {
80  return query.ResolveRoutineInfo(invoke) as FunctionInfo;
81  }
82 
83  public static RoutineInfo ResolveRoutineInfo(this IQuery query, Invoke invoke) {
84  var routine = query.ResolveRoutine(invoke);
85  if (routine == null)
86  return null;
87 
88  return routine.RoutineInfo;
89  }
90 
91  public static DataObject InvokeSystemFunction(this IQuery query, string functionName,
92  params SqlExpression[] args) {
93  var resolvedName = new ObjectName(SystemSchema.SchemaName, functionName);
94  var invoke = new Invoke(resolvedName, args);
95  return query.InvokeFunction(invoke);
96  }
97 
98  public static DataObject InvokeFunction(this IQuery query, Invoke invoke) {
99  var result = invoke.Execute(query);
100  return result.ReturnValue;
101  }
102 
103  public static DataObject InvokeFunction(this IQuery query, ObjectName functionName,
104  params SqlExpression[] args) {
105  return query.InvokeFunction(new Invoke(functionName, args));
106  }
107  }
108 }
The system uses instances of this interface to resolve routines given a user invocation.
Defines a routine that is a function, that means it returns a value after its execution.
Definition: IFunction.cs:26
static DataObject InvokeFunction(this IQuery query, Invoke invoke)
static RoutineInfo ResolveRoutineInfo(this IQuery query, Invoke invoke)
static IRoutine ResolveRoutine(this IQuery query, Invoke invoke)
static IRoutine ResolveSystemRoutine(this IQuery query, Invoke invoke)
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 bool IsSystemFunction(this IQuery query, Invoke invoke)
static IRoutine ResolveUserRoutine(this IQuery query, Invoke invoke)
static DataObject InvokeSystemFunction(this IQuery query, string functionName, params SqlExpression[] args)
static IFunction ResolveFunction(this IQuery query, ObjectName functionName, params SqlExpression[] args)
ISession Session
Definition: IQuery.cs:23
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
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 bool IsAggregateFunction(this IQuery query, Invoke invoke)
The function signature information that are used to resolve a function within a context.
Definition: FunctionInfo.cs:30
static DataObject InvokeFunction(this IQuery query, ObjectName functionName, params SqlExpression[] args)
static IFunction ResolveFunction(this IQuery query, 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
Defines the base class for instances that represent SQL expression tree nodes.
static FunctionInfo ResolveFunctionInfo(this IQuery query, Invoke invoke)
new IQueryContext Context
Definition: IQuery.cs:21