Documentation
¶
Overview ¶
Package sql provides a generic database connector that pairs a standard database/sql connection with a caller-supplied row scanner.
Connector[T] opens and pings the database on construction, then exposes a single Query method that executes a query and maps each row to T using the provided ScanFunc.
Usage ¶
type User struct {
ID int
Name string
}
conn, err := sql.NewSQL(sql.SQLConfig{
Driver: "postgres",
DSN: "postgres://user:pass@localhost/mydb?sslmode=disable",
}, func(rows *stdsql.Rows) (User, error) {
var u User
return u, rows.Scan(&u.ID, &u.Name)
})
if err != nil {
log.Fatal(err)
}
defer conn.Close()
users, err := conn.Query(ctx, "SELECT id, name FROM users WHERE active = $1", true)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connector ¶
type Connector[T any] struct { // contains filtered or unexported fields }
Connector is a generic database source that opens a connection using SQLConfig and converts each result row into T via a caller-supplied ScanFunc.
func NewSQL ¶
NewSQL opens and pings the database described by cfg, then returns a Connector ready to execute queries. The caller is responsible for calling Close when done.
type SQLConfig ¶
type SQLConfig struct {
// Driver is the database driver name registered via sql.Register
// (e.g. "postgres", "mysql", "sqlite3").
Driver string
// DSN is the data source name passed verbatim to sql.Open.
DSN string
}
SQLConfig holds the parameters needed to open a database connection.