scrud

package module
v0.0.0-...-1039f8e Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2017 License: MIT Imports: 10 Imported by: 0

README

See huge

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

Constants

This section is empty.

Variables

View Source
var ErrNoRows = sql.ErrNoRows

Functions

func MapToStruct

func MapToStruct(data interface{}, m map[string]interface{}) error

map's key should be column name

func StructToMap

func StructToMap(data interface{}, columns ...string) (map[string]interface{}, error)

return map's key is column name

Types

type Bool

type Bool bool // null as false

func (*Bool) Scan

func (v *Bool) Scan(value interface{}) error

func (Bool) Value

func (v Bool) Value() (driver.Value, error)

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

func Open

func Open(driverName, dataSourceName string) (*DB, error)

func (*DB) Begin

func (db *DB) Begin() (*Tx, error)

func (*DB) Delete

func (db *DB) Delete(data interface{}) error

delete by primary key, data must be struct or *struct

func (*DB) Fetch

func (db *DB) Fetch(query Expression) *Rows

fetch run a query expression that return rows, typically a select

func (*DB) Insert

func (db *DB) Insert(data interface{}) (int64, error)

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) Load

func (db *DB) Load(data interface{}) (int64, error)

func (*DB) ManyToMany

func (db *DB) ManyToMany(field string, data interface{}) *ManyToMany

return many to many manager of the field

func (*DB) Run

func (db *DB) Run(query Expression) (sql.Result, error)

run a query expression that doesn't return rows, such as insert, update and delete

func (*DB) Select

func (db *DB) Select(data interface{}, columns ...string) error

select by primary key, data must be *struct

columns specify which to retrieve, to exclude put minus sign at the fisrt

func (*DB) SelectRelation

func (db *DB) SelectRelation(field string, data interface{}, columns ...string) error

select relation field, data must be *struct

columns specify which relation's to retrieve, to exclude put minus sign at the fisrt

func (*DB) Snapshot

func (db *DB) Snapshot() *Snapshot

func (*DB) Starter

func (db *DB) Starter() Starter

return a starter to expand query expression

func (*DB) Update

func (db *DB) Update(data interface{}, columns ...string) error

update by primary key, if have auto now column data must be *struct

columns specify which to update, to exclude put minus sign at the fisrt

type Float32

type Float32 float32 // null as 0.0

func (*Float32) Scan

func (v *Float32) Scan(value interface{}) error

func (Float32) Value

func (v Float32) Value() (driver.Value, error)

type Float64

type Float64 float64 // null as 0.0

func (*Float64) Scan

func (v *Float64) Scan(value interface{}) error

func (Float64) Value

func (v Float64) Value() (driver.Value, error)

type Int

type Int int // null as 0

func (*Int) Scan

func (v *Int) Scan(value interface{}) error

func (Int) Value

func (v Int) Value() (driver.Value, error)

type Int16

type Int16 int16 // null as 0

func (*Int16) Scan

func (v *Int16) Scan(value interface{}) error

func (Int16) Value

func (v Int16) Value() (driver.Value, error)

type Int32

type Int32 int32 // null as 0

func (*Int32) Scan

func (v *Int32) Scan(value interface{}) error

func (Int32) Value

func (v Int32) Value() (driver.Value, error)

type Int64

type Int64 int64 // null as 0

func (*Int64) Scan

func (v *Int64) Scan(value interface{}) error

func (Int64) Value

func (v Int64) Value() (driver.Value, error)

type Int8

type Int8 int8 // null as 0

func (*Int8) Scan

func (v *Int8) Scan(value interface{}) error

func (Int8) Value

func (v Int8) Value() (driver.Value, error)

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

type Rows struct {
	*sql.Rows
	// contains filtered or unexported fields
}

func (*Rows) All

func (r *Rows) All(i interface{}) (err error)

scan rows to slice of struct then close the rows

func (*Rows) Err

func (r *Rows) Err() error

func (*Rows) MapAll

func (r *Rows) MapAll(i interface{}) ([]map[string]interface{}, error)

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

func (r *Rows) MapOne(i interface{}) (map[string]interface{}, error)

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

func (r *Rows) MapScan(i interface{}) (map[string]interface{}, error)

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) One

func (r *Rows) One(i interface{}) error

scan one row to struct then close the rows

func (*Rows) Row

func (r *Rows) Row(dest ...interface{}) error

func (*Rows) Scan

func (r *Rows) Scan(i interface{}) error

scan one row to struct

type Snapshot

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

snapshot manager

func (*Snapshot) Delete

func (s *Snapshot) Delete(id int64, data interface{}) error

func (*Snapshot) Insert

func (s *Snapshot) Insert(data interface{}) (int64, time.Time, error)

func (*Snapshot) Select

func (s *Snapshot) Select(id int64, data interface{}, columns ...string) (time.Time, error)

type String

type String string // null as empty string

func (*String) Scan

func (v *String) Scan(value interface{}) error

func (String) Value

func (v String) Value() (driver.Value, error)

type Time

type Time time.Time // null as zero time

func (*Time) Scan

func (v *Time) Scan(value interface{}) error

func (Time) Value

func (v Time) Value() (driver.Value, error)

type Tx

type Tx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

func (*Tx) Delete

func (tx *Tx) Delete(data interface{}) error

func (*Tx) Fetch

func (tx *Tx) Fetch(query Expression) *Rows

func (*Tx) Insert

func (tx *Tx) Insert(data interface{}) (int64, error)

func (*Tx) Load

func (tx *Tx) Load(data interface{}) (int64, error)

func (*Tx) ManyToMany

func (tx *Tx) ManyToMany(field string, data interface{}) *ManyToMany

func (*Tx) Run

func (tx *Tx) Run(query Expression) (sql.Result, error)

func (*Tx) Select

func (tx *Tx) Select(data interface{}, columns ...string) error

func (*Tx) SelectRelation

func (tx *Tx) SelectRelation(field string, data interface{}, columns ...string) error

func (*Tx) Snapshot

func (tx *Tx) Snapshot() *Snapshot

func (*Tx) Starter

func (tx *Tx) Starter() Starter

func (*Tx) Update

func (tx *Tx) Update(data interface{}, columns ...string) error

Directories

Path Synopsis
Global name format functions
Global name format functions
internal
Go write SQL import .
Go write SQL import .

Jump to

Keyboard shortcuts

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