sago

package module
v0.0.0-...-d4fedc4 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2019 License: MIT Imports: 15 Imported by: 0

README

SAGO (SQL Assistant for GO)

HOW TO USE

  1. Define DAO struct
type UserDao struct{
    DB *sql.DB
    FindByName func(name string)(*User,error)
}
  1. Write SQL in XML
<sago>
    <table>user</table>
    <type>UserDao</type>
    <select name="FindByName" args="name">
        select {{.fields}} from {{.table}} where `name` = {{arg .name}}
    </select>
</sago>
  1. Create sago manager,Scan XMLs and Mapped to DAO object
dao:=&UserDao{
    DB:db,
}
s:=sago.New()
s.ScanDir("./xmlsDirPath")
s.Map(dao)
dao.FindByName("foo")

Documentation

Index

Constants

View Source
const (
	Dir errorSago = iota // dir error
	Xml                  // unmarshal xml error
	YAML
	NoDBField
	NoCacheInManager
	BadCacheField
	BadSQLTemplate
	WrongTypeToMap
	XMLMappedWrong
)
View Source
const (
	MethodNameArg = "arg"
	MethodInArg   = "in"
)

Variables

View Source
var DefaultManager = New()
View Source
var FileConflict = errors.New("file conflict")
View Source
var ShowSQL = false

Functions

func AddFunc

func AddFunc(name string, fnFactory func(ctx *FnCtx) (fn TemplateFunc))

func Map

func Map(objs ...interface{}) error

func MustMap

func MustMap(obj interface{})

func ScanDir

func ScanDir(dirPath string) (e error)

Types

type Cache

type Cache interface {
	Set(dir string, key string, v interface{})
	Get(dir string, key string) (v interface{}, ok bool)
}

type Central

type Central struct {
	Cache Cache
	// contains filtered or unexported fields
}

func New

func New() *Central

func (*Central) AddFunc

func (m *Central) AddFunc(name string, fnFactory func(ctx *FnCtx) (fn TemplateFunc))

func (*Central) Map

func (m *Central) Map(daoObjects ...interface{}) error

参数的基本类型必须是 Ptr 读取变量并注入配置文件中配置的方法

func (*Central) ScanDir

func (m *Central) ScanDir(dirPath string) (e error)

扫描文件下以所有以 .sql.xml 结尾的文件 <sago>

<package></package>
<type></type>
<table></table>
<select name="FindByName" args="name">
	select {{.fields}} from {{.table}} where `name` = {{arg .name}}
</select>
<execute></execute>
<insert></insert>

</sago>

type File

type File struct {
	XMLName  xml.Name     `xml:"sago"`
	Package  string       `xml:"package"`
	Type     string       `xml:"type"`
	Table    string       `xml:"table"`
	Selects  []SQLContent `xml:"select"`
	Executes []SQLContent `xml:"execute"`
	Inserts  []SQLContent `xml:"insert"`
}

func (File) Name

func (f File) Name() string

type Fn

type Fn struct {
	Name string
	Type string
	SQL  string
	Args []string
}

type FnCtx

type FnCtx struct {
	Args []interface{}
}

type SQLContent

type SQLContent struct {
	Name string `xml:"name,attr"`
	Args string `xml:"args,attr"`
	SQL  string `xml:",chardata"`
}

type SQLExecutor

type SQLExecutor struct {
	Cache Cache

	Table        string
	FieldsString string
	Fn           Fn
	Tpl          *template.Template

	DB *sqlx.DB

	ReturnTypes []reflect.Type
	// contains filtered or unexported fields
}

func NewSQLExecutor

func NewSQLExecutor(table string, structTypeName string, returnTypes []reflect.Type, fn *Fn, tpl *template.Template, db *sql.DB, funcFactories []TemplateFuncFactory) *SQLExecutor

func (*SQLExecutor) Execute

func (e *SQLExecutor) Execute(args []reflect.Value) (results []reflect.Value)

func (*SQLExecutor) Insert

func (e *SQLExecutor) Insert(args []reflect.Value) (results []reflect.Value)

func (*SQLExecutor) Select

func (e *SQLExecutor) Select(args []reflect.Value) (results []reflect.Value)

func (*SQLExecutor) SelectCache

func (e *SQLExecutor) SelectCache(args []reflect.Value) (results []reflect.Value)

type SQLSet

type SQLSet struct {
	Package   string
	Type      string
	Table     string
	Functions map[string]*Fn
}

type TemplateFunc

type TemplateFunc func(interface{}) (string, error)

type TemplateFuncFactory

type TemplateFuncFactory struct {
	Name   string
	Create func(ctx *FnCtx) TemplateFunc
}

Directories

Path Synopsis
examples
dao

Jump to

Keyboard shortcuts

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