diff --git a/README.md b/README.md index fa9f6c1d..043c217a 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,13 @@ db, err := Open("postgres", "user=gorm dbname=gorm sslmode=disable") // db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") // db, err = Open("sqlite3", "/tmp/gorm.db") +// Get database connection handle [*sql.DB](http://golang.org/pkg/database/sql/#DB) +d := db.DB() -// Set the maximum idle database connections -db.SetPool(100) - +// With it you could use package `database/sql`'s builtin methods +db.DB().SetMaxIdleConns(10) +db.DB().SetMaxOpenConns(100) +db.DB().Ping() // By default, table name is plural of struct type, you can use struct type as table name with: db.SingularTable(true) diff --git a/gorm_test.go b/gorm_test.go index 89da4998..cbcfbf14 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -91,7 +91,7 @@ func init() { // CREATE DATABASE 'gorm'; // GRANT ALL ON gorm.* TO 'gorm'@'localhost'; fmt.Println("testing mysql...") - db, err = Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True") + db, err = Open("mysql", "gorm:gorm@/gorim?charset=utf8&parseTime=True") case "sqlite": fmt.Println("testing sqlite3...") db, err = Open("sqlite3", "/tmp/gorm.db") @@ -101,12 +101,11 @@ func init() { } // db.SetLogger(log.New(os.Stdout, "\r\n", 0)) // db.LogMode(true) - if err != nil { panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err)) } - db.SetPool(10) + db.DB().SetMaxIdleConns(10) if err := db.DropTable(&User{}).Error; err != nil { fmt.Printf("Got error when try to delete table users, %+v\n", err) diff --git a/main.go b/main.go index 7f3304ba..57b31809 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package gorm import ( "database/sql" + "github.com/jinzhu/gorm/dialect" ) @@ -26,6 +27,10 @@ func Open(driver, source string) (db DB, err error) { return } +func (s *DB) DB() *sql.DB { + return s.db.(*sql.DB) +} + func (s *DB) SetPool(n int) { if db, ok := s.parent.db.(sqlDb); ok { db.SetMaxIdleConns(n)