// Source File Name = update.sqC
//
// Licensed Materials - Property of IBM
//
// (C) COPYRIGHT International Business Machines Corp. 1995, 1997
// All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
//
// PURPOSE: This sample program demonstrates the use of static SQL.
// It updates all managers in the STAFF table of the
// SAMPLE database and changes their job from 'Mgr' to
// 'Clerk', deletes all who are 'Sales', and inserts a
// row. In all three SQL statements (UPDATE, DELETE, INSERT)
// a host variable is implemented.
// Finally, a ROLLBACK is done so that the SAMPLE
// database remains unchanged.
//
// For more information about these samples see the README file.
//
// For more information on programming in C++, see the
// - "Programming in C and C++" section of the Application Development Guide
//
// For more information on building C++ applications, see the:
// - "Building C++ Applications" section of the Application Building Guide.
//
// For more information on the SQL language see the SQL Reference.
//
// For more information about these samples see the README file.
//
// For more information on programming in C++, see the
// - "Programming in C and C++" section of the Application Development Guide
//
// For more information on building C++ applications, see the:
// - "Building C++ Applications" section of the Application Building Guide.
//
// For more information on the SQL language see the SQL Reference.
//
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <sqlenv.h>
#include <sqlca.h>
#include "util.h"
#define CHECKERR(CE_STR) if (check_error (CE_STR, &sqlca) != 0)
class Update {
public:
Update ();
Update (char *, char *, char *);
UpdateStaff (char *);
DeleteStaff (char *);
~Update();
private:
EXEC SQL BEGIN DECLARE SECTION;
char dbname[9];
char userid[9];
char passwd[19];
char jobUpdate[6];
EXEC SQL END DECLARE SECTION;
struct sqlca sqlca;
};
Update::Update () {
cout << "Connect to database with default userid and password\n";
EXEC SQL CONNECT TO sample;
CHECKERR ("CONNECT TO") exit(1);
}
Update::Update (char *dbname, char *userid, char *passwd) {
cout << "Connecting to database " << dbname << '\n';
EXEC SQL CONNECT TO :dbname USER :userid USING :passwd;
CHECKERR ("CONNECT TO SAMPLE") exit(1);
}
Update::UpdateStaff(char *jobUpdate) {
EXEC SQL UPDATE staff SET job = :jobUpdate WHERE job = 'Mgr';
CHECKERR ("UPDATE STAFF") return 1;
cout << "All 'Mgr' have been demoted to '" << jobUpdate << "'!\n";
return 0;
}
Update::DeleteStaff(char *jobUpdate) {
EXEC SQL DELETE FROM staff WHERE job = :jobUpdate;
CHECKERR ("DELETE FROM STAFF") return 1;
cout << "All '" << jobUpdate << "' people have been deleted!\n";
return 0;
}
Update::~Update() {
EXEC SQL ROLLBACK;
CHECKERR ("ROLLBACK") exit(1);
cout << "On second thought ... changes rolled back\n";
cout << "Connect resetting from database\n";
EXEC SQL CONNECT RESET;
}
int main (int argc, char *argv[]) {
cout << "Sample C++ program : updat.sqC\n";
if (argc == 3) {
Update updateSample ("SAMPLE", argv[1], argv[2]);
updateSample.UpdateStaff("Clerk");
updateSample.DeleteStaff("Sales");
} else if (argc == 1) {
Update updateSample;
updateSample.UpdateStaff("Clerk");
updateSample.DeleteStaff("Sales");
} else {
cout << "\nUSAGE : updat [userid password]\n\n";
} // end if
return 0;
} // end of program : update.sqC