📄 handlers.c
字号:
/************************************************************ * * * Copyright (c) 2001-2007 McObject LLC. All Right Reserved.* * * ************************************************************//* Event handlers (call back functions) implementaions */#include "platform.h"#include <stdio.h>#include <stdlib.h>#include "evdb.h"#include "handlers.h"int unregister_events(mco_db_h db) { MCO_RET rc; mco_trans_h t; mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t); mco_unregister_newEvent_handler( t, & new_handler); mco_unregister_deleteEvent_handler( t, & delete_handler); mco_unregister_updateEvent_handler( t, & update_handler1); mco_unregister_updateEvent_handler( t, & update_handler2); rc = mco_trans_commit(t); return rc;}int register_events(mco_db_h db) { MCO_RET rc; mco_trans_h t; mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t); mco_register_newEvent_handler( t, & new_handler, (void*)0 ); mco_register_deleteEvent_handler(t, & delete_handler, (void * )0); mco_register_updateEvent_handler( t, & update_handler1, (void *)0, MCO_BEFORE_UPDATE ); mco_register_updateEvent_handler( t, & update_handler2, (void *)0, MCO_AFTER_UPDATE ); rc = mco_trans_commit(t); return rc;}/* Handler for the "<new>" event. Reads the autoid and prints it out */MCO_RET new_handler(mco_trans_h t, MyClass * obj, MCO_EVENT_TYPE et, /*INOUT */void *param) { int8 u8; param = (int *)1; MyClass_autoid_get( obj, &u8);#ifdef MCO_CFG_QUAD_STRUCT printf("Event \"Object New\" : object (%ld,%ld) is created\n",u8.lo, u8.hi);#else /* MCO_CFG_QUAD_STRUCT */ printf("Event \"Object New\" : object (%lld) is created\n",u8);#endif /* MCO_CFG_QUAD_STRUCT */ return MCO_S_OK;}/* Handler for the "<delete>" event. Note that the handler is called before * the current transaction is committed. Therefore, the object is still * valid; the object handle is passed back to the handler and is used to obtain the * autoid of the object. * The event's handler return value is passed into the "delete" function and is * later examined by the mco_trans_commit(). If the value is anything but * MCO_S_OK, the transaction is rollbacked. In this sample every other delete * transaction is rolled back. */MCO_RET delete_handler( mco_trans_h t, MyClass * obj, MCO_EVENT_TYPE et, /*INOUT*/ void *user_param) { int8 u8; MyClass_autoid_get( obj, &u8);#ifdef MCO_CFG_QUAD_STRUCT printf("Event \"Object Delete\": object (%ld,%ld) is being deleted...", u8.lo, u8.hi); return (((u8.lo + u8.hi) %2) ? 1: MCO_S_OK);#else /* MCO_CFG_QUAD_STRUCT */ printf("Event \"Object Delete\": object (%lld) is being deleted...", u8 ); return (((u8) %2) ? 1: MCO_S_OK);#endif /* MCO_CFG_QUAD_STRUCT */}/* Handler for the "update" event.This handler is called before the update transaction * was commited - hence the value of the field being changed is reported unchanged yet. */MCO_RET update_handler1( mco_trans_h t, MyClass * obj, MCO_EVENT_TYPE et, /*INOUT*/ void *param) { uint4 u4; int8 u8; MyClass_autoid_get( obj, &u8); MyClass_u4_get(obj, &u4);#ifdef MCO_CFG_QUAD_STRUCT printf("Event \"Object Update\" (before commit): object (%ld,%ld) value %d\n",u8.lo, u8.hi, u4);#else /* MCO_CFG_QUAD_STRUCT */ printf("Event \"Object Update\" (before commit): object (%lld) value %d\n",u8, u4);#endif /* MCO_CFG_QUAD_STRUCT */ return MCO_S_OK;}/* Another handler for the "<update>" event, only this time it is called after the * transaction was committed. So, the updated field value is already commited to * the database. */MCO_RET update_handler2( mco_trans_h t, MyClass * obj, MCO_EVENT_TYPE et, /*INOUT*/ void *param) { uint4 u4; int8 u8; MyClass_autoid_get( obj, &u8); MyClass_u4_get(obj, &u4);#ifdef MCO_CFG_QUAD_STRUCT printf("Event \"Object Update\" (after commit): object (%ld,%ld) value %d\n",u8.lo, u8.hi, u4);#else /* MCO_CFG_QUAD_STRUCT */ printf("Event \"Object Update\" (after commit): object (%lld) value %d\n",u8, u4);#endif /* MCO_CFG_QUAD_STRUCT */ return MCO_S_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -