database.h: eliminate db_*_song()

Use the C++ API.
This commit is contained in:
Max Kellermann 2013-01-03 00:30:15 +01:00
parent fa3d1156a6
commit b4b0b34e5a
10 changed files with 102 additions and 142 deletions

View File

@ -131,28 +131,6 @@ db_get_directory(const char *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
db_save(GError **error_r)
{

View File

@ -27,7 +27,6 @@
#include "Directory.hxx"
extern "C" {
#include "database.h"
#include "client.h"
#include "song.h"
#include "tag.h"

View File

@ -21,13 +21,13 @@
#include "InotifyUpdate.hxx"
#include "InotifySource.hxx"
#include "InotifyQueue.hxx"
#include "database.h"
#include "Mapper.hxx"
extern "C" {
#include "path.h"
}
#include <glib.h>
#include <assert.h>
#include <sys/inotify.h>
#include <sys/stat.h>

View File

@ -27,12 +27,14 @@
extern "C" {
#include "playlist_internal.h"
#include "player_control.h"
#include "database.h"
#include "uri.h"
#include "song.h"
#include "idle.h"
}
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
#include <stdlib.h>
static void playlist_increment_version(struct playlist *playlist)
@ -103,37 +105,30 @@ playlist_append_song(struct playlist *playlist, struct player_control *pc,
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
playlist_append_uri(struct playlist *playlist, struct player_control *pc,
const char *uri, unsigned *added_id)
{
struct song *song;
g_debug("add to playlist: %s", uri);
song = song_by_uri(uri);
if (song == NULL)
return PLAYLIST_RESULT_NO_SUCH_SONG;
const Database *db = nullptr;
struct song *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 =
playlist_append_song(playlist, pc, song, added_id);
if (song_in_database(song))
db_return_song(song);
if (db != nullptr)
db->ReturnSong(song);
return result;
}

View File

@ -20,6 +20,8 @@
#include "config.h"
#include "PlaylistFile.hxx"
#include "PlaylistSave.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "song.h"
#include "io_error.h"
#include "Mapper.hxx"
@ -28,7 +30,6 @@ extern "C" {
#include "text_file.h"
#include "path.h"
#include "uri.h"
#include "database.h"
#include "idle.h"
#include "conf.h"
}
@ -423,16 +424,16 @@ spl_append_uri(const char *url, const char *utf8file, GError **error_r)
song_free(song);
return success;
} else {
struct song *song = db_get_song(url);
if (song == NULL) {
g_set_error_literal(error_r, playlist_quark(),
PLAYLIST_RESULT_NO_SUCH_SONG,
"No such song");
const Database *db = GetDatabase(error_r);
if (db == nullptr)
return false;
song *song = db->GetSong(url, error_r);
if (song == nullptr)
return false;
}
bool success = spl_append_song(utf8file, song, error_r);
db_return_song(song);
db->ReturnSong(song);
return success;
}
}

View File

@ -24,13 +24,14 @@
#include "PlaylistSong.hxx"
#include "QueuePrint.hxx"
#include "SongPrint.hxx"
#include "DatabaseGlue.hxx"
#include "DatabasePlugin.hxx"
extern "C" {
#include "playlist_list.h"
#include "playlist_plugin.h"
#include "playlist.h"
#include "song.h"
#include "database.h"
#include "client.h"
#include "input_stream.h"
}
@ -112,6 +113,22 @@ playlist_print_changes_position(struct client *client,
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
spl_print(struct client *client, const char *name_utf8, bool detail,
GError **error_r)
@ -124,21 +141,9 @@ spl_print(struct client *client, const char *name_utf8, bool detail,
}
for (const auto &uri_utf8 : contents) {
bool wrote = false;
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) {
if (!detail || !PrintSongDetails(client, uri_utf8.c_str()))
client_printf(client, SONG_FILE "%s\n",
uri_utf8.c_str());
}
}
return true;

View File

@ -20,9 +20,10 @@
#include "config.h"
#include "PlaylistSong.hxx"
#include "Mapper.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
extern "C" {
#include "database.h"
#include "song.h"
#include "uri.h"
#include "path.h"
@ -105,13 +106,17 @@ playlist_check_load_song(const struct song *song, const char *uri, bool secure)
if (dest == NULL)
return NULL;
} 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)
/* not found in database */
return NULL;
dest = song_dup_detached(tmp);
db_return_song(tmp);
db->ReturnSong(tmp);
}
return apply_song_metadata(dest, song);

View File

@ -21,11 +21,12 @@
#include "QueueSave.hxx"
#include "song.h"
#include "SongSave.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
extern "C" {
#include "queue.h"
#include "uri.h"
#include "database.h"
#include "text_file.h"
}
@ -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
queue_load_song(FILE *fp, GString *buffer, const char *line,
struct queue *queue)
{
struct song *song;
if (queue_is_full(queue))
return;
@ -95,6 +86,9 @@ queue_load_song(FILE *fp, GString *buffer, const char *line,
return;
}
const Database *db = nullptr;
struct song *song;
if (g_str_has_prefix(line, SONG_BEGIN)) {
const char *uri = line + sizeof(SONG_BEGIN) - 1;
if (!uri_has_scheme(uri) && !g_path_is_absolute(uri))
@ -115,15 +109,23 @@ queue_load_song(FILE *fp, GString *buffer, const char *line,
return;
}
line = endptr + 1;
const char *uri = endptr + 1;
song = get_song(line);
if (song == NULL)
return;
if (uri_has_scheme(uri)) {
song = song_remote_new(uri);
} else {
db = GetDatabase(nullptr);
if (db == nullptr)
return;
song = db->GetSong(uri, nullptr);
if (song == nullptr)
return;
}
}
queue_append(queue, song, priority);
if (song_in_database(song))
db_return_song(song);
if (db != nullptr)
db->ReturnSong(song);
}

View File

@ -21,9 +21,12 @@
#include "StickerCommands.hxx"
#include "SongPrint.hxx"
#include "DatabaseLock.hxx"
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
#include "SongSticker.hxx"
#include "StickerPrint.hxx"
#include "StickerDatabase.hxx"
#include "CommandError.hxx"
extern "C" {
#include "protocol/result.h"
@ -51,20 +54,19 @@ sticker_song_find_print_cb(struct song *song, const char *value,
static enum command_return
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 */
if (argc == 5 && strcmp(argv[1], "get") == 0) {
struct song *song;
char *value;
song *song = db->GetSong(argv[3], &error);
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]);
db_return_song(song);
char *value = sticker_song_get_value(song, argv[4]);
db->ReturnSong(song);
if (value == NULL) {
command_error(client, ACK_ERROR_NO_EXIST,
"no such sticker");
@ -77,18 +79,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_OK;
/* list song song_id */
} else if (argc == 4 && strcmp(argv[1], "list") == 0) {
struct song *song;
struct sticker *sticker;
song *song = db->GetSong(argv[3], &error);
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);
db_return_song(song);
sticker *sticker = sticker_song_get(song);
db->ReturnSong(song);
if (sticker) {
sticker_print(client, sticker);
sticker_free(sticker);
@ -97,18 +93,12 @@ handle_sticker_song(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_OK;
/* set song song_id id key */
} else if (argc == 6 && strcmp(argv[1], "set") == 0) {
struct song *song;
bool ret;
song *song = db->GetSong(argv[3], &error);
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 = sticker_song_set_value(song, argv[4], argv[5]);
db_return_song(song);
bool ret = sticker_song_set_value(song, argv[4], argv[5]);
db->ReturnSong(song);
if (!ret) {
command_error(client, ACK_ERROR_SYSTEM,
"failed to set sticker value");
@ -119,20 +109,14 @@ handle_sticker_song(struct client *client, int argc, char *argv[])
/* delete song song_id [key] */
} else if ((argc == 4 || argc == 5) &&
strcmp(argv[1], "delete") == 0) {
struct song *song;
bool ret;
song *song = db->GetSong(argv[3], &error);
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_value(song, argv[4]);
db_return_song(song);
db->ReturnSong(song);
if (!ret) {
command_error(client, ACK_ERROR_SYSTEM,
"no such sticker");

View File

@ -69,15 +69,6 @@ gcc_pure
struct directory *
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.
*/