Changeset 0.17.1 (#76)
This commit is contained in:
37
server/handlers/pg_helpers.go
Normal file
37
server/handlers/pg_helpers.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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{}
|
||||
Reference in New Issue
Block a user