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

SQL Template Strings #15

Closed
knpwrs opened this issue Dec 30, 2019 · 3 comments
Closed

SQL Template Strings #15

knpwrs opened this issue Dec 30, 2019 · 3 comments
Labels
question Further information is requested

Comments

@knpwrs
Copy link

knpwrs commented Dec 30, 2019

One of my favorite libraries from the node world is sql-template-strings. It allows users to take something like this:

pg.query('SELECT author FROM books WHERE name = $1 AND author = $2', [book, author])

And instead write this:

pg.query(SQL`SELECT author FROM books WHERE name = ${book} AND author = ${author}`)

Notice the placement of the book and author variables -- they are placed inline inside the query. sql-template-strings handles creating a prepared statement and ordering the parameters for query execution.

sqlx reminds me a lot of the first snippet, with the primary additional benefit being that we can get type-safety inferred from the query (which on its own is very cool!). I don't know a lot about Rust macros, but I was wondering if it would be possible to take the sqlx macro a step further and additionally implement sql-template-strings-style inline parameters.

Edit:

Here's another example from the sql-template-strings README showing the benefit when a lot of parameters are used:

db.query(
  'INSERT INTO books (name, author, isbn, category, recommended_age, pages, price) VALUES (?, ?, ?, ?, ?, ?, ?)',
  [name, author, isbn, category, recommendedAge, pages, price]
)
// is better written as
db.query(SQL`
  INSERT
  INTO    books
          (name, author, isbn, category, recommended_age, pages, price)
  VALUES  (${name}, ${author}, ${isbn}, ${category}, ${recommendedAge}, ${pages}, ${price})
`)
@mehcode
Copy link
Member

mehcode commented Dec 30, 2019

I currently would like to do a bit in this area. Several things I'd like to "solve" ( some only in the macro variant ) about bind parameters.

The next step is to figure out an ergonomic method for batch insertions..

@mehcode mehcode added the question Further information is requested label Dec 31, 2019
@scull7
Copy link

scull7 commented May 8, 2020

Are only the positional "?" parameters supported at this point?

Ah, sorry I did not see the following issue:
#199

@mehcode
Copy link
Member

mehcode commented Jun 10, 2020

I'm going to close this for now. We've since decided to not implicitly touch the query string. So unfortunately stuff like I mentioned in the above comment isn't directly possible.


We feel that placeholder ( ?, @p1, $1 ) abstraction is the job of a query builder or ORM that may go on top of SQLx.

With that said, we are planning on adding a low-level dynamic query builder in the near future that would allow this.


I do want to explore query builders at some future but that's not on even our long-term roadmap which is why I'm closing this issue. I do still like the idea expressed in the opening. I feel it might be cool for someone to do a Maud ( https://maud.lambda.xyz/ ) style DSL using SQLx which can do stuff like that.

@mehcode mehcode closed this as completed Jun 10, 2020
losfair pushed a commit to losfair/sqlx that referenced this issue Oct 1, 2022
* Add support for full Unicode character set

* Add server tests for unicode support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants