[evolution-data-server] sqlitedb: Improve national phone number matching



commit a3526c06166073e3c96a4bfef6a294b5cc71f78b
Author: Mathias Hasselmann <mathias openismus com>
Date:   Fri Mar 1 14:05:28 2013 +0100

    sqlitedb: Improve national phone number matching
    
    The sqlitedb backend and e_phone_number_compare() showed different
    behavior for a few cases of national and short number matching.
    This syncs the behavior.

 .../libedata-book/e-book-backend-sqlitedb.c        |   43 +++++++++++++------
 tests/libebook/client/test-client-custom-summary.c |   21 +++++----
 2 files changed, 41 insertions(+), 23 deletions(-)
---
diff --git a/addressbook/libedata-book/e-book-backend-sqlitedb.c 
b/addressbook/libedata-book/e-book-backend-sqlitedb.c
index f729442..fb8f12d 100644
--- a/addressbook/libedata-book/e-book-backend-sqlitedb.c
+++ b/addressbook/libedata-book/e-book-backend-sqlitedb.c
@@ -1121,6 +1121,7 @@ ixphone_compare_national (gpointer data,
                           gint len2,
                           gconstpointer arg2)
 {
+       const gchar *const country_code = data;
        const gchar *const str1 = arg1;
        const gchar *const str2 = arg2;
        const gchar *sep1 = memchr (str1, '|', len1);
@@ -1147,12 +1148,17 @@ ixphone_compare_national (gpointer data,
         * "+31|2215423789" first and then miss "|2215423789" because
         * we traverse the binary tree in wrong direction.
         */
-       if (cmp == 0 && sep1 != str1 && sep2 != str2) {
-               /* Also compare the country code if the national number
-                * matches and both numbers have a country code. */
-               cmp = e_strcmp2n (
-                       str1, sep1 - str1,
-                       str2, sep2 - str2);
+       if (cmp == 0) {
+               if (sep1 == str1) {
+                       if (sep2 != str2)
+                               cmp = e_strcmp2n (country_code, strlen (country_code), str2, sep2 - str2);
+               } else if (sep2 == str2) {
+                       cmp = e_strcmp2n (str1, sep1 - str1, country_code, 2);
+               } else {
+                       /* Also compare the country code if the national number
+                        * matches and both numbers have a country code. */
+                       cmp = e_strcmp2n (str1, sep1 - str1, str2, sep2 - str2);
+               }
        }
 
        if (booksql_debug ()) {
@@ -1185,10 +1191,13 @@ create_collation (gpointer data,
                ret = sqlite3_create_collation (
                        db, name, SQLITE_UTF8, GINT_TO_POINTER (country_code),
                        ixphone_compare_for_country);
-       } else if (strcmp (name, "ixphone_nn") == 0) {
-               ret = sqlite3_create_collation (
-                       db, name, SQLITE_UTF8, NULL,
-                       ixphone_compare_national);
+       } else if (strcmp (name, "ixphone_national") == 0) {
+               country_code = e_phone_number_get_country_code_for_region (NULL);
+
+               ret = sqlite3_create_collation_v2 (
+                       db, name, SQLITE_UTF8,
+                       g_strdup_printf ("+%d", country_code),
+                       ixphone_compare_national, g_free);
        }
 
        if (ret == SQLITE_OK) {
@@ -3171,24 +3180,28 @@ field_name_and_query_term (EBookBackendSqliteDB *ebsdb,
                        }
 
                        if (match == MATCH_PHONE_NUMBER) {
-                               extra = g_strdup_printf (" COLLATE ixphone_%d", country_code);
-
                                value = convert_string_value (
                                        ebsdb, query_term_input, region,
                                        CONVERT_NORMALIZE | CONVERT_PHONE, match);
-                       } else {
-                               extra = g_strdup (" COLLATE ixphone_nn");
 
+                               extra = sqlite3_mprintf (" COLLATE ixphone_%d", country_code);
+                       } else {
                                if (match == MATCH_NATIONAL_PHONE_NUMBER) {
                                        value = convert_string_value (
                                                ebsdb, query_term_input, region,
                                                CONVERT_PHONE, MATCH_NATIONAL_PHONE_NUMBER);
+
+                                       extra = sqlite3_mprintf (" COLLATE ixphone_national");
                                } else {
                                        gchar *const digits = extract_digits (query_term_input);
                                        value = convert_string_value (
                                                ebsdb, digits, region,
                                                CONVERT_NOTHING, MATCH_ENDS_WITH);
                                        g_free (digits);
+
+                                       extra = sqlite3_mprintf (
+                                               " AND (%q LIKE '|%%' OR %q LIKE '+%d|%%')",
+                                               field_name, field_name, country_code);
                                }
 
                        }
@@ -3358,6 +3371,8 @@ convert_match_exp (struct _ESExp *f,
 
                                g_free (field_name);
                                g_free (query_term);
+
+                               sqlite3_free (extra_term);
                        }
                }
        }
diff --git a/tests/libebook/client/test-client-custom-summary.c 
b/tests/libebook/client/test-client-custom-summary.c
index 88d2540..cadb398 100644
--- a/tests/libebook/client/test-client-custom-summary.c
+++ b/tests/libebook/client/test-client-custom-summary.c
@@ -338,7 +338,11 @@ main (gint argc,
 #endif
        g_test_init (&argc, &argv, NULL);
 
-       setlocale (LC_ALL, "en_US.UTF-8");
+       /* Change environment so that the addressbook factory inherits this setting */
+       g_setenv ("LC_ALL", "en_US.UTF-8", TRUE);
+       setlocale (LC_ALL, "");
+
+       g_assert_cmpstr (setlocale (LC_ADDRESS, NULL), ==, "en_US.UTF-8");
 
        /* Test all queries in 8 different combinations specified by the 'suites'
         */
@@ -417,7 +421,7 @@ main (gint argc,
                 * matching with EBookBackendSexp when the default summary is used
                 */
                add_client_test (
-                       suites[i].prefix, "/EqPhone/Exact", suites[i].func,
+                       suites[i].prefix, "/EqPhone/Exact/Common", suites[i].func,
                        e_book_query_field_test (E_CONTACT_TEL, E_BOOK_QUERY_EQUALS_PHONE_NUMBER, "+1 
221.542.3789"),
                        1, suites[i].direct, suites[i].custom, TRUE);
 
@@ -446,12 +450,12 @@ main (gint argc,
                 * have the same national number regardless of country codes.
                 *
                 * | Active Country Code: +1 | Query: 221.542.3789 | vCard Data: +1-221-5423789 | Matches: 
yes |
-                * | Active Country Code: +1 | Query: 221.542.3789 | vCard Data: +31-221-5423789 | Matches: 
yes  |
+                * | Active Country Code: +1 | Query: 221.542.3789 | vCard Data: +31-221-5423789 | Matches: 
no  |
                 */
                add_client_test (
-                       suites[i].prefix, "/EqPhone/National", suites[i].func,
+                       suites[i].prefix, "/EqPhone/National/Common", suites[i].func,
                        e_book_query_field_test (E_CONTACT_TEL, E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER, 
"221.542.3789"),
-                       2, suites[i].direct, suites[i].custom, TRUE);
+                       1, suites[i].direct, suites[i].custom, TRUE);
 
                /* Test that a query term with a specified country returns only vCards that 
                 * are specifically in the specified country code.
@@ -483,14 +487,13 @@ main (gint argc,
                 */
                add_client_test (
                        suites[i].prefix, "/EqPhone/National/CountryAbsent/QueryOtherCountry", suites[i].func,
-                       e_book_query_field_test (E_CONTACT_TEL,
-                       E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER, "+49 514-845-8436"),
-                       1, suites[i].direct, suites[i].custom, TRUE);
+                       e_book_query_field_test (E_CONTACT_TEL, E_BOOK_QUERY_EQUALS_NATIONAL_PHONE_NUMBER, 
"+49 514-845-8436"),
+                       0, suites[i].direct, suites[i].custom, TRUE);
 
                add_client_test (
                        suites[i].prefix, "/EqPhone/Short", suites[i].func,
                        e_book_query_field_test (E_CONTACT_TEL, E_BOOK_QUERY_EQUALS_SHORT_PHONE_NUMBER, 
"5423789"),
-                       2, suites[i].direct, suites[i].custom, TRUE);
+                       1, suites[i].direct, suites[i].custom, TRUE);
 
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]