Fix build error in sticker-receiver
[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 = ?)) ORDER BY sticker_info_id DESC LIMIT ?, ?"
94 #define STICKER_DB_GET_RECORD_ID_BY_APP_ID "SELECT sticker_info_id from sticker_info WHERE app_id = ? ORDER BY sticker_info_id DESC 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 = ?)) ORDER BY sticker_info_id DESC 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 = ?)) ORDER BY sticker_info_id DESC 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 = ?)) ORDER BY sticker_info_id DESC 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 = ?)) ORDER BY sticker_info_id DESC 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     if (is_corrupted && _recover_db() != SQLITE_OK)
322         return NULL;
323
324     ret = sqlite3_open(STICKER_DB_PATH, &db);
325     if (ret != SQLITE_OK) {
326         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
327         return NULL;
328     }
329
330     ret = sqlite3_exec(db, "PRAGMA foreign_keys = ON", NULL, NULL, &err);
331     if (ret != SQLITE_OK) {
332         LOGE("Failed to turn on foreign keys : %s", err);
333     }
334
335     if (err)
336         sqlite3_free(err);
337
338     return db;
339 }
340
341 int stickerd_db_insert_sticker_info(int *record_id, sticker_info_db *sticker_info)
342 {
343     int ret;
344     sqlite3 *db = NULL;
345     sqlite3_stmt *stmt = NULL;
346
347     db = _db_open();
348     if (!db)
349         return STICKERD_SERVER_ERROR_DB_FAILED;
350
351     ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_INFO, -1, &stmt, NULL);
352     if (ret != SQLITE_OK) {
353         LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
354         goto cleanup;
355     }
356
357     sqlite3_bind_text(stmt, 1, sticker_info->app_id, -1, SQLITE_TRANSIENT);
358     sqlite3_bind_int(stmt, 2, sticker_info->type);
359     sqlite3_bind_text(stmt, 3, sticker_info->uri, -1, SQLITE_TRANSIENT);
360     sqlite3_bind_text(stmt, 4, sticker_info->thumbnail, -1, SQLITE_TRANSIENT);
361     sqlite3_bind_text(stmt, 5, sticker_info->description, -1, SQLITE_TRANSIENT);
362     sqlite3_bind_text(stmt, 6, sticker_info->group, -1, SQLITE_TRANSIENT);
363     sqlite3_bind_int(stmt, 7, sticker_info->display_type);
364
365     ret = sqlite3_step(stmt);
366     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
367         LOGE("sqlite3_step() failed : ret(%d)", ret);
368         goto cleanup;
369     } else if (sqlite3_changes(db) == 0) {
370         LOGE("No changes to DB");
371         goto cleanup;
372     }
373
374     sqlite3_finalize(stmt);
375     stmt = NULL;
376
377     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_LATEST_RECORD_ID, -1, &stmt, NULL);
378     if (ret != SQLITE_OK) {
379         LOGE("fail to get sticker id : %s", sqlite3_errmsg(db));
380         goto cleanup;
381     }
382
383     ret = sqlite3_step(stmt);
384     if (ret == SQLITE_ERROR) {
385         LOGE("sqlite3_step() failed : ret(%d)", ret);
386         *record_id = 0;
387         goto cleanup;
388     } else {
389         *record_id = sqlite3_column_int(stmt, 0);
390         LOGD("record_id : %d", *record_id);
391     }
392
393     GList *list;
394     for(list = sticker_info->keyword; list != NULL; list=list->next) {
395         sqlite3_finalize(stmt);
396         stmt = NULL;
397
398         ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_KEYWORD_INFO, -1, &stmt, NULL);
399         if (ret != SQLITE_OK) {
400             LOGE("fail to insert sticker keyword : %s", sqlite3_errmsg(db));
401             goto cleanup;
402         }
403
404         sqlite3_bind_int(stmt, 1, *record_id);
405         sqlite3_bind_text(stmt, 2, (char *)list->data, -1, SQLITE_TRANSIENT);
406
407         ret = sqlite3_step(stmt);
408         if (ret != SQLITE_OK && ret != SQLITE_DONE) {
409             LOGE("sqlite3_step() failed : ret(%d)", ret);
410             goto cleanup;
411         } else if (sqlite3_changes(db) == 0) {
412             LOGE("No changes to DB");
413             goto cleanup;
414         }
415     }
416
417     sqlite3_finalize(stmt);
418     sqlite3_close(db);
419
420     return STICKERD_SERVER_ERROR_NONE;
421
422 cleanup:
423     sqlite3_finalize(stmt);
424     sqlite3_close(db);
425
426     return STICKERD_SERVER_ERROR_DB_FAILED;
427 }
428
429 int stickerd_db_delete_sticker_info(int record_id)
430 {
431     int ret;
432     sqlite3 *db = NULL;
433     sqlite3_stmt *stmt = NULL;
434
435     db = _db_open();
436     if (!db)
437         return STICKERD_SERVER_ERROR_DB_FAILED;
438
439     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID, -1, &stmt, NULL);
440     if (ret != SQLITE_OK) {
441         LOGE("fail to get image files : %s", sqlite3_errmsg(db));
442         goto cleanup;
443     }
444
445     sqlite3_bind_int(stmt, 1, record_id);
446
447     ret = sqlite3_step(stmt);
448     if (ret == SQLITE_ERROR) {
449         LOGE("sqlite3_step() failed : ret(%d)", ret);
450         goto cleanup;
451     }
452
453     int uri_type = sqlite3_column_int(stmt, 0);
454     const unsigned char *uri = sqlite3_column_text(stmt, 1);
455     const unsigned char *thumbnail = sqlite3_column_text(stmt, 2);
456
457     if (uri_type == 1 && unlink((const char *)uri) == -1)
458         LOGE("fail to delete sticker file");
459
460     if (thumbnail && unlink((const char *)thumbnail) == -1)
461         LOGE("fail to delete thumbnail image");
462
463     sqlite3_finalize(stmt);
464     stmt = NULL;
465
466     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO, -1, &stmt, NULL);
467     if (ret != SQLITE_OK) {
468         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
469         goto cleanup;
470     }
471
472     sqlite3_bind_int(stmt, 1, record_id);
473
474     ret = sqlite3_step(stmt);
475     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
476         LOGE("sqlite3_step() failed : ret(%d)", ret);
477         goto cleanup;
478     } else if (sqlite3_changes(db) == 0) {
479         LOGE("No changes to DB");
480         goto cleanup;
481     }
482
483     sqlite3_finalize(stmt);
484     sqlite3_close(db);
485
486     return STICKERD_SERVER_ERROR_NONE;
487
488 cleanup:
489     sqlite3_finalize(stmt);
490     sqlite3_close(db);
491
492     return STICKERD_SERVER_ERROR_DB_FAILED;
493 }
494
495 int stickerd_db_delete_sticker_info_by_uri(char *uri)
496 {
497     int ret;
498     sqlite3 *db = NULL;
499     sqlite3_stmt *stmt = NULL;
500
501     db = _db_open();
502     if (!db)
503         return STICKERD_SERVER_ERROR_DB_FAILED;
504
505     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_URI, -1, &stmt, NULL);
506     if (ret != SQLITE_OK) {
507         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
508         goto cleanup;
509     }
510
511     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
512
513     ret = sqlite3_step(stmt);
514     if (ret == SQLITE_ERROR) {
515         LOGE("sqlite3_step() failed : ret(%d)", ret);
516         goto cleanup;
517     }
518
519     int uri_type = sqlite3_column_int(stmt, 0);
520     const unsigned char *thumbnail = sqlite3_column_text(stmt, 1);
521
522     if (uri_type == 1 && unlink((const char *)uri) == -1)
523         LOGE("fail to delete sticker file");
524
525     if (thumbnail && unlink((const char *)thumbnail) == -1)
526         LOGE("fail to delete thumbnail image");
527
528     sqlite3_finalize(stmt);
529     stmt = NULL;
530
531     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO_BY_URI, -1, &stmt, NULL);
532     if (ret != SQLITE_OK) {
533         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
534         goto cleanup;
535     }
536
537     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
538
539     ret = sqlite3_step(stmt);
540     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
541         LOGE("sqlite3_step() failed : ret(%d)", ret);
542         goto cleanup;
543     } else if (sqlite3_changes(db) == 0) {
544         LOGE("No changes to DB");
545         goto cleanup;
546     }
547
548     sqlite3_finalize(stmt);
549     sqlite3_close(db);
550
551     return STICKERD_SERVER_ERROR_NONE;
552
553 cleanup:
554     sqlite3_finalize(stmt);
555     sqlite3_close(db);
556
557     return STICKERD_SERVER_ERROR_DB_FAILED;
558 }
559
560 int stickerd_db_update_sticker_info(int record_id, sticker_info_db_type type, void *data)
561 {
562     int ret;
563     sqlite3 *db = NULL;
564     sqlite3_stmt *stmt = NULL;
565
566     db = _db_open();
567     if (!db)
568         return STICKERD_SERVER_ERROR_DB_FAILED;
569
570     const char* query = _db_get_query(type, CMD_UPDATE);
571     ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
572     if (ret != SQLITE_OK) {
573         LOGE("fail to update sticker information : %s", sqlite3_errmsg(db));
574         goto cleanup;
575     }
576
577     if (type == STICKER_DB_STICKER_TYPE || type == STICKER_DB_STICKER_DISP_TYPE) {
578         sqlite3_bind_int(stmt, 1, *(int *)data);
579     } else if (type == STICKER_DB_STICKER_KEYWORD) {
580         sqlite3_bind_int(stmt, 1, record_id);
581     } else {
582         sqlite3_bind_text(stmt, 1, (char *)data, -1, SQLITE_TRANSIENT);
583     }
584
585     if (type != STICKER_DB_STICKER_KEYWORD)
586         sqlite3_bind_int(stmt, 2, record_id);
587
588     ret = sqlite3_step(stmt);
589     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
590         LOGE("sqlite3_step() failed : ret(%d)", ret);
591         goto cleanup;
592     }
593
594     if (type == STICKER_DB_STICKER_KEYWORD) {
595         GList *list;
596         for(list = (GList *)data; list != NULL; list=list->next) {
597             sqlite3_finalize(stmt);
598             stmt = NULL;
599
600             ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_KEYWORD_INFO, -1, &stmt, NULL);
601             if (ret != SQLITE_OK) {
602                 LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
603                 goto cleanup;
604             }
605
606             sqlite3_bind_int(stmt, 1, record_id);
607             sqlite3_bind_text(stmt, 2, (char *)list->data, -1, SQLITE_TRANSIENT);
608
609             ret = sqlite3_step(stmt);
610             if (ret != SQLITE_OK && ret != SQLITE_DONE) {
611                 LOGE("sqlite3_step() failed : ret(%d)", ret);
612                 goto cleanup;
613             } else if (sqlite3_changes(db) == 0) {
614                 LOGE("No changes to DB");
615                 goto cleanup;
616             }
617         }
618     }
619
620     sqlite3_finalize(stmt);
621     sqlite3_close(db);
622
623     return STICKERD_SERVER_ERROR_NONE;
624
625 cleanup:
626     sqlite3_finalize(stmt);
627     sqlite3_close(db);
628
629     return STICKERD_SERVER_ERROR_DB_FAILED;
630 }
631
632 int stickerd_db_get_sticker_info_by_record_id(int record_id, sticker_info_db *sticker_info)
633 {
634     int ret;
635     sqlite3 *db = NULL;
636     sqlite3_stmt *stmt = NULL;
637
638     db = _db_open();
639     if (!db)
640         return STICKERD_SERVER_ERROR_DB_FAILED;
641
642     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_INFO_BY_RECORD_ID, -1, &stmt, NULL);
643     if (ret != SQLITE_OK) {
644         LOGE("fail to get sticker information : %s", sqlite3_errmsg(db));
645         goto cleanup;
646     }
647
648     sqlite3_bind_int(stmt, 1, record_id);
649
650     ret = sqlite3_step(stmt);
651     if (ret == SQLITE_ERROR) {
652         LOGE("sqlite3_step() failed : ret(%d)", ret);
653         goto cleanup;
654     }
655
656     const unsigned char *tmp_app_id = sqlite3_column_text(stmt, 1);
657     if (tmp_app_id) {
658         sticker_info->app_id = strdup((const char *)tmp_app_id);
659     } else {
660         LOGW("invalid record_id : %d", record_id);
661         goto cleanup;
662     }
663
664     sticker_info->type = sqlite3_column_int(stmt, 2);
665
666     const unsigned char *tmp_uri = sqlite3_column_text(stmt, 3);
667     if (tmp_uri)
668         sticker_info->uri = strdup((const char *)tmp_uri);
669
670     const unsigned char *tmp_thumbnail = sqlite3_column_text(stmt, 4);
671     if (tmp_thumbnail)
672         sticker_info->thumbnail = strdup((const char *)tmp_thumbnail);
673
674     const unsigned char *tmp_description = sqlite3_column_text(stmt, 5);
675     if (tmp_description)
676         sticker_info->description = strdup((const char *)tmp_description);
677
678     const unsigned char *tmp_group = sqlite3_column_text(stmt, 6);
679     if (tmp_group)
680         sticker_info->group = strdup((const char *)tmp_group);
681
682     const unsigned char *tmp_date = sqlite3_column_text(stmt, 7);
683     if (tmp_date)
684         sticker_info->date = strdup((const char *)tmp_date);
685
686     sticker_info->display_type = sqlite3_column_int(stmt, 8);
687
688     sqlite3_finalize(stmt);
689     stmt = NULL;
690
691     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID, -1, &stmt, NULL);
692     if (ret != SQLITE_OK) {
693         LOGE("fail to get sticker keyword : %s", sqlite3_errmsg(db));
694         goto cleanup;
695     }
696
697     sqlite3_bind_int(stmt, 1, record_id);
698
699     while (sqlite3_step(stmt) == SQLITE_ROW) {
700         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
701         if (keyword)
702             sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
703     }
704
705     sqlite3_finalize(stmt);
706     sqlite3_close(db);
707
708     return STICKERD_SERVER_ERROR_NONE;
709
710 cleanup:
711     sqlite3_finalize(stmt);
712     sqlite3_close(db);
713
714     return STICKERD_SERVER_ERROR_DB_FAILED;
715 }
716
717 int stickerd_db_get_group_list(GVariantBuilder *builder, char *app_id)
718 {
719     int ret;
720     sqlite3 *db = NULL;
721     sqlite3_stmt *stmt = NULL;
722
723     db = _db_open();
724     if (!db)
725         return STICKERD_SERVER_ERROR_DB_FAILED;
726
727     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_ALL_GROUP_LIST, -1, &stmt, NULL);
728     if (ret != SQLITE_OK) {
729         LOGE("fail to get group list : %s", sqlite3_errmsg(db));
730         goto cleanup;
731     }
732
733     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
734
735     while (sqlite3_step(stmt) == SQLITE_ROW) {
736         const unsigned char *group = sqlite3_column_text(stmt, 0);
737         if (group)
738             g_variant_builder_add(builder, "(s)", strdup((const char *)group));
739     }
740
741     sqlite3_finalize(stmt);
742     sqlite3_close(db);
743
744     return STICKERD_SERVER_ERROR_NONE;
745
746 cleanup:
747     sqlite3_finalize(stmt);
748     sqlite3_close(db);
749
750     return STICKERD_SERVER_ERROR_DB_FAILED;
751 }
752
753 int stickerd_db_get_keyword_list(GVariantBuilder *builder, char *app_id)
754 {
755     int ret;
756     sqlite3 *db = NULL;
757     sqlite3_stmt *stmt = NULL;
758
759     db = _db_open();
760     if (!db)
761         return STICKERD_SERVER_ERROR_DB_FAILED;
762
763     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_ALL_KEYWORD_LIST, -1, &stmt, NULL);
764     if (ret != SQLITE_OK) {
765         LOGE("fail to get keyword list : %s", sqlite3_errmsg(db));
766         goto cleanup;
767     }
768
769     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
770
771     while (sqlite3_step(stmt) == SQLITE_ROW) {
772         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
773         if (keyword)
774             g_variant_builder_add(builder, "(s)", strdup((const char *)keyword));
775     }
776
777     sqlite3_finalize(stmt);
778     sqlite3_close(db);
779
780     return STICKERD_SERVER_ERROR_NONE;
781
782 cleanup:
783     sqlite3_finalize(stmt);
784     sqlite3_close(db);
785
786     return STICKERD_SERVER_ERROR_DB_FAILED;
787 }
788
789 int stickerd_db_get_sticker_count(int *count, char *app_id)
790 {
791     int ret;
792     sqlite3 *db = NULL;
793     sqlite3_stmt *stmt = NULL;
794
795     db = _db_open();
796     if (!db)
797         return STICKERD_SERVER_ERROR_DB_FAILED;
798
799     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_COUNT, -1, &stmt, NULL);
800     if (ret != SQLITE_OK) {
801         LOGE("fail to get sticker count : %s", sqlite3_errmsg(db));
802         goto cleanup;
803     }
804
805     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
806
807     ret = sqlite3_step(stmt);
808     if (ret == SQLITE_ERROR) {
809         LOGE("sqlite3_step() failed : ret(%d)", ret);
810         goto cleanup;
811     }
812
813     *count = sqlite3_column_int(stmt, 0);
814
815     sqlite3_finalize(stmt);
816     sqlite3_close(db);
817
818     return STICKERD_SERVER_ERROR_NONE;
819
820 cleanup:
821     sqlite3_finalize(stmt);
822     sqlite3_close(db);
823
824     return STICKERD_SERVER_ERROR_DB_FAILED;
825 }
826
827 int stickerd_db_get_record_id(sticker_info_db_type type, GList **id_list, void *data, char *app_id, int offset, int count)
828 {
829     int ret;
830     sqlite3 *db = NULL;
831     sqlite3_stmt *stmt = NULL;
832
833     db = _db_open();
834     if (!db)
835         return STICKERD_SERVER_ERROR_DB_FAILED;
836
837     const char* query = _db_get_query(type, CMD_SELECT);
838     ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
839     if (ret != SQLITE_OK) {
840         LOGE("fail to get record id : %s", sqlite3_errmsg(db));
841         goto cleanup;
842     }
843
844     if (type == STICKER_DB_STICKER_ALL || type == STICKER_DB_STICKER_APPID) {
845         sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
846         sqlite3_bind_int(stmt, 2, offset);
847         sqlite3_bind_int(stmt, 3, count);
848     } else if (type == STICKER_DB_STICKER_RECENT_HISTORY) {
849         sqlite3_bind_int(stmt, 1, count);
850     } else {
851         if (type == STICKER_DB_STICKER_TYPE || type == STICKER_DB_STICKER_DISP_TYPE)
852             sqlite3_bind_int(stmt, 1, *(int *)data);
853         else
854             sqlite3_bind_text(stmt, 1, (char *)data, -1, SQLITE_TRANSIENT);
855
856         sqlite3_bind_text(stmt, 2, app_id, -1, SQLITE_TRANSIENT);
857         sqlite3_bind_int(stmt, 3, offset);
858         sqlite3_bind_int(stmt, 4, count);
859     }
860
861     while (sqlite3_step(stmt) == SQLITE_ROW) {
862         const unsigned char *tmp_id = sqlite3_column_text(stmt, 0);
863         if (tmp_id)
864             *id_list = g_list_append(*id_list, strdup((const char *)tmp_id));
865     }
866
867     sqlite3_finalize(stmt);
868     sqlite3_close(db);
869
870     return STICKERD_SERVER_ERROR_NONE;
871
872 cleanup:
873     sqlite3_finalize(stmt);
874     sqlite3_close(db);
875
876     return STICKERD_SERVER_ERROR_DB_FAILED;
877 }
878
879 int stickerd_db_get_group_list_by_display_type(GVariantBuilder *builder, char *app_id, int disp_type)
880 {
881     int ret;
882     sqlite3 *db = NULL;
883     sqlite3_stmt *stmt = NULL;
884
885     db = _db_open();
886     if (!db)
887         return STICKERD_SERVER_ERROR_DB_FAILED;
888
889     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_GROUP_LIST_BY_DISP_TYPE, -1, &stmt, NULL);
890     if (ret != SQLITE_OK) {
891         LOGE("fail to get group list : %s", sqlite3_errmsg(db));
892         goto cleanup;
893     }
894
895     sqlite3_bind_int(stmt, 1, disp_type);
896     sqlite3_bind_text(stmt, 2, app_id, -1, SQLITE_TRANSIENT);
897
898     while (sqlite3_step(stmt) == SQLITE_ROW) {
899         const unsigned char *group = sqlite3_column_text(stmt, 0);
900         if (group)
901             g_variant_builder_add(builder, "(s)", strdup((const char *)group));
902     }
903
904     sqlite3_finalize(stmt);
905     sqlite3_close(db);
906
907     return STICKERD_SERVER_ERROR_NONE;
908
909 cleanup:
910     sqlite3_finalize(stmt);
911     sqlite3_close(db);
912
913     return STICKERD_SERVER_ERROR_DB_FAILED;
914 }
915
916 int stickerd_db_check_file_exists(int *result, char *uri)
917 {
918     int ret;
919     sqlite3 *db = NULL;
920     sqlite3_stmt *stmt = NULL;
921
922     db = _db_open();
923     if (!db)
924         return STICKERD_SERVER_ERROR_DB_FAILED;
925
926     ret = sqlite3_prepare_v2(db, STICKER_DB_CHECK_FILE_EXISTS, -1, &stmt, NULL);
927     if (ret != SQLITE_OK) {
928         LOGE("fail to check file exists : %s", sqlite3_errmsg(db));
929         goto cleanup;
930     }
931
932     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
933
934     ret = sqlite3_step(stmt);
935     if (ret == SQLITE_ERROR) {
936         LOGE("sqlite3_step() failed : ret(%d)", ret);
937         goto cleanup;
938     }
939
940     *result = sqlite3_column_int(stmt, 0);
941
942     sqlite3_finalize(stmt);
943     sqlite3_close(db);
944
945     return STICKERD_SERVER_ERROR_NONE;
946
947 cleanup:
948     sqlite3_finalize(stmt);
949     sqlite3_close(db);
950
951     return STICKERD_SERVER_ERROR_DB_FAILED;
952 }
953
954 int stickerd_db_insert_recent_sticker_info(int record_id)
955 {
956     int ret;
957     sqlite3 *db = NULL;
958     sqlite3_stmt *stmt = NULL;
959
960     db = _db_open();
961     if (!db)
962         return STICKERD_SERVER_ERROR_DB_FAILED;
963
964     ret = sqlite3_prepare_v2(db, STICKER_DB_CHECK_RECENT_HISTORY_EXISTS, -1, &stmt, NULL);
965     if (ret != SQLITE_OK) {
966         LOGE("fail to check recent sticker exists : %s", sqlite3_errmsg(db));
967         goto cleanup;
968     }
969
970     sqlite3_bind_int(stmt, 1, record_id);
971
972     ret = sqlite3_step(stmt);
973     if (ret == SQLITE_ERROR) {
974         LOGE("sqlite3_step() failed : ret(%d)", ret);
975         goto cleanup;
976     }
977
978     int result = sqlite3_column_int(stmt, 0);
979
980     sqlite3_finalize(stmt);
981     stmt = NULL;
982
983     if (result)
984         ret = sqlite3_prepare_v2(db, STICKER_DB_UPDATE_RECENT_HISTORY, -1, &stmt, NULL);
985     else
986         ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_RECENT_HISTORY, -1, &stmt, NULL);
987
988     if (ret != SQLITE_OK) {
989         LOGE("fail to update recent history : %s", sqlite3_errmsg(db));
990         goto cleanup;
991     }
992
993     sqlite3_bind_int(stmt, 1, record_id);
994
995     ret = sqlite3_step(stmt);
996     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
997         LOGE("sqlite3_step() failed : ret(%d)", ret);
998         goto cleanup;
999     } else if (sqlite3_changes(db) == 0) {
1000         LOGE("No changes to DB");
1001         goto cleanup;
1002     }
1003
1004     sqlite3_finalize(stmt);
1005     sqlite3_close(db);
1006
1007     return STICKERD_SERVER_ERROR_NONE;
1008
1009 cleanup:
1010     sqlite3_finalize(stmt);
1011     sqlite3_close(db);
1012
1013     return STICKERD_SERVER_ERROR_DB_FAILED;
1014 }
1015
1016 int stickerd_db_get_sticker_info_by_uri(char *uri, sticker_info_db *sticker_info)
1017 {
1018     int ret;
1019     sqlite3 *db = NULL;
1020     sqlite3_stmt *stmt = NULL;
1021
1022     db = _db_open();
1023     if (!db)
1024         return STICKERD_SERVER_ERROR_DB_FAILED;
1025
1026     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_INFO_BY_URI, -1, &stmt, NULL);
1027     if (ret != SQLITE_OK) {
1028         LOGE("fail to get sticker information : %s", sqlite3_errmsg(db));
1029         goto cleanup;
1030     }
1031
1032     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
1033
1034     ret = sqlite3_step(stmt);
1035     if (ret == SQLITE_ERROR) {
1036         LOGE("sqlite3_step() failed : ret(%d)", ret);
1037         goto cleanup;
1038     }
1039
1040     sticker_info->record_id = sqlite3_column_int(stmt, 0);
1041
1042     const unsigned char *tmp_app_id = sqlite3_column_text(stmt, 1);
1043     if (tmp_app_id)
1044         sticker_info->app_id = strdup((const char *)tmp_app_id);
1045
1046     sticker_info->type = sqlite3_column_int(stmt, 2);
1047
1048     const unsigned char *tmp_uri = sqlite3_column_text(stmt, 3);
1049     if (tmp_uri)
1050         sticker_info->uri = strdup((const char *)tmp_uri);
1051
1052     const unsigned char *tmp_thumbnail = sqlite3_column_text(stmt, 4);
1053     if (tmp_thumbnail)
1054         sticker_info->thumbnail = strdup((const char *)tmp_thumbnail);
1055
1056     const unsigned char *tmp_description = sqlite3_column_text(stmt, 5);
1057     if (tmp_description)
1058         sticker_info->description = strdup((const char *)tmp_description);
1059
1060     const unsigned char *tmp_group = sqlite3_column_text(stmt, 6);
1061     if (tmp_group)
1062         sticker_info->group = strdup((const char *)tmp_group);
1063
1064     const unsigned char *tmp_date = sqlite3_column_text(stmt, 7);
1065     if (tmp_date)
1066         sticker_info->date = strdup((const char *)tmp_date);
1067
1068     sticker_info->display_type = sqlite3_column_int(stmt, 8);
1069
1070     sqlite3_finalize(stmt);
1071     stmt = NULL;
1072
1073     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID, -1, &stmt, NULL);
1074     if (ret != SQLITE_OK) {
1075         LOGE("fail to get sticker keyword : %s", sqlite3_errmsg(db));
1076         goto cleanup;
1077     }
1078
1079     sqlite3_bind_int(stmt, 1, sticker_info->record_id);
1080
1081     while (sqlite3_step(stmt) == SQLITE_ROW) {
1082         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
1083         if (keyword)
1084             sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
1085     }
1086
1087     sqlite3_finalize(stmt);
1088     sqlite3_close(db);
1089
1090     return STICKERD_SERVER_ERROR_NONE;
1091
1092 cleanup:
1093     sqlite3_finalize(stmt);
1094     sqlite3_close(db);
1095
1096     return STICKERD_SERVER_ERROR_DB_FAILED;
1097 }