sqlitedatabase.h 3.04 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25
**
****************************************************************************/

hjk's avatar
hjk committed
26
#pragma once
27

Marco Bubke's avatar
Marco Bubke committed
28
#include "sqlitedatabasebackend.h"
Marco Bubke's avatar
Marco Bubke committed
29
#include "sqliteglobal.h"
30
#include "sqlitetable.h"
Marco Bubke's avatar
Marco Bubke committed
31

32 33
#include <utils/smallstring.h>

34
#include <mutex>
35
#include <vector>
36

Marco Bubke's avatar
Marco Bubke committed
37 38
namespace Sqlite {

Marco Bubke's avatar
Marco Bubke committed
39
class SQLITE_EXPORT Database
40
{
Marco Bubke's avatar
Marco Bubke committed
41
    template <typename Database>
42 43 44
    friend class AbstractTransaction;
    friend class Statement;
    friend class Backend;
45

46
public:
47 48
    using MutexType = std::mutex;

Marco Bubke's avatar
Marco Bubke committed
49
    Database();
50
    Database(Utils::PathString &&databaseFilePath, JournalMode journalMode=JournalMode::Wal);
51

Marco Bubke's avatar
Marco Bubke committed
52 53
    Database(const Database &) = delete;
    bool operator=(const Database &) = delete;
54

Marco Bubke's avatar
Marco Bubke committed
55 56
    Database(Database &&) = delete;
    bool operator=(Database &&) = delete;
57 58

    void open();
59
    void open(Utils::PathString &&databaseFilePath);
60 61 62 63
    void close();

    bool isOpen() const;

Marco Bubke's avatar
Marco Bubke committed
64 65
    Table &addTable();
    const std::vector<Table> &tables() const;
66

67 68
    void setDatabaseFilePath(Utils::PathString &&databaseFilePath);
    const Utils::PathString &databaseFilePath() const;
69 70 71 72

    void setJournalMode(JournalMode journalMode);
    JournalMode journalMode() const;

73 74 75
    void setOpenMode(OpenMode openMode);
    OpenMode openMode() const;

76 77
    void execute(Utils::SmallStringView sqlStatement);

Marco Bubke's avatar
Marco Bubke committed
78
    DatabaseBackend &backend();
79

Marco Bubke's avatar
Marco Bubke committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    int64_t lastInsertedRowId() const
    {
        return m_databaseBackend.lastInsertedRowId();
    }

    int changesCount()
    {
        return m_databaseBackend.changesCount();
    }

    int totalChangesCount()
    {
        return m_databaseBackend.totalChangesCount();
    }

95 96
private:
    void initializeTables();
97
    std::mutex &databaseMutex() { return m_databaseMutex; }
98 99

private:
100
    Utils::PathString m_databaseFilePath;
Marco Bubke's avatar
Marco Bubke committed
101 102
    DatabaseBackend m_databaseBackend;
    std::vector<Table> m_sqliteTables;
103
    std::mutex m_databaseMutex;
104 105
    JournalMode m_journalMode = JournalMode::Wal;
    OpenMode m_openMode = OpenMode::ReadWrite;
Marco Bubke's avatar
Marco Bubke committed
106
    bool m_isOpen = false;
107
};
Marco Bubke's avatar
Marco Bubke committed
108 109

} // namespace Sqlite