DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Table.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;
19 using System.Collections.Generic;
20 
21 using Deveel.Data.Index;
23 using Deveel.Data.Types;
24 
25 namespace Deveel.Data.Sql.Tables {
26  public abstract class Table : IQueryTable, ILockable {
27  // Stores col name -> col index lookups
28  private Dictionary<ObjectName, int> colNameLookup;
29  private readonly object colLookupLock = new object();
30 
31  ~Table() {
32  Dispose(false);
33  }
34 
35  public abstract IEnumerator<Row> GetEnumerator();
36 
37  IEnumerator IEnumerable.GetEnumerator() {
38  return GetEnumerator();
39  }
40 
41  public void Dispose() {
42  Dispose(true);
43  GC.SuppressFinalize(this);
44  }
45 
46  protected virtual void Dispose(bool disposing) {
47  }
48 
49  public abstract IContext Context { get; }
50 
51  public abstract TableInfo TableInfo { get; }
52 
53  public bool IsLocked { get; private set; }
54 
55  object ILockable.RefId {
56  get { return TableInfo.TableName; }
57  }
58 
60  get { return ColumnCount; }
61  }
62 
63  protected virtual int ColumnCount {
64  get { return TableInfo.ColumnCount; }
65  }
66 
67  public abstract int RowCount { get; }
68 
69  public abstract void Lock();
70 
71  public abstract void Release();
72 
73  protected virtual void OnLockAcquired(Lock @lock) {
74  Lock();
75  }
76 
77  protected virtual void OnLockReleased(Lock @lock) {
78  Release();
79  }
80 
81  void ILockable.Acquired(Lock @lock) {
82  try {
83  OnLockAcquired(@lock);
84  } finally {
85  IsLocked = true;
86  }
87  }
88 
89  void ILockable.Released(Lock @lock) {
90  try {
91  OnLockReleased(@lock);
92  } finally {
93  IsLocked = true;
94  }
95  }
96 
97  protected virtual int IndexOfColumn(ObjectName columnName) {
98  return TableInfo.IndexOfColumn(columnName.Name);
99  }
100 
101  protected virtual ObjectName GetResolvedColumnName(int column) {
102  return TableInfo[column].FullColumnName;
103  }
104 
106  return GetResolvedColumnName(column);
107  }
108 
109  ColumnIndex IQueryTable.GetIndex(int column, int originalColumn, ITable table) {
110  return GetIndex(column, originalColumn, table);
111  }
112 
113  protected virtual ColumnIndex GetIndex(int column, int originalColumn, ITable table) {
114  return GetIndex(column);
115  }
116 
117  protected abstract IEnumerable<int> ResolveRows(int column, IEnumerable<int> rowSet, ITable ancestor);
118 
119  IEnumerable<int> IQueryTable.ResolveRows(int columnOffset, IEnumerable<int> rows, ITable ancestor) {
120  return ResolveRows(columnOffset, rows, ancestor);
121  }
122 
123  protected abstract RawTableInfo GetRawTableInfo(RawTableInfo rootInfo);
124 
126  return GetRawTableInfo(rootInfo);
127  }
128 
129  public abstract DataObject GetValue(long rowNumber, int columnOffset);
130 
131  public ColumnIndex GetIndex(int columnOffset) {
132  return GetIndex(columnOffset, columnOffset, this);
133  }
134 
135  public ObjectName FullName {
136  get { return TableInfo.TableName; }
137  }
138 
140  get { return DbObjectType.Table; }
141  }
142 
143  protected int FindColumn(ObjectName columnName) {
144  lock (colLookupLock) {
145  if (colNameLookup == null)
146  colNameLookup = new Dictionary<ObjectName, int>(30);
147 
148  int index;
149  if (!colNameLookup.TryGetValue(columnName, out index)) {
150  index = IndexOfColumn(columnName);
151  colNameLookup[columnName] = index;
152  }
153 
154  return index;
155  }
156  }
157 
159  return FindColumn(columnName);
160  }
161 
162  private SqlType GetColumnType(int columnOffset) {
163  return TableInfo[columnOffset].ColumnType;
164  }
165 
166  private SqlType GetColumnType(ObjectName columnName) {
167  return GetColumnType(FindColumn(columnName));
168  }
169 
171  return new TableVariableResolver(this);
172  }
173 
174  #region TableVariableResolver
175 
178  : this(table, -1) {
179  }
180 
181  public TableVariableResolver(Table table, int rowIndex) {
182  this.table = table;
183  this.rowIndex = rowIndex;
184  }
185 
186  private readonly Table table;
187  private readonly int rowIndex;
188 
189  private int FindColumnName(ObjectName columnName) {
190  int colIndex = table.FindColumn(columnName);
191  if (colIndex == -1) {
192  throw new InvalidOperationException("Can't find column: " + columnName);
193  }
194  return colIndex;
195  }
196 
197  public DataObject Resolve(ObjectName columnName) {
198  return table.GetValue(rowIndex, FindColumnName(columnName));
199  }
200 
201  public SqlType ReturnType(ObjectName columnName) {
202  return table.GetColumnType(columnName);
203  }
204 
205  public ITableVariableResolver ForRow(int row) {
206  return new TableVariableResolver(table, row);
207  }
208  }
209 
210  #endregion
211  }
212 }
DataObject Resolve(ObjectName columnName)
Returns the value of a given variable.
Definition: Table.cs:197
virtual int IndexOfColumn(ObjectName columnName)
Definition: Table.cs:97
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
virtual ColumnIndex GetIndex(int column, int originalColumn, ITable table)
Definition: Table.cs:113
virtual void Dispose(bool disposing)
Definition: Table.cs:46
SqlType GetColumnType(ObjectName columnName)
Definition: Table.cs:166
Dictionary< ObjectName, int > colNameLookup
Definition: Table.cs:28
Represents a database object, such as a table, a trigger, a type or a column.
Definition: IDbObject.cs:24
int FindColumn(ObjectName columnName)
Definition: Table.cs:143
IEnumerable< int > ResolveRows(int column, IEnumerable< int > rowSet, ITable ancestor)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
ObjectName TableName
Gets the fully qualified name of the table that is ensured to be unique within the system...
Definition: TableInfo.cs:97
virtual ObjectName GetResolvedColumnName(int column)
Definition: Table.cs:101
ITableVariableResolver GetVariableResolver()
int FindColumn(ObjectName columnName)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
TableVariableResolver(Table table, int rowIndex)
Definition: Table.cs:181
DbObjectType ObjectType
Gets the type of database object that the implementation is for
Definition: IDbObject.cs:35
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
virtual void OnLockAcquired(Lock @lock)
Definition: Table.cs:73
int FindColumnName(ObjectName columnName)
Definition: Table.cs:189
ColumnIndex GetIndex(int columnOffset)
Gets an index for given column that can be used to select values from this table. ...
Definition: Table.cs:131
int IndexOfColumn(string columnName)
Gets the offset of the column with the given name.
Definition: TableInfo.cs:310
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
ObjectName GetResolvedColumnName(int columnOffset)
SqlType ReturnType(ObjectName columnName)
Returns the SqlType of object the given variable is.
Definition: Table.cs:201
int ColumnCount
Gets a count of the columns defined by this object.
Definition: TableInfo.cs:159
RawTableInfo GetRawTableInfo(RawTableInfo rootInfo)
ColumnIndex GetIndex(int column, int originalColumn, ITable table)
DbObjectType
The kind of objects that can be handled by a database system and its managers
Definition: DbObjectType.cs:27
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
SqlType GetColumnType(int columnOffset)
Definition: Table.cs:162
ITableVariableResolver ForRow(int row)
Definition: Table.cs:205
virtual void OnLockReleased(Lock @lock)
Definition: Table.cs:77