columnmetadata.java
来自「derby database source code.good for you.」· Java 代码 · 共 926 行 · 第 1/3 页
JAVA
926 行
/* Derby - Class org.apache.derby.client.am.ColumnMetaData Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/package org.apache.derby.client.am;// Under JDBC 2, we must new up our parameter meta data as column meta data instances// Once we move to JDK 1.4 pre-req, create a ResultSetMetaData class and make this class abstractpublic class ColumnMetaData implements java.sql.ResultSetMetaData { public int columns_; public boolean[] nullable_; // Although this is describe information, it is tagged transient for now becuase it is not currently used. transient public int[] singleMixedByteOrDouble_; // 1 means single, 2 means double, 3 means mixed-byte, 0 not applicable // All of the following state data comes from the SQLDA reply. //Data from SQLDHGRP public short sqldHold_; public short sqldReturn_; public short sqldScroll_; public short sqldSensitive_; public short sqldFcode_; public short sqldKeytype_; public String sqldRdbnam_; // catalog name, not used by driver, placeholder only public String sqldSchema_; // schema name, not used by driver, placeholder only //data comes from SQLDAGRP public int[] sqlPrecision_; // adjusted sqllen; public int[] sqlScale_; public long[] sqlLength_; // This is maximum length for varchar fields // These are the derby sql types, for use only by ResultSetMetaData, other code should use jdbcTypes_. // sqlTypes_ is currently not set for input column meta data. public int[] sqlType_; public int[] sqlCcsid_; // With the exception of sqlNames_ and sqlxParmmode_, the following members are only allocated when needed //Data from SQLDOPTGRP public String[] sqlName_; // column name, pre-allocated public String[] sqlLabel_; // column label public short[] sqlUnnamed_; public String[] sqlComment_; //Data from SQLDXGRP public short[] sqlxKeymem_; public short[] sqlxGenerated_; public short[] sqlxParmmode_; // pre-allocated public String[] sqlxCorname_; public String[] sqlxName_; public String[] sqlxBasename_; // table name public int[] sqlxUpdatable_; public String[] sqlxSchema_; // schema name public String[] sqlxRdbnam_; // catalog name //-----------------------------transient state-------------------------------- // For performance only, not part of logical model. public transient int[][] protocolTypesCache_ = null; public transient java.util.Hashtable protocolTypeToOverrideLidMapping_ = null; public transient java.util.ArrayList mddOverrideArray_ = null; public transient int[] types_; public transient int[] clientParamtertype_; public transient LogWriter logWriter_; // only set on execute replies, this is not describe information. // only used for result set meta data. public transient int resultSetConcurrency_; transient private java.util.Hashtable columnNameToIndexCache_ = null; transient private boolean statementClosed_ = false; void markClosed() { statementClosed_ = true; nullDataForGC(); } void checkForClosedStatement() throws SqlException { // agent_.checkForDeferredExceptions(); if (statementClosed_) { throw new SqlException(logWriter_, "Statement closed"); } } //---------------------navigational members----------------------------------- //---------------------constructors/finalizer--------------------------------- // Called by NETColumnMetaData constructor before #columns is parsed out yet. public ColumnMetaData(LogWriter logWriter) { logWriter_ = logWriter; } // For creating column meta data when describe input is not available. // The upper bound that is passed in is determined by counting the number of parameter markers. // Called by PreparedStatement.flowPrepareStatement() and flowDescribeInputOutput() // only when describe input is not available. public ColumnMetaData(LogWriter logWriter, int upperBound) { logWriter_ = logWriter; initializeCache(upperBound); } public void initializeCache(int upperBound) { columns_ = upperBound; nullable_ = new boolean[upperBound]; types_ = new int[upperBound]; clientParamtertype_ = new int[upperBound]; singleMixedByteOrDouble_ = new int[upperBound]; // 1 means single, 2 means double, 3 means mixed-byte, 0 not applicable sqlPrecision_ = new int[upperBound]; sqlScale_ = new int[upperBound]; sqlLength_ = new long[upperBound]; sqlType_ = new int[upperBound]; sqlCcsid_ = new int[upperBound]; sqlName_ = new String[upperBound]; sqlxParmmode_ = new short[upperBound]; } protected void finalize() throws java.lang.Throwable { super.finalize(); } //--------------------Abstract material layer call-down methods----------------- //------------------material layer event callback methods----------------------- // ---------------------------jdbc 1------------------------------------------ public int getColumnCount() throws SqlException { checkForClosedStatement(); return columns_; } public boolean isAutoIncrement(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); return false; } public boolean isCaseSensitive(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); //return true if the SQLTYPE is CHAR, VARCHAR, LOGVARCHAR or CLOB int type = types_[column - 1]; return type == Types.CHAR || type == Types.VARCHAR || type == Types.LONGVARCHAR || type == Types.CLOB; } // all searchable except distinct public boolean isSearchable(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); return true; } public boolean isCurrency(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); return false; } public int isNullable(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); if (nullable_[column - 1]) { return java.sql.ResultSetMetaData.columnNullable; } else { return java.sql.ResultSetMetaData.columnNoNulls; } } public boolean isSigned(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); //return true only if the SQLType is SMALLINT, INT, BIGINT, FLOAT, REAL, DOUBLE, NUMERIC OR DECIMAL int type = types_[column - 1]; return type == Types.SMALLINT || type == Types.INTEGER || type == Types.BIGINT || type == java.sql.Types.FLOAT || type == Types.REAL || type == Types.DOUBLE || type == java.sql.Types.NUMERIC || type == Types.DECIMAL; } public int getColumnDisplaySize(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); int jdbcType = types_[column - 1]; switch (jdbcType) { case Types.INTEGER: return 11; case Types.SMALLINT: return 6; case Types.BIGINT: return 20; case Types.REAL: return 13; case Types.DOUBLE: case java.sql.Types.FLOAT: return 22; case Types.DECIMAL: case java.sql.Types.NUMERIC: return getPrecision(column) + 2; // add 1 for sign and 1 for decimal case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.CLOB: return (int) sqlLength_[column - 1]; case Types.DATE: return 10; case Types.TIME: return 8; case Types.TIMESTAMP: return 26; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: case Types.BLOB: return (int) (2 * sqlLength_[column - 1]); // eg. "FF" represents just one byte default: throw new SqlException(logWriter_, "not supported"); } } public String getColumnLabel(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); // return labels if label is turned on, otherwise, return column name if (sqlLabel_ != null && sqlLabel_[column - 1] != null) { return sqlLabel_[column - 1]; } if (sqlName_ == null || sqlName_[column - 1] == null) { assignColumnName(column); } return sqlName_[column - 1]; } public String getColumnName(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); // The Javadoc and Jdbc book explicitly state that the empty string ("") is returned if "not applicable" // for the following methods: // getSchemaName() // getTableName() // getCatalogName() // Since the empty string is a valid string and is not really a proper table name, schema name, or catalog name, // we're not sure why the empty string was chosen over null, except possibly to be friendly to lazy jdbc apps // that may not be checking for nulls, thereby minimizing potential NPE's. // By induction, it would make sense to return the empty string when column name is not available/applicable. // // The JDBC specification contains blanket statements about SQL compliance levels, // so elaboration within the JDBC specification is often bypassed. // Personally, I would prefer to return Java null for all the not-applicable cases, // but it appears that we have precedent for the empty ("") string. // // We assume a straightforward induction from jdbc spec that the column name be "" (empty) // in preference to null or NULL for the not applicable case. // if (sqlName_ == null || sqlName_[column - 1] == null) { assignColumnName(column); } return sqlName_[column - 1]; } public String getSchemaName(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); if (sqlxSchema_ == null || sqlxSchema_[column - 1] == null) { return ""; // Per jdbc spec } return sqlxSchema_[column - 1]; } public int getPrecision(int column) throws SqlException { checkForClosedStatement(); checkForValidColumnIndex(column); int jdbcType = types_[column - 1];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?