Skip to content

Commit 2c74336

Browse files
parseWhere of PuddySqlTags updated.
1 parent 7af0fc2 commit 2c74336

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/PuddySqlTags.mjs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,36 @@ class PuddySqlTags {
441441
const include = group.include;
442442

443443
/**
444-
* @param {boolean} not
445-
* @param {string} param
446-
* @param {boolean} [useLike=false]
447-
* @returns {string}
444+
* Generates a subquery for checking tag inclusion/exclusion.
445+
*
446+
* @param {boolean} not - Whether the condition is a negation (NOT).
447+
* @param {string} param - The parameter placeholder (e.g., `$1`, `$2`, etc.).
448+
* @param {boolean} [useLike=false] - Whether to use `LIKE` instead of `=`.
449+
* @returns {string} The SQL snippet for the tag existence check.
450+
* @throws {Error} If SQLite mode is active and `tagsTable` is not defined.
448451
*/
449-
const createQuery = (not, param, useLike = false) =>
452+
const createQuery = (not, param, useLike = false) => {
450453
// Sqlite3
451-
!this.#isPgMode
452-
? `${not ? 'NOT ' : ''}EXISTS (SELECT 1 FROM ${
453-
this.#useJsonEach
454-
? `${this.#jsonEach}(${tagsColumn}) WHERE value ${useLike ? 'LIKE' : '='} ${param}`
455-
: `${tagsColumn} WHERE ${tagsTable}.${tagsColumn} ${useLike ? 'LIKE' : '='} ${param}`
456-
})`
457-
: // Postgre
458-
`${not ? 'NOT (' : ''}${param} = ANY(${tagsColumn})${not ? ')' : ''}`;
454+
if (!this.#isPgMode) {
455+
let result;
456+
if (this.#useJsonEach) {
457+
result = `${this.#jsonEach}(${tagsColumn}) WHERE value ${useLike ? 'LIKE' : '='} ${param}`;
458+
} else {
459+
if (typeof tagsTable !== 'string' || tagsTable.trim() === '') {
460+
throw new Error(
461+
`Missing or invalid 'tagsTable'. Expected a non-empty string when using SQLite mode without json_each.`,
462+
);
463+
}
464+
result = `${tagsColumn} WHERE ${tagsTable}.${tagsColumn} ${useLike ? 'LIKE' : '='} ${param}`;
465+
}
466+
467+
return `${not ? 'NOT ' : ''}EXISTS (SELECT 1 FROM ${result})`;
468+
}
469+
// PostgreSQL
470+
else {
471+
return `${not ? 'NOT (' : ''}${param} = ANY(${tagsColumn})${not ? ')' : ''}`;
472+
}
473+
};
459474

460475
/**
461476
* @param {string} tag

0 commit comments

Comments
 (0)