Skip to content

Commit dfd9575

Browse files
committed
feat: allow empty sql tags (fixes #93)
1 parent fcb6488 commit dfd9575

File tree

4 files changed

+70
-32
lines changed

4 files changed

+70
-32
lines changed

src/factories/createSqlTag.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ import {
2929
SqlToken,
3030
UnnestToken,
3131
} from '../tokens';
32-
import {
33-
InvalidInputError,
34-
} from '../errors';
3532
import createSqlTokenSqlFragment from './createSqlTokenSqlFragment';
3633

3734
const log = Logger.child({
@@ -80,14 +77,6 @@ export default () => {
8077
}
8178
}
8279

83-
if (rawSql.trim() === '') {
84-
throw new InvalidInputError('Unexpected SQL input. Query cannot be empty.');
85-
}
86-
87-
if (rawSql.trim() === '$1') {
88-
throw new InvalidInputError('Unexpected SQL input. Query cannot be empty. Found only value binding.');
89-
}
90-
9180
const query = deepFreeze({
9281
sql: rawSql,
9382
type: SqlToken,

src/routines/executeQuery.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
BackendTerminatedError,
1414
CheckIntegrityConstraintViolationError,
1515
ForeignKeyIntegrityConstraintViolationError,
16+
InvalidInputError,
1617
NotNullIntegrityConstraintViolationError,
1718
QueryCancelledError,
1819
UnexpectedStateError,
@@ -51,6 +52,14 @@ export default async (
5152
throw new UnexpectedStateError('Cannot use terminated connection.');
5253
}
5354

55+
if (rawSql.trim() === '') {
56+
throw new InvalidInputError('Unexpected SQL input. Query cannot be empty.');
57+
}
58+
59+
if (rawSql.trim() === '$1') {
60+
throw new InvalidInputError('Unexpected SQL input. Query cannot be empty. Found only value binding.');
61+
}
62+
5463
const queryInputTime = process.hrtime.bigint();
5564

5665
let stackTrace = null;

test/slonik/routines/executeQuery.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// @flow
2+
3+
import Roarr from 'roarr';
4+
import test, {
5+
beforeEach,
6+
} from 'ava';
7+
import executeQuery from '../../../src/routines/executeQuery';
8+
import {
9+
InvalidInputError,
10+
} from '../../../src/errors';
11+
import createClientConfiguration from '../../helpers/createClientConfiguration';
12+
13+
const createConnectionStub = () => {
14+
return {
15+
connection: {
16+
slonik: {
17+
terminated: false,
18+
},
19+
},
20+
};
21+
};
22+
23+
beforeEach((t) => {
24+
t.context.logger = Roarr;
25+
t.context.connection = createConnectionStub();
26+
t.context.executionRoutine = () => {};
27+
});
28+
29+
test('throws a descriptive error if query is empty', async (t) => {
30+
const error = await t.throwsAsync(() => {
31+
return executeQuery(
32+
t.context.logger,
33+
t.context.connection,
34+
createClientConfiguration(),
35+
'',
36+
[],
37+
'foo',
38+
t.context.executionRoutine
39+
);
40+
});
41+
42+
t.assert(error instanceof InvalidInputError);
43+
t.assert(error.message === 'Unexpected SQL input. Query cannot be empty.');
44+
});
45+
46+
test('throws a descriptive error if the entire query is a value binding', async (t) => {
47+
const error = await t.throwsAsync(() => {
48+
return executeQuery(
49+
t.context.logger,
50+
t.context.connection,
51+
createClientConfiguration(),
52+
'$1',
53+
[],
54+
'foo',
55+
t.context.executionRoutine
56+
);
57+
});
58+
59+
t.assert(error instanceof InvalidInputError);
60+
t.assert(error.message === 'Unexpected SQL input. Query cannot be empty. Found only value binding.');
61+
});

test/slonik/templateTags/sql/sql.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import createSqlTag from '../../../../src/factories/createSqlTag';
55
import {
66
SqlToken,
77
} from '../../../../src/tokens';
8-
import {
9-
InvalidInputError,
10-
} from '../../../../src/errors';
118

129
const sql = createSqlTag();
1310

@@ -21,24 +18,6 @@ test('creates an object describing a query', (t) => {
2118
});
2219
});
2320

24-
test('throws a descriptive error if query is empty', (t) => {
25-
const error = t.throws(() => {
26-
sql``;
27-
});
28-
29-
t.assert(error instanceof InvalidInputError);
30-
t.assert(error.message === 'Unexpected SQL input. Query cannot be empty.');
31-
});
32-
33-
test('throws a descriptive error if the entire query is a value binding', (t) => {
34-
const error = t.throws(() => {
35-
sql`${1}`;
36-
});
37-
38-
t.assert(error instanceof InvalidInputError);
39-
t.assert(error.message === 'Unexpected SQL input. Query cannot be empty. Found only value binding.');
40-
});
41-
4221
test('creates an object describing query value bindings', (t) => {
4322
const query = sql`SELECT ${'foo'}`;
4423

0 commit comments

Comments
 (0)