Add a api to organize db's data and option table 45/167345/2
authormk5004.lee <mk5004.lee@samsung.com>
Wed, 17 Jan 2018 06:51:41 +0000 (15:51 +0900)
committermk5004.lee <mk5004.lee@samsung.com>
Wed, 17 Jan 2018 08:02:23 +0000 (17:02 +0900)
- If pkgname does not exist in the setting table, delete it.

Change-Id: Ic9357a9a9950e9daa3556585898c7317f6f4e179
Signed-off-by: mk5004.lee <mk5004.lee@samsung.com>
include/badge_db.h
include/badge_setting_service.h
src/badge_internal.c
src/badge_setting_service.c

index 1f342cf..f469574 100755 (executable)
@@ -32,6 +32,8 @@ extern "C" {
 
 #define BADGE_DB_PATH tzplatform_mkpath(TZ_SYS_DB, ".badge.db")
 #define BADGE_SETTING_DB_TABLE "badge_setting"
+#define BADGE_TABLE_NAME "badge_data"
+#define BADGE_OPTION_TABLE_NAME "badge_option"
 
 int badge_db_insert(const char *pkgname, const char *writable_pkg, const char *caller, uid_t uid);
 int badge_db_delete(const char *pkgname, const char *caller_pkg, uid_t uid, pid_t pid);
index 130ca2e..11d9e03 100644 (file)
@@ -44,6 +44,7 @@ int badge_setting_delete_package_for_uid(const char *pkgname, uid_t uid);
 int badge_setting_refresh_setting_table(uid_t uid);
 int badge_db_update_setting(char *pkgname, char *appid, int allow_to_display, uid_t uid);
 int badge_setting_is_existed_appid(const char *appid, bool *is_existed, uid_t uid);
+int badge_setting_arrange_tables(uid_t uid);
 
 /**
  * @}
index b625a28..7945f08 100755 (executable)
@@ -40,8 +40,6 @@
 #include "badge_setting_service.h"
 
 #define BADGE_PKGNAME_LEN 512
-#define BADGE_TABLE_NAME "badge_data"
-#define BADGE_OPTION_TABLE_NAME "badge_option"
 
 #define BADGE_CHANGED_NOTI     "badge_changed"
 
index 3a5e7f4..9a61b7d 100644 (file)
@@ -693,3 +693,64 @@ out:
        return ret;
 }
 
+EXPORT_API int badge_setting_arrange_tables(uid_t uid)
+{
+       int ret = BADGE_ERROR_NONE;
+       int sqlret;
+       sqlite3 *db = NULL;
+       char *sqlbuf = NULL;
+
+       sqlret = db_util_open(BADGE_DB_PATH, &db, 0);
+       if (sqlret != SQLITE_OK || !db) {
+               ERR("Failed to db_util_open [%d]", sqlret);
+               if (sqlret == SQLITE_PERM)
+                       return BADGE_ERROR_PERMISSION_DENIED;
+               return BADGE_ERROR_FROM_DB;
+       }
+
+       sqlbuf = sqlite3_mprintf("DELETE FROM %Q WHERE pkgname not in "
+                               "(SELECT pkgname FROM %Q) AND uid = %d",
+                               BADGE_TABLE_NAME, BADGE_SETTING_DB_TABLE, uid);
+       if (sqlbuf == NULL) {
+               /* LCOV_EXCL_START */
+               ERR("Failed to alloc query");
+               ret = BADGE_ERROR_OUT_OF_MEMORY;
+               goto out;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = badge_db_exec(db, sqlbuf, NULL);
+       if (ret != BADGE_ERROR_NONE)
+               goto out;
+
+       if (sqlbuf) {
+               sqlite3_free(sqlbuf);
+               sqlbuf = NULL;
+       }
+
+       sqlbuf = sqlite3_mprintf("DELETE FROM %Q WHERE pkgname not in "
+                               "(SELECT pkgname FROM %Q) AND uid = %d",
+                               BADGE_OPTION_TABLE_NAME, BADGE_SETTING_DB_TABLE,
+                               uid);
+       if (sqlbuf == NULL) {
+               /* LCOV_EXCL_START */
+               ERR("Failed to alloc query");
+               ret = BADGE_ERROR_OUT_OF_MEMORY;
+               goto out;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = badge_db_exec(db, sqlbuf, NULL);
+
+out:
+       if (sqlbuf)
+               sqlite3_free(sqlbuf);
+
+       if (db) {
+               sqlret = db_util_close(db);
+               if (sqlret != SQLITE_OK)
+                       WARN("Failed to db_util_close [%d]", sqlret);
+       }
+
+       return ret;
+}