Use sqlite3 API directly instead of using db-util 50/197750/6 accepted/tizen/unified/20190128.061337 submit/tizen/20190121.015422
authorMinje Ahn <minje.ahn@samsung.com>
Wed, 16 Jan 2019 05:45:16 +0000 (14:45 +0900)
committerhj kim <backto.kim@samsung.com>
Thu, 17 Jan 2019 02:46:22 +0000 (11:46 +0900)
1. Remove db-util dependency. db-util is a simple wrapper of sqlite so it is recommended to use sqlite directly
2. add code of "localize collation" from the libslp-db-util to support localized collate fileter option in media-content

Change-Id: Idc7314ba7cd26f4390e0df60abfed9d1a1a50e4b
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
Makefile.am
configure.ac
lib/media-util-db.c
packaging/media-server.spec

index 456c881..24fc646 100755 (executable)
@@ -21,7 +21,6 @@ COMMON_CFLAGS = -I${srcdir}/lib/include \
                $(GTHREAD_CFLAGS) \
                $(DLOG_CFLAGS) \
                $(SQLITE3_CFLAGS) \
-               $(DB_UTIL_CFLAGS) \
                $(INIPARSER_CFLAGS) \
                $(LIBPMCONTROL_CFLAGS) \
                $(LIBSYSTEMDLOGIN_CFLAGS) \
@@ -37,7 +36,6 @@ COMMON_LDADD =        libmedia-utils.la \
                $(GTHREAD_LIBS) \
                $(DLOG_LIBS) \
                $(SQLITE3_LIBS) \
-               $(DB_UTIL_LIBS) \
                $(INIPARSER_LIBS) \
                $(LIBPMCONTROL_LIBS) \
                $(LIBSYSTEMDLOGIN_LIBS) \
@@ -72,7 +70,7 @@ libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \
                              $(DLOG_CFLAGS) \
                              $(GIO_CFLAGS) \
                              $(SQLITE3_CFLAGS) \
-                             $(DB_UTIL_CFLAGS) \
+                             $(ICUI18N_CFLAGS) \
                              $(PHONESTATUS_CFLAGS) \
                              $(TZ_PLATFORM_CONFIG_CFLAGS) \
                              $(LIBSMACK_CFLAGS) \
@@ -85,7 +83,7 @@ libmedia_utils_la_LIBADD  =   $(GLIB_LIBS) \
                               $(DLOG_LIBS) \
                              $(GIO_LIBS) \
                              $(SQLITE3_LIBS) \
-                             $(DB_UTIL_LIBS) \
+                             $(ICUI18N_LIBS) \
                              $(PHONESTATUS_LIBS) \
                              $(TZ_PLATFORM_CONFIG_LIBS) \
                              $(LIBSMACK_LIBS) \
@@ -116,7 +114,6 @@ media_server_SOURCES = src/common/media-common-utils.c \
 
 media_server_CFLAGS = -I${srcdir}/src/server/include \
                        $(SQLITE3_CFLAGS) \
-                       $(DB_UTIL_CFLAGS) \
                        $(COMMON_CFLAGS) \
                         $(PHONESTATUS_CFLAGS) \
                         $(SYSTEM_INFO_CFLAGS)
@@ -201,7 +198,6 @@ mediadb_update_CFLAGS = -I${srcdir}/lib/include \
                         -D_FILE_OFFSET_BITS=64 \
                        $(GTHREAD_CFLAGS) \
                        $(SQLITE3_CFLAGS) \
-                       $(DB_UTIL_CFLAGS) \
                        $(GLIB_CFLAGS) \
                        $(GIO_CFLAGS)
 
@@ -212,7 +208,6 @@ mediadb_update_LDFLAGS = -pie
 mediadb_update_LDADD = libmedia-utils.la \
                        $(GLIB_LIBS) \
                        $(SQLITE3_LIBS) \
-                       $(DB_UTIL_LIBS) \
                        $(GTHREAD_LIBS) \
                        $(GIO_LIBS) \
                        -ldl
index 6fc6b0f..31107fe 100755 (executable)
@@ -79,9 +79,9 @@ PKG_CHECK_MODULES(SQLITE3, sqlite3)
 AC_SUBST(SQLITE3_CFLAGS)
 AC_SUBST(SQLITE3_LIBS)
 
-PKG_CHECK_MODULES(DB_UTIL, db-util)
-AC_SUBST(DB_UTIL_CFLAGS)
-AC_SUBST(DB_UTIL_LIBS)
+PKG_CHECK_MODULES(ICUI18N, icu-i18n)
+AC_SUBST(ICUI18N_CFLAGS)
+AC_SUBST(ICUI18N_LIBS)
 
 PKG_CHECK_MODULES(INIPARSER, iniparser)
 AC_SUBST(INIPARSER_CFLAGS)
index 23c683c..55e5774 100755 (executable)
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/smack.h>
-#include <db-util.h>
+
+#include <dlfcn.h>
+
+#include <unicode/utypes.h>
+#include <unicode/ucol.h>
+#include <unicode/uiter.h>
+#include <unicode/ustring.h>
+#include <vconf.h>
+
 #include "media-server-ipc.h"
 #include "media-util-dbg.h"
 #include "media-util-internal.h"
 
 #define BATCH_START "BEGIN"
 #define BATCH_END "COMMIT"
+#define PATH_PLUGIN_LIBICU                             PATH_LIBDIR"/libicui18n.so"
+
+UCollator *(*icu_ucol_open)(const char *, UErrorCode *);
+void (*icu_ucol_close)(UCollator *);
+void (*icu_ucol_setStrength)(UCollator *, UCollationStrength);
+UCollationResult (*icu_ucol_strcollIter)(const UCollator *, UCharIterator *, UCharIterator *, UErrorCode *);
+void (*icu_uiter_setUTF8)(UCharIterator *, const char *, int32_t);
+void (*icu_uloc_setDefault)(const char* localeID, UErrorCode* status);
+const char* (*icu_uloc_getDefault)(void);
+
+static void *funcHandle = NULL;
 
 static __thread GArray *sql_list = NULL;
 
@@ -60,6 +79,101 @@ static int __media_db_busy_handler(void *pData, int count)
        return 200 - count;
 }
 
+/* The collating function must return an integer that is negative, zero or positive */
+static int __media_db_collate_icu_8(void *ucol, int str1_len, const void *str1, int str2_len, const void *str2)
+{
+       UCharIterator uiter1, uiter2;
+       UErrorCode error = U_ZERO_ERROR;
+
+       icu_ucol_strcollIter = dlsym(funcHandle, "ucol_strcollIter");
+       icu_uiter_setUTF8 = dlsym(funcHandle, "uiter_setUTF8");
+
+       icu_uiter_setUTF8(&uiter1, (const char *) str1, str1_len);
+       icu_uiter_setUTF8(&uiter2, (const char *) str2, str2_len);
+
+       UCollationResult result = icu_ucol_strcollIter((UCollator *) ucol, &uiter1, &uiter2, &error);
+       if (U_FAILURE(error)) {
+               MSAPI_DBG_ERR("ucol_strcollIter error: %d", error);
+               return -1;
+       }
+
+       return result;
+}
+
+void __media_db_collation_cb(void* pArg, sqlite3* handle, int charset, const char* name)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       UErrorCode status = U_ZERO_ERROR;
+       const char* locale = NULL;
+       char *lang = NULL;
+       UCollator* ucol = NULL;
+
+       if (handle == NULL || !MS_STRING_VALID(name)) {
+               MSAPI_DBG_ERR("Input parameter is NULL");
+               return;
+       }
+
+       if (charset == SQLITE_UTF8 && !sqlite3_stricmp(name, "localized")) {
+               funcHandle = dlopen(PATH_PLUGIN_LIBICU, RTLD_LAZY);
+               if (funcHandle == NULL) {
+                       MSAPI_DBG_ERR("dlopen failed");
+                       return;
+               }
+
+               icu_ucol_open = dlsym(funcHandle, "ucol_open");
+               icu_ucol_close = dlsym(funcHandle, "ucol_close");
+               icu_ucol_setStrength = dlsym(funcHandle, "ucol_setStrength");
+               icu_uloc_setDefault = dlsym(funcHandle, "uloc_setDefault");
+               icu_uloc_getDefault = dlsym(funcHandle, "uloc_getDefault");
+
+               lang = vconf_get_str(VCONFKEY_LANGSET);
+               /* get current locale */
+               if (lang) {
+                       icu_uloc_setDefault(lang, &status);
+                       MS_SAFE_FREE(lang);
+               } else {
+                       MSAPI_DBG_ERR("Fail to get current language vconf");
+                       dlclose(funcHandle);
+                       return;
+               }
+               locale = icu_uloc_getDefault();
+               if (locale == NULL) {
+                       MSAPI_DBG_ERR("Fail to get current locale");
+                       dlclose(funcHandle);
+                       return;
+               }
+               MSAPI_DBG_INFO("locale : %s", locale);
+
+               ucol = icu_ucol_open(locale, &status);
+               if (status == U_USING_DEFAULT_WARNING) {
+                       MSAPI_DBG("ucol_open success with default collate option");
+               } else if (U_FAILURE(status)) {
+                       MSAPI_DBG_ERR("ucol_open fail : %d", status);
+                       dlclose(funcHandle);
+                       return;
+               }
+
+               icu_ucol_setStrength(ucol, UCOL_SECONDARY); /* NOTICE : MCD sets UCOL_PRIMARY */
+               if (U_FAILURE(status)) {
+                       MSAPI_DBG_ERR("ucol_setStrength fail : %d", status);
+                       dlclose(funcHandle);
+                       return;
+               }
+
+               ret = sqlite3_create_collation_v2(handle, name, SQLITE_UTF8, ucol, __media_db_collate_icu_8, (void *)icu_ucol_close);
+               if (ret != SQLITE_OK) {
+                       MSAPI_DBG_ERR("sqlite3_create_collation_v2 fail : %d", ret);
+                       icu_ucol_close(ucol);
+               } else {
+                       MSAPI_DBG("sqlite3_create_collation_v2 success");
+               }
+
+               dlclose(funcHandle);
+       } else {
+               MSAPI_DBG_ERR("No matching collator for %s", name);
+       }
+}
+
 static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, bool need_write)
 {
        int ret = SQLITE_OK;
@@ -71,10 +185,9 @@ static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, boo
 
        /*Connect DB*/
        if (need_write)
-               ret = db_util_open_with_options(db_path, db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+               ret = sqlite3_open_v2(db_path, db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
        else
-               ret = db_util_open_with_options(db_path, db_handle, SQLITE_OPEN_READONLY, NULL);
-
+               ret = sqlite3_open_v2(db_path, db_handle, SQLITE_OPEN_READONLY, NULL);
 
        if (SQLITE_OK != ret) {
                MSAPI_DBG_ERR("error when db open path[%s],ret[%d]", db_path, ret);
@@ -120,6 +233,9 @@ static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, boo
                return MS_MEDIA_ERR_DB_CONNECT_FAIL;
        }
 
+       /* Set for localized collate */
+       sqlite3_collation_needed(*db_handle, NULL, __media_db_collation_cb);
+
        return MS_MEDIA_ERR_NONE;
 }
 
index b1291c7..0dcb792 100755 (executable)
@@ -16,7 +16,7 @@ BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(sqlite3)
-BuildRequires:  pkgconfig(db-util)
+BuildRequires:  pkgconfig(icu-i18n)
 BuildRequires:  pkgconfig(iniparser)
 BuildRequires:  pkgconfig(libsmack)
 BuildRequires:  pkgconfig(libsystemd-login)