Documentation
¶
Overview ¶
utility package for working with PostgreSQL arrays and composite types.
Index ¶
- Variables
- func Col(name string, k Valstructor) *col
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) Delete(vs ...RecordValue) error
- func (db *DB) From(name string) *Query
- func (db *DB) Insert(vs ...RecordValue) error
- func (db *DB) New(name string, args interface{}) (RecordValue, error)
- func (db *DB) Query(q string, vals ...interface{}) (*Rows, error)
- func (db *DB) Relation(name string) (*Relation, error)
- func (db *DB) Relations() (rels map[string]*Relation, err error)
- func (db *DB) Update(vs ...RecordValue) error
- func (db *DB) Upsert(vs ...RecordValue) error
- type IterValue
- type MapValue
- type Query
- func (q *Query) And(w string, params ...interface{}) *Query
- func (q *Query) ArrayAgg(name string) (Value, error)
- func (q *Query) Avg(name string) (Value, error)
- func (q *Query) Count() (int64, error)
- func (q *Query) Fetch() ([]RecordValue, error)
- func (q *Query) FetchOne() (RecordValue, error)
- func (q *Query) For(v RecordValue) *Query
- func (q *Query) Get(pk interface{}) (RecordValue, error)
- func (q *Query) Limit(n int) *Query
- func (q *Query) Max(name string) (Value, error)
- func (q *Query) Min(name string) (Value, error)
- func (q *Query) Offset(n int) *Query
- func (q *Query) Sum(name string) (Value, error)
- func (q *Query) Where(w string, params ...interface{}) *Query
- type RecordValue
- type Relation
- type Rows
- type Tx
- func (tx *Tx) Delete(vs ...RecordValue) error
- func (tx *Tx) From(name string) *Query
- func (tx *Tx) Insert(vs ...RecordValue) error
- func (tx *Tx) Query(q string, vals ...interface{}) (*Rows, error)
- func (tx *Tx) Relations() (rels map[string]*Relation, err error)
- func (tx *Tx) Update(vs ...RecordValue) error
- func (tx *Tx) Upsert(vs ...RecordValue) (err error)
- type Valstructor
- type Value
- func BigInt(data interface{}) (Value, error)
- func Bool(data interface{}) (Value, error)
- func Bytea(data interface{}) (Value, error)
- func Double(data interface{}) (Value, error)
- func HStore(data interface{}) (Value, error)
- func Integer(data interface{}) (Value, error)
- func Real(data interface{}) (Value, error)
- func SmallInt(data interface{}) (Value, error)
- func Text(data interface{}) (Value, error)
- func Timestamp(data interface{}) (Value, error)
Constants ¶
This section is empty.
Variables ¶
var ( Decimal = Numeric Int = Integer Int2 = SmallInt Int4 = Integer Int8 = BigInt Serial = Integer BigSerial = BigInt )
Value aliases
Functions ¶
func Col ¶
func Col(name string, k Valstructor) *col
allows you name fields before passing them to the Record constructor
myRecordKind := Record( Col("name", Text) ) v := myRecordKind()
Then any values created with names can be fetched by name from the value:
name := v.Get("name").String()
Types ¶
type DB ¶
wrapper type around sql.DB adds methods for getting meta infomation from the db (via Relations), automatically building Value types and convience functions for dealing with RecordValues (via *Rowss)
func Open ¶
Analog of sql.Open that returns a *DB requires a "postgres" driver (lib/pq) is registered
func (*DB) Delete ¶
func (db *DB) Delete(vs ...RecordValue) error
DELETE the given RecordValue(s) into the db runs multiple INSERTs within a transaction
func (*DB) From ¶
Create a Query for a named relation any errors are defered until an actual query is performed
func (*DB) Insert ¶
func (db *DB) Insert(vs ...RecordValue) error
INSERT the given RecordValue(s) into the db runs multiple INSERTs within a transaction
func (*DB) New ¶
func (db *DB) New(name string, args interface{}) (RecordValue, error)
Create a new RecordValue for the named relation
func (*DB) Update ¶
func (db *DB) Update(vs ...RecordValue) error
UPDATE the given RecordValue(s) into the db runs multiple INSERTs within a transaction
func (*DB) Upsert ¶
func (db *DB) Upsert(vs ...RecordValue) error
INSERT OR UPDATE the given RecordValue(s) into the db runs multiple INSERTs within a transaction
type IterValue ¶
type IterValue interface { // IterValue also fulfils Value Value // Fetch all sub-values as a slice Values() []Value // Fetch a sub-value by slice index ValueAt(int) Value // Add a value to the list of sub-values Append(interface{}) error }
values that have a slice of sub-values will impliment this interface
type MapValue ¶
type MapValue interface { // MapValue but also fulfil Value Value // fetch all sub-values as a map Map() map[string]Value // Fetch a sub-value by name ValueBy(name string) Value // Equivilent to calling ValueBy(name).Val() Get(name string) interface{} // Equivilent to calling ValueBy(name).Scan(src) Set(name string, src interface{}) error }
values that have named sub-values (like Record) this interface will be implimented
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
the Query type is used to build simple/common queries most methods return a new Query so they can be chained with any errors being defered until a call that causes a db.Query
func (*Query) Fetch ¶
func (q *Query) Fetch() ([]RecordValue, error)
perform a SELECT for the current query and return a slice of RecordValues
func (*Query) FetchOne ¶
func (q *Query) FetchOne() (RecordValue, error)
perform a SELECT and return a single RecordValue for this query will return nil if no rows where returned
func (*Query) Get ¶
func (q *Query) Get(pk interface{}) (RecordValue, error)
create a new Query with a WHERE filter for the relation's primary key and the call FetchOne
type RecordValue ¶
type RecordValue interface { IterValue // fetch all sub-values as a map Map() map[string]Value // Fetch a sub-value by name ValueBy(name string) Value // Equivilent to calling ValueBy(name).Val() Get(name string) interface{} // Equivilent to calling ValueBy(name).Scan(src) Set(name string, src interface{}) error // Return the relation (if any) that this Record belongs to Relation() *Relation // Set the parent relation for this RecordValue SetRelation(*Relation) }
RecordValuess are both MapValues and IterValues
type Relation ¶
type Relation struct { Name string // contains filtered or unexported fields }
Relation holds column and reference info about a relation. Usually inferred from the database. See Relation methods on DB
func (*Relation) Cols ¶
func (r *Relation) Cols() []*col
return list of column data in the order postgresql expects them
func (*Relation) New ¶
func (r *Relation) New(data interface{}) (RecordValue, error)
return a new RecordValue that represents a row from this relation
type Rows ¶
wrapper type around sql.Rows adds the ScanRecord method to make it easier to Scan Row Values
func (*Rows) ScanRecord ¶
func (rs *Rows) ScanRecord(v RecordValue) error
Similar to sql.Rows#Scan but scans all values into a RecordValue
type Tx ¶
wrapper type around sql.Tx Adds methods for INSERTing, UPDATEing and DELETEing RecordValues
func (*Tx) From ¶
Create a Query for a named relation any errors are defered until an actual query is performed
func (*Tx) Upsert ¶
func (tx *Tx) Upsert(vs ...RecordValue) (err error)
UPDATE or INSERT RecordValue(s)
type Valstructor ¶
A `Valstructor` creates and initializes a new `Value`
func Array ¶
func Array(el Valstructor) Valstructor
func Char ¶
func Char(n int) Valstructor
func Numeric ¶
func Numeric(prec int, scale int) Valstructor
stored as string currently TODO: use some Value of arbitary precision for this
func Record ¶
func Record(cols ...*col) Valstructor
same as Row, but takes a list of Col's as arguments that allow you to name the fields
func VarChar ¶
func VarChar(n int) Valstructor
type Value ¶
type Value interface { // Return wether this Value is currently NULL IsNull() bool // Return a representation of this Value as a string String() string // Return the underlying data as an interface Val() interface{} // Values are Valuable driver.Valuer // Values are Scannable sql.Scanner // contains filtered or unexported methods }
Value is the interface for all value kinds this interface along with IterValue and MapValue will allow you to access any combination of types