#ifndef S_ORACLE # include #endif /************************************************************************** *This is a DEMO program. To test, compile the file to generate the executable *demoquery. Then demoquery can be invoked from a command prompt. *It will have the following output: Initializing OCI Process Registering query : select last_name, employees.department_id, department_name from employees, departments where employee_id = 200 and employees.department_id = departments.department_id Query Id 23 Waiting for Notifications *Then from another session, log in as AEGIS/AEGIS and perform the following * DML transactions. It will cause two notifications to be generated. update departments set department_name ='Global Admin' where department_id=10; commit; update departments set department_name ='Adminstration' where department_id=10; commit; *The demoquery program will now show the following output corresponding *to the notifications received. Query 23 is changed Table changed is AEGIS.DEPARTMENTS table_op 4 Row changed is AAAMBoAABAAAKX2AAA row_op 4 Query 23 is changed Table changed is AEGIS.DEPARTMENTS table_op 4 Row changed is AAAMBoAABAAAKX2AAA row_op 4 *The demo program waits for exactly 10 notifications to be received before *logging off and unregistering the subscription. ***************************************************************************/ /*--------------------------------------------------------------------------- PRIVATE TYPES AND CONSTANTS ---------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------- STATIC FUNCTION DECLARATIONS ---------------------------------------------------------------------------*/ #include #include #include #include #define MAXSTRLENGTH 1024 #define bit(a,b) ((a)&(b)) static int notifications_processed = 0; static OCISubscription *subhandle1 = (OCISubscription *)0; static OCISubscription *subhandle2 = (OCISubscription *)0; static void checker(/*_ OCIError *errhp, sword status _*/); static void registerQuery(/*_ OCISvcCtx *svchp, OCIError *errhp, OCIStmt *stmthp, OCIEnv *envhp _*/); static void myCallback (/*_ dvoid *ctx, OCISubscription *subscrhp, dvoid *payload, ub4 *payl, dvoid *descriptor, ub4 mode _*/); static int NotificationDriver(/*_ int argc, char *argv[] _*/); static sword status; static boolean logged_on = FALSE; static void processRowChanges(OCIEnv *envhp, OCIError *errhp, OCIStmt *stmthp, OCIColl *row_changes); static void processTableChanges(OCIEnv *envhp, OCIError *errhp, OCIStmt *stmthp, OCIColl *table_changes); static void processQueryChanges(OCIEnv *envhp, OCIError *errhp, OCIStmt *stmthp, OCIColl *query_changes); static int nonractests2(/*_ int argc, char *argv[] _*/); int main(int argc, char **argv) { NotificationDriver(argc, argv); return 0; } int NotificationDriver(argc, argv) int argc; char *argv[]; { OCIEnv *envhp; OCISvcCtx *svchp, *svchp2; OCIError *errhp, *errhp2; OCISession *authp, *authp2; OCIStmt *stmthp, *stmthp2; OCIDuration dur, dur2; int i; dvoid *tmp; OCISession *usrhp; OCIServer *srvhp; int ctr = 0; printf("Initializing OCI Process\n"); /* Initialize the environment. The environment has to be initialized with OCI_EVENTS and OCI_OBJECTS to create a continuous query notification registration and receive notifications. */ OCIEnvCreate( (OCIEnv **) &envhp, OCI_EVENTS|OCI_OBJECT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t)) 0, (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0 ); OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) 0); /* server contexts */ OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0); OCIHandleAlloc((dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0); checker(errhp,OCIServerAttach(srvhp, errhp, (text *) "AEGIS", (sb4)strlen("AEGIS"), (ub4) OCI_DEFAULT)); /* set attribute server context in the service context */ OCIAttrSet( (dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *)srvhp, (ub4) 0, (ub4) OCI_ATTR_SERVER, (OCIError *) errhp); /* allocate a user context handle */ OCIHandleAlloc((dvoid *)envhp, (dvoid **)&usrhp, (ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0); OCIAttrSet((dvoid *)usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)((text *)"aegis"), (ub4)strlen((char *)"aegis"), OCI_ATTR_USERNAME, errhp); OCIAttrSet((dvoid *)usrhp, (ub4)OCI_HTYPE_SESSION, (dvoid *)((text *)"aegis"), (ub4)strlen((char *)"aegis"), OCI_ATTR_PASSWORD, errhp); printf("OCISessionBegin ..."); checker(errhp,OCISessionBegin (svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT)); /* Allocate a statement handle */ OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp, (ub4) OCI_HTYPE_STMT, 52, (dvoid **) &tmp); OCIAttrSet((dvoid *)svchp, (ub4)OCI_HTYPE_SVCCTX, (dvoid *)usrhp, (ub4)0, OCI_ATTR_SESSION, errhp); registerQuery(svchp, errhp, stmthp, envhp); printf("Waiting for Notifications\n"); while (notifications_processed !=10) { sleep(1); ctr++; printf("ctr:%d\n", ctr); if (ctr == 120) { break; } } printf ("Going to unregister AEGIS\n"); fflush(stdout); /* Unregister AEGIS */ printf("OCISubscriptionUnRegister ..."); checker(errhp, OCISubscriptionUnRegister(svchp, subhandle1, errhp, OCI_DEFAULT)); checker(errhp, OCISessionEnd(svchp, errhp, usrhp, (ub4) 0)); printf("AEGIS Logged off.\n"); if (subhandle1) OCIHandleFree((dvoid *)subhandle1, OCI_HTYPE_SUBSCRIPTION); if (stmthp) OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT); if (srvhp) OCIHandleFree((dvoid *) srvhp, (ub4) OCI_HTYPE_SERVER); if (svchp) OCIHandleFree((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX); if (authp) OCIHandleFree((dvoid *) usrhp, (ub4) OCI_HTYPE_SESSION); if (errhp) OCIHandleFree((dvoid *) errhp, (ub4) OCI_HTYPE_ERROR); if (envhp) OCIHandleFree((dvoid *) envhp, (ub4) OCI_HTYPE_ENV); return 0; } void checker(errhp, status) OCIError *errhp; sword status; { text errbuf[512]; sb4 errcode = 0; int retval = 1; switch (status) { case OCI_SUCCESS: retval = 0; (void) printf("OCI_SUCCESS\n"); break; case OCI_SUCCESS_WITH_INFO: (void) printf("Error - OCI_SUCCESS_WITH_INFO\n"); break; case OCI_NEED_DATA: (void) printf("Error - OCI_NEED_DATA\n"); break; case OCI_NO_DATA: (void) printf("Error - OCI_NODATA\n"); break; case OCI_ERROR: (void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); (void) printf("Error - %.*s\n", 512, errbuf); break; case OCI_INVALID_HANDLE: (void) printf("Error - OCI_INVALID_HANDLE\n"); break; case OCI_STILL_EXECUTING: (void) printf("Error - OCI_STILL_EXECUTE\n"); break; case OCI_CONTINUE: (void) printf("Error - OCI_CONTINUE\n"); break; default: (void) printf("Unsupported OCI return code\n"); break; } if (retval) { exit(1); } } void processRowChanges(OCIEnv *envhp, OCIError *errhp, OCIStmt *stmthp, OCIColl *row_changes) { dvoid **row_descp; dvoid *row_desc; boolean exist; ub2 i, j; dvoid *elemind = (dvoid *)0; oratext *row_id; ub4 row_op; sb4 num_rows; if (!row_changes) return; checker(errhp, OCICollSize(envhp, errhp, (CONST OCIColl *) row_changes, &num_rows)); for (i=0; i