worm

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: Apache-2.0, Apache-2.0 Imports: 16 Imported by: 0

README

介绍

worm是一款方便易用的Go语言ORM库,worm具有使用简单,运行性能高,功能强大的特点。具体特征如下:

  • 通过Struct的Tag与数据库字段进行映射,让您免于经常拼写SQL的麻烦。
  • 支持Struct映射、原生SQL以及SQL builder三种模式来操作数据库,并且Struct映射、原生SQL以及SQL builder可混合使用。
  • Struct映射、SQL builder支持链式API,可使用Where, And, Or, ID, In, Limit, GroupBy, OrderBy, Having等函数构造查询条件。
  • 可通过Join、LeftJoin、RightJoin来进行数据库表之间的关联查询。
  • 支持事务支持,可在会话中开启事务,在事务中可以混用Struct映射、原生SQL以及SQL builder来操作数据库。
  • 支持预编译模式访问数据库,会话开启预编译模式后,任何SQL都会使用缓存的Statement,可以提升数据库访问效率。
  • 支持使用数据库的读写分离模式来访问一组数据库。
  • 支持在Insert,Update,Delete,Get, Find操作中使用钩子方法。
  • 支持SQL日志的输出,并且可通过日志钩子自定义SQL日志的输出,日志钩子支持Context。
  • 可根据数据库自动生成库表对应的Model结构体。

目前worm支持的数据库有:mysql、postgres、sqlite、sqlserver。

安装

go get github.com/haming123/wego/worm

文档

请点击:详细文档

快速开始

创建实体类

//建表语句
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `passwd` varchar(32) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `updated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
);

数据库表user对应的实体类的定义如下:

type User struct {
	Id          int64   	`db:"id;autoid"`
	Name        string  	`db:"name"`
	Age         int64   	`db:"age"`
	Passwd      string  	`db:"passwd"`
	Created     time.Time	`db:"created;n_update"`
}
func (ent *User) TableName() string {
	return "user"
}

worm使用名称为"db"的Tag映射数据库字段,"db"后面是字段的名称,autoid用于说明该字段是自增型单主键,n_update用于说明该字段不可用于update语句中。

创建DbEngine

本文中的例子使用的都是mysql数据库。若要创建一个mysql数据库的DbEngine,您可调用worm.NewMysql()函数或者调用worm.InitMysql()函数。

package main
import (
	"database/sql"
	"log"
	_ "github.com/go-sql-driver/mysql"
	"github.com/haming123/wego/worm"
)
func main() {
	var err error
	cnnstr := "user:pwd@tcp(127.0.0.1:3306)/db?charset=utf8&parseTime=True"
	dbcnn, err := sql.Open("mysql", cnnstr)
	if err != nil {
		log.Println(err)
		return
	}
	err = dbcnn.Ping()
	if err != nil {
		log.Println(err)
		return
	}
	err = worm.InitMysql(dbcnn)
	if err != nil {
		log.Println(err)
		return
	}
}

插入记录

user := User{Name:"name1", Age: 21, Created: time.Now()}
id, err := worm.Model(&user).Insert()
//insert into user set name=?,age=?,passwd=?,created=?

更新数据

//通过ID更新数据
user := User{Name:"name2", Age: 22}
affected, err := worm.Model(&user).ID(1).Update()
//update user set name=?,age=?,passwd=? where id=?

为了防止误操作,更新数据时必须指定查询条件,若没有指定查询条件worm不会执行该操作。

删除数据

//通过ID删除数据
affected, err := worm.Model(&User{}).ID(1).Delete()
//delete from user where id=?

为了防止误操作,删除数据时必须指定查询条件,若没有指定查询条件worm不会执行该操作。

查询单条记录

//通过ID查询数据
user := User{}
_, err := worm.Model(&user).ID(1).Get()
//select id,name,age,passwd,created from user where id=? limit 1

查询多条记录

//查询全部数据
users := []User{}
err := worm.Model(&User{}).Find(&users)
//select id,name,age,passwd,created from user

//使用limit查询数据
users = []User{}
err := worm.Model(&User{}).Limit(5).Offset(2).Find(&users)
//select id,name,age,passwd,created from user limit 2, 5

//使用order查询数据
users = []User{}
err = worm.Model(&User{}).OrderBy("name asc").Find(&users)
//select id,name,age,passwd,created from users order by name asc

查询指定的字段

worm允许通过Select方法选择特定的字段, 或者使用Omit方法排除特定字段

//查询指定的字段
user := User{}
_, err := worm.Model(&user).Select("id", "name", "age").ID(1).Get()
// select id,name,age from users where id=1 limit 1

user = User{}
_, err = worm.Model(&user).Omit("passwd").ID(1).Get()
//select id,name,age,created from users where id=1 limit 1

条件查询

worm支持链式API,可使用Where, And, Or, ID, In等函数构造查询条件。

users := []User{}
err := worm.Model(&User{}).Where("age>?", 0).Find(&users)
//select id,name,age,passwd,created from user where age>?

//and
users = []User{}
err := worm.Model(&User{}).Where("age>?", 0).And("id<?", 10).Find(&users)
//select id,name,age,passwd,created from user where age>? and id<?

//like
users = []User{}
err := worm.Model(&User{}).Where("name like ?", "%name%").Find(&users)
//select id,name,age,passwd,created from user where name like ?

//in
users = []User{}
err := worm.Model(&User{}).Where("age>?", 0).AndIn("id", 5,6).Find(&users)
//select id,name,age,passwd,created from user where age>? and id in (?,?)

worm占位符统一使用?,worm会根据数据库类型,自动替换占位符,例如postgresql数据库把?替换成$1,$2...

获取记录条数

num, err := Model(&User{}).Where("id>?", 0).Count()
//select count(1) from user where id>?

num, err := Model(&User{}).Where("id>?", 0).Count("name")
//select count(name) from user where id>?

检测记录是否存在

has, err := worm.Model(&User{}).Where("id=?", 1).Exist()
//select id,name,age,passwd,created from user where id=? limit 1

Update或Insert数据

var user = User{Name:"name2", Age: 22}
affected,insertId,err := worm.Model(&user).UpdateOrInsert(1)
//根据id判断是Update还是Insert, 若id>0,则调用Update,否则调用Insert
//insert into user set name=?,age=?,passwd=?,created=?
//update user set name=?,age=?,passwd=? where id=?

批量新增

users := []User{User{DB_name:"batch1", Age: 33}, User{DB_name:"batch2", Age: 33} }
res, err := Model(&User{}).BatchInsert(&users)
//insert into user set name=?,age=?,created=?
//TX: time=141.074ms affected=2; prepare

使用原生SQL

//插入记录
res, err :=  worm.SQL("insert into user set name=?, age=?", "name1", 1).Exec()
//更新记录
res, err =  worm.SQL("update user set name=? where id=?", "name2", id).Exec()
//删除记录
res, err = worm.SQL("delete from user where id=?", id).Exec()

//查询单条记录
var name string; var age int64
has, err = worm.SQL("select name,age from user where id=?", 6).Get(&name, &age)

//查询单条记录到model对象
var ent User
has, err := worm.SQL("select * from user where id=?", 6).GetModel(&ent)

//查询多条记录到model数组
var users []User
err := worm.SQL( "select * from user where id>?", 5).FindModel(&users)

使用Sql Builder

//插入记录
id, err := worm.Table("user").Value("name", "name1").Value("age", 21).Insert()
//更新记录
affected, err := worm.Table("user").Value("name", "name2").Where("id=?", id).Update()
//删除记录
affected, err = worm.Table("user").Where("id=?", id).Delete()

//查询单条记录
var name string; var age int64
has, err = worm.Table("user").Select("name", "age").Where("id=?", 6).Get(&name, &age)

//查询单条记录到model对象
var ent User
has, err := worm.Table("user").Select("*").Where("id=?", 6).GetModel(&ent)

//查询多条记录
var users []User
err :=  worm.Table("user").Select("*").Where("id>?", 0).FindModel(&users)

关联查询

定义一个book表以及相应的实体类:

CREATE TABLE `book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `author` bigint(20) NOT NULL DEFAULT '0',
  `name` varchar(16) NOT NULL DEFAULT '',
  `price` decimal(11,2) NOT NULL DEFAULT 0.0,
  PRIMARY KEY (`id`)
);
type Book struct {
	Id          int64   	`db:"id;autoincr"`
	Name        string  	`db:"name"`
	Author  	int64       `db:"author"`
	Price       float32     `db:"price"`
}
func (ent *Book) TableName() string {
	return "book"
}

在book表中,通过author字段与user表的id字段相关联。若要查询一个用户购买的书,在worm中可以通过Join来查询:

type UserBook struct {
    User
    Book
}
var datas []UserBook
md := worm.Model(&User{}).Select("id","name","age").TableAlias("u")
md.Join(&Book{}, "b", "b.author=u.id", "name")
err := md.Where("u.id>?", 0).Find(&datas)
//select u.id as u_id,u.name as u_name,u.age as u_age,b.name as b_name from user u join book b on b.author=u.id where u.id>0

除了Join,您还可以使用LeftJoin以及RightJoin进行左连接、右连接查询。

事务处理

当使用事务处理时,需要创建 Session对象,并开启数据库事务。在进行事务处理时,在事务中可以混用Model模式、原生SQL模式以及SQL builder模式来操作数据库:

tx := worm.NewSession()
tx.TxBegin()
user := User{Name:"name1", Age: 21, Created: time.Now()}
id, err := tx.Model(&user).Insert()
if err != nil{
    tx.TxRollback()
    return
}
_, err = tx.Table("user").Value("age", 20).Value("name", "zhangsan").Where("id=?", id).Update()
if err != nil{
    tx.TxRollback()
    return
}
_, err = tx.SQL("delete from user where id=?", id).Exec()
if err != nil{
    tx.TxRollback()
    return
}
tx.TxCommit()

使用SQL语句预编译

worm支持SQL语句的预编译,使用SQL语句预编译可以提升数据库访问的效率。在worm中可以通过三种方式开启SQL语句预编译:全局开启、会话中开启、语句中开启。

//全局开启,所有操作都会创建并缓存预编译
worm.UsePrepare(true)

//会话中开启, 会话中的sql语句会创建并缓存预编译
dbs := worm.NewSession()
dbs.UsePrepare(true)

//语句中使用UsePrepare
user := User{}
_, err := Model(&user).UsePrepare(true).Where("id=?", 1).Get()

Documentation

Index

Constants

View Source
const (
	JOIN_INNER int = 0
	JOIN_LJOIN int = 1
	JOIN_RJOIN int = 2
)
View Source
const (
	SQL_TYPE_INS int = 1
	SQL_TYPE_UPD int = 2
	SQL_TYPE_DEL int = 3
	SQL_TYPE_SEL int = 4
)
View Source
const (
	STR_AUTOINCR   string = "autoincr" //自增int
	STR_AUTOID     string = "autoid"   //自增型单主键
	STR_INTID      string = "intid"    //int型单主键
	STR_NOT_INSERT string = "n_insert" //构造insert语句时忽略
	STR_NOT_UPDATE string = "n_update" //构造update语句时忽略
	STR_NOT_SELECT string = "n_select" //构造select语句时忽略
)
View Source
const (
	STMT_NOT_USE     int = 0
	STMT_EXE_PREPARE int = 1
	STMT_USE_CACHE   int = 2
)

返回参数中的状态:1 没有命中cache, 2 命中cache

View Source
const MaxNestDeep int = 10

匿名字段嵌套的最大深度

Variables

View Source
var DbConn *sql.DB = nil
View Source
var SlaveDb *sql.DB = nil

Functions

func AddSlave

func AddSlave(db *sql.DB, db_name string, weight int) error

func CopyDataFromModel

func CopyDataFromModel(md *DbModel, vo_ptr interface{}, mo_ptr interface{}) (int, error)

执行vo=mo的赋值操作,只有名称相同、类型相同的字段才能赋值 若md != nil,则获取mo的字段地址,并调用md的set_flag_by_addr函数来选中该字段

func CopyDataToModel

func CopyDataToModel(md *DbModel, vo_ptr interface{}, mo_ptr interface{}) (int, error)

执行mo=vo的赋值操作,只有名称相同、类型相同的字段才能赋值 若md != nil,则获取mo的字段地址,并调用md的set_flag_by_addr函数来选中该字段

func CreateIntArray

func CreateIntArray(arr_ptr interface{}, field string, def_val ...int64) ([]int64, error)

从对象数组中获取整形字段的值

func GetBool

func GetBool(md *DbModel, fld_ptr *bool) bool

func GetDirectType

func GetDirectType(t reflect.Type) reflect.Type

获取直接类型

func GetFloat32

func GetFloat32(md *DbModel, fld_ptr *float32) float32

func GetFloat64

func GetFloat64(md *DbModel, fld_ptr *float64) float64

func GetIndirect

func GetIndirect(md *DbModel, fld_ptr interface{}) interface{}

func GetInt

func GetInt(md *DbModel, fld_ptr *int) int

func GetInt32

func GetInt32(md *DbModel, fld_ptr *int32) int32

func GetInt64

func GetInt64(md *DbModel, fld_ptr *int64) int64

func GetPointer

func GetPointer(md *DbModel, fld_ptr interface{}) interface{}

func GetString

func GetString(md *DbModel, fld_ptr *string) string

func GetTime

func GetTime(md *DbModel, fld_ptr *time.Time) time.Time

func InitEngine

func InitEngine(dialect Dialect, db *sql.DB) error

func InitMysql

func InitMysql(db *sql.DB) error

func InitPostgres

func InitPostgres(db *sql.DB) error

func InitSqlServer

func InitSqlServer(db *sql.DB) error

func InitSqlite3

func InitSqlite3(db *sql.DB) error

func LLike added in v1.0.7

func LLike(val string) string

func LRLike added in v1.0.7

func LRLike(val string) string

func OpenDb added in v1.0.6

func OpenDb() (*sql.DB, error)

func OpenSalveDb added in v1.0.6

func OpenSalveDb() (*sql.DB, error)

func RLike added in v1.0.7

func RLike(val string) string

func Scan

func Scan(rows DbRows, dest ...interface{}) error

func ScanModel

func ScanModel(rows DbRows, ent_ptr interface{}) error

func SetDebugLogLevel

func SetDebugLogLevel(level Level)

func SetMaxStmtCacheNum

func SetMaxStmtCacheNum(max_len int)

func SetSqlLogCB

func SetSqlLogCB(cb SqlPrintCB)

func SetValue

func SetValue(md *DbModel, fld_ptr interface{}, val interface{}) error

func ShowSqlLog

func ShowSqlLog(flag bool)

func UsePrepare

func UsePrepare(flag bool)

Types

type AfterDeleteInterface

type AfterDeleteInterface interface {
	AfterDelete(ctx context.Context)
}

type AfterInsertInterface

type AfterInsertInterface interface {
	AfterInsert(ctx context.Context)
}

type AfterQueryInterface

type AfterQueryInterface interface {
	AfterQuery(ctx context.Context)
}

type AfterUpdateInterface

type AfterUpdateInterface interface {
	AfterUpdate(ctx context.Context)
}

type BatchResult

type BatchResult struct {
	Count int64
	Err   error
}

func (*BatchResult) LastInsertId

func (this *BatchResult) LastInsertId() (int64, error)

func (*BatchResult) RowsAffected

func (this *BatchResult) RowsAffected() (int64, error)

type BeforeDeleteInterface

type BeforeDeleteInterface interface {
	BeforeDelete(ctx context.Context)
}

type BeforeInsertInterface

type BeforeInsertInterface interface {
	BeforeInsert(ctx context.Context)
}

type BeforeQueryInterface

type BeforeQueryInterface interface {
	BeforeQuery(ctx context.Context)
}

type BeforeUpdateInterface

type BeforeUpdateInterface interface {
	BeforeUpdate(ctx context.Context)
}

type ColumnInfo

type ColumnInfo struct {
	Name            string
	SQLType         string
	DbType          string
	Comment         string
	Length          int
	Length2         int
	Nullable        bool
	IsPrimaryKey    bool
	IsAutoIncrement bool
}

type DbEngine

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

func GetDefaultDbEngine added in v1.1.2

func GetDefaultDbEngine() *DbEngine

func NewEngine

func NewEngine(dialect Dialect, db *sql.DB) (*DbEngine, error)

dialect:数据库驱动 db数据库链接

func NewMysql

func NewMysql(db *sql.DB) (*DbEngine, error)

func NewPostgres

func NewPostgres(db *sql.DB) (*DbEngine, error)

func NewSqlServer

func NewSqlServer(db *sql.DB) (*DbEngine, error)

func NewSqlite3

func NewSqlite3(db *sql.DB) (*DbEngine, error)

func (*DbEngine) AddSlave

func (engine *DbEngine) AddSlave(db *sql.DB, db_name string, weight int) error

添加一个从库

func (*DbEngine) CleanStmtCache added in v1.1.2

func (engine *DbEngine) CleanStmtCache()

清空预处理缓存

func (*DbEngine) DB

func (engine *DbEngine) DB() *sql.DB

获取数据库连接池

func (*DbEngine) DefaultSession added in v1.1.2

func (engine *DbEngine) DefaultSession() *DbSession

func (*DbEngine) GetDialect

func (engine *DbEngine) GetDialect() Dialect

获取数据库方言对象

func (*DbEngine) Joint

func (engine *DbEngine) Joint(ent_ptr interface{}, alias string, fields ...string) *DbJoint

func (*DbEngine) Model

func (engine *DbEngine) Model(ent_ptr interface{}) *DbModel

func (*DbEngine) NewSession

func (engine *DbEngine) NewSession() *DbSession

func (*DbEngine) SQL

func (engine *DbEngine) SQL(sql_str string, args ...interface{}) *DbSQL

func (*DbEngine) SetMaxStmtCacheNum

func (engine *DbEngine) SetMaxStmtCacheNum(max_len int)

设置可缓存的最大stmt数量

func (*DbEngine) SetSqlLogCB

func (engine *DbEngine) SetSqlLogCB(cb SqlPrintCB)

设置sql log回调函数

func (*DbEngine) ShowSqlLog

func (engine *DbEngine) ShowSqlLog(flag bool)

是否显示Sql日志

func (*DbEngine) Table

func (engine *DbEngine) Table(table_name string) *DbTable

func (*DbEngine) UsePrepare

func (dbs *DbEngine) UsePrepare(flag bool)

是否启用预处理

type DbJoint

type DbJoint struct {
	SqlContex

	Err error
	// contains filtered or unexported fields
}

func Joint

func Joint(ent_ptr interface{}, alias string, fields ...string) *DbJoint

func NewJoint

func NewJoint(dbs *DbSession, ent_ptr interface{}, alias string, fields ...string) *DbJoint

func (*DbJoint) And

func (lk *DbJoint) And(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) AndExp

func (lk *DbJoint) AndExp(sqlw_sub *DbWhere) *DbJoint

func (*DbJoint) AndIf

func (lk *DbJoint) AndIf(cond bool, sql string, vals ...interface{}) *DbJoint

func (*DbJoint) AndIn

func (lk *DbJoint) AndIn(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) AndNotIn

func (lk *DbJoint) AndNotIn(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) Context

func (lk *DbJoint) Context(ctx context.Context) *DbJoint

func (*DbJoint) CopyModelData2Eo added in v1.1.0

func (lk *DbJoint) CopyModelData2Eo(cache *JointEoFieldCache, v_vo reflect.Value)

把Model中地址的值赋值给vo对象

func (*DbJoint) CopyModelData2Vo added in v1.1.0

func (lk *DbJoint) CopyModelData2Vo(vo_ptr VoLoader)

把Model中地址的值赋值给vo对象

func (*DbJoint) Find

func (lk *DbJoint) Find(arr_ptr interface{}) error

func (*DbJoint) Get

func (lk *DbJoint) Get(args ...interface{}) (bool, error)

func (*DbJoint) GetContext

func (lk *DbJoint) GetContext() context.Context

func (*DbJoint) Join

func (lk *DbJoint) Join(ent_ptr interface{}, alias string, join_on string, fields ...string) *DbJoint

func (*DbJoint) LeftJoin

func (lk *DbJoint) LeftJoin(ent_ptr interface{}, alias string, join_on string, fields ...string) *DbJoint

func (*DbJoint) Limit

func (lk *DbJoint) Limit(rows int64) *DbJoint

func (*DbJoint) Offset

func (lk *DbJoint) Offset(offset int64) *DbJoint

func (*DbJoint) Or

func (lk *DbJoint) Or(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) OrExp

func (lk *DbJoint) OrExp(sqlw_sub *DbWhere) *DbJoint

func (*DbJoint) OrIf

func (lk *DbJoint) OrIf(cond bool, sql string, vals ...interface{}) *DbJoint

func (*DbJoint) OrIn

func (lk *DbJoint) OrIn(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) OrNotIn

func (lk *DbJoint) OrNotIn(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) OrderBy

func (lk *DbJoint) OrderBy(val string) *DbJoint

func (*DbJoint) Page

func (lk *DbJoint) Page(rows int64, page_no int64) *DbJoint

func (*DbJoint) RightJoin

func (lk *DbJoint) RightJoin(ent_ptr interface{}, alias string, join_on string, fields ...string) *DbJoint

func (*DbJoint) Scan

func (lk *DbJoint) Scan() (bool, error)

func (*DbJoint) SetWhere

func (lk *DbJoint) SetWhere(sqlw *DbWhere) *DbJoint

func (*DbJoint) ShowLog

func (lk *DbJoint) ShowLog(val bool) *DbJoint

func (*DbJoint) Top

func (lk *DbJoint) Top(rows int64) *DbJoint

func (*DbJoint) UseMaster

func (lk *DbJoint) UseMaster(val bool) *DbJoint

func (*DbJoint) UsePrepare

func (lk *DbJoint) UsePrepare(val bool) *DbJoint

func (*DbJoint) Where

func (lk *DbJoint) Where(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) WhereIf

func (lk *DbJoint) WhereIf(cond bool, sql string, vals ...interface{}) *DbJoint

func (*DbJoint) WhereIn

func (lk *DbJoint) WhereIn(sql string, vals ...interface{}) *DbJoint

func (*DbJoint) WhereNotIn

func (lk *DbJoint) WhereNotIn(sql string, vals ...interface{}) *DbJoint

type DbModel

type DbModel struct {
	SqlContex

	Err error
	// contains filtered or unexported fields
}

func Model

func Model(ent_ptr interface{}) *DbModel

func NewModel

func NewModel(dbs *DbSession, ent_ptr interface{}, flag bool) *DbModel

func (*DbModel) AddField added in v1.1.0

func (md *DbModel) AddField(fields ...string) *DbModel

追加选中一批字段

func (*DbModel) AddFieldX added in v1.1.0

func (md *DbModel) AddFieldX(fields ...interface{}) *DbModel

追加选中一批字段

func (*DbModel) And

func (md *DbModel) And(sql string, vals ...interface{}) *DbModel

func (*DbModel) AndExp

func (md *DbModel) AndExp(sqlw_sub *DbWhere) *DbModel

func (*DbModel) AndIf

func (md *DbModel) AndIf(cond bool, sql string, vals ...interface{}) *DbModel

func (*DbModel) AndIn

func (md *DbModel) AndIn(sql string, vals ...interface{}) *DbModel

func (*DbModel) AndNotIn

func (md *DbModel) AndNotIn(sql string, vals ...interface{}) *DbModel

func (*DbModel) AndX

func (md *DbModel) AndX(field_ptr interface{}, oper string, vals ...interface{}) *DbModel

func (*DbModel) BatchInsert

func (md *DbModel) BatchInsert(arr_ptr interface{}) (sql.Result, error)

func (*DbModel) Context

func (md *DbModel) Context(ctx context.Context) *DbModel

func (*DbModel) Count

func (md *DbModel) Count(field ...string) (int64, error)

func (*DbModel) Delete

func (md *DbModel) Delete() (int64, error)

func (*DbModel) DistinctCount

func (md *DbModel) DistinctCount(field string) (int64, error)

func (*DbModel) Exist

func (md *DbModel) Exist() (bool, error)

func (*DbModel) Find

func (md *DbModel) Find(arr_ptr interface{}) error

func (*DbModel) Get

func (md *DbModel) Get(args ...interface{}) (bool, error)

func (*DbModel) GetContext

func (md *DbModel) GetContext() context.Context

func (*DbModel) GetFieldFlag4Insert added in v0.2.0

func (md *DbModel) GetFieldFlag4Insert(i int) bool

func (*DbModel) GetFieldFlag4Select added in v0.2.0

func (md *DbModel) GetFieldFlag4Select(i int) bool

func (*DbModel) GetFieldFlag4Update added in v0.2.0

func (md *DbModel) GetFieldFlag4Update(i int) bool

func (*DbModel) GetIf added in v1.0.7

func (md *DbModel) GetIf(flag bool, args ...interface{}) (bool, error)

若flag==true,则正常执行 若flag==false,则退出执行

func (*DbModel) GetModelEnt

func (md *DbModel) GetModelEnt() interface{}

func (*DbModel) ID

func (md *DbModel) ID(val int64) *DbModel

func (*DbModel) Insert

func (md *DbModel) Insert(args ...interface{}) (int64, error)

func (*DbModel) InsertIfNotExist

func (md *DbModel) InsertIfNotExist(args ...interface{}) (affected int64, insertId int64, err error)

若不存在记录,则调用Insert

func (*DbModel) Limit

func (md *DbModel) Limit(rows int64) *DbModel

func (*DbModel) Lock added in v1.2.1

func (md *DbModel) Lock(val string) *DbModel

func (*DbModel) Offset

func (md *DbModel) Offset(offset int64) *DbModel

func (*DbModel) Omit

func (md *DbModel) Omit(fields ...string) *DbModel

排除若干字段,其余全部选中

func (*DbModel) OmitALL

func (md *DbModel) OmitALL() *DbModel

func (*DbModel) OmitX

func (md *DbModel) OmitX(fields ...interface{}) *DbModel

排除若干字段,其余全部选中

func (*DbModel) Or

func (md *DbModel) Or(sql string, vals ...interface{}) *DbModel

func (*DbModel) OrExp

func (md *DbModel) OrExp(sqlw_sub *DbWhere) *DbModel

func (*DbModel) OrIf

func (md *DbModel) OrIf(cond bool, sql string, vals ...interface{}) *DbModel

func (*DbModel) OrIn

func (md *DbModel) OrIn(sql string, vals ...interface{}) *DbModel

func (*DbModel) OrNotIn

func (md *DbModel) OrNotIn(sql string, vals ...interface{}) *DbModel

func (*DbModel) OrX

func (md *DbModel) OrX(field_ptr interface{}, oper string, vals ...interface{}) *DbModel

func (*DbModel) OrderBy

func (md *DbModel) OrderBy(val string) *DbModel

func (*DbModel) Page

func (md *DbModel) Page(rows int64, page_no int64) *DbModel

func (*DbModel) Reset

func (md *DbModel) Reset()

重置model状态,保留以下字段的内容: ent_ptr、flds_info、flds_addr、table_name、field_id、name_map_db、name_map_go

func (*DbModel) Rows

func (md *DbModel) Rows() (ModelRows, error)

func (*DbModel) Save

func (md *DbModel) Save(args ...interface{}) (affected int64, insertId int64, err error)

若存在记录,则调用Update,否则调用Insert

func (*DbModel) Scan

func (md *DbModel) Scan() (bool, error)

func (*DbModel) Select

func (md *DbModel) Select(fields ...string) *DbModel

选中一批字段

func (*DbModel) SelectALL

func (md *DbModel) SelectALL() *DbModel

func (*DbModel) SelectX

func (md *DbModel) SelectX(fields ...interface{}) *DbModel

选中一批字段

func (*DbModel) SetDbSession

func (md *DbModel) SetDbSession(dbs *DbSession) *DbModel

func (*DbModel) SetValue

func (md *DbModel) SetValue(fld_ptr interface{}, val interface{}) error

设置字段的值,并选中该字段

func (*DbModel) SetWhere

func (md *DbModel) SetWhere(sqlw *DbWhere) *DbModel

func (*DbModel) ShowLog

func (md *DbModel) ShowLog(val bool) *DbModel

func (*DbModel) Table added in v1.1.1

func (md *DbModel) Table(val string) *DbModel

func (*DbModel) TableAlias

func (md *DbModel) TableAlias(alias string) *DbJoint

func (*DbModel) TableName

func (md *DbModel) TableName(val string) *DbModel

func (*DbModel) Top

func (md *DbModel) Top(rows int64) *DbModel

func (*DbModel) Update

func (md *DbModel) Update(args ...interface{}) (int64, error)

func (*DbModel) UpdateOrInsert

func (md *DbModel) UpdateOrInsert(id int64, args ...interface{}) (affected int64, entId int64, err error)

id>0调用Update, 否则调用Insert

func (*DbModel) UseMaster

func (md *DbModel) UseMaster(val bool) *DbModel

func (*DbModel) UsePrepare

func (md *DbModel) UsePrepare(val bool) *DbModel

func (*DbModel) Where

func (md *DbModel) Where(sql string, vals ...interface{}) *DbModel

func (*DbModel) WhereIf

func (md *DbModel) WhereIf(cond bool, sql string, vals ...interface{}) *DbModel

func (*DbModel) WhereIn

func (md *DbModel) WhereIn(sql string, vals ...interface{}) *DbModel

func (*DbModel) WhereNotIn

func (md *DbModel) WhereNotIn(sql string, vals ...interface{}) *DbModel

type DbRows added in v1.1.0

type DbRows struct {
	*sql.Rows
}

func (*DbRows) Scan added in v1.1.0

func (rows *DbRows) Scan(dest ...interface{}) error

type DbSQL

type DbSQL struct {
	SqlContex
	// contains filtered or unexported fields
}

func NewDbSQL

func NewDbSQL(dbs *DbSession, sql_str string, args ...interface{}) *DbSQL

func SQL

func SQL(sql_str string, args ...interface{}) *DbSQL

func (*DbSQL) Context

func (tb *DbSQL) Context(ctx context.Context) *DbSQL

func (*DbSQL) Exec

func (tb *DbSQL) Exec() (sql.Result, error)

func (*DbSQL) FindFloat

func (tb *DbSQL) FindFloat() ([]float64, error)

func (*DbSQL) FindInt

func (tb *DbSQL) FindInt() ([]int64, error)

func (*DbSQL) FindIntFloat added in v1.0.7

func (tb *DbSQL) FindIntFloat() ([]KeyVal4IntFloat, error)

func (*DbSQL) FindIntInt added in v1.0.7

func (tb *DbSQL) FindIntInt() ([]KeyVal4IntInt, error)

func (*DbSQL) FindIntString added in v1.0.7

func (tb *DbSQL) FindIntString() ([]KeyVal4IntString, error)

func (*DbSQL) FindIntTime added in v1.0.7

func (tb *DbSQL) FindIntTime() ([]KeyVal4IntTime, error)

func (*DbSQL) FindModel

func (tb *DbSQL) FindModel(arr_ptr interface{}) error

func (*DbSQL) FindRow

func (tb *DbSQL) FindRow() (*StringTable, error)

func (*DbSQL) FindString

func (tb *DbSQL) FindString() ([]string, error)

func (*DbSQL) FindStringFloat added in v1.0.7

func (tb *DbSQL) FindStringFloat() ([]KeyVal4StringFloat, error)

func (*DbSQL) FindStringInt added in v1.0.7

func (tb *DbSQL) FindStringInt() ([]KeyVal4StringInt, error)

func (*DbSQL) FindStringString added in v1.0.7

func (tb *DbSQL) FindStringString() ([]KeyVal4StringString, error)

func (*DbSQL) FindStringTime added in v1.0.7

func (tb *DbSQL) FindStringTime() ([]KeyVal4StringTime, error)

func (*DbSQL) FindTime added in v1.0.7

func (tb *DbSQL) FindTime() ([]time.Time, error)

func (*DbSQL) FindValues added in v1.0.7

func (tb *DbSQL) FindValues(arr_ptr_arr ...interface{}) (int, error)

func (*DbSQL) Get

func (tb *DbSQL) Get(arg ...interface{}) (bool, error)

func (*DbSQL) GetContext

func (tb *DbSQL) GetContext() context.Context

func (*DbSQL) GetFloat

func (tb *DbSQL) GetFloat() (sql.NullFloat64, error)

func (*DbSQL) GetInt

func (tb *DbSQL) GetInt() (sql.NullInt64, error)

func (*DbSQL) GetModel

func (tb *DbSQL) GetModel(ent_ptr interface{}) (bool, error)

func (*DbSQL) GetRow

func (tb *DbSQL) GetRow() (StringRow, error)

func (*DbSQL) GetString

func (tb *DbSQL) GetString() (sql.NullString, error)

func (*DbSQL) GetTime added in v1.0.6

func (tb *DbSQL) GetTime() (sql.NullTime, error)

func (*DbSQL) GetValues added in v1.0.7

func (tb *DbSQL) GetValues(arg ...interface{}) (bool, error)

func (*DbSQL) ModelRows added in v1.1.0

func (tb *DbSQL) ModelRows() (StructRows, error)

func (*DbSQL) Rows

func (tb *DbSQL) Rows() (DbRows, error)

func (*DbSQL) ShowLog

func (tb *DbSQL) ShowLog(val bool) *DbSQL

func (*DbSQL) SqlRows added in v1.1.0

func (tb *DbSQL) SqlRows() (*sql.Rows, error)

func (*DbSQL) StringRows added in v1.1.0

func (tb *DbSQL) StringRows() (StringRows, error)

func (*DbSQL) UseMaster

func (tb *DbSQL) UseMaster(val bool) *DbSQL

func (*DbSQL) UsePrepare

func (tb *DbSQL) UsePrepare(val bool) *DbSQL

type DbSession

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

func NewDbSession

func NewDbSession(engine *DbEngine) *DbSession

func NewSession

func NewSession() *DbSession

func Session added in v1.1.1

func Session() *DbSession

func TxBegin added in v1.1.1

func TxBegin() (*DbSession, error)

func (*DbSession) DB

func (dbs *DbSession) DB() *sql.DB

func (*DbSession) ExecQuery

func (dbs *DbSession) ExecQuery(ctx *SqlContex, sql_tpl string, args ...interface{}) (res *sql.Rows, err error)

执行数据库查询

func (*DbSession) ExecSQL

func (dbs *DbSession) ExecSQL(ctx *SqlContex, sql_tpl string, args ...interface{}) (res sql.Result, err error)

执行一个sql命令

func (*DbSession) Joint

func (dbs *DbSession) Joint(ent_ptr interface{}, alias string, fields ...string) *DbJoint

func (*DbSession) Model

func (dbs *DbSession) Model(ent_ptr interface{}) *DbModel

func (*DbSession) NewModel

func (dbs *DbSession) NewModel(ent_ptr interface{}, flag bool) *DbModel

func (*DbSession) Prepare

func (dbs *DbSession) Prepare(sql_tpl string) (*sql.Stmt, error)

提交一个sql预处理 如果是事务会话,则使用tx_raw.Prepare 否则使用db_raw.Prepare提交预处理

func (*DbSession) SQL

func (dbs *DbSession) SQL(sql_str string, args ...interface{}) *DbSQL

func (*DbSession) ShowLog

func (dbs *DbSession) ShowLog(flag bool)

是否显示日志

func (*DbSession) Table

func (dbs *DbSession) Table(table_name string) *DbTable

func (*DbSession) Tx

func (dbs *DbSession) Tx() *sql.Tx

func (*DbSession) TxBegin

func (dbs *DbSession) TxBegin() error

开启数据库事务

func (*DbSession) TxCommit

func (dbs *DbSession) TxCommit() error

执行事务提交

func (*DbSession) TxRollback

func (dbs *DbSession) TxRollback() error

执行事务回滚

func (*DbSession) UsePrepare

func (dbs *DbSession) UsePrepare(flag bool)

是否启用预处理

type DbSlave

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

type DbTable

type DbTable struct {
	SqlContex
	// contains filtered or unexported fields
}

func NewDbTable

func NewDbTable(dbs *DbSession, table_name string) *DbTable

func Table

func Table(table_name string) *DbTable

func (*DbTable) Alias

func (tb *DbTable) Alias(alias string) *DbTable

func (*DbTable) And

func (tb *DbTable) And(sql string, vals ...interface{}) *DbTable

func (*DbTable) AndExp

func (tb *DbTable) AndExp(sqlw_sub *DbWhere) *DbTable

func (*DbTable) AndIf

func (tb *DbTable) AndIf(cond bool, sql string, vals ...interface{}) *DbTable

func (*DbTable) AndIn

func (tb *DbTable) AndIn(sql string, vals ...interface{}) *DbTable

func (*DbTable) AndNotIn

func (tb *DbTable) AndNotIn(sql string, vals ...interface{}) *DbTable

func (*DbTable) Context

func (tb *DbTable) Context(ctx context.Context) *DbTable

func (*DbTable) Count

func (tb *DbTable) Count(field ...string) (int64, error)

func (*DbTable) Delete

func (tb *DbTable) Delete() (int64, error)

func (*DbTable) DistinctCount

func (tb *DbTable) DistinctCount(field string) (int64, error)

func (*DbTable) Exist

func (tb *DbTable) Exist() (bool, error)

func (*DbTable) FindFloat

func (tb *DbTable) FindFloat() ([]float64, error)

func (*DbTable) FindInt

func (tb *DbTable) FindInt() ([]int64, error)

func (*DbTable) FindIntFloat added in v1.0.7

func (tb *DbTable) FindIntFloat() ([]KeyVal4IntFloat, error)

func (*DbTable) FindIntInt added in v1.0.7

func (tb *DbTable) FindIntInt() ([]KeyVal4IntInt, error)

func (*DbTable) FindIntString added in v1.0.7

func (tb *DbTable) FindIntString() ([]KeyVal4IntString, error)

func (*DbTable) FindIntTime added in v1.0.7

func (tb *DbTable) FindIntTime() ([]KeyVal4IntTime, error)

func (*DbTable) FindModel

func (tb *DbTable) FindModel(arr_ptr interface{}) error

func (*DbTable) FindRow

func (tb *DbTable) FindRow() (*StringTable, error)

func (*DbTable) FindString

func (tb *DbTable) FindString() ([]string, error)

func (*DbTable) FindStringFloat added in v1.0.7

func (tb *DbTable) FindStringFloat() ([]KeyVal4StringFloat, error)

func (*DbTable) FindStringInt added in v1.0.7

func (tb *DbTable) FindStringInt() ([]KeyVal4StringInt, error)

func (*DbTable) FindStringString added in v1.0.7

func (tb *DbTable) FindStringString() ([]KeyVal4StringString, error)

func (*DbTable) FindStringTime added in v1.0.7

func (tb *DbTable) FindStringTime() ([]KeyVal4StringTime, error)

func (*DbTable) FindTime added in v1.0.7

func (tb *DbTable) FindTime() ([]time.Time, error)

func (*DbTable) FindValues added in v1.0.7

func (tb *DbTable) FindValues(arr_ptr_arr ...interface{}) (int, error)

func (*DbTable) Get

func (tb *DbTable) Get(arg ...interface{}) (bool, error)

func (*DbTable) GetContext

func (tb *DbTable) GetContext() context.Context

func (*DbTable) GetFlaot

func (tb *DbTable) GetFlaot() (sql.NullFloat64, error)

func (*DbTable) GetFloat added in v1.0.5

func (tb *DbTable) GetFloat() (sql.NullFloat64, error)

func (*DbTable) GetInt

func (tb *DbTable) GetInt() (sql.NullInt64, error)

func (*DbTable) GetModel

func (tb *DbTable) GetModel(ent_ptr interface{}) (bool, error)

func (*DbTable) GetRow

func (tb *DbTable) GetRow() (StringRow, error)

func (*DbTable) GetString

func (tb *DbTable) GetString() (sql.NullString, error)

func (*DbTable) GetTime added in v1.0.6

func (tb *DbTable) GetTime() (sql.NullTime, error)

func (*DbTable) GetValues added in v1.0.7

func (tb *DbTable) GetValues(arg ...interface{}) (bool, error)

func (*DbTable) GroupBy

func (tb *DbTable) GroupBy(val string) *DbTable

func (*DbTable) Having

func (tb *DbTable) Having(sql string, vals ...interface{}) *DbTable

func (*DbTable) ID

func (tb *DbTable) ID(val int64) *DbTable

func (*DbTable) Insert

func (tb *DbTable) Insert() (int64, error)

func (*DbTable) Join

func (tb *DbTable) Join(table string, alias string, join_on string) *DbTable

func (*DbTable) LeftJoin

func (tb *DbTable) LeftJoin(table string, alias string, join_on string) *DbTable

func (*DbTable) Limit

func (tb *DbTable) Limit(rows int64) *DbTable

func (*DbTable) Lock added in v1.2.1

func (tb *DbTable) Lock(val string) *DbTable

func (*DbTable) ModelRows added in v1.1.0

func (tb *DbTable) ModelRows() (StructRows, error)

func (*DbTable) Offset

func (tb *DbTable) Offset(offset int64) *DbTable

func (*DbTable) Or

func (tb *DbTable) Or(sql string, vals ...interface{}) *DbTable

func (*DbTable) OrExp

func (tb *DbTable) OrExp(sqlw_sub *DbWhere) *DbTable

func (*DbTable) OrIf

func (tb *DbTable) OrIf(cond bool, sql string, vals ...interface{}) *DbTable

func (*DbTable) OrIn

func (tb *DbTable) OrIn(sql string, vals ...interface{}) *DbTable

func (*DbTable) OrNotIn

func (tb *DbTable) OrNotIn(sql string, vals ...interface{}) *DbTable

func (*DbTable) OrderBy

func (tb *DbTable) OrderBy(val string) *DbTable

func (*DbTable) Output added in v0.2.0

func (tb *DbTable) Output(output string) *DbTable

func (*DbTable) Page

func (tb *DbTable) Page(rows int64, page_no int64) *DbTable

func (*DbTable) ReturnIng added in v0.3.0

func (tb *DbTable) ReturnIng(returning string) *DbTable

func (*DbTable) RightJoin

func (tb *DbTable) RightJoin(table string, alias string, join_on string) *DbTable

func (*DbTable) Rows

func (tb *DbTable) Rows() (DbRows, error)

func (*DbTable) Select

func (tb *DbTable) Select(fields ...string) *DbTable

func (*DbTable) SetWhere

func (tb *DbTable) SetWhere(sqlw *DbWhere) *DbTable

func (*DbTable) ShowLog

func (tb *DbTable) ShowLog(val bool) *DbTable

func (*DbTable) SqlRows added in v1.1.0

func (tb *DbTable) SqlRows() (*sql.Rows, error)

func (*DbTable) StringRows added in v1.1.0

func (tb *DbTable) StringRows() (StringRows, error)

func (*DbTable) Top

func (tb *DbTable) Top(rows int64) *DbTable

func (*DbTable) Update

func (tb *DbTable) Update() (int64, error)

func (*DbTable) UseMaster

func (tb *DbTable) UseMaster(val bool) *DbTable

func (*DbTable) UsePrepare

func (tb *DbTable) UsePrepare(val bool) *DbTable

func (*DbTable) Value

func (tb *DbTable) Value(col_name string, val interface{}) *DbTable

func (*DbTable) Values

func (tb *DbTable) Values(map_data ValueMap) *DbTable

func (*DbTable) Where

func (tb *DbTable) Where(sql string, vals ...interface{}) *DbTable

func (*DbTable) WhereIf

func (tb *DbTable) WhereIf(cond bool, sql string, vals ...interface{}) *DbTable

func (*DbTable) WhereIn

func (tb *DbTable) WhereIn(sql string, vals ...interface{}) *DbTable

func (*DbTable) WhereNotIn

func (tb *DbTable) WhereNotIn(sql string, vals ...interface{}) *DbTable

type DbWhere

type DbWhere struct {
	Tpl_sql string
	Values  []interface{}
}

https://gitee.com/fifsky/gosql

func SQLW

func SQLW(sql string, vals ...interface{}) *DbWhere

func (*DbWhere) And

func (sqlw *DbWhere) And(sql string, vals ...interface{}) *DbWhere

func (*DbWhere) AndExp

func (sqlw *DbWhere) AndExp(sqlw_sub *DbWhere) *DbWhere

func (*DbWhere) AndIf

func (sqlw *DbWhere) AndIf(cond bool, sql string, vals ...interface{}) *DbWhere

func (*DbWhere) AndIn

func (sqlw *DbWhere) AndIn(field string, vals ...interface{}) *DbWhere

func (*DbWhere) AndNotIn

func (sqlw *DbWhere) AndNotIn(field string, vals ...interface{}) *DbWhere

func (*DbWhere) GenPlatSQl

func (sqlw *DbWhere) GenPlatSQl() string

func (*DbWhere) GetTpl

func (sqlw *DbWhere) GetTpl() string

func (*DbWhere) GetValues

func (sqlw *DbWhere) GetValues() []interface{}

func (*DbWhere) Init

func (sqlw *DbWhere) Init(sql string, vals ...interface{}) *DbWhere

func (*DbWhere) Or

func (sqlw *DbWhere) Or(sql string, vals ...interface{}) *DbWhere

func (*DbWhere) OrExp

func (sqlw *DbWhere) OrExp(sqlw_sub *DbWhere) *DbWhere

func (*DbWhere) OrIf

func (sqlw *DbWhere) OrIf(cond bool, sql string, vals ...interface{}) *DbWhere

func (*DbWhere) OrIn

func (sqlw *DbWhere) OrIn(field string, vals ...interface{}) *DbWhere

func (*DbWhere) OrNotIn

func (sqlw *DbWhere) OrNotIn(field string, vals ...interface{}) *DbWhere

func (*DbWhere) Reset

func (sqlw *DbWhere) Reset()

type Dialect

type Dialect interface {
	GetName() string
	DbType2GoType(colType string) string
	GetColumns(db_raw *sql.DB, tableName string) ([]ColumnInfo, error)
	LimitSql(offset int64, limit int64) string
	ParsePlaceholder(sql_tpl string) string

	ModelInsertHasOutput(md *DbModel) bool
	GenModelInsertSql(md *DbModel) string
	GenModelUpdateSql(md *DbModel) string
	GenModelDeleteSql(md *DbModel) string
	GenModelGetSql(md *DbModel) string
	GenModelFindSql(md *DbModel) string

	TableInsertHasOutput(tb *DbTable) bool
	GenTableInsertSql(tb *DbTable) (string, []interface{})
	GenTableUpdateSql(tb *DbTable) (string, []interface{})
	GenTableDeleteSql(tb *DbTable) string
	GenTableGetSql(tb *DbTable) string
	GenTableFindSql(tb *DbTable) string

	GenJointGetSql(lk *DbJoint) string
	GenJointFindSql(lk *DbJoint) string
}

type DialectBase added in v0.3.0

type DialectBase struct {
}

func (*DialectBase) DbType2GoType added in v0.3.0

func (db *DialectBase) DbType2GoType(colType string) string

func (*DialectBase) GenJointFindSql added in v0.3.0

func (db *DialectBase) GenJointFindSql(lk *DbJoint) string

func (*DialectBase) GenJointGetSql added in v0.3.0

func (db *DialectBase) GenJointGetSql(lk *DbJoint) string

func (*DialectBase) GenModelDeleteSql added in v0.3.0

func (db *DialectBase) GenModelDeleteSql(md *DbModel) string

func (*DialectBase) GenModelFindSql added in v0.3.0

func (db *DialectBase) GenModelFindSql(md *DbModel) string

func (*DialectBase) GenModelGetSql added in v0.3.0

func (db *DialectBase) GenModelGetSql(md *DbModel) string

func (*DialectBase) GenModelInsertSql added in v0.3.0

func (db *DialectBase) GenModelInsertSql(md *DbModel) string

func (*DialectBase) GenModelUpdateSql added in v0.3.0

func (db *DialectBase) GenModelUpdateSql(md *DbModel) string

func (*DialectBase) GenTableDeleteSql added in v0.3.0

func (db *DialectBase) GenTableDeleteSql(tb *DbTable) string

func (*DialectBase) GenTableFindSql added in v0.3.0

func (db *DialectBase) GenTableFindSql(tb *DbTable) string

func (*DialectBase) GenTableGetSql added in v0.3.0

func (db *DialectBase) GenTableGetSql(tb *DbTable) string

func (*DialectBase) GenTableInsertSql added in v0.3.0

func (db *DialectBase) GenTableInsertSql(tb *DbTable) (string, []interface{})

生成insert sql语句 sql语句与values数组必须在一个循环中生成,因为map多次遍历时次序可能不同

func (*DialectBase) GenTableUpdateSql added in v0.3.0

func (db *DialectBase) GenTableUpdateSql(tb *DbTable) (string, []interface{})

生成 update sql语句 sql语句与values数组必须在一个循环中生成,因为map多次遍历时次序可能不同

func (*DialectBase) GetColumns added in v0.3.0

func (db *DialectBase) GetColumns(db_raw *sql.DB, tableName string) ([]ColumnInfo, error)

func (*DialectBase) GetName added in v0.3.0

func (db *DialectBase) GetName() string

func (*DialectBase) LimitSql added in v0.3.0

func (db *DialectBase) LimitSql(offset int64, limit int64) string

func (*DialectBase) ModelInsertHasOutput added in v0.3.0

func (db *DialectBase) ModelInsertHasOutput(md *DbModel) bool

func (*DialectBase) ParsePlaceholder added in v0.3.0

func (db *DialectBase) ParsePlaceholder(sql_tpl string) string

func (*DialectBase) TableInsertHasOutput added in v0.3.0

func (db *DialectBase) TableInsertHasOutput(tb *DbTable) bool

type FieldIndex added in v1.1.0

type FieldIndex struct {
	FieldName string
	VoIndex   []int
	MoIndex   int
	VoField   FieldPos
}

type FieldInfo

type FieldInfo struct {
	FieldIndex int
	FieldName  string
	FieldType  reflect.Type
	DbName     string
	AutoIncr   bool
	AutoId     bool
	IntId      bool
	NotInsert  bool
	NotUpdate  bool
	NotSelect  bool
}

type FieldPos added in v1.1.0

type FieldPos [MaxNestDeep]int

type FieldValue

type FieldValue struct {
	FName string
	VAddr interface{}
	Flag  bool
}

func (*FieldValue) Scan

func (fd *FieldValue) Scan(value interface{}) error

Scan implements the Scanner interface.

type JointEoFieldCache added in v1.1.0

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

type KeyVal added in v1.1.2

type KeyVal struct {
	Key string
	Val interface{}
}

type KeyVal4IntFloat added in v1.0.7

type KeyVal4IntFloat struct {
	Key int64
	Val float64
}

type KeyVal4IntInt added in v1.0.7

type KeyVal4IntInt struct {
	Key int64
	Val int64
}

KeyVal use int64 as key

type KeyVal4IntString added in v1.0.7

type KeyVal4IntString struct {
	Key int64
	Val string
}

type KeyVal4IntTime added in v1.0.7

type KeyVal4IntTime struct {
	Key int64
	Val time.Time
}

type KeyVal4StringFloat added in v1.0.7

type KeyVal4StringFloat struct {
	Key string
	Val float64
}

type KeyVal4StringInt added in v1.0.7

type KeyVal4StringInt struct {
	Key string
	Val int64
}

KeyVal use string as key

type KeyVal4StringString added in v1.0.7

type KeyVal4StringString struct {
	Key string
	Val string
}

type KeyVal4StringTime added in v1.0.7

type KeyVal4StringTime struct {
	Key string
	Val time.Time
}

type Level

type Level int
const (
	LOG_OFF Level = iota
	LOG_FATAL
	LOG_ERROR
	LOG_WARN
	LOG_INFO
	LOG_DEBUG
)

type LogContex

type LogContex struct {
	Ctx     context.Context
	Session *DbSession
	DbName  string
	SqlType string
	Start   time.Time
	SQL     string
	Args    []interface{}
	Err     error
	Result  sql.Result
	ExeTime time.Duration
	IsTx    bool
	UseStmt int
}

func (*LogContex) GetPrintResult

func (ctx *LogContex) GetPrintResult() string

生成执行结果日志

func (*LogContex) GetPrintSQL

func (ctx *LogContex) GetPrintSQL() string

生成SQL请求日志

type LogPrintCB

type LogPrintCB func(level Level, msg string)

打印调试日志

type ModelInfo

type ModelInfo struct {
	Fields    []FieldInfo
	TableName string
	FieldID   string
	NameMapDb map[string]int
	NameMapGo map[string]int
}

type ModelPool

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

func NewModelPool added in v1.1.0

func NewModelPool(ent interface{}, size ...int) *ModelPool

func (*ModelPool) Get

func (p *ModelPool) Get(dbs ...*DbSession) *DbModel

分配一个model

func (*ModelPool) Put

func (p *ModelPool) Put(md *DbModel)

model入池

type ModelRows

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

func (*ModelRows) Scan added in v1.1.0

func (rows *ModelRows) Scan(ent_ptr interface{}) error

func (*ModelRows) ScanModel

func (rows *ModelRows) ScanModel(ent_ptr interface{}) error

type PublicFields added in v1.1.0

type PublicFields struct {
	//Model字段索引号
	ModelField int
	//其他字段索引信息
	Fields []FieldIndex
}

func NewPublicFields added in v1.1.0

func NewPublicFields(num int) *PublicFields

type ScanArray added in v1.0.7

type ScanArray struct {
	Val reflect.Value
	Arr reflect.Value
}

type SimpleLogger

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

func NewSimpleLogger

func NewSimpleLogger() *SimpleLogger

func (*SimpleLogger) Debug

func (lg *SimpleLogger) Debug(v ...interface{})

func (*SimpleLogger) Debugf

func (lg *SimpleLogger) Debugf(format string, v ...interface{})

func (*SimpleLogger) Error

func (lg *SimpleLogger) Error(v ...interface{})

func (*SimpleLogger) Errorf

func (lg *SimpleLogger) Errorf(format string, v ...interface{})

func (*SimpleLogger) Fatal

func (lg *SimpleLogger) Fatal(v ...interface{})

func (*SimpleLogger) Fatalf

func (lg *SimpleLogger) Fatalf(format string, v ...interface{})

func (*SimpleLogger) Info

func (lg *SimpleLogger) Info(v ...interface{})

func (*SimpleLogger) Infof

func (lg *SimpleLogger) Infof(format string, v ...interface{})

func (*SimpleLogger) Output

func (lg *SimpleLogger) Output(level_str string, msg string) error

func (*SimpleLogger) SetLevel

func (lg *SimpleLogger) SetLevel(level Level)

func (*SimpleLogger) Warn

func (lg *SimpleLogger) Warn(v ...interface{})

func (*SimpleLogger) Warnf

func (lg *SimpleLogger) Warnf(format string, v ...interface{})

type SqlContex

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

func (*SqlContex) Reset

func (ctx *SqlContex) Reset()

type SqlExp

type SqlExp struct {
	Tpl_sql string
	Values  []interface{}
}

func Expr

func Expr(expr string, args ...interface{}) SqlExp

type SqlPrintCB

type SqlPrintCB func(ctx *LogContex)

打印SQL日志

type StmtCache

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

func NewStmtCache

func NewStmtCache(max_num int) *StmtCache

func (*StmtCache) CleanStmtList

func (sc *StmtCache) CleanStmtList()

func (*StmtCache) CleanWasteList

func (sc *StmtCache) CleanWasteList(tm_now int64)

清理wasteList中的stmt 为了防止stmt清理时被其他协程使用,因此wasteList中的stmt一定要超过60秒才能close 若不过过60秒,继续加入到wasteList中,等待下次清理

func (*StmtCache) Close

func (sc *StmtCache) Close()

func (*StmtCache) GetStmt

func (sc *StmtCache) GetStmt(query string) (*sql.Stmt, bool)

从cacheList中获取一个stmt 若存在,将stmt移动到列头

func (*StmtCache) Prepare

func (sc *StmtCache) Prepare(dbs *DbSession, db *sql.DB, query string) (*sql.Stmt, int, error)

func (*StmtCache) RemoveOldest

func (sc *StmtCache) RemoveOldest()

cache_list的容量超过max_size时,将陈旧的stmt移到wasteList中 若距离上次清理时间超过指定的值,则启动清理协程

func (*StmtCache) SetStmt

func (sc *StmtCache) SetStmt(query string, stmt *sql.Stmt)

添加一个stmt到从cacheList 若存在,则直接移动到列头,否则添加一个item到cacheList中 若stmt的数量超出max_size,则将最后的stmt移动到wasteList中

func (*StmtCache) TryCleanWasteList

func (sc *StmtCache) TryCleanWasteList()

若距离上次清理时间>30秒,则进行实际清理

type StmtItem

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

type StringPair added in v1.1.0

type StringPair struct {
	Name  string
	Value string
}

type StringRow

type StringRow map[string]string

func ScanStringRow

func ScanStringRow(rows DbRows) (StringRow, error)

type StringRows added in v1.1.0

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

func (*StringRows) Scan added in v1.1.0

func (rows *StringRows) Scan(ent_ptr *StringRow) error

type StringTable

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

func (*StringTable) GetColCount

func (data *StringTable) GetColCount() int

func (*StringTable) GetColumns

func (data *StringTable) GetColumns() []string

func (*StringTable) GetRowCount

func (data *StringTable) GetRowCount() int

func (*StringTable) GetRowData

func (data *StringTable) GetRowData(r_no int) StringRow

type StructRows added in v1.1.0

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

func (*StructRows) Scan added in v1.1.0

func (rows *StructRows) Scan(ent_ptr interface{}) error

type TableName

type TableName interface {
	TableName() string
}

type ValueArray added in v1.1.2

type ValueArray []KeyVal

type ValueMap

type ValueMap map[string]interface{}

type VoLoader

type VoLoader interface {
	LoadFromModel(md *DbModel, mo interface{})
}

type VoSaver

type VoSaver interface {
	SaveToModel(md *DbModel, mo interface{})
}

Jump to

Keyboard shortcuts

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