import { LeQuerybuilderAuroraMysqlDataApiError, LeQuerybuilderAuroraMysqlDataApiErrorEnum } from ".."; export interface ContainSelectQuery { selects: QBSelectField[]; } export interface QBSelectField { name: string; type: string; alias?: string; } export interface QBSelectedField { name: string; type: string; } export function select(confobj: ContainSelectQuery, field: QBSelectField) { confobj.selects.push(field); } export function resetSelect(confobj: ContainSelectQuery) { confobj.selects = []; } export function quickSelect(confobj: ContainSelectQuery, name: string, alias?: string) { confobj.selects.push({ alias, name, type: null, }); } export function formatSelect(confobj: ContainSelectQuery) { const selectArr = []; const selectedFields = []; for (const theselect of confobj.selects) { selectArr.push(getSelectStr(theselect)); selectedFields.push({ name: bestAlias(theselect), type: theselect.type, }); } return { str: selectArr.join(", "), selectedFields, }; } function getSelectStr(theselect: QBSelectField) { if (typeof theselect.alias === "string" && theselect.alias !== "") { return theselect.name + " AS `" + theselect.alias + "`"; } return theselect.name; } function bestAlias(theselect: QBSelectField) { if (typeof theselect.alias === "string" && theselect.alias !== "") { return theselect.alias; } const lastsection = theselect.name.split(".").pop(); const result = lastsection.match(/^([a-zA-Z])(\w*)/g); if (result && result.length) { return result[0]; } throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); }