//***************************************************************************
// (c) Copyright IBM Corp. 2007 All rights reserved.
// 
// The following sample of source code ("Sample") is owned by International 
// Business Machines Corporation or one of its subsidiaries ("IBM") and is 
// copyrighted and licensed, not sold. You may use, copy, modify, and 
// distribute the Sample in any form without payment to IBM, for the purpose of 
// assisting you in the development of your applications.
// 
// The Sample code is provided to you on an "AS IS" basis, without warranty of 
// any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR 
// IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do 
// not allow for the exclusion or limitation of implied warranties, so the above 
// limitations or exclusions may not apply to you. IBM shall not be liable for 
// any damages you suffer as a result of using, copying, modifying or 
// distributing the Sample, even if IBM has been advised of the possibility of 
// such damages.
//***************************************************************************
//
// SOURCE FILE NAME: TbPriv.sqlj
//
// SAMPLE: How to grant, display and revoke privileges on a table
//
// SQL Statements USED:
//         GRANT
//         SELECT
//         REVOKE
//         COMMIT
//
// Classes used from Util.sqlj are:
//         Db
//         SqljException
//
//                           
// Output will vary depending on the JDBC driver connectivity used.
//***************************************************************************
//
// For more information on the sample programs, see the README file.
//
// For information on developing Java applications see the Developing Java Applications book.
//
// For information on using SQL statements, see the SQL Reference.
//
// For the latest information on programming, compiling, and running DB2
// applications, visit the DB2 Information Center at
//     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
//**************************************************************************/

import java.lang.*;
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;

#sql iterator TbPriv_Cursor(String, String, String, String, String,
                            String, String, String, String);

class TbPriv
{
  public static void main(String argv[])
  {
    try
    {
      Db db = new Db(argv);

      System.out.println();
      System.out.println(
        "THIS SAMPLE SHOWS HOW TO GRANT, DISPLAY AND REVOKE \n" +
        "PRIVILEGES ON A TABLE.");

      // connect to the 'sample' database
      db.getDefaultContext();

      grant();
      display();
      revoke();

      // disconnect from the 'sample' database
      db.disconnect();
    }
    catch (Exception e)
    {
      SqljException sqljExc = new SqljException(e);
      sqljExc.handle();
    }
  } // main

  static void grant()
  {
    System.out.println();
    System.out.println(
      "----------------------------------------------------------\n" +
      "USE THE SQL STATEMENTS:\n" +
      "  GRANT\n" +
      "  COMMIT\n" +
      "TO GRANT PRIVILEGES ON A TABLE.");

    System.out.println();
    System.out.println(
      "  GRANT SELECT, INSERT, UPDATE(salary, comm)\n" +
      "    ON TABLE staff\n" +
      "    TO USER user1");

    try
    {
      #sql {GRANT SELECT, INSERT, UPDATE(salary, comm)
               ON TABLE staff
               TO USER user1};

      System.out.println();
      System.out.println("  COMMIT");

      #sql {COMMIT};
    }
    catch (Exception e)
    {
      SqljException sqljExc = new SqljException(e);
      sqljExc.handle();
    }
  } // grant

  static void display()
  {
    System.out.println();
    System.out.println(
      "----------------------------------------------------------\n" +
      "USE THE SQL STATEMENT:\n" +
      "  SELECT\n" +
      "TO DISPLAY PRIVILEGES ON A TABLE.");

    System.out.println();
    System.out.println(
      "  SELECT granteetype, controlauth, alterauth,\n" +
      "         deleteauth, indexauth, insertauth,\n" +
      "         selectauth, refauth, updateauth\n" +
      "    FROM syscat.tabauth\n" +
      "    WHERE grantee = 'USER1' AND\n" +
      "          tabname = 'STAFF'");

    try
    {
      TbPriv_Cursor cur;
      String granteetype = null;
      String controlauth = null;
      String alterauth = null;
      String deleteauth = null;
      String indexauth = null;
      String insertauth = null;
      String selectauth = null;
      String refauth = null;
      String updateauth = null;

      #sql cur = {SELECT granteetype, controlauth, alterauth,
                         deleteauth, indexauth, insertauth,
                         selectauth, refauth, updateauth
                    FROM syscat.tabauth
                    WHERE grantee = 'USER1' AND
                          tabname = 'STAFF'};

      #sql {FETCH :cur INTO :granteetype, :controlauth, :alterauth,
                            :deleteauth, :indexauth, :insertauth,
                            :selectauth, :refauth, :updateauth};

      System.out.println();
      System.out.println(
        "  Grantee Type     = " + granteetype + "\n" +
        "  CONTROL priv.    = " + controlauth + "\n" +
        "  ALTER priv.      = " + alterauth + "\n" +
        "  DELETE priv.     = " + deleteauth + "\n" +
        "  INDEX priv.      = " + indexauth + "\n" +
        "  INSERT priv.     = " + insertauth + "\n" +
        "  SELECT priv.     = " + selectauth + "\n" +
        "  REFERENCES priv. = " + refauth + "\n" +
        "  UPDATE priv.     = " + updateauth);

    }
    catch (Exception e)
    {
      SqljException sqljExc = new SqljException(e);
      sqljExc.handle();
    }
  } // display

  static void revoke()
  {
    System.out.println();
    System.out.println(
      "----------------------------------------------------------\n" +
      "USE THE SQL STATEMENTS:\n" +
      "  REVOKE\n" +
      "  COMMIT\n" +
      "TO REVOKE PRIVILEGES ON A TABLE.");

    System.out.println();
    System.out.println(
      "  REVOKE SELECT, INSERT, UPDATE\n" +
      "    ON TABLE staff\n" +
      "    FROM USER user1");

    try
    {
      #sql {REVOKE SELECT, INSERT, UPDATE
              ON TABLE staff
              FROM USER user1};

      System.out.println();
      System.out.println("  COMMIT");

      #sql {COMMIT};
    }
    catch (Exception e)
    {
      SqljException sqljExc = new SqljException(e);
      sqljExc.handle();
    }
  } // revoke
} // TbPriv