Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whereILike issue with sqlite (#5604) #5687

Merged
7 changes: 7 additions & 0 deletions lib/dialects/sqlite3/query/sqlite-querycompiler.js
Original file line number Diff line number Diff line change
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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add test for the not branch too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused, if I understood correctly the _not should add a not to the sql statement
to use it with an ilike query with sqlite or any other dialect it has to look like

knex('table').whereNot('col', 'ilike', 'test_string').select();

Since there's no a whereNotILike function, what the test should be?

Copy link
Collaborator

@kibertoad kibertoad Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to set that "not" flag currently at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this query? no I kept it in case there will be a support for functions like whereNotILike

statement,
'like '
)}${this._valueClause(statement)}`;
}
}

module.exports = QueryCompiler_SQLite3;
24 changes: 22 additions & 2 deletions test/integration2/query/select/where.spec.js
Original file line number Diff line number Diff line change
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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comment that sqlite only supports case-insensitive search

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would split this into 2 tests, as the test name no longer represents the test behavior when in SQLite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done @rluvaton

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
Original file line number Diff line number Diff line change
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
Loading