y

package module
v0.0.0-...-921a7b9 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2016 License: MIT Imports: 9 Imported by: 0

README

y

Build Status Go Report Card

Be faster with Y. The simplest ORM-like framework for Golang. The purpose of the library is a pure data modeling from a scratch.

Install

go get gopkg.in/Masterminds/squirrel.v1
go get github.com/Repo2/y

RDBS

You can use both databases: MySQL (default) and Postgres. If you wanted to use Postgres you should call SetBuilderProvider method once before any Y routine:

y.SetBuilderProvider(y.Postgres)

Y has been tested with following drivers:

  • github.com/go-sql-driver/mysql
  • github.com/lib/pq

Actions

Fetch

Fetch executes SELECT statement and returns a collection of objects.

type Account struct {
  ID   int64  `y:"id,pk"`
  Name string `y:"name"`
}
c, err := y.New(Account{}).Fetch(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", c.List())
FindBy

FindBy modifies a query for custom selection.

type Order struct {
  ID    int64 `y:"id,pk"`
  Price int   `y:"price"`
}
c, err := y.New(Order{}).
  FindBy(
  func(b squirrel.SelectBuilder) squirrel.SelectBuilder {
    return b.Where("price > ?", 10)
  }).
  Fetch(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", c.List())
Load

Load executes SELECT statement for one row and loads the object in self.

type Todo struct {
  ID    int64  `y:"id,pk"`
  Title string `y:"title"`
}
todo := Todo{ID: 1}
err := y.New(&todo).Load(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", todo)
Put

Put executes INSERT statement and assigns auto-increment value.

type User struct {
  ID   int64  `y:"id,pk,autoincr"`
  Name string `y:"name"`
}
user := User{Name: "Harry"}
_, err := y.New(&user).Put(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", user)

You can use Put for batch statement also.

type Log struct {
  Msg string
}
logs := []Log{
  {"It"}, {"Works"},
}
affected, err := y.New(logs).Put(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", affected)
Update

Update executes UPDATE statement. The action compares origin and modified objects by their version in the database.

type Car struct {
  ID    int64 `y:"id,pk"`
  Power int   `y:"power"`
  y.Versionable
}
var err error
car := Car{ID: 1}
err = y.New(&car).MustLoad(db).Update(db, y.Values{"power": 50})
if err != nil {
  log.Panicln(err)
}
log.Printf("%#v\n", car)
Delete

Delete executes DELETE statement. The action removes an object by primary keys.

type Account struct {
	ID    int64 `y:",pk"`
	Email string
}
acc := Account{ID: 1}
affected, err := y.New(acc).Delete(db)
if err != nil {
  log.Panicln(err)
}
log.Printf("Affected rows: %d\n", affected)
Join

Join builds relations by foreign keys

type Device struct {
  ID     int64 `y:",pk"`
  UserID int64 `y:",fk"`
  Name   string
}
type User struct {
  ID          int64     `y:"id,pk"`
  DeviceArray []*Device `y:"-"`
}
users, err := y.New(User{}).Fetch(db)
if err != nil {
  log.Panicln(err)
}
if !users.Empty() {
  devices, _ := y.New(Device{}).Join(db, users)
  log.Printf("All users with their devices: %#v\n", users)
  log.Printf("All devices: %#v\n", devices)
}

Pull requests

Please, be free to make pull requests or issues posting.

License

MIT

Original source: https://github.com/Repo2/y

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// MySQL describes behavior for LastInsertId and question placeholder
	MySQL = mysqlProvider{}
	// Postgres describes behavior for SERIAL data type and dollar placeholder
	Postgres = postgresProvider{}
)
View Source
var ByEq = func(eq sq.Eq) Qualifier {
	return func(b sq.SelectBuilder) sq.SelectBuilder {
		return b.Where(eq)
	}
}

ByEq returns the filter by squirrel.Eq

View Source
var ByID = func(id interface{}) Qualifier {
	return ByEq(sq.Eq{"id": id})
}

ByID returns the filter by ID

View Source
var Debug = true

Debug enables additional info

View Source
var (
	ErrNoAffectedRows = errors.New("y/errors: no affected rows found")
)

Functions

func DeleteBy

func DeleteBy(db sq.BaseRunner, p *Proxy, by Values) (int64, error)

DeleteBy removes a proxy by values

func Put

func Put(db sq.BaseRunner, p *Proxy) (int64, error)

Put inserts new objects

func SetBuilderProvider

func SetBuilderProvider(rdb provider)

SetBuilderProvider defines statement builder type

func Truncate

func Truncate(db sq.BaseRunner, p *Proxy) (err error)

Truncate erases all data from a table

func Tx

func Tx(db *sql.DB, pipes ...TxPipe) (err error)

Tx executes statements in a transaction

func Update

func Update(db sq.BaseRunner, p *Proxy, v Values) error

Update saves object changes

Types

type Changer

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

Changer updates object values

func (*Changer) Update

func (c *Changer) Update(db sq.BaseRunner) (err error)

Update saves object changes in db after version validation

type Collection

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

Collection contains items and indexes

func Fetch

func Fetch(db sq.BaseRunner, v interface{}) (*Collection, error)

Fetch loads a collection of objects

func (*Collection) Empty

func (c *Collection) Empty() bool

Empty returns false if no items exist

func (*Collection) First

func (c *Collection) First() interface{}

First returns the first item

func (*Collection) Join

func (c *Collection) Join(j *Collection)

Join links related collection

func (*Collection) List

func (c *Collection) List() interface{}

List returns all items

func (*Collection) Size

func (c *Collection) Size() int

Size returns count of items

type Finder

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

Finder loads a collection from a database

func (*Finder) Fetch

func (f *Finder) Fetch(db sq.BaseRunner) (*Collection, error)

Fetch make a query and creates a collection

func (*Finder) Load

func (f *Finder) Load(db sq.BaseRunner) error

Load fetches an object from db and loads in self proxy

func (*Finder) Qualify

func (f *Finder) Qualify(q Qualifier) *Finder

Qualify updates select builder

type Modifier

type Modifier func(v interface{}) interface{}

Modifier changes a value for update statement

func IncrFloat

func IncrFloat(to interface{}) Modifier

IncrFloat returns a modifier for float32/float64 increment

func IncrInt

func IncrInt(to interface{}) Modifier

IncrInt returns a modifier for int/int8/int16/int32/int64 increment

type Proxy

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

Proxy contains a schema of a type

func New

func New(v interface{}) *Proxy

New creates a proxy of an interface

func (*Proxy) Collection

func (p *Proxy) Collection() *Collection

Collection creates a collection of proxy values

func (*Proxy) Delete

func (p *Proxy) Delete(db sq.BaseRunner) (int64, error)

Delete removes a proxy by primary

func (*Proxy) DeleteBy

func (p *Proxy) DeleteBy(db sq.BaseRunner, by Values) (int64, error)

DeleteBy removes a proxy by values

func (*Proxy) Fetch

func (p *Proxy) Fetch(db sq.BaseRunner) (*Collection, error)

Fetch returns a collection of objects

func (*Proxy) Field

func (p *Proxy) Field(name string) reflect.Value

Field returns reflected field by name

func (*Proxy) Find

func (p *Proxy) Find() *Finder

Find returns Finder with qualified query

func (*Proxy) FindBy

func (p *Proxy) FindBy(q Qualifier) *Finder

FindBy returns Finder with qualified query

func (*Proxy) Join

func (p *Proxy) Join(db sq.BaseRunner, in *Collection) (*Collection, error)

Join adds related collection to self

func (*Proxy) Load

func (p *Proxy) Load(db sq.BaseRunner) error

Load fetches an object from db by primary key

func (*Proxy) Map

func (p *Proxy) Map() Values

Map returns a simple map of struct values

func (*Proxy) MustLoad

func (p *Proxy) MustLoad(db sq.BaseRunner) *Proxy

MustLoad fetches an object and panic if an error occurred

func (*Proxy) Put

func (p *Proxy) Put(db sq.BaseRunner) (int64, error)

Put creates a new object

func (*Proxy) Query

func (p *Proxy) Query(b sq.SelectBuilder) *Finder

Query returns Finder with a custom query

func (*Proxy) Truncate

func (p *Proxy) Truncate(db sq.BaseRunner) error

Truncate erases all data

func (*Proxy) Update

func (p *Proxy) Update(db sq.BaseRunner, v Values) error

Update saves changes of the object

type Qualifier

type Qualifier func(sq.SelectBuilder) sq.SelectBuilder

Qualifier updates a select builder if you need

type TxPipe

type TxPipe func(sq.BaseRunner) error

TxPipe run some db statement

type Values

type Values map[string]interface{}

Values is the map of something

type Versionable

type Versionable struct {
	Version sql.NullInt64 `json:"-" y:"_version"`
}

Versionable mixins a version to a model

func MakeVersionable

func MakeVersionable(n int64) Versionable

MakeVersionable inits a new version

Jump to

Keyboard shortcuts

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