import _, { isUndefined } from "lodash"; import sqlGenGrabConcatStr from "./sql-generator-grab-concat-str"; import sqlGenGenJoinStr from "./sql-generator-gen-join-str"; import sqlGenGrabSelectFieldSQL from "./sql-generator-grab-select-field-sql"; import { columnRef, quoteIdentifier, quoteQualified, sqlJoinType, sqlLogicalOperator, } from "./sql-identifier"; export default function sqlGenGenQueryStr(params) { let str = "SELECT"; const genObject = params.genObject; const table_name = params.table_name; const full_text_match_str = params.full_text_match_str; const full_text_search_str = params.full_text_search_str; let sqlSearhValues = []; if (genObject?.select_sql) { str += ` ${genObject.select_sql}`; } else if (genObject?.selectFields?.[0]) { if (genObject.join) { str += sqlGenGrabSelectFieldSQL({ selectFields: genObject.selectFields, append_table_names: true, table_name, }); } else { str += sqlGenGrabSelectFieldSQL({ selectFields: genObject.selectFields, table_name, }); } } else { if (genObject?.join) { str += ` ${table_name}.*`; } else { str += " *"; } } if (genObject?.countSubQueries) { let countSqls = []; for (let i = 0; i < genObject.countSubQueries.length; i++) { const countSubQuery = genObject.countSubQueries[i]; if (!countSubQuery) continue; const tableAlias = countSubQuery.table_alias; const countTableRef = quoteQualified(tableAlias || countSubQuery.table); let subQStr = `(SELECT COUNT(*)`; subQStr += ` FROM ${quoteQualified(countSubQuery.table)}${tableAlias ? ` ${quoteIdentifier(tableAlias)}` : ""}`; subQStr += ` WHERE (`; for (let j = 0; j < countSubQuery.srcTrgMap.length; j++) { const csqSrc = countSubQuery.srcTrgMap[j]; if (!csqSrc) continue; subQStr += ` ${countTableRef}.${quoteIdentifier(csqSrc.src)}`; if (typeof csqSrc.trg == "string") { subQStr += ` = ?`; sqlSearhValues.push(csqSrc.trg); } else if (typeof csqSrc.trg == "object") { subQStr += ` = ${columnRef(csqSrc.trg.field, csqSrc.trg.table)}`; } if (j < countSubQuery.srcTrgMap.length - 1) { subQStr += ` AND `; } } subQStr += ` )) AS ${quoteIdentifier(countSubQuery.alias)}`; countSqls.push(subQStr); } str += `, ${countSqls.join(",")}`; } if (genObject?.join) { const existingJoinTableNames = [table_name]; const joinSelects = genObject.join .flat() .filter((j) => !isUndefined(j)) .map((joinObj) => { const joinTableName = joinObj.alias ? joinObj.alias : joinObj.tableName; const joinTableRef = quoteQualified(joinTableName); if (existingJoinTableNames.includes(joinTableRef)) return null; existingJoinTableNames.push(joinTableRef); if (joinObj.group_concat) { return sqlGenGrabConcatStr({ field: columnRef(joinObj.group_concat.field, joinTableName), alias: joinObj.group_concat.alias, separator: joinObj.group_concat.separator, }); } else if (joinObj.selectFields) { return joinObj.selectFields .map((selectField) => { if (typeof selectField == "string") { return columnRef(selectField, joinTableName); } else if (typeof selectField == "object") { const fieldRef = columnRef(String(selectField.field), joinTableName); let aliasSelectField = fieldRef; if (selectField.count) { aliasSelectField = `COUNT(${fieldRef})`; } else if (selectField.sum) { aliasSelectField = `SUM(${selectField.distinct ? "DISTINCT " : ""}${fieldRef})`; } else if (selectField.average) { aliasSelectField = `AVG(${fieldRef})`; } else if (selectField.max) { aliasSelectField = `MAX(${fieldRef})`; } else if (selectField.min) { aliasSelectField = `MIN(${fieldRef})`; } else if (selectField.group_concat && selectField.alias) { return sqlGenGrabConcatStr({ field: fieldRef, alias: selectField.alias, separator: selectField.group_concat.separator, distinct: selectField.group_concat.distinct, }); } else if (selectField.distinct) { aliasSelectField = `DISTINCT ${fieldRef}`; } if (selectField.alias) aliasSelectField += ` AS ${quoteIdentifier(selectField.alias)}`; return aliasSelectField; } }) .join(","); } else { return `${joinTableRef}.*`; } }) .filter((_) => Boolean(_)); if (joinSelects.length) { str += "," + joinSelects.join(","); } } if (genObject?.fullTextSearch && genObject.fullTextSearch.scoreAlias && full_text_match_str && full_text_search_str) { str += `, ${full_text_match_str} AS ${quoteIdentifier(genObject.fullTextSearch.scoreAlias)}`; sqlSearhValues.push(full_text_search_str); } str += ` FROM ${table_name}`; if (genObject?.join) { str += " " + genObject.join .flat() .filter((j) => !isUndefined(j)) .map((join) => { return (sqlJoinType(join.joinType) + " " + (join.alias ? `${quoteQualified(join.tableName)} ${quoteIdentifier(join.alias)}` : `${quoteQualified(join.tableName)}`) + " ON " + (() => { if (Array.isArray(join.match)) { return ("(" + join.match .filter((mtch) => !_.isUndefined(mtch)) .map((mtch) => { const { str, values } = sqlGenGenJoinStr({ mtch, join, table_name, }); sqlSearhValues.push(...values); return str; }) .join(` ${sqlLogicalOperator(join.operator)} `) + ")"); } else if (typeof join.match == "object") { const { str, values } = sqlGenGenJoinStr({ mtch: join.match, join, table_name, }); sqlSearhValues.push(...values); return str; } })()); }) .join(" "); } return { str, values: sqlSearhValues }; }