25
25
#include <unistd.h>
29
midori_bookmarks_insert_item_db (sqlite3* db,
34
midori_bookmarks_update_item_db (sqlite3* db,
38
midori_bookmarks_insert_item_db (sqlite3* db,
44
KatzeItem* old_parent;
47
const gchar* uri = NULL;
48
const gchar* desc = NULL;
51
/* Bookmarks must have a name, import may produce invalid items */
52
g_return_val_if_fail (katze_item_get_name (item), seq);
57
if (katze_item_get_meta_integer (item, "id") > 0)
58
id = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer(item, "id"));
60
id = g_strdup_printf ("NULL");
62
if (KATZE_ITEM_IS_BOOKMARK (item))
63
uri = katze_item_get_uri (item);
65
if (katze_item_get_text (item))
66
desc = katze_item_get_text (item);
68
/* Use folder, otherwise fallback to parent folder */
69
old_parent = katze_item_get_parent (item);
71
new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, parentid);
72
else if (old_parent && katze_item_get_meta_integer (old_parent, "id") > 0)
73
new_parentid = g_strdup_printf ("%" G_GINT64_FORMAT, katze_item_get_meta_integer (old_parent, "id"));
75
new_parentid = g_strdup_printf ("NULL");
77
sqlcmd = sqlite3_mprintf (
78
"INSERT INTO bookmarks (id, parentid, title, uri, desc, toolbar, app) "
79
"VALUES (%q, %q, '%q', '%q', '%q', %d, %d)",
82
katze_item_get_name (item),
83
katze_str_non_null (uri),
84
katze_str_non_null (desc),
85
katze_item_get_meta_boolean (item, "toolbar"),
86
katze_item_get_meta_boolean (item, "app"));
88
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) == SQLITE_OK)
91
if (g_str_equal (id, "NULL"))
93
KatzeArray* seq_array;
95
sqlite3_free (sqlcmd);
96
sqlcmd = sqlite3_mprintf (
97
"SELECT seq FROM sqlite_sequence WHERE name = 'bookmarks'");
99
seq_array = katze_array_from_sqlite (db, sqlcmd);
100
if (katze_array_get_nth_item (seq_array, 0))
102
KatzeItem* seq_item = katze_array_get_nth_item (seq_array, 0);
104
seq = katze_item_get_meta_integer (seq_item, "seq");
105
katze_item_set_meta_integer (item, "id", seq);
107
g_object_unref (seq_array);
112
g_printerr (_("Failed to add bookmark item: %s\n"), errmsg);
113
sqlite3_free (errmsg);
116
sqlite3_free (sqlcmd);
117
g_free (new_parentid);
124
midori_bookmarks_update_item_db (sqlite3* db,
133
id = g_strdup_printf ("%" G_GINT64_FORMAT,
134
katze_item_get_meta_integer (item, "id"));
136
if (katze_item_get_meta_integer (item, "parentid") > 0)
137
parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
138
katze_item_get_meta_integer (item, "parentid"));
140
parentid = g_strdup_printf ("NULL");
142
sqlcmd = sqlite3_mprintf (
143
"UPDATE bookmarks SET "
144
"parentid=%q, title='%q', uri='%q', desc='%q', toolbar=%d, app=%d "
147
katze_item_get_name (item),
148
katze_str_non_null (katze_item_get_uri (item)),
149
katze_str_non_null (katze_item_get_meta_string (item, "desc")),
150
katze_item_get_meta_boolean (item, "toolbar"),
151
katze_item_get_meta_boolean (item, "app"),
155
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
158
g_printerr (_("Failed to update bookmark: %s\n"), errmsg);
159
sqlite3_free (errmsg);
162
sqlite3_free (sqlcmd);
170
* midori_bookmarks_db_update_item:
171
* @bookmarks: the main bookmark array
172
* @item: #KatzeItem the item to update
174
* Updates the @item in the bookmark data base.
179
midori_array_update_item (KatzeArray* bookmarks,
182
g_return_if_fail (KATZE_IS_ARRAY (bookmarks));
183
g_return_if_fail (KATZE_IS_ITEM (item));
184
g_return_if_fail (katze_item_get_meta_string (item, "id"));
185
g_return_if_fail (0 != katze_item_get_meta_integer (item, "id"));
187
sqlite3* db = g_object_get_data (G_OBJECT (bookmarks), "db");
189
g_return_if_fail (db);
191
midori_bookmarks_update_item_db (db, item);
29
195
midori_bookmarks_dbtracer (void* dummy,
30
196
const char* query)
319
485
sqlite3_close (db);
489
midori_bookmarks_import_array (KatzeArray* bookmarks,
499
KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
501
katze_item_set_meta_integer (item, "parentid", parentid);
502
katze_array_add_item (bookmarks, item);
503
if (KATZE_IS_ARRAY (item))
504
midori_bookmarks_import_array (bookmarks, KATZE_ARRAY (item),
505
katze_item_get_meta_integer(item, "id"));
511
* midori_array_query_recursive:
512
* @array: the main bookmark array
513
* @fields: comma separated list of fields
514
* @condition: condition, like "folder = '%q'"
515
* @value: a value to be inserted if @condition contains %q
516
* @recursive: if %TRUE include children
518
* Stores the result in a #KatzeArray.
520
* Return value: a #KatzeArray on success, %NULL otherwise
525
midori_array_query_recursive (KatzeArray* bookmarks,
527
const gchar* condition,
538
g_return_val_if_fail (KATZE_IS_ARRAY (bookmarks), NULL);
539
g_return_val_if_fail (fields, NULL);
540
g_return_val_if_fail (condition, NULL);
541
db = g_object_get_data (G_OBJECT (bookmarks), "db");
542
g_return_val_if_fail (db != NULL, NULL);
544
sqlcmd = g_strdup_printf ("SELECT %s FROM bookmarks WHERE %s "
545
"ORDER BY (uri='') ASC, title DESC", fields, condition);
546
if (strstr (condition, "%q"))
548
sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
549
array = katze_array_from_sqlite (db, sqlcmd_value);
550
sqlite3_free (sqlcmd_value);
553
array = katze_array_from_sqlite (db, sqlcmd);
559
KATZE_ARRAY_FOREACH_ITEM_L (item, array, list)
561
if (KATZE_ITEM_IS_FOLDER (item))
563
gchar* parentid = g_strdup_printf ("%" G_GINT64_FORMAT,
564
katze_item_get_meta_integer (item, "id"));
565
KatzeArray* subarray = midori_array_query_recursive (bookmarks,
566
fields, "parentid=%q", parentid, TRUE);
570
KATZE_ARRAY_FOREACH_ITEM_L (subitem, subarray, sublist)
572
katze_array_add_item (KATZE_ARRAY (item), subitem);
575
g_object_unref (subarray);
584
count_from_sqlite (sqlite3* db,
591
result = sqlite3_prepare_v2 (db, sqlcmd, -1, &stmt, NULL);
592
if (result != SQLITE_OK)
595
g_assert (sqlite3_column_count (stmt) == 1);
597
if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
598
count = sqlite3_column_int64(stmt, 0);
600
sqlite3_clear_bindings (stmt);
601
sqlite3_reset (stmt);
607
midori_array_count_recursive_by_id (KatzeArray* bookmarks,
608
const gchar* condition,
622
g_return_val_if_fail (condition, -1);
623
g_return_val_if_fail (KATZE_IS_ARRAY (bookmarks), -1);
624
db = g_object_get_data (G_OBJECT (bookmarks), "db");
625
g_return_val_if_fail (db != NULL, -1);
627
g_assert(!strstr("parentid", condition));
630
sqlcmd = g_strdup_printf ("SELECT COUNT(*) FROM bookmarks "
631
"WHERE parentid = %" G_GINT64_FORMAT " AND %s",
635
sqlcmd = g_strdup_printf ("SELECT COUNT(*) FROM bookmarks "
636
"WHERE parentid IS NULL AND %s ",
639
if (strstr (condition, "%q"))
641
sqlcmd_value = sqlite3_mprintf (sqlcmd, value ? value : "");
642
count = count_from_sqlite (db, sqlcmd_value);
643
sqlite3_free (sqlcmd_value);
646
count = count_from_sqlite (db, sqlcmd);
650
if (!recursive || (count < 0))
656
sqlcmd_value = sqlite3_mprintf (
657
"SELECT id FROM bookmarks "
658
"WHERE parentid = %" G_GINT64_FORMAT " AND uri = ''", id);
660
sqlcmd_value = sqlite3_mprintf (
661
"SELECT id FROM bookmarks "
662
"WHERE parentid IS NULL AND uri = ''");
664
if (sqlite3_prepare_v2 (db, sqlcmd_value, -1, &stmt, NULL) == SQLITE_OK)
666
g_assert (sqlite3_column_count (stmt) == 1);
668
if ((result = sqlite3_step (stmt)) == SQLITE_ROW)
670
gint64* pid = g_new (gint64, 1);
672
*pid = sqlite3_column_int64(stmt, 0);
673
ids = g_list_append (ids, pid);
676
sqlite3_clear_bindings (stmt);
677
sqlite3_reset (stmt);
680
sqlite3_free (sqlcmd_value);
685
gint64 sub_count = midori_array_count_recursive_by_id (bookmarks,
688
*(gint64*)(iter_ids->data),
693
g_list_free_full (ids, g_free);
698
iter_ids = g_list_next (iter_ids);
701
g_list_free_full (ids, g_free);
706
* midori_array_count_recursive:
707
* @array: the main bookmark array
708
* @condition: condition, like "folder = '%q'"
709
* @value: a value to be inserted if @condition contains %q
710
* @recursive: if %TRUE include children
712
* Return value: the number of elements on success, -1 otherwise
717
midori_array_count_recursive (KatzeArray* bookmarks,
718
const gchar* condition,
725
g_return_val_if_fail (!folder || KATZE_ITEM_IS_FOLDER (folder), -1);
727
id = folder ? katze_item_get_meta_integer (folder, "id") : 0;
729
return midori_array_count_recursive_by_id (bookmarks, condition,