/ * * $ ID: TEST.CC, V 1.4 2004/03/13 15:05:50 JPR5 EXP $ * /
#include
Extern "C" {#include
#include "orapp.hh"
/ * * PURPOSE: * * This is a test script that creates a trigger, sequence and table * with `` test_ '' as a prefix (to avoid any collisions with * pre-existing tables on the specific TNS), and tests various * functionality of the ORAPP API against it. When completed, the * program will clean up after itself and should leave the TNS in * the original state (before the script was run). * * Please note that if the test program does not complete For * Whatever Reason (Crash, Network Failure, Valgrind), IT Will Clean * Up on Startup Also, In Case Any of The Old `` Test_ * '' stuff is * laying arround. * /
Const char * TABLE_DROP = "DROP TABLE TEST_TABLE"; const char * TABLE_CREATE = "CREATE TABLE TEST_TABLE (/ N" "ID Number Default '0' NOT NULL, / N" "str varchar2 (250) Default 'NOT NULL / N "" ""
Const char * sequence_drop = "drop sequence test_sequence"; const char * sequence_create = "create sequence test_sequence / n" "increment by 1 start with 1 nominvalue NomaxValue Nocache";
const char * trigger_DROP = "DROP TRIGGER test_TRIGGER"; const char * trigger_CREATE = "CREATE OR REPLACE TRIGGER test_TRIGGER / n" "BEFORE INSERT ON test_TABLE / n" "FOR EACH ROW / n" "BEGIN / n" "SELECT test_SEQUENCE.NEXTVAL INTO: New.id from Dual; / N "" End; "; BOOL ORAPP_CONNECT (ORAPP :: Connection & DB, Const Char * TNS, Const Char * User, Const Char * Pass) {Printf (" *** connecting TO% S /% S @% S / N ", User, Pass, TNS);
IF (! DB.Connect (TNS, User, Pass) {Printf (">>> ("> DB.Error (). C_STR ()); Return False;}
PRINTF ("*** connected to:% s / n", db.version (). c_str ());
Return True;}
BOOL ORAPP_DISCONNECT (ORAPP :: Connection & DB) {Printf ("*** disconnecting ... / n");
IF (! db.disconnect ()) {printf (">>> (Failed to disconnect) / n"); return false;}
Return True;}
BOOL ORAPP_SETUP (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
Printf ("*** setting up test_table (safe to ignore errors about missing objects) / n");
IF (! Q-> EXECUTE (TRIGGER_DROP)) Printf (">>> (ORAPP_SETUP)% S [% S] / N", db.error (). c_str (), q-> statement ());
IF (! Q-> EXECUTE (SEQUENCE_DROP)) Printf (">>> (ORAPP_SETUP)% S [% S] / n", db.Error (). c_str (), q-> statement ());
IF (! Q-> EXECUTE (Table_Drop)) Printf (">>> (ORAPP_SETUP)% S [% s] / n", db.Error (). c_str (), q-> statement ());
IF (! Q-> EXECUTE (Table_create) ||! Q-> EXECUTE (SEQUENCE_CREATE) ||! Q-> EXECUTE (TRIGGER_CREATE)) {Printf (">>> (ORAPP_SETUP)% S [% s] / n" , db.error (). c_str (), q-> statement ()); return false;} printf ("*** done / n");
Return True;}
Bool ORAPP_UNSETUP (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
Printf ("*** tearing down test_table / n");
IF (! Q-> EXECUTE (Trigger_Drop) ||! Q-> EXECUTE (SEQUENCE_DROP) ||! Q-> EXECUTE (Table_Drop)) {Printf (">>> (ORAPP_UNSETUP)% S [% s] / n" , db. Error (). c_str (), q-> statement ()); return false;}
Printf ("*** done / n");
Return True;}
Void ORAPP_SELECT_STRAIGHT (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "SELECT ID, STR from Test_Table";
Printf ("*** executing select:% s / n", q-> statement ());
IF (! Q-> EXECUTE ()) {PrintF (">>> (" >>), db.Error (). c_str ()); RETURN;}
ORAPP :: ROW * R;
While ((r = q-> fetch ())) {printf ("::: rows fetched =% u, width =% u / n", q-> rows (), r-> width ());
Unsigned i; for (i = 0; i
i = 42; Printf ("::: row% u NAMED% S (Should Be Null) / N", I, R-> Name (i));
Printf ("::::" ID =% u, str = [% s], unknown = [% s] / n ", (unsigned) (* r) [" ID "], (const char *) (Const char *) * r) ["Str"], (const char *) ("unknown"]); printf (":::: (row) ID =% u, str = [% s], unknown = [% S] / n ", (unsigned) (* r) [0], (const char *) (* r) [1], (const char *) (* r) [50]); }printf (" ** * DONE / N ");
Void ORAPP_SELECT_BIND_READ (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "SELECT ID, STR" << "from test_table" << "where id =: id";
Printf ("*** executing select:% s / n", q-> statement ());
Unsigned ID = 1;
Q-> Bind (": id", id);
IF (! Q-> EXECUTE ()) {PrintF (">>> (" >>), db.Error (). c_str ()); RETURN;}
ORAPP :: ROW * R;
While ((r = q-> fetch ())) {printf ("::: rows fetched =% u / n", q-> rows ()); printf ("::::" ID =% U , str = [% s], unknown = [% s] / n ", (unsigned) (* r) [" ID "], (const char *) (* r) [" str "], (const char * (* r) ["unknown"]); Printf (":::: (row) ID =% u, str = [% s], unknown = [% s] / n", (unsigned) (* r) [0] (const char *) (* r) [1], (const char *) (* r) [50]);
Printf ("*** done / n");}
Void ORAPP_SELECT_BIND_WRITE (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "begin" << "SELECT ID, STR INTO: ID,: STR" << "from test_table" << "where id = 2;" << "end;"
UNSIGNED ID; Char BUF [20] = {0};
Printf ("*** executing select:% s / n", q-> statement ());
Q-> Bind (": id", ID); Q-> Bind (": str", buf, sizeof (buf));
IF (! Q-> EXECUTE ()) {PrintF (">>> (" >>), db.Error (). c_str ()); RETURN;}
Printf ("::: ID =% U, STR = [% s] / n", ID, buf);
Printf ("*** done / n");}
Void ORAPP_SELECT_BIND_BOTH (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "" begin "<<" SELECT ID, STR INTO: ID,: STR "<<" from test_table "<<" WHERE ID =: in_ID; "<<" End; "
Printf ("*** executing select:% s / n", q-> statement ());
Unsigned in_id = 3, id; char buf [20] = {0};
Q-> bind (": in_id", in_id); Q-> Bind (": id", ID); Q-> Bind (": str", buf, sizeof (buf));
IF (! Q-> EXECUTE ()) {PrintF (">>> (" >>), db.Error (). c_str ()); RETURN;}
Printf ("::: ID =% U, STR = [% s] / n", ID, buf);
Printf ("*** done / n");}
Void ORAPP_UPDATE_STRAIGHT (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "Update Test_Table Set Str = 'Meep Meep' Where ID = 9";
Printf ("*** executing update:% s / n", q-> statement ());
IF (! q-> execute ()) {printf (">>> (" >> () Db.Error (). c_str ()); Return;}
Printf ("*** done / n");}
Void ORAPP_UPDATE_BIND_READ (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "Update test_table set str = 'meep meep' where id =: id";
Printf ("*** executing update:% s / n", q-> statement ()); unsigned id = 10;
Q-> Bind (": id", id);
IF (! q-> execute ()) {printf (">>> (" >> () Db.Error (). c_str ()); Return;}
Printf ("*** done / n");}
Void ORAPP_UPDATE_BIND_WRITE (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "Update Test_Table Set Str =: Str where id = 11";
Printf ("*** executing update:% s / n", q-> statement ());
Char * buf = "OOGA BOOGA";
Q-> Bind (": str", buf);
IF (! q-> execute ()) {printf (">>> (" >> () Db.Error (). c_str ()); Return;}
Printf ("*** done / n");}
Void ORAPP_UPDATE_BIND_BOTH (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "Update test_table set str =: str where id =: id";
Printf ("*** executing update:% s / n", q-> statement ());
UNSIGNED ID = 12; char * buf = "ooga booga";
Q-> Bind (": id", id); Q-> Bind (": str", buf);
IF (! q-> execute ()) {printf (">>> (" >> () Db.Error (). c_str ()); Return;}
Printf ("*** done / n");}
Void ORAPP_DELETE_STRAIGHT (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "delete from test_table where id = 14";
Printf ("*** executing delete:% s / n", q-> statement ());
IF (! Q-> EXECUTE ()) {Printf (">>> (" >>), db.error (). c_str ()); return;}
Printf ("*** done / n");}
Void ORAPP_DELETE_BIND (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query (); * q << "delete from test_table where id =: id";
Printf ("*** executing delete:% s / n", q-> statement ());
UNSIGNED ID = 15;
Q-> Bind (": id", id);
IF (! Q-> EXECUTE ()) {Printf (">>> (" >>), db.error (). c_str ()); return;}
Printf ("*** done / n");}
Void ORAPP_POPULATE_TABLE (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
Printf ("*** populating table / n");
If (! q-> execute ("INSERT INTO TEST_TABLE (STR) VALUES ('One')") ||! q-> execute ("INSERT INTO TEST_TABLE (STR) VALUES ('Two')") ||! q -> EXECUTE ("INSERT INTO TEST_TABLE (STR) VALUES (" INTO Test_TABLE (STR) VALUES ("INTO TEST_TABLE (STR) VALUES ('Four')") Printf (">>> Populate failed)% S / N ", db.Error (). c_str ());
Void ORAPP_WIPE_TABLE (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
Printf ("*** wiping table / n");
Q-> EXECUTE ("delete from test_table");
Void ORAPP_INSERT_STRAIGHT (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "INSERT INTO TEST_TABLE" << "(STR)" << "VALUES ('hi mom')"
Printf ("*** executing INSERT:% S / N", Q-> Statement ()); if (! q-> execute ()) Printf (">>> (INSERT FAILED)% S / N", DB (). c_str ());
* Q << "Insert INTO TEST_TABLE" << "(STR)" << "VALUES ('Bye Mom')"
Printf ("*** executing INSERT:% S / N", Q-> Statement ()); if (! q-> execute ()) Printf (">>> (INSERT FAILED)% S / N", DB (). c_str ()); Printf ("*** done / n");}
Void ORAPP_INSERT_BIND (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
* Q << "INSERT INTO TEST_TABLE (STR) VALUES (: STR)";
Printf ("*** executing insert:% s / n", q-> statement ());
Char * buf1 = "OOGA";
Q-> Bind (": str", buf1);
IF (! q> execute ()) Printf (">>> (" >>> () DB.Error (). c_str ());
Printf ("*** executing insert:% s / n", q-> statement ());
CHAR * BUF2 = "booga";
* Q << "INSERT INTO TEST_TABLE (STR) VALUES (: STR)";
Q-> Bind (": str", buf2);
IF (! q> execute ()) Printf (">>> (" >>> () DB.Error (). c_str ());
Printf ("*** done / n");}
Void ORAPP_FEATURE_ASSIGN (ORAPP :: Connection & DB) {ORAPP :: query * q = db.query ();
Q-> Assign ("SELECT% U As One,% F AS TWO,% S As Three from Dual", 1, 2., "3");
Printf ("*** executing select:% s / n", q-> statement ());
IF (! Q-> EXECUTE ()) {PrintF (">>> (" >>), db.Error (). c_str ()); RETURN;}
ORAPP :: ROW * R = Q-> fetch (); if (! R) {Printf (">> failed to get row / n "); return;}
Printf ("::: one =% s, two =% s, three =% s, unknown = [% s] / n", (const char *) (* r) ["one"], (const char * (* r) ["Two"], (const char *) (* r) ["through"] (* r) ["unknown"]);
Printf ("*** done / n");}
Void logfunc (const char * s) {printf (">>> [% s] / n", s);} int usage (const char * argv) {printf ("USAGE:% s <-hc> <-u Username> <-p Password> <-t tnsname> / n ", argv);
Printf ("/ TH / Tusage Information (What You're Reading / N"); Printf ("/ TC / Trun Test Continuously"); Printf ("/ TU / TUSERNAME WITH TABLE, SEQUENCE And Trigger Privileges / N "); Printf (" / TP / TPASSWORD WITH / N "); Printf (" / TT / TTNSNAME OF DB TO USE for Tests / N ");
Return 1;}
void give_explanation (void) {
Printf ("/ n" "*** / n" "NOTE: The purpose of this program is to excercise the features of the / n" "ORAPP API. if it does not terminate in error, the all tests / n" " Completed successfully. this program will es "*** / n" "/ n"); "*** / n" "/ n");
Sleep (5);
}
INT Main (int Argc, char ** argv) {IF ("Oracle_Home" == NULL) {Printf ("Error: Oracle_Home Unset, bailing ../ n"); return 1;}
Const char * TNS = "", * user = "", * pass = ""; bool continuous = false;
Char C; WHILE ((C = Getopt (Argc, Argv, "HCT: U: P:"))! = EOF) Switch (c) {CASE 'T': TNS = OPTARG; Break; Case 'u': user = OPTARG; Break; Case 'p': pass = OPTARG; Break; Case 'c': Continuous = true; break; default: return usage (argv [0]);}
IF (! * tns ||! * user ||! * pass) {printf ("error: missing tnsname, username or password / n"); return usage (argv [0]);} give_explanation ();
ORAPP :: Log_to (logfunc); ORAPP :: Connection DB;
Printf ("- ORAPP API V% S / N", ORAPP :: Version;
IF (! ORAPP_CONNECT (DB, TNS, USER, PASS) RETURN 1;
DO {if (! ORAPP_SETUP (DB)) Return 1;
/ * * First, Some Basic SELECT TESTS. * /
ORAPP_POPULATE_TABLE (DB);
ORAPP_SELECT_STRAIGHT (DB); ORAPP_SELECT_BIND_READ (DB); ORAPP_SELECT_BIND_WRITE (DB); ORAPP_SELECT_BIND_BOTH (DB);
ORAPP_WIPE_TABLE (DB);
/ * * TEST INSERT FUNCTIONAL. * /
ORAPP_INSERT_STRAIGHT (DB); ORAPP_INSERT_BIND (DB);
ORAPP_SELECT_STRAIGHT (DB);
ORAPP_WIPE_TABLE (DB);
/ * * Test Update functionAlity. * /
ORAPP_POPULATE_TABLE (DB);
ORAPP_UPDATE_STRAIGHT (DB); ORAPP_UPDATE_BIND_READ (DB); ORAPP_UPDATE_BIND_WRITE (DB); ORAPP_UPDATE_BIND_BOTH (DB);
ORAPP_SELECT_STRAIGHT (DB);
ORAPP_WIPE_TABLE (DB);
/ * * Test delete functionality. * /
ORAPP_POPULATE_TABLE (DB);
ORAPP_DELETE_STRAIGHT (DB); ORAPP_DELETE_BIND (DB);
ORAPP_SELECT_STRAIGHT (DB);
ORAPP_WIPE_TABLE (DB);
/ * * Ooh, neato. * /
ORAPP_FEATURE_ASSIGN (DB);
/ * * We OUT. * /
IF (! ORAPP_UNSETUP (DB)) Return 1;
WHILE (Continuous);
IF (! ORAPP_DISCONNECT (DB)) RETURN 1;
Printf ("--- done / n");
Return 0;}