52 : m_databaseValid(false),
53 m_databaseName(DatabaseName),
54 m_databaseType(DatabaseType),
55 m_lock(QMutex::Recursive),
62 QMutexLocker locker(&
m_lock);
72 QMutexLocker locker(&
m_lock);
81 QMutexLocker locker(&
m_lock);
88 LOG(VB_GENERAL, LOG_WARNING, QStringLiteral(
"%1 open connections.").arg(
m_connectionMap.size()));
94 QString name = it.value();
95 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Removing connection '%1'").arg(name));
96 QSqlDatabase::removeDatabase(name);
106 QThread* thread = QThread::currentThread();
107 QMutexLocker locker(&
m_lock);
111 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"Removing connection '%1'").arg(name));
112 QSqlDatabase::removeDatabase(name);
127 QThread* thread = QThread::currentThread();
130 QMutexLocker locker(&
m_lock);
135 if (thread->objectName().isEmpty())
137 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Database access is only available from TorcQThread"));
141 QString name = QStringLiteral(
"%1-%2").arg(thread->objectName(), QString::number((
unsigned long long)thread));
142 QSqlDatabase newdb = QSqlDatabase::addDatabase(
m_databaseType, name);
145 newdb.setConnectOptions(QStringLiteral(
"QSQLITE_BUSY_TIMEOUT=1"));
149 QMutexLocker locker(&
m_lock);
151 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"New connection '%1'").arg(name));
167 QSqlError error = Query->lastError();
169 if (error.type() == QSqlError::NoError)
172 if (!error.databaseText().isEmpty())
174 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Database Error: %1")
175 .arg(error.databaseText()));
179 if (!error.driverText().isEmpty())
181 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Driver Error: %1")
182 .arg(error.driverText()));
198 QSqlError error = Database->lastError();
200 if (error.type() == QSqlError::NoError)
203 if (!error.databaseText().isEmpty())
205 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Database Error: %1")
206 .arg(error.databaseText()));
210 if (!error.driverText().isEmpty())
212 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Driver Error: %1")
213 .arg(error.driverText()));
231 QMutexLocker locker(&
m_lock);
234 if (!db.isValid() || !db.isOpen())
237 QSqlQuery query(QStringLiteral(
"SELECT name, value from settings;"), db);
242 LOG(VB_GENERAL, LOG_DEBUG, QStringLiteral(
"'%1' : '%2'").arg(query.value(0).toString(), query.value(1).toString()));
243 Settings.insert(query.value(0).toString(), query.value(1).toString());
252 QMutexLocker locker(&
m_lock);
258 if (!db.isValid() || !db.isOpen())
260 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Failed to open database connection."));
265 query.prepare(QStringLiteral(
"DELETE FROM settings where name=:NAME;"));
266 query.bindValue(QStringLiteral(
":NAME"), Name);
270 query.prepare(QStringLiteral(
"INSERT INTO settings (name, value) VALUES (:NAME, :VALUE);"));
271 query.bindValue(QStringLiteral(
":NAME"), Name);
272 query.bindValue(QStringLiteral(
":VALUE"), Value);
TorcDB(const QString &DatabaseName, const QString &DatabaseType)
bool IsValid(void)
Returns true if the datbase has been opened/created.
void CloseThreadConnection(void)
Close the database connection for the current thread.
QString GetThreadConnection(void)
Retrieve a database connection for the current thread.
QHash< QThread *, QString > m_connectionMap
#define LOG(_MASK_, _LEVEL_, _STRING_)
void CloseConnections(void)
Close all cached database connections.
void SetSetting(const QString &Name, const QString &Value)
Set the setting Name to the value Value.
void LoadSettings(QMap< QString, QString > &Settings)
Retrieve all persistent settings stored in the database.
static bool DebugError(QSqlQuery *Query)
Log database errors following a failed query.