sqlc
sqlc copied to clipboard
Optionally prepend schema for multi-tenancy
This PR adds the flag emit_schema_name.
This will put before all the tables a %s, and will replace them inside the function
The generated result would look something like this
const getItemsByIds = `-- name: GetItemsByIds :many
SELECT id FROM ` + "`" + `%s` + "`" + `.items
ORDER BY ` + "`" + `index` + "`" + ` ASC
`
// Will add schema name as parameter
func (q *Queries) GetItemsByIds(ctx context.Context, schema string) ([]uint32, error) {
// Will replace at runtime the schema placeholder
rows, err := q.db.QueryContext(ctx, strings.ReplaceAll(getItemsByIds, "%s", schema))
if err != nil {
return nil, err
}
defer rows.Close()
var items []uint32
for rows.Next() {
var id uint32
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}