forked from ondra/colnod-connector
97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
#include "databasemanager.h"
|
|
|
|
DatabaseManager::DatabaseManager(QString host, QString dbName, QString user, QString password)
|
|
{
|
|
|
|
db = QSqlDatabase::addDatabase("QPSQL");
|
|
|
|
db.setDatabaseName(dbName);
|
|
db.setHostName(host);
|
|
db.setUserName(user);
|
|
db.setPassword(password);
|
|
opened = db.open();
|
|
if (!opened)
|
|
{
|
|
qDebug() << "Opening error" + db.lastError().text();
|
|
}
|
|
query = QSqlQuery("query", db);
|
|
}
|
|
|
|
bool DatabaseManager::saveSubject(Subject* subject, QString table)
|
|
{
|
|
QString id = subject->idToStringWithoutBraces();
|
|
if (checkIfExists(id, table))
|
|
return false;
|
|
query.prepare(QString("INSERT INTO %1 (id, name, last_update) "
|
|
"VALUES (:id, :name, :last_update)").arg(table));
|
|
query.bindValue(":id", id);
|
|
query.bindValue(":name", subject->getName());
|
|
query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(subject->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
|
query.exec();
|
|
return true;
|
|
}
|
|
|
|
bool DatabaseManager::saveToken(Token* token, QString table)
|
|
{
|
|
QString id = token->idToStringWithoutBraces();
|
|
if (checkIfExists(id, table))
|
|
return false;
|
|
query.prepare(QString("INSERT INTO %1 (id, last_update, token_data, subject_id) "
|
|
"VALUES (:id, :last_update, :data, :subject_id)").arg(table));
|
|
query.bindValue(":id", id);
|
|
query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(token->getLastUpdate()).toString("yyyy-MM-dd hh:mm:ss.zzz"));
|
|
query.bindValue(":data", token->getTokenData());
|
|
query.bindValue(":subject_id", token->subjectIdToStringWithoutBraces());
|
|
query.exec();
|
|
return true;
|
|
}
|
|
|
|
void DatabaseManager::saveSubjectAttribute(Subject *subject, QString table)
|
|
{
|
|
auto attributes = subject->getAtr();
|
|
for (auto attribute : attributes){
|
|
query.prepare(QString("INSERT INTO %1 (id, attribute_key, attribute_value, subject_id)"
|
|
"VALUES (:id, :key, :value, :subject_id)").arg(table));
|
|
query.bindValue(":id", subject->idToStringWithoutBraces() + " " + attribute.first);
|
|
query.bindValue(":key", attribute.first);
|
|
query.bindValue(":value", attribute.second);
|
|
query.bindValue(":subject_id", subject->idToStringWithoutBraces());
|
|
query.exec();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
QString DatabaseManager::getTestMessage(int id)
|
|
{
|
|
query.prepare("SELECT message FROM test WHERE id = :id");
|
|
query.bindValue(":id", id);
|
|
query.exec();
|
|
query.next();
|
|
return query.value(0).toString();
|
|
}
|
|
|
|
void DatabaseManager::createTestRecord(QString msg)
|
|
{
|
|
query.exec("SELECT MAX(id) FROM test");
|
|
query.next();
|
|
int id = query.value(0).toInt() + 1;
|
|
query.prepare("INSERT INTO test (id, message) VALUES (:id, :msg)");
|
|
query.bindValue(":id", id);
|
|
query.bindValue(":msg", msg);
|
|
query.exec();
|
|
query.next();
|
|
qDebug() << "U inserted new row, id: " << id << "msg: " << getTestMessage(id);
|
|
|
|
}
|
|
|
|
|
|
bool DatabaseManager::checkIfExists(QString id, QString table)
|
|
{
|
|
query.prepare(QString("SELECT exists(SELECT 1 FROM %1 WHERE id = :id)").arg(table));
|
|
query.bindValue(":id", id);
|
|
query.exec();
|
|
query.next();
|
|
return query.value(0).toBool();
|
|
}
|