sqltemplate

package
v0.0.0-...-84fae08 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

* SQL生成模板 用于根据struct的定义生成update语句和insert语句 规则

  1、表名
   默认t_stuct的名称(小写,驼峰转 xx_xx)
   可以通过setTablePreFix来指定默认的表前缀
   可以通过tag Table:""指定表名

  2、字段名称
    默认字段名称(小写,驼峰转 xx_xx)
	可以通过 tag Field:""指定字段名

  3、特例
    通过tag Option:"" 来指定。
	可选值:auto、pk、not 分别表示 自动增长、主健、忽略

Index

Constants

View Source
const Max_RETRY = 10

最大重试次数

Variables

This section is empty.

Functions

func GetColName

func GetColName(field reflect.StructField) string

Types

type BaseDao

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

基础数据操作对象

func (*BaseDao) Delete

func (this *BaseDao) Delete(obj utils.Object, cols ...string) (int64, error)

func (*BaseDao) Exec

func (this *BaseDao) Exec(sqltemplate string, objs ...interface{}) (int64, error)

执行单条sql

func (*BaseDao) ExecSqlBatch

func (this *BaseDao) ExecSqlBatch(sqls ...string) error

批量执行简单的sql语句

func (*BaseDao) Find

func (this *BaseDao) Find(obj utils.Object, cols ...string) bool

func (*BaseDao) FindBySql

func (this *BaseDao) FindBySql(obj utils.Object, sqlTemplate string, args ...interface{}) bool

func (*BaseDao) GetSession

func (this *BaseDao) GetSession() *Connection

func (*BaseDao) Insert

func (this *BaseDao) Insert(obj utils.Object) (int64, error)

插入,返回auto id和错误信息

func (*BaseDao) InsertAndUpdate

func (this *BaseDao) InsertAndUpdate(iobj interface{}, sqltemplate string, args ...interface{}) error

插入对象,并根据对象的id,更新后续的sql语句,一般为update,其中这个关联id必须是第一个参数

func (*BaseDao) InsertBatch

func (this *BaseDao) InsertBatch(objs []utils.Object) (ids []int64, e error)

插入,返回auto id和错误信息

func (*BaseDao) Query

func (this *BaseDao) Query(obj utils.Object, page Page, cols ...string) []interface{}

func (*BaseDao) QueryAll

func (this *BaseDao) QueryAll(obj utils.Object, cols ...string) []interface{}

func (*BaseDao) QueryAllBySql

func (this *BaseDao) QueryAllBySql(obj utils.Object, sqlTemplate string, args ...interface{}) []interface{}

func (*BaseDao) SetDs

func (this *BaseDao) SetDs(c *DataSource)

func (*BaseDao) Update

func (this *BaseDao) Update(obj utils.Object, cols ...string) (int64, error)

更新,返回更新的条数和错误信息

func (*BaseDao) UpdateBatch

func (this *BaseDao) UpdateBatch(objs []utils.Object, cols ...string) (counts []int64, e error)

type Connection

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

*

数据库 连接包装

func (*Connection) Begin

func (this *Connection) Begin()

func (*Connection) Close

func (this *Connection) Close()

func (*Connection) Commit

func (this *Connection) Commit()

func (*Connection) Delete

func (this *Connection) Delete(obj interface{}, col ...string) (id int64, affect int64, err error)

func (*Connection) ExeSql

func (this *Connection) ExeSql(sql string, objs ...interface{}) (id int64, affect int64, err error)

func (*Connection) Find

func (this *Connection) Find(obj interface{}, col ...string) bool

func (*Connection) Insert

func (this *Connection) Insert(obj interface{}) (id int64, affect int64, err error)

func (*Connection) Query

func (this *Connection) Query(result interface{}, sql string, objs ...interface{}) bool

func (*Connection) QueryByPage

func (this *Connection) QueryByPage(result interface{}, page Page, sql string, objs ...interface{}) []interface{}

func (*Connection) Rollback

func (this *Connection) Rollback()

func (*Connection) SimpleQuery

func (this *Connection) SimpleQuery(sql string, obj ...interface{}) bool

func (*Connection) Update

func (this *Connection) Update(obj interface{}, col ...string) (id int64, affect int64, err error)

type DataSource

type DataSource struct {
	DBtype     string
	DBurl      string
	DBuser     string
	DBpassword string
	DBname     string
	DBmapper   string //mapper文件夹
	// contains filtered or unexported fields
}

*

data source

func (*DataSource) CanAssignableTo

func (this *DataSource) CanAssignableTo(t reflect.Type) bool

func (*DataSource) Factory

func (this *DataSource) Factory(config string) interface{}

func (*DataSource) GetConnection

func (this *DataSource) GetConnection() *Connection

获取连接

func (*DataSource) GetDao

func (this *DataSource) GetDao() *BaseDao

func (*DataSource) GetMapperDao

func (this *DataSource) GetMapperDao(namespace string) *MapperDao

func (*DataSource) Init

func (this *DataSource) Init()

type MapperDao

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

func (*MapperDao) BeginTransaction

func (this *MapperDao) BeginTransaction()

func (*MapperDao) Count

func (this *MapperDao) Count(obj utils.Object, id string) int64

func (*MapperDao) CountByObj

func (this *MapperDao) CountByObj(obj utils.Object, col ...string) int64

func (*MapperDao) Delete

func (this *MapperDao) Delete(obj utils.Object, id string) (int64, error)

func (*MapperDao) DeleteByObj

func (this *MapperDao) DeleteByObj(obj utils.Object, col ...string) (int64, error)

func (*MapperDao) Exist

func (this *MapperDao) Exist(obj utils.Object, id string) bool

func (*MapperDao) ExistByObj

func (this *MapperDao) ExistByObj(obj utils.Object, col ...string) bool

func (*MapperDao) Find

func (this *MapperDao) Find(obj utils.Object, id string) bool

func (*MapperDao) FindByObj

func (this *MapperDao) FindByObj(obj utils.Object, col ...string) bool

func (*MapperDao) FinishTransaction

func (this *MapperDao) FinishTransaction()

func (*MapperDao) Insert

func (this *MapperDao) Insert(obj utils.Object, id string) (int64, error)

func (*MapperDao) InsertAuto

func (this *MapperDao) InsertAuto(obj utils.Object) (int64, error)

一般的insert只有一条,自动选择一条,如果不存在insert,将使用template来完成插入

func (*MapperDao) InsertByObj

func (this *MapperDao) InsertByObj(obj utils.Object) (int64, error)

func (*MapperDao) InsertOrUpdateByExist

func (this *MapperDao) InsertOrUpdateByExist(obj utils.Object, exitsCols []string, updateCols []string) (int64, error)

插入或更新,当存在数据的时候更新,不存在则插入

func (*MapperDao) Query

func (this *MapperDao) Query(obj utils.Object, page Page, id string) []interface{}

func (*MapperDao) QueryAll

func (this *MapperDao) QueryAll(obj utils.Object, id string) []interface{}

func (*MapperDao) QueryAllByObj

func (this *MapperDao) QueryAllByObj(obj utils.Object, col ...string) []interface{}

func (*MapperDao) QueryByObj

func (this *MapperDao) QueryByObj(obj utils.Object, page Page, col ...string) []interface{}

func (*MapperDao) Update

func (this *MapperDao) Update(obj utils.Object, id string) (int64, error)

func (*MapperDao) UpdateByObj

func (this *MapperDao) UpdateByObj(obj utils.Object, col ...string) (int64, error)

type MapperFile

type MapperFile struct {
	Name  string `yaml:"namespace"`
	Nodes []MapperNode
}

type MapperNode

type MapperNode struct {
	Code     string
	Type     methodType
	Template string
}

func (*MapperNode) ToFunction

func (this *MapperNode) ToFunction(namespace string) mapperfunction

type Page

type Page struct {
	Size  int
	Index int
	Count int
}

*

分页

type SqlTemplate

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

func (*SqlTemplate) CreateDeleteSql

func (this *SqlTemplate) CreateDeleteSql(target interface{}, col ...string) (string, []interface{}, error)

func (*SqlTemplate) CreateFromWhereSql

func (this *SqlTemplate) CreateFromWhereSql(target interface{}, col ...string) (string, []interface{}, error)

func (*SqlTemplate) CreateInserSql

func (this *SqlTemplate) CreateInserSql(target interface{}) (string, []interface{}, error)

func (*SqlTemplate) CreateQuerySql

func (this *SqlTemplate) CreateQuerySql(target interface{}, col ...string) (string, []interface{}, error)

func (*SqlTemplate) CreateUpdateSql

func (this *SqlTemplate) CreateUpdateSql(target interface{}, col ...string) (string, []interface{}, error)

*

update 指定的是更新字段
条件按主键,所以一定需要定义主键字段
对于批量更新会做新的方法来完成

func (*SqlTemplate) GetInsertSql

func (this *SqlTemplate) GetInsertSql(target interface{}) (string, []interface{}, error)

func (*SqlTemplate) SetTablePrefix

func (this *SqlTemplate) SetTablePrefix(prefix string)

func (*SqlTemplate) StructValueToCustomArray

func (this *SqlTemplate) StructValueToCustomArray(target interface{}, col ...string) ([]interface{}, error)

type SqltemplateCollect

type SqltemplateCollect map[string]mapperfunction

type SqltemplateManager

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

func (*SqltemplateManager) AddCollectFromFile

func (this *SqltemplateManager) AddCollectFromFile(f string)

func (*SqltemplateManager) BuildDao

func (this *SqltemplateManager) BuildDao(ds *DataSource, namespace string) *MapperDao

Jump to

Keyboard shortcuts

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