KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > order > OrderPlanMerger


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.order;
2
3
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
8 import com.daffodilwoods.database.resource.*;
9 import com.daffodilwoods.database.sqlinitiator.*;
10
11 /**
12  * OrderPlanMerger Class provides functionality for merging for several type of
13  * OrderPlan i.e. it gives the Utility for merging of OrderPlans of groupByClause , orderBy and distinct clause.
14  * <p>Title: </p>
15  * <p>Description: </p>
16  * <p>Copyright: Copyright (c) 2003</p>
17  * <p>Company: </p>
18  * @author unascribed
19  * @version 1.0
20  */

21 public class OrderPlanMerger {
22
23    /**
24     * It is required to get resultant orderPlan by merging all passed singletablelevel
25     * Order.
26     * @param orderPlan1 orderPlan of GroupBy Clause
27     * @param orderPlan2 orderPlan of orderBy Clause
28     * @return mergedOrderPlan
29     * @throws DException
30     */

31
32    public static _Order getOrder(_SingleTableOrderPlan[] stop) throws DException {
33       _Order selectOrder = null;
34       for (int i = 0; i < stop.length; i++) {
35          _Order order = stop[i].getOrder();
36          selectOrder = GeneralPurposeStaticClass.getJoinOrdered(selectOrder, order);
37       }
38       return selectOrder;
39    }
40    /**
41     * This method is needed to get the combined order from passed single table
42     * order plans. It is required when single table order plan can't be shifted
43     * to single table and is used at join level or group by level.
44     * @param stop
45     * @return
46     * @throws DException
47     */

48    public static _Order getOrderFromSingleTableOrderPlan(_SingleTableOrderPlan[] stop) throws DException {
49       int length = stop.length;
50       _Order selectOrder = null;
51       if (length == 1) {
52          return stop[0].getOrder();
53       }
54       for (int i = 0; i < length; i++) {
55          _Order order = stop[i].getOrder();
56          if (order != null) {
57             selectOrder = GeneralPurposeStaticClass.getJoinOrdered(selectOrder, order);
58          }
59       }
60       return selectOrder;
61    }
62
63    /**
64     * Creates the SingleTablePlanLevel Order Plans Object from the Orders Available
65     * SingleTableOrdePlans created are grouped according to the tableName
66     * @param orders - Orders which are solvable at SingleTablePlan Level
67     * @return
68     * @throws DException
69     */

70    public static _SingleTableOrderPlan[] getSingleTableOrderPlans(_Order order) throws DException {
71       ColumnDetails[] orderColumns = order.getKeyColumnDetails();
72       TableDetails tt = checkForSingleTable(orderColumns);
73       if (tt != null) {
74          SingleTableOrderPlan stop = new SingleTableOrderPlan();
75          stop.setOrder(order);
76          stop.setTableDetails(tt);
77          return new _SingleTableOrderPlan[] {stop};
78       }
79       boolean[] orderSpecification = order.getOrderOfColumns();
80       ArrayList result = new ArrayList(4);
81       int previousIndex = 0;
82       TableDetails tableName1 = orderColumns[previousIndex].getTable();
83       for (int i = 1, length = orderColumns.length; i < length; i++) {
84          TableDetails tableName2 = orderColumns[i].getTable();
85          if (tableName1 != tableName2) {
86             result.add(getSingleTableOrderPlan(previousIndex, i, orderColumns, orderSpecification));
87             previousIndex = i;
88             tableName1 = tableName2;
89          }
90       }
91       result.add(getSingleTableOrderPlan(previousIndex, orderColumns.length, orderColumns, orderSpecification));
92       return (_SingleTableOrderPlan[]) result.toArray(new _SingleTableOrderPlan[result.size()]);
93    }
94
95    private static TableDetails checkForSingleTable(ColumnDetails[] cd) throws DException {
96       int length = cd.length;
97       TableDetails table = cd[0].getTable();
98       for (int i = 1; i < length; i++) {
99          if (table != cd[i].getTable()) {
100             return null;
101          }
102       }
103       return table;
104    }
105
106    /**
107     * Creates the SingleTableOrderPlan from previousIndex to current Index passed with the help of Columns
108     * and boolean array passed.
109     * @param previousIndex
110     * @param currentIndex
111     * @param columnDetails array
112     * @param orderSpecification array
113     * @throws DException
114     */

115    private static _SingleTableOrderPlan getSingleTableOrderPlan(int previousIndex, int currentIndex, ColumnDetails[] orderColumns, boolean[] orderSpecification) throws DException {
116       ColumnDetails[] singleTableOrderColumns = new ColumnDetails[currentIndex - previousIndex];
117       boolean[] singleTableOrderSpecification = new boolean[currentIndex - previousIndex];
118       SingleTableOrderPlan stop = new SingleTableOrderPlan();
119       for (int i = previousIndex, j = 0; i < currentIndex; i++, j++) {
120          singleTableOrderColumns[j] = orderColumns[i];
121          singleTableOrderSpecification[j] = orderSpecification[i];
122       }
123       stop.setOrder(new SelectOrder(singleTableOrderColumns, singleTableOrderSpecification));
124       stop.setTableDetails(orderColumns[previousIndex].getTable());
125       return stop;
126    }
127 }
128
Popular Tags