Skip to content

Commit

Permalink
update sql regexp to default to empty string when field is null (actu…
Browse files Browse the repository at this point in the history
  • Loading branch information
qedi-r authored and a-gradina committed Sep 24, 2024
1 parent 6582d12 commit eb65c58
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function asyncTransaction(
}

function regexp(regex: string, text: string | null) {
return new RegExp(regex).test(text) ? 1 : 0;
return new RegExp(regex).test(text || '') ? 1 : 0;
}

export function openDatabase(pathOrBuffer: string | Buffer) {
Expand Down
22 changes: 22 additions & 0 deletions packages/loot-core/src/platform/server/sqlite/index.web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ beforeAll(() => {

const initSQL = `
CREATE TABLE numbers (id TEXT PRIMARY KEY, number INTEGER);
CREATE TABLE textstrings (id TEXT PRIMARY KEY, string TEXT);
`;

describe('Web sqlite', () => {
Expand Down Expand Up @@ -85,4 +86,25 @@ describe('Web sqlite', () => {
// @ts-expect-error Property 'number' does not exist on type 'unknown'
expect(rows[2].number).toBe(6);
});

it('should match regex on text fields', async () => {
const db = await openDatabase();
execQuery(db, initSQL);

runQuery(
db,
"INSERT INTO textstrings (id, string) VALUES ('id1', 'not empty string')",
);
runQuery(db, "INSERT INTO textstrings (id) VALUES ('id2')");

const rows = runQuery(
db,
'SELECT id FROM textstrings where REGEXP("n.", string)',
null,
true,
);
expect(rows.length).toBe(1);
// @ts-expect-error Property 'id' does not exist on type 'unknown'
expect(rows[0].id).toBe('id1');
});
});
2 changes: 1 addition & 1 deletion packages/loot-core/src/platform/server/sqlite/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export async function asyncTransaction(db: Database, fn: () => Promise<void>) {
}

function regexp(regex: string, text: string) {
return new RegExp(regex).test(text) ? 1 : 0;
return new RegExp(regex).test(text || '') ? 1 : 0;
}

export async function openDatabase(pathOrBuffer?: string | Buffer) {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3480.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [qedi-r]
---

Fix 'matches' operator incorrectly matching empty strings.

0 comments on commit eb65c58

Please sign in to comment.