advsql/store.go

48 lines
650 B
Go

package advsql
import (
"database/sql"
"fmt"
)
type Store struct {
db *sql.DB
}
func NewStoreMySQL(host string, port uint16, user, pass, db string) (*Store, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", user, pass, host, port, db)
conn, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
return NewStore(conn), nil
}
func NewStore(db *sql.DB) *Store {
return &Store{
db: db,
}
}
func (s *Store) Close() error {
stmts := []*sql.Stmt{}
var err error
for _, stmt := range stmts {
err = stmt.Close()
if err != nil {
return err
}
}
err = s.db.Close()
if err != nil {
return err
}
return nil
}