# HG changeset patch # User Olaf Wintermann # Date 1733609403 -3600 # Node ID 4c12c95f4846ffe2c4d4d57e68c8adcb57f6bedd # Parent cf6031ceab4258f415cce63f303fd57afba37665 extend DBUField and implement dbuClassFree diff -r cf6031ceab42 -r 4c12c95f4846 dbutils/class.c --- a/dbutils/class.c Sat Dec 07 19:02:57 2024 +0100 +++ b/dbutils/class.c Sat Dec 07 23:10:03 2024 +0100 @@ -34,18 +34,29 @@ #include +static void field_destructor(DBUField *f) { + if(f->destructor) { + f->destructor(f); + } + free(f); +} + DBUClass* dbuClassCreate(const char *name) { DBUClass *cls = malloc(sizeof(DBUClass)); memset(cls, 0, sizeof(DBUClass)); cls->name = cx_strdup(cx_str(name)); cls->fields = cxHashMapCreateSimple(CX_STORE_POINTERS); + cls->fields->collection.simple_destructor = (cx_destructor_func)field_destructor; return cls; } void dbuClassFree(DBUClass *cls) { - // TODO + cxMapDestroy(cls->fields); + free(cls->name.ptr); + free(cls->primary_key_column.ptr); + free(cls); } void dbuClassAddField(DBUClass *cls, const char *name, DBUField *field) { diff -r cf6031ceab42 -r 4c12c95f4846 dbutils/dbutils/dbutils.h --- a/dbutils/dbutils/dbutils.h Sat Dec 07 19:02:57 2024 +0100 +++ b/dbutils/dbutils/dbutils.h Sat Dec 07 23:10:03 2024 +0100 @@ -85,19 +85,15 @@ CxMap *fields; /* - * sizeof the struct - * - * Must be specified, if no constructor function is registered + * object size used for allocation, typically sizeof(some_struct) */ size_t obj_size; /* - * optional constructor function + * optional initializer function * - * If no constructor is specified, the object is created with - * malloc(obj_size) + memset to 0 */ - void* (*constructor)(const CxAllocator *a); + void* (*init)(const CxAllocator *a); }; /* @@ -105,16 +101,45 @@ */ struct DBUField { /* - * called, if the field is null + * called, if the field is null (optional) */ int (*initDefaultValue)(DBUField *f, const CxAllocator *a, DBUObject obj); /* - * init primitve type + * called, if a field was not requested in a query (optional) + */ + int (*initExcluded)(DBUField *f, const CxAllocator *a, DBUObject obj); + + /* + * init primitve type (required) + * + * This must be implemented */ int (*initValue)(DBUField *f, const CxAllocator *a, DBUObject obj, const char *value, size_t length); + /* + * integer initialization function (optional) + * + * if a result columns contains an integer and this pointer is + * non-null, this function is called + */ + int (*initIntValue)(DBUField *f, const CxAllocator *a, DBUObject obj, int64_t value); + /* + * double initialization function (optional) + */ + int (*initDoubleValue)(DBUField *f, const CxAllocator *a, DBUObject obj, double value); + + /* + * destructor (optional) + * + * this callback must not free the DBUField* ptr + */ + void (*destructor)(DBUField *f); + + /* + * if true, null values are not accepted and result in an error + */ bool nonnull; bool query_length; }; diff -r cf6031ceab42 -r 4c12c95f4846 dbutils/field.c --- a/dbutils/field.c Sat Dec 07 19:02:57 2024 +0100 +++ b/dbutils/field.c Sat Dec 07 23:10:03 2024 +0100 @@ -481,7 +481,7 @@ bool nonnull, union DBUDefValue def) { - DBUOffsetField *field = malloc(sizeof(DBUOffsetField)); + DBUOffsetField *field = calloc(1, sizeof(DBUOffsetField)); field->field.initDefaultValue = def_init; field->field.initValue = init; field->field.nonnull = nonnull; @@ -509,7 +509,7 @@ off_t offset, bool nonnull) { - DBUOffsetField *field = malloc(sizeof(DBUOffsetField)); + DBUOffsetField *field = calloc(1, sizeof(DBUOffsetField)); field->field.initDefaultValue = NULL; field->field.initValue = (DBUFieldInitFunc)field_init_bool; field->field.nonnull = nonnull; @@ -524,7 +524,7 @@ off_t offset, bool nonnull) { - DBUOffsetField *field = malloc(sizeof(DBUOffsetField)); + DBUOffsetField *field = calloc(1, sizeof(DBUOffsetField)); field->field.initDefaultValue = NULL; field->field.initValue = (DBUFieldInitFunc)init; field->field.nonnull = nonnull; @@ -541,7 +541,7 @@ off_t offset, bool nonnull) { - DBUOffsetField *field = malloc(sizeof(DBUOffsetField)); + DBUOffsetField *field = calloc(1, sizeof(DBUOffsetField)); field->field.initDefaultValue = NULL; field->field.initValue = (DBUFieldInitFunc)init; field->field.nonnull = nonnull;