summaryrefslogtreecommitdiff
path: root/music.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-01-04 02:10:27 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2011-01-04 02:10:27 +0100
commitb73c8b20a034a4e8ac9ab8740453e35160c20833 (patch)
tree350302168da233ddc1eed20ee4bc4ca1fa2c9467 /music.cpp
parent8149e0487f6e658af71339f41a4f2f8413876cf0 (diff)
Implemented music::update(), needs some work to make queries work on different engines.
Diffstat (limited to 'music.cpp')
-rw-r--r--music.cpp44
1 files changed, 40 insertions, 4 deletions
diff --git a/music.cpp b/music.cpp
index 15ddbba..f74de9d 100644
--- a/music.cpp
+++ b/music.cpp
@@ -87,9 +87,49 @@ void music::begin_update(const std::string path) {
}
void music::update(const MusicDirectory& dir) {
+ soci::session sql(config::vm["audist.database"].as<std::string>());
+
BOOST_FOREACH(fs::path t, dir.tracks) {
std::cout << "track " << t << std::endl;
+ Tag::p tag = Tag::load(t.string());
+ BOOST_FOREACH(Tag::Fields::value_type& f, tag->fields) {
+ std::cout << boost::format(" %s: %s") % f.first % f.second << std::endl;
+ }
+
+ int artist_id = 0, album_id = 0, track_id = 0;
+
+ if(tag->has_field("artist")) {
+ sql << "SELECT id FROM artists WHERE name = :name", soci::use(tag->fields["artist"]), soci::into(artist_id);
+ if(!sql.got_data())
+ sql << "INSERT INTO artists (name) VALUES (:name) RETURNING id", soci::use(tag->fields["artist"]), soci::into(artist_id);
+ }
+
+ if(tag->has_field("album")) {
+ std::string query(boost::str(boost::format("SELECT id FROM albums WHERE %s AND name = :name") %
+ (artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL")));
+ sql << query, soci::use(tag->fields["album"]), soci::into(album_id);
+ if(!sql.got_data()) {
+ soci::indicator ind = (artist_id ? soci::i_ok : soci::i_null);
+ sql << "INSERT INTO albums (artist_id, name) VALUES (:artist_id, :name) RETURNING id",
+ soci::use(artist_id, ind), soci::use(tag->fields["album"]), soci::into(album_id);
+ }
+ }
+
+ if(tag->has_field("title")) {
+ std::string query(boost::str(boost::format("SELECT id FROM tracks WHERE %s AND %s AND name = :name") %
+ (artist_id ? boost::str(boost::format("artist_id = %d") % artist_id) : "artist_id IS NULL") %
+ (album_id ? boost::str(boost::format("album_id = %d") % album_id) : "album_id IS NULL")));
+ sql << query, soci::use(tag->fields["title"]), soci::into(track_id);
+ if(!sql.got_data()) {
+ soci::indicator artist_ind = (artist_id ? soci::i_ok : soci::i_null),
+ album_ind = (album_id ? soci::i_ok : soci::i_null);
+ sql << "INSERT INTO tracks (artist_id, album_id, name, file_name) VALUES (:artist_id, :album_id, :name, :file_name)",
+ soci::use(artist_id, artist_ind), soci::use(album_id, album_ind), soci::use(tag->fields["title"]), soci::use(t.string());
+ }
+ }
}
+ sql.close();
+
std::for_each(dir.directories.begin(), dir.directories.end(), update);
}
@@ -128,10 +168,6 @@ MusicDirectory::MusicDirectory(const fs::path root) {
void MusicTrack::render(HTTP::Connection::p req) {
req->add_header("content-type", "application/octet-stream");
- // tag test
- Tag *t = new ID3Tag(path.string());
- delete t;
-
if(req->args.count("decoder") && req->args.count("encoder")) {
DecoderFilter::p d = decoder::get_decoder(req->args["decoder"]);
EncoderFilter::p e = encoder::get_encoder(req->args["encoder"]);