Skip to content

Commit

Permalink
Fix whereILike issue with sqlite (#5604) (#5687)
Browse files Browse the repository at this point in the history
Co-authored-by: Raz Luvaton <16746759+rluvaton@users.noreply.github.com>
  • Loading branch information
mohammedbalila and rluvaton committed Apr 7, 2024
1 parent aedba5e commit 9659a20
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/dialects/sqlite3/query/sqlite-querycompiler.js
Expand Up @@ -329,6 +329,13 @@ class QueryCompiler_SQLite3 extends QueryCompiler {
onJsonPathEquals(clause) {
return this._onJsonPathEquals('json_extract', clause);
}

whereILike(statement) {
return `${this._columnClause(statement)} ${this._not(
statement,
'like '
)}${this._valueClause(statement)}`;
}
}

module.exports = QueryCompiler_SQLite3;
24 changes: 22 additions & 2 deletions test/integration2/query/select/where.spec.js
Expand Up @@ -556,7 +556,14 @@ describe('Where', function () {

describe('where like', async function () {
beforeEach(function () {
if (!(isPostgreSQL(knex) || isMssql(knex) || isMysql(knex))) {
if (
!(
isPostgreSQL(knex) ||
isSQLite(knex) ||
isMssql(knex) ||
isMysql(knex)
)
) {
return this.skip();
}
});
Expand Down Expand Up @@ -630,10 +637,23 @@ describe('Where', function () {
expect(result[7].email).to.equal('test8@example.com');
});

it("doesn't find data using whereLike when different case sensitivity", async () => {
it("doesn't find data using whereLike when different case sensitivity", async function () {
const result = await knex('accounts').whereLike('email', 'Test1%');
// sqlite only supports case-insensitive search
if (isSQLite(knex)) {
this.skip();
}
expect(result).to.deep.equal([]);
});

it('supports only case-insensitive searches in sqlite', async function () {
const result = await knex('accounts').whereILike('email', 'Test1%');
if (!isSQLite(knex)) {
this.skip();
}
expect(result.length).to.equal(1);
expect(result[0].email).to.equal('test1@example.com');
});
});

it('Retains array bindings, #228', function () {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -879,6 +879,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] where [name] collate SQL_Latin1_General_CP1_CI_AS like ?',
bindings: ['luk%'],
},
sqlite3: {
sql: 'select * from `users` where `name` like ?',
bindings: ['luk%'],
},
});
});

Expand All @@ -903,6 +907,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] where [name] collate SQL_Latin1_General_CP1_CS_AS like ? and [name] collate SQL_Latin1_General_CP1_CS_AS like ? or [name] collate SQL_Latin1_General_CP1_CS_AS like ?',
bindings: ['luk1%', 'luk2%', 'luk3%'],
},
sqlite3: {
sql: 'select * from `users` where `name` like ? and `name` like ? or `name` like ?',
bindings: ['luk1%', 'luk2%', 'luk3%'],
},
}
);
});
Expand All @@ -928,6 +936,10 @@ describe('QueryBuilder', () => {
sql: 'select * from [users] where [name] collate SQL_Latin1_General_CP1_CI_AS like ? and [name] collate SQL_Latin1_General_CP1_CI_AS like ? or [name] collate SQL_Latin1_General_CP1_CI_AS like ?',
bindings: ['luk1%', 'luk2%', 'luk3%'],
},
sqlite3: {
sql: 'select * from `users` where `name` like ? and `name` like ? or `name` like ?',
bindings: ['luk1%', 'luk2%', 'luk3%'],
},
}
);
});
Expand Down

0 comments on commit 9659a20

Please sign in to comment.