source: SMSSender/lib/libgateway/src/persistence/AbstractAccountStorageOfficer.cpp @ 43:10dd65d71f7f

3.0
Last change on this file since 43:10dd65d71f7f was 43:10dd65d71f7f, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • Did a lot of work...
File size: 3.8 KB
Line 
1#include "AbstractAccountStorageOfficer.h"
2
3#include <QVariant>
4#include <QSqlQuery>
5#include <QDebug>
6#include <QSqlError>
7
8#include <typeconvert.h>
9#include <StorageExceptions.h>
10
11
12AbstractAccountStorageOfficer::AbstractAccountStorageOfficer(IAccount* account, IStorage* storage)
13    : AbstractStorageOfficer(storage)
14    , account_(account)
15{
16    QSqlQuery query = storage->createQuery();
17    query.prepare("CREATE TABLE IF NOT EXISTS t_accounts ("
18                    "accountId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
19                    "name      TEXT    NOT NULL UNIQUE, "
20                    "type      TEXT    NOT NULL "
21                  "); ");
22    if (!query.exec()) {
23        throw Storage::EWriteException(tr("Could not create the account table in the database."));
24    }
25}
26
27
28void AbstractAccountStorageOfficer::readFromStorage(){
29    QSqlQuery query = storage()->createQuery();
30    query.exec("SELECT name "
31                 "FROM t_accounts "
32                 "WHERE (accountId = '" + QString::number(account()->id()) + "'); ");
33    if (!query.next()) {
34        throw Storage::EReadException(tr("No such account in the database! [accountId: %1]").arg(account()->id()));
35    }
36
37    account()->setName(query.value(0).toString());
38    account()->setEnabled(storage()->readBool(getNamespace(), "enabled", true));
39}
40
41void AbstractAccountStorageOfficer::writeToStorage() const {
42    QSqlQuery query = storage()->createQuery();
43    query.exec("SELECT accountId "
44                 "FROM t_accounts "
45                 "WHERE (accountId = '" + QString::number(account()->id()) + "'); ");
46
47    if (!query.next()){
48        // Insert
49
50        query.prepare("INSERT INTO t_accounts (name, type) "
51                        "VALUES ("
52                          ":accountName, "
53                          ":gatewayName"
54                        "); ");
55        query.bindValue(":accountName", account()->name());
56        query.bindValue(":gatewayName", account()->gateway()->name());
57        if (!query.exec()) {
58            throw Storage::EWriteException(tr("The account could not have been written to the database."));
59        }
60
61        // Set the accountId since it is undefined if the account was not saved before.
62        query.exec("SELECT accountId "
63                     "FROM t_accounts "
64                     "WHERE (name = '" + account()->name() + "'); ");
65        if (!query.next()) {
66            throw Storage::EWriteException(tr("The account could not have been written to the database."));
67        }
68        account()->setId(query.value(0).toInt());
69    }else{
70        // Update
71        query.prepare("UPDATE t_accounts SET "
72                        "name=:accountName, "
73                        "type=:gatewayName "
74                      "WHERE (accountId = :accountId); ");
75        query.bindValue(":accountName", account()->name());
76        query.bindValue(":gatewayName", account()->gateway()->name());
77        query.bindValue(":accountId", account()->id());
78
79        if (!query.exec()) {
80            qCritical() << query.lastError().text();
81            throw Storage::EWriteException(tr("The account could not have been written to the database."));
82        }
83    }
84    storage()->writeBool(getNamespace(), "enabled", account()->isEnabled());
85}
86
87void AbstractAccountStorageOfficer::removeFromStorage() {
88    storage()->removeValues(getNamespace());
89
90    QSqlQuery query = storage()->createQuery();
91    query.prepare("DELETE FROM t_accounts "
92                    "WHERE (accountId = '" + QString::number(account()->id()) + "'); ");
93    if (!query.exec()) {
94        throw Storage::EWriteException(tr("Could not remove the account from the database."));
95    }
96
97    account()->setId(-1); // Invalidate
98}
99
100
101
102QString AbstractAccountStorageOfficer::getNamespace() const {
103    return "account_" + QString::number(account()->id());
104}
105
106
107IAccount* AbstractAccountStorageOfficer::account() const {
108    return account_;
109}
Note: See TracBrowser for help on using the repository browser.