From: Andrea Zagli Date: Sun, 25 Sep 2016 15:29:31 +0000 (+0200) Subject: Refactoring of ZakConfiKey (only gchar *). X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=50f727f9333b3328070c09d5bb805d0bcce95795;p=zakconfi%2Flibzakconfi Refactoring of ZakConfiKey (only gchar *). --- diff --git a/plugins/db/plgdb.c b/plugins/db/plgdb.c index 446507a..9d01540 100644 --- a/plugins/db/plgdb.c +++ b/plugins/db/plgdb.c @@ -359,9 +359,7 @@ zak_confi_db_plugin_get_children (ZakConfiPluggable *pluggable, GNode *parentNod GNode *newNode; ZakConfiKey *ck = g_new0 (ZakConfiKey, 1); - ck->id_config = priv->id_config; - ck->id = gdaex_data_model_get_field_value_integer_at (dm, i, "id"); - ck->id_parent = gdaex_data_model_get_field_value_integer_at (dm, i, "id_parent"); + ck->config = g_strdup (priv->name); ck->key = g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, i, "key")); ck->value = g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, i, "value")); ck->description = g_strdup (gdaex_data_model_get_field_value_stringify_at (dm, i, "description")); @@ -369,7 +367,10 @@ zak_confi_db_plugin_get_children (ZakConfiPluggable *pluggable, GNode *parentNod newNode = g_node_append_data (parentNode, ck); - zak_confi_db_plugin_get_children (pluggable, newNode, ck->id, g_strconcat (path, (g_strcmp0 (path, "") == 0 ? "" : "/"), ck->key, NULL)); + zak_confi_db_plugin_get_children (pluggable, + newNode, + gdaex_data_model_get_field_value_integer_at (dm, i, "id"), + g_strconcat (path, (g_strcmp0 (path, "") == 0 ? "" : "/"), ck->key, NULL)); } g_object_unref (dm); } @@ -387,7 +388,6 @@ static GList gchar *where; guint row; - guint id; guint rows; ZakConfiDBPluginPrivate *priv = ZAK_CONFI_DB_PLUGIN_GET_PRIVATE (pluggable); @@ -506,13 +506,11 @@ GNode ZakConfiKey *ck = g_new0 (ZakConfiKey, 1); - ck->id_config = priv->id_config; - ck->id = 0; - ck->id_parent = 0; + ck->config = g_strdup (priv->name); ck->path = ""; - ck->description = ""; ck->key = g_strdup ("/"); ck->value = ""; + ck->description = ""; node = g_node_new (ck); @@ -574,7 +572,7 @@ static ZakConfiKey sql = g_strdup_printf ("SELECT id" " FROM %cvalues%c" " WHERE id_configs = %d" - " AND name = '%s'", + " AND key = '%s'", priv->chrquot, priv->chrquot, priv->id_config, gdaex_strescape (key_, NULL)); @@ -582,7 +580,6 @@ static ZakConfiKey g_free (sql); if (dm != NULL && gda_data_model_get_n_rows (dm) > 0) { - g_free (sql); id = gdaex_data_model_get_value_integer_at (dm, 0, 0); g_object_unref (dm); @@ -607,7 +604,6 @@ static ZakConfiKey } else { - g_free (sql); id = 0; /* find new id */ @@ -645,12 +641,7 @@ static ZakConfiKey } ck = g_new0 (ZakConfiKey, 1); - ck->id_config = priv->id_config; - ck->id = id; - ck->id_parent = id_parent; - ck->key = g_strdup (key_); - ck->value = g_strdup (""); - ck->description = g_strdup (""); + ck->config = g_strdup (priv->name); if (id_parent == 0) { ck->path = g_strdup (""); @@ -659,6 +650,9 @@ static ZakConfiKey { ck->path = g_strdup (parent_); } + ck->key = g_strdup (key_); + ck->value = g_strdup (""); + ck->description = g_strdup (""); g_free (key_); } @@ -666,7 +660,7 @@ static ZakConfiKey if (ck != NULL) { - if (ck->id_parent != 0) + if (g_strcmp0 (ck->path, "") != 0) { path = g_strconcat (ck->path, "/", key, NULL); } @@ -703,14 +697,15 @@ zak_confi_db_plugin_key_set_key (ZakConfiPluggable *pluggable, " value = '%s'," " description = '%s'" " WHERE id_configs = %d" - " AND id = %d", + " AND id = (SELECT id FROM %cconfigs%c WHERE name = '%s')", priv->chrquot, priv->chrquot, priv->chrquot, priv->chrquot, gdaex_strescape (ck->key, NULL), gdaex_strescape (ck->value, NULL), gdaex_strescape (ck->description, NULL), priv->id_config, - ck->id); + priv->chrquot, priv->chrquot, + ck->config); ret = (gdaex_execute (priv->gdaex, sql) >= 0); g_free (sql); @@ -744,13 +739,11 @@ static ZakConfiKey } ck = g_new0 (ZakConfiKey, 1); - ck->id_config = gdaex_data_model_get_field_value_integer_at (dm, 0, "id_configs"); - ck->id = gdaex_data_model_get_field_value_integer_at (dm, 0, "id"); - ck->id_parent = gdaex_data_model_get_field_value_integer_at (dm, 0, "id_parent"); + ck->config = g_strdup (priv->name); + ck->path = g_strdup (path_); ck->key = gdaex_data_model_get_field_value_stringify_at (dm, 0, "key"); ck->value = gdaex_data_model_get_field_value_stringify_at (dm, 0, "value"); ck->description = gdaex_data_model_get_field_value_stringify_at (dm, 0, "description"); - ck->path = g_strdup (path_); if (dm != NULL) { @@ -791,10 +784,15 @@ zak_confi_db_plugin_delete_id_from_db_values (ZakConfiPluggable *pluggable, gint static gboolean zak_confi_db_plugin_remove_path_traverse_func (GNode *node, gpointer data) { + GdaDataModel *dm; + ZakConfiPluggable* pluggable; + + pluggable = (ZakConfiPluggable *)data; ZakConfiKey *ck = (ZakConfiKey *)node->data; - if (ck->id != 0) + if (g_strcmp0 (ck->value, "") != 0) { - zak_confi_db_plugin_delete_id_from_db_values ((ZakConfiPluggable *)data, ck->id); + dm = zak_confi_db_plugin_path_get_data_model (pluggable, g_strdup_printf ("%s/%s", ck->path, ck->key)); + zak_confi_db_plugin_delete_id_from_db_values (pluggable, gdaex_data_model_get_field_value_integer_at (dm, 0, "id")); } return FALSE; diff --git a/plugins/file/plgfile.c b/plugins/file/plgfile.c index c4edfc7..7382d80 100644 --- a/plugins/file/plgfile.c +++ b/plugins/file/plgfile.c @@ -406,10 +406,11 @@ GNode ZakConfiKey *ck = g_new0 (ZakConfiKey, 1); + ck->config = "Default"; ck->path = ""; - ck->description = ""; ck->key = g_strdup ("/"); ck->value = ""; + ck->description = ""; node = g_node_new (ck); @@ -438,10 +439,11 @@ static ZakConfiKey if (zak_confi_file_plugin_path_get_group_and_key (path, &group, &key_)) { ck = g_new0 (ZakConfiKey, 1); + ck->config = "Default"; + ck->path = g_strdup (path); ck->key = g_strdup (key); ck->value = zak_confi_file_plugin_path_get_value (pluggable, path); ck->description = g_key_file_get_comment (priv->kfile, group, key, NULL);; - ck->path = g_strdup (path); g_free (group); g_free (key_); @@ -491,10 +493,11 @@ static ZakConfiKey if (zak_confi_file_plugin_path_get_group_and_key (path_, &group, &key)) { ck = g_new0 (ZakConfiKey, 1); + ck->config = "Default"; + ck->path = g_strdup (group); ck->key = g_strdup (key); ck->value = zak_confi_file_plugin_path_get_value (pluggable, path_); ck->description = g_key_file_get_comment (priv->kfile, group, key, NULL); - ck->path = g_strdup (group); g_free (group); g_free (key); diff --git a/src/commons.c b/src/commons.c index 5b01a8d..342bb8c 100644 --- a/src/commons.c +++ b/src/commons.c @@ -23,7 +23,7 @@ #include #endif -#include +#include "commons.h" ZakConfiConfi *zak_confi_confi_copy (ZakConfiConfi *confi) @@ -53,13 +53,11 @@ ZakConfiKey ZakConfiKey *b; b = g_slice_new (ZakConfiKey); - b->id_config = key->id_config; - b->id = key->id; - b->id_parent = key->id_parent; + b->config = g_strdup (key->config); + b->path = g_strdup (key->path); b->key = g_strdup (key->key); b->value = g_strdup (key->value); b->description = g_strdup (key->description); - b->path = g_strdup (key->path); return b; } @@ -67,10 +65,11 @@ ZakConfiKey void zak_confi_key_free (ZakConfiKey *key) { + g_free (key->config); + g_free (key->path); g_free (key->key); g_free (key->value); g_free (key->description); - g_free (key->path); g_slice_free (ZakConfiKey, key); } diff --git a/src/commons.h b/src/commons.h index f3a866c..514094b 100644 --- a/src/commons.h +++ b/src/commons.h @@ -46,13 +46,11 @@ GType zak_confi_key_get_type (); typedef struct _ZakConfiKey ZakConfiKey; struct _ZakConfiKey { - gint id_config; - gint id; - gint id_parent; + gchar *config; + gchar *path; gchar *key; gchar *value; gchar *description; - gchar *path; }; ZakConfiKey *zak_confi_key_copy (ZakConfiKey *key); diff --git a/src/confi.c b/src/confi.c index 9304550..2d5ae68 100644 --- a/src/confi.c +++ b/src/confi.c @@ -249,7 +249,7 @@ PeasPluginInfo * contains configuration. * @filter: (nullable): * - * Returns: (element-type ZakConfi) (transfer container): a #GList of #ZakConfiZakConfi. If there's no configurations, returns a valid + * Returns: (element-type ZakConfiConfi) (transfer container): a #GList of #ZakConfiConfi. If there's no configurations, returns a valid * #GList but with a unique NULL element. */ GList diff --git a/tests/test.sql b/tests/test.sql index d6bdd41..b5eaabe 100644 --- a/tests/test.sql +++ b/tests/test.sql @@ -1,4 +1,4 @@ -INSERT INTO configs (id, name, description) VALUES (1, 'Default', ''); +INSERT INTO configs (id, name, description) VALUES (1, 'Default', 'Default configuration from file'); INSERT INTO "values" (id_configs, id, id_parent, key, value, description) VALUES (1, 1, 0, 'folder', '', ''); INSERT INTO "values" (id_configs, id, id_parent, key, value, description) VALUES (1, 2, 1, 'key1', 'value key 1', '');