diff --git a/src/factories/createSqlTag.js b/src/factories/createSqlTag.js index 84b619ec..3c5347bd 100644 --- a/src/factories/createSqlTag.js +++ b/src/factories/createSqlTag.js @@ -29,9 +29,6 @@ import { SqlToken, UnnestToken, } from '../tokens'; -import { - InvalidInputError, -} from '../errors'; import createSqlTokenSqlFragment from './createSqlTokenSqlFragment'; const log = Logger.child({ @@ -80,14 +77,6 @@ export default () => { } } - if (rawSql.trim() === '') { - throw new InvalidInputError('Unexpected SQL input. Query cannot be empty.'); - } - - if (rawSql.trim() === '$1') { - throw new InvalidInputError('Unexpected SQL input. Query cannot be empty. Found only value binding.'); - } - const query = deepFreeze({ sql: rawSql, type: SqlToken, diff --git a/src/routines/executeQuery.js b/src/routines/executeQuery.js index 02570ede..18ebb97f 100644 --- a/src/routines/executeQuery.js +++ b/src/routines/executeQuery.js @@ -13,6 +13,7 @@ import { BackendTerminatedError, CheckIntegrityConstraintViolationError, ForeignKeyIntegrityConstraintViolationError, + InvalidInputError, NotNullIntegrityConstraintViolationError, QueryCancelledError, UnexpectedStateError, @@ -51,6 +52,14 @@ export default async ( throw new UnexpectedStateError('Cannot use terminated connection.'); } + if (rawSql.trim() === '') { + throw new InvalidInputError('Unexpected SQL input. Query cannot be empty.'); + } + + if (rawSql.trim() === '$1') { + throw new InvalidInputError('Unexpected SQL input. Query cannot be empty. Found only value binding.'); + } + const queryInputTime = process.hrtime.bigint(); let stackTrace = null; diff --git a/test/slonik/routines/executeQuery.js b/test/slonik/routines/executeQuery.js new file mode 100644 index 00000000..2a92e617 --- /dev/null +++ b/test/slonik/routines/executeQuery.js @@ -0,0 +1,61 @@ +// @flow + +import Roarr from 'roarr'; +import test, { + beforeEach, +} from 'ava'; +import executeQuery from '../../../src/routines/executeQuery'; +import { + InvalidInputError, +} from '../../../src/errors'; +import createClientConfiguration from '../../helpers/createClientConfiguration'; + +const createConnectionStub = () => { + return { + connection: { + slonik: { + terminated: false, + }, + }, + }; +}; + +beforeEach((t) => { + t.context.logger = Roarr; + t.context.connection = createConnectionStub(); + t.context.executionRoutine = () => {}; +}); + +test('throws a descriptive error if query is empty', async (t) => { + const error = await t.throwsAsync(() => { + return executeQuery( + t.context.logger, + t.context.connection, + createClientConfiguration(), + '', + [], + 'foo', + t.context.executionRoutine + ); + }); + + t.assert(error instanceof InvalidInputError); + t.assert(error.message === 'Unexpected SQL input. Query cannot be empty.'); +}); + +test('throws a descriptive error if the entire query is a value binding', async (t) => { + const error = await t.throwsAsync(() => { + return executeQuery( + t.context.logger, + t.context.connection, + createClientConfiguration(), + '$1', + [], + 'foo', + t.context.executionRoutine + ); + }); + + t.assert(error instanceof InvalidInputError); + t.assert(error.message === 'Unexpected SQL input. Query cannot be empty. Found only value binding.'); +}); diff --git a/test/slonik/templateTags/sql/sql.js b/test/slonik/templateTags/sql/sql.js index 1b71c4cc..e5385073 100644 --- a/test/slonik/templateTags/sql/sql.js +++ b/test/slonik/templateTags/sql/sql.js @@ -5,9 +5,6 @@ import createSqlTag from '../../../../src/factories/createSqlTag'; import { SqlToken, } from '../../../../src/tokens'; -import { - InvalidInputError, -} from '../../../../src/errors'; const sql = createSqlTag(); @@ -21,24 +18,6 @@ test('creates an object describing a query', (t) => { }); }); -test('throws a descriptive error if query is empty', (t) => { - const error = t.throws(() => { - sql``; - }); - - t.assert(error instanceof InvalidInputError); - t.assert(error.message === 'Unexpected SQL input. Query cannot be empty.'); -}); - -test('throws a descriptive error if the entire query is a value binding', (t) => { - const error = t.throws(() => { - sql`${1}`; - }); - - t.assert(error instanceof InvalidInputError); - t.assert(error.message === 'Unexpected SQL input. Query cannot be empty. Found only value binding.'); -}); - test('creates an object describing query value bindings', (t) => { const query = sql`SELECT ${'foo'}`;