Documentation
¶
Overview ¶
Package pbpgx provides a toolkit for easier Protocol Buffers interaction with PostgreSQL databases.
Index ¶
- func Query[M proto.Message](ctx context.Context, x Executor, sql string, args ...interface{}) ([]M, error)
- func QueryRow[M proto.Message](ctx context.Context, x Executor, sql string, args ...interface{}) (M, error)
- func QueryStream[M proto.Message](x Executor, stream ServerStream[M], sql string, args ...interface{}) error
- func Scan[M proto.Message](rows pgx.Rows) (result []M, err error)
- func ScanOne[M proto.Message](rows pgx.Rows) (M, error)
- func ScanStream[M proto.Message](rows pgx.Rows, stream ServerStream[M]) error
- type Executor
- type ServerStream
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Query ¶
func Query[M proto.Message](ctx context.Context, x Executor, sql string, args ...interface{}) ([]M, error)
Query runs the passed sql with args on the Executor x, and returns a slice of type M containing the results. See Scan for more details.
Example ¶
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) defer cancel() conn, err := pgx.Connect(ctx, "user=pbpgx_tester host=db port=5432 dbname=pbpgx_tester") if err != nil { panic(err) } defer conn.Close(ctx) result := new(gen.ProductList) result.Products, err = pbpgx.Query[*gen.Product](ctx, conn, "select title, price, created from products where id in ($1, $2, $3);", 2, 4, 5) if err != nil { panic(err) } out, _ := protojson.Marshal(result) fmt.Println(string(out)) // {"products":[{"title":"two","price":10.45},{"title":"four","price":100},{"title":"five","price":0.9}]}
Output:
func QueryRow ¶
func QueryRow[M proto.Message](ctx context.Context, x Executor, sql string, args ...interface{}) (M, error)
QueryRow runs the passed sql with args on the Executor x, and returns one row Scanned into a message of type M. See Scan for more details.
In case of no rows, pgx.ErrNoRows is returned.
func QueryStream ¶
func QueryStream[M proto.Message](x Executor, stream ServerStream[M], sql string, args ...interface{}) error
QueryStream runs the passed sql with args on the Executor x. Results are send to stream. See ScanStream for more details.
func Scan ¶
Scan returns a slice of proto messages of type M, filled with data from rows. It matches field names from rows to field names of the proto message type M. An error is returned if a column name in rows is not found in te message type's field names, if a matched message field is of an unsupported type or any scan error reported by the pgx driver.
Example ¶
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) defer cancel() conn, err := pgx.Connect(ctx, "user=pbpgx_tester host=db port=5432 dbname=pbpgx_tester") if err != nil { panic(err) } defer conn.Close(ctx) rows, err := conn.Query(ctx, "select id, title, price, created from products;") if err != nil { panic(err) } result := new(gen.ProductList) result.Products, err = pbpgx.Scan[*gen.Product](rows) if err != nil { panic(err) } out, _ := protojson.Marshal(result) fmt.Println(string(out)) // {"products":[{"id":"1","title":"one","price":9.99,"created":"2022-01-07T13:47:07Z"},{"id":"2","title":"two","price":10.45,"created":"2022-01-07T13:47:08Z"},{"id":"3","title":"three","created":"2022-01-07T13:47:09Z"},{"id":"4","title":"four","price":100,"created":"2022-01-07T13:47:10Z"},{"id":"5","title":"five","price":0.9,"created":"2022-01-07T13:47:11Z"}]}
Output:
func ScanOne ¶
ScanOne returns a single instance of proto Message with type M, filled with data from rows. pgx.ErrNoRows is returned when there are rows to scan. See Scan for field name matching rules.
func ScanStream ¶
func ScanStream[M proto.Message](rows pgx.Rows, stream ServerStream[M]) error
ScanStream writes instances of proto messages with type M to stream.Send(), filled with data from rows. ScanStream returns a nil error when rows is exhausted or an error when one is encountered, durng scanning or sending. Messages may already have been send when returning an error. See Scan for field name matching rules.
Types ¶
type Executor ¶
type Executor interface { Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row }
Executor is a common interface for database connection types. For example pgxpool.Pool, pgx[pool].Conn and pgx[pool].Tx all satisfy this interface.
Directories
¶
Path | Synopsis |
---|---|
Package crud provides Create Read Update and Delete functionality on top of pbpgx.
|
Package crud provides Create Read Update and Delete functionality on top of pbpgx. |
internal
|
|
support
Package support defines the supported and unsupported field types for binding.
|
Package support defines the supported and unsupported field types for binding. |