diff options
Diffstat (limited to 'src/sqlite.c')
| -rw-r--r-- | src/sqlite.c | 72 |
1 files changed, 41 insertions, 31 deletions
diff --git a/src/sqlite.c b/src/sqlite.c index 1526e344e53..08bf696b8ca 100644 --- a/src/sqlite.c +++ b/src/sqlite.c | |||
| @@ -50,6 +50,7 @@ DEF_DLL_FN (SQLITE_API int, sqlite3_bind_int64, | |||
| 50 | DEF_DLL_FN (SQLITE_API int, sqlite3_bind_double, (sqlite3_stmt*, int, double)); | 50 | DEF_DLL_FN (SQLITE_API int, sqlite3_bind_double, (sqlite3_stmt*, int, double)); |
| 51 | DEF_DLL_FN (SQLITE_API int, sqlite3_bind_null, (sqlite3_stmt*, int)); | 51 | DEF_DLL_FN (SQLITE_API int, sqlite3_bind_null, (sqlite3_stmt*, int)); |
| 52 | DEF_DLL_FN (SQLITE_API int, sqlite3_bind_int, (sqlite3_stmt*, int, int)); | 52 | DEF_DLL_FN (SQLITE_API int, sqlite3_bind_int, (sqlite3_stmt*, int, int)); |
| 53 | DEF_DLL_FN (SQLITE_API int, sqlite3_extended_errcode, (sqlite3*)); | ||
| 53 | DEF_DLL_FN (SQLITE_API const char*, sqlite3_errmsg, (sqlite3*)); | 54 | DEF_DLL_FN (SQLITE_API const char*, sqlite3_errmsg, (sqlite3*)); |
| 54 | DEF_DLL_FN (SQLITE_API const char*, sqlite3_errstr, (int)); | 55 | DEF_DLL_FN (SQLITE_API const char*, sqlite3_errstr, (int)); |
| 55 | DEF_DLL_FN (SQLITE_API int, sqlite3_step, (sqlite3_stmt*)); | 56 | DEF_DLL_FN (SQLITE_API int, sqlite3_step, (sqlite3_stmt*)); |
| @@ -88,6 +89,7 @@ DEF_DLL_FN (SQLITE_API int, sqlite3_load_extension, | |||
| 88 | # undef sqlite3_bind_double | 89 | # undef sqlite3_bind_double |
| 89 | # undef sqlite3_bind_null | 90 | # undef sqlite3_bind_null |
| 90 | # undef sqlite3_bind_int | 91 | # undef sqlite3_bind_int |
| 92 | # undef sqlite3_extended_errcode | ||
| 91 | # undef sqlite3_errmsg | 93 | # undef sqlite3_errmsg |
| 92 | # undef sqlite3_errstr | 94 | # undef sqlite3_errstr |
| 93 | # undef sqlite3_step | 95 | # undef sqlite3_step |
| @@ -113,6 +115,7 @@ DEF_DLL_FN (SQLITE_API int, sqlite3_load_extension, | |||
| 113 | # define sqlite3_bind_double fn_sqlite3_bind_double | 115 | # define sqlite3_bind_double fn_sqlite3_bind_double |
| 114 | # define sqlite3_bind_null fn_sqlite3_bind_null | 116 | # define sqlite3_bind_null fn_sqlite3_bind_null |
| 115 | # define sqlite3_bind_int fn_sqlite3_bind_int | 117 | # define sqlite3_bind_int fn_sqlite3_bind_int |
| 118 | # define sqlite3_extended_errcode fn_sqlite3_extended_errcode | ||
| 116 | # define sqlite3_errmsg fn_sqlite3_errmsg | 119 | # define sqlite3_errmsg fn_sqlite3_errmsg |
| 117 | # define sqlite3_errstr fn_sqlite3_errstr | 120 | # define sqlite3_errstr fn_sqlite3_errstr |
| 118 | # define sqlite3_step fn_sqlite3_step | 121 | # define sqlite3_step fn_sqlite3_step |
| @@ -141,6 +144,7 @@ load_dll_functions (HMODULE library) | |||
| 141 | LOAD_DLL_FN (library, sqlite3_bind_double); | 144 | LOAD_DLL_FN (library, sqlite3_bind_double); |
| 142 | LOAD_DLL_FN (library, sqlite3_bind_null); | 145 | LOAD_DLL_FN (library, sqlite3_bind_null); |
| 143 | LOAD_DLL_FN (library, sqlite3_bind_int); | 146 | LOAD_DLL_FN (library, sqlite3_bind_int); |
| 147 | LOAD_DLL_FN (library, sqlite3_extended_errcode); | ||
| 144 | LOAD_DLL_FN (library, sqlite3_errmsg); | 148 | LOAD_DLL_FN (library, sqlite3_errmsg); |
| 145 | LOAD_DLL_FN (library, sqlite3_errstr); | 149 | LOAD_DLL_FN (library, sqlite3_errstr); |
| 146 | LOAD_DLL_FN (library, sqlite3_step); | 150 | LOAD_DLL_FN (library, sqlite3_step); |
| @@ -233,13 +237,13 @@ check_sqlite (Lisp_Object db, bool is_statement) | |||
| 233 | init_sqlite_functions (); | 237 | init_sqlite_functions (); |
| 234 | CHECK_SQLITE (db); | 238 | CHECK_SQLITE (db); |
| 235 | if (is_statement && !XSQLITE (db)->is_statement) | 239 | if (is_statement && !XSQLITE (db)->is_statement) |
| 236 | xsignal1 (Qerror, build_string ("Invalid set object")); | 240 | xsignal1 (Qsqlite_error, build_string ("Invalid set object")); |
| 237 | else if (!is_statement && XSQLITE (db)->is_statement) | 241 | else if (!is_statement && XSQLITE (db)->is_statement) |
| 238 | xsignal1 (Qerror, build_string ("Invalid database object")); | 242 | xsignal1 (Qsqlite_error, build_string ("Invalid database object")); |
| 239 | if (!is_statement && !XSQLITE (db)->db) | 243 | if (!is_statement && !XSQLITE (db)->db) |
| 240 | xsignal1 (Qerror, build_string ("Database closed")); | 244 | xsignal1 (Qsqlite_error, build_string ("Database closed")); |
| 241 | else if (is_statement && !XSQLITE (db)->db) | 245 | else if (is_statement && !XSQLITE (db)->db) |
| 242 | xsignal1 (Qerror, build_string ("Statement closed")); | 246 | xsignal1 (Qsqlite_error, build_string ("Statement closed")); |
| 243 | } | 247 | } |
| 244 | 248 | ||
| 245 | static int db_count = 0; | 249 | static int db_count = 0; |
| @@ -259,7 +263,7 @@ If FILE is nil, an in-memory database will be opened instead. */) | |||
| 259 | #endif | 263 | #endif |
| 260 | 264 | ||
| 261 | if (!init_sqlite_functions ()) | 265 | if (!init_sqlite_functions ()) |
| 262 | xsignal1 (Qerror, build_string ("sqlite support is not available")); | 266 | xsignal1 (Qsqlite_error, build_string ("sqlite support is not available")); |
| 263 | 267 | ||
| 264 | if (!NILP (file)) | 268 | if (!NILP (file)) |
| 265 | name = ENCODE_FILE (Fexpand_file_name (file, Qnil)); | 269 | name = ENCODE_FILE (Fexpand_file_name (file, Qnil)); |
| @@ -272,7 +276,7 @@ If FILE is nil, an in-memory database will be opened instead. */) | |||
| 272 | name = CALLN (Fformat, memory_fmt, make_int (++db_count)); | 276 | name = CALLN (Fformat, memory_fmt, make_int (++db_count)); |
| 273 | flags |= SQLITE_OPEN_MEMORY; | 277 | flags |= SQLITE_OPEN_MEMORY; |
| 274 | #else | 278 | #else |
| 275 | xsignal1 (Qerror, build_string ("sqlite in-memory is not available")); | 279 | xsignal1 (Qsqlite_error, build_string ("sqlite in-memory is not available")); |
| 276 | #endif | 280 | #endif |
| 277 | } | 281 | } |
| 278 | 282 | ||
| @@ -342,7 +346,7 @@ bind_values (sqlite3 *db, sqlite3_stmt *stmt, Lisp_Object values) | |||
| 342 | if (blob) | 346 | if (blob) |
| 343 | { | 347 | { |
| 344 | if (SBYTES (value) != SCHARS (value)) | 348 | if (SBYTES (value) != SCHARS (value)) |
| 345 | xsignal1 (Qerror, build_string ("BLOB values must be unibyte")); | 349 | xsignal1 (Qsqlite_error, build_string ("BLOB values must be unibyte")); |
| 346 | ret = sqlite3_bind_blob (stmt, i + 1, | 350 | ret = sqlite3_bind_blob (stmt, i + 1, |
| 347 | SSDATA (value), SBYTES (value), | 351 | SSDATA (value), SBYTES (value), |
| 348 | NULL); | 352 | NULL); |
| @@ -422,16 +426,15 @@ row_to_value (sqlite3_stmt *stmt) | |||
| 422 | } | 426 | } |
| 423 | 427 | ||
| 424 | static Lisp_Object | 428 | static Lisp_Object |
| 425 | sqlite_prepare_errmsg (int code, sqlite3 *sdb) | 429 | sqlite_prepare_errdata (int code, sqlite3 *sdb) |
| 426 | { | 430 | { |
| 427 | Lisp_Object errmsg = build_string (sqlite3_errstr (code)); | 431 | Lisp_Object errstr = build_string (sqlite3_errstr (code)); |
| 432 | Lisp_Object errcode = make_fixnum (code); | ||
| 428 | /* More details about what went wrong. */ | 433 | /* More details about what went wrong. */ |
| 429 | const char *sql_error = sqlite3_errmsg (sdb); | 434 | Lisp_Object ext_errcode = make_fixnum (sqlite3_extended_errcode (sdb)); |
| 430 | if (sql_error) | 435 | const char *errmsg = sqlite3_errmsg (sdb); |
| 431 | return CALLN (Fformat, build_string ("%s (%s)"), | 436 | return list4 (errstr, errmsg ? build_string (errmsg) : Qnil, |
| 432 | errmsg, build_string (sql_error)); | 437 | errcode, ext_errcode); |
| 433 | else | ||
| 434 | return errmsg; | ||
| 435 | } | 438 | } |
| 436 | 439 | ||
| 437 | DEFUN ("sqlite-execute", Fsqlite_execute, Ssqlite_execute, 2, 3, 0, | 440 | DEFUN ("sqlite-execute", Fsqlite_execute, Ssqlite_execute, 2, 3, 0, |
| @@ -447,7 +450,7 @@ Value is the number of affected rows. */) | |||
| 447 | check_sqlite (db, false); | 450 | check_sqlite (db, false); |
| 448 | CHECK_STRING (query); | 451 | CHECK_STRING (query); |
| 449 | if (!(NILP (values) || CONSP (values) || VECTORP (values))) | 452 | if (!(NILP (values) || CONSP (values) || VECTORP (values))) |
| 450 | xsignal1 (Qerror, build_string ("VALUES must be a list or a vector")); | 453 | xsignal1 (Qsqlite_error, build_string ("VALUES must be a list or a vector")); |
| 451 | 454 | ||
| 452 | sqlite3 *sdb = XSQLITE (db)->db; | 455 | sqlite3 *sdb = XSQLITE (db)->db; |
| 453 | Lisp_Object errmsg = Qnil, | 456 | Lisp_Object errmsg = Qnil, |
| @@ -466,7 +469,7 @@ Value is the number of affected rows. */) | |||
| 466 | sqlite3_reset (stmt); | 469 | sqlite3_reset (stmt); |
| 467 | } | 470 | } |
| 468 | 471 | ||
| 469 | errmsg = sqlite_prepare_errmsg (ret, sdb); | 472 | errmsg = sqlite_prepare_errdata (ret, sdb); |
| 470 | goto exit; | 473 | goto exit; |
| 471 | } | 474 | } |
| 472 | 475 | ||
| @@ -505,7 +508,7 @@ Value is the number of affected rows. */) | |||
| 505 | exit: | 508 | exit: |
| 506 | sqlite3_finalize (stmt); | 509 | sqlite3_finalize (stmt); |
| 507 | xsignal1 (ret == SQLITE_LOCKED || ret == SQLITE_BUSY? | 510 | xsignal1 (ret == SQLITE_LOCKED || ret == SQLITE_BUSY? |
| 508 | Qsqlite_locked_error: Qerror, | 511 | Qsqlite_locked_error: Qsqlite_error, |
| 509 | errmsg); | 512 | errmsg); |
| 510 | } | 513 | } |
| 511 | 514 | ||
| @@ -525,14 +528,15 @@ DEFUN ("sqlite-select", Fsqlite_select, Ssqlite_select, 2, 4, 0, | |||
| 525 | If VALUES is non-nil, it should be a list or a vector specifying the | 528 | If VALUES is non-nil, it should be a list or a vector specifying the |
| 526 | values that will be interpolated into a parameterized statement. | 529 | values that will be interpolated into a parameterized statement. |
| 527 | 530 | ||
| 528 | By default, the return value is a list where the first element is a | 531 | By default, the return value is a list, whose contents depend on |
| 529 | list of column names, and the rest of the elements are the matching data. | 532 | the value of the optional argument RETURN-TYPE. |
| 530 | 533 | ||
| 531 | RETURN-TYPE can be either nil (which means that the matching data | 534 | If RETURN-TYPE is nil or omitted, the function returns a list of rows |
| 532 | should be returned as a list of rows), or `full' (the same, but the | 535 | matching QUERY. If RETURN-TYPE is `full', the function returns a |
| 533 | first element in the return list will be the column names), or `set', | 536 | list whose first element is the list of column names, and the rest |
| 534 | which means that we return a set object that can be queried with | 537 | of the elements are the rows matching QUERY. If RETURN-TYPE is `set', |
| 535 | `sqlite-next' and other functions to get the data. */) | 538 | the function returns a set object that can be queried with functions |
| 539 | like `sqlite-next' etc., in order to get the data. */) | ||
| 536 | (Lisp_Object db, Lisp_Object query, Lisp_Object values, | 540 | (Lisp_Object db, Lisp_Object query, Lisp_Object values, |
| 537 | Lisp_Object return_type) | 541 | Lisp_Object return_type) |
| 538 | { | 542 | { |
| @@ -540,7 +544,7 @@ which means that we return a set object that can be queried with | |||
| 540 | CHECK_STRING (query); | 544 | CHECK_STRING (query); |
| 541 | 545 | ||
| 542 | if (!(NILP (values) || CONSP (values) || VECTORP (values))) | 546 | if (!(NILP (values) || CONSP (values) || VECTORP (values))) |
| 543 | xsignal1 (Qerror, build_string ("VALUES must be a list or a vector")); | 547 | xsignal1 (Qsqlite_error, build_string ("VALUES must be a list or a vector")); |
| 544 | 548 | ||
| 545 | sqlite3 *sdb = XSQLITE (db)->db; | 549 | sqlite3 *sdb = XSQLITE (db)->db; |
| 546 | Lisp_Object retval = Qnil, errmsg = Qnil, | 550 | Lisp_Object retval = Qnil, errmsg = Qnil, |
| @@ -553,7 +557,7 @@ which means that we return a set object that can be queried with | |||
| 553 | { | 557 | { |
| 554 | if (stmt) | 558 | if (stmt) |
| 555 | sqlite3_finalize (stmt); | 559 | sqlite3_finalize (stmt); |
| 556 | errmsg = sqlite_prepare_errmsg (ret, sdb); | 560 | errmsg = sqlite_prepare_errdata (ret, sdb); |
| 557 | goto exit; | 561 | goto exit; |
| 558 | } | 562 | } |
| 559 | 563 | ||
| @@ -589,7 +593,7 @@ which means that we return a set object that can be queried with | |||
| 589 | 593 | ||
| 590 | exit: | 594 | exit: |
| 591 | if (! NILP (errmsg)) | 595 | if (! NILP (errmsg)) |
| 592 | xsignal1 (Qerror, errmsg); | 596 | xsignal1 (Qsqlite_error, errmsg); |
| 593 | 597 | ||
| 594 | return retval; | 598 | return retval; |
| 595 | } | 599 | } |
| @@ -675,7 +679,7 @@ Only modules on Emacs' list of allowed modules can be loaded. */) | |||
| 675 | } | 679 | } |
| 676 | 680 | ||
| 677 | if (!do_allow) | 681 | if (!do_allow) |
| 678 | xsignal (Qerror, build_string ("Module name not on allowlist")); | 682 | xsignal1 (Qsqlite_error, build_string ("Module name not on allowlist")); |
| 679 | 683 | ||
| 680 | int result = sqlite3_load_extension | 684 | int result = sqlite3_load_extension |
| 681 | (XSQLITE (db)->db, | 685 | (XSQLITE (db)->db, |
| @@ -695,7 +699,7 @@ DEFUN ("sqlite-next", Fsqlite_next, Ssqlite_next, 1, 1, 0, | |||
| 695 | 699 | ||
| 696 | int ret = sqlite3_step (XSQLITE (set)->stmt); | 700 | int ret = sqlite3_step (XSQLITE (set)->stmt); |
| 697 | if (ret != SQLITE_ROW && ret != SQLITE_OK && ret != SQLITE_DONE) | 701 | if (ret != SQLITE_ROW && ret != SQLITE_OK && ret != SQLITE_DONE) |
| 698 | xsignal1 (Qerror, build_string (sqlite3_errmsg (XSQLITE (set)->db))); | 702 | xsignal1 (Qsqlite_error, build_string (sqlite3_errmsg (XSQLITE (set)->db))); |
| 699 | 703 | ||
| 700 | if (ret == SQLITE_DONE) | 704 | if (ret == SQLITE_DONE) |
| 701 | { | 705 | { |
| @@ -794,9 +798,15 @@ syms_of_sqlite (void) | |||
| 794 | defsubr (&Ssqlitep); | 798 | defsubr (&Ssqlitep); |
| 795 | defsubr (&Ssqlite_available_p); | 799 | defsubr (&Ssqlite_available_p); |
| 796 | 800 | ||
| 801 | DEFSYM (Qsqlite_error, "sqlite-error"); | ||
| 802 | Fput (Qsqlite_error, Qerror_conditions, | ||
| 803 | Fpurecopy (list2 (Qsqlite_error, Qerror))); | ||
| 804 | Fput (Qsqlite_error, Qerror_message, | ||
| 805 | build_pure_c_string ("Database error")); | ||
| 806 | |||
| 797 | DEFSYM (Qsqlite_locked_error, "sqlite-locked-error"); | 807 | DEFSYM (Qsqlite_locked_error, "sqlite-locked-error"); |
| 798 | Fput (Qsqlite_locked_error, Qerror_conditions, | 808 | Fput (Qsqlite_locked_error, Qerror_conditions, |
| 799 | Fpurecopy (list2 (Qsqlite_locked_error, Qerror))); | 809 | Fpurecopy (list3 (Qsqlite_locked_error, Qsqlite_error, Qerror))); |
| 800 | Fput (Qsqlite_locked_error, Qerror_message, | 810 | Fput (Qsqlite_locked_error, Qerror_message, |
| 801 | build_pure_c_string ("Database locked")); | 811 | build_pure_c_string ("Database locked")); |
| 802 | 812 | ||