]> saetta.ns0.it Git - zakbookmarks/libzakbookmarks/commitdiff
Implemented Db plugin interface functions (without test).
authorAndrea Zagli <azagli@libero.it>
Sun, 21 Jul 2019 08:06:55 +0000 (10:06 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 21 Jul 2019 08:06:55 +0000 (10:06 +0200)
plugins/db/plgdb.c

index 6d2c332e98da095b4d53f66f95745350c3cceb6b..6d5ab500c7a41ff704e7b648ec3b94d818890da0 100644 (file)
@@ -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