Skip to content

Commit

Permalink
fix: correctly handle value bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 9, 2024
1 parent 4c158a9 commit 8f05f8c
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-icons-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik-sql-tag-raw": patch
---

correctly handle value binders
76 changes: 38 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions packages/slonik-sql-tag-raw/src/factories/createRaqSqlToken.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { createRaqSqlToken as raw } from './createRaqSqlToken';
import { createTestRunner } from './helpers.test/createTestRunner';
import { createPool, sql } from 'slonik';

const { test } = createTestRunner();

test('constructs raw SQL token', async (t) => {
const pool = await createPool(t.context.dsn);

const rows = await pool.any(sql.unsafe`
SELECT 1 "id"
UNION
${raw('SELECT 2 id')}
`);

t.deepEqual(rows, [
{
id: 1,
},
{
id: 2,
},
]);

await pool.end();
});

test('constructs raw SQL token with value bindings', async (t) => {
const pool = await createPool(t.context.dsn);

const rows = await pool.any(sql.unsafe`
SELECT 1 "id"
UNION
${raw('SELECT $1 id', [2])}
`);

t.deepEqual(rows, [
{
id: 1,
},
{
id: 2,
},
]);

await pool.end();
});

test('constructs raw SQL token with value bindings (offset)', async (t) => {
const pool = await createPool(t.context.dsn);

const rows = await pool.any(sql.unsafe`
SELECT ${1}::int4 "id"
UNION
${raw('SELECT $1 id', [2])}
`);

t.deepEqual(rows, [
{
id: 1,
},
{
id: 2,
},
]);

await pool.end();
});

test('constructs raw SQL token with named value bindings', async (t) => {
const pool = await createPool(t.context.dsn);

const rows = await pool.any(sql.unsafe`
SELECT 1 "id"
UNION
${raw('SELECT :id id', {
id: 2,
})}
`);

t.deepEqual(rows, [
{
id: 1,
},
{
id: 2,
},
]);

await pool.end();
});

test('constructs raw SQL token with named value bindings (offset)', async (t) => {
const pool = await createPool(t.context.dsn);

const rows = await pool.any(sql.unsafe`
SELECT ${1}::int4 "id"
UNION
${raw('SELECT :id id', {
id: 2,
})}
`);

t.deepEqual(rows, [
{
id: 1,
},
{
id: 2,
},
]);

await pool.end();
});
Loading

0 comments on commit 8f05f8c

Please sign in to comment.