Skip to content

Commit

Permalink
storage: add functions to convert between SQL and Go
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 5, 2024
1 parent cd95db4 commit 8209872
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 10 deletions.
2 changes: 2 additions & 0 deletions migrations/0005_length_functions.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE OR REPLACE FUNCTION from_go_duration(IN d BIGINT) RETURNS INT return d DIV 1000000000;
CREATE OR REPLACE FUNCTION to_go_duration(IN s INT) RETURNS BIGINT return s * 1000000000;
1 change: 0 additions & 1 deletion migrations/0005_pending_length.up.sql

This file was deleted.

6 changes: 6 additions & 0 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func (s *Song) FillMetadata() {
} else if s.Title != "" && s.Metadata == "" {
s.Metadata = s.Title
}
s.Hash = NewSongHash(s.Metadata)
}

// HasTrack returns true if t != nil, can be used as Song.HasTrack to check if a track
Expand Down Expand Up @@ -820,6 +821,11 @@ type SubmissionStorage interface {
InsertSubmission(PendingSong) error
// GetSubmission returns a pending song by ID
GetSubmission(SubmissionID) (*PendingSong, error)
// RemoveSubmission removes a pending song by ID
RemoveSubmission(SubmissionID) error

// InsertSubmissionStatus updates post-pending data
InsertSubmissionStatus(PendingSong) error
}

type SubmissionStats struct {
Expand Down
46 changes: 41 additions & 5 deletions storage/mariadb/submissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ type SubmissionStorage struct {
handle handle
}

func (ss SubmissionStorage) InsertSubmissionStatus(pend radio.PendingSong) error {
panic("not implemented")
return nil
}

func (ss SubmissionStorage) RemoveSubmission(id radio.SubmissionID) error {
panic("not implemented")
return nil
}

// LastSubmissionTime implements radio.SubmissionStorage
func (ss SubmissionStorage) LastSubmissionTime(identifier string) (time.Time, error) {
const op errors.Op = "mariadb/SubmissionStorage.LastSubmissionTime"
Expand Down Expand Up @@ -100,9 +110,35 @@ func (ss SubmissionStorage) InsertSubmission(song radio.PendingSong) error {

query := `
INSERT INTO
pending (artist, track, album, path, comment, origname, submitter, submitted, replacement, bitrate, length, format, mode)
VALUES
(:artist, :title, :album, :filepath, :comment, :filename, :useridentifier, :submittedat, :replacementid, :bitrate, :length, :format, :encodingmode);
pending (
artist,
track,
album,
path,
comment,
origname,
submitter,
submitted,
replacement,
bitrate,
length,
format,
mode
) VALUES (
:artist,
:title,
:album,
:filepath,
:comment,
:filename,
:useridentifier,
:submittedat,
:replacementid,
:bitrate,
from_go_duration(:length),
:format,
:encodingmode
);
`

_, err := sqlx.NamedExec(ss.handle, query, song)
Expand All @@ -129,7 +165,7 @@ func (ss SubmissionStorage) All() ([]radio.PendingSong, error) {
submitted AS submittedat,
replacement AS replacementid,
bitrate,
length,
to_go_duration(length) AS length,
format,
mode AS encodingmode
FROM pending;`
Expand Down Expand Up @@ -164,7 +200,7 @@ func (ss SubmissionStorage) GetSubmission(id radio.SubmissionID) (*radio.Pending
submitted AS submittedat,
replacement AS replacementid,
bitrate,
length,
to_go_duration(length) AS length,
format,
mode AS encodingmode
FROM pending WHERE id=?;`
Expand Down
93 changes: 89 additions & 4 deletions storage/mariadb/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type databaseTrack struct {
// Hash is shared between tracks and esong
Hash radio.SongHash
// LastPlayed is shared between tracks and eplay
LastPlayed mysql.NullTime
LastPlayed sql.NullTime

// esong fields
ID sql.NullInt64
Expand All @@ -34,7 +34,7 @@ type databaseTrack struct {
Path sql.NullString
Tags sql.NullString
Priority sql.NullInt64
LastRequested mysql.NullTime
LastRequested sql.NullTime
Usable sql.NullInt64
Acceptor sql.NullString `db:"accepter"`
LastEditor sql.NullString
Expand Down Expand Up @@ -109,6 +109,31 @@ func (ss SongStorage) Create(metadata string) (*radio.Song, error) {
return song, nil
}

func (ss SongStorage) Update(song radio.Song) error {
const op errors.Op = "mariadb/SongStorage.Update"

if song.ID == 0 {
return errors.E(op, errors.InvalidArgument, "ID was zero", song)
}

var query = `
UPDATE tracks SET
hash=:hash,
len=from_go_duration(:length),
meta=:metadata,
hash_link=:hash
WHERE
id=:id;
`

_, err := sqlx.NamedExec(ss.handle, query, song)
if err != nil {
return errors.E(op, err)
}

return nil
}

// FromMetadata implements radio.SongStorage
func (ss SongStorage) FromMetadata(metadata string) (*radio.Song, error) {
const op errors.Op = "mariadb/SongStorage.FromMetadata"
Expand Down Expand Up @@ -512,6 +537,56 @@ func IsDuplicateKeyErr(err error) bool {
return mysqlError.Number == 1062
}

func (ts TrackStorage) Update(song radio.Song) error {
const op errors.Op = "mariadb/TrackStorage.Update"

if !song.HasTrack() {
return errors.E(op, errors.InvalidArgument, "nil DatabaseTrack", song)
}

if song.TrackID == 0 {
return errors.E(op, errors.InvalidArgument, "TrackID was zero", song)
}

handle, tx, err := requireTx(ts.handle)
if err != nil {
return errors.E(op, errors.TransactionBegin)
}
defer tx.Rollback()

ss := SongStorage{handle}
if err = ss.Update(song); err != nil {
return errors.E(op, err)
}

var query = `
UPDATE tracks SET
artist=:artist,
track=:title,
album=:album,
path=:filepath,
tags=:tags,
priority=:priority,
lastplayed=:lastplayed,
lastrequested=:lastrequested,
usable=:usable,
accepter=:acceptor,
lasteditor=:lasteditor,
hash=:hash,
requestcount=:requestcount,
need_reupload=:needreupload
WHERE
id=:trackid;
`

_, err = sqlx.NamedExec(handle, query, song)
if err != nil {
return errors.E(op, err)
}

return tx.Commit()
}

func (ts TrackStorage) Insert(song radio.Song) (radio.TrackID, error) {
const op errors.Op = "mariadb/TrackStorage.Insert"
// check if we have a database track at all
Expand Down Expand Up @@ -539,21 +614,31 @@ func (ts TrackStorage) Insert(song radio.Song) (radio.TrackID, error) {
album,
path,
tags,
priority,
lastplayed,
lastrequested,
usable,
accepter,
lasteditor,
hash
hash,
requestcount,
need_reupload
) VALUES (
0,
:artist,
:title,
:album,
:filepath,
:tags,
:priority,
:lastplayed,
:lastrequested,
:usable,
:acceptor,
:lasteditor,
:hash
:hash,
:requestcount,
:needreupload
);
`

Expand Down
24 changes: 24 additions & 0 deletions website/admin/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,27 @@ func (pf *PendingForm) Validate() bool {

return len(pf.Errors) == 0
}

func (pf *PendingForm) ToSong(user radio.User) radio.Song {
var song radio.Song

if pf.Status == radio.SubmissionAccepted {
song.DatabaseTrack = new(radio.DatabaseTrack)
song.Artist = pf.Artist
song.Title = pf.Title
song.Album = pf.Album
song.FillMetadata()
song.Tags = pf.Tags
song.FilePath = pf.FilePath
if pf.ReplacementID != 0 {
song.TrackID = pf.ReplacementID
song.NeedReplacement = false
}
song.Length = pf.Length
song.Usable = true
song.Acceptor = user.Username
song.LastEditor = user.Username
}

return song
}

0 comments on commit 8209872

Please sign in to comment.