gorm/main.go

117 lines
2.6 KiB
Go
Raw Normal View History

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-11-06 17:43:41 +04:00
db *sql.DB
driver string
DebugMode bool
SingularTableName 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)
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-11-06 17:43:41 +04:00
func (s *DB) SingularTable(result bool) {
s.SingularTableName = result
}
2013-10-29 03:39:26 +04:00
func (s *DB) buildChain() *Chain {
2013-11-06 17:43:41 +04:00
return &Chain{db: s.db, driver: s.driver, debug: s.DebugMode, singularTableName: s.SingularTableName}
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-31 13:31:00 +04:00
func (s *DB) Not(querystring interface{}, args ...interface{}) *Chain {
return s.buildChain().Not(querystring, args...)
}
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
}
func (s *DB) Last(out interface{}, where ...interface{}) *Chain {
return s.buildChain().Last(out, where...)
}
func (s *DB) Attrs(attrs ...interface{}) *Chain {
return s.buildChain().Attrs(attrs...)
2013-10-30 11:21:58 +04:00
}
func (s *DB) Assign(attrs ...interface{}) *Chain {
return s.buildChain().Assign(attrs...)
2013-10-31 04:15:19 +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
}
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-29 15:33:26 +04:00
func (s *DB) Debug() *Chain {
return s.buildChain().Debug()
}
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
}
2013-11-01 11:01:39 +04:00
func (s *DB) DropTable(value interface{}) *Chain {
return s.buildChain().DropTable(value)
}