Ticket #1919: mfd.diff
| File mfd.diff, 20.8 KB (added by Colin Guthrie <mythtv@…>, 6 years ago) |
|---|
-
mfd/plugins/mmusic/mmusic.cpp
77 77 // This is a "magic" number signifying what we want to see 78 78 // 79 79 80 desired_database_version = "100 5";80 desired_database_version = "1006"; 81 81 82 82 // 83 83 // Initialize our container and set things up for a clean slate … … 526 526 527 527 MSqlQuery query(MSqlQuery::InitCon()); 528 528 529 query.exec("SELECT intid, filename FROM musicmetadata;");529 query.exec("SELECT song_id, filename FROM music_songs ;"); 530 530 531 531 if(query.isActive()) 532 532 { … … 544 544 545 545 ++count; 546 546 MSqlQuery delete_query(MSqlQuery::InitCon()); 547 delete_query.prepare("DELETE FROM music metadata WHERE intid = ?");547 delete_query.prepare("DELETE FROM music_songs WHERE song_id = ?"); 548 548 delete_query.bindValue(0, query.value(0).toUInt()); 549 549 delete_query.exec(); 550 550 log(QString("removed item %1 (\"%2\") from the database") … … 556 556 } 557 557 else 558 558 { 559 warning("something wrong with your music metadatatable");559 warning("something wrong with your music_songs table"); 560 560 } 561 561 562 562 if(count > 0) … … 638 638 639 639 MSqlQuery query(MSqlQuery::InitCon()); 640 640 641 query.exec("SELECT COUNT(filename) FROM music metadata;");641 query.exec("SELECT COUNT(filename) FROM music_songs;"); 642 642 643 643 if(!query.isActive()) 644 644 { 645 645 if(!sent_musicmetadata_table_warning) 646 646 { 647 warning("cannot get data from a table called music metadata");647 warning("cannot get data from a table called music_songs"); 648 648 sent_musicmetadata_table_warning = true; 649 649 } 650 650 return false; 651 651 652 652 } 653 653 654 query.exec("SELECT COUNT(playlist id) FROM musicplaylist");654 query.exec("SELECT COUNT(playlist_id) FROM music_saved_playlists "); 655 655 656 656 if(!query.isActive()) 657 657 { 658 658 if(!sent_playlist_table_warning) 659 659 { 660 warning("cannot get data from a table called musi splaylist");660 warning("cannot get data from a table called music_saved_playlists"); 661 661 sent_playlist_table_warning = true; 662 662 } 663 663 return false; … … 977 977 978 978 MSqlQuery query(MSqlQuery::InitCon()); 979 979 980 query.prepare("SELECT intid, artist, album, title, genre, " 981 "year, tracknum, length, rating, " 982 "lastplay, playcount, mythdigest, size, date_added, " 983 "date_modified, format, description, comment, " 984 "compilation, composer, disc_count, disc_number, " 985 "track_count, start_time, stop_time, eq_preset, " 986 "relative_volume, sample_rate, bpm " 987 "FROM musicmetadata WHERE filename = ? ;"); 980 query.prepare("SELECT music_songs.song_id, music_artists.artist_name, " 981 "music_albums.album_name, music_songs.name, " 982 "music_genres.genre, music_songs.year, " 983 "music_songs.track, music_songs.length, " 984 "music_songs.rating, music_songs.lastplay, " 985 "music_songs.numplays, music_songs.mythdigest," 986 "music_songs.size, music_songs.date_entered, " 987 "music_songs.date_modified, music_songs.format, " 988 "music_songs.description, music_songs.comment, " 989 "music_songs.compilation, music_comp_artists.artist_name AS composer, " 990 "music_songs.disc_count, music_songs.disc_number, " 991 "music_songs.track_count, music_songs.start_time, " 992 "music_songs.stop_time, music_songs.eq_preset, " 993 "music_songs.relative_volume, music_songs.bitrate, " 994 "music_songs.bpm " 995 "FROM music_songs " 996 "LEFT JOIN music_artists ON music_songs.artist_id=music_artists.artist_id " 997 "LEFT JOIN music_artists AS music_comp_artists ON music_songs.compilationartist_id=music_comp_artists.artist_id " 998 "LEFT JOIN music_albums ON music_songs.album_id=music_albums.album_id " 999 "LEFT JOIN music_genres ON music_songs.genre_id=music_genres.genre_id " 1000 "WHERE filename = ? ;"); 988 1001 989 1002 query.bindValue(0, sqlfilename.utf8()); 990 1003 991 1004 query.exec(); 992 1005 993 1006 if (query.isActive()) … … 1198 1211 1199 1212 MSqlQuery query(MSqlQuery::InitCon()); 1200 1213 1201 query.prepare("INSERT INTO music metadata(filename, mythdigest) "1214 query.prepare("INSERT INTO music_songs(filename, mythdigest) " 1202 1215 "values ( ? , ?)"); 1203 1216 1204 1217 query.bindValue(0, sqlfilename.utf8()); … … 1213 1226 return NULL; 1214 1227 } 1215 1228 1216 query.prepare("SELECT intid FROM musicmetadata"1229 query.prepare("SELECT song_id FROM music_songs " 1217 1230 "WHERE mythdigest = ? ;"); 1218 1231 query.bindValue(0, new_item->getMythDigest()); 1219 1232 query.exec(); … … 1322 1335 1323 1336 MSqlQuery query(MSqlQuery::InitCon()); 1324 1337 1325 query.prepare("UPDATE musicmetadata SET " 1326 "title = ? , " 1327 "artist = ? , " 1328 "album = ? , " 1329 "genre = ? , " 1338 1339 //Genrecheck 1340 //Albumcheck 1341 //Artist 1342 int ArtistID; 1343 query.prepare("SELECT music_artists.artist_id FROM music_artists " 1344 " WHERE (((music_artists.artist_name)=:ARTIST));"); 1345 query.bindValue(":ARTIST", an_item->getArtist().utf8()); 1346 query.exec(); 1347 // cout << query.executedQuery() << endl; 1348 if (query.size() > 0) 1349 { 1350 query.next(); 1351 ArtistID = query.value(0).toInt(); 1352 } 1353 else 1354 { 1355 query.prepare("INSERT INTO music_artists (artist_name) VALUES (:ARTIST);"); 1356 query.bindValue(":ARTIST", an_item->getArtist().utf8()); 1357 query.exec(); 1358 // cout << query.executedQuery() << endl; 1359 query.prepare("SELECT music_artists.artist_id FROM music_artists " 1360 " WHERE (((music_artists.artist_name)=:ARTIST));"); 1361 query.bindValue(":ARTIST", an_item->getArtist().utf8()); 1362 query.exec(); 1363 // cout << query.executedQuery() << endl; 1364 query.next(); 1365 ArtistID = query.value(0).toInt(); 1366 } 1367 1368 //Compilation Artist 1369 int CoArtistID; 1370 query.prepare("SELECT music_artists.artist_id FROM music_artists " 1371 " WHERE (((music_artists.artist_name)=:ARTIST));"); 1372 query.bindValue(":ARTIST", an_item->getComposer().utf8()); 1373 query.exec(); 1374 // cout << query.executedQuery() << endl; 1375 if (query.size() > 0) 1376 { 1377 query.next(); 1378 CoArtistID = query.value(0).toInt(); 1379 } 1380 else 1381 { 1382 query.prepare("INSERT INTO music_artists (artist_name) VALUES (:ARTIST);"); 1383 query.bindValue(":ARTIST", an_item->getComposer().utf8()); 1384 query.exec(); 1385 // cout << query.executedQuery() << endl; 1386 query.prepare("SELECT music_artists.artist_id FROM music_artists " 1387 " WHERE (((music_artists.artist_name)=:ARTIST));"); 1388 query.bindValue(":ARTIST", an_item->getComposer().utf8()); 1389 query.exec(); 1390 // cout << query.executedQuery() << endl; 1391 query.next(); 1392 CoArtistID = query.value(0).toInt(); 1393 } 1394 1395 //Album 1396 int AlbumID; 1397 query.prepare("SELECT music_albums.album_id FROM music_albums " 1398 " WHERE (((music_albums.album_name)=:ALBUM));"); 1399 query.bindValue(":ALBUM", an_item->getAlbum().utf8()); 1400 query.exec(); 1401 // cout << query.executedQuery() << endl; 1402 if (query.size() > 0) 1403 { 1404 query.next(); 1405 AlbumID = query.value(0).toInt(); 1406 } 1407 else 1408 { 1409 query.prepare("INSERT INTO music_albums (album_name) VALUES (:ALBUM);"); 1410 query.bindValue(":ALBUM", an_item->getAlbum().utf8()); 1411 query.exec(); 1412 // cout << query.executedQuery() << endl; 1413 1414 query.prepare("SELECT music_albums.album_id FROM music_albums " 1415 " WHERE (((music_albums.album_name)=:ALBUM));"); 1416 query.bindValue(":ALBUM", an_item->getAlbum().utf8()); 1417 query.exec(); 1418 // cout << query.executedQuery() << endl; 1419 query.next(); 1420 1421 AlbumID = query.value(0).toInt(); 1422 } 1423 1424 //Genres 1425 int GenreID; 1426 query.prepare("SELECT music_genres.genre_id FROM music_genres " 1427 " WHERE (((music_genres.genre)=:GENRE));"); 1428 query.bindValue(":GENRE", an_item->getGenre().utf8()); 1429 query.exec(); 1430 // cout << query.executedQuery() << endl; 1431 if (query.size() > 0) 1432 { 1433 query.next(); 1434 GenreID = query.value(0).toInt(); 1435 } 1436 else 1437 { 1438 query.prepare("INSERT INTO music_genres (genre) VALUES (:GENRE);"); 1439 query.bindValue(":GENRE", an_item->getGenre().utf8()); 1440 query.exec(); 1441 // cout << query.executedQuery() << endl; 1442 1443 query.prepare("SELECT music_genres.genre_id FROM music_genres " 1444 " WHERE (((music_genres.genre)=:GENRE));"); 1445 query.bindValue(":GENRE", an_item->getGenre().utf8()); 1446 query.exec(); 1447 // cout << query.executedQuery() << endl; 1448 query.next(); 1449 GenreID = query.value(0).toInt(); 1450 } 1451 1452 query.prepare("UPDATE music_songs SET " 1453 "name = ? , " 1454 "artist_id = ? , " 1455 "album_id = ? , " 1456 "genre_id = ? , " 1330 1457 "year = ? , " 1331 "track num= ? , "1458 "track = ? , " 1332 1459 "length = ? , " 1333 1460 "rating = ? , " 1334 1461 "lastplay = ? , " 1335 " playcount= ? , "1462 "numplays = ? , " 1336 1463 "mythdigest = ? , " 1337 1464 "size = ? , " 1338 "date_ added = ? , "1465 "date_entered = ? , " 1339 1466 "date_modified = ? , " 1340 1467 "format = ? , " 1341 1468 "description = ? , " 1342 1469 "comment = ? , " 1343 1470 "compilation = ? , " 1344 "comp oser= ? , "1471 "compilationartist_id = ? , " 1345 1472 "disc_count = ? , " 1346 1473 "disc_number = ? , " 1347 1474 "track_count = ? , " … … 1349 1476 "stop_time = ? , " 1350 1477 "eq_preset = ? , " 1351 1478 "relative_volume = ? , " 1352 " sample_rate = ? , "1479 "bitrate = ? , " 1353 1480 "bpm = ? " 1354 "WHERE intid = ? ;");1481 "WHERE song_id = ? ;"); 1355 1482 1356 1483 query.bindValue(0, an_item->getTitle().utf8()); 1357 query.bindValue(1, an_item->getArtist().utf8());1358 query.bindValue(2, an_item->getAlbum().utf8());1359 query.bindValue(3, an_item->getGenre().utf8());1484 query.bindValue(1, ArtistID); 1485 query.bindValue(2, AlbumID); 1486 query.bindValue(3, GenreID); 1360 1487 query.bindValue(4, an_item->getYear()); 1361 1488 query.bindValue(5, an_item->getTrack()); 1362 1489 query.bindValue(6, an_item->getLength()); … … 1371 1498 query.bindValue(15, an_item->getDescription().utf8()); 1372 1499 query.bindValue(16, an_item->getComment().utf8()); 1373 1500 query.bindValue(17, an_item->getCompilation()); 1374 query.bindValue(18, an_item->getComposer().utf8());1501 query.bindValue(18, CoArtistID); 1375 1502 query.bindValue(19, an_item->getDiscCount()); 1376 1503 query.bindValue(20, an_item->getDiscNumber()); 1377 1504 query.bindValue(21, an_item->getTrackCount()); … … 1467 1594 1468 1595 MSqlQuery query(MSqlQuery::InitCon()); 1469 1596 1470 query.prepare("SELECT name, songlist, playlistid FROM musicplaylist " 1471 "WHERE name != ? " 1472 "AND name != ? " 1473 "AND hostname = ? ;"); 1597 query.prepare("SELECT playlist_name, playlist_songs, playlist_id FROM music_saved_playlists " 1598 "WHERE playlist_name != ? " 1599 "AND playlist_name != ? ;"); 1474 1600 1475 1601 query.bindValue(0, "backup_playlist_storage"); 1476 1602 query.bindValue(1, "default_playlist_storage"); 1477 query.bindValue(2, hostname);1478 1603 1479 1604 query.exec(); 1480 1605 … … 1617 1742 1618 1743 MSqlQuery query(MSqlQuery::InitCon()); 1619 1744 1620 query.prepare("UPDATE musicplaylist SET songlist = ?, name = ? WHERE " 1621 "playlistid = ? ;"); 1745 int songcount = 0, playtime = 0, an_int; 1746 QStringList list = QStringList::split(",", db_song_list_string); 1747 QStringList::iterator it = list.begin(); 1748 for (; it != list.end(); it++) 1749 { 1750 an_int = QString(*it).toInt(); 1751 if (an_int != 0) 1752 { 1753 songcount++; 1754 query.prepare("SELECT length FROM music_songs WHERE song_id = :ID ;"); 1755 query.bindValue(":ID", an_int); 1756 query.exec(); 1757 query.next(); 1758 playtime += query.value(0).toInt(); 1759 } 1760 } 1622 1761 1762 query.prepare("UPDATE music_saved_playlists SET playlist_songs = ?, playlist_name = ? WHERE " 1763 "playlist_id = ?, songcount = ?, time = ? ;"); 1764 1623 1765 query.bindValue(0, db_song_list_string); 1624 1766 query.bindValue(1, a_playlist->getName().utf8()); 1625 1767 query.bindValue(2, a_playlist->getDbId()); 1768 query.bindValue(3, songcount); 1769 query.bindValue(4, playtime); 1626 1770 1627 1771 query.exec(); 1628 1772 … … 1665 1809 1666 1810 MSqlQuery query(MSqlQuery::InitCon()); 1667 1811 1668 query.prepare("INSERT INTO musicplaylist (name, hostname, songlist) values (?, ?, ?) ; "); 1812 int songcount = 0, playtime = 0, an_int; 1813 QStringList list = QStringList::split(",", db_song_list_string); 1814 QStringList::iterator it = list.begin(); 1815 for (; it != list.end(); it++) 1816 { 1817 an_int = QString(*it).toInt(); 1818 if (an_int != 0) 1819 { 1820 songcount++; 1821 query.prepare("SELECT length FROM music_songs WHERE song_id = :ID ;"); 1822 query.bindValue(":ID", an_int); 1823 query.exec(); 1824 query.next(); 1825 playtime += query.value(0).toInt(); 1826 } 1827 } 1669 1828 1829 query.prepare("INSERT INTO music_saved_playlists (playlist_name, hostname, playlist_songs, songcount, time) values (?, ?, ?, ?, ?) ; "); 1830 1670 1831 query.bindValue(0, a_playlist->getName().utf8()); 1671 1832 query.bindValue(1, hostname); 1672 1833 query.bindValue(2, db_song_list_string); 1834 query.bindValue(3, songcount); 1835 query.bindValue(4, playtime); 1673 1836 1674 1837 query.exec(); 1675 1838 … … 1681 1844 return; 1682 1845 } 1683 1846 1684 query.prepare("SELECT playlistid FROM musicplaylist WHERE name = ? AND hostname = ? AND songlist = ? ; "); 1847 if (a_playlist->getName() == "default_playlist_storage" || a_playlist->getName() == "backup_playlist_storage") 1848 { 1849 query.prepare("SELECT playlist_id FROM music_saved_playlists WHERE playlist_name = ? AND hostname = ? AND playlist_songs = ? ; "); 1850 query.bindValue(0, a_playlist->getName().utf8()); 1851 query.bindValue(1, hostname); 1852 query.bindValue(2, db_song_list_string); 1853 } 1854 else 1855 { 1856 query.prepare("SELECT playlist_id FROM music_saved_playlists WHERE playlist_name = ? AND playlist_songs = ? ; "); 1857 query.bindValue(0, a_playlist->getName().utf8()); 1858 query.bindValue(1, db_song_list_string); 1859 } 1685 1860 1686 query.bindValue(0, a_playlist->getName().utf8());1687 query.bindValue(1, hostname);1688 query.bindValue(2, db_song_list_string);1689 1861 1690 1862 query.exec(); 1691 1863 … … 1776 1948 1777 1949 MSqlQuery query(MSqlQuery::InitCon()); 1778 1950 1779 query.prepare("DELETE FROM music playlist WHERE playlistid = ? ; ");1951 query.prepare("DELETE FROM music_saved_playlists WHERE playlist_id = ? ; "); 1780 1952 1781 1953 query.bindValue(0, playlist_database_id); 1782 1954 -
mfd/dbcheck.cpp
9 9 #include "mythtv/mythcontext.h" 10 10 #include "mythtv/mythdbcon.h" 11 11 12 const QString currentDatabaseVersion = "100 5";12 const QString currentDatabaseVersion = "1006"; 13 13 14 14 static void UpdateDBVersionNumber(const QString &newnumber) 15 15 { … … 323 323 324 324 performActualUpdate(updates, "1005", dbver); 325 325 } 326 327 328 if (dbver == "1005") 329 { 330 const QString updates[] = { 331 "CREATE TABLE music_albums (" 332 " album_id int(11) unsigned NOT NULL auto_increment PRIMARY KEY," 333 " artist_id int(11) unsigned NOT NULL default '0'," 334 " album_name varchar(255) NOT NULL default ''," 335 " year smallint(6) NOT NULL default '0'," 336 " compilation tinyint(1) unsigned NOT NULL default '0'," 337 " INDEX idx_album_name(album_name)" 338 ");", 339 "CREATE TABLE music_artists (" 340 " artist_id int(11) unsigned NOT NULL auto_increment PRIMARY KEY," 341 " artist_name varchar(255) NOT NULL default ''," 342 " INDEX idx_artist_name(artist_name)" 343 ");", 344 "CREATE TABLE music_genres (" 345 " genre_id int(11) unsigned NOT NULL auto_increment PRIMARY KEY," 346 " genre varchar(25) NOT NULL default ''," 347 " INDEX idx_genre(genre)" 348 ");", 349 "CREATE TABLE music_playlists (" 350 " playlist_id int(11) unsigned NOT NULL auto_increment PRIMARY KEY," 351 " playlist_name varchar(255) NOT NULL default ''," 352 " playlist_songs text NOT NULL default ''," 353 " last_accessed timestamp NOT NULL," 354 " length int(11) unsigned NOT NULL default '0'," 355 " songcount smallint(8) unsigned NOT NULL default '0'," 356 " hostname VARCHAR(255) NOT NULL default ''" 357 ");", 358 "CREATE TABLE music_songs (" 359 " song_id int(11) unsigned NOT NULL auto_increment PRIMARY KEY," 360 " filename text NOT NULL default ''," 361 " name varchar(255) NOT NULL default ''," 362 " track smallint(6) unsigned NOT NULL default '0'," 363 " artist_id int(11) unsigned NOT NULL default '0'," 364 " album_id int(11) unsigned NOT NULL default '0'," 365 " genre_id int(11) unsigned NOT NULL default '0'," 366 " year smallint(6) NOT NULL default '0'," 367 " length int(11) unsigned NOT NULL default '0'," 368 " numplays int(11) unsigned NOT NULL default '0'," 369 " rating tinyint(4) unsigned NOT NULL default '0'," 370 " lastplay timestamp NOT NULL," 371 " date_entered datetime default NULL," 372 " date_modified datetime default NULL," 373 " format varchar(4) NOT NULL default '0'," 374 " mythdigest VARCHAR(255)," 375 " size BIGINT(20) unsigned," 376 " description VARCHAR(255)," 377 " comment VARCHAR(255)," 378 " disc_count SMALLINT(5) UNSIGNED DEFAULT '0'," 379 " disc_number SMALLINT(5) UNSIGNED DEFAULT '0'," 380 " track_count SMALLINT(5) UNSIGNED DEFAULT '0'," 381 " start_time INT(10) UNSIGNED DEFAULT '0'," 382 " stop_time INT(10) UNSIGNED," 383 " eq_preset VARCHAR(255)," 384 " relative_volume TINYINT DEFAULT '0'," 385 " bpm SMALLINT(5) UNSIGNED," 386 " INDEX idx_name(name)," 387 " INDEX idx_mythdigest(mythdigest)" 388 ");", 389 "CREATE TABLE music_stats (" 390 " num_artists smallint(5) unsigned NOT NULL default '0'," 391 " num_albums smallint(5) unsigned NOT NULL default '0'," 392 " num_songs mediumint(8) unsigned NOT NULL default '0'," 393 " num_genres tinyint(3) unsigned NOT NULL default '0'," 394 " total_time varchar(12) NOT NULL default '0'," 395 " total_size varchar(10) NOT NULL default '0'" 396 ");", 397 "RENAME TABLE smartplaylist TO music_smartplaylists;", 398 "RENAME TABLE smartplaylistitem TO music_smartplaylist_items;", 399 "RENAME TABLE smartplaylistcategory TO music_smartplaylist_categories;", 400 // Run necessary SQL to migrate the table structure 401 "CREATE TEMPORARY TABLE tmp_artists" 402 " SELECT DISTINCT artist FROM musicmetadata;", 403 "INSERT INTO tmp_artists" 404 " SELECT DISTINCT compilation_artist" 405 " FROM musicmetadata" 406 " WHERE compilation_artist<>artist;", 407 "INSERT INTO music_artists (artist_name) SELECT DISTINCT artist FROM tmp_artists;", 408 "INSERT INTO music_albums (artist_id, album_name, year, compilation) " 409 " SELECT artist_id, album, ROUND(AVG(year)) AS year, IF(SUM(compilation),1,0) AS compilation" 410 " FROM musicmetadata" 411 " LEFT JOIN music_artists ON compilation_artist=artist_name" 412 " GROUP BY artist_id, album;", 413 "INSERT INTO music_genres (genre) SELECT DISTINCT genre FROM musicmetadata;", 414 "INSERT INTO music_songs " 415 " (song_id, artist_id, album_id, genre_id, year, lastplay," 416 " date_entered, date_modified, name, track, length, size, numplays," 417 " rating, filename)" 418 " SELECT intid, ma.artist_id, mb.album_id, mg.genre_id, mmd.year, lastplay," 419 " date_added, date_modified, title, tracknum, length, IFNULL(size,0), playcount," 420 " rating, filename" 421 " FROM musicmetadata AS mmd" 422 " LEFT JOIN music_artists AS ma ON mmd.artist=ma.artist_name" 423 " LEFT JOIN music_artists AS mc ON mmd.compilation_artist=mc.artist_name" 424 " LEFT JOIN music_albums AS mb ON mmd.album=mb.album_name AND mc.artist_id=mb.artist_id" 425 " LEFT JOIN music_genres AS mg ON mmd.genre=mg.genre;", 426 "INSERT INTO music_playlists" 427 " (playlist_id,playlist_name,playlist_songs,hostname)" 428 " SELECT playlistid, name, songlist, hostname" 429 " FROM musicplaylist;", 430 // Set all playlists to be global by killing the hostname 431 "UPDATE music_playlists" 432 " SET hostname=''" 433 " WHERE playlist_name='default_playlist_storage'" 434 " OR playlist_name='backup_playlist_storage';", 435 //"DROP TABLE musicmetadata;", 436 //"DROP TABLE musicplaylist;", 437 //RENAME TABLE music_smartplaylists TO smartplaylist;RENAME TABLE music_smartplaylist_categories TO smartplaylistcategory;RENAME TABLE music_smartplaylist_items TO smartplaylistitem; DROP TABLE music_albums; DROP TABLE music_artists; DROP TABLE music_genres; DROP TABLE music_playlists; DROP TABLE music_songs; DROP TABLE music_stats;UPDATE settings SET data=1005 WHERE value='MusicDBSchemaVer'; 438 "" 439 }; 440 performActualUpdate(updates, "1006", dbver); 441 } 326 442 } 327 443
