Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

java.sql.ResultSet com::mysql::jdbc::DatabaseMetaData::getTables ( String  catalog,
String  schemaPattern,
String  tableNamePattern,
String[]  types 
) throws SQLException [inline]

Get a description of tables available in a catalog.

Only table descriptions matching the catalog, schema, table name and type criteria are returned. They are ordered by TABLE_TYPE, TABLE_SCHEM and TABLE_NAME.

Each table description has the following columns:

  1. TABLE_CAT String => table catalog (may be null)
  2. TABLE_SCHEM String => table schema (may be null)
  3. TABLE_NAME String => table name
  4. TABLE_TYPE String => table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
  5. REMARKS String => explanatory comment on the table

Note: Some databases may not return information for all tables.

Parameters:
catalog a catalog name; "" retrieves those without a catalog
schemaPattern a schema name pattern; "" retrieves those without a schema
tableNamePattern a table name pattern
types a list of table types to include; null returns all types
Returns:
ResultSet each row is a table description
Exceptions:
SQLException DOCUMENT ME!
See also:
getSearchStringEscape

Definition at line 3322 of file DatabaseMetaData.java.

References com::mysql::jdbc::Statement::close(), com::mysql::jdbc::ResultSet::close(), conn, com::mysql::jdbc::Connection::createStatement(), database, com::mysql::jdbc::Statement::executeQuery(), com::mysql::jdbc::ResultSet::findColumn(), com::mysql::jdbc::ResultSet::getBytes(), com::mysql::jdbc::ResultSet::getString(), com::mysql::jdbc::ResultSet::next(), quotedId, s2b(), com::mysql::jdbc::Statement::setEscapeProcessing(), and com::mysql::jdbc::Connection::versionMeetsMinimum().

Referenced by getColumns().

                                                                     {
        String databasePart = "";

        if (catalog != null) {
            if (!catalog.equals("")) {
                databasePart = " FROM " + this.quotedId + catalog +
                    this.quotedId;
            }
        } else {
            databasePart = " FROM " + this.quotedId + this.database +
                this.quotedId;
        }

        if (tableNamePattern == null) {
            tableNamePattern = "%";
        }

        Statement stmt = null;
        ResultSet results = null;

        try {
            stmt = this.conn.createStatement();
            stmt.setEscapeProcessing(false);

            if (!this.conn.versionMeetsMinimum(5, 0, 2)) {
                  results = stmt.executeQuery("SHOW TABLES " + databasePart +
                    " LIKE '" + tableNamePattern + "'");
            } else {
                  results = stmt.executeQuery("SHOW FULL TABLES " + databasePart +
                        " LIKE '" + tableNamePattern + "'");
            }

            Field[] fields = new Field[5];
            fields[0] = new Field("", "TABLE_CAT", java.sql.Types.VARCHAR,
                    (catalog == null) ? 0 : catalog.length());
            fields[1] = new Field("", "TABLE_SCHEM", java.sql.Types.VARCHAR, 0);
            fields[2] = new Field("", "TABLE_NAME", java.sql.Types.VARCHAR, 255);
            fields[3] = new Field("", "TABLE_TYPE", java.sql.Types.VARCHAR, 5);
            fields[4] = new Field("", "REMARKS", java.sql.Types.VARCHAR, 0);

            ArrayList tuples = new ArrayList();
           
            boolean shouldReportTables = false;
            boolean shouldReportViews = false;

            if (types == null) {
                shouldReportTables = true;
                shouldReportViews = true;
            } else {
                for (int i = 0; i < types.length; i++) {
                    if ("TABLE".equalsIgnoreCase(types[i])) {
                        shouldReportTables = true;
                    }

                    if ("VIEW".equalsIgnoreCase(types[i])) {
                        shouldReportViews = true;
                    }
                }
            }

            int typeColumnIndex = 0;
            boolean hasTableTypes = false;
            
            if (shouldReportViews) {
                  try {
                        // Both column names have been in use in the source tree
                        // so far....
                        typeColumnIndex = results.findColumn("table_type");
                        hasTableTypes = true;
                  } catch (SQLException sqlEx) {
                        
                      // We should probably check SQLState here, but that
                      // can change depending on the server version and
                      // user properties, however, we'll get a 'true' 
                      // SQLException when we actually try to find the 
                      // 'Type' column
                      // 
                        try {
                              typeColumnIndex = results.findColumn("Type");
                        hasTableTypes = true;
                        } catch (SQLException sqlEx2) {
                              hasTableTypes = false;
                        }
                  }
            }

            TreeMap tablesOrderedByName = null;
            TreeMap viewsOrderedByName = null;
            
            while (results.next()) {
                  byte[][] row = new byte[5][];
                row[0] = (catalog == null) ? new byte[0] : s2b(catalog);
                row[1] = null;
                row[2] = results.getBytes(1);
                row[4] = new byte[0];

                if (hasTableTypes) {
                    String tableType = results.getString(typeColumnIndex);

                    if (("table".equalsIgnoreCase(tableType) || 
                              "base table".equalsIgnoreCase(tableType)) &&
                            shouldReportTables) {
                        row[3] = TABLE_AS_BYTES;
                        
                        if (tablesOrderedByName == null) {
                              tablesOrderedByName = new TreeMap();
                        }
                        
                        tablesOrderedByName.put(results.getString(1), row);
                    } else if ("view".equalsIgnoreCase(tableType) &&
                            shouldReportViews) {
                        row[3] = VIEW_AS_BYTES;
                        
                        if (viewsOrderedByName == null) {
                              viewsOrderedByName = new TreeMap();
                        }
                        
                        viewsOrderedByName.put(results.getString(1), row);
                    } else {
                        // punt?
                        row[3] = TABLE_AS_BYTES;
                        
                        if (tablesOrderedByName == null) {
                              tablesOrderedByName = new TreeMap();
                        }
                        
                        tablesOrderedByName.put(results.getString(1), row);
                    }
                } else {
                    // Pre-MySQL-5.0.1, tables only
                    row[3] = TABLE_AS_BYTES;
                    
                    if (tablesOrderedByName == null) {
                        tablesOrderedByName = new TreeMap();
                    }
                    
                    tablesOrderedByName.put(results.getString(1), row);
                }
            }

            //They are ordered by TABLE_TYPE,
            // * TABLE_SCHEM and TABLE_NAME.
            
            if (tablesOrderedByName != null) {
                  Iterator tablesIter = tablesOrderedByName.values().iterator();

                  while (tablesIter.hasNext()) {
                        tuples.add(tablesIter.next());
                  }
            }

            if (viewsOrderedByName != null) {
                  Iterator viewsIter = viewsOrderedByName.values().iterator();

                  while (viewsIter.hasNext()) {
                        tuples.add(viewsIter.next());
                  }
            }

            java.sql.ResultSet tables = buildResultSet(fields, tuples);

            return tables;
        } finally {
            if (results != null) {
                try {
                    results.close();
                } catch (Exception ex) {
                    ;
                }

                results = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception ex) {
                    ;
                }

                stmt = null;
            }
        }
    }


Generated by  Doxygen 1.6.0   Back to index