Renamed from arcdb: module path, cmd/archer, internal/archererr, docs, build scripts and ARCHER_* environment variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
// Package sqlutil holds small helpers for building SQL text safely.
|
|
package sqlutil
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Ident quotes a MySQL identifier.
|
|
func Ident(s string) string {
|
|
return "`" + strings.ReplaceAll(s, "`", "``") + "`"
|
|
}
|
|
|
|
// Qualified quotes a database-qualified table name.
|
|
func Qualified(db, table string) string {
|
|
if db == "" {
|
|
return Ident(table)
|
|
}
|
|
return Ident(db) + "." + Ident(table)
|
|
}
|
|
|
|
// Columns joins quoted column names with commas.
|
|
func Columns(cols []string) string {
|
|
parts := make([]string, len(cols))
|
|
for i, c := range cols {
|
|
parts[i] = Ident(c)
|
|
}
|
|
return strings.Join(parts, ", ")
|
|
}
|
|
|
|
// Placeholders returns n comma separated question marks.
|
|
func Placeholders(n int) string {
|
|
if n <= 0 {
|
|
return ""
|
|
}
|
|
return strings.Repeat("?, ", n-1) + "?"
|
|
}
|
|
|
|
// RowTuple renders a parenthesized, quoted column list.
|
|
func RowTuple(cols []string) string {
|
|
return "(" + Columns(cols) + ")"
|
|
}
|
|
|
|
// ParamTuple renders a parenthesized list of n placeholders.
|
|
func ParamTuple(n int) string {
|
|
return "(" + Placeholders(n) + ")"
|
|
}
|
|
|
|
// GreaterThan renders a keyset comparison (cols) > (?, ?, ...). For a single
|
|
// column it emits the simpler col > ?.
|
|
func GreaterThan(cols []string) string {
|
|
if len(cols) == 1 {
|
|
return Ident(cols[0]) + " > ?"
|
|
}
|
|
return RowTuple(cols) + " > " + ParamTuple(len(cols))
|
|
}
|
|
|
|
// InList renders (cols) IN ((?,?),(?,?),...). count is the number of tuples.
|
|
func InList(cols []string, count int) string {
|
|
if count == 0 {
|
|
return "1=0"
|
|
}
|
|
tuple := ParamTuple(len(cols))
|
|
tuples := make([]string, count)
|
|
for i := range tuples {
|
|
tuples[i] = tuple
|
|
}
|
|
if len(cols) == 1 {
|
|
return Ident(cols[0]) + " IN (" + Placeholders(count) + ")"
|
|
}
|
|
return RowTuple(cols) + " IN (" + strings.Join(tuples, ", ") + ")"
|
|
}
|