DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
UserContextTable.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 
20 using Deveel.Data.Index;
21 using Deveel.Data.Sql.Triggers;
22 
23 namespace Deveel.Data.Sql.Tables {
28  public UserContextTable(IQuery context, ITable table) {
29  Context = context;
30  Table = table;
31  }
32 
33  public ITable Table { get; private set; }
34 
35  public IQuery Context { get; private set; }
36 
37  private IMutableTable MutableTable {
38  get { return Table as IMutableTable; }
39  }
40 
41  private bool IsMutable {
42  get { return Table is IMutableTable; }
43  }
44 
45  public override IEnumerator<Row> GetEnumerator() {
46  return Table.GetEnumerator();
47  }
48 
49  public override TableInfo TableInfo {
50  get { return Table.TableInfo; }
51  }
52 
53  public override int RowCount {
54  get { return Table.RowCount; }
55  }
56 
57  protected override int ColumnCount {
58  get { return Table.ColumnCount(); }
59  }
60 
61  private void OnTableEvent(TriggerEventType eventType, RowId rowId, Row row) {
62  Context.FireTriggers(new TableEvent(this, eventType, rowId, row));
63  }
64 
65  protected override IEnumerable<int> ResolveRows(int column, IEnumerable<int> rowSet, ITable ancestor) {
66  if (!TableName.Equals(ancestor.TableInfo.TableName) &&
68  throw new Exception("Method routed to incorrect table ancestor.");
69 
70  return rowSet;
71  }
72 
73  protected override ColumnIndex GetColumnIndex(int columnOffset) {
74  return Table.GetIndex(columnOffset);
75  }
76 
77  protected override void SetupIndexes(Type indexType) {
78  }
79 
80  public override DataObject GetValue(long rowNumber, int columnOffset) {
81  return Table.GetValue(rowNumber, columnOffset);
82  }
83 
84  public override void Lock() {
85  if (IsMutable)
86  MutableTable.AddLock();
87  }
88 
89  public override void Release() {
90  if (IsMutable)
91  MutableTable.RemoveLock();
92  }
93 
95  get { return MutableTable.EventRegistry; }
96  }
97 
99  MutableTable.AddLock();
100  }
101 
103  MutableTable.RemoveLock();
104  }
105 
106  public RowId AddRow(Row row) {
107  OnTableEvent(TriggerEventType.BeforeInsert, RowId.Null, row);
108 
109  var newRowId = MutableTable.AddRow(row);
110 
111  OnTableEvent(TriggerEventType.AfterInsert, newRowId, row);
112 
113  return newRowId;
114  }
115 
116  public void UpdateRow(Row row) {
117  if (row == null)
118  throw new ArgumentNullException("row");
119 
120  var rowId = row.RowId;
121  if (rowId.IsNull)
122  throw new ArgumentException("Cannot update a row with NULL ROWID");
123 
124  OnTableEvent(TriggerEventType.BeforeUpdate, rowId, row);
125 
126  MutableTable.UpdateRow(row);
127  }
128 
129  public bool RemoveRow(RowId rowId) {
130  OnTableEvent(TriggerEventType.BeforeDelete, rowId, null);
131 
132  // TODO: Maybe we should return the row removed here
133  var result = MutableTable.RemoveRow(rowId);
134 
135  OnTableEvent(TriggerEventType.AfterDelete, rowId, null);
136 
137  return result;
138  }
139 
141  MutableTable.FlushIndexes();
142  }
143 
145  MutableTable.AssertConstraints();
146  }
147 
148  protected override void Dispose(bool disposing) {
149  Context = null;
150  Table = null;
151 
152  base.Dispose(disposing);
153  }
154  }
155 }
abstract DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
int IQueryTable. ColumnCount
Definition: Table.cs:59
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
A wrapper around a table that fires triggers on table events.
void UpdateRow(Row row)
Updates the values of a row into the table.
TriggerEventType
The different types of high layer trigger events.
override ColumnIndex GetColumnIndex(int columnOffset)
override void SetupIndexes(Type indexType)
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
override IEnumerable< int > ResolveRows(int column, IEnumerable< int > rowSet, ITable ancestor)
abstract IEnumerator< Row > GetEnumerator()
RowId AddRow(Row row)
Persists a new row to the table.
A single row in a table of a database.
Definition: Row.cs:44
override void Dispose(bool disposing)
override bool Equals(object obj)
Definition: ObjectName.cs:241
void OnTableEvent(TriggerEventType eventType, RowId rowId, Row row)
ObjectName TableName
Gets the fully qualified name of the table that is ensured to be unique within the system...
Definition: TableInfo.cs:97
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
static readonly RowId Null
Gets a NULL instance of RowId.
Definition: RowId.cs:28
TableInfo TableInfo
Gets the metadata information of the table, used to resolve the column sources.
Definition: ITable.cs:47
Exposes the context of an event fired on a table.
Definition: TableEvent.cs:26
Defines the value of a ROWID object, that is a unique reference within a database system to a single ...
Definition: RowId.cs:24
override IEnumerator< Row > GetEnumerator()
bool RemoveRow(RowId rowId)
Deletes row identified by the given coordinates from the table.
void AssertConstraints()
Performs all constraint integrity checks and actions to any modifications based on any changes that h...
abstract int RowCount
Definition: Table.cs:67
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
ColumnIndex IQueryTable. GetIndex(int column, int originalColumn, ITable table)
Definition: Table.cs:109
UserContextTable(IQuery context, ITable table)
void FlushIndexes()
Flushes all changes made on this table to the backing index scheme.
An interface that defines contracts to alter the contents of a table.
abstract TableInfo TableInfo
Definition: Table.cs:51
RowId RowId
Gets the row unique identifier within the database.
Definition: Row.cs:101