From 7da2e04cca7b9777385725270768269d85551689 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Fri, 8 Nov 2024 21:44:24 +0100 Subject: [PATCH] SQL Query Generator Function Update --- functions/sql/sql-generator.js | 25 ++++++++++++++--------- package-shared/types/index.d.ts | 1 + package-shared/utils/backend/grabDbSSL.js | 6 +++--- package.json | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/functions/sql/sql-generator.js b/functions/sql/sql-generator.js index c21a928..d91a7f9 100644 --- a/functions/sql/sql-generator.js +++ b/functions/sql/sql-generator.js @@ -18,7 +18,17 @@ function sqlGenerator({ tableName, genObject }) { const queryObj = finalQuery?.[field]; if (!queryObj) return; - let str = `${field}=?`; + const finalFieldName = (() => { + if (queryObj?.tableName) { + return `${queryObj.tableName}.${field}`; + } + if (genObject.join) { + return `${tableName}.${field}`; + } + return field; + })(); + + let str = `${finalFieldName}=?`; if ( typeof queryObj.value == "string" || @@ -26,7 +36,7 @@ function sqlGenerator({ tableName, genObject }) { ) { const valueParsed = String(queryObj.value); if (queryObj.equality == "LIKE") { - str = `LOWER(${field}) LIKE LOWER('%${valueParsed}%')`; + str = `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`; } else { sqlSearhValues.push(valueParsed); } @@ -37,10 +47,10 @@ function sqlGenerator({ tableName, genObject }) { const valueParsed = val; if (queryObj.equality == "LIKE") { strArray.push( - `LOWER(${field}) LIKE LOWER('%${valueParsed}%')` + `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')` ); } else { - strArray.push(`${field} = ?`); + strArray.push(`${finalFieldName} = ?`); sqlSearhValues.push(valueParsed); } }); @@ -148,12 +158,7 @@ function sqlGenerator({ tableName, genObject }) { if (sqlSearhString) { const stringOperator = genObject?.searchOperator || "AND"; - queryString += ` WHERE ${sqlSearhString - .map((str) => { - if (genObject.join) return `${tableName}.${str}`; - return str; - }) - .join(` ${stringOperator} `)} `; + queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)} `; } if (genObject.order) diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index 6ce59c9..320da78 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -1158,6 +1158,7 @@ export type ServerQueryQueryObject = { value: string | string[]; operator?: "AND" | "OR"; equality?: "EQUAL" | "LIKE"; + tableName?: string; }; }; diff --git a/package-shared/utils/backend/grabDbSSL.js b/package-shared/utils/backend/grabDbSSL.js index 1a79911..b4b5b2f 100644 --- a/package-shared/utils/backend/grabDbSSL.js +++ b/package-shared/utils/backend/grabDbSSL.js @@ -8,9 +8,9 @@ const fs = require("fs"); module.exports = function grabDbSSL() { const SSL_DIR = process.env.DSQL_SSL_DIR; if (!SSL_DIR?.match(/./)) { - console.log( - "No SSL certificate provided. Query will run in normal mode. To add SSL add an env path dir `DSQL_SSL_DIR` with a file named `ca-cert.pem`" - ); + // console.log( + // "No SSL certificate provided. Query will run in normal mode. To add SSL add an env path dir `DSQL_SSL_DIR` with a file named `ca-cert.pem`" + // ); return undefined; } diff --git a/package.json b/package.json index 4f9cbf6..c84378b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "2.5.2", + "version": "2.5.3", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {