pgproc

package module
v0.0.0-...-5ceb0f9 Latest Latest
Warning

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

Go to latest
Published: May 9, 2017 License: MIT Imports: 8 Imported by: 0

README

Build Status

Coverage Status

pgproc - Easily call PostgreSQL procedures from Go (golang)

If you are using PostgreSQL server programming functionalities (user-defined procedures and data types essentially) and want to access these functions from your Go programs, this library is made for you.

Thanks to both PostgreSQL and Go reflection APIs, it is possible to automatically map types of parameters and returned values between both worlds.

The main advantage of the pgproc library is to be able to call in a single pair of lines a PostgreSQL stored procedure and get its result in a Go native of struct variable.

This library is at an early development stage, please have a look at the tests files to see the covered cases.

Install

$ go get github.com/feloy/pgproc

Usage

package main

import (
        "fmt"
        "github.com/feloy/pgproc"
)

var (
        user     = "myuser"
        password = "mysecret"
        host     = "localhost"
        dbname   = "mydb"
        base     *pgproc.PgProc
)

func main() {
        conninfo := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
                user, password, host, dbname)
        base, err := pgproc.NewPgProc(conninfo)
        if err != nil {
                panic("cannot connect to database")
        }

        // Call an SQL procedure returning an integer value
        var val int
        base.Call(&val, "public", "age_of_captain")
        fmt.Printf("The captain is %d years old\n", val)

        // Call an SQL procedures returning a composite value
	var str struct {
	        Age  int
                Name string
        }
	base.Call(&str, "public", "get_captain_info")
	fmt.Println(str)

}

results in:

$ go run main.go
The captain is 42 years old
{42 Ford Prefect}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DateMinusInfinity = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
	DateInfinity      = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
)

Functions

func ScanCompositeRow

func ScanCompositeRow(row *sql.Row, rt *returnType, result interface{}) error

func ScanCompositeRows

func ScanCompositeRows(rows *sql.Rows, rt *returnType, result interface{}) error

Types

type PgProc

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

func NewPgProc

func NewPgProc(conninfo string) (*PgProc, error)

NewPgProc creates a new connection to a PostgreSQL database

func (*PgProc) Call

func (p *PgProc) Call(result interface{}, schema string, proc string, params ...interface{}) error

Call calls a PostgreSQL procedure and stores the result

func (*PgProc) SetConcurrency

func (p *PgProc) SetConcurrency(n int)

Jump to

Keyboard shortcuts

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