Add method to close gorm db connection

This commit is contained in:
Jinzhu 2014-04-24 10:54:27 +08:00
parent 8e3181494d
commit fd3ce3b39a
2 changed files with 7 additions and 3 deletions

View File

@ -28,6 +28,10 @@ func Open(driver, source string) (DB, error) {
return db, err return db, err
} }
func (s *DB) Close() error {
return s.parent.db.(*sql.DB).Close()
}
func (s *DB) DB() *sql.DB { func (s *DB) DB() *sql.DB {
return s.db.(*sql.DB) return s.db.(*sql.DB)
} }
@ -131,13 +135,13 @@ func (s *DB) Assign(attrs ...interface{}) *DB {
func (s *DB) First(out interface{}, where ...interface{}) *DB { func (s *DB) First(out interface{}, where ...interface{}) *DB {
scope := s.clone().NewScope(out) scope := s.clone().NewScope(out)
scope.Search = scope.Search.clone().order(scope.TableName()+"."+scope.PrimaryKey()).limit(1) scope.Search = scope.Search.clone().order(scope.TableName() + "." + scope.PrimaryKey()).limit(1)
return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
} }
func (s *DB) Last(out interface{}, where ...interface{}) *DB { func (s *DB) Last(out interface{}, where ...interface{}) *DB {
scope := s.clone().NewScope(out) scope := s.clone().NewScope(out)
scope.Search = scope.Search.clone().order(scope.TableName()+"."+scope.PrimaryKey() + " DESC").limit(1) scope.Search = scope.Search.clone().order(scope.TableName() + "." + scope.PrimaryKey() + " DESC").limit(1)
return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db return scope.inlineCondition(where...).callCallbacks(s.parent.callback.queries).db
} }

View File

@ -7,9 +7,9 @@ import (
"fmt" "fmt"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/jinzhu/gorm"
"os" "os"
"reflect" "reflect"