DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SystemTableContainer.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.Sql;
20 using Deveel.Data.Sql.Tables;
22 
23 namespace Deveel.Data {
25  protected SystemTableContainer(ITransaction transaction, ObjectName tableName) {
26  Transaction = transaction;
27  TableName = tableName;
28  }
29 
30  public ITransaction Transaction { get; private set; }
31 
32  public ObjectName TableName { get; private set; }
33 
34  public int TableCount {
35  get { return Transaction.TableExists(TableName) ? Transaction.GetTable(TableName).RowCount : 0; }
36  }
37 
38  public int FindByName(ObjectName name) {
39  if (Transaction.RealTableExists(TableName)) {
40  // Search the table. We assume that the schema and name of the object
41  // are in columns 0 and 1 respectively.
42  var table = Transaction.GetTable(TableName);
43  var rowE = table.GetEnumerator();
44  int p = 0;
45  while (rowE.MoveNext()) {
46  int rowIndex = rowE.Current.RowId.RowNumber;
47  var obName = table.GetValue(rowIndex, 1);
48  if (obName.Value.ToString().Equals(name.Name)) {
49  var obSchema = table.GetValue(rowIndex, 0);
50  if (obSchema.Value.ToString().Equals(name.ParentName)) {
51  // Match so return this
52  return p;
53  }
54  }
55  ++p;
56  }
57  }
58 
59  return -1;
60  }
61 
62  public ObjectName GetTableName(int offset) {
63  if (Transaction.RealTableExists(TableName)) {
64  // Search the table. We assume that the schema and name of the object
65  // are in columns 0 and 1 respectively.
66  var table = Transaction.GetTable(TableName);
67  var rowE = table.GetEnumerator();
68  int p = 0;
69  while (rowE.MoveNext()) {
70  int rowIndex = rowE.Current.RowId.RowNumber;
71  if (offset == p) {
72  var obSchema = table.GetValue(rowIndex, 0);
73  var obName = table.GetValue(rowIndex, 1);
74  return new ObjectName(new ObjectName(obSchema.Value.ToString()), obName.Value.ToString());
75  }
76  ++p;
77  }
78  }
79 
80  throw new Exception("Out of bounds.");
81  }
82 
83  public abstract TableInfo GetTableInfo(int offset);
84 
85  public abstract string GetTableType(int offset);
86 
87  public bool ContainsTable(ObjectName name) {
88  // This set can not contain the table that is backing it, so we always
89  // return false for that. This check stops an annoying recursive
90  // situation for table name resolution.
91  if (name.Equals(TableName))
92  return false;
93 
94  return FindByName(name) != -1;
95  }
96 
97  public abstract ITable GetTable(int offset);
98  }
99 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
override string ToString()
Definition: ObjectName.cs:225
ObjectName GetTableName(int offset)
Gets the name of the table at the given index in this container.
The system implementation of a transaction model that handles isolated operations within a database c...
Definition: Transaction.cs:35
Describes the name of an object within a database.
Definition: ObjectName.cs:44
override bool Equals(object obj)
Definition: ObjectName.cs:241
SystemTableContainer(ITransaction transaction, ObjectName tableName)
A container for any system tables that are generated from information inside the database engine...
int FindByName(ObjectName name)
Finds the index in this container of the given table by its name.
bool ContainsTable(ObjectName name)
Checks if a table with the given name is contained in the current context.
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
The simplest implementation of a transaction.
Definition: ITransaction.cs:30
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41