DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CompositeTable.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.Index;
22 
23 namespace Deveel.Data.Sql.Tables {
25  private readonly ITable mainTable;
26  private readonly ITable[] composites;
27 
28  private readonly IList<int>[] rowIndexes;
29  private readonly ColumnIndex[] columnIndexes;
30 
31  private int rootsLocked;
32 
33  public CompositeTable(ITable mainTable, ITable[] composites, CompositeFunction function, bool all) {
34  this.mainTable = mainTable;
35  this.composites = composites;
36 
37  columnIndexes = new ColumnIndex[mainTable.TableInfo.ColumnCount];
38  int size = composites.Length;
39  rowIndexes = new IList<int>[size];
40 
41  if (function == CompositeFunction.Union) {
42  // Include all row sets in all tables
43  for (int i = 0; i < size; ++i) {
44  rowIndexes[i] = composites[i].SelectAllRows().ToList();
45  }
46 
47  RemoveDuplicates(all);
48  } else {
49  throw new InvalidOperationException("Unrecognised composite function");
50  }
51 
52  }
53 
54  private void RemoveDuplicates(bool all) {
55  if (!all)
56  throw new NotImplementedException();
57  }
58 
59  public CompositeTable(ITable[] composites, CompositeFunction function, bool all)
60  : this(composites[0], composites, function, all) {
61  }
62 
63  public override IEnumerator<Row> GetEnumerator() {
64  return new SimpleRowEnumerator(this);
65  }
66 
67  public override IContext Context {
68  get { return mainTable.Context; }
69  }
70 
71  protected override int ColumnCount {
72  get { return mainTable.TableInfo.ColumnCount; }
73  }
74 
75  public override void Lock() {
76  // For each table, recurse.
77  rootsLocked++;
78  for (int i = 0; i < composites.Length; ++i) {
79  composites[i].Lock();
80  }
81  }
82 
83  public override void Release() {
84  // For each table, recurse.
85  rootsLocked--;
86  for (int i = 0; i < composites.Length; ++i) {
87  composites[i].Release();
88  }
89  }
90 
91  protected override RawTableInfo GetRawTableInfo(RawTableInfo rootInfo) {
92  var rows = this.Select(x => x.RowId.RowNumber).ToArray();
93  rootInfo.Add(this, rows);
94  return rootInfo;
95  }
96 
97  public override int RowCount {
98  get { return rowIndexes.Sum(t => t.Count); }
99  }
100 
101  protected override ColumnIndex GetIndex(int column, int originalColumn, ITable table) {
102  var index = columnIndexes[column];
103  if (index == null) {
104  index = new BlindSearchIndex(this, column);
105  columnIndexes[column] = index;
106  }
107 
108  // If we are getting a scheme for this table, simple return the information
109  // from the column_trees Vector.
110  if (table == this)
111  return index;
112 
113  // Otherwise, get the scheme to calculate a subset of the given scheme.
114  return index.GetSubset(table, originalColumn);
115  }
116 
117  protected override IEnumerable<int> ResolveRows(int column, IEnumerable<int> rowSet, ITable ancestor) {
118  if (ancestor != this)
119  throw new InvalidOperationException();
120 
121  return rowSet;
122  }
123 
124  public override DataObject GetValue(long rowNumber, int columnOffset) {
125  for (int i = 0; i < rowIndexes.Length; ++i) {
126  var list = rowIndexes[i];
127  int sz = list.Count;
128  if (rowNumber < sz)
129  return composites[i].GetValue(list[(int)rowNumber], columnOffset);
130 
131  rowNumber -= sz;
132  }
133 
134  throw new ArgumentOutOfRangeException("rowNumber", String.Format("Row '{0}' out of range.", rowNumber));
135  }
136 
137  protected override ObjectName GetResolvedColumnName(int column) {
138  return mainTable.GetResolvedColumnName(column);
139  }
140 
141  protected override int IndexOfColumn(ObjectName columnName) {
142  return mainTable.IndexOfColumn(columnName);
143  }
144 
145  public override TableInfo TableInfo {
146  get { return mainTable.TableInfo; }
147  }
148 
149  public bool TypeEquals(IRootTable other) {
150  return this == other;
151  }
152  }
153 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
TableInfo(ObjectName tableName)
Constructs the object with the given table name.
Definition: TableInfo.cs:54
override IEnumerable< int > ResolveRows(int column, IEnumerable< int > rowSet, ITable ancestor)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
override IEnumerator< Row > GetEnumerator()
readonly ColumnIndex[] columnIndexes
CompositeTable(ITable[] composites, CompositeFunction function, bool all)
readonly IList< int >[] rowIndexes
override ColumnIndex GetIndex(int column, int originalColumn, ITable table)
override int IndexOfColumn(ObjectName columnName)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
TableInfo TableInfo
Gets the metadata information of the table, used to resolve the column sources.
Definition: ITable.cs:47
void Add(IRootTable table, IList< int > rowSet)
Definition: RawTableInfo.cs:34
override RawTableInfo GetRawTableInfo(RawTableInfo rootInfo)
int ColumnCount
Gets a count of the columns defined by this object.
Definition: TableInfo.cs:159
CompositeFunction
The kind of composite function in a CompositeTable.
CompositeTable(ITable mainTable, ITable[] composites, CompositeFunction function, bool all)
override ObjectName GetResolvedColumnName(int column)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
Interface that is implemented by all root tables.
Definition: IRootTable.cs:33