forked from mirror/gorm
Use method DB() to get database connection handle
This commit is contained in:
parent
a0ac9992cd
commit
90a4581a7f
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
5
main.go
5
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)
|
||||
|
|
Loading…
Reference in New Issue