model

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2021 License: Apache-2.0 Imports: 23 Imported by: 13

Documentation

Overview

The `model` package essentially provides a lightweight object relational model (ORM) based on sqlite3 intended to support the needs of the `container` package. Each entity (table) is modeled by a struct. Each field (column) is Described using tags:

 sql: "-"
    The field is omitted.
`sql:"pk"`
    The primary key.
`sql:"pk(field;field;..)"`
    The generated primary key.
`sql:"key"`
    The field is part of the natural key.
`sql:"fk(table flags...)"`
    Foreign key with optional flags:
      +must = referenced model must exist.
      +cascade = cascade delete.
`sql:"unique(G)"`
    Unique index. `G` = unique-together fields.
`sql:"index(G)"`
    Non-unique index. `G` = unique-together fields.
`sql:"const"`
    The field is immutable and not included on update.
`sql:"virtual"`
    The field is read-only and managed internally by the DB.
`sql:"dn"`
    The field detail level.  n = level number.
`sql:incremented`
    The field is auto-incremented.

Each struct must implement the `Model` interface. Basic CRUD operations may be performed on each model using the `DB` interface which together with the `Model` interface provides value-added features and optimizations.

Examples:

Define a model.

type Person struct {
    ID    string `sql:"pk"`
    First string `sql:"key"`
    Last  string `sql:"key"
    Age   int
}

func (p *Person) Pk() string {
    return p.ID
}

Insert the model:

person := &Person{
    First: "Elmer",
    Last:  "Fudd",
    Age: 55,
}

err := DB.Insert(person)

In the event the primary key (PK) field is not populated, the DB will derive (generate) its value as a sha1 of the natural key fields.

Update the model:

person.Age = 62
err := DB.Update(person)

Delete the model by natural key:

person := &Person{
    First: "Elmer",
    Last:  "Fudd",
}

err := DB.Delete(person)

Get (fetch) a single model by natural key. This will populate the fields with data from the DB.

 person := &Person{
     First: "Elmer",
     Last:  "Fudd",
 }

err := DB.Get(person)

List (fetch) all models.

persons := []Person{}
err := DB.List(&persons, ListOptions{})

List (fetch) specific models. The `ListOptions` may be used to qualify or paginate the List() result set. All predicates may be combined.

Count (only):

err := DB.List(&persons, ListOptions{Count: true})

Paginate the result:

err := DB.List(
    &persons,
    ListOptions{
        Page: {
            Offset: 3, // page 3.
            Limit: 10, // 10 models per page.
        },
    })

List specific models. List persons with the last name of "Fudd" and legal to vote.

err := DB.List(
    &persons,
    ListOptions{
        Predicate: And(
            Eq("Name", "Fudd"),
            Gt("Age": 17),
        },
    })

Index

Constants

View Source
const (
	// SQL tag.
	Tag = "sql"
	// Max detail level.
	MaxDetail = 9
)

Variables

View Source
var (
	Started uint8 = 0x00
	Parity  uint8 = 0x01
	Error   uint8 = 0x02
	End     uint8 = 0x04
	Created uint8 = 0x10
	Updated uint8 = 0x20
	Deleted uint8 = 0x40
)

Event Actions.

View Source
var (
	// Must have PK.
	MustHavePkErr = errors.New("must have PK field")
	// Parameter must be pointer error.
	MustBePtrErr = errors.New("must be pointer")
	// Must be slice pointer.
	MustBeSlicePtrErr = errors.New("must be slice pointer")
	// Parameter must be struct error.
	MustBeObjectErr = errors.New("must be object")
	// Field type error.
	FieldTypeErr = errors.New("field type must be (int, str, bool")
	// PK field type error.
	PkTypeErr = errors.New("pk field must be (int, str)")
	// Generated PK error.
	GenPkTypeErr = errors.New("PK field must be `str` when generated")
	// Invalid field referenced in predicate.
	PredicateRefErr = errors.New("predicate referenced unknown field")
	// Invalid predicate for type of field.
	PredicateTypeErr = errors.New("predicate type not valid for field")
	// Invalid predicate value.
	PredicateValueErr = errors.New("predicate value not valid")
	// Invalid detail level.
	DetailErr = errors.New("detail level must be <= MaxDetail")
)

Errors

View Source
var DefaultDetail = 0

The default (field) detail level when listing models. Applications using custom detail levels must adjust to highest level used. Example:

func init() {
  model.DefaultDetail = 2
}
View Source
var DeleteSQL = `
DELETE FROM {{.Table}}
WHERE
{{ .Pk.Name }} = {{ .Pk.Param }}
;
`
View Source
var DetailRegex = regexp.MustCompile(`(d)([0-9]+)`)

Regex used for detail.

View Source
var FkRegex = regexp.MustCompile(`(fk)(\()(.+)(\))`)

Regex used for `fk(table)` tags.

View Source
var GetSQL = `` /* 143-byte string literal not displayed */
View Source
var IndexDDL = `` /* 141-byte string literal not displayed */
View Source
var IndexRegex = regexp.MustCompile(`(index)(\()(.+)(\))`)

Regex used for `index(group)` tags.

View Source
var InsertSQL = `` /* 196-byte string literal not displayed */

SQL templates.

View Source
var LabelSQL = `` /* 246-byte string literal not displayed */

Label SQL.

View Source
var ListSQL = `` /* 434-byte string literal not displayed */
View Source
var NotFound = sql.ErrNoRows

Errors.

View Source
var PkRegex = regexp.MustCompile(`(pk)((\()(.+)(\)))?`)

Regex used for `pk(fields)` tags.

View Source
var TableDDL = `` /* 177-byte string literal not displayed */

DDL templates.

View Source
var UniqueRegex = regexp.MustCompile(`(unique)(\()(.+)(\))`)

Regex used for `unique(group)` tags.

View Source
var UpdateSQL = `` /* 159-byte string literal not displayed */

Functions

func Describe added in v0.3.6

func Describe(model Model) (s string)

Model description.

Types

type AndPredicate

type AndPredicate struct {
	CompoundPredicate
}

And predicate.

func And

func And(predicates ...Predicate) *AndPredicate

AND predicate.

func (*AndPredicate) Build

func (p *AndPredicate) Build(options *ListOptions) error

Build.

func (*AndPredicate) Expr

func (p *AndPredicate) Expr() string

Render the expression.

type Base

type Base struct {
	// Primary key (digest).
	PK string `sql:"pk"`
	// The raw json-encoded k8s resource.
	Object string `sql:""`
}

func (*Base) Pk

func (m *Base) Pk() string

Get the primary key.

type Client

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

Database client.

func (*Client) Begin

func (r *Client) Begin(labels ...string) (tx *Tx, error error)

Begin a transaction.

func (*Client) Close

func (r *Client) Close(delete bool) (err error)

Close the database. The session pool and journal are closed.

func (*Client) Count

func (r *Client) Count(model Model, predicate Predicate) (n int64, err error)

Count models.

func (*Client) Delete

func (r *Client) Delete(model Model) (err error)

Delete the model. Delegated to Tx.Delete().

func (*Client) EndWatch added in v0.1.1

func (r *Client) EndWatch(watch *Watch)

End watch.

func (*Client) Execute added in v0.3.7

func (r *Client) Execute(sql string) (result sql.Result, err error)

Execute SQL. Delegated to Tx.Execute().

func (*Client) Find added in v0.6.0

func (r *Client) Find(model interface{}, options ListOptions) (itr fb.Iterator, err error)

Find models.

func (*Client) Get

func (r *Client) Get(model Model) (err error)

Get the model.

func (*Client) Insert

func (r *Client) Insert(model Model) (err error)

Insert the model. Delegated to Tx.Insert().

func (*Client) List

func (r *Client) List(list interface{}, options ListOptions) (err error)

List models. The `list` must be: *[]Model.

func (*Client) Open

func (r *Client) Open(delete bool) (err error)

Create the database. Build the schema to support the specified models. See: Pool.Open().

func (*Client) Update

func (r *Client) Update(model Model) (err error)

Update the model. Delegated to Tx.Update().

func (*Client) Watch

func (r *Client) Watch(model Model, handler EventHandler) (w *Watch, err error)

Watch model events.

type CompoundPredicate

type CompoundPredicate struct {
	// List of predicates.
	Predicates []Predicate
}

Compound predicate.

type DB

type DB interface {
	// Open and build the schema.
	Open(bool) error
	// Close.
	Close(bool) error
	// Execute SQL.
	Execute(sql string) (sql.Result, error)
	// Get the specified model.
	Get(Model) error
	// List models based on the type of slice.
	List(interface{}, ListOptions) error
	// Find models.
	Find(interface{}, ListOptions) (fb.Iterator, error)
	// Count based on the specified model.
	Count(Model, Predicate) (int64, error)
	// Begin a transaction.
	Begin(...string) (*Tx, error)
	// Insert a model.
	Insert(Model) error
	// Update a model.
	Update(Model) error
	// Delete a model.
	Delete(Model) error
	// Watch a model collection.
	Watch(Model, EventHandler) (*Watch, error)
	// End a watch.
	EndWatch(watch *Watch)
}

Database client.

func New

func New(path string, models ...interface{}) DB

New database.

type DBTX

type DBTX interface {
	Exec(string, ...interface{}) (sql.Result, error)
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
}

Database client interface. Support model methods taking either sql.DB or sql.Tx.

type DataModel added in v0.7.0

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

DataModel. Map of definitions.

func NewModel added in v0.7.0

func NewModel(models []interface{}) (dm *DataModel, err error)

New data Model.

func (*DataModel) Add added in v0.7.0

func (r *DataModel) Add(md *Definition)

Add definition.

func (*DataModel) DDL added in v0.7.0

func (r *DataModel) DDL() (list []string, err error)

Build the DDL.

func (*DataModel) Definitions added in v0.7.0

func (r *DataModel) Definitions() (list Definitions)

Definitions.

func (*DataModel) Deleted added in v0.7.0

func (r *DataModel) Deleted(tx *Tx, model interface{}) (cascaded fb.Iterator, err error)

Find models to be (cascade) deleted.

func (*DataModel) Find added in v0.7.0

func (r *DataModel) Find(kind string) (md *Definition, found bool)

Find by kind.

func (*DataModel) FindWith added in v0.7.0

func (r *DataModel) FindWith(model interface{}) (md *Definition, found bool)

Find with model.

type Definition added in v0.7.0

type Definition struct {
	Kind   string
	Fields []*Field
	// contains filtered or unexported fields
}

Model definition.

func Inspect added in v0.7.0

func Inspect(model interface{}) (md *Definition, err error)

func (*Definition) Field added in v0.7.0

func (r *Definition) Field(name string) *Field

Field by name.

func (*Definition) Fks added in v0.7.0

func (r *Definition) Fks() []*FK

Get foreign keys for the model.

func (*Definition) IsKind added in v0.7.0

func (r *Definition) IsKind(kind string) bool

Match (case-insensitive) by kind.

func (*Definition) KeyFields added in v0.7.0

func (r *Definition) KeyFields() []*Field

Get the natural key `Fields` for the model.

func (*Definition) MutableFields added in v0.7.0

func (r *Definition) MutableFields() []*Field

Get the mutable `Fields` for the model.

func (*Definition) NewModel added in v0.7.0

func (r *Definition) NewModel() (m interface{})

New model for kind.

func (*Definition) PkField added in v0.7.0

func (r *Definition) PkField() *Field

Get the PK field.

func (*Definition) RealFields added in v0.7.0

func (r *Definition) RealFields(fields []*Field) []*Field

Get the non-virtual `Fields` for the model.

type Definitions added in v0.7.0

type Definitions []*Definition

Model definitions.

func (*Definitions) Append added in v0.7.0

func (r *Definitions) Append(md *Definition)

Append model definition.

func (*Definitions) Delete added in v0.7.0

func (r *Definitions) Delete(index int)

Delete model definition.

func (*Definitions) Head added in v0.7.0

func (r *Definitions) Head(delete bool) (md *Definition)

Head (first) model definition. Returns: nil when empty.

func (*Definitions) Pop added in v0.7.0

func (r *Definitions) Pop() (md *Definition)

Pop model definition. Returns: nil when empty.

func (*Definitions) Push added in v0.7.0

func (r *Definitions) Push(md *Definition)

Push model definition.

func (*Definitions) Reverse added in v0.7.0

func (r *Definitions) Reverse()

Reverse.

func (*Definitions) Top added in v0.7.0

func (r *Definitions) Top() (md *Definition)

Top (last) model definition. Returns: nil when empty.

type EqPredicate

type EqPredicate struct {
	SimplePredicate
}

Equals (=) predicate.

func Eq

func Eq(field string, value interface{}) *EqPredicate

New Eq (=) predicate.

func (*EqPredicate) Build

func (p *EqPredicate) Build(options *ListOptions) error

Build.

func (*EqPredicate) Expr

func (p *EqPredicate) Expr() string

Render the expression.

type Event

type Event struct {
	// ID.
	ID uint64
	// Labels.
	Labels []string
	// The event subject.
	Model Model
	// The event action (created|updated|deleted).
	Action uint8
	// The updated model.
	Updated Model
}

Model event.

func (*Event) HasLabel added in v0.6.1

func (r *Event) HasLabel(label string) bool

Get whether the event has the specified label.

func (*Event) String added in v0.3.5

func (r *Event) String() string

String representation.

type EventHandler

type EventHandler interface {
	// Watch options.
	Options() WatchOptions
	// Watch has started.
	Started(watchID uint64)
	// Parity marker.
	// The watch has delivered the initial set
	// of `Created` events.
	Parity()
	// A model has been created.
	Created(Event)
	// A model has been updated.
	Updated(Event)
	// A model has been deleted.
	Deleted(Event)
	// An error has occurred delivering an event.
	Error(error)
	// An event watch has ended.
	End()
}

Event handler.

type FK

type FK struct {
	// FK owner.
	Owner *Field
	// Target table name.
	Table string
	// Target field name.
	Field string
	// +must option enforced by constraint.
	Must bool
	// +cascade delete option.
	Cascade bool
}

FK constraint.

func (*FK) DDL

func (f *FK) DDL(field *Field) string

Get DDL.

type Field

type Field struct {
	// reflect.Type of the field.
	Type *reflect.StructField
	// reflect.Value of the field.
	Value *reflect.Value
	// Field name.
	Name string
	// SQL tag.
	Tag string
	// contains filtered or unexported fields
}

Model (struct) Field

func (*Field) AsValue

func (f *Field) AsValue(object interface{}) (value interface{}, err error)

Convert the specified `object` to a value (type) appropriate for the field.

func (*Field) DDL

func (f *Field) DDL() string

Column DDL.

func (*Field) Detail added in v0.2.5

func (f *Field) Detail() (level int)

Detail level. Defaults:

Key fields = 0.
     Other = DefaultDetail

func (*Field) Encoded added in v0.2.5

func (f *Field) Encoded() (encoded bool)

Get whether the field is `json` encoded.

func (*Field) Fk

func (f *Field) Fk() (fk *FK)

Get whether the field is a foreign key. Format: fk(table flags..) where flags are optional. Flags:

+must = referenced model must exist.
+cascade = cascade delete.

func (*Field) Incremented added in v0.4.3

func (f *Field) Incremented() bool

Get whether field is auto-incremented.

func (*Field) Index added in v0.3.7

func (f *Field) Index() []string

Get whether the field has non-unique index.

func (*Field) Key

func (f *Field) Key() bool

Get whether field is a natural key.

func (*Field) MatchDetail added in v0.2.5

func (f *Field) MatchDetail(level int) bool

Match detail level.

func (*Field) Mutable

func (f *Field) Mutable() bool

Get whether field is mutable. Only mutable fields will be updated.

func (*Field) Param

func (f *Field) Param() string

Get as SQL param.

func (*Field) Pk

func (f *Field) Pk() (matched bool)

Get whether field is the primary key.

func (*Field) Ptr

func (f *Field) Ptr() interface{}

Pointer used for Scan().

func (*Field) Pull

func (f *Field) Pull() interface{}

Pull from model. Populate the appropriate `staging` field using the model field value.

func (*Field) Push

func (f *Field) Push()

Push to the model. Set the model field value using the `staging` field.

func (*Field) Unique

func (f *Field) Unique() []string

Get whether the field is unique.

func (*Field) Validate

func (f *Field) Validate() error

Validate.

func (*Field) Virtual added in v0.2.5

func (f *Field) Virtual() bool

Get whether field is virtual. A `virtual` field is read-only and managed internally in the DB.

func (*Field) WithFields added in v0.4.3

func (f *Field) WithFields() (withFields map[string]bool)

Fields used to generate the primary key. Map of lower-cased field names. May be empty when generation is not enabled.

type FkRef added in v0.7.0

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

FK reference.

type FkRelation added in v0.7.0

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

FK relation.

func (*FkRelation) DDL added in v0.7.0

func (r *FkRelation) DDL(md *Definition) (ddl []string, err error)

Build constraint DDL.

func (*FkRelation) Definitions added in v0.7.0

func (r *FkRelation) Definitions() (list Definitions)

Model definitions dependency-sorted as needed to be created.

func (*FkRelation) Referencing added in v0.7.0

func (r *FkRelation) Referencing(md *Definition) (list []*FkRef)

Find model definitions that reference the specified definition.

type GtPredicate

type GtPredicate struct {
	SimplePredicate
}

Greater than (>) predicate.

func Gt

func Gt(field string, value interface{}) *GtPredicate

New Gt (>) predicate.

func (*GtPredicate) Build

func (p *GtPredicate) Build(options *ListOptions) error

Build.

func (*GtPredicate) Expr

func (p *GtPredicate) Expr() string

Render the expression.

type Journal

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

DB journal. Provides model watch events.

func (*Journal) Close added in v0.3.1

func (r *Journal) Close() (err error)

Close the journal. End all watches.

func (*Journal) End

func (r *Journal) End(watch *Watch)

End watch.

func (*Journal) Report added in v0.5.0

func (r *Journal) Report(staged *fb.List)

Transaction committed. Recorded (staged) events are forwarded to watches.

func (*Journal) Watch

func (r *Journal) Watch(model Model, handler EventHandler) (*Watch, error)

Watch a `watch` of the specified model. The returned watch has not been started. See: Watch.Start().

type Label

type Label struct {
	PK     string `sql:"pk(parent;kind;name)"`
	Parent string `sql:"key"`
	Kind   string `sql:"key"`
	Name   string `sql:"key"`
	Value  string `sql:""`
}

Label model

func (*Label) Equals

func (l *Label) Equals(other Model) bool

func (*Label) Labels

func (l *Label) Labels() Labels

func (*Label) Pk

func (l *Label) Pk() string

func (*Label) String

func (l *Label) String() string

type LabelPredicate

type LabelPredicate struct {
	// Labels
	Labels
	// contains filtered or unexported fields
}

Label predicate.

func Match

func Match(labels Labels) *LabelPredicate

Label predicate.

func (*LabelPredicate) Build

func (p *LabelPredicate) Build(options *ListOptions) error

Build.

func (*LabelPredicate) Expr

func (p *LabelPredicate) Expr() string

Render the expression.

func (*LabelPredicate) Kind

func (p *LabelPredicate) Kind() string

Label (parent) kind.

func (*LabelPredicate) Len

func (p *LabelPredicate) Len() int

Get the number of labels.

func (*LabelPredicate) List

func (p *LabelPredicate) List() []Label

List of labels.

func (*LabelPredicate) Pk

func (p *LabelPredicate) Pk() *Field

PK field name.

type Labeled added in v0.4.3

type Labeled interface {
	// Get labels.
	Labels() Labels
}

Labeled model.

type Labeler added in v0.1.1

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

Labeler.

func (*Labeler) Delete added in v0.1.1

func (r *Labeler) Delete(model Model) (err error)

Delete labels for a model in the DB.

func (*Labeler) Insert added in v0.1.1

func (r *Labeler) Insert(model Model) (err error)

Insert labels for the model into the DB.

func (*Labeler) Replace added in v0.1.1

func (r *Labeler) Replace(model Model) (err error)

Replace labels.

type Labels

type Labels map[string]string

Labels collection.

type ListOptions

type ListOptions struct {
	// Pagination.
	Page *Page
	// Sort by field position.
	Sort []int
	// Field detail level.
	// Defaults:
	//   0 = primary and natural fields.
	//   1 = other fields.
	Detail int
	// Predicate
	Predicate Predicate
	// contains filtered or unexported fields
}

List options.

func (*ListOptions) Build

func (l *ListOptions) Build(md *Definition) (err error)

Validate options.

func (*ListOptions) Fields added in v0.2.5

func (l *ListOptions) Fields() (filtered []*Field)

Fields filtered by detail level.

func (*ListOptions) Param

func (l *ListOptions) Param(name string, value interface{}) (p string)

Get an appropriate parameter name. Builds a parameter and adds it to the options.param list.

func (*ListOptions) Params

func (l *ListOptions) Params() []interface{}

Get params referenced by the predicate.

type LtPredicate

type LtPredicate struct {
	SimplePredicate
}

Less than (<) predicate.

func Lt

func Lt(field string, value interface{}) *LtPredicate

New Lt (<) predicate.

func (*LtPredicate) Build

func (p *LtPredicate) Build(options *ListOptions) error

Build.

func (*LtPredicate) Expr

func (p *LtPredicate) Expr() string

Render the expression.

type Model

type Model interface {
	// Get the primary key.
	Pk() string
}

Model Each model represents a table in the DB.

func Clone added in v0.2.6

func Clone(model Model) Model

Create new the model.

type NeqPredicate

type NeqPredicate struct {
	SimplePredicate
}

NotEqual (!=) predicate.

func Neq

func Neq(field string, value interface{}) *NeqPredicate

New Neq (!=) predicate.

func (*NeqPredicate) Build

func (p *NeqPredicate) Build(options *ListOptions) error

Build.

func (*NeqPredicate) Expr

func (p *NeqPredicate) Expr() string

Render the expression.

type OrPredicate

type OrPredicate struct {
	CompoundPredicate
}

OR predicate.

func Or

func Or(predicates ...Predicate) *OrPredicate

OR predicate.

func (*OrPredicate) Build

func (p *OrPredicate) Build(options *ListOptions) error

Build.

func (*OrPredicate) Expr

func (p *OrPredicate) Expr() string

Render the expression.

type Page

type Page struct {
	// The page offset.
	Offset int
	// The number of items per/page.
	Limit int
}

Page. Support pagination.

func (*Page) Slice

func (p *Page) Slice(collection interface{})

Slice the collection according to the page definition. The `collection` must be a pointer to a `Slice` which is modified as needed.

type Pool added in v0.5.0

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

Session pool.

func (*Pool) Close added in v0.5.0

func (p *Pool) Close() (err error)

Close the pool. Close DB connections.

func (*Pool) Open added in v0.5.0

func (p *Pool) Open(nWriter, nReader int, path string, journal *Journal) (err error)

Open the pool. Create sessions with DB connections. For sqlite3:

Even with journal=WAL, nWriter must be (1) to
prevent SQLITE_LOCKED error.

func (*Pool) Reader added in v0.5.0

func (p *Pool) Reader() *Session

Get the next reader. This may block until available.

func (*Pool) Writer added in v0.5.0

func (p *Pool) Writer() *Session

Get the next writer. This may block until available.

type Predicate

type Predicate interface {
	// Build the predicate.
	Build(*ListOptions) error
	// Get the SQL expression.
	Expr() string
}

List predicate.

type Row

type Row interface {
	Scan(...interface{}) error
}

Database interface. Support model `Scan` taking either sql.Row or sql.Rows.

type Serial added in v0.3.5

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

Serial number pool.

type Session added in v0.5.0

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

DB session. Encapsulates the sql.DB.

func (*Session) Begin added in v0.5.0

func (s *Session) Begin() (tx *sql.Tx, err error)

Begin a transaction.

func (*Session) Return added in v0.5.0

func (s *Session) Return()

Return the session to the pool. After is has been returned, it MUST no longer be used.

type SimplePredicate

type SimplePredicate struct {
	// Field name.
	Field string
	// Field value.
	Value interface{}
	// contains filtered or unexported fields
}

Simple predicate.

type StockEventHandler added in v0.3.1

type StockEventHandler struct{}

Stock event handler. Provides default event methods.

func (*StockEventHandler) Created added in v0.3.1

func (r *StockEventHandler) Created(Event)

A model has been created.

func (*StockEventHandler) Deleted added in v0.3.1

func (r *StockEventHandler) Deleted(Event)

A model has been deleted.

func (*StockEventHandler) End added in v0.3.1

func (r *StockEventHandler) End()

An event watch has ended.

func (*StockEventHandler) Error added in v0.3.1

func (r *StockEventHandler) Error(error)

An error has occurred delivering an event.

func (*StockEventHandler) Options added in v0.4.2

func (r *StockEventHandler) Options() WatchOptions

Watch options.

func (*StockEventHandler) Parity added in v0.3.1

func (r *StockEventHandler) Parity()

Watch has parity.

func (*StockEventHandler) Started added in v0.3.1

func (r *StockEventHandler) Started(uint64)

Watch has started.

func (*StockEventHandler) Updated added in v0.3.1

func (r *StockEventHandler) Updated(Event)

A model has been updated.

type Table

type Table struct {
	// Database connection.
	DB DBTX
}

Represents a table in the DB. Using reflect, the model is inspected to determine the table name and columns. The column definition is specified using field tags:

pk - Primary key.
key - Natural key.
fk:<table>(field) - Foreign key.
unique(<group>) - Unique constraint collated by <group>.
const - Not updated.

func (Table) Constraints

func (t Table) Constraints(md *Definition, dm *DataModel) (constraints []string, err error)

Get constraint DDL.

func (Table) Count

func (t Table) Count(model interface{}, predicate Predicate) (count int64, err error)

Count the models in the DB. Qualified by the model field values and list options. Else, ALL models are counted.

func (Table) DDL

func (t Table) DDL(model interface{}, dm *DataModel) (list []string, err error)

Get table and index create DDL.

func (Table) Delete

func (t Table) Delete(model interface{}) (err error)

Delete the model in the DB. Expects the primary key (PK) to be set.

func (Table) EnsurePk added in v0.4.3

func (t Table) EnsurePk(md *Definition)

Ensure PK is generated as specified/needed.

func (Table) Find added in v0.6.0

func (t Table) Find(model interface{}, options ListOptions) (itr fb.Iterator, err error)

Find models in the DB. Qualified by the list options.

func (Table) Get

func (t Table) Get(model interface{}) (err error)

Get the model in the DB. Expects the primary key (PK) to be set. Fetch the row and populate the fields in the model.

func (Table) IndexDDL added in v0.3.7

func (t Table) IndexDDL(md *Definition) (list []string, err error)

Build non-unique index DDL.

func (Table) Insert

func (t Table) Insert(model interface{}) (err error)

Insert the model in the DB. Expects the primary key (PK) to be set.

func (Table) KeyIndexDDL added in v0.3.7

func (t Table) KeyIndexDDL(md *Definition) (list []string, err error)

Build natural key index DDL.

func (Table) List

func (t Table) List(list interface{}, options ListOptions) (err error)

List the model in the DB. Qualified by the list options.

func (Table) Name

func (t Table) Name(model interface{}) string

Get the table name for the model.

func (Table) Params

func (t Table) Params(md *Definition) (list []interface{})

Get the `Fields` referenced as param in SQL.

func (Table) TableDDL added in v0.3.7

func (t Table) TableDDL(md *Definition, dm *DataModel) (list []string, err error)

Build table DDL.

func (Table) Update

func (t Table) Update(model interface{}) (err error)

Update the model in the DB. Expects the primary key (PK) to be set.

type TmplData

type TmplData struct {
	// Table name.
	Table string
	// Index name.
	Index string
	// Fields.
	Fields []*Field
	// Constraint DDL.
	Constraints []string
	// Natural key fields.
	Keys []*Field
	// Primary key.
	Pk *Field
	// List options.
	Options *ListOptions
	// Count
	Count bool
}

Template data.

func (TmplData) Page

func (t TmplData) Page() *Page

Pagination.

func (TmplData) Predicate

func (t TmplData) Predicate() Predicate

Predicate

func (TmplData) Sort

func (t TmplData) Sort() []int

Sort criteria

type Tx

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

Database transaction.

func (*Tx) Commit

func (r *Tx) Commit() (err error)

Commit a transaction. Staged changes are committed in the DB. The transaction is ended and the session returned.

func (*Tx) Count added in v0.1.1

func (r *Tx) Count(model Model, predicate Predicate) (n int64, err error)

Count models.

func (*Tx) Delete added in v0.1.1

func (r *Tx) Delete(model Model) (err error)

Delete (cascading) of the model.

func (*Tx) End

func (r *Tx) End() (err error)

End a transaction. Staged changes are discarded. The session is returned.

func (*Tx) Execute added in v0.5.0

func (r *Tx) Execute(sql string) (result sql.Result, err error)

Execute SQL.

func (*Tx) Find added in v0.6.0

func (r *Tx) Find(model interface{}, options ListOptions) (itr fb.Iterator, err error)

List models.

func (*Tx) Get added in v0.1.1

func (r *Tx) Get(model Model) (err error)

Get the model.

func (*Tx) Insert added in v0.1.1

func (r *Tx) Insert(model Model) (err error)

Insert the model.

func (*Tx) List added in v0.1.1

func (r *Tx) List(list interface{}, options ListOptions) (err error)

List models. The `list` must be: *[]Model.

func (*Tx) Update added in v0.1.1

func (r *Tx) Update(model Model) (err error)

Update the model.

type Watch

type Watch struct {
	// Model to be watched.
	Model Model
	// Event handler.
	Handler EventHandler
	// contains filtered or unexported fields
}

Model event watch.

func (*Watch) Alive added in v0.3.1

func (w *Watch) Alive() bool

The watch has not ended.

func (*Watch) End

func (w *Watch) End()

End the watch.

func (*Watch) Match

func (w *Watch) Match(model Model) bool

Match by model `kind`.

func (*Watch) Start

func (w *Watch) Start(snapshot fb.Iterator)

Run the watch. Forward events to the `handler`.

func (*Watch) String added in v0.3.5

func (w *Watch) String() string

String representation.

type WatchOptions added in v0.4.2

type WatchOptions struct {
	// Initial snapshot.
	// List models and report as `Created` events.
	Snapshot bool
}

Watch options.

Jump to

Keyboard shortcuts

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