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
105 lines
2.5 KiB
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"git.aiterp.net/rpdata/api/database/postgres/psqlcore"
|
|
"git.aiterp.net/rpdata/api/internal/config"
|
|
"git.aiterp.net/rpdata/api/repositories"
|
|
"time"
|
|
)
|
|
|
|
func Connect(cfg config.Database) (*DB, error) {
|
|
sslMode := "disable"
|
|
if cfg.SSL {
|
|
sslMode = "enable"
|
|
}
|
|
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
|
|
cfg.Host, cfg.Port, cfg.Username, cfg.Password, cfg.Db, sslMode,
|
|
)
|
|
|
|
timeout, cancel := context.WithTimeout(context.Background(), time.Second*15)
|
|
defer cancel()
|
|
|
|
db, err := sql.Open("postgres", psqlInfo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := db.PingContext(timeout); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := psqlcore.New(db)
|
|
if err := q.EnsureCounter(timeout, "data_character_id"); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := q.EnsureCounter(timeout, "data_change_id"); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := q.EnsureCounter(timeout, "log_short_id"); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DB{
|
|
db: db,
|
|
insertWithIDs: cfg.RestoreIDs,
|
|
}, nil
|
|
}
|
|
|
|
type DB struct {
|
|
insertWithIDs bool
|
|
db *sql.DB
|
|
}
|
|
|
|
func (d *DB) Changes() repositories.ChangeRepository {
|
|
return &changeRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Channels() repositories.ChannelRepository {
|
|
return &channelRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Characters() repositories.CharacterRepository {
|
|
return &characterRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Tags() repositories.TagRepository {
|
|
return &tagRepository{db: d.db}
|
|
}
|
|
|
|
func (d *DB) Logs() repositories.LogRepository {
|
|
return &logRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Posts() repositories.PostRepository {
|
|
return &postRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Stories() repositories.StoryRepository {
|
|
return &storyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Chapters() repositories.ChapterRepository {
|
|
return &chapterRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Comments() repositories.CommentRepository {
|
|
return &commentRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Keys() repositories.KeyRepository {
|
|
return &keyRepository{insertWithIDs: d.insertWithIDs, db: d.db}
|
|
}
|
|
|
|
func (d *DB) Users() repositories.UserRepository {
|
|
return &userRepository{db: d.db}
|
|
}
|
|
|
|
func (d *DB) Files() repositories.FileRepository {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (d *DB) Close(context.Context) error {
|
|
return d.db.Close()
|
|
}
|