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.

52 lines
1.0 KiB

  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. return &itemRepository{db: d.db, scopeID: scopeID}
  21. }
  22. func (d *Database) ItemsMultiScope(scopeIDs []int) database.ItemRepository {
  23. //TODO implement me
  24. panic("implement me")
  25. }
  26. func Connect(host string, port int, username, password, database string) (*Database, error) {
  27. db, err := sql.Open("mysql", fmt.Sprintf(
  28. "%s:%s@(%s:%d)/%s?parseTime=true", username, password, host, port, database,
  29. ))
  30. if err != nil {
  31. return nil, err
  32. }
  33. db.SetMaxOpenConns(10)
  34. db.SetMaxIdleConns(10)
  35. db.SetConnMaxIdleTime(time.Minute)
  36. err = db.Ping()
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &Database{db: db}, nil
  41. }