Torc  0.1
torcsqlitedb.cpp
Go to the documentation of this file.
1 /* Class TorcSQLiteDB
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Qt
24 #include <QtSql>
25 
26 // Torc
27 #include "torclogging.h"
28 #include "torcsqlitedb.h"
29 
42 TorcSQLiteDB::TorcSQLiteDB(const QString &DatabaseName)
43  : TorcDB(DatabaseName, QStringLiteral("QSQLITE"))
44 {
45  InitDatabase();
46 }
47 
52 {
53  QMutexLocker locker(&m_lock);
54  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Attempting to open '%1'")
55  .arg(m_databaseName));
56 
57  QSqlDatabase db = QSqlDatabase::database(GetThreadConnection());
58  DebugError(&db);
59  if (!db.isValid())
60  {
61  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to get valid database connection."));
62  return false;
63  }
64 
65  if (!db.open())
66  {
67  LOG(VB_GENERAL, LOG_ERR, QStringLiteral("Failed to open database."));
68  return false;
69  }
70 
71  QSqlQuery query(db);
72 
73  query.exec(QStringLiteral("PRAGMA locking_mode = NORMAL"));
74  if (DebugError(&query))
75  return false;
76 
77  // Create the settings table if it doesn't exist
78  query.exec("CREATE TABLE IF NOT EXISTS settings "
79  "( name VARCHAR(128) NOT NULL,"
80  " value VARCHAR(16000) NOT NULL );");
81  DebugError(&query);
82 
83  // Check the creation date for existing installations
84  query.exec(QStringLiteral("SELECT value FROM settings where name='DB_DateCreated'"));
85  DebugError(&query);
86 
87  query.first();
88  if (query.isValid())
89  {
90  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Settings table was created on %1")
91  .arg(query.value(0).toString()));
92  }
93  else
94  {
95  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Initialising settings table."));
96  QString createdon = QDateTime::currentDateTimeUtc().toString(Qt::ISODate);
97  query.exec(
98  QStringLiteral("INSERT INTO settings (name, value) VALUES ('DB_DateCreated', '%1');")
99  .arg(createdon));
100  DebugError(&query);
101  }
102 
103  // optimise the database
104  query.exec(QStringLiteral("PRAGMA page_size = 4096"));
105  DebugError(&query);
106  query.exec(QStringLiteral("PRAGMA cache_size = 16384"));
107  DebugError(&query);
108  query.exec(QStringLiteral("PRAGMA temp_store = MEMORY"));
109  DebugError(&query);
110  query.exec(QStringLiteral("PRAGMA journal_mode = OFF"));
111  DebugError(&query);
112  query.exec(QStringLiteral("PRAGMA synchronous = OFF"));
113  DebugError(&query);
114 
115  m_databaseValid = true;
116 
117  return true;
118 }
TorcSQLiteDB(const QString &DatabaseName)
QString GetThreadConnection(void)
Retrieve a database connection for the current thread.
Definition: torcdb.cpp:125
Base Sql database access class.
Definition: torcdb.h:11
bool InitDatabase(void) override
Create and/or open an SQLiteDatabase.
QMutex m_lock
Definition: torcdb.h:34
bool m_databaseValid
Definition: torcdb.h:31
QString m_databaseName
Definition: torcdb.h:32
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
static bool DebugError(QSqlQuery *Query)
Log database errors following a failed query.
Definition: torcdb.cpp:162