jet

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

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

Go to latest
Published: Jan 5, 2014 License: MIT Imports: 13 Imported by: 15

README

Jet is a super-flexible lightweight SQL interface (GoDoc)

Features:

  • En/decode custom types using ComplexValue
  • Prepared statement LRU cache
  • Unpack query results to any format
  • Can expand queries for map and slice arguments (e.g. $1 to $1, $2, $3, useful for hstore or set membership queries)
  • Serializes hstore columns to maps
  • Simple migration API
  • Customizable column name mapper

Open

db, err := jet.Open("postgres", "...")

Insert Rows

db.Query(`INSERT INTO "fruits" ( "name", "price" ) VALUES ( $1, $2 )`, "banana", 2.99).Run()

Run is Jet's Exec equivalent and is used instead of Rows() when no return values are expected

Query Rows

var rows []*struct{
  Name  string
  Price int
}
db.Query(`SELECT * FROM "fruits"`).Rows(&rows)

Jet's column mapper is very powerful. It tries to map the columns to any value you provide. You're not required to use a fixed output format. In this case rows could be anything e.g struct, *struct, []struct, []*struct, Type, *Type, []Type, []*Type even map[string]interface{} or just simple values like int or *int. You get the idea.

Hstore

Jet can also deserialize hstore columns for you. In this case the header column is a hstore value.

var out struct{
  Header  map[string]interface{}
  Body    string
}
db.Query(`SELECT * FROM "emails"`).Rows(&out)

Map and Slice Expansion

If you want to do e.g. hstore inserts or set membership queries, Jet can automatically expand the query and adjust the argument list for you.

  • Maps expand to k1, v1, k2, v2, ...
  • Slices expand to v1, v2, ...

Passing in a map argument

db.Query(`INSERT INTO "emails" ( "header", "body" ) VALUES ( hstore(ARRAY[ $1 ]), $2 )`, aMap, aBody).Run()

will expanded the query to

INSERT INTO "emails" ( "header", "body" ) VALUES ( hstore(ARRAY[ $1, $2, $3, $4 ... ]), $5 )

Passing in a slice argument

db.Query(`SELECT * FROM "files" WHERE "files"."name" IN ( $1 )`, aSlice)

will expand the query to

SELECT * FROM "files" WHERE "files"."name" IN ( $1, $2, $3, ... )

Documentation

Index

Constants

View Source
const (
	TableName  string = "migrations"
	ColumnName string = "version"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnConverter

type ColumnConverter interface {
	ColumnToFieldName(col string) string
}

ColumnConverter converts between struct field names and database row names

var SnakeCaseConverter ColumnConverter = &snakeConv{}

SnakeCaseConverter converts column names from snake_case to CamelCase field names.

type ComplexValue

type ComplexValue interface {
	Encode() interface{}

	// Decode receives a plain value to decode, never a pointer.
	Decode(v interface{}) error
}

ComplexValue implements methods for en/decoding custom values to a format the driver understands.

type Db

type Db struct {
	*sql.DB

	// LogFunc is the log function to use for query logging.
	// Defaults to nil.
	LogFunc LogFunc

	// The column converter to use.
	// Defaults to SnakeCaseConverter.
	ColumnConverter ColumnConverter
	// contains filtered or unexported fields
}

func Open

func Open(driverName, dataSourceName string) (*Db, error)

Open opens a new database connection.

func (*Db) Begin

func (db *Db) Begin() (*Tx, error)

Begin starts a new transaction

func (*Db) Query

func (db *Db) Query(query string, args ...interface{}) Runnable

Query creates a prepared query that can be run with Rows or Run.

func (*Db) SetMaxCachedStatements

func (db *Db) SetMaxCachedStatements(n int)

SetMaxCachedStatements sets the max number of statements to cache in the LRU. The default is 500.

type LogFunc

type LogFunc func(queryId, query string, args ...interface{})

LogFunc can be set on the Db instance to allow query logging.

type Migration

type Migration struct {
	Up   string
	Down string
	// contains filtered or unexported fields
}

Migration represents a migration step with up and down SQL strings.

type Runnable

type Runnable interface {
	// Run runs the query without returning results
	Run() error
	// Rows runs the query writing the rows to the specified map or struct array.
	Rows(v interface{}) error
}

type Stmts

type Stmts struct {
	CreateTableSQL   string
	SelectVersionSQL string
	InsertVersionSQL string
	UpdateVersionSQL string
}

type Suite

type Suite struct {
	Migrations []*Migration
	Stmts      *Stmts
	// contains filtered or unexported fields
}

func (*Suite) Add

func (s *Suite) Add(m *Migration)

Add adds a migration step to the suite.

func (*Suite) AddSQL

func (s *Suite) AddSQL(up, down string)

AddSQL is a shorthand for Add taking strings for the up and down SQL.

func (*Suite) Migrate

func (s *Suite) Migrate(db *Db) (int, int, error)

Migrate applies all migrations.

func (*Suite) Reset

func (s *Suite) Reset(db *Db) (int, int, error)

Reset rolls back all migrations

func (*Suite) Rollback

func (s *Suite) Rollback(db *Db) (int, int, error)

Rollback rolls back 1 step.

func (*Suite) Run

func (s *Suite) Run(db *Db, up bool, maxSteps int) (int, int, error)

Run runs the entire migration suite. If up is false, it will run in reverse. It returns the current migration id, the number of steps applied in this run and an error (if one occurred).

func (*Suite) Step

func (s *Suite) Step(db *Db) (int, int, error)

Step applies 1 migration (upward).

type Tx

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

Tx represents a transaction instance. It can be created using Begin on the *Db object.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits the transaction

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) Runnable

Query creates a prepared query that can be run with Rows or Run.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback rolls back the transaction

Jump to

Keyboard shortcuts

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