Skip to content

Commit

Permalink
Add docs for pgxscan and sqlscan packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgysavva committed Jun 28, 2020
1 parent fa97c22 commit b780922
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 11 deletions.
13 changes: 7 additions & 6 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Package dbscan allows scanning data from database rows into complex Go types.
// Package dbscan allows scanning data from abstract database rows into complex Go types.
/*
dbscan works with abstract Rows and doesn't depend on any specific database or library.
If a type implements Rows interface it can leverage full functional of this package.
Subpackages sqlscan and pgxscan are wrappers around this package
they contain functions and adapters tailored to database/sql and
github.com/jackc/pgx/v4 libraries correspondingly. sqlscan and pgxscan proxy all calls to dbscan internally.
Subpackages github.com/georgysavva/dbscan/sqlscan
and github.com/georgysavva/dbscan/pgxscan are wrappers around this package
they contain functions and adapters tailored to database/sql
and github.com/jackc/pgx/v4 libraries correspondingly. sqlscan and pgxscan proxy all calls to dbscan internally.
dbscan does all the logic, but generally, it shouldn't be imported by the application code directly.
If you are working with database/sql - use sqlscan subpackage.
If you are working with database/sql - use github.com/georgysavva/dbscan/sqlscan package.
If you are working with pgx - use pgxscan subpackage.
If you are working with pgx - use github.com/georgysavva/dbscan/pgxscan package.
Scanning into struct
Expand Down
40 changes: 40 additions & 0 deletions pgxscan/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package pgxscan allows scanning data from pgx.Rows into complex Go types.
/*
pgxscan is a wrapper around github.com/georgysavva/dbscan package.
It contains adapters and proxy functions that are meant to connect github.com/jackc/pgx/v4
with github.com/georgysavva/dbscan functionality. pgxscan mirrors all capabilities provided by dbscan.
See dbscan docs to get familiar with all details and features.
How to use
The most common way to use pgxscan is by calling QueryAll or QueryOne function,
it's as simple as this:
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
db, _ := pgxpool.Connect(ctx, "example-connection-url")
// Use QueryAll to query multiple records.
var users []*User
if err := pgxscan.QueryAll(
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.
// Use QueryOne to query exactly one record.
var user User
if err := pgxscan.QueryOne(
ctx, &user, db, `SELECT user_id, name, email, age from users where id='bob'`,
); err != nil {
// Handle query or rows processing error.
}
// users variable now contains data from all rows.
*/
package pgxscan
4 changes: 2 additions & 2 deletions pgxscan/pgxscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
_ QueryI = *new(pgx.Tx)
)

// QueryAll is a helper function that queries the rows and calls the ScanAll function.
// QueryAll is a high-level function that queries the rows and calls the ScanAll function.
// See ScanAll for details.
func QueryAll(ctx context.Context, dst interface{}, q QueryI, query string, args ...interface{}) error {
rows, err := q.Query(ctx, query, args...)
Expand All @@ -31,7 +31,7 @@ func QueryAll(ctx context.Context, dst interface{}, q QueryI, query string, args
return errors.WithStack(err)
}

// QueryOne is a helper function that queries the rows and calls the ScanOne function.
// QueryOne is a high-level function that queries the rows and calls the ScanOne function.
// See ScanOne for details.
func QueryOne(ctx context.Context, dst interface{}, q QueryI, query string, args ...interface{}) error {
rows, err := q.Query(ctx, query, args...)
Expand Down
40 changes: 40 additions & 0 deletions sqlscan/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package sqlscan allows scanning data from *sql.Rows into complex Go types.
/*
sqlscan is a wrapper around github.com/georgysavva/dbscan package.
It contains adapters and proxy functions that are meant to connect database/sql
with github.com/georgysavva/dbscan functionality. sqlscan mirrors all capabilities provided by dbscan.
See dbscan docs to get familiar with all details and features.
How to use
The most common way to use sqlscan is by calling QueryAll or QueryOne function,
it's as simple as this:
type User struct {
ID string `db:"user_id"`
Name string
Email string
Age int
}
db, _ := sql.Open("pgx", "example-connection-url")
// Use QueryAll to query multiple records.
var users []*User
if err := sqlscan.QueryAll(
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.
// Use QueryOne to query exactly one record.
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.
}
// users variable now contains data from all rows.
*/
package sqlscan
6 changes: 3 additions & 3 deletions sqlscan/sqlscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
)

// QueryI is something that sqlscan can query and get the *sql.Rows.
// QueryI is something that sqlscan can query and get the *sql.Rows,
// For example: *sql.DB, *sql.Conn or *sql.Tx.
type QueryI interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Expand All @@ -19,7 +19,7 @@ var (
_ QueryI = &sql.Tx{}
)

// QueryAll is a helper function that queries the rows and calls the ScanAll function.
// QueryAll is a high-level function that queries the rows and calls the ScanAll function.
// See ScanAll for details.
func QueryAll(ctx context.Context, dst interface{}, q QueryI, query string, args ...interface{}) error {
rows, err := q.QueryContext(ctx, query, args...)
Expand All @@ -30,7 +30,7 @@ func QueryAll(ctx context.Context, dst interface{}, q QueryI, query string, args
return errors.WithStack(err)
}

// QueryOne is a helper function that queries the rows and calls the ScanOne function.
// QueryOne is a high-level function that queries the rows and calls the ScanOne function.
// See ScanOne for details.
func QueryOne(ctx context.Context, dst interface{}, q QueryI, query string, args ...interface{}) error {
rows, err := q.QueryContext(ctx, query, args...)
Expand Down

0 comments on commit b780922

Please sign in to comment.