-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSqlQuery.cpp
161 lines (133 loc) · 5.7 KB
/
SqlQuery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright (C) 2011-2013 Klarälvdalens Datakonsult AB,
a KDAB Group company, [email protected],
author Volker Krause <[email protected]>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "SqlQuery.h"
#include "SqlExceptions.h"
#include "SqlQueryManager.h"
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
#include "SqlQueryWatcher.h"
#include "kdthreadrunner.h"
#endif
#include <QSqlDatabase>
#include <QDebug>
#include <QVariant>
SqlQuery::SqlQuery(const QString &query /*= QString()*/, const QSqlDatabase& db /*= QSqlDatabase()*/ ) :
QSqlQuery( db ), m_db( db )
{
m_connectionName = db.connectionName();
SqlQueryManager::instance()->registerQuery(this);
if ( !query.isEmpty() )
exec( query );
}
SqlQuery::SqlQuery(const QSqlDatabase& db) :
QSqlQuery( db ), m_db(db)
{
m_connectionName = db.connectionName();
SqlQueryManager::instance()->registerQuery(this);
}
SqlQuery::SqlQuery(const SqlQuery &other ) :
QSqlQuery( other ), m_db(other.m_db)
{
m_connectionName = m_db.connectionName();
SqlQueryManager::instance()->registerQuery(this);
}
SqlQuery::~SqlQuery()
{
SqlQueryManager::instance()->unregisterQuery(this);
}
SqlQuery& SqlQuery::operator=(const SqlQuery& other)
{
QSqlQuery::operator=(other);
m_db = other.m_db;
m_connectionName = m_db.connectionName();
//Debug line that helps finding leaking queries. It is intentionally not a SQLDEBUG.
// qDebug() << "operator= " << lastQuery() << this;
return *this;
}
void SqlQuery::exec()
{
SqlQueryManager::instance()->checkDbIsAlive(m_db);
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
KDThreadRunner<SqlQueryWatcherHelper> watcher;
SqlQueryWatcherHelper* helper = watcher.startThread();
#endif
bool result = QSqlQuery::exec();
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
QMetaObject::invokeMethod( helper, "quit", Qt::QueuedConnection );
watcher.wait();
#endif
if (!result && ( !m_db.isOpen() || !m_db.isValid() ) ) {
SqlQueryManager::instance()->checkDbIsAlive(m_db); //double check is needed, as Qt might not set m_db.isOpen() to false after connection loss if no queries were run meantime.
result = QSqlQuery::exec();
}
if ( !result ) {
qWarning() << Q_FUNC_INFO << "Exec failed: " << this << QSqlQuery::lastError() << " query was: " << QSqlQuery::lastQuery()
<< ", executed query: " << QSqlQuery::executedQuery() << " bound values: "<< QSqlQuery::boundValues().values();
// qWarning() << "Database status: " << m_db.isOpen() << m_db.isValid() << m_db.isOpenError();
throw SqlException( QSqlQuery::lastError() );
}
}
void SqlQuery::exec(const QString& query)
{
SqlQueryManager::instance()->checkDbIsAlive(m_db);
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
KDThreadRunner<SqlQueryWatcherHelper> watcher;
SqlQueryWatcherHelper* helper = watcher.startThread();
#endif
bool result = QSqlQuery::exec( query );
if (!result && ( !m_db.isOpen() || !m_db.isValid() ) ) {
SqlQueryManager::instance()->checkDbIsAlive(m_db); //double check is needed, as Qt might not set m_db.isOpen() to false after connection loss if no queries were run meantime.
result = QSqlQuery::exec();
}
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
QMetaObject::invokeMethod( helper, "quit", Qt::QueuedConnection );
watcher.wait();
#endif
if ( !result ) {
qWarning() << Q_FUNC_INFO << "Exec(query) failed: " << this << QSqlQuery::lastError() << " query was: " << QSqlQuery::lastQuery()
<< ", executed query: " << QSqlQuery::executedQuery() << " bound values: "<< QSqlQuery::boundValues().values();
// qWarning() << "Database status: " << m_db.isOpen() << m_db.isValid() << m_db.isOpenError();
throw SqlException( QSqlQuery::lastError() );
}
}
void SqlQuery::prepare(const QString& query)
{
SqlQueryManager::instance()->checkDbIsAlive(m_db);
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
KDThreadRunner<SqlQueryWatcherHelper> watcher;
SqlQueryWatcherHelper* helper = watcher.startThread();
#endif
bool result = QSqlQuery::prepare( query );
if (!result && ( !m_db.isOpen() || !m_db.isValid() ) ) {
SqlQueryManager::instance()->checkDbIsAlive(m_db); //double check is needed, as Qt might not set m_db.isOpen() to false after connection loss if no queries were run meantime.
result = QSqlQuery::prepare( query );
}
#ifdef SQLATE_ENABLE_NETWORK_WATCHER
QMetaObject::invokeMethod( helper, "quit", Qt::QueuedConnection );
watcher.wait();
#endif
if ( !result ) {
qWarning() << Q_FUNC_INFO << "Prepare failed: " << this << QSqlQuery::lastError() << " query was: " << QSqlQuery::lastQuery()
<< ", executed query: " << QSqlQuery::executedQuery() << " bound values: "<< QSqlQuery::boundValues().values();
// qWarning() << "Database status: " << m_db.isOpen() << m_db.isValid() << m_db.isOpenError();
throw SqlException( QSqlQuery::lastError() );
}
}
bool SqlQuery::prepareWithoutCheck(const QString& query)
{
return QSqlQuery::prepare(query);
}