KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.interpret.Show 13 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 java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import com.mckoi.database.*;
35 import com.mckoi.database.sql.ParseException;
36 import com.mckoi.util.Stats;
37 import com.mckoi.database.global.Types;
38 import com.mckoi.database.global.StandardMessages;
39 import com.mckoi.database.global.SQLTypes;
40 import com.mckoi.database.jdbc.SQLQuery;
41
42 /**
43  * Statement that handles SHOW and DESCRIBE sql commands.
44  *
45  * @author Tobias Downer
46  */

47
48 public class Show extends Statement {
49
50   // Various show statics,
51
static final int TABLES = 1;
52   static final int STATUS = 2;
53   static final int DESCRIBE_TABLE = 3;
54   static final int CONNECTIONS = 4;
55   static final int PRODUCT = 5;
56   static final int CONNECTION_INFO = 6;
57
58   /**
59    * The name the table that we are to update.
60    */

61   String JavaDoc table_name;
62
63   /**
64    * The type of information that we are to show.
65    */

66   String JavaDoc show_type;
67
68   /**
69    * Arguments of the show statement.
70    */

71   Expression[] args;
72
73   /**
74    * The search expression for the show statement (where clause).
75    */

76   SearchExpression where_clause = new SearchExpression();
77
78   /**
79    * Convenience, creates an empty table with the given column names.
80    */

81   TemporaryTable createEmptyTable(Database d, String JavaDoc name, String JavaDoc[] cols)
82                                                    throws DatabaseException {
83     // Describe the given table...
84
DataTableColumnDef[] fields = new DataTableColumnDef[cols.length];
85     for (int i = 0; i < cols.length; ++i) {
86       fields[i] = DataTableColumnDef.createStringColumn(cols[i]);
87     }
88     TemporaryTable temp_table = new TemporaryTable(d, name, fields);
89     // No entries...
90
temp_table.setupAllSelectableSchemes();
91     return temp_table;
92   }
93
94   // ---------- Implemented from Statement ----------
95

96   public void prepare() throws DatabaseException {
97     // Get the show variables from the query model
98
show_type = (String JavaDoc) cmd.getObject("show");
99     show_type = show_type.toLowerCase();
100     table_name = (String JavaDoc) cmd.getObject("table_name");
101     args = (Expression[]) cmd.getObject("args");
102     where_clause = (SearchExpression) cmd.getObject("where_clause");
103   }
104
105   public Table evaluate() throws DatabaseException {
106
107     DatabaseQueryContext context = new DatabaseQueryContext(database);
108     Database d = database.getDatabase();
109
110     // Construct an executor for interpreting SQL queries inside here.
111
SQLQueryExecutor executor = new SQLQueryExecutor();
112
113     // The table we are showing,
114
TemporaryTable show_table;
115
116     try {
117
118       // How we order the result set
119
int[] order_set = null;
120
121       if (show_type.equals("schema")) {
122
123         SQLQuery query = new SQLQuery(
124            " SELECT \"name\" AS \"schema_name\", " +
125            " \"type\", " +
126            " \"other\" AS \"notes\" " +
127            " FROM SYS_JDBC.ThisUserSchemaInfo " +
128            "ORDER BY \"schema_name\"");
129         return executor.execute(database, query);
130
131       }
132       else if (show_type.equals("tables")) {
133
134         String JavaDoc current_schema = database.getCurrentSchema();
135
136         SQLQuery query = new SQLQuery(
137            " SELECT \"Tables.TABLE_NAME\" AS \"table_name\", " +
138            " I_PRIVILEGE_STRING(\"agg_priv_bit\") AS \"user_privs\", " +
139            " \"Tables.TABLE_TYPE\" as \"table_type\" " +
140            " FROM SYS_JDBC.Tables, " +
141            " ( SELECT AGGOR(\"priv_bit\") agg_priv_bit, " +
142            " \"object\", \"param\" " +
143            " FROM SYS_JDBC.ThisUserSimpleGrant " +
144            " WHERE \"object\" = 1 " +
145            " GROUP BY \"param\" )" +
146            " WHERE \"Tables.TABLE_SCHEM\" = ? " +
147            " AND CONCAT(\"Tables.TABLE_SCHEM\", '.', \"Tables.TABLE_NAME\") = \"param\" " +
148            "ORDER BY \"Tables.TABLE_NAME\"");
149         query.addVar(current_schema);
150
151         return executor.execute(database, query);
152
153       }
154       else if (show_type.equals("status")) {
155
156         SQLQuery query = new SQLQuery(
157            " SELECT \"stat_name\" AS \"name\", " +
158            " \"value\" " +
159            " FROM SYS_INFO.sUSRDatabaseStatistics ");
160
161         return executor.execute(database, query);
162
163       }
164       else if (show_type.equals("describe_table")) {
165
166         TableName tname = resolveTableName(table_name, database);
167         if (!database.tableExists(tname)) {
168           throw new StatementException(
169                               "Unable to find table '" + table_name + "'");
170         }
171
172         SQLQuery query = new SQLQuery(
173           " SELECT \"column\" AS \"name\", " +
174           " i_sql_type(\"type_desc\", \"size\", \"scale\") AS \"type\", " +
175           " \"not_null\", " +
176           " \"index_str\" AS \"index\", " +
177           " \"default\" " +
178           " FROM SYS_JDBC.ThisUserTableColumns " +
179           " WHERE \"schema\" = ? " +
180           " AND \"table\" = ? " +
181           "ORDER BY \"seq_no\" ");
182         query.addVar(tname.getSchema());
183         query.addVar(tname.getName());
184
185         return executor.execute(database, query);
186
187       }
188       else if (show_type.equals("connections")) {
189
190         SQLQuery query = new SQLQuery(
191            "SELECT * FROM SYS_INFO.sUSRCurrentConnections");
192
193         return executor.execute(database, query);
194
195       }
196       else if (show_type.equals("product")) {
197
198         SQLQuery query = new SQLQuery(
199            "SELECT \"name\", \"version\" FROM " +
200            " ( SELECT \"value\" AS \"name\" FROM SYS_INFO.sUSRProductInfo " +
201            " WHERE \"var\" = 'name' ), " +
202            " ( SELECT \"value\" AS \"version\" FROM SYS_INFO.sUSRProductInfo " +
203            " WHERE \"var\" = 'version' ) "
204         );
205
206         return executor.execute(database, query);
207
208       }
209       else if (show_type.equals("connection_info")) {
210
211         SQLQuery query = new SQLQuery(
212            "SELECT * FROM SYS_INFO.sUSRConnectionInfo"
213         );
214
215         return executor.execute(database, query);
216
217       }
218
219       else if (show_type.equals("jdbc_procedures")) {
220         // Need implementing?
221
show_table = createEmptyTable(d, "JDBCProcedures",
222             new String JavaDoc[] { "PROCEDURE_CAT", "PROCEDURE_SCHEM", "PROCEDURE_NAME",
223                            "R1", "R2", "R3", "REMARKS", "PROCEDURE_TYPE" });
224       }
225
226       else if (show_type.equals("jdbc_procedure_columns")) {
227         // Need implementing?
228
show_table = createEmptyTable(d, "JDBCProcedureColumns",
229             new String JavaDoc[] { "PROCEDURE_CAT", "PROCEDURE_SCHEM", "PROCEDURE_NAME",
230                            "COLUMN_NAME", "COLUMN_TYPE", "DATA_TYPE",
231                            "TYPE_NAME", "PRECISION", "LENGTH", "SCALE",
232                            "RADIX", "NULLABLE", "REMARKS" });
233       }
234
235       else if (show_type.equals("jdbc_catalogs")) {
236         // Need implementing?
237
show_table = createEmptyTable(d, "JDBCCatalogs",
238                                       new String JavaDoc[] { "TABLE_CAT" });
239       }
240
241       else if (show_type.equals("jdbc_table_types")) {
242         // Describe the given table...
243
DataTableColumnDef[] fields = new DataTableColumnDef[1];
244         fields[0] = DataTableColumnDef.createStringColumn("TABLE_TYPE");
245
246         TemporaryTable temp_table =
247                            new TemporaryTable(d, "JDBCTableTypes", fields);
248         String JavaDoc[] supported_types = {
249             "TABLE", "VIEW", "SYSTEM TABLE",
250             "TRIGGER", "FUNCTION", "SEQUENCE" };
251         for (int i = 0; i < supported_types.length; ++i) {
252           temp_table.newRow();
253           temp_table.setRowObject(TObject.stringVal(supported_types[i]),
254                                   "JDBCTableTypes.TABLE_TYPE");
255         }
256         temp_table.setupAllSelectableSchemes();
257         show_table = temp_table;
258         order_set = new int[] { 0 };
259       }
260
261       else if (show_type.equals("jdbc_best_row_identifier")) {
262         // Need implementing?
263
show_table = createEmptyTable(d, "JDBCBestRowIdentifier",
264               new String JavaDoc[] { "SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
265                 "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS",
266                 "PSEUDO_COLUMN" });
267       }
268
269       else if (show_type.equals("jdbc_version_columns")) {
270         // Need implementing?
271
show_table = createEmptyTable(d, "JDBCVersionColumn",
272               new String JavaDoc[] { "SCOPE", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
273                 "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS",
274                 "PSEUDO_COLUMN" });
275       }
276
277       else if (show_type.equals("jdbc_index_info")) {
278         // Need implementing?
279
show_table = createEmptyTable(d, "JDBCIndexInfo",
280               new String JavaDoc[] { "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME",
281                 "NON_UNIQUE", "INDEX_QUALIFIER", "INDEX_NAME", "TYPE",
282                 "ORDINAL_POSITION", "COLUMN_NAME", "ASC_OR_DESC",
283                 "CARDINALITY", "PAGES", "FILTER_CONDITION"
284               });
285       }
286
287       else {
288         throw new StatementException("Unknown SHOW identifier: " + show_type);
289       }
290
291     }
292     catch (SQLException JavaDoc e) {
293       throw new DatabaseException("SQL Error: " + e.getMessage());
294     }
295     catch (ParseException e) {
296       throw new DatabaseException("Parse Error: " + e.getMessage());
297     }
298     catch (TransactionException e) {
299       throw new DatabaseException("Transaction Error: " + e.getMessage());
300     }
301
302     return show_table;
303
304   }
305
306
307 }
308
Popular Tags