~mixxxdevelopers/mixxx/engine-control-refactor

« back to all changes in this revision

Viewing changes to mixxx/src/library/dao/trackdao.cpp

  • Committer: RJ Ryan
  • Date: 2013-06-04 00:41:29 UTC
  • mfrom: (2890.22.101 mixxx)
  • Revision ID: rryan@mixxx.org-20130604004129-8jjxkicsb3givu4a
MergingĀ fromĀ lp:mixxx.

Show diffs side-by-side

added added

removed removed

Lines of Context:
111
111
    return libraryTrackId;
112
112
}
113
113
 
 
114
QList<int> TrackDAO::getTrackIds(QList<QFileInfo> files) {
 
115
    QStringList pathList;
 
116
    FieldEscaper escaper(m_database);
 
117
    foreach (QFileInfo file, files) {
 
118
        pathList << escaper.escapeString(file.absoluteFilePath());
 
119
    }
 
120
    QSqlQuery query(m_database);
 
121
    query.prepare(QString("SELECT library.id FROM library INNER JOIN "
 
122
                          "track_locations ON library.location = track_locations.id "
 
123
                          "WHERE track_locations.location in (%1)").arg(pathList.join(",")));
 
124
 
 
125
    if (!query.exec()) {
 
126
        LOG_FAILED_QUERY(query);
 
127
    }
 
128
 
 
129
    QList<int> ids;
 
130
    while (query.next()) {
 
131
        ids.append(query.value(query.record().indexOf("id")).toInt());
 
132
    }
 
133
 
 
134
    return ids;
 
135
}
 
136
 
114
137
// Some code (eg. drag and drop) needs to just get a track's location, and it's
115
138
// not worth retrieving a whole TrackInfoObject.
116
139
QString TrackDAO::getTrackLocation(int trackId) {
118
141
             << QThread::currentThread() << m_database.connectionName();
119
142
    QSqlQuery query(m_database);
120
143
    QString trackLocation = "";
121
 
    query.prepare("SELECT track_locations.location FROM track_locations INNER JOIN library ON library.location = track_locations.id WHERE library.id=:id");
 
144
    query.prepare("SELECT track_locations.location FROM track_locations "
 
145
                  "INNER JOIN library ON library.location = track_locations.id "
 
146
                  "WHERE library.id=:id");
122
147
    query.bindValue(":id", trackId);
123
148
    if (!query.exec()) {
124
149
        LOG_FAILED_QUERY(query);
289
314
    m_pQueryLibraryInsert->bindValue(":header_parsed", pTrack->getHeaderParsed() ? 1 : 0);
290
315
 
291
316
    const QByteArray* pBeatsBlob = NULL;
292
 
    QString blobVersion = "";
 
317
    QString beatsVersion = "";
 
318
    QString beatsSubVersion = "";
293
319
    BeatsPointer pBeats = pTrack->getBeats();
294
320
    // Fall back on cached BPM
295
321
    double dBpm = pTrack->getBpm();
296
322
 
297
323
    if (pBeats) {
298
324
        pBeatsBlob = pBeats->toByteArray();
299
 
        blobVersion = pBeats->getVersion();
 
325
        beatsVersion = pBeats->getVersion();
 
326
        beatsSubVersion = pBeats->getSubVersion();
300
327
        dBpm = pBeats->getBpm();
301
328
    }
302
329
 
303
330
    m_pQueryLibraryInsert->bindValue(":bpm", dBpm);
304
 
    m_pQueryLibraryInsert->bindValue(":beats_version", blobVersion);
 
331
    m_pQueryLibraryInsert->bindValue(":beats_version", beatsVersion);
 
332
    m_pQueryLibraryInsert->bindValue(":beats_sub_version", beatsSubVersion);
305
333
    m_pQueryLibraryInsert->bindValue(":beats", pBeatsBlob ? *pBeatsBlob : QVariant(QVariant::ByteArray));
306
334
    delete pBeatsBlob;
307
335
}
498
526
    addTracksFinish();
499
527
}
500
528
 
501
 
QList<int> TrackDAO::addTracks(QList<QFileInfo> fileInfoList, bool unremove) {
 
529
QList<int> TrackDAO::addTracks(const QList<QFileInfo> &fileInfoList,
 
530
                               bool unremove) {
 
531
    QSqlQuery query(m_database);
502
532
    QList<int> trackIDs;
503
 
        TrackInfoObject* pTrack;
504
 
 
 
533
 
 
534
    // Create a temporary database of the paths of all the imported tracks.
 
535
    query.prepare("CREATE TEMP TABLE playlist_import "
 
536
                  "(location varchar (512))");
 
537
    if (!query.exec()) {
 
538
        LOG_FAILED_QUERY(query);
 
539
        return trackIDs;
 
540
    }
 
541
 
 
542
    // Prepare to add tracks to the database.
 
543
    // This also begins an SQL transaction.
505
544
    addTracksPrepare();
506
545
 
507
 
    //create the list of TrackInfoObjects from the fileInfoList
508
 
    QMutableListIterator<QFileInfo> it(fileInfoList);
509
 
    while (it.hasNext()) {
510
 
        QFileInfo& info = it.next();
511
 
        pTrack = new TrackInfoObject(info);
 
546
    // All all the track paths to this database.
 
547
    query.prepare("INSERT INTO playlist_import (location) "
 
548
                  "VALUES (:location)");
 
549
    foreach (const QFileInfo &rFileInfo, fileInfoList) {
 
550
        query.bindValue(":location", rFileInfo.absoluteFilePath());
 
551
        if (!query.exec()) {
 
552
            LOG_FAILED_QUERY(query);
 
553
        }
 
554
    }
 
555
 
 
556
    query.prepare("SELECT library.id FROM playlist_import, "
 
557
                  "track_locations, library WHERE library.location = track_locations.id "
 
558
                  "AND playlist_import.location = track_locations.location");
 
559
    if (!query.exec()) {
 
560
        LOG_FAILED_QUERY(query);
 
561
    }
 
562
    while (query.next()) {
 
563
        int trackId = query.value(query.record().indexOf("id")).toInt();
 
564
        trackIDs.append(trackId);
 
565
    }
 
566
 
 
567
    // If imported-playlist tracks are to be unremoved, do that for all playlist
 
568
    // tracks that were already in the database.
 
569
    if (unremove) {
 
570
        QStringList idStringList;
 
571
        foreach (int id, trackIDs) {
 
572
            idStringList.append(QString::number(id));
 
573
        }
 
574
        query.prepare(QString("UPDATE library SET mixxx_deleted=0 "
 
575
                              "WHERE id in (%1) AND mixxx_deleted=1")
 
576
                      .arg(idStringList.join (",")));
 
577
        if (!query.exec()) {
 
578
            LOG_FAILED_QUERY(query);
 
579
        }
 
580
    }
 
581
 
 
582
    // Any tracks not already in the database need to be added.
 
583
    query.prepare("SELECT location FROM playlist_import "
 
584
                  "WHERE NOT EXISTS (SELECT location FROM track_locations "
 
585
                  "WHERE playlist_import.location = track_locations.location)");
 
586
    if (!query.exec()) {
 
587
        LOG_FAILED_QUERY(query);
 
588
    }
 
589
    while (query.next()) {
 
590
        QString filePath = query.value(query.record().indexOf("location")).toString();
 
591
        TrackInfoObject* pTrack = new TrackInfoObject(QFileInfo(filePath));
512
592
        addTracksAdd(pTrack, unremove);
513
593
        int trackID = pTrack->getId();
514
594
        if (trackID >= 0) {
517
597
        delete pTrack;
518
598
    }
519
599
 
 
600
    // Drop the temporary playlist-import table.
 
601
    query.prepare("DROP TABLE IF EXISTS playlist_import");
 
602
    if (!query.exec()) {
 
603
        LOG_FAILED_QUERY(query);
 
604
    }
 
605
 
 
606
    // Finish adding tracks to the database.
520
607
    addTracksFinish();
 
608
 
 
609
    // Return the list of track IDs added to the database.
521
610
    return trackIDs;
522
611
}
523
612
 
534
623
        LOG_FAILED_QUERY(query);
535
624
    }
536
625
 
537
 
    // This is signal is received by beasetrackcache to remove the tracks from cache
 
626
    // This signal is received by basetrackcache to remove the tracks from cache
538
627
    QSet<int> tracksRemovedSet = QSet<int>::fromList(ids);
539
628
    emit(tracksRemoved(tracksRemovedSet));
540
629
}
745
834
            if (pBeats) {
746
835
                pTrack->setBeats(pBeats);
747
836
            } else {
748
 
                pTrack->setBpm(bpm.toFloat());
 
837
                pTrack->setBpm(bpm.toDouble());
749
838
            }
750
839
            pTrack->setBpmLock(has_bpm_lock);
751
840