This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/handlers/pg_helpers.go
2026-02-28 01:40:31 +00:00

38 lines
952 B
Go

package handlers
import (
"database/sql"
"encoding/json"
"github.com/lib/pq"
)
// pgScanStringArray returns a sql.Scanner that scans a Postgres text[] array
// into a Go string slice. Used by handler-level Postgres-specific queries.
func pgScanStringArray(dest *[]string) interface{ Scan(src interface{}) error } {
return &pgStringArrayScanner{dest: dest}
}
type pgStringArrayScanner struct {
dest *[]string
}
func (s *pgStringArrayScanner) Scan(src interface{}) error {
var arr pq.StringArray
if err := arr.Scan(src); err != nil {
// Fallback: try JSON array (for SQLite compatibility if accidentally called)
if b, ok := src.([]byte); ok {
return json.Unmarshal(b, s.dest)
}
if str, ok := src.(string); ok {
return json.Unmarshal([]byte(str), s.dest)
}
return err
}
*s.dest = []string(arr)
return nil
}
// Ensure pq is importable even if not directly referenced elsewhere.
var _ sql.Scanner = &pgStringArrayScanner{}