#1 2016-01-26 12:58:18

dolfin
Member
Registered: 2015-09-11
Posts: 22

Case insensitive sql like function

Hi!

Is there any way to force like active case-insesitivly?
I have a REST server and trying to perform such request fom ajax client:
http://localhost:8080/root/guest/?select=*&where=Name like '%a%'
and I want to get both "...a..." and "...A..." values in the results.
How can I achieve this?

Offline

#2 2016-01-26 13:16:38

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Case insensitive sql like function

Use the debugger in TSQLRestServer.URI to find out why the WHERE clause is not taken in account.
Perhaps there is an encoding problem at client side.

Offline

#3 2016-01-26 14:09:20

dolfin
Member
Registered: 2015-09-11
Posts: 22

Re: Case insensitive sql like function

I played a little bit more with where clause and found out that the problem happens only with cyrillic characters. For latin characters everything works fine. So comparison for letters 'я' and 'Я' is different.
How can I deal with this issue?

Offline

#4 2016-01-26 14:12:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Case insensitive sql like function

AFAIK this is a SQLite3 known limitation.
See https://www.sqlite.org/faq.html#q18

The idea may be to use your own SQL function.
See http://synopse.info/files/html/Synopse% … ml#TITL_22
You may define Unicode-aware uppercase function for instance to SQlite3.pas.

Offline

#5 2016-01-26 14:52:19

dolfin
Member
Registered: 2015-09-11
Posts: 22

Re: Case insensitive sql like function

Thanks for the idea. But how I will force the framework to use my funciton instead of builtin one.
I'd not want to change source files.

Offline

#6 2016-01-27 18:00:48

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Case insensitive sql like function

This is a pain in every database (case-insensitive for Cyrillic letters).
For Oracle we execute statements:

ALTER SESSION SET NLS_COMP=LINGUISTIC; ALTER SESSION SET NLS_SORT=BINARY_CI;

and create functional indexes

For Postgre fallback to ILIKE operator.

For SQLite3 there is a good article here (Russian  language) with Delphi samples. The main idea is to redefine LIKE function using sqlite3_create_function_v2() as AB note above

Last edited by mpv (2016-01-27 18:58:02)

Offline

#7 2016-03-04 19:32:11

pszawa
Member
Registered: 2014-06-05
Posts: 10

Re: Case insensitive sql like function

I had a similar problem with Polish letters. I solved this by modifying sqlite3.c - I extend sqlite3UpperToLower array (to handle all Polish characters just add about 120 items) and changed:


# define GlobUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }

to:

# define GlobUpperToLower(A)   if( !((A)&~0x17f) ){ A = sqlite3UpperToLower[A]; }

previously I had such a solution like this:

static u32 tryPolishLetters(u32 A) {
    switch(A) {        
        case 0x17B: // Ż
            return 0x17C;
        case 0x179: // Ź
            return 0x17A;
        case 0x15A: // Ś
            return 0x15B;
        case 0xD3:  // Ó
            return 0xF3;
        case 0x143: // Ń
            return 0x144;
        case 0x141: // Ł
            return 0x142;
        case 0x118: // Ę
            return 0x119;
        case 0x106: // Ć
            return 0x107;
        case 0x104: // Ą
            return 0x105;
    }
    return A;
}

#if defined(SQLITE_EBCDIC)
# define sqlite3Utf8Read(A)    (*((*A)++))
# define GlobUpperToLower(A)   A = sqlite3UpperToLower[A]
#else
# define GlobUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; } else { A = tryPolishLetters(A); }
#endif

but i think the first is better.

It may not be the best solution, but it works smile

Offline

#8 2016-03-05 08:51:03

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Case insensitive sql like function

AFAIR you could use your own SQL function for comparisons and even indexes, in latest version of SQLite3.
See https://www.sqlite.org/c3ref/create_collation.html and https://www.sqlite.org/syntax/indexed-column.html

Offline

#9 2016-03-05 18:29:48

pszawa
Member
Registered: 2014-06-05
Posts: 10

Re: Case insensitive sql like function

but it seems to me that this has no effect on the behavior of the LIKE operator, and it can only be changed by modifying the database engine

Offline

Board footer

Powered by FluxBB