pgxhelpers

package module
v0.0.0-...-44f50b8 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 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+

Scan row into struct

ScanStruct(r *pgx.Row, dest interface{}) error

The helper that scans a pgx.Row into destination struct passed by reference based on the db fields tags. Implementation is heavily based on the jmoiron/sqlx and utilises its reflection utilities.

package main

import (
	"time"

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

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

conn, _ := pgx.Connect(pgx.ConnConfig{...})

var result MyEntity
row := conn.QueryRow("SELECT * FROM my_entity WHERE id = $1", someID)
pgxHelpers.ScanStruct(row, &result)

Scan rows into structs list

package main

import (
	"time"

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

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

conn, _ := pgx.Connect(pgx.ConnConfig{...})

var results []*MyEntity
rows, _ := conn.Query("SELECT * FROM my_entity WHERE id = $1", someID)
defer rows.Close()
pgxHelpers.ScanStructs(rows, func() interface{} {
    return new(MyEntity)
}, func(r interface{}) {
    results = append(results, r.(*MyEntity))
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanStruct

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

ScanStruct scans a pgx.Row into destination struct passed by reference based on the "db" fields tags

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