diff options
| author | Lars Ingebrigtsen | 2022-10-11 02:18:19 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2022-10-11 02:18:19 +0200 |
| commit | 36ab1644964ae5a933bd9808536f60d4ae64c99f (patch) | |
| tree | c488176bb145222d1881e64cb7d1e1a893975d82 /src/sqlite.c | |
| parent | ebc19f56aaeb98b834eea1ce8768ca13bed8578c (diff) | |
| download | emacs-36ab1644964ae5a933bd9808536f60d4ae64c99f.tar.gz emacs-36ab1644964ae5a933bd9808536f60d4ae64c99f.zip | |
Improve error message from sqlite-execute
* src/sqlite.c (sqlite_prepare_errmsg): New function.
(Fsqlite_execute): Use it to get the same error format for both
execute and select.
(Fsqlite_select): Factored out from here.
Diffstat (limited to 'src/sqlite.c')
| -rw-r--r-- | src/sqlite.c | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/sqlite.c b/src/sqlite.c index 65b1dc492f6..1526e344e53 100644 --- a/src/sqlite.c +++ b/src/sqlite.c | |||
| @@ -421,6 +421,19 @@ row_to_value (sqlite3_stmt *stmt) | |||
| 421 | return Fnreverse (values); | 421 | return Fnreverse (values); |
| 422 | } | 422 | } |
| 423 | 423 | ||
| 424 | static Lisp_Object | ||
| 425 | sqlite_prepare_errmsg (int code, sqlite3 *sdb) | ||
| 426 | { | ||
| 427 | Lisp_Object errmsg = build_string (sqlite3_errstr (code)); | ||
| 428 | /* More details about what went wrong. */ | ||
| 429 | const char *sql_error = sqlite3_errmsg (sdb); | ||
| 430 | if (sql_error) | ||
| 431 | return CALLN (Fformat, build_string ("%s (%s)"), | ||
| 432 | errmsg, build_string (sql_error)); | ||
| 433 | else | ||
| 434 | return errmsg; | ||
| 435 | } | ||
| 436 | |||
| 424 | DEFUN ("sqlite-execute", Fsqlite_execute, Ssqlite_execute, 2, 3, 0, | 437 | DEFUN ("sqlite-execute", Fsqlite_execute, Ssqlite_execute, 2, 3, 0, |
| 425 | doc: /* Execute a non-select SQL statement. | 438 | doc: /* Execute a non-select SQL statement. |
| 426 | If VALUES is non-nil, it should be a vector or a list of values | 439 | If VALUES is non-nil, it should be a vector or a list of values |
| @@ -437,8 +450,8 @@ Value is the number of affected rows. */) | |||
| 437 | xsignal1 (Qerror, build_string ("VALUES must be a list or a vector")); | 450 | xsignal1 (Qerror, build_string ("VALUES must be a list or a vector")); |
| 438 | 451 | ||
| 439 | sqlite3 *sdb = XSQLITE (db)->db; | 452 | sqlite3 *sdb = XSQLITE (db)->db; |
| 440 | const char *errmsg = NULL; | 453 | Lisp_Object errmsg = Qnil, |
| 441 | Lisp_Object encoded = encode_string (query); | 454 | encoded = encode_string (query); |
| 442 | sqlite3_stmt *stmt = NULL; | 455 | sqlite3_stmt *stmt = NULL; |
| 443 | 456 | ||
| 444 | /* We only execute the first statement -- if there's several | 457 | /* We only execute the first statement -- if there's several |
| @@ -453,7 +466,7 @@ Value is the number of affected rows. */) | |||
| 453 | sqlite3_reset (stmt); | 466 | sqlite3_reset (stmt); |
| 454 | } | 467 | } |
| 455 | 468 | ||
| 456 | errmsg = sqlite3_errmsg (sdb); | 469 | errmsg = sqlite_prepare_errmsg (ret, sdb); |
| 457 | goto exit; | 470 | goto exit; |
| 458 | } | 471 | } |
| 459 | 472 | ||
| @@ -463,7 +476,7 @@ Value is the number of affected rows. */) | |||
| 463 | const char *err = bind_values (sdb, stmt, values); | 476 | const char *err = bind_values (sdb, stmt, values); |
| 464 | if (err != NULL) | 477 | if (err != NULL) |
| 465 | { | 478 | { |
| 466 | errmsg = err; | 479 | errmsg = build_string (err); |
| 467 | goto exit; | 480 | goto exit; |
| 468 | } | 481 | } |
| 469 | } | 482 | } |
| @@ -487,13 +500,13 @@ Value is the number of affected rows. */) | |||
| 487 | return rows; | 500 | return rows; |
| 488 | } | 501 | } |
| 489 | else | 502 | else |
| 490 | errmsg = sqlite3_errmsg (sdb); | 503 | errmsg = build_string (sqlite3_errmsg (sdb)); |
| 491 | 504 | ||
| 492 | exit: | 505 | exit: |
| 493 | sqlite3_finalize (stmt); | 506 | sqlite3_finalize (stmt); |
| 494 | xsignal1 (ret == SQLITE_LOCKED || ret == SQLITE_BUSY? | 507 | xsignal1 (ret == SQLITE_LOCKED || ret == SQLITE_BUSY? |
| 495 | Qsqlite_locked_error: Qerror, | 508 | Qsqlite_locked_error: Qerror, |
| 496 | build_string (errmsg)); | 509 | errmsg); |
| 497 | } | 510 | } |
| 498 | 511 | ||
| 499 | static Lisp_Object | 512 | static Lisp_Object |
| @@ -540,12 +553,7 @@ which means that we return a set object that can be queried with | |||
| 540 | { | 553 | { |
| 541 | if (stmt) | 554 | if (stmt) |
| 542 | sqlite3_finalize (stmt); | 555 | sqlite3_finalize (stmt); |
| 543 | errmsg = build_string (sqlite3_errstr (ret)); | 556 | errmsg = sqlite_prepare_errmsg (ret, sdb); |
| 544 | /* More details about what went wrong. */ | ||
| 545 | const char *sql_error = sqlite3_errmsg (sdb); | ||
| 546 | if (sql_error) | ||
| 547 | errmsg = CALLN (Fformat, build_string ("%s (%s)"), | ||
| 548 | errmsg, build_string (sql_error)); | ||
| 549 | goto exit; | 557 | goto exit; |
| 550 | } | 558 | } |
| 551 | 559 | ||