From: Gukhwan Cho Date: Fri, 16 Oct 2015 07:10:19 +0000 (+0900) Subject: get sorted group_name by collation cb func X-Git-Tag: accepted/tizen/mobile/20151119.033537~4^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f8e42314a008624d26cd4af3682688e6025e5012;p=platform%2Fcore%2Fpim%2Fcontacts-service.git get sorted group_name by collation cb func Change-Id: I9bb8835ffc3a5df5506420517bcd8f29dcbcb7b7 Signed-off-by: Gukhwan Cho --- diff --git a/server/ctsvc_server_sqlite.c b/server/ctsvc_server_sqlite.c index 8de5548..b5615ff 100644 --- a/server/ctsvc_server_sqlite.c +++ b/server/ctsvc_server_sqlite.c @@ -90,6 +90,10 @@ int ctsvc_server_db_open(sqlite3 **db) ctsvc_db_phone_number_equal_callback, NULL, NULL); RETVM_IF(SQLITE_OK != ret, CONTACTS_ERROR_DB, "sqlite3_create_function() Fail(%d)", ret); + ret = sqlite3_create_collation(server_db, "_NAME_SORT_", SQLITE_UTF8, (void *)SQLITE_UTF8, + ctsvc_db_group_name_sort_callback); + RETVM_IF(SQLITE_OK != ret, CONTACTS_ERROR_DB, + "sqlite3_create_collation() Fail(%d)", ret); } if (db) *db = server_db; diff --git a/server/db/ctsvc_db_plugin_group_helper.c b/server/db/ctsvc_db_plugin_group_helper.c index 716844e..88525fc 100644 --- a/server/db/ctsvc_db_plugin_group_helper.c +++ b/server/db/ctsvc_db_plugin_group_helper.c @@ -27,6 +27,9 @@ #include "ctsvc_db_utils.h" #include "ctsvc_db_plugin_group_helper.h" #include "ctsvc_notify.h" +#include "ctsvc_normalize.h" +#include "ctsvc_server_setting.h" +#include "ctsvc_localize.h" /* * Whenever deleting group, this function will be called @@ -55,4 +58,79 @@ void ctsvc_db_group_delete_callback(sqlite3_context *context, int argc, sqlite3_ return; } +static int _ctsvc_db_group_name_collation_str(const char *str1, const char *str2) +{ + int ret; + int len1, len2; + char *str_dest1 = NULL; + char *str_dest2 = NULL; + + ctsvc_collation_str((char *)str1, &str_dest1); + ctsvc_collation_str((char *)str2, &str_dest2); + + len1 = strlen(str_dest1); + len2 = strlen(str_dest2); + if (len1 < len2) + ret = strncmp(str_dest1, str_dest2, len1); + else + ret = strncmp(str_dest1, str_dest2, len2); + + free(str_dest1); + free(str_dest2); + return ret; +} + +int ctsvc_db_group_name_sort_callback(void *context, int str1_len, const void *str1, int str2_len, const void *str2) +{ + int ret; + int str1_sort_type; + int str2_sort_type; + char str_src1[CTSVC_STR_SHORT_LEN] = {0}; + char str_src2[CTSVC_STR_SHORT_LEN] = {0}; + int prim_sort = ctsvc_get_primary_sort(); + + strncpy(str_src1, str1, str1_len); + strncpy(str_src2, str2, str2_len); + str1_sort_type = ctsvc_get_name_sort_type(str_src1); + str2_sort_type = ctsvc_get_name_sort_type(str_src2); + + switch (str1_sort_type) { + case CTSVC_SORT_NUMBER: + if (CTSVC_SORT_OTHERS == str2_sort_type) + ret = 1; + else if (CTSVC_SORT_NUMBER == str2_sort_type) + ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2); + else + ret = -1; + break; + + case CTSVC_SORT_OTHERS: + if (CTSVC_SORT_OTHERS == str2_sort_type) + ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2); + else + ret = -1; + break; + + default: + if (CTSVC_SORT_NUMBER >= str2_sort_type) { + ret = 1; + } + else { + if (str1_sort_type != str2_sort_type) { + if (str1_sort_type == prim_sort) + ret = -1; + else if (str2_sort_type == prim_sort) + ret = 1; + else + ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2); + } + else { + ret = _ctsvc_db_group_name_collation_str(str_src1, str_src2); + } + } + break; + } + + return ret; +} diff --git a/server/db/ctsvc_db_plugin_group_helper.h b/server/db/ctsvc_db_plugin_group_helper.h index 87ca39f..8d13174 100644 --- a/server/db/ctsvc_db_plugin_group_helper.h +++ b/server/db/ctsvc_db_plugin_group_helper.h @@ -25,5 +25,6 @@ #include void ctsvc_db_group_delete_callback(sqlite3_context *context, int argc, sqlite3_value ** argv); +int ctsvc_db_group_name_sort_callback(void *context, int str1_len, const void *str1, int str2_len, const void *str2); #endif /* __CTSVC_DB_PLUGIN_GROUP_HELPER_H__ */ diff --git a/server/db/ctsvc_db_query.c b/server/db/ctsvc_db_query.c index 60add16..7fb9d2d 100644 --- a/server/db/ctsvc_db_query.c +++ b/server/db/ctsvc_db_query.c @@ -1026,6 +1026,11 @@ int ctsvc_db_make_get_records_query_stmt(ctsvc_query_s *s_query, int offset, int temp_len = SAFE_SNPRINTF(&query, &query_size, len, field_name); if (0 <= temp_len) len+= temp_len; + if (CTSVC_PROPERTY_GROUP_NAME == s_query->sort_property_id) { + temp_len = SAFE_SNPRINTF(&query, &query_size, len, " COLLATE _NAME_SORT_ "); + if (0 <= temp_len) len+= temp_len; + } + if (false == s_query->sort_asc) { temp_len = SAFE_SNPRINTF(&query, &query_size, len, " DESC "); if (0 <= temp_len) len+= temp_len; diff --git a/server/db/ctsvc_db_sqlite.c b/server/db/ctsvc_db_sqlite.c index f196841..b559d7c 100644 --- a/server/db/ctsvc_db_sqlite.c +++ b/server/db/ctsvc_db_sqlite.c @@ -89,6 +89,10 @@ int ctsvc_db_open(void) ctsvc_db_phone_number_equal_callback, NULL, NULL); RETVM_IF(SQLITE_OK != ret, CONTACTS_ERROR_DB, "sqlite3_create_function() Fail(%d)", ret); + ret = sqlite3_create_collation(ctsvc_db, "_NAME_SORT_", SQLITE_UTF8, (void *)SQLITE_UTF8, + ctsvc_db_group_name_sort_callback); + RETVM_IF(SQLITE_OK != ret, CONTACTS_ERROR_DB, + "sqlite3_create_collation() Fail(%d)", ret); } return CONTACTS_ERROR_NONE /*CTS_SUCCESS*/;