📄 databasemetadata.java
字号:
* 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 tableName a table name pattern * @param columnNamePattern a column name pattern * * @return ResultSet each row is a column description * * @throws java.sql.SQLException if a database access error occurs * * @see #getSearchStringEscape */ public java.sql.ResultSet getColumns(String catalog, String schemaPattern, String tableName, String columnNamePattern) throws java.sql.SQLException { String databasePart = ""; if (columnNamePattern == null) { columnNamePattern = "%"; } if (catalog != null) { if (!catalog.equals("")) { databasePart = " FROM " + this.quotedId + catalog + this.quotedId; } } else { databasePart = " FROM " + this.quotedId + this.database + this.quotedId; } ArrayList tableNameList = new ArrayList(); int tablenameLength = 0; if (tableName == 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); if (tableNameFromList.length() > tablenameLength) { tablenameLength = tableNameFromList.length(); } } } 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, tableName, new String[0]); while (tables.next()) { String tableNameFromList = tables.getString("TABLE_NAME"); tableNameList.add(tableNameFromList); if (tableNameFromList.length() > tablenameLength) { tablenameLength = tableNameFromList.length(); } } } finally { if (tables != null) { try { tables.close(); } catch (SQLException sqlEx) { AssertionFailedException.shouldNotHappen(sqlEx); } tables = null; } } } int catalogLength = 0; byte[] connectionCatalogAsBytes = null; if (catalog != null) { catalogLength = catalog.length(); connectionCatalogAsBytes = s2b(catalog); } else { catalog = ""; connectionCatalogAsBytes = s2b(this.conn.getCatalog()); catalogLength = 0; } java.util.Iterator tableNames = tableNameList.iterator(); Field[] fields = new Field[18]; fields[0] = new Field("", "TABLE_CAT", Types.CHAR, catalogLength); fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 0); fields[2] = new Field("", "TABLE_NAME", Types.CHAR, tablenameLength); 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); ArrayList tuples = new ArrayList(); while (tableNames.hasNext()) { String tableNamePattern = (String) tableNames.next(); Statement stmt = null; ResultSet results = null; try { stmt = this.conn.createStatement(); if (stmt.getMaxRows() != 0) { stmt.setMaxRows(0); } StringBuffer queryBuf = new StringBuffer("SHOW COLUMNS FROM "); queryBuf.append(this.quotedId); queryBuf.append(tableNamePattern); queryBuf.append(this.quotedId); queryBuf.append(databasePart); queryBuf.append(" LIKE '"); queryBuf.append(columnNamePattern); queryBuf.append("'"); results = stmt.executeQuery(queryBuf.toString()); int ordPos = 1; while (results.next()) { byte[][] rowVal = new byte[18][]; rowVal[0] = connectionCatalogAsBytes; // TABLE_CAT rowVal[1] = null; // TABLE_SCHEM (No schemas in MySQL) rowVal[2] = s2b(tableNamePattern); // TABLE_NAME rowVal[3] = results.getBytes("Field"); String typeInfo = results.getString("Type"); if (Driver.DEBUG) { System.out.println("Type: " + typeInfo); } String mysqlType = ""; String fullMysqlType = null; if (typeInfo.indexOf("(") != -1) { mysqlType = typeInfo.substring(0, typeInfo.indexOf("(")); } else { mysqlType = typeInfo; } int indexOfUnsignedInMysqlType = mysqlType.toLowerCase().indexOf("unsigned"); if (indexOfUnsignedInMysqlType != -1) { mysqlType = mysqlType.substring(0, (indexOfUnsignedInMysqlType - 1)); } // Add unsigned to typename reported to enduser as 'native type', if present if (typeInfo.toLowerCase().indexOf("unsigned") != -1) { fullMysqlType = mysqlType + " unsigned"; } else { fullMysqlType = mysqlType; } if (this.conn.capitalizeDBMDTypes()) { fullMysqlType = fullMysqlType.toUpperCase(); } /* * Convert to XOPEN (thanks JK) */ rowVal[4] = Integer.toString(MysqlDefs.mysqlToJavaType( mysqlType)).getBytes(); // DATA_TYPE (jdbc) rowVal[5] = s2b(fullMysqlType); // TYPE_NAME (native) // 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)); } rowVal[6] = Integer.toString(maxLength).getBytes(); rowVal[8] = new byte[] { (byte) '0' }; } else if (typeInfo.indexOf(",") != -1) { // Numeric with decimals String size = typeInfo.substring((typeInfo.indexOf( "(") + 1), (typeInfo.indexOf(","))); String decimals = typeInfo.substring((typeInfo .indexOf(",") + 1), (typeInfo.indexOf(")"))); rowVal[6] = s2b(size); rowVal[8] = s2b(decimals); } else { String size = "0"; /* If the size is specified with the DDL, use that */ if (typeInfo.indexOf("(") != -1) { size = typeInfo.substring((typeInfo.indexOf("(") + 1), (typeInfo.indexOf(")"))); } else if (typeInfo.equalsIgnoreCase("tinyint")) { size = "1"; } else if (typeInfo.equalsIgnoreCase("smallint")) { size = "6"; } else if (typeInfo.equalsIgnoreCase("mediumint")) { size = "6"; } else if (typeInfo.equalsIgnoreCase("int")) { size = "11"; } else if (typeInfo.equalsIgnoreCase("integer")) { size = "11"; } else if (typeInfo.equalsIgnoreCase("bigint")) { size = "25"; } else if (typeInfo.equalsIgnoreCase("int24")) { size = "25"; } else if (typeInfo.equalsIgnoreCase("real")) { size = "12"; } else if (typeInfo.equalsIgnoreCase("float")) { size = "12"; } else if (typeInfo.equalsIgnoreCase("decimal")) { size = "12"; } else if (typeInfo.equalsIgnoreCase("numeric")) { size = "12"; } else if (typeInfo.equalsIgnoreCase("double")) { size = "22"; } else if (typeInfo.equalsIgnoreCase("char")) { size = "1"; } else if (typeInfo.equalsIgnoreCase("varchar")) { size = "255"; } else if (typeInfo.equalsIgnoreCase("date")) { size = "10"; } else if (typeInfo.equalsIgnoreCase("time")) { size = "8"; } else if (typeInfo.equalsIgnoreCase("timestamp")) { size = "19"; } else if (typeInfo.equalsIgnoreCase("datetime")) { size = "19"; } else if (typeInfo.equalsIgnoreCase("tinyblob")) { size = "255"; } else if (typeInfo.equalsIgnoreCase("blob")) { size = "65535"; } else if (typeInfo.equalsIgnoreCase("mediumblob")) { size = "16277215"; } else if (typeInfo.equalsIgnoreCase("longblob")) { size = Integer.toString(Integer.MAX_VALUE); } else if (typeInfo.equalsIgnoreCase("tinytext")) { size = "255"; } else if (typeInfo.equalsIgnoreCase("text")) { size = "65535"; } else if (typeInfo.equalsIgnoreCase("mediumtext")) { size = "16277215"; } else if (typeInfo.equalsIgnoreCase("longtext")) { size = Integer.toString(Integer.MAX_VALUE); } else if (typeInfo.equalsIgnoreCase("enum")) { size = "255"; } else if (typeInfo.equalsIgnoreCase("set")) { size = "255"; } rowVal[6] = size.getBytes(); rowVal[8] = new byte[] { (byte) '0' }; } } else { rowVal[8] = new byte[] { (byte) '0' }; rowVal[6] = new byte[] { (byte) '0' }; } rowVal[7] = Integer.toString(MysqlIO.getMaxBuf()).getBytes(); // BUFFER_LENGTH rowVal[9] = new byte[] { (byte) '1', (byte) '0' }; // NUM_PREC_RADIX (is this right for char?) String nullable = results.getString("Null"); // Nullable? if (nullable != null) { if (nullable.equals("YES")) { rowVal[10] = Integer.toString(java.sql.DatabaseMetaData.columnNullable) .getBytes(); rowVal[17] = "YES".getBytes(); // IS_NULLABLE } else { rowVal[10] = Integer.toString(java.sql.DatabaseMetaData.columnNoNulls) .getBytes(); rowVal[17] = "NO".getBytes(); } } else { rowVal[10] = Integer.toString(java.sql.DatabaseMetaData.columnNoNulls) .getBytes(); rowVal[17] = "NO".getBytes(); } // // Doesn't always have this field, depending on version // // // REMARK column // try { 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -