sqlite_orm icon indicating copy to clipboard operation
sqlite_orm copied to clipboard

Case insensitive search COLLATE

Open ElephantInTheRoomFirst opened this issue 9 years ago • 10 comments

I would like to get data from database if where clause is true. But I want the seach as case insensitive. Is there any chance to support COLLATE NOCASE keyword in query? https://stackoverflow.com/questions/973541/how-to-set-sqlite3-to-be-case-insensitive-when-string-comparing

ElephantInTheRoomFirst avatar Nov 18 '17 20:11 ElephantInTheRoomFirst

Hey. Thanks for a great idea. I've been looking for some new features for sqlite_orm. As I understand there are three COLLATE options:

  • COLLATE BINARY
  • COLLATE NOCASE
  • COLLATE RTRIM and one can specify collate option to both select and column schema query. Let me think a little bit more and I'll give you a feedback today-tomorrow. Thanks

fnc12 avatar Nov 19 '17 06:11 fnc12

@Ninja-007 Ok. I looked through available queries with COLLATE postfix operator and this is how it works for me: one can write multiple COLLATE operators (with any of arguments BINARY, NOCASE or RTRIM) in a single query with one COLLATE per one comparison operator. For example:

CREATE TABLE ototo2(t1 text, t2 text);
SELECT * FROM ototo2 WHERE t1 = 'mark' COLLATE NOCASE AND t2 = 'oreily' COLLATE NOCASE;

This query can be mapped to sqlite_orm like this:

storage.get_all<Object>(where(is_equal(&Object::t1, "mark").collate_nocase() and is_equal(&Object::t2, "oreily").collate_nocase());

Using this function with operator== will require one extra parentheses:

storage.get_all<Object>(where((c(&Object::t1) == "mark").collate_nocase() and (c(&Object::t2) == "oreily").collate_nocase());

COLLATE BINARY and COLLATE RTRIM should map similarly: collate_binary() and collate_rtrim().

Second case is schema option (constraint):

CREATE TABLE ototo(t1 TEXT COLLATE NOCASE, t2 TEXT);

This thing would be implemented like this:

auto storage = make_storage("file.sqlite", 
    make_table("objects",
        make_column("t1", &Object::t1, collate_nocase()),
        make_column("t2", &Object::t2)
    )
);

This is how I plan to add COLLATE operator to sqlite_orm. Probably I missed something. Please feel free to add your opinion.

fnc12 avatar Nov 19 '17 13:11 fnc12

Thanks. What about if I just want one query which is case insensitive?

ElephantInTheRoomFirst avatar Nov 20 '17 17:11 ElephantInTheRoomFirst

Yes.

storage.get_all<Object>(where(is_equal(&Object::t1, "mark").collate_nocase());

or

storage.get_all<Object>(where((c(&Object::t1) == "mark").collate_nocase());

Is it ok?

fnc12 avatar Nov 20 '17 17:11 fnc12

It should be OK. Thanks.

ElephantInTheRoomFirst avatar Nov 26 '17 15:11 ElephantInTheRoomFirst

@Ninja-007 thanks for response. I'll ad this functionality in a few days

fnc12 avatar Nov 26 '17 15:11 fnc12

please check out the latest commit and new example. I'm gonna add COLLATE operator as constraint and close the issue afterwards

fnc12 avatar Dec 02 '17 13:12 fnc12

Added COLLATE constraint to the latest commit

fnc12 avatar Dec 24 '17 19:12 fnc12

Oh sh*it. Looks like COLATE can be used right after ORDER BY. I leave it opened until this feature is finished

fnc12 avatar Dec 24 '17 20:12 fnc12

Done in the latest commit.

fnc12 avatar Dec 30 '17 22:12 fnc12