Reference · Database

sql

signature
sql(str) -> str

What 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"))
output
select * from posts where id = ?select 1

Edge cases

print(sql`select 1` == "select 1")let table = sql_name("posts")print(sql`select * from ${table} order by id`)print(len(sql``))
output
trueselect * from "posts" order by id0

Errors

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.

this one fails
print(sql(42))
output
line 1: `css` expects a str as argument 1, got int

Chaining

The string goes to the database like any other; the tag reads well with the query in a variable first.

in a program
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
10Useful? Like it, or add to its thread below.

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