493 if(cxBufferInit(&sql, NULL, 2048, a, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
493 if(cxBufferInit(&sql, NULL, 2048, a, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
494 return 1; |
494 return 1; |
495 } |
495 } |
496 |
496 |
497 if(pg_create_propfind_query(rq, iscollection, ext, numext, &sql)) { |
497 if(pg_create_propfind_query(rq, iscollection, ext, numext, &sql)) { |
|
498 cxBufferDestroy(&sql); |
498 return 1; |
499 return 1; |
499 } |
500 } |
500 query = sql.space; |
501 query = sql.space; |
|
502 log_ereport(LOG_DEBUG, "pg_dav_propfind_init query: %.*s\n", (int)sql.size, sql.space); |
501 |
503 |
502 // get all resources and properties |
504 // get all resources and properties |
503 size_t href_len = strlen(href); |
505 size_t href_len = strlen(href); |
504 char *href_param = pool_malloc(rq->sn->pool, href_len + 1); |
506 char *href_param = pool_malloc(rq->sn->pool, href_len + 1); |
505 memcpy(href_param, href, href_len); |
507 memcpy(href_param, href, href_len); |
519 size_t bufsize = rq->propcount < 200 ? 8 + rq->propcount * 32 : 4096; |
521 size_t bufsize = rq->propcount < 200 ? 8 + rq->propcount * 32 : 4096; |
520 if(cxBufferInit(&xmlns_buf, NULL, bufsize, a, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
522 if(cxBufferInit(&xmlns_buf, NULL, bufsize, a, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
521 return 1; |
523 return 1; |
522 } |
524 } |
523 if(cxBufferInit(&pname_buf, NULL, bufsize, a, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
525 if(cxBufferInit(&pname_buf, NULL, bufsize, a, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
|
526 cxBufferDestroy(&sql); |
524 cxBufferDestroy(&xmlns_buf); |
527 cxBufferDestroy(&xmlns_buf); |
525 return 1; |
528 return 1; |
526 } |
529 } |
527 if(pg_create_property_param_arrays(*outplist, &xmlns_buf, &pname_buf)) { |
530 if(pg_create_property_param_arrays(*outplist, &xmlns_buf, &pname_buf)) { |
|
531 cxBufferDestroy(&sql); |
528 cxBufferDestroy(&xmlns_buf); |
532 cxBufferDestroy(&xmlns_buf); |
529 cxBufferDestroy(&pname_buf); |
533 cxBufferDestroy(&pname_buf); |
530 return 1; |
534 return 1; |
531 } |
535 } |
532 buf_initialized = TRUE; |
536 buf_initialized = TRUE; |
547 NULL, |
551 NULL, |
548 NULL, |
552 NULL, |
549 0); // 0: result in text format |
553 0); // 0: result in text format |
550 int nrows = PQntuples(result); |
554 int nrows = PQntuples(result); |
551 pool_free(rq->sn->pool, href_param); |
555 pool_free(rq->sn->pool, href_param); |
|
556 cxBufferDestroy(&sql); |
552 if(buf_initialized) { |
557 if(buf_initialized) { |
553 cxBufferDestroy(&xmlns_buf); |
558 cxBufferDestroy(&xmlns_buf); |
554 cxBufferDestroy(&pname_buf); |
559 cxBufferDestroy(&pname_buf); |
555 } |
560 } |
556 if(nrows < 1) { |
561 if(nrows < 1) { |
573 pg->nrows = nrows; |
578 pg->nrows = nrows; |
574 |
579 |
575 pg->ext = ext; |
580 pg->ext = ext; |
576 pg->numext = numext; |
581 pg->numext = numext; |
577 if(ext) { |
582 if(ext) { |
|
583 // build a map of all ext field names (excluding first fields from |
|
584 // the resource and property table) |
|
585 int nfields = PQnfields(result); |
|
586 CxMap *fieldmap = cxHashMapCreate(pool_allocator(rq->sn->pool), sizeof(int), nfields); |
|
587 if(!fieldmap) { |
|
588 PQclear(result); |
|
589 return 1; |
|
590 } |
|
591 // start with index 15 (see pg_dav_propfind_do for first column nums) |
|
592 for(int i=15;i<nfields;i++) { |
|
593 char *name = PQfname(result, i); |
|
594 if(name) { |
|
595 if(cxMapPut(fieldmap, name, &i)) { |
|
596 PQclear(result); |
|
597 return 1; |
|
598 } |
|
599 } |
|
600 } |
|
601 |
578 // get field_nums for all property extensions |
602 // get field_nums for all property extensions |
579 for(int i=0;i<numext;i++) { |
603 for(int i=0;i<numext;i++) { |
580 PgPropfindExtCol *c = &ext[i]; |
604 PgPropfindExtCol *c = &ext[i]; |
581 c->field_num = PQfnumber(result, c->ext->column); |
605 //c->field_num = PQfnumber(result, c->ext->column); |
582 } |
606 int *fieldnum = cxMapGet(fieldmap, c->ext->column); |
|
607 c->field_num = *fieldnum; |
|
608 } |
|
609 |
|
610 cxMapDestroy(fieldmap); |
583 } |
611 } |
584 |
612 |
585 return 0; |
613 return 0; |
586 } |
614 } |
587 |
615 |
615 // 10: property xmlns |
643 // 10: property xmlns |
616 // 11: property name |
644 // 11: property name |
617 // 12: property lang |
645 // 12: property lang |
618 // 13: property nsdeflist |
646 // 13: property nsdeflist |
619 // 14: property value |
647 // 14: property value |
620 |
648 |
621 char *path = PQgetvalue(result, r, 0); |
649 char *path = PQgetvalue(result, r, 0); |
622 char *res_id = PQgetvalue(result, r, 1); |
650 char *res_id = PQgetvalue(result, r, 1); |
623 char *iscollection_str = PQgetvalue(result, r, 4); |
651 char *iscollection_str = PQgetvalue(result, r, 4); |
624 WSBool iscollection = iscollection_str && iscollection_str[0] == 't'; |
652 WSBool iscollection = iscollection_str && iscollection_str[0] == 't'; |
625 int64_t resource_id; |
653 int64_t resource_id; |