2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-10-28 08:46:50 +04:00
|
|
|
import "database/sql"
|
2013-10-25 14:04:48 +04:00
|
|
|
|
|
|
|
type DB struct {
|
2013-10-28 08:52:06 +04:00
|
|
|
db *sql.DB
|
|
|
|
driver string
|
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)
|
|
|
|
db.driver = driver
|
2013-10-25 14:04:48 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-28 06:09:44 +04:00
|
|
|
func (s *DB) SetPool(n int) {
|
2013-10-28 08:52:06 +04:00
|
|
|
s.db.SetMaxIdleConns(n)
|
2013-10-28 06:09:44 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 03:39:26 +04:00
|
|
|
func (s *DB) buildChain() *Chain {
|
2013-10-28 08:52:06 +04:00
|
|
|
return &Chain{db: s.db, driver: s.driver}
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Where(querystring interface{}, args ...interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Where(querystring, args...)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 16:54:23 +04:00
|
|
|
func (s *DB) First(out interface{}, where ...interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().First(out, where...)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 14:02:28 +04:00
|
|
|
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *Chain {
|
|
|
|
return s.buildChain().FirstOrInit(out, where...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) FirstOrCreate(out interface{}, where ...interface{}) *Chain {
|
|
|
|
return s.buildChain().FirstOrCreate(out, where...)
|
|
|
|
}
|
|
|
|
|
2013-10-27 16:54:23 +04:00
|
|
|
func (s *DB) Find(out interface{}, where ...interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Find(out, where...)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Limit(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Limit(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Offset(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Offset(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Order(value string, reorder ...bool) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Order(value, reorder...)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Select(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Select(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Save(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Save(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Delete(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Delete(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-29 05:01:48 +04:00
|
|
|
func (s *DB) Unscoped() *Chain {
|
|
|
|
return s.buildChain().Unscoped()
|
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Exec(sql string) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Exec(sql)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
2013-10-26 11:47:30 +04:00
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) Model(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Model(value)
|
2013-10-27 04:06:01 +04:00
|
|
|
}
|
|
|
|
|
2013-10-28 16:27:25 +04:00
|
|
|
func (s *DB) Table(name string) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().Table(name)
|
2013-10-28 16:27:25 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 15:41:58 +04:00
|
|
|
func (s *DB) CreateTable(value interface{}) *Chain {
|
2013-10-29 03:39:26 +04:00
|
|
|
return s.buildChain().CreateTable(value)
|
2013-10-26 11:47:30 +04:00
|
|
|
}
|