sqlkungfu

package module
v0.0.0-...-8f9b0eb Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2015 License: MIT Imports: 8 Imported by: 1

README

sqlkungfu

sqlkungfu is a very simple data binding package, built along with database/sql package. It doesn't assume fixed bindings between your structs and tables. Instead, it offers us the ability to avail ourself of the powerful combination of golang and SQL.

Unmarshal

common
select id, name, phone, sex from persons limit 1
var data struct{
	ID    uint
	Name  string
	Phone string
	Sex   string
}
sqlkungfu.Unmarshal(rows, &data)

var data []string
sqlkungfu.Unmarshal(rows, &data)

var data [4]string
sqlkungfu.Unmarshal(rows, &data)

var data map[string]interface{}
sqlkungfu.Unmarshal(rows, &data)
select id, name, phone, sex from persons limit 10
var data []struct{
	ID    uint
	Name  string
	Phone string
	Sex   string
}
sqlkungfu.Unmarshal(rows, &data)

var data [][]string
sqlkungfu.Unmarshal(rows, &data)

var data [10][4]string
sqlkungfu.Unmarshal(rows, &data)

var data []map[string]interface{}
sqlkungfu.Unmarshal(rows, &data)
schema
select 'num.id', 'num.age', 'text.name', 'text.phone', 'text.sex' from persons limit 10
var data []struct{
	Num struct{
		ID    uint
		Age uint
	}
	Text struct{
		Name  string
		Phone string
		Sex   string
	}
}
sqlkungfu.Unmarshal(rows, &data)

Insert/Update

simple is power

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMaster = Master{
	SchemaSeparator:  ".",
	MapKey:           "sqlmapkey",
	TagKey:           "sqlkungfu",
	MapUint8ToString: true,
	QuoteColumn: func(c string) string {
		return "`" + c + "`"
	},
}
View Source
var ErrNoRows = errors.New("sqlkungfu: no rows in result set")

Functions

func InitMigration

func InitMigration(db *sql.DB) (err error)

func Insert

func Insert(db *sql.DB, v interface{}, cfgs ...interface{}) (string, sql.Result, error)

func Unmarshal

func Unmarshal(rows *sql.Rows, v interface{}) (err error)

func Update

func Update(db *sql.DB, v interface{}, cfgs ...interface{}) (string, sql.Result, error)

Types

type Date

type Date time.Time

func (*Date) Scan

func (d *Date) Scan(value interface{}) (err error)

func (Date) String

func (d Date) String() string

func (Date) Value

func (d Date) Value() (v driver.Value, err error)

type Master

type Master struct {
	SchemaSeparator  string
	MapKey           string
	MapUint8ToString bool
	TagKey           string

	// http://blog.christosoft.de/2012/10/sqlite-escaping-table-acolumn-names/
	// http://stackoverflow.com/questions/2901453/sql-standard-to-escape-column-names
	QuoteColumn func(string) string

	// TODO: with tag support
	ColumnMap    func(s interface{}, name string) string
	FieldNameMap func(s interface{}, name string) string
}

func NewMaster

func NewMaster() Master

func (Master) Insert

func (m Master) Insert(db *sql.DB, v interface{}, cfgs ...interface{}) (insert string, r sql.Result, err error)

INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);

func (Master) Unmarshal

func (m Master) Unmarshal(rows *sql.Rows, v interface{}) (err error)

map normal join To-Do: - do not override non-nil values - convert []uint8 into a string for interface{} values - support inline field tag

func (Master) Update

func (m Master) Update(db *sql.DB, v interface{}, cfgs ...interface{}) (update string, r sql.Result, err error)

UPDATE table_name SET column1=value1,column2=value2,...

type Migration

type Migration struct {
	Checksum  string
	CreatedAt time.Time
}

func Exec

func Exec(db *sql.DB, query string, args ...interface{}) (m Migration, r sql.Result, err error)

func MustExec

func MustExec(db *sql.DB, query string, args ...interface{}) (m Migration, r sql.Result)

type TableName

type TableName string

InsertNull bool

Jump to

Keyboard shortcuts

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