This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
app/sources/sqlprofilemodel.cpp
2018-08-11 23:54:34 +02:00

79 lines
2.1 KiB
C++

#include "headers/sqlprofilemodel.h"
static void createTable()
{
if (QSqlDatabase::database().tables().contains(QStringLiteral("Contacts"))) {
// The table already exists; we don't need to do anything.
return;
}
QSqlQuery query;
//creat eth etable to store the profiles
if (!query.exec(
"CREATE TABLE IF NOT EXISTS `profiles` ( "
" `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,"
" `name` TEXT NOT NULL "
" );")) {
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
//create the table to store the times
if (!query.exec(
"CREATE TABLE IF NOT EXISTS `times` ("
" `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,"
" `profileid` INTEGER NOT NULL,"
" `time` INTEGER NOT NULL, "
" `timestamp` INTEGER NOT NULL"
" );")) {
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
}
SqlProfileModel::SqlProfileModel(QObject *parent) : QSqlTableModel(parent)
{
qDebug("ProfileModel constructor");
createTable();
setTable("profiles");
setEditStrategy(QSqlTableModel::OnManualSubmit);
select();
}
QVariant SqlProfileModel::data(const QModelIndex &index, int role) const
{
if (role < Qt::UserRole)
return QSqlTableModel::data(index, role);
const QSqlRecord sqlRecord = record(index.row());
return sqlRecord.value(role - Qt::UserRole);
}
QHash<int, QByteArray> SqlProfileModel::roleNames() const
{
QHash<int, QByteArray> names;
names[Qt::UserRole + 0] = "id";
names[Qt::UserRole + 1] = "name";
return names;
}
bool SqlProfileModel::append(QString name)
{
qDebug() << name;
QSqlRecord newRecord = record();
newRecord.setValue("name", name);
if (!insertRecord(rowCount(), newRecord)) {
qWarning() << "Failed to add profile:" << lastError().text();
return(false);
}
submitAll();
return(true);
}
void SqlProfileModel::remove(int row)
{
removeRows(row, 1);
submitAll();
}