DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ExternalFunction.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.Reflection;
19 
20 using Deveel.Data.Types;
21 
22 namespace Deveel.Data.Routines {
23  public sealed class ExternalFunction : Function {
24  private MethodInfo method;
25 
26  public ExternalFunction(FunctionInfo functionInfo)
27  : base(functionInfo) {
28  if (functionInfo.FunctionType != FunctionType.External)
29  throw new ArgumentException("The information specified are not pointing to any external function.");
30  }
31 
32  public override InvokeResult Execute(InvokeContext context) {
33  if (method == null)
34  method = DiscoverMethod();
35 
36  if (method == null)
37  throw new InvalidOperationException();
38 
39  var args = context.EvaluatedArguments;
40 
41  try {
42  var methodArgs = ConvertArguments(method, args);
43  var result = method.Invoke(null, methodArgs);
44 
45  return context.Result(ConvertValue(result, ReturnType()));
46  } catch (Exception) {
47  throw;
48  }
49  }
50 
51  private DataObject ConvertValue(object value, SqlType sqlType) {
52  throw new NotImplementedException();
53  }
54 
55  private object[] ConvertArguments(MethodInfo methodInfo, DataObject[] args) {
56  var methodParams = methodInfo.GetParameters();
57 
58  if (methodParams.Length != args.Length)
59  throw new InvalidOperationException(); // TODO: support the case of the Unbounded parameters
60 
61  var values = new object[args.Length];
62  for (int i = 0; i < args.Length; i++) {
63  var paramType = methodParams[i].ParameterType;
64  var sqlType = PrimitiveTypes.FromType(paramType);
65 
66  values[i] = sqlType.ConvertTo(args[i].Value, paramType);
67  }
68 
69  return values;
70  }
71 
72  public override SqlType ReturnType(InvokeContext context) {
73  if (method == null)
74  method = DiscoverMethod();
75 
76  return base.ReturnType(context);
77  }
78 
79  private MethodInfo DiscoverMethod() {
80  var type = FunctionInfo.ExternalType;
81  var methodName = FunctionInfo.ExternalMethodName;
82  if (String.IsNullOrEmpty(methodName))
83  methodName = FunctionInfo.RoutineName.Name;
84 
85  throw new NotImplementedException();
86  }
87  }
88 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
A long string in the system.
override SqlType ReturnType(InvokeContext context)
Resolves the function return type against the given context.
A system routine that returns a value at the end of its execution.
Definition: Function.cs:31
DataObject ConvertValue(object value, SqlType sqlType)
object[] ConvertArguments(MethodInfo methodInfo, DataObject[] args)
ObjectName RoutineName
Gets the name of the routine that uniquely identifies it in a system context.
Definition: RoutineInfo.cs:65
ExternalFunction(FunctionInfo functionInfo)
Represents the result of the execution of a routine.
Definition: InvokeResult.cs:25
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
FunctionType FunctionType
Gets the kind of function.
Definition: FunctionInfo.cs:96
virtual object ConvertTo(ISqlObject obj, Type destType)
Definition: SqlType.cs:188
FunctionType
The different type of a function.
Definition: FunctionType.cs:25
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
The function signature information that are used to resolve a function within a context.
Definition: FunctionInfo.cs:30
InvokeResult Result(DataObject value)
override InvokeResult Execute(InvokeContext context)
Executes the function and provides a result.
static SqlType FromType(Type type)