⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handlers.c

📁 PB 熟悉的哥们希望大家可以互相学习一下
💻 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"

#if defined (_WIN32) || defined (WIN32)
#define _INT8_FORMAT "I64"
#else
#define _INT8_FORMAT "ll"
#endif

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);
    mco_unregister_updateEvent2_handler(t, &update2_handler1);
    mco_unregister_updateEvent2_handler(t, &update2_handler2);
    mco_unregister_updateEvent3_handler(t, &update3_handler1);
    mco_unregister_updateEvent3_handler(t, &update3_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);
    mco_register_updateEvent2_handler(t, &update2_handler1, (void*)0, MCO_BEFORE_UPDATE);
    mco_register_updateEvent2_handler(t, &update2_handler2, (void*)0, MCO_AFTER_UPDATE);
    mco_register_updateEvent3_handler(t, &update3_handler1, (void*)0, MCO_BEFORE_UPDATE);
    mco_register_updateEvent3_handler(t, &update3_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 (%"_INT8_FORMAT"d) 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 (%"_INT8_FORMAT"d) 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 (%"_INT8_FORMAT"d) 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 (%I64d) value %d\n", u8, u4);
    #endif /* MCO_CFG_QUAD_STRUCT */
    return MCO_S_OK;
}

/* 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 update2_handler1(mco_trans_h t, MyClass* obj, MCO_EVENT_TYPE et,  /*INOUT*/void* param)
{
    uint1 bf = 0;
    int8 u8;

    MyClass_autoid_get(obj, &u8);
    MyClass_bfield_get(obj, &bf);
    #ifdef MCO_CFG_QUAD_STRUCT
        printf("Event \"Object Update\" (before commit): object (%ld,%ld) bitfield value 0x%X\n", u8.lo, u8.hi, bf);
    #else /* MCO_CFG_QUAD_STRUCT */
        printf("Event \"Object Update\" (before commit): object (%"_INT8_FORMAT"d) bitfield value 0x%X\n", u8, bf);
    #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 update2_handler2(mco_trans_h t, MyClass* obj, MCO_EVENT_TYPE et,  /*INOUT*/void* param)
{
    uint1 bf = 0;
    int8 u8;
    
    MyClass_autoid_get(obj, &u8);
    MyClass_bfield_get(obj, &bf);
#ifdef MCO_CFG_QUAD_STRUCT
    printf("Event \"Object Update\" (after commit): object (%ld,%ld) bitfield value 0x%X\n", u8.lo, u8.hi, bf);
#else /* MCO_CFG_QUAD_STRUCT */
    printf("Event \"Object Update\" (after commit): object (%"_INT8_FORMAT"d) bitfield value 0x%X\n", u8, bf);
#endif /* MCO_CFG_QUAD_STRUCT */
    return MCO_S_OK;
}

/* 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 update3_handler1(mco_trans_h t, MyClass* obj, MCO_EVENT_TYPE et,  /*INOUT*/void* param)
{
    uint1 barr = 0;
    int8 u8;

    MyClass_autoid_get(obj, &u8);
    MyClass_bitArr_get(obj, &barr);
    #ifdef MCO_CFG_QUAD_STRUCT
        printf("Event \"Object Update\" (before commit): object (%ld,%ld) bitArr value 0x%X\n", u8.lo, u8.hi, barr);
    #else /* MCO_CFG_QUAD_STRUCT */
        printf("Event \"Object Update\" (before commit): object (%"_INT8_FORMAT"d) bitArr value 0x%X\n", u8, barr);
    #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 update3_handler2(mco_trans_h t, MyClass* obj, MCO_EVENT_TYPE et,  /*INOUT*/void* param)
{
    uint1 barr = 0;
    int8 u8;
    
    MyClass_autoid_get(obj, &u8);
    MyClass_bitArr_get(obj, &barr);
#ifdef MCO_CFG_QUAD_STRUCT
    printf("Event \"Object Update\" (after commit): object (%ld,%ld) bitArr value 0x%X\n", u8.lo, u8.hi, barr);
#else /* MCO_CFG_QUAD_STRUCT */
    printf("Event \"Object Update\" (after commit): object (%"_INT8_FORMAT"d) bitArr value 0x%X\n", u8, barr);
#endif /* MCO_CFG_QUAD_STRUCT */
    return MCO_S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -