📄 databasemetadata.java
字号:
/* Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems 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.UnsupportedEncodingException;import java.lang.reflect.Constructor;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 IteratorWithCleanup { abstract void close() throws SQLException; abstract boolean hasNext() throws SQLException; abstract Object next() throws SQLException; } class LocalAndReferencedColumns { String constraintName; List localColumnsList; String referencedCatalog; List referencedColumnsList; String referencedTable; LocalAndReferencedColumns(List localColumns, List refColumns, String constName, String refCatalog, String refTable) { this.localColumnsList = localColumns; this.referencedColumnsList = refColumns; this.constraintName = constName; this.referencedTable = refTable; this.referencedCatalog = refCatalog; } } 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; Integer columnSize; short dataType; Integer decimalDigits; String isNullable; int nullability; int numPrecRadix = 10; String typeName; TypeDescriptor(String typeInfo, String nullabilityInfo) throws SQLException { if (typeInfo == null) { throw SQLError.createSQLException("NULL typeinfo not supported.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } 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 boolean isUnsigned = false; if (StringUtils.indexOfIgnoreCase(typeInfo, "unsigned") != -1) { fullMysqlType = mysqlType + " unsigned"; isUnsigned = true; } 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 (StringUtils.startsWithIgnoreCase(typeInfo, "enum")) { 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 = Constants.integerValueOf(maxLength); this.decimalDigits = null; } else if (StringUtils.startsWithIgnoreCase(typeInfo, "set")) { String temp = typeInfo.substring(typeInfo.indexOf("(") + 1, typeInfo.lastIndexOf(")")); java.util.StringTokenizer tokenizer = new java.util.StringTokenizer( temp, ","); int maxLength = 0; int numElements = tokenizer.countTokens(); if (numElements > 0) { maxLength += (numElements - 1); } while (tokenizer.hasMoreTokens()) { String setMember = tokenizer.nextToken().trim(); if (setMember.startsWith("'") && setMember.endsWith("'")) { maxLength += setMember.length() - 2; } else { maxLength += setMember.length(); } } this.columnSize = Constants.integerValueOf(maxLength); this.decimalDigits = null; } else if (typeInfo.indexOf(",") != -1) { // Numeric with decimals this.columnSize = Integer.valueOf(typeInfo.substring((typeInfo .indexOf("(") + 1), (typeInfo.indexOf(","))).trim()); this.decimalDigits = Integer.valueOf(typeInfo.substring( (typeInfo.indexOf(",") + 1), (typeInfo.indexOf(")"))).trim()); } else { this.columnSize = null; this.decimalDigits = null; /* If the size is specified with the DDL, use that */ if ((StringUtils.indexOfIgnoreCase(typeInfo, "char") != -1 || StringUtils.indexOfIgnoreCase(typeInfo, "text") != -1 || StringUtils.indexOfIgnoreCase(typeInfo, "blob") != -1 || StringUtils .indexOfIgnoreCase(typeInfo, "binary") != -1 || StringUtils .indexOfIgnoreCase(typeInfo, "bit") != -1) && typeInfo.indexOf("(") != -1) { int endParenIndex = typeInfo.indexOf(")"); if (endParenIndex == -1) { endParenIndex = typeInfo.length(); } this.columnSize = Integer.valueOf(typeInfo.substring( (typeInfo.indexOf("(") + 1), endParenIndex).trim()); // Adjust for pseudo-boolean if (conn.getTinyInt1isBit() && this.columnSize.intValue() == 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 (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "tinyint")) { if (conn.getTinyInt1isBit() && typeInfo.indexOf("(1)") != -1) { if (conn.getTransformedBitIsBoolean()) { this.dataType = Types.BOOLEAN; this.typeName = "BOOLEAN"; } else { this.dataType = Types.BIT; this.typeName = "BIT"; } } else { this.columnSize = Constants.integerValueOf(3); this.decimalDigits = Constants.integerValueOf(0); } } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "smallint")) { this.columnSize = Constants.integerValueOf(5); this.decimalDigits = Constants.integerValueOf(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "mediumint")) { this.columnSize = Constants.integerValueOf(isUnsigned ? 8 : 7); this.decimalDigits = Constants.integerValueOf(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "int")) { this.columnSize = Constants.integerValueOf(10); this.decimalDigits = Constants.integerValueOf(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "integer")) { this.columnSize = Constants.integerValueOf(10); this.decimalDigits = Constants.integerValueOf(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "bigint")) { this.columnSize = Constants.integerValueOf(isUnsigned ? 20 : 19); this.decimalDigits = Constants.integerValueOf(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "int24")) { this.columnSize = Constants.integerValueOf(19); this.decimalDigits = Constants.integerValueOf(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "real")) { this.columnSize = Constants.integerValueOf(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "float")) { this.columnSize = Constants.integerValueOf(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "decimal")) { this.columnSize = Constants.integerValueOf(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "numeric")) { this.columnSize = Constants.integerValueOf(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "double")) { this.columnSize = Constants.integerValueOf(22); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "char")) { this.columnSize = Constants.integerValueOf(1); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "varchar")) { this.columnSize = Constants.integerValueOf(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "timestamp")) { this.columnSize = Constants.integerValueOf(19); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "datetime")) { this.columnSize = Constants.integerValueOf(19); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "date")) { this.columnSize = Constants.integerValueOf(10); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "time")) { this.columnSize = Constants.integerValueOf(8); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "tinyblob")) { this.columnSize = Constants.integerValueOf(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "blob")) { this.columnSize = Constants.integerValueOf(65535); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "mediumblob")) { this.columnSize = Constants.integerValueOf(16777215); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "longblob")) { this.columnSize = Constants.integerValueOf(Integer.MAX_VALUE); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "tinytext")) { this.columnSize = Constants.integerValueOf(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "text")) { this.columnSize = Constants.integerValueOf(65535); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "mediumtext")) { this.columnSize = Constants.integerValueOf(16777215); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "longtext")) { this.columnSize = Constants.integerValueOf(Integer.MAX_VALUE); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "enum")) { this.columnSize = Constants.integerValueOf(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "set")) { this.columnSize = Constants.integerValueOf(255); } } // BUFFER_LENGTH this.bufferLength = MysqlIO.getMaxBuf(); // NUM_PREC_RADIX (is this right for char?) this.numPrecRadix = 10; // Nullable?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -