KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > Select


1 /**
2  * com.mckoi.database.interpret.Select 09 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import com.mckoi.debug.*;
29 import com.mckoi.util.IntegerVector;
30 import java.util.Set JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collections JavaDoc;
36
37 /**
38  * Logic for interpreting an SQL SELECT statement.
39  *
40  * @author Tobias Downer
41  */

42
43 public class Select extends Statement {
44
45   /**
46    * The TableSelectExpression representing the select query itself.
47    */

48   private TableSelectExpression select_expression;
49
50   /**
51    * The list of all columns to order by. (ByColumn)
52    */

53   private ArrayList JavaDoc order_by;
54
55   /**
56    * The list of columns in the 'order_by' clause fully resolved.
57    */

58   private Variable[] order_cols;
59
60   /**
61    * The plan for evaluating this select expression.
62    */

63   private QueryPlanNode plan;
64
65
66
67   /**
68    * Checks the permissions for this user to determine if they are allowed to
69    * select (read) from tables in this plan. If the user is not allowed to
70    * select from a table in the plan, a UserAccessException is thrown. This is
71    * a static method.
72    */

73   static final void checkUserSelectPermissions(
74             DatabaseQueryContext context, User user, QueryPlanNode plan)
75                                throws UserAccessException, DatabaseException {
76   
77     // Discover the list of TableName objects this query touches,
78
ArrayList JavaDoc touched_tables = plan.discoverTableNames(new ArrayList JavaDoc());
79     Database dbase = context.getDatabase();
80     // Check that the user is allowed to select from these tables.
81
for (int i = 0; i < touched_tables.size(); ++i) {
82       TableName t = (TableName) touched_tables.get(i);
83       if (!dbase.canUserSelectFromTableObject(context, user, t, null)) {
84         throw new UserAccessException(
85                              "User not permitted to select from table: " + t);
86       }
87     }
88   }
89
90   /**
91    * Prepares the select statement with a Database object. This sets up
92    * internal state so that it correctly maps to a database. Also, this
93    * checks format to ensure there are no run-time syntax problems. This must
94    * be called because we 'evaluate' the statement.
95    * <p>
96    * NOTE: Care must be taken to ensure that all methods called here are safe
97    * in as far as modifications to the data occuring. The rules for
98    * safety should be as follows. If the database is in EXCLUSIVE mode,
99    * then we need to wait until it's switched back to SHARED mode
100    * before this method is called.
101    * All collection of information done here should not involve any table
102    * state info. except for column count, column names, column types, etc.
103    * Queries such as obtaining the row count, selectable scheme information,
104    * and certainly 'getCellContents' must never be called during prepare.
105    * When prepare finishes, the affected tables are locked and the query ia
106    * safe to 'evaluate' at which time table state is safe to inspect.
107    */

108   public void prepare() throws DatabaseException {
109     DatabaseConnection db = database;
110
111     // Prepare this object from the StatementTree,
112
// The select expression itself
113
select_expression =
114                   (TableSelectExpression) cmd.getObject("table_expression");
115     // The order by information
116
order_by = (ArrayList JavaDoc) cmd.getObject("order_by");
117
118     // Generate the TableExpressionFromSet hierarchy for the expression,
119
TableExpressionFromSet from_set =
120                              Planner.generateFromSet(select_expression, db);
121
122     // Form the plan
123
plan = Planner.formQueryPlan(db, select_expression, from_set, order_by);
124     
125   }
126
127
128   /**
129    * Evaluates the select statement with the given Database context.
130    */

131   public Table evaluate() throws DatabaseException {
132
133     DatabaseQueryContext context = new DatabaseQueryContext(database);
134
135     // Check the permissions for this user to select from the tables in the
136
// given plan.
137
checkUserSelectPermissions(context, user, plan);
138
139     boolean error = true;
140     try {
141       Table t = plan.evaluate(context);
142       error = false;
143       return t;
144     }
145     finally {
146       // If an error occured, dump the query plan to the debug log.
147
// Or just dump the query plan if debug level = INFORMATION
148
if (Debug().isInterestedIn(Lvl.INFORMATION) ||
149           (error && Debug().isInterestedIn(Lvl.WARNING))) {
150         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
151         plan.debugString(0, buf);
152
153         Debug().write(Lvl.WARNING, this,
154                     "Query Plan debug:\n" +
155                     buf.toString());
156       }
157     }
158
159   }
160
161
162   /**
163    * Outputs information for debugging.
164    */

165   public String JavaDoc toString() {
166     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
167     buf.append("[ SELECT: expression=");
168     buf.append(select_expression.toString());
169     buf.append(" ORDER_BY=");
170     buf.append(order_by);
171     buf.append(" ]");
172     return new String JavaDoc(buf);
173   }
174
175 }
176
Popular Tags