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 
22 namespace Deveel.Data.Sql.Cursors {
23  public static class ContextExtensions {
24  public static bool DeclareCursor(this IContext context, CursorInfo cursorInfo) {
25  if (context.CursorExists(cursorInfo.CursorName))
26  throw new InvalidOperationException(String.Format("A cursor named '{0}' was already defined in the context.",
27  cursorInfo.CursorName));
28 
29  var currentContext = context;
30  while (currentContext != null) {
31  if (currentContext is ICursorScope) {
32  var scope = (ICursorScope)currentContext;
33  scope.DeclareCursor(cursorInfo);
34  return true;
35  }
36 
37  currentContext = currentContext.Parent;
38  }
39 
40  return false;
41  }
42 
43  public static void DeclareCursor(this IContext context, string cursorName, SqlQueryExpression query) {
44  DeclareCursor(context, cursorName, (CursorFlags)0, query);
45  }
46 
47  public static void DeclareCursor(this IContext context, string cursorName, CursorFlags flags, SqlQueryExpression query) {
48  context.DeclareCursor(new CursorInfo(cursorName, flags, query));
49  }
50 
51  public static void DeclareInsensitiveCursor(this IContext context, string cursorName, SqlQueryExpression query) {
52  DeclareInsensitiveCursor(context, cursorName, query, false);
53  }
54 
55  public static void DeclareInsensitiveCursor(this IContext context, string cursorName, SqlQueryExpression query, bool withScroll) {
56  var flags = CursorFlags.Insensitive;
57  if (withScroll)
58  flags |= CursorFlags.Scroll;
59 
60  context.DeclareCursor(cursorName, flags, query);
61  }
62 
63 
64  public static bool CursorExists(this IContext context, string cursorName) {
65  var currentContext = context;
66  while (currentContext != null) {
67  if (currentContext is ICursorScope) {
68  var scope = (ICursorScope) currentContext;
69  if (scope.CursorExists(cursorName))
70  return true;
71  }
72 
73  currentContext = currentContext.Parent;
74  }
75 
76  return false;
77  }
78 
79  public static Cursor FindCursor(this IContext context, string cursorName) {
80  var currentContext = context;
81  while (currentContext != null) {
82  if (currentContext is ICursorScope) {
83  var scope = (ICursorScope)currentContext;
84  var cursor = scope.GetCursor(cursorName);
85  if (cursor != null)
86  return cursor;
87  }
88 
89  currentContext = currentContext.Parent;
90  }
91 
92  return null;
93  }
94 
95  public static bool DropCursor(this IContext context, string cursorName) {
96  var currentContext = context;
97  while (currentContext != null) {
98  if (currentContext is ICursorScope) {
99  var scope = (ICursorScope)currentContext;
100  if (scope.CursorExists(cursorName))
101  return scope.DropCursor(cursorName);
102  }
103 
104  currentContext = currentContext.Parent;
105  }
106 
107  return false;
108  }
109 
110  public static bool CloseCursor(this IContext context, string cursorName) {
111  var cursor = context.FindCursor(cursorName);
112  if (cursor == null)
113  return false;
114 
115  cursor.Close();
116  return true;
117  }
118 
119  public static bool OpenCursor(this IContext context, IRequest request, string cursorName, params SqlExpression[] args) {
120  var cursor = context.FindCursor(cursorName);
121  if (cursor == null)
122  return false;
123 
124  // TODO: support the evaluate in context (and not just IQueryContext)
125  throw new NotImplementedException();
126  }
127  }
128 }
static void DeclareInsensitiveCursor(this IContext context, string cursorName, SqlQueryExpression query)
static void DeclareCursor(this IContext context, string cursorName, CursorFlags flags, SqlQueryExpression query)
static bool CloseCursor(this IContext context, string cursorName)
static bool DeclareCursor(this IContext context, CursorInfo cursorInfo)
static void DeclareInsensitiveCursor(this IContext context, string cursorName, SqlQueryExpression query, bool withScroll)
static Cursor FindCursor(this IContext context, string cursorName)
static void DeclareCursor(this IContext context, string cursorName, SqlQueryExpression query)
static bool DropCursor(this IContext context, string cursorName)
static bool CursorExists(this IContext context, string cursorName)
static bool OpenCursor(this IContext context, IRequest request, string cursorName, params SqlExpression[] args)
Defines the base class for instances that represent SQL expression tree nodes.