DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ContextExtensions.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.Services;
21 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql.Variables {
24  public static class ContextExtensions {
25  public static Variable FindVariable(this IContext context, string variableName) {
26  var currentContext = context;
27  while (currentContext != null) {
28  if (currentContext is IVariableScope) {
29  var scope = (IVariableScope) currentContext;
30  var variable = scope.GetVariable(variableName);
31  if (variable != null)
32  return variable;
33  }
34 
35  currentContext = currentContext.Parent;
36  }
37 
38 
39  // not found in the hierarchy
40  return null;
41  }
42 
43  public static bool VariableExists(this IContext context, string variableName) {
44  var currentContext = context;
45  while (currentContext != null) {
46  if (currentContext is IVariableScope) {
47  var scope = (IVariableScope)currentContext;
48  if (scope.HasVariable(variableName))
49  return true;
50  }
51 
52  currentContext = currentContext.Parent;
53  }
54 
55 
56  // not found in the hierarchy
57  return false;
58  }
59 
60  public static bool DropVariable(this IContext context, string variableName) {
61  var currentContext = context;
62  while (currentContext != null) {
63  if (currentContext is IVariableScope) {
64  var scope = (IVariableScope)currentContext;
65  if (scope.HasVariable(variableName))
66  return scope.DropVariable(variableName);
67  }
68 
69  currentContext = currentContext.Parent;
70  }
71 
72 
73  // not found in the hierarchy
74  return false;
75  }
76 
77  public static Variable DeclareVariable(this IContext context, VariableInfo variableInfo) {
78  if (context.VariableExists(variableInfo.VariableName))
79  throw new InvalidOperationException(String.Format("Variable '{0}' already defined in the context hierarchy.", variableInfo.VariableName));
80 
81  var currentContext = context;
82  while (currentContext != null) {
83  if (currentContext is IVariableScope) {
84  var scope = (IVariableScope)currentContext;
85  return scope.DefineVariable(variableInfo);
86  }
87 
88  currentContext = currentContext.Parent;
89  }
90 
91  // not found in the hierarchy
92  return null;
93  }
94 
95  public static Variable DeclareVariable(this IContext context, string variableName, SqlType variableType) {
96  return DeclareVariable(context, variableName, variableType, false);
97  }
98 
99  public static Variable DeclareVariable(this IContext context, string variableName, SqlType variableType, bool constant) {
100  return context.DeclareVariable(new VariableInfo(variableName, variableType, constant));
101  }
102 
103  public static Variable SetVariable(this IContext context, string variableName, SqlExpression value) {
104  var currentContext = context;
105  while (currentContext != null) {
106  if (currentContext is IVariableScope) {
107  var scope = (IVariableScope) currentContext;
108  if (scope.HasVariable(variableName)) {
109  // TODO: support also in-context evaluation
110  var constantValue = value.EvaluateToConstant(null, context.VariableResolver());
111  return scope.SetVariable(variableName, constantValue);
112  }
113  }
114 
115  currentContext = currentContext.Parent;
116  }
117 
118  currentContext = context;
119  while (currentContext != null) {
120  if (currentContext is IVariableScope) {
121  var scope = (IVariableScope)currentContext;
122  // TODO: support also in-context evaluation
123  var constantValue = value.EvaluateToConstant(null, context.VariableResolver());
124  return scope.SetVariable(variableName, constantValue);
125  }
126 
127  currentContext = currentContext.Parent;
128  }
129 
130  // not found in the hierarchy
131  return null;
132  }
133 
134  public static IVariableResolver VariableResolver(this IContext context) {
135  return new ContextVariableResolver(context);
136  }
137 
138  #region ContextVariableResolver
139 
141  private IContext context;
142 
144  this.context = context;
145  }
146 
147  public DataObject Resolve(ObjectName variableName) {
148  var variable = context.FindVariable(variableName.Name);
149  if (variable == null)
150  return null;
151 
152  return variable.Value;
153  }
154 
155  public SqlType ReturnType(ObjectName variableName) {
156  var variable = context.FindVariable(variableName.Name);
157  if (variable == null)
158  return null;
159 
160  return variable.Type;
161  }
162 
163  public void Dispose() {
164  context = null;
165  }
166  }
167 
168  #endregion
169  }
170 }
static bool VariableExists(this IContext context, string variableName)
A long string in the system.
static IVariableResolver VariableResolver(this IContext context)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
static Variable DeclareVariable(this IContext context, string variableName, SqlType variableType, bool constant)
static bool DropVariable(this IContext context, string variableName)
static Variable FindVariable(this IContext context, string variableName)
static Variable SetVariable(this IContext context, string variableName, SqlExpression value)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
static Variable DeclareVariable(this IContext context, VariableInfo variableInfo)
An interface to resolve a variable name to a constant object.
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
SqlType ReturnType(ObjectName variableName)
Returns the SqlType of object the given variable is.
Defines the base class for instances that represent SQL expression tree nodes.
static Variable DeclareVariable(this IContext context, string variableName, SqlType variableType)
DataObject Resolve(ObjectName variableName)
Returns the value of a given variable.