package mysql import ( "context" "database/sql" "fmt" "git.aiterp.net/stufflog3/stufflog3-api/internal/database" "git.aiterp.net/stufflog3/stufflog3-api/internal/database/mysql/mysqlcore" "time" _ "github.com/go-sql-driver/mysql" ) type Database struct { db *sql.DB q *mysqlcore.Queries } func (d *Database) Scopes() database.ScopeRepository { return &scopeRepository{db: d.db, q: d.q} } func (d *Database) Stats(scopeID int) database.StatRepository { return &statsRepository{ db: d.db, q: d.q, scopeID: scopeID, } } func (d *Database) Items(scopeID int) database.ItemRepository { return &itemRepository{db: d.db, q: d.q, scopeID: scopeID} } func (d *Database) Projects(scopeID int) database.ProjectRepository { return &projectRepository{ db: d.db, q: d.q, scopeID: scopeID, items: &itemRepository{db: d.db, q: d.q, scopeID: scopeID}, } } func (d *Database) ItemsMultiScope(scopeIDs []int) database.ItemMultiScopeRepository { //TODO implement me panic("implement me") } 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 := mysqlcore.Prepare(context.Background(), db) if err != nil { return nil, err } return &Database{db: db, q: q}, nil }