From 1ace521f3f60e26c079b11962f16578ec48c001b Mon Sep 17 00:00:00 2001 From: Guru Sharan Kumar Ram Date: Thu, 18 Sep 2025 00:14:35 +0530 Subject: [PATCH 1/2] fix: remove empty WHERE clause when no conditions exist --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 0fb0894..edf0e54 100644 --- a/index.js +++ b/index.js @@ -192,27 +192,27 @@ class PagiHelp { joinQuery; let replacements = []; - let whereQuery = " WHERE "; + let whereQuery = ""; if (additionalWhereConditions.length > 0) { - whereQuery += this.genSchema(additionalWhereConditions, replacements, true) + " AND "; + whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + this.genSchema(additionalWhereConditions, replacements, true); } if (filterConditions.length > 0) { - whereQuery += this.genSchema(filterConditions, replacements, false) + " AND "; + whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + this.genSchema(filterConditions, replacements, false); } - if(searchColumnList && searchColumnList.length>0 && paginationObject.search !== "") { - whereQuery = whereQuery + "( "; + if(searchColumnList && searchColumnList.length > 0 && paginationObject.search !== "") { + whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + "( "; for (let column of searchColumnList) { - whereQuery = whereQuery + column + " LIKE ? OR "; + whereQuery += column + " LIKE ? OR "; replacements.push(`%${paginationObject.search}%`); } whereQuery = rtrim(whereQuery, "OR "); - whereQuery = whereQuery + " )"; - } else { - whereQuery = rtrim(whereQuery, "AND "); + whereQuery += " )"; } + whereQuery = rtrim(whereQuery, "AND "); + whereQuery = rtrim(whereQuery, "OR "); query = query + whereQuery; countQuery = countQuery + whereQuery; From 2639ef71f6c6e124c2c69a38cb51570f74853be4 Mon Sep 17 00:00:00 2001 From: Guru Sharan Kumar Ram Date: Thu, 18 Sep 2025 04:39:51 +0530 Subject: [PATCH 2/2] refactor: improve space handling for AND/OR in whereQuery --- index.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index edf0e54..efa6fce 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,7 @@ class PagiHelp { returnString += this.tupleCreator(schemaObject, replacements, asItIs) + " AND "; } else { - let subString = "( "; + let subString = "("; for (let subObject of schemaObject) { subString += this.genSchema(subObject, replacements, asItIs) + " OR "; } @@ -184,7 +184,7 @@ class PagiHelp { "`" + joinQuery; - let totalCountQuery = + let totalCountQuery = "SELECT COUNT(*) AS countValue " + " FROM `" + tableName + @@ -202,17 +202,14 @@ class PagiHelp { } if(searchColumnList && searchColumnList.length > 0 && paginationObject.search !== "") { - whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + "( "; + whereQuery += (whereQuery === "" ? " WHERE " : " AND ") + "("; for (let column of searchColumnList) { - whereQuery += column + " LIKE ? OR "; - replacements.push(`%${paginationObject.search}%`); + whereQuery += column + " LIKE ? OR "; + replacements.push(`%${paginationObject.search}%`); } - whereQuery = rtrim(whereQuery, "OR "); - whereQuery += " )"; + whereQuery = whereQuery.replace(/\s*(AND|OR)\s*$/i, ""); + whereQuery += ")"; } - - whereQuery = rtrim(whereQuery, "AND "); - whereQuery = rtrim(whereQuery, "OR "); query = query + whereQuery; countQuery = countQuery + whereQuery;