2013-10-25 14:04:48 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-10-25 18:31:56 +04:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
2013-10-25 14:04:48 +04:00
|
|
|
|
|
|
|
type DB struct {
|
2013-10-26 13:28:52 +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-25 14:04:48 +04:00
|
|
|
db.Db, err = sql.Open(driver, source)
|
2013-10-26 13:28:52 +04:00
|
|
|
db.Driver = driver
|
2013-10-25 14:31:10 +04:00
|
|
|
// SetMaxIdleConns pools
|
2013-10-25 14:04:48 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-25 18:31:56 +04:00
|
|
|
func (s *DB) buildORM() *Orm {
|
2013-10-27 04:06:01 +04:00
|
|
|
return &Orm{db: s.Db, driver: s.Driver}
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Where(querystring interface{}, args ...interface{}) *Orm {
|
|
|
|
return s.buildORM().Where(querystring, args...)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) First(out interface{}) *Orm {
|
|
|
|
return s.buildORM().First(out)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Find(out interface{}) *Orm {
|
|
|
|
return s.buildORM().Find(out)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Limit(value interface{}) *Orm {
|
|
|
|
return s.buildORM().Limit(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Offset(value interface{}) *Orm {
|
|
|
|
return s.buildORM().Offset(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Order(value string, reorder ...bool) *Orm {
|
|
|
|
return s.buildORM().Order(value, reorder...)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Select(value interface{}) *Orm {
|
|
|
|
return s.buildORM().Select(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Save(value interface{}) *Orm {
|
|
|
|
return s.buildORM().Save(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Delete(value interface{}) *Orm {
|
|
|
|
return s.buildORM().Delete(value)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Exec(sql string) *Orm {
|
|
|
|
return s.buildORM().Exec(sql)
|
2013-10-25 14:04:48 +04:00
|
|
|
}
|
2013-10-26 11:47:30 +04:00
|
|
|
|
2013-10-27 04:06:01 +04:00
|
|
|
func (s *DB) Model(value interface{}) *Orm {
|
|
|
|
return s.buildORM().Model(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DB) CreateTable(value interface{}) *Orm {
|
|
|
|
return s.buildORM().CreateTable(value)
|
2013-10-26 11:47:30 +04:00
|
|
|
}
|