Example #1
0
int DatabaseHelper::updatePaper(const Paper& paper)
{
    removePaper(paper.getId());
    return addPaper(paper);
}
Example #2
0
void DatabaseHelper::removePaper(const Paper& paper)
{
    removePaper(paper.getId());
}
Example #3
0
int DatabaseHelper::addPaper(const Paper& paper)
{
    int bookTitleId = getBookTitleId(paper.getBookTitle());
    if (bookTitleId == -1) {
        bookTitleId = addBookTitle(paper.getBookTitle());
    }

    vector<int> authorsId(paper.getAuthors().size());
    for (vector<int>::size_type i = 0; i < paper.getAuthors().size(); ++i) {
        const string& author = paper.getAuthors().at(i);
        int authorId = getAuthorId(author);
        if (authorId == -1) {
            authorId = addAuthor(author);
        }
        authorsId[i] = authorId;
    }

    vector<int> tagsId(paper.getTags().size());
    for (vector<int>::size_type i = 0; i < paper.getTags().size(); ++i) {
        const string& tag = paper.getTags().at(i);
        int tagId = getTagId(tag);
        if (tagId == -1) {
            tagId = addTag(tag);
        }
        tagsId[i] = tagId;
    }

    QSqlQuery query;
    if (paper.getId() <= 0) {
        query.prepare("INSERT INTO pl_paper(year, book_title_id, title, path, comment) "
                      "VALUES(:year, :book_title_id, :title, :path, :comment)");
        query.bindValue(":year", paper.getYear());
        query.bindValue(":book_title_id", bookTitleId);
        query.bindValue(":title", paper.getTitle().c_str());
        query.bindValue(":path", paper.getPath().c_str());
        query.bindValue(":comment", paper.getComment().c_str());
    } else {
        query.prepare("INSERT INTO pl_paper(paper_id, year, book_title_id, title, path, comment) "
                      "VALUES(:paper_id, :year, :book_title_id, :title, :path, :comment)");
        query.bindValue(":paper_id", paper.getId());
        query.bindValue(":year", paper.getYear());
        query.bindValue(":book_title_id", bookTitleId);
        query.bindValue(":title", paper.getTitle().c_str());
        query.bindValue(":path", paper.getPath().c_str());
        query.bindValue(":comment", paper.getComment().c_str());
    }
    query.exec();

    int paperId = query.lastInsertId().toInt();
    QVariantList paperIdList, authorIdList;
    for (int i = 0; i < static_cast<int>(authorsId.size()); ++i) {
        paperIdList.append(paperId);
        authorIdList.append(authorsId[i]);
    }

    query.prepare("INSERT INTO pl_paper2author(paper_id, author_id) "
                  "VALUES(:paper_id, :author_id)");
    query.bindValue(":paper_id", paperIdList);
    query.bindValue(":author_id", authorIdList);
    query.execBatch();

    paperIdList.clear();
    QVariantList tagIdList;
    for (int i = 0; i < static_cast<int>(tagsId.size()); ++i) {
        paperIdList.append(paperId);
        tagIdList.append(tagsId[i]);
    }

    query.prepare("INSERT INTO pl_paper2tag(paper_id, tag_id) "
                  "VALUES(:paper_id, :tag_id)");
    query.bindValue(":paper_id", paperIdList);
    query.bindValue(":tag_id", tagIdList);
    query.execBatch();

    return paperId;
}