libidav/davqlexec.c

changeset 135
664aeaec8d25
parent 134
4bccc18820e8
child 136
59058927b885
--- a/libidav/davqlexec.c	Tue Jul 07 20:47:02 2015 +0200
+++ b/libidav/davqlexec.c	Wed Jul 08 17:31:26 2015 +0200
@@ -38,30 +38,27 @@
 #include "session.h"
 #include "resource.h"
 
-DavResult* dav_statement_exec(DavSession *sn, DavQLStatement *st, ...) {
+DavResult dav_statement_exec(DavSession *sn, DavQLStatement *st, ...) {
     va_list ap;
     va_start(ap, st);
-    DavResult *result = dav_statement_execv(sn, st, ap);
+    DavResult result = dav_statement_execv(sn, st, ap);
     va_end(ap);
     return result;
 }
 
-DavResult* dav_statement_execv(DavSession *sn, DavQLStatement *st, va_list ap) {
-    DavResult *result = dav_session_malloc(sn, sizeof(DavResult));
-    result->result = NULL;
-    result->status = 1;
+DavResult dav_statement_execv(DavSession *sn, DavQLStatement *st, va_list ap) {
+    DavResult result;
+    result.result = NULL;
+    result.status = 1;
     
     // make sure the statement was successfully parsed
     if(st->type == DAVQL_ERROR) {
+        DavResult result;
         return result;
     }
     
-    // get path string
-    davqlerror_t error;
-    sstr_t path = dav_format_string(sn->mp->allocator, st->path, ap, &error);
-    
     if(st->type == DAVQL_SELECT) {
-        *result = dav_exec_select(sn, st, path.ptr, ap);
+        return dav_exec_select(sn, st, ap);
     } else {
         // TODO
     }
@@ -203,6 +200,7 @@
 }
 
 static int reset_properties(DavSession *sn, DavResult *result, DavResource *res, UcxList *fields) {
+    return 0;
     UcxMap *new_properties = ucx_map_new_a(sn->mp->allocator, 32);
     DavResourceData *data = (DavResourceData*)res->data;
     
@@ -295,7 +293,7 @@
 /*
  * execute a davql select statement
  */
-DavResult dav_exec_select(DavSession *sn, DavQLStatement *st, char* path, va_list ap) {
+DavResult dav_exec_select(DavSession *sn, DavQLStatement *st, va_list ap) {
     UcxMempool *mp = ucx_mempool_new(128);
     DavResult result;
     result.result = NULL;
@@ -345,13 +343,23 @@
         } 
     }
     
+    // get path string
+    davqlerror_t error;
+    sstr_t path = dav_format_string(mp->allocator, st->path, ap, &error);
+    if(error) {
+        // TODO: cleanup
+        return result;
+    }
+    
+    int depth = st->depth == DAV_DEPTH_PLACEHOLDER ? va_arg(ap, int) : st->depth;
+    
     UcxBuffer *where = dav_compile_expr(sn->context, mp->allocator, st->where, ap);
-    if(!where) {
+    if(st->where && !where) {
         ucx_mempool_destroy(mp);
         return result;
     }
     
-    DavResource *selroot = dav_resource_new(sn, path);
+    DavResource *selroot = dav_resource_new(sn, path.ptr);
     
     UcxList *stack = NULL; // stack with DavResource* elements
     // initialize the stack with the requested resource
@@ -431,7 +439,7 @@
                             if(!reset_properties(sn, &result, child, cfieldlist)) {
                                 resource_add_child(root, child);
                                 if(child->iscollection &&
-                                    (st->depth < 0 || st->depth > sr->depth+1))
+                                    (depth < 0 || depth > sr->depth+1))
                                 {
                                     DavQLRes *rs = ucx_mempool_malloc(
                                             mp,
@@ -515,6 +523,7 @@
         }
         case DAVQL_TIMESTAMP: {
             if(src.ptr[0] == '%') {
+                cmd.type = DAVQL_CMD_TIMESTAMP;
                 cmd.data.timestamp = va_arg(ap, time_t);
                 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
             } else {
@@ -764,6 +773,13 @@
 }
 
 int dav_exec_expr(UcxBuffer *bcode, DavResource *res, DavQLStackObj *result) {
+    if(!bcode) {
+        result->type = 0;
+        result->length = 0;
+        result->data.integer = 1;
+        return 0;
+    }
+    
     size_t count = bcode->pos / sizeof(DavQLCmd);
     DavQLCmd *cmds = (DavQLCmd*)bcode->space;
     
@@ -793,7 +809,7 @@
         DavQLCmd cmd = cmds[i];
         switch(cmd.type) {
             case DAVQL_CMD_INT: {
-                printf("int %lld\n", cmd.data.integer);
+                //printf("int %lld\n", cmd.data.integer);
                 obj.type = 0;
                 obj.length = 0;
                 obj.data.integer = cmd.data.integer;
@@ -801,7 +817,7 @@
                 break;
             }
             case DAVQL_CMD_STRING: {
-                printf("string \"%.*s\"\n", cmd.data.string.length, cmd.data.string.ptr);
+                //printf("string \"%.*s\"\n", cmd.data.string.length, cmd.data.string.ptr);
                 obj.type = 1;
                 obj.length = cmd.data.string.length;
                 obj.data.string = cmd.data.string.ptr;
@@ -809,16 +825,16 @@
                 break;
             }
             case DAVQL_CMD_TIMESTAMP: {
-                printf("timestamp %d\n", cmd.data.timestamp);
+                //printf("timestamp %d\n", cmd.data.timestamp);
                 obj.type = 0;
                 obj.length = 0;
-                obj.data.integer = cmd.data.timestamp;
+                obj.data.integer = (int)cmd.data.timestamp;
                 DAVQL_PUSH(obj);
                 break;
             }
             case DAVQL_CMD_RES_IDENTIFIER: {
                 char *rid[8] = {"name", "path", "href", "contentlength", "contenttype", "creationdate", "lastmodified", "iscollection"};
-                printf("resprop %s\n", rid[cmd.data.resprop]);
+                //printf("resprop %s\n", rid[cmd.data.resprop]);
                 switch(cmd.data.resprop) {
                     case DAVQL_RES_NAME: {
                         obj.type = 1;
@@ -873,7 +889,7 @@
                 break;
             }
             case DAVQL_CMD_PROP_IDENTIFIER: {
-                printf("property %s:%s\n", cmd.data.property.ns, cmd.data.property.name);
+                //printf("property %s:%s\n", cmd.data.property.ns, cmd.data.property.name);
                 char *value = dav_get_property_ns(res, cmd.data.property.ns, cmd.data.property.name);
                 obj.type = 1;
                 obj.length = value ? strlen(value) : 0;
@@ -886,7 +902,7 @@
             //    break;
             //}
             case DAVQL_CMD_OP_UNARY_SUB: {
-                printf("usub\n");
+                //printf("usub\n");
                 obj = DAVQL_POP();
                 if(obj.type == 0) {
                     obj.data.integer = -obj.data.integer;
@@ -898,7 +914,7 @@
                 break;
             }
             case DAVQL_CMD_OP_UNARY_NEG: {
-                printf("uneg\n");
+                //printf("uneg\n");
                 obj = DAVQL_POP();
                 if(obj.type == 0) {
                     obj.data.integer = obj.data.integer == 0 ? 1 : 0;
@@ -910,7 +926,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_ADD: {
-                printf("add\n");
+                //printf("add\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -921,7 +937,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_SUB: {
-                printf("sub\n");
+                //printf("sub\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -934,7 +950,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_MUL: {
-                printf("mul\n");
+                //printf("mul\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -947,7 +963,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_DIV: {
-                printf("div\n");
+                //printf("div\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -960,7 +976,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_AND: {
-                printf("and\n");
+                //printf("and\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -973,7 +989,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_OR: {
-                printf("or\n");
+                //printf("or\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -986,7 +1002,7 @@
                 break;
             }
             case DAVQL_CMD_OP_BINARY_XOR: {
-                printf("xor\n");
+                //printf("xor\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -999,11 +1015,11 @@
                 break;
             }
             case DAVQL_CMD_OP_LOGICAL_NOT: {
-                printf("not\n");
+                //printf("not\n");
                 break;
             }
             case DAVQL_CMD_OP_LOGICAL_AND: {
-                printf("land\n");
+                //printf("land\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 int v1 = obj1.type == 0 ? (int)obj1.data.integer : (obj1.data.string ? 1 : 0);
@@ -1012,7 +1028,7 @@
                 break;
             }
             case DAVQL_CMD_OP_LOGICAL_OR_L: {
-                printf("or_l %d\n", cmd.data.integer);
+                //printf("or_l %d\n", cmd.data.integer);
                 DavQLStackObj obj1 = DAVQL_POP();
                 if((obj1.type == 0 && obj1.data.integer) || (obj1.type == 1 && obj1.data.string)) {
                     DAVQL_PUSH_INT(1);
@@ -1021,7 +1037,7 @@
                 break;
             }
             case DAVQL_CMD_OP_LOGICAL_OR: {
-                printf("or\n");
+                //printf("or\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 int v1 = obj1.type == 0 ? (int)obj1.data.integer : (obj1.data.string ? 1 : 0);
@@ -1030,7 +1046,7 @@
                 break;
             }
             case DAVQL_CMD_OP_LOGICAL_XOR: {
-                printf("lxor\n");
+                //printf("lxor\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 int v1 = obj1.type == 0 ? (int)obj1.data.integer : (obj1.data.string ? 1 : 0);
@@ -1039,7 +1055,7 @@
                 break;
             }
             case DAVQL_CMD_OP_EQ: {
-                printf("eq\n");
+                //printf("eq\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -1050,7 +1066,7 @@
                 break;
             }
             case DAVQL_CMD_OP_NEQ: {
-                printf("neq\n");
+                //printf("neq\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -1061,7 +1077,7 @@
                 break;
             }
             case DAVQL_CMD_OP_LT: {
-                printf("lt\n");
+                //printf("lt\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -1072,7 +1088,7 @@
                 break;
             }
             case DAVQL_CMD_OP_GT: {
-                printf("gt\n");
+                //printf("gt\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -1083,7 +1099,7 @@
                 break;
             }
             case DAVQL_CMD_OP_LE: {
-                printf("le\n");
+                //printf("le\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -1094,7 +1110,7 @@
                 break;
             }
             case DAVQL_CMD_OP_GE: {
-                printf("ge\n");
+                //printf("ge\n");
                 DavQLStackObj obj2 = DAVQL_POP();
                 DavQLStackObj obj1 = DAVQL_POP();
                 if(obj1.type == 0 && obj2.type == 0) {
@@ -1105,15 +1121,15 @@
                 break;
             }
             case DAVQL_CMD_OP_LIKE: {
-                printf("like\n");
+                //printf("like\n");
                 break;
             }
             case DAVQL_CMD_OP_UNLIKE: {
-                printf("unlike\n");
+                //printf("unlike\n");
                 break;
             }
             case DAVQL_CMD_CALL: {
-                printf("call %x\n", cmd.data.func);
+                //printf("call %x\n", cmd.data.func);
                 break;
             }
         }

mercurial