ssql

package module
v0.0.0-...-954c267 Latest Latest
Warning

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

Go to latest
Published: May 9, 2018 License: MIT Imports: 7 Imported by: 0

README

SSQL

SSQL is a thin layer over sqlx and goyesql.

When establishing a database connection, in addition to the arguments accepted by sql.Open, SSQL accepts a pattern. The pattern is passed to zglob to obtain a list of files, which is then parsed using goyesql. SSQL then allows you refer to the parsed SQL statements by name - in the form "[base filename].[statement name]" when calling sql/sqlx functions.

Installation

$ go get -u gitlab.com/shasderias/ssql

Usage

Create one or more SQL files.

-- /pkg/foo/sql/foo.sql

-- name: All
SELECT *
FROM foo;

-- name: Get
SELECT *
FROM foo
WHERE id = $1;
-- /pkg/bar/sql/bar.sql

-- name: All
SELECT *
FROM bar;

-- name: Get
SELECT *
FROM bar
WHERE id = $1;

Call ssql.Open.

// ssql.Open's first 2 arguments are the same as sql.Open (driver, database URL)
// ssql.Open's last argument is a pattern that is passed to zglob for matching
db, _ := ssql.Open("postgres", "postgres://mydb:mydb@localhost/mydb", "./pkg/**/.sql")

foo := Foo{}
// Get has the same semantics as sqlx's Get, except that a name should be passed
// as the 2nd argument
_ = db.Get(&foo, "foo.Get", 1)

bars := []Bar{}
// Select has the same semantics as sqlx's Select, except that a name should be
// passed as the 2nd argument
_ = db.Select(&bars, "bar.All")

thing := Thing{}
// The underlying sqlx.DB can be accessed via DB
_ = db.DB().Select(&thing, "SELECT * FROM things WHERE id = $1", 1)

Implemented Methods

The following methods from sql/sqlx have been implemented.

type DB interface {
	Query(name string, args ...interface{}) (*sql.Rows, error)
	QueryRow(name string, args ...interface{}) *sql.Row
	Get(dest interface{}, name string, args ...interface{}) error
	Select(dest interface{}, name string, args ...interface{}) error
	Exec(name string, args ...interface{}) (sql.Result, error)
	NamedExec(name string, arg interface{}) (sql.Result, error)
	Beginx() (Tx, error)
	DB() *sqlx.DB
}

type Tx interface {
	Query(name string, args ...interface{}) (*sql.Rows, error)
	QueryRow(name string, args ...interface{}) *sql.Row
	Get(dest interface{}, name string, args ...interface{}) error
	Select(dest interface{}, name string, args ...interface{}) error
	Exec(name string, args ...interface{}) (sql.Result, error)
	NamedExec(name string, arg interface{}) (sql.Result, error)
	Commit() error
	Rollback() error
}

Querier Interface

For code that may or may not be executed in a transaction, the ssql.Querier interface defines the subset of functions common to DB and Tx.

type Querier interface {
	Query(name string, args ...interface{}) (*sql.Rows, error)
	QueryRow(name string, args ...interface{}) *sql.Row
	Get(dest interface{}, name string, args ...interface{}) error
	Select(dest interface{}, name string, args ...interface{}) error
	Exec(name string, args ...interface{}) (sql.Result, error)
	NamedExec(name string, arg interface{}) (sql.Result, error)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	Query(name string, args ...interface{}) (*sql.Rows, error)
	QueryRow(name string, args ...interface{}) *sql.Row
	Get(dest interface{}, name string, args ...interface{}) error
	Select(dest interface{}, name string, args ...interface{}) error
	Exec(name string, args ...interface{}) (sql.Result, error)
	NamedExec(name string, arg interface{}) (sql.Result, error)
	Beginx() (Tx, error)
	DB() *sqlx.DB
}

func Open

func Open(driverName, dataSourceName, sqlPath string) (DB, error)

type ErrStmtNotFound

type ErrStmtNotFound string

func (ErrStmtNotFound) Error

func (err ErrStmtNotFound) Error() string

type Querier

type Querier interface {
	Query(name string, args ...interface{}) (*sql.Rows, error)
	QueryRow(name string, args ...interface{}) *sql.Row
	Get(dest interface{}, name string, args ...interface{}) error
	Select(dest interface{}, name string, args ...interface{}) error
	Exec(name string, args ...interface{}) (sql.Result, error)
	NamedExec(name string, arg interface{}) (sql.Result, error)
}

type SqlxDB

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

func (SqlxDB) Beginx

func (db SqlxDB) Beginx() (Tx, error)

func (SqlxDB) DB

func (db SqlxDB) DB() *sqlx.DB

func (SqlxDB) Exec

func (db SqlxDB) Exec(name string, args ...interface{}) (sql.Result, error)

func (SqlxDB) Get

func (db SqlxDB) Get(dest interface{}, name string, args ...interface{}) error

func (SqlxDB) NamedExec

func (db SqlxDB) NamedExec(name string, arg interface{}) (sql.Result, error)

func (SqlxDB) Query

func (db SqlxDB) Query(name string, args ...interface{}) (*sql.Rows, error)

func (SqlxDB) QueryRow

func (db SqlxDB) QueryRow(name string, args ...interface{}) *sql.Row

func (SqlxDB) Select

func (db SqlxDB) Select(dest interface{}, name string, args ...interface{}) error

type SqlxTx

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

func (SqlxTx) Commit

func (tx SqlxTx) Commit() error

func (SqlxTx) Exec

func (tx SqlxTx) Exec(name string, args ...interface{}) (sql.Result, error)

func (SqlxTx) Get

func (tx SqlxTx) Get(dest interface{}, name string, args ...interface{}) error

func (SqlxTx) NamedExec

func (tx SqlxTx) NamedExec(name string, arg interface{}) (sql.Result, error)

func (SqlxTx) Query

func (tx SqlxTx) Query(name string, args ...interface{}) (*sql.Rows, error)

func (SqlxTx) QueryRow

func (tx SqlxTx) QueryRow(name string, args ...interface{}) *sql.Row

func (SqlxTx) Rollback

func (tx SqlxTx) Rollback() error

func (SqlxTx) Select

func (tx SqlxTx) Select(dest interface{}, name string, args ...interface{}) error

type Tx

type Tx interface {
	Query(name string, args ...interface{}) (*sql.Rows, error)
	QueryRow(name string, args ...interface{}) *sql.Row
	Get(dest interface{}, name string, args ...interface{}) error
	Select(dest interface{}, name string, args ...interface{}) error
	Exec(name string, args ...interface{}) (sql.Result, error)
	NamedExec(name string, arg interface{}) (sql.Result, error)
	Commit() error
	Rollback() error
}

Jump to

Keyboard shortcuts

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