pgxhelpers

package module
v4.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2020 License: MIT Imports: 6 Imported by: 1

README

pgx-helpers

Various helpers for jackc/pgx PostgreSQL driver for Go.

Versions

  • v3 is compatible with pgx 3.x+
  • v4 is compatible with pgx 4.x+

Helpers

Scan row into struct

Unfortunately conn.QueryRow()/pgx.Row cannot be used with ScanStruct() in pgx/v4 because of the interface changes. See pgx issue discussion for details. Nevertheless, it is still possible to scan single row into struct - function works with conn.QueryRow()/pgx.Rows, and it is up to a caller to ensure that only one row is selected, e.g. by adding LIMIT 1 to a query or selecting by primary or unique key.

package main

import (
    "context"
    "log"
    "os"
    "time"

    "github.com/jackc/pgx/v4"
    pgxHelpers "github.com/vgarvardt/pgx-helpers/v4"
)

type MyEntity struct {
    ID        string    `db:"id"`
    CreatedAt time.Time `db:"created_at"`
    SomeData  string    `db:"some_data"`
}

func main() {
    conn, err := pgx.Connect(context.Background(), os.Getenv("PG_DSN"))
    if err != nil {
        log.Fatal(err)
    }

    rows, err := conn.Query(context.Background(), "SELECT * FROM my_entity WHERE id = $1", someID)
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()

    result := new(MyEntity)
    err = pgxHelpers.ScanStruct(rows, result)
    if err != nil {
        log.Fatal(err)
    }
}
Scan rows into structs list
package main

import (
    "context"
    "log"
    "os"
    "time"

    "github.com/jackc/pgx/v4"
    pgxHelpers "github.com/vgarvardt/pgx-helpers/v4"
)

type MyEntity struct {
    ID        string    `db:"id"`
    CreatedAt time.Time `db:"created_at"`
    SomeData  string    `db:"some_data"`
}

func main() {
    conn, err := pgx.Connect(context.Background(), os.Getenv("PG_DSN"))
    if err != nil {
        log.Fatal(err)
    }

    rows, err := conn.Query(context.Background(), "SELECT * FROM my_entity WHERE created_at >= $1", time.Now().Add(-time.Hour))
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()

    var results []*MyEntity
    err = pgxHelpers.ScanStructs(rows, func() interface{} {
        return new(MyEntity)
    }, func(r interface{}) {
        results = append(results, r.(*MyEntity))
    })
    if err != nil {
        log.Fatal(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanStruct

func ScanStruct(r pgx.Rows, dest interface{}) error

ScanStruct scans a pgx.Rows into destination struct passed by reference based on the "db" fields tags. This is workaround function for pgx.Rows with single row as pgx/v4 does not allow to get row metadata from pgx.Row - see https://github.com/jackc/pgx/issues/627 for details.

If there are no rows pgx.ErrNoRows is returned. If there are more than one row in the result - they are ignored. Function call closes rows, so caller may skip it.

func ScanStructs

func ScanStructs(r pgx.Rows, newDest func() interface{}, appendResult func(r interface{})) error

ScanStructs scans a pgx.Rows into destination structs list passed by reference based on the "db" fields tags

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL