Loggest thy stuff
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.

48 lines
941 B

  1. package mysql
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "git.aiterp.net/stufflog3/stufflog3-api/internal/database"
  6. "time"
  7. _ "github.com/go-sql-driver/mysql"
  8. )
  9. type Database struct {
  10. db *sql.DB
  11. }
  12. func (d *Database) Scopes() database.ScopeRepository {
  13. return &scopeRepository{db: d.db}
  14. }
  15. func (d *Database) Stats(scopeID int) database.StatRepository {
  16. //TODO implement me
  17. panic("implement me")
  18. }
  19. func (d *Database) Items(scopeID int) database.ItemRepository {
  20. //TODO implement me
  21. panic("implement me")
  22. }
  23. func Connect(host string, port int, username, password, database string) (*Database, error) {
  24. db, err := sql.Open("mysql", fmt.Sprintf(
  25. "%s:%s@(%s:%d)/%s?parseTime=true", username, password, host, port, database,
  26. ))
  27. if err != nil {
  28. return nil, err
  29. }
  30. db.SetMaxOpenConns(10)
  31. db.SetMaxIdleConns(10)
  32. db.SetConnMaxIdleTime(time.Minute)
  33. err = db.Ping()
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &Database{db: db}, nil
  38. }