@@ -525,15 +525,16 @@ static inline int _json_to_cols(cJSON *Jcols, db_key_t** _c)
525525
526526
527527static inline int _json_to_filters (cJSON * Jfilter ,
528- db_key_t * * _k , db_op_t * * _o , db_val_t * * _v , int only_equal )
528+ db_key_t * * _k , db_op_t * * _o , db_val_t * * _v , int are_keys )
529529{
530- static db_key_t * keys ;
531- static db_op_t * ops ;
532- static db_val_t * vals ;
533- static unsigned int keys_size = 0 ;
530+ /* first set if for filters, second for keys (only for udpates) */
531+ static db_key_t * keys [2 ];
532+ static db_op_t * ops [2 ];
533+ static db_val_t * vals [2 ];
534+ static unsigned int keys_size [2 ] = {0 ,0 };
534535 cJSON * filter , * node ;
535536 str * str_keys ;
536- int nk , i ;
537+ int nk , i , x = are_keys ? 1 : 0 ;
537538
538539 if (Jfilter -> type != cJSON_Array ) {
539540 LM_ERR ("bad JSON format, 'filter' must be an array\n" );
@@ -554,73 +555,73 @@ static inline int _json_to_filters(cJSON *Jfilter,
554555 }
555556
556557 /* resize the array of cols if we need more */
557- if (nk > keys_size ) {
558+ if (nk > keys_size [ x ] ) {
558559 /* need a larger set of keys */
559- keys = (db_key_t * )pkg_realloc ( keys ,
560+ keys [ x ] = (db_key_t * )pkg_realloc ( keys [ x ] ,
560561 nk * (sizeof (db_key_t )+ sizeof (str )+ sizeof (db_op_t )+ sizeof (db_val_t ))
561562 );
562- if (keys == NULL ) {
563- keys_size = 0 ;
563+ if (keys [ x ] == NULL ) {
564+ keys_size [ x ] = 0 ;
564565 LM_ERR ("failed to allocate the needed %d keys/cols\n" ,nk );
565566 goto error ;
566567 }
567- str_keys = (str * )( keys + nk );
568- ops = (db_op_t * )(str_keys + nk );
569- vals = (db_val_t * )(ops + nk );
568+ str_keys = (str * )( keys [ x ] + nk );
569+ ops [ x ] = (db_op_t * )(str_keys + nk );
570+ vals [ x ] = (db_val_t * )(ops [ x ] + nk );
570571 /* link db_keys to strs */
571572 for ( i = 0 ; i < nk ; i ++ )
572- keys [i ] = & str_keys [i ];
573+ keys [x ][ i ] = & str_keys [i ];
573574
574- keys_size = nk ;
575+ keys_size [ x ] = nk ;
575576 }
576577
577578 /* iterate again to fill in the cols */
578579 for ( filter = Jfilter -> child ,i = 0 ; filter ; filter = filter -> next ,i ++ ) {
579580 /* name of the key/col */
580581 node = filter -> child ;
581- keys [i ]-> s = node -> string ;
582- keys [i ]-> len = strlen (node -> string );
582+ keys [x ][ i ]-> s = node -> string ;
583+ keys [x ][ i ]-> len = strlen (node -> string );
583584 /* operator */
584585 if (node -> type == cJSON_Object ) {
585586 node = node -> child ;
586- ops [i ] = node -> string ;
587- if (only_equal && memcmp (ops [i ],OP_EQ ,sizeof (OP_EQ ))) {
587+ ops [x ][ i ] = node -> string ;
588+ if (are_keys && memcmp (ops [i ],OP_EQ ,sizeof (OP_EQ ))) {
588589 LM_ERR ("only equal allowed between keys and values at "
589590 "pos %d\n" ,i );
590591 goto error ;
591592 }
592593 } else {
593- ops [i ] = OP_EQ ;
594+ ops [x ][ i ] = OP_EQ ;
594595 }
595596 /* value */
596597 switch (node -> type ) {
597598 case cJSON_NULL :
598- vals [i ].type = 0 ;
599- vals [i ].nul = 1 ;
600- vals [i ].free = 0 ;
601- vals [i ].val .bigint_val = 0 ;
599+ vals [x ][ i ].type = 0 ;
600+ vals [x ][ i ].nul = 1 ;
601+ vals [x ][ i ].free = 0 ;
602+ vals [x ][ i ].val .bigint_val = 0 ;
602603 break ;
603604 case cJSON_String :
604- vals [i ].type = DB_STRING ;
605- vals [i ].nul = 0 ;
606- vals [i ].free = 0 ;
607- vals [i ].val .string_val = node -> valuestring ;
605+ vals [x ][ i ].type = DB_STRING ;
606+ vals [x ][ i ].nul = 0 ;
607+ vals [x ][ i ].free = 0 ;
608+ vals [x ][ i ].val .string_val = node -> valuestring ;
608609 break ;
609610 case cJSON_Number :
610- vals [i ].type = DB_INT ;
611- vals [i ].nul = 0 ;
612- vals [i ].free = 0 ;
613- vals [i ].val .int_val = node -> valueint ;
611+ vals [x ][ i ].type = DB_INT ;
612+ vals [x ][ i ].nul = 0 ;
613+ vals [x ][ i ].free = 0 ;
614+ vals [x ][ i ].val .int_val = node -> valueint ;
614615 break ;
615616 default :
616617 LM_ERR ("unsupported value node %d\n" ,node -> type );
617618 goto error ;
618619 }
619620 }
620621
621- * _k = keys ;
622- * _o = ops ;
623- * _v = vals ;
622+ * _k = keys [ x ] ;
623+ * _o = ops [ x ] ;
624+ * _v = vals [ x ] ;
624625
625626 return nk ;
626627error :
0 commit comments