📄 databasemetadata.java
字号:
* are ordered by schema name.
*
* <P>The schema column is:
* <OL>
* <LI><B>TABLE_SCHEM</B> String => schema name
* </OL>
*
* @return ResultSet each row has a single String column that is a
* schema name
*/
public java.sql.ResultSet getSchemas() throws java.sql.SQLException
{
Field[] Fields = new Field[1];
Fields[0] = new Field("", "TABLE_SCHEM", java.sql.Types.CHAR, 0);
Vector Tuples = new Vector();
ResultSet RS = new ResultSet(Fields, Tuples, _Conn);
return RS;
}
/**
* Get the catalog names available in this database. The results
* are ordered by catalog name.
*
* <P>The catalog column is:
* <OL>
* <LI><B>TABLE_CAT</B> String => catalog name
* </OL>
*
* @return ResultSet each row has a single String column that is a
* catalog name
*/
public java.sql.ResultSet getCatalogs() throws java.sql.SQLException
{
java.sql.ResultSet RS = _Conn.createStatement().executeQuery(
"SHOW DATABASES");
java.sql.ResultSetMetaData RSMD = RS.getMetaData();
Field[] Fields = new Field[1];
Fields[0] = new Field("", "TABLE_CAT", Types.VARCHAR,
RSMD.getColumnDisplaySize(1));
Vector Tuples = new Vector();
while (RS.next()) {
byte[][] RowVal = new byte[1][];
RowVal[0] = RS.getBytes(1);
Tuples.addElement(RowVal);
}
return new ResultSet(Fields, Tuples, _Conn);
}
/**
* Get the table types available in this database. The results
* are ordered by table type.
*
* <P>The table type is:
* <OL>
* <LI><B>TABLE_TYPE</B> String => table type. Typical types are "TABLE",
* "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY",
* "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
* </OL>
*
* @return ResultSet each row has a single String column that is a
* table type
*/
public java.sql.ResultSet getTableTypes() throws java.sql.SQLException
{
Vector Tuples = new Vector();
Field[] Fields = new Field[1];
Fields[0] = new Field("", "TABLE_TYPE", Types.VARCHAR, 5);
byte[][] TType = new byte[1][];
TType[0] = _Table_As_Bytes;
Tuples.addElement(TType);
return new ResultSet(Fields, Tuples, _Conn);
}
/**
* Get a description of table columns available in a catalog.
*
* <P>Only column descriptions matching the catalog, schema, table
* and column name criteria are returned. They are ordered by
* TABLE_SCHEM, TABLE_NAME and ORDINAL_POSITION.
*
* <P>Each column description has the following columns:
* <OL>
* <LI><B>TABLE_CAT</B> String => table catalog (may be null)
* <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
* <LI><B>TABLE_NAME</B> String => table name
* <LI><B>COLUMN_NAME</B> String => column name
* <LI><B>DATA_TYPE</B> short => SQL type from java.sql.Types
* <LI><B>TYPE_NAME</B> String => Data source dependent type name
* <LI><B>COLUMN_SIZE</B> int => column size. For char or date
* types this is the maximum number of characters, for numeric or
* decimal types this is precision.
* <LI><B>BUFFER_LENGTH</B> is not used.
* <LI><B>DECIMAL_DIGITS</B> int => the number of fractional digits
* <LI><B>NUM_PREC_RADIX</B> int => Radix (typically either 10 or 2)
* <LI><B>NULLABLE</B> int => is NULL allowed?
* <UL>
* <LI> columnNoNulls - might not allow NULL values
* <LI> columnNullable - definitely allows NULL values
* <LI> columnNullableUnknown - nullability unknown
* </UL>
* <LI><B>REMARKS</B> String => comment describing column (may be null)
* <LI><B>COLUMN_DEF</B> String => default value (may be null)
* <LI><B>SQL_DATA_TYPE</B> int => unused
* <LI><B>SQL_DATETIME_SUB</B> int => unused
* <LI><B>CHAR_OCTET_LENGTH</B> int => for char types the
* maximum number of bytes in the column
* <LI><B>ORDINAL_POSITION</B> int => index of column in table
* (starting at 1)
* <LI><B>IS_NULLABLE</B> String => "NO" means column definitely
* does not allow NULL values; "YES" means the column might
* allow NULL values. An empty string means nobody knows.
* </OL>
*
* @param catalog a catalog name; "" retrieves those without a catalog
* @param schemaPattern a schema name pattern; "" retrieves those
* without a schema
* @param tableNamePattern a table name pattern
* @param columnNamePattern a column name pattern
* @return ResultSet each row is a column description
* @see #getSearchStringEscape
*/
public java.sql.ResultSet getColumns(String Catalog, String SchemaPattern,
String TableName,
String ColumnNamePattern)
throws java.sql.SQLException
{
String DB_Sub = "";
if (ColumnNamePattern == null) {
ColumnNamePattern = "%";
}
if (Catalog != null) {
if (!Catalog.equals("")) {
DB_Sub = " FROM " + Catalog;
}
}
else {
DB_Sub = " FROM " + _Database;
}
Vector TableNameList = new Vector();
int tablename_length = 0;
if (TableName == null) {
// Select from all tables
java.sql.ResultSet Tables = getTables(Catalog, SchemaPattern, "%", new String[0]);
while (Tables.next()) {
String TN = Tables.getString("TABLE_NAME");
TableNameList.addElement(TN);
if (TN.length() > tablename_length) {
tablename_length = TN.length();
}
}
Tables.close();
}
else {
TableNameList.addElement(TableName);
tablename_length = TableName.length();
}
int catalog_length = 0;
if (Catalog != null) {
catalog_length = Catalog.length();
}
java.util.Enumeration TableNames = TableNameList.elements();
Field[] Fields = new Field[18];
Fields[0] = new Field("", "TABLE_CAT" , Types.CHAR,
catalog_length);
Fields[1] = new Field("", "TABLE_SCHEM" , Types.CHAR, 0);
Fields[2] = new Field("", "TABLE_NAME" , Types.CHAR,
tablename_length);
Fields[3] = new Field("", "COLUMN_NAME" , Types.CHAR, 32);
Fields[4] = new Field("", "DATA_TYPE" , Types.SMALLINT, 5);
Fields[5] = new Field("", "TYPE_NAME" , Types.CHAR, 16);
Fields[6] = new Field("", "COLUMN_SIZE" , Types.INTEGER,
Integer.toString(Integer.MAX_VALUE).length());
Fields[7] = new Field("", "BUFFER_LENGTH" , Types.INTEGER, 10);
Fields[8] = new Field("", "DECIMAL_DIGITS" , Types.INTEGER, 10);
Fields[9] = new Field("", "NUM_PREC_RADIX" , Types.INTEGER, 10);
Fields[10] = new Field("", "NULLABLE" , Types.INTEGER, 10);
Fields[11] = new Field("", "REMARKS" , Types.CHAR, 0);
Fields[12] = new Field("", "COLUMN_DEF" , Types.CHAR, 0);
Fields[13] = new Field("", "SQL_DATA_TYPE" , Types.INTEGER, 10);
Fields[14] = new Field("", "SQL_DATETIME_SUB", Types.INTEGER, 10);
Fields[15] = new Field("", "CHAR_OCTET_LENGTH", Types.INTEGER,
Integer.toString(Integer.MAX_VALUE).length());
Fields[16] = new Field("", "ORDINAL_POSITION" , Types.INTEGER, 10);
Fields[17] = new Field("", "IS_NULLABLE" , Types.CHAR, 3);
Vector Tuples = new Vector();
while (TableNames.hasMoreElements()) {
String TableNamePattern = (String)TableNames.nextElement();
org.gjt.mm.mysql.ResultSet RS = _Conn.execSQL(
"show columns from " +
TableNamePattern + DB_Sub + " like '"
+ ColumnNamePattern + "'", -1);
RS.setConnection(_Conn);
java.sql.ResultSetMetaData RSMD = RS.getMetaData();
int ord_pos = 1;
/*
* Fix up Catalog and TableName
*/
if (Catalog == null) {
Catalog = "";
}
if (TableName == null) {
TableName = "";
}
while (RS.next()) {
byte[][] RowVal = new byte[18][];
RowVal[0] = Catalog.getBytes(); // TABLE_CAT
RowVal[1] = new byte[0]; // TABLE_SCHEM (No schemas in MySQL)
RowVal[2] = TableName.getBytes(); // TABLE_NAME
RowVal[3] = RS.getBytes("Field");
String TypeInfo = RS.getString("Type");
if (Driver.debug) {
System.out.println("Type: " + TypeInfo);
}
String MysqlType = "";
if (TypeInfo.indexOf("(") != -1) {
MysqlType = TypeInfo.substring(0, TypeInfo.indexOf("("));
}
else {
MysqlType = TypeInfo;
}
/*
* Convert to XOPEN (thanks JK)
*/
RowVal[4] = Integer.toString(MysqlDefs.mysqlToJavaType(MysqlType)).getBytes(); // DATA_TYPE (jdbc)
RowVal[5] = MysqlType.getBytes(); // TYPE_NAME (native)
// Figure Out the Size
if (TypeInfo != null) {
if (TypeInfo.indexOf("enum") != -1) {
String Temp = TypeInfo.substring(TypeInfo.indexOf("("), TypeInfo.indexOf(")"));
java.util.StringTokenizer ST = new java.util.StringTokenizer(Temp, ",");
int max_length = 0;
while (ST.hasMoreTokens()) {
max_length = Math.max(max_length, (ST.nextToken().length() - 2));
}
RowVal[6] = Integer.toString(max_length).getBytes();
RowVal[8] = new byte[] {(byte)'0'};
}
else if (TypeInfo.indexOf(",") != -1) {
// Numeric with decimals
String Size = TypeInfo.substring((TypeInfo.indexOf("(") + 1),
(TypeInfo.indexOf(",")));
String Decimals = TypeInfo.substring((TypeInfo.indexOf(",") + 1),
(TypeInfo.indexOf(")")));
RowVal[6] = Size.getBytes();
RowVal[8] = Decimals.getBytes();
}
else {
String Size = "0";
/* If the size is specified with the DDL, use that */
if (TypeInfo.indexOf("(") != -1) {
Size = TypeInfo.substring((TypeInfo.indexOf("(") + 1),
(TypeInfo.indexOf(")")));
}
/* Otherwise resort to defaults */
else if (TypeInfo.toLowerCase().equals("tinyint")) {
Size = "1";
}
else if (TypeInfo.toLowerCase().equals("smallint")) {
Size = "6";
}
else if (TypeInfo.toLowerCase().equals("mediumint")) {
Size = "6";
}
else if (TypeInfo.toLowerCase().equals("int")) {
Size = "11";
}
else if (TypeInfo.toLowerCase().equals("integer")) {
Size = "11";
}
else if (TypeInfo.toLowerCase().equals("bigint")) {
Size = "25";
}
else if (TypeInfo.toLowerCase().equals("int24")) {
Size = "25";
}
else if (TypeInfo.toLowerCase().equals("real")) {
Size = "12";
}
else if (TypeInfo.toLowerCase().equals("float")) {
Size = "12";
}
else if (TypeInfo.toLowerCase().equals("decimal")) {
Size = "12";
}
else if (TypeInfo.toLowerCase().equals("numeric")) {
Size = "12";
}
else if (TypeInfo.toLowerCase().equals("double")) {
Size = "22";
}
else if (TypeInfo.toLowerCase().equals("char")) {
Size = "1";
}
else if (TypeInfo.toLowerCase().equals("varchar")) {
Size = "255";
}
else if (TypeInfo.toLowerCase().equals("date")) {
Size = "10";
}
else if (TypeInfo.toLowerCase().equals("time")) {
Size = "8";
}
else if (TypeInfo.toLowerCase().equals("timestamp")) {
Size = "19";
}
else if (TypeInfo.toLowerCase().equals("datetime")) {
Size = "19";
}
else if (TypeInfo.toLowerCase().equals("tinyblob")) {
Size = "255";
}
else if (TypeInfo.toLowerCase().equals("blob")) {
Size = Integer.toString(Math.min(65535, MysqlIO.getMaxBuf()));
}
else if (TypeInfo.toLowerCase().equals("mediumblob")) {
Size = Integer.toString(Math.min(16277215, MysqlIO.getMaxBuf()));
}
else if (TypeInfo.toLowerCase().equals("longblob")) {
Size = (Integer.toString(MysqlIO.getMaxBuf()).compareTo("2147483657") < 0 ? Integer.toString(MysqlIO.getMaxBuf()) : "2147483657");
}
else if (TypeInfo.toLowerCase().equals("tinytext")) {
Size = "255";
}
else if (TypeInfo.toLowerCase().equals("text")) {
Size = "65535";
}
else if (TypeInfo.toLowerCase().equals("mediumtext")) {
Size = Integer.toString(Math.min(16277215, MysqlIO.getMaxBuf()));
}
else if (TypeInfo.toLowerCase().equals("enum")) {
Size = "255";
}
else if (TypeInfo.toLowerCase().equals("set")) {
Size = "255";
}
RowVal[6] = Size.getBytes();
RowVal[8] = new byte[] {(byte)'0'};
}
}
else {
RowVal[8] = new byte[] {(byte)'0'};
RowVal[6] = new byte[] {(byte)'0'};
}
RowVal[7] = Integer.toString(MysqlIO.MAXBUF).getBytes(); // BUFFER_LENGTH
RowVal[9] = new byte[] {(byte)'1',(byte)'0'}; // NUM_PREC_RADIX (is this right for char?)
String Nullable = RS.getString("Null");
// Nullable?
if (Nullable != null) {
if (Nullable.equals("YES")) {
RowVal[10] = Integer.toString(columnNullable).getBytes();
RowVal[17] = new String("YES").getBytes(); // IS_NULLABLE
}
else {
RowVal[10] = Integer.toString(columnNoNulls).getBytes();
RowVal[17] = new String("NO").getBytes();
}
}
else {
RowVal[10] = Integer.toString(columnNoNulls).getBytes();
RowVal[17] = new String("NO").getBytes();
}
RowVal[11] = new byte[0]; // REMARK
// COLUMN_DEF
byte[] Default = RS.getBytes("Default");
if (Default != null) {
RowVal[12] = Default;
}
else {
RowVal[12] = new byte[0];
}
RowVal[13] = new byte[] {(byte)'0'}; // SQL_DATA_TYPE
RowVal[14] = new byte[] {(byte)'0'}; // SQL_DATE_TIME_SUB
RowVal[15] = RowVal[6]; // CHAR_OCTET_LENGTH
RowVal[16] = Integer.toString(ord_pos++).getBytes(); // ORDINAL_POSITION
Tuples.addElement(RowVal);
}
RS.close();
}
ResultSet Results = new ResultSet(Fields, Tuples, _Conn);
return Results;
}
/**
* Get a description of the access rights for a table's columns.
*
* <P>Only privileges matching the column name criteria are
* returned. They are ordered by COLUMN_NAME and PRIVILEGE.
*
* <P>Each privilige description has the following columns:
* <OL>
* <LI><B>TABLE_CAT</B> String => table catalog (may be null)
* <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
* <LI><B>TABLE_NAME</B> String => table name
* <LI><B>COLUMN_NAME</B> String => column name
* <LI><B>GRANTOR</B> => grantor of access (may be null)
* <LI><B>GRANTEE</B> String => grantee of access
* <LI><B>PRIVILEGE</B> String => name of access (SELECT,
* INSERT, UPDATE, REFRENCES, ...)
* <LI><B>IS_GRANTABLE</B> String => "YES" if grantee is permitted
* to grant to others; "NO" if not; null if unknown
* </OL>
*
* @param catalog a catalog name; "" retrieves those without a catalog
* @param schema a schema name; "" retrieves those without a schema
* @param table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -