You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
865 B

package mysql
import (
"context"
"database/sql"
"fmt"
"git.aiterp.net/rpdata2/rpdata2-take2/ports/mysql/mysqlgen"
"git.aiterp.net/rpdata2/rpdata2-take2/tag"
"time"
)
type Database struct {
db *sql.DB
q *mysqlgen.Queries
}
func (db *Database) Tags() tag.Repository {
return &tagRepository{
db: db.db,
q: db.q,
}
}
func Connect(host string, port int, username, password, database string) (*Database, error) {
db, err := sql.Open("mysql", fmt.Sprintf(
"%s:%s@(%s:%d)/%s?parseTime=true", username, password, host, port, database,
))
if err != nil {
return nil, err
}
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
db.SetConnMaxIdleTime(time.Minute)
err = db.Ping()
if err != nil {
return nil, err
}
q, err := mysqlgen.Prepare(context.Background(), db)
if err != nil {
return nil, err
}
return &Database{db: db, q: q}, nil
}