Documentation ¶
Overview ¶
Go struct/SQL CRUD
import "github.com/cxr29/scrud" import _ "github.com/go-sql-driver/mysql" db, err := scrud.Open("mysql", "user:password@/database") // A, B is struct or *struct n, err := db.Insert(A) // insert n, err = db.Insert([]A{}) // batch insert err = db.Select(&A, ...) // select by primary key, support include or exclude columns err = db.SelectRelation("B", &A, ...) // select relation field, support include or exclude columns err = db.Update(A, ...) // update by primary key, support include or exclude columns err = db.Delete(A) // delete by primary key m2m := db.ManyToMany("B", A) // many to many field manager err = m2m.Add(B, ...) // add relation err = m2m.Set(B, ...) // set relation, empty other err = m2m.Remove(B, ...) // remove relation has, err := m2m.Has(B) // check relation err = m2m.Empty() // empty relation result, err := db.Run(qe) // run a query expression that doesn't return rows err = db.Fetch(qe).One(&A) // run a query expression and fetch one row to struct err = db.Fetch(qe).All(&[]A{}) // run a query expression and fetch rows to slice of struct m, err := db.Fetch(qe).MapOne(nil) // run a query expression and fetch one row as map, support set column type a, err := db.Fetch(qe).MapAll(nil) // run a query expression and fetch rows as slice of map, support set column type
See https://github.com/cxr29/scrud for more details
Index ¶
- Variables
- func MapToStruct(data interface{}, m map[string]interface{}) error
- func StructToMap(data interface{}, columns ...string) (map[string]interface{}, error)
- type Bool
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) Delete(data interface{}) error
- func (db *DB) Fetch(query Expression) *Rows
- func (db *DB) Insert(data interface{}) (int64, error)
- func (db *DB) Load(data interface{}) (int64, error)
- func (db *DB) ManyToMany(field string, data interface{}) *ManyToMany
- func (db *DB) Run(query Expression) (sql.Result, error)
- func (db *DB) Select(data interface{}, columns ...string) error
- func (db *DB) SelectRelation(field string, data interface{}, columns ...string) error
- func (db *DB) Snapshot() *Snapshot
- func (db *DB) Starter() Starter
- func (db *DB) Update(data interface{}, columns ...string) error
- type Float32
- type Float64
- type Int
- type Int16
- type Int32
- type Int64
- type Int8
- type ManyToMany
- type Rows
- func (r *Rows) All(i interface{}) (err error)
- func (r *Rows) Err() error
- func (r *Rows) MapAll(i interface{}) ([]map[string]interface{}, error)
- func (r *Rows) MapOne(i interface{}) (map[string]interface{}, error)
- func (r *Rows) MapScan(i interface{}) (map[string]interface{}, error)
- func (r *Rows) One(i interface{}) error
- func (r *Rows) Row(dest ...interface{}) error
- func (r *Rows) Scan(i interface{}) error
- type Snapshot
- type String
- type Time
- type Tx
- func (tx *Tx) Delete(data interface{}) error
- func (tx *Tx) Fetch(query Expression) *Rows
- func (tx *Tx) Insert(data interface{}) (int64, error)
- func (tx *Tx) Load(data interface{}) (int64, error)
- func (tx *Tx) ManyToMany(field string, data interface{}) *ManyToMany
- func (tx *Tx) Run(query Expression) (sql.Result, error)
- func (tx *Tx) Select(data interface{}, columns ...string) error
- func (tx *Tx) SelectRelation(field string, data interface{}, columns ...string) error
- func (tx *Tx) Snapshot() *Snapshot
- func (tx *Tx) Starter() Starter
- func (tx *Tx) Update(data interface{}, columns ...string) error
Constants ¶
This section is empty.
Variables ¶
var ErrNoRows = sql.ErrNoRows
Functions ¶
func MapToStruct ¶
map's key should be column name
func StructToMap ¶
return map's key is column name
Types ¶
type DB ¶
func (*DB) Insert ¶
insert struct, if have auto column data must be *struct
batch insert if data is array or slice and not set auto increment column
func (*DB) ManyToMany ¶
func (db *DB) ManyToMany(field string, data interface{}) *ManyToMany
return many to many manager of the field
func (*DB) Select ¶
select by primary key, data must be *struct
columns specify which to retrieve, to exclude put minus sign at the fisrt
func (*DB) SelectRelation ¶
select relation field, data must be *struct
columns specify which relation's to retrieve, to exclude put minus sign at the fisrt
type ManyToMany ¶
type ManyToMany struct {
// contains filtered or unexported fields
}
many to many field manager
func (*ManyToMany) Add ¶
func (m2m *ManyToMany) Add(data ...interface{}) error
data should be the relation's type
func (*ManyToMany) Empty ¶
func (m2m *ManyToMany) Empty() error
func (*ManyToMany) Has ¶
func (m2m *ManyToMany) Has(data interface{}) (bool, error)
data should be the relation's type
func (*ManyToMany) Remove ¶
func (m2m *ManyToMany) Remove(data ...interface{}) error
data should be the relation's type
func (*ManyToMany) Set ¶
func (m2m *ManyToMany) Set(data ...interface{}) error
data should be the relation's type
type Rows ¶
func (*Rows) MapAll ¶
scan rows as slice of map then close the rows
i: column type, nil/bool/ints/uints/floats/string/time.Time/[]byte/[]interface{}/map[string]interface{}/sql.Scanner/struct
if struct, setter column will be the setter type and not call the setter
func (*Rows) MapOne ¶
scan one row as map then close the rows
i: column type, nil/bool/ints/uints/floats/string/time.Time/[]byte/[]interface{}/map[string]interface{}/sql.Scanner/struct
if struct, setter column will be the setter type and not call the setter
func (*Rows) MapScan ¶
i: column type, nil/bool/ints/uints/floats/string/time.Time/[]byte/[]interface{}/map[string]interface{}/sql.Scanner/struct
if struct, setter column will be the setter type and not call the setter
type Tx ¶
func (*Tx) ManyToMany ¶
func (tx *Tx) ManyToMany(field string, data interface{}) *ManyToMany