DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TableQuery.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;
19 using System.Collections.Generic;
20 using System.Linq.Expressions;
21 
22 using Deveel.Data.Sql;
24 
25 namespace Deveel.Data.Linq {
26  class TableQuery {
27  private readonly List<ColumnQuery> columnQueries;
28 
29  public TableQuery() {
30  columnQueries = new List<ColumnQuery>();
31  }
32 
33  public void Column(string columnName, SqlExpression expression) {
34  columnQueries.Add(new ColumnQuery(columnName, expression));
35  }
36 
37  public void Column(string columnName, Expression expression) {
38  // TODO: convert the source Linq Expression to a SQL Expression
39  throw new NotImplementedException();
40  }
41 
42  public IEnumerable Execute(Type elementType, ITable table) {
43  var mapping = new TableTypeMapper(elementType);
44 
45  var finalTable = table;
46  foreach (var columnQuery in columnQueries) {
47  finalTable = ExecuteColumnQuery(finalTable, columnQuery);
48  }
49 
50  mapping.BuildMap(finalTable);
51 
52  var listType = typeof (List<>).MakeGenericType(elementType);
53  var result = (IList) Activator.CreateInstance(listType);
54 
55  foreach (var row in finalTable) {
56  var rowNumber = row.RowId.RowNumber;
57  var mapped = mapping.Construct(finalTable, rowNumber);
58  result.Add(mapped);
59  }
60 
61  return result;
62  }
63 
64  private ITable ExecuteColumnQuery(ITable table, ColumnQuery columnQuery) {
65  var expression = columnQuery.Expression;
66  return table.ExhaustiveSelect(null, expression);
67  }
68  }
69 }
void Column(string columnName, Expression expression)
Definition: TableQuery.cs:37
void Column(string columnName, SqlExpression expression)
Definition: TableQuery.cs:33
ITable ExecuteColumnQuery(ITable table, ColumnQuery columnQuery)
Definition: TableQuery.cs:64
IEnumerable Execute(Type elementType, ITable table)
Definition: TableQuery.cs:42
Defines the base class for instances that represent SQL expression tree nodes.
readonly List< ColumnQuery > columnQueries
Definition: TableQuery.cs:27