-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathhelper.ts
More file actions
109 lines (94 loc) · 3.23 KB
/
helper.ts
File metadata and controls
109 lines (94 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import fs from 'fs';
import path from 'path';
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') ||
tmp.includes('BEGIN STATEMENT SET;') ||
tmp.includes('BEGIN');
if (!isMulti) {
// 非批量的先简单按照分号切割
if (tmp.endsWith(';')) {
result.push(tmp.trim());
tmp = '';
}
} else {
if (tmp.endsWith('END;')) {
result.push(tmp.trim());
tmp = '';
}
}
}
return result;
};
/**
* Read a sql string with special range
* @param range line and column start from 1
*/
export const readSQLByRange = (
sqlSource: {
dirname?: string;
fileName?: string;
sql?: string;
},
range: { startLine: number; endLine: number; startColumn?: number; endColumn?: number }
) => {
const { dirname, fileName, sql } = sqlSource;
const { startLine, endLine, startColumn, endColumn } = range;
if (endLine < startLine) throw new RangeError('endLine must greater or equal than startLine!');
if (!sql && (!dirname || !fileName))
throw new Error('A sql input or file info params is required!');
const content =
sql !== undefined
? sql
: fs.readFileSync(path.join(dirname, 'fixtures', fileName), 'utf-8');
let index = 0;
let middleText = '';
let startLineText = '';
let endLineText = '';
let currLine = 1;
while (index < content.length && currLine <= endLine) {
const char = content[index];
if (char === '\n') {
currLine++;
}
if (currLine === startLine) {
// The line break at the beginning needs to be discarded.
if (!(char === '\n' && startLineText === '')) {
startLineText += char;
}
} else if (
currLine > startLine &&
(currLine < endLine || (currLine === endLine && char === '\n'))
) {
middleText += char;
} else if (currLine === endLine && startLine !== endLine) {
endLineText += char;
}
index++;
}
startLineText = startLineText.slice(
startColumn !== undefined ? startColumn - 1 : 0,
endLine === startLine && endColumn !== undefined ? endColumn - 1 : undefined
);
endLineText = endLineText.slice(0, endColumn !== undefined ? endColumn - 1 : undefined);
return startLineText + middleText + endLineText;
};
export function commentOtherLine(sqlContent: string, line: number[] | number) {
const slices = sqlContent.split('\n').map((item, index) => {
if (
(Array.isArray(line) && (index + 1 < line[0] || index + 1 > line[1])) ||
(typeof line === 'number' && index + 1 !== line)
) {
return '-- ' + item;
} else {
return item;
}
});
return slices.join('\n');
}