1 24 25 package com.mckoi.database.interpret; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.Properties ; 31 import java.util.Arrays ; 32 import java.util.Collections ; 33 import java.sql.SQLException ; 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 47 48 public class Show extends Statement { 49 50 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 61 String table_name; 62 63 66 String show_type; 67 68 71 Expression[] args; 72 73 76 SearchExpression where_clause = new SearchExpression(); 77 78 81 TemporaryTable createEmptyTable(Database d, String name, String [] cols) 82 throws DatabaseException { 83 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 temp_table.setupAllSelectableSchemes(); 91 return temp_table; 92 } 93 94 96 public void prepare() throws DatabaseException { 97 show_type = (String ) cmd.getObject("show"); 99 show_type = show_type.toLowerCase(); 100 table_name = (String ) 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 SQLQueryExecutor executor = new SQLQueryExecutor(); 112 113 TemporaryTable show_table; 115 116 try { 117 118 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 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 show_table = createEmptyTable(d, "JDBCProcedures", 222 new String [] { "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 show_table = createEmptyTable(d, "JDBCProcedureColumns", 229 new String [] { "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 show_table = createEmptyTable(d, "JDBCCatalogs", 238 new String [] { "TABLE_CAT" }); 239 } 240 241 else if (show_type.equals("jdbc_table_types")) { 242 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 [] 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 show_table = createEmptyTable(d, "JDBCBestRowIdentifier", 264 new String [] { "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 show_table = createEmptyTable(d, "JDBCVersionColumn", 272 new String [] { "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 show_table = createEmptyTable(d, "JDBCIndexInfo", 280 new String [] { "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 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 |