manager

package module
v0.0.0-...-49f96c6 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2016 License: MIT Imports: 5 Imported by: 1

README

Manager

GoDoc Build Status

Totally not an ORM for Go.

For use with the SQL toolkits Sol and Fields.

Manager adds convenience methods such as Get, auto-joins, and injected conditional clauses to SQL tables.

// Serial and Timestamp will be set by the database
type Item struct {
    fields.Serial
    Name   string
    IsFree bool
    fields.Timestamp
}

func (item Item) Error(conn sol.Conn) *errors.Error {
    return nil
}

func (item *Item) Save(conn sol.Conn) error {
    return conn.Query(Items.Insert().Values(item).Returning(), item)
}

// Create a Table and immediately wrap it in a manager. Since the TableElem
// is embedded all its methods are available
var Items = postgres.Table("items",
    fields.Serial{},
    sol.Column("name", types.Varchar().Limit(32).NotNull()),
    sol.Column("is_free", types.Boolean().NotNull()),
    sol.PrimaryKey("id"),
    fields.Timestamp{},
)

var ItemsManager = manager.New(Items)

func main() {
    Items.Use(conn) // A connection must be set

    item := NewItem("a")
    Items.Save(&item) // Pass a pointer for database-level field assignments
    log.Println(item.Exists())

    Items.Get(&item, item.ID)
}

Extend the manager by embedding it in your own struct.

Happy Hacking!

aodin, 2016

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All() string

func Has

func Has(name string) bool

func List

func List() string

List call List on the default Schemas

func SQL

func SQL(all bool, names ...string) string

func String

func String() string

String calls String on the default Schemas

Types

type Managed

type Managed interface {
	Error(sol.Conn) *errors.Error
	Exists() bool
	Keys() []interface{} // Primary keys
	Save(sol.Conn) error
}

Managed is an implementation target for database-backed struct types

type Manager

type Manager struct {
	*postgres.TableElem
	// contains filtered or unexported fields
}

Manager wraps a table and provides helper methods

func New

func New(table *postgres.TableElem) Manager

New creates a new manager

func (Manager) AddTo

func (m Manager) AddTo(schema *Schema) error

AddTo adds the manager's table to the given Schema

func (*Manager) All

func (m *Manager) All(dest interface{}) error

TODO default select

func (*Manager) BulkCreate

func (m *Manager) BulkCreate(obj interface{}) error

BulkCreate allow the creation of multiple objects

func (*Manager) Conn

func (m *Manager) Conn() sol.Conn

Conn returns the manager's connection TODO GetConn? What methods of the connection should be available?

func (Manager) Filter

func (m Manager) Filter(clauses ...sol.Clause) Manager

Filter injects a clause whenever a DELETE, UPDATE, or SELECT statement is queried

func (Manager) FilterDelete

func (m Manager) FilterDelete(clauses ...sol.Clause) Manager

FilterDelete injects a clause whenever a DELETE statement is queried

func (Manager) FilterSelect

func (m Manager) FilterSelect(clauses ...sol.Clause) Manager

FilterSelect injects a clause whenever a SELECT statement is queried

func (Manager) FilterUpdate

func (m Manager) FilterUpdate(clauses ...sol.Clause) Manager

FilterUpdate injects a clause whenever an UPDATE statement is queried

func (*Manager) Get

func (m *Manager) Get(dest interface{}, keys ...interface{}) error

func (*Manager) GetBy

func (m *Manager) GetBy(dest interface{}, k string, v interface{}) error

GetBy gets an object by the given field and value

func (*Manager) Query

func (m *Manager) Query(stmt sol.Executable, dest ...interface{}) error

Query allows clauses to be injected into various statements

func (*Manager) Save

func (m *Manager) Save(obj Managed) error

Save creates a new object, unless Exists() == true, then it updates

func (*Manager) SetConn

func (m *Manager) SetConn(conn sol.Conn)

SetConn replaces the current connection

func (*Manager) UpdateValues

func (m *Manager) UpdateValues(obj Managed, values ...sol.Values) error

UpdateValues updates the given obj with the given values

func (Manager) Use

func (m Manager) Use(conn sol.Conn) Manager

Use is an alias of Using

func (Manager) Using

func (m Manager) Using(conn sol.Conn) Manager

Using returns a new instance of the Manager with the given connection

type Schema

type Schema struct {
	Name   string
	Tables []sol.Tabular
}

func Get

func Get(name string) *Schema

func NewSchema

func NewSchema(name string) *Schema

NewSchema creates a new schema - a simple way to aggregate tables

func (*Schema) Add

func (schema *Schema) Add(table sol.Tabular) error

Add adds a table to the Schema. It will error if a table with the same name already exists in the Schema

func (Schema) String

func (schema Schema) String() (out string)

type Schemas

type Schemas []*Schema

func Defaults

func Defaults() Schemas

Defaults returns the default Schemas

func (Schemas) All

func (schemas Schemas) All() string

All is an alias for String

func (Schemas) Get

func (schemas Schemas) Get(name string) *Schema

func (Schemas) Has

func (schemas Schemas) Has(name string) bool

func (Schemas) List

func (schemas Schemas) List() string

List returns the schemas

func (Schemas) SQL

func (schemas Schemas) SQL(all bool, names ...string) string

func (Schemas) String

func (schemas Schemas) String() (out string)

Jump to

Keyboard shortcuts

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