orm

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 15 Imported by: 0

README

目录

orm

数据库及表信息

数据库:
host: 127.0.0.1
port: 3306
userName: root
password: 123456
database: dd

表结构:
CREATE TABLE `gateway` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '' COMMENT '名称',
  `path` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '' COMMENT '路径',
  `second_limit` int DEFAULT '5000' COMMENT '每秒请求数',
  PRIMARY KEY (`id`),
  UNIQUE KEY `path` (`path`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

CREATE TABLE `orm` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `lid` bigint(10) unsigned zerofill DEFAULT '0000000000' COMMENT '逻辑ID',
  `name` varchar(32) DEFAULT NULL COMMENT '名字',
  `pwd` char(32) DEFAULT NULL COMMENT '密码',
  `created_at` timestamp(6) NULL DEFAULT NULL COMMENT '创建时间',
  `updated_at` datetime DEFAULT NULL COMMENT '修改时间',
  `remark` text COMMENT '备注',
  `price` decimal(18,2) unsigned zerofill DEFAULT '0000000000000000.00' COMMENT '价格',
  `is_on` bit(19) DEFAULT b'1' COMMENT '是否启用',
  `logo` blob COMMENT '头像',
  `amount` float(12,4) unsigned DEFAULT '0.0000' COMMENT '总量',
  `dfd` bigint unsigned NOT NULL DEFAULT '3' COMMENT '余额',
  `sdf` set('1','2','3','4') NOT NULL DEFAULT '' COMMENT 'set测试',
  `em` enum('1','3','5','7') NOT NULL DEFAULT '1' COMMENT 'enum测试',
  `jn_` json NOT NULL COMMENT 'json测试',
  PRIMARY KEY (`id`),
  KEY `is_on` (`is_on`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

CREATE TABLE `user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `nickname` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '' COMMENT '昵称',
  `created_at` bigint unsigned DEFAULT NULL COMMENT '创建时间',
  `updated_at` bigint unsigned DEFAULT '0' COMMENT '更新时间',
  `is_on` tinyint unsigned DEFAULT '0' COMMENT '启用状态',
  PRIMARY KEY (`id`),
  KEY `created_at` (`created_at`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

显示所有数据库表

代码

package main

import (
	"github.com/grpc-boot/base"
	"github.com/grpc-boot/orm"
)

func main() {
	groupOption := &orm.GroupOption{
		RetryInterval: 60,
		Masters: []orm.PoolOption{
			{
				Dsn:             "root:123456@tcp(127.0.0.1:3306)/dd?timeout=5s&readTimeout=6s",
				MaxConnLifetime: 600,
				MaxOpenConns:    50,
				MaxIdleConns:    10,
			},
		},
	}

	group, err := orm.NewMysqlGroup(groupOption)

	if err != nil {
		base.RedFatal("instance mysql group err:%s", err.Error())
	}

	var tableList []string
	tableList, err = group.Tables("", false)
	if err != nil {
		base.RedFatal("query table err:%s", err.Error())
	}

	base.Green("%+v", tableList)

	var t *orm.Table
	for _, table := range tableList {
		t, err = group.Table(table, false)
		if err != nil {
			base.RedFatal("query table info err:%s", err.Error())
		}
		base.Green(t.ToStruct())
	}
}

输出

[gateway orm user]
type Gateway struct {
    Id uint64 `json:"id" borm:"id"`
    Name string `json:"name" borm:"name"`
    Path string `json:"path" borm:"path"`
    SecondLimit int64 `json:"second_limit" borm:"second_limit"`
}
type Orm struct {
    Id uint64 `json:"id" borm:"id"`
    Lid uint64 `json:"lid" borm:"lid"`
    Name string `json:"name" borm:"name"`
    Pwd string `json:"pwd" borm:"pwd"`
    CreatedAt string `json:"created_at" borm:"created_at"`
    UpdatedAt string `json:"updated_at" borm:"updated_at"`
    Remark string `json:"remark" borm:"remark"`
    Price float64 `json:"price" borm:"price"`
    IsOn int8 `json:"is_on" borm:"is_on"`
    Logo []byte `json:"logo" borm:"logo"`
    Amount float64 `json:"amount" borm:"amount"`
    Dfd uint64 `json:"dfd" borm:"dfd"`
    Sdf string `json:"sdf" borm:"sdf"`
    Em string `json:"em" borm:"em"`
    Jn string `json:"jn_" borm:"jn_"`
}
type User struct {
    Id uint64 `json:"id" borm:"id"`
    Nickname string `json:"nickname" borm:"nickname"`
    CreatedAt uint64 `json:"created_at" borm:"created_at"`
    UpdatedAt uint64 `json:"updated_at" borm:"updated_at"`
    IsOn uint8 `json:"is_on" borm:"is_on"`
}

insert语句

insert by orm.Row

代码:

// insert1 INSERT INTO `user`(`nickname`,`created_at`,`is_on`)VALUES(?, ?, ?)
func insert1() {
	res, err := group.Insert("`user`", orm.Row{
		"`nickname`": time.Now().String()[0:22],
		"`created_at`": time.Now().Unix(),
		"`is_on`": 1,
	})
	if err != nil {
		base.RedFatal("insert err:%s", err.Error())
	}

	id, _ := res.LastInsertId()

	base.Green("insert id:%d", id)
}

输出:

insert id:1
insert by object
type User struct {
	Id        int64  `borm:"id,primary"`
	NickName  string `borm:"nickname"`
	IsOn      uint8  `borm:"is_on,required"`
	CreatedAt int64  `borm:"created_at"`
	UpdatedAt int64  `borm:"updated_at"`
}

func (u *User) TableName() string {
	return `user`
}

// BeforeCreate insert时候执行
func (u *User) BeforeCreate() {
	u.CreatedAt = time.Now().Unix()
}

// BeforeSave insert与update均会执行
func (u *User) BeforeSave() {
	u.UpdatedAt = time.Now().Unix()
}

func insert2() {
	u := User{
		NickName:  fmt.Sprintf("orm_%s", time.Now().String()[0:22]),
		IsOn:      uint8(rand.Intn(1)),
	}

	res, err := group.InsertObj(&u)
	if err != nil {
		base.RedFatal("insert err:%s", err.Error())
	}

	id, _ := res.LastInsertId()

	base.Green("insert id:%d", id)
}

select语句

简单查询

代码

func select1() {
	query := orm.AcquireQuery4Mysql()
	defer query.Close()

	query.From("`user`").Limit(10)
	rows, err := group.Find(query, false)
	if err != nil {
		base.RedFatal("query err:%s", err.Error())
	}

	var (
		d, _ = jsoniter.Marshal(rows)
		args = []interface{}{}
	)

	base.Green("sql: %s \nselect 1: %s", query.Sql(&args), d)
}

输出

sql: SELECT * FROM `user` LIMIT 0,10 
select 1: [{"is_on":"0","id":"1","nickname":"m_2022-01-08 22:22:29","created_at":"1641651749","updated_at":"0"},{"id":"3","nickname":"2022-01-09 17:05:14.07","created_at":"1641719114","updated_at":"0","is_on":"1"},{"id":"4","nickname":"2022-01-09 17:05:21.03","created_at":"1641719121","updated_at":"0","is_on":"1"},{"is_on":"1","id":"5","nickname":"2022-01-09 17:05:24.97","created_at":"1641719124","updated_at":"0"},{"created_at":"1641719144","updated_at":"0","is_on":"1","id":"6","nickname":"2022-01-09 17:05:44.13"},{"created_at":"1641719449","updated_at":"0","is_on":"1","id":"7","nickname":"2022-01-09 17:10:49.81"},{"id":"8","nickname":"2022-01-09 17:11:20.00","created_at":"1641719480","updated_at":"0","is_on":"1"},{"updated_at":"0","is_on":"1","id":"9","nickname":"2022-01-09 17:11:45.41","created_at":"1641719505"},{"created_at":"1641719578","updated_at":"0","is_on":"1","id":"10","nickname":"2022-01-09 17:12:58.31"},{"id":"11","nickname":"2022-01-09 17:24:21.37","created_at":"1641720261","updated_at":"0","is_on":"1"}]
带Where条件

代码

func select2() {
	query := orm.AcquireQuery4Mysql()
	defer query.Close()

	query.Select("`id`", "`nickname`", "`is_on`").
		From("`user`").
		Where(orm.AndWhere(orm.FieldMap{
			"`is_on`": {1},
		})).
		Order("`id` DESC", "`created_at` DESC").
		Limit(10)

	rows, err := group.Find(query, true)
	if err != nil {
		base.RedFatal("query err:%s", err.Error())
	}

	var (
		d, _ = jsoniter.Marshal(rows)
		args = []interface{}{}
	)

	base.Green("sql: %s \nselect 2: %s", query.Sql(&args), d)
}

输出

sql: SELECT `id`,`nickname`,`is_on` FROM `user` WHERE (`is_on` = ?) ORDER BY `id` DESC,`created_at` DESC LIMIT 0,10 
select 2: [{"is_on":"1","id":"15","nickname":"2022-01-09 17:27:28.79"},{"id":"14","nickname":"2022-01-09 17:26:23.29","is_on":"1"},{"id":"13","nickname":"2022-01-09 17:26:12.72","is_on":"1"},{"is_on":"1","id":"12","nickname":"2022-01-09 17:24:42.33"},{"id":"11","nickname":"2022-01-09 17:24:21.37","is_on":"1"},{"id":"10","nickname":"2022-01-09 17:12:58.31","is_on":"1"},{"id":"9","nickname":"2022-01-09 17:11:45.41","is_on":"1"},{"id":"8","nickname":"2022-01-09 17:11:20.00","is_on":"1"},{"id":"7","nickname":"2022-01-09 17:10:49.81","is_on":"1"},{"nickname":"2022-01-09 17:05:44.13","is_on":"1","id":"6"}]
复杂Where条件

代码

func select3() {
	query := orm.AcquireQuery4Mysql()
	defer query.Close()

	query.Select("`id`", "`nickname`", "`is_on`").
		From("`user`").
		Where(orm.AndWhere(orm.FieldMap{
			"`is_on`": {1},
		})).
		Or(orm.AndCondition(orm.FieldMap{
			"`id`":{"IN", 1, 2, 3, 4, 5, 6},
			"`nickname`":{"LIKE", "2022%"},
	    })).
		And(orm.OrCondition(orm.FieldMap{
			"created_at":{"<=", time.Now().Unix()},
			"updated_at":{0},
		})).
		Order("`id` DESC", "`created_at` DESC").
		Limit(10)

	rows, err := group.Find(query, true)
	if err != nil {
		base.RedFatal("query err:%s", err.Error())
	}

	var (
		d, _ = jsoniter.Marshal(rows)
		args = []interface{}{}
	)

	base.Green("sql: %s \nselect 3: %s", query.Sql(&args), d)
}

输出

sql: SELECT `id`,`nickname`,`is_on` FROM `user` WHERE (`is_on` = ?) OR (`id` IN(?,?,?,?,?,?) AND `nickname` LIKE ?) AND (created_at <= ? OR updated_at = ?) ORDER BY `id` DESC,`created_at` DESC LIMIT 0,10 
select 3: [{"id":"15","nickname":"2022-01-09 17:27:28.79","is_on":"1"},{"id":"14","nickname":"2022-01-09 17:26:23.29","is_on":"1"},{"is_on":"1","id":"13","nickname":"2022-01-09 17:26:12.72"},{"id":"12","nickname":"2022-01-09 17:24:42.33","is_on":"1"},{"nickname":"2022-01-09 17:24:21.37","is_on":"1","id":"11"},{"id":"10","nickname":"2022-01-09 17:12:58.31","is_on":"1"},{"id":"9","nickname":"2022-01-09 17:11:45.41","is_on":"1"},{"nickname":"2022-01-09 17:11:20.00","is_on":"1","id":"8"},{"id":"7","nickname":"2022-01-09 17:10:49.81","is_on":"1"},{"id":"6","nickname":"2022-01-09 17:05:44.13","is_on":"1"}]

Documentation

Index

Constants

View Source
const (
	Or  = `OR`
	And = `AND`
)
View Source
const (
	Bit        = "bit"
	TinyInt    = "tinyint"
	SmallInt   = "smallint"
	MediumInt  = "mediumint"
	Int        = "int"
	BigInt     = "bigint"
	Float      = "float"
	Double     = "double"
	Decimal    = "decimal"
	TinyText   = "tinytext"
	MediumText = "mediumtext"
	Text       = "text"
	LongText   = "longtext"
	Json       = "json"
	Varchar    = "varchar"
	Char       = "char"
	Timestamp  = "timestamp"
	Date       = "date"
	Datetime   = "datetime"
	TinyBlob   = "tinyblob"
	MediumBlob = "mediumblob"
	Blob       = "blob"
	LongBlob   = "longblob"
	Set        = "set"
	Enum       = "enum"
)

Variables

View Source
var (
	ErrInvalidRowsTypes     = errors.New(`only *struct or []*struct types are supported`)
	ErrNotFoundField        = errors.New(`failed to match the field from the struct to the database. Please configure the borm tag correctly`)
	ErrNotFoundPrimaryField = errors.New(`failed to found primary field. Please configure the primary on borm tag correctly`)
	ErrInvalidTypes         = errors.New(`only *struct types are supported`)
	ErrInvalidFieldTypes    = errors.New(`only bool(1 is true, other is false),string、float64、float32、int、uint、int8、uint8、int16、uint16、int32、uint32、int64 and uint64 types are supported`)
)
View Source
var (
	ErrNoMasterConn = errors.New("mysql group: no master connection available")
	ErrNoSlaveConn  = errors.New("mysql group: no slave connection available")
)

Functions

func FormatRows

func FormatRows(rows *sql.Rows, handler RowFormat)

FormatRows 格式化数据库行

func SqlDelete

func SqlDelete(args *[]interface{}, table string, where Where) (sql string)

SqlDelete 生成删除sql

func SqlDeleteByObj

func SqlDeleteByObj(args *[]interface{}, obj interface{}) (sqlStr string, err error)

SqlDeleteByObj ---

func SqlFindOneObj

func SqlFindOneObj(args *[]interface{}, where Where, obj interface{}) (sql string, err error)

SqlFindOneObj ---

func SqlInsert

func SqlInsert(args *[]interface{}, table string, rows ...Row) (sql string)

SqlInsert 生成插入sql

func SqlInsertObjs

func SqlInsertObjs(args *[]interface{}, rows interface{}) (sql string, err error)

SqlInsertObjs ---

func SqlUpdate

func SqlUpdate(args *[]interface{}, table string, set Row, where Where) (sql string)

SqlUpdate 生成更新sql

func SqlUpdateByObj

func SqlUpdateByObj(args *[]interface{}, obj interface{}) (sqlStr string, err error)

SqlUpdateByObj ---

func ToMap

func ToMap(rows *sql.Rows) ([]map[string]string, error)

ToMap 格式化数据库行为[]map[string]string

func ToObj

func ToObj(rows *sql.Rows, obj interface{}) error

ToObj 格式化数据库行到obj

func ToObjList

func ToObjList(rows *sql.Rows, obj interface{}) ([]interface{}, error)

ToObjList 格式化数据库行为[]interface{}

Types

type Column added in v1.1.0

type Column struct {
	Field      string `json:"field"`
	Type       string `json:"type"`
	Length     int    `json:"length"`
	Point      int    `json:"point"`
	Unsigned   bool   `json:"unsigned"`
	Collation  string `json:"collation"`
	Null       bool   `json:"null"`
	Key        string `json:"key"`
	Default    string `json:"default"`
	Extra      string `json:"extra"`
	Privileges string `json:"privileges"`
	Comment    string `json:"comment"`
}

func (*Column) GoType added in v1.1.0

func (c *Column) GoType() string

GoType 转换为go类型

func (*Column) ToProperty added in v1.1.0

func (c *Column) ToProperty() string

type Condition

type Condition interface {
	Opt() (opt string)
	Sql(args *[]interface{}) (sql string)
}

Condition 条件

func AndCondition

func AndCondition(fields FieldMap) Condition

AndCondition And条件

func OrCondition

func OrCondition(fields FieldMap) Condition

OrCondition Or条件

type FieldMap added in v1.1.0

type FieldMap map[string][]interface{}

FieldMap 列条件

type Group

type Group interface {
	// BadPool 获取BadPool列表
	BadPool(isMaster bool) (list []int)
	// Query 查询
	Query(useMaster bool, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	// QueryContext with context 查询
	QueryContext(ctx context.Context, useMaster bool, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	// Exec 执行
	Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
	// ExecContext with context 执行
	ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)

	// Tables 获取表列表
	Tables(pattern string, useMaster bool) (tableList []string, err error)
	Table(table string, useMaster bool) (t *Table, err error)

	// InsertObj 插入对象
	InsertObj(obj interface{}) (result sql.Result, err error)
	// InsertObjContext with context 插入对象
	InsertObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	// DeleteObj 删除对象
	DeleteObj(obj interface{}) (result sql.Result, err error)
	// DeleteObjContext with context 删除对象
	DeleteObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	// UpdateObj 更新对象
	UpdateObj(obj interface{}) (result sql.Result, err error)
	// UpdateObjContext with context 更新对象
	UpdateObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	// Find 根据Query查询
	Find(query Query, useMaster bool) (rows []map[string]string, err error)
	// FindContext with context 根据Query查询
	FindContext(ctx context.Context, query Query, useMaster bool) (rows []map[string]string, err error)
	// FindAll 根据Query查询,返回对象列表
	FindAll(query Query, obj interface{}, useMaster bool) (objList []interface{}, err error)
	// FindAllContext with context 根据Query查询,返回对象列表
	FindAllContext(ctx context.Context, query Query, obj interface{}, useMaster bool) (objList []interface{}, err error)
	// FindOne 查询一个
	FindOne(table string, where Where, useMaster bool) (row map[string]string, err error)
	// FindOneContext with context  查询一个
	FindOneContext(ctx context.Context, table string, where Where, useMaster bool) (row map[string]string, err error)
	// FindOneObj 查询一个对象
	FindOneObj(where Where, obj interface{}, useMaster bool) (err error)
	// FindOneObjContext with context  查询一个对象
	FindOneObjContext(ctx context.Context, where Where, obj interface{}, useMaster bool) (err error)
	// Insert 插入
	Insert(table string, rows ...Row) (result sql.Result, err error)
	// InsertContext with context 插入
	InsertContext(ctx context.Context, table string, rows ...Row) (result sql.Result, err error)
	// DeleteAll 删除
	DeleteAll(table string, where Where) (result sql.Result, err error)
	// DeleteAllContext with context 删除
	DeleteAllContext(ctx context.Context, table string, where Where) (result sql.Result, err error)
	// UpdateAll 更新
	UpdateAll(table string, set Row, where Where) (result sql.Result, err error)
	// UpdateAllContext with context 更新
	UpdateAllContext(ctx context.Context, table string, set Row, where Where) (result sql.Result, err error)

	// Begin 开启事务
	Begin() (Transaction, error)
	// BeginTx with context 开启事务
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
}

func NewMysqlGroup

func NewMysqlGroup(groupOption *GroupOption) (Group, error)

type GroupOption

type GroupOption struct {
	Masters []PoolOption `yaml:"masters" json:"masters"`
	Slaves  []PoolOption `yaml:"slaves" json:"slaves"`
	//单位s
	RetryInterval int64 `yaml:"retryInterval" json:"retryInterval"`
}

type Pool

type Pool interface {
	// Query 查询
	Query(sqlStr string, args ...interface{}) (rows *sql.Rows, err error)
	// QueryContext with context 查询
	QueryContext(ctx context.Context, sqlStr string, args ...interface{}) (rows *sql.Rows, err error)
	// Exec 执行
	Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
	// ExecContext with context 执行
	ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
	// Begin 开启事务
	Begin() (Transaction, error)
	// BeginTx with context 开启事务
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
}

type PoolOption

type PoolOption struct {
	//格式:"userName:password@schema(host:port)/dbName",如:root:123456@tcp(127.0.0.1:3306)/test
	Dsn string `yaml:"dsn" json:"dsn"`
	//单位s
	MaxConnLifetime int `yaml:"maxConnLifetime" json:"maxConnLifetime"`
	MaxOpenConns    int `yaml:"maxOpenConns" json:"maxOpenConns"`
	MaxIdleConns    int `yaml:"maxIdleConns" json:"maxIdleConns"`
}

type Query

type Query interface {
	// Select Select表达式
	Select(columns ...string) Query
	// From From表达式
	From(table string) Query
	// Where Where表达式
	Where(where Where) Query
	// And 附加And where
	And(condition Condition) Query
	// Or 附加Or where
	Or(condition Condition) Query
	// Group Group表达式
	Group(fields ...string) Query
	// Having Having表达式
	Having(having string) Query
	// Order Order表达式
	Order(orders ...string) Query
	// Offset Offset表达式
	Offset(offset int64) Query
	// Limit Limit表达式
	Limit(limit int64) Query
	// Sql 生成sql
	Sql(arguments *[]interface{}) (sql string)
	// Close 释放Query
	Close()
}

Query Query对象

func AcquireQuery4Mysql added in v1.1.0

func AcquireQuery4Mysql() Query

AcquireQuery4Mysql 获取mysqlQuery对象

type Row added in v1.1.0

type Row map[string]interface{}

Row 行

type RowFormat

type RowFormat func(fieldValue map[string][]byte)

RowFormat 格式化数据库行函数

type Table added in v1.1.0

type Table struct {
	Name    string   `json:"name"`
	Columns []Column `json:"columns"`
}

func (*Table) ToStruct added in v1.1.0

func (t *Table) ToStruct() string

ToStruct 转换为go结构体字符串

type Transaction added in v1.0.1

type Transaction interface {
	// Commit 提交事务
	Commit() (err error)
	// Rollback 回滚事务
	Rollback() (err error)
	// Query 查询
	Query(sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	// QueryContext with context 查询
	QueryContext(ctx context.Context, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
	// Exec 执行
	Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
	// ExecContext with context 执行
	ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
	// InsertObj 插入对象
	InsertObj(obj interface{}) (result sql.Result, err error)
	// InsertObjContext with context 插入对象
	InsertObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	// DeleteObj 删除对象
	DeleteObj(obj interface{}) (result sql.Result, err error)
	// DeleteObjContext with context 删除对象
	DeleteObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	// UpdateObj 更新对象
	UpdateObj(obj interface{}) (result sql.Result, err error)
	// UpdateObjContext with context 更新对象
	UpdateObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
	// Find 根据Query查询
	Find(query Query) (rows []map[string]string, err error)
	// FindContext with context 根据Query查询
	FindContext(ctx context.Context, query Query) (rows []map[string]string, err error)
	// FindAll 根据Query查询,返回对象列表
	FindAll(query Query, obj interface{}) (objList []interface{}, err error)
	// FindAllContext with context 根据Query查询,返回对象列表
	FindAllContext(ctx context.Context, query Query, obj interface{}) (objList []interface{}, err error)
	// FindOne 查询一个
	FindOne(table string, where Where) (row map[string]string, err error)
	// FindOneContext with context  查询一个
	FindOneContext(ctx context.Context, table string, where Where) (row map[string]string, err error)
	// FindOneObj 查询一个对象
	FindOneObj(where Where, obj interface{}) (err error)
	// FindOneObjContext with context  查询一个对象
	FindOneObjContext(ctx context.Context, where Where, obj interface{}) (err error)
	// Insert 插入
	Insert(table string, rows ...Row) (result sql.Result, err error)
	// InsertContext with context 插入
	InsertContext(ctx context.Context, table string, rows ...Row) (result sql.Result, err error)
	// DeleteAll 删除
	DeleteAll(table string, where Where) (result sql.Result, err error)
	// DeleteAllContext with context 删除
	DeleteAllContext(ctx context.Context, table string, where Where) (result sql.Result, err error)
	// UpdateAll 更新
	UpdateAll(table string, set Row, where Where) (result sql.Result, err error)
	// UpdateAllContext with context  更新
	UpdateAllContext(ctx context.Context, table string, set Row, where Where) (result sql.Result, err error)
}

type Where

type Where interface {
	HasWhere() bool
	And(condition Condition) Where
	Or(condition Condition) Where
	Sql(args *[]interface{}) (sql string)
	Reset()
}

Where Where对象

func AndWhere

func AndWhere(fm FieldMap) Where

AndWhere 实例化And条件Where

func NewWhere added in v1.1.0

func NewWhere(condition Condition) Where

NewWhere 实例化Where

func OrWhere

func OrWhere(fm FieldMap) Where

OrWhere 实例化Or条件Where

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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