KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > SubQuery


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import org.hsqldb.lib.ObjectComparator;
35
36 /**
37  * Represents an SQL view or anonymous subquery (inline virtual table
38  * descriptor) nested within an SQL statement. <p>
39  *
40  * Implements {@link org.hsqldb.lib.ObjectComparator ObjectComparator} to
41  * provide the correct order of materialization for nested views / subqueries.
42  *
43  * @author boucherb@users
44  * @author fredt@users
45  */

46 class SubQuery implements ObjectComparator {
47
48     int level;
49     boolean hasParams;
50     boolean isResolved;
51     boolean isExistsPredicate;
52     boolean uniqueRows;
53     Select select;
54     Table table;
55     View view;
56     boolean isMaterialised;
57
58     void populateTable(Session session) throws HsqlException {
59
60         Result r = select.getResult(session, isExistsPredicate ? 1
61                                                                : 0);
62
63         if (uniqueRows) {
64             r.removeDuplicates(session, select.iResultLen);
65         }
66
67         table.insertResult(session, r);
68     }
69
70     /**
71      * This results in the following sort order:
72      *
73      * view subqueries, then other subqueries
74      *
75      * view subqueries:
76      * views sorted by creation order (earlier declaration first)
77      *
78      * other subqueries:
79      * subqueries sorted by depth within select query (deep == higher level)
80      *
81      */

82     public int compare(Object JavaDoc a, Object JavaDoc b) {
83
84         SubQuery sqa = (SubQuery) a;
85         SubQuery sqb = (SubQuery) b;
86
87         if (sqa.view == null && sqb.view == null) {
88             return sqb.level - sqa.level;
89         } else if (sqa.view != null && sqb.view != null) {
90             Database db = sqa.view.database;
91             int ia = db.schemaManager.getTableIndex(sqa.view);
92             int ib = db.schemaManager.getTableIndex(sqb.view);
93
94             if (ia == -1) {
95                 ia = db.schemaManager.getTables(
96                     sqa.view.getSchemaName()).size();
97             }
98
99             if (ib == -1) {
100                 ib = db.schemaManager.getTables(
101                     sqb.view.getSchemaName()).size();
102             }
103
104             int diff = ia - ib;
105
106             return diff == 0 ? sqb.level - sqa.level
107                              : diff;
108         } else {
109             return sqa.view == null ? 1
110                                     : -1;
111         }
112     }
113 }
114
Popular Tags