Updates
This commit is contained in:
@@ -2,6 +2,7 @@ import { ServerQueryParam } from "../../../types";
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
};
|
||||
type Return = {
|
||||
string: string;
|
||||
@@ -11,5 +12,5 @@ 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;
|
||||
export {};
|
||||
|
||||
+18
-16
@@ -5,7 +5,7 @@ exports.default = sqlGenerator;
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
function sqlGenerator({ tableName, genObject }) {
|
||||
function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
if (!genObject)
|
||||
return undefined;
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
@@ -17,10 +17,10 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
function genSqlSrchStr({ queryObj, join, field, }) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj === null || queryObj === void 0 ? void 0 : queryObj.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${tableName}.${field}`;
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@@ -87,18 +87,18 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
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}=${(() => {
|
||||
return `${dbFullName}.${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"
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
: mtch.target}`;
|
||||
}
|
||||
return `${typeof mtch.target == "object"
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
@@ -110,7 +110,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
let str = "SELECT";
|
||||
if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${tableName}.${fld}`).join(",")}`;
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${dbFullName}.${tableName}.${fld}`).join(",")}`;
|
||||
}
|
||||
else {
|
||||
str += ` ${(_c = genObject.selectFields) === null || _c === void 0 ? void 0 : _c.join(",")}`;
|
||||
@@ -118,7 +118,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
}
|
||||
else {
|
||||
if (genObject.join) {
|
||||
str += ` ${tableName}.*`;
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
}
|
||||
else {
|
||||
str += " *";
|
||||
@@ -141,12 +141,12 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
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;
|
||||
@@ -155,13 +155,13 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
.join(",");
|
||||
}
|
||||
else {
|
||||
return `${joinTableName}.*`;
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
str += ` FROM ${tableName}`;
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
" " +
|
||||
@@ -170,8 +170,10 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
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)) {
|
||||
@@ -198,7 +200,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
}
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${genObject.join
|
||||
? `${tableName}.${genObject.order.field}`
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field} ${genObject.order.strategy}`;
|
||||
if (genObject.limit)
|
||||
queryString += ` LIMIT ${genObject.limit}`;
|
||||
|
||||
Reference in New Issue
Block a user