import { LeQuerybuilderAuroraMysqlDataApiError, LeQuerybuilderAuroraMysqlDataApiErrorEnum } from "../error"; import { andWhere, formatWhere, where, WhereQuery, WhereQueryParams } from "./where-query"; export interface ContainJoinableTable { tables: JoinableTable[]; } export interface JoinableTable { alias?: string; name: string; schema?: string; joins: JoinableTableJoinTo[]; } export interface JoinableTableJoinTo { alias?: string; name: string; schema?: string; joinQueryEnum: JoinQueryEnum; wheres: WhereQuery[]; } export interface ContainJoinQuery { tables: JoinableTable[]; } export enum JoinQueryEnum { LeftJoin, RightJoin, } export interface JoinQuery { str?: string; type: JoinQueryEnum; } function getTableName(thetable: { alias?: string; name: string; schema?: string; }): string { let val = thetable.name; if (typeof thetable.schema === "string" && thetable.schema !== "") { val = thetable.schema + "." + val; } if (typeof thetable.alias === "string" && thetable.alias !== "") { val = val + " AS " + thetable.alias; } return val; } export function table(confobj: ContainJoinableTable, tableName: string, alias: string = "") { if (confobj.tables.length !== 0) { throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } confobj.tables.push({ alias, joins: [], name: tableName, schema: "", }); } export function into(confobj: ContainJoinableTable, tableName: string) { return table(confobj, tableName); } function join(confobj: ContainJoinableTable, tableName: string, alias: string = "", joinQueryEnum: JoinQueryEnum) { if (confobj.tables.length === 0) { throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } const lastTableJoins = confobj.tables[confobj.tables.length - 1].joins; lastTableJoins.push({ alias, joinQueryEnum, name: tableName, schema: "", wheres: [], }); } export function leftJoin(confobj: ContainJoinableTable, tableName: string, alias: string = "") { return join(confobj, tableName, alias, JoinQueryEnum.LeftJoin); } export function rightJoin(confobj: ContainJoinableTable, tableName: string, alias: string = "") { return join(confobj, tableName, alias, JoinQueryEnum.RightJoin); } export function on(confobj: ContainJoinableTable, str: string, params: WhereQueryParams[]= []) { if (confobj.tables.length === 0) { throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } const lastTableJoins = confobj.tables[confobj.tables.length - 1].joins; const lastJoin = lastTableJoins[lastTableJoins.length - 1]; andWhere(lastJoin, str, params); } export function formatJoinableTables(confobj: ContainJoinableTable) { if (confobj.tables.length === 0) { throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } const tableArr = []; const params: WhereQueryParams[] = []; const baseTable = confobj.tables[0]; tableArr.push(getTableName(baseTable)); for (const joinTable of baseTable.joins) { if (joinTable.wheres.length === 0) { throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } switch (joinTable.joinQueryEnum) { case (JoinQueryEnum.LeftJoin): tableArr.push("LEFT JOIN"); break; case (JoinQueryEnum.RightJoin): tableArr.push("RIGHT JOIN"); break; } tableArr.push(getTableName(joinTable)); tableArr.push("ON"); const whereResult = formatWhere(joinTable); tableArr.push(whereResult.str); for (const p of whereResult.params) { params.push(p); } } return { str: tableArr.join(" "), params, }; }