Reference · Database
db.sql_name
db.sql_name(str | list) -> strWhat 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")}`))"title" "title"[{ title: "Lamp" }, { title: "Desk" }]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
- Use the connection's own whenever the SQL goes to a connection: a bare
sql_namequotes for the program's database, which may be another kind. - A list gives the names separated by commas; an empty list gives the empty string.
- It needs no effect: a function declared
with httpmay call it, though not the connection'squery.
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())"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.
let shop = db.connect("shop.db")print(shop.sql_name("x; drop table items"))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)"id", "title""id"See also
sql_name, db.connect, sql_param, db.query
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