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



commit b9b1cf2985aa662d5ad9d1cf13a5f88f51ebfcbc
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        |   42 +++++++++++++------
 tests/libebook/client/test-client-custom-summary.c |   18 +++++---
 2 files changed, 40 insertions(+), 20 deletions(-)
---
diff --git a/addressbook/libedata-book/e-book-backend-sqlitedb.c 
b/addressbook/libedata-book/e-book-backend-sqlitedb.c
index b6311b1..4b785fd 100644
--- a/addressbook/libedata-book/e-book-backend-sqlitedb.c
+++ b/addressbook/libedata-book/e-book-backend-sqlitedb.c
@@ -1122,6 +1122,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,11 +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) {
+                       cmp = e_strcmp2n (country_code, 2, 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 ()) {
@@ -1184,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) {
@@ -3167,24 +3177,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);
                                }
 
                        }
@@ -3354,6 +3368,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 3e569fb..83ceef1 100644
--- a/tests/libebook/client/test-client-custom-summary.c
+++ b/tests/libebook/client/test-client-custom-summary.c
@@ -330,7 +330,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'
         */
@@ -398,7 +402,7 @@ main (gint argc,
                /* These queries will do an index lookup with a custom summary, and a full table scan
                 * matching with EBookBackendSexp when the default summary is used
                 */
-               add_client_test (suites[i].prefix, "/EqPhone/Exact", suites[i].func,
+               add_client_test (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);
 
@@ -426,11 +430,11 @@ 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,
+               add_client_test (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.
@@ -462,11 +466,11 @@ 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);
+                                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]