Reference · Database

db.sql_name

signature
db.sql_name(str | list) -> str

What it does

A table's or a column's name, checked and quoted for the database it is called on, for SQL that cannot take it as a ? parameter. On a connection from db.connect, shop.sql_name(col) quotes as that database reads names; db.sql_name(col) quotes for the program's database, as sql_name does. MySQL gets backticks (`title`), since it reads "title" as text; SQLite and PostgreSQL get double quotes. A list of names gives them separated by commas. The names it takes are those sql_name takes: letters, digits (of any script) and _, not starting with a digit, at most 63 bytes. It asks the database nothing, so it needs no effect, and quillo check does not warn about SQL built with it.

Examples

let shop = db.connect("shop.db")shop.exec("create table items (id integer, title text)")shop.exec("insert into items values (1, 'Lamp'), (2, 'Desk')")let col = "title"print(shop.sql_name(col), db.sql_name(col))print(shop.query(`select ${shop.sql_name(col)} from items order by ${shop.sql_name("id")}`))
output
"title" "title"[{ title: "Lamp" }, { title: "Desk" }]
in a program
db.open("app.db")                                  // the program's: SQLitelet shop = db.connect(env("SHOP_URL"))             // mysql://...let col = "title"print(sql_name(col), shop.sql_name(col))           // "title" `title`shop.query(`select ${shop.sql_name(col)} from items`)

Edge cases

let shop = db.connect("shop.db")print(shop.sql_name(["id", "title"]))print(len(shop.sql_name([])))fn cols() with time = shop.sql_name(["a", "b"])print(cols())
output
"id", "title"0"a", "b"

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
let shop = db.connect("shop.db")print(shop.sql_name("x; drop table items"))
output
line 2: `sql_name`: "x; drop table items" is not a name SQL can take (letters, digits and _)

Chaining

let shop = db.connect("shop.db")print(["id", "title"] | shop.sql_name)print(["id"] | db.sql_name)
output
"id", "title""id"

See also

sql_name, db.connect, sql_param, db.query

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

Thread 0 posts

On db.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