SQL Query Generator Function Update

This commit is contained in:
Benjamin Toby 2024-11-08 21:44:24 +01:00
parent 9142f3b3e0
commit 7da2e04cca
4 changed files with 20 additions and 14 deletions

View File

@ -18,7 +18,17 @@ function sqlGenerator({ tableName, genObject }) {
const queryObj = finalQuery?.[field]; const queryObj = finalQuery?.[field];
if (!queryObj) return; 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 ( if (
typeof queryObj.value == "string" || typeof queryObj.value == "string" ||
@ -26,7 +36,7 @@ function sqlGenerator({ tableName, genObject }) {
) { ) {
const valueParsed = String(queryObj.value); const valueParsed = String(queryObj.value);
if (queryObj.equality == "LIKE") { if (queryObj.equality == "LIKE") {
str = `LOWER(${field}) LIKE LOWER('%${valueParsed}%')`; str = `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`;
} else { } else {
sqlSearhValues.push(valueParsed); sqlSearhValues.push(valueParsed);
} }
@ -37,10 +47,10 @@ function sqlGenerator({ tableName, genObject }) {
const valueParsed = val; const valueParsed = val;
if (queryObj.equality == "LIKE") { if (queryObj.equality == "LIKE") {
strArray.push( strArray.push(
`LOWER(${field}) LIKE LOWER('%${valueParsed}%')` `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`
); );
} else { } else {
strArray.push(`${field} = ?`); strArray.push(`${finalFieldName} = ?`);
sqlSearhValues.push(valueParsed); sqlSearhValues.push(valueParsed);
} }
}); });
@ -148,12 +158,7 @@ function sqlGenerator({ tableName, genObject }) {
if (sqlSearhString) { if (sqlSearhString) {
const stringOperator = genObject?.searchOperator || "AND"; const stringOperator = genObject?.searchOperator || "AND";
queryString += ` WHERE ${sqlSearhString queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)} `;
.map((str) => {
if (genObject.join) return `${tableName}.${str}`;
return str;
})
.join(` ${stringOperator} `)} `;
} }
if (genObject.order) if (genObject.order)

View File

@ -1158,6 +1158,7 @@ export type ServerQueryQueryObject = {
value: string | string[]; value: string | string[];
operator?: "AND" | "OR"; operator?: "AND" | "OR";
equality?: "EQUAL" | "LIKE"; equality?: "EQUAL" | "LIKE";
tableName?: string;
}; };
}; };

View File

@ -8,9 +8,9 @@ const fs = require("fs");
module.exports = function grabDbSSL() { module.exports = function grabDbSSL() {
const SSL_DIR = process.env.DSQL_SSL_DIR; const SSL_DIR = process.env.DSQL_SSL_DIR;
if (!SSL_DIR?.match(/./)) { if (!SSL_DIR?.match(/./)) {
console.log( // 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`" // "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; return undefined;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "datasquirel", "name": "datasquirel",
"version": "2.5.2", "version": "2.5.3",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "index.js", "main": "index.js",
"bin": { "bin": {