コード例 #1
0
Dbaudiofile::Dbaudiofile(int id)
  : artist(""), album(""), title(""),
    year(0), bitrate(0), length(0), rating(0), track(0), playlist_id(-1)
{
  db_id = id;

  GraphicalAudio *ga = get_class<GraphicalAudio>(dgettext("mms-audio", "Audio"));

  ga->db_mutex.enterMutex();

  SQLQuery *q = ga->db.query("Folders", ("SELECT filename, is_folder FROM %t WHERE id='" + conv::itos(id) + "'").c_str());
  if (q && q->numberOfTuples() > 0) {
    path = (*q)[0]["filename"];
    if ((*q)[0]["is_folder"] == "0")
      get_info_from_db(path, ga);
  } else
    assert(false);

  ga->db_mutex.leaveMutex();
}
コード例 #2
0
void Dbaudiofile::get_info_from_db(const string& path, GraphicalAudio *ga)
{
  SQLQuery *q = ga->db.query("Audio", ("SELECT * FROM %t WHERE filename='" + string_format::escape_db_string(path) + "'").c_str());
  if (q && q->numberOfTuples() > 0) {

    SQLRow &row = (*q)[0];

    title = row["Title"];
    bitrate = conv::atoi(row["Bitrate"]);
    length = conv::atoi(row["Length"]);
    track= conv::atoi(row["Track"]);

    SQLQuery *q2 = ga->db.query("Artist", ("SELECT name FROM %t WHERE id='" + row["Artist"] + "'").c_str());
    if (q2 && q2->numberOfTuples() > 0) {
      SQLRow &c_row = (*q2)[0];
      artist = c_row["name"];
    }
    delete q2;

    q2 = ga->db.query("Album", ("SELECT name FROM %t WHERE id='" + row["Album"] + "'").c_str());
    if (q2 && q2->numberOfTuples() > 0) {
      SQLRow &c_row = (*q2)[0];
      album = c_row["name"];
    }
    delete q2;

  } else {

    FileRef fileref(path.c_str(), true, AudioProperties::Accurate);

    if (!fileref.isNull() && fileref.tag()) {
      artist = fileref.tag()->artist().toCString(true);
      title = fileref.tag()->title().toCString(true);
      album = fileref.tag()->album().toCString(true);
      year = fileref.tag()->year();
      track = fileref.tag()->track();
    }

    if (!fileref.isNull() && fileref.audioProperties()) {
      length = fileref.audioProperties()->length();
      bitrate = fileref.audioProperties()->bitrate();
    }

    int artist_id = 0;

    SQLQuery *q2 = ga->db.query("Artist", ("SELECT id FROM %t WHERE name='" + string_format::escape_db_string(artist) + "'").c_str());
    if (q2 && q2->numberOfTuples() > 0) {
      SQLRow &row = (*q2)[0];
      artist_id = conv::atoi(row["id"]);
    }
    delete q2;

    if (artist_id == 0) {
      char *tmp = sqlite3_mprintf("INSERT INTO Artist VALUES(NULL, '%q', '%q')", artist.c_str(), string_format::lowercase(artist).c_str());
      ga->db.execute(tmp);
      sqlite3_free(tmp);

      q2 = ga->db.query("Artist", ("SELECT id FROM %t WHERE name='" + string_format::escape_db_string(artist) + "'").c_str());
      if (q2 && q2->numberOfTuples() > 0) {
	SQLRow &row = (*q2)[0];
	artist_id = conv::atoi(row["id"]);
      }
      delete q2;
    }

    int album_id = 0;

    q2 = ga->db.query("Album", ("SELECT id FROM %t WHERE name='" + string_format::escape_db_string(album) + "'").c_str());
    if (q2 && q2->numberOfTuples() > 0) {
      SQLRow &row = (*q2)[0];
      album_id = conv::atoi(row["id"]);
    }
    delete q2;

    if (album_id == 0) {
      char *tmp = sqlite3_mprintf("INSERT INTO Album VALUES(NULL, '%q', '%q')", album.c_str(), string_format::lowercase(album).c_str());
      ga->db.execute(tmp);
      sqlite3_free(tmp);

      q2 = ga->db.query("Album", ("SELECT id FROM %t WHERE name='" + string_format::escape_db_string(album) + "'").c_str());
      if (q2 && q2->numberOfTuples() > 0) {
	SQLRow &row = (*q2)[0];
	album_id = conv::atoi(row["id"]);
      }
      delete q2;
    }

    char *tmp = sqlite3_mprintf("INSERT INTO Audio VALUES(NULL, '%q', '%q', '%q', '%q', '%q', '%q', '%q', '%q')",
				conv::itos(artist_id).c_str(), conv::itos(album_id).c_str(),
				title.c_str(), string_format::lowercase(title).c_str(), path.c_str(),
				conv::ftos(bitrate).c_str(), conv::itos(length).c_str(),
				conv::itos(track).c_str());
    ga->db.execute(tmp);
    sqlite3_free(tmp);
  }

  delete q;

  fetched = true;

#ifdef use_imms
  rating = ga->imms->get_rating(path);
#endif
}