# UDA Event Handler

Database events can be managed by registering an event handler. The function ``mco_uda_register_event_handler()`` requires an argument of type `mco_uda_event_handler` as define below in `mcouda.h`:

        typedef MCO_RET (*mco_uda_event_handler)( mco_trans_h t, mco_uda_object_handle_p obj, const mco_dict_event_info_t *event_info, /* inout */ void *param );

Following is an example of an event handler implementation:

        MCO_RET sync_event_handler(mco_trans_h t, mco_uda_object_handle_p obj, const mco_dict_event_info_t *event_info, void *param)
        {
            mco_dict_struct_info_t struct_info;
            mco_dict_field_info_t field_info;
            mco_uda_value_t val;
            autoid_t obj_autoid;

            mco_dict_struct(metadict, 0, obj->struct_no, &struct_info);
            mco_dict_field_name(metadict, 0, obj->struct_no, "autoid", &field_info);
            val.type = MCO_DD_AUTOID;
            mco_uda_get(obj, field_info.field_no, 0, &val);
            obj_autoid = val.v.u8;

            if (event_info->type == MCO_EVENT_UPDATE || event_info->type == MCO_EVENT_CLASS_UPDATE)
            {
                mco_dict_field(metadict, 0, obj->struct_no, event_info->field_no, &field_info);
                printf("Sync. event for obj %" INT8_FORMAT "d: class %s, event_no=%d, type=UPDATE, when=%s, field=%s, value=",
                obj_autoid, struct_info.name, event_info->event_no, ((int)param == MCO_AFTER_UPDATE) ? "AFTER" : "BEFORE", field_info.name);

                print_obj_field(obj, &field_info);
            } else {
                printf("Sync. event for obj %" INT8_FORMAT "d: class %s, event_no=%d, type=", obj_autoid, struct_info.name, event_info->event_no);
                switch (event_info->type) {
                    case MCO_EVENT_NEW        : printf("NEW"); break;
                    case MCO_EVENT_DELETE     : printf("DELETE"); break;
                    case MCO_EVENT_DELETE_ALL : printf("DELETE_ALL"); break;
                    case MCO_EVENT_CHECKPOINT : printf("CHECKPOINT"); break;
                    default                   : printf("UNKNOWN"); dbg_exit(1);
                }
            }
            printf("\n");
            return MCO_S_OK;
        }