sqlitedatabase.cpp 2.88 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 26 27 28 29
**
****************************************************************************/

#include "sqlitedatabase.h"

#include "sqlitetable.h"

Marco Bubke's avatar
Marco Bubke committed
30 31
namespace Sqlite {

32
SqliteDatabase::SqliteDatabase()
Marco Bubke's avatar
Marco Bubke committed
33
    : m_journalMode(JournalMode::Wal)
34 35 36 37 38
{
}

SqliteDatabase::~SqliteDatabase()
{
Marco Bubke's avatar
Marco Bubke committed
39
    qDeleteAll(m_sqliteTables);
40 41 42 43
}

void SqliteDatabase::open()
{
44 45
    m_databaseBackend.open(m_databaseFilePath);
    m_databaseBackend.setJournalMode(journalMode());
Marco Bubke's avatar
Marco Bubke committed
46 47
    initializeTables();
    m_isOpen = true;
48 49
}

50
void SqliteDatabase::open(Utils::PathString &&databaseFilePath)
51
{
52
    setDatabaseFilePath(std::move(databaseFilePath));
53 54 55
    open();
}

56 57
void SqliteDatabase::close()
{
Marco Bubke's avatar
Marco Bubke committed
58
    m_isOpen = false;
59
    m_databaseBackend.close();
60 61 62 63
}

bool SqliteDatabase::isOpen() const
{
Marco Bubke's avatar
Marco Bubke committed
64
    return m_isOpen;
65 66 67 68 69
}

void SqliteDatabase::addTable(SqliteTable *newSqliteTable)
{
    newSqliteTable->setSqliteDatabase(this);
70
    m_sqliteTables.push_back(newSqliteTable);
71 72
}

73
const std::vector<SqliteTable *> &SqliteDatabase::tables() const
74
{
Marco Bubke's avatar
Marco Bubke committed
75
    return m_sqliteTables;
76 77
}

78
void SqliteDatabase::setDatabaseFilePath(Utils::PathString &&databaseFilePath)
79
{
80
    m_databaseFilePath = std::move(databaseFilePath);
81 82
}

83
const Utils::PathString &SqliteDatabase::databaseFilePath() const
84
{
Marco Bubke's avatar
Marco Bubke committed
85
    return m_databaseFilePath;
86 87 88 89
}

void SqliteDatabase::setJournalMode(JournalMode journalMode)
{
Marco Bubke's avatar
Marco Bubke committed
90
    m_journalMode = journalMode;
91 92 93 94
}

JournalMode SqliteDatabase::journalMode() const
{
Marco Bubke's avatar
Marco Bubke committed
95
    return m_journalMode;
96 97
}

98 99 100 101 102 103 104 105 106 107
int SqliteDatabase::changesCount()
{
    return m_databaseBackend.changesCount();
}

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

108 109 110 111 112 113
void SqliteDatabase::initializeTables()
{
    for (SqliteTable *table: tables())
        table->initialize();
}

114 115 116 117 118
SqliteDatabaseBackend &SqliteDatabase::backend()
{
    return m_databaseBackend;
}

Marco Bubke's avatar
Marco Bubke committed
119
} // namespace Sqlite