001: /**
002: * com.mckoi.database.interpret.Show 13 Sep 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database.interpret;
024:
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.util.Date;
028: import java.util.Properties;
029: import java.util.Arrays;
030: import java.util.Collections;
031: import java.sql.SQLException;
032: import com.mckoi.database.*;
033: import com.mckoi.database.sql.ParseException;
034: import com.mckoi.util.Stats;
035: import com.mckoi.database.global.Types;
036: import com.mckoi.database.global.StandardMessages;
037: import com.mckoi.database.global.SQLTypes;
038: import com.mckoi.database.jdbc.SQLQuery;
039:
040: /**
041: * Statement that handles SHOW and DESCRIBE sql commands.
042: *
043: * @author Tobias Downer
044: */
045:
046: public class Show extends Statement {
047:
048: // Various show statics,
049: static final int TABLES = 1;
050: static final int STATUS = 2;
051: static final int DESCRIBE_TABLE = 3;
052: static final int CONNECTIONS = 4;
053: static final int PRODUCT = 5;
054: static final int CONNECTION_INFO = 6;
055:
056: /**
057: * The name the table that we are to update.
058: */
059: String table_name;
060:
061: /**
062: * The type of information that we are to show.
063: */
064: String show_type;
065:
066: /**
067: * Arguments of the show statement.
068: */
069: Expression[] args;
070:
071: /**
072: * The search expression for the show statement (where clause).
073: */
074: SearchExpression where_clause = new SearchExpression();
075:
076: /**
077: * Convenience, creates an empty table with the given column names.
078: */
079: TemporaryTable createEmptyTable(Database d, String name,
080: String[] cols) throws DatabaseException {
081: // Describe the given table...
082: DataTableColumnDef[] fields = new DataTableColumnDef[cols.length];
083: for (int i = 0; i < cols.length; ++i) {
084: fields[i] = DataTableColumnDef.createStringColumn(cols[i]);
085: }
086: TemporaryTable temp_table = new TemporaryTable(d, name, fields);
087: // No entries...
088: temp_table.setupAllSelectableSchemes();
089: return temp_table;
090: }
091:
092: // ---------- Implemented from Statement ----------
093:
094: public void prepare() throws DatabaseException {
095: // Get the show variables from the query model
096: show_type = (String) cmd.getObject("show");
097: show_type = show_type.toLowerCase();
098: table_name = (String) cmd.getObject("table_name");
099: args = (Expression[]) cmd.getObject("args");
100: where_clause = (SearchExpression) cmd.getObject("where_clause");
101: }
102:
103: public Table evaluate() throws DatabaseException {
104:
105: DatabaseQueryContext context = new DatabaseQueryContext(
106: database);
107: Database d = database.getDatabase();
108:
109: // Construct an executor for interpreting SQL queries inside here.
110: SQLQueryExecutor executor = new SQLQueryExecutor();
111:
112: // The table we are showing,
113: TemporaryTable show_table;
114:
115: try {
116:
117: // How we order the result set
118: int[] order_set = null;
119:
120: if (show_type.equals("schema")) {
121:
122: SQLQuery query = new SQLQuery(
123: " SELECT \"name\" AS \"schema_name\", "
124: + " \"type\", "
125: + " \"other\" AS \"notes\" "
126: + " FROM SYS_JDBC.ThisUserSchemaInfo "
127: + "ORDER BY \"schema_name\"");
128: return executor.execute(database, query);
129:
130: } else if (show_type.equals("tables")) {
131:
132: String current_schema = database.getCurrentSchema();
133:
134: SQLQuery query = new SQLQuery(
135: " SELECT \"Tables.TABLE_NAME\" AS \"table_name\", "
136: + " I_PRIVILEGE_STRING(\"agg_priv_bit\") AS \"user_privs\", "
137: + " \"Tables.TABLE_TYPE\" as \"table_type\" "
138: + " FROM SYS_JDBC.Tables, "
139: + " ( SELECT AGGOR(\"priv_bit\") agg_priv_bit, "
140: + " \"object\", \"param\" "
141: + " FROM SYS_JDBC.ThisUserSimpleGrant "
142: + " WHERE \"object\" = 1 "
143: + " GROUP BY \"param\" )"
144: + " WHERE \"Tables.TABLE_SCHEM\" = ? "
145: + " AND CONCAT(\"Tables.TABLE_SCHEM\", '.', \"Tables.TABLE_NAME\") = \"param\" "
146: + "ORDER BY \"Tables.TABLE_NAME\"");
147: query.addVar(current_schema);
148:
149: return executor.execute(database, query);
150:
151: } else if (show_type.equals("status")) {
152:
153: SQLQuery query = new SQLQuery(
154: " SELECT \"stat_name\" AS \"name\", "
155: + " \"value\" "
156: + " FROM SYS_INFO.sUSRDatabaseStatistics ");
157:
158: return executor.execute(database, query);
159:
160: } else if (show_type.equals("describe_table")) {
161:
162: TableName tname = resolveTableName(table_name, database);
163: if (!database.tableExists(tname)) {
164: throw new StatementException(
165: "Unable to find table '" + table_name + "'");
166: }
167:
168: SQLQuery query = new SQLQuery(
169: " SELECT \"column\" AS \"name\", "
170: + " i_sql_type(\"type_desc\", \"size\", \"scale\") AS \"type\", "
171: + " \"not_null\", "
172: + " \"index_str\" AS \"index\", "
173: + " \"default\" "
174: + " FROM SYS_JDBC.ThisUserTableColumns "
175: + " WHERE \"schema\" = ? "
176: + " AND \"table\" = ? "
177: + "ORDER BY \"seq_no\" ");
178: query.addVar(tname.getSchema());
179: query.addVar(tname.getName());
180:
181: return executor.execute(database, query);
182:
183: } else if (show_type.equals("connections")) {
184:
185: SQLQuery query = new SQLQuery(
186: "SELECT * FROM SYS_INFO.sUSRCurrentConnections");
187:
188: return executor.execute(database, query);
189:
190: } else if (show_type.equals("product")) {
191:
192: SQLQuery query = new SQLQuery(
193: "SELECT \"name\", \"version\" FROM "
194: + " ( SELECT \"value\" AS \"name\" FROM SYS_INFO.sUSRProductInfo "
195: + " WHERE \"var\" = 'name' ), "
196: + " ( SELECT \"value\" AS \"version\" FROM SYS_INFO.sUSRProductInfo "
197: + " WHERE \"var\" = 'version' ) ");
198:
199: return executor.execute(database, query);
200:
201: } else if (show_type.equals("connection_info")) {
202:
203: SQLQuery query = new SQLQuery(
204: "SELECT * FROM SYS_INFO.sUSRConnectionInfo");
205:
206: return executor.execute(database, query);
207:
208: }
209:
210: else if (show_type.equals("jdbc_procedures")) {
211: // Need implementing?
212: show_table = createEmptyTable(d, "JDBCProcedures",
213: new String[] { "PROCEDURE_CAT",
214: "PROCEDURE_SCHEM", "PROCEDURE_NAME",
215: "R1", "R2", "R3", "REMARKS",
216: "PROCEDURE_TYPE" });
217: }
218:
219: else if (show_type.equals("jdbc_procedure_columns")) {
220: // Need implementing?
221: show_table = createEmptyTable(d,
222: "JDBCProcedureColumns",
223: new String[] { "PROCEDURE_CAT",
224: "PROCEDURE_SCHEM", "PROCEDURE_NAME",
225: "COLUMN_NAME", "COLUMN_TYPE",
226: "DATA_TYPE", "TYPE_NAME", "PRECISION",
227: "LENGTH", "SCALE", "RADIX", "NULLABLE",
228: "REMARKS" });
229: }
230:
231: else if (show_type.equals("jdbc_catalogs")) {
232: // Need implementing?
233: show_table = createEmptyTable(d, "JDBCCatalogs",
234: new String[] { "TABLE_CAT" });
235: }
236:
237: else if (show_type.equals("jdbc_table_types")) {
238: // Describe the given table...
239: DataTableColumnDef[] fields = new DataTableColumnDef[1];
240: fields[0] = DataTableColumnDef
241: .createStringColumn("TABLE_TYPE");
242:
243: TemporaryTable temp_table = new TemporaryTable(d,
244: "JDBCTableTypes", fields);
245: String[] supported_types = { "TABLE", "VIEW",
246: "SYSTEM TABLE", "TRIGGER", "FUNCTION",
247: "SEQUENCE" };
248: for (int i = 0; i < supported_types.length; ++i) {
249: temp_table.newRow();
250: temp_table.setRowObject(TObject
251: .stringVal(supported_types[i]),
252: "JDBCTableTypes.TABLE_TYPE");
253: }
254: temp_table.setupAllSelectableSchemes();
255: show_table = temp_table;
256: order_set = new int[] { 0 };
257: }
258:
259: else if (show_type.equals("jdbc_best_row_identifier")) {
260: // Need implementing?
261: show_table = createEmptyTable(d,
262: "JDBCBestRowIdentifier", new String[] {
263: "SCOPE", "COLUMN_NAME", "DATA_TYPE",
264: "TYPE_NAME", "COLUMN_SIZE",
265: "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[] { "SCOPE", "COLUMN_NAME",
273: "DATA_TYPE", "TYPE_NAME",
274: "COLUMN_SIZE", "BUFFER_LENGTH",
275: "DECIMAL_DIGITS", "PSEUDO_COLUMN" });
276: }
277:
278: else if (show_type.equals("jdbc_index_info")) {
279: // Need implementing?
280: show_table = createEmptyTable(d, "JDBCIndexInfo",
281: new String[] { "TABLE_CAT", "TABLE_SCHEM",
282: "TABLE_NAME", "NON_UNIQUE",
283: "INDEX_QUALIFIER", "INDEX_NAME",
284: "TYPE", "ORDINAL_POSITION",
285: "COLUMN_NAME", "ASC_OR_DESC",
286: "CARDINALITY", "PAGES",
287: "FILTER_CONDITION" });
288: }
289:
290: else {
291: throw new StatementException(
292: "Unknown SHOW identifier: " + show_type);
293: }
294:
295: } catch (SQLException e) {
296: throw new DatabaseException("SQL Error: " + e.getMessage());
297: } catch (ParseException e) {
298: throw new DatabaseException("Parse Error: "
299: + e.getMessage());
300: } catch (TransactionException e) {
301: throw new DatabaseException("Transaction Error: "
302: + e.getMessage());
303: }
304:
305: return show_table;
306:
307: }
308:
309: }
|