本文介绍在 go 中构建可扩展、安全的 postgresql 参数化查询的方法,解决可选搜索条件导致的 sql 拼接难题,避免 sql 注入风险,同时保持代码简洁与可维护性。
在 Go 中使用 database/sql 驱动(如 github.com/lib/pq 或 github.com/jackc/pgx/v5)执行 PostgreSQL 查询时,硬编码动态 WHERE 条件或手动拼接 $1, $2 占位符极易出错——尤其当部分搜索参数(如全文检索关键词、分页限制)为可选时,不仅需动态追加 SQL 片段,还必须严格同步参数顺序与占位符索引,稍有疏忽即引发 pq: invalid parameter reference 或逻辑错误。
推荐采用 “增量式查询构建”模式:以基础查询为起点,通过条件判断逐步追加 AND 子句,并同步维护参数切片。关键在于:所有占位符始终使用 $N 形式,且 $N 的 N 值由当前 params 切片长度实时计算,确保索引绝对一致。
以下是一个生产就绪的示例,适配你原始需求中的多表联查与可选全文检索:
func buildJobsQuery(nameQuery, locationQuery string, limit, offset int) (string, []interface{}) {
base := `
SELECT json_agg(row_to_json(t)) FROM (
SELECT jobs.*, companies.name AS company_name, locations.name AS location_name
FROM jobs
JOIN companies ON jobs.company_id = companies.id
JOIN locations ON jobs.location_id = locations.id
WHERE true`
params := []interface{}{}
// 可选:按职位/公司/地点联合全文检索
if nameQuery != "" {
base += " AND to_tsvector(jobs.name || ' ' || companies.name || ' ' || locations.name) @@ to_tsquery($" + strconv.Itoa(len(params)+1) + ")"
params = append(params, nameQuery)
}
// 可选:按地点名称精确全文匹配
if locationQuery != "" {
base += " AND to_tsvector(locations.name) @@ to_tsquery($" + strconv.Itoa(len(params)+1) + ")"
params = append(params, locationQuery)
}
// 分页支持(limit 和 offset 均为可选)
if limit > 0 {
base += " LIMIT $" + strconv.Itoa(len(params)+1)
params = append(params, limit)
if offset > 0 {
base += " OFFSET $" + strconv.Itoa(len(params)+1)
params = append(params, offset)
}
}
base += ") t"
return base, params
}
// 使用示例
query, args := buildJobsQuery("engineer", "Berlin", 20, 0)
rows, err := db.Query(query, args...)
if err != nil {
log.Fatal(err)
}
✅ 核心优势: