types.go 5.7 KB
Newer Older
A
astaxie 已提交
1
// Beego (http://beego.me/)
A
astaxie 已提交
2

A
astaxie 已提交
3
// @description beego is an open-source, high-performance web framework for the Go programming language.
A
astaxie 已提交
4

A
astaxie 已提交
5
// @link        http://github.com/astaxie/beego for the canonical source repository
A
astaxie 已提交
6

A
astaxie 已提交
7
// @license     http://github.com/astaxie/beego/blob/master/LICENSE
A
astaxie 已提交
8 9

// @authors     astaxie, slene
A
astaxie 已提交
10

S
slene 已提交
11 12 13 14 15
package orm

import (
	"database/sql"
	"reflect"
16
	"time"
S
slene 已提交
17 18
)

F
FuXiaoHei 已提交
19
// database driver
S
slene 已提交
20 21 22 23 24
type Driver interface {
	Name() string
	Type() DriverType
}

F
FuXiaoHei 已提交
25
// field info
S
slene 已提交
26 27 28 29 30 31 32
type Fielder interface {
	String() string
	FieldType() int
	SetRaw(interface{}) error
	RawValue() interface{}
}

F
FuXiaoHei 已提交
33
// orm struct
S
slene 已提交
34
type Ormer interface {
35
	Read(interface{}, ...string) error
K
Kyle McCullough 已提交
36
	ReadOrCreate(interface{}, string, ...string) (bool, int64, error)
37
	Insert(interface{}) (int64, error)
S
slene 已提交
38
	InsertMulti(int, interface{}) (int64, error)
39
	Update(interface{}, ...string) (int64, error)
40
	Delete(interface{}) (int64, error)
41 42
	LoadRelated(interface{}, string, ...interface{}) (int64, error)
	QueryM2M(interface{}, string) QueryM2Mer
S
slene 已提交
43 44 45 46 47 48
	QueryTable(interface{}) QuerySeter
	Using(string) error
	Begin() error
	Commit() error
	Rollback() error
	Raw(string, ...interface{}) RawSeter
S
slene 已提交
49
	Driver() Driver
50
	GetDB() dbQuerier
S
slene 已提交
51 52
}

F
FuXiaoHei 已提交
53
// insert prepared statement
S
slene 已提交
54
type Inserter interface {
55
	Insert(interface{}) (int64, error)
S
slene 已提交
56 57 58
	Close() error
}

F
FuXiaoHei 已提交
59
// query seter
S
slene 已提交
60 61 62
type QuerySeter interface {
	Filter(string, ...interface{}) QuerySeter
	Exclude(string, ...interface{}) QuerySeter
S
slene 已提交
63
	SetCond(*Condition) QuerySeter
64
	Limit(interface{}, ...interface{}) QuerySeter
65
	Offset(interface{}) QuerySeter
S
slene 已提交
66 67 68
	OrderBy(...string) QuerySeter
	RelatedSel(...interface{}) QuerySeter
	Count() (int64, error)
S
slene 已提交
69
	Exist() bool
S
slene 已提交
70 71 72
	Update(Params) (int64, error)
	Delete() (int64, error)
	PrepareInsert() (Inserter, error)
73 74
	All(interface{}, ...string) (int64, error)
	One(interface{}, ...string) error
S
slene 已提交
75 76 77
	Values(*[]Params, ...string) (int64, error)
	ValuesList(*[]ParamsList, ...string) (int64, error)
	ValuesFlat(*ParamsList, string) (int64, error)
78 79
	RowsToMap(*Params, string, string) (int64, error)
	RowsToStruct(interface{}, string, string) (int64, error)
S
slene 已提交
80 81
}

F
FuXiaoHei 已提交
82
// model to model query struct
83 84 85 86 87 88 89 90
type QueryM2Mer interface {
	Add(...interface{}) (int64, error)
	Remove(...interface{}) (int64, error)
	Exist(interface{}) bool
	Clear() (int64, error)
	Count() (int64, error)
}

F
FuXiaoHei 已提交
91
// raw query statement
S
slene 已提交
92
type RawPreparer interface {
93
	Exec(...interface{}) (sql.Result, error)
S
slene 已提交
94 95 96
	Close() error
}

F
FuXiaoHei 已提交
97
// raw query seter
S
slene 已提交
98
type RawSeter interface {
99
	Exec() (sql.Result, error)
S
slene 已提交
100 101 102
	QueryRow(...interface{}) error
	QueryRows(...interface{}) (int64, error)
	SetArgs(...interface{}) RawSeter
103 104 105 106 107
	Values(*[]Params, ...string) (int64, error)
	ValuesList(*[]ParamsList, ...string) (int64, error)
	ValuesFlat(*ParamsList, ...string) (int64, error)
	RowsToMap(*Params, string, string) (int64, error)
	RowsToStruct(interface{}, string, string) (int64, error)
S
slene 已提交
108 109 110
	Prepare() (RawPreparer, error)
}

F
FuXiaoHei 已提交
111
// statement querier
S
slene 已提交
112 113 114 115 116 117 118
type stmtQuerier interface {
	Close() error
	Exec(args ...interface{}) (sql.Result, error)
	Query(args ...interface{}) (*sql.Rows, error)
	QueryRow(args ...interface{}) *sql.Row
}

F
FuXiaoHei 已提交
119
// db querier
S
slene 已提交
120 121 122 123 124 125 126
type dbQuerier interface {
	Prepare(query string) (*sql.Stmt, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

127 128 129 130 131 132 133 134
// type DB interface {
// 	Begin() (*sql.Tx, error)
// 	Prepare(query string) (stmtQuerier, error)
// 	Exec(query string, args ...interface{}) (sql.Result, error)
// 	Query(query string, args ...interface{}) (*sql.Rows, error)
// 	QueryRow(query string, args ...interface{}) *sql.Row
// }

F
FuXiaoHei 已提交
135
// transaction beginner
S
slene 已提交
136 137 138 139
type txer interface {
	Begin() (*sql.Tx, error)
}

F
FuXiaoHei 已提交
140
// transaction ending
S
slene 已提交
141 142 143 144 145
type txEnder interface {
	Commit() error
	Rollback() error
}

F
FuXiaoHei 已提交
146
// base database struct
S
slene 已提交
147
type dbBaser interface {
148
	Read(dbQuerier, *modelInfo, reflect.Value, *time.Location, []string) error
149
	Insert(dbQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
S
slene 已提交
150 151
	InsertMulti(dbQuerier, *modelInfo, reflect.Value, int, *time.Location) (int64, error)
	InsertValue(dbQuerier, *modelInfo, bool, []string, []interface{}) (int64, error)
152
	InsertStmt(stmtQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
153
	Update(dbQuerier, *modelInfo, reflect.Value, *time.Location, []string) (int64, error)
154
	Delete(dbQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
155
	ReadBatch(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, *time.Location, []string) (int64, error)
156
	SupportUpdateJoin() bool
157 158 159
	UpdateBatch(dbQuerier, *querySet, *modelInfo, *Condition, Params, *time.Location) (int64, error)
	DeleteBatch(dbQuerier, *querySet, *modelInfo, *Condition, *time.Location) (int64, error)
	Count(dbQuerier, *querySet, *modelInfo, *Condition, *time.Location) (int64, error)
160
	OperatorSql(string) string
161 162
	GenerateOperatorSql(*modelInfo, *fieldInfo, string, []interface{}, *time.Location) (string, []interface{})
	GenerateOperatorLeftCol(*fieldInfo, string, *string)
S
slene 已提交
163
	PrepareInsert(dbQuerier, *modelInfo) (stmtQuerier, string, error)
164
	ReadValues(dbQuerier, *querySet, *modelInfo, *Condition, []string, interface{}, *time.Location) (int64, error)
165
	RowsTo(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, string, string, *time.Location) (int64, error)
166 167 168
	MaxLimit() uint64
	TableQuote() string
	ReplaceMarks(*string)
S
slene 已提交
169
	HasReturningID(*modelInfo, *string) bool
170 171
	TimeFromDB(*time.Time, *time.Location)
	TimeToDB(*time.Time, *time.Location)
S
slene 已提交
172
	DbTypes() map[string]string
S
slene 已提交
173 174 175 176 177
	GetTables(dbQuerier) (map[string]bool, error)
	GetColumns(dbQuerier, string) (map[string][3]string, error)
	ShowTablesQuery() string
	ShowColumnsQuery(string) string
	IndexExists(dbQuerier, string, string) bool
178
	collectFieldValue(*modelInfo, *fieldInfo, reflect.Value, bool, *time.Location) (interface{}, error)
S
slene 已提交
179
}