From 89ac89bb0ca2bf030847dac76ba2290740686a50 Mon Sep 17 00:00:00 2001 From: killa Date: Thu, 10 Oct 2024 15:32:38 +0800 Subject: [PATCH] chore: make generateSql async (#248) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change ## Summary by CodeRabbit - **New Features** - Enhanced SQL generation process with asynchronous handling for improved performance and reliability. - **Bug Fixes** - Ensured that all SQL generation operations complete before executing dependent database queries, preventing potential timing issues. --- core/dal-runtime/src/DataSource.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/dal-runtime/src/DataSource.ts b/core/dal-runtime/src/DataSource.ts index 61412eb1..d6abd88d 100644 --- a/core/dal-runtime/src/DataSource.ts +++ b/core/dal-runtime/src/DataSource.ts @@ -28,7 +28,7 @@ export class DataSource implements IDataSource { * @param sqlName * @param data */ - generateSql(sqlName: string, data: object): ExecuteSql { + async generateSql(sqlName: string, data: object): Promise { const sql = this.sqlMap.generate(sqlName, data, this.mysqlDataSource.timezone!); const sqlType = this.sqlMap.getType(sqlName); const template = this.sqlMap.getTemplateString(sqlName); @@ -41,12 +41,12 @@ export class DataSource implements IDataSource { async count(sqlName: string, data?: any): Promise { const newData = Object.assign({ $$count: true }, data); - const executeSql = this.generateSql(sqlName, newData); + const executeSql = await this.generateSql(sqlName, newData); return await this.#paginateCount(executeSql.sql); } async execute(sqlName: string, data?: any): Promise> { - const executeSql = this.generateSql(sqlName, data); + const executeSql = await this.generateSql(sqlName, data); const rows = await this.mysqlDataSource.query(executeSql.sql); return rows.map(t => { return TableModelInstanceBuilder.buildInstance(this.tableModel, t); @@ -54,7 +54,7 @@ export class DataSource implements IDataSource { } async executeRaw(sqlName: string, data?: any): Promise> { - const executeSql = this.generateSql(sqlName, data); + const executeSql = await this.generateSql(sqlName, data); return await this.mysqlDataSource.query(executeSql.sql); } @@ -72,8 +72,8 @@ export class DataSource implements IDataSource { async paginate(sqlName: string, data: any, currentPage: number, perPageCount: number): Promise> { const limit = `LIMIT ${(currentPage - 1) * perPageCount}, ${perPageCount}`; - const sql = this.generateSql(sqlName, data).sql + ' ' + limit; - const countSql = this.generateSql(sqlName, Object.assign({ $$count: true }, data)).sql; + const sql = (await this.generateSql(sqlName, data)).sql + ' ' + limit; + const countSql = (await this.generateSql(sqlName, Object.assign({ $$count: true }, data))).sql; const ret = await Promise.all([