Logo Search packages:      
Sourcecode: mysql-connector-java version File versions

java.sql.ResultSet com::mysql::jdbc::DatabaseMetaData::getColumnPrivileges ( String  catalog,
String  schema,
String  table,
String  columnNamePattern 
) throws SQLException [inline]

Get a description of the access rights for a table's columns.

Only privileges matching the column name criteria are returned. They are ordered by COLUMN_NAME and PRIVILEGE.

Each privilige description has the following columns:

  1. TABLE_CAT String => table catalog (may be null)
  2. TABLE_SCHEM String => table schema (may be null)
  3. TABLE_NAME String => table name
  4. COLUMN_NAME String => column name
  5. GRANTOR => grantor of access (may be null)
  6. GRANTEE String => grantee of access
  7. PRIVILEGE String => name of access (SELECT, INSERT, UPDATE, REFRENCES, ...)
  8. IS_GRANTABLE String => "YES" if grantee is permitted to grant to others; "NO" if not; null if unknown

Parameters:
catalog a catalog name; "" retrieves those without a catalog
schema a schema name; "" retrieves those without a schema
table a table name
columnNamePattern a column name pattern
Returns:
ResultSet each row is a column privilege description
Exceptions:
SQLException if a database access error occurs
See also:
getSearchStringEscape

Definition at line 507 of file DatabaseMetaData.java.

References com::mysql::jdbc::Statement::close(), com::mysql::jdbc::ResultSet::close(), conn, com::mysql::jdbc::Connection::createStatement(), com::mysql::jdbc::Statement::executeQuery(), com::mysql::jdbc::ResultSet::getString(), com::mysql::jdbc::ConnectionProperties::getUseHostsInPrivileges(), com::mysql::jdbc::ResultSet::next(), s2b(), and com::mysql::jdbc::Statement::setEscapeProcessing().

                            {
        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 ");

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

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

        Statement stmt = null;
        ResultSet results = null;
        ArrayList grantRows = new ArrayList();

        try {
            stmt = this.conn.createStatement();
            stmt.setEscapeProcessing(false);
            results = stmt.executeQuery(grantQuery.toString());

            while (results.next()) {
                String host = results.getString(1);
                String db = 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) && 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);
    }


Generated by  Doxygen 1.6.0   Back to index