gorm/main.go

147 lines
3.0 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-11-14 13:35:17 +04:00
import "github.com/jinzhu/gorm/dialect"
2013-10-25 14:04:48 +04:00
2013-11-10 18:29:53 +04:00
var singularTableName bool
2013-11-13 20:03:31 +04:00
var tagIdentifier string
func init() {
tagIdentifier = "sql"
}
2013-11-10 18:29:53 +04:00
2013-10-25 14:04:48 +04:00
type DB struct {
2013-11-15 18:29:42 +04:00
db sql_common
dialect dialect.Dialect
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-10-25 14:04:48 +04:00
return
}
2013-10-28 06:09:44 +04:00
func (s *DB) SetPool(n int) {
2013-11-11 09:16:08 +04:00
if db, ok := s.db.(sql_db); ok {
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) {
tagIdentifier = str
}
2013-11-11 11:48:31 +04:00
func (s *DB) SetLogger(l Logger) {
s.logger = l
2013-11-11 05:05:14 +04:00
}
2013-11-11 07:11:49 +04:00
func (s *DB) LogMode(b bool) {
2013-11-15 18:29:42 +04:00
s.logMode = b
2013-11-11 07:11:49 +04:00
}
2013-11-06 17:43:41 +04:00
func (s *DB) SingularTable(result bool) {
2013-11-10 18:29:53 +04:00
singularTableName = result
2013-11-06 17:43:41 +04:00
}
2013-10-29 03:39:26 +04:00
func (s *DB) buildChain() *Chain {
2013-11-11 09:40:35 +04:00
return &Chain{db: s.db, d: s}
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-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)
}
2013-11-07 05:09:54 +04:00
func (s *DB) AutoMigrate(value interface{}) *Chain {
return s.buildChain().AutoMigrate(value)
}
2013-11-11 09:16:08 +04:00
2013-11-11 11:48:31 +04:00
func (s *DB) Debug() *Chain {
return s.buildChain().Debug()
}
2013-11-11 09:16:08 +04:00
func (s *DB) Begin() *Chain {
return s.buildChain().Begin()
}