DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TemporaryTable.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 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql.Tables {
24  public class TemporaryTable : BaseDataTable {
25  private readonly TableInfo tableInfo;
26  private int rowCount;
27  private List<DataObject[]> rows;
28 
29  public TemporaryTable(TableInfo tableInfo)
30  : this((IDatabaseContext)null, tableInfo) {
31  }
32 
33  public TemporaryTable(IContext context, TableInfo tableInfo)
34  : base(context) {
35  this.tableInfo = tableInfo.AsReadOnly();
36  rows = new List<DataObject[]>();
37  }
38 
39  public TemporaryTable(string name, TableInfo sourceTableInfo)
40  : this(null, name, sourceTableInfo) {
41  }
42 
43  public TemporaryTable(IDatabaseContext context, string name, TableInfo sourceTableInfo)
44  : this(context, sourceTableInfo.Alias(new ObjectName(name))) {
45  }
46 
47  public TemporaryTable(IDatabaseContext context, string name, IEnumerable<ColumnInfo> columns)
48  : this(context, name, MakeTableInfo(name, columns)) {
49  }
50 
51  public override TableInfo TableInfo {
52  get { return tableInfo; }
53  }
54 
55  public override int RowCount {
56  get { return rowCount; }
57  }
58 
59  public override void Lock() {
60  }
61 
62  public override void Release() {
63  }
64 
65  private static TableInfo MakeTableInfo(string tableName, IEnumerable<ColumnInfo> columns) {
66  var tableInfo = new TableInfo(ObjectName.Parse(tableName));
67  foreach (var columnInfo in columns) {
68  tableInfo.AddColumn(columnInfo);
69  }
70  return tableInfo;
71  }
72 
73  public int NewRow() {
74  rows.Add(new DataObject[ColumnCount]);
75  ++rowCount;
76  return rowCount - 1;
77  }
78 
79  public int NewRow(DataObject[] row) {
80  if (row == null)
81  throw new ArgumentNullException("row");
82  if (row.Length != ColumnCount)
83  throw new ArgumentException();
84 
85  var rowNumber = NewRow();
86  for (int i = 0; i < row.Length; i++) {
87  SetValue(rowNumber, i, row[i]);
88  }
89 
90  return rowNumber;
91  }
92 
93  public override DataObject GetValue(long rowNumber, int columnOffset) {
94  if (rowNumber >= rows.Count)
95  throw new ArgumentOutOfRangeException("rowNumber");
96  if (columnOffset < 0 || columnOffset > ColumnCount)
97  throw new ArgumentOutOfRangeException("columnOffset");
98 
99  var row = rows[(int) rowNumber];
100  return row[columnOffset];
101  }
102 
103  public void SetValue(long rowNumber, int columnOffset, DataObject value) {
104  if (rowNumber < 0 || rowNumber >= rows.Count)
105  throw new ArgumentOutOfRangeException("rowNumber");
106  if (columnOffset < 0 || columnOffset >= ColumnCount)
107  throw new ArgumentOutOfRangeException("columnOffset");
108 
109  var row = rows[(int) rowNumber];
110  row[columnOffset] = value;
111  }
112 
113  public void SetValue(int columnOffset, DataObject value) {
114  SetValue(rowCount - 1, columnOffset, value);
115  }
116 
117  public void BuildIndexes() {
118  BuildIndexes(DefaultIndexTypes.InsertSearch);
119  }
120 
121  public void BuildIndexes(string indexName) {
122  SetupIndexes(indexName);
123 
124  for (int i = 0; i < rowCount; i++) {
125  AddRowToIndex(i);
126  }
127  }
128 
129  public void CopyFrom(ITable table, int row) {
130  NewRow();
131 
132  var columnNames = new ObjectName[table.ColumnCount()];
133  for (int i = 0; i < columnNames.Length; ++i) {
134  columnNames[i] = table.GetResolvedColumnName(i);
135  }
136 
137  for (int i = 0; i < ColumnCount; ++i) {
138  var v = GetResolvedColumnName(i);
139  var colName = v.Name;
140 
141  try {
142  int columnOffset = -1;
143  for (int n = 0; n < columnNames.Length || columnOffset == -1; ++n) {
144  if (columnNames[n].Name.Equals(colName)) {
145  columnOffset = n;
146  }
147  }
148 
149  var value = table.GetValue(row, columnOffset);
150  SetValue(rowCount-1, i, value);
151  } catch (Exception e) {
152  throw new InvalidOperationException(e.Message, e);
153  }
154  }
155  }
156 
157 
158  public override IEnumerator<Row> GetEnumerator() {
159  return new SimpleRowEnumerator(this);
160  }
161 
162  public static TemporaryTable SingleColumnTable(IContext database, string columnName, SqlType columnType) {
163  var tableInfo = new TableInfo(new ObjectName("single"));
164  tableInfo.AddColumn(columnName, columnType);
165  tableInfo = tableInfo.AsReadOnly();
166  return new TemporaryTable(database, tableInfo);
167  }
168  }
169 }
TemporaryTable(IDatabaseContext context, string name, IEnumerable< ColumnInfo > columns)
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
TemporaryTable(IContext context, TableInfo tableInfo)
The context of a single database within a system.
Describes the name of an object within a database.
Definition: ObjectName.cs:44
DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
static TemporaryTable SingleColumnTable(IContext database, string columnName, SqlType columnType)
void AddColumn(ColumnInfo column)
Adds a new column to the table at the last position of the columns list in the table metadata...
Definition: TableInfo.cs:230
TemporaryTable(IDatabaseContext context, string name, TableInfo sourceTableInfo)
TableInfo AsReadOnly()
Creates a new instance of TableInfo as an immutable copy of this table metadata.
Definition: TableInfo.cs:342
static TableInfo MakeTableInfo(string tableName, IEnumerable< ColumnInfo > columns)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
override DataObject GetValue(long rowNumber, int columnOffset)
Gets a single cell within the table that is located at the given column offset and row...
void SetValue(long rowNumber, int columnOffset, DataObject value)
void SetValue(int columnOffset, DataObject value)
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41
TemporaryTable(string name, TableInfo sourceTableInfo)
void CopyFrom(ITable table, int row)
override IEnumerator< Row > GetEnumerator()