From: <whi...@li...> - 2007-03-10 13:36:59
|
Update of /cvsroot/whitebeam/whitebeam/templates/pgsql/audit-pgsql In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv16138 Modified Files: audit.cxx audit.h audit.skel audit.tmpl audit_stub.cxx Log Message: Add 'createMany' method that allows a number of audit records to be created in a single atomic operation. If any of the individual records can't be created the entire operation fails. Index: audit.cxx =================================================================== RCS file: /cvsroot/whitebeam/whitebeam/templates/pgsql/audit-pgsql/audit.cxx,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** audit.cxx 25 Feb 2007 14:57:46 -0000 1.5 --- audit.cxx 10 Mar 2007 13:36:25 -0000 1.6 *************** *** 355,363 **** /******************************************************************************** ! AUDIT_create ! Create a new audit record. If the new record is successfully created then this ! function returns the unique ID for that row in the table. ! ********************************************************************************/ ! static INT_64 AUDIT_create(CrbServerReq *req, const char *type, const char *entity_type, --- 355,367 ---- /******************************************************************************** ! AUDIT_createRecord ! Create a single audit record. This method assumes that the calling function ! starts and will commit/rollback the transaction as required. ! ! If the record is created then this method returns the ID of the new record. If ! the record cannot be created for whatever reason then the method returns -1. ! ********************************************************************************/ ! static INT_64 AUDIT_createRecord( ! CrbServerReq *req, const char *type, const char *entity_type, *************** *** 367,376 **** const char *customData) { ! RB_BOOL isOK=TRUE; ! const char *params[10]; ! INT_64 audit_id=-1; ! ! if (summary==NULL || *summary==0 || entity_type==NULL || *entity_type==0 || type==NULL || *type==0) ! return -1; char entityIdStr[40], contactIdStr[40]; --- 371,376 ---- const char *customData) { ! const char *params[10]; ! INT_64 audit_id=-1; char entityIdStr[40], contactIdStr[40]; *************** *** 382,391 **** params[2] = (const char *)entityIdStr; - // Check database connection - if (!db_connect()) - return -1; - - PQclear(PQexec(db_conn, "BEGIN;")); - // Check that the referenced entity actually exists. If not // then the record cannot be created. --- 382,385 ---- *************** *** 402,406 **** // Check the entity reference exists pg_res = db_exec_prepared(db_conn, checkReq, params); ! isOK = (pg_res!=NULL); if (isOK) { --- 396,400 ---- // Check the entity reference exists pg_res = db_exec_prepared(db_conn, checkReq, params); ! RB_BOOL isOK = (pg_res!=NULL); if (isOK) { *************** *** 457,462 **** } } ! if (isOK) PQclear(PQexec(db_conn, "COMMIT;")); else --- 451,485 ---- } } + return audit_id; + } ! /******************************************************************************** ! AUDIT_create ! Create a new audit record. If the new record is successfully created then this ! function returns the unique ID for that row in the table. ! ********************************************************************************/ ! static INT_64 AUDIT_create(CrbServerReq *req, ! const char *type, ! const char *entity_type, ! INT_64 entity_id, ! INT_64 contact_id, ! const char *summary, ! const char *customData) { ! ! RB_BOOL isOK=TRUE; ! INT_64 audit_id=-1; ! ! if (summary==NULL || *summary==0 || entity_type==NULL || *entity_type==0 || type==NULL || *type==0) ! return -1; ! ! // Check database connection ! if (!db_connect()) ! return -1; ! ! PQclear(PQexec(db_conn, "BEGIN;")); ! ! audit_id = AUDIT_createRecord(req,type,entity_type,entity_id,contact_id,summary,customData); ! ! if (audit_id>0) PQclear(PQexec(db_conn, "COMMIT;")); else *************** *** 465,468 **** --- 488,545 ---- return audit_id; } + + /******************************************************************************** + AUDIT_createMany + Create one or more audit records as a single atomic transaction. Either all records + are created or none are created. Return : + -1 : One or more audit records could not be created + +ve : All requested records have been created + ********************************************************************************/ + static INT_32 AUDIT_createMany( + CrbServerReq *req, + CrbTemplateThing &data) { + + RB_BOOL isOK=TRUE; + INT_64 audit_id=-1; + INT_32 recCount=0; + + // Check database connection + if (!db_connect()) + return -1; + + // Makes sure we have an array and that the length is at least 1. + if (data.Type() != CrbTemplateThing::array || data.GetSize() < 1) { + // NO - need at least one record to create + return -1; + } + + PQclear(PQexec(db_conn, "BEGIN;")); + + // Extract the parameters for this record. + CrbTauditRecord *params; + + for (int i = 0; isOK==TRUE && i < data.GetSize(); i++) { + params = CrbTauditRecord::MakeArrayEntry(data,i); + + if (params==NULL) + isOK = FALSE; + else { + audit_id = AUDIT_createRecord(req,params->type,params->entityType,params->entityID,params->contactID,params->summary,params->customData); + isOK = (audit_id>0); + recCount++; + CrbTauditRecord::FinishArrayEntry(params); + } + } + + if (isOK>0) + PQclear(PQexec(db_conn, "COMMIT;")); + else { + recCount=-1; + PQclear(PQexec(db_conn, "ROLLBACK;")); + } + + return recCount; + } + /******************************************************************************** AUDIT_modify *************** *** 946,955 **** /************************************************************* CrbTaudit::create ! ---- Replace this comment with a description of the ! ---- method then write the implementation below. Parameters: ! data : ????? Return: ! ???????????? *************************************************************/ BOOL CrbTaudit::create( CrbServerReq *req, CrbTemplateThing &rval, --- 1023,1031 ---- /************************************************************* CrbTaudit::create ! Create an audit record Parameters: ! data : Return: ! ID of the new audit record created *************************************************************/ BOOL CrbTaudit::create( CrbServerReq *req, CrbTemplateThing &rval, *************** *** 967,970 **** --- 1043,1062 ---- } /************************************************************* + CrbTaudit::createMany + Create a number of audit records as a single atomic unit. + Parameters: + data : Array of audit description structures + Return: + Either the number of records created (same as size of data array) + or zero if at least one record could not be created + *************************************************************/ + BOOL CrbTaudit::createMany( CrbServerReq *req, CrbTemplateThing &rval, + CrbTemplateThing & data) { + + rval = AUDIT_createMany(req, data); + + return TRUE; + } + /************************************************************* CrbTaudit::modify ---- Replace this comment with a description of the Index: audit.skel =================================================================== RCS file: /cvsroot/whitebeam/whitebeam/templates/pgsql/audit-pgsql/audit.skel,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** audit.skel 8 Jan 2007 09:45:16 -0000 1.3 --- audit.skel 10 Mar 2007 13:36:25 -0000 1.4 *************** *** 93,96 **** --- 93,112 ---- } /************************************************************* + CrbTaudit::createMany + ---- Replace this comment with a description of the + ---- method then write the implementation below. + Parameters: + data : ????? + Return: + ???????????? + *************************************************************/ + BOOL CrbTaudit::createMany( CrbServerReq *req, CrbTemplateThing &rval, + CrbTemplateThing & data) { + // Insert your method implementation HERE!!!!! + + // Return 'TRUE' if the operator works OK + return FALSE; + } + /************************************************************* CrbTaudit::get ---- Replace this comment with a description of the Index: audit_stub.cxx =================================================================== RCS file: /cvsroot/whitebeam/whitebeam/templates/pgsql/audit-pgsql/audit_stub.cxx,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** audit_stub.cxx 25 Feb 2007 14:57:46 -0000 1.5 --- audit_stub.cxx 10 Mar 2007 13:36:25 -0000 1.6 *************** *** 171,174 **** --- 171,188 ---- /************************************************************** + createMany_audit_ + Message stub function to serve the whitebeam method: + rb.audit.createMany + **************************************************************/ + static BOOL createMany_audit_(CrbServerReq *req) { + CrbTRedbourne &rb=CrbTRedbourne::rb(); + + CrbTemplateThing &rval=req->Return(); + + return rb.audit.createMany( req, rval, + *req->Param(0)); + } + + /************************************************************** get_audit_ Message stub function to serve the whitebeam method: *************** *** 227,230 **** --- 241,245 ---- {"rb.audit.count", 1, FALSE, FALSE, count_audit_ , NULL }, {"rb.audit.create", 1, FALSE, FALSE, create_audit_ , NULL }, + {"rb.audit.createMany", 1, FALSE, FALSE, createMany_audit_ , NULL }, {"rb.audit.get", 1, TRUE , FALSE, get_audit_ , get_auditRecord_audit_ }, {"rb.audit.modify", 1, FALSE, FALSE, modify_audit_ , NULL }, Index: audit.h =================================================================== RCS file: /cvsroot/whitebeam/whitebeam/templates/pgsql/audit-pgsql/audit.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** audit.h 25 Feb 2007 14:57:46 -0000 1.5 --- audit.h 10 Mar 2007 13:36:25 -0000 1.6 *************** *** 67,70 **** --- 67,73 ---- CrbTemplateThing &rval, CrbTauditRecord & data); + BOOL createMany( CrbServerReq *req, + CrbTemplateThing &rval, + CrbTemplateThing & data); BOOL get( CrbServerReq *req, CrbTemplateThing &rval, Index: audit.tmpl =================================================================== RCS file: /cvsroot/whitebeam/whitebeam/templates/pgsql/audit-pgsql/audit.tmpl,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** audit.tmpl 25 Feb 2007 14:57:46 -0000 1.5 --- audit.tmpl 10 Mar 2007 13:36:25 -0000 1.6 *************** *** 118,121 **** --- 118,135 ---- </method> + <method name="createMany" return="number"> + <doc> + The number of audit records created (0 or same as number requested) + <summary>Create a number of audit records as an atomic unit</summary> + <remarks> + <p>Create one or more audit records as an atomic unit. Either all requested audit records are created or none are created.</p> + </remarks> + <seealso>create, modify, remove</seealso> + </doc> + <struct name="data" type="auditRecord" array="yes"> + <doc>Array of descriptions of audit records. </doc> + </struct> + </method> + <method name="modify" return="number"> <doc> |