Add new elements for component-based application
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_db.c
index 62ffed3..185aecb 100644 (file)
@@ -329,6 +329,9 @@ static const char *parser_init_queries[] = {
        QUERY_CREATE_TABLE_PACKAGE_APP_INFO_FOR_UID,
        QUERY_CREATE_TRIGGER_UPDATE_PACKAGE_APP_INFO_FOR_UID,
        QUERY_CREATE_TABLE_PACKAGE_APP_SPLASH_SCREEN,
+       QUERY_CREATE_TABLE_PACKAGE_DEPENDENCY_INFO,
+       QUERY_CREATE_TABLE_PACKAGE_PLUGIN_INFO,
+       QUERY_CREATE_TABLE_PACKAGE_APP_COMPONENT_INFO,
        NULL,
 };
 
@@ -342,11 +345,23 @@ static const char *cert_init_queries[] = {
        NULL
 };
 
-static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
+static int __create_tables(sqlite3 *db, const char **queries)
 {
        int ret;
-       const char **queries;
        int i;
+       for (i = 0; queries[i] != NULL; i++) {
+               ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
+               if (ret != SQLITE_OK) {
+                       _LOGE("exec failed: %s", sqlite3_errmsg(db));
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
+{
+       const char **queries;
 
        if (__set_db_version(db))
                return -1;
@@ -360,13 +375,9 @@ static int __initialize_db(sqlite3 *db, const char *dbpath, uid_t uid)
                return -1;
        }
 
-       for (i = 0; queries[i] != NULL; i++) {
-               ret = sqlite3_exec(db, queries[i], NULL, NULL, NULL);
-               if (ret != SQLITE_OK) {
-                       _LOGE("exec failed: %s", sqlite3_errmsg(db));
-                       return -1;
-               }
-       }
+       __BEGIN_TRANSACTION(db);
+       __DO_TRANSACTION(db, __create_tables(db, queries));
+       __END_TRANSACTION(db);
 
        if (__set_db_permission(dbpath, uid))
                _LOGE("failed to set db permission");
@@ -748,8 +759,8 @@ static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
 {
        static const char query[] =
                "INSERT INTO package_app_app_control (app_id, app_control,"
-               "  visibility) "
-               "VALUES (?, ?, ?)";
+               "  visibility, app_control_id) "
+               "VALUES (?, ?, ?, ?)";
        int ret;
        sqlite3_stmt *stmt;
        int idx;
@@ -781,6 +792,7 @@ static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
                __BIND_TEXT(db, stmt, idx++, app->appid);
                __BIND_TEXT(db, stmt, idx++, app_control);
                __BIND_TEXT(db, stmt, idx++, ac->visibility);
+               __BIND_TEXT(db, stmt, idx++, ac->id);
 
                ret = sqlite3_step(stmt);
                if (ret != SQLITE_DONE) {
@@ -1199,6 +1211,51 @@ static int __insert_splashscreen_info(sqlite3 *db, application_x *app,
        return 0;
 }
 
+static int __insert_component_info(sqlite3 *db, application_x *app)
+{
+       static const char query[] =
+               "INSERT INTO package_app_component_info (app_id, component_id, "
+               "type, launch_mode) VALUES(?, ?, ?, ?)";
+       int ret;
+       sqlite3_stmt *stmt;
+       int idx;
+       GList *tmp;
+       component_x *c;
+
+       if (app->components == NULL)
+               return 0;
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       for (tmp = app->components; tmp; tmp = tmp->next) {
+               c = (component_x *)tmp->data;
+               if (c == NULL)
+                       continue;
+
+               idx = 1;
+               __BIND_TEXT(db, stmt, idx++, app->appid);
+               __BIND_TEXT(db, stmt, idx++, c->id);
+               __BIND_TEXT(db, stmt, idx++, c->type);
+               __BIND_TEXT(db, stmt, idx++, c->launch_mode);
+
+               ret = sqlite3_step(stmt);
+               if (ret != SQLITE_DONE) {
+                       _LOGE("step failed: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return -1;
+               }
+               sqlite3_reset(stmt);
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
 static void __trimfunc(GList *trim_list)
 {
        char *trim_data;
@@ -1703,6 +1760,81 @@ static int __insert_package_privilege_info(sqlite3 *db, manifest_x *mfx)
        return 0;
 }
 
+static int __insert_package_plugin_execution_info(sqlite3 *db,
+               manifest_x *mfx)
+{
+       static const char query[] =
+               "INSERT INTO package_plugin_info "
+               "(pkgid, appid, plugin_type, plugin_name) "
+               "VALUES (?, ?, ?, ?)";
+       int ret;
+       sqlite3_stmt *stmt;
+       int idx;
+       GList *tmp;
+       plugin_x *plugin;
+
+       if (!mfx->plugin)
+               return 0;
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       for (tmp = mfx->plugin; tmp; tmp = tmp->next) {
+               plugin = (plugin_x *)tmp->data;
+               if (plugin == NULL)
+                       continue;
+
+               idx = 1;
+               __BIND_TEXT(db, stmt, idx++, plugin->pkgid);
+               __BIND_TEXT(db, stmt, idx++, plugin->appid);
+               __BIND_TEXT(db, stmt, idx++, plugin->plugin_type);
+               __BIND_TEXT(db, stmt, idx++, plugin->plugin_name);
+
+               ret = sqlite3_step(stmt);
+               if (ret != SQLITE_DONE) {
+                       _LOGE("step failed: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return -1;
+               }
+               sqlite3_reset(stmt);
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+static int __delete_package_plugin_execution_info(sqlite3 *db,
+               const char *pkgid)
+{
+       static const char query[] =
+               "DELETE FROM package_plugin_info WHERE pkgid=?";
+       int ret;
+       sqlite3_stmt *stmt;
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, 1, pkgid);
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_DONE) {
+               _LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
 static int __insert_package_appdefined_privilege_info(sqlite3 *db,
                manifest_x *mfx)
 {
@@ -1747,6 +1879,49 @@ static int __insert_package_appdefined_privilege_info(sqlite3 *db,
        return 0;
 }
 
+static int __insert_package_dependency_info(sqlite3 *db, manifest_x *mfx)
+{
+       static const char query[] =
+               "INSERT INTO package_dependency_info"
+               "  (package, depends_on, type, required_version) "
+               "VALUES (?, ?, ?, ?)";
+       int ret;
+       sqlite3_stmt *stmt;
+       int idx;
+       GList *tmp;
+       dependency_x *dep;
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       for (tmp = mfx->dependencies; tmp; tmp = tmp->next) {
+               dep = (dependency_x *)tmp->data;
+               if (dep == NULL)
+                       continue;
+
+               idx = 1;
+               __BIND_TEXT(db, stmt, idx++, mfx->package);
+               __BIND_TEXT(db, stmt, idx++, dep->depends_on);
+               __BIND_TEXT(db, stmt, idx++, dep->type);
+               __BIND_TEXT(db, stmt, idx++, dep->required_version);
+
+               ret = sqlite3_step(stmt);
+               if (ret != SQLITE_DONE) {
+                       _LOGE("step failed: %s", sqlite3_errmsg(db));
+                       sqlite3_finalize(stmt);
+                       return -1;
+               }
+               sqlite3_reset(stmt);
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
 /* _PRODUCT_LAUNCHING_ENHANCED_
  *  app->indicatordisplay, app->portraitimg, app->landscapeimg,
  *  app->guestmode_appstatus
@@ -1900,6 +2075,13 @@ static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
                        sqlite3_finalize(stmt);
                        return -1;
                }
+
+               if (!strcmp(app->component_type, "componentbasedapp")) {
+                       if (__insert_component_info(db, app)) {
+                               sqlite3_finalize(stmt);
+                               return -1;
+                       }
+               }
        }
 
        sqlite3_finalize(stmt);
@@ -2090,6 +2272,8 @@ static int __insert_package_info(sqlite3 *db, manifest_x *mfx)
                return -1;
        if (__insert_package_appdefined_privilege_info(db, mfx))
                return -1;
+       if (__insert_package_dependency_info(db, mfx))
+               return -1;
 
        return 0;
 }
@@ -3012,3 +3196,92 @@ API int pkgmgr_parser_unregister_all_pkg_update_info_in_db(void)
        return pkgmgr_parser_unregister_all_pkg_update_info_in_usr_db(
                        __getuid());
 }
+
+API int pkgmgr_parser_register_pkg_plugin_info_in_usr_db(
+               manifest_x *mfx, uid_t uid)
+{
+       int ret;
+       const char *dbpath;
+       sqlite3 *db;
+
+       if (!mfx)
+               return PM_PARSER_R_EINVAL;
+       dbpath = __get_parser_db_path(uid);
+       ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
+       if (ret != SQLITE_OK) {
+               _LOGE("open db failed: %d", ret);
+               return PM_PARSER_R_ERROR;
+       }
+
+       __BEGIN_TRANSACTION(db);
+       __DO_TRANSACTION(db, __insert_package_plugin_execution_info(db, mfx));
+       __END_TRANSACTION(db);
+
+       sqlite3_close_v2(db);
+
+       return PM_PARSER_R_OK;
+}
+
+API int pkgmgr_parser_register_pkg_plugin_info_in_db(manifest_x *mfx)
+{
+       return pkgmgr_parser_register_pkg_plugin_info_in_usr_db(mfx, __getuid());
+}
+
+API int pkgmgr_parser_update_pkg_plugin_info_in_usr_db(
+               manifest_x *mfx, uid_t uid)
+{
+       int ret;
+       const char *dbpath;
+       sqlite3 *db;
+
+       if (!mfx)
+               return PM_PARSER_R_EINVAL;
+       dbpath = __get_parser_db_path(uid);
+       ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
+       if (ret != SQLITE_OK) {
+               _LOGE("open db failed: %d", ret);
+               return PM_PARSER_R_ERROR;
+       }
+
+       __BEGIN_TRANSACTION(db);
+       __DO_TRANSACTION(db, __delete_package_plugin_execution_info(db, mfx->package));
+       __DO_TRANSACTION(db, __insert_package_plugin_execution_info(db, mfx));
+       __END_TRANSACTION(db);
+
+       sqlite3_close_v2(db);
+
+       return PM_PARSER_R_OK;
+}
+
+API int pkgmgr_parser_update_pkg_plugin_info_in_db(manifest_x *mfx)
+{
+       return pkgmgr_parser_update_pkg_plugin_info_in_usr_db(mfx, __getuid());
+}
+
+API int pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(
+               const char *pkgid, uid_t uid)
+{
+       int ret;
+       const char *dbpath;
+       sqlite3 *db;
+
+       dbpath = __get_parser_db_path(uid);
+       ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
+       if (ret != SQLITE_OK) {
+               _LOGE("open db failed: %d", ret);
+               return PM_PARSER_R_ERROR;
+       }
+
+       __BEGIN_TRANSACTION(db);
+       __DO_TRANSACTION(db, __delete_package_plugin_execution_info(db, pkgid));
+       __END_TRANSACTION(db);
+
+       sqlite3_close_v2(db);
+
+       return PM_PARSER_R_OK;
+}
+
+API int pkgmgr_parser_unregister_pkg_plugin_info_in_db(const char *pkgid)
+{
+       return pkgmgr_parser_unregister_pkg_plugin_info_in_usr_db(pkgid, __getuid());
+}