?? databasemetadata.java
字號:
/* Copyright (C) 2002-2007 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.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; } 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 { 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 (typeInfo != null) { 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 = new Integer(maxLength); this.decimalDigits = null; } else if (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()) { String setMember = tokenizer.nextToken().trim(); if (setMember.startsWith("'") && setMember.endsWith("'")) { maxLength += setMember.length() - 2; } else { maxLength += setMember.length(); } } this.columnSize = new Integer(maxLength); this.decimalDigits = null; } else if (typeInfo.indexOf(",") != -1) { // Numeric with decimals this.columnSize = new Integer(typeInfo.substring((typeInfo .indexOf("(") + 1), (typeInfo.indexOf(","))).trim()); this.decimalDigits = new Integer(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 = new Integer(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 = new Integer(3); this.decimalDigits = new Integer(0); } } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "smallint")) { this.columnSize = new Integer(5); this.decimalDigits = new Integer(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "mediumint")) { this.columnSize = new Integer(isUnsigned ? 8 : 7); this.decimalDigits = new Integer(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "int")) { this.columnSize = new Integer(10); this.decimalDigits = new Integer(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "integer")) { this.columnSize = new Integer(10); this.decimalDigits = new Integer(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "bigint")) { this.columnSize = new Integer(isUnsigned ? 20 : 19); this.decimalDigits = new Integer(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "int24")) { this.columnSize = new Integer(19); this.decimalDigits = new Integer(0); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "real")) { this.columnSize = new Integer(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "float")) { this.columnSize = new Integer(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "decimal")) { this.columnSize = new Integer(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "numeric")) { this.columnSize = new Integer(12); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "double")) { this.columnSize = new Integer(22); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "char")) { this.columnSize = new Integer(1); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "varchar")) { this.columnSize = new Integer(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "date")) { this.columnSize = null; } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "time")) { this.columnSize = null; } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "timestamp")) { this.columnSize = null; } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "datetime")) { this.columnSize = null; } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "tinyblob")) { this.columnSize = new Integer(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "blob")) { this.columnSize = new Integer(65535); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "mediumblob")) { this.columnSize = new Integer(16777215); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "longblob")) { this.columnSize = new Integer(Integer.MAX_VALUE); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "tinytext")) { this.columnSize = new Integer(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "text")) { this.columnSize = new Integer(65535); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "mediumtext")) { this.columnSize = new Integer(16777215); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "longtext")) { this.columnSize = new Integer(Integer.MAX_VALUE); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "enum")) { this.columnSize = new Integer(255); } else if (StringUtils.startsWithIgnoreCaseAndWs(typeInfo, "set")) { this.columnSize = new Integer(255); } } } else { this.decimalDigits = null; this.columnSize = null; } // BUFFER_LENGTH this.bufferLength = MysqlIO.getMaxBuf(); // NUM_PREC_RADIX (is this right for char?) this.numPrecRadix = 10;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -