2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2013-11-16 07:41:31 +04:00
|
|
|
"errors"
|
2013-11-16 07:36:30 +04:00
|
|
|
)
|
2013-11-16 07:41:31 +04:00
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
import "github.com/jinzhu/gorm/dialect"
|
2013-11-10 18:29:53 +04:00
|
|
|
|
2013-10-25 14:04:48 +04:00
|
|
|
type DB struct {
|
2013-11-16 07:36:30 +04:00
|
|
|
db sqlCommon
|
|
|
|
parent *DB
|
|
|
|
search *search
|
|
|
|
data interface{}
|
|
|
|
Error error
|
|
|
|
dialect dialect.Dialect
|
|
|
|
tagIdentifier string
|
|
|
|
singularTable bool
|
|
|
|
logger Logger
|
|
|
|
logMode bool
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-25 18:31:56 +04:00
|
|
|
func Open(driver, source string) (db DB, err error) {
|
2013-10-28 08:52:06 +04:00
|
|
|
db.db, err = sql.Open(driver, source)
|
2013-11-15 15:43:45 +04:00
|
|
|
db.dialect = dialect.New(driver)
|
2013-11-16 07:36:30 +04:00
|
|
|
db.parent = &db
|
2013-10-25 14:04:48 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-28 06:09:44 +04:00
|
|
|
func (s *DB) SetPool(n int) {
|
2013-11-16 04:15:21 +04:00
|
|
|
if db, ok := s.db.(sqlDb); ok {
|
2013-11-11 09:16:08 +04:00
|
|
|
db.SetMaxIdleConns(n)
|
|
|
|
}
|
2013-10-28 06:09:44 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-11 11:48:31 +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-11-16 10:15:33 +04:00
|
|
|
s.logMode = b
|
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-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
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) First(out interface{}, where ...interface{}) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(out).where(where...).first().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 {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(out).where(where...).last().db
|
2013-10-31 13:31:00 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Find(out interface{}, where ...interface{}) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(out).where(where...).query().db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Attrs(attrs ...interface{}) *DB {
|
|
|
|
return s.clone().search.attrs(attrs...).db
|
2013-11-04 13:58:56 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Assign(attrs ...interface{}) *DB {
|
|
|
|
return s.clone().search.assign(attrs...).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 11:01:31 +04:00
|
|
|
if s.clone().First(out, where...).Error != nil {
|
|
|
|
return s.clone().do(out).where(where).initialize().db
|
2013-11-16 07:36:30 +04:00
|
|
|
} else {
|
|
|
|
if len(s.search.assignAttrs) > 0 {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(out).updateAttrs(s.search.assignAttrs).db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
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 11:01:31 +04:00
|
|
|
if s.clone().First(out, where...).Error != nil {
|
|
|
|
return s.clone().do(out).where(where...).initialize().db.Save(out)
|
2013-11-16 07:36:30 +04:00
|
|
|
} else {
|
|
|
|
if len(s.search.assignAttrs) > 0 {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(out).updateAttrs(s.search.assignAttrs).update().db
|
2013-11-16 07:36:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) Updates(values interface{}, ignore_protected_attrs ...bool) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(s.data).begin().updateAttrs(values, ignore_protected_attrs...).update().commit_or_rollback().db
|
2013-11-16 08:19:35 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Save(value interface{}) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(value).begin().save().commit_or_rollback().db
|
2013-10-29 14:02:28 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Delete(value interface{}) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.clone().do(value).begin().delete().commit_or_rollback().db
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Exec(sql string) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.do(nil).exec(sql).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()
|
|
|
|
c.data = value
|
|
|
|
return c
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) Related(value interface{}, foreign_keys ...string) *DB {
|
2013-11-16 11:01:31 +04:00
|
|
|
old_data := s.data
|
|
|
|
return s.do(value).related(old_data, foreign_keys...).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 {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.do(s.data).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 {
|
2013-11-16 11:01:31 +04:00
|
|
|
return s.do(s.data).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
|
|
|
// Debug
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Transactions
|
|
|
|
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 {
|
|
|
|
c.err(errors.New("Can't start a transaction."))
|
|
|
|
}
|
|
|
|
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-16 07:36:30 +04:00
|
|
|
// Migrations
|
|
|
|
func (s *DB) CreateTable(value interface{}) *DB {
|
|
|
|
s.do(value).createTable()
|
|
|
|
return s
|
2013-10-27 04:06:01 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) DropTable(value interface{}) *DB {
|
|
|
|
s.do(value).dropTable()
|
|
|
|
return s
|
2013-10-28 16:27:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) AutoMigrate(value interface{}) *DB {
|
|
|
|
s.do(value).autoMigrate()
|
|
|
|
return s
|
2013-10-26 11:47:30 +04:00
|
|
|
}
|
2013-11-01 11:01:39 +04:00
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) UpdateColumn(column string, typ string) *DB {
|
|
|
|
s.do(s.data).updateColumn(column, typ)
|
|
|
|
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 {
|
|
|
|
s.do(s.data).dropColumn(column)
|
|
|
|
return s
|
2013-11-07 05:09:54 +04:00
|
|
|
}
|
2013-11-11 09:16:08 +04:00
|
|
|
|
2013-11-16 07:36:30 +04:00
|
|
|
func (s *DB) AddIndex(column string, index_name ...string) *DB {
|
|
|
|
s.do(s.data).addIndex(column, index_name...)
|
|
|
|
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 {
|
|
|
|
s.do(s.data).removeIndex(column)
|
|
|
|
return s
|
2013-11-11 09:16:08 +04:00
|
|
|
}
|