DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
OuterTable.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 
22 namespace Deveel.Data.Sql.Tables {
23  internal class OuterTable : VirtualTable {
24  private readonly IList<int>[] outerRows;
25  private int outerRowCount;
26 
27  private OuterTable(IEnumerable<ITable> tables, IList<IList<int>> rows)
28  : base(tables, rows) {
29  outerRows = new IList<int>[rows.Count];
30  }
31 
32  public static OuterTable Create(ITable inputTable) {
33  var baseTable = inputTable.GetRawTableInfo();
34  var tables = baseTable.GetTables();
35  var rows = baseTable.GetRows();
36 
37  return new OuterTable(tables, rows);
38  }
39 
40  public override int RowCount {
41  get { return base.RowCount + outerRowCount; }
42  }
43 
44  public void MergeIn(ITable outsideTable) {
45  var rawTableInfo = outsideTable.GetRawTableInfo();
46 
47  // Get the base information,
48  var baseTables = ReferenceTables;
49  IList<int>[] baseRows = ReferenceRows;
50 
51  // The tables and rows being merged in.
52  var tables = rawTableInfo.GetTables();
53  var rows = rawTableInfo.GetRows();
54  // The number of rows being merged in.
55  outerRowCount = rows[0].Count;
56 
57  for (int i = 0; i < baseTables.Length; ++i) {
58  var btable = baseTables[i];
59  int index = -1;
60  for (int n = 0; n < tables.Length && index == -1; ++n) {
61  if (btable == tables[n]) {
62  index = n;
63  }
64  }
65 
66  // If the table wasn't found, then set 'NULL' to this base_table
67  if (index == -1) {
68  outerRows[i] = null;
69  } else {
70  List<int> list = new List<int>(outerRowCount);
71  outerRows[i] = list;
72  // Merge in the rows from the input table,
73  IList<int> toMerge = rows[index];
74  if (toMerge.Count != outerRowCount)
75  throw new InvalidOperationException("Wrong size for rows being merged in.");
76 
77  list.AddRange(toMerge);
78  }
79  }
80  }
81 
82  protected override ColumnIndex GetIndex(int column, int originalColumn, ITable table) {
83  if (ColumnIndices[column] == null) {
84  // EFFICIENCY: We implement this with a blind search...
85  var index = new BlindSearchIndex(this, column);
86  ColumnIndices[column] = index.GetSubset(this, column);
87  }
88 
89  if (table == this)
90  return ColumnIndices[column];
91 
92  return ColumnIndices[column].GetSubset(table, originalColumn);
93  }
94 
95  public override DataObject GetValue(long rowNumber, int columnOffset) {
96  int tableNum = ColumnTable[columnOffset];
97  var parentTable = ReferenceTables[tableNum];
98 
99  if (rowNumber >= outerRowCount) {
100  rowNumber = ReferenceRows[tableNum][(int)rowNumber - outerRowCount];
101  return parentTable.GetValue(rowNumber, ColumnFilter[columnOffset]);
102  }
103 
104  if (outerRows[tableNum] == null)
105  // Special case, handling outer entries (NULL)
106  return new DataObject(TableInfo[columnOffset].ColumnType, null);
107 
108  rowNumber = outerRows[tableNum][(int)rowNumber];
109  return parentTable.GetValue(rowNumber, ColumnFilter[columnOffset]);
110  }
111  }
112 }
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...
Definition: OuterTable.cs:95
OuterTable(IEnumerable< ITable > tables, IList< IList< int >> rows)
Definition: OuterTable.cs:27
static OuterTable Create(ITable inputTable)
Definition: OuterTable.cs:32
override ColumnIndex GetIndex(int column, int originalColumn, ITable table)
Definition: OuterTable.cs:82
void MergeIn(ITable outsideTable)
Definition: OuterTable.cs:44
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
readonly IList< int >[] outerRows
Definition: OuterTable.cs:24
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41