xorm/ge_session.go

765 lines
22 KiB
Go
Raw Normal View History

2017-09-25 12:32:49 +00:00
package xorm
import (
"database/sql"
"github.com/go-xorm/builder"
)
2017-09-26 01:45:51 +00:00
type EGSession struct {
eg *EngineGroup
2017-09-25 12:32:49 +00:00
operation []string
args map[string]interface{}
err error
}
2017-09-26 01:45:51 +00:00
func (egs *EGSession) operate(session *Session) *Session {
for _, v := range egs.operation {
2017-09-25 12:32:49 +00:00
switch v {
case "Before":
2017-09-26 01:45:51 +00:00
args := egs.args["Before"].(BeforeArgs)
2017-09-25 12:32:49 +00:00
session = session.Before(args.closures)
case "After":
2017-09-26 01:45:51 +00:00
args := egs.args["After"].(AfterArgs)
2017-09-25 12:32:49 +00:00
session = session.After(args.closures)
case "Table":
2017-09-26 01:45:51 +00:00
args := egs.args["Table"].(TableArgs)
2017-09-25 12:32:49 +00:00
session = session.Table(args.tableNameOrBean)
case "Alias":
2017-09-26 01:45:51 +00:00
args := egs.args["Alias"].(AliasArgs)
2017-09-25 12:32:49 +00:00
session = session.Alias(args.alias)
case "NoCascade":
session = session.NoCascade()
case "ForUpdate":
session = session.ForUpdate()
case "NoAutoCondition":
2017-09-26 01:45:51 +00:00
args := egs.args["NoAutoCondition"].(NoAutoConditionArgs)
2017-09-25 12:32:49 +00:00
session = session.NoAutoCondition(args.no...)
case "Limit":
2017-09-26 01:45:51 +00:00
args := egs.args["Limit"].(LimitArgs)
2017-09-25 12:32:49 +00:00
session = session.Limit(args.limit, args.start...)
case "OrderBy":
2017-09-26 01:45:51 +00:00
args := egs.args["OrderBy"].(OrderByArgs)
2017-09-25 12:32:49 +00:00
session = session.OrderBy(args.order)
case "Desc":
2017-09-26 01:45:51 +00:00
args := egs.args["Desc"].(DescArgs)
2017-09-25 12:32:49 +00:00
session = session.Desc(args.colNames...)
case "Asc":
2017-09-26 01:45:51 +00:00
args := egs.args["Asc"].(AscArgs)
2017-09-25 12:32:49 +00:00
session = session.Asc(args.colNames...)
case "StoreEngine":
2017-09-26 01:45:51 +00:00
args := egs.args["StoreEngine"].(StoreEngineArgs)
2017-09-25 12:32:49 +00:00
session = session.StoreEngine(args.storeEngine)
case "Charset":
2017-09-26 01:45:51 +00:00
args := egs.args["Charset"].(CharsetArgs)
2017-09-25 12:32:49 +00:00
session = session.Charset(args.charset)
case "Cascade":
2017-09-26 01:45:51 +00:00
args := egs.args["Cascade"].(CascadeArgs)
2017-09-25 12:32:49 +00:00
session = session.Cascade(args.trueOrFalse...)
case "NoCache":
session = session.NoCache()
case "Join":
2017-09-26 01:45:51 +00:00
args := egs.args["Join"].(JoinArgs)
2017-09-25 12:32:49 +00:00
session = session.Join(args.joinOperator, args.tablename, args.condition, args.args...)
case "GroupBy":
2017-09-26 01:45:51 +00:00
args := egs.args["GroupBy"].(GroupByArgs)
2017-09-25 12:32:49 +00:00
session = session.GroupBy(args.keys)
case "Having":
2017-09-26 01:45:51 +00:00
args := egs.args["Having"].(HavingArgs)
2017-09-25 12:32:49 +00:00
session = session.Having(args.conditions)
case "Unscoped":
session = session.Unscoped()
case "Incr":
2017-09-26 01:45:51 +00:00
args := egs.args["Incr"].(IncrArgs)
2017-09-25 12:32:49 +00:00
session = session.Incr(args.column, args.args...)
case "Decr":
2017-09-26 01:45:51 +00:00
args := egs.args["Decr"].(DecrArgs)
2017-09-25 12:32:49 +00:00
session = session.Decr(args.column, args.args...)
case "SetExpr":
2017-09-26 01:45:51 +00:00
args := egs.args["SetExpr"].(SetExprArgs)
2017-09-25 12:32:49 +00:00
session = session.SetExpr(args.column, args.expression)
case "Select":
2017-09-26 01:45:51 +00:00
args := egs.args["Select"].(SelectArgs)
2017-09-25 12:32:49 +00:00
session = session.Select(args.str)
case "Cols":
2017-09-26 01:45:51 +00:00
args := egs.args["Cols"].(ColsArgs)
2017-09-25 12:32:49 +00:00
session = session.Cols(args.columns...)
case "AllCols":
session = session.AllCols()
case "MustCols":
2017-09-26 01:45:51 +00:00
args := egs.args["MustCols"].(MustColsArgs)
2017-09-25 12:32:49 +00:00
session = session.MustCols(args.columns...)
case "UseBool":
2017-09-26 01:45:51 +00:00
args := egs.args["UseBool"].(UseBoolArgs)
2017-09-25 12:32:49 +00:00
session = session.UseBool(args.columns...)
case "Distinct":
2017-09-26 01:45:51 +00:00
args := egs.args["Distinct"].(DistinctArgs)
2017-09-25 12:32:49 +00:00
session = session.Distinct(args.columns...)
case "Omit":
2017-09-26 01:45:51 +00:00
args := egs.args["Omit"].(OmitArgs)
2017-09-25 12:32:49 +00:00
session = session.Omit(args.columns...)
case "Nullable":
2017-09-26 01:45:51 +00:00
args := egs.args["Nullable"].(NullableArgs)
2017-09-25 12:32:49 +00:00
session = session.Nullable(args.columns...)
case "NoAutoTime":
session = session.NoAutoTime()
case "Sql":
2017-09-26 01:45:51 +00:00
args := egs.args["Sql"].(SqlArgs)
2017-09-25 12:32:49 +00:00
session = session.Sql(args.query, args.args...)
case "SQL":
2017-09-26 01:45:51 +00:00
args := egs.args["SQL"].(SqlArgs)
2017-09-25 12:32:49 +00:00
session = session.SQL(args.query, args.args...)
case "Where":
2017-09-26 01:45:51 +00:00
args := egs.args["Where"].(WhereArgs)
2017-09-25 12:32:49 +00:00
session = session.Where(args.query, args.args...)
case "And":
2017-09-26 01:45:51 +00:00
args := egs.args["And"].(AndArgs)
2017-09-25 12:32:49 +00:00
session = session.And(args.query, args.args...)
case "Or":
2017-09-26 01:45:51 +00:00
args := egs.args["Or"].(OrArgs)
2017-09-25 12:32:49 +00:00
session = session.Or(args.query, args.args...)
case "Id":
2017-09-26 01:45:51 +00:00
args := egs.args["Id"].(IdArgs)
2017-09-25 12:32:49 +00:00
session = session.Id(args.id)
case "ID":
2017-09-26 01:45:51 +00:00
args := egs.args["ID"].(IDArgs)
2017-09-25 12:32:49 +00:00
session = session.ID(args.id)
case "In":
2017-09-26 01:45:51 +00:00
args := egs.args["In"].(InArgs)
2017-09-25 12:32:49 +00:00
session = session.In(args.column, args.args...)
case "NotIn":
2017-09-26 01:45:51 +00:00
args := egs.args["NotIn"].(NotInArgs)
2017-09-25 12:32:49 +00:00
session = session.NotIn(args.column, args.args...)
case "BufferSize":
2017-09-26 01:45:51 +00:00
args := egs.args["BufferSize"].(BufferSizeArgs)
2017-09-25 12:32:49 +00:00
session = session.BufferSize(args.size)
}
}
return session
}
// Before Apply before Processor, affected bean is passed to closure arg
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Before(closures func(interface{})) *EGSession {
egs.operation = append(egs.operation, "Before")
2017-09-25 12:32:49 +00:00
args := BeforeArgs{
closures: closures,
}
2017-09-26 01:45:51 +00:00
egs.args["Before"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// After Apply after Processor, affected bean is passed to closure arg
2017-09-26 01:45:51 +00:00
func (egs *EGSession) After(closures func(interface{})) *EGSession {
egs.operation = append(egs.operation, "After")
2017-09-25 12:32:49 +00:00
args := AfterArgs{
closures: closures,
}
2017-09-26 01:45:51 +00:00
egs.args["After"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Table can input a string or pointer to struct for special a table to operate.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Table(tableNameOrBean interface{}) *EGSession {
egs.operation = append(egs.operation, "Table")
2017-09-25 12:32:49 +00:00
args := TableArgs{
tableNameOrBean: tableNameOrBean,
}
2017-09-26 01:45:51 +00:00
egs.args["Table"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Alias set the table alias
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Alias(alias string) *EGSession {
egs.operation = append(egs.operation, "Alias")
2017-09-25 12:32:49 +00:00
args := AliasArgs{
alias: alias,
}
2017-09-26 01:45:51 +00:00
egs.args["Alias"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// NoCascade indicate that no cascade load child object
2017-09-26 01:45:51 +00:00
func (egs *EGSession) NoCascade() *EGSession {
egs.operation = append(egs.operation, "NoCascade")
return egs
2017-09-25 12:32:49 +00:00
}
// ForUpdate Set Read/Write locking for UPDATE
2017-09-26 01:45:51 +00:00
func (egs *EGSession) ForUpdate() *EGSession {
egs.operation = append(egs.operation, "ForUpdate")
return egs
2017-09-25 12:32:49 +00:00
}
// NoAutoCondition disable generate SQL condition from beans
2017-09-26 01:45:51 +00:00
func (egs *EGSession) NoAutoCondition(no ...bool) *EGSession {
egs.operation = append(egs.operation, "NoAutoCondition")
2017-09-25 12:32:49 +00:00
args := NoAutoConditionArgs{
no: no,
}
2017-09-26 01:45:51 +00:00
egs.args["NoAutoCondition"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Limit provide limit and offset query condition
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Limit(limit int, start ...int) *EGSession {
egs.operation = append(egs.operation, "Limit")
2017-09-25 12:32:49 +00:00
args := LimitArgs{
limit: limit,
start: start,
}
2017-09-26 01:45:51 +00:00
egs.args["Limit"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// OrderBy provide order by query condition, the input parameter is the content
// after order by on a sql statement.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) OrderBy(order string) *EGSession {
egs.operation = append(egs.operation, "OrderBy")
2017-09-25 12:32:49 +00:00
args := OrderByArgs{
order: order,
}
2017-09-26 01:45:51 +00:00
egs.args["OrderBy"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Desc provide desc order by query condition, the input parameters are columns.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Desc(colNames ...string) *EGSession {
egs.operation = append(egs.operation, "Desc")
2017-09-25 12:32:49 +00:00
args := DescArgs{
colNames: colNames,
}
2017-09-26 01:45:51 +00:00
egs.args["Desc"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Asc provide asc order by query condition, the input parameters are columns.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Asc(colNames ...string) *EGSession {
egs.operation = append(egs.operation, "Asc")
2017-09-25 12:32:49 +00:00
args := AscArgs{
colNames: colNames,
}
2017-09-26 01:45:51 +00:00
egs.args["Asc"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// StoreEngine is only avialble mysql dialect currently
2017-09-26 01:45:51 +00:00
func (egs *EGSession) StoreEngine(storeEngine string) *EGSession {
egs.operation = append(egs.operation, "StoreEngine")
2017-09-25 12:32:49 +00:00
args := StoreEngineArgs{
storeEngine: storeEngine,
}
2017-09-26 01:45:51 +00:00
egs.args["StoreEngine"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Charset is only avialble mysql dialect currently
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Charset(charset string) *EGSession {
egs.operation = append(egs.operation, "Charset")
2017-09-25 12:32:49 +00:00
args := CharsetArgs{
charset: charset,
}
2017-09-26 01:45:51 +00:00
egs.args["Charset"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Cascade indicates if loading sub Struct
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Cascade(trueOrFalse ...bool) *EGSession {
egs.operation = append(egs.operation, "Cascade")
2017-09-25 12:32:49 +00:00
args := CascadeArgs{
trueOrFalse: trueOrFalse,
}
2017-09-26 01:45:51 +00:00
egs.args["Cascade"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// NoCache ask this session do not retrieve data from cache system and
// get data from database directly.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) NoCache() *EGSession {
egs.operation = append(egs.operation, "NoCache")
return egs
2017-09-25 12:32:49 +00:00
}
// Join join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "Join")
2017-09-25 12:32:49 +00:00
joinArgs := JoinArgs{
joinOperator: joinOperator,
tablename: tablename,
condition: condition,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["Join"] = joinArgs
return egs
2017-09-25 12:32:49 +00:00
}
// GroupBy Generate Group By statement
2017-09-26 01:45:51 +00:00
func (egs *EGSession) GroupBy(keys string) *EGSession {
egs.operation = append(egs.operation, "GroupBy")
2017-09-25 12:32:49 +00:00
args := GroupByArgs{
keys: keys,
}
2017-09-26 01:45:51 +00:00
egs.args["GroupBy"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Having Generate Having statement
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Having(conditions string) *EGSession {
egs.operation = append(egs.operation, "Having")
2017-09-25 12:32:49 +00:00
args := HavingArgs{
conditions: conditions,
}
2017-09-26 01:45:51 +00:00
egs.args["Having"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Unscoped always disable struct tag "deleted"
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Unscoped() *EGSession {
egs.operation = append(egs.operation, "Unscoped")
return egs
2017-09-25 12:32:49 +00:00
}
// Incr provides a query string like "count = count + 1"
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Incr(column string, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "Incr")
2017-09-25 12:32:49 +00:00
incrArgs := IncrArgs{
column: column,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["Incr"] = incrArgs
return egs
2017-09-25 12:32:49 +00:00
}
// Decr provides a query string like "count = count - 1"
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Decr(column string, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "Decr")
2017-09-25 12:32:49 +00:00
decrArgs := DecrArgs{
column: column,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["Decr"] = decrArgs
return egs
2017-09-25 12:32:49 +00:00
}
// SetExpr provides a query string like "column = {expression}"
2017-09-26 01:45:51 +00:00
func (egs *EGSession) SetExpr(column string, expression string) *EGSession {
egs.operation = append(egs.operation, "SetExpr")
2017-09-25 12:32:49 +00:00
args := SetExprArgs{
column: column,
expression: expression,
}
2017-09-26 01:45:51 +00:00
egs.args["SetExpr"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Select provides some columns to special
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Select(str string) *EGSession {
egs.operation = append(egs.operation, "Select")
2017-09-25 12:32:49 +00:00
args := SelectArgs{
str: str,
}
2017-09-26 01:45:51 +00:00
egs.args["Select"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Cols provides some columns to special
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Cols(columns ...string) *EGSession {
egs.operation = append(egs.operation, "Cols")
2017-09-25 12:32:49 +00:00
args := ColsArgs{
columns: columns,
}
2017-09-26 01:45:51 +00:00
egs.args["Cols"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// AllCols ask all columns
2017-09-26 01:45:51 +00:00
func (egs *EGSession) AllCols() *EGSession {
egs.operation = append(egs.operation, "AllCols")
return egs
2017-09-25 12:32:49 +00:00
}
// MustCols specify some columns must use even if they are empty
2017-09-26 01:45:51 +00:00
func (egs *EGSession) MustCols(columns ...string) *EGSession {
egs.operation = append(egs.operation, "MustCols")
2017-09-25 12:32:49 +00:00
args := MustColsArgs{
columns: columns,
}
2017-09-26 01:45:51 +00:00
egs.args["MustCols"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// 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
2017-09-26 01:45:51 +00:00
func (egs *EGSession) UseBool(columns ...string) *EGSession {
egs.operation = append(egs.operation, "UseBool")
2017-09-25 12:32:49 +00:00
args := UseBoolArgs{
columns: columns,
}
2017-09-26 01:45:51 +00:00
egs.args["UseBool"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// 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
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Distinct(columns ...string) *EGSession {
egs.operation = append(egs.operation, "Distinct")
2017-09-25 12:32:49 +00:00
args := DistinctArgs{
columns: columns,
}
2017-09-26 01:45:51 +00:00
egs.args["Distinct"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Omit Only not use the parameters as select or update columns
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Omit(columns ...string) *EGSession {
egs.operation = append(egs.operation, "Omit")
2017-09-25 12:32:49 +00:00
args := OmitArgs{
columns: columns,
}
2017-09-26 01:45:51 +00:00
egs.args["Omit"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Nullable Set null when column is zero-value and nullable for update
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Nullable(columns ...string) *EGSession {
egs.operation = append(egs.operation, "Nullable")
2017-09-25 12:32:49 +00:00
args := NullableArgs{
columns: columns,
}
2017-09-26 01:45:51 +00:00
egs.args["Nullable"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// NoAutoTime means do not automatically give created field and updated field
// the current time on the current session temporarily
2017-09-26 01:45:51 +00:00
func (egs *EGSession) NoAutoTime() *EGSession {
egs.operation = append(egs.operation, "NoAutoTime")
return egs
2017-09-25 12:32:49 +00:00
}
// 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.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Sql(query string, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "Sql")
2017-09-25 12:32:49 +00:00
sqlArgs := SqlArgs{
query: query,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["Sql"] = sqlArgs
return egs
2017-09-25 12:32:49 +00:00
}
2017-09-25 13:24:51 +00:00
type SQLArgs struct {
query interface{}
args []interface{}
}
2017-09-25 12:32:49 +00:00
// 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.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) SQL(query interface{}, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "SQL")
2017-09-25 13:24:51 +00:00
sqlArgs := SQLArgs{
2017-09-25 12:32:49 +00:00
query: query,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["SQL"] = sqlArgs
return egs
2017-09-25 12:32:49 +00:00
}
// Where provides custom query condition.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Where(query interface{}, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "Where")
2017-09-25 12:32:49 +00:00
whereArgs := WhereArgs{
query: query,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["Where"] = whereArgs
return egs
2017-09-25 12:32:49 +00:00
}
type AndArgs struct {
query interface{}
args []interface{}
}
// And provides custom query condition.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) And(query interface{}, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "And")
2017-09-25 12:32:49 +00:00
andArgs := AndArgs{
query: query,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["And"] = andArgs
return egs
2017-09-25 12:32:49 +00:00
}
type OrArgs struct {
query interface{}
args []interface{}
}
// Or provides custom query condition.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Or(query interface{}, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "Or")
2017-09-25 12:32:49 +00:00
orArgs := OrArgs{
query: query,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["Or"] = orArgs
return egs
2017-09-25 12:32:49 +00:00
}
// Id provides converting id as a query condition
//
// Deprecated: use ID instead
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Id(id interface{}) *EGSession {
egs.operation = append(egs.operation, "Id")
2017-09-25 12:32:49 +00:00
args := IdArgs{
id: id,
}
2017-09-26 01:45:51 +00:00
egs.args["Id"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// ID provides converting id as a query condition
2017-09-26 01:45:51 +00:00
func (egs *EGSession) ID(id interface{}) *EGSession {
egs.operation = append(egs.operation, "ID")
2017-09-25 12:32:49 +00:00
args := IDArgs{
id: id,
}
2017-09-26 01:45:51 +00:00
egs.args["ID"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// In provides a query string like "id in (1, 2, 3)"
2017-09-26 01:45:51 +00:00
func (egs *EGSession) In(column string, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "In")
2017-09-25 12:32:49 +00:00
inArgs := InArgs{
column: column,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["In"] = inArgs
return egs
2017-09-25 12:32:49 +00:00
}
// NotIn provides a query string like "id in (1, 2, 3)"
2017-09-26 01:45:51 +00:00
func (egs *EGSession) NotIn(column string, args ...interface{}) *EGSession {
egs.operation = append(egs.operation, "NotIn")
2017-09-25 12:32:49 +00:00
notInArgs := NotInArgs{
column: column,
args: args,
}
2017-09-26 01:45:51 +00:00
egs.args["NotIn"] = notInArgs
return egs
2017-09-25 12:32:49 +00:00
}
// Conds returns session query conditions except auto bean conditions
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Conds() builder.Cond {
return egs.eg.Master().NewSession().Conds()
2017-09-25 12:32:49 +00:00
}
// Delete records, bean's non-empty fields are conditions
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Delete(bean interface{}) (int64, error) {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Delete(bean)
}
// Exist returns true if the record exist otherwise return false
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Exist(bean ...interface{}) (bool, error) {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Exist(bean...)
}
// Find retrieve records from table, condiBeans's non-empty fields
// are conditions. beans could be []Struct, []*Struct, map[int64]Struct
// map[int64]*Struct
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Find(rowsSlicePtr interface{}, condiBean ...interface{}) error {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Find(rowsSlicePtr, condiBean...)
}
// Get retrieve one record from database, bean's non-empty fields
// will be as conditions
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Get(bean interface{}) (bool, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Get(bean)
}
// Insert insert one or more beans
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Insert(beans ...interface{}) (int64, error) {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Insert(beans...)
}
// Rows return sql.Rows compatible Rows obj, as a forward Iterator object for iterating record by record, bean's non-empty fields
// are conditions.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Rows(bean interface{}) (*Rows, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Rows(bean)
}
// Iterate record by record handle records from table, condiBeans's non-empty fields
// are conditions. beans could be []Struct, []*Struct, map[int64]Struct
// map[int64]*Struct
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Iterate(bean interface{}, fun IterFunc) error {
return egs.eg.Slave().Iterate(bean, fun)
2017-09-25 12:32:49 +00:00
}
// BufferSize sets the buffersize for iterate
2017-09-26 01:45:51 +00:00
func (egs *EGSession) BufferSize(size int) *EGSession {
egs.operation = append(egs.operation, "BufferSize")
2017-09-25 12:32:49 +00:00
args := BufferSizeArgs{
size: size,
}
2017-09-26 01:45:51 +00:00
egs.args["BufferSize"] = args
return egs
2017-09-25 12:32:49 +00:00
}
// Query runs a raw sql and return records as []map[string][]byte
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Query(sqlStr string, args ...interface{}) ([]map[string][]byte, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Query(sqlStr, args...)
}
// QueryString runs a raw sql and return records as []map[string]string
2017-09-26 01:45:51 +00:00
func (egs *EGSession) QueryString(sqlStr string, args ...interface{}) ([]map[string]string, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.QueryString(sqlStr, args...)
}
// QueryInterface runs a raw sql and return records as []map[string]interface{}
2017-09-26 01:45:51 +00:00
func (egs *EGSession) QueryInterface(sqlStr string, args ...interface{}) ([]map[string]interface{}, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.QueryInterface(sqlStr, args...)
}
// Exec raw sql
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Exec(sqlStr string, args ...interface{}) (sql.Result, error) {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Exec(sqlStr, args...)
}
// CreateTable create a table according a bean
2017-09-26 01:45:51 +00:00
func (egs *EGSession) CreateTable(bean interface{}) error {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
return session.CreateTable(bean)
}
// CreateIndexes create indexes
2017-09-26 01:45:51 +00:00
func (egs *EGSession) CreateIndexes(bean interface{}) error {
return egs.eg.Master().CreateIndexes(bean)
2017-09-25 12:32:49 +00:00
}
// CreateUniques create uniques
2017-09-26 01:45:51 +00:00
func (egs *EGSession) CreateUniques(bean interface{}) error {
return egs.eg.Master().CreateUniques(bean)
2017-09-25 12:32:49 +00:00
}
// DropIndexes drop indexes
2017-09-26 01:45:51 +00:00
func (egs *EGSession) DropIndexes(bean interface{}) error {
return egs.eg.Master().DropIndexes(bean)
2017-09-25 12:32:49 +00:00
}
// DropTable drop table will drop table if exist, if drop failed, it will return error
2017-09-26 01:45:51 +00:00
func (egs *EGSession) DropTable(beanOrTableName interface{}) error {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
return session.DropTable(beanOrTableName)
}
// IsTableExist if a table is exist
2017-09-26 01:45:51 +00:00
func (egs *EGSession) IsTableExist(beanOrTableName interface{}) (bool, error) {
return egs.eg.Master().IsTableExist(beanOrTableName)
2017-09-25 12:32:49 +00:00
}
// IsTableEmpty if table have any records
2017-09-26 01:45:51 +00:00
func (egs *EGSession) IsTableEmpty(bean interface{}) (bool, error) {
return egs.eg.Master().IsTableEmpty(bean)
2017-09-25 12:32:49 +00:00
}
// Sync2 synchronize structs to database tables
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Sync2(beans ...interface{}) error {
return egs.eg.Master().Sync2(beans...)
2017-09-25 12:32:49 +00:00
}
// Count counts the records. bean's non-empty fields
// are conditions.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Count(bean ...interface{}) (int64, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Count(bean...)
}
// sum call sum some column. bean's non-empty fields are conditions.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Sum(bean interface{}, columnName string) (res float64, err error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Sum(bean, columnName)
}
// SumInt call sum some column. bean's non-empty fields are conditions.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) SumInt(bean interface{}, columnName string) (res int64, err error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.SumInt(bean, columnName)
}
// Sums call sum some columns. bean's non-empty fields are conditions.
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Sums(bean interface{}, columnNames ...string) ([]float64, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Sums(bean, columnNames...)
}
// SumsInt sum specify columns and return as []int64 instead of []float64
2017-09-26 01:45:51 +00:00
func (egs *EGSession) SumsInt(bean interface{}, columnNames ...string) ([]int64, error) {
session := egs.eg.Slave().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.SumsInt(bean, columnNames...)
}
// 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
2017-09-26 01:45:51 +00:00
func (egs *EGSession) Update(bean interface{}, condiBean ...interface{}) (int64, error) {
session := egs.eg.Master().NewSession()
2017-09-25 12:32:49 +00:00
defer session.Close()
2017-09-26 01:45:51 +00:00
session = egs.operate(session)
2017-09-25 12:32:49 +00:00
return session.Update(bean, condiBean...)
}