# mco_uda_register_udf
RT

> Register user-defined functions.

### Prototype

        MCO_RET mco_uda_register_udf(	/*IN*/ const mco_metadict_header_t * metadict,
                         /*IN*/ unsigned short dict_no,
                         /*IN*/ unsigned short struct_no,
                         /*IN*/ unsigned short index_no,
                         /*IN*/ mco_userdef_funcs_h udf_map,
                         /*IN*/ mco_uda_userdef_funcs_h udf_entry,
                         /*IN*/ void *user_context );

### Arguments

metadict | The address of an initialized mco_metadict_header_t structure
dict_no | The number of the dictionary (must be between 0 and `mco_metadict_count()` - 1)
struct_no | The structure/class number (must be between 0 and `mco_dict_struct_count()` - 1)
index_no | The index number (must be between 0 and `struct_info.index_count` - 1)
udf_map | A pointer to the memory buffer allocated to store user-defined functions. (Note that it must be the same for all registered UDFs)
udf_entry | The user-defined function
user_context | The application-specific data passed into the user-defined compare functions

### Description

Registers user-defined functions with the database runtime.

### Return Codes

MCO_E_UDA_DICT_NOTFOUND | Invalid `dict_no`
MCO_E_UDA_STRUCT_NOTFOUND | Invalid `struct_no`
MCO_E_UDA_STRUCT_NOT_CLASS | The `struct_no` is not a class
MCO_E_UDA_INDEX_NOTFOUND | Invalid `index_no`
MCO_E_UDA_INDEX_NOT_USERDEF | The index referenced by `index_no` is not a user-defined index

### Example

        Application snippet:

        const char * dbname = "SimpleDb";
        ...
        /* User-defined compare & hash functions */
        int2 cmp_obj_obj(mco_uda_object_handle_p obj1, unsigned short index1,
            mco_uda_object_handle_p obj2, unsigned short index2, void *param_)
        {
            mco_uda_value_t val1, val2;
            char buf1[20], buf2[20];
            userdef_param_t *param = (userdef_param_t *) param_; /* Get context */

            /* Get first object's value */
            val1.type = MCO_DD_STRING;
            val1.v.p.size = sizeof(buf1);
            val1.v.p.p.c = buf1;
            mco_uda_get(obj1, param->fld_no, index1, &val1);
            /* Get second object's value */
            val2.type = MCO_DD_STRING;
            val2.v.p.size = sizeof(buf2);
            val2.v.p.p.c = buf2;
            mco_uda_get(obj2, param->fld_no, index2, &val2);

            /* Compare values */
            return STR_CMP(buf1, buf2);
        }

        int2 cmp_obj_ext(mco_uda_object_handle_p obj, unsigned short index,
            mco_uda_value_t *keys, uint2 keys_count, void *param_)
        {
            mco_uda_value_t val;
            char buf[20];
            userdef_param_t *param = (userdef_param_t *) param_;

            /* Get object's value */
            val.type = MCO_DD_STRING;
            val.v.p.size = sizeof(buf);
            val.v.p.p.c = buf;
            mco_uda_get(obj, param->fld_no, index, &val);

            /* Compare object's value and external key */
            return STR_CMP(buf, keys[0].v.p.p.c);
        }

        uint4 hash_obj(mco_uda_object_handle_p obj, unsigned short index, void *param_)
        {
            mco_uda_value_t val;
            char buf[20];
            userdef_param_t *param = (userdef_param_t *) param_;

            /* Get object's value */
            val.type = MCO_DD_STRING;
            val.v.p.size = sizeof(buf);
            val.v.p.p.c = buf;
            mco_uda_get(obj, param->fld_no, index, &val);

            /* Hash value */
            return strlen(buf);
        }

        uint4 hash_ext(mco_uda_value_t *keys, uint2 keys_count, void *param)
        {
            /* Hash external key */
            return strlen(keys[0].v.p.p.c);
        }

        int main(int argc, char* argv[])
        {
            MCO_RET rc;
            mco_runtime_info_t info;
            unsigned int dict_no = 0;
            unsigned int record_no = 1;
            unsigned int tdudf_no = 2;
            unsigned short name_no = 1;
            mco_metadict_header_t *header;
            unsigned int udf_map_size;
            mco_userdef_funcs_h udf_map;
            mco_uda_userdef_funcs_t udf_entry;

            ...
            mco_runtime_start();
            mco_get_runtime_info(&info);
            ...

            header = (mco_metadict_header_t *) malloc(size);
            mco_metadict_init(header, size); /* initialize the metadict */
            ...
            /* Allocate udfmap */
            mco_uda_get_udfmap_size(metadict, dict_no, &udf_map_size);
            udf_map = (mco_userdef_funcs_h) malloc(udf_map_size);
            ...
            /* Register user-defined compare & hash functions */
            udf_entry.fcomp     = cmp_obj_obj;
            udf_entry.fcomp_ext = cmp_obj_ext;
            udf_entry.fhash     = hash_obj;
            udf_entry.fhash_ext = hash_ext;
            ...
            param.fld_no = name_no; /* Will pass name_no to compare/hash functions */

            rc = mco_uda_register_udf(metadict, dict_no, record_no, tudf_no, udf_map, &udf_entry, (void*) &param);

            ...
        }

### Files

Header file:
mco.h
Source file:
mcouda.c
Library:
libmcouda.a