Reference · Database

sql_param

signature
sql_param(str | list) -> str

What it does

A named SQL parameter, :title, checked: the name is letters, digits and _ (of any script, as sql_name takes them), not starting with a digit and at most 63 bytes long. A list of names gives them separated by commas, :title, :body, for an insert ... values (...) whose values come from a map. quillo check does not warn about SQL built with it.

Examples

print(sql_param("title"))print(sql_param(["title", "body"]))let cols = ["title", "body"]                       // the program's list, not the visitor'sprint(`insert into posts (${sql_name(cols)}) values (${sql_param(cols)})`)
output
:title:title, :bodyinsert into posts ("title", "body") values (:title, :body)

Edge cases

print(sql_param("_x1"), sql_param("A9"))print(len(sql_param([])))print(sql_param(keys({ b: 1, a: 2 })))
output
:_x1 :A90:b, :a

Errors

A name with anything but letters, digits and _, an empty one, one starting with a digit, one over 63 bytes, or a value that is not a string (or a list of strings) is a runtime error, not an error value.

this one fails
print(sql_param("a-b"))
output
line 1: `sql_param`: "a-b" is not a name SQL can take (letters, digits and _)
this one fails
print(sql_param(null))
output
line 1: `sql_param` takes a name (a str) or a list of them, got null

Chaining

let cols = ["title", "body"]let set = cols | map((c) => `${sql_name(c)} = ${sql_param(c)}`) | join(", ")print(`update posts set ${set} where id = :id`)
output
update posts set "title" = :title, "body" = :body where id = :id

An update takes the pairs, which every database takes; a column list, set (a, b) = (...), is not MySQL's, nor PostgreSQL's for one column. quillo check follows map and join over sql_name and sql_param, through every value the function can give, so the db.exec below gets no warning; a raw ${c} in that function still does. It trusts only the builtins: a function or a parameter of the program's own named sql_name, map or join is not taken for them.

in a program
let row = { title: "Hello", body: "...", id: 7 }db.exec(`update posts set ${set} where id = :id`, row)

See also

sql_name, sql, keys, db.exec

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On sql_param(): 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