Skip to main content

Quotes

Identifiers in SQL often need dialect-specific quoting. Use dialect.Quote("part1", "part2") (e.g. psql.Quote, mysql.Quote, sqlite.Quote) to build a qualified quoted name: "schema"."table" on PostgreSQL/SQLite, `schema`.`table` on MySQL.

Bob uses two patterns:

  1. string parameters that mean a single SQL identifier — the dialect quotes them when rendering (you pass "kind", SQL gets "kind").
  2. any parameters — rendered with bob.Express. Pass dialect.Quote(...) for identifiers; a bare string is written verbatim (table names without quotes, SQL fragments, function names like generate_series).

For qualified column names in SET / ON CONFLICT DO UPDATE, use SetExpr (or im.UpdateCol on MySQL), not SetCol.

When the API quotes for you

API / parameterDialectsNotes
sm/im/um/dm/mm.With(name, columns...)psql, mysql, sqliteCTE name and column list
sm.Window(name)psql, mysql, sqliteNamed window
wm.BasedOn(name)psql, mysql, sqliteReference to existing window
im.OnConflictOnConstraint(name)psql, sqliteConstraint name
um/dm.WhereCurrentOf(cursor)psqlCursor name
um/im/mm.SetCol("col")psql; um/im on sqlite; um + im.UpdateCol on mysqlLHS of single-column SET
um/im/mm.SetCols("a", "b")psql onlyTuple assignment (a, b) = ...
mm.Columns("a", "b")psql onlyMERGE ... INSERT (a, b) column list
im.SetExcluded("a", "b")psql, sqlite"a" = EXCLUDED."a", ...
im.Excluded("col")psql, sqliteEXCLUDED."col"
im.Into(table, "c1", "c2")allInsert column names (table is any, see below)
FromChain.As(alias, "c1", ...)allTable alias and optional rename columns
JoinChain.Using("c1", "c2")allUSING column names
OrderBy(...).Collate(name)allCollation name
fm.Columns("col", datatype)psqlColumn name in table-function definition (datatype is literal SQL)

When you pass any and use Quote() yourself

API / parameterTypical use
sm.From(table)From("users") → unquoted; From(psql.Quote("users")) or From(psql.Quote("public", "users")) when quoted
im.Into(name, ...)Into(psql.Quote("distributors"), "did", "dname") — table any, columns still string and auto-quoted
um.Table(name) / TableAs(name, alias)Table(psql.Quote("employees")); alias is a string and quoted via WriteQuoted
dm.From / mm.Into / mm.UsingSame as From / Into
sm.Columns(...)Columns(psql.Quote("id")) — each item is any
im.OnConflict(...)OnConflict(psql.Quote("id"))
Returning(...)Returning(psql.Quote("id"))
sm.ForUpdate / ForShare / ForNoKeyUpdate / ForKeyShareForUpdate(psql.Quote("my_table"))
sm.OrderBy / GroupBy / HavingExpression or Quote as needed
psql.F(name, args...)F("generate_series", 1, 3) — literal name; F(psql.Quote("pg_catalog", "array_agg"), col) for qualified

Cast(exp, typname string) appends the type name as literal text (not an identifier slot).

Examples

// Qualified quoted name
psql.Quote("schema_name", "table_name") // "schema_name"."table_name"

// Column in SET (quoted automatically)
um.SetCol("kind").ToArg("Drama")

// Qualified column in SET (SetExpr = SetCol with an expression LHS)
um.SetExpr(psql.Quote("employees", "id")).ToArg(1)

// Several assignments in um.Set (EQ omits comparison parentheses in SET)
um.Set(
psql.Quote("employees", "dept_id").EQ(psql.Quote("accounts", "dept_id")),
)

// MERGE update
mm.SetCol("price").To(psql.Quote("u", "price"))

// Table in FROM (pass Quote when you want quoting)
sm.From(psql.Quote("films"))

// Function name
psql.F(psql.Quote("pg_catalog", "array_agg"), psql.Quote("x"))