[musicdb] cosmetic: tidy up the song table definition and GetSongByFileName
authorJonathan Marshall <jmarshall@xbmc.org>
Sat, 30 Nov 2013 05:21:16 +0000 (18:21 +1300)
committerJonathan Marshall <jmarshall@xbmc.org>
Tue, 24 Dec 2013 00:48:49 +0000 (13:48 +1300)
xbmc/music/MusicDatabase.cpp

index d438094..3e2f6f4 100644 (file)
@@ -131,7 +131,16 @@ bool CMusicDatabase::CreateTables()
     CLog::Log(LOGINFO, "create path table");
     m_pDS->exec("CREATE TABLE path ( idPath integer primary key, strPath varchar(512), strHash text)\n");
     CLog::Log(LOGINFO, "create song table");
-    m_pDS->exec("CREATE TABLE song ( idSong integer primary key, idAlbum integer, idPath integer, strArtists text, strGenres text, strTitle varchar(512), iTrack integer, iDuration integer, iYear integer, dwFileNameCRC text, strFileName text, strMusicBrainzTrackID text, iTimesPlayed integer, iStartOffset integer, iEndOffset integer, idThumb integer, lastplayed varchar(20) default NULL, rating char default '0', comment text)\n");
+    m_pDS->exec("CREATE TABLE song ( idSong integer primary key, "
+                " idAlbum integer, idPath integer, "
+                " strArtists text, strGenres text, strTitle varchar(512), "
+                " iTrack integer, iDuration integer, iYear integer, "
+                " dwFileNameCRC text, "
+                " strFileName text, strMusicBrainzTrackID text, "
+                " iTimesPlayed integer, iStartOffset integer, iEndOffset integer, "
+                " idThumb integer, "
+                " lastplayed varchar(20) default NULL, "
+                " rating char default '0', comment text)");
     CLog::Log(LOGINFO, "create song_artist table");
     m_pDS->exec("CREATE TABLE song_artist ( idArtist integer, idSong integer, strJoinPhrase text, boolFeatured integer, iOrder integer )\n");
     CLog::Log(LOGINFO, "create song_genre table");
@@ -1171,47 +1180,33 @@ CArtist CMusicDatabase::GetArtistFromDataset(const dbiplus::sql_record* const re
 
 bool CMusicDatabase::GetSongByFileName(const CStdString& strFileName, CSong& song, int startOffset)
 {
-  try
+  song.Clear();
+  CURL url(strFileName);
+
+  if (url.GetProtocol()=="musicdb")
   {
-    song.Clear();
-    CURL url(strFileName);
+    CStdString strFile = URIUtils::GetFileName(strFileName);
+    URIUtils::RemoveExtension(strFile);
+    return GetSong(atol(strFile.c_str()), song);
+  }
 
-    if (url.GetProtocol()=="musicdb")
-    {
-      CStdString strFile = URIUtils::GetFileName(strFileName);
-      URIUtils::RemoveExtension(strFile);
-      return GetSong(atol(strFile.c_str()), song);
-    }
+  CStdString strPath = URIUtils::GetDirectory(strFileName);
+  URIUtils::AddSlashAtEnd(strPath);
 
-    if (NULL == m_pDB.get()) return false;
-    if (NULL == m_pDS.get()) return false;
+  if (NULL == m_pDB.get()) return false;
+  if (NULL == m_pDS.get()) return false;
 
-    CStdString strPath = URIUtils::GetDirectory(strFileName);
-    URIUtils::AddSlashAtEnd(strPath);
+  DWORD crc = ComputeCRC(strFileName);
 
-    DWORD crc = ComputeCRC(strFileName);
-    CStdString strSQL=PrepareSQL("select * from songview "
-                                "where dwFileNameCRC='%ul' and strPath='%s'"
-                                , crc,
-                                strPath.c_str());
-    if (startOffset)
-      strSQL += PrepareSQL(" AND iStartOffset=%i", startOffset);
+  CStdString strSQL = PrepareSQL("select idSong from songview "
+                                 "where dwFileNameCRC='%ul' and strPath='%s'",
+                                 crc, strPath.c_str());
+  if (startOffset)
+    strSQL += PrepareSQL(" AND iStartOffset=%i", startOffset);
 
-    if (!m_pDS->query(strSQL.c_str())) return false;
-    int iRowsFound = m_pDS->num_rows();
-    if (iRowsFound == 0)
-    {
-      m_pDS->close();
-      return false;
-    }
-    song = GetSongFromDataset();
-    m_pDS->close(); // cleanup recordset data
-    return true;
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "%s(%s) failed", __FUNCTION__, strFileName.c_str());
-  }
+  int idSong = (int)strtol(GetSingleValue(strSQL).c_str(), NULL, 10);
+  if (idSong > 0)
+    return GetSong(idSong, song);
 
   return false;
 }