📄 databasemetadata.java
字号:
if ((user == null) || (user.length() == 0)) { user = "%"; } StringBuffer fullUser = new StringBuffer(user); if ((host != null) && this.conn.getUseHostsInPrivileges()) { fullUser.append("@"); fullUser.append(host); } String columnName = results.getString(6); String allPrivileges = results.getString(7); if (allPrivileges != null) { allPrivileges = allPrivileges.toUpperCase(Locale.ENGLISH); StringTokenizer st = new StringTokenizer(allPrivileges, ","); while (st.hasMoreTokens()) { String privilege = st.nextToken().trim(); byte[][] tuple = new byte[8][]; tuple[0] = s2b(db); tuple[1] = null; tuple[2] = s2b(table); tuple[3] = s2b(columnName); if (grantor != null) { tuple[4] = s2b(grantor); } else { tuple[4] = null; } tuple[5] = s2b(fullUser.toString()); tuple[6] = s2b(privilege); tuple[7] = null; grantRows.add(tuple); } } } } finally { if (results != null) { try { results.close(); } catch (Exception ex) { ; } results = null; } if (stmt != null) { try { stmt.close(); } catch (Exception ex) { ; } stmt = null; } } return buildResultSet(fields, grantRows); } /** * 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> * <P> * Each column description has the following columns: * <OL> * <li> <B>TABLE_CAT</B> String => table catalog (may be null) </li> * <li> <B>TABLE_SCHEM</B> String => table schema (may be null) </li> * <li> <B>TABLE_NAME</B> String => table name </li> * <li> <B>COLUMN_NAME</B> String => column name </li> * <li> <B>DATA_TYPE</B> short => SQL type from java.sql.Types </li> * <li> <B>TYPE_NAME</B> String => Data source dependent type name </li> * <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> * <li> <B>BUFFER_LENGTH</B> is not used. </li> * <li> <B>DECIMAL_DIGITS</B> int => the number of fractional digits </li> * <li> <B>NUM_PREC_RADIX</B> int => Radix (typically either 10 or 2) </li> * <li> <B>NULLABLE</B> int => is NULL allowed? * <UL> * <li> columnNoNulls - might not allow NULL values </li> * <li> columnNullable - definitely allows NULL values </li> * <li> columnNullableUnknown - nullability unknown </li> * </ul> * </li> * <li> <B>REMARKS</B> String => comment describing column (may be null) * </li> * <li> <B>COLUMN_DEF</B> String => default value (may be null) </li> * <li> <B>SQL_DATA_TYPE</B> int => unused </li> * <li> <B>SQL_DATETIME_SUB</B> int => unused </li> * <li> <B>CHAR_OCTET_LENGTH</B> int => for char types the maximum number * of bytes in the column </li> * <li> <B>ORDINAL_POSITION</B> int => index of column in table (starting * at 1) </li> * <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. </li> * </ol> * </p> * * @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 * @throws SQLException * if a database access error occurs * @see #getSearchStringEscape */ public java.sql.ResultSet getColumns(final String catalog, final String schemaPattern, final String tableNamePattern, String columnNamePattern) throws SQLException { if (columnNamePattern == null) { if (this.conn.getNullNamePatternMatchesAll()) { columnNamePattern = "%"; } else { throw new SQLException( "Column name pattern can not be NULL or empty.", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } } final String colPattern = columnNamePattern; Field[] fields = new Field[18]; fields[0] = new Field("", "TABLE_CAT", Types.CHAR, 255); fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 0); fields[2] = new Field("", "TABLE_NAME", Types.CHAR, 255); 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); final ArrayList rows = new ArrayList(); final Statement stmt = this.conn.getMetadataSafeStatement(); try { new IterateBlock(getCatalogIterator(catalog)) { void forEach(Object catalogStr) throws SQLException { ArrayList tableNameList = new ArrayList(); if (tableNamePattern == null) { // Select from all tables java.sql.ResultSet tables = null; try { tables = getTables(catalog, schemaPattern, "%", new String[0]); while (tables.next()) { String tableNameFromList = tables .getString("TABLE_NAME"); tableNameList.add(tableNameFromList); } } finally { if (tables != null) { try { tables.close(); } catch (Exception sqlEx) { AssertionFailedException .shouldNotHappen(sqlEx); } tables = null; } } } else { java.sql.ResultSet tables = null; try { tables = getTables(catalog, schemaPattern, tableNamePattern, new String[0]); while (tables.next()) { String tableNameFromList = tables .getString("TABLE_NAME"); tableNameList.add(tableNameFromList); } } finally { if (tables != null) { try { tables.close(); } catch (SQLException sqlEx) { AssertionFailedException .shouldNotHappen(sqlEx); } tables = null; } } } java.util.Iterator tableNames = tableNameList.iterator(); while (tableNames.hasNext()) { String tableName = (String) tableNames.next(); ResultSet results = null; try { StringBuffer queryBuf = new StringBuffer("SHOW "); if (conn.versionMeetsMinimum(4, 1, 0)) { queryBuf.append("FULL "); } queryBuf.append("COLUMNS FROM "); queryBuf.append(quotedId); queryBuf.append(tableName); queryBuf.append(quotedId); queryBuf.append(" FROM "); queryBuf.append(quotedId); queryBuf.append(catalogStr.toString()); queryBuf.append(quotedId); queryBuf.append(" LIKE '"); queryBuf.append(colPattern); queryBuf.append("'"); // Return correct ordinals if column name pattern is // not '%' // Currently, MySQL doesn't show enough data to do // this, so we do it the 'hard' way...Once _SYSTEM // tables are in, this should be much easier boolean fixUpOrdinalsRequired = false; Map ordinalFixUpMap = null; if (!colPattern.equals("%")) { fixUpOrdinalsRequired = true; StringBuffer fullColumnQueryBuf = new StringBuffer( "SHOW "); if (conn.versionMeetsMinimum(4, 1, 0)) { fullColumnQueryBuf.append("FULL "); } fullColumnQueryBuf.append("COLUMNS FROM "); fullColumnQueryBuf.append(quotedId); fullColumnQueryBuf.append(tableName); fullColumnQueryBuf.append(quotedId); fullColumnQueryBuf.append(" FROM "); fullColumnQueryBuf.append(quotedId); fullColumnQueryBuf .append(catalogStr.toString()); fullColumnQueryBuf.append(quotedId); results = stmt.executeQuery(fullColumnQueryBuf .toString()); ordinalFixUpMap = new HashMap(); int fullOrdinalPos = 1; while (results.next()) { String fullOrdColName = results .getString("Field"); ordinalFixUpMap.put(fullOrdColName, new Integer(fullOrdinalPos++)); } } results = stmt.executeQuery(queryBuf.toString()); int ordPos = 1; while (results.next()) { byte[][] rowVal = new byte[18][]; rowVal[0] = s2b(catalog); // TABLE_CAT rowVal[1] = null; // TABLE_SCHEM (No schemas // in MySQL) rowVal[2] = s2b(tableNamePattern); // TABLE_NAME rowVal[3] = results.getBytes("Field"); TypeDescriptor typeDesc = new TypeDescriptor( results.getString("Type"), results .getString("Null")); rowVal[4] = Short.toString(typeDesc.dataType) .getBytes(); // DATA_TYPE (jdbc) rowVal[5] = s2b(typeDesc.typeName); // TYPE_NAME // (native) rowVal[6] = s2b(Integer .toString(typeDesc.columnSize)); rowVal[7] = s2b(Integer .toString(typeDesc.bufferLength)); rowVal[8] = s2b(Integer .toString(typeDesc.decimalDigits)); rowVal[9] = s2b(Integer .toString(typeDesc.numPrecRadix)); rowVal[10] = s2b(Integer .toString(typeDesc.nullability)); // // Doesn't always have this field, depending on // version // // // REMARK column // try { if (conn.versionMeetsMinimum(4, 1, 0)) { rowVal[11] = results .getBytes("Comment"); } else { rowVal[11] = results.getBytes("Extra"); } } catch (Exception E) { rowVal[11] = new byte[0]; } // COLUMN_DEF rowVal[12] = results.getBytes("Default"); 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 // ORDINAL_POSITION if (!fixUpOrdinalsRequired) { rowVal[16] = Integer.toString(ordPos++) .getBytes(); } else { String origColName = results .getString("Field"); Integer realOrdinal = (Integer) ordinalFixUpMap .get(origColName); if (realOrdinal != null) { rowVal[16] = realOrdinal.toString() .getBytes(); } else { throw new SQLException( "Can not find column in full column list to determine true ordinal position.", SQLError.SQL_STATE_GENERAL_ERROR); } } rowVal[17] = s2b(typeDesc.isNullable); rows.add(rowVal); } } finally { if (results != null) { try { results.close(); } catch (Exception ex) { ; } results = null; } } } } }.doForAll(); } finally { if (stmt != null) { stmt.close(); } } java.sql.ResultSet results = buildResultSet(fields, rows); return results; } /** * JDBC 2.0 Return the connection that produced this metadata object. * * @return the connection that produced this metadata object. * @throws SQLException * if a database error occurs */ public java.sql.Connection getConnection() throws SQLException { return this.conn; } /** * Get a description of the foreign key columns in the foreign key table * that reference the primary key columns of the primary key table (describe * how one table imports another's key.) This should normally return a * single foreign key/primary key pair (most tables only import a foreign * key from a table once.) They are ordered by FKTABLE_CAT, FKTABLE_SCHEM, * FKTABLE_NAME, and KEY_SEQ. * <P> * Each foreign key column description has the following columns: * <OL> * <li> <B>PKTABLE_CAT</B> String => primary key table catalog (may be * null) </li> * <li> <B>PKTABLE_SCHEM</B> String => primary key table schema (may be * null) </li> * <li> <B>PKTABLE_NAME</B> String => primary key table name </li> * <li> <B>PKCOLUMN_NAME</B> String => primary key column name </li> * <li> <B>FKTABLE_CAT</B> String => foreign key table catalog (may be * null) being exported (may be null) </li> * <li> <B>FKTABLE_SCHEM</B> String => foreign
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -