Exemplo n.º 1
0
bool ContentNode::updateContentNode(QSqlDatabase& database,
                                    const ContentNode & node)
{
    QSqlQuery query(database);
    query.prepare ("update content set "
                   " name = ?, location = ?, title = ?, authors = ?, "
                   " description = ?, last_access = ?, publisher = ?, md5 = ?, "
                   " size = ?, rating = ?, read_time = ?, read_count = ?, "
                   " progress = ?, attributes = ? "
                   " where id = ?");

    query.addBindValue(node.name());
    query.addBindValue(node.location());
    query.addBindValue(node.title());
    query.addBindValue(node.authors());
    query.addBindValue(node.description());
    query.addBindValue(node.last_access());
    query.addBindValue(node.publisher());

    query.addBindValue(node.md5());
    query.addBindValue(node.size());
    query.addBindValue(node.rating());
    query.addBindValue(node.read_time());
    query.addBindValue(node.read_count());

    query.addBindValue(node.progress());
    query.addBindValue(node.attributes());

    query.addBindValue(node.id());

    return query.exec();
}
Exemplo n.º 2
0
bool ContentNode::createContentNode(QSqlDatabase& database,
                                    ContentNode & node)
{
    QSqlQuery query(database);
    query.prepare ("insert into content "
                   "(name, location, title, authors, description, "
                   "last_access, publisher, md5, "
                   "size, rating, read_time, read_count, progress, attributes ) "
                   " values "
                   "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

    query.addBindValue(node.name());
    query.addBindValue(node.location());
    query.addBindValue(node.title());
    query.addBindValue(node.authors());
    query.addBindValue(node.description());
    query.addBindValue(node.last_access());
    query.addBindValue(node.publisher());

    query.addBindValue(node.md5());
    query.addBindValue(node.size());
    query.addBindValue(node.rating());
    query.addBindValue(node.read_time());
    query.addBindValue(node.read_count());
    query.addBindValue(node.progress());
    query.addBindValue(node.attributes());

    if (query.exec())
    {
        node.id_ = query.lastInsertId().toInt();
        return true;
    }
    return false;
}
Exemplo n.º 3
0
/// TODO, need a better way to identify the file.
/// Lookup the file name.
/// Check the location.
/// File content hash. (Which part to hash and length).
bool ContentNode::getContentNode(QSqlDatabase & database,
                                 ContentNode &node)
{
    QSqlQuery query(database);
    query.prepare( "select id, title, authors, description, "
                   "last_access, publisher, md5, "
                   "rating, read_time, read_count, progress, attributes "
                   "from content where name = :name and location = :location "
                   "and size = :size" );
    query.bindValue(":name", node.name());
    query.bindValue(":location", node.location());
    query.bindValue(":size", node.size());

    if (query.exec() && query.next())
    {
        int index = 0;
        node.id_ = query.value(index++).toInt();
        node.title_ = query.value(index++).toString();
        node.mutable_authors() = query.value(index++).toString();
        node.mutable_description() = query.value(index++).toString();
        node.mutable_last_access() = query.value(index++).toString();
        node.mutable_publisher() = query.value(index++).toString();
        node.mutable_md5() = query.value(index++).toString();
        node.mutable_rating() = query.value(index++).toInt();
        node.mutable_read_time() = query.value(index++).toInt();
        node.mutable_read_count() = query.value(index++).toInt();
        node.mutable_progress() = query.value(index++).toString();
        node.mutable_attributes() = query.value(index++).toByteArray();
        return true;
    }
    return false;
}
Exemplo n.º 4
0
/// Update content by url.
bool ContentNode::updateContentNodeByUrl(QSqlDatabase& database,
        const ContentNode & node,
        const QString & url)
{
    // Check at first.
    ContentNode tmp;
    bool found = getContentNodeByUrl(database, url, tmp);

    if (!found)
    {
        QSqlQuery query(database);
        query.prepare ("INSERT into content  "
                       " (name, location, title, authors, "
                       " description, last_access, publisher, md5, "
                       " size, rating, read_time, read_count, "
                       " progress, attributes) values "
                       " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");

        query.addBindValue(node.name());
        query.addBindValue(node.location());
        query.addBindValue(node.title());
        query.addBindValue(node.authors());
        query.addBindValue(node.description());
        query.addBindValue(node.last_access());
        query.addBindValue(node.publisher());
        query.addBindValue(url);
        query.addBindValue(node.size());
        query.addBindValue(node.rating());
        query.addBindValue(node.read_time());
        query.addBindValue(node.read_count());
        query.addBindValue(node.progress());
        query.addBindValue(node.attributes());

        if (!query.exec())
        {
            qDebug() << query.lastError();
            return false;
        }
    }
    else
    {
        QSqlQuery query(database);
        query.prepare ("update content set "
                       " name = ?, location = ?, title = ?, authors = ?, "
                       " description = ?, last_access = ?, publisher = ?,  "
                       " size = ?, rating = ?, read_time = ?, read_count = ?, "
                       " progress = ?, attributes = ? "
                       " where md5 = ?");

        query.addBindValue(node.name());
        query.addBindValue(node.location());
        query.addBindValue(node.title());
        query.addBindValue(node.authors());
        query.addBindValue(node.description());
        query.addBindValue(node.last_access());
        query.addBindValue(node.publisher());

        query.addBindValue(node.size());
        query.addBindValue(node.rating());
        query.addBindValue(node.read_time());
        query.addBindValue(node.read_count());

        query.addBindValue(node.progress());
        query.addBindValue(node.attributes());

        query.addBindValue(url);

        if (!query.exec())
        {
            qDebug() << query.lastError();
            return false;
        }
    }
    return true;
}
Exemplo n.º 5
0
// Need to define the search policy.
// - Match the name, location and size.
// - Otherwise, we just ignore the request.
// - We can also use the hash code to match the file.
/// Query the content node in the specified database.
/// \param database The sqlitepp wrapper of database.
/// \param info The content node information returned by this function.
/// \param absolute_path The absolute path of the node.
/// \param create Create the content node automatically if not found.
/// \return This function returns true if the content node is already in
/// the database, otherwise it returns false.
bool
ContentNode::getContentNode(QSqlDatabase &database,
                            ContentNode & node,
                            const QString & absolute_path,
                            bool create)
{
    // Initialize query parameters and initialize all stuff
    // that does not rely on the database.
    QFileInfo info(absolute_path);

    node.mutable_size() = info.size();
    node.mutable_location() = info.path();
    node.mutable_name() = info.fileName();

    // Query by name, location and size.
    QSqlQuery query(database);
    query.prepare( "select id, title, authors, description, "
                   "last_access, publisher, md5, "
                   "rating, read_time, read_count, progress, attributes "
                   "from content where name = :name and location = :location "
                   "and size = :size" );
    query.bindValue(":name", node.name());
    query.bindValue(":location", node.location());
    query.bindValue(":size", node.size());

    if (query.exec() && query.next())
    {
        int index = 0;
        node.id_ = query.value(index++).toInt();
        node.title_ = query.value(index++).toString();
        node.mutable_authors() = query.value(index++).toString();
        node.mutable_description() = query.value(index++).toString();
        node.mutable_last_access() = query.value(index++).toString();
        node.mutable_publisher() = query.value(index++).toString();
        node.mutable_md5() = query.value(index++).toString();
        node.mutable_rating() = query.value(index++).toInt();
        node.mutable_read_time() = query.value(index++).toInt();
        node.mutable_read_count() = query.value(index++).toInt();
        node.mutable_progress() = query.value(index++).toString();
        node.mutable_attributes() = query.value(index++).toByteArray();
        return true;
    }

    // File has been moved, check the name and size.
    /*
    {
        statement st(database);
        st  << "select id, title, authors, description, "
            << "last_access, publisher, md5, "
            << "rating, read_time, read_count "
            << "from content where name = :name and size = :size",
            node.id_),
            into(info.mutable_title()),
            into(info.mutable_authors()),
            into(info.mutable_description()),
            into(info.mutable_last_access()),
            into(info.mutable_publisher()),
            into(info.mutable_md5()),
            into(info.mutable_rating()),
            into(info.mutable_read_time()),
            into(info.mutable_read_count()),
            use(info.name()),
            use(info.size());
        if (st.exec())
        {
            return true;
        }
    }

    // File name has been changed, check the md5 only.
    // Check if the md5 is ready or not. Make sure md5
    // is calculated only once.
    // TODO, maybe just ignore the node as it's not important.
    if (info.md5().empty() &&
        FileMd5Sum(absolute_path, info.mutable_md5()))
    {
        statement st(database);
        st  << "select id, title, authors, description, "
            << "last_access, publisher, "
            << "rating, read_time, read_count "
            << "from content where md5 = :md5",
            into(info.id_),
            into(info.mutable_title()),
            into(info.mutable_authors()),
            into(info.mutable_description()),
            into(info.mutable_last_access()),
            into(info.mutable_publisher()),
            into(info.mutable_rating()),
            into(info.mutable_read_time()),
            into(info.mutable_read_count()),
            use(info.md5());
        if (st.exec())
        {
            return true;
        }
    }
    */

    // Insert into the database. Should change the function name.
    if (create)
    {
        createContentNode(database, node);
    }
    return false;
}