5
5
#include < QVariant>
6
6
#include < QVector>
7
7
#include < QSet>
8
+ #include < QRegularExpression>
8
9
#ifdef QT_DEBUG
9
10
#include < QDebug>
10
11
#endif
11
-
12
+ #include < exception/qtexception.h>
13
+ #include < exception/sqlexception.h>
14
+ #include < QSqlError>
15
+ #include < QSqlDriver>
12
16
using namespace SqlUtil4 ;
13
17
14
18
QSqlDatabase Sql::connectMySql (const QString &host, const QString &user, const QString &pass,const QString &dbname,const QString &conname , int port)
@@ -142,6 +146,16 @@ QSqlDatabase Sql::connectSqlite(const QString &dbFile,const QString & connection
142
146
143
147
144
148
if (con.open ()) {
149
+ #ifdef QT_DEBUG
150
+ qDebug ()<<connectionName;
151
+ #endif
152
+ // #ifdef QT_DEBUG
153
+ // auto q=Sql::query(con,"pragma COMPILE_OPTIONS");
154
+ // while(q.next())
155
+ // {
156
+ // qDebug()<<q.value(0);
157
+ // }
158
+ // #endif
145
159
return con;
146
160
} else {
147
161
throw SqlException (con.lastError ().nativeErrorCode (),
@@ -156,8 +170,8 @@ QSqlQuery Sql::query(const QSqlDatabase & sqlCon, const QString & sql, const QVa
156
170
q.addBindValue (param);
157
171
if (!q.exec ()) {
158
172
159
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
160
- sqlCon.driver ()->lastError ().text (),getDebugString (sql,QVariantList () << param));
173
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
174
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sql,QVariantList () << param));
161
175
}
162
176
return q;
163
177
@@ -173,8 +187,8 @@ QSqlQuery Sql::query(const QSqlDatabase & sqlCon, const QString & sql) {
173
187
if (q.prepare (sql)) {
174
188
if (!q.exec ()) {
175
189
176
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
177
- sqlCon.driver ()->lastError ().text (),sql);
190
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
191
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),sql);
178
192
}
179
193
return q;
180
194
@@ -198,8 +212,8 @@ QSqlQuery Sql::query(const QSqlDatabase & sqlCon, const QString & sql, const QLi
198
212
#ifdef QT_DEBUG
199
213
qDebug ().noquote () << " Error:" + Sql::getDebugString (sql,params);
200
214
#endif
201
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
202
- sqlCon.driver ()->lastError ().text (),getDebugString (sql, params));
215
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
216
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sql, params));
203
217
}
204
218
return q;
205
219
@@ -215,9 +229,11 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, const QL
215
229
{
216
230
#ifdef QT_DEBUG
217
231
qDebug ().noquote () << sqlCon.lastError ().databaseText ();
218
- throwSqlExceptionWithLine (sqlCon.lastError ().nativeErrorCode (),
219
- !sqlCon.lastError ().text ().isEmpty ()? sqlCon.lastError ().text ():sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, params));
232
+ qDebug ().noquote () << getDebugString (sqlQuery,params);
220
233
#endif
234
+ throwSqlExceptionWithLine (q.lastError ().nativeErrorCode (),
235
+ !q.lastError ().text ().isEmpty ()? q.lastError ().text ():sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, params));
236
+
221
237
}
222
238
for (int i = 0 ; i < params.size (); i++) {
223
239
q.addBindValue (params.at (i));
@@ -227,10 +243,11 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, const QL
227
243
if (!q.exec ()) {
228
244
#ifdef QT_DEBUG
229
245
qDebug ().noquote () << sqlQuery;
230
- qDebug ().noquote () << " Error:" + Sql::getDebugString (sqlQuery,params);
246
+ qDebug ().noquote () << " Query:" + Sql::getDebugString (sqlQuery,params);
247
+ qDebug ().noquote () << " Error:" +q.lastError ().text ();
231
248
#endif
232
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
233
- !sqlCon .lastError ().text ().isEmpty ()? sqlCon .lastError ().text ():sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, params));
249
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
250
+ !q .lastError ().text ().isEmpty ()? q .lastError ().text ():sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, params));
234
251
}
235
252
}
236
253
@@ -243,8 +260,8 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, int64_t
243
260
244
261
245
262
if (!res) {
246
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
247
- sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
263
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
264
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
248
265
}
249
266
}
250
267
@@ -257,8 +274,8 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, int para
257
274
258
275
259
276
if (!res) {
260
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
261
- sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
277
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
278
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
262
279
}
263
280
}
264
281
@@ -271,11 +288,26 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, const QS
271
288
272
289
273
290
if (!res) {
274
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
275
- sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
291
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
292
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
276
293
}
277
294
}
278
295
296
+ void Sql::execute (const QSqlDatabase &sqlCon, const QString &sqlQuery, const QVariant ¶m)
297
+ {
298
+ QSqlQuery q (sqlCon);
299
+ bool res = q.prepare (sqlQuery);
300
+ q.addBindValue (param);
301
+
302
+ res = res & q.exec ();
303
+
304
+
305
+ if (!res) {
306
+ throwSqlExceptionWithLine (q.lastError ().nativeErrorCode (),
307
+ !q.lastError ().text ().isEmpty () ?q.lastError ().text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
308
+ }
309
+ }
310
+
279
311
void Sql::execute (const QSqlDatabase & sqlCon, const QString &sqlQuery, double param) {
280
312
QSqlQuery q (sqlCon);
281
313
bool res = q.prepare (sqlQuery);
@@ -285,8 +317,8 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, double p
285
317
286
318
287
319
if (!res) {
288
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
289
- sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
320
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
321
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
290
322
}
291
323
}
292
324
@@ -299,8 +331,8 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, bool par
299
331
300
332
301
333
if (!res) {
302
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
303
- sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
334
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
335
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
304
336
}
305
337
}
306
338
@@ -314,16 +346,16 @@ void Sql::execute(const QSqlDatabase & sqlCon, const QString &sqlQuery, float pa
314
346
315
347
316
348
if (!res) {
317
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
318
- sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
349
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
350
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),getDebugString (sqlQuery, QVariantList () << param ));
319
351
}
320
352
}
321
353
322
354
void Sql::execute (const QSqlDatabase & sqlCon, const QString &sqlQuery) {
323
355
QSqlQuery q (sqlCon);
324
356
if (!q.exec (sqlQuery)) {
325
- throwSqlExceptionWithLine (sqlCon .lastError ().nativeErrorCode (),
326
- sqlCon.driver ()->lastError ().text (),sqlQuery);
357
+ throwSqlExceptionWithLine (q .lastError ().nativeErrorCode (),
358
+ !q. lastError (). text (). isEmpty () ?q. lastError (). text () : sqlCon.driver ()->lastError ().text (),sqlQuery);
327
359
}
328
360
}
329
361
@@ -393,7 +425,7 @@ QSqlRecord Sql::fetchRow(const QSqlDatabase & sqlCon, const QString & sql, const
393
425
394
426
throwSqlExceptionWithLine (q.lastError ().nativeErrorCode (), q.lastError ().text (), getDebugString (sql, params ));
395
427
}
396
-
428
+ # if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
397
429
QSqlRecord Sql::fetchRow (const QSqlDatabase &sqlCon, const QString &sql, const QVector<int64_t > ¶ms)
398
430
{
399
431
QSqlQuery q (sqlCon);
@@ -420,7 +452,7 @@ QSqlRecord Sql::fetchRow(const QSqlDatabase &sqlCon, const QString &sql, const Q
420
452
}
421
453
throwSqlExceptionWithLine (q.lastError ().nativeErrorCode (), q.lastError ().text (), getDebugString (sql, vparams ));
422
454
}
423
-
455
+ # endif
424
456
425
457
QSqlRecord Sql::fetchRow (const QSqlDatabase &sqlCon, const QString &sql, const QSet<int64_t > ¶ms)
426
458
{
@@ -512,7 +544,7 @@ int Sql::fetchInt(const QSqlDatabase & sqlCon, const QString &sql, const QList<Q
512
544
}
513
545
return val;
514
546
}
515
-
547
+ # if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
516
548
int Sql::fetchInt (const QSqlDatabase &sqlCon, const QString &sql, const QVector<int64_t > ¶ms)
517
549
{
518
550
bool ok;
@@ -524,7 +556,7 @@ int Sql::fetchInt(const QSqlDatabase &sqlCon, const QString &sql, const QVector<
524
556
}
525
557
return val;
526
558
}
527
-
559
+ # endif
528
560
int Sql::fetchInt (const QSqlDatabase &sqlCon, const QString &sql, const QSet<int64_t > ¶ms)
529
561
{
530
562
bool ok;
@@ -631,7 +663,7 @@ uint Sql::fetchUInt(const QSqlDatabase &sqlCon, const QString &sql, const QVaria
631
663
}
632
664
return val;
633
665
}
634
-
666
+ # if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
635
667
uint Sql::fetchUInt (const QSqlDatabase &sqlCon, const QString &sql, const QVector<int64_t > ¶ms)
636
668
{
637
669
bool ok;
@@ -643,7 +675,7 @@ uint Sql::fetchUInt(const QSqlDatabase &sqlCon, const QString &sql, const QVecto
643
675
}
644
676
return val;
645
677
}
646
-
678
+ # endif
647
679
648
680
QString Sql::fetchString (const QSqlDatabase & sqlCon, const QString &sql, const QList<QVariant> ¶ms)
649
681
{
@@ -678,15 +710,15 @@ int64_t Sql::insert(const QSqlDatabase &sqlCon, const QString &sql, const QList<
678
710
}
679
711
680
712
QString SqlUtil4::Sql::getDebugString (const QString &sql, const QList<QVariant> ¶ms) {
681
- QString result (sql);
682
- for (int i = 0 ; i < params.size (); i++) {
683
- // qDebug()<<params.at(i).typeName();
684
- QString v = QString (params.at (i).typeName ()) != QStringLiteral (" QByteArray" ) ? params.at (i).toString () : QString (params.at (i).toByteArray ().toHex ());
685
- QRegExp e (" ^[0-9][0-9]*$" );
686
- result.replace (result.indexOf (QChar (' ?' )), 1 ,
687
- v.isNull () ? QStringLiteral (" NULL" ) : e.exactMatch (v ) ? v : QStringLiteral (" '" ) + v + QStringLiteral (" '" ));
688
- }
689
- return result;
713
+ QString result (sql);
714
+ for (int i = 0 ; i < params.size (); i++) {
715
+ // qDebug()<<params.at(i).typeName();
716
+ QString v = QString (params.at (i).typeName ()) != QStringLiteral (" QByteArray" ) ? params.at (i).toString () : QString (params.at (i).toByteArray ().toHex ());
717
+ QRegularExpression e (" ^[0-9][0-9]*$" );
718
+ result.replace (result.indexOf (QChar (' ?' )), 1 ,
719
+ v.isNull () ? QStringLiteral (" NULL" ) : e.match (v). hasMatch ( ) ? v : QStringLiteral (" '" ) + v + QStringLiteral (" '" ));
720
+ }
721
+ return result;
690
722
}
691
723
692
724
@@ -698,8 +730,8 @@ QString SqlUtil4::Sql::getDebugString(const QString &sql, QList<QPair<QString,QV
698
730
for (int i = 0 ; i < params.size (); i++) {
699
731
// qDebug()<<params.at(i).typeName();
700
732
QString v = QString (params[i].second .typeName ()) != QStringLiteral (" QByteArray" ) ? params[i].second .toString () : QString (params[i].second .toByteArray ().toHex ());
701
- QRegExp e (" ^[0-9][0-9]*$" );
702
- result.replace (" :" +params[i].first , v.isNull () ? QStringLiteral (" NULL" ) : e. exactMatch (v ) ? v : QStringLiteral (" '" ) + v + QStringLiteral (" '" ));
733
+ QRegularExpression e (" ^[0-9][0-9]*$" );
734
+ result.replace (" :" +params[i].first , v.isNull () ? QStringLiteral (" NULL" ) :e. match (v). hasMatch ( ) ? v : QStringLiteral (" '" ) + v + QStringLiteral (" '" ));
703
735
}
704
736
return result;
705
737
}
0 commit comments