/****************************************************************************
** (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, compiling, and running DB2
** applications, visit the DB2 Information Center at
** http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <sqlenv.h>
#include <sqlutil.h>
#include "utilemb.h"
#if ((__cplusplus >= 199711L) && !defined DB2HP && !defined DB2AIX) || \
(DB2LINUX && (__LP64__ || (__GNUC__ >= 3)) )
#include <iostream>
using namespace std;
#else
#include <iostream.h>
#endif
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;
class TbPriv
{
public:
int Grant();
int Display();
int Revoke();
};
int TbPriv::Grant()
{
struct sqlca sqlca;
cout << "\n-----------------------------------------------------------";
cout << "\nUSE THE SQL STATEMENTS:" << endl;
cout << " GRANT (Table, View, or Nickname Privileges)" << endl;
cout << " COMMIT" << endl;
cout << "TO GRANT PRIVILEGES AT TABLE LEVEL." << endl;
cout << "\n GRANT SELECT, INSERT, UPDATE(salary, comm)" << endl
<< " ON TABLE staff" << endl
<< " TO USER user1" << endl;
EXEC SQL GRANT SELECT, INSERT, UPDATE(salary, comm)
ON TABLE staff
TO USER user1;
EMB_SQL_CHECK("privileges at table level -- grant");
cout << " COMMIT" << endl;
EXEC SQL COMMIT;
EMB_SQL_CHECK("transaction -- commit");
return 0;
} //TbPriv::Grant
int TbPriv::Display()
{
struct sqlca sqlca;
cout << "\n-----------------------------------------------------------";
cout << "\nUSE THE SQL STATEMENT:" << endl;
cout << " SELECT INTO" << endl;
cout << "TO DISPLAY PRIVILEGES AT TABLE LEVEL." << endl;
cout << "\n SELECT granteetype, controlauth, alterauth, deleteauth,"
<< "\n indexauth, insertauth, selectauth, refauth, updateauth"
<< "\n FROM syscat.tabauth"
<< "\n WHERE grantee = 'USER1' AND tabname = 'STAFF'" << endl;
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");
cout << "\n Grantee Type = " << granteetype[0] << endl;
cout << " CONTROL priv. = " << controlauth[0] << endl;
cout << " ALTER priv. = " << alterauth[0] << endl;
cout << " DELETE priv. = " << deleteauth[0] << endl;
cout << " INDEX priv. = " << indexauth[0] << endl;
cout << " INSERT priv. = " << insertauth[0] << endl;
cout << " SELECT priv. = " << selectauth[0] << endl;
cout << " REFERENCES priv. = " << refauth[0] << endl;
cout << " UPDATE priv. = " << updateauth[0] << endl;
return 0;
} //TbPriv::Display
int TbPriv::Revoke()
{
struct sqlca sqlca;
cout << "\n-----------------------------------------------------------";
cout << "\nUSE THE SQL STATEMENTS:" << endl;
cout << " REVOKE (Table, View, or Nickname Privileges)" << endl;
cout << " COMMIT" << endl;
cout << "TO REVOKE PRIVILEGES AT TABLE LEVEL." << endl;
cout << "\n REVOKE SELECT, INSERT, UPDATE"
<< " ON TABLE staff FROM USER user1" << endl;
EXEC SQL REVOKE SELECT, INSERT, UPDATE ON TABLE staff FROM USER user1;
EMB_SQL_CHECK("privileges at table level -- revoke");
cout << " COMMIT" << endl;
EXEC SQL COMMIT;
EMB_SQL_CHECK("transaction -- commit");
return 0;
} //TbPriv::Revoke
int main(int argc, char *argv[])
{
int rc = 0;
CmdLineArgs check;
TbPriv priv;
DbEmb db;
// check the command line arguments
rc = check.CmdLineArgsCheck1(argc, argv, db);
if (rc != 0)
{
return rc;
}
cout << "\nTHIS SAMPLE SHOWS HOW TO GRANT/DISPLAY/REVOKE PRIVILEGES."
<< endl;
// connect to the database
rc = db.Connect();
if (rc != 0)
{
return rc;
}
rc = priv.Grant();
rc = priv.Display();
rc = priv.Revoke();
// disconnect from the database
rc = db.Disconnect();
if (rc != 0)
{
return rc;
}
return 0;
} //main