aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/sqlite/sqlitesessions.h
blob: 2694ac93d3f87bfa205f797116525e8a1a57d9f4 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "sqlite3_fwd.h"
#include "sqlitedatabase.h"
#include "sqlitesessionchangeset.h"
#include "sqlitewritestatement.h"

namespace Sqlite {

namespace Internal {

class SQLITE_EXPORT SessionsBase
{
public:
    SessionsBase(Database &database,
                 Utils::SmallStringView sessionsTableName,
                 const source_location &sourceLocation)
        : sessionsTableName(sessionsTableName)
    {
        createSessionTable(database, sourceLocation);
    }

    void createSessionTable(Database &database, const source_location &sourceLocation);

public:
    Utils::SmallString sessionsTableName;
};
} // namespace Internal

class SQLITE_EXPORT Sessions : public Internal::SessionsBase
{
public:
    Sessions(Database &database,
             Utils::SmallStringView databaseName,
             Utils::SmallStringView sessionsTableName,
             const source_location &sourceLocation = source_location::current())
        : SessionsBase(database, sessionsTableName, sourceLocation)
        , database(database)
        , insertSession{Utils::PathString::join(
                            {"INSERT INTO ", sessionsTableName, "(changeset) VALUES(?)"}),
                        database,
                        sourceLocation}
        , databaseName(databaseName)
    {}
    ~Sessions();

    void setAttachedTables(Utils::SmallStringVector tables);

    void create(const source_location &sourceLocation = source_location::current());
    void commit(const source_location &sourceLocation = source_location::current());
    void rollback();

    void revert(const source_location &sourceLocation = source_location::current());
    void apply(const source_location &sourceLocation = source_location::current());
    void applyAndUpdateSessions(const source_location &sourceLocation = source_location::current());
    void deleteAll(const source_location &sourceLocation = source_location::current());

    SessionChangeSets changeSets(const source_location &sourceLocation = source_location::current()) const;

private:
    void attachTables(const Utils::SmallStringVector &tables, const source_location &sourceLocation);

    struct Deleter
    {
        SQLITE_EXPORT void operator()(sqlite3_session *statement);
    };

public:
    Database &database;
    WriteStatement<1> insertSession;
    Utils::SmallString databaseName;
    Utils::SmallStringVector tableNames;
    std::unique_ptr<sqlite3_session, Deleter> session;
};

} // namespace Sqlite