dbutils/object.c

changeset 3
69ea9040d896
child 4
1908c8b1599f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dbutils/object.c	Sun Dec 08 15:46:03 2024 +0100
@@ -0,0 +1,114 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cx/array_list.h>
+#include <cx/hash_map.h>
+
+#include "object.h"
+
+
+CxList* dbuQuerySingleType(DBUContext *ctx, DBUQuery *query, const char *type) {
+    DBUClass *cls = cxMapGet(ctx->classes, type);
+    if(!cls) {
+        return NULL;
+    }
+    
+    // execute sql
+    if(query->exec(query)) {
+        query->free(query);
+        return NULL;
+    }
+    
+    DBUResult *result = query->getResult(query);
+    if(!result) {
+        return NULL;
+    }
+    query->free(query);
+    
+    // prepare list
+    CxList *list = cxArrayListCreateSimple(CX_STORE_POINTERS, 16);
+    if(!list) {
+        result->free(result);
+    }
+    
+    // map result to class fields
+    int numcols = result->numFields(result);
+    DBUFieldMapping *fields = calloc(numcols, sizeof(DBUFieldMapping));
+    if(!fields) {
+        result->free(result);
+        cxListDestroy(list);
+        return NULL;
+    }
+    int numfields = 0;
+    for(int i=0;i<numcols;i++) {
+        DBUField *field = cxMapGet(cls->fields, result->fieldName(result, i));
+        if(field) {
+            DBUFieldMapping mapping;
+            mapping.field = field;
+            mapping.index = i;
+            mapping.type = result->fieldType(result, i);
+            fields[numfields++] = mapping;
+        }
+    }
+    
+    const CxAllocator *a = cxDefaultAllocator;
+    
+    // get result
+    while(result->hasData(result)) {
+        void *obj = malloc(cls->obj_size);
+        if(!obj) {
+            break;
+        }
+        memset(obj, 0, sizeof(cls->obj_size));
+        
+        for(int i=0;i<numfields;i++) {
+            DBUFieldMapping field = fields[i];
+            int isnull = result->isNull(result, field.index);
+            
+            if(isnull) {
+                field.field->initDefaultValue(field.field, a, obj);
+            } else {
+                cxstring text = result->getText(result, field.index);
+                field.field->initValue(field.field, a, obj, text.ptr, text.length);
+            }
+        }
+        
+        cxListAdd(list, obj);
+        
+        // load next row
+        result->nextRow(result);
+    }
+    
+    result->free(result);
+    
+    return list;
+}

mercurial