pgwp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2016 License: MIT Imports: 4 Imported by: 0

README

####SQL worker pool around sqlx in Go.

License MIT Build Status Go Report Card GoDoc Coverage Status

#####Usage:

ws := 10 // 10 workers/connections.
qlen := 20 // 20 channel queue length before block.
pool, err := pgwp.Connect("postgres", "connection string", ws, qlen)
// err check

var list []Item
pool.Select(&list, "SELECT * FROM people WHERE name=$1 LIMIT 10", "lisa" )

Credits:

https://github.com/jmoiron/sqlx

license:

The MIT License (MIT)

Copyright (c) 2016 Angel Del Castillo

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 pgwp contains a wrapper around sqlx for pooling.

The MIT License (MIT)

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.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Timeout controls query execution timeout.
	Timeout = time.Duration(4 * time.Second)

	// ErrTimeout is returned when a query exceeds Timeout execution.
	ErrTimeout = errors.New("pgwp: operation timeout")
)

Functions

This section is empty.

Types

type ConnectFunc

type ConnectFunc func() (*sqlx.DB, error)

ConnectFunc type is called when a new connection is required.

type ExecFunc

type ExecFunc func(db *sqlx.DB) error

ExecFunc defines behavior for functions that return error

type Pool

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

Pool type contains all workers with his respectives connections.

func Connect

func Connect(driver, url string, workers, queueLen int) (*Pool, error)

Connect handle sqlx.Connect function. Receives workers and queue size.

DEPRECATED. Use Open func.

func Open

func Open(connectionFunc ConnectFunc, workers, queue int) (*Pool, error)

Open return a new pool. Receive a database connection function for later reuse.

workers define number of connections open to database. queue defines length channel for queries before block. It's useful for control over application demand.

func (*Pool) Close

func (p *Pool) Close()

Close closes all connections.

func (*Pool) Get

func (p *Pool) Get(result interface{}, query string, args ...interface{}) error

Get wrapper for sqlx.Get.

func (*Pool) MustExec

func (p *Pool) MustExec(query string, args ...interface{}) sql.Result

MustExec wrapper for sqlx.MustExec.

func (*Pool) Run

func (p *Pool) Run(fn func(db *sqlx.DB) error) error

Run purpose is to allow database operations if are not yet implemented. for general purposes see other funcs before using it.

func (*Pool) Select

func (p *Pool) Select(results interface{}, query string, args ...interface{}) error

Select wrapper for sqlx.Select.

type QueryFunc

type QueryFunc func(db *sqlx.DB) sql.Result

QueryFunc defines behavior for functions that return sql.Result

type ResultErr

type ResultErr struct {
	Error error
}

ResultErr satisfies sql.Result

It's used when a timeout occurs

func (*ResultErr) LastInsertId

func (r *ResultErr) LastInsertId() (int64, error)

LastInsertId satisfies sql.Result

func (*ResultErr) RowsAffected

func (r *ResultErr) RowsAffected() (int64, error)

RowsAffected satisfies sql.Result

type Worker

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

Worker defines database connection.

func (*Worker) Exec

func (o *Worker) Exec(fn ExecFunc, timeout time.Duration) error

Exec runs a ExecFunc on worker connection.

func (*Worker) Query

func (o *Worker) Query(fn QueryFunc, timeout time.Duration) sql.Result

Query runs a QueryFunc on worker connection.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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