// retoor #include "tool.h" #include "db.h" #include "r_config.h" #include #include #include static struct json_object *db_get_get_description(void); static char *db_get_execute(tool_t *self, struct json_object *args); static void db_get_print_action(const char *name, struct json_object *args); static struct json_object *db_set_get_description(void); static char *db_set_execute(tool_t *self, struct json_object *args); static void db_set_print_action(const char *name, struct json_object *args); static struct json_object *db_query_get_description(void); static char *db_query_execute(tool_t *self, struct json_object *args); static void db_query_print_action(const char *name, struct json_object *args); static const tool_vtable_t db_get_vtable = { .get_description = db_get_get_description, .execute = db_get_execute, .print_action = db_get_print_action }; static const tool_vtable_t db_set_vtable = { .get_description = db_set_get_description, .execute = db_set_execute, .print_action = db_set_print_action }; static const tool_vtable_t db_query_vtable = { .get_description = db_query_get_description, .execute = db_query_execute, .print_action = db_query_print_action }; static tool_t db_get_tool = { .vtable = &db_get_vtable, .name = "db_get" }; static tool_t db_set_tool = { .vtable = &db_set_vtable, .name = "db_set" }; static tool_t db_query_tool = { .vtable = &db_query_vtable, .name = "db_query" }; tool_t *tool_db_get_create(void) { return &db_get_tool; } tool_t *tool_db_set_create(void) { return &db_set_tool; } tool_t *tool_db_query_create(void) { return &db_query_tool; } static void db_get_print_action(const char *name, struct json_object *args) { (void)name; if (!args) return; struct json_object *key; if (json_object_object_get_ex(args, "key", &key)) { fprintf(stderr, " -> Getting from database: %s\n", json_object_get_string(key)); } } static char *db_get_execute(tool_t *self, struct json_object *args) { (void)self; struct json_object *key_obj; if (!json_object_object_get_ex(args, "key", &key_obj)) { return strdup("Error: missing 'key' argument"); } db_handle db = db_open(NULL); if (!db) return strdup("Failed to open database."); char *value = NULL; r_status_t status = db_kv_get(db, json_object_get_string(key_obj), &value); db_close(db); if (status != R_SUCCESS || !value) { return strdup("Failed to retrieve value from the database."); } struct json_object *result = json_object_new_object(); json_object_object_add(result, "value", json_object_new_string(value)); char *response = strdup(json_object_to_json_string(result)); json_object_put(result); free(value); return response; } static struct json_object *db_get_get_description(void) { struct json_object *root = json_object_new_object(); json_object_object_add(root, "type", json_object_new_string("function")); struct json_object *function = json_object_new_object(); json_object_object_add(function, "name", json_object_new_string("db_get")); json_object_object_add(function, "description", json_object_new_string("Retrieves a value from the database for a given key.")); struct json_object *parameters = json_object_new_object(); json_object_object_add(parameters, "type", json_object_new_string("object")); struct json_object *properties = json_object_new_object(); struct json_object *key = json_object_new_object(); json_object_object_add(key, "type", json_object_new_string("string")); json_object_object_add(key, "description", json_object_new_string("The key to retrieve from the database.")); json_object_object_add(properties, "key", key); json_object_object_add(parameters, "properties", properties); struct json_object *required = json_object_new_array(); json_object_array_add(required, json_object_new_string("key")); json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0)); json_object_object_add(function, "parameters", parameters); r_config_handle cfg = r_config_get_instance(); if (r_config_use_strict(cfg)) { json_object_object_add(function, "strict", json_object_new_boolean(1)); } json_object_object_add(root, "function", function); return root; } static void db_set_print_action(const char *name, struct json_object *args) { (void)name; if (!args) return; struct json_object *key; if (json_object_object_get_ex(args, "key", &key)) { fprintf(stderr, " -> Storing in database: %s\n", json_object_get_string(key)); } } static char *db_set_execute(tool_t *self, struct json_object *args) { (void)self; struct json_object *key_obj, *value_obj; if (!json_object_object_get_ex(args, "key", &key_obj)) { return strdup("Error: missing 'key' argument"); } if (!json_object_object_get_ex(args, "value", &value_obj)) { return strdup("Error: missing 'value' argument"); } db_handle db = db_open(NULL); if (!db) return strdup("Failed to open database."); r_status_t status = db_kv_set(db, json_object_get_string(key_obj), json_object_get_string(value_obj)); db_close(db); if (status != R_SUCCESS) { return strdup("Failed to set value in the database."); } return strdup("Value successfully stored."); } static struct json_object *db_set_get_description(void) { struct json_object *root = json_object_new_object(); json_object_object_add(root, "type", json_object_new_string("function")); struct json_object *function = json_object_new_object(); json_object_object_add(function, "name", json_object_new_string("db_set")); json_object_object_add(function, "description", json_object_new_string("Sets a value in the database for a given key.")); struct json_object *parameters = json_object_new_object(); json_object_object_add(parameters, "type", json_object_new_string("object")); struct json_object *properties = json_object_new_object(); struct json_object *key = json_object_new_object(); json_object_object_add(key, "type", json_object_new_string("string")); json_object_object_add(key, "description", json_object_new_string("The key to set in the database.")); json_object_object_add(properties, "key", key); struct json_object *value = json_object_new_object(); json_object_object_add(value, "type", json_object_new_string("string")); json_object_object_add(value, "description", json_object_new_string("The value to set for the given key.")); json_object_object_add(properties, "value", value); json_object_object_add(parameters, "properties", properties); struct json_object *required = json_object_new_array(); json_object_array_add(required, json_object_new_string("key")); json_object_array_add(required, json_object_new_string("value")); json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0)); json_object_object_add(function, "parameters", parameters); r_config_handle cfg = r_config_get_instance(); if (r_config_use_strict(cfg)) { json_object_object_add(function, "strict", json_object_new_boolean(1)); } json_object_object_add(root, "function", function); return root; } static void db_query_print_action(const char *name, struct json_object *args) { (void)name; if (!args) return; struct json_object *query; if (json_object_object_get_ex(args, "query", &query)) { const char *q = json_object_get_string(query); if (strlen(q) > 60) { fprintf(stderr, " -> Executing SQL: %.60s...\n", q); } else { fprintf(stderr, " -> Executing SQL: %s\n", q); } } } static char *db_query_execute(tool_t *self, struct json_object *args) { (void)self; struct json_object *query_obj; if (!json_object_object_get_ex(args, "query", &query_obj)) { return strdup("Error: missing 'query' argument"); } db_handle db = db_open(NULL); if (!db) return strdup("Failed to open database."); struct json_object *result = NULL; r_status_t status = db_execute(db, json_object_get_string(query_obj), &result); db_close(db); if (status != R_SUCCESS) { return strdup("Failed to execute query on the database."); } char *response = strdup(json_object_to_json_string(result)); json_object_put(result); return response; } static struct json_object *db_query_get_description(void) { struct json_object *root = json_object_new_object(); json_object_object_add(root, "type", json_object_new_string("function")); struct json_object *function = json_object_new_object(); json_object_object_add(function, "name", json_object_new_string("db_query")); json_object_object_add(function, "description", json_object_new_string("Executes a query on the database and returns the results.")); struct json_object *parameters = json_object_new_object(); json_object_object_add(parameters, "type", json_object_new_string("object")); struct json_object *properties = json_object_new_object(); struct json_object *query = json_object_new_object(); json_object_object_add(query, "type", json_object_new_string("string")); json_object_object_add(query, "description", json_object_new_string("The SQL query to execute.")); json_object_object_add(properties, "query", query); json_object_object_add(parameters, "properties", properties); struct json_object *required = json_object_new_array(); json_object_array_add(required, json_object_new_string("query")); json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0)); json_object_object_add(function, "parameters", parameters); r_config_handle cfg = r_config_get_instance(); if (r_config_use_strict(cfg)) { json_object_object_add(function, "strict", json_object_new_boolean(1)); } json_object_object_add(root, "function", function); return root; }