Commit b4b0b34e authored by Max Kellermann's avatar Max Kellermann

database.h: eliminate db_*_song()

Use the C++ API.
parent fa3d1156
...@@ -131,28 +131,6 @@ db_get_directory(const char *name) ...@@ -131,28 +131,6 @@ db_get_directory(const char *name)
return music_root->LookupDirectory(name); return music_root->LookupDirectory(name);
} }
struct song *
db_get_song(const char *file)
{
assert(file != NULL);
g_debug("get song: %s", file);
if (db == NULL)
return NULL;
return db->GetSong(file, NULL);
}
void
db_return_song(struct song *song)
{
assert(db != nullptr);
assert(song != nullptr);
db->ReturnSong(song);
}
bool bool
db_save(GError **error_r) db_save(GError **error_r)
{ {
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include "Directory.hxx" #include "Directory.hxx"
extern "C" { extern "C" {
#include "database.h"
#include "client.h" #include "client.h"
#include "song.h" #include "song.h"
#include "tag.h" #include "tag.h"
......
...@@ -21,13 +21,13 @@ ...@@ -21,13 +21,13 @@
#include "InotifyUpdate.hxx" #include "InotifyUpdate.hxx"
#include "InotifySource.hxx" #include "InotifySource.hxx"
#include "InotifyQueue.hxx" #include "InotifyQueue.hxx"
#include "database.h"
#include "Mapper.hxx" #include "Mapper.hxx"
extern "C" { extern "C" {
#include "path.h" #include "path.h"
} }
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <sys/inotify.h> #include <sys/inotify.h>
#include <sys/stat.h> #include <sys/stat.h>
......
...@@ -27,12 +27,14 @@ ...@@ -27,12 +27,14 @@
extern "C" { extern "C" {
#include "playlist_internal.h" #include "playlist_internal.h"
#include "player_control.h" #include "player_control.h"
#include "database.h"
#include "uri.h" #include "uri.h"
#include "song.h" #include "song.h"
#include "idle.h" #include "idle.h"
} }
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
#include <stdlib.h> #include <stdlib.h>
static void playlist_increment_version(struct playlist *playlist) static void playlist_increment_version(struct playlist *playlist)
...@@ -103,37 +105,30 @@ playlist_append_song(struct playlist *playlist, struct player_control *pc, ...@@ -103,37 +105,30 @@ playlist_append_song(struct playlist *playlist, struct player_control *pc,
return PLAYLIST_RESULT_SUCCESS; return PLAYLIST_RESULT_SUCCESS;
} }
static struct song *
song_by_uri(const char *uri)
{
struct song *song;
song = db_get_song(uri);
if (song != NULL)
return song;
if (uri_has_scheme(uri))
return song_remote_new(uri);
return NULL;
}
enum playlist_result enum playlist_result
playlist_append_uri(struct playlist *playlist, struct player_control *pc, playlist_append_uri(struct playlist *playlist, struct player_control *pc,
const char *uri, unsigned *added_id) const char *uri, unsigned *added_id)
{ {
struct song *song;
g_debug("add to playlist: %s", uri); g_debug("add to playlist: %s", uri);
song = song_by_uri(uri); const Database *db = nullptr;
if (song == NULL) struct song *song;
return PLAYLIST_RESULT_NO_SUCH_SONG; if (uri_has_scheme(uri)) {
song = song_remote_new(uri);
} else {
db = GetDatabase(nullptr);
if (db == nullptr)
return PLAYLIST_RESULT_NO_SUCH_SONG;
song = db->GetSong(uri, nullptr);
if (song == nullptr)
return PLAYLIST_RESULT_NO_SUCH_SONG;
}
enum playlist_result result = enum playlist_result result =
playlist_append_song(playlist, pc, song, added_id); playlist_append_song(playlist, pc, song, added_id);
if (song_in_database(song)) if (db != nullptr)
db_return_song(song); db->ReturnSong(song);
return result; return result;
} }
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "config.h" #include "config.h"
#include "PlaylistFile.hxx" #include "PlaylistFile.hxx"
#include "PlaylistSave.hxx" #include "PlaylistSave.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "song.h" #include "song.h"
#include "io_error.h" #include "io_error.h"
#include "Mapper.hxx" #include "Mapper.hxx"
...@@ -28,7 +30,6 @@ extern "C" { ...@@ -28,7 +30,6 @@ extern "C" {
#include "text_file.h" #include "text_file.h"
#include "path.h" #include "path.h"
#include "uri.h" #include "uri.h"
#include "database.h"
#include "idle.h" #include "idle.h"
#include "conf.h" #include "conf.h"
} }
...@@ -423,16 +424,16 @@ spl_append_uri(const char *url, const char *utf8file, GError **error_r) ...@@ -423,16 +424,16 @@ spl_append_uri(const char *url, const char *utf8file, GError **error_r)
song_free(song); song_free(song);
return success; return success;
} else { } else {
struct song *song = db_get_song(url); const Database *db = GetDatabase(error_r);
if (song == NULL) { if (db == nullptr)
g_set_error_literal(error_r, playlist_quark(), return false;
PLAYLIST_RESULT_NO_SUCH_SONG,
"No such song"); song *song = db->GetSong(url, error_r);
if (song == nullptr)
return false; return false;
}
bool success = spl_append_song(utf8file, song, error_r); bool success = spl_append_song(utf8file, song, error_r);
db_return_song(song); db->ReturnSong(song);
return success; return success;
} }
} }
......
...@@ -24,13 +24,14 @@ ...@@ -24,13 +24,14 @@
#include "PlaylistSong.hxx" #include "PlaylistSong.hxx"
#include "QueuePrint.hxx" #include "QueuePrint.hxx"
#include "SongPrint.hxx" #include "SongPrint.hxx"
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
extern "C" { extern "C" {
#include "playlist_list.h" #include "playlist_list.h"
#include "playlist_plugin.h" #include "playlist_plugin.h"
#include "playlist.h" #include "playlist.h"
#include "song.h" #include "song.h"
#include "database.h"
#include "client.h" #include "client.h"
#include "input_stream.h" #include "input_stream.h"
} }
...@@ -112,6 +113,22 @@ playlist_print_changes_position(struct client *client, ...@@ -112,6 +113,22 @@ playlist_print_changes_position(struct client *client,
queue_print_changes_position(client, &playlist->queue, version); queue_print_changes_position(client, &playlist->queue, version);
} }
static bool
PrintSongDetails(struct client *client, const char *uri_utf8)
{
const Database *db = GetDatabase(nullptr);
if (db == nullptr)
return false;
song *song = db->GetSong(uri_utf8, nullptr);
if (song == nullptr)
return false;
song_print_info(client, song);
db->ReturnSong(song);
return true;
}
bool bool
spl_print(struct client *client, const char *name_utf8, bool detail, spl_print(struct client *client, const char *name_utf8, bool detail,
GError **error_r) GError **error_r)
...@@ -124,21 +141,9 @@ spl_print(struct client *client, const char *name_utf8, bool detail, ...@@ -124,21 +141,9 @@ spl_print(struct client *client, const char *name_utf8, bool detail,
} }
for (const auto &uri_utf8 : contents) { for (const auto &uri_utf8 : contents) {
bool wrote = false; if (!detail || !PrintSongDetails(client, uri_utf8.c_str()))
if (detail) {
struct song *song = db_get_song(uri_utf8.c_str());
if (song) {
song_print_info(client, song);
db_return_song(song);
wrote = true;
}
}
if (!wrote) {
client_printf(client, SONG_FILE "%s\n", client_printf(client, SONG_FILE "%s\n",
uri_utf8.c_str()); uri_utf8.c_str());
}
} }
return true; return true;
......
...@@ -20,9 +20,10 @@ ...@@ -20,9 +20,10 @@
#include "config.h" #include "config.h"
#include "PlaylistSong.hxx" #include "PlaylistSong.hxx"
#include "Mapper.hxx" #include "Mapper.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
extern "C" { extern "C" {
#include "database.h"
#include "song.h" #include "song.h"
#include "uri.h" #include "uri.h"
#include "path.h" #include "path.h"
...@@ -105,13 +106,17 @@ playlist_check_load_song(const struct song *song, const char *uri, bool secure) ...@@ -105,13 +106,17 @@ playlist_check_load_song(const struct song *song, const char *uri, bool secure)
if (dest == NULL) if (dest == NULL)
return NULL; return NULL;
} else { } else {
struct song *tmp = db_get_song(uri); const Database *db = GetDatabase(nullptr);
if (db == nullptr)
return nullptr;
struct song *tmp = db->GetSong(uri, nullptr);
if (tmp == NULL) if (tmp == NULL)
/* not found in database */ /* not found in database */
return NULL; return NULL;
dest = song_dup_detached(tmp); dest = song_dup_detached(tmp);
db_return_song(tmp); db->ReturnSong(tmp);
} }
return apply_song_metadata(dest, song); return apply_song_metadata(dest, song);
......
...@@ -21,11 +21,12 @@ ...@@ -21,11 +21,12 @@
#include "QueueSave.hxx" #include "QueueSave.hxx"
#include "song.h" #include "song.h"
#include "SongSave.hxx" #include "SongSave.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
extern "C" { extern "C" {
#include "queue.h" #include "queue.h"
#include "uri.h" #include "uri.h"
#include "database.h"
#include "text_file.h" #include "text_file.h"
} }
...@@ -69,20 +70,10 @@ queue_save(FILE *fp, const struct queue *queue) ...@@ -69,20 +70,10 @@ queue_save(FILE *fp, const struct queue *queue)
} }
} }
static struct song *
get_song(const char *uri)
{
return uri_has_scheme(uri)
? song_remote_new(uri)
: db_get_song(uri);
}
void void
queue_load_song(FILE *fp, GString *buffer, const char *line, queue_load_song(FILE *fp, GString *buffer, const char *line,
struct queue *queue) struct queue *queue)
{ {
struct song *song;
if (queue_is_full(queue)) if (queue_is_full(queue))
return; return;
...@@ -95,6 +86,9 @@ queue_load_song(FILE *fp, GString *buffer, const char *line, ...@@ -95,6 +86,9 @@ queue_load_song(FILE *fp, GString *buffer, const char *line,
return; return;
} }
const Database *db = nullptr;
struct song *song;
if (g_str_has_prefix(line, SONG_BEGIN)) { if (g_str_has_prefix(line, SONG_BEGIN)) {
const char *uri = line + sizeof(SONG_BEGIN) - 1; const char *uri = line + sizeof(SONG_BEGIN) - 1;
if (!uri_has_scheme(uri) && !g_path_is_absolute(uri)) if (!uri_has_scheme(uri) && !g_path_is_absolute(uri))
...@@ -115,15 +109,23 @@ queue_load_song(FILE *fp, GString *buffer, const char *line, ...@@ -115,15 +109,23 @@ queue_load_song(FILE *fp, GString *buffer, const char *line,
return; return;
} }
line = endptr + 1; const char *uri = endptr + 1;
song = get_song(line); if (uri_has_scheme(uri)) {
if (song == NULL) song = song_remote_new(uri);
return; } else {
db = GetDatabase(nullptr);
if (db == nullptr)
return;
song = db->GetSong(uri, nullptr);
if (song == nullptr)
return;
}
} }
queue_append(queue, song, priority); queue_append(queue, song, priority);
if (song_in_database(song)) if (db != nullptr)
db_return_song(song); db->ReturnSong(song);
} }
...@@ -21,9 +21,12 @@ ...@@ -21,9 +21,12 @@
#include "StickerCommands.hxx" #include "StickerCommands.hxx"
#include "SongPrint.hxx" #include "SongPrint.hxx"
#include "DatabaseLock.hxx" #include "DatabaseLock.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "SongSticker.hxx" #include "SongSticker.hxx"
#include "StickerPrint.hxx" #include "StickerPrint.hxx"
#include "StickerDatabase.hxx" #include "StickerDatabase.hxx"
#include "CommandError.hxx"
extern "C" { extern "C" {
#include "protocol/result.h" #include "protocol/result.h"
...@@ -51,20 +54,19 @@ sticker_song_find_print_cb(struct song *song, const char *value, ...@@ -51,20 +54,19 @@ sticker_song_find_print_cb(struct song *song, const char *value,
static enum command_return static enum command_return
handle_sticker_song(struct client *client, int argc, char *argv[]) handle_sticker_song(struct client *client, int argc, char *argv[])
{ {
GError *error = nullptr;
const Database *db = GetDatabase(&error);
if (db == nullptr)
return print_error(client, error);
/* get song song_id key */ /* get song song_id key */
if (argc == 5 && strcmp(argv[1], "get") == 0) { if (argc == 5 && strcmp(argv[1], "get") == 0) {
struct song *song; song *song = db->GetSong(argv[3], &error);
char *value; if (song == nullptr)
return print_error(client, error);
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
value = sticker_song_get_value(song, argv[4]); char *value = sticker_song_get_value(song, argv[4]);
db_return_song(song); db->ReturnSong(song);
if (value == NULL) { if (value == NULL) {
command_error(client, ACK_ERROR_NO_EXIST, command_error(client, ACK_ERROR_NO_EXIST,
"no such sticker"); "no such sticker");
...@@ -77,18 +79,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[]) ...@@ -77,18 +79,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
/* list song song_id */ /* list song song_id */
} else if (argc == 4 && strcmp(argv[1], "list") == 0) { } else if (argc == 4 && strcmp(argv[1], "list") == 0) {
struct song *song; song *song = db->GetSong(argv[3], &error);
struct sticker *sticker; if (song == nullptr)
return print_error(client, error);
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
sticker = sticker_song_get(song); sticker *sticker = sticker_song_get(song);
db_return_song(song); db->ReturnSong(song);
if (sticker) { if (sticker) {
sticker_print(client, sticker); sticker_print(client, sticker);
sticker_free(sticker); sticker_free(sticker);
...@@ -97,18 +93,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[]) ...@@ -97,18 +93,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_OK; return COMMAND_RETURN_OK;
/* set song song_id id key */ /* set song song_id id key */
} else if (argc == 6 && strcmp(argv[1], "set") == 0) { } else if (argc == 6 && strcmp(argv[1], "set") == 0) {
struct song *song; song *song = db->GetSong(argv[3], &error);
bool ret; if (song == nullptr)
return print_error(client, error);
song = db_get_song(argv[3]); bool ret = sticker_song_set_value(song, argv[4], argv[5]);
if (song == NULL) { db->ReturnSong(song);
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
ret = sticker_song_set_value(song, argv[4], argv[5]);
db_return_song(song);
if (!ret) { if (!ret) {
command_error(client, ACK_ERROR_SYSTEM, command_error(client, ACK_ERROR_SYSTEM,
"failed to set sticker value"); "failed to set sticker value");
...@@ -119,20 +109,14 @@ handle_sticker_song(struct client *client, int argc, char *argv[]) ...@@ -119,20 +109,14 @@ handle_sticker_song(struct client *client, int argc, char *argv[])
/* delete song song_id [key] */ /* delete song song_id [key] */
} else if ((argc == 4 || argc == 5) && } else if ((argc == 4 || argc == 5) &&
strcmp(argv[1], "delete") == 0) { strcmp(argv[1], "delete") == 0) {
struct song *song; song *song = db->GetSong(argv[3], &error);
bool ret; if (song == nullptr)
return print_error(client, error);
song = db_get_song(argv[3]);
if (song == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such song");
return COMMAND_RETURN_ERROR;
}
ret = argc == 4 bool ret = argc == 4
? sticker_song_delete(song) ? sticker_song_delete(song)
: sticker_song_delete_value(song, argv[4]); : sticker_song_delete_value(song, argv[4]);
db_return_song(song); db->ReturnSong(song);
if (!ret) { if (!ret) {
command_error(client, ACK_ERROR_SYSTEM, command_error(client, ACK_ERROR_SYSTEM,
"no such sticker"); "no such sticker");
......
...@@ -69,15 +69,6 @@ gcc_pure ...@@ -69,15 +69,6 @@ gcc_pure
struct directory * struct directory *
db_get_directory(const char *name); db_get_directory(const char *name);
gcc_nonnull(1)
gcc_pure
struct song *
db_get_song(const char *file);
gcc_nonnull(1)
void
db_return_song(struct song *song);
/** /**
* May only be used if db_is_simple() returns true. * May only be used if db_is_simple() returns true.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment