GraphQL API and utilities for the rpdata project
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.

105 lines
2.5 KiB

  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "git.aiterp.net/rpdata/api/database/postgres/psqlcore"
  7. "git.aiterp.net/rpdata/api/internal/config"
  8. "git.aiterp.net/rpdata/api/repositories"
  9. "time"
  10. )
  11. func Connect(cfg config.Database) (*DB, error) {
  12. sslMode := "disable"
  13. if cfg.SSL {
  14. sslMode = "enable"
  15. }
  16. psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
  17. cfg.Host, cfg.Port, cfg.Username, cfg.Password, cfg.Db, sslMode,
  18. )
  19. timeout, cancel := context.WithTimeout(context.Background(), time.Second*15)
  20. defer cancel()
  21. db, err := sql.Open("postgres", psqlInfo)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if err := db.PingContext(timeout); err != nil {
  26. return nil, err
  27. }
  28. q := psqlcore.New(db)
  29. if err := q.EnsureCounter(timeout, "data_character_id"); err != nil {
  30. return nil, err
  31. }
  32. if err := q.EnsureCounter(timeout, "data_change_id"); err != nil {
  33. return nil, err
  34. }
  35. if err := q.EnsureCounter(timeout, "log_short_id"); err != nil {
  36. return nil, err
  37. }
  38. return &DB{
  39. db: db,
  40. insertWithIDs: cfg.RestoreIDs,
  41. }, nil
  42. }
  43. type DB struct {
  44. insertWithIDs bool
  45. db *sql.DB
  46. }
  47. func (d *DB) Changes() repositories.ChangeRepository {
  48. return &changeRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  49. }
  50. func (d *DB) Channels() repositories.ChannelRepository {
  51. return &channelRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  52. }
  53. func (d *DB) Characters() repositories.CharacterRepository {
  54. return &characterRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  55. }
  56. func (d *DB) Tags() repositories.TagRepository {
  57. return &tagRepository{db: d.db}
  58. }
  59. func (d *DB) Logs() repositories.LogRepository {
  60. return &logRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  61. }
  62. func (d *DB) Posts() repositories.PostRepository {
  63. return &postRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  64. }
  65. func (d *DB) Stories() repositories.StoryRepository {
  66. return &storyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  67. }
  68. func (d *DB) Chapters() repositories.ChapterRepository {
  69. return &chapterRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  70. }
  71. func (d *DB) Comments() repositories.CommentRepository {
  72. return &commentRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  73. }
  74. func (d *DB) Keys() repositories.KeyRepository {
  75. return &keyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
  76. }
  77. func (d *DB) Users() repositories.UserRepository {
  78. return &userRepository{db: d.db}
  79. }
  80. func (d *DB) Files() repositories.FileRepository {
  81. panic("implement me")
  82. }
  83. func (d *DB) Close(context.Context) error {
  84. return d.db.Close()
  85. }