📄 databasemetadata.java
字号:
/* Copyright (C) 2002-2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. 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.io.IOException;import java.io.StreamTokenizer;import java.io.StringReader;import java.io.UnsupportedEncodingException;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Types;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;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 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> * * @author Mark Matthews * @version $Id: DatabaseMetaData.java,v 1.27.4.66 2005/05/03 18:40:39 mmatthews * Exp $ */public class DatabaseMetaData implements java.sql.DatabaseMetaData { protected abstract class IterateBlock { IteratorWithCleanup iterator; IterateBlock(IteratorWithCleanup i) { iterator = i; } public void doForAll() throws SQLException { try { while (iterator.hasNext()) { forEach(iterator.next()); } } finally { iterator.close(); } } abstract void forEach(Object each) throws SQLException; } protected abstract class IteratorWithCleanup { abstract void close() throws SQLException; abstract boolean hasNext() throws SQLException; abstract Object next() throws SQLException; } protected class ResultSetIterator extends IteratorWithCleanup { int colIndex; ResultSet resultSet; ResultSetIterator(ResultSet rs, int index) { resultSet = rs; colIndex = index; } void close() throws SQLException { resultSet.close(); } boolean hasNext() throws SQLException { return resultSet.next(); } Object next() throws SQLException { return resultSet.getObject(colIndex); } } protected class SingleStringIterator extends IteratorWithCleanup { boolean onFirst = true; String value; SingleStringIterator(String s) { value = s; } void close() throws SQLException { // not needed } boolean hasNext() throws SQLException { return onFirst; } Object next() throws SQLException { onFirst = false; return value; } } /** * Parses and represents common data type information used by various * column/parameter methods. */ class TypeDescriptor { int bufferLength; int charOctetLength; int columnSize; short dataType; int decimalDigits; String isNullable; int nullability; int numPrecRadix = 10; String typeName; TypeDescriptor(String typeInfo, String nullabilityInfo) throws SQLException { String mysqlType = ""; String fullMysqlType = null; if (typeInfo.indexOf("(") != -1) { mysqlType = typeInfo.substring(0, typeInfo.indexOf("(")); } else { mysqlType = typeInfo; } int indexOfUnsignedInMysqlType = StringUtils.indexOfIgnoreCase( mysqlType, "unsigned"); if (indexOfUnsignedInMysqlType != -1) { mysqlType = mysqlType.substring(0, (indexOfUnsignedInMysqlType - 1)); } // Add unsigned to typename reported to enduser as 'native type', if // present if (StringUtils.indexOfIgnoreCase(typeInfo, "unsigned") != -1) { fullMysqlType = mysqlType + " unsigned"; } else { fullMysqlType = mysqlType; } if (conn.getCapitalizeTypeNames()) { fullMysqlType = fullMysqlType.toUpperCase(Locale.ENGLISH); } this.dataType = (short) MysqlDefs.mysqlToJavaType(mysqlType); this.typeName = fullMysqlType; // Figure Out the Size if (typeInfo != null) { if (StringUtils.startsWithIgnoreCase(typeInfo, "enum") || StringUtils.startsWithIgnoreCase(typeInfo, "set")) { String temp = typeInfo.substring(typeInfo.indexOf("("), typeInfo.lastIndexOf(")")); java.util.StringTokenizer tokenizer = new java.util.StringTokenizer( temp, ","); int maxLength = 0; while (tokenizer.hasMoreTokens()) { maxLength = Math.max(maxLength, (tokenizer.nextToken() .length() - 2)); } this.columnSize = maxLength; this.decimalDigits = 0; } else if (typeInfo.indexOf(",") != -1) { // Numeric with decimals this.columnSize = Integer.parseInt(typeInfo.substring( (typeInfo.indexOf("(") + 1), (typeInfo.indexOf(",")))); this.decimalDigits = Integer.parseInt(typeInfo.substring( (typeInfo.indexOf(",") + 1), (typeInfo.indexOf(")")))); } else { this.columnSize = 0; /* If the size is specified with the DDL, use that */ if (typeInfo.indexOf("(") != -1) { int endParenIndex = typeInfo.indexOf(")"); if (endParenIndex == -1) { endParenIndex = typeInfo.length(); } this.columnSize = Integer.parseInt(typeInfo.substring( (typeInfo.indexOf("(") + 1), endParenIndex)); // Adjust for pseudo-boolean if (conn.getTinyInt1isBit() && this.columnSize == 1 && StringUtils.startsWithIgnoreCase(typeInfo, 0, "tinyint")) { if (conn.getTransformedBitIsBoolean()) { this.dataType = Types.BOOLEAN; this.typeName = "BOOLEAN"; } else { this.dataType = Types.BIT; this.typeName = "BIT"; } } } else if (typeInfo.equalsIgnoreCase("tinyint")) { this.columnSize = 1; } else if (typeInfo.equalsIgnoreCase("smallint")) { this.columnSize = 6; } else if (typeInfo.equalsIgnoreCase("mediumint")) { this.columnSize = 6; } else if (typeInfo.equalsIgnoreCase("int")) { this.columnSize = 11; } else if (typeInfo.equalsIgnoreCase("integer")) { this.columnSize = 11; } else if (typeInfo.equalsIgnoreCase("bigint")) { this.columnSize = 25; } else if (typeInfo.equalsIgnoreCase("int24")) { this.columnSize = 25; } else if (typeInfo.equalsIgnoreCase("real")) { this.columnSize = 12; } else if (typeInfo.equalsIgnoreCase("float")) { this.columnSize = 12; } else if (typeInfo.equalsIgnoreCase("decimal")) { this.columnSize = 12; } else if (typeInfo.equalsIgnoreCase("numeric")) { this.columnSize = 12; } else if (typeInfo.equalsIgnoreCase("double")) { this.columnSize = 22; } else if (typeInfo.equalsIgnoreCase("char")) { this.columnSize = 1; } else if (typeInfo.equalsIgnoreCase("varchar")) { this.columnSize = 255; } else if (typeInfo.equalsIgnoreCase("date")) { this.columnSize = 10; } else if (typeInfo.equalsIgnoreCase("time")) { this.columnSize = 8; } else if (typeInfo.equalsIgnoreCase("timestamp")) { this.columnSize = 19; } else if (typeInfo.equalsIgnoreCase("datetime")) { this.columnSize = 19; } else if (typeInfo.equalsIgnoreCase("tinyblob")) { this.columnSize = 255; } else if (typeInfo.equalsIgnoreCase("blob")) { this.columnSize = 65535; } else if (typeInfo.equalsIgnoreCase("mediumblob")) { this.columnSize = 16277215; } else if (typeInfo.equalsIgnoreCase("longblob")) { this.columnSize = Integer.MAX_VALUE; } else if (typeInfo.equalsIgnoreCase("tinytext")) { this.columnSize = 255; } else if (typeInfo.equalsIgnoreCase("text")) { this.columnSize = 65535; } else if (typeInfo.equalsIgnoreCase("mediumtext")) { this.columnSize = 16277215; } else if (typeInfo.equalsIgnoreCase("longtext")) { this.columnSize = Integer.MAX_VALUE; } else if (typeInfo.equalsIgnoreCase("enum")) { this.columnSize = 255; } else if (typeInfo.equalsIgnoreCase("set")) { this.columnSize = 255; } this.decimalDigits = 0; } } else { this.decimalDigits = 0; this.columnSize = 0; } // BUFFER_LENGTH this.bufferLength = MysqlIO.getMaxBuf(); // NUM_PREC_RADIX (is this right for char?) this.numPrecRadix = 10; // Nullable? if (nullabilityInfo != null) { if (nullabilityInfo.equals("YES")) { this.nullability = java.sql.DatabaseMetaData.columnNullable; this.isNullable = "YES"; // IS_NULLABLE } else { this.nullability = java.sql.DatabaseMetaData.columnNoNulls; this.isNullable = "NO"; } } else { this.nullability = java.sql.DatabaseMetaData.columnNoNulls; this.isNullable = "NO"; } } } private static final int DEFERRABILITY = 13; private static final int DELETE_RULE = 10; private static final int FK_NAME = 11; private static final int FKCOLUMN_NAME = 7; private static final int FKTABLE_CAT = 4; private static final int FKTABLE_NAME = 6; private static final int FKTABLE_SCHEM = 5; private static final int KEY_SEQ = 8; private static final int PK_NAME = 12; private static final int PKCOLUMN_NAME = 3; // // Column indexes used by all DBMD foreign key // ResultSets // private static final int PKTABLE_CAT = 0; private static final int PKTABLE_NAME = 2; private static final int PKTABLE_SCHEM = 1; /** The table type for generic tables that support foreign keys. */ private static final String SUPPORTS_FK = "SUPPORTS_FK"; private static final byte[] TABLE_AS_BYTES = "TABLE".getBytes(); private static final int UPDATE_RULE = 9; private static final byte[] VIEW_AS_BYTES = "VIEW".getBytes(); /** The connection to the database */ protected Connection conn; /** The 'current' database name being used */ private String database = null; /** What character to use when quoting identifiers */ private String quotedId = null; /** * Creates a new DatabaseMetaData object. * * @param connToSet * DOCUMENT ME! * @param databaseToSet * DOCUMENT ME! */ public DatabaseMetaData(Connection connToSet, String databaseToSet) { this.conn = connToSet; this.database = databaseToSet; try { this.quotedId = this.conn.supportsQuotedIdentifiers() ? getIdentifierQuoteString() : ""; } catch (SQLException sqlEx) { // Forced by API, never thrown from getIdentifierQuoteString() in // this // implementation. AssertionFailedException.shouldNotHappen(sqlEx); } } /** * Can all the procedures returned by getProcedures be called by the current * user? * * @return true if so * @throws SQLException * DOCUMENT ME! */ public boolean allProceduresAreCallable() throws SQLException { return false; } /** * Can all the tables returned by getTable be SELECTed by the current user? * * @return true if so * @throws SQLException * DOCUMENT ME!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -