⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databasemetadata.java

📁 SearchPathServer
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

			while (Tables.next())
			{
				String TN = Tables.getString("TABLE_NAME");

				TableNameList.addElement(TN);

				if (TN.length() > tablename_length)
				{
					tablename_length = TN.length();
				}
			}
			Tables.close();
		}

		int catalog_length = 0;

		if (Catalog != null)
		{
			catalog_length = Catalog.length();
		}
		else
		{
			Catalog = "";
			catalog_length = 0;
		}

		java.util.Enumeration TableNames = TableNameList.elements();

		Field[] Fields = new Field[18];

		Fields[0] = new Field("", "TABLE_CAT", Types.CHAR, catalog_length);
		Fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 0);
		Fields[2] = new Field("", "TABLE_NAME", Types.CHAR, tablename_length);
		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);

		Vector Tuples = new Vector();

		while (TableNames.hasMoreElements())
		{

			String TableNamePattern = (String) TableNames.nextElement();

			com.mysql.jdbc.ResultSet RS =
				_conn.execSQL(
					"show columns from "
						+ TableNamePattern
						+ DB_Sub
						+ " like '"
						+ ColumnNamePattern
						+ "'",
					-1);
			RS.setConnection(_conn);

			java.sql.ResultSetMetaData RSMD = RS.getMetaData();

			int ord_pos = 1;

			while (RS.next())
			{
				byte[][] RowVal = new byte[18][];

				RowVal[0] = Catalog.getBytes(); // TABLE_CAT
				RowVal[1] = new byte[0]; // TABLE_SCHEM (No schemas in MySQL)

				RowVal[2] = TableNamePattern.getBytes(); // TABLE_NAME
				RowVal[3] = RS.getBytes("Field");

				String TypeInfo = RS.getString("Type");
				if (Driver.debug)
				{
					System.out.println("Type: " + TypeInfo);
				}
				String MysqlType = "";

				if (TypeInfo.indexOf("(") != -1)
				{
					MysqlType = TypeInfo.substring(0, TypeInfo.indexOf("("));
				}
				else
				{
					MysqlType = TypeInfo;
				}

				if (_conn.capitalizeDBMDTypes())
				{
					MysqlType = MysqlType.toUpperCase();
				}

				/* 
				 * Convert to XOPEN (thanks JK)
				 */

				RowVal[4] = Integer.toString(MysqlDefs.mysqlToJavaType(MysqlType)).getBytes();
				// DATA_TYPE (jdbc)

				RowVal[5] = MysqlType.getBytes(); // TYPE_NAME (native)

				// Figure Out the Size

				if (TypeInfo != null)
				{
					if (TypeInfo.indexOf("enum") != -1 || TypeInfo.indexOf("set") != -1)
					{
						String Temp =
							TypeInfo.substring(TypeInfo.indexOf("("), TypeInfo.lastIndexOf(")"));

						java.util.StringTokenizer ST = new java.util.StringTokenizer(Temp, ",");

						int max_length = 0;

						while (ST.hasMoreTokens())
						{
							max_length = Math.max(max_length, (ST.nextToken().length() - 2));
						}
						RowVal[6] = Integer.toString(max_length).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] = Size.getBytes();
						RowVal[8] = Decimals.getBytes();
					}
					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(")")));
						}
						/* Otherwise resort to defaults */
						else if (TypeInfo.toLowerCase().equals("tinyint"))
						{
							Size = "1";
						}
						else if (TypeInfo.toLowerCase().equals("smallint"))
						{
							Size = "6";
						}
						else if (TypeInfo.toLowerCase().equals("mediumint"))
						{
							Size = "6";
						}
						else if (TypeInfo.toLowerCase().equals("int"))
						{
							Size = "11";
						}
						else if (TypeInfo.toLowerCase().equals("integer"))
						{
							Size = "11";
						}
						else if (TypeInfo.toLowerCase().equals("bigint"))
						{
							Size = "25";
						}
						else if (TypeInfo.toLowerCase().equals("int24"))
						{
							Size = "25";
						}
						else if (TypeInfo.toLowerCase().equals("real"))
						{
							Size = "12";
						}
						else if (TypeInfo.toLowerCase().equals("float"))
						{
							Size = "12";
						}
						else if (TypeInfo.toLowerCase().equals("decimal"))
						{
							Size = "12";
						}
						else if (TypeInfo.toLowerCase().equals("numeric"))
						{
							Size = "12";
						}
						else if (TypeInfo.toLowerCase().equals("double"))
						{
							Size = "22";
						}
						else if (TypeInfo.toLowerCase().equals("char"))
						{
							Size = "1";
						}
						else if (TypeInfo.toLowerCase().equals("varchar"))
						{
							Size = "255";
						}
						else if (TypeInfo.toLowerCase().equals("date"))
						{
							Size = "10";
						}
						else if (TypeInfo.toLowerCase().equals("time"))
						{
							Size = "8";
						}
						else if (TypeInfo.toLowerCase().equals("timestamp"))
						{
							Size = "19";
						}
						else if (TypeInfo.toLowerCase().equals("datetime"))
						{
							Size = "19";
						}
						else if (TypeInfo.toLowerCase().equals("tinyblob"))
						{
							Size = "255";
						}
						else if (TypeInfo.toLowerCase().equals("blob"))
						{
							Size = Integer.toString(Math.min(65535, MysqlIO.getMaxBuf()));
						}
						else if (TypeInfo.toLowerCase().equals("mediumblob"))
						{
							Size = Integer.toString(Math.min(16277215, MysqlIO.getMaxBuf()));
						}
						else if (TypeInfo.toLowerCase().equals("longblob"))
						{
							Size =
								(Integer.toString(MysqlIO.getMaxBuf()).compareTo("2147483657") < 0
									? Integer.toString(MysqlIO.getMaxBuf())
									: "2147483657");
						}
						else if (TypeInfo.toLowerCase().equals("tinytext"))
						{
							Size = "255";
						}
						else if (TypeInfo.toLowerCase().equals("text"))
						{
							Size = "65535";
						}
						else if (TypeInfo.toLowerCase().equals("mediumtext"))
						{
							Size = Integer.toString(Math.min(16277215, MysqlIO.getMaxBuf()));
						}
						else if (TypeInfo.toLowerCase().equals("enum"))
						{
							Size = "255";
						}
						else if (TypeInfo.toLowerCase().equals("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.MAXBUF).getBytes(); // BUFFER_LENGTH
				RowVal[9] = new byte[] {(byte) '1', (byte) '0' };
				// NUM_PREC_RADIX (is this right for char?)

				String Nullable = RS.getString("Null");

				// Nullable?

				if (Nullable != null)
				{
					if (Nullable.equals("YES"))
					{
						RowVal[10] =
							Integer.toString(java.sql.DatabaseMetaData.columnNullable).getBytes();
						RowVal[17] = new String("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] = RS.getString("Extra").getBytes();
				}
				catch (Exception E)
				{
					RowVal[11] = new byte[0];
				}

				// COLUMN_DEF
				byte[] Default = RS.getBytes("Default");

				if (Default != null)
				{
					RowVal[12] = Default;
				}
				else
				{
					RowVal[12] = new byte[0];
				}

				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
				RowVal[16] = Integer.toString(ord_pos++).getBytes(); // ORDINAL_POSITION

				Tuples.addElement(RowVal);
			}
			RS.close();
		}

		java.sql.ResultSet Results = buildResultSet(Fields, Tuples, _conn);
		return Results;
	}

	/**
	 * Get a description of the access rights for a table's columns.
	 *
	 * <P>Only privileges matching the column name criteria are
	 * returned.  They are ordered by COLUMN_NAME and PRIVILEGE.
	 *
	 * <P>Each privilige description has the following columns:
	 *  <OL>
	 *    <LI><B>TABLE_CAT</B> String => table catalog (may be null)
	 *    <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
	 *    <LI><B>TABLE_NAME</B> String => table name
	 *    <LI><B>COLUMN_NAME</B> String => column name
	 *    <LI><B>GRANTOR</B> => grantor of access (may be null)
	 *    <LI><B>GRANTEE</B> String => grantee of access
	 *    <LI><B>PRIVILEGE</B> String => name of access (SELECT,
	 *      INSERT, UPDATE, REFRENCES, ...)
	 *    <LI><B>IS_GRANTABLE</B> String => "YES" if grantee is permitted
	 *      to grant to others; "NO" if not; null if unknown
	 *  </OL>
	 *
	 * @param catalog a catalog name; "" retrieves those without a catalog
	 * @param schema a schema name; "" retrieves those without a schema
	 * @param table a table name
	 * @param columnNamePattern a column name pattern
	 * @return ResultSet each row is a column privilege description
	 * @see #getSearchStringEscape
	 */

	public java.sql.ResultSet getColumnPrivileges(
		String catalog,
		String schema,
		String table,
		String columnNamePattern)
		throws java.sql.SQLException
	{
		Field[] fields = new Field[8];

		fields[0] = new Field("", "TABLE_CAT", Types.CHAR, 64);
		fields[1] = new Field("", "TABLE_SCHEM", Types.CHAR, 1);
		fields[2] = new Field("", "TABLE_NAME", Types.CHAR, 64);
		fields[3] = new Field("", "COLUMN_NAME", Types.CHAR, 64);
		fields[4] = new Field("", "GRANTOR", Types.CHAR, 77);
		fields[5] = new Field("", "GRANTEE", Types.CHAR, 77);
		fields[6] = new Field("", "PRIVILEGE", Types.CHAR, 64);
		fields[7] = new Field("", "IS_GRANTABLE", Types.CHAR, 3);

		StringBuffer grantQuery =
			new StringBuffer("SELECT c.host, c.db, t.grantor, c.user, c.table_name, c.column_name, c.column_priv from mysql.columns_priv c, mysql.tables_priv t where c.host = t.host and c.db = t.db and c.table_name = t.table_name ");

		grantQuery.append(" WHERE ");

		if (catalog != null && catalog.length() != 0)
		{
			grantQuery.append(" c.db='");
			grantQuery.append(catalog);
			grantQuery.append("' AND ");
		}

		grantQuery.append("c.table_name ='");
		grantQuery.append(table);
		grantQuery.append("' AND c.column_name like '");
		grantQuery.append(columnNamePattern);

		com.mysql.jdbc.ResultSet results = null;

		Vector grantRows = new Vector();

		try
		{
			results = _conn.execSQL(grantQuery.toString(), -1);

			results.setConnection(_conn);

			while (results.next())
			{
				String host = results.getString(1);
				String database = results.getString(2);
				String grantor = results.getString(3);
				String user = results.getString(4);

				if (user == null || user.length() == 0)
				{
					user = "%";
				}

				StringBuffer fullUser = new StringBuffer(user);

				if (host != null)
				{
					fullUser.append("@");
					fullUser.append(host);
				}

				String columnName = results.getString(6);

				String allPrivileges = results.getString(7);

				if (allPrivileges != null)
				{
					allPrivileges = allPrivileges.toUpperCase();

					StringTokenizer st = new StringTokenizer(allPrivileges, ",");

					while (st.hasMoreTokens())
					{
						String privilege = st.nextToken().trim();

						byte[][] tuple = new byte[8][];

						tuple[0] = s2b(database);
						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.addElement(tuple);
					}
				}
			}
		}
		finally
		{
			if (results != null)
			{
				try
				{
					results.close();
				}
				catch (Exception ex)
				{
				}
			}
		}

		return buildResultSet(fields, grantRows, _conn);
	}

	/**
	 * Get a description of the access rights for each table available
	 * in a catalog.
	 *
	 * <P>Only privileges matching the schema and table name
	 * criteria are returned.  They are ordered by TABLE_SCHEM,
	 * TABLE_NAME, and PRIVILEGE.
	 *
	 * <P>Each privilige description has the following columns:
	 *  <OL>
	 *    <LI><B>TABLE_CAT</B> String => table catalog (may be null)
	 *    <LI><B>TABLE_SCHEM</B> String => table schema (may be null)
	 *    <LI><B>TABLE_NAME</B> String => table name
	 *    <LI><B>COLUMN_NAME</B> String => column name
	 *    <LI><B>GRANTOR</B> => grantor of access (may be null)
	 *    <LI><B>GRANTEE</B> String => grantee of access
	 *    <LI><B>PRIVILEGE</B> String => name of access (SELECT,
	 *      INSERT, UPDATE, REFRENCES, ...)
	 *    <LI><B>IS_GRANTABLE</B> String => "YES" if grantee is permitted
	 *      to grant to others; "NO" if not; null if unknown
	 *  </OL>
	 *
	 * @param catalog a catalog name; "" retrieves those without a catalog
	 * @param schemaPattern a schema name pattern; "" retrieves those
	 * without a schema
	 * @param tableNamePa

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -