DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
BaseDataTable.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 using Deveel.Data;
22 using Deveel.Data.Index;
23 using Deveel.Data.Services;
24 
25 namespace Deveel.Data.Sql.Tables {
26  public abstract class BaseDataTable : RootTable {
27  private readonly IContext context;
28 
29  private ColumnIndex[] indexes;
30 
31  protected BaseDataTable()
32  : this(null) {
33  }
34 
35  protected BaseDataTable(IContext context) {
36  this.context = context;
37  }
38 
39  public override IContext Context {
40  get { return context; }
41  }
42 
43  protected override ObjectName GetResolvedColumnName(int column) {
44  var columnName = TableInfo[column].ColumnName;
45  return new ObjectName(TableName, columnName);
46  }
47 
48  protected override int IndexOfColumn(ObjectName columnName) {
49  // Check this is the correct table first...
50  var tableName = columnName.Parent;
51  var tableInfo = TableInfo;
52  if (tableName != null && tableName.Equals(TableName)) {
53  // Look for the column name
54  string colName = columnName.Name;
55  int size = ColumnCount;
56  for (int i = 0; i < size; ++i) {
57  var col = tableInfo[i];
58  if (col.ColumnName.Equals(colName)) {
59  return i;
60  }
61  }
62  }
63 
64  return -1;
65  }
66 
67  protected virtual ColumnIndex GetColumnIndex(int columnOffset) {
68  return indexes[columnOffset];
69  }
70 
71  protected override ColumnIndex GetIndex(int column, int originalColumn, ITable table) {
72  var index = GetColumnIndex(column);
73  if (table == this)
74  return index;
75 
76  // Otherwise, get the scheme to calculate a subset of the given scheme.
77  return index.GetSubset(table, originalColumn);
78  }
79 
80  protected void SetupIndexes(string indexTypeName) {
81  Type indexType;
82  if (String.Equals(indexTypeName, DefaultIndexTypes.BlindSearch)) {
83  indexType = typeof (BlindSearchIndex);
84  } else if (String.Equals(indexTypeName, DefaultIndexTypes.InsertSearch)) {
85  indexType = typeof (InsertSearchIndex);
86  } else {
87 #if PCL
88  indexType = Type.GetType(indexTypeName, false);
89 #else
90  indexType = Type.GetType(indexTypeName, false, true);
91 #endif
92  }
93 
94  if (indexType == null) {
95  indexType = typeof (BlindSearchIndex);
96  } else if (!typeof (ColumnIndex).IsAssignableFrom(indexType)) {
97  throw new InvalidOperationException(String.Format("The type '{0}' is not a valid table index.", indexType));
98  }
99 
100  SetupIndexes(indexType);
101  }
102 
103  protected virtual void SetupIndexes(Type indexType) {
104  indexes = new ColumnIndex[ColumnCount];
105  for (int i = 0; i < ColumnCount; i++) {
106  if (indexType == typeof (BlindSearchIndex)) {
107  indexes[i] = new BlindSearchIndex(this, i);
108  } else if (indexType == typeof (InsertSearchIndex)) {
109  indexes[i] = new InsertSearchIndex(this, i);
110  } else {
111  var index = Activator.CreateInstance(indexType, this, i) as ColumnIndex;
112  if (index == null)
113  throw new InvalidOperationException();
114 
115  indexes[i] = index;
116  }
117  }
118  }
119 
120  protected override RawTableInfo GetRawTableInfo(RawTableInfo rootInfo) {
121  var rows = this.Select(row => row.RowId.RowNumber).ToList();
122  rootInfo.Add(this, rows);
123  return rootInfo;
124  }
125 
126  protected override IEnumerable<int> ResolveRows(int column, IEnumerable<int> rowSet, ITable ancestor) {
127  if (ancestor != this)
128  throw new Exception("Method routed to incorrect table ancestor.");
129 
130  return rowSet;
131  }
132 
133  public void AddToIndex(int rowNumber, int columnNumber) {
134  bool indexableType = TableInfo[columnNumber].IsIndexable;
135  if (indexableType) {
136  var index = GetColumnIndex(columnNumber);
137  index.Insert(rowNumber);
138  }
139  }
140 
141  public void AddRowToIndex(int rowNumber) {
142  int colCount = ColumnCount;
143  var tableInfo = TableInfo;
144  for (int i = 0; i < colCount; ++i) {
145  if (tableInfo[i].IsIndexable) {
146  var index = GetColumnIndex(i);
147  index.Insert(rowNumber);
148  }
149  }
150  }
151 
152  public void RemoveRowFromIndex(int rowNumber) {
153  int colCount = ColumnCount;
154  var tableInfo = TableInfo;
155  for (int i = 0; i < colCount; ++i) {
156  if (tableInfo[i].IsIndexable) {
157  var index = GetColumnIndex(i);
158  index.Remove(rowNumber);
159  }
160  }
161  }
162  }
163 }
override int IndexOfColumn(ObjectName columnName)
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
override ObjectName GetResolvedColumnName(int column)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
void RemoveRowFromIndex(int rowNumber)
override RawTableInfo GetRawTableInfo(RawTableInfo rootInfo)
A user-defined TYPE that holds complex objects in a database column.
virtual ColumnIndex GetColumnIndex(int columnOffset)
virtual void SetupIndexes(Type indexType)
void Add(IRootTable table, IList< int > rowSet)
Definition: RawTableInfo.cs:34
ObjectName Parent
Gets the parent reference of the current one, if any or null if none.
Definition: ObjectName.cs:99
void AddToIndex(int rowNumber, int columnNumber)
string Name
Gets the name of the object being referenced.
Definition: ObjectName.cs:108
override ColumnIndex GetIndex(int column, int originalColumn, ITable table)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
override IEnumerable< int > ResolveRows(int column, IEnumerable< int > rowSet, ITable ancestor)
void SetupIndexes(string indexTypeName)