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

Allow to build complex queries by bypass SqlStatement instance as parameter #30

Open
Strate opened this issue Mar 11, 2017 · 6 comments

Comments

@Strate
Copy link

Strate commented Mar 11, 2017

Seems that it is not possible right now to do something like this:

import {SQL} from "sql-template-strings"

function prepareSet(values) { /* implementation skipped */}

pg.query(SQL`UPDATE ${SQL("table")} SET ${SQL(prepareSet({key: value})} WHERE id = ${id}`)

I mean that if parameter is already instance of SQLStatement, it could be inserted to query as-is (with possible merging nested parameters).

What do you think about it?

@felixfbecker
Copy link
Owner

This functionality is provided with .append(). I think passing template strings into template strings is confusing tbh.

@Strate
Copy link
Author

Strate commented Mar 12, 2017

As for me, this one:

pg.query(SQL`UPDATE ${SQL("table")} SET ${SQL(prepareSet({key: value})} WHERE id = ${id}`)

is less confusing than

pg.query(SQL`UPDATE `.append("table SET").append(prepareSet({key: value})).append(SQL` WHERE id = ${id}`)

btw, you can choose the way as you want, I suggest just to allow passing SQLStatement as template variable.

Would you accept PR?

@felixfbecker
Copy link
Owner

I guess why not, as long as it has test cases covering all the cases where both statements have values and not

@felixfbecker
Copy link
Owner

Btw, SQL("table") doesn't actually work. The first parameter to a template string tag is an array.

@Janpot
Copy link

Janpot commented Jan 30, 2018

Maybe you could draw inspiration from pg-template-tag? This one allows nesting. Also the immutable nature makes it feel more like working with a string, which comes more natural to me.

WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
@WebReflection
Copy link
Contributor

WebReflection commented Mar 1, 2019

My pull request would allow the following:

// example (no idea what prepareSet would do)
const prepareSet = obj => SQL`preparedName = ${obj.key}`;

// auto back-tick escaping of single/double quoted parts + auto merge of values
SQL`UPDATE '${`table_${name}`}' SET ${prepareSet({
  key: value
})} WHERE id = ${id}`;

Hope that works better for this case too 👋

WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
WebReflection pushed a commit to WebReflection/node-sql-template-strings that referenced this issue Mar 1, 2019
This MR addresses all concerns and optimizations reised in felixfbecker#30 and felixfbecker#44

  * SQLStatement can be used as value
  * raw names can be passed via `'${'table_' + name}'` with `'` or `"` transform
  * `.append(...)` doesn't need a space at the beginning, it's handled automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants