Simple pgx wrapper to execute and scan query results

Overview

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.
Owner
Alexey Popov
Alexey Popov
Go-Postgresql-Query-Builder - A query builder for Postgresql in Go

Postgresql Query Builder for Go This query builder aims to make complex queries

Samuel Banks 4 May 24, 2022
Scan database/sql rows directly to structs, slices, and primitive types

Scan Scan standard lib database rows directly to structs or slices. For the most comprehensive and up-to-date docs see the godoc Examples Multiple Row

Brett Jones 273 Jun 22, 2022
A go Library for scan database/sql rows to struct、slice、other types. And it support multiple databases connection management

ploto A go Library for scan database/sql rows to struct、slice、other types. And it support multiple databases connection management It's not an ORM. wo

solar 2 Mar 16, 2022
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.

trdsql CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. It is a tool like q, textql and others. The difference from these tools is t

Noboru Saito 1.2k Jun 22, 2022
Go-postgres - go-postgres library provide NoSQL functionality which can execute queries with pool of connections

GO Postgres go-postgres library provide NoSQL functionality which can execute queries with pool of connections. What is this repository for? Establish

Damindu Lakmal 0 Dec 31, 2021
Go-get-it - Simple database query script for UNIX-terminal usage

go-get-it Simple database query script for UNIX-terminal usage Supports MongoDB Quick start Commands: Usage of ggi: -c string MongoDB collectio

Andrew Aleynikov 0 Mar 16, 2022
OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases and file formats using SQL.

OctoSQL OctoSQL is a query tool that allows you to join, analyse and transform data from multiple databases, streaming sources and file formats using

Jacob Martin 3.1k Jun 27, 2022
BQB is a lightweight and easy to use query builder that works with sqlite, mysql, mariadb, postgres, and others.

Basic Query Builder Why Simple, lightweight, and fast Supports any and all syntax by the nature of how it works Doesn't require learning special synta

Aaron M 37 Jun 11, 2022
A Go SQL query builder and struct mapper.

godb - a Go query builder and struct mapper godb is a simple Go query builder and struct mapper, not a full-featured ORM. godb does not manage relatio

Samuel GAY 715 Jun 15, 2022
Use SQL to query host, DNS and exploit information using Shodan. Open source CLI. No DB required.

Shodan Plugin for Steampipe Query Shodan with SQL Use SQL to query host, DNS and exploit information using Shodan. For example: select * from shod

Turbot 23 May 10, 2022
Query and Provision Cloud Infrastructure using an extensible SQL based grammar

Deploy, Manage and Query Cloud Infrastructure using SQL [Documentation] [Developer Guide] Cloud infrastructure coding using SQL InfraQL allows you to

InfraQL 22 Apr 5, 2022
Use SQL to instantly query users, groups, applications and more from Okta. Open source CLI. No DB required.

Okta Plugin for Steampipe Use SQL to query infrastructure including users, groups, applications and more from Okta. Get started → Documentation: Table

Turbot 4 Dec 15, 2021
Use SQL to instantly query instances, networks, databases, and more from Scaleway. Open source CLI. No DB required.

Scaleway Plugin for Steampipe Use SQL to query infrastructure servers, networks, databases and more from your Scaleway project. Get started → Document

Turbot 5 Dec 16, 2021
Use SQL to query information including Users, Groups, Clients, Roles and more from Keycloak.

Keycloak Plugin for Steampipe [WIP] THIS IS NOT ACTIVE NOR WORKING YET - DO NOT USE Use SQL to query information including Users, Groups, Clients, Rol

APS Group 2 Nov 30, 2021
Use SQL to query instances, domains and more from Prometheus.

Use SQL to instantly query Prometheus metrics, alerts, labels and more. Open source CLI. No DB required.

Turbot 13 Mar 29, 2022
Use SQL to instantly query resources, data sources and more from Terraform code. Open source CLI. No DB required.

Terraform Plugin for Steampipe Use SQL to query data from Terraform configuration files. Get started → Documentation: Table definitions & examples Com

Turbot 10 May 16, 2022
Use SQL to instantly query Algolia indexes and configuration. Open source CLI. No DB required

Use SQL to instantly query Algolia indexes and configuration. Open source CLI. No DB required

Turbot 1 Feb 12, 2022
Use SQL to query databases, logs and more from PlanetScale

Use SQL to instantly query PlanetScale databases, branches and more. Open source CLI. No DB required.

Turbot 1 Feb 13, 2022
Steampipe plugin to query your Baleen namespaces, custom rules and more

Baleen plugin for Steampipe Use SQL to query namespaces, rules and more from Baleen. Get started → Documentation: Table definitions & examples Quick s

François de Metz 2 Jun 16, 2022