DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
FunctionInfo.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.Sql;
22 using Deveel.Data.Types;
23 
24 namespace Deveel.Data.Routines {
30  public sealed class FunctionInfo : RoutineInfo {
36  public FunctionInfo(ObjectName routineName, FunctionType functionType)
37  : this(routineName, (SqlType)null, functionType) {
38  }
39 
40  public FunctionInfo(ObjectName routineName, SqlType returnType)
41  : this(routineName, returnType, FunctionType.Static) {
42  }
43 
44  public FunctionInfo(ObjectName routineName, SqlType returnType, FunctionType functionType)
45  : base(routineName) {
46  ReturnType = returnType;
47  FunctionType = functionType;
48  AssertUnboundAtEnd();
49  }
50 
51  public FunctionInfo(ObjectName routineName, RoutineParameter[] parameters)
52  : this(routineName, parameters, FunctionType.Static) {
53  }
54 
55  public FunctionInfo(ObjectName routineName, RoutineParameter[] parameters, FunctionType functionType)
56  : this(routineName, parameters, null, functionType) {
57  }
58 
59  public FunctionInfo(ObjectName routineName, RoutineParameter[] parameters, SqlType returnType)
60  : this(routineName, parameters, returnType, FunctionType.Static) {
61  }
62 
63  public FunctionInfo(ObjectName routineName, RoutineParameter[] parameters, SqlType returnType, FunctionType functionType)
64  : base(routineName, parameters) {
65  ReturnType = returnType;
66  FunctionType = functionType;
67  AssertUnboundAtEnd();
68  }
69 
70  public override RoutineType RoutineType {
71  get { return RoutineType.Function; }
72  }
73 
74  public SqlType ReturnType { get; private set; }
75 
76  private void AssertUnboundAtEnd() {
77  for (int i = 0; i < Parameters.Length; i++) {
78  var param = Parameters[i];
79  if (param.IsUnbounded) {
80  if (HasUnboundParameter)
81  throw new ArgumentException("Cannot specify more than one 'unbounded' argument for a function.");
82 
83  if (i != Parameters.Length - 1)
84  throw new ArgumentException("An unbounded parameter must be present only at the end.");
85 
86  HasUnboundParameter = true;
87  }
88  }
89  }
90 
91  private bool HasUnboundParameter { get; set; }
92 
96  public FunctionType FunctionType { get; private set; }
97 
98  internal override bool MatchesInvoke(Invoke request, IQuery query) {
99  if (request == null)
100  return false;
101 
102  bool ignoreCase = true;
103  if (query != null)
104  ignoreCase = query.IgnoreIdentifiersCase();
105 
106  if (!RoutineName.Equals(request.RoutineName, ignoreCase))
107  return false;
108 
109  // TODO: add a better resolution to obtain the final type of the argument
110  // and compare it to the parameter type definition
111  bool unboundedSeen = false;
112  for (int i = 0; i < request.Arguments.Length; i++) {
113  var argType = request.Arguments[i].ReturnType(query, null);
114 
115  if (i + 1 > Parameters.Length) {
116  if (!unboundedSeen)
117  return false;
118 
119  // TODO: verify the type of the argument (how to evaluate?)
120 
121  } else {
122  var param = Parameters[i];
123  unboundedSeen = param.IsUnbounded;
124 
125  var paramType = param.Type;
126 
127  if (!paramType.IsComparable(argType))
128  return false;
129  }
130  }
131 
132  if (!unboundedSeen &&
133  request.Arguments.Length != Parameters.Length)
134  return false;
135 
136  return true;
137  }
138  }
139 }
SqlExpression[] Arguments
Gets an array of arguments to be passed to the invoked routine.
Definition: Invoke.cs:63
FunctionInfo(ObjectName routineName, RoutineParameter[] parameters)
Definition: FunctionInfo.cs:51
FunctionInfo(ObjectName routineName, RoutineParameter[] parameters, SqlType returnType, FunctionType functionType)
Definition: FunctionInfo.cs:63
FunctionInfo(ObjectName routineName, RoutineParameter[] parameters, SqlType returnType)
Definition: FunctionInfo.cs:59
FunctionInfo(ObjectName routineName, FunctionType functionType)
Constructs a FunctionInfo without arguments.
Definition: FunctionInfo.cs:36
Describes the name of an object within a database.
Definition: ObjectName.cs:44
A type that represents a static function.
RoutineType
The type of routine program.
Definition: RoutineType.cs:23
The information about the invocation of a routine, including the full name and arguments (as SqlExpre...
Definition: Invoke.cs:30
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
ObjectName RoutineName
Gets the fully qualified name of the routine to invoke.
Definition: Invoke.cs:58
Defines the metadata for a routine that are used to resolve within a context.
Definition: RoutineInfo.cs:28
FunctionInfo(ObjectName routineName, RoutineParameter[] parameters, FunctionType functionType)
Definition: FunctionInfo.cs:55
FunctionInfo(ObjectName routineName, SqlType returnType, FunctionType functionType)
Definition: FunctionInfo.cs:44
FunctionInfo(ObjectName routineName, SqlType returnType)
Definition: FunctionInfo.cs:40
override bool MatchesInvoke(Invoke request, IQuery query)
Definition: FunctionInfo.cs:98