Fix issues 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  */
55
56 #define STICKER_DB_PATH tzplatform_mkpath(TZ_SYS_DB, ".sticker_info.db")
57 #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)"
58 #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)"
59 #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)"
60
61 #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'), ?)"
62 #define STICKER_DB_INSERT_STICKER_KEYWORD_INFO "INSERT INTO sticker_keyword_info (sticker_info_id, keyword) VALUES (?, ?)"
63
64 #define STICKER_DB_DELETE_STICKER_INFO "DELETE FROM sticker_info WHERE sticker_info_id = ?"
65 #define STICKER_DB_DELETE_STICKER_KEYWORD_INFO "DELETE FROM sticker_keyword_info WHERE sticker_info_id = ?"
66 #define STICKER_DB_DELETE_STICKER_INFO_BY_URI "DELETE FROM sticker_info WHERE uri = ?"
67
68 #define STICKER_DB_UPDATE_STICKER_TYPE "UPDATE sticker_info SET type = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
69 #define STICKER_DB_UPDATE_STICKER_URI "UPDATE sticker_info SET uri = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
70 #define STICKER_DB_UPDATE_STICKER_THUMBNAIL "UPDATE sticker_info SET thumbnail = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
71 #define STICKER_DB_UPDATE_STICKER_DESCRIPTION "UPDATE sticker_info SET description = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
72 #define STICKER_DB_UPDATE_STICKER_GROUP "UPDATE sticker_info SET group_name = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
73 #define STICKER_DB_UPDATE_STICKER_DISP_TYPE "UPDATE sticker_info SET display_type = ?, date = DateTime('now','localtime') WHERE sticker_info_id = ?"
74
75 #define STICKER_DB_GET_LATEST_RECORD_ID "SELECT sticker_info_id FROM sticker_info ORDER BY sticker_info_id DESC LIMIT 1"
76 #define STICKER_DB_GET_STICKER_INFO_BY_RECORD_ID "SELECT * FROM sticker_info WHERE sticker_info_id = ?"
77 #define STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID "SELECT keyword FROM sticker_keyword_info WHERE sticker_info_id = ?"
78 #define STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID "SELECT type, uri, thumbnail FROM sticker_info WHERE sticker_info_id = ?"
79 #define STICKER_DB_GET_IMAGE_INFO_BY_URI "SELECT type, thumbnail FROM sticker_info WHERE uri = ?"
80 #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 = ?))"
81 #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 = ?)))"
82 #define STICKER_DB_GET_STICKER_COUNT "SELECT count(*) FROM sticker_info WHERE app_id = ?"
83 #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 ?, ?"
84 #define STICKER_DB_GET_RECORD_ID_BY_APP_ID "SELECT sticker_info_id from sticker_info WHERE app_id = ? LIMIT ?, ?"
85 #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 ?, ?"
86 #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 ?, ?"
87 #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 ?, ?"
88 #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 ?, ?"
89 #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 = ?))"
90 #define STICKER_DB_CHECK_FILE_EXISTS "SELECT EXISTS(SELECT 1 FROM sticker_info WHERE uri = ? LIMIT 1)"
91
92 typedef enum
93 {
94     CMD_UPDATE,
95     CMD_SELECT,
96 } command_type;
97
98 static gboolean is_corrupted = FALSE;
99
100 static const char *_db_get_query(sticker_info_db_type sticker_type, command_type cmd_type)
101 {
102     static const char* query = NULL;
103
104     if (cmd_type == CMD_UPDATE) {
105         switch(sticker_type) {
106             case STICKER_DB_STICKER_TYPE:
107             query = STICKER_DB_UPDATE_STICKER_TYPE;
108             break;
109             case STICKER_DB_STICKER_URI:
110             query = STICKER_DB_UPDATE_STICKER_URI;
111             break;
112             case STICKER_DB_STICKER_THUMBNAIL:
113             query = STICKER_DB_UPDATE_STICKER_THUMBNAIL;
114             break;
115             case STICKER_DB_STICKER_DESCRIPTION:
116             query = STICKER_DB_UPDATE_STICKER_DESCRIPTION;
117             break;
118             case STICKER_DB_STICKER_GROUP:
119             query = STICKER_DB_UPDATE_STICKER_GROUP;
120             break;
121             case STICKER_DB_STICKER_KEYWORD:
122             query = STICKER_DB_DELETE_STICKER_KEYWORD_INFO;
123             break;
124             case STICKER_DB_STICKER_DISP_TYPE:
125             query = STICKER_DB_UPDATE_STICKER_DISP_TYPE;
126             break;
127             default :
128             query = "";
129             break;
130         }
131     } else if (cmd_type == CMD_SELECT) {
132         switch(sticker_type) {
133             case STICKER_DB_STICKER_ALL:
134             query = STICKER_DB_GET_ALL_RECORD_ID;
135             break;
136             case STICKER_DB_STICKER_APPID:
137             query = STICKER_DB_GET_RECORD_ID_BY_APP_ID;
138             break;
139             case STICKER_DB_STICKER_TYPE:
140             query = STICKER_DB_GET_RECORD_ID_BY_TYPE;
141             break;
142             case STICKER_DB_STICKER_GROUP:
143             query = STICKER_DB_GET_RECORD_ID_BY_GROUP;
144             break;
145             case STICKER_DB_STICKER_KEYWORD:
146             query = STICKER_DB_GET_RECORD_ID_BY_KEYWORD;
147             break;
148             case STICKER_DB_STICKER_DISP_TYPE:
149             query = STICKER_DB_GET_RECORD_ID_BY_DISP_TYPE;
150             break;
151             default :
152             query = "";
153             break;
154         }
155     }
156
157     return query;
158 }
159
160 static int _recover_db(void)
161 {
162     int ret = STICKERD_SERVER_ERROR_NONE;
163     sqlite3 *db = NULL;
164     char *err = NULL;
165
166     LOGD("Start to recover sticker db");
167     //Remove sticker database file
168     if (unlink(STICKER_DB_PATH) == -1)
169         LOGE("Failed to remove db file");
170
171     ret = sqlite3_open_v2(STICKER_DB_PATH, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
172     if (ret != SQLITE_OK) {
173         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
174         if (unlink(STICKER_DB_PATH) == -1)
175             LOGE("Failed to remove db file");
176         ret = STICKERD_SERVER_ERROR_DB_FAILED;
177         goto cleanup;
178     }
179
180     ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
181     if (ret != SQLITE_OK) {
182         LOGE("Failed to create sticker_info table : %s" , err);
183         ret = STICKERD_SERVER_ERROR_DB_FAILED;
184         goto cleanup;
185     }
186
187     ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
188     if (ret != SQLITE_OK) {
189         LOGE("Failed to create sticker_keyword_info table : %s", err);
190         ret = STICKERD_SERVER_ERROR_DB_FAILED;
191         goto cleanup;
192     }
193
194     ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err);
195     if (ret != SQLITE_OK) {
196         LOGE("Failed to create sticker_whitelist_info table : %s", err);
197         ret = STICKERD_SERVER_ERROR_DB_FAILED;
198     }
199
200     is_corrupted = FALSE;
201
202 cleanup:
203     if (err)
204         sqlite3_free(err);
205
206     if (db)
207         sqlite3_close(db);
208
209     return ret;
210 }
211
212 static int _integrity_check_cb(void *pid, int argc, char **argv, char **notUsed)
213 {
214     if (strcmp(argv[0], "ok") != 0) {
215         LOGE("DB integrity check failed : %s", argv[0]);
216         is_corrupted = TRUE;
217         return -1;
218     }
219
220     LOGD("Result integrity : %s", argv[0]);
221     return 0;
222 }
223
224 int stickerd_db_init(void)
225 {
226     int ret = STICKERD_SERVER_ERROR_NONE;
227     sqlite3 *db = NULL;
228     char *err = NULL;
229
230     ret = sqlite3_open_v2(STICKER_DB_PATH, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
231     if (ret != SQLITE_OK) {
232         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
233         ret = STICKERD_SERVER_ERROR_DB_FAILED;
234         goto cleanup;
235     }
236
237     ret = sqlite3_exec(db, STICKER_INFO_CREATE_TABLE, NULL, NULL, &err);
238     if (ret != SQLITE_OK) {
239         LOGE("Failed to create sticker_info table : %s" , err);
240         ret = STICKERD_SERVER_ERROR_DB_FAILED;
241         goto cleanup;
242     }
243
244     ret = sqlite3_exec(db, STICKER_KEYWORD_INFO_CREATE_TABLE, NULL, NULL, &err);
245     if (ret != SQLITE_OK) {
246         LOGE("Failed to create sticker_keyword_info table : %s", err);
247         ret = STICKERD_SERVER_ERROR_DB_FAILED;
248         goto cleanup;
249     }
250
251     ret = sqlite3_exec(db, STICKER_WHITELIST_INFO_CREATE_TABLE, NULL, NULL, &err);
252     if (ret != SQLITE_OK) {
253         LOGE("Failed to create sticker_whitelist_info table : %s", err);
254         ret = STICKERD_SERVER_ERROR_DB_FAILED;
255         goto cleanup;
256     }
257
258     ret = sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, &err);
259     if (ret != SQLITE_OK) {
260         LOGE("Failed to set journal_mode : %s", err);
261         ret = STICKERD_SERVER_ERROR_DB_FAILED;
262         goto cleanup;
263     }
264
265     ret = sqlite3_exec(db, "PRAGMA integrity_check", _integrity_check_cb, NULL, &err);
266     if (ret != SQLITE_OK) {
267         LOGE("Failed to check integrity : %s", err);
268         ret = STICKERD_SERVER_ERROR_DB_FAILED;
269     }
270
271 cleanup:
272     if (err)
273         sqlite3_free(err);
274
275     if (db)
276         sqlite3_close(db);
277
278     if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB || is_corrupted)
279         ret = _recover_db();
280
281     return ret;
282 }
283
284 static sqlite3 *_db_open(void)
285 {
286     int ret;
287     sqlite3 *db = NULL;
288     char *err = NULL;
289
290     ret = sqlite3_open(STICKER_DB_PATH, &db);
291     if (ret != SQLITE_OK) {
292         LOGE("Failed to open db : %s", sqlite3_errmsg(db));
293         return NULL;
294     }
295
296     ret = sqlite3_exec(db, "PRAGMA foreign_keys = ON", NULL, NULL, &err);
297     if (ret != SQLITE_OK) {
298         LOGE("Failed to turn on foreign keys : %s", err);
299     }
300
301     if (err)
302         sqlite3_free(err);
303
304     return db;
305 }
306
307 int stickerd_db_insert_sticker_info(int *record_id, sticker_info_db *sticker_info)
308 {
309     int ret;
310     sqlite3 *db = NULL;
311     sqlite3_stmt *stmt = NULL;
312
313     db = _db_open();
314     if (!db)
315         return STICKERD_SERVER_ERROR_DB_FAILED;
316
317     ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_INFO, -1, &stmt, NULL);
318     if (ret != SQLITE_OK) {
319         LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
320         goto cleanup;
321     }
322
323     sqlite3_bind_text(stmt, 1, sticker_info->app_id, -1, SQLITE_TRANSIENT);
324     sqlite3_bind_int(stmt, 2, sticker_info->type);
325     sqlite3_bind_text(stmt, 3, sticker_info->uri, -1, SQLITE_TRANSIENT);
326     sqlite3_bind_text(stmt, 4, sticker_info->thumbnail, -1, SQLITE_TRANSIENT);
327     sqlite3_bind_text(stmt, 5, sticker_info->description, -1, SQLITE_TRANSIENT);
328     sqlite3_bind_text(stmt, 6, sticker_info->group, -1, SQLITE_TRANSIENT);
329     sqlite3_bind_int(stmt, 7, sticker_info->display_type);
330
331     ret = sqlite3_step(stmt);
332     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
333         LOGE("sqlite3_step() failed : ret(%d)", ret);
334         goto cleanup;
335     } else if (sqlite3_changes(db) == 0) {
336         LOGE("No changes to DB");
337         goto cleanup;
338     }
339
340     sqlite3_finalize(stmt);
341     stmt = NULL;
342
343     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_LATEST_RECORD_ID, -1, &stmt, NULL);
344     if (ret != SQLITE_OK) {
345         LOGE("fail to get sticker id : %s", sqlite3_errmsg(db));
346         goto cleanup;
347     }
348
349     ret = sqlite3_step(stmt);
350     if (ret == SQLITE_ERROR) {
351         LOGE("sqlite3_step() failed : ret(%d)", ret);
352         *record_id = 0;
353         goto cleanup;
354     } else {
355         *record_id = sqlite3_column_int(stmt, 0);
356         LOGD("record_id : %d", *record_id);
357     }
358
359     GList *list;
360     for(list = sticker_info->keyword; list != NULL; list=list->next) {
361         sqlite3_finalize(stmt);
362         stmt = NULL;
363
364         ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_KEYWORD_INFO, -1, &stmt, NULL);
365         if (ret != SQLITE_OK) {
366             LOGE("fail to insert sticker keyword : %s", sqlite3_errmsg(db));
367             goto cleanup;
368         }
369
370         sqlite3_bind_int(stmt, 1, *record_id);
371         sqlite3_bind_text(stmt, 2, (char *)list->data, -1, SQLITE_TRANSIENT);
372
373         ret = sqlite3_step(stmt);
374         if (ret != SQLITE_OK && ret != SQLITE_DONE) {
375             LOGE("sqlite3_step() failed : ret(%d)", ret);
376             goto cleanup;
377         } else if (sqlite3_changes(db) == 0) {
378             LOGE("No changes to DB");
379             goto cleanup;
380         }
381     }
382
383     sqlite3_finalize(stmt);
384     sqlite3_close(db);
385
386     return STICKERD_SERVER_ERROR_NONE;
387
388 cleanup:
389     sqlite3_finalize(stmt);
390     sqlite3_close(db);
391
392     return STICKERD_SERVER_ERROR_DB_FAILED;
393 }
394
395 int stickerd_db_delete_sticker_info(int record_id)
396 {
397     int ret;
398     sqlite3 *db = NULL;
399     sqlite3_stmt *stmt = NULL;
400
401     db = _db_open();
402     if (!db)
403         return STICKERD_SERVER_ERROR_DB_FAILED;
404
405     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_RECORED_ID, -1, &stmt, NULL);
406     if (ret != SQLITE_OK) {
407         LOGE("fail to get image files : %s", sqlite3_errmsg(db));
408         goto cleanup;
409     }
410
411     sqlite3_bind_int(stmt, 1, record_id);
412
413     ret = sqlite3_step(stmt);
414     if (ret == SQLITE_ERROR) {
415         LOGE("sqlite3_step() failed : ret(%d)", ret);
416         goto cleanup;
417     }
418
419     int uri_type = sqlite3_column_int(stmt, 0);
420     const unsigned char *uri = sqlite3_column_text(stmt, 1);
421     const unsigned char *thumbnail = sqlite3_column_text(stmt, 2);
422
423     if (uri_type == 1 && unlink((const char *)uri) == -1)
424         LOGE("fail to delete sticker file");
425
426     if (thumbnail && unlink((const char *)thumbnail) == -1)
427         LOGE("fail to delete thumbnail image");
428
429     sqlite3_finalize(stmt);
430     stmt = NULL;
431
432     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO, -1, &stmt, NULL);
433     if (ret != SQLITE_OK) {
434         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
435         goto cleanup;
436     }
437
438     sqlite3_bind_int(stmt, 1, record_id);
439
440     ret = sqlite3_step(stmt);
441     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
442         LOGE("sqlite3_step() failed : ret(%d)", ret);
443         goto cleanup;
444     } else if (sqlite3_changes(db) == 0) {
445         LOGE("No changes to DB");
446         goto cleanup;
447     }
448
449     sqlite3_finalize(stmt);
450     sqlite3_close(db);
451
452     return STICKERD_SERVER_ERROR_NONE;
453
454 cleanup:
455     sqlite3_finalize(stmt);
456     sqlite3_close(db);
457
458     return STICKERD_SERVER_ERROR_DB_FAILED;
459 }
460
461 int stickerd_db_delete_sticker_info_by_uri(char *uri)
462 {
463     int ret;
464     sqlite3 *db = NULL;
465     sqlite3_stmt *stmt = NULL;
466
467     db = _db_open();
468     if (!db)
469         return STICKERD_SERVER_ERROR_DB_FAILED;
470
471     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_IMAGE_INFO_BY_URI, -1, &stmt, NULL);
472     if (ret != SQLITE_OK) {
473         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
474         goto cleanup;
475     }
476
477     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
478
479     ret = sqlite3_step(stmt);
480     if (ret == SQLITE_ERROR) {
481         LOGE("sqlite3_step() failed : ret(%d)", ret);
482         goto cleanup;
483     }
484
485     int uri_type = sqlite3_column_int(stmt, 0);
486     const unsigned char *thumbnail = sqlite3_column_text(stmt, 1);
487
488     if (uri_type == 1 && unlink((const char *)uri) == -1)
489         LOGE("fail to delete sticker file");
490
491     if (thumbnail && unlink((const char *)thumbnail) == -1)
492         LOGE("fail to delete thumbnail image");
493
494     sqlite3_finalize(stmt);
495     stmt = NULL;
496
497     ret = sqlite3_prepare_v2(db, STICKER_DB_DELETE_STICKER_INFO_BY_URI, -1, &stmt, NULL);
498     if (ret != SQLITE_OK) {
499         LOGE("fail to delete sticker information : %s", sqlite3_errmsg(db));
500         goto cleanup;
501     }
502
503     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
504
505     ret = sqlite3_step(stmt);
506     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
507         LOGE("sqlite3_step() failed : ret(%d)", ret);
508         goto cleanup;
509     } else if (sqlite3_changes(db) == 0) {
510         LOGE("No changes to DB");
511         goto cleanup;
512     }
513
514     sqlite3_finalize(stmt);
515     sqlite3_close(db);
516
517     return STICKERD_SERVER_ERROR_NONE;
518
519 cleanup:
520     sqlite3_finalize(stmt);
521     sqlite3_close(db);
522
523     return STICKERD_SERVER_ERROR_DB_FAILED;
524 }
525
526 int stickerd_db_update_sticker_info(int record_id, sticker_info_db_type type, void *data)
527 {
528     int ret;
529     sqlite3 *db = NULL;
530     sqlite3_stmt *stmt = NULL;
531
532     db = _db_open();
533     if (!db)
534         return STICKERD_SERVER_ERROR_DB_FAILED;
535
536     const char* query = _db_get_query(type, CMD_UPDATE);
537     ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
538     if (ret != SQLITE_OK) {
539         LOGE("fail to update sticker information : %s", sqlite3_errmsg(db));
540         goto cleanup;
541     }
542
543     if (type == STICKER_DB_STICKER_TYPE || type == STICKER_DB_STICKER_DISP_TYPE) {
544         sqlite3_bind_int(stmt, 1, *(int *)data);
545     } else if (type == STICKER_DB_STICKER_KEYWORD) {
546         sqlite3_bind_int(stmt, 1, record_id);
547     } else {
548         sqlite3_bind_text(stmt, 1, (char *)data, -1, SQLITE_TRANSIENT);
549     }
550
551     if (type != STICKER_DB_STICKER_KEYWORD)
552         sqlite3_bind_int(stmt, 2, record_id);
553
554     ret = sqlite3_step(stmt);
555     if (ret != SQLITE_OK && ret != SQLITE_DONE) {
556         LOGE("sqlite3_step() failed : ret(%d)", ret);
557         goto cleanup;
558     }
559
560     if (type == STICKER_DB_STICKER_KEYWORD) {
561         GList *list;
562         for(list = (GList *)data; list != NULL; list=list->next) {
563             sqlite3_finalize(stmt);
564             stmt = NULL;
565
566             ret = sqlite3_prepare_v2(db, STICKER_DB_INSERT_STICKER_KEYWORD_INFO, -1, &stmt, NULL);
567             if (ret != SQLITE_OK) {
568                 LOGE("fail to insert sticker information : %s", sqlite3_errmsg(db));
569                 goto cleanup;
570             }
571
572             sqlite3_bind_int(stmt, 1, record_id);
573             sqlite3_bind_text(stmt, 2, (char *)list->data, -1, SQLITE_TRANSIENT);
574
575             ret = sqlite3_step(stmt);
576             if (ret != SQLITE_OK && ret != SQLITE_DONE) {
577                 LOGE("sqlite3_step() failed : ret(%d)", ret);
578                 goto cleanup;
579             } else if (sqlite3_changes(db) == 0) {
580                 LOGE("No changes to DB");
581                 goto cleanup;
582             }
583         }
584     }
585
586     sqlite3_finalize(stmt);
587     sqlite3_close(db);
588
589     return STICKERD_SERVER_ERROR_NONE;
590
591 cleanup:
592     sqlite3_finalize(stmt);
593     sqlite3_close(db);
594
595     return STICKERD_SERVER_ERROR_DB_FAILED;
596 }
597
598 int stickerd_db_get_sticker_info_by_record_id(int record_id, sticker_info_db *sticker_info)
599 {
600     int ret;
601     sqlite3 *db = NULL;
602     sqlite3_stmt *stmt = NULL;
603
604     db = _db_open();
605     if (!db)
606         return STICKERD_SERVER_ERROR_DB_FAILED;
607
608     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_INFO_BY_RECORD_ID, -1, &stmt, NULL);
609     if (ret != SQLITE_OK) {
610         LOGE("fail to get sticker information : %s", sqlite3_errmsg(db));
611         goto cleanup;
612     }
613
614     sqlite3_bind_int(stmt, 1, record_id);
615
616     ret = sqlite3_step(stmt);
617     if (ret == SQLITE_ERROR) {
618         LOGE("sqlite3_step() failed : ret(%d)", ret);
619         goto cleanup;
620     }
621
622     const unsigned char *tmp_app_id = sqlite3_column_text(stmt, 1);
623     if (tmp_app_id) {
624         sticker_info->app_id = strdup((const char *)tmp_app_id);
625     } else {
626         LOGW("invalid record_id : %d", record_id);
627         goto cleanup;
628     }
629
630     sticker_info->type = sqlite3_column_int(stmt, 2);
631
632     const unsigned char *tmp_uri = sqlite3_column_text(stmt, 3);
633     if (tmp_uri)
634         sticker_info->uri = strdup((const char *)tmp_uri);
635
636     const unsigned char *tmp_thumbnail = sqlite3_column_text(stmt, 4);
637     if (tmp_thumbnail)
638         sticker_info->thumbnail = strdup((const char *)tmp_thumbnail);
639
640     const unsigned char *tmp_description = sqlite3_column_text(stmt, 5);
641     if (tmp_description)
642         sticker_info->description = strdup((const char *)tmp_description);
643
644     const unsigned char *tmp_group = sqlite3_column_text(stmt, 6);
645     if (tmp_group)
646         sticker_info->group = strdup((const char *)tmp_group);
647
648     const unsigned char *tmp_date = sqlite3_column_text(stmt, 7);
649     if (tmp_date)
650         sticker_info->date = strdup((const char *)tmp_date);
651
652     sticker_info->display_type = sqlite3_column_int(stmt, 8);
653
654     sqlite3_finalize(stmt);
655     stmt = NULL;
656
657     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_KEYWORD_INFO_BY_RECORD_ID, -1, &stmt, NULL);
658     if (ret != SQLITE_OK) {
659         LOGE("fail to get sticker keyword : %s", sqlite3_errmsg(db));
660         goto cleanup;
661     }
662
663     sqlite3_bind_int(stmt, 1, record_id);
664
665     while (sqlite3_step(stmt) == SQLITE_ROW) {
666         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
667         if (keyword)
668             sticker_info->keyword = g_list_append(sticker_info->keyword, strdup((const char *)keyword));
669     }
670
671     sqlite3_finalize(stmt);
672     sqlite3_close(db);
673
674     return STICKERD_SERVER_ERROR_NONE;
675
676 cleanup:
677     sqlite3_finalize(stmt);
678     sqlite3_close(db);
679
680     return STICKERD_SERVER_ERROR_DB_FAILED;
681 }
682
683 int stickerd_db_get_group_list(GVariantBuilder *builder, char *app_id)
684 {
685     int ret;
686     sqlite3 *db = NULL;
687     sqlite3_stmt *stmt = NULL;
688
689     db = _db_open();
690     if (!db)
691         return STICKERD_SERVER_ERROR_DB_FAILED;
692
693     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_ALL_GROUP_LIST, -1, &stmt, NULL);
694     if (ret != SQLITE_OK) {
695         LOGE("fail to get group list : %s", sqlite3_errmsg(db));
696         goto cleanup;
697     }
698
699     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
700
701     while (sqlite3_step(stmt) == SQLITE_ROW) {
702         const unsigned char *group = sqlite3_column_text(stmt, 0);
703         if (group)
704             g_variant_builder_add(builder, "(s)", strdup((const char *)group));
705     }
706
707     sqlite3_finalize(stmt);
708     sqlite3_close(db);
709
710     return STICKERD_SERVER_ERROR_NONE;
711
712 cleanup:
713     sqlite3_finalize(stmt);
714     sqlite3_close(db);
715
716     return STICKERD_SERVER_ERROR_DB_FAILED;
717 }
718
719 int stickerd_db_get_keyword_list(GVariantBuilder *builder, char *app_id)
720 {
721     int ret;
722     sqlite3 *db = NULL;
723     sqlite3_stmt *stmt = NULL;
724
725     db = _db_open();
726     if (!db)
727         return STICKERD_SERVER_ERROR_DB_FAILED;
728
729     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_ALL_KEYWORD_LIST, -1, &stmt, NULL);
730     if (ret != SQLITE_OK) {
731         LOGE("fail to get keyword list : %s", sqlite3_errmsg(db));
732         goto cleanup;
733     }
734
735     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
736
737     while (sqlite3_step(stmt) == SQLITE_ROW) {
738         const unsigned char *keyword = sqlite3_column_text(stmt, 0);
739         if (keyword)
740             g_variant_builder_add(builder, "(s)", strdup((const char *)keyword));
741     }
742
743     sqlite3_finalize(stmt);
744     sqlite3_close(db);
745
746     return STICKERD_SERVER_ERROR_NONE;
747
748 cleanup:
749     sqlite3_finalize(stmt);
750     sqlite3_close(db);
751
752     return STICKERD_SERVER_ERROR_DB_FAILED;
753 }
754
755 int stickerd_db_get_sticker_count(int *count, char *app_id)
756 {
757     int ret;
758     sqlite3 *db = NULL;
759     sqlite3_stmt *stmt = NULL;
760
761     db = _db_open();
762     if (!db)
763         return STICKERD_SERVER_ERROR_DB_FAILED;
764
765     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_STICKER_COUNT, -1, &stmt, NULL);
766     if (ret != SQLITE_OK) {
767         LOGE("fail to get sticker count : %s", sqlite3_errmsg(db));
768         goto cleanup;
769     }
770
771     sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
772
773     ret = sqlite3_step(stmt);
774     if (ret == SQLITE_ERROR) {
775         LOGE("sqlite3_step() failed : ret(%d)", ret);
776         goto cleanup;
777     }
778
779     *count = sqlite3_column_int(stmt, 0);
780
781     sqlite3_finalize(stmt);
782     sqlite3_close(db);
783
784     return STICKERD_SERVER_ERROR_NONE;
785
786 cleanup:
787     sqlite3_finalize(stmt);
788     sqlite3_close(db);
789
790     return STICKERD_SERVER_ERROR_DB_FAILED;
791 }
792
793 int stickerd_db_get_record_id(sticker_info_db_type type, GList **id_list, void *data, char *app_id, int offset, int count)
794 {
795     int ret;
796     sqlite3 *db = NULL;
797     sqlite3_stmt *stmt = NULL;
798
799     db = _db_open();
800     if (!db)
801         return STICKERD_SERVER_ERROR_DB_FAILED;
802
803     const char* query = _db_get_query(type, CMD_SELECT);
804     ret = sqlite3_prepare_v2(db, query, -1, &stmt, NULL);
805     if (ret != SQLITE_OK) {
806         LOGE("fail to get record id : %s", sqlite3_errmsg(db));
807         goto cleanup;
808     }
809
810     if (type == STICKER_DB_STICKER_ALL || type == STICKER_DB_STICKER_APPID) {
811         sqlite3_bind_text(stmt, 1, app_id, -1, SQLITE_TRANSIENT);
812         sqlite3_bind_int(stmt, 2, offset);
813         sqlite3_bind_int(stmt, 3, count);
814     } else {
815         if (type == STICKER_DB_STICKER_TYPE || type == STICKER_DB_STICKER_DISP_TYPE)
816             sqlite3_bind_int(stmt, 1, *(int *)data);
817         else
818             sqlite3_bind_text(stmt, 1, (char *)data, -1, SQLITE_TRANSIENT);
819
820         sqlite3_bind_text(stmt, 2, app_id, -1, SQLITE_TRANSIENT);
821         sqlite3_bind_int(stmt, 3, offset);
822         sqlite3_bind_int(stmt, 4, count);
823     }
824
825     while (sqlite3_step(stmt) == SQLITE_ROW) {
826         const unsigned char *tmp_id = sqlite3_column_text(stmt, 0);
827         if (tmp_id)
828             *id_list = g_list_append(*id_list, strdup((const char *)tmp_id));
829     }
830
831     sqlite3_finalize(stmt);
832     sqlite3_close(db);
833
834     return STICKERD_SERVER_ERROR_NONE;
835
836 cleanup:
837     sqlite3_finalize(stmt);
838     sqlite3_close(db);
839
840     return STICKERD_SERVER_ERROR_DB_FAILED;
841 }
842
843 int stickerd_db_get_group_list_by_display_type(GVariantBuilder *builder, char *app_id, int disp_type)
844 {
845     int ret;
846     sqlite3 *db = NULL;
847     sqlite3_stmt *stmt = NULL;
848
849     db = _db_open();
850     if (!db)
851         return STICKERD_SERVER_ERROR_DB_FAILED;
852
853     ret = sqlite3_prepare_v2(db, STICKER_DB_GET_GROUP_LIST_BY_DISP_TYPE, -1, &stmt, NULL);
854     if (ret != SQLITE_OK) {
855         LOGE("fail to get group list : %s", sqlite3_errmsg(db));
856         goto cleanup;
857     }
858
859     sqlite3_bind_int(stmt, 1, disp_type);
860     sqlite3_bind_text(stmt, 2, app_id, -1, SQLITE_TRANSIENT);
861
862     while (sqlite3_step(stmt) == SQLITE_ROW) {
863         const unsigned char *group = sqlite3_column_text(stmt, 0);
864         if (group)
865             g_variant_builder_add(builder, "(s)", strdup((const char *)group));
866     }
867
868     sqlite3_finalize(stmt);
869     sqlite3_close(db);
870
871     return STICKERD_SERVER_ERROR_NONE;
872
873 cleanup:
874     sqlite3_finalize(stmt);
875     sqlite3_close(db);
876
877     return STICKERD_SERVER_ERROR_DB_FAILED;
878 }
879
880 int stickerd_db_check_file_exists(int *result, char *uri)
881 {
882     int ret;
883     sqlite3 *db = NULL;
884     sqlite3_stmt *stmt = NULL;
885
886     db = _db_open();
887     if (!db)
888         return STICKERD_SERVER_ERROR_DB_FAILED;
889
890     ret = sqlite3_prepare_v2(db, STICKER_DB_CHECK_FILE_EXISTS, -1, &stmt, NULL);
891     if (ret != SQLITE_OK) {
892         LOGE("fail to check file exists : %s", sqlite3_errmsg(db));
893         goto cleanup;
894     }
895
896     sqlite3_bind_text(stmt, 1, uri, -1, SQLITE_TRANSIENT);
897
898     ret = sqlite3_step(stmt);
899     if (ret == SQLITE_ERROR) {
900         LOGE("sqlite3_step() failed : ret(%d)", ret);
901         goto cleanup;
902     }
903
904     *result = sqlite3_column_int(stmt, 0);
905
906     sqlite3_finalize(stmt);
907     sqlite3_close(db);
908
909     return STICKERD_SERVER_ERROR_NONE;
910
911 cleanup:
912     sqlite3_finalize(stmt);
913     sqlite3_close(db);
914
915     return STICKERD_SERVER_ERROR_DB_FAILED;
916 }