2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-06-25 02:39:37 +00:00
|
|
|
// @authors astaxie, slene
|
2013-07-30 12:32:38 +00:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"reflect"
|
2013-08-13 09:16:12 +00:00
|
|
|
"time"
|
2013-07-30 12:32:38 +00:00
|
|
|
)
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// database driver
|
2013-08-07 11:11:44 +00:00
|
|
|
type Driver interface {
|
|
|
|
Name() string
|
|
|
|
Type() DriverType
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// field info
|
2013-07-30 12:32:38 +00:00
|
|
|
type Fielder interface {
|
|
|
|
String() string
|
|
|
|
FieldType() int
|
|
|
|
SetRaw(interface{}) error
|
|
|
|
RawValue() interface{}
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// orm struct
|
2013-07-30 12:32:38 +00:00
|
|
|
type Ormer interface {
|
2013-09-12 11:04:39 +00:00
|
|
|
Read(interface{}, ...string) error
|
2014-01-22 05:58:57 +00:00
|
|
|
ReadOrCreate(interface{}, string, ...string) (bool, int64, error)
|
2013-08-09 12:14:18 +00:00
|
|
|
Insert(interface{}) (int64, error)
|
2014-01-06 03:07:03 +00:00
|
|
|
InsertMulti(int, interface{}) (int64, error)
|
2013-09-12 11:04:39 +00:00
|
|
|
Update(interface{}, ...string) (int64, error)
|
2013-08-09 12:14:18 +00:00
|
|
|
Delete(interface{}) (int64, error)
|
2013-10-14 14:31:35 +00:00
|
|
|
LoadRelated(interface{}, string, ...interface{}) (int64, error)
|
|
|
|
QueryM2M(interface{}, string) QueryM2Mer
|
2013-07-30 12:32:38 +00:00
|
|
|
QueryTable(interface{}) QuerySeter
|
|
|
|
Using(string) error
|
|
|
|
Begin() error
|
|
|
|
Commit() error
|
|
|
|
Rollback() error
|
|
|
|
Raw(string, ...interface{}) RawSeter
|
2013-08-07 11:11:44 +00:00
|
|
|
Driver() Driver
|
2014-01-26 17:48:00 +00:00
|
|
|
GetDB() dbQuerier
|
2013-07-30 12:32:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// insert prepared statement
|
2013-07-30 12:32:38 +00:00
|
|
|
type Inserter interface {
|
2013-08-09 12:14:18 +00:00
|
|
|
Insert(interface{}) (int64, error)
|
2013-07-30 12:32:38 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// query seter
|
2013-07-30 12:32:38 +00:00
|
|
|
type QuerySeter interface {
|
|
|
|
Filter(string, ...interface{}) QuerySeter
|
|
|
|
Exclude(string, ...interface{}) QuerySeter
|
2013-08-07 11:11:44 +00:00
|
|
|
SetCond(*Condition) QuerySeter
|
2013-09-13 10:06:44 +00:00
|
|
|
Limit(interface{}, ...interface{}) QuerySeter
|
2013-08-16 12:01:18 +00:00
|
|
|
Offset(interface{}) QuerySeter
|
2013-07-30 12:32:38 +00:00
|
|
|
OrderBy(...string) QuerySeter
|
|
|
|
RelatedSel(...interface{}) QuerySeter
|
|
|
|
Count() (int64, error)
|
2013-10-11 22:57:14 +00:00
|
|
|
Exist() bool
|
2013-07-30 12:32:38 +00:00
|
|
|
Update(Params) (int64, error)
|
|
|
|
Delete() (int64, error)
|
|
|
|
PrepareInsert() (Inserter, error)
|
2013-09-12 11:04:39 +00:00
|
|
|
All(interface{}, ...string) (int64, error)
|
|
|
|
One(interface{}, ...string) error
|
2013-07-30 12:32:38 +00:00
|
|
|
Values(*[]Params, ...string) (int64, error)
|
|
|
|
ValuesList(*[]ParamsList, ...string) (int64, error)
|
|
|
|
ValuesFlat(*ParamsList, string) (int64, error)
|
2014-01-26 17:48:00 +00:00
|
|
|
RowsToMap(*Params, string, string) (int64, error)
|
|
|
|
RowsToStruct(interface{}, string, string) (int64, error)
|
2013-07-30 12:32:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// model to model query struct
|
2013-10-14 14:31:35 +00:00
|
|
|
type QueryM2Mer interface {
|
|
|
|
Add(...interface{}) (int64, error)
|
|
|
|
Remove(...interface{}) (int64, error)
|
|
|
|
Exist(interface{}) bool
|
|
|
|
Clear() (int64, error)
|
|
|
|
Count() (int64, error)
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// raw query statement
|
2013-07-30 12:32:38 +00:00
|
|
|
type RawPreparer interface {
|
2013-08-10 16:15:26 +00:00
|
|
|
Exec(...interface{}) (sql.Result, error)
|
2013-07-30 12:32:38 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// raw query seter
|
2013-07-30 12:32:38 +00:00
|
|
|
type RawSeter interface {
|
2013-08-10 16:15:26 +00:00
|
|
|
Exec() (sql.Result, error)
|
2013-08-07 11:11:44 +00:00
|
|
|
QueryRow(...interface{}) error
|
|
|
|
QueryRows(...interface{}) (int64, error)
|
|
|
|
SetArgs(...interface{}) RawSeter
|
2014-01-26 17:48:00 +00:00
|
|
|
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)
|
2013-07-30 12:32:38 +00:00
|
|
|
Prepare() (RawPreparer, error)
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// statement querier
|
2013-08-09 05:20:19 +00:00
|
|
|
type stmtQuerier interface {
|
|
|
|
Close() error
|
|
|
|
Exec(args ...interface{}) (sql.Result, error)
|
|
|
|
Query(args ...interface{}) (*sql.Rows, error)
|
|
|
|
QueryRow(args ...interface{}) *sql.Row
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// db querier
|
2013-07-30 12:32:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-01-26 17:48:00 +00:00
|
|
|
// 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
|
|
|
|
// }
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// transaction beginner
|
2013-08-09 05:20:19 +00:00
|
|
|
type txer interface {
|
|
|
|
Begin() (*sql.Tx, error)
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// transaction ending
|
2013-08-09 05:20:19 +00:00
|
|
|
type txEnder interface {
|
|
|
|
Commit() error
|
|
|
|
Rollback() error
|
|
|
|
}
|
|
|
|
|
2014-01-17 15:28:54 +00:00
|
|
|
// base database struct
|
2013-07-30 12:32:38 +00:00
|
|
|
type dbBaser interface {
|
2013-09-12 11:04:39 +00:00
|
|
|
Read(dbQuerier, *modelInfo, reflect.Value, *time.Location, []string) error
|
2013-08-13 09:16:12 +00:00
|
|
|
Insert(dbQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
|
2014-01-06 03:07:03 +00:00
|
|
|
InsertMulti(dbQuerier, *modelInfo, reflect.Value, int, *time.Location) (int64, error)
|
|
|
|
InsertValue(dbQuerier, *modelInfo, bool, []string, []interface{}) (int64, error)
|
2013-08-13 09:16:12 +00:00
|
|
|
InsertStmt(stmtQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
|
2013-09-12 11:04:39 +00:00
|
|
|
Update(dbQuerier, *modelInfo, reflect.Value, *time.Location, []string) (int64, error)
|
2013-08-13 09:16:12 +00:00
|
|
|
Delete(dbQuerier, *modelInfo, reflect.Value, *time.Location) (int64, error)
|
2013-09-12 11:04:39 +00:00
|
|
|
ReadBatch(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, *time.Location, []string) (int64, error)
|
2013-08-10 16:15:26 +00:00
|
|
|
SupportUpdateJoin() bool
|
2013-08-13 09:16:12 +00:00
|
|
|
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)
|
2013-08-10 16:15:26 +00:00
|
|
|
OperatorSql(string) string
|
2013-08-13 09:16:12 +00:00
|
|
|
GenerateOperatorSql(*modelInfo, *fieldInfo, string, []interface{}, *time.Location) (string, []interface{})
|
|
|
|
GenerateOperatorLeftCol(*fieldInfo, string, *string)
|
2013-08-09 05:20:19 +00:00
|
|
|
PrepareInsert(dbQuerier, *modelInfo) (stmtQuerier, string, error)
|
2013-08-13 09:16:12 +00:00
|
|
|
ReadValues(dbQuerier, *querySet, *modelInfo, *Condition, []string, interface{}, *time.Location) (int64, error)
|
2014-01-26 17:48:00 +00:00
|
|
|
RowsTo(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, string, string, *time.Location) (int64, error)
|
2013-08-10 16:15:26 +00:00
|
|
|
MaxLimit() uint64
|
|
|
|
TableQuote() string
|
|
|
|
ReplaceMarks(*string)
|
2013-08-11 14:27:45 +00:00
|
|
|
HasReturningID(*modelInfo, *string) bool
|
2013-08-13 09:16:12 +00:00
|
|
|
TimeFromDB(*time.Time, *time.Location)
|
|
|
|
TimeToDB(*time.Time, *time.Location)
|
2013-08-19 14:37:39 +00:00
|
|
|
DbTypes() map[string]string
|
2013-08-27 04:33:27 +00:00
|
|
|
GetTables(dbQuerier) (map[string]bool, error)
|
|
|
|
GetColumns(dbQuerier, string) (map[string][3]string, error)
|
|
|
|
ShowTablesQuery() string
|
|
|
|
ShowColumnsQuery(string) string
|
|
|
|
IndexExists(dbQuerier, string, string) bool
|
2013-10-14 14:31:35 +00:00
|
|
|
collectFieldValue(*modelInfo, *fieldInfo, reflect.Value, bool, *time.Location) (interface{}, error)
|
2013-07-30 12:32:38 +00:00
|
|
|
}
|