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.

69 lines
1.5 KiB

3 years ago
  1. package mysql
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "git.aiterp.net/stufflog3/stufflog3-api/internal/database"
  7. "git.aiterp.net/stufflog3/stufflog3-api/internal/database/mysql/mysqlcore"
  8. "time"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. type Database struct {
  12. db *sql.DB
  13. q *mysqlcore.Queries
  14. }
  15. func (d *Database) Scopes() database.ScopeRepository {
  16. return &scopeRepository{db: d.db, q: d.q}
  17. }
  18. func (d *Database) Stats(scopeID int) database.StatRepository {
  19. return &statsRepository{
  20. db: d.db, q: d.q,
  21. scopeID: scopeID,
  22. }
  23. }
  24. func (d *Database) Items(scopeID int) database.ItemRepository {
  25. return &itemRepository{db: d.db, q: d.q, scopeID: scopeID}
  26. }
  27. func (d *Database) Projects(scopeID int) database.ProjectRepository {
  28. return &projectRepository{
  29. db: d.db, q: d.q, scopeID: scopeID,
  30. items: &itemRepository{db: d.db, q: d.q, scopeID: scopeID},
  31. }
  32. }
  33. func (d *Database) ItemsMultiScope(scopeIDs []int) database.ItemMultiScopeRepository {
  34. //TODO implement me
  35. panic("implement me")
  36. }
  37. func Connect(host string, port int, username, password, database string) (*Database, error) {
  38. db, err := sql.Open("mysql", fmt.Sprintf(
  39. "%s:%s@(%s:%d)/%s?parseTime=true", username, password, host, port, database,
  40. ))
  41. if err != nil {
  42. return nil, err
  43. }
  44. db.SetMaxOpenConns(10)
  45. db.SetMaxIdleConns(10)
  46. db.SetConnMaxIdleTime(time.Minute)
  47. err = db.Ping()
  48. if err != nil {
  49. return nil, err
  50. }
  51. q, err := mysqlcore.Prepare(context.Background(), db)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &Database{db: db, q: q}, nil
  56. }