Allowing the use of an existing database/sql pool

This commit is contained in:
Carlos Alexandro Becker
2015-09-08 21:39:11 -03:00
committed by Carlos A Becker
parent 96c90e2150
commit b96a0c781e

View File

@@ -31,9 +31,19 @@ type Session struct {
ExpiresOn time.Time `db:"expires_on"` ExpiresOn time.Time `db:"expires_on"`
} }
// NewPGStore creates a new PGStore instance // NewPGStore creates a new PGStore instance and a new database/sql pool
func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore { func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore {
db, err := sql.Open("postgres", dbURL) db, err := sql.Open("postgres", dbURL)
if err != nil {
// Ignore and return nil
return nil
}
return NewPGStoreFromPool(db, keyPairs...)
}
// NewPGStoreFromPool creates a new PGStore instance from an existing
// database/sql pool
func NewPGStoreFromPool(db *sql.DB, keyPairs ...[]byte) *PGStore {
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}} dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
dbStore := &PGStore{ dbStore := &PGStore{
@@ -45,14 +55,9 @@ func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore {
DbMap: dbmap, DbMap: dbmap,
} }
if err != nil {
// Ignore and return nil
return nil
}
// Create table if it doesn't exist // Create table if it doesn't exist
dbmap.AddTableWithName(Session{}, "http_sessions").SetKeys(true, "Id") dbmap.AddTableWithName(Session{}, "http_sessions").SetKeys(true, "Id")
err = dbmap.CreateTablesIfNotExists() err := dbmap.CreateTablesIfNotExists()
if err != nil { if err != nil {
// Ignore and return nil // Ignore and return nil