PushHistoryDAO.cpp Example File
pushCollector/10.0/pushCollector/src/dao/PushHistoryDAO.cpp
#include "PushHistoryDAO.hpp"
PushHistoryDAO::PushHistoryDAO()
{
}
PushHistoryDAO::~PushHistoryDAO()
{
}
bool PushHistoryDAO::createPushHistoryTable()
{
const QString query("CREATE TABLE IF NOT EXISTS pushhistory (rownum INTEGER PRIMARY KEY AUTOINCREMENT, itemid TEXT);");
QSqlQuery sqlQuery(query, SQLConnection());
const QSqlError err = sqlQuery.lastError();
if (err.isValid()) {
qWarning() << "Error executing SQL statement: " << query << ". ERROR: " << err.text();
return false;
}
return true;
}
bool PushHistoryDAO::add(const PushHistoryItem &item)
{
QSqlQuery sqlQuery(SQLConnection());
const QString query("INSERT INTO pushhistory (rownum, itemid) VALUES(:rownum, :itemid)");
sqlQuery.prepare(query);
sqlQuery.bindValue(":itemid", item.itemId());
sqlQuery.exec();
const QSqlError err = sqlQuery.lastError();
if (err.isValid()) {
qWarning() << "Error executing SQL statement: " << query << ". ERROR: " << err.text();
return false;
}
return true;
}
bool PushHistoryDAO::removeOldest()
{
const QString query("DELETE FROM pushhistory WHERE rownum = (SELECT min(rownum) FROM pushhistory);");
QSqlQuery sqlQuery(query, SQLConnection());
const QSqlError err = sqlQuery.lastError();
if (err.isValid()) {
qWarning() << "Error executing SQL statement: " << query << ". ERROR: " << err.text();
return false;
}
return true;
}
bool PushHistoryDAO::removeAll()
{
const QString query("DROP TABLE pushhistory;");
QSqlQuery sqlQuery(query, SQLConnection());
const QSqlError err = sqlQuery.lastError();
if (err.isValid()) {
qWarning() << "Error executing SQL statement: " << query << ". ERROR: " << err.text();
return false;
}
return true;
}
PushHistoryItem PushHistoryDAO::pushHistoryItem(const QString &pushHistoryItemId)
{
PushHistoryItem pushHistoryItem("");
QSqlQuery sqlQuery(SQLConnection());
const QString query("SELECT rownum, itemid FROM pushhistory WHERE itemid = :itemId;");
sqlQuery.prepare(query);
sqlQuery.bindValue(":itemid", pushHistoryItemId);
sqlQuery.exec();
const QSqlError err = sqlQuery.lastError();
if (err.isValid()) {
qWarning() << "Error executing SQL statement: " << query << ". ERROR: " << err.text();
} else if (sqlQuery.next()){
pushHistoryItem.setSeqNum(sqlQuery.value(0).toInt());
pushHistoryItem.setItemId(sqlQuery.value(1).toString());
}
return pushHistoryItem;
}
int PushHistoryDAO::pushHistoryCount()
{
int count = -1;
const QString query("SELECT COUNT(*) AS count FROM pushhistory;");
QSqlQuery sqlQuery(query, SQLConnection());
const QSqlError err = sqlQuery.lastError();
if (err.isValid()) {
qWarning() << "Error executing SQL statement: " << query << ". ERROR: " << err.text();
} else if (sqlQuery.next()) {
count = sqlQuery.value(0).toInt();
}
return count;
}