@@ -31,11 +31,13 @@ namespace sqlite {
31
31
32
32
class sqlite_exception : public std ::runtime_error {
33
33
public:
34
- sqlite_exception (const char * msg, int code = -1 ): runtime_error(msg), code(code) {}
35
- sqlite_exception (int code): runtime_error(sqlite3_errstr(code)), code(code) {}
34
+ sqlite_exception (const char * msg, std::string sql, int code = -1 ): runtime_error(msg), code(code), sql(sql ) {}
35
+ sqlite_exception (int code, std::string sql ): runtime_error(sqlite3_errstr(code)), code(code), sql(sql ) {}
36
36
int get_code () {return code;}
37
+ std::string get_sql () {return sql;}
37
38
private:
38
39
int code;
40
+ std::string sql;
39
41
};
40
42
41
43
namespace exceptions {
@@ -75,34 +77,34 @@ namespace sqlite {
75
77
class more_rows : public sqlite_exception { using sqlite_exception::sqlite_exception; };
76
78
class no_rows : public sqlite_exception { using sqlite_exception::sqlite_exception; };
77
79
78
- static void throw_sqlite_error (const int & error_code) {
79
- if (error_code == SQLITE_ERROR) throw exceptions::error (error_code);
80
- else if (error_code == SQLITE_INTERNAL) throw exceptions::internal (error_code);
81
- else if (error_code == SQLITE_PERM) throw exceptions::perm (error_code);
82
- else if (error_code == SQLITE_ABORT) throw exceptions::abort (error_code);
83
- else if (error_code == SQLITE_BUSY) throw exceptions::busy (error_code);
84
- else if (error_code == SQLITE_LOCKED) throw exceptions::locked (error_code);
85
- else if (error_code == SQLITE_NOMEM) throw exceptions::nomem (error_code);
86
- else if (error_code == SQLITE_READONLY) throw exceptions::readonly (error_code);
87
- else if (error_code == SQLITE_INTERRUPT) throw exceptions::interrupt (error_code);
88
- else if (error_code == SQLITE_IOERR) throw exceptions::ioerr (error_code);
89
- else if (error_code == SQLITE_CORRUPT) throw exceptions::corrupt (error_code);
90
- else if (error_code == SQLITE_NOTFOUND) throw exceptions::notfound (error_code);
91
- else if (error_code == SQLITE_FULL) throw exceptions::full (error_code);
92
- else if (error_code == SQLITE_CANTOPEN) throw exceptions::cantopen (error_code);
93
- else if (error_code == SQLITE_PROTOCOL) throw exceptions::protocol (error_code);
94
- else if (error_code == SQLITE_EMPTY) throw exceptions::empty (error_code);
95
- else if (error_code == SQLITE_SCHEMA) throw exceptions::schema (error_code);
96
- else if (error_code == SQLITE_TOOBIG) throw exceptions::toobig (error_code);
97
- else if (error_code == SQLITE_CONSTRAINT) throw exceptions::constraint (error_code);
98
- else if (error_code == SQLITE_MISMATCH) throw exceptions::mismatch (error_code);
99
- else if (error_code == SQLITE_MISUSE) throw exceptions::misuse (error_code);
100
- else if (error_code == SQLITE_NOLFS) throw exceptions::nolfs (error_code);
101
- else if (error_code == SQLITE_AUTH) throw exceptions::auth (error_code);
102
- else if (error_code == SQLITE_FORMAT) throw exceptions::format (error_code);
103
- else if (error_code == SQLITE_RANGE) throw exceptions::range (error_code);
104
- else if (error_code == SQLITE_NOTADB) throw exceptions::notadb (error_code);
105
- else throw sqlite_exception (error_code);
80
+ static void throw_sqlite_error (const int & error_code, const std::string &sql = " " ) {
81
+ if (error_code == SQLITE_ERROR) throw exceptions::error (error_code, sql );
82
+ else if (error_code == SQLITE_INTERNAL) throw exceptions::internal (error_code, sql );
83
+ else if (error_code == SQLITE_PERM) throw exceptions::perm (error_code, sql );
84
+ else if (error_code == SQLITE_ABORT) throw exceptions::abort (error_code, sql );
85
+ else if (error_code == SQLITE_BUSY) throw exceptions::busy (error_code, sql );
86
+ else if (error_code == SQLITE_LOCKED) throw exceptions::locked (error_code, sql );
87
+ else if (error_code == SQLITE_NOMEM) throw exceptions::nomem (error_code, sql );
88
+ else if (error_code == SQLITE_READONLY) throw exceptions::readonly (error_code, sql );
89
+ else if (error_code == SQLITE_INTERRUPT) throw exceptions::interrupt (error_code, sql );
90
+ else if (error_code == SQLITE_IOERR) throw exceptions::ioerr (error_code, sql );
91
+ else if (error_code == SQLITE_CORRUPT) throw exceptions::corrupt (error_code, sql );
92
+ else if (error_code == SQLITE_NOTFOUND) throw exceptions::notfound (error_code, sql );
93
+ else if (error_code == SQLITE_FULL) throw exceptions::full (error_code, sql );
94
+ else if (error_code == SQLITE_CANTOPEN) throw exceptions::cantopen (error_code, sql );
95
+ else if (error_code == SQLITE_PROTOCOL) throw exceptions::protocol (error_code, sql );
96
+ else if (error_code == SQLITE_EMPTY) throw exceptions::empty (error_code, sql );
97
+ else if (error_code == SQLITE_SCHEMA) throw exceptions::schema (error_code, sql );
98
+ else if (error_code == SQLITE_TOOBIG) throw exceptions::toobig (error_code, sql );
99
+ else if (error_code == SQLITE_CONSTRAINT) throw exceptions::constraint (error_code, sql );
100
+ else if (error_code == SQLITE_MISMATCH) throw exceptions::mismatch (error_code, sql );
101
+ else if (error_code == SQLITE_MISUSE) throw exceptions::misuse (error_code, sql );
102
+ else if (error_code == SQLITE_NOLFS) throw exceptions::nolfs (error_code, sql );
103
+ else if (error_code == SQLITE_AUTH) throw exceptions::auth (error_code, sql );
104
+ else if (error_code == SQLITE_FORMAT) throw exceptions::format (error_code, sql );
105
+ else if (error_code == SQLITE_RANGE) throw exceptions::range (error_code, sql );
106
+ else if (error_code == SQLITE_NOTADB) throw exceptions::notadb (error_code, sql );
107
+ else throw sqlite_exception (error_code, sql );
106
108
}
107
109
}
108
110
@@ -150,10 +152,24 @@ namespace sqlite {
150
152
while ((hresult = sqlite3_step (_stmt.get ())) == SQLITE_ROW) {}
151
153
152
154
if (hresult != SQLITE_DONE) {
153
- exceptions::throw_sqlite_error (hresult);
155
+ exceptions::throw_sqlite_error (hresult, sql () );
154
156
}
155
157
used (true ); /* prevent from executing again when goes out of scope */
156
158
}
159
+
160
+ std::string sql () {
161
+ #if SQLITE_VERSION_NUMBER >= 3014000
162
+ auto sqlite_deleter = [](void *ptr) {sqlite3_free (ptr);};
163
+ std::unique_ptr<char , decltype (sqlite_deleter)> str (sqlite3_expanded_sql (_stmt.get ()), sqlite_deleter);
164
+ return str ? str.get () : original_sql ();
165
+ #else
166
+ return original_sql ();
167
+ #endif
168
+ }
169
+
170
+ std::string original_sql () {
171
+ return sqlite3_sql (_stmt.get ());
172
+ }
157
173
158
174
void used (bool state) { execution_started = state; }
159
175
bool used () const { return execution_started; }
@@ -176,7 +192,7 @@ namespace sqlite {
176
192
}
177
193
178
194
if (hresult != SQLITE_DONE) {
179
- exceptions::throw_sqlite_error (hresult);
195
+ exceptions::throw_sqlite_error (hresult, sql () );
180
196
}
181
197
reset ();
182
198
}
@@ -188,15 +204,15 @@ namespace sqlite {
188
204
if ((hresult = sqlite3_step (_stmt.get ())) == SQLITE_ROW) {
189
205
call_back ();
190
206
} else if (hresult == SQLITE_DONE) {
191
- throw exceptions::no_rows (" no rows to extract: exactly 1 row expected" , SQLITE_DONE);
207
+ throw exceptions::no_rows (" no rows to extract: exactly 1 row expected" , sql (), SQLITE_DONE);
192
208
}
193
209
194
210
if ((hresult = sqlite3_step (_stmt.get ())) == SQLITE_ROW) {
195
- throw exceptions::more_rows (" not all rows extracted" , SQLITE_ROW);
211
+ throw exceptions::more_rows (" not all rows extracted" , sql (), SQLITE_ROW);
196
212
}
197
213
198
214
if (hresult != SQLITE_DONE) {
199
- exceptions::throw_sqlite_error (hresult);
215
+ exceptions::throw_sqlite_error (hresult, sql () );
200
216
}
201
217
reset ();
202
218
}
@@ -213,7 +229,7 @@ namespace sqlite {
213
229
int hresult;
214
230
sqlite3_stmt* tmp = nullptr ;
215
231
hresult = sqlite3_prepare_v2 (_db.get (), sql.data (), -1 , &tmp, nullptr );
216
- if ((hresult) != SQLITE_OK) exceptions::throw_sqlite_error (hresult);
232
+ if ((hresult) != SQLITE_OK) exceptions::throw_sqlite_error (hresult, sql );
217
233
return tmp;
218
234
}
219
235
@@ -526,7 +542,7 @@ namespace sqlite {
526
542
inline database_binder& operator <<(database_binder& db, const int & val) {
527
543
int hresult;
528
544
if ((hresult = sqlite3_bind_int (db._stmt .get (), db._inx , val)) != SQLITE_OK) {
529
- exceptions::throw_sqlite_error (hresult);
545
+ exceptions::throw_sqlite_error (hresult, db. sql () );
530
546
}
531
547
++db._inx ;
532
548
return db;
@@ -553,7 +569,7 @@ namespace sqlite {
553
569
inline database_binder& operator <<(database_binder& db, const sqlite_int64& val) {
554
570
int hresult;
555
571
if ((hresult = sqlite3_bind_int64 (db._stmt .get (), db._inx , val)) != SQLITE_OK) {
556
- exceptions::throw_sqlite_error (hresult);
572
+ exceptions::throw_sqlite_error (hresult, db. sql () );
557
573
}
558
574
559
575
++db._inx ;
@@ -581,7 +597,7 @@ namespace sqlite {
581
597
inline database_binder& operator <<(database_binder& db, const float & val) {
582
598
int hresult;
583
599
if ((hresult = sqlite3_bind_double (db._stmt .get (), db._inx , double (val))) != SQLITE_OK) {
584
- exceptions::throw_sqlite_error (hresult);
600
+ exceptions::throw_sqlite_error (hresult, db. sql () );
585
601
}
586
602
587
603
++db._inx ;
@@ -609,7 +625,7 @@ namespace sqlite {
609
625
inline database_binder& operator <<(database_binder& db, const double & val) {
610
626
int hresult;
611
627
if ((hresult = sqlite3_bind_double (db._stmt .get (), db._inx , val)) != SQLITE_OK) {
612
- exceptions::throw_sqlite_error (hresult);
628
+ exceptions::throw_sqlite_error (hresult, db. sql () );
613
629
}
614
630
615
631
++db._inx ;
@@ -639,7 +655,7 @@ namespace sqlite {
639
655
int bytes = vec.size () * sizeof (T);
640
656
int hresult;
641
657
if ((hresult = sqlite3_bind_blob (db._stmt .get (), db._inx , buf, bytes, SQLITE_TRANSIENT)) != SQLITE_OK) {
642
- exceptions::throw_sqlite_error (hresult);
658
+ exceptions::throw_sqlite_error (hresult, db. sql () );
643
659
}
644
660
++db._inx ;
645
661
return db;
@@ -672,7 +688,7 @@ namespace sqlite {
672
688
inline database_binder& operator <<(database_binder& db, std::nullptr_t ) {
673
689
int hresult;
674
690
if ((hresult = sqlite3_bind_null (db._stmt .get (), db._inx )) != SQLITE_OK) {
675
- exceptions::throw_sqlite_error (hresult);
691
+ exceptions::throw_sqlite_error (hresult, db. sql () );
676
692
}
677
693
++db._inx ;
678
694
return db;
@@ -734,7 +750,7 @@ namespace sqlite {
734
750
inline database_binder& operator <<(database_binder& db, const std::string& txt) {
735
751
int hresult;
736
752
if ((hresult = sqlite3_bind_text (db._stmt .get (), db._inx , txt.data (), -1 , SQLITE_TRANSIENT)) != SQLITE_OK) {
737
- exceptions::throw_sqlite_error (hresult);
753
+ exceptions::throw_sqlite_error (hresult, db. sql () );
738
754
}
739
755
740
756
++db._inx ;
@@ -765,7 +781,7 @@ namespace sqlite {
765
781
inline database_binder& operator <<(database_binder& db, const std::u16string& txt) {
766
782
int hresult;
767
783
if ((hresult = sqlite3_bind_text16 (db._stmt .get (), db._inx , txt.data (), -1 , SQLITE_TRANSIENT)) != SQLITE_OK) {
768
- exceptions::throw_sqlite_error (hresult);
784
+ exceptions::throw_sqlite_error (hresult, db. sql () );
769
785
}
770
786
771
787
++db._inx ;
@@ -782,7 +798,7 @@ namespace sqlite {
782
798
}
783
799
int hresult;
784
800
if ((hresult = sqlite3_bind_null (db._stmt .get (), db._inx )) != SQLITE_OK) {
785
- exceptions::throw_sqlite_error (hresult);
801
+ exceptions::throw_sqlite_error (hresult, db. sql () );
786
802
}
787
803
788
804
++db._inx ;
@@ -823,7 +839,7 @@ namespace sqlite {
823
839
}
824
840
int hresult;
825
841
if ((hresult = sqlite3_bind_null (db._stmt .get (), db._inx )) != SQLITE_OK) {
826
- exceptions::throw_sqlite_error (hresult);
842
+ exceptions::throw_sqlite_error (hresult, db. sql () );
827
843
}
828
844
829
845
++db._inx ;
0 commit comments