gobatis

package module
v0.0.0-...-53e5199 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2021 License: Apache-2.0 Imports: 14 Imported by: 5

README

gobatis

Introduction

A sample db tools with binding between sqls and xml nodes, similar like mybatis style.

Features

  • Pure native
  • No third dependencies
  • Multiple DS & DB
  • Dynamic SQL & Func
  • Native SQL Query
  • Pagable
  • Raw XML

Supports database

  1. MySQL(Default dialect)
  2. SQLite/SQLite3
  3. MSSQL/SQLServer

Install

  • GOPATH
mkdir -p $GOPATH/src/github.com/billcoding/gobatis

cd $GOPATH/src/github.com/billcoding

git clone https://github.com/billcoding/gobatis.git gobatis
  • Go mod
require github.com/billcoding/gobatis latest

Usage

  • Insert
userMapper := Default().DSN(dsn).Mapper("user")
err := userMapper.Update("insert").Exec("inserted")
  • Delete
userMapper := Default().DSN(dsn).Mapper("user")
err := userMapper.Update("delete").Exec(1)
  • Update
userMapper := Default().DSN(dsn).Mapper("user")
err := userMapper.Update("update").Exec("updated", 1)
  • Select Simple
var batis = Default().DSN(dsn)
userMapper := batis.Mapper("user")
userMapper.Select("selectSimple").Exec().Call(func(rows *sql.Rows) {
    if rows.Next() {
        t := ""
        rows.Scan(&t)
        fmt.Printf("time is %v\n", t)
    }
})

  • Select Struct
var batis = Default().DSN(dsn)
type User struct {
    Id   int    `db:"id"`
    Name string `db:"name"`
}
userList := batis.Mapper("user").Select("selectStruct").Exec().List(new(User))
})

XML Definition

  • CUD defines Update node
<?xml version="1.0" encoding="UTF-8"?>
<batis-mapper binding="user">

    <update id="insert">
        insert into user(`name`) values (?)
    </update>

    <update id="delete">
        delete from user where id = ?
    </update>

    <update id="update">
        update user set name = ? where id = ?
    </update>

</batis-mapper>
  • R defines Select node
<?xml version="1.0" encoding="UTF-8"?>
<batis-mapper binding="user">

    <select id="select">
        select * from user
    </update>

</batis-mapper>
  • Template defines
<?xml version="1.0" encoding="UTF-8"?>
<batis-mapper binding="user2">

    <select id="query">
        select * from user as u where 1 = 1
        {{ if ne .id "" }}
        and u.id = {{ .id }}
        {{ end }}
        {{ if ne .name "" }}
        and u.name = '{{ .name }}'
        {{ end }}
    </select>

    <update id="insert">
        insert into user(name) values
        {{ range $index, $element := . }}{{ if gt $index 0 }},{{ end }} ('{{$element.Name}}'){{ end }}
    </update>

</batis-mapper>

Multiple DataSource Support

  • Register DataSource
batis.MultiDS.Add(DSNAME, DSN)
batis.MultiDS.AddWithDialect(Dialect, DSNAME, DSN)
  • Select DataSource
mapper.DS(DSNAME)
mapper.selectWithDS(DSNAME)
mapper.updateWithDS(DSNAME)

Transaction Support

  • Begin tx
batis.TxMapper(BINDING)
  • Commit tx
txMapper.Commit()
  • Rollback tx
txMapper.Rollback()

Env Support

  • Auto Scan Mapper Files
BATIS_AUTO_SCAN

e.g.

BATIS_AUTO_SCAN=1|0
BATIS_AUTO_SCAN=on|ON
BATIS_AUTO_SCAN=true|TRUE
  • Print SQL
BATIS_PRINT_SQL

e.g.

BATIS_PRINT_SQL=1|0
BATIS_PRINT_SQL=on|ON
BATIS_PRINT_SQL=true|TRUE
  • Mapper path
BATIS_MAPPER_PATH

e.g.

BATIS_MAPPER_PATH=/tmp/myapp/mapper
  • DSN
BATIS_DSN

e.g.

1. BATIS_DSN=root:123@tcp(192.168.1.8:3306)/test
2. BATIS_DSN=_,root:123@tcp(192.168.1.8:3306)/test
3. BATIS_DSN=master,root:123@tcp(192.168.1.8:3306)/test|slave,root:123@tcp(192.168.1.9:3306)/test

Native SQL Query

mapper.SqlQuery(SQL).Query()
mapper.SqlQuery(SQL).Exec()

Raw XML

batis.AddRaw(XML)

Page

selectMapper.Page(new(TYPE), OFFSET, SIZE) 

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batis

type Batis struct {
	Logger  *logrus.Logger
	MultiDS *multiDS
	FuncMap template.FuncMap
	// contains filtered or unexported fields
}

Batis struct

func Default

func Default() *Batis

Default return default Batis

func New

func New() *Batis

New return new Batis

func (*Batis) AddFS

func (b *Batis) AddFS(FS *embed.FS, name string) *Batis

AddFS add FS xml

func (*Batis) AddRaw

func (b *Batis) AddRaw(rawXML string) *Batis

AddRaw add raw xml

func (*Batis) Begin

func (b *Batis) Begin() *TX

func (*Batis) DSN

func (b *Batis) DSN(dsn string) *Batis

DSN register dsn with master ds

func (*Batis) DSNWithDialect

func (b *Batis) DSNWithDialect(dialect Dialect, dsn string) *Batis

DSNWithDialect register dsn with master ds and dialect

func (*Batis) Mapper

func (b *Batis) Mapper(binding string) *mapper

Mapper return cached mapper

func (*Batis) SetConnMaxIdleTime

func (b *Batis) SetConnMaxIdleTime(n time.Duration) *Batis

func (*Batis) SetConnMaxLifetime

func (b *Batis) SetConnMaxLifetime(n time.Duration) *Batis

func (*Batis) SetMaxIdleConn

func (b *Batis) SetMaxIdleConn(n int) *Batis

func (*Batis) SetMaxOpenConn

func (b *Batis) SetMaxOpenConn(n int) *Batis

func (*Batis) SqlQuery

func (b *Batis) SqlQuery(sql string) *sqlQuery

SqlQuery return sql query from batis

type DB

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

DB struct

type DS

type DS struct {
	// Named of DS
	Name string
	// DSN of DS
	DSN string
	// contains filtered or unexported fields
}

DS struct

type Dialect

type Dialect string
const (
	MySQL   Dialect = "mysql"     //see  https://github.com/go-sql-driver/mysql
	SQLite  Dialect = "sqlite3"   //see  https://github.com/mattn/go-sqlite3
	SQLite3 Dialect = "sqlite3"   //see  https://github.com/mattn/go-sqlite3
	MSSQL   Dialect = "sqlserver" //see  https://github.com/denisenkom/go-mssqldb
)

type Helper

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

Helper struct

func NewHelper

func NewHelper(binding, id string) *Helper

NewHelper return new helper

func NewHelperWithBatis

func NewHelperWithBatis(batis *Batis, binding, id string) *Helper

NewHelperWithBatis return new helper with DS

func NewHelperWithBatisAndDS

func NewHelperWithBatisAndDS(batis *Batis, binding, id, ds string) *Helper

NewHelperWithBatisAndDS return new helper with DS

func NewHelperWithDS

func NewHelperWithDS(binding, id, ds string) *Helper

NewHelperWithDS return new helper with DS

func (*Helper) Select

func (h *Helper) Select() *SelectMapper

Select return query

func (*Helper) Update

func (h *Helper) Update() *UpdateMapper

Update return update

type Page

type Page struct {
	Offset    int           `json:"offset"`
	Size      int           `json:"size"`
	TotalRows int           `json:"total_rows"`
	TotalPage int           `json:"total_page"`
	List      []interface{} `json:"list"`
}

type PageMap

type PageMap struct {
	Offset    int                      `json:"offset"`
	Size      int                      `json:"size"`
	TotalRows int                      `json:"total_rows"`
	TotalPage int                      `json:"total_page"`
	List      []map[string]interface{} `json:"list"`
}

type SelectMapper

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

func (*SelectMapper) Args

func (m *SelectMapper) Args(args ...interface{}) *SelectMapper

Args set args

func (*SelectMapper) Exec

func (m *SelectMapper) Exec() *selectCall

Exec select exec

func (*SelectMapper) Page

func (m *SelectMapper) Page(rptr interface{}, offset, size int) *Page

Page select page

func (*SelectMapper) PageMap

func (m *SelectMapper) PageMap(offset, size int) *PageMap

PageMap select Page

func (*SelectMapper) Prepare

func (m *SelectMapper) Prepare(data interface{}) *SelectMapper

Prepare using text/template

func (*SelectMapper) PrepareWithFunc

func (m *SelectMapper) PrepareWithFunc(data interface{}, funcMap template.FuncMap) *SelectMapper

PrepareWithFunc using text/template with func

type TX

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

TX struct

func (*TX) Commit

func (tx *TX) Commit() error

func (*TX) Rollback

func (tx *TX) Rollback() error

func (*TX) Update

func (tx *TX) Update(m *UpdateMapper) error

type UpdateMapper

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

func (*UpdateMapper) AffectedRows

func (m *UpdateMapper) AffectedRows() int64

AffectedRows get affectedRows

func (*UpdateMapper) Args

func (m *UpdateMapper) Args(args ...interface{}) *UpdateMapper

Args set args

func (*UpdateMapper) Exec

func (m *UpdateMapper) Exec() error

Exec update exec

func (*UpdateMapper) InsertedId

func (m *UpdateMapper) InsertedId() int64

InsertedId get insertedId

func (*UpdateMapper) Prepare

func (m *UpdateMapper) Prepare(data interface{}) *UpdateMapper

Prepare using text/template

func (*UpdateMapper) PrepareWithFunc

func (m *UpdateMapper) PrepareWithFunc(data interface{}, funcMap template.FuncMap) *UpdateMapper

PrepareWithFunc using text/template

Jump to

Keyboard shortcuts

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