pig

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2021 License: MIT Imports: 6 Imported by: 0

README

pig

Build PkgGoDev Go Report Card Coverage Status

Simple pgx wrapper to execute and scan query results.

Features

  • All-in-one tool;
  • Simple transactions management:
    • You can set idle_in_transaction_session_timeout local option (read more),
    • You can set statement_timeout local option (read more).

Usage

Execute query
package main

import (
	"context"
	"log"

	"github.com/alexeyco/pig"
	"github.com/jackc/pgx/v4"
)

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

	p := pig.New(conn)

	affectedRows, err := p.Query().Exec("DELETE FROM things WHERE id = $1", 123)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("affected", affectedRows, "rows")
}
Get single entity
package main

import (
	"context"
	"log"

	"github.com/alexeyco/pig"
	"github.com/jackc/pgx/v4"
)

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

	p := pig.New(conn)

	var cnt int64
	err = p.Query().Get(&cnt, "SELECT count(*) FROM things")
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(cnt, "things found")
}
Select multiple entities
package main

import (
	"context"
	"log"

	"github.com/alexeyco/pig"
	"github.com/jackc/pgx/v4"
)

type Thing struct {
	ID       int64  `db:"id"`
	Name     string `db:"name"`
	Quantity int64  `db:"quantity"`
}

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

	p := pig.New(conn)

	var things []Thing
	err = p.Query().Select(&things, "SELECT * FROM things")
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(things)
}
Make transactions
package main

import (
	"context"
	"log"
	"time"

	"github.com/alexeyco/pig"
	"github.com/jackc/pgx/v4"
)

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

	p := pig.New(conn)

	var affectedRows int64
	err = p.Tx(pig.TransactionTimeout(time.Second)).
		Exec(func(ex *pig.Ex) error {
			affectedRows, err = p.Query().Exec("DELETE FROM things WHERE id = $1", 123)
			if err != nil {
				return err
			}

			return nil
		})
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("affected", affectedRows, "rows")
}

License

MIT License

Copyright (c) 2021 Alexey Popov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

Package pig – simple pgx wrapper to execute and scan query results.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	BeginFunc(context.Context, func(pgx.Tx) error) error
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
}

Conn connection interface.

type Ex

type Ex struct {
	// contains filtered or unexported fields
}

Ex to execute queries.

func (*Ex) Exec

func (e *Ex) Exec(sql string, args ...interface{}) (int64, error)

Exec query and return affected rows.

func (*Ex) Get

func (e *Ex) Get(dst interface{}, sql string, args ...interface{}) error

Get single record.

func (*Ex) Select

func (e *Ex) Select(dst interface{}, sql string, args ...interface{}) error

Select multiple records.

type Handler

type Handler func(*Ex) error

Handler to execute transaction.

type Option

type Option func(*Options)

Option func.

func Ctx

func Ctx(ctx context.Context) Option

Ctx sets query or tx context.

func StatementTimeout

func StatementTimeout(d time.Duration) Option

StatementTimeout sets transaction statement timeout (ignored with queries).

func TransactionTimeout

func TransactionTimeout(d time.Duration) Option

TransactionTimeout sets transaction timeout (ignored with queries).

type Options

type Options struct {
	Context            context.Context
	TransactionTimeout int64
	StatementTimeout   int64
}

Options query or tx options.

type Pig

type Pig struct {
	// contains filtered or unexported fields
}

Pig pgx wrapper.

func New

func New(conn Conn) *Pig

New returns new pig instance.

func (*Pig) Conn

func (p *Pig) Conn() Conn

Conn returns pgx connection.

func (*Pig) Query

func (p *Pig) Query(options ...Option) *Ex

Query returns new query executor.

Example
conn, err := pgx.Connect(context.Background(), "")
if err != nil {
	log.Fatalln(err)
}

p := pig.New(conn)

// Execute query
affectedRows, err := p.Query().Exec("DELETE FROM things WHERE id = $1", 123)
if err != nil {
	log.Fatalln(err)
}

log.Println("affected", affectedRows, "rows")

// Get single record from database
var cnt int64
err = p.Query().Get(&cnt, "SELECT count(*) FROM things")
if err != nil {
	log.Fatalln(err)
}

type Thing struct {
	ID       int64  `db:"id"`
	Name     string `db:"name"`
	Quantity int64  `db:"quantity"`
}

// Select multiple records
var things []Thing
err = p.Query().Select(&things, "SELECT * FROM things")
if err != nil {
	log.Fatalln(err)
}

log.Println(things)
Output:

func (*Pig) Tx

func (p *Pig) Tx(options ...Option) *Tx

Tx returns new transaction.

Example
conn, err := pgx.Connect(context.Background(), "")
if err != nil {
	log.Fatalln(err)
}

p := pig.New(conn)

var affectedRows int64
err = p.Tx().Exec(func(ex *pig.Ex) error {
	affectedRows, err = p.Query().Exec("DELETE FROM things WHERE id = $1", 123)
	if err != nil {
		return err
	}

	return nil
})
if err != nil {
	log.Fatalln(err)
}

log.Println("affected", affectedRows, "rows")
Output:

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx transaction.

func (*Tx) Exec

func (tx *Tx) Exec(handler Handler) error

Exec to execute transaction.

Jump to

Keyboard shortcuts

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