DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CursorManager.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.Collections.Generic;
19 using System.Linq;
20 
21 namespace Deveel.Data.Sql.Cursors {
22  public sealed class CursorManager : IObjectManager {
23  private List<Cursor> cursors;
24 
25  public CursorManager(ICursorScope scope) {
26  if (scope == null)
27  throw new ArgumentNullException("scope");
28 
29  Scope = scope;
30  cursors = new List<Cursor>();
31  }
32 
34  Dispose(false);
35  }
36 
37  public ICursorScope Scope { get; private set; }
38 
39  public void Dispose() {
40  Dispose(true);
41  GC.SuppressFinalize(this);
42  }
43 
44  private void Dispose(bool disposing) {
45  if (disposing) {
46  if (cursors != null) {
47  foreach (var cursor in cursors) {
48  cursor.Dispose();
49  }
50 
51  cursors.Clear();
52  }
53  }
54 
55  cursors = null;
56  Scope = null;
57  }
58 
60  get { return DbObjectType.Cursor; }
61  }
62 
64  }
65 
67  DeclareCursor((CursorInfo)objInfo);
68  }
69 
71  return CursorExists(objName.Name);
72  }
73 
75  return CursorExists(objName.Name);
76  }
77 
79  return GetCursor(objName.Name);
80  }
81 
83  throw new NotSupportedException("Cannot alter a cursor");
84  }
85 
87  return DropCursor(objName.Name);
88  }
89 
90  public ObjectName ResolveName(ObjectName objName, bool ignoreCase) {
91  var ojectName = objName.Name;
92  var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
93  foreach (var cursor in cursors) {
94  var cursorName = cursor.CursorInfo.CursorName;
95  if (cursorName.Equals(ojectName, comparison))
96  return new ObjectName(cursorName);
97  }
98 
99  return null;
100  }
101 
102  public void DeclareCursor(CursorInfo cursorInfo) {
103  if (cursorInfo == null)
104  throw new ArgumentNullException("cursorInfo");
105 
106 
107  lock (this) {
108  var cursorName = cursorInfo.CursorName;
109  if (cursors.Any(x => x.CursorInfo.CursorName.Equals(cursorName, StringComparison.OrdinalIgnoreCase)))
110  throw new ArgumentException(String.Format("Cursor '{0}' was already declared.", cursorName));
111 
112  var cursor = new Cursor(cursorInfo);
113  cursors.Add(cursor);
114  }
115  }
116 
117  public bool CursorExists(string cursorName) {
118  var ignoreCase = Scope.IgnoreCase;
119  var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
120  return cursors.Any(x => x.CursorInfo.CursorName.Equals(cursorName, comparison));
121  }
122 
123  public Cursor GetCursor(string cursorName) {
124  var ignoreCase = Scope.IgnoreCase;
125  var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
126  return cursors.FirstOrDefault(x => x.CursorInfo.CursorName.Equals(cursorName, comparison));
127  }
128 
129  internal void DisposeCursor(Cursor cursor) {
130  var name = cursor.CursorInfo.CursorName;
131  for (int i = cursors.Count - 1; i >= 0; i--) {
132  var cursorName = cursors[i].CursorInfo.CursorName;
133  if (cursorName.Equals(name, StringComparison.OrdinalIgnoreCase))
134  cursors.RemoveAt(i);
135  }
136  }
137 
138  public bool DropCursor(string cursorName) {
139  var ignoreCase = Scope.IgnoreCase;
140  var comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
141  for (int i = cursors.Count - 1; i >= 0; i--) {
142  var cursor = cursors[i];
143  if (cursor.CursorInfo.CursorName.Equals(cursorName, comparison)) {
144  cursors.RemoveAt(i);
145  cursor.Dispose();
146  return true;
147  }
148  }
149 
150  return false;
151  }
152  }
153 }
bool AlterObject(IObjectInfo objInfo)
Modifies an existing object managed, identified by IObjectInfo.FullName component of the given specif...
Cursor GetCursor(string cursorName)
bool DropObject(ObjectName objName)
Deletes a database object handled by this manager from the system.
void Create()
Initializes the manager into the underlying system.
bool RealObjectExists(ObjectName objName)
Checks if an object really exists in the system.
bool CursorExists(string cursorName)
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
ObjectName ResolveName(ObjectName objName, bool ignoreCase)
Normalizes the input object name using the case sensitivity specified.
Describes the name of an object within a database.
Definition: ObjectName.cs:44
void CreateObject(IObjectInfo objInfo)
Create a new object of the ObjectType given the specifications given.
bool DropCursor(string cursorName)
DbObjectType ObjectType
Gets the type of objects managed by this instance.
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
IDbObject GetObject(ObjectName objName)
Gets a database object managed by this manager.
bool ObjectExists(ObjectName objName)
Checks if an object identified by the given name is managed by this instance.
void DeclareCursor(CursorInfo cursorInfo)
Defines the contract for the business managers of database objects of a given type.
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27