data

package module
v0.0.0-...-afa2fd6 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2018 License: MIT Imports: 6 Imported by: 0

README

data

A simple postgres database driver extension to provide simplified query and transaction execution

Created for the sake of learning and memes

Usage

Using this is actually really simple, you just need to follow these steps:

Connect

Connect to the database. There's an exported Connect function just pass in the url string form.

Prepare

You can either manually roll your own Statement objects, or use the Prepare function.
The prepare function takes in your query along with any arguments which should be injected into it.

Execute

Either execute your transactions, i.e. your inserts, updates, deletes, Or your queries which return data.
For single statement operations, you may prepare and execute/query in one step using the PrepareAnd... methods.
You can either query for a single row, or multiple. It's up to you.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRows = errors.New("Query returned no rows")
View Source
var ErrNoTxStarted = errors.New("No transactions in progress")
View Source
var ErrPriorTxIssue = errors.New("Transaction was rolled back due to issues with underlying transactions")

Functions

func BeginTx

func BeginTx() error

BeginTx starts a transaction

func CommitTx

func CommitTx() error

CommitTx commits the last in progress transaction

func Connect

func Connect(url string) error

Connect takes in a valid psql connection string and opens the connection

func ExecuteStatements

func ExecuteStatements(stmts ...Statement) error

ExecuteStatements allows the user to execute >= 1 statement in a single sql transaction

func ExecuteTx

func ExecuteTx() error

ExecuteTx determines whether a commit or rollback needs to take place by checking whether any of the components which make up the latest transaction produced an error. A commit occurs on no error, otherwise a rollback takes place

func IsConnected

func IsConnected() bool

IsConnected pings the database and returns true if the connection is still active

func PrepareAndExecute

func PrepareAndExecute(query string, args ...interface{}) (sql.Result, error)

PrepareAndExecute performs the actions of Prepare and Execute a single statement in one step rather than forcing the user to manually Prepare a statement.

func PrepareAndQuery

func PrepareAndQuery(query string, args ...interface{}) (*sql.Rows, error)

PrepareAndQuery performs the actions of Prepare and Query a single statement in one step rather than forcing the user to manually Prepare a statement.

func Query

func Query(stmt Statement) (*sql.Rows, error)

Query allows the user to perform a query where >= 1 rows are expected back It simply returns the rows so that the user may manipulate the data as they please

func QueryRow

func QueryRow(stmt Statement, data ...interface{}) error

QueryRow allows the user to perform an sql query where only one row is expected The results of the query are put into the passed in data interfaces

func RollbackTx

func RollbackTx() error

RollbackTx rolls back the last in progress transaction

Types

type Connection

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

func BuildConnection

func BuildConnection(connection string) (*Connection, error)

BuildConnection takes in a connection string and builds a wrapped connection from it

func (*Connection) BeginTx

func (c *Connection) BeginTx() error

BeginTx starts a transaction

func (*Connection) CommitTx

func (c *Connection) CommitTx() error

CommitTx commits the last in progress transaction

func (*Connection) ExecuteStatements

func (c *Connection) ExecuteStatements(stmts ...Statement) error

ExecuteStatements allows the user to execute >= 1 statement in a single sql transaction

func (*Connection) ExecuteTx

func (c *Connection) ExecuteTx() error

ExecuteTx determines whether a commit or rollback needs to take place by checking whether any of the components which make up the latest transaction produced an error. A commit occurs on no error, otherwise a rollback takes place. The boolean value returned is an indicator of whether the transaction attempted to commit

func (*Connection) IsConnected

func (c *Connection) IsConnected() bool

IsConnected pings the database and returns true if the connection is still active

func (*Connection) PrepareAndExecute

func (c *Connection) PrepareAndExecute(query string, args ...interface{}) (sql.Result, error)

PrepareAndExecute performs the actions of Prepare and Execute a single statement in one step rather than forcing the user to manually Prepare a statement.

func (*Connection) PrepareAndQuery

func (c *Connection) PrepareAndQuery(query string, args ...interface{}) (*sql.Rows, error)

PrepareAndQuery performs the actions of Prepare and Query a single statement in one step rather than forcing the user to manually Prepare a statement.

func (*Connection) Query

func (c *Connection) Query(stmt Statement) (*sql.Rows, error)

Query allows the user to perform a query where >= 1 rows are expected back It simply returns the rows so that the user may manipulate the data as they please

func (*Connection) QueryRow

func (c *Connection) QueryRow(stmt Statement, data ...interface{}) error

QueryRow allows the user to perform an sql query where only one row is expected The results of the query are put into the passed in data interfaces

func (*Connection) RollbackTx

func (c *Connection) RollbackTx() error

RollbackTx rolls back the last in progress transaction

type Statement

type Statement struct {
	Query string
	Args  []interface{}
}

Statement is a struct which represents a prepared statement. Calling Prepare allows the user to not have to construct this manually.

func Prepare

func Prepare(query string, args ...interface{}) Statement

Prepare creates a Statement which may be used in any of the Execute or Query functions

func (Statement) Hash

func (s Statement) Hash() uint64

Hash creates a uint64 representation of the statement

Jump to

Keyboard shortcuts

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