This commit is contained in:
Benjamin Toby
2025-01-14 16:50:40 +01:00
parent 9cd2707157
commit 82152082f3
6 changed files with 46 additions and 36 deletions
@@ -7,6 +7,7 @@ import {
type Param = {
genObject?: ServerQueryParam;
tableName: string;
dbFullName: string;
};
type Return =
@@ -20,7 +21,11 @@ type Return =
* # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
*/
export default function sqlGenerator({ tableName, genObject }: Param): Return {
export default function sqlGenerator({
tableName,
genObject,
dbFullName,
}: Param): Return {
if (!genObject) return undefined;
const finalQuery = genObject.query ? genObject.query : undefined;
@@ -43,10 +48,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
}) {
const finalFieldName = (() => {
if (queryObj?.tableName) {
return `${queryObj.tableName}.${field}`;
return `${dbFullName}.${queryObj.tableName}.${field}`;
}
if (join) {
return `${tableName}.${field}`;
return `${dbFullName}.${tableName}.${field}`;
}
return field;
})();
@@ -128,7 +133,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin
) {
return `${
return `${dbFullName}.${
typeof mtch.source == "object" ? mtch.source.tableName : tableName
}.${
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
@@ -138,7 +143,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
}
if (join.alias) {
return `${
return `${dbFullName}.${
typeof mtch.target == "object"
? mtch.target.tableName
: join.alias
@@ -149,7 +154,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
}`;
}
return `${
return `${dbFullName}.${
typeof mtch.target == "object"
? mtch.target.tableName
: join.tableName
@@ -166,14 +171,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
if (genObject.selectFields?.[0]) {
if (genObject.join) {
str += ` ${genObject.selectFields
?.map((fld) => `${tableName}.${fld}`)
?.map((fld) => `${dbFullName}.${tableName}.${fld}`)
.join(",")}`;
} else {
str += ` ${genObject.selectFields?.join(",")}`;
}
} else {
if (genObject.join) {
str += ` ${tableName}.*`;
str += ` ${dbFullName}.${tableName}.*`;
} else {
str += " *";
}
@@ -199,11 +204,11 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
return joinObj.selectFields
.map((selectField) => {
if (typeof selectField == "string") {
return `${joinTableName}.${selectField}`;
return `${dbFullName}.${joinTableName}.${selectField}`;
} else if (typeof selectField == "object") {
let aliasSelectField = selectField.count
? `COUNT(${joinTableName}.${selectField.field})`
: `${joinTableName}.${selectField.field}`;
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
: `${dbFullName}.${joinTableName}.${selectField.field}`;
if (selectField.alias)
aliasSelectField += ` AS ${selectField.alias}`;
return aliasSelectField;
@@ -211,14 +216,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
})
.join(",");
} else {
return `${joinTableName}.*`;
return `${dbFullName}.${joinTableName}.*`;
}
})
.filter((_) => Boolean(_))
.join(",");
}
str += ` FROM ${tableName}`;
str += ` FROM ${dbFullName}.${tableName}`;
if (genObject.join) {
str +=
@@ -229,8 +234,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
join.joinType +
" " +
(join.alias
? join.tableName + " " + join.alias
: join.tableName) +
? `${dbFullName}.${join.tableName}` +
" " +
join.alias
: `${dbFullName}.${join.tableName}`) +
" ON " +
(() => {
if (Array.isArray(join.match)) {
@@ -267,7 +274,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
if (genObject.order)
queryString += ` ORDER BY ${
genObject.join
? `${tableName}.${genObject.order.field}`
? `${dbFullName}.${tableName}.${genObject.order.field}`
: genObject.order.field
} ${genObject.order.strategy}`;