summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2a56e7b)
raw | patch | inline | side by side (parent: 2a56e7b)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 13 Oct 2014 07:07:37 +0000 (09:07 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 13 Oct 2014 07:07:37 +0000 (09:07 +0200) |
src/core/store.c | patch | blob | history | |
src/include/core/store.h | patch | blob | history | |
t/unit/core/store_test.c | patch | blob | history |
diff --git a/src/core/store.c b/src/core/store.c
index f4f2b58f0d700c0df8967aafddd6429261732af8..94b823bfeea6ee81f5189e6646d9051f74f4347a 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
{
sdb_data_t tmp;
- if ((! obj) || (! res))
+ if (! obj)
return -1;
switch (field) {
tmp.data.string = strdup(SDB_OBJ(obj)->name);
if (! tmp.data.string)
return -1;
- *res = tmp;
break;
case SDB_FIELD_LAST_UPDATE:
- res->type = SDB_TYPE_DATETIME;
- res->data.datetime = obj->last_update;
+ tmp.type = SDB_TYPE_DATETIME;
+ tmp.data.datetime = obj->last_update;
break;
case SDB_FIELD_AGE:
- res->type = SDB_TYPE_DATETIME;
- res->data.datetime = sdb_gettime() - obj->last_update;
+ tmp.type = SDB_TYPE_DATETIME;
+ tmp.data.datetime = sdb_gettime() - obj->last_update;
break;
case SDB_FIELD_INTERVAL:
- res->type = SDB_TYPE_DATETIME;
- res->data.datetime = obj->interval;
+ tmp.type = SDB_TYPE_DATETIME;
+ tmp.data.datetime = obj->interval;
break;
case SDB_FIELD_BACKEND:
/* TODO: add support for storing array values in a data object
default:
return -1;
}
+ if (res)
+ *res = tmp;
+ else
+ sdb_data_free_datum(&tmp);
return 0;
} /* sdb_store_get_field */
index 90416a2cc0699cc3df67d50ac516a1d415a9f058..7aeb2875e9cbe302ad0311fc352e824eba8435a1 100644 (file)
--- a/src/include/core/store.h
+++ b/src/include/core/store.h
* sdb_store_get_field:
* Get the value of a stored object's queryable field. The caller is
* responsible for freeing any dynamically allocated memory possibly stored in
- * the returned value.
+ * the returned value. If 'res' is NULL, the function will return whether the
+ * field exists.
*
* Note: Retrieving the backend this way is not currently supported.
*
index 00e3c64389d3843321bb2fcbb396f8174fb0ed68..c1071fb584e0f18b333eb18607c1193f374a4fc1 100644 (file)
--- a/t/unit/core/store_test.c
+++ b/t/unit/core/store_test.c
fail_unless(check < 0,
"sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
"expected: <0");
- check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
- fail_unless(check < 0,
- "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
- "expected: <0");
check = sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, &value);
fail_unless(check < 0,
"sdb_store_get_field(NULL, SDB_FIELD_LAST_UPDATE, <value>) = %d; "
"expected: <0");
+ check = sdb_store_get_field(host, SDB_FIELD_LAST_UPDATE, NULL);
+ fail_unless(check == 0,
+ "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+ "expected: 0");
+ /* 'name' is dynamically allocated; make sure it's not leaked even
+ * if there is no result parameter */
+ check = sdb_store_get_field(host, SDB_FIELD_NAME, NULL);
+ fail_unless(check == 0,
+ "sdb_store_get_field(<host>, SDB_FIELD_LAST_UPDATE, NULL) = %d; "
+ "expected: 0");
+
check = sdb_store_get_field(host, SDB_FIELD_NAME, &value);
fail_unless(check == 0,
"sdb_store_get_field(<host>, SDB_FIELD_NAME, <value>) = "