sqlitedatabase.cpp 2.47 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()
{
Marco Bubke's avatar
Marco Bubke committed
44 45 46 47
    m_sqliteDatabaseBackEnd.open(m_databaseFilePath);
    m_sqliteDatabaseBackEnd.setJournalMode(journalMode());
    initializeTables();
    m_isOpen = true;
48 49 50 51
}

void SqliteDatabase::close()
{
Marco Bubke's avatar
Marco Bubke committed
52 53
    m_isOpen = false;
    m_sqliteDatabaseBackEnd.close();
54 55 56 57
}

bool SqliteDatabase::isOpen() const
{
Marco Bubke's avatar
Marco Bubke committed
58
    return m_isOpen;
59 60 61 62 63
}

void SqliteDatabase::addTable(SqliteTable *newSqliteTable)
{
    newSqliteTable->setSqliteDatabase(this);
Marco Bubke's avatar
Marco Bubke committed
64
    m_sqliteTables.append(newSqliteTable);
65 66 67 68
}

const QVector<SqliteTable *> &SqliteDatabase::tables() const
{
Marco Bubke's avatar
Marco Bubke committed
69
    return m_sqliteTables;
70 71 72 73
}

void SqliteDatabase::setDatabaseFilePath(const QString &databaseFilePath)
{
Marco Bubke's avatar
Marco Bubke committed
74
    m_databaseFilePath = databaseFilePath;
75 76 77 78
}

const QString &SqliteDatabase::databaseFilePath() const
{
Marco Bubke's avatar
Marco Bubke committed
79
    return m_databaseFilePath;
80 81 82 83
}

void SqliteDatabase::setJournalMode(JournalMode journalMode)
{
Marco Bubke's avatar
Marco Bubke committed
84
    m_journalMode = journalMode;
85 86 87 88
}

JournalMode SqliteDatabase::journalMode() const
{
Marco Bubke's avatar
Marco Bubke committed
89
    return m_journalMode;
90 91 92 93 94 95 96 97
}

void SqliteDatabase::initializeTables()
{
    for (SqliteTable *table: tables())
        table->initialize();
}

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