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

80 lines
2.1 KiB
C++
Raw Normal View History

2018-07-28 23:05:26 +02:00
#include "headers/sqlprofilemodel.h"
2018-07-22 16:47:55 +02:00
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,"
2018-08-02 12:50:55 +02:00
" `time` INTEGER NOT NULL, "
" `timestamp` INTEGER NOT NULL"
2018-07-22 16:47:55 +02:00
" );")) {
qFatal("Failed to query database: %s", qPrintable(query.lastError().text()));
}
}
SqlProfileModel::SqlProfileModel(QObject *parent) : QSqlTableModel(parent)
{
qDebug("ProfileModel constructor");
createTable();
setTable("profiles");
2018-08-03 15:18:23 +02:00
setEditStrategy(QSqlTableModel::OnManualSubmit);
2018-07-22 16:47:55 +02:00
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;
}
2018-08-03 15:18:23 +02:00
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);
}
2018-08-11 23:54:34 +02:00
void SqlProfileModel::remove(int row)
{
removeRows(row, 1);
submitAll();
}