/****************************************************************************
** (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.sqc 
**    
** SAMPLE: How to grant, display, and revoke privileges 
**           
** SQL STATEMENTS USED:
**         GRANT (Table, View, or Nickname Privileges)
**         SELECT
**         REVOKE (Table, View, or Nickname Privileges)
**
**                           
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing embedded SQL applications see the Developing Embedded SQL Applications book.
**
** For information on using SQL statements, see the SQL Reference.
**
** For the latest information on programming, building, and running DB2 
** applications, visit the DB2 Information Center: 
**     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include "utilemb.h"

int TbPrivGrant(void);
int TbPrivDisplay(void);
int TbPrivRevoke(void);
int TbSchemaNameGet(void);
int TbColumnInfoGet(void);

EXEC SQL BEGIN DECLARE SECTION;
  char granteetype[2];
  char controlauth[2];
  char alterauth[2];
  char deleteauth[2];
  char indexauth[2];
  char insertauth[2];
  char selectauth[2];
  char refauth[2];
  char updateauth[2];
EXEC SQL END DECLARE SECTION;

int main(int argc, char *argv[])
{
  int rc = 0;
  char dbAlias[SQL_ALIAS_SZ + 1];
  char user[USERID_SZ + 1];
  char pswd[PSWD_SZ + 1];

  /* check the command line arguments */
  rc = CmdLineArgsCheck1(argc, argv, dbAlias, user, pswd);
  if (rc != 0)
  {
    return rc;
  }

  printf("\nHOW TO GRANT/DISPLAY/REVOKE TABLE PRIVILEGES.\n");

  /* connect to the database */
  rc = DbConn(dbAlias, user, pswd);
  if (rc != 0)
  {
    return rc;
  }

  rc = TbPrivGrant();
  rc = TbPrivDisplay();
  rc = TbPrivRevoke();

  /* disconnect from the database */
  rc = DbDisconn(dbAlias);
  if (rc != 0)
  {
    return rc;
  }

  return 0;
} /* main */

int TbPrivGrant(void)
{
  struct sqlca sqlca;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE SQL STATEMENTS:\n");
  printf("  GRANT (Table, View, or Nickname Privileges)\n");
  printf("  COMMIT\n");
  printf("TO GRANT PRIVILEGES AT TABLE LEVEL.\n");

  printf("\n  GRANT SELECT, INSERT, UPDATE(salary, comm)\n"
         "      ON TABLE staff\n"
         "      TO USER user1\n");

  EXEC SQL GRANT SELECT, INSERT, UPDATE(salary, comm)
    ON TABLE staff
    TO USER user1;
  EMB_SQL_CHECK("privileges at table level -- grant");

  printf("  COMMIT\n");

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  return 0;
} /* TbPrivGrant */

int TbPrivDisplay(void)
{
  struct sqlca sqlca;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE SQL STATEMENT:\n");
  printf("  SELECT INTO\n");
  printf("TO DISPLAY PRIVILEGES AT TABLE LEVEL.\n");

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

  EXEC SQL SELECT granteetype, controlauth, alterauth, deleteauth,
                  indexauth, insertauth, selectauth, refauth, updateauth
    INTO :granteetype, :controlauth, :alterauth, :deleteauth, :indexauth,
         :insertauth, :selectauth, :refauth, :updateauth
    FROM syscat.tabauth
    WHERE grantee = 'USER1' AND tabname = 'STAFF';
  EMB_SQL_CHECK("privileges at table level -- get");

  printf("\n  Grantee Type     = %c\n", granteetype[0]);
  printf("  CONTROL priv.    = %c\n", controlauth[0]);
  printf("  ALTER priv.      = %c\n", alterauth[0]);
  printf("  DELETE priv.     = %c\n", deleteauth[0]);
  printf("  INDEX priv.      = %c\n", indexauth[0]);
  printf("  INSERT priv.     = %c\n", insertauth[0]);
  printf("  SELECT priv.     = %c\n", selectauth[0]);
  printf("  REFERENCES priv. = %c\n", refauth[0]);
  printf("  UPDATE priv.     = %c\n", updateauth[0]);

  return 0;
} /* TbPrivDisplay */

int TbPrivRevoke(void)
{
  struct sqlca sqlca;

  printf("\n-----------------------------------------------------------");
  printf("\nUSE THE SQL STATEMENTS:\n");
  printf("  REVOKE (Table, View, or Nickname Privileges)\n");
  printf("  COMMIT\n");
  printf("TO REVOKE PRIVILEGES AT TABLE LEVEL.\n");

  printf("\n  REVOKE SELECT, INSERT, UPDATE ON TABLE staff FROM USER user1");
  printf("\n");

  EXEC SQL REVOKE SELECT, INSERT, UPDATE ON TABLE staff FROM USER user1;
  EMB_SQL_CHECK("privileges at table level -- revoke");

  printf("  COMMIT\n");

  EXEC SQL COMMIT;
  EMB_SQL_CHECK("transaction -- commit");

  return 0;
} /* TbPrivRevoke */