implemented new constructors for DB

This commit is contained in:
Timon Ringwald 2022-07-08 22:09:53 +02:00
parent 2662378374
commit 26975dc3a1

15
db.go
View File

@ -12,16 +12,19 @@ type Database struct {
closefuncs []func() error closefuncs []func() error
} }
func NewDatabase(host string, port uint16, user, pass, db string) (*Database, error) { func NewDatabase(conn *sql.DB) *Database {
return &Database{
db: conn,
closefuncs: make([]func() error, 0),
}
}
func NewMysqlDatabase(host string, port uint16, user, pass, db string) (*Database, error) {
conn, err := sql.Open("mysql", connString(host, port, user, pass, db)) conn, err := sql.Open("mysql", connString(host, port, user, pass, db))
if err != nil { if err != nil {
return nil, adverr.Wrap("could not connect to database", err) return nil, adverr.Wrap("could not connect to database", err)
} }
return NewDatabase(conn), nil
return &Database{
db: conn,
closefuncs: make([]func() error, 0),
}, nil
} }
func (db *Database) prepare(query string) (*sql.Stmt, error) { func (db *Database) prepare(query string) (*sql.Stmt, error) {