• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 #ifndef EJDB_TEST_H
3 #define EJDB_TEST_H
4 
5 /**************************************************************************************************
6  * EJDB2
7  *
8  * MIT License
9  *
10  * Copyright (c) 2012-2022 Softmotions Ltd <info@softmotions.com>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  *  copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *************************************************************************************************/
30 
31 #include "ejdb2.h"
32 #include "ejdb2_internal.h"
33 #include <string.h>
34 
put_json(EJDB db,const char * coll,const char * json)35 static iwrc put_json(EJDB db, const char *coll, const char *json) {
36   const size_t len = strlen(json);
37   char buf[len + 1];
38   memcpy(buf, json, len);
39   buf[len] = '\0';
40   for (int i = 0; buf[i]; ++i) {
41     if (buf[i] == '\'') {
42       buf[i] = '"';
43     }
44   }
45   JBL jbl;
46   int64_t llv;
47   iwrc rc = jbl_from_json(&jbl, buf);
48   RCRET(rc);
49   rc = ejdb_put_new(db, coll, jbl, &llv);
50   jbl_destroy(&jbl);
51   return rc;
52 }
53 
put_json2(EJDB db,const char * coll,const char * json,int64_t * id)54 static iwrc put_json2(EJDB db, const char *coll, const char *json, int64_t *id) {
55   const size_t len = strlen(json);
56   char buf[len + 1];
57   memcpy(buf, json, len);
58   buf[len] = '\0';
59   for (int i = 0; buf[i]; ++i) {
60     if (buf[i] == '\'') {
61       buf[i] = '"';
62     }
63   }
64   JBL jbl;
65   iwrc rc = jbl_from_json(&jbl, buf);
66   RCRET(rc);
67 
68   if (*id == 0) {
69     rc = ejdb_put_new(db, coll, jbl, id);
70   } else {
71     rc = ejdb_put(db, coll, jbl, *id);
72   }
73   jbl_destroy(&jbl);
74   return rc;
75 }
76 
patch_json(EJDB db,const char * coll,const char * patchjson,int64_t id)77 static iwrc patch_json(EJDB db, const char *coll, const char *patchjson, int64_t id) {
78   const size_t len = strlen(patchjson);
79   char buf[len + 1];
80   memcpy(buf, patchjson, len);
81   buf[len] = '\0';
82   for (int i = 0; buf[i]; ++i) {
83     if (buf[i] == '\'') {
84       buf[i] = '"';
85     }
86   }
87   return ejdb_patch(db, coll, buf, id);
88 }
89 
90 #endif
91