Java Code Examples for java.sql.DatabaseMetaData#getColumnPrivileges()

The following examples show how to use java.sql.DatabaseMetaData#getColumnPrivileges() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaResultSetTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testGetColumnPrivileges() throws SQLException {
  DatabaseMetaData metadata = getDatabaseMetadata();
  try (ResultSet rs = metadata.getColumnPrivileges(null, null, null, null)) {
    ResultSetMetaData rsMeta = rs.getMetaData();

    assertEquals(8, rsMeta.getColumnCount());
    assertColumn(rsMeta, 1, "TABLE_CAT", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 2, "TABLE_SCHEM", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 3, "TABLE_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 4, "COLUMN_NAME", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 5, "GRANTOR", Types.VARCHAR, DatabaseMetaData.columnNullable);
    assertColumn(rsMeta, 6, "GRANTEE", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 7, "PRIVILEGE", Types.VARCHAR, DatabaseMetaData.columnNoNulls);
    assertColumn(rsMeta, 8, "IS_GRANTABLE", Types.VARCHAR, DatabaseMetaData.columnNullable);
  }
}
 
Example 2
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for testing getColumnPrivileges - calls dmd.getIndexInfo for the
 * JDBC call, and getColumnPrivilegesODBC for the ODBC procedure
 * @throws SQLException
 */
private ResultSet[] getColumnPrivileges(String catalog, String schema, String table,
    String columnNamePattern) throws SQLException
{
    ResultSet[] rss = new ResultSet[2];
    DatabaseMetaData dmd = getDMD();
    rss[0]= dmd.getColumnPrivileges(catalog, schema, table, columnNamePattern);
    rss[1]= getColumnPrivilegesODBC(catalog, schema, table, columnNamePattern);

    String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
            "COLUMN_NAME","GRANTOR","GRANTEE","PRIVILEGE","IS_GRANTABLE"};
    int [] columnTypes = {
            Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
            Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
    boolean [] nullability = {false,false,false,false,false,false,false,false};

    assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
    assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);

    return rss;
}
 
Example 3
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for testing getColumnPrivileges - calls dmd.getIndexInfo for the
 * JDBC call, and getColumnPrivilegesODBC for the ODBC procedure
 * @throws SQLException
 */
private ResultSet[] getColumnPrivileges(String catalog, String schema, String table,
    String columnNamePattern) throws SQLException
{
    ResultSet[] rss = new ResultSet[2];
    DatabaseMetaData dmd = getDMD();
    rss[0]= dmd.getColumnPrivileges(catalog, schema, table, columnNamePattern);
    rss[1]= getColumnPrivilegesODBC(catalog, schema, table, columnNamePattern);

    String [] columnNames = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME",
            "COLUMN_NAME","GRANTOR","GRANTEE","PRIVILEGE","IS_GRANTABLE"};
    int [] columnTypes = {
            Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
            Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
    boolean [] nullability = {false,false,false,false,false,false,false,false};

    assertMetaDataResultSet(rss[0], columnNames, columnTypes, nullability);
    assertMetaDataResultSet(rss[1], columnNames, columnTypes, nullability);

    return rss;
}
 
Example 4
Source File: Show.java    From jsqsh with Apache License 2.0 6 votes vote down vote up
private ResultSet doColumn(Session session, Connection con, Options options)
    throws SQLException {
    
    if (options.arguments.size() < 2
        || options.arguments.size() > 3
        || !options.arguments.get(1).equalsIgnoreCase("privs")) {
        
        session.err.println("Use: \\show column privs [-p col-pattern] [[[catalog.]schema-pattern.]table-pattern]");
        return null;
    }
    
    SQLConnectionContext ctx = (SQLConnectionContext) session.getConnectionContext();
    SQLObjectName name =
        (options.arguments.size() == 3) ? new SQLObjectName(ctx, options.arguments.get(2))
            : new SQLObjectName(ctx, "%");
        
    DatabaseMetaData meta = con.getMetaData();
    return meta.getColumnPrivileges(
        (options.catalog != null ? options.catalog : name.getCatalog()),
        (options.schemaPattern != null ? options.schemaPattern : name.getSchema()),
        (options.tablePattern != null ? options.tablePattern : name.getName()),
        ctx.normalizeIdentifier(options.pattern));
}
 
Example 5
Source File: TestDatabaseMetaData.java    From evosql with Apache License 2.0 5 votes vote down vote up
/**
 * Basic test of DatabaseMetaData functions that access system tables
 */
public void testTwo() throws Exception {

    Connection conn = newConnection();
    int        updateCount;

    try {
        TestUtil.testScript(conn, "testrun/hsqldb/TestSelf.txt");

        DatabaseMetaData dbmeta = conn.getMetaData();

        dbmeta.allProceduresAreCallable();
        dbmeta.getBestRowIdentifier(null, null, "T_1",
                                    DatabaseMetaData.bestRowTransaction,
                                    true);
        dbmeta.getCatalogs();
        dbmeta.getColumnPrivileges(null, "PUBLIC", "T_1", "%");
        dbmeta.getColumns("PUBLIC", "PUBLIC", "T_1", "%");
        dbmeta.getCrossReference(null, null, "T_1", null, null, "T_1");
        dbmeta.getExportedKeys(null, null, "T_1");
        dbmeta.getFunctionColumns(null, "%", "%", "%");
        dbmeta.getFunctions(null, "%", "%");
        dbmeta.getImportedKeys("PUBLIC", "PUBLIC", "T_1");
        dbmeta.getIndexInfo("PUBLIC", "PUBLIC", "T1", true, true);
        dbmeta.getPrimaryKeys("PUBLIC", "PUBLIC", "T_1");
        dbmeta.getProcedureColumns(null, null, "%", "%");
        dbmeta.getProcedures("PUBLIC", "%", "%");
        dbmeta.getSchemas(null, "#");
        dbmeta.getTablePrivileges(null, "%", "%");
        dbmeta.getUDTs(null, "%", "%", new int[]{ Types.DISTINCT });
    } catch (Exception e) {
        assertTrue("unable to prepare or execute DDL", false);
    } finally {
        conn.close();
    }
}
 
Example 6
Source File: MetadataTest.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Tests the implementation of Information Schema for column privileges.
 */
public void testGetColumnPrivilegesUsingInfoSchema() throws Exception {
    String dontRunPropertyName = "com.mysql.jdbc.testsuite.cantGrant";

    if (!runTestIfSysPropDefined(dontRunPropertyName)) {
        if (versionMeetsMinimum(5, 0, 7)) {
            Properties props = new Properties();

            props.put("useInformationSchema", "true");
            Connection conn1 = null;
            Statement stmt1 = null;
            String userHostQuoted = null;

            boolean grantFailed = true;

            try {
                conn1 = getConnectionWithProps(props);
                stmt1 = conn1.createStatement();
                createTable("t1", "(c1 int)");
                this.rs = stmt1.executeQuery("SELECT USER()");
                this.rs.next();
                String user = this.rs.getString(1);
                List<String> userHost = StringUtils.split(user, "@", false);
                if (userHost.size() < 2) {
                    fail("This test requires a JDBC URL with a user, and won't work with the anonymous user. "
                            + "You can skip this test by setting the system property " + dontRunPropertyName);
                }
                userHostQuoted = "'" + userHost.get(0) + "'@'" + userHost.get(1) + "'";

                try {
                    stmt1.executeUpdate("GRANT update (c1) on t1 to " + userHostQuoted);

                    grantFailed = false;

                } catch (SQLException sqlEx) {
                    fail("This testcase needs to be run with a URL that allows the user to issue GRANTs "
                            + " in the current database. You can skip this test by setting the system property \"" + dontRunPropertyName + "\".");
                }

                if (!grantFailed) {
                    DatabaseMetaData metaData = conn1.getMetaData();
                    this.rs = metaData.getColumnPrivileges(null, null, "t1", null);
                    this.rs.next();
                    assertEquals("t1", this.rs.getString("TABLE_NAME"));
                    assertEquals("c1", this.rs.getString("COLUMN_NAME"));
                    assertEquals(userHostQuoted, this.rs.getString("GRANTEE"));
                    assertEquals("UPDATE", this.rs.getString("PRIVILEGE"));
                }
            } finally {
                if (stmt1 != null) {

                    if (!grantFailed) {
                        stmt1.executeUpdate("REVOKE UPDATE (c1) ON t1 FROM " + userHostQuoted);
                    }

                    stmt1.close();
                }

                if (conn1 != null) {
                    conn1.close();
                }
            }
        }
    }
}
 
Example 7
Source File: MetadataTest.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests the implementation of Information Schema for column privileges.
 */
public void testGetColumnPrivilegesUsingInfoSchema() throws Exception {
    String dontRunPropertyName = "com.mysql.jdbc.testsuite.cantGrant";

    if (!runTestIfSysPropDefined(dontRunPropertyName)) {
        if (versionMeetsMinimum(5, 0, 7)) {
            Properties props = new Properties();

            props.put("useInformationSchema", "true");
            Connection conn1 = null;
            Statement stmt1 = null;
            String userHostQuoted = null;

            boolean grantFailed = true;

            try {
                conn1 = getConnectionWithProps(props);
                stmt1 = conn1.createStatement();
                createTable("t1", "(c1 int)");
                this.rs = stmt1.executeQuery("SELECT USER()");
                this.rs.next();
                String user = this.rs.getString(1);
                List<String> userHost = StringUtils.split(user, "@", false);
                if (userHost.size() < 2) {
                    fail("This test requires a JDBC URL with a user, and won't work with the anonymous user. "
                            + "You can skip this test by setting the system property " + dontRunPropertyName);
                }
                userHostQuoted = "'" + userHost.get(0) + "'@'" + userHost.get(1) + "'";

                try {
                    stmt1.executeUpdate("GRANT update (c1) on t1 to " + userHostQuoted);

                    grantFailed = false;

                } catch (SQLException sqlEx) {
                    fail("This testcase needs to be run with a URL that allows the user to issue GRANTs "
                            + " in the current database. You can skip this test by setting the system property \"" + dontRunPropertyName + "\".");
                }

                if (!grantFailed) {
                    DatabaseMetaData metaData = conn1.getMetaData();
                    this.rs = metaData.getColumnPrivileges(null, null, "t1", null);
                    this.rs.next();
                    assertEquals("t1", this.rs.getString("TABLE_NAME"));
                    assertEquals("c1", this.rs.getString("COLUMN_NAME"));
                    assertEquals(userHostQuoted, this.rs.getString("GRANTEE"));
                    assertEquals("UPDATE", this.rs.getString("PRIVILEGE"));
                }
            } finally {
                if (stmt1 != null) {

                    if (!grantFailed) {
                        stmt1.executeUpdate("REVOKE UPDATE (c1) ON t1 FROM " + userHostQuoted);
                    }

                    stmt1.close();
                }

                if (conn1 != null) {
                    conn1.close();
                }
            }
        }
    }
}
 
Example 8
Source File: MetadataTest.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests the implementation of Information Schema for column privileges.
 */
public void testGetColumnPrivilegesUsingInfoSchema() throws Exception {

    if (!runTestIfSysPropDefined(PropertyDefinitions.SYSP_testsuite_cantGrant)) {
        Properties props = new Properties();

        props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "true");
        props.setProperty(PropertyKey.nullCatalogMeansCurrent.getKeyName(), "true");
        Connection conn1 = null;
        Statement stmt1 = null;
        String userHostQuoted = null;

        boolean grantFailed = true;

        try {
            conn1 = getConnectionWithProps(props);
            stmt1 = conn1.createStatement();
            createTable("t1", "(c1 int)");
            this.rs = stmt1.executeQuery("SELECT CURRENT_USER()");
            this.rs.next();
            String user = this.rs.getString(1);
            List<String> userHost = StringUtils.split(user, "@", false);
            if (userHost.size() < 2) {
                fail("This test requires a JDBC URL with a user, and won't work with the anonymous user. "
                        + "You can skip this test by setting the system property " + PropertyDefinitions.SYSP_testsuite_cantGrant);
            }
            userHostQuoted = "'" + userHost.get(0) + "'@'" + userHost.get(1) + "'";

            try {
                stmt1.executeUpdate("GRANT update (c1) on t1 to " + userHostQuoted);

                grantFailed = false;

            } catch (SQLException sqlEx) {
                fail("This testcase needs to be run with a URL that allows the user to issue GRANTs "
                        + " in the current database. You can skip this test by setting the system property \""
                        + PropertyDefinitions.SYSP_testsuite_cantGrant + "\".");
            }

            if (!grantFailed) {
                DatabaseMetaData metaData = conn1.getMetaData();
                this.rs = metaData.getColumnPrivileges(null, null, "t1", null);
                this.rs.next();
                assertEquals("t1", this.rs.getString("TABLE_NAME"));
                assertEquals("c1", this.rs.getString("COLUMN_NAME"));
                assertEquals(userHostQuoted, this.rs.getString("GRANTEE"));
                assertEquals("UPDATE", this.rs.getString("PRIVILEGE"));
            }
        } finally {
            if (stmt1 != null) {

                if (!grantFailed) {
                    stmt1.executeUpdate("REVOKE UPDATE (c1) ON t1 FROM " + userHostQuoted);
                }

                stmt1.close();
            }

            if (conn1 != null) {
                conn1.close();
            }
        }
    }
}