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];
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)

View File

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

View File

@ -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;
}

View File

@ -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": {