1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
|
/*
* Copyright (C) 2014 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "../../mem/mem.h"
#include "../../dprint.h"
#include "../../lib/srdb1/db_ut.h"
#include "../../lib/srdb1/db_query.h"
#include "mongodb_connection.h"
#include "mongodb_dbase.h"
#define DB_MONGODB_ROWS_STEP 1000
typedef struct db_mongodb_result {
mongoc_collection_t *collection; /*!< Collection link */
mongoc_cursor_t *cursor; /*!< Cursor link */
bson_t *rdoc;
int idx;
bson_t *colsdoc;
int nrcols;
int maxrows;
} db_mongodb_result_t;
/*
* Initialize database module
* No function should be called before this
*/
db1_con_t* db_mongodb_init(const str* _url)
{
return db_do_init(_url, (void *)db_mongodb_new_connection);
}
/*
* Shut down database module
* No function should be called after this
*/
void db_mongodb_close(db1_con_t* _h)
{
db_do_close(_h, db_mongodb_free_connection);
}
int db_mongodb_bson_add(bson_t *doc, const db_key_t _k, const db_val_t *_v, int idx)
{
int vtype;
vtype = VAL_TYPE(_v);
if(VAL_NULL(_v)) {
if(!bson_append_null(doc, _k->s, _k->len)) {
LM_ERR("failed to append int to bson doc %.*s = %d [%d]\n",
_k->len, _k->s, VAL_INT(_v), idx);
goto error;
}
goto done;
}
switch(vtype) {
case DB1_INT:
if(!bson_append_int32(doc, _k->s, _k->len,
VAL_INT(_v))) {
LM_ERR("failed to append int to bson doc %.*s = %d [%d]\n",
_k->len, _k->s, VAL_INT(_v), idx);
goto error;
}
break;
case DB1_BIGINT:
if(!bson_append_int64(doc, _k->s, _k->len,
VAL_BIGINT(_v ))) {
LM_ERR("failed to append bigint to bson doc %.*s = %lld [%d]\n",
_k->len, _k->s, VAL_BIGINT(_v), idx);
goto error;
}
return -1;
case DB1_DOUBLE:
if(!bson_append_double(doc, _k->s, _k->len,
VAL_DOUBLE(_v))) {
LM_ERR("failed to append double to bson doc %.*s = %f [%d]\n",
_k->len, _k->s, VAL_DOUBLE(_v), idx);
goto error;
}
break;
case DB1_STRING:
if(!bson_append_utf8(doc, _k->s, _k->len,
VAL_STRING(_v), strlen(VAL_STRING(_v))) ) {
LM_ERR("failed to append string to bson doc %.*s = %s [%d]\n",
_k->len, _k->s, VAL_STRING(_v), idx);
goto error;
}
break;
case DB1_STR:
if(!bson_append_utf8(doc, _k->s, _k->len,
VAL_STR(_v).s, VAL_STR(_v).len) ) {
LM_ERR("failed to append str to bson doc %.*s = %.*s [%d]\n",
_k->len, _k->s, VAL_STR(_v).len, VAL_STR(_v).s, idx);
goto error;
}
break;
case DB1_DATETIME:
if(!bson_append_time_t(doc, _k->s, _k->len,
VAL_TIME(_v))) {
LM_ERR("failed to append time to bson doc %.*s = %ld [%d]\n",
_k->len, _k->s, VAL_TIME(_v), idx);
goto error;
}
break;
case DB1_BLOB:
if(!bson_append_binary(doc, _k->s, _k->len,
BSON_SUBTYPE_BINARY,
(const uint8_t *)VAL_BLOB(_v).s, VAL_BLOB(_v).len) ) {
LM_ERR("failed to append blob to bson doc %.*s = [bin] [%d]\n",
_k->len, _k->s, idx);
goto error;
}
break;
case DB1_BITMAP:
if(!bson_append_int32(doc, _k->s, _k->len,
VAL_INT(_v))) {
LM_ERR("failed to append bitmap to bson doc %.*s = %d [%d]\n",
_k->len, _k->s, VAL_INT(_v), idx);
goto error;
}
break;
default:
LM_ERR("val type [%d] not supported\n", vtype);
return -1;
}
done:
return 0;
error:
return -1;
}
/*!
* \brief Get and convert columns from a result
*
* Get and convert columns from a result, fills the result structure
* with data from the database.
* \param _h database connection
* \param _r database result set
* \return 0 on success, negative on failure
*/
int db_mongodb_get_columns(const db1_con_t* _h, db1_res_t* _r)
{
int col;
db_mongodb_result_t *mgres;
bson_iter_t riter;
bson_iter_t citer;
bson_t *cdoc;
const char *colname;
bson_type_t coltype;
if ((!_h) || (!_r)) {
LM_ERR("invalid parameter\n");
return -1;
}
mgres = (db_mongodb_result_t*)RES_PTR(_r);
if(!mgres->rdoc) {
mgres->nrcols = 0;
return 0;
}
if(mgres->nrcols==0 || mgres->colsdoc==NULL) {
mgres->nrcols = (int)bson_count_keys(mgres->rdoc);
if(mgres->nrcols==0) {
LM_ERR("no keys in bson document\n");
return -1;
}
cdoc = mgres->rdoc;
} else {
cdoc = mgres->colsdoc;
}
RES_COL_N(_r) = mgres->nrcols;
if (!RES_COL_N(_r)) {
LM_ERR("no columns returned from the query\n");
return -2;
} else {
LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
}
if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
RES_COL_N(_r) = 0;
LM_ERR("could not allocate columns\n");
return -3;
}
if (!bson_iter_init (&citer, cdoc)) {
LM_ERR("failed to initialize columns iterator\n");
return -3;
}
if(mgres->colsdoc) {
if (!bson_iter_init (&riter, mgres->rdoc)) {
LM_ERR("failed to initialize result iterator\n");
return -3;
}
}
col = 0;
while (bson_iter_next (&citer)) {
if(col >= RES_COL_N(_r)) {
LM_ERR("invalid number of columns (%d/%d)\n", col, RES_COL_N(_r));
return -4;
}
colname = bson_iter_key (&citer);
LM_DBG("Found a field[%d] named: %s\n", col, colname);
if(mgres->colsdoc) {
if(!bson_iter_find(&riter, colname)) {
LM_ERR("field [%s] not found in result iterator\n",
colname);
return -4;
}
coltype = bson_iter_type(&riter);
} else {
coltype = bson_iter_type(&citer);
}
RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
if (! RES_NAMES(_r)[col]) {
LM_ERR("no private memory left\n");
db_free_columns(_r);
return -4;
}
LM_DBG("allocate %lu bytes for RES_NAMES[%d] at %p\n",
(unsigned long)sizeof(str), col, RES_NAMES(_r)[col]);
/* pointer linked here is part of the result structure */
RES_NAMES(_r)[col]->s = (char*)colname;
RES_NAMES(_r)[col]->len = strlen(colname);
switch(coltype) {
case BSON_TYPE_BOOL:
case BSON_TYPE_INT32:
case BSON_TYPE_TIMESTAMP:
LM_DBG("use DB1_INT result type\n");
RES_TYPES(_r)[col] = DB1_INT;
break;
case BSON_TYPE_INT64:
LM_DBG("use DB1_BIGINT result type\n");
RES_TYPES(_r)[col] = DB1_BIGINT;
break;
case BSON_TYPE_DOUBLE:
LM_DBG("use DB1_DOUBLE result type\n");
RES_TYPES(_r)[col] = DB1_DOUBLE;
break;
case BSON_TYPE_DATE_TIME:
LM_DBG("use DB1_DATETIME result type\n");
RES_TYPES(_r)[col] = DB1_DATETIME;
break;
case BSON_TYPE_BINARY:
LM_DBG("use DB1_BLOB result type\n");
RES_TYPES(_r)[col] = DB1_BLOB;
break;
case BSON_TYPE_UTF8:
LM_DBG("use DB1_STRING result type\n");
RES_TYPES(_r)[col] = DB1_STRING;
break;
#if 0
case BSON_TYPE_EOD:
case BSON_TYPE_DOCUMENT:
case BSON_TYPE_ARRAY:
case BSON_TYPE_UNDEFINED:
case BSON_TYPE_OID:
case BSON_TYPE_NULL:
case BSON_TYPE_REGEX:
case BSON_TYPE_DBPOINTER:
case BSON_TYPE_CODE:
case BSON_TYPE_SYMBOL:
case BSON_TYPE_CODEWSCOPE:
case BSON_TYPE_MAXKEY:
case BSON_TYPE_MINKEY:
#endif
default:
LM_WARN("unhandled data type column (%.*s) type id (%d), "
"use DB1_STRING as default\n", RES_NAMES(_r)[col]->len,
RES_NAMES(_r)[col]->s, coltype);
RES_TYPES(_r)[col] = DB1_STRING;
break;
}
LM_DBG("RES_NAMES(%p)[%d]=[%.*s] (%d)\n", RES_NAMES(_r)[col], col,
RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, coltype);
col++;
}
return 0;
}
/*!
* \brief Convert rows from mongodb to db API representation
* \param _h database connection
* \param _r database result set
* \return 0 on success, negative on failure
*/
static int db_mongdob_convert_bson(const db1_con_t* _h, db1_res_t* _r,
int _row, const bson_t *_rdoc)
{
static str dummy_string = {"", 0};
int col;
db_mongodb_result_t *mgres;
const char *colname;
bson_type_t coltype;
bson_iter_t riter;
bson_iter_t citer;
bson_iter_t *piter;
db_val_t* dval;
uint32_t i32tmp;
bson_subtype_t subtype;
bson_t *cdoc;
mgres = (db_mongodb_result_t*)RES_PTR(_r);
if(mgres->nrcols==0) {
LM_ERR("no fields to convert\n");
return -1;
}
if(mgres->colsdoc==NULL) {
cdoc = (bson_t*)_rdoc;
} else {
cdoc = (bson_t*)mgres->colsdoc;
}
if (!bson_iter_init (&citer, cdoc)) {
LM_ERR("failed to initialize columns iterator\n");
return -3;
}
if(mgres->colsdoc) {
if (!bson_iter_init (&riter, _rdoc)) {
LM_ERR("failed to initialize result iterator\n");
return -3;
}
}
if (db_allocate_row(_r, &(RES_ROWS(_r)[_row])) != 0) {
LM_ERR("could not allocate row: %d\n", _row);
return -2;
}
col = 0;
while (bson_iter_next (&citer)) {
if(col >= RES_COL_N(_r)) {
LM_ERR("invalid number of columns (%d/%d)\n", col, RES_COL_N(_r));
return -4;
}
colname = bson_iter_key (&citer);
LM_DBG("looking for field[%d] named: %s\n", col, colname);
if(mgres->colsdoc) {
if(!bson_iter_find(&riter, colname)) {
LM_ERR("field [%s] not found in result iterator\n",
colname);
return -4;
}
piter = &riter;
} else {
piter = &citer;
}
coltype = bson_iter_type(piter);
dval = &(ROW_VALUES(&(RES_ROWS(_r)[_row]))[col]);
VAL_TYPE(dval) = RES_TYPES(_r)[col];
switch(coltype) {
case BSON_TYPE_BOOL:
VAL_INT(dval) = (int)bson_iter_bool (piter);
break;
case BSON_TYPE_INT32:
VAL_INT(dval) = bson_iter_int32 (piter);
break;
case BSON_TYPE_TIMESTAMP:
bson_iter_timestamp (piter,
(uint32_t*)&VAL_INT(dval), &i32tmp);
break;
case BSON_TYPE_INT64:
VAL_BIGINT(dval) = bson_iter_int64 (piter);
break;
case BSON_TYPE_DOUBLE:
VAL_DOUBLE(dval) = bson_iter_double (piter);
break;
case BSON_TYPE_DATE_TIME:
VAL_TIME(dval) = (time_t)(bson_iter_date_time (piter)/1000);
break;
case BSON_TYPE_BINARY:
bson_iter_binary (piter, &subtype,
(uint32_t*)&VAL_BLOB(dval).len, (const uint8_t**)&VAL_BLOB(dval).s);
break;
case BSON_TYPE_UTF8:
VAL_STRING(dval) = (char*)bson_iter_utf8 (piter, &i32tmp);
break;
case BSON_TYPE_OID:
break;
case BSON_TYPE_NULL:
memset(dval, 0, sizeof(db_val_t));
/* Initialize the string pointers to a dummy empty
* string so that we do not crash when the NULL flag
* is set but the module does not check it properly
*/
VAL_STRING(dval) = dummy_string.s;
VAL_STR(dval) = dummy_string;
VAL_BLOB(dval) = dummy_string;
VAL_TYPE(dval) = RES_TYPES(_r)[col];
VAL_NULL(dval) = 1;
break;
#if 0
case BSON_TYPE_EOD:
case BSON_TYPE_DOCUMENT:
case BSON_TYPE_ARRAY:
case BSON_TYPE_UNDEFINED:
case BSON_TYPE_REGEX:
case BSON_TYPE_DBPOINTER:
case BSON_TYPE_CODE:
case BSON_TYPE_SYMBOL:
case BSON_TYPE_CODEWSCOPE:
case BSON_TYPE_MAXKEY:
case BSON_TYPE_MINKEY:
#endif
default:
LM_WARN("unhandled data type column (%.*s) type id (%d), "
"use DB1_STRING as default\n", RES_NAMES(_r)[col]->len,
RES_NAMES(_r)[col]->s, coltype);
RES_TYPES(_r)[col] = DB1_STRING;
break;
}
LM_DBG("RES_NAMES(%p)[%d]=[%.*s] (%d)\n", RES_NAMES(_r)[col], col,
RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, coltype);
col++;
}
return 0;
}
/*!
* \brief Convert rows from mongodb to db API representation
* \param _h database connection
* \param _r database result set
* \return 0 on success, negative on failure
*/
static int db_mongdob_convert_result(const db1_con_t* _h, db1_res_t* _r)
{
int row;
db_mongodb_result_t *mgres;
const bson_t *itdoc;
char *jstr;
if ((!_h) || (!_r)) {
LM_ERR("invalid parameter\n");
return -1;
}
mgres = (db_mongodb_result_t*)RES_PTR(_r);
if(!mgres->rdoc) {
mgres->nrcols = 0;
return 0;
}
if(mgres->nrcols==0) {
LM_DBG("no fields to return\n");
return 0;
}
if(!mongoc_cursor_more (mgres->cursor)) {
RES_ROW_N(_r) = 1;
mgres->maxrows = 1;
} else {
RES_ROW_N(_r) = DB_MONGODB_ROWS_STEP;
mgres->maxrows = DB_MONGODB_ROWS_STEP;
}
if (db_allocate_rows(_r) < 0) {
LM_ERR("could not allocate rows\n");
RES_ROW_N(_r) = 0;
return -2;
}
itdoc = mgres->rdoc;
row = 0;
do {
if(row >= RES_ROW_N(_r)) {
if (db_reallocate_rows(_r,
RES_ROW_N(_r)+DB_MONGODB_ROWS_STEP) < 0) {
LM_ERR("could not reallocate rows\n");
return -2;
}
mgres->maxrows = RES_ROW_N(_r);
}
if(is_printable(L_DBG)) {
jstr = bson_as_json (itdoc, NULL);
LM_DBG("selected document: %s\n", jstr);
bson_free (jstr);
}
if(db_mongdob_convert_bson(_h, _r, row, itdoc)) {
LM_ERR("failed to convert bson at pos %d\n", row);
return -1;
}
row++;
} while (mongoc_cursor_more (mgres->cursor)
&& mongoc_cursor_next (mgres->cursor, &itdoc));
RES_ROW_N(_r) = row;
LM_DBG("retrieved number of rows: %d\n", row);
return 0;
}
db1_res_t* db_mongodb_new_result(void)
{
db1_res_t* obj;
obj = db_new_result();
if (!obj)
return NULL;
RES_PTR(obj) = pkg_malloc(sizeof(db_mongodb_result_t));
if (!RES_PTR(obj)) {
db_free_result(obj);
return NULL;
}
memset(RES_PTR(obj), 0, sizeof(db_mongodb_result_t));
return obj;
}
/*
* Retrieve result set
*/
static int db_mongodb_store_result(const db1_con_t* _h, db1_res_t** _r)
{
km_mongodb_con_t *mgcon;
db_mongodb_result_t *mgres;
const bson_t *itdoc;
mgcon = MONGODB_CON(_h);
if(!_r) {
LM_ERR("invalid result parameter\n");
return -1;
}
*_r = db_mongodb_new_result();
if (!*_r) {
LM_ERR("no memory left for result \n");
goto error;
}
mgres = (db_mongodb_result_t*)RES_PTR(*_r);
mgres->collection = mgcon->collection;
mgcon->collection = NULL;
mgres->cursor = mgcon->cursor;
mgcon->cursor = NULL;
mgres->colsdoc = mgcon->colsdoc;
mgcon->colsdoc = NULL;
mgres->nrcols = mgcon->nrcols;
mgcon->nrcols = 0;
if(!mongoc_cursor_more (mgres->cursor)
|| !mongoc_cursor_next (mgres->cursor, &itdoc)
|| !itdoc) {
LM_DBG("no result from mongodb\n");
return 0;
}
/* first document linked internally in result to get columns */
mgres->rdoc = (bson_t*)itdoc;
if(db_mongodb_get_columns(_h, *_r)<0) {
LM_ERR("failed to set the columns\n");
goto error;
}
if(db_mongdob_convert_result(_h, *_r)<0) {
LM_ERR("failed to set the rows in result\n");
goto error;
}
return 0;
error:
if(mgcon->colsdoc) {
bson_destroy (mgcon->colsdoc);
mgcon->colsdoc = NULL;
}
mgcon->nrcols = 0;
if(mgcon->cursor) {
mongoc_cursor_destroy (mgcon->cursor);
mgcon->cursor = NULL;
}
if(mgcon->collection) {
mongoc_collection_destroy (mgcon->collection);
mgcon->collection = NULL;
}
return -1;
}
/*
* Release a result set from memory
*/
int db_mongodb_free_result(db1_con_t* _h, db1_res_t* _r)
{
if(!_r)
return -1;
if(RES_PTR(_r)) {
if(((db_mongodb_result_t*)RES_PTR(_r))->rdoc) {
bson_destroy(((db_mongodb_result_t*)RES_PTR(_r))->rdoc);
((db_mongodb_result_t*)RES_PTR(_r))->rdoc = NULL;
}
if(((db_mongodb_result_t*)RES_PTR(_r))->colsdoc) {
bson_destroy (((db_mongodb_result_t*)RES_PTR(_r))->colsdoc);
((db_mongodb_result_t*)RES_PTR(_r))->colsdoc = NULL;
}
((db_mongodb_result_t*)RES_PTR(_r))->nrcols = 0;
if(((db_mongodb_result_t*)RES_PTR(_r))->cursor) {
mongoc_cursor_destroy (((db_mongodb_result_t*)RES_PTR(_r))->cursor);
((db_mongodb_result_t*)RES_PTR(_r))->cursor = NULL;
}
if(((db_mongodb_result_t*)RES_PTR(_r))->collection) {
mongoc_collection_destroy (((db_mongodb_result_t*)RES_PTR(_r))->collection);
((db_mongodb_result_t*)RES_PTR(_r))->collection = NULL;
}
pkg_free(RES_PTR(_r));
}
db_free_result(_r);
return 0;
}
/*
* Query table for specified rows
* _h: structure representing database connection
* _k: key names
* _op: operators
* _v: values of the keys that must match
* _c: column names to return
* _n: number of key=values pairs to compare
* _nc: number of columns to return
* _o: order by the specified column
*/
int db_mongodb_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
const db_key_t _o, db1_res_t** _r)
{
int i;
km_mongodb_con_t *mgcon;
mongoc_client_t *client;
bson_t *seldoc = NULL;
char *cname;
char b1;
char *jstr;
mgcon = MONGODB_CON(_h);
if(mgcon==NULL || mgcon->id== NULL || mgcon->con==NULL) {
LM_ERR("connection to server is null\n");
return -1;
}
if(mgcon->collection) {
mongoc_collection_destroy (mgcon->collection);
mgcon->collection = NULL;
}
if(mgcon->cursor) {
mongoc_cursor_destroy (mgcon->cursor);
mgcon->cursor = NULL;
}
if(mgcon->colsdoc) {
bson_destroy (mgcon->colsdoc);
mgcon->colsdoc = NULL;
}
mgcon->nrcols = 0;
client = mgcon->con;
if(CON_TABLE(_h)->s==NULL) {
LM_ERR("collection (table) name not set\n");
return -1;
}
if(_r) *_r = NULL;
b1 = '\0';
if(CON_TABLE(_h)->s[CON_TABLE(_h)->len]!='\0') {
b1 = CON_TABLE(_h)->s[CON_TABLE(_h)->len];
CON_TABLE(_h)->s[CON_TABLE(_h)->len] = '\0';
}
cname = CON_TABLE(_h)->s;
LM_DBG("query to collection [%s]\n", cname);
mgcon->collection = mongoc_client_get_collection(client, mgcon->id->database, cname);
if(mgcon->collection==NULL) {
LM_ERR("cannot get collection (table): %s\n", cname);
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
return -1;
}
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
seldoc = bson_new();
if(seldoc==NULL) {
LM_ERR("cannot initialize query bson document\n");
goto error;
}
for(i = 0; i < _n; i++) {
if(db_mongodb_bson_add(seldoc, _k[i], _v+i, i)<0)
goto error;
}
if(is_printable(L_DBG)) {
jstr = bson_as_json (seldoc, NULL);
LM_DBG("query filter: %s\n", jstr);
bson_free (jstr);
}
if(_nc > 0) {
mgcon->colsdoc = bson_new();
if(mgcon->colsdoc==NULL) {
LM_ERR("cannot initialize columns bson document\n");
goto error;
}
for(i = 0; i < _nc; i++) {
if(!bson_append_int32(mgcon->colsdoc, _c[i]->s, _c[i]->len, 1))
{
LM_ERR("failed to append int to columns bson %.*s = %d [%d]\n",
_c[i]->len, _c[i]->s, 1, i);
goto error;
}
}
if(is_printable(L_DBG)) {
jstr = bson_as_json (mgcon->colsdoc, NULL);
LM_DBG("columns filter: %s\n", jstr);
bson_free (jstr);
}
mgcon->nrcols = _nc;
}
mgcon->cursor = mongoc_collection_find (mgcon->collection,
MONGOC_QUERY_NONE, 0, 0, 0,
seldoc, mgcon->colsdoc, NULL);
if(!_r) {
goto done;
}
if(db_mongodb_store_result(_h, _r)<0) {
LM_ERR("failed to store result\n");
goto error;
}
done:
bson_destroy (seldoc);
return 0;
error:
LM_ERR("failed to do the query\n");
if(seldoc) bson_destroy (seldoc);
if(mgcon->colsdoc) {
bson_destroy (mgcon->colsdoc);
mgcon->colsdoc = NULL;
}
mgcon->nrcols = 0;
if(mgcon->collection) {
mongoc_collection_destroy (mgcon->collection);
mgcon->collection = NULL;
}
if(mgcon->cursor) {
mongoc_cursor_destroy (mgcon->cursor);
mgcon->cursor = NULL;
}
if(_r && *_r) { db_mongodb_free_result((db1_con_t*)_h, *_r); *_r = NULL; }
return -1;
}
/*!
* \brief Gets a partial result set, fetch rows from a result
*
* Gets a partial result set, fetch a number of rows from a databae result.
* This function initialize the given result structure on the first run, and
* fetches the nrows number of rows. On subsequenting runs, it uses the
* existing result and fetches more rows, until it reaches the end of the
* result set. Because of this the result needs to be null in the first
* invocation of the function. If the number of wanted rows is zero, the
* function returns anything with a result of zero.
* \param _h structure representing the database connection
* \param _r pointer to a structure representing the result
* \param nrows number of fetched rows
* \return return zero on success, negative value on failure
*/
int db_mongodb_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows)
{
return -1;
}
/*
* Execute a raw SQL query
*/
int db_mongodb_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
{
return -1;
}
/*
* Insert a row into specified table
* _h: structure representing database connection
* _k: key names
* _v: values of the keys
* _n: number of key=value pairs
*/
int db_mongodb_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
{
int i;
km_mongodb_con_t *mgcon;
mongoc_client_t *client;
mongoc_collection_t *collection = NULL;
bson_error_t error;
bson_t *doc = NULL;
char *cname;
char *jstr;
char b1;
mgcon = MONGODB_CON(_h);
if(mgcon==NULL || mgcon->id== NULL || mgcon->con==NULL) {
LM_ERR("connection to server is null\n");
return -1;
}
client = mgcon->con;
if(CON_TABLE(_h)->s==NULL) {
LM_ERR("collection (table) name not set\n");
return -1;
}
b1 = '\0';
if(CON_TABLE(_h)->s[CON_TABLE(_h)->len]!='\0') {
b1 = CON_TABLE(_h)->s[CON_TABLE(_h)->len];
CON_TABLE(_h)->s[CON_TABLE(_h)->len] = '\0';
}
cname = CON_TABLE(_h)->s;
collection = mongoc_client_get_collection(client, mgcon->id->database, cname);
if(collection==NULL) {
LM_ERR("cannot get collection (table): %s\n", cname);
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
return -1;
}
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
doc = bson_new();
if(doc==NULL) {
LM_ERR("cannot initialize bson document\n");
goto error;
}
for(i = 0; i < _n; i++) {
if(db_mongodb_bson_add(doc, _k[i], _v+i, i)<0)
goto error;
}
if(is_printable(L_DBG)) {
jstr = bson_as_json (doc, NULL);
LM_DBG("insert document: %s\n", jstr);
bson_free (jstr);
}
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
LM_ERR("failed to insert in collection: %s\n", error.message);
goto error;
}
bson_destroy (doc);
mongoc_collection_destroy (collection);
return 0;
error:
if(doc) bson_destroy (doc);
if(collection) mongoc_collection_destroy (collection);
return -1;
}
/*
* Delete a row from the specified table
* _h: structure representing database connection
* _k: key names
* _o: operators
* _v: values of the keys that must match
* _n: number of key=value pairs
*/
int db_mongodb_delete(const db1_con_t* _h, const db_key_t* _k,
const db_op_t* _o, const db_val_t* _v, const int _n)
{
int i;
km_mongodb_con_t *mgcon;
mongoc_client_t *client;
mongoc_collection_t *collection = NULL;
bson_error_t error;
bson_t *doc = NULL;
char *cname;
char *jstr;
char b1;
mgcon = MONGODB_CON(_h);
if(mgcon==NULL || mgcon->id== NULL || mgcon->con==NULL) {
LM_ERR("connection to server is null\n");
return -1;
}
client = mgcon->con;
if(CON_TABLE(_h)->s==NULL) {
LM_ERR("collection (table) name not set\n");
return -1;
}
b1 = '\0';
if(CON_TABLE(_h)->s[CON_TABLE(_h)->len]!='\0') {
b1 = CON_TABLE(_h)->s[CON_TABLE(_h)->len];
CON_TABLE(_h)->s[CON_TABLE(_h)->len] = '\0';
}
cname = CON_TABLE(_h)->s;
collection = mongoc_client_get_collection(client, mgcon->id->database,
cname);
if(collection==NULL) {
LM_ERR("cannot get collection (table): %s\n", cname);
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
return -1;
}
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
doc = bson_new();
if(doc==NULL) {
LM_ERR("cannot initialize bson document\n");
goto error;
}
for(i = 0; i < _n; i++) {
if(db_mongodb_bson_add(doc, _k[i], _v+i, i)<0)
goto error;
}
if(is_printable(L_DBG)) {
jstr = bson_as_json (doc, NULL);
LM_DBG("delete filter document: %s\n", jstr);
bson_free (jstr);
}
if (!mongoc_collection_remove (collection, MONGOC_REMOVE_NONE,
doc, NULL, &error)) {
LM_ERR("failed to delete in collection: %s\n", error.message);
goto error;
}
bson_destroy (doc);
mongoc_collection_destroy (collection);
return 0;
error:
if(doc) bson_destroy (doc);
if(collection) mongoc_collection_destroy (collection);
return -1;
}
/*
* Update some rows in the specified table
* _h: structure representing database connection
* _k: key names
* _o: operators
* _v: values of the keys that must match
* _uk: updated columns
* _uv: updated values of the columns
* _n: number of key=value pairs
* _un: number of columns to update
*/
int db_mongodb_update(const db1_con_t* _h, const db_key_t* _k,
const db_op_t* _o, const db_val_t* _v, const db_key_t* _uk,
const db_val_t* _uv, const int _n, const int _un)
{
int i;
km_mongodb_con_t *mgcon;
mongoc_client_t *client;
mongoc_collection_t *collection = NULL;
bson_error_t error;
bson_t *mdoc = NULL;
bson_t *udoc = NULL;
char *cname;
char b1;
mgcon = MONGODB_CON(_h);
if(mgcon==NULL || mgcon->id== NULL || mgcon->con==NULL) {
LM_ERR("connection to server is null\n");
return -1;
}
client = mgcon->con;
if(CON_TABLE(_h)->s==NULL) {
LM_ERR("collection (table) name not set\n");
return -1;
}
b1 = '\0';
if(CON_TABLE(_h)->s[CON_TABLE(_h)->len]!='\0') {
b1 = CON_TABLE(_h)->s[CON_TABLE(_h)->len];
CON_TABLE(_h)->s[CON_TABLE(_h)->len] = '\0';
}
cname = CON_TABLE(_h)->s;
collection = mongoc_client_get_collection(client, mgcon->id->database,
cname);
if(collection==NULL) {
LM_ERR("cannot get collection (table): %s\n", cname);
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
return -1;
}
if(b1 != '\0') CON_TABLE(_h)->s[CON_TABLE(_h)->len] = b1;
udoc = bson_new();
if(udoc==NULL) {
LM_ERR("cannot initialize update bson document\n");
goto error;
}
mdoc = bson_new();
if(mdoc==NULL) {
LM_ERR("cannot initialize match bson document\n");
goto error;
}
for(i = 0; i < _un; i++) {
if(db_mongodb_bson_add(udoc, _uk[i], _uv+i, i)<0)
goto error;
}
for(i = 0; i < _n; i++) {
if(db_mongodb_bson_add(mdoc, _k[i], _v+i, i)<0)
goto error;
}
if (!mongoc_collection_find_and_modify (collection, mdoc, NULL, udoc, NULL,
false, false, false, NULL, &error)) {
LM_ERR("failed to update in collection: %s\n", error.message);
goto error;
}
bson_destroy (mdoc);
bson_destroy (udoc);
mongoc_collection_destroy (collection);
return 0;
error:
if(mdoc) bson_destroy (mdoc);
if(udoc) bson_destroy (udoc);
if(collection) mongoc_collection_destroy (collection);
return -1;
}
/*
* Just like insert, but replace the row if it exists
*/
int db_mongodb_replace(const db1_con_t* _h, const db_key_t* _k,
const db_val_t* _v, const int _n, const int _un, const int _m)
{
return -1;
}
/*
* Store name of table that will be used by
* subsequent database functions
*/
int db_mongodb_use_table(db1_con_t* _h, const str* _t)
{
return db_use_table(_h, _t);
}
|