DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Invoke.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.Linq;
19 using System.Text;
20 
21 using Deveel.Data;
22 using Deveel.Data.Sql;
24 
25 namespace Deveel.Data.Routines {
30  public sealed class Invoke {
31  private IRoutine cached;
32 
39  public Invoke(ObjectName routineName)
40  : this(routineName, new SqlExpression[0]) {
41  }
42 
50  public Invoke(ObjectName routineName, SqlExpression[] arguments) {
51  RoutineName = routineName;
52  Arguments = arguments;
53  }
54 
58  public ObjectName RoutineName { get; private set; }
59 
63  public SqlExpression[] Arguments { get; private set; }
64 
73  public bool IsGlobArgument {
74  get {
75  return Arguments != null &&
76  Arguments.Length == 1 &&
77  Arguments[0] is SqlConstantExpression &&
78  ((SqlConstantExpression) Arguments[0]).Value.Value.ToString() == "*";
79  }
80  }
81 
91  public bool IsAggregate(IRequest query) {
92  if (query.Query.IsAggregateFunction(this))
93  return true;
94 
95  // Look at parameterss
96  return Arguments.Any(x => x.HasAggregate(query));
97  }
98 
115  public IRoutine ResolveRoutine(IRequest context) {
116  if (cached != null)
117  return cached;
118 
119  if (context == null) {
120  cached = SystemFunctions.Provider.ResolveFunction(this, null);
121  } else {
122  cached = context.Query.ResolveRoutine(this);
123  }
124 
125  if (cached == null)
126  throw new InvalidOperationException(String.Format("Unable to resolve the call {0} to a function", this));
127 
128  return cached;
129  }
130 
131  public IFunction ResolveFunction(IQuery context) {
132  return ResolveRoutine(context) as IFunction;
133  }
134 
136  return ResolveRoutine(context) as IProcedure;
137  }
138 
148  return ResolveRoutine(null) as IFunction;
149  }
150 
152  return Execute(null);
153  }
154 
155  public InvokeResult Execute(IRequest query) {
156  return Execute(query, null);
157  }
158 
159  public InvokeResult Execute(IRequest query, IVariableResolver resolver) {
160  return Execute(query, resolver, null);
161  }
162 
164  var routine = ResolveRoutine(query);
165  var executeContext = new InvokeContext(this, routine, resolver, group, query);
166  return routine.Execute(executeContext);
167  }
168 
169  public override String ToString() {
170  var buf = new StringBuilder();
171  buf.Append(RoutineName);
172  buf.Append('(');
173  for (int i = 0; i < Arguments.Length; ++i) {
174  buf.Append(Arguments[i]);
175  if (i < Arguments.Length - 1) {
176  buf.Append(',');
177  }
178  }
179  buf.Append(')');
180  return buf.ToString();
181  }
182  }
183 }
IFunction ResolveFunction(IQuery context)
Definition: Invoke.cs:131
Invoke(ObjectName routineName, SqlExpression[] arguments)
Constructs a new Invoke with the given name of the routine and the arguments.
Definition: Invoke.cs:50
Defines a routine that is a function, that means it returns a value after its execution.
Definition: IFunction.cs:26
IRoutine ResolveRoutine(IRequest context)
Resolves the routine target of the invocation within the give context.
Definition: Invoke.cs:115
bool IsAggregate(IRequest query)
Checks if the target of the invocation is an aggregate function.
Definition: Invoke.cs:91
InvokeResult Execute(IRequest query, IVariableResolver resolver)
Definition: Invoke.cs:159
InvokeResult Execute(IRequest query)
Definition: Invoke.cs:155
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
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
Invoke(ObjectName routineName)
Constructs a new Invoke with the given name of the routine and no arguments.
Definition: Invoke.cs:39
DataObject Value
Gets the constant value of the expression.
Defines a contract used by grouping functions to find information about the current group being evalu...
Represents the result of the execution of a routine.
Definition: InvokeResult.cs:25
The information about the invocation of a routine, including the full name and arguments (as SqlExpre...
Definition: Invoke.cs:30
A specific kind of routine that does not return any value when executed.
Definition: IProcedure.cs:29
An interface to resolve a variable name to a constant object.
An expression that holds a constant value.
IFunction ResolveFunction(Invoke invoke, IQuery query)
InvokeResult Execute()
Definition: Invoke.cs:151
InvokeResult Execute(IRequest query, IVariableResolver resolver, IGroupResolver group)
Definition: Invoke.cs:163
Defines the base class for instances that represent SQL expression tree nodes.
override String ToString()
Definition: Invoke.cs:169
IFunction ResolveSystemFunction()
Resolves this routine invocation to a system function.
Definition: Invoke.cs:147
static FunctionProvider Provider
IProcedure ResolveProcedure(IQuery context)
Definition: Invoke.cs:135