Skip to content

Commit

Permalink
Support multi-line SQL statements (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Feb 5, 2025
1 parent cc91bd9 commit 261e125
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/queries/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const getSyntaxProxySQL = (options: {
});

const statement: Statement = {
statement: text,
// Collapse whitespace and newlines into single spaces, then trim leading or
// trailing spaces.
statement: text.replace(/\s+/g, ' ').trim(),
params,
};

Expand Down
24 changes: 24 additions & 0 deletions tests/queries/statements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,27 @@ test('using raw SQL in batch', async () => {

expect(statementHandlerSpy).not.toHaveBeenCalled();
});

test('using raw SQL with multiple lines', async () => {
let statement: Statement | undefined;

const sqlProxy = getSyntaxProxySQL({
callback: (value) => {
statement = value;
},
});

const accountHandle = 'elaine';

sqlProxy`
UPDATE accounts
SET "points" = 11
WHERE "handle" = ${accountHandle}
RETURNING *
`;

expect(statement).toMatchObject({
statement: 'UPDATE accounts SET "points" = 11 WHERE "handle" = $1 RETURNING *',
params: ['elaine'],
});
});

0 comments on commit 261e125

Please sign in to comment.