datasquirel/package-shared/functions/dsql/sql/sql-generator.js
Benjamin Toby a642034eea Updates
2024-12-28 19:27:02 +01:00

268 lines
9.4 KiB
JavaScript

// @ts-check
/**
* # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
* @type {import("../../../types").SqlGeneratorFn}
*/
function sqlGenerator({ tableName, genObject }) {
if (!genObject) return undefined;
const finalQuery = genObject.query ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
/** @type {string[]} */
const sqlSearhValues = [];
/**
* # Generate Query
* @param {object} param
* @param {import("../../../types").ServerQueryQueryObject[string]} param.queryObj
* @param {import("../../../types").ServerQueryParamsJoin[]} [param.join]
* @param {string} [param.field]
*/
function genSqlSrchStr({ queryObj, join, field }) {
const finalFieldName = (() => {
if (queryObj?.tableName) {
return `${queryObj.tableName}.${field}`;
}
if (join) {
return `${tableName}.${field}`;
}
return field;
})();
let str = `${finalFieldName}=?`;
if (
typeof queryObj.value == "string" ||
typeof queryObj.value == "number"
) {
const valueParsed = String(queryObj.value);
if (queryObj.equality == "LIKE") {
str = `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`;
} else if (queryObj.equality == "NOT EQUAL") {
str = `${finalFieldName} != ?`;
sqlSearhValues.push(valueParsed);
} else {
sqlSearhValues.push(valueParsed);
}
} else if (Array.isArray(queryObj.value)) {
/** @type {string[]} */
const strArray = [];
queryObj.value.forEach((val) => {
const valueParsed = val;
if (queryObj.equality == "LIKE") {
strArray.push(
`LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`
);
} else if (queryObj.equality == "NOT EQUAL") {
strArray.push(`${finalFieldName} != ?`);
sqlSearhValues.push(valueParsed);
} else {
strArray.push(`${finalFieldName} = ?`);
sqlSearhValues.push(valueParsed);
}
});
str = "(" + strArray.join(` ${queryObj.operator || "AND"} `) + ")";
}
return str;
}
const sqlSearhString = queryKeys?.map((field) => {
const queryObj =
/** @type {import("../../../types").ServerQueryQueryObject} */ (
finalQuery?.[field]
);
if (!queryObj) return;
if (queryObj.__query) {
const subQueryGroup =
/** @type {import("../../../types").ServerQueryQueryObject}} */ (
queryObj.__query
);
const subSearchKeys = Object.keys(subQueryGroup);
const subSearchString = subSearchKeys.map((_field) => {
const newSubQueryObj = subQueryGroup?.[_field];
return genSqlSrchStr({
queryObj: newSubQueryObj,
field: _field,
join: genObject.join,
});
});
console.log("queryObj.operator", queryObj.operator);
return (
"(" +
subSearchString.join(` ${queryObj.operator || "AND"} `) +
")"
);
}
return genSqlSrchStr({ queryObj, field, join: genObject.join });
});
function generateJoinStr(
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
/** @type {import("../../../types").ServerQueryParamsJoin} */ join
) {
return `${
typeof mtch.source == "object" ? mtch.source.tableName : tableName
}.${
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
}=${(() => {
if (mtch.targetLiteral) {
return `'${mtch.targetLiteral}'`;
}
if (join.alias) {
return `${
typeof mtch.target == "object"
? mtch.target.tableName
: join.alias
}.${
typeof mtch.target == "object"
? mtch.target.fieldName
: mtch.target
}`;
}
return `${
typeof mtch.target == "object"
? mtch.target.tableName
: join.tableName
}.${
typeof mtch.target == "object"
? mtch.target.fieldName
: mtch.target
}`;
})()}`;
}
let queryString = (() => {
let str = "SELECT";
if (genObject.selectFields?.[0]) {
if (genObject.join) {
str += ` ${genObject.selectFields
?.map((fld) => `${tableName}.${fld}`)
.join(",")}`;
} else {
str += ` ${genObject.selectFields?.join(",")}`;
}
} else {
if (genObject.join) {
str += ` ${tableName}.*`;
} else {
str += " *";
}
}
if (genObject.join) {
/** @type {string[]} */
const existingJoinTableNames = [tableName];
str +=
"," +
genObject.join
.map((joinObj) => {
const joinTableName = joinObj.alias
? joinObj.alias
: joinObj.tableName;
if (existingJoinTableNames.includes(joinTableName))
return null;
existingJoinTableNames.push(joinTableName);
if (joinObj.selectFields) {
return joinObj.selectFields
.map((selectField) => {
if (typeof selectField == "string") {
return `${joinTableName}.${selectField}`;
} else if (typeof selectField == "object") {
let aliasSelectField = selectField.count
? `COUNT(${joinTableName}.${selectField.field})`
: `${joinTableName}.${selectField.field}`;
if (selectField.alias)
aliasSelectField += ` AS ${selectField.alias}`;
return aliasSelectField;
}
})
.join(",");
} else {
return `${joinTableName}.*`;
}
})
.filter((_) => Boolean(_))
.join(",");
}
str += ` FROM ${tableName}`;
if (genObject.join) {
str +=
" " +
genObject.join
.map((join) => {
return (
join.joinType +
" " +
(join.alias
? join.tableName + " " + join.alias
: join.tableName) +
" ON " +
(() => {
if (Array.isArray(join.match)) {
return (
"(" +
join.match
.map((mtch) =>
generateJoinStr(mtch, join)
)
.join(
join.operator
? ` ${join.operator} `
: " AND "
) +
")"
);
} else if (typeof join.match == "object") {
return generateJoinStr(join.match, join);
}
})()
);
})
.join(" ");
}
return str;
})();
if (sqlSearhString?.[0] && sqlSearhString.find((str) => str)) {
const stringOperator = genObject?.searchOperator || "AND";
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)} `;
}
if (genObject.order)
queryString += ` ORDER BY ${
genObject.join
? `${tableName}.${genObject.order.field}`
: genObject.order.field
} ${genObject.order.strategy}`;
if (genObject.limit) queryString += ` LIMIT ${genObject.limit}`;
if (genObject.offset) queryString += ` OFFSET ${genObject.offset}`;
return {
string: queryString,
values: sqlSearhValues,
};
}
module.exports = sqlGenerator;