Skip to content

Commit f82a6c3

Browse files
committed
feat: soft-split SQL statements at 2+ blank lines when ; is missing
When statements are separated by 2+ blank lines followed by a SQL keyword, auto-split into separate statements even without ; delimiter. Also handles comment lines between blank lines, and suppresses false splits for CTE main queries (WITH ... AS (...) \n\n\n SELECT ...).
1 parent b551ffd commit f82a6c3

2 files changed

Lines changed: 112 additions & 27 deletions

File tree

‎src/composables/useSqlStatements.ts‎

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -322,49 +322,84 @@ function splitRangesAtBlankLines(ranges: StatementRange[], content: string): Sta
322322

323323
let blankRun = 0
324324
let seenContent = false
325-
const splitLineIndices: number[] = []
325+
let inBlockComment = false
326+
// Track the line index of the last actual SQL content (for split boundaries)
327+
let lastContentLine = -1
328+
// When the segment starts with `WITH`, suppress soft-split for the next DML
329+
// keyword — it's the main query of a CTE, not a new statement.
330+
let segmentFirstKeyword: string | null = null
331+
const FIRST_DML_KEYWORDS = new Set(['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'MERGE', 'REPLACE'])
332+
// Each split: { sqlLine: the SQL keyword line, prevContentLine: last content before blank run }
333+
const splits: { sqlLine: number, prevContentLine: number }[] = []
326334

327335
for (let i = 0; i < lines.length; i++) {
328336
const trimmed = lines[i].trim()
329337

338+
// Lines inside a multi-line block comment — skip without resetting blankRun
339+
if (inBlockComment) {
340+
if (trimmed.endsWith('*/'))
341+
inBlockComment = false
342+
continue
343+
}
344+
330345
if (trimmed.length === 0) {
331346
if (seenContent)
332347
blankRun++
348+
continue
333349
}
334-
else {
335-
if (!seenContent) {
336-
seenContent = true
337-
}
338-
else if (blankRun >= 2) {
339-
const firstWord = trimmed.split(/\s+/)[0]?.toUpperCase()
340-
if (firstWord && SOFT_STATEMENT_KEYWORDS.has(firstWord))
341-
splitLineIndices.push(i)
342-
}
343-
blankRun = 0
350+
351+
// Comment-only lines should not reset the blank-run counter,
352+
// so that `SELECT ...\n\n\n-- comment\nSELECT ...` still splits.
353+
if (trimmed.startsWith('--') || trimmed.startsWith('#')) {
354+
continue
344355
}
356+
if (trimmed.startsWith('/*')) {
357+
if (!trimmed.endsWith('*/'))
358+
inBlockComment = true
359+
continue
360+
}
361+
362+
// Actual SQL content
363+
const firstWord = trimmed.split(/\s+/)[0]?.toUpperCase()
364+
if (!seenContent) {
365+
seenContent = true
366+
segmentFirstKeyword = firstWord ?? null
367+
}
368+
else if (blankRun >= 2 && firstWord && SOFT_STATEMENT_KEYWORDS.has(firstWord)) {
369+
// When the segment starts with WITH, the next DML keyword is the main
370+
// query of the CTE (e.g. `WITH cte AS (...) \n\n\n SELECT ...`).
371+
// Splitting there would orphan the CTE definition.
372+
const isCteMainQuery = segmentFirstKeyword === 'WITH' && firstWord && FIRST_DML_KEYWORDS.has(firstWord)
373+
if (!isCteMainQuery)
374+
splits.push({ sqlLine: i, prevContentLine: lastContentLine })
375+
}
376+
blankRun = 0
377+
lastContentLine = i
345378
}
346379

347-
if (splitLineIndices.length === 0) {
380+
if (splits.length === 0) {
348381
result.push(range)
349382
continue
350383
}
351384

352-
let prevIdx = 0
353-
for (const splitIdx of splitLineIndices) {
354-
const segStart = range.startOffset + lineOffsets[prevIdx]
355-
const segEnd = range.startOffset + lineOffsets[splitIdx]
356-
// Trim trailing whitespace so the preceding range ends at its last content
357-
const segText = content.slice(segStart, segEnd).trimEnd()
358-
if (segText.length > 0)
359-
result.push(buildRange(content, segStart, segStart + segText.length))
360-
prevIdx = splitIdx
385+
// Build sub-ranges. Each preceding segment ends at its last content line
386+
// (excluding trailing blank/comment lines). Each new segment starts at the
387+
// SQL keyword line in `splits[].sqlLine`.
388+
let segStart = range.startOffset + lineOffsets[0]
389+
390+
for (const { sqlLine, prevContentLine } of splits) {
391+
// End the preceding segment right after `prevContentLine`
392+
const segEnd = range.startOffset + lineOffsets[prevContentLine] + lines[prevContentLine].length
393+
result.push(buildRange(content, segStart, segEnd))
394+
segStart = range.startOffset + lineOffsets[sqlLine]
361395
}
362-
// Trim leading whitespace on the final segment
363-
const lastStart = range.startOffset + lineOffsets[prevIdx]
364-
const lastText = content.slice(lastStart, range.endOffset).trimStart()
365-
if (lastText.length > 0) {
366-
const trimmed = lastText.length - lastText.trimStart().length
367-
result.push(buildRange(content, lastStart + trimmed, range.endOffset))
396+
397+
// Final segment (trim leading whitespace)
398+
const rawFinal = content.slice(segStart, range.endOffset)
399+
const finalTrimmed = rawFinal.trimStart()
400+
if (finalTrimmed.length > 0) {
401+
const skipped = rawFinal.length - finalTrimmed.length
402+
result.push(buildRange(content, segStart + skipped, range.endOffset))
368403
}
369404
}
370405

‎tests/composables/useSqlStatements.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,56 @@ describe('parseSqlStatements', () => {
215215
expect(result).toHaveLength(1)
216216
})
217217

218+
it('splits when 2 blank lines are followed by a comment then SQL keyword', () => {
219+
const sql = [
220+
'SELECT 1', // line 1
221+
'', // line 2
222+
'', // line 3
223+
'', // line 4
224+
'-- comment', // line 5
225+
'SELECT 2', // line 6
226+
].join('\n')
227+
228+
const result = parseSqlStatements(sql)
229+
expect(result).toHaveLength(2)
230+
expect(result[0].statement).toBe('SELECT 1')
231+
expect(result[1].statement).toBe('SELECT 2')
232+
expect(result[1].position.startLineNumber).toBe(6)
233+
})
234+
235+
it('does not split CTE with blank lines before main SELECT', () => {
236+
const sql = [
237+
'WITH cte AS (', // line 1
238+
' SELECT 1', // line 2
239+
')', // line 3
240+
'', // line 4
241+
'', // line 5
242+
'SELECT * FROM cte', // line 6
243+
].join('\n')
244+
245+
const result = parseSqlStatements(sql)
246+
expect(result).toHaveLength(1)
247+
expect(result[0].statement).toContain('WITH cte AS')
248+
expect(result[0].statement).toContain('SELECT * FROM cte')
249+
})
250+
251+
it('does not split CTE INSERT with blank lines before INSERT', () => {
252+
const sql = [
253+
'WITH cte AS (', // line 1
254+
' SELECT 1', // line 2
255+
')', // line 3
256+
'', // line 4
257+
'', // line 5
258+
'INSERT INTO t', // line 6
259+
'SELECT * FROM cte', // line 7
260+
].join('\n')
261+
262+
const result = parseSqlStatements(sql)
263+
expect(result).toHaveLength(1)
264+
expect(result[0].statement).toContain('WITH cte AS')
265+
expect(result[0].statement).toContain('INSERT INTO t')
266+
})
267+
218268
it('sets correct positions for soft-split statements', () => {
219269
const sql = 'SELECT 1\n\n\nSELECT 2'
220270
const result = parseSqlStatements(sql)

0 commit comments

Comments
 (0)