2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2013-12-04 10:29:44 +04:00
|
|
|
|
2013-11-16 14:01:44 +04:00
|
|
|
"github.com/jinzhu/gorm/dialect"
|
|
|
|
)
|
2013-11-10 18:29:53 +04:00
|
|
|
|
2013-10-25 14:04:48 +04:00
|
|
|
type DB struct {
|
2014-01-04 11:08:00 +04:00
|
|
|
Value interface{}
|
2014-01-26 08:41:37 +04:00
|
|
|
callback *callback
|
2014-01-23 12:43:39 +04:00
|
|
|
Error error
|
2013-11-16 07:36:30 +04:00
|
|
|
db sqlCommon
|
|
|
|
parent *DB
|
|
|
|
search *search
|
2014-01-23 12:43:39 +04:00
|
|
|
logMode int
|
|
|
|
logger logger
|
2013-11-16 07:36:30 +04:00
|
|
|
dialect dialect.Dialect
|
|
|
|
tagIdentifier string
|
|
|
|
singularTable bool
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2014-01-23 12:43:39 +04:00
|
|
|
func Open(driver, source string) (DB, error) {
|
|
|
|
var err error
|
2014-01-26 08:41:37 +04:00
|
|
|
db := DB{dialect: dialect.New(driver), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback}
|
2013-10-28 08:52:06 +04:00
|
|
|
db.db, err = sql.Open(driver, source)
|
2013-11-16 07:36:30 +04:00
|
|
|
db.parent = &db
|
2014-01-23 12:43:39 +04:00
|
|
|
return db, err
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-12-04 10:29:44 +04:00
|
|
|
func (s *DB) DB() *sql.DB {
|
|
|
|
return s.db.(*sql.DB)
|
|
|
|
}
|
|
|
|
|
2014-01-29 06:25:58 +04:00
|
|
|
func (s *DB) Callback() *callback {
|
|
|
|
s.parent.callback = s.parent.callback.clone()
|
|
|
|
return s.parent.callback
|
|
|
|
}
|
|
|
|
|
2013-11-13 20:03:31 +04:00
|
|
|
func (s *DB) SetTagIdentifier(str string) {
|
2013-11-16 07:36:30 +04:00
|
|
|
s.parent.tagIdentifier = str
|
2013-11-13 20:03:31 +04:00
|
|
|
}
|
|
|
|
|
2014-01-03 15:23:41 +04:00
|
|
|
func (s *DB) SetLogger(l logger) {
|
2013-11-16 07:36:30 +04:00
|
|
|
s.parent.logger = l
|
2013-11-11 05:05:14 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 11:01:31 +04:00
|
|
|
func (s *DB) LogMode(b bool) *DB {
|
2013-12-17 15:16:03 +04:00
|
|
|
if b {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) SingularTable(b bool) {
|
|
|
|
s.parent.singularTable = b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Where(query interface{}, args ...interface{}) *DB {
|
|
|
|
return s.clone().search.where(query, args...).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Or(query interface{}, args ...interface{}) *DB {
|
2013-11-16 11:01:31 +04: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 {
|
|
|
|
return s.clone().search.not(query, args...).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Limit(value interface{}) *DB {
|
|
|
|
return s.clone().search.limit(value).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Offset(value interface{}) *DB {
|
|
|
|
return s.clone().search.offset(value).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Order(value string, reorder ...bool) *DB {
|
|
|
|
return s.clone().search.order(value, reorder...).db
|
2013-11-11 07:11:49 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Select(value interface{}) *DB {
|
|
|
|
return s.clone().search.selects(value).db
|
2013-11-06 17:43:41 +04:00
|
|
|
}
|
|
|
|
|
2013-11-17 09:22:09 +04:00
|
|
|
func (s *DB) Group(query string) *DB {
|
|
|
|
return s.clone().search.group(query).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Having(query string, values ...interface{}) *DB {
|
|
|
|
return s.clone().search.having(query, values...).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Joins(query string) *DB {
|
|
|
|
return s.clone().search.joins(query).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Includes(value interface{}) *DB {
|
|
|
|
return s.clone().search.includes(value).db
|
|
|
|
}
|
|
|
|
|
2013-11-18 10:35:44 +04:00
|
|
|
func (s *DB) Scopes(funcs ...func(*DB) *DB) *DB {
|
|
|
|
c := s
|
|
|
|
for _, f := range funcs {
|
|
|
|
c = f(c)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Unscoped() *DB {
|
|
|
|
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 {
|
|
|
|
return s.clone().search.attrs(attrs...).db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Assign(attrs ...interface{}) *DB {
|
|
|
|
return s.clone().search.assign(attrs...).db
|
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
2014-01-28 05:30:30 +04:00
|
|
|
scope := s.clone().NewScope(out)
|
|
|
|
scope.Search = scope.Search.clone().order(scope.PrimaryKey()).limit(1)
|
2014-01-28 08:28:44 +04:00
|
|
|
return scope.inlineCondition(where...).callCallbacks(s.parent.callback.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-01-28 05:30:30 +04:00
|
|
|
scope := s.clone().NewScope(out)
|
|
|
|
scope.Search = scope.Search.clone().order(scope.PrimaryKey() + " DESC").limit(1)
|
2014-01-28 08:28:44 +04:00
|
|
|
return scope.inlineCondition(where...).callCallbacks(s.parent.callback.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 {
|
2014-01-28 08:28:44 +04:00
|
|
|
return s.clone().NewScope(out).inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
|
2013-10-25 14:04:48 +04: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
|
|
|
}
|
|
|
|
|
2014-01-03 14:14:51 +04:00
|
|
|
func (s *DB) Scan(dest interface{}) *DB {
|
2014-01-28 05:48:44 +04:00
|
|
|
scope := s.clone().NewScope(s.Value).Set("gorm:query_destination", dest)
|
|
|
|
Query(scope)
|
|
|
|
return scope.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()
|
|
|
|
if c.First(out, where...).Error == RecordNotFound {
|
2014-01-28 08:28:44 +04:00
|
|
|
c.NewScope(out).inlineCondition(where...).initialize()
|
2014-01-28 06:23:31 +04:00
|
|
|
} else {
|
2014-01-28 12:56:51 +04:00
|
|
|
c.NewScope(out).updatedAttrsWithValues(convertInterfaceToMap(s.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()
|
|
|
|
if c.First(out, where...).Error == RecordNotFound {
|
2014-01-28 08:28:44 +04:00
|
|
|
c.NewScope(out).inlineCondition(where...).initialize().callCallbacks(s.parent.callback.creates)
|
2014-01-28 12:56:51 +04:00
|
|
|
} else if len(s.search.AssignAttrs) > 0 {
|
|
|
|
c.NewScope(out).Set("gorm:update_interface", s.search.AssignAttrs).callCallbacks(s.parent.callback.updates)
|
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-01-27 18:36:08 +04:00
|
|
|
return s.clone().NewScope(s.Value).
|
|
|
|
Set("gorm:update_interface", values).
|
|
|
|
Set("gorm:ignore_protected_attrs", len(ignoreProtectedAttrs) > 0).
|
|
|
|
callCallbacks(s.parent.callback.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 {
|
|
|
|
return s.clone().NewScope(s.Value).
|
|
|
|
Set("gorm:update_interface", values).
|
|
|
|
Set("gorm:update_column", true).
|
|
|
|
callCallbacks(s.parent.callback.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() {
|
2014-01-27 07:06:13 +04:00
|
|
|
return scope.callCallbacks(s.parent.callback.creates).db
|
2014-01-26 15:34:06 +04:00
|
|
|
} else {
|
2014-01-27 07:06:13 +04:00
|
|
|
return scope.callCallbacks(s.parent.callback.updates).db
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
2013-10-29 14:02:28 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Delete(value interface{}) *DB {
|
2014-01-27 04:26:59 +04:00
|
|
|
return s.clone().NewScope(value).callCallbacks(s.parent.callback.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 {
|
2014-01-28 12:56:51 +04: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)
|
|
|
|
scope.Raw(scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values}))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-28 13:09:43 +04:00
|
|
|
func (s *DB) Related(value interface{}, foreignKeys ...string) *DB {
|
|
|
|
return s.clone().NewScope(s.Value).related(value, foreignKeys...).db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Pluck(column string, value interface{}) *DB {
|
2014-01-28 07:37:32 +04:00
|
|
|
return s.NewScope(s.Value).pluck(column, value).db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Count(value interface{}) *DB {
|
2014-01-28 07:38:53 +04:00
|
|
|
return s.NewScope(s.Value).count(value).db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Table(name string) *DB {
|
2013-11-16 07:41:31 +04:00
|
|
|
return s.clone().search.table(name).db
|
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)
|
|
|
|
c.err(err)
|
|
|
|
} else {
|
2013-11-16 14:01:44 +04:00
|
|
|
c.err(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 {
|
|
|
|
s.err(db.Commit())
|
|
|
|
} else {
|
|
|
|
s.err(NoValidTransaction)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Rollback() *DB {
|
|
|
|
if db, ok := s.db.(sqlTx); ok {
|
|
|
|
s.err(db.Rollback())
|
|
|
|
} else {
|
|
|
|
s.err(NoValidTransaction)
|
|
|
|
}
|
|
|
|
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 {
|
2014-01-28 06:23:31 +04: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
|
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
// Migrations
|
|
|
|
func (s *DB) CreateTable(value interface{}) *DB {
|
2014-01-28 11:54:19 +04:00
|
|
|
return s.clone().NewScope(value).createTable().db
|
2013-10-27 04:06:01 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) DropTable(value interface{}) *DB {
|
2014-01-28 11:54:19 +04:00
|
|
|
return s.clone().NewScope(value).dropTable().db
|
2013-10-28 16:27:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) AutoMigrate(value interface{}) *DB {
|
2014-01-28 11:54:19 +04:00
|
|
|
return s.clone().NewScope(value).autoMigrate().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 {
|
2014-01-28 11:54:19 +04:00
|
|
|
s.clone().NewScope(s.Value).modifyColumn(column, typ)
|
2013-11-16 07:36:30 +04:00
|
|
|
return s
|
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 {
|
2014-01-28 11:54:19 +04:00
|
|
|
s.clone().NewScope(s.Value).dropColumn(column)
|
2013-11-16 07:36:30 +04:00
|
|
|
return s
|
2013-11-07 05:09:54 +04:00
|
|
|
}
|
2013-11-11 09:16:08 +04:00
|
|
|
|
2014-01-28 13:09:43 +04:00
|
|
|
func (s *DB) AddIndex(column string, indexName ...string) *DB {
|
|
|
|
s.clone().NewScope(s.Value).addIndex(column, indexName...)
|
2013-11-16 07:36:30 +04:00
|
|
|
return s
|
2013-11-11 11:48:31 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) RemoveIndex(column string) *DB {
|
2014-01-28 11:54:19 +04:00
|
|
|
s.clone().NewScope(s.Value).removeIndex(column)
|
2013-11-16 07:36:30 +04:00
|
|
|
return s
|
2013-11-11 09:16:08 +04:00
|
|
|
}
|