Skip to content
Snippets Groups Projects
Unverified Commit 634508a3 authored by Ryan Bianchi's avatar Ryan Bianchi Committed by GitHub
Browse files

update sql regexp to default to empty string when field is null (#3480)

parent ec55e8dc
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
......@@ -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', () => {
......@@ -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');
});
});
......@@ -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) {
......
---
category: Bugfix
authors: [qedi-r]
---
Fix 'matches' operator incorrectly matching empty strings.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment