Reference · Database
sql
sql(str) -> strWhat it does
The text as it is. Written as a tag on a template, sql`...`, it tells editors to
highlight the text as SQL. It changes nothing about the query: values still go in as
? parameters, and quillo check still warns when a value is built into SQL with
${...} or + on its way to db.query, db.one or db.exec. Braces in the
template are plain text; ${...} fills in values.
Examples
let q = sql`select * from posts where id = ?`print(q)print(sql("select 1"))select * from posts where id = ?select 1Edge cases
- The result is a plain string, equal to the text written: nothing is parsed, quoted or escaped.
- A value filled in with
${...}is part of the text, as in any template; a table's or a column's name goes in with sql_name, a named parameter with sql_param. - An empty template gives the empty string.
print(sql`select 1` == "select 1")let table = sql_name("posts")print(sql`select * from ${table} order by id`)print(len(sql``))trueselect * from "posts" order by id0Errors
A value that is not a string is a runtime error, not an error value. The message
names css, the tag it shares its code with.
print(sql(42))line 1: `css` expects a str as argument 1, got intChaining
The string goes to the database like any other; the tag reads well with the query in a variable first.
let q = sql`select * from posts where author = ? order by created desc`let posts = db.query(q, [author])let one = sql`select * from posts where id = ?` | db.one([id])See also
sql_name, sql_param, db.query, css
Thread 0 posts
On sql(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo