pgxe

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2021 License: MIT Imports: 7 Imported by: 0

README

PGXE - PGX Extension

Refrence Coverage

This library aims to reduce the cognitive load while trying to make sql queries and hastens development.

Query preparation is fast (for most queries 500ns - 800ns) because it cuts certain corners such as not handling comments at all, which may be problematic for you or not. It is recommended that in production you use just pgx because the overhead of this library might be sometimes a bit too much in systems that need to scale extremely well

Utilizes the pgx library under the hood to achieve fast performance.

It also uses a modified fork of scany (github.com/anton7r/pgx-scany which removes unneccessary imports) to scan database rows into structs.

Installation

go get -u github.com/anton7r/pgxe

Features

  • Simplified SQLX like API
  • Named Queries Supported and they are fast

Code example

The code example is not guranteed to have correct go-syntax, so if you spot a mistake please file an issue :)

import "github.com/anton7r/pgxe"

type Product struct {
    Id int
    Name string
    Price decimal??
}

func main() {
    db := pgxe.Connect(pgxe.Connection{
        User:     "admin",
        Password: "superSecretPassword",
        Database: "postgres",
        Host:     "localhost",
        Port:     "5432",

        Logger:   logger //pgx.Logger
    })

    //The id placeholder in the query has to be same case as defined in the struct so that the field can be found
    //Case insensitivity would decrease the performance of our lexer by at the worst case ~5x, so it is better for it to be case sensitive
    query := "SELECT * FROM products WHERE id = :Id"
    rows, err := db.NamedQuery(query, &Product{Id:4})
    if err != nil {
        println(err.Error())
        return
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	User     string // pgx defaults this to your OS username
	Password string
	Database string // the name of the database
	Host     string // defaults to localhost
	Port     string // defaults to 5432

	SslMode string // defaults to disable

	PoolMaxConns          int // default 4
	PoolMinConns          int // default 0
	PoolMaxConnLifetime   int // default time.Hour
	PoolMaxConnIdleTime   int // default time.Minute * 30
	PoolHealthCheckPeriod int // default time.Minute

	Logger pgx.Logger
}

type DB

type DB struct {
	Pool *pgxpool.Pool
}

DB stores the internal PgxPool

func Connect

func Connect(conn Connection) (*DB, error)

Connect connects to the database NOTE: It does not support all the features that are on connection string yet Use is currently recommended to be used instead

func Use

func Use(conn *pgxpool.Pool) *DB

Use uses an already existing connection It assumes that the fed connection pool is not null

func (*DB) Close

func (DB *DB) Close()

Close closes the underlying pool

func (*DB) Exec

func (DB *DB) Exec(query string, args ...interface{}) (pgconn.CommandTag, error)

Exec is used to run actions on database that wont return any values to the client

func (*DB) Get

func (DB *DB) Get(target interface{}, query string, args ...interface{}) error

Get is a high-level function that is used to retrieve data from database into a single struct

func (*DB) NamedExec

func (DB *DB) NamedExec(query string, arg interface{}) (pgconn.CommandTag, error)

NamedExec simplifies the parameters and allows the use of named parameters

func (*DB) NamedGet

func (DB *DB) NamedGet(target interface{}, query string, arg interface{}) error

NamedGet is a high-level function that is used to retrieve data from database into structs

func (*DB) NamedQuery

func (DB *DB) NamedQuery(query string, arg interface{}) (pgx.Rows, error)

NamedQuery simplifies the parameters and allows the use of named parameters

func (*DB) NamedQueryRow

func (DB *DB) NamedQueryRow(query string, arg interface{}) (pgx.Row, error)

NamedQueryRow simplifies the parameters and allows the use of named parameters

func (*DB) NamedSelect

func (DB *DB) NamedSelect(target interface{}, query string, arg interface{}) error

NamedSelect is a high-level function that is used to retrieve data from database into structs

func (*DB) Query

func (DB *DB) Query(query string, args ...interface{}) (pgx.Rows, error)

Query is used to retrieve multiple rows from the database

func (*DB) QueryRow

func (DB *DB) QueryRow(query string, args ...interface{}) pgx.Row

QueryRow is used to retrieve a single row from the database

func (*DB) Select

func (DB *DB) Select(target interface{}, query string, args ...interface{}) error

Select is a high-level function that is used to retrieve data from database into slices which has structs.

type PreparedNamedQuery added in v0.2.0

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

func PrepareNamed added in v0.2.0

func PrepareNamed(sql string) (*PreparedNamedQuery, error)

Prepares a named SQL-query with :FieldName placeholders NOTE: This does not use the pgx way to prepare queries under the hood

func (*PreparedNamedQuery) Exec added in v0.2.0

func (p *PreparedNamedQuery) Exec(db *DB, arg interface{}) (pgconn.CommandTag, error)

func (*PreparedNamedQuery) Get added in v0.2.0

func (p *PreparedNamedQuery) Get(db *DB, target interface{}, arg interface{}) error

func (*PreparedNamedQuery) Query added in v0.2.0

func (p *PreparedNamedQuery) Query(db *DB, arg interface{}) (pgx.Rows, error)

func (*PreparedNamedQuery) QueryRow added in v0.2.0

func (p *PreparedNamedQuery) QueryRow(db *DB, arg interface{}) (pgx.Row, error)

func (*PreparedNamedQuery) Select added in v0.2.0

func (p *PreparedNamedQuery) Select(db *DB, target interface{}, arg interface{}) error

type PreparedQuery added in v0.2.0

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

func Prepare added in v0.2.0

func Prepare(sql string) *PreparedQuery

Prepares a traditional SQL-query with $1, $2, $3 placeholders NOTE: This does not use the pgx way to prepare queries under the hood

func (*PreparedQuery) Exec added in v0.2.0

func (p *PreparedQuery) Exec(db *DB, args ...interface{}) (pgconn.CommandTag, error)

func (*PreparedQuery) Get added in v0.2.0

func (p *PreparedQuery) Get(db *DB, target interface{}, args ...interface{}) error

func (*PreparedQuery) Query added in v0.2.0

func (p *PreparedQuery) Query(db *DB, args ...interface{}) (pgx.Rows, error)

func (*PreparedQuery) QueryRow added in v0.2.0

func (p *PreparedQuery) QueryRow(db *DB, args ...interface{}) (pgx.Row, error)

func (*PreparedQuery) Select added in v0.2.0

func (p *PreparedQuery) Select(db *DB, target interface{}, args ...interface{}) error

Directories

Path Synopsis
internal
lexer
The job of this package is to parse the queries and replace placeholder values with the real values
The job of this package is to parse the queries and replace placeholder values with the real values

Jump to

Keyboard shortcuts

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