2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2014-07-30 16:48:36 +04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2015-01-20 07:15:24 +03:00
|
|
|
"strings"
|
2014-08-23 12:00:04 +04:00
|
|
|
"time"
|
2013-11-16 14:01:44 +04:00
|
|
|
)
|
2013-11-10 18:29:53 +04:00
|
|
|
|
2014-08-23 12:00:04 +04:00
|
|
|
// NowFunc returns current time, this function is exported in order to be able
|
2015-02-24 22:17:35 +03:00
|
|
|
// to give the flexibility to the developer to customize it according to their
|
2014-08-23 12:00:04 +04:00
|
|
|
// needs
|
|
|
|
//
|
|
|
|
// e.g: return time.Now().UTC()
|
|
|
|
//
|
|
|
|
var NowFunc = func() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2013-10-25 14:04:48 +04:00
|
|
|
type DB struct {
|
2015-02-28 06:48:18 +03:00
|
|
|
Value interface{}
|
|
|
|
Error error
|
|
|
|
RowsAffected int64
|
2016-01-16 16:55:00 +03:00
|
|
|
callbacks *Callback
|
2015-02-28 06:48:18 +03:00
|
|
|
db sqlCommon
|
|
|
|
parent *DB
|
|
|
|
search *search
|
|
|
|
logMode int
|
|
|
|
logger logger
|
|
|
|
dialect Dialect
|
|
|
|
singularTable bool
|
|
|
|
source string
|
|
|
|
values map[string]interface{}
|
|
|
|
joinTableHandlers map[string]JoinTableHandler
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2016-01-10 16:26:55 +03:00
|
|
|
func Open(dialect string, args ...interface{}) (*DB, error) {
|
2014-08-05 06:32:15 +04:00
|
|
|
var db DB
|
2014-01-23 12:43:39 +04:00
|
|
|
var err error
|
2014-08-05 06:32:15 +04:00
|
|
|
|
2014-11-22 22:24:26 +03:00
|
|
|
if len(args) == 0 {
|
2014-08-05 06:32:15 +04:00
|
|
|
err = errors.New("invalid database source")
|
2015-02-17 17:55:14 +03:00
|
|
|
} else {
|
|
|
|
var source string
|
|
|
|
var dbSql sqlCommon
|
|
|
|
|
|
|
|
switch value := args[0].(type) {
|
|
|
|
case string:
|
|
|
|
var driver = dialect
|
|
|
|
if len(args) == 1 {
|
|
|
|
source = value
|
|
|
|
} else if len(args) >= 2 {
|
|
|
|
driver = value
|
|
|
|
source = args[1].(string)
|
|
|
|
}
|
|
|
|
dbSql, err = sql.Open(driver, source)
|
|
|
|
case sqlCommon:
|
|
|
|
source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String()
|
|
|
|
dbSql = value
|
|
|
|
}
|
2014-08-05 06:32:15 +04:00
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
db = DB{
|
2016-02-15 09:09:24 +03:00
|
|
|
dialect: newDialect(dialect, dbSql.(*sql.DB)),
|
2016-01-16 14:20:52 +03:00
|
|
|
logger: defaultLogger,
|
2016-01-16 16:55:00 +03:00
|
|
|
callbacks: defaultCallback,
|
2016-01-16 14:20:52 +03:00
|
|
|
source: source,
|
|
|
|
values: map[string]interface{}{},
|
|
|
|
db: dbSql,
|
2014-11-22 22:24:26 +03:00
|
|
|
}
|
2015-02-17 17:55:14 +03:00
|
|
|
db.parent = &db
|
2015-08-02 02:37:33 +03:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = db.DB().Ping() // Send a ping to make sure the database connection is alive.
|
|
|
|
}
|
2014-08-05 06:32:15 +04:00
|
|
|
}
|
2014-11-22 22:24:26 +03:00
|
|
|
|
2016-01-10 16:26:55 +03:00
|
|
|
return &db, err
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2014-04-24 06:54:27 +04:00
|
|
|
func (s *DB) Close() error {
|
|
|
|
return s.parent.db.(*sql.DB).Close()
|
|
|
|
}
|
|
|
|
|
2013-12-04 10:29:44 +04:00
|
|
|
func (s *DB) DB() *sql.DB {
|
|
|
|
return s.db.(*sql.DB)
|
|
|
|
}
|
|
|
|
|
2015-03-04 08:55:39 +03:00
|
|
|
func (s *DB) New() *DB {
|
|
|
|
clone := s.clone()
|
|
|
|
clone.search = nil
|
|
|
|
clone.Value = nil
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2015-02-26 07:35:33 +03:00
|
|
|
// NewScope create scope for callbacks, including DB's search information
|
2016-01-15 16:03:35 +03:00
|
|
|
func (s *DB) NewScope(value interface{}) *Scope {
|
|
|
|
dbClone := s.clone()
|
2015-02-26 07:35:33 +03:00
|
|
|
dbClone.Value = value
|
2015-03-12 08:52:29 +03:00
|
|
|
return &Scope{db: dbClone, Search: dbClone.search.clone(), Value: value}
|
2015-02-26 07:35:33 +03:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
// CommonDB Return the underlying sql.DB or sql.Tx instance.
|
2014-03-17 14:08:44 +04:00
|
|
|
// Use of this method is discouraged. It's mainly intended to allow
|
|
|
|
// coexistence with legacy non-GORM code.
|
|
|
|
func (s *DB) CommonDB() sqlCommon {
|
|
|
|
return s.db
|
2014-03-16 20:24:32 +04:00
|
|
|
}
|
|
|
|
|
2016-01-16 16:55:00 +03:00
|
|
|
func (s *DB) Callback() *Callback {
|
2016-01-16 14:20:52 +03:00
|
|
|
s.parent.callbacks = s.parent.callbacks.clone()
|
|
|
|
return s.parent.callbacks
|
2014-01-29 06:25:58 +04:00
|
|
|
}
|
|
|
|
|
2014-01-03 15:23:41 +04:00
|
|
|
func (s *DB) SetLogger(l logger) {
|
2015-08-01 06:09:17 +03:00
|
|
|
s.logger = l
|
2013-11-11 05:05:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func (s *DB) LogMode(enable bool) *DB {
|
|
|
|
if enable {
|
2013-12-17 15:16:03 +04:00
|
|
|
s.logMode = 2
|
|
|
|
} else {
|
|
|
|
s.logMode = 1
|
|
|
|
}
|
2013-11-16 11:01:31 +04:00
|
|
|
return s
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func (s *DB) SingularTable(enable bool) {
|
2015-10-01 02:09:00 +03:00
|
|
|
modelStructsMap = newModelStructsMap()
|
2015-02-17 17:55:14 +03:00
|
|
|
s.parent.singularTable = enable
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Where(query interface{}, args ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Where(query, args...).db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Or(query interface{}, args ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Or(query, args...).db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Not(query interface{}, args ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Not(query, args...).db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 15:32:52 +03:00
|
|
|
func (s *DB) Limit(limit int) *DB {
|
|
|
|
return s.clone().search.Limit(limit).db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 15:32:52 +03:00
|
|
|
func (s *DB) Offset(offset int) *DB {
|
|
|
|
return s.clone().search.Offset(offset).db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Order(value string, reorder ...bool) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Order(value, reorder...).db
|
2013-11-11 07:11:49 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 15:12:32 +03:00
|
|
|
func (s *DB) Select(query interface{}, args ...interface{}) *DB {
|
2015-03-12 10:50:38 +03:00
|
|
|
return s.clone().search.Select(query, args...).db
|
2013-11-06 17:43:41 +04:00
|
|
|
}
|
|
|
|
|
2015-03-12 13:01:27 +03:00
|
|
|
func (s *DB) Omit(columns ...string) *DB {
|
|
|
|
return s.clone().search.Omit(columns...).db
|
|
|
|
}
|
|
|
|
|
2013-11-17 09:22:09 +04:00
|
|
|
func (s *DB) Group(query string) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Group(query).db
|
2013-11-17 09:22:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Having(query string, values ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Having(query, values...).db
|
2013-11-17 09:22:09 +04:00
|
|
|
}
|
|
|
|
|
2016-02-16 17:48:26 +03:00
|
|
|
func (s *DB) Joins(query string, args ...interface{}) *DB {
|
|
|
|
return s.clone().search.Joins(query, args...).db
|
2013-11-17 09:22:09 +04:00
|
|
|
}
|
|
|
|
|
2013-11-18 10:35:44 +04:00
|
|
|
func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
|
|
|
|
for _, f := range funcs {
|
2015-02-17 17:55:14 +03:00
|
|
|
s = f(s)
|
2013-11-18 10:35:44 +04:00
|
|
|
}
|
2015-02-17 17:55:14 +03:00
|
|
|
return s
|
2013-11-18 10:35:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Unscoped() *DB {
|
2015-03-12 10:01:59 +03:00
|
|
|
return s.clone().search.unscoped().db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-28 05:48:44 +04:00
|
|
|
func (s *DB) Attrs(attrs ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Attrs(attrs...).db
|
2014-01-28 05:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Assign(attrs ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Assign(attrs...).db
|
2014-01-28 05:48:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
2014-10-28 12:18:11 +03:00
|
|
|
newScope := s.clone().NewScope(out)
|
2015-03-12 08:52:29 +03:00
|
|
|
newScope.Search.Limit(1)
|
2015-03-23 06:07:39 +03:00
|
|
|
return newScope.Set("gorm:order_by_primary_key", "ASC").
|
2016-01-16 14:20:52 +03:00
|
|
|
inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Last(out interface{}, where ...interface{}) *DB {
|
2014-10-28 12:18:11 +03:00
|
|
|
newScope := s.clone().NewScope(out)
|
2015-03-12 08:52:29 +03:00
|
|
|
newScope.Search.Limit(1)
|
2015-03-23 06:07:39 +03:00
|
|
|
return newScope.Set("gorm:order_by_primary_key", "DESC").
|
2016-01-16 14:20:52 +03:00
|
|
|
inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
|
2013-10-31 13:31:00 +04:00
|
|
|
}
|
2014-01-28 05:30:30 +04:00
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
|
2016-01-16 14:20:52 +03:00
|
|
|
return s.clone().NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callbacks.queries).db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2015-04-17 13:27:20 +03:00
|
|
|
func (s *DB) Scan(dest interface{}) *DB {
|
2016-01-16 14:20:52 +03:00
|
|
|
return s.clone().NewScope(s.Value).Set("gorm:query_destination", dest).callCallbacks(s.parent.callbacks.queries).db
|
2015-04-17 13:27:20 +03:00
|
|
|
}
|
|
|
|
|
2013-11-17 08:02:22 +04:00
|
|
|
func (s *DB) Row() *sql.Row {
|
2014-01-28 05:48:44 +04:00
|
|
|
return s.NewScope(s.Value).row()
|
2013-11-17 08:02:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Rows() (*sql.Rows, error) {
|
2014-01-28 05:48:44 +04:00
|
|
|
return s.NewScope(s.Value).rows()
|
2013-11-17 08:02:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-14 18:29:06 +03:00
|
|
|
func (s *DB) ScanRows(rows *sql.Rows, value interface{}) error {
|
|
|
|
var (
|
|
|
|
clone = s.clone()
|
|
|
|
scope = clone.NewScope(value)
|
|
|
|
columns, err = rows.Columns()
|
|
|
|
)
|
|
|
|
|
|
|
|
if clone.AddError(err) == nil {
|
|
|
|
scope.scan(rows, columns, scope.Fields())
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone.Error
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:27:20 +03:00
|
|
|
func (s *DB) Pluck(column string, value interface{}) *DB {
|
|
|
|
return s.NewScope(s.Value).pluck(column, value).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Count(value interface{}) *DB {
|
|
|
|
return s.NewScope(s.Value).count(value).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
|
|
|
|
return s.clone().NewScope(s.Value).related(value, foreignKeys...).db
|
2013-10-30 11:21:58 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {
|
2013-11-16 14:01:44 +04:00
|
|
|
c := s.clone()
|
2015-02-17 17:55:14 +03:00
|
|
|
if result := c.First(out, where...); result.Error != nil {
|
|
|
|
if !result.RecordNotFound() {
|
|
|
|
return result
|
2014-06-23 16:10:50 +04:00
|
|
|
}
|
2014-01-28 08:28:44 +04:00
|
|
|
c.NewScope(out).inlineCondition(where...).initialize()
|
2014-01-28 06:23:31 +04:00
|
|
|
} else {
|
2015-10-12 17:38:41 +03:00
|
|
|
c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(c.search.assignAttrs), false)
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
2013-11-16 14:01:44 +04:00
|
|
|
return c
|
2013-10-31 04:15:19 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *DB {
|
2013-11-16 14:01:44 +04:00
|
|
|
c := s.clone()
|
2015-02-17 17:55:14 +03:00
|
|
|
if result := c.First(out, where...); result.Error != nil {
|
|
|
|
if !result.RecordNotFound() {
|
|
|
|
return result
|
2014-06-23 16:10:50 +04:00
|
|
|
}
|
2016-01-16 14:20:52 +03:00
|
|
|
c.AddError(c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(c.parent.callbacks.creates).db.Error)
|
2015-03-12 08:52:29 +03:00
|
|
|
} else if len(c.search.assignAttrs) > 0 {
|
2016-01-16 14:20:52 +03:00
|
|
|
c.AddError(c.NewScope(out).InstanceSet("gorm:update_interface", c.search.assignAttrs).callCallbacks(c.parent.callbacks.updates).db.Error)
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
2013-11-16 14:01:44 +04:00
|
|
|
return c
|
2013-10-29 14:02:28 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 08:19:35 +04:00
|
|
|
func (s *DB) Update(attrs ...interface{}) *DB {
|
|
|
|
return s.Updates(toSearchableMap(attrs...), true)
|
|
|
|
}
|
|
|
|
|
2014-01-27 07:06:13 +04:00
|
|
|
func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
|
2014-08-20 13:05:02 +04:00
|
|
|
return s.clone().NewScope(s.Value).
|
2014-01-27 18:36:08 +04:00
|
|
|
Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
|
2014-08-20 13:05:02 +04:00
|
|
|
InstanceSet("gorm:update_interface", values).
|
2016-01-16 14:20:52 +03:00
|
|
|
callCallbacks(s.parent.callbacks.updates).db
|
2013-11-16 08:19:35 +04:00
|
|
|
}
|
|
|
|
|
2013-11-17 17:39:50 +04:00
|
|
|
func (s *DB) UpdateColumn(attrs ...interface{}) *DB {
|
2014-01-28 04:27:12 +04:00
|
|
|
return s.UpdateColumns(toSearchableMap(attrs...))
|
2013-11-17 17:39:50 +04:00
|
|
|
}
|
|
|
|
|
2014-01-28 04:27:12 +04:00
|
|
|
func (s *DB) UpdateColumns(values interface{}) *DB {
|
2014-08-20 13:05:02 +04:00
|
|
|
return s.clone().NewScope(s.Value).
|
2014-01-28 04:27:12 +04:00
|
|
|
Set("gorm:update_column", true).
|
2015-03-29 00:15:12 +03:00
|
|
|
Set("gorm:save_associations", false).
|
2014-08-20 13:05:02 +04:00
|
|
|
InstanceSet("gorm:update_interface", values).
|
2016-01-16 14:20:52 +03:00
|
|
|
callCallbacks(s.parent.callbacks.updates).db
|
2013-11-17 17:39:50 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Save(value interface{}) *DB {
|
2014-01-27 04:26:59 +04:00
|
|
|
scope := s.clone().NewScope(value)
|
2014-01-26 15:34:06 +04:00
|
|
|
if scope.PrimaryKeyZero() {
|
2016-01-16 14:20:52 +03:00
|
|
|
return scope.callCallbacks(s.parent.callbacks.creates).db
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
2016-01-16 14:20:52 +03:00
|
|
|
return scope.callCallbacks(s.parent.callbacks.updates).db
|
2013-10-29 14:02:28 +04:00
|
|
|
}
|
|
|
|
|
2014-06-12 00:51:27 +04:00
|
|
|
func (s *DB) Create(value interface{}) *DB {
|
|
|
|
scope := s.clone().NewScope(value)
|
2016-01-16 14:20:52 +03:00
|
|
|
return scope.callCallbacks(s.parent.callbacks.creates).db
|
2014-06-12 00:51:27 +04:00
|
|
|
}
|
|
|
|
|
2014-07-24 14:30:12 +04:00
|
|
|
func (s *DB) Delete(value interface{}, where ...interface{}) *DB {
|
2016-01-16 14:20:52 +03:00
|
|
|
return s.clone().NewScope(value).inlineCondition(where...).callCallbacks(s.parent.callbacks.deletes).db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-03 14:14:51 +04:00
|
|
|
func (s *DB) Raw(sql string, values ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Raw(true).Where(sql, values...).db
|
2014-01-03 14:14:51 +04:00
|
|
|
}
|
|
|
|
|
2013-11-17 12:47:39 +04:00
|
|
|
func (s *DB) Exec(sql string, values ...interface{}) *DB {
|
2014-01-28 06:29:12 +04:00
|
|
|
scope := s.clone().NewScope(nil)
|
2016-01-15 16:03:35 +03:00
|
|
|
generatedSQL := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
|
|
|
|
generatedSQL = strings.TrimSuffix(strings.TrimPrefix(generatedSQL, "("), ")")
|
|
|
|
scope.Raw(generatedSQL)
|
2014-01-28 06:29:12 +04:00
|
|
|
return scope.Exec().db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Model(value interface{}) *DB {
|
|
|
|
c := s.clone()
|
2014-01-04 11:08:00 +04:00
|
|
|
c.Value = value
|
2013-11-16 07:36:30 +04:00
|
|
|
return c
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Table(name string) *DB {
|
2015-02-26 11:08:15 +03:00
|
|
|
clone := s.clone()
|
2015-03-12 08:52:29 +03:00
|
|
|
clone.search.Table(name)
|
2015-02-26 11:08:15 +03:00
|
|
|
clone.Value = nil
|
|
|
|
return clone
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Debug() *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().LogMode(true)
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Begin() *DB {
|
|
|
|
c := s.clone()
|
|
|
|
if db, ok := c.db.(sqlDb); ok {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
c.db = interface{}(tx).(sqlCommon)
|
2015-08-13 11:42:13 +03:00
|
|
|
c.AddError(err)
|
2013-11-16 07:36:30 +04:00
|
|
|
} else {
|
2015-08-13 11:42:13 +03:00
|
|
|
c.AddError(CantStartTransaction)
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
return c
|
2013-10-29 05:01:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Commit() *DB {
|
|
|
|
if db, ok := s.db.(sqlTx); ok {
|
2015-08-13 11:42:13 +03:00
|
|
|
s.AddError(db.Commit())
|
2013-11-16 07:36:30 +04:00
|
|
|
} else {
|
2015-08-13 11:42:13 +03:00
|
|
|
s.AddError(NoValidTransaction)
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Rollback() *DB {
|
|
|
|
if db, ok := s.db.(sqlTx); ok {
|
2015-08-13 11:42:13 +03:00
|
|
|
s.AddError(db.Rollback())
|
2013-11-16 07:36:30 +04:00
|
|
|
} else {
|
2015-08-13 11:42:13 +03:00
|
|
|
s.AddError(NoValidTransaction)
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
return s
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
2013-10-26 11:47:30 +04:00
|
|
|
|
2013-11-23 17:38:31 +04:00
|
|
|
func (s *DB) NewRecord(value interface{}) bool {
|
2015-08-12 19:32:18 +03:00
|
|
|
return s.clone().NewScope(value).PrimaryKeyZero()
|
2013-11-23 17:38:31 +04:00
|
|
|
}
|
|
|
|
|
2013-11-24 07:29:37 +04:00
|
|
|
func (s *DB) RecordNotFound() bool {
|
|
|
|
return s.Error == RecordNotFound
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:03:35 +03:00
|
|
|
// CreateTable create table for models
|
|
|
|
func (s *DB) CreateTable(models ...interface{}) *DB {
|
2015-08-26 06:54:07 +03:00
|
|
|
db := s.clone()
|
2016-01-15 16:03:35 +03:00
|
|
|
for _, model := range models {
|
|
|
|
db = db.NewScope(model).createTable().db
|
2015-08-26 06:54:07 +03:00
|
|
|
}
|
|
|
|
return db
|
2013-10-27 04:06:01 +04:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:03:35 +03:00
|
|
|
// DropTable drop table for models
|
2015-08-26 06:54:07 +03:00
|
|
|
func (s *DB) DropTable(values ...interface{}) *DB {
|
|
|
|
db := s.clone()
|
|
|
|
for _, value := range values {
|
2016-01-03 12:20:24 +03:00
|
|
|
if tableName, ok := value.(string); ok {
|
|
|
|
db = db.Table(tableName)
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:54:07 +03:00
|
|
|
db = db.NewScope(value).dropTable().db
|
|
|
|
}
|
|
|
|
return db
|
2013-10-28 16:27:25 +04:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:03:35 +03:00
|
|
|
// DropTableIfExists drop table for models only when it exists
|
2015-08-26 06:54:07 +03:00
|
|
|
func (s *DB) DropTableIfExists(values ...interface{}) *DB {
|
|
|
|
db := s.clone()
|
|
|
|
for _, value := range values {
|
2015-12-16 16:00:56 +03:00
|
|
|
if tableName, ok := value.(string); ok {
|
|
|
|
db = db.Table(tableName)
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:54:07 +03:00
|
|
|
db = db.NewScope(value).dropTableIfExists().db
|
|
|
|
}
|
|
|
|
return db
|
2014-08-05 18:14:40 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 09:28:54 +04:00
|
|
|
func (s *DB) HasTable(value interface{}) bool {
|
2016-02-14 09:17:14 +03:00
|
|
|
var (
|
|
|
|
scope = s.clone().NewScope(value)
|
|
|
|
tableName string
|
|
|
|
)
|
|
|
|
|
|
|
|
if name, ok := value.(string); ok {
|
|
|
|
tableName = name
|
|
|
|
} else {
|
|
|
|
tableName = scope.TableName()
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
has := scope.Dialect().HasTable(tableName)
|
2015-08-13 11:42:13 +03:00
|
|
|
s.AddError(scope.db.Error)
|
2015-08-09 00:08:25 +03:00
|
|
|
return has
|
2014-08-29 09:28:54 +04:00
|
|
|
}
|
|
|
|
|
2014-08-20 07:56:39 +04:00
|
|
|
func (s *DB) AutoMigrate(values ...interface{}) *DB {
|
|
|
|
db := s.clone()
|
|
|
|
for _, value := range values {
|
2016-01-17 12:12:19 +03:00
|
|
|
db = db.NewScope(value).autoMigrate().db
|
2014-08-20 07:56:39 +04:00
|
|
|
}
|
|
|
|
return db
|
2013-10-26 11:47:30 +04:00
|
|
|
}
|
2013-11-01 11:01:39 +04:00
|
|
|
|
2013-11-17 16:38:43 +04:00
|
|
|
func (s *DB) ModifyColumn(column string, typ string) *DB {
|
2015-06-24 01:27:21 +03:00
|
|
|
scope := s.clone().NewScope(s.Value)
|
|
|
|
scope.modifyColumn(column, typ)
|
|
|
|
return scope.db
|
2013-11-01 11:01:39 +04:00
|
|
|
}
|
2013-11-07 05:09:54 +04:00
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) DropColumn(column string) *DB {
|
2015-06-24 01:27:21 +03:00
|
|
|
scope := s.clone().NewScope(s.Value)
|
|
|
|
scope.dropColumn(column)
|
|
|
|
return scope.db
|
2013-11-07 05:09:54 +04:00
|
|
|
}
|
2013-11-11 09:16:08 +04:00
|
|
|
|
2014-06-01 02:12:31 +04:00
|
|
|
func (s *DB) AddIndex(indexName string, column ...string) *DB {
|
2016-01-12 08:44:16 +03:00
|
|
|
scope := s.Unscoped().NewScope(s.Value)
|
2015-06-24 01:27:21 +03:00
|
|
|
scope.addIndex(false, indexName, column...)
|
|
|
|
return scope.db
|
2014-06-01 02:35:56 +04:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:02:40 +03:00
|
|
|
func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB {
|
2015-06-24 01:27:21 +03:00
|
|
|
scope := s.clone().NewScope(s.Value)
|
|
|
|
scope.addIndex(true, indexName, column...)
|
|
|
|
return scope.db
|
2015-03-02 18:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) RemoveIndex(indexName string) *DB {
|
2015-06-24 01:27:21 +03:00
|
|
|
scope := s.clone().NewScope(s.Value)
|
|
|
|
scope.removeIndex(indexName)
|
|
|
|
return scope.db
|
2015-03-02 18:02:40 +03:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:03:35 +03:00
|
|
|
// AddForeignKey Add foreign key to the given scope
|
|
|
|
// Example: db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
|
2015-02-07 14:04:10 +03:00
|
|
|
func (s *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) *DB {
|
2015-06-24 01:27:21 +03:00
|
|
|
scope := s.clone().NewScope(s.Value)
|
|
|
|
scope.addForeignKey(field, dest, onDelete, onUpdate)
|
2015-06-24 09:09:59 +03:00
|
|
|
return scope.db
|
2015-02-07 14:04:10 +03:00
|
|
|
}
|
|
|
|
|
2014-07-30 10:30:21 +04:00
|
|
|
func (s *DB) Association(column string) *Association {
|
2015-02-17 17:55:14 +03:00
|
|
|
var err error
|
2014-07-30 10:30:21 +04:00
|
|
|
scope := s.clone().NewScope(s.Value)
|
2014-07-30 16:48:36 +04:00
|
|
|
|
2015-03-11 06:28:30 +03:00
|
|
|
if primaryField := scope.PrimaryField(); primaryField.IsBlank {
|
2015-02-17 17:55:14 +03:00
|
|
|
err = errors.New("primary key can't be nil")
|
2014-07-30 16:48:36 +04:00
|
|
|
} else {
|
2015-02-17 17:55:14 +03:00
|
|
|
if field, ok := scope.FieldByName(column); ok {
|
2015-07-30 09:26:48 +03:00
|
|
|
if field.Relationship == nil || len(field.Relationship.ForeignFieldNames) == 0 {
|
2015-02-17 17:55:14 +03:00
|
|
|
err = fmt.Errorf("invalid association %v for %v", column, scope.IndirectValue().Type())
|
|
|
|
} else {
|
2016-01-16 07:18:04 +03:00
|
|
|
return &Association{scope: scope, column: column, field: field}
|
2015-02-17 17:55:14 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("%v doesn't have column %v", scope.IndirectValue().Type(), column)
|
|
|
|
}
|
2014-07-30 16:48:36 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
return &Association{Error: err}
|
2013-11-11 09:16:08 +04:00
|
|
|
}
|
2014-08-20 12:25:01 +04:00
|
|
|
|
2015-02-01 18:19:29 +03:00
|
|
|
func (s *DB) Preload(column string, conditions ...interface{}) *DB {
|
2015-03-12 08:52:29 +03:00
|
|
|
return s.clone().search.Preload(column, conditions...).db
|
2015-02-01 18:19:29 +03:00
|
|
|
}
|
|
|
|
|
2014-08-20 12:25:01 +04:00
|
|
|
// Set set value by name
|
|
|
|
func (s *DB) Set(name string, value interface{}) *DB {
|
2014-08-25 13:10:46 +04:00
|
|
|
return s.clone().InstantSet(name, value)
|
2014-08-20 13:05:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 13:10:46 +04:00
|
|
|
func (s *DB) InstantSet(name string, value interface{}) *DB {
|
2014-08-20 12:25:01 +04:00
|
|
|
s.values[name] = value
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get get value by name
|
|
|
|
func (s *DB) Get(name string) (value interface{}, ok bool) {
|
|
|
|
value, ok = s.values[name]
|
|
|
|
return
|
|
|
|
}
|
2015-03-19 13:23:54 +03:00
|
|
|
|
|
|
|
func (s *DB) SetJoinTableHandler(source interface{}, column string, handler JoinTableHandlerInterface) {
|
2015-06-23 09:19:59 +03:00
|
|
|
scope := s.NewScope(source)
|
|
|
|
for _, field := range scope.GetModelStruct().StructFields {
|
2015-03-19 13:23:54 +03:00
|
|
|
if field.Name == column || field.DBName == column {
|
2016-01-03 05:00:18 +03:00
|
|
|
if many2many := field.TagSettings["MANY2MANY"]; many2many != "" {
|
2015-03-20 06:11:30 +03:00
|
|
|
source := (&Scope{Value: source}).GetModelStruct().ModelType
|
|
|
|
destination := (&Scope{Value: reflect.New(field.Struct.Type).Interface()}).GetModelStruct().ModelType
|
|
|
|
handler.Setup(field.Relationship, many2many, source, destination)
|
|
|
|
field.Relationship.JoinTableHandler = handler
|
2016-02-15 09:09:24 +03:00
|
|
|
if table := handler.Table(s); scope.Dialect().HasTable(table) {
|
2015-06-23 09:19:59 +03:00
|
|
|
s.Table(table).AutoMigrate(handler)
|
|
|
|
}
|
2015-03-20 06:11:30 +03:00
|
|
|
}
|
2015-03-19 13:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 11:42:13 +03:00
|
|
|
|
|
|
|
func (s *DB) AddError(err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if err != RecordNotFound {
|
|
|
|
if s.logMode == 0 {
|
|
|
|
go s.print(fileWithLineNum(), err)
|
|
|
|
} else {
|
|
|
|
s.log(err)
|
|
|
|
}
|
|
|
|
|
2015-08-18 06:06:10 +03:00
|
|
|
errors := Errors{errors: s.GetErrors()}
|
|
|
|
errors.Add(err)
|
|
|
|
if len(errors.GetErrors()) > 1 {
|
|
|
|
err = errors
|
2015-08-14 09:53:26 +03:00
|
|
|
}
|
2015-08-13 11:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Error = err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-14 09:53:26 +03:00
|
|
|
func (s *DB) GetErrors() (errors []error) {
|
|
|
|
if errs, ok := s.Error.(errorsInterface); ok {
|
|
|
|
return errs.GetErrors()
|
|
|
|
} else if s.Error != nil {
|
2015-08-13 11:42:13 +03:00
|
|
|
return []error{s.Error}
|
|
|
|
}
|
2015-08-14 09:53:26 +03:00
|
|
|
return
|
2015-08-13 11:42:13 +03:00
|
|
|
}
|