#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include "sqlitemodel.h"
}
sqliteModel::~sqliteModel(){
}
int sqliteModel
::rowCount(const QModelIndex &parent
) const{ Q_UNUSED(parent);
return m_msgList.count();
qDebug()<< m_msgList.count();
}
QHash<int, QByteArray> sqliteModel::roleNames() const{
QHash<int, QByteArray> roleNames;
roleNames.insert(idRole, "id");
roleNames.insert(nameRole, "userName");
roleNames.insert(msgRole, "eventMessage");
roleNames.insert(dateRole, "dateTime");
qDebug()<< roleNames;
return roleNames;
}
if (index.row() < 0 || index.row() >= m_msgList.count()){
}
if(role == idRole){
userEventLogMsg msg = m_msgList[index.row()];
text = msg.id;
qDebug() << text;
}
else if(role == nameRole){
userEventLogMsg msg = m_msgList[index.row()];
text = msg.username;
qDebug() << text;
}
else if(role == msgRole){
userEventLogMsg msg = m_msgList[index.row()];
text = msg.eventmessage;
qDebug() << text;
}
if(role == dateRole){
userEventLogMsg msg = m_msgList[index.row()];
text = msg.datetime;
qDebug() << text;
}
return text;
}
void sqliteModel::addEvent(const userEventLogMsg &msg){
m_msgList.insert(0, msg);
endInsertRows();
}
void sqliteModel::dbConnect(){
if(!m_selectDataBase.isValid()){
qDebug() << "error in opening DB";
m_selectDataBase
= QSqlDatabase::addDatabase("QSQLITE",
"conn2");
m_selectDataBase.setDatabaseName("/home/amet/userLog.db");
}
else{
qDebug() <<"connected to DB" ;
}
m_selectDataBase.open();
}
void sqliteModel
::sqlSelect(QString tableName
){
dbConnect();
if(!m_selectDataBase.open()){
qDebug() << "database was closed";
m_selectDataBase.open();
}
else{
qDebug() << "database is open";
}
selectQuery.prepare("SELECT id, userName, eventMessage, dateTime FROM "+tableName+";");
selectQuery.addBindValue(tableName);
if(selectQuery.exec()){
qDebug()<<"sql statement exicuted fine";
}
else{
qDebug() << "Errors accured with sql statement";
qDebug() << selectQuery.lastError();
}
userEventLogMsg msg;
while (selectQuery.next()){
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toString();
addEvent(msg);
}
m_selectDataBase.close();
}
void sqliteModel
::createDailyTable(QString tableName
) {
dbConnect();
createTableQry.prepare("CREATE TABLE userlogevents"+date+"AS SELECT * FROM ? WHERE 0");
createTableQry.addBindValue(tableName);
createTableQry.exec();
m_selectDataBase.close();
}
void sqliteModel
::deleteDailyTable(QString tableName
) {
dbConnect();
//--- If the table is older than 30 days drop it---//
deleteTableQry.prepare("DROP TABLE ?");
deleteTableQry.addBindValue(tableName);
if(deleteTableQry.exec()){
qDebug()<<"sql statement exicuted fine";
}
else{
qDebug() << "Errors accured with sql statement";
qDebug() << deleteTableQry.lastError();
}
m_selectDataBase.close();
}
void sqliteModel
::searchDateText(const QString &dateText
) {
qDebug() << "c++: sqliteModel::searchDateText:" << dateText;
dbConnect();
if(!m_selectDataBase.open())
{
qDebug() << "database was closed";
m_selectDataBase.open();
}
else{
qDebug() << "database is open";
}
selectQuery.prepare("SELECT id, userName, eventMessage, dateTime FROM userlogevents WHERE dateText = ?");
selectQuery.addBindValue(dateText);
if(selectQuery.exec()){
qDebug()<<"sql statement exicuted fine";
}
else{
qDebug() << "Errors accured with sql statement";
qDebug() << selectQuery.lastError();
}
userEventLogMsg msg;
while (selectQuery.next()){
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toString();
addEvent(msg);
}
m_selectDataBase.close();
}
void sqliteModel
::searchUserNameText(const QString &userNameText
) {
qDebug() << "c++: sqliteModel::searchUserNameText:" << userNameText;
dbConnect();
if(!m_selectDataBase.open())
{
qDebug() << "database was closed";
m_selectDataBase.open();
}
else{
qDebug() << "database is open";
}
selectQuery.prepare("SELECT id, userName, eventMessage, dateTime FROM userlogevents WHERE userName = ?");
selectQuery.addBindValue(userNameText);
if(selectQuery.exec()){
qDebug()<<"sql statement exicuted fine";
}
else{
qDebug() << "Errors accured with sql statement";
qDebug() << selectQuery.lastError();
}
userEventLogMsg msg;
while (selectQuery.next()){
msg.id = selectQuery.value(0).toString();
msg.username = selectQuery.value(1).toString();
msg.eventmessage = selectQuery.value(2).toString();
msg.datetime = selectQuery.value(3).toString();
addEvent(msg);
}
m_selectDataBase.close();
}
Bookmarks