Skip to content

Commit

Permalink
Merge pull request #226 from liam-hq/comment-with-semicolon
Browse files Browse the repository at this point in the history
Fix handling of `--` comment lines with semicolons in `processSQLInChunks`
  • Loading branch information
MH4GF authored Dec 12, 2024
2 parents 210ca57 + 93de611 commit 217e2de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,18 @@ describe(processSQLInChunks, () => {

expect(callback).not.toHaveBeenCalled()
})

it('should correctly process SQL chunks while ignoring semicolons in comment lines starting with "--"', async () => {
const input =
'SELECT 1;\nSELECT 2;\n-- This is a comment line; additional text here should be ignored.\nSELECT 3;\nSELECT 4;'
const chunkSize = 3
const callback = vi.fn()

await processSQLInChunks(input, chunkSize, callback)

expect(callback).toHaveBeenCalledTimes(2)
expect(callback).toHaveBeenCalledWith('SELECT 1;\nSELECT 2;\nSELECT 3;')
expect(callback).toHaveBeenCalledWith('SELECT 4;')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ export const processSQLInChunks = async (
chunkSize: number,
callback: (chunk: string) => Promise<void>,
): Promise<void> => {
const lines = input.split('\n')
const semicolon = ';'
// Even though the parser can handle "--", we remove such lines for ease of splitting by semicolons.
const lines = input.split('\n').filter((line) => !line.startsWith('--'))

let partialStmt = ''

for (let i = 0; i < lines.length; i += chunkSize) {
const chunk = lines.slice(i, i + chunkSize).join('\n')
const combined = partialStmt + chunk

const lastSemicolonIndex = combined.lastIndexOf(';')
const lastSemicolonIndex = combined.lastIndexOf(semicolon)
if (lastSemicolonIndex === -1) {
partialStmt = combined
continue
Expand Down

0 comments on commit 217e2de

Please sign in to comment.