import { QueryBuilderBaseParamEnum, WhereQueryEnum } from "../enums"; import { RDSDataService } from "aws-sdk"; import { LeQuerybuilderAuroraMysqlDataApiError, LeQuerybuilderAuroraMysqlDataApiErrorEnum } from "../error"; export interface ContainWhereQuery { wheres: WhereQuery[]; } export interface WhereQuery { str?: string; type: WhereQueryEnum; params?: WhereQueryParams[]; } export interface QueryBuilderWhereFieldBlob { type: QueryBuilderBaseParamEnum.Blob; val: RDSDataService._Blob; } export interface QueryBuilderWhereFieldString { type: QueryBuilderBaseParamEnum.String; val: RDSDataService.String; } export interface QueryBuilderWhereFieldDouble { type: QueryBuilderBaseParamEnum.Double; val: RDSDataService.BoxedDouble; } export interface QueryBuilderWhereFieldLong { type: QueryBuilderBaseParamEnum.Long; val: RDSDataService.BoxedLong; } export interface QueryBuilderWhereFieldBoolean { val: RDSDataService.BoxedBoolean; type: QueryBuilderBaseParamEnum.Boolean; } export type WhereQueryParams = QueryBuilderWhereFieldBlob | QueryBuilderWhereFieldString | QueryBuilderWhereFieldDouble | QueryBuilderWhereFieldLong | QueryBuilderWhereFieldBoolean; function validateWhereStr(str: string, params: WhereQueryParams[]) { const match = str.match(/(\:\?)/g) || []; if (match.length !== params.length) { throw new LeQuerybuilderAuroraMysqlDataApiError(LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } } export function where(conf: ContainWhereQuery, str: string, params: WhereQueryParams[]) { validateWhereStr(str, params); conf.wheres.push({ params: [...params], str, type: WhereQueryEnum.AndWhere, }); // } export function andWhere(conf: ContainWhereQuery, str: string, params: WhereQueryParams[]) { return where(conf, str, params); // } export function orWhere(conf: ContainWhereQuery, str: string, params: WhereQueryParams[]) { validateWhereStr(str, params); conf.wheres.push({ params: [...params], str, type: WhereQueryEnum.OrWhere, }); } export function andWhereOpen(conf: ContainWhereQuery) { conf.wheres.push({ params: [], str: "", type: WhereQueryEnum.AndWhereOpen, }); } export function orWhereOpen(conf: ContainWhereQuery) { conf.wheres.push({ params: [], str: "", type: WhereQueryEnum.OrWhereOpen, }); } export function whereClose(conf: ContainWhereQuery) { conf.wheres.push({ params: [], str: "", type: WhereQueryEnum.Close, }); } export function formatWhere(conf: ContainWhereQuery) { let prevOp: WhereQueryEnum = null; const strArr: string[] = []; const params: WhereQueryParams[] = []; let openCount = 0; for (const thewhere of conf.wheres) { const validPrev = [WhereQueryEnum.AndWhere, WhereQueryEnum.OrWhere].indexOf(prevOp) !== -1; switch (thewhere.type) { case WhereQueryEnum.AndWhereOpen: openCount++; strArr.push("AND ("); break; case WhereQueryEnum.OrWhereOpen: openCount++; strArr.push("OR ("); break; case WhereQueryEnum.Close: openCount--; strArr.push(")"); break; case WhereQueryEnum.AndWhere: if (validPrev) { strArr.push("AND"); } strArr.push(thewhere.str); for (const p of thewhere.params) { params.push(p); } break; case WhereQueryEnum.OrWhere: if (validPrev) { strArr.push("OR"); } strArr.push(thewhere.str); for (const p of thewhere.params) { params.push(p); } break; } prevOp = thewhere.type; } if (openCount !== 0) { throw new LeQuerybuilderAuroraMysqlDataApiError( LeQuerybuilderAuroraMysqlDataApiErrorEnum.FormatError); } return { str: strArr.join(" "), params, }; }