2017-01-22 12:40:32 +00:00
|
|
|
/* Copyright (C) 2010-2017 The RetroArch team
|
2016-03-21 17:16:05 +00:00
|
|
|
*
|
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
* The following license statement only applies to this file (libretrodb_tool.c).
|
|
|
|
* ---------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge,
|
|
|
|
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2015-01-19 21:47:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-16 15:33:18 +00:00
|
|
|
#include <string/stdstring.h>
|
|
|
|
|
2015-01-23 04:59:47 +00:00
|
|
|
#include "libretrodb.h"
|
2015-01-19 21:47:09 +00:00
|
|
|
#include "rmsgpack_dom.h"
|
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
int rv;
|
2015-09-17 07:46:26 +00:00
|
|
|
libretrodb_t *db;
|
|
|
|
libretrodb_cursor_t *cur;
|
2015-01-23 04:59:47 +00:00
|
|
|
libretrodb_query_t *q;
|
2015-01-24 03:04:56 +00:00
|
|
|
struct rmsgpack_dom_value item;
|
2015-01-23 04:59:47 +00:00
|
|
|
const char *command, *path, *query_exp, *error;
|
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
printf("Usage: %s <db file> <command> [extra args...]\n", argv[0]);
|
|
|
|
printf("Available Commands:\n");
|
|
|
|
printf("\tlist\n");
|
|
|
|
printf("\tcreate-index <index name> <field name>\n");
|
|
|
|
printf("\tfind <query expression>\n");
|
2017-11-16 00:42:22 +00:00
|
|
|
printf("\tget-names <query expression>\n");
|
2015-01-24 03:04:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
command = argv[2];
|
|
|
|
path = argv[1];
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-09-17 07:46:26 +00:00
|
|
|
db = libretrodb_new();
|
|
|
|
cur = libretrodb_cursor_new();
|
|
|
|
|
|
|
|
if (!db || !cur)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((rv = libretrodb_open(path, db)) != 0)
|
2015-01-24 03:04:56 +00:00
|
|
|
{
|
|
|
|
printf("Could not open db file '%s': %s\n", path, strerror(-rv));
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
2017-04-22 06:51:16 +00:00
|
|
|
else if (memcmp(command, "list", 4) == 0)
|
2015-01-24 03:04:56 +00:00
|
|
|
{
|
2015-09-17 07:46:26 +00:00
|
|
|
if ((rv = libretrodb_cursor_open(db, cur, NULL)) != 0)
|
2015-01-24 03:04:56 +00:00
|
|
|
{
|
|
|
|
printf("Could not open cursor: %s\n", strerror(-rv));
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != 3)
|
|
|
|
{
|
|
|
|
printf("Usage: %s <db file> list\n", argv[0]);
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:46:26 +00:00
|
|
|
while (libretrodb_cursor_read_item(cur, &item) == 0)
|
2015-01-24 03:04:56 +00:00
|
|
|
{
|
|
|
|
rmsgpack_dom_value_print(&item);
|
|
|
|
printf("\n");
|
|
|
|
rmsgpack_dom_value_free(&item);
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 06:51:16 +00:00
|
|
|
else if (memcmp(command, "find", 4) == 0)
|
2015-01-24 03:04:56 +00:00
|
|
|
{
|
|
|
|
if (argc != 4)
|
|
|
|
{
|
|
|
|
printf("Usage: %s <db file> find <query expression>\n", argv[0]);
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
query_exp = argv[3];
|
|
|
|
error = NULL;
|
2015-09-17 07:46:26 +00:00
|
|
|
q = libretrodb_query_compile(db, query_exp, strlen(query_exp), &error);
|
2015-01-23 04:59:47 +00:00
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
printf("%s\n", error);
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-09-17 07:46:26 +00:00
|
|
|
if ((rv = libretrodb_cursor_open(db, cur, q)) != 0)
|
2015-01-23 04:59:47 +00:00
|
|
|
{
|
2015-01-24 03:04:56 +00:00
|
|
|
printf("Could not open cursor: %s\n", strerror(-rv));
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-09-17 07:46:26 +00:00
|
|
|
while (libretrodb_cursor_read_item(cur, &item) == 0)
|
2015-01-23 04:59:47 +00:00
|
|
|
{
|
2015-01-24 03:04:56 +00:00
|
|
|
rmsgpack_dom_value_print(&item);
|
|
|
|
printf("\n");
|
|
|
|
rmsgpack_dom_value_free(&item);
|
|
|
|
}
|
|
|
|
}
|
2017-11-16 00:42:22 +00:00
|
|
|
else if (memcmp(command, "get-names", 9) == 0)
|
|
|
|
{
|
|
|
|
if (argc != 4)
|
|
|
|
{
|
2017-11-19 21:25:17 +00:00
|
|
|
printf("Usage: %s <db file> get-names <query expression>\n", argv[0]);
|
2017-11-16 00:42:22 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-12-12 07:55:31 +00:00
|
|
|
|
2017-11-16 00:42:22 +00:00
|
|
|
query_exp = argv[3];
|
|
|
|
error = NULL;
|
|
|
|
q = libretrodb_query_compile(db, query_exp, strlen(query_exp), &error);
|
2017-12-12 07:55:31 +00:00
|
|
|
|
2017-11-16 00:42:22 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
printf("%s\n", error);
|
|
|
|
goto error;
|
|
|
|
}
|
2017-12-12 07:55:31 +00:00
|
|
|
|
2017-11-16 00:42:22 +00:00
|
|
|
if ((rv = libretrodb_cursor_open(db, cur, q)) != 0)
|
|
|
|
{
|
|
|
|
printf("Could not open cursor: %s\n", strerror(-rv));
|
|
|
|
goto error;
|
|
|
|
}
|
2017-12-12 07:55:31 +00:00
|
|
|
|
2017-11-16 00:42:22 +00:00
|
|
|
while (libretrodb_cursor_read_item(cur, &item) == 0)
|
|
|
|
{
|
|
|
|
if (item.type == RDT_MAP) //should always be true, but if false the program would segfault
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < item.val.map.len; i++)
|
|
|
|
{
|
|
|
|
if (item.val.map.items[i].key.type == RDT_STRING && (strncmp(item.val.map.items[i].key.val.string.buff, "name", item.val.map.items[i].key.val.string.len) == 0))
|
|
|
|
{
|
|
|
|
rmsgpack_dom_value_print(&item.val.map.items[i].value);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsgpack_dom_value_free(&item);
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 06:51:16 +00:00
|
|
|
else if (memcmp(command, "create-index", 12) == 0)
|
2015-01-24 03:04:56 +00:00
|
|
|
{
|
|
|
|
const char * index_name, * field_name;
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
if (argc != 5)
|
|
|
|
{
|
|
|
|
printf("Usage: %s <db file> create-index <index name> <field name>\n", argv[0]);
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-01-24 03:04:56 +00:00
|
|
|
index_name = argv[3];
|
|
|
|
field_name = argv[4];
|
2015-01-19 21:47:09 +00:00
|
|
|
|
2015-09-17 07:46:26 +00:00
|
|
|
libretrodb_create_index(db, index_name, field_name);
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-24 07:17:04 +00:00
|
|
|
printf("Unknown command %s\n", argv[2]);
|
2015-09-17 07:46:26 +00:00
|
|
|
goto error;
|
2015-01-24 03:04:56 +00:00
|
|
|
}
|
2015-01-27 03:02:10 +00:00
|
|
|
|
2017-11-18 00:44:15 +00:00
|
|
|
libretrodb_cursor_close(cur);
|
2015-09-17 07:46:26 +00:00
|
|
|
libretrodb_close(db);
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (db)
|
|
|
|
libretrodb_free(db);
|
|
|
|
if (cur)
|
|
|
|
libretrodb_cursor_free(cur);
|
2017-11-18 00:44:15 +00:00
|
|
|
if (q)
|
|
|
|
libretrodb_query_free(q);
|
2015-01-27 03:02:10 +00:00
|
|
|
return 1;
|
2015-01-19 21:47:09 +00:00
|
|
|
}
|