Documentation
¶
Overview ¶
Package sqlscan allows scanning data into Go structs and other composite types, when working with database/sql library.
Essentially, sqlscan is a wrapper around github.com/georgysavva/scany/dbscan package. sqlscan connects database/sql with dbscan functionality. It contains adapters that are meant to work with *sql.Rows and proxy all calls to dbscan. sqlscan mirrors all capabilities provided by dbscan. It's encouraged to read dbscan docs first to get familiar with all concepts and features.
How to use ¶
The most common way to use sqlscan is by calling Query or QueryOne function, it's as simple as this:
type User struct {
UserID string
Name string
Email string
Age int
}
db, _ := sql.Open("postgres", "example-connection-url")
// Use Query to query multiple records.
var users []*User
sqlscan.Query(ctx, &users, db, `SELECT user_id, name, email, age FROM users`)
// users variable now contains data from all rows.
Index ¶
- func NotFound(err error) bool
- func Query(ctx context.Context, dst interface{}, q Querier, query string, ...) error
- func QueryOne(ctx context.Context, dst interface{}, q Querier, query string, ...) error
- func ScanAll(dst interface{}, rows *sql.Rows) error
- func ScanOne(dst interface{}, rows *sql.Rows) error
- func ScanRow(dst interface{}, rows *sql.Rows) error
- type Querier
- type RowScanner
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NotFound ¶
NotFound is a wrapper around the dbscan.NotFound function. See dbscan.NotFound for details.
func Query ¶
func Query(ctx context.Context, dst interface{}, q Querier, query string, args ...interface{}) error
Query is a high-level function that queries rows and calls the ScanAll function. See ScanAll for details.
Example ¶
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
db, _ := sql.Open("postgres", "example-connection-url")
var users []*User
if err := sqlscan.Query(
ctx, &users, db, `SELECT user_id, name, email, age FROM users`,
); err != nil {
// Handle query or rows processing error.
}
// users variable now contains data from all rows.
func QueryOne ¶
func QueryOne(ctx context.Context, dst interface{}, q Querier, query string, args ...interface{}) error
QueryOne is a high-level function that queries rows and calls the ScanOne function. See ScanOne for details.
Example ¶
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
db, _ := sql.Open("postgres", "example-connection-url")
var user User
if err := sqlscan.QueryOne(
ctx, &user, db, `SELECT user_id, name, email, age FROM users WHERE id='bob'`,
); err != nil {
// Handle query or rows processing error.
}
// user variable now contains data from all rows.
func ScanAll ¶
ScanAll is a wrapper around the dbscan.ScanAll function. See dbscan.ScanAll for details.
Example ¶
package main
import (
"database/sql"
"github.com/georgysavva/scany/sqlscan"
)
func main() {
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
// Query *sql.Rows from the database.
db, _ := sql.Open("postgres", "example-connection-url")
rows, _ := db.Query(`SELECT user_id, name, email, age FROM users`)
var users []*User
if err := sqlscan.ScanAll(&users, rows); err != nil {
// Handle rows processing error
}
// users variable now contains data from all rows.
}
func ScanOne ¶
ScanOne is a wrapper around the dbscan.ScanOne function. See dbscan.ScanOne for details.
Example ¶
package main
import (
"database/sql"
"github.com/georgysavva/scany/sqlscan"
)
func main() {
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
// Query *sql.Rows from the database.
db, _ := sql.Open("postgres", "example-connection-url")
rows, _ := db.Query(`SELECT user_id, name, email, age FROM users WHERE id='bob'`)
var user User
if err := sqlscan.ScanOne(&user, rows); err != nil {
// Handle rows processing error.
}
// user variable now contains data from the single row.
}
func ScanRow ¶
ScanRow is a wrapper around the dbscan.ScanRow function. See dbscan.ScanRow for details.
Example ¶
package main
import (
"database/sql"
"github.com/georgysavva/scany/sqlscan"
)
func main() {
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
// Query *sql.Rows from the database.
db, _ := sql.Open("postgres", "example-connection-url")
rows, _ := db.Query(`SELECT user_id, name, email, age FROM users`)
// Make sure rows are always closed.
defer rows.Close()
for rows.Next() {
var user User
if err := sqlscan.ScanRow(&user, rows); err != nil {
// Handle row scanning error.
}
// user variable now contains data from the current row.
}
if err := rows.Err(); err != nil {
// Handle rows final error.
}
}
Types ¶
type Querier ¶
type Querier interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}
Querier is something that sqlscan can query and get the *sql.Rows from. For example, it can be: *sql.DB, *sql.Conn or *sql.Tx.
type RowScanner ¶
type RowScanner struct {
*dbscan.RowScanner
}
RowScanner is a wrapper around the dbscan.RowScanner type. See dbscan.RowScanner for details.
Example ¶
package main
import (
"database/sql"
"github.com/georgysavva/scany/sqlscan"
)
func main() {
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
// Query *sql.Rows from the database.
db, _ := sql.Open("postgres", "example-connection-url")
rows, _ := db.Query(`SELECT user_id, name, email, age FROM users`)
// Make sure rows are always closed.
defer rows.Close()
rs := sqlscan.NewRowScanner(rows)
for rows.Next() {
var user User
if err := rs.Scan(&user); err != nil {
// Handle row scanning error.
}
// user variable now contains data from the current row.
}
if err := rows.Err(); err != nil {
// Handle rows final error.
}
}
func NewRowScanner ¶
func NewRowScanner(rows *sql.Rows) *RowScanner
NewRowScanner returns a new RowScanner instance.