stufflog graphql server
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.

141 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package mysqldriver
  2. import (
  3. "context"
  4. "database/sql"
  5. "git.aiterp.net/stufflog/server/database/repositories"
  6. "github.com/jmoiron/sqlx"
  7. "github.com/pressly/goose"
  8. "strings"
  9. // Mysql Driver
  10. _ "github.com/go-sql-driver/mysql"
  11. )
  12. type DB struct {
  13. db *sqlx.DB
  14. activities *activityRepository
  15. issues *issueRepository
  16. issueTasks *issueTaskRepository
  17. issueItems *issueItemRepository
  18. items *itemRepository
  19. projects *projectRepository
  20. sessions *sessionRepository
  21. users *userRepository
  22. projectStatuses *projectStatusRepository
  23. logs *logRepository
  24. }
  25. func (db *DB) Activities() repositories.ActivityRepository {
  26. return db.activities
  27. }
  28. func (db *DB) Issues() repositories.IssueRepository {
  29. return db.issues
  30. }
  31. func (db *DB) IssueTasks() repositories.IssueTaskRepository {
  32. return db.issueTasks
  33. }
  34. func (db *DB) IssueItems() repositories.IssueItemRepository {
  35. return db.issueItems
  36. }
  37. func (db *DB) Items() repositories.ItemRepository {
  38. return db.items
  39. }
  40. func (db *DB) Projects() repositories.ProjectRepository {
  41. return db.projects
  42. }
  43. func (db *DB) Session() repositories.SessionRepository {
  44. return db.sessions
  45. }
  46. func (db *DB) Users() repositories.UserRepository {
  47. return db.users
  48. }
  49. func (db *DB) ProjectStatuses() repositories.ProjectStatusRepository {
  50. return db.projectStatuses
  51. }
  52. func (db *DB) Logs() repositories.LogRepository {
  53. return db.logs
  54. }
  55. func (db *DB) Migrate() error {
  56. err := goose.SetDialect("mysql")
  57. if err != nil {
  58. return err
  59. }
  60. return goose.Up(db.db.DB, "migrations/mysql")
  61. }
  62. func Open(connectionString string) (*DB, error) {
  63. // Ensure parseTime is true
  64. qpos := strings.LastIndexByte(connectionString, '?')
  65. if qpos != -1 {
  66. connectionString += "&parseTime=true"
  67. } else {
  68. connectionString += "?parseTime=true"
  69. }
  70. // Connect to the database
  71. db, err := sqlx.Connect("mysql", connectionString)
  72. if err != nil {
  73. return nil, err
  74. }
  75. // Test the connection
  76. err = db.Ping()
  77. if err != nil {
  78. return nil, err
  79. }
  80. // Setup repositories
  81. activities := &activityRepository{db: db}
  82. issues := &issueRepository{db: db}
  83. items := &itemRepository{db: db}
  84. projects := &projectRepository{db: db}
  85. users := &userRepository{db: db}
  86. sessions := &sessionRepository{db: db}
  87. projectStatuses := &projectStatusRepository{db: db}
  88. issueTasks := &issueTaskRepository{db: db}
  89. issueItems := &issueItemRepository{db: db}
  90. logs := &logRepository{db: db}
  91. return &DB{
  92. db: db,
  93. activities: activities,
  94. issues: issues,
  95. issueTasks: issueTasks,
  96. issueItems: issueItems,
  97. items: items,
  98. projects: projects,
  99. users: users,
  100. sessions: sessions,
  101. projectStatuses: projectStatuses,
  102. logs: logs,
  103. }, nil
  104. }
  105. func incCounter(ctx context.Context, tx *sqlx.Tx, kind, name string) (int, error) {
  106. value := 1
  107. err := tx.GetContext(ctx, &value, `
  108. SELECT value FROM counters WHERE kind=? AND name=? FOR UPDATE
  109. `, kind, name)
  110. if err != nil && err != sql.ErrNoRows {
  111. return -1, err
  112. }
  113. _, err = tx.ExecContext(ctx, "REPLACE INTO counters (kind, name, value) VALUES (?, ?, ?)", kind, name, value+1)
  114. if err != nil {
  115. return -1, err
  116. }
  117. return value, nil
  118. }