From c41beeaa5a0b5dd5e11c626d468f35f1ee2e5722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E8=BF=82=E8=BF=82?= Date: Thu, 11 May 2023 19:31:03 +0800 Subject: [PATCH] refactor: readSQL support multiple tables (#95) --- test/helper.ts | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/test/helper.ts b/test/helper.ts index 1062e67..ef59b07 100644 --- a/test/helper.ts +++ b/test/helper.ts @@ -1,10 +1,30 @@ import fs from 'fs'; import path from 'path'; -export const readSQL = (dirname: string, fileName: string, isSegment = true) => { - const sqlFiles = fs.readFileSync(path.join(dirname, 'fixtures', fileName), 'utf-8') - if (!isSegment) return [sqlFiles]; - return sqlFiles.split(';') - .filter(Boolean) - .map((i) => i.trim()); -} \ No newline at end of file +export const readSQL = (dirname: string, fileName: string) => { + const content = fs.readFileSync(path.join(dirname, 'fixtures', fileName), 'utf-8'); + const result: string[] = []; + let tmp = ''; + + for (let index = 0; index < content.length; index++) { + const char = content[index]; + tmp += char; + + const isMulti = tmp.includes('EXECUTE STATEMENT SET'); + + if (!isMulti) { + // 非批量的先简单按照分号切割 + if (tmp.endsWith(';')) { + result.push(tmp.trim()); + tmp = ''; + } + } else { + if (tmp.endsWith('END;')) { + result.push(tmp.trim()); + tmp = ''; + } + } + } + + return result; +};