DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SortNode.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 
19 using Deveel.Data;
21 using Deveel.Data.Sql.Tables;
22 
23 namespace Deveel.Data.Sql.Query {
24  [Serializable]
26  public SortNode(IQueryPlanNode child, ObjectName[] columnNames, bool[] ascending)
27  : base(child) {
28 
29  // How we handle ascending/descending order
30  // ----------------------------------------
31  // Internally to the database, all columns are naturally ordered in
32  // ascending order (start at lowest and end on highest). When a column
33  // is ordered in descending order, a fast way to achieve this is to take
34  // the ascending set and reverse it. This works for single columns,
35  // however some thought is required for handling multiple column. We
36  // order columns from RHS to LHS. If LHS is descending then this will
37  // order the RHS incorrectly if we leave as is. Therefore, we must do
38  // some pre-processing that looks ahead on any descending orders and
39  // reverses the order of the columns to the right. This pre-processing
40  // is done in the first pass.
41 
42  int sz = ascending.Length;
43  for (int n = 0; n < sz - 1; ++n) {
44  if (!ascending[n]) { // if descending...
45  // Reverse order of all columns to the right...
46  for (int p = n + 1; p < sz; ++p) {
47  ascending[p] = !ascending[p];
48  }
49  }
50  }
51 
52  ColumnNames = columnNames;
53  Ascending = ascending;
54  }
55 
56  private SortNode(ObjectData data)
57  : base(data) {
58  ColumnNames = data.GetValue<ObjectName[]>("Columns");
59  Ascending = data.GetValue<bool[]>("Ascending");
60  }
61 
62  public ObjectName[] ColumnNames { get; private set; }
63 
64  public bool[] Ascending { get; private set; }
65 
66  public override ITable Evaluate(IRequest context) {
67  var t = Child.Evaluate(context);
68  return t.OrderBy(ColumnNames, Ascending);
69  }
70 
71  protected override void GetData(SerializeData data) {
72  data.SetValue("Columns", ColumnNames);
73  data.SetValue("Ascending", Ascending);
74  }
75  }
76 }
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
A IQueryPlanNode with a single child.
SortNode(IQueryPlanNode child, ObjectName[] columnNames, bool[] ascending)
Definition: SortNode.cs:26
void SetValue(string key, Type type, object value)
Describes the name of an object within a database.
Definition: ObjectName.cs:44
SortNode(ObjectData data)
Definition: SortNode.cs:56
A node element of a query plan tree. /summary>
override ITable Evaluate(IRequest context)
Definition: SortNode.cs:66
override void GetData(SerializeData data)
Definition: SortNode.cs:71