DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SubsetColumnTable.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 {
24  private readonly int[] columnMap;
25  private int[] reverseColumnMap;
27  private readonly ObjectName[] aliases;
28 
29  public SubsetColumnTable(ITable parent, int[] columnMap, ObjectName[] aliases)
30  : base(parent) {
31  this.aliases = aliases;
32  this.columnMap = columnMap;
33 
34  SetColumnMap(columnMap);
35  }
36 
37  private void SetColumnMap(int[] mapping) {
38  reverseColumnMap = new int[Parent.ColumnCount()];
39  for (int i = 0; i < reverseColumnMap.Length; ++i) {
40  reverseColumnMap[i] = -1;
41  }
42 
43  var parentInfo = Parent.TableInfo;
44  subsetTableInfo = new TableInfo(parentInfo.TableName);
45 
46  for (int i = 0; i < mapping.Length; ++i) {
47  int mapTo = mapping[i];
48 
49  var origColumnInfo = Parent.TableInfo[mapTo];
50  var columnInfo = new ColumnInfo(aliases[i].Name, origColumnInfo.ColumnType) {
51  DefaultExpression = origColumnInfo.DefaultExpression,
52  IsNotNull = origColumnInfo.IsNotNull,
53  IndexType = origColumnInfo.IndexType
54  };
55 
56  subsetTableInfo.AddColumnSafe(columnInfo);
57 
58  reverseColumnMap[mapTo] = i;
59  }
60 
61  subsetTableInfo = subsetTableInfo.AsReadOnly();
62  }
63 
64  protected override int ColumnCount {
65  get { return aliases.Length; }
66  }
67 
68  public override TableInfo TableInfo {
69  get { return subsetTableInfo; }
70  }
71 
72  protected override int IndexOfColumn(ObjectName columnName) {
73  for (int i = 0; i < aliases.Length; ++i) {
74  if (columnName.Equals(aliases[i])) {
75  return i;
76  }
77  }
78  return -1;
79  }
80 
81  protected override ObjectName GetResolvedColumnName(int column) {
82  return aliases[column];
83  }
84 
85  protected override ColumnIndex GetIndex(int column, int originalColumn, ITable table) {
86  // We need to map the original_column if the original column is a reference
87  // in this subset column table. Otherwise we leave as is.
88  // The reason is because FilterTable pretends the call came from its
89  // parent if a request is made on this table.
90  int mappedOriginalColumn = originalColumn;
91  if (table == this) {
92  mappedOriginalColumn = columnMap[originalColumn];
93  }
94 
95  return base.GetIndex(columnMap[column], mappedOriginalColumn, table);
96  }
97 
98  protected override IEnumerable<int> ResolveRows(int column, IEnumerable<int> rowSet, ITable ancestor) {
99  return base.ResolveRows(columnMap[column], rowSet, ancestor);
100  }
101 
102  public override DataObject GetValue(long rowNumber, int columnOffset) {
103  return Parent.GetValue(rowNumber, columnMap[columnOffset]);
104  }
105 
106  public bool TypeEquals(IRootTable other) {
107  return this == other;
108  }
109  }
110 }
Defines the metadata properties of a column within a table of a database.
Definition: ColumnInfo.cs:36
override int IndexOfColumn(ObjectName columnName)
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
override ColumnIndex GetIndex(int column, int originalColumn, ITable table)
SubsetColumnTable(ITable parent, int[] columnMap, ObjectName[] aliases)
void AddColumnSafe(ColumnInfo column)
Definition: TableInfo.cs:203
Describes the name of an object within a database.
Definition: ObjectName.cs:44
override bool Equals(object obj)
Definition: ObjectName.cs:241
TableInfo AsReadOnly()
Creates a new instance of TableInfo as an immutable copy of this table metadata.
Definition: TableInfo.cs:342
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
override IEnumerable< int > ResolveRows(int column, IEnumerable< int > rowSet, ITable ancestor)
SqlExpression DefaultExpression
Gets or sets a SqlExpression used as a DEFAULT when a constraint for the column is to SET DEFAULT...
Definition: ColumnInfo.cs:151
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
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
override ObjectName GetResolvedColumnName(int column)