Fossil SCM

Attempt to improve error messages resulting from SQL errors.

drh 2019-09-13 13:54 trunk
Commit b94e15cff72b105e9fa6ecbf404baeb4036827bd1b6fb75ed7459574fba2c361
1 file changed +28 -13
+28 -13
--- src/db.c
+++ src/db.c
@@ -88,10 +88,22 @@
8888
@ error Database\serror:\s%F(z)
8989
cgi_reply();
9090
}
9191
fossil_fatal("Database error: %s", z);
9292
}
93
+
94
+/*
95
+** Check a result code. If it is not SQLITE_OK, print the
96
+** corresponding error message and exit.
97
+*/
98
+static void db_check_result(int rc, Stmt *pStmt){
99
+ if( rc!=SQLITE_OK ){
100
+ db_err("SQL error (%d,%d: %s) while running [%s]",
101
+ rc, sqlite3_extended_errcode(g.db),
102
+ sqlite3_errmsg(g.db), blob_str(&pStmt->sql));
103
+ }
104
+}
93105
94106
/*
95107
** All static variable that a used by only this file are gathered into
96108
** the following structure.
97109
*/
@@ -478,11 +490,11 @@
478490
*/
479491
int db_reset(Stmt *pStmt){
480492
int rc;
481493
db_stats(pStmt);
482494
rc = sqlite3_reset(pStmt->pStmt);
483
- db_check_result(rc);
495
+ db_check_result(rc, pStmt);
484496
return rc;
485497
}
486498
int db_finalize(Stmt *pStmt){
487499
int rc;
488500
if( pStmt->pNext ){
@@ -496,11 +508,11 @@
496508
pStmt->pNext = 0;
497509
pStmt->pPrev = 0;
498510
db_stats(pStmt);
499511
blob_reset(&pStmt->sql);
500512
rc = sqlite3_finalize(pStmt->pStmt);
501
- db_check_result(rc);
513
+ db_check_result(rc, pStmt);
502514
pStmt->pStmt = 0;
503515
return rc;
504516
}
505517
506518
/*
@@ -576,30 +588,33 @@
576588
void db_ephemeral_blob(Stmt *pStmt, int N, Blob *pBlob){
577589
blob_init(pBlob, sqlite3_column_blob(pStmt->pStmt, N),
578590
sqlite3_column_bytes(pStmt->pStmt, N));
579591
}
580592
581
-/*
582
-** Check a result code. If it is not SQLITE_OK, print the
583
-** corresponding error message and exit.
584
-*/
585
-void db_check_result(int rc){
586
- if( rc!=SQLITE_OK ){
587
- db_err("SQL error: %s", sqlite3_errmsg(g.db));
588
- }
589
-}
590
-
591593
/*
592594
** Execute a single prepared statement until it finishes.
593595
*/
594596
int db_exec(Stmt *pStmt){
595597
int rc;
596598
while( (rc = db_step(pStmt))==SQLITE_ROW ){}
597599
rc = db_reset(pStmt);
598
- db_check_result(rc);
600
+ db_check_result(rc, pStmt);
599601
return rc;
600602
}
603
+
604
+/*
605
+** COMMAND: test-db-exec-error
606
+**
607
+** Invoke the db_exec() interface with an erroneous SQL statement
608
+** in order to verify the error handling logic.
609
+*/
610
+void db_test_db_exec_cmd(void){
611
+ Stmt err;
612
+ db_find_and_open_repository(0,0);
613
+ db_prepare(&err, "INSERT INTO repository.config(name) VALUES(NULL);");
614
+ db_exec(&err);
615
+}
601616
602617
/*
603618
** Print the output of one or more SQL queries on standard output.
604619
** This routine is used for debugging purposes only.
605620
*/
606621
--- src/db.c
+++ src/db.c
@@ -88,10 +88,22 @@
88 @ error Database\serror:\s%F(z)
89 cgi_reply();
90 }
91 fossil_fatal("Database error: %s", z);
92 }
 
 
 
 
 
 
 
 
 
 
 
 
93
94 /*
95 ** All static variable that a used by only this file are gathered into
96 ** the following structure.
97 */
@@ -478,11 +490,11 @@
478 */
479 int db_reset(Stmt *pStmt){
480 int rc;
481 db_stats(pStmt);
482 rc = sqlite3_reset(pStmt->pStmt);
483 db_check_result(rc);
484 return rc;
485 }
486 int db_finalize(Stmt *pStmt){
487 int rc;
488 if( pStmt->pNext ){
@@ -496,11 +508,11 @@
496 pStmt->pNext = 0;
497 pStmt->pPrev = 0;
498 db_stats(pStmt);
499 blob_reset(&pStmt->sql);
500 rc = sqlite3_finalize(pStmt->pStmt);
501 db_check_result(rc);
502 pStmt->pStmt = 0;
503 return rc;
504 }
505
506 /*
@@ -576,30 +588,33 @@
576 void db_ephemeral_blob(Stmt *pStmt, int N, Blob *pBlob){
577 blob_init(pBlob, sqlite3_column_blob(pStmt->pStmt, N),
578 sqlite3_column_bytes(pStmt->pStmt, N));
579 }
580
581 /*
582 ** Check a result code. If it is not SQLITE_OK, print the
583 ** corresponding error message and exit.
584 */
585 void db_check_result(int rc){
586 if( rc!=SQLITE_OK ){
587 db_err("SQL error: %s", sqlite3_errmsg(g.db));
588 }
589 }
590
591 /*
592 ** Execute a single prepared statement until it finishes.
593 */
594 int db_exec(Stmt *pStmt){
595 int rc;
596 while( (rc = db_step(pStmt))==SQLITE_ROW ){}
597 rc = db_reset(pStmt);
598 db_check_result(rc);
599 return rc;
600 }
 
 
 
 
 
 
 
 
 
 
 
 
 
601
602 /*
603 ** Print the output of one or more SQL queries on standard output.
604 ** This routine is used for debugging purposes only.
605 */
606
--- src/db.c
+++ src/db.c
@@ -88,10 +88,22 @@
88 @ error Database\serror:\s%F(z)
89 cgi_reply();
90 }
91 fossil_fatal("Database error: %s", z);
92 }
93
94 /*
95 ** Check a result code. If it is not SQLITE_OK, print the
96 ** corresponding error message and exit.
97 */
98 static void db_check_result(int rc, Stmt *pStmt){
99 if( rc!=SQLITE_OK ){
100 db_err("SQL error (%d,%d: %s) while running [%s]",
101 rc, sqlite3_extended_errcode(g.db),
102 sqlite3_errmsg(g.db), blob_str(&pStmt->sql));
103 }
104 }
105
106 /*
107 ** All static variable that a used by only this file are gathered into
108 ** the following structure.
109 */
@@ -478,11 +490,11 @@
490 */
491 int db_reset(Stmt *pStmt){
492 int rc;
493 db_stats(pStmt);
494 rc = sqlite3_reset(pStmt->pStmt);
495 db_check_result(rc, pStmt);
496 return rc;
497 }
498 int db_finalize(Stmt *pStmt){
499 int rc;
500 if( pStmt->pNext ){
@@ -496,11 +508,11 @@
508 pStmt->pNext = 0;
509 pStmt->pPrev = 0;
510 db_stats(pStmt);
511 blob_reset(&pStmt->sql);
512 rc = sqlite3_finalize(pStmt->pStmt);
513 db_check_result(rc, pStmt);
514 pStmt->pStmt = 0;
515 return rc;
516 }
517
518 /*
@@ -576,30 +588,33 @@
588 void db_ephemeral_blob(Stmt *pStmt, int N, Blob *pBlob){
589 blob_init(pBlob, sqlite3_column_blob(pStmt->pStmt, N),
590 sqlite3_column_bytes(pStmt->pStmt, N));
591 }
592
 
 
 
 
 
 
 
 
 
 
593 /*
594 ** Execute a single prepared statement until it finishes.
595 */
596 int db_exec(Stmt *pStmt){
597 int rc;
598 while( (rc = db_step(pStmt))==SQLITE_ROW ){}
599 rc = db_reset(pStmt);
600 db_check_result(rc, pStmt);
601 return rc;
602 }
603
604 /*
605 ** COMMAND: test-db-exec-error
606 **
607 ** Invoke the db_exec() interface with an erroneous SQL statement
608 ** in order to verify the error handling logic.
609 */
610 void db_test_db_exec_cmd(void){
611 Stmt err;
612 db_find_and_open_repository(0,0);
613 db_prepare(&err, "INSERT INTO repository.config(name) VALUES(NULL);");
614 db_exec(&err);
615 }
616
617 /*
618 ** Print the output of one or more SQL queries on standard output.
619 ** This routine is used for debugging purposes only.
620 */
621

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button