From: Andrea Zagli Date: Sun, 21 Jul 2019 08:06:55 +0000 (+0200) Subject: Implemented Db plugin interface functions (without test). X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=f4f5c9e7c341a496912a7ce0969e9e2a078bc2c4;p=zakbookmarks%2Flibzakbookmarks Implemented Db plugin interface functions (without test). --- diff --git a/plugins/db/plgdb.c b/plugins/db/plgdb.c index 6d2c332..6d5ab50 100644 --- a/plugins/db/plgdb.c +++ b/plugins/db/plgdb.c @@ -139,19 +139,135 @@ zak_bks_db_plugin_initialize (ZakBksPluggable *pluggable, const gchar *cnc_strin static GList *zak_bks_db_plugin_get_bookmarks (ZakBksPluggable *pluggable) { + GdaExSqlBuilder *sqlb; + GdaDataModel *dm; + GdaDataModelIter *iter; + GList *ret; + + ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable); + + ret = NULL; + + sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT); + + gdaex_sql_builder_from (sqlb, "bookmarks", NULL); + + gdaex_sql_builder_order (sqlb, "name", NULL); + + dm = gdaex_sql_builder_query (sqlb, priv->gdaex, NULL); + if (!gdaex_data_model_is_empty (dm)) + { + iter = gda_data_model_create_iter (dm); + while (gda_data_model_iter_next (dm, iter)) + { + ZakBksBookmark *bk; + bk = g_new0 (ZakBksBookmark, 1); + bk->name = gdaex_data_model_iter_get_field_value_stringify_at (iter, "name"); + bk->url = gdaex_data_model_iter_get_field_value_stringify_at (iter, "url"); + + ret = g_list_append (ret, bk); + } + } + + return ret; } static void zak_bks_db_plugin_add_bookmark (ZakBksPluggable *pluggable, ZakBksBookmark *bk) { + GdaExSqlBuilder *sqlb; + GdaDataModel *dm; + GValue *gval; + + ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable); + + sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_SELECT); + gdaex_sql_builder_from (sqlb, "bookmarks", NULL); + + gval = g_new0 (GValue, 1); + g_value_init (gval, G_TYPE_STRING); + g_value_set_string (gval, bk->url); + gdaex_sql_builder_where (sqlb, 0, + "bookmarks", "url", "", + GDA_SQL_OPERATOR_TYPE_EQ, + gval, + NULL); + g_value_unset (gval); + + dm = gdaex_sql_builder_query (sqlb, priv->gdaex, NULL); + if (!gdaex_data_model_is_empty (dm)) + { + sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_INSERT); + + gval = g_new0 (GValue, 1); + g_value_init (gval, G_TYPE_STRING); + g_value_set_string (gval, bk->name); + gdaex_sql_builder_field (sqlb, + "bookmarks", "name", NULL, + gval); + g_value_unset (gval); + + gval = g_new0 (GValue, 1); + g_value_init (gval, G_TYPE_STRING); + g_value_set_string (gval, bk->url); + gdaex_sql_builder_field (sqlb, + "bookmarks", "url", NULL, + gval); + g_value_unset (gval); + + gdaex_sql_builder_execute (sqlb, priv->gdaex, NULL); + } + else + { + sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_UPDATE); + + gval = g_new0 (GValue, 1); + g_value_init (gval, G_TYPE_STRING); + g_value_set_string (gval, bk->name); + gdaex_sql_builder_field (sqlb, + "bookmarks", "name", NULL, + gval); + g_value_unset (gval); + + gval = g_new0 (GValue, 1); + g_value_init (gval, G_TYPE_STRING); + g_value_set_string (gval, bk->url); + gdaex_sql_builder_where (sqlb, 0, + "bookmarks", "url", "", + GDA_SQL_OPERATOR_TYPE_EQ, + gval, + NULL); + g_value_unset (gval); + + gdaex_sql_builder_execute (sqlb, priv->gdaex, NULL); + } } static void zak_bks_db_plugin_remove_bookmark (ZakBksPluggable *pluggable, ZakBksBookmark *bk) { + GdaExSqlBuilder *sqlb; + GValue *gval; + + ZakBksDBPluginPrivate *priv = ZAK_BKS_DB_PLUGIN_GET_PRIVATE (pluggable); + + sqlb = gdaex_sql_builder_new (GDA_SQL_STATEMENT_DELETE); + + gdaex_sql_builder_from (sqlb, "bookmarks", NULL); + + gval = g_new0 (GValue, 1); + g_value_init (gval, G_TYPE_STRING); + g_value_set_string (gval, bk->url); + gdaex_sql_builder_where (sqlb, 0, + "bookmarks", "url", "", + GDA_SQL_OPERATOR_TYPE_EQ, + gval, + NULL); + g_value_unset (gval); + gdaex_sql_builder_execute (sqlb, priv->gdaex, NULL); } static void