Fix filter count api 89/46489/4
authorSangyoon Jang <s89.jang@samsung.com>
Fri, 21 Aug 2015 09:51:17 +0000 (18:51 +0900)
committerSangyoon Jang <s89.jang@samsung.com>
Mon, 24 Aug 2015 02:01:53 +0000 (11:01 +0900)
Change-Id: I04898c44b77971635921924e94824da1926eff45
Signed-off-by: Sangyoon Jang <s89.jang@samsung.com>
src/pkgmgrinfo_appinfo.c
src/pkgmgrinfo_pkginfo.c

index 3b5b4f4..e59c3c8 100644 (file)
 #include "pkgmgrinfo_private.h"
 #include "pkgmgr_parser.h"
 
-#define FILTER_QUERY_COUNT_APP "select count(DISTINCT package_app_info.app_id) " \
-                               "from package_app_info LEFT OUTER JOIN package_app_localized_info " \
-                               "ON package_app_info.app_id=package_app_localized_info.app_id " \
-                               "and package_app_localized_info.app_locale='%s' " \
-                               "LEFT OUTER JOIN package_app_app_svc " \
-                               "ON package_app_info.app_id=package_app_app_svc.app_id " \
-                               "LEFT OUTER JOIN package_app_app_category " \
-                               "ON package_app_info.app_id=package_app_app_category.app_id where "
-
 static bool _get_bool_value(const char *str)
 {
        if (str == NULL)
@@ -1940,81 +1931,25 @@ API int pkgmgrinfo_appinfo_filter_add_string(pkgmgrinfo_appinfo_filter_h handle,
        return PMINFO_R_OK;
 }
 
-static int __count_cb(void *data, int ncols, char **coltxt, char **colname)
-{
-       int *p = (int*)data;
-       *p = atoi(coltxt[0]);
-       _LOGE("count value is %d\n", *p);
-       return 0;
-}
-
 API int pkgmgrinfo_appinfo_usr_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count, uid_t uid)
 {
-       retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
-       retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
-       char *locale = NULL;
-       char *condition = NULL;
-       char *error_message = NULL;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char where[MAX_QUERY_LEN] = {'\0'};
-       GSList *list;
-       int ret = 0;
+       int ret;
+       GList *list = NULL;
 
-       pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
-       /*Get current locale*/
-       locale = _get_system_locale();
-       if (locale == NULL) {
-               _LOGE("manifest locale is NULL\n");
-               return PMINFO_R_ERROR;
+       if (handle == NULL || count == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
        }
 
-       ret = __open_manifest_db(uid, true);
-       if (ret == -1) {
-               _LOGE("Fail to open manifest DB\n");
-               free(locale);
+       ret = _appinfo_get_filtered_list(handle, uid, &list);
+       if (ret != PMINFO_R_OK)
                return PMINFO_R_ERROR;
-       }
 
-       /*Start constructing query*/
-       snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_APP, locale);
+       *count = g_list_length(list);
 
-       /*Get where clause*/
-       for (list = filter->list; list; list = g_slist_next(list)) {
-               __get_filter_condition(list->data, &condition);
-               if (condition) {
-                       strncat(where, condition, sizeof(where) - strlen(where) -1);
-                       where[sizeof(where) - 1] = '\0';
-                       free(condition);
-                       condition = NULL;
-               }
-               if (g_slist_next(list)) {
-                       strncat(where, " and ", sizeof(where) - strlen(where) - 1);
-                       where[sizeof(where) - 1] = '\0';
-               }
-       }
-       if (strlen(where) > 0) {
-               strncat(query, where, sizeof(query) - strlen(query) - 1);
-               query[sizeof(query) - 1] = '\0';
-       }
+       g_list_free_full(list, free);
 
-       /*Execute Query*/
-       if (SQLITE_OK !=
-           sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
-               _LOGE("Don't execute query = %s error message = %s\n", query,
-                      error_message);
-               sqlite3_free(error_message);
-               ret = PMINFO_R_ERROR;
-               *count = 0;
-               goto err;
-       }
-       ret = PMINFO_R_OK;
-err:
-       if (locale) {
-               free(locale);
-               locale = NULL;
-       }
-       __close_manifest_db();
-       return ret;
+       return PMINFO_R_OK;
 }
 
 API int pkgmgrinfo_appinfo_filter_count(pkgmgrinfo_appinfo_filter_h handle, int *count)
index cd0d0e7..be7df99 100644 (file)
 #include "pkgmgr_parser_db.h"
 #include "pkgmgr_parser_internal.h"
 
-#define FILTER_QUERY_COUNT_PACKAGE     "select count(DISTINCT package_info.package) " \
-                               "from package_info LEFT OUTER JOIN package_localized_info " \
-                               "ON package_info.package=package_localized_info.package " \
-                               "and package_localized_info.package_locale='%s' where "
-
-
 static int _pkginfo_get_pkginfo(const char *pkgid, uid_t uid,
                pkgmgr_pkginfo_x **pkginfo);
 static char *_get_filtered_query(const char *query_raw,
@@ -69,14 +63,6 @@ static gint __compare_func(gconstpointer data1, gconstpointer data2)
                return -1;
 }
 
-static int __count_cb(void *data, int ncols, char **coltxt, char **colname)
-{
-       int *p = (int*)data;
-       *p = atoi(coltxt[0]);
-       _LOGE("count value is %d\n", *p);
-       return 0;
-}
-
 static void __destroy_each_node(gpointer data, gpointer user_data)
 {
        ret_if(data == NULL);
@@ -1905,72 +1891,23 @@ API int pkgmgrinfo_pkginfo_filter_add_string(pkgmgrinfo_pkginfo_filter_h handle,
 
 API int pkgmgrinfo_pkginfo_usr_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count, uid_t uid)
 {
-       retvm_if(handle == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
-       retvm_if(count == NULL, PMINFO_R_EINVAL, "Filter handle input parameter is NULL\n");
-       char *locale = NULL;
-       char *condition = NULL;
-       char *error_message = NULL;
-       char query[MAX_QUERY_LEN] = {'\0'};
-       char where[MAX_QUERY_LEN] = {'\0'};
-       GSList *list;
-       int ret = 0;
+       int ret;
+       GList *list = NULL;
 
-       pkgmgrinfo_filter_x *filter = (pkgmgrinfo_filter_x*)handle;
-       filter->uid = uid;
-       /*Get current locale*/
-       locale = _get_system_locale();
-       if (locale == NULL) {
-               _LOGE("manifest locale is NULL\n");
-               return PMINFO_R_ERROR;
+       if (handle == NULL || count == NULL) {
+               _LOGE("invalid parameter");
+               return PMINFO_R_EINVAL;
        }
 
-       ret = __open_manifest_db(uid, true);
-       if (ret == -1) {
-               _LOGE("Fail to open manifest DB\n");
-               free(locale);
+       ret = _pkginfo_get_filtered_list((pkgmgrinfo_filter_x *)handle, uid, &list);
+       if (ret != PMINFO_R_OK)
                return PMINFO_R_ERROR;
-       }
 
-       /*Start constructing query*/
-       snprintf(query, MAX_QUERY_LEN - 1, FILTER_QUERY_COUNT_PACKAGE, locale);
+       *count = g_list_length(list);
 
-       /*Get where clause*/
-       for (list = filter->list; list; list = g_slist_next(list)) {
-               __get_filter_condition(list->data, &condition);
-               if (condition) {
-                       strncat(where, condition, sizeof(where) - strlen(where) -1);
-                       where[sizeof(where) - 1] = '\0';
-                       free(condition);
-                       condition = NULL;
-               }
-               if (g_slist_next(list)) {
-                       strncat(where, " and ", sizeof(where) - strlen(where) - 1);
-                       where[sizeof(where) - 1] = '\0';
-               }
-       }
-       if (strlen(where) > 0) {
-               strncat(query, where, sizeof(query) - strlen(query) - 1);
-               query[sizeof(query) - 1] = '\0';
-       }
+       g_list_free_full(list, free);
 
-       /*Execute Query*/
-       if (SQLITE_OK !=
-           sqlite3_exec(GET_DB(manifest_db), query, __count_cb, (void *)count, &error_message)) {
-               _LOGE("Don't execute query = %s error message = %s\n", query,
-                      error_message);
-               sqlite3_free(error_message);
-               ret = PMINFO_R_ERROR;
-               *count = 0;
-               goto err;
-       }
-       ret = PMINFO_R_OK;
-err:
-       if (locale) {
-               free(locale);
-               locale = NULL;
-       }
-       __close_manifest_db();
-       return ret;
+       return PMINFO_R_OK;
 }
 
 API int pkgmgrinfo_pkginfo_filter_count(pkgmgrinfo_pkginfo_filter_h handle, int *count)