/*
Copyright (C) 2002 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mysql.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeMap;
/**
* JDBC Interface to Mysql functions
*
* <p>
* This class provides information about the database as a whole.
* </p>
*
* <p>
* Many of the methods here return lists of information in ResultSets. You can
* use the normal ResultSet methods such as getString and getInt to retrieve
* the data from these ResultSets. If a given form of metadata is not
* available, these methods show throw a java.sql.SQLException.
* </p>
*
* <p>
* Some of these methods take arguments that are String patterns. These
* methods all have names such as fooPattern. Within a pattern String "%"
* means match any substring of 0 or more characters and "_" means match any
* one character.
* </p>
*
* @version $Id: DatabaseMetaData.java,v 1.20 2002/12/06 21:27:07 mmatthew Exp
* $
* @author Mark Matthews
*/
public class DatabaseMetaData implements java.sql.DatabaseMetaData {
//~ Static fields/initializers ---------------------------------------------
private static final byte[] TABLE_AS_BYTES = "TABLE".getBytes();
/**
* The table type for generic tables that support foreign
* keys.
*/
private static final String SUPPORTS_FK = "SUPPORTS_FK";
//
// Column indexes used by all DBMD foreign key
// ResultSets
//
private static final int PKTABLE_CAT = 0;
private static final int PKTABLE_SCHEM = 1;
private static final int PKTABLE_NAME = 2;
private static final int PKCOLUMN_NAME = 3;
private static final int FKTABLE_CAT = 4;
private static final int FKTABLE_SCHEM = 5;
private static final int FKTABLE_NAME = 6;
private static final int FKCOLUMN_NAME = 7;
private static final int KEY_SEQ = 8;
private static final int UPDATE_RULE = 9;
private static final int DELETE_RULE = 10;
private static final int FK_NAME = 11;
private static final int PK_NAME = 12;
private static final int DEFERRABILITY = 13;
//~ Instance fields --------------------------------------------------------
/**
* The connection to the database
*/
private Connection conn;
/**
* The 'current' database name being used
*/
private String database = null;
/**
* What character to use when quoting identifiers
*/
private String quotedId = null;
//~ Constructors -----------------------------------------------------------
/**
* Creates a new DatabaseMetaData object.
*
* @param conn DOCUMENT ME!
* @param database DOCUMENT ME!
*/
public DatabaseMetaData(Connection conn, String database) {
this.conn = conn;
this.database = database;
try {
this.quotedId = this.conn.supportsQuotedIdentifiers()
? getIdentifierQuoteString() : "";
} catch (SQLException sqlEx) {
// Forced by API, never thrown from getIdentifierQuoteString() in this
// implementation.
AssertionFailedException.shouldNotHappen(sqlEx);
}
}
//~ Methods ----------------------------------------------------------------
/**
* @see DatabaseMetaData#getAttributes(String, String, String, String)
*/
public java.sql.ResultSet getAttributes(String arg0, String arg1,
String arg2, String arg3) throws SQLException {
Field[] fields = new Field[21];
fields[0] = new Field("", "TYPE_CAT", Types.CHAR, 32);
fields[1] = new Field("", "TYPE_SCHEM", Types.CHAR, 32);
fields[2] = new Field("", "TYPE_NAME", Types.CHAR, 32);
fields[3] = new Field("", "ATTR_NAME", Types.CHAR, 32);
fields[4] = new Field("", "DATA_TYPE", Types.SMALLINT, 32);
fields[5] = new Field("", "ATTR_TYPE_NAME", Types.CHAR, 32);
fields[6] = new Field("", "ATTR_SIZE", Types.INTEGER, 32);
fields[7] = new Field("", "DECIMAL_DIGITS", Types.INTEGER, 32);
fields[8] = new Field("", "NUM_PREC_RADIX", Types.INTEGER, 32);
fields[9] = new Field("", "NULLABLE ", Types.INTEGER, 32);
fields[10] = new Field("", "REMARKS", Types.CHAR, 32);
fields[11] = new Field("", "ATTR_DEF", Types.CHAR, 32);
fields[12] = new Field("", "SQL_DATA_TYPE", Types.INTEGER, 32);
fields[13] = new Field("", "SQL_DATETIME_SUB", Types.INTEGER, 32);
fields[14] = new Field("", "CHAR_OCTET_LENGTH", Types.INTEGER, 32);
fields[15] = new Field("", "ORDINAL_POSITION", Types.INTEGER, 32);
fields[16] = new Field("", "IS_NULLABLE", Types.CHAR, 32);
fields[17] = new Field("", "SCOPE_CATALOG", Types.CHAR, 32);
fields[18] = new Field("", "SCOPE_SCHEMA", Types.CHAR, 32);
fields[19] = new Field("", "SCOPE_TABLE", Types.CHAR, 32);
fields[20] = new Field("", "SOURCE_DATA_TYPE", Types.SMALLINT, 32);
return buildResultSet(fields, new ArrayList());
}
/**
* Get a description of a table's optimal set of columns that uniquely
* identifies a row. They are ordered by SCOPE.
*
* <P>
* Each column description has the following columns:
*
* <OL>
* <li>
* <B>SCOPE</B> short => actual scope of result
*
* <UL>
* <li>
* bestRowTemporary - very temporary, while using row
* </li>
* <li>
* bestRowTransaction - valid for remainder of current transaction
* </li>
* <li>
* bestRowSession - valid for remainder of current session
* </li>
* </ul>
* </li>
* <li>
* <B>COLUMN_NAME</B> String => column name
* </li>
* <li>
* <B>DATA_TYPE</B> short => SQL data type from java.sql.Types
* </li>
* <li>
* <B>TYPE_NAME</B> String => Data source dependent type name
* </li>
* <li>
* <B>COLUMN_SIZE</B> int => precision
* </li>
* <li>
* <B>BUFFER_LENGTH</B> int => not used
* </li>
* <li>
* <B>DECIMAL_DIGITS</B> short => scale
* </li>
* <li>
* <B>PSEUDO_COLUMN</B> short => is this a pseudo column like an Oracle
* ROWID
*
* <UL>
* <li>
* bestRowUnknown - may or may not be pseudo column
* </li>
* <li>
* bestRowNotPseudo - is NOT a pseudo column
* </li>
* <li>
* bestRowPseudo - is a pseudo column
* </li>
* </ul>
* </li>
* </ol>
* </p>
*
* @param catalog a catalog name; "" retrieves those without a catalog
* @param schema a schema name; "" retrieves those without a schema
* @param table a table name
* @param scope the scope of interest; use same values as SCOPE
* @param nullable include columns that are nullable?
* @return ResultSet each row is a column description
* @throws java.sql.SQLException D