torm

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: Apache-2.0 Imports: 14 Imported by: 0

README

简介


torm是基于xorm的mysql访问组件,在xorm基础上提供代码生成、索引查询方法、初始化管理等增强功能

特点

  • 支持原生xorm操作
  • 简单易使用
  • 自动生成表映射文件
  • 默认提供所有按索引查询方法,包括count、limit等

文档

[开发文档]

Documentation

Overview

=============================================================== * Copyright (C) 2020 All rights reserved. * * FileName:hook.go * Author:WuGuoFu * Date: 2020-06-17 * Description: * ================================================================

Index

Constants

This section is empty.

Variables

View Source
var (
	HookExec   func(func() (sql.Result, error)) (sql.Result, error)
	HookGet    func(func() (bool, error)) (bool, error)
	HookFind   func(func() error) error
	HookInsert func(func() (int64, error)) (int64, error)
	HookDelete func(func() (int64, error)) (int64, error)
	HookQuery  func(func() ([]map[string][]byte, error)) ([]map[string][]byte, error)
	HookUpdate func(func() (int64, error)) (int64, error)
)

Functions

func AddMysql

func AddMysql(options []MysqlOption)

add mysql without config file.

Types

type DBDao

type DBDao struct {
	Engine *Engine
	// contains filtered or unexported fields
}

func GetDbInstance

func GetDbInstance(db, cluster string) *DBDao

GetDbInstance returns DBDao instance.

func GetDbInstanceWithCtx

func GetDbInstanceWithCtx(ctx context.Context, db, cluster string) *DBDao

GetDbInstanceWithCtx returns DBDao instance. If it's a stress test scenario,return benchmark_dbName.

func GetDefault

func GetDefault(cluster string) *DBDao

func (*DBDao) Close

func (this *DBDao) Close()

Close the engine

func (*DBDao) GetSession

func (this *DBDao) GetSession() *Session

GetSession returns a new Session

type DbBaseDao

type DbBaseDao struct {
	Engine  *Engine
	Session *Session
}

func (*DbBaseDao) BuildQuery

func (this *DbBaseDao) BuildQuery(input Param, name string)

Construct sql query statement and execute.

func (*DbBaseDao) Count

func (this *DbBaseDao) Count(bean ...interface{}) (int64, error)

Count counts the records. bean's non-empty fields are conditions.

func (*DbBaseDao) Create

func (this *DbBaseDao) Create(bean interface{}) (int64, error)

Insert one record.

func (*DbBaseDao) Delete

func (this *DbBaseDao) Delete(bean interface{}) (int64, error)

Delete records, bean's non-empty fields are conditions, Delete with ID primary key.

func (*DbBaseDao) Exec

func (this *DbBaseDao) Exec(sqlOrArgs ...interface{}) (sql.Result, error)

Exec raw sql

func (*DbBaseDao) Exist

func (this *DbBaseDao) Exist(bean ...interface{}) (bool, error)

Exist returns true if the record exist otherwise return false

func (*DbBaseDao) Find

func (this *DbBaseDao) Find(beans interface{}, condiBeans ...interface{}) error

Find retrieve records from table, condiBeans's non-empty fields are conditions. beans could be []Struct, []*Struct, map[int64]Struct map[int64]*Struct

func (*DbBaseDao) FindAndCount

func (this *DbBaseDao) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error)

FindAndCount find the results and also return the counts

func (*DbBaseDao) Get

func (this *DbBaseDao) Get(bean interface{}) (bool, error)

Get retrieve one record from table, bean's non-empty fields are conditions

func (*DbBaseDao) InitSession

func (this *DbBaseDao) InitSession()

Mysql instance set session.

func (*DbBaseDao) Query

func (this *DbBaseDao) Query(sqlorArgs ...interface{}) (resultsSlice []map[string][]byte, err error)

Query a raw sql and return records as []map[string][]byte

func (*DbBaseDao) QueryInterface

func (this *DbBaseDao) QueryInterface(sqlorArgs ...interface{}) ([]map[string]interface{}, error)

QueryInterface runs a raw sql and return records as []map[string]interface{}

func (*DbBaseDao) QueryString

func (this *DbBaseDao) QueryString(sqlorArgs ...interface{}) ([]map[string]string, error)

QueryString runs a raw sql and return records as []map[string]string

func (*DbBaseDao) SetTable

func (this *DbBaseDao) SetTable(tableName string)

SetTable can specify a table name.

func (*DbBaseDao) Sum

func (this *DbBaseDao) Sum(bean interface{}, colName string) (float64, error)

Sum sum the records by some column. bean's non-empty fields are conditions.

func (*DbBaseDao) SumInt

func (this *DbBaseDao) SumInt(bean interface{}, colName string) (int64, error)

SumInt sum the records by some column. bean's non-empty fields are conditions.

func (*DbBaseDao) Sums

func (this *DbBaseDao) Sums(bean interface{}, colNames ...string) ([]float64, error)

Sums sum the records by some columns. bean's non-empty fields are conditions.

func (*DbBaseDao) SumsInt

func (this *DbBaseDao) SumsInt(bean interface{}, colNames ...string) ([]int64, error)

SumsInt like Sums but return slice of int64 instead of float64.

func (*DbBaseDao) Update

func (this *DbBaseDao) Update(bean interface{}) (int64, error)

Update records, bean's non-empty fields are updated contents, Update with ID primary key.

func (*DbBaseDao) UpdateCols

func (this *DbBaseDao) UpdateCols(bean interface{}, cols ...string) (int64, error)

Update special specified fields, Update with ID primary key.

func (*DbBaseDao) UpdateEngine

func (this *DbBaseDao) UpdateEngine(v ...interface{})

Update MySQL execution engine.

type Engine

type Engine struct {
	*xorm.Engine
}

func (*Engine) After

func (engine *Engine) After(closures func(interface{})) *Session

After apply after insert Processor, affected bean is passed to closure arg

func (*Engine) Alias

func (engine *Engine) Alias(alias string) *Session

Alias set the table alias

func (*Engine) AllCols

func (engine *Engine) AllCols() *Session

AllCols indicates that all columns should be use

func (*Engine) Asc

func (engine *Engine) Asc(colNames ...string) *Session

Asc will generate "ORDER BY column1,column2 Asc" This method can chainable use.

engine.Desc("name").Asc("age").Find(&users)
// SELECT * FROM user ORDER BY name DESC, age ASC

func (*Engine) Before

func (engine *Engine) Before(closures func(interface{})) *Session

Before apply before Processor, affected bean is passed to closure arg

func (*Engine) Cascade

func (engine *Engine) Cascade(trueOrFalse ...bool) *Session

Cascade use cascade or not

func (*Engine) Charset

func (engine *Engine) Charset(charset string) *Session

Charset set charset when create table, only support mysql now

func (*Engine) Cols

func (engine *Engine) Cols(columns ...string) *Session

Cols only use the parameters as select or update columns

func (*Engine) Context

func (engine *Engine) Context(ctx context.Context) *Session

Context creates a session with the context

func (*Engine) Decr

func (engine *Engine) Decr(column string, arg ...interface{}) *Session

Decr provides a update string like "column = column - ?"

func (*Engine) Desc

func (engine *Engine) Desc(colNames ...string) *Session

Desc will generate "ORDER BY column1 DESC, column2 DESC"

func (*Engine) Distinct

func (engine *Engine) Distinct(columns ...string) *Session

Distinct use for distinct columns. Caution: when you are using cache, distinct will not be cached because cache system need id, but distinct will not provide id

func (*Engine) GroupBy

func (engine *Engine) GroupBy(keys string) *Session

GroupBy generate group by statement

func (*Engine) Having

func (engine *Engine) Having(conditions string) *Session

Having generate having statement

func (*Engine) ID

func (engine *Engine) ID(id interface{}) *Session

ID method provoide a condition as (id) = ?

func (*Engine) Id

func (engine *Engine) Id(id interface{}) *Session

Id will be deprecated, please use ID instead

func (*Engine) In

func (engine *Engine) In(column string, args ...interface{}) *Session

In will generate "column IN (?, ?)"

func (*Engine) Incr

func (engine *Engine) Incr(column string, arg ...interface{}) *Session

Incr provides a update string like "column = column + ?"

func (*Engine) Join

func (engine *Engine) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session

Join the join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN

func (*Engine) Limit

func (engine *Engine) Limit(limit int, start ...int) *Session

Limit will generate "LIMIT start, limit"

func (*Engine) MustCols

func (engine *Engine) MustCols(columns ...string) *Session

MustCols specify some columns must use even if they are empty

func (*Engine) NewSession

func (engine *Engine) NewSession() *Session

NewSession New a session

func (*Engine) NoAutoCondition

func (engine *Engine) NoAutoCondition(no ...bool) *Session

NoAutoCondition disable auto generate Where condition from bean or not

func (*Engine) NoAutoTime

func (engine *Engine) NoAutoTime() *Session

NoAutoTime Default if your struct has "created" or "updated" filed tag, the fields will automatically be filled with current time when Insert or Update invoked. Call NoAutoTime if you dont' want to fill automatically.

func (*Engine) NoCache

func (engine *Engine) NoCache() *Session

func (*Engine) NoCascade

func (engine *Engine) NoCascade() *Session

NoCascade If you do not want to auto cascade load object

func (*Engine) NotIn

func (engine *Engine) NotIn(column string, args ...interface{}) *Session

NotIn will generate "column NOT IN (?, ?)"

func (*Engine) Nullable

func (engine *Engine) Nullable(columns ...string) *Session

Nullable set null when column is zero-value and nullable for update

func (*Engine) Omit

func (engine *Engine) Omit(columns ...string) *Session

Omit only not use the parameters as select or update columns

func (*Engine) OrderBy

func (engine *Engine) OrderBy(order string) *Session

OrderBy will generate "ORDER BY order"

func (*Engine) PingContext

func (engine *Engine) PingContext(ctx context.Context) error

PingContext tests if database is alive

func (*Engine) Prepare

func (engine *Engine) Prepare() *Session

Prepare enables prepare statement

func (*Engine) SQL

func (engine *Engine) SQL(query interface{}, args ...interface{}) *Session

SQL method let's you manually write raw SQL and operate For example:

engine.SQL("select * from user").Find(&users)

This code will execute "select * from user" and set the records to users

func (*Engine) Select

func (engine *Engine) Select(str string) *Session

Select customerize your select columns or contents

func (*Engine) SetDefaultContext

func (engine *Engine) SetDefaultContext(ctx context.Context)

SetDefaultContext set the default context

func (*Engine) SetExpr

func (engine *Engine) SetExpr(column string, expression string) *Session

SetExpr provides a update string like "column = {expression}"

func (*Engine) Sql deprecated

func (engine *Engine) Sql(querystring string, args ...interface{}) *Session

Sql provides raw sql input parameter. When you have a complex SQL statement and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.

Deprecated: use SQL instead.

func (*Engine) StoreEngine

func (engine *Engine) StoreEngine(storeEngine string) *Session

StoreEngine set store engine when create table, only support mysql now

func (*Engine) Table

func (engine *Engine) Table(tableNameOrBean interface{}) *Session

Table temporarily change the Get, Find, Update's table

func (*Engine) UseBool

func (engine *Engine) UseBool(columns ...string) *Session

UseBool xorm automatically retrieve condition according struct, but if struct has bool field, it will ignore them. So use UseBool to tell system to do not ignore them. If no parameters, it will use all the bool field of struct, or it will use parameters's columns

func (*Engine) Where

func (engine *Engine) Where(query interface{}, args ...interface{}) *Session

Where method provide a condition query

type MysqlOption

type MysqlOption struct {
	Cluster string
	Hosts   []string
}

type Param

type Param interface{}

type ParamDesc

type ParamDesc bool

type ParamIn

type ParamIn []interface{}

func CastToParamIn

func CastToParamIn(input interface{}) ParamIn

cast input to []interface{} type.

type ParamInDesc

type ParamInDesc ParamIn

func CastToParamInDesc

func CastToParamInDesc(input interface{}) ParamInDesc

cast input to ParamIn type.

type ParamNil

type ParamNil struct{}

type ParamRange

type ParamRange struct {
	Min interface{}
	Max interface{}
}

type ParamRangeDesc

type ParamRangeDesc ParamRange

type Session

type Session struct {
	*xorm.Session
	// contains filtered or unexported fields
}

func (*Session) After

func (session *Session) After(closures func(interface{})) *Session

After Apply after Processor, affected bean is passed to closure arg

func (*Session) Alias

func (session *Session) Alias(alias string) *Session

Alias set the table alias

func (*Session) AllCols

func (session *Session) AllCols() *Session

AllCols ask all columns

func (*Session) And

func (session *Session) And(query interface{}, args ...interface{}) *Session

And provides custom query condition.

func (*Session) Asc

func (session *Session) Asc(colNames ...string) *Session

Asc provide asc order by query condition, the input parameters are columns.

func (*Session) Before

func (session *Session) Before(closures func(interface{})) *Session

Before Apply before Processor, affected bean is passed to closure arg

func (*Session) Cascade

func (session *Session) Cascade(trueOrFalse ...bool) *Session

Cascade indicates if loading sub Struct

func (*Session) Charset

func (session *Session) Charset(charset string) *Session

Charset is only avialble mysql dialect currently

func (*Session) Cols

func (session *Session) Cols(columns ...string) *Session

Cols provides some columns to special

func (*Session) Context

func (session *Session) Context(ctx context.Context) *Session

Context sets the context on this session

func (*Session) Decr

func (session *Session) Decr(column string, arg ...interface{}) *Session

Decr provides a query string like "count = count - 1"

func (*Session) Delete

func (session *Session) Delete(bean interface{}) (int64, error)

Delete records, bean's non-empty fields are conditions

func (*Session) Desc

func (session *Session) Desc(colNames ...string) *Session

Desc provide desc order by query condition, the input parameters are columns.

func (*Session) Distinct

func (session *Session) Distinct(columns ...string) *Session

Distinct use for distinct columns. Caution: when you are using cache, distinct will not be cached because cache system need id, but distinct will not provide id

func (*Session) Exec

func (session *Session) Exec(sqlOrArgs ...interface{}) (sql.Result, error)

Exec raw sql

func (*Session) Find

func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{}) error

Find retrieve records from table, condiBeans's non-empty fields are conditions. beans could be []Struct, []*Struct, map[int64]Struct map[int64]*Struct

func (*Session) FindAndCount

func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error)

FindAndCount find the results and also return the counts

func (*Session) ForUpdate

func (session *Session) ForUpdate() *Session

ForUpdate Set Read/Write locking for UPDATE

func (*Session) Get

func (session *Session) Get(bean interface{}) (bool, error)

Get retrieve one record from database, bean's non-empty fields will be as conditions

func (*Session) GroupBy

func (session *Session) GroupBy(keys string) *Session

GroupBy Generate Group By statement

func (*Session) Having

func (session *Session) Having(conditions string) *Session

Having Generate Having statement

func (*Session) ID

func (session *Session) ID(id interface{}) *Session

ID provides converting id as a query condition

func (*Session) Id deprecated

func (session *Session) Id(id interface{}) *Session

Id provides converting id as a query condition

Deprecated: use ID instead

func (*Session) In

func (session *Session) In(column string, args ...interface{}) *Session

In provides a query string like "id in (1, 2, 3)"

func (*Session) Incr

func (session *Session) Incr(column string, arg ...interface{}) *Session

Incr provides a query string like "count = count + 1"

func (*Session) Insert

func (session *Session) Insert(beans ...interface{}) (int64, error)

Insert insert one or more beans

func (*Session) InsertMulti

func (session *Session) InsertMulti(rowsSlicePtr interface{}) (int64, error)

InsertMulti insert multiple records

func (*Session) InsertOne

func (session *Session) InsertOne(bean interface{}) (int64, error)

InsertOne insert only one struct into database as a record. The in parameter bean must a struct or a point to struct. The return parameter is inserted and error

func (*Session) Join

func (session *Session) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session

Join join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN

func (*Session) LastSQL

func (session *Session) LastSQL() (string, []interface{})

LastSQL returns last query information

func (*Session) Limit

func (session *Session) Limit(limit int, start ...int) *Session

Limit provide limit and offset query condition

func (*Session) MustCols

func (session *Session) MustCols(columns ...string) *Session

MustCols specify some columns must use even if they are empty

func (*Session) NoAutoCondition

func (session *Session) NoAutoCondition(no ...bool) *Session

NoAutoCondition disable generate SQL condition from beans

func (*Session) NoAutoTime

func (session *Session) NoAutoTime() *Session

NoAutoTime means do not automatically give created field and updated field the current time on the current session temporarily

func (*Session) NoCache

func (session *Session) NoCache() *Session

NoCache ask this session do not retrieve data from cache system and get data from database directly.

func (*Session) NoCascade

func (session *Session) NoCascade() *Session

NoCascade indicate that no cascade load child object

func (*Session) NotIn

func (session *Session) NotIn(column string, args ...interface{}) *Session

NotIn provides a query string like "id in (1, 2, 3)"

func (*Session) Nullable

func (session *Session) Nullable(columns ...string) *Session

Nullable Set null when column is zero-value and nullable for update

func (*Session) Omit

func (session *Session) Omit(columns ...string) *Session

Omit Only not use the parameters as select or update columns

func (*Session) Or

func (session *Session) Or(query interface{}, args ...interface{}) *Session

Or provides custom query condition.

func (*Session) OrderBy

func (session *Session) OrderBy(order string) *Session

OrderBy provide order by query condition, the input parameter is the content after order by on a sql statement.

func (*Session) PingContext

func (session *Session) PingContext(ctx context.Context) error

PingContext test if database is ok

func (*Session) Prepare

func (session *Session) Prepare() *Session

Prepare set a flag to session that should be prepare statement before execute query

func (*Session) Query

func (session *Session) Query(sqlOrArgs ...interface{}) ([]map[string][]byte, error)

Query runs a raw sql and return records as []map[string][]byte

func (*Session) QueryInterface

func (session *Session) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error)

QueryInterface runs a raw sql and return records as []map[string]interface{}

func (*Session) QuerySliceString

func (session *Session) QuerySliceString(sqlOrArgs ...interface{}) ([][]string, error)

QuerySliceString runs a raw sql and return records as [][]string

func (*Session) QueryString

func (session *Session) QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error)

QueryString runs a raw sql and return records as []map[string]string

func (*Session) SQL

func (session *Session) SQL(query interface{}, args ...interface{}) *Session

SQL provides raw sql input parameter. When you have a complex SQL statement and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.

func (*Session) Select

func (session *Session) Select(str string) *Session

Select provides some columns to special

func (*Session) SetExpr

func (session *Session) SetExpr(column string, expression string) *Session

SetExpr provides a query string like "column = {expression}"

func (*Session) Sql deprecated

func (session *Session) Sql(query string, args ...interface{}) *Session

Sql provides raw sql input parameter. When you have a complex SQL statement and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.

Deprecated: use SQL instead.

func (*Session) StoreEngine

func (session *Session) StoreEngine(storeEngine string) *Session

StoreEngine is only avialble mysql dialect currently

func (*Session) Table

func (session *Session) Table(tableNameOrBean interface{}) *Session

Table can input a string or pointer to struct for special a table to operate.

func (*Session) Unscoped

func (session *Session) Unscoped() *Session

Unscoped always disable struct tag "deleted"

func (*Session) Update

func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int64, error)

Update records, bean's non-empty fields are updated contents, condiBean' non-empty filds are conditions CAUTION:

1.bool will defaultly be updated content nor conditions
 You should call UseBool if you have bool to use.
2.float32 & float64 may be not inexact as conditions

func (*Session) UseBool

func (session *Session) UseBool(columns ...string) *Session

UseBool automatically retrieve condition according struct, but if struct has bool field, it will ignore them. So use UseBool to tell system to do not ignore them. If no parameters, it will use all the bool field of struct, or it will use parameters's columns

func (*Session) Where

func (session *Session) Where(query interface{}, args ...interface{}) *Session

Where provides custom query condition.

Jump to

Keyboard shortcuts

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