- method db_get_option($name) {
- my $sth = $db->prepare_cached("select $name from options");
- $sth->execute;
- my @result = $sth->fetchrow_array;
- $sth->finish;
- undef $sth;
-
- return $result[0];
- }
-
- method db_set_option( $name, $value ) {
- my $sth = $db->prepare_cached("update options set $name = ?");
- $sth->execute($value);
- }
-
- method db_store_song($song, $artist, $album) {
- return unless length($song) and length($artist) and length($album);
-
- $db->prepare_cached(
- <<'SQL')->execute( $song, $artist, $album, $db_generation );
-INSERT INTO songs(path, artist, album, generation)
-VALUES($1, $2, $3, $4)
-ON CONFLICT ON CONSTRAINT songs_pkey DO
-UPDATE SET artist = $2
- , album = $3
- , generation = $4
-SQL
- $db->prepare_cached(<<'SQL')->execute( $artist, $album, $db_generation );
-INSERT INTO albums(artist, album, generation)
-VALUES($1, $2, $3)
-ON CONFLICT ON CONSTRAINT albums_pkey DO
-UPDATE SET generation = $3
-SQL
- $db->prepare_cached(<<'SQL')->execute( $artist, $db_generation );
-INSERT INTO artists(artist, generation)
-VALUES($1, $2)
-ON CONFLICT ON CONSTRAINT artists_pkey DO
-UPDATE SET generation = $2
-SQL
- }
-
- method db_remove_stale_entries {
- my $sth =
- $db->prepare_cached('DELETE FROM songs WHERE generation <> ?');
- $sth->execute($db_generation);
- $log->debug( sprintf( "Deleted %d stale songs", $sth->rows ) );
-
- $sth = $db->prepare_cached('DELETE FROM albums WHERE generation <> ?');
- $sth->execute($db_generation);
- $log->debug( sprintf( "Deleted %d stale albums", $sth->rows ) );
-
- $sth =
- $db->prepare_cached('DELETE FROM artists WHERE generation <> ?');
- $sth->execute($db_generation);
- $log->debug( sprintf( "Deleted %d stale artists", $sth->rows ) );
- }
-
- method db_note_song_qeued($item) {
- $db->prepare_cached(
- 'UPDATE songs SET last_queued=current_timestamp WHERE path=?')
- ->execute( $item->{song} );
- $db->prepare_cached(
- 'UPDATE artists SET last_queued=CURRENT_TIMESTAMP WHERE artist=?')
- ->execute( $item->{artist} );
- $db->prepare_cached(
- 'UPDATE albums SET last_queued=CURRENT_TIMESTAMP WHERE artist=? AND album=?'
- )->execute( $item->{artist}, $item->{album} );
- }
-