From fd3ce3b39a456b6968be00c86ca7f3414fcc1f2d Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 24 Apr 2014 10:54:27 +0800 Subject: [PATCH] Add method to close gorm db connection --- main.go | 8 ++++++-- main_test.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c98ca2b6..8fca8612 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,10 @@ func Open(driver, source string) (DB, error) { return db, err } +func (s *DB) Close() error { + return s.parent.db.(*sql.DB).Close() +} + func (s *DB) 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 { 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 } func (s *DB) Last(out interface{}, where ...interface{}) *DB { 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 } diff --git a/main_test.go b/main_test.go index e4016407..31e9d5f5 100644 --- a/main_test.go +++ b/main_test.go @@ -7,9 +7,9 @@ import ( "fmt" _ "github.com/go-sql-driver/mysql" + "github.com/jinzhu/gorm" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" - "github.com/jinzhu/gorm" "os" "reflect"