Case insensitive search COLLATE
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
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 BINARYCOLLATE NOCASECOLLATE RTRIMand 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
@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.
Thanks. What about if I just want one query which is case insensitive?
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?
It should be OK. Thanks.
@Ninja-007 thanks for response. I'll ad this functionality in a few days
please check out the latest commit and new example.
I'm gonna add COLLATE operator as constraint and close the issue afterwards
Added COLLATE constraint to the latest commit
Oh sh*it. Looks like COLATE can be used right after ORDER BY. I leave it opened until this feature is finished
Done in the latest commit.