Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable raw SQL to be inserted into tagged-template queries #130

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jawj
Copy link
Collaborator

@jawj jawj commented Dec 10, 2024

Enable raw SQL strings to be inserted into tagged-template queries by wrapping them in the sql(...) function, addressing #120.

For example:

const func = 'pi()';
await sql`SELECT ${sql(func)} AS composed_func_result`;

In this implementation, queries with parameters may not be inserted into other queries.

In principle, we could have tagged-template queries be more generally composable — including queries with parameters.

This would be nice to have, but requires more serious refactoring, since in that case the ParameterizedQuery object can't be put together until just before execution (since you don't know what numbers the parameters are going to have).

@jawj jawj requested a review from pffigueiredo December 10, 2024 14:21
@jawj
Copy link
Collaborator Author

jawj commented Dec 10, 2024

(hold on — there's a problem here)

@@ -1,3 +1,5 @@
Insert raw text into tagged-template `sql` queries by wrapping with the `sql()` function (issue #120).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add an "## Unreleased" header to these items

// composed sql for literal strings (SQL injection risk)
const func = 'pi()';
await sql`SELECT ${sql(func)} AS composed_func_result`;
await sql`SELECT ${11} AS eleven, ${sql(func)} AS composed_func_result, ${12} AS twelve`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a test case with a try... catch... using a "Tagged-template query with parameters" to make sure we are handling that right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants