From 7fca2485fc537f492d364d83a26590900bda724a Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 29 Aug 2020 18:20:07 +0200 Subject: [PATCH] Added functions ::get_*_from_json. --- .gitignore | 1 + src/gdaex.c | 40 +++++++++++ src/gdaex.h | 12 ++++ tests/Makefile.am | 1 + tests/getsqlfromhashtable.c | 4 +- tests/getsqlfromjson.c | 134 ++++++++++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 tests/getsqlfromjson.c diff --git a/.gitignore b/.gitignore index a052ce4..dc1a5f7 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ tests/*.exe tests/select tests/getsql tests/getsqlfromhashtable +tests/getsqlfromjson tests/sqlbuilder tests/metastore *~ diff --git a/src/gdaex.c b/src/gdaex.c index d3e574a..2aed5d2 100644 --- a/src/gdaex.c +++ b/src/gdaex.c @@ -3883,6 +3883,46 @@ const gchar return ret; } +GdaSqlBuilder +*gdaex_get_gda_sqlbuilder_from_json (GdaEx *gdaex, + GdaExSqlType sqltype, + const gchar *table_name, + JsonNode *keys, + JsonNode *fields) +{ + GHashTable *ht_keys; + GHashTable *ht_fields; + + ht_keys = zak_utils_json_to_hashtable_gvalue (keys); + ht_fields = zak_utils_json_to_hashtable_gvalue (fields); + + return gdaex_get_gda_sqlbuilder_from_hashtable (gdaex, + sqltype, + table_name, + ht_keys, + ht_fields); +} + +const gchar +*gdaex_get_sql_from_json (GdaEx *gdaex, + GdaExSqlType sqltype, + const gchar *table_name, + JsonNode *keys, + JsonNode *fields) +{ + GHashTable *ht_keys; + GHashTable *ht_fields; + + ht_keys = zak_utils_json_to_hashtable_gvalue (keys); + ht_fields = zak_utils_json_to_hashtable_gvalue (fields); + + return gdaex_get_sql_from_hashtable (gdaex, + sqltype, + table_name, + ht_keys, + ht_fields); +} + guint gdaex_get_new_id (GdaEx *gdaex, const gchar *table_name, diff --git a/src/gdaex.h b/src/gdaex.h index eca4fe8..5779625 100644 --- a/src/gdaex.h +++ b/src/gdaex.h @@ -325,6 +325,18 @@ const gchar *gdaex_get_sql_from_hashtable (GdaEx *gdaex, GHashTable *keys, GHashTable *fields); +GdaSqlBuilder *gdaex_get_gda_sqlbuilder_from_json (GdaEx *gdaex, + GdaExSqlType sqltype, + const gchar *table_name, + JsonNode *keys, + JsonNode *fields); + +const gchar *gdaex_get_sql_from_json (GdaEx *gdaex, + GdaExSqlType sqltype, + const gchar *table_name, + JsonNode *keys, + JsonNode *fields); + guint gdaex_get_new_id (GdaEx *gdaex, const gchar *table_name, const gchar *id_field_name, diff --git a/tests/Makefile.am b/tests/Makefile.am index a6fa781..cfd672f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -11,6 +11,7 @@ noinst_PROGRAMS = \ doubletosql \ fill_liststore \ getsqlfromhashtable \ + getsqlfromjson \ metastore \ query_editor \ select \ diff --git a/tests/getsqlfromhashtable.c b/tests/getsqlfromhashtable.c index 5e6505d..d131b30 100644 --- a/tests/getsqlfromhashtable.c +++ b/tests/getsqlfromhashtable.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2019 Andrea Zagli + * Copyright (C) 2013-2020 Andrea Zagli * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,7 +37,7 @@ main (int argc, char **argv) gtk_init (&argc, &argv); - gdaex = gdaex_new_from_string (argv[1]); + gdaex = gdaex_new_from_string (g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=grid.db", TESTSDIR)); if (gdaex == NULL) { g_error ("Unable to connect to the db."); diff --git a/tests/getsqlfromjson.c b/tests/getsqlfromjson.c new file mode 100644 index 0000000..ee8fa32 --- /dev/null +++ b/tests/getsqlfromjson.c @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2020 Andrea Zagli + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +#include + +#include +#include + +#include + +void +foreach (gpointer key, + gpointer value, + gpointer user_data) +{ + g_printf ("%s: %s\n", + (gchar *)key, + (gchar *)g_value_get_string (value)); +} + + +int +main (int argc, char **argv) +{ + GdaEx *gdaex; + + gchar *keys; + gchar *fields; + + JsonParser *parser; + JsonNode *jnode_keys; + JsonNode *jnode_fields; + + GHashTable *ht_fields; + GHashTable *ht_keys; + + gtk_init (&argc, &argv); + + gdaex = gdaex_new_from_string (g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=grid.db", TESTSDIR)); + if (gdaex == NULL) + { + g_error ("Unable to connect to the db."); + } + + fields = g_strdup ("{\"id\": \"100\"," + "\"name\": \"v_name\"," + "\"surname\": \"v_surname\"," + "\"birthday\": \"2010-08-23\"," + "\"address\": \"rue morgue, 44\"," + "\"incoming\": \"1342.81\"}"); + + parser = json_parser_new (); + + if (!json_parser_load_from_data (parser, fields, -1, NULL)) + { + return 0; + } + + jnode_fields = json_parser_get_root (parser); + + ht_fields = zak_utils_json_to_hashtable_gvalue (jnode_fields); + + g_hash_table_foreach (ht_fields, foreach, NULL); + + g_message ("%s", gdaex_get_sql_from_json (gdaex, GDAEX_SQL_SELECT, "clients", NULL, jnode_fields)); + g_message ("%s", gdaex_get_sql_from_hashtable (gdaex, GDAEX_SQL_SELECT, "clients", NULL, ht_fields)); + + g_message ("%s", gdaex_get_sql_from_json (gdaex, GDAEX_SQL_INSERT, "clients", NULL, jnode_fields)); + g_message ("%s", gdaex_get_sql_from_hashtable (gdaex, GDAEX_SQL_INSERT, "clients", NULL, ht_fields)); + + keys = g_strdup ("{\"id\": \"44\"}"); + + parser = json_parser_new (); + + if (!json_parser_load_from_data (parser, keys, -1, NULL)) + { + return 0; + } + + jnode_keys = json_parser_get_root (parser); + + ht_keys = zak_utils_json_to_hashtable_gvalue (jnode_keys); + + g_hash_table_foreach (ht_keys, foreach, NULL); + + g_message ("%s", gdaex_get_sql_from_json (gdaex, GDAEX_SQL_SELECT, "clients", jnode_keys, jnode_fields)); + g_message ("%s", gdaex_get_sql_from_hashtable (gdaex, GDAEX_SQL_SELECT, "clients", ht_keys, ht_fields)); + + g_free (fields); + + fields = g_strdup ("{\"name\": \"my new name\"," + "\"surname\": \"and this is the new sur'name with '\"}"); + + parser = json_parser_new (); + + if (!json_parser_load_from_data (parser, fields, -1, NULL)) + { + return 0; + } + + jnode_fields = json_parser_get_root (parser); + + ht_fields = zak_utils_json_to_hashtable_gvalue (jnode_fields); + + g_hash_table_foreach (ht_fields, foreach, NULL); + + g_message ("%s", gdaex_get_sql_from_json (gdaex, GDAEX_SQL_UPDATE, "clients", jnode_keys, jnode_fields)); + g_message ("%s", gdaex_get_sql_from_hashtable (gdaex, GDAEX_SQL_UPDATE, "clients", ht_keys, ht_fields)); + + g_free (fields); + g_free (keys); + + g_message ("%s", gdaex_get_sql_from_json (gdaex, GDAEX_SQL_DELETE, "clients", jnode_keys, NULL)); + g_message ("%s", gdaex_get_sql_from_hashtable (gdaex, GDAEX_SQL_DELETE, "clients", ht_keys, NULL)); + + return 0; +} -- 2.49.0