aboutsummaryrefslogtreecommitdiffstats
path: root/ll-database-binding/src
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-10-24 17:18:44 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-10-24 17:18:44 +0200
commit36fbb49b0b8ca2d07953a385f1601429015b566f (patch)
treef337491de1a0c9fb9089040f19ac331e4fdcbac3 /ll-database-binding/src
parent391d9c38e2f5052903148d8b91269eac41662b81 (diff)
database: Add get_key function
Change-Id: I7789734f46a58f5c3e1e7e75143869dd33f53e03 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'll-database-binding/src')
-rw-r--r--ll-database-binding/src/ll-database-binding.c92
1 files changed, 60 insertions, 32 deletions
diff --git a/ll-database-binding/src/ll-database-binding.c b/ll-database-binding/src/ll-database-binding.c
index 250c392..5b63fa9 100644
--- a/ll-database-binding/src/ll-database-binding.c
+++ b/ll-database-binding/src/ll-database-binding.c
@@ -98,38 +98,76 @@ static int ll_database_binding_init()
}
/**
- * @brief Handle the @c read verb.
- * @param[in] req The query.
+ * Returns the database key for the 'req'
*/
-static void verb_insert(struct afb_req req)
+static int get_key(struct afb_req req, DBT *key)
{
- DBT key;
- DBT data;
- int ret;
+ char *appid, *data;
+ const char *jkey;
- char* rkey;
- const char* tag;
- const char* value;
+ size_t ljkey, lappid, size;
struct json_object* args;
struct json_object* item;
+ /* get the key */
args = afb_req_json(req);
-
- if (!args)
+ if (!json_object_object_get_ex(args, "key", &item))
{
- afb_req_fail(req, "No argument provided.", NULL);
- return;
+ afb_req_fail(req, "no-key", NULL);
+ return -1;
+ }
+ if (!item
+ || !(jkey = json_object_get_string(item))
+ || !(ljkey = strlen(jkey)))
+ {
+ afb_req_fail(req, "bad-key", NULL);
+ return -1;
}
- if (!json_object_object_get_ex(args, "key", &item) || !item) tag = NULL;
- else tag = json_object_get_string(item);
+ /* get the appid */
+ appid = afb_req_get_application_id(req);
+ if (!appid)
+ {
+ afb_req_fail(req, "bad-context", NULL);
+ return -1;
+ }
- if (!tag || !strlen(tag))
+ /* make the db-key */
+ lappid = strlen(appid);
+ size = lappid + ljkey + 2;
+ data = realloc(appid, size);
+ if (!data)
{
- afb_req_fail(req, "No tag provided.", NULL);
- return;
+ free(appid);
+ afb_req_fail(req, "out-of-memory", NULL);
+ return -1;
}
+ data[lappid] = ':';
+ memcpy(&data[lappid + 1], jkey, ljkey + 1);
+
+ /* return the key */
+ key->data = data;
+ key->size = (uint32_t)size;
+ return 0;
+}
+
+/**
+ * @brief Handle the @c read verb.
+ * @param[in] req The query.
+ */
+static void verb_insert(struct afb_req req)
+{
+ int ret;
+ DBT key;
+ DBT data;
+
+ const char* value;
+
+ struct json_object* args;
+ struct json_object* item;
+
+ args = afb_req_json(req);
if (!json_object_object_get_ex(args, "value", &item) || !item) value = NULL;
else value = json_object_get_string(item);
@@ -140,20 +178,10 @@ static void verb_insert(struct afb_req req)
return;
}
- rkey = malloc(strlen(USERNAME) + strlen(APPNAME) + strlen(tag) + 3);
- strcpy(rkey, USERNAME);
- strcat(rkey, ":");
- strcat(rkey, APPNAME);
- strcat(rkey, ":");
- strcat(rkey, tag);
-
- AFB_INFO("insert: key=%s, value=%s", rkey, value);
+ if (get_key(req, &key))
+ return;
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
-
- key.data = rkey;
- key.size = (uint32_t)strlen(rkey);
+ AFB_INFO("insert: key=%s, value=%s", (char*)key.data, value);
data.data = (void*)value;
data.size = (uint32_t)strlen(value);
@@ -162,7 +190,7 @@ static void verb_insert(struct afb_req req)
afb_req_success_f(req, NULL, "db success: insertion %s=%s.", (char*)key.data, (char*)data.data);
else
afb_req_fail_f(req, "Failed to insert datas.", "db fail: insertion : %s=%s - %s", (char*)key.data, (char*)data.data, db_strerror(ret));
- free(rkey);
+ free(key.data);
}
static void verb_update(struct afb_req req)