Fix issue detected by static analysis tool
[platform/core/uifw/capi-ui-sticker.git] / server / stickerd_db_manager.c
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <unistd.h>
18 #include <string.h>
19 #include <dlog.h>
20 #include <sqlite3.h>
21
22 #include "stickerd_db_manager.h"
23 #include "stickerd_error.h"
24
25 #ifdef LOG_TAG
26 #undef LOG_TAG
27 #endif
28 #define LOG_TAG "STICKERD_DB_MANAGER"
29
30 /*
31  * Table Information
32  *
33  * sticker_info
34  * +-----------------+--------+------+------+-----------+-------------+------------+------+--------------+
35  * | INT             | TEXT   | INT  | TEXT | TEXT      | TEXT        | TEXT       | TEXT | INT          |
36  * +-----------------+--------+------+------+-----------+-------------+------------+------+--------------+
37  * | sticker_info_id | app_id | type | uri  | thumbnail | description | group_name | date | display_type |
38  * +-----------------+--------+------+------+-----------+-------------+------------+------+--------------+
39  *
40  * sticker_keyword_info
41  * +------------+-----------------+---------+
42  * | INT        | INT             | TEXT    |
43  * +------------+-----------------+---------+
44  * | keyword_id | sticker_info_id | keyword |
45  * +------------+-----------------+---------+
46  *
47  * sticker_whitelist_info
48  * +--------------+-------------+-------------+
49  * | INT          | TEXT        | TEXT        |
50  * +--------------+-------------+-------------+
51  * | whitelist_id | provider_id | consumer_id |
52  * +------------+---------------+-------------+
53  *
54  * sticker_recent_history_info
55  * +------------+-----------------+-------+-----------+
56  * | INT        | INT             | INT   | TEXT      |
57  * +------------+-----------------+-------+-----------+
58  * | history_id | sticker_info_id | count | timestamp |
59  * +------------+-----------------+-------+-----------+
60  *
61  */
62
63 #define STICKER_DB_PATH tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db")
64 #define STICKER_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_info(sticker_info_id INTEGER PRIMARY KEY AUTOINCREMENT, app_id TEXT NOT NULL, type INTEGER NOT NULL, uri TEXT NOT NULL, thumbnail TEXT, description TEXT, group_name TEXT NOT NULL, date TEXT NOT NULL, display_type INTEGER)"
65 #define STICKER_KEYWORD_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_keyword_info(keyword_id INTEGER PRIMARY KEY AUTOINCREMENT, sticker_info_id INTEGER, keyword TEXT NOT NULL, FOREIGN KEY (sticker_info_id) REFERENCES sticker_info(sticker_info_id) ON DELETE CASCADE)"
66 #define STICKER_WHITELIST_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_whitelist_info(whitelist_id INTEGER PRIMARY KEY AUTOINCREMENT, provider_id TEXT NOT NULL, consumer_id TEXT NOT NULL)"
67 #define STICKER_RECENT_HISTORY_INFO_CREATE_TABLE "CREATE TABLE IF NOT EXISTS sticker_recent_history_info(history_id INTEGER PRIMARY KEY AUTOINCREMENT, sticker_info_id INTEGER, count INTEGER NOT NULL, timestamp TEXT NOT NULL, FOREIGN KEY (sticker_info_id) REFERENCES sticker_info(sticker_info_id) ON DELETE CASCADE)"
68
69 #define STICKER_DB_INSERT_STICKER_INFO "INSERT INTO sticker_info (app_id, type, uri, thumbnail, description, group_name, date, display_type) VALUES (?, ?, ?, ?, ?, ?, DateTime('now','localtime'), ?)"
70 #define STICKER_DB_INSERT_STICKER_KEYWORD_INFO "INSERT INTO sticker_keyword_info (sticker_info_id, keyword) VALUES (?, ?)"
71 #define STICKER_DB_INSERT_RECENT_HISTORY "INSERT INTO sticker_recent_history_info (sticker_info_id, count, timestamp) VALUES (?, 1, DateTime('now','localtime'))"
72
73 #define STICKER_DB_DELETE_STICKER_INFO "DELETE FROM sticker_info WHERE sticker_info_id = ?"
74 #define STICKER_DB_DELETE_STICKER_KEYWORD_INFO "DELETE FROM sticker_keyword_info WHERE sticker_info_id = ?"
75 #define STICKER_DB_DELETE_STICKER_INFO_BY_URI "DELETE FROM sticker_info WHERE uri = ?"
76
77 #define STICKER_DB_UPDATE_STICKER_TYPE "UPDATE sticker_info SET type = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
78 #define STICKER_DB_UPDATE_STICKER_URI "UPDATE sticker_info SET uri = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
79 #define STICKER_DB_UPDATE_STICKER_THUMBNAIL "UPDATE sticker_info SET thumbnail = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
80 #define STICKER_DB_UPDATE_STICKER_DESCRIPTION "UPDATE sticker_info SET description = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
81 #define STICKER_DB_UPDATE_STICKER_GROUP "UPDATE sticker_info SET group_name = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
82 #define STICKER_DB_UPDATE_STICKER_DISP_TYPE "UPDATE sticker_info SET display_type = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
83 #define STICKER_DB_UPDATE_RECENT_HISTORY "UPDATE sticker_recent_history_info SET count = count + 1, timestamp = DateTime('now','localtime') WHERE sticker_info_id = ?"
84
85 #define STICKER_DB_GET_LATEST_RECORD_ID "SELECT sticker_info_id FROM sticker_info ORDER BY sticker_info_id DESC LIMIT 1"
86 #define STICKER_DB_GET_STICKER_INFO_BY_RECORD_ID "SELECT * FROM sticker_info WHERE sticker_info_id = ?"
87 #define STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID "SELECT keyword FROM sticker_keyword_info WHERE sticker_info_id = ?"
88 #define STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID "SELECT type, uri, thumbnail FROM sticker_info WHERE sticker_info_id = ?"
89 #define STICKER_DB_GET_IMAGE_INFO_BY_URI "SELECT type, thumbnail FROM sticker_info WHERE uri = ?"
90 #define STICKER_DB_GET_ALL_GROUP_LIST "SELECT DISTINCT group_name FROM sticker_info WHERE app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?))"
91 #define STICKER_DB_GET_ALL_KEYWORD_LIST "SELECT DISTINCT keyword FROM sticker_keyword_info WHERE sticker_info_id IN (SELECT sticker_info_id from sticker_info WHERE app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)))"
92 #define STICKER_DB_GET_STICKER_COUNT "SELECT count(*) FROM sticker_info WHERE app_id = ?"
93 #define STICKER_DB_GET_ALL_RECORD_ID "SELECT sticker_info_id FROM sticker_info WHERE app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)) LIMIT ?, ?"
94 #define STICKER_DB_GET_RECORD_ID_BY_APP_ID "SELECT sticker_info_id from sticker_info WHERE app_id = ? LIMIT ?, ?"
95 #define STICKER_DB_GET_RECORD_ID_BY_TYPE "SELECT sticker_info_id FROM sticker_info WHERE type = ? AND app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)) LIMIT ?, ?"
96 #define STICKER_DB_GET_RECORD_ID_BY_GROUP "SELECT sticker_info_id from sticker_info WHERE group_name = ? AND app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)) LIMIT ?, ?"
97 #define STICKER_DB_GET_RECORD_ID_BY_KEYWORD "SELECT sticker_info_id FROM sticker_keyword_info WHERE keyword = ? INTERSECT SELECT sticker_info_id from sticker_info WHERE app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)) LIMIT ?, ?"
98 #define STICKER_DB_GET_RECORD_ID_BY_DISP_TYPE "SELECT sticker_info_id FROM sticker_info WHERE display_type = ? AND app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?)) LIMIT ?, ?"
99 #define STICKER_DB_GET_GROUP_LIST_BY_DISP_TYPE "SELECT DISTINCT group_name FROM sticker_info WHERE display_type = ? AND app_id NOT IN (SELECT DISTINCT provider_id FROM sticker_whitelist_info WHERE provider_id NOT IN (SELECT provider_id FROM sticker_whitelist_info WHERE consumer_id = ?))"
100 #define STICKER_DB_CHECK_FILE_EXISTS "SELECT EXISTS(SELECT 1 FROM sticker_info WHERE uri = ? LIMIT 1)"
101 #define STICKER_DB_CHECK_RECENT_HISTORY_EXISTS "SELECT EXISTS(SELECT 1 FROM sticker_recent_history_info WHERE sticker_info_id = ? LIMIT 1)"
102 #define STICKER_DB_GET_RECENT_HISTORY "SELECT sticker_info_id FROM sticker_recent_history_info ORDER BY datetime(timestamp) DESC LIMIT ?"
103 #define STICKER_DB_GET_STICKER_INFO_BY_URI "SELECT * FROM sticker_info WHERE uri = ?"
104
105 typedef enum
106 {
107     CMD_UPDATE,
108     CMD_SELECT,
109 } command_type;
110
111 static gboolean is_corrupted = FALSE;
112
113 static const char *_db_get_query(sticker_info_db_type sticker_type, command_type cmd_type)
114 {
115     static const char* query = NULL;
116
117     if (cmd_type == CMD_UPDATE) {
118         switch(sticker_type) {
119             case STICKER_DB_STICKER_TYPE:
120             query = STICKER_DB_UPDATE_STICKER_TYPE;
121             break;
122             case STICKER_DB_STICKER_URI:
123             query = STICKER_DB_UPDATE_STICKER_URI;
124             break;
125             case STICKER_DB_STICKER_THUMBNAIL:
126             query = STICKER_DB_UPDATE_STICKER_THUMBNAIL;
127             break;
128             case STICKER_DB_STICKER_DESCRIPTION:
129             query = STICKER_DB_UPDATE_STICKER_DESCRIPTION;
130             break;
131             case STICKER_DB_STICKER_GROUP:
132             query = STICKER_DB_UPDATE_STICKER_GROUP;
133             break;
134             case STICKER_DB_STICKER_KEYWORD:
135             query = STICKER_DB_DELETE_STICKER_KEYWORD_INFO;
136             break;
137             case STICKER_DB_STICKER_DISP_TYPE:
138             query = STICKER_DB_UPDATE_STICKER_DISP_TYPE;
139             break;
140             default :
141             query = "";
142             break;
143         }
144     } else if (cmd_type == CMD_SELECT) {
145         switch(sticker_type) {
146             case STICKER_DB_STICKER_ALL:
147             query = STICKER_DB_GET_ALL_RECORD_ID;
148             break;
149             case STICKER_DB_STICKER_APPID:
150             query = STICKER_DB_GET_RECORD_ID_BY_APP_ID;
151             break;
152             case STICKER_DB_STICKER_TYPE:
153             query = STICKER_DB_GET_RECORD_ID_BY_TYPE;
154             break;
155             case STICKER_DB_STICKER_GROUP:
156             query = STICKER_DB_GET_RECORD_ID_BY_GROUP;
157             break;
158             case STICKER_DB_STICKER_KEYWORD:
159             query = STICKER_DB_GET_RECORD_ID_BY_KEYWORD;
160             break;
161             case STICKER_DB_STICKER_DISP_TYPE:
162             query = STICKER_DB_GET_RECORD_ID_BY_DISP_TYPE;
163             break;
164             case STICKER_DB_STICKER_RECENT_HISTORY:
165             query = STICKER_DB_GET_RECENT_HISTORY;
166             break;
167             default :
168             query = "";
169             break;
170         }
171     }
172
173     return query;
174 }
175
176 static int _recover_db(void)
177 {
178     int ret = STICKERD_SERVER_ERROR_NONE;
179     sqlite3 *db = NULL;
180     char *err = NULL;
181
182     LOGD("Start to recover sticker db");
183     //Remove sticker database file
184     if (unlink(STICKER_DB_PATH) == -1)
185         LOGE("Failed to remove db file");
186
187     ret = sqlite3_open_v2(STICKER_DB_PATH, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
188     if (ret != SQLITE_OK) {
189         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
190         if (unlink(STICKER_DB_PATH) == -1)
191             LOGE("Failed to remove db file");
192         ret = STICKERD_SERVER_ERROR_DB_FAILED;
193         goto cleanup;
194     }
195
196     ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
197     if (ret != SQLITE_OK) {
198         LOGE("Failed to create sticker_info table : %s" , err);
199         ret = STICKERD_SERVER_ERROR_DB_FAILED;
200         goto cleanup;
201     }
202
203     ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
204     if (ret != SQLITE_OK) {
205         LOGE("Failed to create sticker_keyword_info table : %s", err);
206         ret = STICKERD_SERVER_ERROR_DB_FAILED;
207         goto cleanup;
208     }
209
210     ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err);
211     if (ret != SQLITE_OK) {
212         LOGE("Failed to create sticker_whitelist_info table : %s", err);
213         ret = STICKERD_SERVER_ERROR_DB_FAILED;
214         goto cleanup;
215     }
216
217     ret = sqlite3_exec(db, STICKER_RECENT_HISTORY_INFO_CREATE_TABLE, NULL, NULL, &err);
218     if (ret != SQLITE_OK) {
219         LOGE("Failed to create sticker_recent_history_info table : %s", err);
220         ret = STICKERD_SERVER_ERROR_DB_FAILED;
221         goto cleanup;
222     }
223
224     is_corrupted = FALSE;
225
226 cleanup:
227     if (err)
228         sqlite3_free(err);
229
230     if (db)
231         sqlite3_close(db);
232
233     return ret;
234 }
235
236 static int _integrity_check_cb(void *pid, int argc, char **argv, char **notUsed)
237 {
238     if (strcmp(argv[0], "ok") != 0) {
239         LOGE("DB integrity check failed : %s", argv[0]);
240         is_corrupted = TRUE;
241         return -1;
242     }
243
244     LOGD("Result integrity : %s", argv[0]);
245     return 0;
246 }
247
248 int stickerd_db_init(void)
249 {
250     int ret = STICKERD_SERVER_ERROR_NONE;
251     sqlite3 *db = NULL;
252     char *err = NULL;
253
254     ret = sqlite3_open_v2(STICKER_DB_PATH, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
255     if (ret != SQLITE_OK) {
256         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
257         ret = STICKERD_SERVER_ERROR_DB_FAILED;
258         goto cleanup;
259     }
260
261     ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
262     if (ret != SQLITE_OK) {
263         LOGE("Failed to create sticker_info table : %s" , err);
264         ret = STICKERD_SERVER_ERROR_DB_FAILED;
265         goto cleanup;
266     }
267
268     ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
269     if (ret != SQLITE_OK) {
270         LOGE("Failed to create sticker_keyword_info table : %s", err);
271         ret = STICKERD_SERVER_ERROR_DB_FAILED;
272         goto cleanup;
273     }
274
275     ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err);
276     if (ret != SQLITE_OK) {
277         LOGE("Failed to create sticker_whitelist_info table : %s", err);
278         ret = STICKERD_SERVER_ERROR_DB_FAILED;
279         goto cleanup;
280     }
281
282     ret = sqlite3_exec(db, STICKER_RECENT_HISTORY_INFO_CREATE_TABLE, NULL, NULL, &err);
283     if (ret != SQLITE_OK) {
284         LOGE("Failed to create sticker_recent_history_info table : %s", err);
285         ret = STICKERD_SERVER_ERROR_DB_FAILED;
286         goto cleanup;
287     }
288
289     ret = sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, &err);
290     if (ret != SQLITE_OK) {
291         LOGE("Failed to set journal_mode : %s", err);
292         ret = STICKERD_SERVER_ERROR_DB_FAILED;
293         goto cleanup;
294     }
295
296     ret = sqlite3_exec(db, "PRAGMA integrity_check", _integrity_check_cb, NULL, &err);
297     if (ret != SQLITE_OK) {
298         LOGE("Failed to check integrity : %s", err);
299         ret = STICKERD_SERVER_ERROR_DB_FAILED;
300     }
301
302 cleanup:
303     if (err)
304         sqlite3_free(err);
305
306     if (db)
307         sqlite3_close(db);
308
309     if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB || is_corrupted)
310         ret = _recover_db();
311
312     return ret;
313 }
314
315 static sqlite3 *_db_open(void)
316 {
317     int ret;
318     sqlite3 *db = NULL;
319     char *err = NULL;
320
321     ret = sqlite3_open(STICKER_DB_PATH, &db);
322     if (ret != SQLITE_OK) {
323         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
324         return NULL;
325     }
326
327     ret = sqlite3_exec(db, "PRAGMA foreign_keys = ON", NULL, NULL, &err);
328     if (ret != SQLITE_OK) {
329         LOGE("Failed to turn on foreign keys : %s", err);
330     }
331
332     if (err)
333         sqlite3_free(err);
334
335     return db;
336 }
337
338 int stickerd_db_insert_sticker_info(int *record_id, sticker_info_db *sticker_info)
339 {
340     int ret;
341     sqlite3 *db = NULL;
342     sqlite3_stmt *stmt = NULL;
343
344     db = _db_open();
345     if (!db)
346         return STICKERD_SERVER_ERROR_DB_FAILED;
347
348     ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_INFO, -1, &stmt, NULL);
349     if (ret != SQLITE_OK) {
350         LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
351         goto cleanup;
352     }
353
354     sqlite3_bind_text(stmt, 1, sticker_info->app_id, -1, SQLITE_TRANSIENT);
355     sqlite3_bind_int(stmt, 2, sticker_info->type);
356     sqlite3_bind_text(stmt, 3, sticker_info->uri, -1, SQLITE_TRANSIENT);
357     sqlite3_bind_text(stmt, 4, sticker_info->thumbnail, -1, SQLITE_TRANSIENT);
358     sqlite3_bind_text(stmt, 5, sticker_info->description, -1, SQLITE_TRANSIENT);
359     sqlite3_bind_text(stmt, 6, sticker_info->group, -1, SQLITE_TRANSIENT);
360     sqlite3_bind_int(stmt, 7, sticker_info->display_type);
361
362     ret = sqlite3_step(stmt);
363     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
364         LOGE("sqlite3_step() failed : ret(%d)", ret);
365         goto cleanup;
366     } else if (sqlite3_changes(db) == 0) {
367         LOGE("No changes to DB");
368         goto cleanup;
369     }
370
371     sqlite3_finalize(stmt);
372     stmt = NULL;
373
374     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_LATEST_RECORD_ID, -1, &stmt, NULL);
375     if (ret != SQLITE_OK) {
376         LOGE("fail to get sticker id : %s", sqlite3_errmsg(db));
377         goto cleanup;
378     }
379
380     ret = sqlite3_step(stmt);
381     if (ret == SQLITE_ERROR) {
382         LOGE("sqlite3_step() failed : ret(%d)", ret);
383         *record_id = 0;
384         goto cleanup;
385     } else {
386         *record_id = sqlite3_column_int(stmt, 0);
387         LOGD("record_id : %d", *record_id);
388     }
389
390     GList *list;
391     for(list = sticker_info->keyword; list != NULL; list=list->next) {
392         sqlite3_finalize(stmt);
393         stmt = NULL;
394
395         ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_KEYWORD_INFO, -1, &stmt, NULL);
396         if (ret != SQLITE_OK) {
397             LOGE("fail to insert sticker keyword : %s", sqlite3_errmsg(db));
398             goto cleanup;
399         }
400
401         sqlite3_bind_int(stmt, 1, *record_id);
402         sqlite3_bind_text(stmt, 2, (char *)list->data, -1, SQLITE_TRANSIENT);
403
404         ret = sqlite3_step(stmt);
405         if (ret != SQLITE_OK && ret != SQLITE_DONE) {
406             LOGE("sqlite3_step() failed : ret(%d)", ret);
407             goto cleanup;
408         } else if (sqlite3_changes(db) == 0) {
409             LOGE("No changes to DB");
410             goto cleanup;
411         }
412     }
413
414     sqlite3_finalize(stmt);
415     sqlite3_close(db);
416
417     return STICKERD_SERVER_ERROR_NONE;
418
419 cleanup:
420     sqlite3_finalize(stmt);
421     sqlite3_close(db);
422
423     return STICKERD_SERVER_ERROR_DB_FAILED;
424 }
425
426 int stickerd_db_delete_sticker_info(int record_id)
427 {
428     int ret;
429     sqlite3 *db = NULL;
430     sqlite3_stmt *stmt = NULL;
431
432     db = _db_open();
433     if (!db)
434         return STICKERD_SERVER_ERROR_DB_FAILED;
435
436     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID, -1, &stmt, NULL);
437     if (ret != SQLITE_OK) {
438         LOGE("fail to get image files : %s", sqlite3_errmsg(db));
439         goto cleanup;
440     }
441
442     sqlite3_bind_int(stmt, 1, record_id);
443
444     ret = sqlite3_step(stmt);
445     if (ret == SQLITE_ERROR) {
446         LOGE("sqlite3_step() failed : ret(%d)", ret);
447         goto cleanup;
448     }
449
450     int uri_type = sqlite3_column_int(stmt, 0);
451     const unsigned char *uri = sqlite3_column_text(stmt, 1);
452     const unsigned char *thumbnail = sqlite3_column_text(stmt, 2);
453
454     if (uri_type == 1 && unlink((const char *)uri) == -1)
455         LOGE("fail to delete sticker file");
456
457     if (thumbnail && unlink((const char *)thumbnail) == -1)
458         LOGE("fail to delete thumbnail image");
459
460     sqlite3_finalize(stmt);
461     stmt = NULL;
462
463     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO, -1, &stmt, NULL);
464     if (ret != SQLITE_OK) {
465         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
466         goto cleanup;
467     }
468
469     sqlite3_bind_int(stmt, 1, record_id);
470
471     ret = sqlite3_step(stmt);
472     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
473         LOGE("sqlite3_step() failed : ret(%d)", ret);
474         goto cleanup;
475     } else if (sqlite3_changes(db) == 0) {
476         LOGE("No changes to DB");
477         goto cleanup;
478     }
479
480     sqlite3_finalize(stmt);
481     sqlite3_close(db);
482
483     return STICKERD_SERVER_ERROR_NONE;
484
485 cleanup:
486     sqlite3_finalize(stmt);
487     sqlite3_close(db);
488
489     return STICKERD_SERVER_ERROR_DB_FAILED;
490 }
491
492 int stickerd_db_delete_sticker_info_by_uri(char *uri)
493 {
494     int ret;
495     sqlite3 *db = NULL;
496     sqlite3_stmt *stmt = NULL;
497
498     db = _db_open();
499     if (!db)
500         return STICKERD_SERVER_ERROR_DB_FAILED;
501
502     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_URI, -1, &stmt, NULL);
503     if (ret != SQLITE_OK) {
504         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
505         goto cleanup;
506     }
507
508     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
509
510     ret = sqlite3_step(stmt);
511     if (ret == SQLITE_ERROR) {
512         LOGE("sqlite3_step() failed : ret(%d)", ret);
513         goto cleanup;
514     }
515
516     int uri_type = sqlite3_column_int(stmt, 0);
517     const unsigned char *thumbnail = sqlite3_column_text(stmt, 1);
518
519     if (uri_type == 1 && unlink((const char *)uri) == -1)
520         LOGE("fail to delete sticker file");
521
522     if (thumbnail && unlink((const char *)thumbnail) == -1)
523         LOGE("fail to delete thumbnail image");
524
525     sqlite3_finalize(stmt);
526     stmt = NULL;
527
528     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO_BY_URI, -1, &stmt, NULL);
529     if (ret != SQLITE_OK) {
530         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
531         goto cleanup;
532     }
533
534     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
535
536     ret = sqlite3_step(stmt);
537     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
538         LOGE("sqlite3_step() failed : ret(%d)", ret);
539         goto cleanup;
540     } else if (sqlite3_changes(db) == 0) {
541         LOGE("No changes to DB");
542         goto cleanup;
543     }
544
545     sqlite3_finalize(stmt);
546     sqlite3_close(db);
547
548     return STICKERD_SERVER_ERROR_NONE;
549
550 cleanup:
551     sqlite3_finalize(stmt);
552     sqlite3_close(db);
553
554     return STICKERD_SERVER_ERROR_DB_FAILED;
555 }
556
557 int stickerd_db_update_sticker_info(int record_id, sticker_info_db_type type, void *data)
558 {
559     int ret;
560     sqlite3 *db = NULL;
561     sqlite3_stmt *stmt = NULL;
562
563     db = _db_open();
564     if (!db)
565         return STICKERD_SERVER_ERROR_DB_FAILED;
566
567     const char* query = _db_get_query(type, CMD_UPDATE);
568     ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
569     if (ret != SQLITE_OK) {
570         LOGE("fail to update sticker information : %s", sqlite3_errmsg(db));
571         goto cleanup;
572     }
573
574     if (type == STICKER_DB_STICKER_TYPE || type == STICKER_DB_STICKER_DISP_TYPE) {
575         sqlite3_bind_int(stmt, 1, *(int *)data);
576     } else if (type == STICKER_DB_STICKER_KEYWORD) {
577         sqlite3_bind_int(stmt, 1, record_id);
578     } else {
579         sqlite3_bind_text(stmt, 1, (char *)data, -1, SQLITE_TRANSIENT);
580     }
581
582     if (type != STICKER_DB_STICKER_KEYWORD)
583         sqlite3_bind_int(stmt, 2, record_id);
584
585     ret = sqlite3_step(stmt);
586     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
587         LOGE("sqlite3_step() failed : ret(%d)", ret);
588         goto cleanup;
589     }
590
591     if (type == STICKER_DB_STICKER_KEYWORD) {
592         GList *list;
593         for(list = (GList *)data; list != NULL; list=list->next) {
594             sqlite3_finalize(stmt);
595             stmt = NULL;
596
597             ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_KEYWORD_INFO, -1, &stmt, NULL);
598             if (ret != SQLITE_OK) {
599                 LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
600                 goto cleanup;
601             }
602
603             sqlite3_bind_int(stmt, 1, record_id);
604             sqlite3_bind_text(stmt, 2, (char *)list->data, -1, SQLITE_TRANSIENT);
605
606             ret = sqlite3_step(stmt);
607             if (ret != SQLITE_OK && ret != SQLITE_DONE) {
608                 LOGE("sqlite3_step() failed : ret(%d)", ret);
609                 goto cleanup;
610             } else if (sqlite3_changes(db) == 0) {
611                 LOGE("No changes to DB");
612                 goto cleanup;
613             }
614         }
615     }
616
617     sqlite3_finalize(stmt);
618     sqlite3_close(db);
619
620     return STICKERD_SERVER_ERROR_NONE;
621
622 cleanup:
623     sqlite3_finalize(stmt);
624     sqlite3_close(db);
625
626     return STICKERD_SERVER_ERROR_DB_FAILED;
627 }
628
629 int stickerd_db_get_sticker_info_by_record_id(int record_id, sticker_info_db *sticker_info)
630 {
631     int ret;
632     sqlite3 *db = NULL;
633     sqlite3_stmt *stmt = NULL;
634
635     db = _db_open();
636     if (!db)
637         return STICKERD_SERVER_ERROR_DB_FAILED;
638
639     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_INFO_BY_RECORD_ID, -1, &stmt, NULL);
640     if (ret != SQLITE_OK) {
641         LOGE("fail to get sticker information : %s", sqlite3_errmsg(db));
642         goto cleanup;
643     }
644
645     sqlite3_bind_int(stmt, 1, record_id);
646
647     ret = sqlite3_step(stmt);
648     if (ret == SQLITE_ERROR) {
649         LOGE("sqlite3_step() failed : ret(%d)", ret);
650         goto cleanup;
651     }
652
653     const unsigned char *tmp_app_id = sqlite3_column_text(stmt, 1);
654     if (tmp_app_id) {
655         sticker_info->app_id = strdup((const char *)tmp_app_id);
656     } else {
657         LOGW("invalid record_id : %d", record_id);
658         goto cleanup;
659     }
660
661     sticker_info->type = sqlite3_column_int(stmt, 2);
662
663     const unsigned char *tmp_uri = sqlite3_column_text(stmt, 3);
664     if (tmp_uri)
665         sticker_info->uri = strdup((const char *)tmp_uri);
666
667     const unsigned char *tmp_thumbnail = sqlite3_column_text(stmt, 4);
668     if (tmp_thumbnail)
669         sticker_info->thumbnail = strdup((const char *)tmp_thumbnail);
670
671     const unsigned char *tmp_description = sqlite3_column_text(stmt, 5);
672     if (tmp_description)
673         sticker_info->description = strdup((const char *)tmp_description);
674
675     const unsigned char *tmp_group = sqlite3_column_text(stmt, 6);
676     if (tmp_group)
677         sticker_info->group = strdup((const char *)tmp_group);
678
679     const unsigned char *tmp_date = sqlite3_column_text(stmt, 7);
680     if (tmp_date)
681         sticker_info->date = strdup((const char *)tmp_date);
682
683     sticker_info->display_type = sqlite3_column_int(stmt, 8);
684
685     sqlite3_finalize(stmt);
686     stmt = NULL;
687
688     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID, -1, &stmt, NULL);
689     if (ret != SQLITE_OK) {
690         LOGE("fail to get sticker keyword : %s", sqlite3_errmsg(db));
691         goto cleanup;
692     }
693
694     sqlite3_bind_int(stmt, 1, record_id);
695
696     while (sqlite3_step(stmt) == SQLITE_ROW) {
697         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
698         if (keyword)
699             sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
700     }
701
702     sqlite3_finalize(stmt);
703     sqlite3_close(db);
704
705     return STICKERD_SERVER_ERROR_NONE;
706
707 cleanup:
708     sqlite3_finalize(stmt);
709     sqlite3_close(db);
710
711     return STICKERD_SERVER_ERROR_DB_FAILED;
712 }
713
714 int stickerd_db_get_group_list(GVariantBuilder *builder, char *app_id)
715 {
716     int ret;
717     sqlite3 *db = NULL;
718     sqlite3_stmt *stmt = NULL;
719
720     db = _db_open();
721     if (!db)
722         return STICKERD_SERVER_ERROR_DB_FAILED;
723
724     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_ALL_GROUP_LIST, -1, &stmt, NULL);
725     if (ret != SQLITE_OK) {
726         LOGE("fail to get group list : %s", sqlite3_errmsg(db));
727         goto cleanup;
728     }
729
730     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
731
732     while (sqlite3_step(stmt) == SQLITE_ROW) {
733         const unsigned char *group = sqlite3_column_text(stmt, 0);
734         if (group)
735             g_variant_builder_add(builder, "(s)", strdup((const char *)group));
736     }
737
738     sqlite3_finalize(stmt);
739     sqlite3_close(db);
740
741     return STICKERD_SERVER_ERROR_NONE;
742
743 cleanup:
744     sqlite3_finalize(stmt);
745     sqlite3_close(db);
746
747     return STICKERD_SERVER_ERROR_DB_FAILED;
748 }
749
750 int stickerd_db_get_keyword_list(GVariantBuilder *builder, char *app_id)
751 {
752     int ret;
753     sqlite3 *db = NULL;
754     sqlite3_stmt *stmt = NULL;
755
756     db = _db_open();
757     if (!db)
758         return STICKERD_SERVER_ERROR_DB_FAILED;
759
760     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_ALL_KEYWORD_LIST, -1, &stmt, NULL);
761     if (ret != SQLITE_OK) {
762         LOGE("fail to get keyword list : %s", sqlite3_errmsg(db));
763         goto cleanup;
764     }
765
766     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
767
768     while (sqlite3_step(stmt) == SQLITE_ROW) {
769         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
770         if (keyword)
771             g_variant_builder_add(builder, "(s)", strdup((const char *)keyword));
772     }
773
774     sqlite3_finalize(stmt);
775     sqlite3_close(db);
776
777     return STICKERD_SERVER_ERROR_NONE;
778
779 cleanup:
780     sqlite3_finalize(stmt);
781     sqlite3_close(db);
782
783     return STICKERD_SERVER_ERROR_DB_FAILED;
784 }
785
786 int stickerd_db_get_sticker_count(int *count, char *app_id)
787 {
788     int ret;
789     sqlite3 *db = NULL;
790     sqlite3_stmt *stmt = NULL;
791
792     db = _db_open();
793     if (!db)
794         return STICKERD_SERVER_ERROR_DB_FAILED;
795
796     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_COUNT, -1, &stmt, NULL);
797     if (ret != SQLITE_OK) {
798         LOGE("fail to get sticker count : %s", sqlite3_errmsg(db));
799         goto cleanup;
800     }
801
802     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
803
804     ret = sqlite3_step(stmt);
805     if (ret == SQLITE_ERROR) {
806         LOGE("sqlite3_step() failed : ret(%d)", ret);
807         goto cleanup;
808     }
809
810     *count = sqlite3_column_int(stmt, 0);
811
812     sqlite3_finalize(stmt);
813     sqlite3_close(db);
814
815     return STICKERD_SERVER_ERROR_NONE;
816
817 cleanup:
818     sqlite3_finalize(stmt);
819     sqlite3_close(db);
820
821     return STICKERD_SERVER_ERROR_DB_FAILED;
822 }
823
824 int stickerd_db_get_record_id(sticker_info_db_type type, GList **id_list, void *data, char *app_id, int offset, int count)
825 {
826     int ret;
827     sqlite3 *db = NULL;
828     sqlite3_stmt *stmt = NULL;
829
830     db = _db_open();
831     if (!db)
832         return STICKERD_SERVER_ERROR_DB_FAILED;
833
834     const char* query = _db_get_query(type, CMD_SELECT);
835     ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
836     if (ret != SQLITE_OK) {
837         LOGE("fail to get record id : %s", sqlite3_errmsg(db));
838         goto cleanup;
839     }
840
841     if (type == STICKER_DB_STICKER_ALL || type == STICKER_DB_STICKER_APPID) {
842         sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
843         sqlite3_bind_int(stmt, 2, offset);
844         sqlite3_bind_int(stmt, 3, count);
845     } else if (type == STICKER_DB_STICKER_RECENT_HISTORY) {
846         sqlite3_bind_int(stmt, 1, count);
847     } else {
848         if (type == STICKER_DB_STICKER_TYPE || type == STICKER_DB_STICKER_DISP_TYPE)
849             sqlite3_bind_int(stmt, 1, *(int *)data);
850         else
851             sqlite3_bind_text(stmt, 1, (char *)data, -1, SQLITE_TRANSIENT);
852
853         sqlite3_bind_text(stmt, 2, app_id, -1, SQLITE_TRANSIENT);
854         sqlite3_bind_int(stmt, 3, offset);
855         sqlite3_bind_int(stmt, 4, count);
856     }
857
858     while (sqlite3_step(stmt) == SQLITE_ROW) {
859         const unsigned char *tmp_id = sqlite3_column_text(stmt, 0);
860         if (tmp_id)
861             *id_list = g_list_append(*id_list, strdup((const char *)tmp_id));
862     }
863
864     sqlite3_finalize(stmt);
865     sqlite3_close(db);
866
867     return STICKERD_SERVER_ERROR_NONE;
868
869 cleanup:
870     sqlite3_finalize(stmt);
871     sqlite3_close(db);
872
873     return STICKERD_SERVER_ERROR_DB_FAILED;
874 }
875
876 int stickerd_db_get_group_list_by_display_type(GVariantBuilder *builder, char *app_id, int disp_type)
877 {
878     int ret;
879     sqlite3 *db = NULL;
880     sqlite3_stmt *stmt = NULL;
881
882     db = _db_open();
883     if (!db)
884         return STICKERD_SERVER_ERROR_DB_FAILED;
885
886     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_GROUP_LIST_BY_DISP_TYPE, -1, &stmt, NULL);
887     if (ret != SQLITE_OK) {
888         LOGE("fail to get group list : %s", sqlite3_errmsg(db));
889         goto cleanup;
890     }
891
892     sqlite3_bind_int(stmt, 1, disp_type);
893     sqlite3_bind_text(stmt, 2, app_id, -1, SQLITE_TRANSIENT);
894
895     while (sqlite3_step(stmt) == SQLITE_ROW) {
896         const unsigned char *group = sqlite3_column_text(stmt, 0);
897         if (group)
898             g_variant_builder_add(builder, "(s)", strdup((const char *)group));
899     }
900
901     sqlite3_finalize(stmt);
902     sqlite3_close(db);
903
904     return STICKERD_SERVER_ERROR_NONE;
905
906 cleanup:
907     sqlite3_finalize(stmt);
908     sqlite3_close(db);
909
910     return STICKERD_SERVER_ERROR_DB_FAILED;
911 }
912
913 int stickerd_db_check_file_exists(int *result, char *uri)
914 {
915     int ret;
916     sqlite3 *db = NULL;
917     sqlite3_stmt *stmt = NULL;
918
919     db = _db_open();
920     if (!db)
921         return STICKERD_SERVER_ERROR_DB_FAILED;
922
923     ret = sqlite3_prepare_v2(db, STICKER_DB_CHECK_FILE_EXISTS, -1, &stmt, NULL);
924     if (ret != SQLITE_OK) {
925         LOGE("fail to check file exists : %s", sqlite3_errmsg(db));
926         goto cleanup;
927     }
928
929     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
930
931     ret = sqlite3_step(stmt);
932     if (ret == SQLITE_ERROR) {
933         LOGE("sqlite3_step() failed : ret(%d)", ret);
934         goto cleanup;
935     }
936
937     *result = sqlite3_column_int(stmt, 0);
938
939     sqlite3_finalize(stmt);
940     sqlite3_close(db);
941
942     return STICKERD_SERVER_ERROR_NONE;
943
944 cleanup:
945     sqlite3_finalize(stmt);
946     sqlite3_close(db);
947
948     return STICKERD_SERVER_ERROR_DB_FAILED;
949 }
950
951 int stickerd_db_insert_recent_sticker_info(int record_id)
952 {
953     int ret;
954     sqlite3 *db = NULL;
955     sqlite3_stmt *stmt = NULL;
956
957     db = _db_open();
958     if (!db)
959         return STICKERD_SERVER_ERROR_DB_FAILED;
960
961     ret = sqlite3_prepare_v2(db, STICKER_DB_CHECK_RECENT_HISTORY_EXISTS, -1, &stmt, NULL);
962     if (ret != SQLITE_OK) {
963         LOGE("fail to check recent sticker exists : %s", sqlite3_errmsg(db));
964         goto cleanup;
965     }
966
967     sqlite3_bind_int(stmt, 1, record_id);
968
969     ret = sqlite3_step(stmt);
970     if (ret == SQLITE_ERROR) {
971         LOGE("sqlite3_step() failed : ret(%d)", ret);
972         goto cleanup;
973     }
974
975     int result = sqlite3_column_int(stmt, 0);
976
977     sqlite3_finalize(stmt);
978     stmt = NULL;
979
980     if (result)
981         ret = sqlite3_prepare_v2(db, STICKER_DB_UPDATE_RECENT_HISTORY, -1, &stmt, NULL);
982     else
983         ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_RECENT_HISTORY, -1, &stmt, NULL);
984
985     if (ret != SQLITE_OK) {
986         LOGE("fail to update recent history : %s", sqlite3_errmsg(db));
987         goto cleanup;
988     }
989
990     sqlite3_bind_int(stmt, 1, record_id);
991
992     ret = sqlite3_step(stmt);
993     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
994         LOGE("sqlite3_step() failed : ret(%d)", ret);
995         goto cleanup;
996     } else if (sqlite3_changes(db) == 0) {
997         LOGE("No changes to DB");
998         goto cleanup;
999     }
1000
1001     sqlite3_finalize(stmt);
1002     sqlite3_close(db);
1003
1004     return STICKERD_SERVER_ERROR_NONE;
1005
1006 cleanup:
1007     sqlite3_finalize(stmt);
1008     sqlite3_close(db);
1009
1010     return STICKERD_SERVER_ERROR_DB_FAILED;
1011 }
1012
1013 int stickerd_db_get_sticker_info_by_uri(char *uri, sticker_info_db *sticker_info)
1014 {
1015     int ret;
1016     int record_id;
1017     sqlite3 *db = NULL;
1018     sqlite3_stmt *stmt = NULL;
1019
1020     db = _db_open();
1021     if (!db)
1022         return STICKERD_SERVER_ERROR_DB_FAILED;
1023
1024     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_INFO_BY_URI, -1, &stmt, NULL);
1025     if (ret != SQLITE_OK) {
1026         LOGE("fail to get sticker information : %s", sqlite3_errmsg(db));
1027         goto cleanup;
1028     }
1029
1030     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
1031
1032     ret = sqlite3_step(stmt);
1033     if (ret == SQLITE_ERROR) {
1034         LOGE("sqlite3_step() failed : ret(%d)", ret);
1035         goto cleanup;
1036     }
1037
1038     record_id = sticker_info->display_type = sqlite3_column_int(stmt, 0);
1039
1040     const unsigned char *tmp_app_id = sqlite3_column_text(stmt, 1);
1041     if (tmp_app_id)
1042         sticker_info->app_id = strdup((const char *)tmp_app_id);
1043
1044     sticker_info->type = sqlite3_column_int(stmt, 2);
1045
1046     const unsigned char *tmp_uri = sqlite3_column_text(stmt, 3);
1047     if (tmp_uri)
1048         sticker_info->uri = strdup((const char *)tmp_uri);
1049
1050     const unsigned char *tmp_thumbnail = sqlite3_column_text(stmt, 4);
1051     if (tmp_thumbnail)
1052         sticker_info->thumbnail = strdup((const char *)tmp_thumbnail);
1053
1054     const unsigned char *tmp_description = sqlite3_column_text(stmt, 5);
1055     if (tmp_description)
1056         sticker_info->description = strdup((const char *)tmp_description);
1057
1058     const unsigned char *tmp_group = sqlite3_column_text(stmt, 6);
1059     if (tmp_group)
1060         sticker_info->group = strdup((const char *)tmp_group);
1061
1062     const unsigned char *tmp_date = sqlite3_column_text(stmt, 7);
1063     if (tmp_date)
1064         sticker_info->date = strdup((const char *)tmp_date);
1065
1066     sticker_info->display_type = sqlite3_column_int(stmt, 8);
1067
1068     sqlite3_finalize(stmt);
1069     stmt = NULL;
1070
1071     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID, -1, &stmt, NULL);
1072     if (ret != SQLITE_OK) {
1073         LOGE("fail to get sticker keyword : %s", sqlite3_errmsg(db));
1074         goto cleanup;
1075     }
1076
1077     sqlite3_bind_int(stmt, 1, record_id);
1078
1079     while (sqlite3_step(stmt) == SQLITE_ROW) {
1080         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
1081         if (keyword)
1082             sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
1083     }
1084
1085     sqlite3_finalize(stmt);
1086     sqlite3_close(db);
1087
1088     return STICKERD_SERVER_ERROR_NONE;
1089
1090 cleanup:
1091     sqlite3_finalize(stmt);
1092     sqlite3_close(db);
1093
1094     return STICKERD_SERVER_ERROR_DB_FAILED;
1095 }