DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CursorState.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 
20 using Deveel.Data.Sql.Tables;
21 
22 namespace Deveel.Data.Sql.Cursors {
23  public sealed class CursorState : IDisposable {
24  internal CursorState(Cursor cursor) {
25  if (cursor == null)
26  throw new ArgumentNullException("cursor");
27 
28  Cursor = cursor;
29  }
30 
32  Dispose(false);
33  }
34 
35  public Cursor Cursor { get; private set; }
36 
37  public CursorStatus Status { get; private set; }
38 
39  public SqlExpression[] OpenArguments { get; private set; }
40 
41  internal ITable Result { get; private set; }
42 
43  private int CurrentOffset { get; set; }
44 
45  internal void Open(ITable result, SqlExpression[] args) {
46  Status = CursorStatus.Open;
47  Result = result;
48  OpenArguments = args;
49  }
50 
51  internal void Close() {
52  Status = CursorStatus.Closed;
53  OpenArguments = null;
54  }
55 
56  internal Row FetchRowFrom(ITable table, FetchDirection direction, int offset) {
57  int rowOffset;
58  if (direction == FetchDirection.Next) {
59  rowOffset = CurrentOffset + 1;
60  } else if (direction == FetchDirection.Prior) {
61  rowOffset = CurrentOffset - 1;
62  } else if (direction == FetchDirection.First) {
63  rowOffset = CurrentOffset = 0;
64  } else if (direction == FetchDirection.Last) {
65  rowOffset = table.RowCount;
66  } else if (direction == FetchDirection.Absolute) {
67  rowOffset = offset;
68  } else if (direction == FetchDirection.Relative) {
69  rowOffset = CurrentOffset + offset;
70  } else {
71  // Should never happen
72  throw new InvalidOperationException();
73  }
74 
75  if (rowOffset < 0 || rowOffset >= table.RowCount)
76  throw new IndexOutOfRangeException(
77  String.Format("The fetch offset '{0}' is smaller than zero or greater than the result set ({1}).", rowOffset,
78  table.RowCount));
79 
80  CurrentOffset = rowOffset;
81  Status = CursorStatus.Fetching;
82 
83  return table.GetRow(rowOffset);
84  }
85 
86  public void Dispose() {
87  Dispose(true);
88  GC.SuppressFinalize(this);
89  }
90 
91  private void Dispose(bool disposing) {
92  if (disposing) {
93  if (Result != null)
94  Result.Dispose();
95  }
96 
97  Result = null;
98  Cursor = null;
99  Status = CursorStatus.Disposed;
100  }
101  }
102 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
void Dispose(bool disposing)
Definition: CursorState.cs:91
A single row in a table of a database.
Definition: Row.cs:44
void Open(ITable result, SqlExpression[] args)
Definition: CursorState.cs:45
int RowCount
Gets the total number of rows in the table.
Definition: ITable.cs:52
Row FetchRowFrom(ITable table, FetchDirection direction, int offset)
Definition: CursorState.cs:56
Defines the base class for instances that represent SQL expression tree nodes.