Reference · Database

sql_name

signature
sql_name(str | list) -> str

What it does

A table's or a column's name, checked and quoted, for SQL that cannot take it as a ? parameter: "title", or `title` when the program's database is MySQL. A list of names gives them separated by commas. A name is letters, digits and _, not starting with a digit and at most 63 bytes long; anything else is refused, so a name made from data (sql_name(`obj_${id}`)) cannot carry SQL into the query, and quillo check does not warn about SQL built with it.

Examples

print(sql_name("title"))print(sql_name(["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", "body"insert into posts ("title", "body") values (:title, :body)

Edge cases

print(sql_name("_x1"), sql_name("obj_42"))print(len(sql_name([])))let long = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijk"print(len(long), len(sql_name(long)))print(sql_name("名前"), sql_name("prénom"), sql_name("नाम"))
output
"_x1" "obj_42"063 65"名前" "prénom" "नाम"

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: the request stops instead of running the SQL.

this one fails
print(sql_name("posts; drop table posts"))
output
line 1: `sql_name`: "posts; drop table posts" is not a name SQL can take (letters, digits and _)
this one fails
print(sql_name(["ok", "1abc"]))
output
line 1: `sql_name`: "1abc" is not a name SQL can take (letters, digits and _)
this one fails
print(sql_name(repeat("a", 64)))
output
line 1: `sql_name`: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" is not a name SQL can take (at most 63 bytes)
this one fails
print(sql_name(42))
output
line 1: `sql_name` takes a name (a str) or a list of them, got int

Chaining

print(["id", "name"] | sql_name)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
"id", "name"update posts set "title" = :title, "body" = :body where id = :id
in a program
fn table_of(id: int) = sql_name(`obj_${id}`)let rows = db.query(`select * from ${table_of(id)} order by id`)

See also

db.sql_name, sql_param, sql, keys, db.query

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

Thread 0 posts

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