summaryrefslogtreecommitdiff
path: root/cpp/src/IceStorm/SqlDB/SqlLLU.cpp
blob: 23a32888c7a56777fd4d668487a4f9e5f28d3664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceStorm/SqlDB/SqlLLU.h>

#include <QtSql/QSqlQuery>
#include <QtCore/QVariant>
#include <QtCore/QStringList>

using namespace IceStorm;
using namespace SqlDB;
using namespace std;

SqlLLU::SqlLLU(const DatabaseConnectionPtr& connection, const string& table) :
    _table(table)
{
    QStringList tables = connection->sqlConnection().tables(QSql::Tables);
    if(!tables.contains(_table.c_str(), Qt::CaseInsensitive))
    {
        QSqlQuery query(connection->sqlConnection());
        string queryString = "CREATE TABLE ";
        queryString += _table;
        queryString += " (id VARCHAR(255) PRIMARY KEY, generation INT, iteration INT);";

        if(!query.exec(queryString.c_str()))
        {
            throwDatabaseException(__FILE__, __LINE__, query.lastError());
        }

        //
        // LLU table only ever contains one entry so we add it now.
        //
        QSqlQuery addQuery(connection->sqlConnection());
        queryString = "INSERT INTO ";
        queryString += _table;
        queryString += " VALUES('_master', '0', '0');";

        if(!addQuery.exec(queryString.c_str()))
        {
            throwDatabaseException(__FILE__, __LINE__, addQuery.lastError());
        }
    }
}

void 
SqlLLU::put(const DatabaseConnectionPtr& connection, const IceStormElection::LogUpdate& update)
{
    QSqlQuery query(connection->sqlConnection());
    ostringstream queryString;
    queryString << "UPDATE " << _table << " SET generation=" << update.generation << ", iteration=" 
                << update.iteration << " WHERE id='_master';";

    if(!query.exec(queryString.str().c_str()))
    {
        throwDatabaseException(__FILE__, __LINE__, query.lastError());
    }
}

IceStormElection::LogUpdate 
SqlLLU::get(const DatabaseConnectionPtr& connection) const
{
    QSqlQuery query(connection->sqlConnection());
    string queryString = "SELECT * FROM ";
    queryString += _table;
    queryString += " WHERE id = '_master';";

    if(!query.exec(queryString.c_str()))
    {
        throwDatabaseException(__FILE__, __LINE__, query.lastError());
    }

    if(query.next())
    {
        IceStormElection::LogUpdate update;
        update.generation = query.value(1).toInt();
        update.iteration = query.value(2).toInt();
        return update;
    }
    else
    {
       throw NotFoundException(__FILE__, __LINE__);
    }
}