types.go 5.4 KB
Newer Older
S
slene 已提交
1 2 3 4 5
package orm

import (
	"database/sql"
	"reflect"
6
	"time"
S
slene 已提交
7 8
)

F
FuXiaoHei 已提交
9
// database driver
S
slene 已提交
10 11 12 13 14
type Driver interface {
	Name() string
	Type() DriverType
}

F
FuXiaoHei 已提交
15
// field info
S
slene 已提交
16 17 18 19 20 21 22
type Fielder interface {
	String() string
	FieldType() int
	SetRaw(interface{}) error
	RawValue() interface{}
}

F
FuXiaoHei 已提交
23
// orm struct
S
slene 已提交
24
type Ormer interface {
25
	Read(interface{}, ...string) error
K
Kyle McCullough 已提交
26
	ReadOrCreate(interface{}, string, ...string) (bool, int64, error)
27
	Insert(interface{}) (int64, error)
S
slene 已提交
28
	InsertMulti(int, interface{}) (int64, error)
29
	Update(interface{}, ...string) (int64, error)
30
	Delete(interface{}) (int64, error)
31 32
	LoadRelated(interface{}, string, ...interface{}) (int64, error)
	QueryM2M(interface{}, string) QueryM2Mer
S
slene 已提交
33 34 35 36 37 38
	QueryTable(interface{}) QuerySeter
	Using(string) error
	Begin() error
	Commit() error
	Rollback() error
	Raw(string, ...interface{}) RawSeter
S
slene 已提交
39
	Driver() Driver
40
	GetDB() dbQuerier
S
slene 已提交
41 42
}

F
FuXiaoHei 已提交
43
// insert prepared statement
S
slene 已提交
44
type Inserter interface {
45
	Insert(interface{}) (int64, error)
S
slene 已提交
46 47 48
	Close() error
}

F
FuXiaoHei 已提交
49
// query seter
S
slene 已提交
50 51 52
type QuerySeter interface {
	Filter(string, ...interface{}) QuerySeter
	Exclude(string, ...interface{}) QuerySeter
S
slene 已提交
53
	SetCond(*Condition) QuerySeter
54
	Limit(interface{}, ...interface{}) QuerySeter
55
	Offset(interface{}) QuerySeter
S
slene 已提交
56 57 58
	OrderBy(...string) QuerySeter
	RelatedSel(...interface{}) QuerySeter
	Count() (int64, error)
S
slene 已提交
59
	Exist() bool
S
slene 已提交
60 61 62
	Update(Params) (int64, error)
	Delete() (int64, error)
	PrepareInsert() (Inserter, error)
63 64
	All(interface{}, ...string) (int64, error)
	One(interface{}, ...string) error
S
slene 已提交
65 66 67
	Values(*[]Params, ...string) (int64, error)
	ValuesList(*[]ParamsList, ...string) (int64, error)
	ValuesFlat(*ParamsList, string) (int64, error)
68 69
	RowsToMap(*Params, string, string) (int64, error)
	RowsToStruct(interface{}, string, string) (int64, error)
S
slene 已提交
70 71
}

F
FuXiaoHei 已提交
72
// model to model query struct
73 74 75 76 77 78 79 80
type QueryM2Mer interface {
	Add(...interface{}) (int64, error)
	Remove(...interface{}) (int64, error)
	Exist(interface{}) bool
	Clear() (int64, error)
	Count() (int64, error)
}

F
FuXiaoHei 已提交
81
// raw query statement
S
slene 已提交
82
type RawPreparer interface {
83
	Exec(...interface{}) (sql.Result, error)
S
slene 已提交
84 85 86
	Close() error
}

F
FuXiaoHei 已提交
87
// raw query seter
S
slene 已提交
88
type RawSeter interface {
89
	Exec() (sql.Result, error)
S
slene 已提交
90 91 92
	QueryRow(...interface{}) error
	QueryRows(...interface{}) (int64, error)
	SetArgs(...interface{}) RawSeter
93 94 95 96 97
	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 已提交
98 99 100
	Prepare() (RawPreparer, error)
}

F
FuXiaoHei 已提交
101
// statement querier
S
slene 已提交
102 103 104 105 106 107 108
type stmtQuerier interface {
	Close() error
	Exec(args ...interface{}) (sql.Result, error)
	Query(args ...interface{}) (*sql.Rows, error)
	QueryRow(args ...interface{}) *sql.Row
}

F
FuXiaoHei 已提交
109
// db querier
S
slene 已提交
110 111 112 113 114 115 116
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
}

117 118 119 120 121 122 123 124
// 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 已提交
125
// transaction beginner
S
slene 已提交
126 127 128 129
type txer interface {
	Begin() (*sql.Tx, error)
}

F
FuXiaoHei 已提交
130
// transaction ending
S
slene 已提交
131 132 133 134 135
type txEnder interface {
	Commit() error
	Rollback() error
}

F
FuXiaoHei 已提交
136
// base database struct
S
slene 已提交
137
type dbBaser interface {
138
	Read(dbQuerier, *modelInfo, reflect.Value, *time.Location, []string) error
139
	Insert(dbQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
S
slene 已提交
140 141
	InsertMulti(dbQuerier, *modelInfo, reflect.Value, int, *time.Location) (int64, error)
	InsertValue(dbQuerier, *modelInfo, bool, []string, []interface{}) (int64, error)
142
	InsertStmt(stmtQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
143
	Update(dbQuerier, *modelInfo, reflect.Value, *time.Location, []string) (int64, error)
144
	Delete(dbQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
145
	ReadBatch(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, *time.Location, []string) (int64, error)
146
	SupportUpdateJoin() bool
147 148 149
	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)
150
	OperatorSql(string) string
151 152
	GenerateOperatorSql(*modelInfo, *fieldInfo, string, []interface{}, *time.Location) (string, []interface{})
	GenerateOperatorLeftCol(*fieldInfo, string, *string)
S
slene 已提交
153
	PrepareInsert(dbQuerier, *modelInfo) (stmtQuerier, string, error)
154
	ReadValues(dbQuerier, *querySet, *modelInfo, *Condition, []string, interface{}, *time.Location) (int64, error)
155
	RowsTo(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, string, string, *time.Location) (int64, error)
156 157 158
	MaxLimit() uint64
	TableQuote() string
	ReplaceMarks(*string)
S
slene 已提交
159
	HasReturningID(*modelInfo, *string) bool
160 161
	TimeFromDB(*time.Time, *time.Location)
	TimeToDB(*time.Time, *time.Location)
S
slene 已提交
162
	DbTypes() map[string]string
S
slene 已提交
163 164 165 166 167
	GetTables(dbQuerier) (map[string]bool, error)
	GetColumns(dbQuerier, string) (map[string][3]string, error)
	ShowTablesQuery() string
	ShowColumnsQuery(string) string
	IndexExists(dbQuerier, string, string) bool
168
	collectFieldValue(*modelInfo, *fieldInfo, reflect.Value, bool, *time.Location) (interface{}, error)
S
slene 已提交
169
}