From 1dee4e3cd008a27789bbce05b0eb47b0eb5d121a Mon Sep 17 00:00:00 2001 From: SomeDude Date: Mon, 17 Mar 2008 16:52:00 +0100 Subject: [PATCH] version 0.2.1 --- CMakeLists.txt | 4 +- include/global.h | 2 +- libs/shttpd/auth.c | 49 +- libs/shttpd/cgi.c | 13 +- libs/shttpd/compat_unix.c | 21 +- libs/shttpd/compat_win32.c | 554 +- libs/shttpd/compat_win32.h | 12 - libs/shttpd/config.c | 427 +- libs/shttpd/config.h | 28 + libs/shttpd/defs.h | 149 +- libs/shttpd/include/shttpd.h | 38 +- libs/shttpd/io_dir.c | 1 + libs/shttpd/io_emb.c | 58 +- libs/shttpd/io_file.c | 11 +- libs/shttpd/io_ssi.c | 67 +- libs/shttpd/log.c | 22 +- libs/shttpd/mime_type.c | 104 - libs/shttpd/shttpd.c | 518 +- libs/shttpd/shttpd.h | 38 +- libs/shttpd/standalone.c | 91 - libs/shttpd/string.c | 6 +- libs/sqlite3/sqlite3.c | 64864 ++++++++++------------------------- libs/sqlite3/sqlite3.h | 3227 +- libs/sqlite3/sqlite3ext.h | 6 +- readme.txt | 4 +- src/freenet/boardlistrequester.cpp | 1 + src/global.cpp | 6 + src/http/httpthread.cpp | 8 +- 28 files changed, 20909 insertions(+), 49420 deletions(-) create mode 100644 libs/shttpd/config.h delete mode 100644 libs/shttpd/mime_type.c delete mode 100644 libs/shttpd/standalone.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cfa932..00b85c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,7 @@ IF(NOT USE_BUNDLED_SQLITE) ENDIF(NOT USE_BUNDLED_SQLITE) FIND_LIBRARY(TINYXML_LIBRARY NAMES tinyxml tinyxml_s) FIND_LIBRARY(PTHREADS_LIBRARY NAMES pthread pthreads pthreadvc2) -FIND_LIBRARY(SHTTPD_LIBRARY NAMES shttpd) +FIND_LIBRARY(SHTTPD_LIBRARY NAMES shttpd shttpd_s) IF(SQLITE3_LIBRARY) TARGET_LINK_LIBRARIES(fms ${SQLITE3_LIBRARY}) @@ -170,6 +170,6 @@ ELSE(SHTTPD_LIBRARY) ELSE(WIN32) SET(SHTTPD_PLATFORM_SRC libs/shttpd/compat_unix.c) ENDIF(WIN32) - ADD_LIBRARY(shttpd ${SHTTPD_PLATFORM_SRC} libs/shttpd/auth.c libs/shttpd/cgi.c libs/shttpd/config.c libs/shttpd/io_cgi.c libs/shttpd/io_dir.c libs/shttpd/io_emb.c libs/shttpd/io_file.c libs/shttpd/io_socket.c libs/shttpd/io_ssi.c libs/shttpd/io_ssl.c libs/shttpd/log.c libs/shttpd/md5.c libs/shttpd/mime_type.c libs/shttpd/shttpd.c libs/shttpd/string.c) + ADD_LIBRARY(shttpd ${SHTTPD_PLATFORM_SRC} libs/shttpd/auth.c libs/shttpd/cgi.c libs/shttpd/config.c libs/shttpd/io_cgi.c libs/shttpd/io_dir.c libs/shttpd/io_emb.c libs/shttpd/io_file.c libs/shttpd/io_socket.c libs/shttpd/io_ssi.c libs/shttpd/io_ssl.c libs/shttpd/log.c libs/shttpd/md5.c libs/shttpd/shttpd.c libs/shttpd/string.c) TARGET_LINK_LIBRARIES(fms shttpd) ENDIF(SHTTPD_LIBRARY) diff --git a/include/global.h b/include/global.h index 30934a8..ddd6fa3 100644 --- a/include/global.h +++ b/include/global.h @@ -5,7 +5,7 @@ //#include #include "pthreadwrapper/thread.h" -#define FMS_VERSION "0.2.0" +#define FMS_VERSION "0.2.1" // opens database and creates tables and initial inserts if necessary void SetupDB(); diff --git a/libs/shttpd/auth.c b/libs/shttpd/auth.c index 86f8eac..212232e 100644 --- a/libs/shttpd/auth.c +++ b/libs/shttpd/auth.c @@ -175,9 +175,10 @@ open_auth_file(struct shttpd_ctx *ctx, const char *path) FILE *fp = NULL; int fd; - if (ctx->global_passwd_file) { + if (ctx->options[OPT_AUTH_GPASSWD] != NULL) { /* Use global passwords file */ - my_snprintf(name, sizeof(name), "%s", ctx->global_passwd_file); + my_snprintf(name, sizeof(name), "%s", + ctx->options[OPT_AUTH_GPASSWD]); } else { /* Try to find .htpasswd in requested directory */ for (p = path, e = p + strlen(p) - 1; e > p; e--) @@ -253,7 +254,8 @@ authorize(struct conn *c, FILE *fp) DBG(("[%.*s] [%.*s] [%.*s]", user.len, user.ptr, domain.len, domain.ptr, ha1.len, ha1.ptr)); - if (vcmp(user_vec, &user) && !memcmp(c->ctx->auth_realm, + if (vcmp(user_vec, &user) && + !memcmp(c->ctx->options[OPT_AUTH_REALM], domain.ptr, domain.len)) { ok = check_password(c->method, &ha1, &digest); break; @@ -267,23 +269,31 @@ authorize(struct conn *c, FILE *fp) int check_authorization(struct conn *c, const char *path) { - FILE *fp = NULL; - int authorized = 1; - -#ifdef EMBEDDED - struct llhead *lp; - struct uri_auth *auth; - - /* Check, is this URL protected by shttpd_protect_url() */ - LL_FOREACH(&c->ctx->uri_auths, lp) { - auth = LL_ENTRY(lp, struct uri_auth, link); - if (!strncmp(c->uri, auth->uri, auth->uri_len)) { - fp = fopen(auth->file_name, "r"); + FILE *fp = NULL; + int len, n, authorized = 1; + const char *p, *s = c->ctx->options[OPT_PROTECT]; + char protected_path[FILENAME_MAX]; + + FOR_EACH_WORD_IN_LIST(s, len) { + + if ((p = memchr(s, '=', len)) == NULL || p >= s + len || p == s) + continue; + + if (!memcmp(c->uri, s, p - s)) { + + n = s + len - p + 1; + if (n > (int) sizeof(protected_path) - 1) + n = sizeof(protected_path) - 1; + + my_strlcpy(protected_path, p + 1, n); + + if ((fp = fopen(protected_path, "r")) == NULL) + elog(E_LOG, c, "check_auth: cannot open %s: %s", + protected_path, strerror(errno)); break; } } -#endif /* EMBEDDED */ - + if (fp == NULL) fp = open_auth_file(c->ctx, path); @@ -301,7 +311,7 @@ is_authorized_for_put(struct conn *c) FILE *fp; int ret = 0; - if ((fp = fopen(c->ctx->put_auth_file, "r")) != NULL) { + if ((fp = fopen(c->ctx->options[OPT_AUTH_PUT], "r")) != NULL) { ret = authorize(c, fp); (void) fclose(fp); } @@ -316,7 +326,8 @@ send_authorization_request(struct conn *c) (void) my_snprintf(buf, sizeof(buf), "Unauthorized\r\n" "WWW-Authenticate: Digest qop=\"auth\", realm=\"%s\", " - "nonce=\"%lu\"", c->ctx->auth_realm, (unsigned long) current_time); + "nonce=\"%lu\"", c->ctx->options[OPT_AUTH_REALM], + (unsigned long) current_time); send_server_error(c, 401, buf); } diff --git a/libs/shttpd/cgi.c b/libs/shttpd/cgi.c index 122dee3..925b61a 100644 --- a/libs/shttpd/cgi.c +++ b/libs/shttpd/cgi.c @@ -145,7 +145,7 @@ prepare_environment(const struct conn *c, const char *prog, struct env_block *blk) { const struct headers *h = &c->ch; - const char *s; + const char *s, *root = c->ctx->options[OPT_ROOT]; size_t len; blk->len = blk->nvars = 0; @@ -155,14 +155,14 @@ prepare_environment(const struct conn *c, const char *prog, addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1"); addenv(blk, "%s", "REDIRECT_STATUS=200"); /* PHP */ addenv(blk, "SERVER_PORT=%d", c->loc_port); - addenv(blk, "SERVER_NAME=%s", c->ctx->auth_realm); - addenv(blk, "SERVER_ROOT=%s", c->ctx->document_root); - addenv(blk, "DOCUMENT_ROOT=%s", c->ctx->document_root); + addenv(blk, "SERVER_NAME=%s", c->ctx->options[OPT_AUTH_REALM]); + addenv(blk, "SERVER_ROOT=%s", root); + addenv(blk, "DOCUMENT_ROOT=%s", root); addenv(blk, "REQUEST_METHOD=%s", known_http_methods[c->method].ptr); addenv(blk, "REMOTE_ADDR=%s", inet_ntoa(c->sa.u.sin.sin_addr)); addenv(blk, "REMOTE_PORT=%hu", ntohs(c->sa.u.sin.sin_port)); addenv(blk, "REQUEST_URI=%s", c->uri); - addenv(blk, "SCRIPT_NAME=%s", prog + strlen(c->ctx->document_root)); + addenv(blk, "SCRIPT_NAME=%s", prog + strlen(root)); addenv(blk, "SCRIPT_FILENAME=%s", prog); /* PHP */ addenv(blk, "PATH_TRANSLATED=%s", prog); @@ -202,7 +202,7 @@ prepare_environment(const struct conn *c, const char *prog, } /* Add user-specified variables */ - s = c->ctx->cgi_vars; + s = c->ctx->options[OPT_CGI_ENVIRONMENT]; FOR_EACH_WORD_IN_LIST(s, len) addenv(blk, "%.*s", len, s); @@ -234,6 +234,7 @@ run_cgi(struct conn *c, const char *prog) int ret, pair[2]; prepare_environment(c, prog, &blk); + pair[0] = pair[1] = -1; /* CGI must be executed in its own directory */ (void) my_snprintf(dir, sizeof(dir), "%s", prog); diff --git a/libs/shttpd/compat_unix.c b/libs/shttpd/compat_unix.c index 649d0c8..b0777e9 100644 --- a/libs/shttpd/compat_unix.c +++ b/libs/shttpd/compat_unix.c @@ -74,8 +74,9 @@ int spawn_process(struct conn *c, const char *prog, char *envblk, char *envp[], int sock, const char *dir) { - int ret; - pid_t pid; + int ret; + pid_t pid; + const char *p, *interp = c->ctx->options[OPT_CGI_INTERPRETER]; envblk = NULL; /* unused */ @@ -87,6 +88,7 @@ spawn_process(struct conn *c, const char *prog, char *envblk, } else if (pid == 0) { /* Child */ + (void) chdir(dir); (void) dup2(sock, 0); (void) dup2(sock, 1); @@ -96,19 +98,22 @@ spawn_process(struct conn *c, const char *prog, char *envblk, if (c->ctx->error_log) (void) dup2(fileno(c->ctx->error_log), 2); + if ((p = strrchr(prog, '/')) != NULL) + p++; + else + p = prog; + /* Execute CGI program */ - if (c->ctx->cgi_interpreter == NULL) { - (void) execle(prog, prog, NULL, envp); + if (interp == NULL) { + (void) execle(p, p, NULL, envp); elog(E_FATAL, c, "redirect: exec(%s)", prog); } else { - (void) execle(c->ctx->cgi_interpreter, - c->ctx->cgi_interpreter, prog, NULL, envp); + (void) execle(interp, interp, p, NULL, envp); elog(E_FATAL, c, "redirect: exec(%s %s)", - c->ctx->cgi_interpreter, prog); + interp, prog); } /* UNREACHED */ - ret = -1; exit(EXIT_FAILURE); } else { diff --git a/libs/shttpd/compat_win32.c b/libs/shttpd/compat_win32.c index fb5b1a1..05f6036 100644 --- a/libs/shttpd/compat_win32.c +++ b/libs/shttpd/compat_win32.c @@ -10,533 +10,50 @@ #include "defs.h" -static const char *config_file = CONFIG; - -#if !defined(NO_GUI) - -static HICON hIcon; /* SHTTPD icon handle */ -HWND hLog; /* Log window */ - -/* - * Dialog box control IDs - */ -#define ID_GROUP 100 -#define ID_SAVE 101 -#define ID_STATUS 102 -#define ID_STATIC 103 -#define ID_SETTINGS 104 -#define ID_QUIT 105 -#define ID_TRAYICON 106 -#define ID_TIMER 107 -#define ID_ICON 108 -#define ID_ADVANCED 109 -#define ID_SHOWLOG 110 -#define ID_LOG 111 - -#define ID_USER 200 -#define ID_DELTA 1000 - -static void -run_server(void *param) -{ - struct shttpd_ctx *ctx = param; - - open_listening_ports(ctx); - - while (WaitForSingleObject(ctx->ev[0], 0) != WAIT_OBJECT_0) - shttpd_poll(ctx, 1000); - - SetEvent(ctx->ev[1]); - shttpd_fini(ctx); -} - -/* - * Save the configuration back into config file - */ static void -save_config(HWND hDlg, FILE *fp) -{ - const struct opt *opt; - char text[FILENAME_MAX]; - int id; - - if (fp == NULL) - elog(E_FATAL, NULL, "save_config: cannot open %s", config_file); - - for (opt = options; opt->name != NULL; opt++) { - id = ID_USER + (opt - options); /* Control ID */ - - /* Do not save if the text is the same as default */ - - if (opt->flags & OPT_BOOL) - (void) fprintf(fp, "%s\t%d\n", - opt->name, IsDlgButtonChecked(hDlg, id)); - else if (GetDlgItemText(hDlg, id, text, sizeof(text)) != 0 && - (opt->def == NULL || strcmp(text, opt->def) != 0)) - (void) fprintf(fp, "%s\t%s\n", opt->name, text); - } - - (void) fclose(fp); -} - -static void -set_control_values(HWND hDlg, const struct shttpd_ctx *ctx) -{ - const struct opt *opt; - const union variant *v; - char buf[FILENAME_MAX]; - int id; - - for (opt = options; opt->name != NULL; opt++) { - id = ID_USER + (opt - options); - v = (union variant *) ((char *) ctx + opt->ofs); - if (opt->flags & OPT_BOOL) { - CheckDlgButton(hDlg, id, - v->v_int ? BST_CHECKED : BST_UNCHECKED); - } else if (opt->flags & OPT_INT) { - my_snprintf(buf, sizeof(buf), "%d", v->v_int); - SetDlgItemText(hDlg, id, buf); - } else { - SetDlgItemText(hDlg, id, v->v_str); - } - } - -} - -static BOOL CALLBACK -DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) +fix_directory_separators(char *path) { - static struct shttpd_ctx *ctx, **pctx; - HANDLE ev; - const struct opt *opt; - DWORD tid; - int id, up; - char text[256]; - - switch (msg) { - - case WM_CLOSE: - KillTimer(hDlg, ID_TIMER); - DestroyWindow(hDlg); - break; - - case WM_COMMAND: - switch (LOWORD(wParam)) { - case ID_SAVE: - EnableWindow(GetDlgItem(hDlg, ID_SAVE), FALSE); - save_config(hDlg, fopen(config_file, "w+")); - ev = ctx->ev[1]; - SetEvent(ctx->ev[0]); - WaitForSingleObject(ev, INFINITE); - *pctx = ctx = init_from_argc_argv(config_file, 0, NULL); - open_listening_ports(ctx); - _beginthread(run_server, 0, ctx); - EnableWindow(GetDlgItem(hDlg, ID_SAVE), TRUE); - - break; - } - - id = ID_USER + ID_DELTA; - for (opt = options; opt->name != NULL; opt++, id++) - if (LOWORD(wParam) == id) { - OPENFILENAME of; - BROWSEINFO bi; - char path[FILENAME_MAX] = ""; - - memset(&of, 0, sizeof(of)); - of.lStructSize = sizeof(of); - of.hwndOwner = (HWND) hDlg; - of.lpstrFile = path; - of.nMaxFile = sizeof(path); - of.lpstrInitialDir = ctx->document_root; - of.Flags = OFN_CREATEPROMPT | OFN_NOCHANGEDIR; - - memset(&bi, 0, sizeof(bi)); - bi.hwndOwner = (HWND) hDlg; - bi.lpszTitle = "Choose WWW root directory:"; - bi.ulFlags = BIF_RETURNONLYFSDIRS; - - if (opt->flags & OPT_DIR) - SHGetPathFromIDList( - SHBrowseForFolder(&bi), path); - else - GetOpenFileName(&of); - - if (path[0] != '\0') - SetWindowText(GetDlgItem(hDlg, - id - ID_DELTA), path); - } - - break; - - case WM_INITDIALOG: - pctx = (struct shttpd_ctx **) lParam; - ctx = *pctx; - SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_SMALL,(LPARAM)hIcon); - SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_BIG,(LPARAM)hIcon); - SetWindowText(hDlg, "SHTTPD settings"); - SetFocus(GetDlgItem(hDlg, ID_SAVE)); - set_control_values(hDlg, ctx); - break; - default: - break; + for (; *path != '\0'; path++) { + if (*path == '/') + *path = '\\'; + if (*path == '\\') + while (path[1] == '\\' || path[1] == '/') + (void) memmove(path + 1, + path + 2, strlen(path + 2) + 1); } - - return FALSE; } -static void * -align(void *ptr, DWORD alig) +static int +protect_against_code_disclosure(const char *path) { - ULONG ul = (ULONG) ptr; + WIN32_FIND_DATA data; + HANDLE handle; + const char *p; - ul += alig; - ul &= ~alig; - - return ((void *) ul); -} - - -static void -add_control(unsigned char **mem, DLGTEMPLATE *dia, WORD type, DWORD id, - DWORD style, WORD x, WORD y, WORD cx, WORD cy, const char *caption) -{ - DLGITEMTEMPLATE *tp; - LPWORD p; - - dia->cdit++; - - *mem = align(*mem, 3); - tp = (DLGITEMTEMPLATE *) *mem; - - tp->id = (WORD)id; - tp->style = style; - tp->dwExtendedStyle = 0; - tp->x = x; - tp->y = y; - tp->cx = cx; - tp->cy = cy; - - p = align(*mem + sizeof(*tp), 1); - *p++ = 0xffff; - *p++ = type; + /* + * Protect against CGI code disclosure under Windows. + * This is very nasty hole. Windows happily opens files with + * some garbage in the end of file name. So fopen("a.cgi ", "r") + * actually opens "a.cgi", and does not return an error! And since + * "a.cgi " does not have valid CGI extension, this leads to + * the CGI code disclosure. + * To protect, here we delete all fishy characters from the + * end of file name. + */ - while (*caption != '\0') - *p++ = (WCHAR) *caption++; - *p++ = 0; - p = align(p, 1); + if ((handle = FindFirstFile(path, &data)) == INVALID_HANDLE_VALUE) + return (FALSE); - *p++ = 0; - *mem = (unsigned char *) p; -} - -static void -show_settings_dialog(struct shttpd_ctx **ctxp) -{ -#define HEIGHT 15 -#define WIDTH 400 -#define LABEL_WIDTH 70 - - unsigned char mem[4096], *p; - DWORD style; - DLGTEMPLATE *dia = (DLGTEMPLATE *) mem; - WORD cl, x, y, width, nelems = 0; - const struct opt *opt; - static int guard; - - static struct { - DLGTEMPLATE template; /* 18 bytes */ - WORD menu, class; - wchar_t caption[1]; - WORD fontsiz; - wchar_t fontface[7]; - } dialog_header = {{WS_CAPTION | WS_POPUP | WS_SYSMENU | WS_VISIBLE | - DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, - 0, 200, 200, WIDTH, 0}, 0, 0, L"", 8, L"Tahoma"}; - - if (guard == 0) - guard++; - else - return; - - (void) memset(mem, 0, sizeof(mem)); - (void) memcpy(mem, &dialog_header, sizeof(dialog_header)); - p = mem + sizeof(dialog_header); - - for (opt = options; opt->name != NULL; opt++) { - - style = WS_CHILD | WS_VISIBLE | WS_TABSTOP; - x = 10 + (WIDTH / 2) * (nelems % 2); - y = (nelems/2 + 1) * HEIGHT + 5; - width = WIDTH / 2 - 20 - LABEL_WIDTH; - if (opt->flags & OPT_INT) { - style |= ES_NUMBER; - cl = 0x81; - style |= WS_BORDER | ES_AUTOHSCROLL; - } else if (opt->flags & OPT_BOOL) { - cl = 0x80; - style |= BS_AUTOCHECKBOX; - } else if (opt->flags & (OPT_DIR | OPT_FILE)) { - style |= WS_BORDER | ES_AUTOHSCROLL; - width -= 20; - cl = 0x81; - add_control(&p, dia, 0x80, - ID_USER + ID_DELTA + (opt - options), - WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, - (WORD) (x + width + LABEL_WIDTH + 5), - y, 15, 12, "..."); - } else { - cl = 0x81; - style |= WS_BORDER | ES_AUTOHSCROLL; - } - add_control(&p, dia, 0x82, ID_STATIC, WS_VISIBLE | WS_CHILD, - x, y, LABEL_WIDTH, HEIGHT, opt->desc); - add_control(&p, dia, cl, ID_USER + (opt - options), style, - (WORD) (x + LABEL_WIDTH), y, width, 12, ""); - nelems++; - } + FindClose(handle); - y = (WORD) (((nelems + 1)/2 + 1) * HEIGHT + 5); - add_control(&p, dia, 0x80, ID_GROUP, WS_CHILD | WS_VISIBLE | - BS_GROUPBOX, 5, 5, WIDTH - 10, y, "Settings"); - y += 10; - add_control(&p, dia, 0x80, ID_SAVE, - WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, - WIDTH - 70, y, 65, 12, "Save Settings"); -#if 0 - add_control(&p, dia, 0x80, ID_ADVANCED, - WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, - WIDTH - 190, y, 110, 12, "Show Advanced Settings >>"); -#endif - add_control(&p, dia, 0x82, ID_STATIC, - WS_CHILD | WS_VISIBLE | WS_DISABLED, - 5, y, 180, 12,"SHTTPD v." VERSION - " (http://shttpd.sourceforge.net)"); + for (p = path + strlen(path); p > path && p[-1] != '\\';) + p--; - dia->cy = ((nelems + 1)/2 + 1) * HEIGHT + 30; - DialogBoxIndirectParam(NULL, dia, NULL, DlgProc, (LPARAM) ctxp); - guard--; -} - -static BOOL CALLBACK -LogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) -{ - static struct shttpd_ctx *ctx; - static HWND hStatus; - HWND hEdit; - RECT rect, rect2, rect3, rect4; - int len, up, widths[] = {120, 220, 330, 460, -1}; - char text[256], buf[1024 * 64]; - - switch (msg) { - - case WM_CLOSE: - KillTimer(hDlg, ID_TIMER); - DestroyWindow(hDlg); - break; - - case WM_APP: - hEdit = GetDlgItem(hDlg, ID_LOG); - len = GetWindowText(hEdit, buf, sizeof(buf)); - if (len > sizeof(buf) * 4 / 5) - len = sizeof(buf) * 4 / 5; - my_snprintf(buf + len, sizeof(buf) - len, - "%s\r\n", (char *) lParam); - SetWindowText(hEdit, buf); - SendMessage(hEdit, WM_VSCROLL, SB_BOTTOM, 0); - break; - - case WM_TIMER: - /* Print statistics on a status bar */ - up = current_time - ctx->start_time; - (void) my_snprintf(text, sizeof(text), - " Up: %3d h %2d min %2d sec", - up / 3600, up / 60 % 60, up % 60); - SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM) text); - (void) my_snprintf(text, sizeof(text), - " Requests: %u", ctx->nrequests); - SendMessage(hStatus, SB_SETTEXT, 1, (LPARAM) text); - (void) my_snprintf(text, sizeof(text), - " Sent: %4.2f Mb", (double) ctx->out / 1048576); - SendMessage(hStatus, SB_SETTEXT, 2, (LPARAM) text); - (void) my_snprintf(text, sizeof(text), - " Received: %4.2f Mb", (double) ctx->in / 1048576); - SendMessage(hStatus, SB_SETTEXT, 3, (LPARAM) text); - break; - - case WM_INITDIALOG: - ctx = (struct shttpd_ctx *) lParam; - SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_SMALL,(LPARAM)hIcon); - SendMessage(hDlg,WM_SETICON,(WPARAM)ICON_BIG,(LPARAM)hIcon); - hStatus = CreateStatusWindow(WS_CHILD | WS_VISIBLE, - "", hDlg, ID_STATUS); - SendMessage(hStatus, SB_SETPARTS, 5, (LPARAM) widths); - SendMessage(hStatus, SB_SETTEXT, 4, (LPARAM) " Running"); - SetWindowText(hDlg, "SHTTPD web server log"); - SetTimer(hDlg, ID_TIMER, 1000, NULL); - GetWindowRect(GetDesktopWindow(), &rect3); - GetWindowRect(hDlg, &rect4); - GetClientRect(hDlg, &rect); - GetClientRect(hStatus, &rect2); - SetWindowPos(GetDlgItem(hDlg, ID_LOG), 0, - 0, 0, rect.right, rect.bottom - rect2.bottom, 0); - SetWindowPos(hDlg, HWND_TOPMOST, - rect3.right - (rect4.right - rect4.left), - rect3.bottom - (rect4.bottom - rect4.top) - 30, - 0, 0, SWP_NOSIZE); - SetFocus(hStatus); - SendMessage(hDlg, WM_TIMER, 0, 0); - hLog = hDlg; - break; - default: - break; - } - - - return (FALSE); -} - -static void -show_log_window(struct shttpd_ctx *ctx) -{ - unsigned char mem[4096], *p; - DWORD style; - DLGTEMPLATE *dia = (DLGTEMPLATE *) mem; - WORD cl, x, y, width, nelems = 0; - - static struct { - DLGTEMPLATE template; /* 18 bytes */ - WORD menu, class; - wchar_t caption[1]; - WORD fontsiz; - wchar_t fontface[7]; - } dialog_header = {{WS_CAPTION | WS_POPUP | WS_VISIBLE | WS_SYSMENU | - DS_SETFONT | WS_DLGFRAME, WS_EX_TOOLWINDOW, - 0, 200, 200, 400, 100}, 0, 0, L"", 8, L"Tahoma"}; - - if (hLog != NULL) - return; - - (void) memset(mem, 0, sizeof(mem)); - (void) memcpy(mem, &dialog_header, sizeof(dialog_header)); - p = mem + sizeof(dialog_header); - - add_control(&p, dia, 0x81, ID_LOG, WS_CHILD | WS_VISIBLE | - WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | - ES_READONLY, 5, 5, WIDTH - 10, 60, ""); - - DialogBoxIndirectParam(NULL, dia, NULL, LogProc, (LPARAM) ctx); - - hLog = NULL; -} - -static LRESULT CALLBACK -WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) -{ - static NOTIFYICONDATA ni; - static struct shttpd_ctx *ctx; - DWORD tid; /* Thread ID */ - HMENU hMenu; - POINT pt; - - switch (msg) { - case WM_CREATE: - ctx = ((CREATESTRUCT *) lParam)->lpCreateParams; - memset(&ni, 0, sizeof(ni)); - ni.cbSize = sizeof(ni); - ni.uID = ID_TRAYICON; - ni.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; - ni.hIcon = hIcon; - ni.hWnd = hWnd; - my_snprintf(ni.szTip, sizeof(ni.szTip), "SHTTPD web server"); - ni.uCallbackMessage = WM_USER; - Shell_NotifyIcon(NIM_ADD, &ni); - ctx->ev[0] = CreateEvent(0, TRUE, FALSE, 0); - ctx->ev[1] = CreateEvent(0, TRUE, FALSE, 0); - _beginthread(run_server, 0, ctx); - break; - case WM_CLOSE: - Shell_NotifyIcon(NIM_DELETE, &ni); - PostQuitMessage(0); - break; - case WM_COMMAND: - switch (LOWORD(wParam)) { - case ID_SETTINGS: - show_settings_dialog(&ctx); - break; - case ID_QUIT: - SendMessage(hWnd, WM_CLOSE, wParam, lParam); - PostQuitMessage(0); - break; - case ID_SHOWLOG: - show_log_window(ctx); - break; - } - break; - case WM_USER: - switch (lParam) { - case WM_RBUTTONUP: - case WM_LBUTTONUP: - case WM_LBUTTONDBLCLK: - hMenu = CreatePopupMenu(); - AppendMenu(hMenu, 0, ID_SETTINGS, "Settings"); - AppendMenu(hMenu, 0, ID_SHOWLOG, "Show Log"); - AppendMenu(hMenu, 0, ID_QUIT, "Exit SHTTPD"); - GetCursorPos(&pt); - TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL); - DestroyMenu(hMenu); - break; - } - break; - } + if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && + strcmp(data.cFileName, p) != 0) + return (FALSE); - return (DefWindowProc(hWnd, msg, wParam, lParam)); -} - -int WINAPI -WinMain(HINSTANCE h, HINSTANCE prev, char *cmdline, int show) -{ - struct shttpd_ctx *ctx; - WNDCLASS cls; - HWND hWnd; - MSG msg; - - ctx = init_from_argc_argv(config_file, 0, NULL); - (void) memset(&cls, 0, sizeof(cls)); - - hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON)); - if (hIcon == NULL) - hIcon = LoadIcon(NULL, IDI_APPLICATION); - cls.lpfnWndProc = (WNDPROC) WindowProc; - cls.hIcon = hIcon; - cls.lpszClassName = "shttpd v." VERSION; - - if (!RegisterClass(&cls)) - elog(E_FATAL, NULL, "RegisterClass: %d", ERRNO); - else if ((hWnd = CreateWindow(cls.lpszClassName, "",WS_OVERLAPPEDWINDOW, - 0, 0, 0, 0, NULL, NULL, NULL, ctx)) == NULL) - elog(E_FATAL, NULL, "CreateWindow: %d", ERRNO); - - while (GetMessage(&msg, (HWND) NULL, 0, 0)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - - return (0); -} -#endif /* NO_GUI */ - -static void -fix_directory_separators(char *path) -{ - for (; *path != '\0'; path++) { - if (*path == '/') - *path = '\\'; - if (*path == '\\') - while (path[1] == '\\' || path[1] == '/') - (void) memmove(path + 1, - path + 2, strlen(path + 2) + 1); - } + return (TRUE); } int @@ -549,6 +66,9 @@ my_open(const char *path, int flags, int mode) fix_directory_separators(buf); MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuf, sizeof(wbuf)); + if (protect_against_code_disclosure(buf) == FALSE) + return (-1); + return (_wopen(wbuf, flags)); } @@ -876,7 +396,7 @@ spawn_process(struct conn *c, const char *prog, char *envblk, si.hStdInput = h[0]; /* If CGI file is a script, try to read the interpreter line */ - if (c->ctx->cgi_interpreter == NULL) { + if (c->ctx->options[OPT_CGI_INTERPRETER] == NULL) { if ((fp = fopen(prog, "r")) != NULL) { (void) fgets(line, sizeof(line), fp); if (memcmp(line, "#!", 2) != 0) @@ -891,7 +411,7 @@ spawn_process(struct conn *c, const char *prog, char *envblk, line + 2, line[2] == '\0' ? "" : " ", prog); } else { (void) my_snprintf(cmdline, sizeof(cmdline), "%s %s", - c->ctx->cgi_interpreter, prog); + c->ctx->options[OPT_CGI_INTERPRETER], prog); } (void) my_snprintf(line, sizeof(line), "%s", dir); diff --git a/libs/shttpd/compat_win32.h b/libs/shttpd/compat_win32.h index a5c1dce..e4e6dba 100644 --- a/libs/shttpd/compat_win32.h +++ b/libs/shttpd/compat_win32.h @@ -20,18 +20,6 @@ #ifndef _WIN32_WCE -#ifdef _MSC_VER /* pragmas not valid on MinGW */ -#pragma comment(lib,"ws2_32") -#pragma comment(lib,"user32") -#pragma comment(lib,"comctl32") -#pragma comment(lib,"comdlg32") -#pragma comment(lib,"shell32") -#ifdef NO_GUI -#pragma comment(linker,"/subsystem:console") -#else -#pragma comment(linker,"/subsystem:windows") -#endif /* NO_GUI */ -#endif /* _MSC_VER */ #include #include #include diff --git a/libs/shttpd/config.c b/libs/shttpd/config.c index dcc6dad..bd290fb 100644 --- a/libs/shttpd/config.c +++ b/libs/shttpd/config.c @@ -10,32 +10,44 @@ #include "defs.h" -/* - * Configuration parameters setters - */ -static void -set_int(struct shttpd_ctx *ctx, void *ptr, const char *string) -{ - ctx = NULL; /* Unused */ - * (int *) ptr = atoi(string); -} +static int isbyte(int n) { return (n >= 0 && n <= 255); } static void -set_str(struct shttpd_ctx *ctx, void *ptr, const char *string) +set_acl(struct shttpd_ctx *ctx, const char *s) { - ctx = NULL; /* Unused */ - * (char **) ptr = my_strdup(string); -} - -static void -set_log_file(struct shttpd_ctx *ctx, void *ptr, const char *string) -{ - FILE **fp = ptr; - ctx = NULL; + struct acl *acl = NULL; + char flag; + int len, a, b, c, d, n, mask; + struct llhead *lp, *tmp; + + /* Delete the old ACLs if any */ + LL_FOREACH_SAFE(&ctx->acl, lp, tmp) + free(LL_ENTRY(lp, struct acl, link)); + + FOR_EACH_WORD_IN_LIST(s, len) { + + mask = 32; + + if (sscanf(s, "%c%d.%d.%d.%d%n",&flag,&a,&b,&c,&d,&n) != 5) { + elog(E_FATAL, NULL, "[%s]: subnet must be " + "[+|-]x.x.x.x[/x]", s); + } else if (flag != '+' && flag != '-') { + elog(E_FATAL, NULL, "flag must be + or -: [%s]", s); + } else if (!isbyte(a)||!isbyte(b)||!isbyte(c)||!isbyte(d)) { + elog(E_FATAL, NULL, "bad ip address: [%s]", s); + } else if ((acl = malloc(sizeof(*acl))) == NULL) { + elog(E_FATAL, NULL, "%s", "cannot malloc subnet"); + } else if (sscanf(s + n, "/%d", &mask) == 0) { + /* Do nothing, no mask specified */ + } else if (mask < 0 || mask > 32) { + elog(E_FATAL, NULL, "bad subnet mask: %d [%s]", n, s); + } - if ((*fp = fopen(string, "a")) == NULL) - elog(E_FATAL, NULL, "cannot open log file %s: %s", - string, strerror(errno)); + acl->ip = (a << 24) | (b << 16) | (c << 8) | d; + acl->mask = mask ? 0xffffffffU << (32 - mask) : 0; + acl->flag = flag; + LL_TAIL(&ctx->acl, &acl->link); + } } #ifndef NO_SSL @@ -43,14 +55,12 @@ set_log_file(struct shttpd_ctx *ctx, void *ptr, const char *string) * Dynamically load SSL library. Set up ctx->ssl_ctx pointer. */ static void -set_ssl(struct shttpd_ctx *ctx, void *arg, const char *pem) +set_ssl(struct shttpd_ctx *ctx, const char *pem) { SSL_CTX *CTX; void *lib; struct ssl_func *fp; - arg = NULL; /* Unused */ - /* Load SSL library dynamically */ if ((lib = dlopen(SSL_LIB, RTLD_LAZY)) == NULL) elog(E_FATAL, NULL, "set_ssl: cannot load %s", SSL_LIB); @@ -73,262 +83,225 @@ set_ssl(struct shttpd_ctx *ctx, void *arg, const char *pem) #endif /* NO_SSL */ static void -set_mime(struct shttpd_ctx *ctx, void *arg, const char *string) +open_log_file(FILE **fpp, const char *path) { - arg = NULL; - set_mime_types(ctx, string); -} + if (*fpp != NULL) + (void) fclose(*fpp); -#define OFS(x) offsetof(struct shttpd_ctx, x) -#define BOOL_OPT "0|1" -const struct opt options[] = { - {'d', "document_root", "Web root directory", set_str, - OFS(document_root), "directory", NULL, OPT_DIR}, - {'i', "index_files", "Index files", set_str, OFS(index_files), - "file_list", INDEX_FILES, OPT_ADVANCED}, - {'p', "listen_ports", "Listening ports", set_str, - OFS(ports), "ports", LISTENING_PORTS, OPT_ADVANCED}, - {'D', "list_directories", "Directory listing", set_int, - OFS(dirlist), BOOL_OPT, "1", OPT_BOOL | OPT_ADVANCED}, -#ifndef NO_CGI - {'c', "cgi_extensions", "CGI extensions", set_str, - OFS(cgi_extensions), "ext_list", CGI_EXT, OPT_ADVANCED}, - {'C', "cgi_interpreter", "CGI interpreter", set_str, - OFS(cgi_interpreter), "file", NULL, OPT_FILE | OPT_ADVANCED}, - {'V', "cgi_envvar", "CGI envir variables", set_str, - OFS(cgi_vars), "X=Y,....", NULL, OPT_ADVANCED}, -#endif /* NO_CGI */ -#if !defined(NO_SSI) - {'S', "ssi_extensions", "SSI extensions", set_str, - OFS(ssi_extensions), "ext_list", SSI_EXT, OPT_ADVANCED}, -#endif /* NO_SSI */ - {'N', "auth_realm", "Authentication realm", set_str, - OFS(auth_realm), "auth_realm", REALM, OPT_ADVANCED}, - {'l', "access_log", "Access log file", set_log_file, - OFS(access_log), "file", NULL, OPT_FILE | OPT_ADVANCED}, - {'e', "error_log", "Error log file", set_log_file, - OFS(error_log), "file", NULL, OPT_FILE | OPT_ADVANCED}, - {'m', "mime_types", "Mime types file", set_mime, - OFS(mime_file), "file", NULL, OPT_FILE | OPT_ADVANCED}, - {'P', "global_htpasswd", "Global passwords file", set_str, - OFS(global_passwd_file), "file", NULL, OPT_FILE | OPT_ADVANCED}, -#ifndef NO_SSL - {'s', "ssl_certificate", "SSL certificate file", set_ssl, - OFS(ssl_ctx), "pem_file", NULL, OPT_FILE | OPT_ADVANCED}, -#endif /* NO_SSL */ - {'U', "put_auth", "PUT,DELETE auth file",set_str, - OFS(put_auth_file), "file", NULL, OPT_FILE | OPT_ADVANCED}, - {'a', "aliases", "Aliases", set_str, - OFS(aliases), "X=Y,...", NULL, OPT_ADVANCED}, - {'b', "io_buf_size", "IO buffer size", set_int, OFS(io_buf_size), - "bytes", DFLT_IO_SIZ, OPT_INT | OPT_ADVANCED}, -#ifdef _WIN32 - {'B', "auto_start", "Autostart with Windows", set_int, - OFS(auto_start), BOOL_OPT, "1", OPT_BOOL}, -#else - {'I', "inetd_mode", "Inetd mode", set_int, - OFS(inetd_mode), BOOL_OPT, NULL, OPT_BOOL }, - {'u', "runtime_uid", "Run as user", set_str, - OFS(uid), "user_name", NULL, 0 }, -#endif /* _WIN32 */ - {0, NULL, NULL, NULL, 0, NULL, NULL, 0 } -}; + if (path == NULL) { + *fpp = NULL; + } else if ((*fpp = fopen(path, "a")) == NULL) { + elog(E_FATAL, NULL, "cannot open log file %s: %s", + path, strerror(errno)); + } +} -static const struct opt * -find_option(int sw, const char *name) +static void +set_alog(struct shttpd_ctx *ctx, const char *path) { - const struct opt *opt; - - for (opt = options; opt->sw != 0; opt++) - if (sw == opt->sw || (name && strcmp(opt->name, name) == 0)) - return (opt); - - return (NULL); + open_log_file(&ctx->access_log, path); } static void -set_option(const struct opt *opt, const char *val, char **tmpvars) +set_elog(struct shttpd_ctx *ctx, const char *path) { - tmpvars += opt - options; + open_log_file(&ctx->error_log, path); +} + +static void show_cfg_page(struct shttpd_arg *arg); - if (*tmpvars != NULL) - free(*tmpvars); +static void +set_cfg_uri(struct shttpd_ctx *ctx, const char *uri) +{ + free_list(&ctx->registered_uris, ®istered_uri_destructor); - *tmpvars = my_strdup(val); + if (uri != NULL) { + shttpd_register_uri(ctx, uri, &show_cfg_page, ctx); + } } -/* - * Initialize shttpd context - */ static void -initialize_context(struct shttpd_ctx *ctx, const char *config_file, - int argc, char *argv[], char **tmpvars) +set_ports(struct shttpd_ctx *ctx, const char *p) { - char line[FILENAME_MAX], root[FILENAME_MAX], - var[sizeof(line)], val[sizeof(line)]; - const char *arg; - size_t i; - const struct opt *opt; - FILE *fp; - struct tm *tm; + int len, is_ssl; - current_time = time(NULL); - tm = localtime(¤t_time); - tz_offset = 0; -#if 0 - tm->tm_gmtoff - 3600 * (tm->tm_isdst > 0 ? 1 : 0); -#endif + free_list(&ctx->listeners, &listener_destructor); - (void) memset(ctx, 0, sizeof(*ctx)); + FOR_EACH_WORD_IN_LIST(p, len) { + is_ssl = p[len - 1] == 's' ? 1 : 0; + if (shttpd_listen(ctx, atoi(p), is_ssl) == -1) + elog(E_FATAL, NULL, + "Cannot open socket on port %d", atoi(p)); + } +} - ctx->start_time = current_time; - InitializeCriticalSection(&ctx->mutex); +static const struct opt { + int index; /* Index in shttpd_ctx */ + const char *name; /* Option name in config file */ + const char *description; /* Description */ + const char *default_value; /* Default option value */ + void (*setter)(struct shttpd_ctx *, const char *); +} known_options[] = { + {OPT_ROOT, "root", "\tWeb root directory", ".", NULL}, + {OPT_INDEX_FILES, "index_files", "Index files", INDEX_FILES, NULL}, + {OPT_PORTS, "ports", "Listening ports", LISTENING_PORTS, set_ports}, + {OPT_DIR_LIST, "dir_list", "Directory listing", "1", NULL}, + {OPT_CFG_URI, "cfg_uri", "Config uri", NULL, set_cfg_uri}, + {OPT_PROTECT, "protect", "URI to htpasswd mapping", NULL, NULL}, +#ifndef NO_CGI + {OPT_CGI_EXTENSIONS, "cgi_ext", "CGI extensions", CGI_EXT, NULL}, + {OPT_CGI_INTERPRETER, "cgi_interp", "CGI interpreter", NULL, NULL}, + {OPT_CGI_ENVIRONMENT, "cgi_env", "Additional CGI env vars", NULL, NULL}, +#endif /* NO_CGI */ + {OPT_SSI_EXTENSIONS, "ssi_ext", "SSI extensions", SSI_EXT, NULL}, +#ifndef NO_AUTH + {OPT_AUTH_REALM, "auth_realm", "Authentication domain name",REALM,NULL}, + {OPT_AUTH_GPASSWD, "auth_gpass", "Global passwords file", NULL, NULL}, + {OPT_AUTH_PUT, "auth_PUT", "PUT,DELETE auth file", NULL, NULL}, +#endif /* !NO_AUTH */ + {OPT_ACCESS_LOG, "access_log", "Access log file", NULL, set_alog}, + {OPT_ERROR_LOG, "error_log", "Error log file", NULL, set_elog}, + {OPT_MIME_TYPES, "mime_types", "Additional mime types list", NULL,NULL}, +#ifndef NO_SSL + {OPT_SSL_CERTIFICATE, "ssl_cert", "SSL certificate file", NULL,set_ssl}, +#endif /* NO_SSL */ + {OPT_ALIASES, "aliases", "Path=URI mappings", NULL, NULL}, + {OPT_ACL, "acl", "\tAllow/deny IP addresses/subnets", NULL, set_acl}, +#ifdef _WIN32 +#else + {OPT_INETD, "inetd", "Inetd mode", "0", NULL}, + {OPT_UID, "uid", "\tRun as user", NULL, NULL}, +#endif /* _WIN32 */ + {-1, NULL, NULL, NULL, NULL} +}; - LL_INIT(&ctx->connections); - LL_INIT(&ctx->mime_types); - LL_INIT(&ctx->registered_uris); - LL_INIT(&ctx->uri_auths); - LL_INIT(&ctx->error_handlers); +void shttpd_set_option(struct shttpd_ctx *ctx, const char *opt, const char *val) +{ + const struct opt *o; -#if !defined(NO_SSI) - LL_INIT(&ctx->ssi_funcs); -#endif /* NO_SSI */ + for (o = known_options; o->name != NULL; o++) + if (!strcmp(opt, o->name)) + break; - /* First pass: set the defaults */ - for (opt = options; opt->sw != 0; opt++) - if (tmpvars[opt - options] == NULL && opt->def != NULL) - tmpvars[opt - options] = my_strdup(opt->def); + if (o->name == NULL) + elog(E_FATAL, NULL, "no such option: [%s]", opt); - /* Second pass: load config file */ - if (config_file != NULL && (fp = fopen(config_file, "r")) != NULL) { - DBG(("init_ctx: config file %s", config_file)); + /* Call option setter first, so it can use both new and old values */ + if (o->setter != NULL) + o->setter(ctx, val); - /* Loop through the lines in config file */ - while (fgets(line, sizeof(line), fp) != NULL) { + /* Free old value if any */ + if (ctx->options[o->index] != NULL) + free(ctx->options[o->index]); + + /* Set new option value */ + ctx->options[o->index] = val ? my_strdup(val) : NULL; +} - /* Skip comments and empty lines */ - if (line[0] == '#' || line[0] == '\n') - continue; +static void +show_cfg_page(struct shttpd_arg *arg) +{ + struct shttpd_ctx *ctx = arg->user_data; + char opt_name[20], value[BUFSIZ]; + const struct opt *o; + + opt_name[0] = value[0] = '\0'; + + if (!strcmp(shttpd_get_env(arg, "REQUEST_METHOD"), "POST")) { + if (arg->flags & SHTTPD_MORE_POST_DATA) + return; + (void) shttpd_get_var("o", arg->in.buf, arg->in.len, + opt_name, sizeof(opt_name)); + (void) shttpd_get_var("v", arg->in.buf, arg->in.len, + value, sizeof(value)); + shttpd_set_option(ctx, opt_name, value[0] ? value : NULL); + } - /* Trim trailing newline character */ - line[strlen(line) - 1] = '\0'; + shttpd_printf(arg, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + "

SHTTPD v. %s

", shttpd_version()); - if (sscanf(line, "%s %[^#\n]", var, val) != 2) - elog(E_FATAL,0,"init_ctx: bad line: [%s]",line); + shttpd_printf(arg, "%s", "" + ""); - if ((opt = find_option(0, var)) == NULL) - elog(E_FATAL, NULL, - "set_option: unknown variable [%s]", var); - set_option(opt, val, tmpvars); - } - (void) fclose(fp); - } + if (opt_name[0] != '\0' && value[0] != '\0') + shttpd_printf(arg, "

Saved: %s=%s

", + opt_name, value[0] ? value : "NULL"); - /* Third pass: process command line args */ - for (i = 1; i < (size_t) argc && argv[i][0] == '-'; i++) - if ((opt = find_option(argv[i][1], NULL)) != NULL) { - arg = argv[i][2] ? &argv[i][2] : argv[++i]; - - if (arg == NULL) - usage(argv[0]); - - set_option(opt, arg, tmpvars); - } else { - usage(argv[0]); - } - /* Call setters functions now */ - for (i = 0; i < NELEMS(options); i++) - if (tmpvars[i] != NULL) { - options[i].setter(ctx, - ((char *) ctx) + options[i].ofs, tmpvars[i]); - free(tmpvars[i]); - } - - /* If document_root is not set, set it to current directory */ - if (ctx->document_root == NULL) { - (void) my_getcwd(root, sizeof(root)); - ctx->document_root = my_strdup(root); + for (o = known_options; o->name != NULL; o++) { + shttpd_printf(arg, + "" + "" + "" + "", + o->name, o->description, o->name, + ctx->options[o->index] ? ctx->options[o->index] : ""); } -#ifdef _WIN32 - {WSADATA data; WSAStartup(MAKEWORD(2,2), &data);} -#endif /* _WIN32 */ - - DBG(("init_ctx: initialized context %p", (void *) ctx)); + shttpd_printf(arg, "%s", "
OptionDescriptionValue
%s%s
"); + arg->flags |= SHTTPD_END_OF_OUTPUT; } - /* * Show usage string and exit. */ void usage(const char *prog) { - const struct opt *opt; + const struct opt *o; (void) fprintf(stderr, "SHTTPD version %s (c) Sergey Lyubka\n" - "usage: %s [OPTIONS] [config_file]\n" - "Note: config line keyword for every option is in the " - "round brackets\n", VERSION, prog); + "usage: %s [options] [config_file]\n", VERSION, prog); #if !defined(NO_AUTH) - (void) fprintf(stderr, "-A \n"); + fprintf(stderr, " -A \n"); #endif /* NO_AUTH */ - for (opt = options; opt->name != NULL; opt++) - (void) fprintf(stderr, "-%c <%s>\t\t%s (%s)\n", - opt->sw, opt->arg, opt->desc, opt->name); + for (o = known_options; o->name != NULL; o++) { + (void) fprintf(stderr, " -%s\t%s", o->name, o->description); + if (o->default_value != NULL) + fprintf(stderr, " (default: %s)", o->default_value); + fputc('\n', stderr); + } exit(EXIT_FAILURE); } -struct shttpd_ctx * -init_from_argc_argv(const char *config_file, int argc, char *argv[]) +struct shttpd_ctx *shttpd_init(void) { struct shttpd_ctx *ctx; - char *tmpvars[NELEMS(options)]; - size_t i; + struct tm *tm; + const struct opt *o; - /* Initialize all temporary holders to NULL */ - for (i = 0; i < NELEMS(tmpvars); i++) - tmpvars[i] = NULL; + if ((ctx = calloc(1, sizeof(*ctx))) == NULL) + elog(E_FATAL, NULL, "cannot allocate shttpd context"); - if ((ctx = malloc(sizeof(*ctx))) != NULL) - initialize_context(ctx, config_file, argc, argv, tmpvars); - - return (ctx); -} + /* Set default values */ + for (o = known_options; o->name != NULL; o++) { + ctx->options[o->index] = o->default_value == NULL ? + NULL : my_strdup(o->default_value); + } -struct shttpd_ctx * -shttpd_init(const char *config_file, ...) -{ - struct shttpd_ctx *ctx; - va_list ap; - const char *opt_name, *opt_value; - char *tmpvars[NELEMS(options)]; - const struct opt *opt; - size_t i; - - /* Initialize all temporary holders to NULL */ - for (i = 0; i < NELEMS(tmpvars); i++) - tmpvars[i] = NULL; - - if ((ctx = malloc(sizeof(*ctx))) != NULL) { - - va_start(ap, config_file); - while ((opt_name = va_arg(ap, const char *)) != NULL) { - opt_value = va_arg(ap, const char *); - - if ((opt = find_option(0, opt_name)) == NULL) - elog(E_FATAL, NULL, "shttpd_init: " - "unknown variable [%s]", opt_name); - set_option(opt, opt_value, tmpvars); - } - va_end(ap); + current_time = ctx->start_time = time(NULL); + tm = localtime(¤t_time); + tz_offset = 0; +#if 0 + tm->tm_gmtoff - 3600 * (tm->tm_isdst > 0 ? 1 : 0); +#endif - initialize_context(ctx, config_file, 0, NULL, tmpvars); - } + InitializeCriticalSection(&ctx->mutex); + + LL_INIT(&ctx->connections); + LL_INIT(&ctx->registered_uris); + LL_INIT(&ctx->error_handlers); + LL_INIT(&ctx->acl); + LL_INIT(&ctx->ssi_funcs); + LL_INIT(&ctx->listeners); + +#ifdef _WIN32 + {WSADATA data; WSAStartup(MAKEWORD(2,2), &data);} +#endif /* _WIN32 */ return (ctx); } diff --git a/libs/shttpd/config.h b/libs/shttpd/config.h new file mode 100644 index 0000000..b94127f --- /dev/null +++ b/libs/shttpd/config.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2004-2005 Sergey Lyubka + * All rights reserved + * + * "THE BEER-WARE LICENSE" (Revision 42): + * Sergey Lyubka wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. + */ + +#ifndef CONFIG_HEADER_DEFINED +#define CONFIG_HEADER_DEFINED + +#define VERSION "1.39" /* Version */ +#define CONFIG_FILE "shttpd.conf" /* Configuration file */ +#define HTPASSWD ".htpasswd" /* Passwords file name */ +#define URI_MAX 16384 /* Default max request size */ +#define LISTENING_PORTS "80" /* Default listening ports */ +#define INDEX_FILES "index.html,index.htm,index.php,index.cgi" +#define CGI_EXT "cgi,pl,php" /* Default CGI extensions */ +#define SSI_EXT "shtml,shtm" /* Default SSI extensions */ +#define REALM "mydomain.com" /* Default authentication realm */ +#define DELIM_CHARS " ," /* Separators for lists */ +#define EXPIRE_TIME 3600 /* Expiration time, seconds */ +#define ENV_MAX 4096 /* Size of environment block */ +#define CGI_ENV_VARS 64 /* Maximum vars passed to CGI */ + +#endif /* CONFIG_HEADER_DEFINED */ diff --git a/libs/shttpd/defs.h b/libs/shttpd/defs.h index 9c606f4..bfcb119 100644 --- a/libs/shttpd/defs.h +++ b/libs/shttpd/defs.h @@ -16,27 +16,7 @@ #include "io.h" #include "shttpd.h" #include "md5.h" - -#define VERSION "1.38" /* Version */ - -#ifndef CONFIG -#define CONFIG "shttpd.conf" /* Configuration file */ -#endif /* CONFIG */ - -#define HTPASSWD ".htpasswd" /* Passwords file name */ -#define DFLT_IO_SIZ "16384" /* Default max request size */ -#define LISTENING_PORTS "80" /* Default listening ports */ -#define INDEX_FILES "index.html index.htm index.php index.cgi" -#define CGI_EXT ".cgi .pl .php" /* Default CGI extensions */ -#define SSI_EXT ".shtml .shtm" /* Default SSI extensions */ -#define REALM "mydomain.com" /* Default authentication realm */ -#define DELIM_CHARS " ," /* Separators for lists */ - -#define EXPIRE_TIME 3600 /* Expiration time, seconds */ -#define ENV_MAX 4096 /* Size of environment block */ -#define CGI_ENV_VARS 64 /* Maximum vars passed to CGI */ -#define URI_MAX 32768 /* Maximum URI size */ -#define MIN_REQ_LEN 16 /* "GET / HTTP/1.1\n\n" */ +#include "config.h" #define NELEMS(ar) (sizeof(ar) / sizeof(ar[0])) @@ -65,6 +45,9 @@ struct vec { int len; }; +#if !defined(_WIN32) +enum {FALSE, TRUE}; +#endif /* _WIN32 */ enum {METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE, METHOD_HEAD}; enum {HDR_DATE, HDR_INT, HDR_STRING}; /* HTTP header types */ enum {E_FATAL = 1, E_LOG = 2}; /* Flags for elog() function */ @@ -96,22 +79,6 @@ union variant { }; /* - * This structure is used to hold mime types and associated file extensions. - */ -struct mime_type { - const char *ext; - int ext_len; - const char *mime; -}; - -struct mime_type_link { - struct llhead link; - char *ext; - int ext_len; - char *mime; -}; - -/* * This is used only in embedded configuration. This structure holds a * registered URI, associated callback function with callback data. * For non-embedded compilation shttpd_callback_t is not defined, so @@ -125,17 +92,6 @@ struct registered_uri { }; /* - * User may bind a passwords file to any URI. This makes that URI password - * protected: anybody who accesses that URI will be asked to authorize. - */ -struct uri_auth { - struct llhead link; - const char *uri; - const char *file_name; - size_t uri_len; -}; - -/* * User may want to handle certain errors. This structure holds the * handlers for corresponding error codes. */ @@ -249,7 +205,7 @@ struct conn { char *headers; /* Request headers */ char *query; /* QUERY_STRING part of the URI */ char *path_info; /* PATH_INFO thing */ - const char *mime_type; /* Mime type */ + struct vec mime_type; /* Mime type */ struct headers ch; /* Parsed client headers */ @@ -261,6 +217,15 @@ struct conn { #endif /* NO_SSI */ }; +enum { + OPT_ROOT, OPT_INDEX_FILES, OPT_PORTS, OPT_DIR_LIST, + OPT_CGI_EXTENSIONS, OPT_CGI_INTERPRETER, OPT_CGI_ENVIRONMENT, + OPT_SSI_EXTENSIONS, OPT_AUTH_REALM, OPT_AUTH_GPASSWD, + OPT_AUTH_PUT, OPT_ACCESS_LOG, OPT_ERROR_LOG, OPT_MIME_TYPES, + OPT_SSL_CERTIFICATE, OPT_ALIASES, OPT_ACL, OPT_INETD, OPT_UID, + OPT_CFG_URI, OPT_PROTECT, + NUM_OPTIONS +}; /* * SHTTPD context @@ -273,36 +238,17 @@ struct shttpd_ctx { SSL_CTX *ssl_ctx; /* SSL context */ struct llhead connections; /* List of connections */ - struct llhead mime_types; /* Known mime types */ struct llhead registered_uris;/* User urls */ - struct llhead uri_auths; /* User auth files */ struct llhead error_handlers; /* Embedded error handlers */ + struct llhead acl; /* Access control list */ + struct llhead ssi_funcs; /* SSI callback functions */ + struct llhead listeners; /* Listening sockets */ FILE *access_log; /* Access log stream */ FILE *error_log; /* Error log stream */ - char *put_auth_file; /* PUT auth file */ - char *document_root; /* Document root */ - char *index_files; /* Index files */ - char *aliases; /* Aliases */ - char *mime_file; /* Mime types file */ -#if !defined(NO_CGI) - char *cgi_vars; /* CGI environment variables */ - char *cgi_extensions; /* CGI extensions */ - char *cgi_interpreter; /* CGI script interpreter */ -#endif /* NO_CGI */ -#if !defined(NO_SSI) - char *ssi_extensions; /* SSI file extensions */ - struct llhead ssi_funcs; /* SSI callback functions */ -#endif /* NO_SSI */ - char *auth_realm; /* Auth realm */ - char *global_passwd_file; /* Global passwords file */ - char *uid; /* Run as user */ - char *ports; /* Listening ports */ - int dirlist; /* Directory listing */ - int gui; /* Show GUI flag */ - int auto_start; /* Start on OS boot */ - int io_buf_size; /* IO buffer size */ - int inetd_mode; /* Inetd flag */ + + char *options[NUM_OPTIONS]; /* Configurable options */ + #if defined(_WIN32) CRITICAL_SECTION mutex; /* For MT case */ HANDLE ev[2]; /* For thread synchronization */ @@ -311,36 +257,34 @@ struct shttpd_ctx { #endif /* _WIN32 */ }; -/* Option setter function */ -typedef void (*optset_t)(struct shttpd_ctx *, void *ptr, const char *string); -struct opt { - int sw; /* Command line switch */ - const char *name; /* Option name in config file */ - const char *desc; /* Description */ - optset_t setter; /* Option setter function */ - size_t ofs; /* Value offset in context */ - const char *arg; /* Argument format */ - const char *def; /* Default option value */ - unsigned int flags; /* Flags */ -#define OPT_BOOL 1 -#define OPT_INT 2 -#define OPT_FILE 4 -#define OPT_DIR 8 -#define OPT_ADVANCED 16 -}; - -extern const struct opt options[]; +#define IS_TRUE(ctx, opt) ((ctx)->options[opt] && (ctx)->options[opt][0] =='1') /* * In SHTTPD, list of values are represented as comma or space separated * string. For example, list of CGI extensions can be represented as * ".cgi,.php,.pl", or ".cgi .php .pl". The macro that follows allows to * loop through the individual values in that list. + * * A "const char *" pointer and size_t variable must be passed to the macro. - * Spaces or commas can be used as delimiters (macro DELIM_CHARS) + * Spaces or commas can be used as delimiters (macro DELIM_CHARS). + * + * In every iteration of the loop, "s" points to the current value, and + * "len" specifies its length. The code inside loop must not change + * "s" and "len" parameters. */ -#define FOR_EACH_WORD_IN_LIST(s,len) \ - for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; s += len + 1) +#define FOR_EACH_WORD_IN_LIST(s,len) \ + for (; s != NULL && (len = strcspn(s, DELIM_CHARS)) != 0; \ + s += len, s+= strspn(s, DELIM_CHARS)) + +/* + * IPv4 ACL entry. Specifies subnet with deny/allow flag + */ +struct acl { + struct llhead link; + uint32_t ip; /* IP, in network byte order */ + uint32_t mask; /* Also in network byte order */ + int flag; /* Either '+' or '-' */ +}; /* * shttpd.c @@ -355,18 +299,15 @@ extern void send_server_error(struct conn *, int code, const char *reason); extern int get_headers_len(const char *buf, size_t buflen); extern void parse_headers(const char *s, int len, struct headers *parsed); extern void open_listening_ports(struct shttpd_ctx *ctx); - -/* - * mime_type.c - */ -extern const char *get_mime_type(struct shttpd_ctx *, const char *uri, int len); -extern void set_mime_types(struct shttpd_ctx *ctx, const char *path); +extern void get_mime_type(struct shttpd_ctx *, const char *, int, struct vec *); +extern void free_list(struct llhead *head, void (*)(struct llhead *)); +extern void registered_uri_destructor(struct llhead *); +extern void listener_destructor(struct llhead *); /* * config.c */ extern void usage(const char *prog); -extern struct shttpd_ctx *init_from_argc_argv(const char *, int, char *[]); /* * log.c @@ -418,7 +359,7 @@ extern void setup_embedded_stream(struct conn *, union variant, void *); extern struct registered_uri *is_registered_uri(struct shttpd_ctx *, const char *uri); extern void do_ssi(struct conn *); -extern void free_ssi_funcs(struct shttpd_ctx *ctx); +extern void ssi_func_destructor(struct llhead *lp); /* * auth.c diff --git a/libs/shttpd/include/shttpd.h b/libs/shttpd/include/shttpd.h index ba349e8..0d8d510 100644 --- a/libs/shttpd/include/shttpd.h +++ b/libs/shttpd/include/shttpd.h @@ -19,7 +19,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * - * $Id: shttpd.h,v 1.3 2007/04/11 13:01:53 drozd Exp $ + * $Id: shttpd.h,v 1.9 2008/02/13 20:44:32 drozd Exp $ */ #ifndef SHTTPD_HEADER_INCLUDED @@ -63,23 +63,20 @@ struct shttpd_arg { * 4. for POST requests, it must process the incoming data (in.buf) of length * 'in.len', and set 'in.num_bytes', which is how many bytes of POST * data is read and can be discarded by SHTTPD. + * 5. If callback allocates arg->state, to keep state, it must deallocate it + * at the end of coonection SHTTPD_CONNECTION_ERROR or SHTTPD_END_OF_OUTPUT */ typedef void (*shttpd_callback_t)(struct shttpd_arg *); /* - * shttpd_init Initialize shttpd context. Parameters: configuration - * file name (may be NULL), then NULL-terminated - * sequence of pairs "option_name", "option_value". - * shttpd_init2 Same as shttpd_init, but the list of option/value - * pairs is passed in arrays + * shttpd_init Initialize shttpd context. + * shttpd_set_option Set new value for option. * shttpd_fini Dealocate the context * shttpd_register_uri Setup the callback function for specified URL. - * shttpd_protect_uri Associate authorization file with an URL. - * shttpd_add_mime_type Add mime type * shtppd_listen Setup a listening socket in the SHTTPD context * shttpd_poll Do connections processing * shttpd_version return string with SHTTPD version - * shttpd_get_var Return POST/GET variable value for given variable name. + * shttpd_get_var Fetch POST/GET variable value by name. Return value len * shttpd_get_header return value of the specified HTTP header * shttpd_get_env return string values for the following * pseudo-variables: "REQUEST_METHOD", "REQUEST_URI", @@ -88,30 +85,25 @@ typedef void (*shttpd_callback_t)(struct shttpd_arg *); struct shttpd_ctx; -struct shttpd_ctx *shttpd_init(const char *config_file, ...); -struct shttpd_ctx *shttpd_init2(const char *config_file, - char *names[], char *values[], size_t num_options); +struct shttpd_ctx *shttpd_init(void); +void shttpd_set_option(struct shttpd_ctx *, const char *opt, const char *val); void shttpd_fini(struct shttpd_ctx *); -void shttpd_add_mime_type(struct shttpd_ctx *, - const char *ext, const char *mime); int shttpd_listen(struct shttpd_ctx *ctx, int port, int is_ssl); -void shttpd_register_uri(struct shttpd_ctx *ctx, - const char *uri, shttpd_callback_t callback, void *user_data); -void shttpd_protect_uri(struct shttpd_ctx *ctx, - const char *uri, const char *file); +void shttpd_register_uri(struct shttpd_ctx *ctx, const char *uri, + shttpd_callback_t callback, void *const user_data); void shttpd_poll(struct shttpd_ctx *, int milliseconds); const char *shttpd_version(void); int shttpd_get_var(const char *var, const char *buf, int buf_len, char *value, int value_len); -const char *shttpd_get_header(struct shttpd_arg *, const char *); -const char *shttpd_get_env(struct shttpd_arg *, const char *); +const char *shttpd_get_header(struct shttpd_arg *, const char *header_name); +const char *shttpd_get_env(struct shttpd_arg *, const char *name); void shttpd_get_http_version(struct shttpd_arg *, unsigned long *major, unsigned long *minor); size_t shttpd_printf(struct shttpd_arg *, const char *fmt, ...); void shttpd_handle_error(struct shttpd_ctx *ctx, int status, - shttpd_callback_t func, void *data); + shttpd_callback_t func, void *const data); void shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name, - shttpd_callback_t func, void *user_data); + shttpd_callback_t func, void *const user_data); /* * The following three functions are for applications that need to @@ -121,7 +113,7 @@ void shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name, * pool for the idle thread by means of shttpd_active(), and add new * connection to the context by means of shttpd_add(). */ -void shttpd_add_socket(struct shttpd_ctx *, int sock); +void shttpd_add_socket(struct shttpd_ctx *, int sock, int is_ssl); int shttpd_accept(int lsn_sock, int milliseconds); int shttpd_active(struct shttpd_ctx *); diff --git a/libs/shttpd/io_dir.c b/libs/shttpd/io_dir.c index 0534c62..e1b6d13 100644 --- a/libs/shttpd/io_dir.c +++ b/libs/shttpd/io_dir.c @@ -121,6 +121,7 @@ get_dir(struct conn *c) } else { c->loc.io.head = my_snprintf(c->loc.io.buf, c->loc.io.size, "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n" "Content-Type: text/html; charset=utf-8\r\n\r\n" "Index of %s" "" diff --git a/libs/shttpd/io_emb.c b/libs/shttpd/io_emb.c index 42cd5c5..762afe5 100644 --- a/libs/shttpd/io_emb.c +++ b/libs/shttpd/io_emb.c @@ -10,7 +10,6 @@ #include "defs.h" -#if defined(EMBEDDED) const char * shttpd_version(void) { @@ -29,6 +28,12 @@ call_user(struct conn *c, struct shttpd_arg *arg, shttpd_callback_t func) arg->in.len = io_data_len(&c->rem.io); arg->in.num_bytes = 0; + if (io_data_len(&c->rem.io) >= c->rem.io.size) + arg->flags |= SHTTPD_POST_BUFFER_FULL; + + if (c->rem.content_len > 0 && c->rem.io.total < c->rem.content_len) + arg->flags |= SHTTPD_MORE_POST_DATA; + func(arg); io_inc_head(&c->loc.io, arg->out.num_bytes); @@ -177,32 +182,6 @@ shttpd_register_uri(struct shttpd_ctx *ctx, } } -#if 0 -struct shttpd_ctx * -shttpd_init2(const char *config_file, char *names[], char *values[], size_t n) -{ - size_t i; - - for (i = 0; i < n; i++) - set_option(names[i], values[i]); - - return (init_ctx(config_file, 0, NULL)); -} -#endif - -void -shttpd_protect_uri(struct shttpd_ctx *ctx, const char *uri, const char *file) -{ - struct uri_auth *auth; - - if ((auth = malloc(sizeof(*auth))) != NULL) { - auth->uri = my_strdup(uri); - auth->file_name = my_strdup(file); - auth->uri_len = strlen(uri); - LL_ADD(&ctx->uri_auths, &auth->link); - } -} - int shttpd_get_var(const char *var, const char *buf, int buf_len, char *value, int value_len) @@ -211,27 +190,24 @@ shttpd_get_var(const char *var, const char *buf, int buf_len, size_t var_len; var_len = strlen(var); - e = buf + buf_len - var_len; + e = buf + buf_len; /* End of QUERY_STRING buffer */ /* buf is "var1=val1&var2=val2...". Find variable first */ - for (p = buf; p < e; p++) - if (!my_strncasecmp(var, p, var_len) && p[var_len] == '=') { + for (p = buf; p + var_len < e; p++) + if ((p == buf || p[-1] == '&') && + p[var_len] == '=' && + !my_strncasecmp(var, p, var_len)) { - /* Found. Shift to variable value */ - e = buf + buf_len; - p += var_len + 1; - - /* Find where the value ends */ + /* Point 'p' to var value, 's' to the end of value */ + p += var_len + 1; if ((s = memchr(p, '&', e - p)) == NULL) s = e; - /* Copy value into the buffer, decoding it */ - url_decode(p, s - p, value, value_len); - - return (1); + /* URL-decode value. Return result length */ + return (url_decode(p, s - p, value, value_len)); } - return (0); + return (-1); } static int @@ -297,5 +273,3 @@ const struct io_class io_embedded = { (int (*)(struct stream *, const void *, size_t)) do_embedded, close_embedded }; - -#endif /* EMBEDDED */ diff --git a/libs/shttpd/io_file.c b/libs/shttpd/io_file.c index d2b3fb6..1dce074 100644 --- a/libs/shttpd/io_file.c +++ b/libs/shttpd/io_file.c @@ -98,8 +98,8 @@ get_file(struct conn *c, struct stat *stp) const char *fmt = "%a, %d %b %Y %H:%M:%S GMT", *msg = "OK"; big_int_t cl; /* Content-Length */ - if (c->mime_type == NULL) - c->mime_type = get_mime_type(c->ctx, c->uri, strlen(c->uri)); + if (c->mime_type.len == 0) + get_mime_type(c->ctx, c->uri, strlen(c->uri), &c->mime_type); cl = (big_int_t) stp->st_size; /* If Range: header specified, act accordingly */ @@ -131,11 +131,12 @@ get_file(struct conn *c, struct stat *stp) "Date: %s\r\n" "Last-Modified: %s\r\n" "Etag: \"%s\"\r\n" - "Content-Type: %s\r\n" + "Content-Type: %.*s\r\n" "Content-Length: %lu\r\n" - "Connection: close\r\n" + "Accept-Ranges: bytes\r\n" "%s\r\n", - status, msg, date, lm, etag, c->mime_type, cl, range); + status, msg, date, lm, etag, + c->mime_type.len, c->mime_type.ptr, cl, range); c->status = status; c->loc.content_len = cl; diff --git a/libs/shttpd/io_ssi.c b/libs/shttpd/io_ssi.c index 84176e1..74993f2 100644 --- a/libs/shttpd/io_ssi.c +++ b/libs/shttpd/io_ssi.c @@ -11,8 +11,6 @@ #include "defs.h" -#if !defined(NO_SSI) - #define CMDBUFSIZ 512 /* SSI command buffer size */ #define NEST_MAX 6 /* Maximum nesting level */ @@ -59,16 +57,12 @@ shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name, } void -free_ssi_funcs(struct shttpd_ctx *ctx) +ssi_func_destructor(struct llhead *lp) { - struct llhead *lp, *tmp; - struct ssi_func *e; + struct ssi_func *e = LL_ENTRY(lp, struct ssi_func, link); - LL_FOREACH_SAFE(&ctx->ssi_funcs, lp, tmp) { - e = LL_ENTRY(lp, struct ssi_func, link); - free(e->name); - free(e); - } + free(e->name); + free(e); } static const struct ssi_func * @@ -94,6 +88,16 @@ call(struct ssi *ssi, const char *name, (void) memset(arg, 0, sizeof(*arg)); + /* + * SSI function may be called with parameters. These parameters + * are passed as arg->in.buf, arg->in.len vector. + */ + arg->in.buf = strchr(name, ' '); + if (arg->in.buf != NULL) { + *arg->in.buf++ = '\0'; + arg->in.len = strlen(arg->in.buf); + } + if ((ssi_func = find_ssi_func(ssi, name)) != NULL) { arg->priv = ssi->conn; arg->user_data = ssi_func->user_data; @@ -135,7 +139,7 @@ get_path(struct conn *conn, const char *src, {NULL, 0}, }; struct vec *vec; - const char *p; + const char *p, *root = conn->ctx->options[OPT_ROOT]; int len; for (vec = accepted; vec->len > 0; vec++) @@ -146,14 +150,14 @@ get_path(struct conn *conn, const char *src, break; if (vec->len == 6) { len = my_snprintf(dst, dst_len, "%s%c%s", - conn->ctx->document_root, DIRSEP, conn->uri); + root, DIRSEP, conn->uri); while (len > 0 && dst[len] != '/') len--; dst += len; dst_len -= len; } else if (vec->len == 9) { len = my_snprintf(dst, dst_len, "%s%c", - conn->ctx->document_root, DIRSEP); + root, DIRSEP); dst += len; dst_len -= len; } @@ -196,16 +200,16 @@ do_include(struct ssi *ssi) static char * trim_spaces(struct ssi_inc *inc) { - unsigned char *p = inc->buf + inc->nbuf - 2; + char *p = inc->buf + inc->nbuf - 2; /* Trim spaces from the right */ *p-- = '\0'; - while (isspace(*p)) + while (isspace(* (unsigned char *) p)) *p-- = '\0'; /* Shift pointer to the start of attributes */ - for (p = inc->buf; !isspace(*p); p++); - while (*p && isspace(*p)) p++; + for (p = inc->buf; !isspace(* (unsigned char *) p); p++); + while (*p && isspace(* (unsigned char *) p)) p++; return (p); } @@ -214,15 +218,16 @@ static void do_if(struct ssi *ssi) { struct ssi_inc *inc = ssi->incs + ssi->nest; - unsigned char *name = trim_spaces(inc); + char *name = trim_spaces(inc); inc->cond = evaluate(ssi, name) ? SSI_GO : SSI_STOP; } + static void do_elif(struct ssi *ssi) { struct ssi_inc *inc = ssi->incs + ssi->nest; - unsigned char *name = trim_spaces(inc); + char *name = trim_spaces(inc); if (inc->cond == SSI_STOP && evaluate(ssi, name)) inc->cond = SSI_GO; @@ -258,8 +263,8 @@ do_call2(struct ssi *ssi, char *buf, int len, int *n) static void do_call(struct ssi *ssi, char *buf, int len, int *n) { - struct ssi_inc *inc = ssi->incs + ssi->nest; - unsigned char *name = trim_spaces(inc); + struct ssi_inc *inc = ssi->incs + ssi->nest; + char *name = trim_spaces(inc); if (inc->cond == SSI_GO) { (void) memmove(inc->buf, name, strlen(name) + 1); @@ -290,8 +295,7 @@ static void do_exec(struct ssi *ssi, char *buf, int len, int *n) { struct ssi_inc *inc = ssi->incs + ssi->nest; - unsigned char *e, *p; - char cmd[sizeof(inc->buf)]; + char cmd[sizeof(inc->buf)], *e, *p; p = trim_spaces(inc); @@ -354,7 +358,6 @@ read_ssi(struct stream *stream, void *vbuf, size_t len) char *buf = vbuf; int ch = EOF, n = 0; - again: if (inc->state == SSI_CALL) @@ -376,6 +379,15 @@ again: } break; + /* + * We are buffering whole SSI command, until closing "-->". + * That means that when do_command() is called, we can rely + * on that full command with arguments is buffered in and + * there is no need for streaming. + * Restrictions: + * 1. The command must fit in CMDBUFSIZ + * 2. HTML comments inside the command ? Not sure about this. + */ case SSI_BUF: if (inc->nbuf >= sizeof(inc->buf) - 1) { pass(inc, buf + n, &n); @@ -393,7 +405,12 @@ again: } break; + case SSI_EXEC: + case SSI_CALL: + break; + default: + /* Never happens */ abort(); break; } @@ -463,5 +480,3 @@ const struct io_class io_ssi = { NULL, close_ssi }; - -#endif /* NO_SSI */ diff --git a/libs/shttpd/log.c b/libs/shttpd/log.c index 4b43610..583142f 100644 --- a/libs/shttpd/log.c +++ b/libs/shttpd/log.c @@ -22,7 +22,7 @@ elog(int flags, struct conn *c, const char *fmt, ...) va_list ap; /* Print to stderr */ - if (c == NULL || c->ctx->inetd_mode == 0) { + if (c == NULL || !IS_TRUE(c->ctx, OPT_INETD)) { va_start(ap, fmt); (void) vfprintf(stderr, fmt, ap); (void) fputc('\n', stderr); @@ -48,15 +48,6 @@ elog(int flags, struct conn *c, const char *fmt, ...) (void) fflush(fp); } -#if defined(_WIN32) && !defined(NO_GUI) - { - extern HWND hLog; - - if (hLog != NULL) - SendMessage(hLog, WM_APP, 0, (LPARAM) buf); - } -#endif /* _WIN32 */ - if (flags & E_FATAL) exit(EXIT_FAILURE); } @@ -85,7 +76,7 @@ log_access(FILE *fp, const struct conn *c) } (void) strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S", - localtime(¤t_time)); + localtime(&c->birth_time)); (void) my_snprintf(buf, sizeof(buf), "%s - %.*s [%s %+05d] \"%s\" %d %lu %s%.*s%s %s%.*s%s", @@ -99,13 +90,4 @@ log_access(FILE *fp, const struct conn *c) (void) fprintf(fp, "%s\n", buf); (void) fflush(fp); } - -#if defined(_WIN32) && !defined(NO_GUI) - { - extern HWND hLog; - - if (hLog != NULL) - SendMessage(hLog, WM_APP, 0, (LPARAM) buf); - } -#endif /* _WIN32 */ } diff --git a/libs/shttpd/mime_type.c b/libs/shttpd/mime_type.c deleted file mode 100644 index 61f8150..0000000 --- a/libs/shttpd/mime_type.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2004-2005 Sergey Lyubka - * All rights reserved - * - * "THE BEER-WARE LICENSE" (Revision 42): - * Sergey Lyubka wrote this file. As long as you retain this notice you - * can do whatever you want with this stuff. If we meet some day, and you think - * this stuff is worth it, you can buy me a beer in return. - */ - -#include "defs.h" - -static const struct mime_type default_mime_types[] = { - {"html", 4, "text/html" }, - {"htm", 3, "text/html" }, - {"txt", 3, "text/plain" }, - {"css", 3, "text/css" }, - {"ico", 3, "image/x-icon" }, - {"gif", 3, "image/gif" }, - {"jpg", 3, "image/jpeg" }, - {"jpeg", 4, "image/jpeg" }, - {"png", 3, "image/png" }, - {"svg", 3, "image/svg+xml" }, - {"torrent", 7, "application/x-bittorrent" }, - {"wav", 3, "audio/x-wav" }, - {"mp3", 3, "audio/x-mp3" }, - {"mid", 3, "audio/mid" }, - {"m3u", 3, "audio/x-mpegurl" }, - {"ram", 3, "audio/x-pn-realaudio" }, - {"ra", 2, "audio/x-pn-realaudio" }, - {"doc", 3, "application/msword", }, - {"exe", 3, "application/octet-stream" }, - {"zip", 3, "application/x-zip-compressed" }, - {"xls", 3, "application/excel" }, - {"tgz", 3, "application/x-tar-gz" }, - {"tar.gz", 6, "application/x-tar-gz" }, - {"tar", 3, "application/x-tar" }, - {"gz", 2, "application/x-gunzip" }, - {"arj", 3, "application/x-arj-compressed" }, - {"rar", 3, "application/x-arj-compressed" }, - {"rtf", 3, "application/rtf" }, - {"pdf", 3, "application/pdf" }, - {"mpg", 3, "video/mpeg" }, - {"mpeg", 4, "video/mpeg" }, - {"asf", 3, "video/x-ms-asf" }, - {"avi", 3, "video/x-msvideo" }, - {"bmp", 3, "image/bmp" }, - {NULL, 0, NULL } -}; - -const char * -get_mime_type(struct shttpd_ctx *ctx, const char *uri, int len) -{ - struct llhead *lp; - const struct mime_type *mt; - struct mime_type_link *mtl; - const char *s; - - /* Firt, loop through the custom mime types if any */ - LL_FOREACH(&ctx->mime_types, lp) { - mtl = LL_ENTRY(lp, struct mime_type_link, link); - s = uri + len - mtl->ext_len; - if (s > uri && s[-1] == '.' && - !my_strncasecmp(mtl->ext, s, mtl->ext_len)) - return (mtl->mime); - } - - /* If no luck, try built-in mime types */ - for (mt = default_mime_types; mt->ext != NULL; mt++) { - s = uri + len - mt->ext_len; - if (s > uri && s[-1] == '.' && - !my_strncasecmp(mt->ext, s, mt->ext_len)) - return (mt->mime); - } - - /* Oops. This extension is unknown to us. Fallback to text/plain */ - return ("text/plain"); -} - -void -set_mime_types(struct shttpd_ctx *ctx, const char *path) -{ - FILE *fp; - char line[512], ext[sizeof(line)], mime[sizeof(line)], *s; - - if ((fp = fopen(path, "r")) == NULL) - elog(E_FATAL, NULL, "set_mime_types: fopen(%s): %s", - path, strerror(errno)); - - while (fgets(line, sizeof(line), fp) != NULL) { - /* Skip empty lines */ - if (line[0] == '#' || line[0] == '\n') - continue; - if (sscanf(line, "%s", mime)) { - s = line + strlen(mime); - while (*s && *s != '\n' && sscanf(s, "%s", ext)) { - shttpd_add_mime_type(ctx, ext, mime); - s += strlen(mime); - } - } - } - - (void) fclose(fp); -} diff --git a/libs/shttpd/shttpd.c b/libs/shttpd/shttpd.c index 36372d2..a0f1bcc 100644 --- a/libs/shttpd/shttpd.c +++ b/libs/shttpd/shttpd.c @@ -10,7 +10,7 @@ /* * Small and portable HTTP server, http://shttpd.sourceforge.net - * $Id: shttpd.c,v 1.10 2007/06/01 17:59:32 drozd Exp $ + * $Id: shttpd.c,v 1.28 2008/02/17 21:45:09 drozd Exp $ */ #include "defs.h" @@ -18,8 +18,6 @@ time_t current_time; /* Current UTC time */ int tz_offset; /* Time zone offset from UTC */ -static LL_HEAD(listeners); /* List of listening sockets */ - const struct vec known_http_methods[] = { {"GET", 3}, {"POST", 4}, @@ -58,6 +56,7 @@ static const struct http_header http_headers[] = { }; struct shttpd_ctx *init_ctx(const char *config_file, int argc, char *argv[]); +static void process_connection(struct conn *, int, int); int url_decode(const char *src, int src_len, char *dst, int dst_len) @@ -78,9 +77,6 @@ url_decode(const char *src, int src_len, char *dst, int dst_len) dst[j] = '%'; } break; - case '+': - dst[j] = ' '; - break; default: dst[j] = src[i]; break; @@ -91,37 +87,21 @@ url_decode(const char *src, int src_len, char *dst, int dst_len) return (j); } -void -shttpd_add_mime_type(struct shttpd_ctx *ctx, const char *ext, const char *mime) -{ - struct mime_type_link *e; - const char *error_msg = "shttpd_add_mime_type: no memory"; - - if ((e = malloc(sizeof(*e))) == NULL) { - elog(E_FATAL, 0, error_msg); - } else if ((e->ext= my_strdup(ext)) == NULL) { - elog(E_FATAL, 0, error_msg); - } else if ((e->mime = my_strdup(mime)) == NULL) { - elog(E_FATAL, 0, error_msg); - } else { - e->ext_len = strlen(ext); - LL_TAIL(&ctx->mime_types, &e->link); - } -} - - static const char * is_alias(struct shttpd_ctx *ctx, const char *uri, struct vec *a_uri, struct vec *a_path) { - const char *p, *s = ctx->aliases; + const char *p, *s = ctx->options[OPT_ALIASES]; size_t len; DBG(("is_alias: aliases [%s]", s == NULL ? "" : s)); FOR_EACH_WORD_IN_LIST(s, len) { - if ((p = memchr(s, '=', len)) != NULL && - memcmp(uri, s, p - s) == 0) { + + if ((p = memchr(s, '=', len)) == NULL || p >= s + len || p == s) + continue; + + if (memcmp(uri, s, p - s) == 0) { a_uri->ptr = s; a_uri->len = p - s; a_path->ptr = ++p; @@ -202,7 +182,8 @@ get_headers_len(const char *buf, size_t buflen) for (s = buf, e = s + buflen - 1; len == 0 && s < e; s++) /* Control characters are not allowed but >=128 is. */ - if (!isprint(*(unsigned char *)s) && *s != '\r' && *s != '\n' && *(unsigned char *)s < 128) + if (!isprint(* (unsigned char *) s) && *s != '\r' && + *s != '\n' && * (unsigned char *) s < 128) len = -1; else if (s[0] == '\n' && s[1] == '\n') len = s - buf + 2; @@ -219,7 +200,6 @@ get_headers_len(const char *buf, size_t buflen) void send_server_error(struct conn *c, int status, const char *reason) { -#ifdef EMBEDDED struct llhead *lp; struct error_handler *e; @@ -231,16 +211,20 @@ send_server_error(struct conn *c, int status, const char *reason) c->loc.io_class->close != NULL) c->loc.io_class->close(&c->loc); io_clear(&c->loc.io); - setup_embedded_stream(c, e->callback, NULL); + setup_embedded_stream(c, e->callback, e->callback_data); return; } } -#endif /* EMBEDDED */ io_clear(&c->loc.io); - c->loc.headers_len = c->loc.io.head = my_snprintf(c->loc.io.buf, - c->loc.io.size, "HTTP/1.1 %d %s\r\n\r\n%d %s", - status, reason, status, reason); + c->loc.io.head = my_snprintf(c->loc.io.buf, c->loc.io.size, + "HTTP/1.1 %d %s\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 12\r\n" + "\r\n" + "Error: %03d\r\n", + status, reason, status); + c->loc.content_len = 10; c->status = status; stop_stream(&c->loc); } @@ -313,8 +297,8 @@ remove_double_dots(char *s) while (*s != '\0') { *p++ = *s++; - if (s[-1] == '/') - while (*s == '.' || *s == '/') + if (s[-1] == '/' || s[-1] == '\\') + while (*s == '.' || *s == '/' || *s == '\\') s++; } *p = '\0'; @@ -367,6 +351,85 @@ parse_headers(const char *s, int len, struct headers *parsed) } } +static const struct { + const char *extension; + int ext_len; + const char *mime_type; +} builtin_mime_types[] = { + {"html", 4, "text/html" }, + {"htm", 3, "text/html" }, + {"txt", 3, "text/plain" }, + {"css", 3, "text/css" }, + {"ico", 3, "image/x-icon" }, + {"gif", 3, "image/gif" }, + {"jpg", 3, "image/jpeg" }, + {"jpeg", 4, "image/jpeg" }, + {"png", 3, "image/png" }, + {"svg", 3, "image/svg+xml" }, + {"torrent", 7, "application/x-bittorrent" }, + {"wav", 3, "audio/x-wav" }, + {"mp3", 3, "audio/x-mp3" }, + {"mid", 3, "audio/mid" }, + {"m3u", 3, "audio/x-mpegurl" }, + {"ram", 3, "audio/x-pn-realaudio" }, + {"ra", 2, "audio/x-pn-realaudio" }, + {"doc", 3, "application/msword", }, + {"exe", 3, "application/octet-stream" }, + {"zip", 3, "application/x-zip-compressed" }, + {"xls", 3, "application/excel" }, + {"tgz", 3, "application/x-tar-gz" }, + {"tar.gz", 6, "application/x-tar-gz" }, + {"tar", 3, "application/x-tar" }, + {"gz", 2, "application/x-gunzip" }, + {"arj", 3, "application/x-arj-compressed" }, + {"rar", 3, "application/x-arj-compressed" }, + {"rtf", 3, "application/rtf" }, + {"pdf", 3, "application/pdf" }, + {"swf", 3, "application/x-shockwave-flash" }, + {"mpg", 3, "video/mpeg" }, + {"mpeg", 4, "video/mpeg" }, + {"asf", 3, "video/x-ms-asf" }, + {"avi", 3, "video/x-msvideo" }, + {"bmp", 3, "image/bmp" }, + {NULL, 0, NULL } +}; + +void +get_mime_type(struct shttpd_ctx *ctx, const char *uri, int len, struct vec *vec) +{ + const char *eq, *p = ctx->options[OPT_MIME_TYPES]; + int i, n, ext_len; + + /* Firt, loop through the custom mime types if any */ + FOR_EACH_WORD_IN_LIST(p, n) { + if ((eq = memchr(p, '=', n)) == NULL || eq >= p + n || eq == p) + continue; + ext_len = eq - p; + if (len > ext_len && uri[len - ext_len - 1] == '.' && + !my_strncasecmp(p, &uri[len - ext_len], ext_len)) { + vec->ptr = eq + 1; + vec->len = p + n - vec->ptr; + return; + } + } + + /* If no luck, try built-in mime types */ + for (i = 0; builtin_mime_types[i].extension != NULL; i++) { + ext_len = builtin_mime_types[i].ext_len; + if (len > ext_len && uri[len - ext_len - 1] == '.' && + !my_strncasecmp(builtin_mime_types[i].extension, + &uri[len - ext_len], ext_len)) { + vec->ptr = builtin_mime_types[i].mime_type; + vec->len = strlen(vec->ptr); + return; + } + } + + /* Oops. This extension is unknown to us. Fallback to text/plain */ + vec->ptr = "text/plain"; + vec->len = strlen(vec->ptr); +} + /* * For given directory path, substitute it to valid index file. * Return 0 if index file has been found, -1 if not found @@ -375,14 +438,14 @@ static int find_index_file(struct conn *c, char *path, size_t maxpath, struct stat *stp) { char buf[FILENAME_MAX]; - const char *s = c->ctx->index_files; + const char *s = c->ctx->options[OPT_INDEX_FILES]; size_t len; FOR_EACH_WORD_IN_LIST(s, len) { my_snprintf(buf, sizeof(buf), "%s%c%.*s",path, DIRSEP, len, s); if (my_stat(buf, stp) == 0) { my_strlcpy(path, buf, maxpath); - c->mime_type = get_mime_type(c->ctx, s, len); + get_mime_type(c->ctx, s, len, &c->mime_type); return (0); } } @@ -404,7 +467,7 @@ get_path_info(struct conn *c, char *path, struct stat *stp) return (0); p = path + strlen(path); - e = path + strlen(c->ctx->document_root) + 2; + e = path + strlen(c->ctx->options[OPT_ROOT]) + 2; /* Strip directory parts of the path one by one */ for (; p > e; p--) @@ -425,13 +488,11 @@ get_path_info(struct conn *c, char *path, struct stat *stp) static void decide_what_to_do(struct conn *c) { - char path[URI_MAX], buf[1024]; + char path[URI_MAX], buf[1024], *root; struct vec alias_uri, alias_path; struct stat st; int rc; -#ifdef EMBEDDED struct registered_uri *ruri; -#endif /* EMBEDDED */ DBG(("decide_what_to_do: [%s]", c->uri)); @@ -441,13 +502,13 @@ decide_what_to_do(struct conn *c) url_decode(c->uri, strlen(c->uri), c->uri, strlen(c->uri) + 1); remove_double_dots(c->uri); - if (strlen(c->uri) + strlen(c->ctx->document_root) >= sizeof(path)) { + root = c->ctx->options[OPT_ROOT]; + if (strlen(c->uri) + strlen(root) >= sizeof(path)) { send_server_error(c, 400, "URI is too long"); return; } - (void) my_snprintf(path, sizeof(path), "%s%s", - c->ctx->document_root, c->uri); + (void) my_snprintf(path, sizeof(path), "%s%s", root, c->uri); /* User may use the aliases - check URI for mount point */ if (is_alias(c->ctx, c->uri, &alias_uri, &alias_path) != NULL) { @@ -462,18 +523,17 @@ decide_what_to_do(struct conn *c) send_authorization_request(c); } else #endif /* NO_AUTH */ -#ifdef EMBEDDED if ((ruri = is_registered_uri(c->ctx, c->uri)) != NULL) { setup_embedded_stream(c, ruri->callback, ruri->callback_data); } else -#endif /* EMBEDDED */ if (strstr(path, HTPASSWD)) { /* Do not allow to view passwords files */ send_server_error(c, 403, "Forbidden"); } else #if !defined(NO_AUTH) if ((c->method == METHOD_PUT || c->method == METHOD_DELETE) && - (c->ctx->put_auth_file == NULL || !is_authorized_for_put(c))) { + (c->ctx->options[OPT_AUTH_PUT] == NULL || + !is_authorized_for_put(c))) { send_authorization_request(c); } else #endif /* NO_AUTH */ @@ -510,17 +570,17 @@ decide_what_to_do(struct conn *c) send_server_error(c, 301, buf); } else if (S_ISDIR(st.st_mode) && find_index_file(c, path, sizeof(path) - 1, &st) == -1 && - c->ctx->dirlist == 0) { + !IS_TRUE(c->ctx, OPT_DIR_LIST)) { send_server_error(c, 403, "Directory Listing Denied"); - } else if (S_ISDIR(st.st_mode) && c->ctx->dirlist) { + } else if (S_ISDIR(st.st_mode) && IS_TRUE(c->ctx, OPT_DIR_LIST)) { if ((c->loc.chan.dir.path = my_strdup(path)) != NULL) get_dir(c); else send_server_error(c, 500, "GET Directory Error"); - } else if (S_ISDIR(st.st_mode) && c->ctx->dirlist == 0) { + } else if (S_ISDIR(st.st_mode) && !IS_TRUE(c->ctx, OPT_DIR_LIST)) { send_server_error(c, 403, "Directory listing denied"); #if !defined(NO_CGI) - } else if (match_extension(path, c->ctx->cgi_extensions)) { + } else if (match_extension(path, c->ctx->options[OPT_CGI_EXTENSIONS])) { if (c->method != METHOD_POST && c->method != METHOD_GET) { send_server_error(c, 501, "Bad method "); } else if ((run_cgi(c, path)) == -1) { @@ -530,7 +590,7 @@ decide_what_to_do(struct conn *c) } #endif /* NO_CGI */ #if !defined(NO_SSI) - } else if (match_extension(path, c->ctx->ssi_extensions)) { + } else if (match_extension(path, c->ctx->options[OPT_SSI_EXTENSIONS])) { if ((c->loc.chan.fd = my_open(path, O_RDONLY | O_BINARY, 0644)) == -1) { send_server_error(c, 500, "SSI open error"); @@ -553,8 +613,6 @@ set_request_method(struct conn *c) { const struct vec *v; - assert(c->rem.io.head >= MIN_REQ_LEN); - /* Set the request method */ for (v = known_http_methods; v->ptr != NULL; v++) if (!memcmp(c->rem.io.buf, v->ptr, v->len)) { @@ -569,23 +627,28 @@ static void parse_http_request(struct conn *c) { char *s, *e, *p, *start; - char *end_number; - int uri_len, req_len; + int uri_len, req_len, n; - s = c->rem.io.buf; - req_len = c->rem.headers_len = get_headers_len(s, c->rem.io.head); + s = io_data(&c->rem.io);; + req_len = c->rem.headers_len = + get_headers_len(s, io_data_len(&c->rem.io)); - if (req_len == 0 && io_space_len(&c->rem.io) == 0) + if (req_len == 0 && io_space_len(&c->rem.io) == 0) { + io_clear(&c->rem.io); send_server_error(c, 400, "Request is too big"); + } + + io_inc_tail(&c->rem.io, req_len); - if (req_len == 0) + if (req_len == 0) { return; - else if (req_len < MIN_REQ_LEN) + } else if (req_len < 16) { /* Minimal: "GET / HTTP/1.0\n\n" */ send_server_error(c, 400, "Bad request"); - else if (set_request_method(c)) + } else if (set_request_method(c)) { send_server_error(c, 501, "Method Not Implemented"); - else if ((c->request = my_strndup(s, req_len)) == NULL) + } else if ((c->request = my_strndup(s, req_len)) == NULL) { send_server_error(c, 500, "Cannot allocate request"); + } if (c->loc.flags & FLAG_CLOSED) return; @@ -607,41 +670,25 @@ parse_http_request(struct conn *c) * First, we skip the REQUEST_METHOD and shift to the URI. */ for (p = c->request, e = p + req_len; *p != ' ' && p < e; p++); - while (p < e && *p == ' ') p++; + while (p < e && *p == ' ') + p++; /* Now remember where URI starts, and shift to the end of URI */ for (start = p; p < e && !isspace((unsigned char)*p); ) p++; uri_len = p - start; + /* Skip space following the URI */ - while (p < e && *p == ' ') p++; + while (p < e && *p == ' ') + p++; /* Now comes the HTTP-Version in the form HTTP/. */ - if (strncmp(p, "HTTP/", 5) != 0) { + if (sscanf(p, "HTTP/%lu.%lu%n", + &c->major_version, &c->minor_version, &n) != 2 || p[n] != '\0') { send_server_error(c, 400, "Bad HTTP version"); - return; - } - p += 5; - /* Parse the HTTP major version number */ - c->major_version = strtoul(p, &end_number, 10); - if (end_number == p || *end_number != '.') { - send_server_error(c, 400, "Bad HTTP major version"); - return; - } - p = end_number + 1; - /* Parse the minor version number */ - c->minor_version = strtoul(p, &end_number, 10); - if (end_number == p || *end_number != '\0') { - send_server_error(c, 400, "Bad HTTP minor version"); - return; - } - /* Version must be <=1.1 */ - if (c->major_version > 1 || + } else if (c->major_version > 1 || (c->major_version == 1 && c->minor_version > 1)) { send_server_error(c, 505, "HTTP version not supported"); - return; - } - - if (uri_len <= 0) { + } else if (uri_len <= 0) { send_server_error(c, 400, "Bad URI"); } else if ((c->uri = malloc(uri_len + 1)) == NULL) { send_server_error(c, 500, "Cannot allocate URI"); @@ -653,20 +700,17 @@ parse_http_request(struct conn *c) /* Remove the length of request from total, count only data */ assert(c->rem.io.total >= (big_int_t) req_len); c->rem.io.total -= req_len; - c->rem.content_len = c->ch.cl.v_big_int; - io_inc_tail(&c->rem.io, req_len); - decide_what_to_do(c); } } void -shttpd_add_socket(struct shttpd_ctx *ctx, int sock) +shttpd_add_socket(struct shttpd_ctx *ctx, int sock, int is_ssl) { struct conn *c; struct usa sa; - int l = ctx->inetd_mode ? E_FATAL : E_LOG; + int l = IS_TRUE(ctx, OPT_INETD) ? E_FATAL : E_LOG; #if !defined(NO_SSL) SSL *ssl = NULL; #endif /* NO_SSL */ @@ -677,16 +721,15 @@ shttpd_add_socket(struct shttpd_ctx *ctx, int sock) if (getpeername(sock, &sa.u.sa, &sa.len)) { elog(l, NULL, "add_socket: %s", strerror(errno)); #if !defined(NO_SSL) - } else if (ctx->ssl_ctx && (ssl = SSL_new(ctx->ssl_ctx)) == NULL) { + } else if (is_ssl && (ssl = SSL_new(ctx->ssl_ctx)) == NULL) { elog(l, NULL, "add_socket: SSL_new: %s", strerror(ERRNO)); (void) closesocket(sock); - } else if (ctx->ssl_ctx && SSL_set_fd(ssl, sock) == 0) { + } else if (is_ssl && SSL_set_fd(ssl, sock) == 0) { elog(l, NULL, "add_socket: SSL_set_fd: %s", strerror(ERRNO)); (void) closesocket(sock); SSL_free(ssl); #endif /* NO_SSL */ - } else if ((c = calloc(1, sizeof(*c) + 2 * ctx->io_buf_size)) == NULL) { - + } else if ((c = calloc(1, sizeof(*c) + 2 * URI_MAX)) == NULL) { #if !defined(NO_SSL) if (ssl) SSL_free(ssl); @@ -713,11 +756,11 @@ shttpd_add_socket(struct shttpd_ctx *ctx, int sock) /* Set IO buffers */ c->loc.io.buf = (char *) (c + 1); - c->rem.io.buf = c->loc.io.buf + ctx->io_buf_size; - c->loc.io.size = c->rem.io.size = ctx->io_buf_size; + c->rem.io.buf = c->loc.io.buf + URI_MAX; + c->loc.io.size = c->rem.io.size = URI_MAX; #if !defined(NO_SSL) - if (ssl) { + if (is_ssl) { c->rem.io_class = &io_ssl; c->rem.chan.ssl.sock = sock; c->rem.chan.ssl.ssl = ssl; @@ -764,7 +807,7 @@ shttpd_listen(struct shttpd_ctx *ctx, int port, int is_ssl) l->is_ssl = is_ssl; l->sock = sock; l->ctx = ctx; - LL_TAIL(&listeners, &l->link); + LL_TAIL(&ctx->listeners, &l->link); DBG(("shttpd_listen: added socket %d", sock)); } @@ -860,21 +903,20 @@ write_stream(struct stream *from, struct stream *to) static void -disconnect(struct conn *c) +disconnect(struct llhead *lp) { - static const struct vec ka = {"keep-alive", 10}; - int dont_close; + struct conn *c = LL_ENTRY(lp, struct conn, link); + static const struct vec vec = {"close", 5}; + int do_close; DBG(("Disconnecting %d (%.*s)", c->rem.chan.sock, c->ch.connection.v_vec.len, c->ch.connection.v_vec.ptr)); -#if !defined(_WIN32) || defined(NO_GUI) - if (c->ctx->access_log != NULL) -#endif /* _WIN32 */ - log_access(c->ctx->access_log, c); + if (c->request != NULL && c->ctx->access_log != NULL) + log_access(c->ctx->access_log, c); /* In inetd mode, exit if request is finished. */ - if (c->ctx->inetd_mode) + if (IS_TRUE(c->ctx, OPT_INETD)) exit(0); if (c->loc.io_class != NULL && c->loc.io_class->close != NULL) @@ -884,25 +926,29 @@ disconnect(struct conn *c) * Check the "Connection: " header before we free c->request * If it its 'keep-alive', then do not close the connection */ - dont_close = c->ch.connection.v_vec.len >= ka.len && - !my_strncasecmp(ka.ptr, c->ch.connection.v_vec.ptr, ka.len); + do_close = (c->ch.connection.v_vec.len >= vec.len && + !my_strncasecmp(vec.ptr, c->ch.connection.v_vec.ptr, vec.len)) || + (c->major_version < 1 || + (c->major_version >= 1 && c->minor_version < 1)); if (c->request) free(c->request); if (c->uri) free(c->uri); - /* Handle Keep-Alive */ - dont_close = 0; - if (dont_close) { + /* Keep the connection open only if we have Content-Length set */ + if (!do_close && c->loc.content_len > 0) { c->loc.io_class = NULL; - c->loc.flags = c->rem.flags = 0; + c->loc.flags = 0; + c->loc.content_len = 0; + c->rem.flags = FLAG_W | FLAG_R; c->query = c->request = c->uri = c->path_info = NULL; - c->mime_type = NULL; + c->mime_type.len = 0; (void) memset(&c->ch, 0, sizeof(c->ch)); - io_clear(&c->rem.io); io_clear(&c->loc.io); - c->rem.io.total = c->loc.io.total = 0; + c->birth_time = current_time; + if (io_data_len(&c->rem.io) > 0) + process_connection(c, 0, 0); } else { if (c->rem.io_class != NULL) c->rem.io_class->close(&c->rem); @@ -917,6 +963,24 @@ disconnect(struct conn *c) } } +static int +is_allowed(const struct shttpd_ctx *ctx, const struct usa *usa) +{ + const struct acl *acl; + const struct llhead *lp; + int allowed = '+'; + uint32_t ip; + + LL_FOREACH(&ctx->acl, lp) { + acl = LL_ENTRY(lp, struct acl, link); + (void) memcpy(&ip, &usa->u.sin.sin_addr, sizeof(ip)); + if (acl->ip == (ntohl(ip) & acl->mask)) + allowed = acl->flag; + } + + return (allowed == '+'); +} + static void add_to_set(int fd, fd_set *set, int *max_fd) { @@ -925,6 +989,45 @@ add_to_set(int fd, fd_set *set, int *max_fd) *max_fd = fd; } +static void +process_connection(struct conn *c, int remote_ready, int local_ready) +{ + /* Read from remote end if it is ready */ + if (remote_ready && io_space_len(&c->rem.io)) + read_stream(&c->rem); + + /* If the request is not parsed yet, do so */ + if (!(c->rem.flags & FLAG_HEADERS_PARSED)) + parse_http_request(c); + + DBG(("loc: %u [%.*s]", io_data_len(&c->loc.io), + io_data_len(&c->loc.io), io_data(&c->loc.io))); + DBG(("rem: %u [%.*s]", io_data_len(&c->rem.io), + io_data_len(&c->rem.io), io_data(&c->rem.io))); + + /* Read from the local end if it is ready */ + if (local_ready && io_space_len(&c->loc.io)) + read_stream(&c->loc); + + if (io_data_len(&c->rem.io) > 0 && (c->loc.flags & FLAG_W) && + c->loc.io_class != NULL && c->loc.io_class->write != NULL) + write_stream(&c->rem, &c->loc); + + if (io_data_len(&c->loc.io) > 0 && c->rem.io_class != NULL) + write_stream(&c->loc, &c->rem); + + if (c->rem.nread_last > 0) + c->ctx->in += c->rem.nread_last; + if (c->loc.nread_last > 0) + c->ctx->out += c->loc.nread_last; + + /* Check whether we should close this connection */ + if ((current_time > c->expire_time) || + (c->rem.flags & FLAG_CLOSED) || + ((c->loc.flags & FLAG_CLOSED) && !io_data_len(&c->loc.io))) + disconnect(&c->link); +} + /* * One iteration of server loop. This is the core of the data exchange. */ @@ -944,7 +1047,7 @@ shttpd_poll(struct shttpd_ctx *ctx, int milliseconds) FD_ZERO(&write_set); /* Add listening sockets to the read set */ - LL_FOREACH(&listeners, lp) { + LL_FOREACH(&ctx->listeners, lp) { l = LL_ENTRY(lp, struct listener, link); FD_SET(l->sock, &read_set); if (l->sock > max_fd) @@ -995,7 +1098,7 @@ shttpd_poll(struct shttpd_ctx *ctx, int milliseconds) } tv.tv_sec = msec / 1000; - tv.tv_usec = msec % 1000; + tv.tv_usec = (msec % 1000) * 1000; /* Check IO readiness */ if (select(max_fd + 1, &read_set, &write_set, NULL, &tv) < 0) { @@ -1013,7 +1116,7 @@ shttpd_poll(struct shttpd_ctx *ctx, int milliseconds) } /* Check for incoming connections on listener sockets */ - LL_FOREACH(&listeners, lp) { + LL_FOREACH(&ctx->listeners, lp) { l = LL_ENTRY(lp, struct listener, link); if (!FD_ISSET(l->sock, &read_set)) continue; @@ -1021,15 +1124,27 @@ shttpd_poll(struct shttpd_ctx *ctx, int milliseconds) sa.len = sizeof(sa.u.sin); if ((sock = accept(l->sock, &sa.u.sa, &sa.len)) != -1) { #if defined(_WIN32) - shttpd_add_socket(ctx, sock); -#else - if (sock < (int) FD_SETSIZE) { - shttpd_add_socket(ctx, sock); + if (!is_allowed(ctx, &sa)) { + elog(E_LOG, NULL, "shttpd_poll: %s " + "is not allowed to connect", + inet_ntoa(sa.u.sin.sin_addr)); + (void) closesocket(sock); } else { + shttpd_add_socket(ctx, sock, l->is_ssl); + } +#else + if (sock >= (int) FD_SETSIZE) { elog(E_LOG, NULL, "shttpd_poll: ctx %p: disarding " "socket %d, too busy", ctx, sock); (void) closesocket(sock); + } else if (!is_allowed(ctx, &sa)) { + elog(E_LOG, NULL, "shttpd_poll: %s " + "is not allowed to connect", + inet_ntoa(sa.u.sin.sin_addr)); + (void) closesocket(sock); + } else { + shttpd_add_socket(ctx, sock, l->is_ssl); } #endif /* _WIN32 */ } @@ -1039,127 +1154,74 @@ shttpd_poll(struct shttpd_ctx *ctx, int milliseconds) /* Process all connections */ LL_FOREACH_SAFE(&ctx->connections, lp, tmp) { c = LL_ENTRY(lp, struct conn, link); - - /* Read from remote end if it is ready */ - if (FD_ISSET(c->rem.chan.fd, &read_set) && - io_space_len(&c->rem.io)) - read_stream(&c->rem); - - /* If the request is not parsed yet, do so */ - if (!(c->rem.flags & FLAG_HEADERS_PARSED)) - parse_http_request(c); - - DBG(("loc: %u [%.*s]", io_data_len(&c->loc.io), - io_data_len(&c->loc.io), io_data(&c->loc.io))); - DBG(("rem: %u [%.*s]", io_data_len(&c->rem.io), - io_data_len(&c->rem.io), io_data(&c->rem.io))); - - /* Read from the local end if it is ready */ - if (io_space_len(&c->loc.io) && + process_connection(c, FD_ISSET(c->rem.chan.fd, &read_set), ((c->loc.flags & FLAG_ALWAYS_READY) - #if !defined(NO_CGI) - ||(c->loc.io_class == &io_cgi && + || (c->loc.io_class == &io_cgi && FD_ISSET(c->loc.chan.fd, &read_set)) #endif /* NO_CGI */ - )) - read_stream(&c->loc); - - if (io_data_len(&c->rem.io) > 0 && (c->loc.flags & FLAG_W) && - c->loc.io_class != NULL && c->loc.io_class->write != NULL) - write_stream(&c->rem, &c->loc); - - if (io_data_len(&c->loc.io) > 0 && c->rem.io_class != NULL) - write_stream(&c->loc, &c->rem); - - if (c->rem.nread_last > 0) - c->ctx->in += c->rem.nread_last; - if (c->loc.nread_last > 0) - c->ctx->out += c->loc.nread_last; - - /* Check whether we should close this connection */ - if ((current_time > c->expire_time) || - (c->rem.flags & FLAG_CLOSED) || - ((c->loc.flags & FLAG_CLOSED) && !io_data_len(&c->loc.io))) - disconnect(c); + )); } } -/* - * Deallocate shttpd object, free up the resources - */ void -shttpd_fini(struct shttpd_ctx *ctx) +free_list(struct llhead *head, void (*dtor)(struct llhead *)) { - struct llhead *lp, *tmp; - struct mime_type_link *mtl; - struct conn *c; - struct listener *l; - struct registered_uri *ruri; + struct llhead *lp, *tmp; - /* Free configured mime types */ - LL_FOREACH_SAFE(&ctx->mime_types, lp, tmp) { - mtl = LL_ENTRY(lp, struct mime_type_link, link); - free(mtl->mime); - free(mtl->ext); - free(mtl); + LL_FOREACH_SAFE(head, lp, tmp) { + LL_DEL(lp); + dtor(lp); } +} - /* Free all connections */ - LL_FOREACH_SAFE(&ctx->connections, lp, tmp) { - c = LL_ENTRY(lp, struct conn, link); - disconnect(c); - } +void +listener_destructor(struct llhead *lp) +{ + struct listener *listener = LL_ENTRY(lp, struct listener, link); - /* Free registered URIs (must be done after disconnect()) */ - LL_FOREACH_SAFE(&ctx->registered_uris, lp, tmp) { - ruri = LL_ENTRY(lp, struct registered_uri, link); - free((void *)ruri->uri); - free(ruri); - } + (void) closesocket(listener->sock); + free(listener); +} - /* Free listener sockets for this context */ - LL_FOREACH_SAFE(&listeners, lp, tmp) { - l = LL_ENTRY(lp, struct listener, link); - (void) closesocket(l->sock); - LL_DEL(&l->link); - free(l); - } +void +registered_uri_destructor(struct llhead *lp) +{ + struct registered_uri *ruri = LL_ENTRY(lp, struct registered_uri, link); -#if !defined(NO_SSI) - free_ssi_funcs(ctx); -#endif /* NO_SSI */ + free((void *) ruri->uri); + free(ruri); +} + +static void +acl_destructor(struct llhead *lp) +{ + struct acl *acl = LL_ENTRY(lp, struct acl, link); + free(acl); +} + +/* + * Deallocate shttpd object, free up the resources + */ +void +shttpd_fini(struct shttpd_ctx *ctx) +{ + size_t i; + + free_list(&ctx->connections, disconnect); + free_list(&ctx->registered_uris, registered_uri_destructor); + free_list(&ctx->acl, acl_destructor); + free_list(&ctx->listeners, listener_destructor); + free_list(&ctx->ssi_funcs, ssi_func_destructor); + + for (i = 0; i < NELEMS(ctx->options); i++) + if (ctx->options[i] != NULL) + free(ctx->options[i]); if (ctx->access_log) (void) fclose(ctx->access_log); if (ctx->error_log) (void) fclose(ctx->error_log); - if (ctx->put_auth_file) free(ctx->put_auth_file); - if (ctx->document_root) free(ctx->document_root); - if (ctx->index_files) free(ctx->index_files); - if (ctx->aliases) free(ctx->aliases); -#if !defined(NO_CGI) - if (ctx->cgi_vars) free(ctx->cgi_vars); - if (ctx->cgi_extensions) free(ctx->cgi_extensions); - if (ctx->cgi_interpreter) free(ctx->cgi_interpreter); -#endif /* NO_CGI */ - if (ctx->auth_realm) free(ctx->auth_realm); - if (ctx->global_passwd_file) free(ctx->global_passwd_file); - if (ctx->uid) free(ctx->uid); /* TODO: free SSL context */ free(ctx); } - -void -open_listening_ports(struct shttpd_ctx *ctx) -{ - const char *p = ctx->ports; - int len, is_ssl; - - FOR_EACH_WORD_IN_LIST(p, len) { - is_ssl = p[len - 1] == 's' ? 1 : 0; - if (shttpd_listen(ctx, atoi(p), is_ssl) == -1) - elog(E_FATAL, NULL, - "Cannot open socket on port %d", atoi(p)); - } -} diff --git a/libs/shttpd/shttpd.h b/libs/shttpd/shttpd.h index ba349e8..0d8d510 100644 --- a/libs/shttpd/shttpd.h +++ b/libs/shttpd/shttpd.h @@ -19,7 +19,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * - * $Id: shttpd.h,v 1.3 2007/04/11 13:01:53 drozd Exp $ + * $Id: shttpd.h,v 1.9 2008/02/13 20:44:32 drozd Exp $ */ #ifndef SHTTPD_HEADER_INCLUDED @@ -63,23 +63,20 @@ struct shttpd_arg { * 4. for POST requests, it must process the incoming data (in.buf) of length * 'in.len', and set 'in.num_bytes', which is how many bytes of POST * data is read and can be discarded by SHTTPD. + * 5. If callback allocates arg->state, to keep state, it must deallocate it + * at the end of coonection SHTTPD_CONNECTION_ERROR or SHTTPD_END_OF_OUTPUT */ typedef void (*shttpd_callback_t)(struct shttpd_arg *); /* - * shttpd_init Initialize shttpd context. Parameters: configuration - * file name (may be NULL), then NULL-terminated - * sequence of pairs "option_name", "option_value". - * shttpd_init2 Same as shttpd_init, but the list of option/value - * pairs is passed in arrays + * shttpd_init Initialize shttpd context. + * shttpd_set_option Set new value for option. * shttpd_fini Dealocate the context * shttpd_register_uri Setup the callback function for specified URL. - * shttpd_protect_uri Associate authorization file with an URL. - * shttpd_add_mime_type Add mime type * shtppd_listen Setup a listening socket in the SHTTPD context * shttpd_poll Do connections processing * shttpd_version return string with SHTTPD version - * shttpd_get_var Return POST/GET variable value for given variable name. + * shttpd_get_var Fetch POST/GET variable value by name. Return value len * shttpd_get_header return value of the specified HTTP header * shttpd_get_env return string values for the following * pseudo-variables: "REQUEST_METHOD", "REQUEST_URI", @@ -88,30 +85,25 @@ typedef void (*shttpd_callback_t)(struct shttpd_arg *); struct shttpd_ctx; -struct shttpd_ctx *shttpd_init(const char *config_file, ...); -struct shttpd_ctx *shttpd_init2(const char *config_file, - char *names[], char *values[], size_t num_options); +struct shttpd_ctx *shttpd_init(void); +void shttpd_set_option(struct shttpd_ctx *, const char *opt, const char *val); void shttpd_fini(struct shttpd_ctx *); -void shttpd_add_mime_type(struct shttpd_ctx *, - const char *ext, const char *mime); int shttpd_listen(struct shttpd_ctx *ctx, int port, int is_ssl); -void shttpd_register_uri(struct shttpd_ctx *ctx, - const char *uri, shttpd_callback_t callback, void *user_data); -void shttpd_protect_uri(struct shttpd_ctx *ctx, - const char *uri, const char *file); +void shttpd_register_uri(struct shttpd_ctx *ctx, const char *uri, + shttpd_callback_t callback, void *const user_data); void shttpd_poll(struct shttpd_ctx *, int milliseconds); const char *shttpd_version(void); int shttpd_get_var(const char *var, const char *buf, int buf_len, char *value, int value_len); -const char *shttpd_get_header(struct shttpd_arg *, const char *); -const char *shttpd_get_env(struct shttpd_arg *, const char *); +const char *shttpd_get_header(struct shttpd_arg *, const char *header_name); +const char *shttpd_get_env(struct shttpd_arg *, const char *name); void shttpd_get_http_version(struct shttpd_arg *, unsigned long *major, unsigned long *minor); size_t shttpd_printf(struct shttpd_arg *, const char *fmt, ...); void shttpd_handle_error(struct shttpd_ctx *ctx, int status, - shttpd_callback_t func, void *data); + shttpd_callback_t func, void *const data); void shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name, - shttpd_callback_t func, void *user_data); + shttpd_callback_t func, void *const user_data); /* * The following three functions are for applications that need to @@ -121,7 +113,7 @@ void shttpd_register_ssi_func(struct shttpd_ctx *ctx, const char *name, * pool for the idle thread by means of shttpd_active(), and add new * connection to the context by means of shttpd_add(). */ -void shttpd_add_socket(struct shttpd_ctx *, int sock); +void shttpd_add_socket(struct shttpd_ctx *, int sock, int is_ssl); int shttpd_accept(int lsn_sock, int milliseconds); int shttpd_active(struct shttpd_ctx *); diff --git a/libs/shttpd/standalone.c b/libs/shttpd/standalone.c deleted file mode 100644 index 41b361e..0000000 --- a/libs/shttpd/standalone.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2004-2005 Sergey Lyubka - * All rights reserved - * - * "THE BEER-WARE LICENSE" (Revision 42): - * Sergey Lyubka wrote this file. As long as you retain this notice you - * can do whatever you want with this stuff. If we meet some day, and you think - * this stuff is worth it, you can buy me a beer in return. - */ - -#include "defs.h" - -static const char *config_file = CONFIG; -static int exit_flag; - -static void -signal_handler(int sig_num) -{ - switch (sig_num) { -#ifndef _WIN32 - case SIGCHLD: - while (waitpid(-1, &sig_num, WNOHANG) > 0) ; - break; -#endif /* !_WIN32 */ - default: - exit_flag = sig_num; - break; - } -} - -int -main(int argc, char *argv[]) -{ - struct shttpd_ctx *ctx; - - current_time = time(NULL); - if (argc > 1 && argv[argc - 2][0] != '-' && argv[argc - 1][0] != '-') - config_file = argv[argc - 1]; - -#if !defined(NO_AUTH) - if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') { - if (argc != 6) - usage(argv[0]); - exit(edit_passwords(argv[2],argv[3],argv[4],argv[5])); - } -#endif /* NO_AUTH */ - - ctx = init_from_argc_argv(config_file, argc, argv); - -#ifndef _WIN32 - /* Switch to alternate UID, it is safe now, after shttpd_listen() */ - if (ctx->uid != NULL) { - struct passwd *pw; - - if ((pw = getpwnam(ctx->uid)) == NULL) - elog(E_FATAL, 0, "main: unknown user [%s]", ctx->uid); - else if (setgid(pw->pw_gid) == -1) - elog(E_FATAL, NULL, "main: setgid(%s): %s", - ctx->uid, strerror(errno)); - else if (setuid(pw->pw_uid) == -1) - elog(E_FATAL, NULL, "main: setuid(%s): %s", - ctx->uid, strerror(errno)); - } - (void) signal(SIGCHLD, signal_handler); - (void) signal(SIGPIPE, SIG_IGN); -#endif /* _WIN32 */ - - (void) signal(SIGTERM, signal_handler); - (void) signal(SIGINT, signal_handler); - - if (ctx->inetd_mode) { - (void) freopen("/dev/null", "a", stderr); - shttpd_add_socket(ctx, fileno(stdin)); - } else { - open_listening_ports(ctx); - } - - elog(E_LOG, NULL, "shttpd %s started on port(s) %s, serving %s", - VERSION, ctx->ports, ctx->document_root); - - while (exit_flag == 0) - shttpd_poll(ctx, 5000); - - elog(E_LOG, NULL, "%d requests %.2lf Mb in %.2lf Mb out. " - "Exit on signal %d", ctx->nrequests, (double) (ctx->in / 1048576), - (double) ctx->out / 1048576, exit_flag); - - shttpd_fini(ctx); - - return (EXIT_SUCCESS); -} diff --git a/libs/shttpd/string.c b/libs/shttpd/string.c index 44f0094..fbcb638 100644 --- a/libs/shttpd/string.c +++ b/libs/shttpd/string.c @@ -87,9 +87,9 @@ match_extension(const char *path, const char *ext_list) path_len = strlen(path); FOR_EACH_WORD_IN_LIST(ext_list, len) - if (len < path_len && + if (len < path_len && path[path_len - len - 1] == '.' && !my_strncasecmp(path + path_len - len, ext_list, len)) - return (1); + return (TRUE); - return (0); + return (FALSE); } diff --git a/libs/sqlite3/sqlite3.c b/libs/sqlite3/sqlite3.c index bf9ddaa..c846057 100644 --- a/libs/sqlite3/sqlite3.c +++ b/libs/sqlite3/sqlite3.c @@ -1,6 +1,6 @@ /****************************************************************************** ** This file is an amalgamation of many separate C source files from SQLite -** version 3.5.4. By combining all the individual C code files into this +** version 3.5.7. By combining all the individual C code files into this ** single large file, the entire code can be compiled as a one translation ** unit. This allows many compilers to do optimizations that would not be ** possible if the files were compiled separately. Performance improvements @@ -11,14 +11,15 @@ ** programs, you need this file and the "sqlite3.h" header file that defines ** the programming interface to the SQLite library. (If you do not have ** the "sqlite3.h" header file at hand, you will find a copy in the first -** 3840 lines past this header comment.) Additional code files may be +** 5413 lines past this header comment.) Additional code files may be ** needed if you want a wrapper to interface SQLite with your choice of ** programming language. The code for the "sqlite3" command-line shell ** is also in a separate file. This file contains only code for the core ** SQLite library. ** -** This amalgamation was generated on 2007-12-14 17:40:56 UTC. +** This amalgamation was generated on 2008-03-17 18:47:08 UTC. */ +#define SQLITE_CORE 1 #define SQLITE_AMALGAMATION 1 #ifndef SQLITE_PRIVATE # define SQLITE_PRIVATE static @@ -40,18 +41,75 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.626 2007/12/13 03:45:08 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.673 2008/03/14 13:02:08 mlcreech Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ /* +** Include the configuration header output by 'configure' if it was run +** (otherwise we get an empty default). +*/ +/************** Include config.h in the middle of sqliteInt.h ****************/ +/************** Begin file config.h ******************************************/ +/* +** 2008 March 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Default configuration header in case the 'configure' script is not used +** +** @(#) $Id: config.h,v 1.1 2008/03/06 07:36:18 mlcreech Exp $ +*/ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +/* We do nothing here, since no assumptions are made by default */ + +#endif + +/************** End of config.h **********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* Needed for various definitions... */ +#define _GNU_SOURCE + +/* +** Include standard header files as necessary +*/ +#ifdef HAVE_STDINT_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif + +/* +** If possible, use the C99 intptr_t type to define an integral type of +** equivalent size to a pointer. (Technically it's >= sizeof(void *), but +** practically it's == sizeof(void *)). We fall back to an int if this type +** isn't defined. +*/ +#ifdef HAVE_INTPTR_T + typedef intptr_t sqlite3_intptr_t; +#else + typedef int sqlite3_intptr_t; +#endif + + +/* ** The macro unlikely() is a hint that surrounds a boolean ** expression that is usually false. Macro likely() surrounds ** a boolean expression that is usually true. GCC is able to ** use these hints to generate better code, sometimes. */ -#if defined(__GNUC__) +#if defined(__GNUC__) && 0 # define likely(X) __builtin_expect((X),1) # define unlikely(X) __builtin_expect((X),0) #else @@ -104,7 +162,7 @@ ** ** This file defines various limits of what SQLite can process. ** -** @(#) $Id: sqliteLimit.h,v 1.5 2007/12/13 21:54:11 drh Exp $ +** @(#) $Id: sqliteLimit.h,v 1.6 2007/12/17 16:20:07 drh Exp $ */ /* @@ -141,10 +199,10 @@ /* ** The maximum length of a single SQL statement in bytes. -** The hard limit is 1 million. +** A value of zero means there is no limit. */ #ifndef SQLITE_MAX_SQL_LENGTH -# define SQLITE_MAX_SQL_LENGTH 1000000 +# define SQLITE_MAX_SQL_LENGTH 0 #endif /* @@ -330,6 +388,40 @@ #endif /* +** Exactly one of the following macros must be defined in order to +** specify which memory allocation subsystem to use. +** +** SQLITE_SYSTEM_MALLOC // Use normal system malloc() +** SQLITE_MEMDEBUG // Debugging version of system malloc() +** SQLITE_MEMORY_SIZE // internal allocator #1 +** SQLITE_MMAP_HEAP_SIZE // internal mmap() allocator +** SQLITE_POW2_MEMORY_SIZE // internal power-of-two allocator +** +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as +** the default. +*/ +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)+\ + defined(SQLITE_MEMORY_SIZE)+defined(SQLITE_MMAP_HEAP_SIZE)+\ + defined(SQLITE_POW2_MEMORY_SIZE)>1 +# error "At most one of the following compile-time configuration options\ + is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG, SQLITE_MEMORY_SIZE,\ + SQLITE_MMAP_HEAP_SIZE, SQLITE_POW2_MEMORY_SIZE" +#endif +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)+\ + defined(SQLITE_MEMORY_SIZE)+defined(SQLITE_MMAP_HEAP_SIZE)+\ + defined(SQLITE_POW2_MEMORY_SIZE)==0 +# define SQLITE_SYSTEM_MALLOC 1 +#endif + +/* +** If SQLITE_MALLOC_SOFT_LIMIT is defined, then try to keep the +** sizes of memory allocations below this value where possible. +*/ +#if defined(SQLITE_POW2_MEMORY_SIZE) && !defined(SQLITE_MALLOC_SOFT_LIMIT) +# define SQLITE_MALLOC_SOFT_LIMIT 1024 +#endif + +/* ** We need to define _XOPEN_SOURCE as follows in order to enable ** recursive mutexes on most unix systems. But Mac OS X is different. ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told, @@ -342,7 +434,7 @@ ** ** See also ticket #2741. */ -#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && SQLITE_THREADSAFE +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE # define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */ #endif @@ -395,7 +487,7 @@ ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. ** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ +** @(#) $Id: sqlite.h.in,v 1.291 2008/03/08 12:37:31 drh Exp $ */ #ifndef _SQLITE3_H_ #define _SQLITE3_H_ @@ -430,79 +522,97 @@ extern "C" { /* ** CAPI3REF: Compile-Time Library Version Numbers {F10010} ** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when +** The SQLITE_VERSION and SQLITE_VERSION_NUMBER #defines in +** the sqlite3.h file specify the version of SQLite with which +** that header file is associated. +** +** The "version" of SQLite is a string of the form "X.Y.Z". +** The phrase "alpha" or "beta" might be appended after the Z. +** The X value is major version number always 3 in SQLite3. +** The X value only changes when backwards compatibility is +** broken and we intend to never break +** backwards compatibility. The Y value is the minor version +** number and only changes when ** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with +** but not backwards compatible. The Z value is release number +** and is incremented with ** each release but resets back to 0 when Y is incremented. ** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. +** +** INVARIANTS: +** +** {F10011} The SQLITE_VERSION #define in the sqlite3.h header file +** evaluates to a string literal that is the SQLite version +** with which the header file is associated. +** +** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer +** with the value (X*1000000 + Y*1000 + Z) where X, Y, and +** Z are the major version, minor version, and release number. */ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 +#define SQLITE_VERSION "3.5.7" +#define SQLITE_VERSION_NUMBER 3005007 /* ** CAPI3REF: Run-Time Library Version Numbers {F10020} +** KEYWORDS: sqlite3_version ** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might +** These features provide the same information as the [SQLITE_VERSION] +** and [SQLITE_VERSION_NUMBER] #defines in the header, but are associated +** with the library instead of the header file. Cautious programmers might ** include a check in their application to verify that ** sqlite3_libversion_number() always returns the value ** [SQLITE_VERSION_NUMBER]. ** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not +** The sqlite3_libversion() function returns the same information as is +** in the sqlite3_version[] string constant. The function is provided +** for use in DLLs since DLL users usually do not have direct access to string ** constants within the DLL. +** +** INVARIANTS: +** +** {F10021} The [sqlite3_libversion_number()] interface returns an integer +** equal to [SQLITE_VERSION_NUMBER]. +** +** {F10022} The [sqlite3_version] string constant contains the text of the +** [SQLITE_VERSION] string. +** +** {F10023} The [sqlite3_libversion()] function returns +** a pointer to the [sqlite3_version] string constant. */ -SQLITE_EXTERN const char sqlite3_version[]; +SQLITE_API const char sqlite3_version[]; SQLITE_API const char *sqlite3_libversion(void); SQLITE_API int sqlite3_libversion_number(void); /* ** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} ** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. +** SQLite can be compiled with or without mutexes. When +** the SQLITE_THREADSAFE C preprocessor macro is true, mutexes +** are enabled and SQLite is threadsafe. When that macro is false, +** the mutexes are omitted. Without the mutexes, it is not safe +** to use SQLite from more than one thread. +** +** There is a measurable performance penalty for enabling mutexes. +** So if speed is of utmost importance, it makes sense to disable +** the mutexes. But for maximum safety, mutexes should be enabled. +** The default behavior is for mutexes to be enabled. +** +** This interface can be used by a program to make sure that the +** version of SQLite that it is linking against was compiled with +** the desired setting of the SQLITE_THREADSAFE macro. +** +** INVARIANTS: +** +** {F10101} The [sqlite3_threadsafe()] function returns nonzero if +** SQLite was compiled with its mutexes enabled or zero +** if SQLite was compiled with mutexes disabled. */ SQLITE_API int sqlite3_threadsafe(void); /* ** CAPI3REF: Database Connection Handle {F12000} +** KEYWORDS: {database connection} ** ** Each open SQLite database is represented by pointer to an instance of the ** opaque structure named "sqlite3". It is useful to think of an sqlite3 @@ -518,16 +628,22 @@ typedef struct sqlite3 sqlite3; /* ** CAPI3REF: 64-Bit Integer Types {F10200} +** KEYWORDS: sqlite_int64 sqlite_uint64 ** -** Because there is no cross-platform way to specify such types +** Because there is no cross-platform way to specify 64-bit integer types ** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} ** ** The sqlite3_int64 and sqlite3_uint64 are the preferred type ** definitions. The sqlite_int64 and sqlite_uint64 types are ** supported for backwards compatibility only. +** +** INVARIANTS: +** +** {F10201} The [sqlite_int64] and [sqlite3_int64] types specify a +** 64-bit signed integer. +** +** {F10202} The [sqlite_uint64] and [sqlite3_uint64] types specify +** a 64-bit unsigned integer. */ #ifdef SQLITE_INT64_TYPE typedef SQLITE_INT64_TYPE sqlite_int64; @@ -553,22 +669,41 @@ typedef sqlite_uint64 sqlite3_uint64; /* ** CAPI3REF: Closing A Database Connection {F12010} ** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. +** This routine is the destructor for the [sqlite3] object. +** +** Applications should [sqlite3_finalize | finalize] all +** [prepared statements] and +** [sqlite3_blob_close | close] all [sqlite3_blob | BLOBs] +** associated with the [sqlite3] object prior +** to attempting to close the [sqlite3] object. +** +** What happens to pending transactions? Are they +** rolled back, or abandoned? +** +** INVARIANTS: +** +** {F12011} The [sqlite3_close()] interface destroys an [sqlite3] object +** allocated by a prior call to [sqlite3_open()], +** [sqlite3_open16()], or [sqlite3_open_v2()]. +** +** {F12012} The [sqlite3_close()] function releases all memory used by the +** connection and closes all open files. ** ** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} +** [prepared statements] that have not been +** finalized by [sqlite3_finalize()], then [sqlite3_close()] +** returns [SQLITE_BUSY] and leaves the connection open. +** +** {F12014} Giving sqlite3_close() a NULL pointer is a harmless no-op. +** +** LIMITATIONS: ** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. +** {U12015} The parameter to [sqlite3_close()] must be an [sqlite3] object +** pointer previously obtained from [sqlite3_open()] or the +** equivalent, or NULL. +** +** {U12016} The parameter to [sqlite3_close()] must not have been previously +** closed. */ SQLITE_API int sqlite3_close(sqlite3 *); @@ -582,60 +717,97 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); /* ** CAPI3REF: One-Step Query Execution Interface {F12100} ** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} +** The sqlite3_exec() interface is a convenient way of running +** one or more SQL statements without a lot of C code. The +** SQL statements are passed in as the second parameter to +** sqlite3_exec(). The statements are evaluated one by one +** until either an error or an interrupt is encountered or +** until they are all done. The 3rd parameter is an optional +** callback that is invoked once for each row of any query results +** produced by the SQL statements. The 5th parameter tells where +** to write any error messages. +** +** The sqlite3_exec() interface is implemented in terms of +** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()]. +** The sqlite3_exec() routine does nothing that cannot be done +** by [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()]. +** The sqlite3_exec() is just a convenient wrapper. +** +** INVARIANTS: +** +** {F12101} The [sqlite3_exec()] interface evaluates zero or more UTF-8 +** encoded, semicolon-separated, SQL statements in the +** zero-terminated string of its 2nd parameter within the +** context of the [sqlite3] object given in the 1st parameter. +** +** {F12104} The return value of [sqlite3_exec()] is SQLITE_OK if all +** SQL statements run successfully. +** +** {F12105} The return value of [sqlite3_exec()] is an appropriate +** non-zero error code if any SQL statement fails. +** +** {F12107} If one or more of the SQL statements handed to [sqlite3_exec()] +** return results and the 3rd parameter is not NULL, then +** the callback function specified by the 3rd parameter is +** invoked once for each row of result. +** +** {F12110} If the callback returns a non-zero value then [sqlite3_exec()] +** will aborted the SQL statement it is currently evaluating, +** skip all subsequent SQL statements, and return [SQLITE_ABORT]. +** What happens to *errmsg here? Does the result code for +** sqlite3_errcode() get set? +** +** {F12113} The [sqlite3_exec()] routine will pass its 4th parameter through +** as the 1st parameter of the callback. +** +** {F12116} The [sqlite3_exec()] routine sets the 2nd parameter of its +** callback to be the number of columns in the current row of +** result. +** +** {F12119} The [sqlite3_exec()] routine sets the 3rd parameter of its +** callback to be an array of pointers to strings holding the +** values for each column in the current result set row as +** obtained from [sqlite3_column_text()]. +** +** {F12122} The [sqlite3_exec()] routine sets the 4th parameter of its +** callback to be an array of pointers to strings holding the +** names of result columns as obtained from [sqlite3_column_name()]. +** +** {F12125} If the 3rd parameter to [sqlite3_exec()] is NULL then +** [sqlite3_exec()] never invokes a callback. All query +** results are silently discarded. +** +** {F12128} If an error occurs while parsing or evaluating any of the SQL +** statements handed to [sqlite3_exec()] then [sqlite3_exec()] will +** return an [error code] other than [SQLITE_OK]. +** +** {F12131} If an error occurs while parsing or evaluating any of the SQL +** handed to [sqlite3_exec()] and if the 5th parameter (errmsg) +** to [sqlite3_exec()] is not NULL, then an error message is +** allocated using the equivalent of [sqlite3_mprintf()] and +** *errmsg is made to point to that message. +** +** {F12134} The [sqlite3_exec()] routine does not change the value of +** *errmsg if errmsg is NULL or if there are no errors. +** +** {F12137} The [sqlite3_exec()] function sets the error code and message +** accessible via [sqlite3_errcode()], [sqlite3_errmsg()], and +** [sqlite3_errmsg16()]. +** +** LIMITATIONS: +** +** {U12141} The first parameter to [sqlite3_exec()] must be an valid and open +** [database connection]. +** +** {U12142} The database connection must not be closed while +** [sqlite3_exec()] is running. +** +** {U12143} The calling function is should use [sqlite3_free()] to free +** the memory that *errmsg is left pointing at once the error +** message is no longer needed. +** +** {U12145} The SQL statement text in the 2nd parameter to [sqlite3_exec()] +** must remain unchanged while [sqlite3_exec()] is running. */ SQLITE_API int sqlite3_exec( sqlite3*, /* An open database */ @@ -647,18 +819,12 @@ SQLITE_API int sqlite3_exec( /* ** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK +** KEYWORDS: SQLITE_OK {error code} {error codes} ** ** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} +** here in order to indicates success or failure. ** ** See also: [SQLITE_IOERR_READ | extended result codes] -** */ #define SQLITE_OK 0 /* Successful result */ /* beginning-of-error-codes */ @@ -694,6 +860,8 @@ SQLITE_API int sqlite3_exec( /* ** CAPI3REF: Extended Result Codes {F10220} +** KEYWORDS: {extended error code} {extended error codes} +** KEYWORDS: {extended result codes} ** ** In its default configuration, SQLite API routines return one of 26 integer ** [SQLITE_OK | result codes]. However, experience has shown that @@ -701,24 +869,30 @@ SQLITE_API int sqlite3_exec( ** much information about problems as programmers might like. In an effort to ** address this, newer versions of SQLite (version 3.3.8 and later) include ** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled +** about errors. The extended result codes are enabled or disabled ** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} +** API. ** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} +** Some of the available extended result codes are listed here. +** One may expect the number of extended result codes will be expand +** over time. Software that uses extended result codes should expect +** to see new result codes in future releases of SQLite. ** ** The SQLITE_OK result code will never be extended. It will always ** be exactly zero. +** +** INVARIANTS: +** +** {F10223} The symbolic name for an extended result code always contains +** a related primary result code as a prefix. +** +** {F10224} Primary result code names contain a single "_" character. +** +** {F10225} Extended result code names contain two or more "_" characters. +** +** {F10226} The numeric value of an extended result code contains the +** numeric value of its corresponding primary result code in +** its least significant 8 bits. */ #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) @@ -736,9 +910,9 @@ SQLITE_API int sqlite3_exec( /* ** CAPI3REF: Flags For File Open Operations {F10230} ** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the +** These bit values are intended for use in the +** 3rd parameter to the [sqlite3_open_v2()] interface and +** in the 4th parameter to the xOpen method of the ** [sqlite3_vfs] object. */ #define SQLITE_OPEN_READONLY 0x00000001 @@ -757,20 +931,20 @@ SQLITE_API int sqlite3_exec( /* ** CAPI3REF: Device Characteristics {F10240} ** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] +** The xDeviceCapabilities method of the [sqlite3_io_methods] ** object returns an integer which is a vector of the these ** bit values expressing I/O characteristics of the mass storage ** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} +** refers to. ** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values +** The SQLITE_IOCAP_ATOMIC property means that all writes of +** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values ** mean that writes of blocks that are nnn bytes in size and ** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means +** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means ** that when data is appended to a file, the data is appended ** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that +** way around. The SQLITE_IOCAP_SEQUENTIAL property means that ** information is written to disk in the same order as calls ** to xWrite(). */ @@ -789,9 +963,9 @@ SQLITE_API int sqlite3_exec( /* ** CAPI3REF: File Locking Levels {F10250} ** -** {F10251} SQLite uses one of the following integer values as the second +** SQLite uses one of these integer values as the second ** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} +** of an [sqlite3_io_methods] object. */ #define SQLITE_LOCK_NONE 0 #define SQLITE_LOCK_SHARED 1 @@ -802,14 +976,14 @@ SQLITE_API int sqlite3_exec( /* ** CAPI3REF: Synchronization Type Flags {F10260} ** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the +** When SQLite invokes the xSync() method of an +** [sqlite3_io_methods] object it uses a combination of ** these integer values as the second argument. ** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the +** When the SQLITE_SYNC_DATAONLY flag is used, it means that the ** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means +** information need not be flushed. The SQLITE_SYNC_NORMAL flag means +** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means ** to use Mac OS-X style fullsync instead of fsync(). */ #define SQLITE_SYNC_NORMAL 0x00002 @@ -836,7 +1010,7 @@ struct sqlite3_file { ** CAPI3REF: OS Interface File Virtual Methods Object {F11120} ** ** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the +** an instance of this object. This object defines the ** methods used to perform various operations against the open file. ** ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or @@ -933,11 +1107,11 @@ struct sqlite3_io_methods { ** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] ** interface. ** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of +** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This +** opcode causes the xFileControl method to write the current state of ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability +** into an integer that the pArg argument points to. This capability ** is used during testing and only needs to be supported when SQLITE_TEST ** is defined. */ @@ -947,8 +1121,8 @@ struct sqlite3_io_methods { ** CAPI3REF: Mutex Handle {F17110} ** ** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only +** abstract type for a mutex object. The SQLite core never looks +** at the internal representation of an [sqlite3_mutex]. It only ** deals with pointers to the [sqlite3_mutex] object. ** ** Mutexes are created using [sqlite3_mutex_alloc()]. @@ -970,13 +1144,13 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** structure used by this VFS. mxPathname is the maximum length of ** a pathname in this VFS. ** -** Registered vfs modules are kept on a linked list formed by +** Registered sqlite3_vfs objects are kept on a linked list formed by ** the pNext pointer. The [sqlite3_vfs_register()] ** and [sqlite3_vfs_unregister()] interfaces manage this list ** in a thread-safe way. The [sqlite3_vfs_find()] interface ** searches the list. ** -** The pNext field is the only fields in the sqlite3_vfs +** The pNext field is the only field in the sqlite3_vfs ** structure that SQLite will ever modify. SQLite will only access ** or modify this field while holding a particular static mutex. ** The application should never modify anything within the sqlite3_vfs @@ -1014,14 +1188,14 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** ** The file I/O implementation can use the object type flags to ** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. +** that does not care about crash recovery or rollback might make +** the open of a journal file a no-op. Writes to this journal would +** also be no-ops, and any attempt to read the journal would return +** SQLITE_IOERR. Or the implementation might recognize that a database +** file will be doing page-aligned sector reads and writes in a random +** order and set up its I/O subsystem accordingly. ** -** {F11144} SQLite might also add one of the following flags to the xOpen +** SQLite might also add one of the following flags to the xOpen ** method: ** **
    @@ -1036,7 +1210,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** for exclusive access. This flag is set for all files except ** for the main database file. {END} ** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite +** {F11148} At least szOsFile bytes of memory are allocated by SQLite ** to hold the [sqlite3_file] structure passed as the third ** argument to xOpen. {END} The xOpen method does not have to ** allocate the structure; it should just fill it in. @@ -1048,7 +1222,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** to test to see if a file is at least readable. {END} The file can be a ** directory. ** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for +** {F11150} SQLite will always allocate at least mxPathname+1 bytes for ** the output buffers for xGetTempname and xFullPathname. {F11151} The exact ** size of the output buffer is also passed as a parameter to both ** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN @@ -1062,7 +1236,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** The xRandomness() function attempts to return nBytes bytes ** of good-quality randomness into zOut. The return value is ** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at +** xSleep() method causes the calling thread to sleep for at ** least the number of microseconds given. The xCurrentTime() ** method returns a Julian Day Number for the current date and ** time. @@ -1097,7 +1271,7 @@ struct sqlite3_vfs { ** ** {F11191} These integer constants can be used as the third parameter to ** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is +** what kind of permissions the xAccess method is ** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method ** simply checks to see if the file exists. {F11193} With ** SQLITE_ACCESS_READWRITE, the xAccess method checks to see @@ -1112,149 +1286,226 @@ struct sqlite3_vfs { /* ** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} ** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. +** The sqlite3_extended_result_codes() routine enables or disables the +** [SQLITE_IOERR_READ | extended result codes] feature of SQLite. +** The extended result codes are disabled by default for historical +** compatibility. ** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. +** INVARIANTS: +** +** {F12201} Each new [database connection] has the +** [extended result codes] feature +** disabled by default. +** +** {F12202} The [sqlite3_extended_result_codes(D,F)] interface will enable +** [extended result codes] for the +** [database connection] D if the F parameter +** is true, or disable them if F is false. */ SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff); /* ** CAPI3REF: Last Insert Rowid {F12220} ** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available +** Each entry in an SQLite table has a unique 64-bit signed +** integer key called the "rowid". The rowid is always available ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If +** names are not also used by explicitly declared columns. If ** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. +** is another alias for the rowid. ** -** {F12224} This routine returns the rowid of the most recent +** This routine returns the rowid of the most recent ** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts +** shown in the first argument. If no successful inserts ** have ever occurred on this database connection, zero is returned. ** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the +** If an INSERT occurs within a trigger, then the rowid of the ** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned +** is running. But once the trigger terminates, the value returned ** by this routine reverts to the last value inserted before the ** trigger fired. ** -** {F12228} An INSERT that fails due to a constraint violation is not a +** An INSERT that fails due to a constraint violation is not a ** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, +** routine. Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, ** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE +** routine when their insertion fails. When INSERT OR REPLACE ** encounters a constraint violation, it does not fail. The ** INSERT continues to completion after deleting rows that caused ** the constraint problem so INSERT OR REPLACE will always change ** the return value of this interface. ** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. +** For the purposes of this routine, an insert is considered to +** be successful even if it is subsequently rolled back. +** +** INVARIANTS: +** +** {F12221} The [sqlite3_last_insert_rowid()] function returns the +** rowid of the most recent successful insert done +** on the same database connection and within the same +** trigger context, or zero if there have +** been no qualifying inserts on that connection. +** +** {F12223} The [sqlite3_last_insert_rowid()] function returns +** same value when called from the same trigger context +** immediately before and after a ROLLBACK. +** +** LIMITATIONS: +** +** {U12232} If a separate thread does a new insert on the same +** database connection while the [sqlite3_last_insert_rowid()] +** function is running and thus changes the last insert rowid, +** then the value returned by [sqlite3_last_insert_rowid()] is +** unpredictable and might not equal either the old or the new +** last insert rowid. */ SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); /* ** CAPI3REF: Count The Number Of Rows Modified {F12240} ** -** {F12241} This function returns the number of database rows that were changed +** This function returns the number of database rows that were changed ** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only +** on the connection specified by the first parameter. Only ** changes that are directly specified by the INSERT, UPDATE, or ** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function +** triggers are not counted. Use the [sqlite3_total_changes()] function ** to find the total number of changes including changes caused by triggers. ** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface +** A "row change" is a change to a single row of a single table +** caused by an INSERT, DELETE, or UPDATE statement. Rows that +** are changed as side effects of REPLACE constraint resolution, +** rollback, ABORT processing, DROP TABLE, or by any other +** mechanisms do not count as direct row changes. +** +** A "trigger context" is a scope of execution that begins and +** ends with the script of a trigger. Most SQL statements are +** evaluated outside of any trigger. This is the "top level" +** trigger context. If a trigger fires from the top level, a +** new trigger context is entered for the duration of that one +** trigger. Subtriggers create subcontexts for their duration. +** +** Calling [sqlite3_exec()] or [sqlite3_step()] recursively does +** not create a new trigger context. +** +** This function returns the number of direct row changes in the +** most recent INSERT, UPDATE, or DELETE statement within the same +** trigger context. +** +** So when called from the top level, this function returns the +** number of changes in the most recent INSERT, UPDATE, or DELETE +** that also occurred at the top level. +** Within the body of a trigger, the sqlite3_changes() interface ** can be called to find the number of ** changes in the most recently completed INSERT, UPDATE, or DELETE ** statement within the body of the same trigger. +** However, the number returned does not include in changes +** caused by subtriggers since they have their own context. ** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without +** SQLite implements the command "DELETE FROM table" without ** a WHERE clause by dropping and recreating the table. (This is much ** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use +** table.) Because of this optimization, the deletions in +** "DELETE FROM table" are not row changes and will not be counted +** by the sqlite3_changes() or [sqlite3_total_changes()] functions. +** To get an accurate count of the number of rows deleted, use ** "DELETE FROM table WHERE 1" instead. ** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. +** INVARIANTS: +** +** {F12241} The [sqlite3_changes()] function returns the number of +** row changes caused by the most recent INSERT, UPDATE, +** or DELETE statement on the same database connection and +** within the same trigger context, or zero if there have +** not been any qualifying row changes. +** +** LIMITATIONS: +** +** {U12252} If a separate thread makes changes on the same database connection +** while [sqlite3_changes()] is running then the value returned +** is unpredictable and unmeaningful. */ SQLITE_API int sqlite3_changes(sqlite3*); /* ** CAPI3REF: Total Number Of Rows Modified {F12260} *** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes +** This function returns the number of row changes caused +** by INSERT, UPDATE or DELETE statements since the database handle +** was opened. The count includes all changes from all trigger +** contexts. But the count does not include changes used to +** implement REPLACE constraints, do rollbacks or ABORT processing, +** or DROP table processing. +** The changes ** are counted as soon as the statement that makes them is completed ** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} +** [sqlite3_finalize()]). ** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without +** SQLite implements the command "DELETE FROM table" without ** a WHERE clause by dropping and recreating the table. (This is much ** faster than going -** through and deleting individual elements form the table.) Because of +** through and deleting individual elements from the table.) Because of ** this optimization, the change count for "DELETE FROM table" will be ** zero regardless of the number of elements that were originally in the ** table. To get an accurate count of the number of rows deleted, use ** "DELETE FROM table WHERE 1" instead. ** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} +** See also the [sqlite3_changes()] interface. +** +** INVARIANTS: +** +** {F12261} The [sqlite3_total_changes()] returns the total number +** of row changes caused by INSERT, UPDATE, and/or DELETE +** statements on the same [database connection], in any +** trigger context, since the database connection was +** created. +** +** LIMITATIONS: +** +** {U12264} If a separate thread makes changes on the same database connection +** while [sqlite3_total_changes()] is running then the value +** returned is unpredictable and unmeaningful. */ SQLITE_API int sqlite3_total_changes(sqlite3*); /* ** CAPI3REF: Interrupt A Long-Running Query {F12270} ** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically +** This function causes any pending database operation to abort and +** return at its earliest opportunity. This routine is typically ** called in response to a user action such as pressing "Cancel" ** or Ctrl-C where the user wants a long query operation to halt ** immediately. ** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it +** It is safe to call this routine from a thread different from the +** thread that is currently running the database operation. But it ** is not safe to call this routine with a database connection that ** is closed or might close before sqlite3_interrupt() returns. ** ** If an SQL is very nearly finished at the time when sqlite3_interrupt() ** is called, then it might not have an opportunity to be interrupted. ** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an +** An SQL operation that is interrupted will return +** [SQLITE_INTERRUPT]. If the interrupted SQL operation is an ** INSERT, UPDATE, or DELETE that is inside an explicit transaction, ** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements +** A call to sqlite3_interrupt() has no effect on SQL statements ** that are started after sqlite3_interrupt() returns. +** +** INVARIANTS: +** +** {F12271} The [sqlite3_interrupt()] interface will force all running +** SQL statements associated with the same database connection +** to halt after processing at most one additional row of +** data. +** +** {F12272} Any SQL statement that is interrupted by [sqlite3_interrupt()] +** will return [SQLITE_INTERRUPT]. +** +** LIMITATIONS: +** +** {U12279} If the database connection closes while [sqlite3_interrupt()] +** is running then bad things will likely happen. */ SQLITE_API void sqlite3_interrupt(sqlite3*); @@ -1266,21 +1517,30 @@ SQLITE_API void sqlite3_interrupt(sqlite3*); ** if additional input is needed before sending the text into ** SQLite for parsing. These routines return true if the input string ** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and +** complete if it ends with a semicolon token and is not a fragment of a +** CREATE TRIGGER statement. Semicolons that are embedded within +** string literals or quoted identifier names or comments are not +** independent tokens (they are part of the token in which they are +** embedded) and thus do not count as a statement terminator. +** +** These routines do not parse the SQL and ** so will not detect syntactically incorrect SQL. ** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} +** INVARIANTS: +** +** {F10511} The sqlite3_complete() and sqlite3_complete16() functions +** return true (non-zero) if and only if the last +** non-whitespace token in their input is a semicolon that +** is not in between the BEGIN and END of a CREATE TRIGGER +** statement. +** +** LIMITATIONS: +** +** {U10512} The input to sqlite3_complete() must be a zero-terminated +** UTF-8 string. +** +** {U10513} The input to sqlite3_complete16() must be a zero-terminated +** UTF-16 string in native byte order. */ SQLITE_API int sqlite3_complete(const char *sql); SQLITE_API int sqlite3_complete16(const void *sql); @@ -1288,29 +1548,29 @@ SQLITE_API int sqlite3_complete16(const void *sql); /* ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} ** -** {F12311} This routine identifies a callback function that might be +** This routine identifies a callback function that might be ** invoked whenever an attempt is made to open a database table ** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] +** If the busy callback is NULL, then [SQLITE_BUSY] ** or [SQLITE_IOERR_BLOCKED] ** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The +** If the busy callback is not NULL, then the +** callback will be invoked with two arguments. The ** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to +** is the third argument to this routine. The second argument to ** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the +** been invoked for this locking event. If the ** busy callback returns 0, then no additional attempts are made to ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt +** If the callback returns non-zero, then another attempt ** is made to open the database for reading and the cycle repeats. ** ** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} +** it will be invoked when there is lock contention. ** If SQLite determines that invoking the busy handler could result in ** a deadlock, it will go ahead and return [SQLITE_BUSY] or ** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} +** busy handler. ** Consider a scenario where one process is holding a read lock that ** it is trying to promote to a reserved lock and ** a second process is holding a reserved lock that it is trying @@ -1322,74 +1582,120 @@ SQLITE_API int sqlite3_complete16(const void *sql); ** will induce the first process to release its read lock and allow ** the second process to proceed. ** -** {F12321} The default busy callback is NULL. {END} +** The default busy callback is NULL. ** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] +** The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] ** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will +** changes will not fit into the in-memory cache. SQLite will ** already hold a RESERVED lock on the database file, but it needs ** to promote this lock to EXCLUSIVE so that it can spill cache ** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory +** readers. If it is unable to promote the lock, then the in-memory ** cache will be left in an inconsistent state and so the error ** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the +** the more severe [SQLITE_IOERR_BLOCKED]. This error code promotion +** forces an automatic rollback of the changes. See the ** ** CorruptionFollowingBusyError wiki page for a discussion of why ** this is important. ** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database +** There can only be a single busy handler defined for each database ** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear +** Note that calling [sqlite3_busy_timeout()] will also set or clear ** the busy handler. ** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. +** INVARIANTS: +** +** {F12311} The [sqlite3_busy_handler()] function replaces the busy handler +** callback in the database connection identified by the 1st +** parameter with a new busy handler identified by the 2nd and 3rd +** parameters. +** +** {F12312} The default busy handler for new database connections is NULL. +** +** {F12314} When two or more database connection share a common cache, +** the busy handler for the database connection currently using +** the cache is invoked when the cache encounters a lock. +** +** {F12316} If a busy handler callback returns zero, then the SQLite +** interface that provoked the locking event will return +** [SQLITE_BUSY]. +** +** {F12318} SQLite will invokes the busy handler with two argument which +** are a copy of the pointer supplied by the 3rd parameter to +** [sqlite3_busy_handler()] and a count of the number of prior +** invocations of the busy handler for the same locking event. +** +** LIMITATIONS: +** +** {U12319} A busy handler should not call close the database connection +** or prepared statement that invoked the busy handler. */ SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); /* ** CAPI3REF: Set A Busy Timeout {F12340} ** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] +** This routine sets a [sqlite3_busy_handler | busy handler] ** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until +** table is locked. The handler will sleep multiple times until ** at least "ms" milliseconds of sleeping have been done. {F12343} After ** "ms" milliseconds of sleeping, the handler returns 0 which ** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. ** -** {F12344} Calling this routine with an argument less than or equal to zero +** Calling this routine with an argument less than or equal to zero ** turns off all busy handlers. ** -** {F12345} There can only be a single busy handler for a particular database +** There can only be a single busy handler for a particular database ** connection. If another busy handler was defined ** (using [sqlite3_busy_handler()]) prior to calling ** this routine, that other busy handler is cleared. +** +** INVARIANTS: +** +** {F12341} The [sqlite3_busy_timeout()] function overrides any prior +** [sqlite3_busy_timeout()] or [sqlite3_busy_handler()] setting +** on the same database connection. +** +** {F12343} If the 2nd parameter to [sqlite3_busy_timeout()] is less than +** or equal to zero, then the busy handler is cleared so that +** all subsequent locking events immediately return [SQLITE_BUSY]. +** +** {F12344} If the 2nd parameter to [sqlite3_busy_timeout()] is a positive +** number N, then a busy handler is set that repeatedly calls +** the xSleep() method in the VFS interface until either the +** lock clears or until the cumulative sleep time reported back +** by xSleep() exceeds N milliseconds. */ SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); /* ** CAPI3REF: Convenience Routines For Running Queries {F12370} ** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} +** Definition: A result table is memory data structure created by the +** [sqlite3_get_table()] interface. A result table records the +** complete query results from one or more queries. +** +** The table conceptually has a number of rows and columns. But +** these numbers are not part of the result table itself. These +** numbers are obtained separately. Let N be the number of rows +** and M be the number of columns. +** +** A result table is an array of pointers to zero-terminated +** UTF-8 strings. There are (N+1)*M elements in the array. +** The first M pointers point to zero-terminated strings that +** contain the names of the columns. +** The remaining entries all point to query results. NULL +** values are give a NULL pointer. All other values are in +** their UTF-8 zero-terminated string representation as returned by +** [sqlite3_column_text()]. +** +** A result table might consists of one or more memory allocations. +** It is not safe to pass a result table directly to [sqlite3_free()]. +** A result table should be deallocated using [sqlite3_free_table()]. ** -** As an example, suppose the query result where this table: +** As an example of the result table format, suppose a query result +** is as follows: ** **
     **        Name        | Age
    @@ -1399,8 +1705,9 @@ SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
     **        Cindy       | 21
     ** 
    ** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: +** There are two column (M==2) and three rows (N==3). Thus the +** result table has 8 entries. Suppose the result table is stored +** in an array names azResult. Then azResult holds this content: ** **
     **        azResult[0] = "Name";
    @@ -1413,28 +1720,55 @@ SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
     **        azResult[7] = "21";
     ** 
    ** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). +** The sqlite3_get_table() function evaluates one or more +** semicolon-separated SQL statements in the zero-terminated UTF-8 +** string of its 2nd parameter. It returns a result table to the +** pointer given in its 3rd parameter. ** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to +** After the calling function has finished using the result, it should +** pass the pointer to the result table to sqlite3_free_table() in order to ** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. +** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling +** function must not try to call [sqlite3_free()] directly. Only +** [sqlite3_free_table()] is able to release the memory properly and safely. +** +** The sqlite3_get_table() interface is implemented as a wrapper around +** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access +** to any internal data structures of SQLite. It uses only the public +** interface defined here. As a consequence, errors that occur in the +** wrapper layer outside of the internal [sqlite3_exec()] call are not +** reflected in subsequent calls to [sqlite3_errcode()] or +** [sqlite3_errmsg()]. +** +** INVARIANTS: +** +** {F12371} If a [sqlite3_get_table()] fails a memory allocation, then +** it frees the result table under construction, aborts the +** query in process, skips any subsequent queries, sets the +** *resultp output pointer to NULL and returns [SQLITE_NOMEM]. +** +** {F12373} If the ncolumn parameter to [sqlite3_get_table()] is not NULL +** then [sqlite3_get_table()] write the number of columns in the +** result set of the query into *ncolumn if the query is +** successful (if the function returns SQLITE_OK). +** +** {F12374} If the nrow parameter to [sqlite3_get_table()] is not NULL +** then [sqlite3_get_table()] write the number of rows in the +** result set of the query into *nrow if the query is +** successful (if the function returns SQLITE_OK). +** +** {F12376} The [sqlite3_get_table()] function sets its *ncolumn value +** to the number of columns in the result set of the query in the +** sql parameter, or to zero if the query in sql has an empty +** result set. */ SQLITE_API int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ + sqlite3*, /* An open database */ + const char *sql, /* SQL to be evaluated */ + char ***pResult, /* Results of the query */ + int *nrow, /* Number of result rows written here */ + int *ncolumn, /* Number of result columns written here */ + char **errmsg /* Error msg written here */ ); SQLITE_API void sqlite3_free_table(char **result); @@ -1444,30 +1778,30 @@ SQLITE_API void sqlite3_free_table(char **result); ** These routines are workalikes of the "printf()" family of functions ** from the standard C library. ** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their +** The sqlite3_mprintf() and sqlite3_vmprintf() routines write their ** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a +** The strings returned by these two routines should be +** released by [sqlite3_free()]. Both routines return a ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough ** memory to hold the resulting string. ** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from +** In sqlite3_snprintf() routine is similar to "snprintf()" from ** the standard C library. The result is written into the ** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the +** the first parameter. Note that the order of the ** first two parameters is reversed from snprintf(). This is an ** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() +** backwards compatibility. Note also that sqlite3_snprintf() ** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that +** characters actually written into the buffer. We admit that ** the number of characters written would be a more useful return ** value but we cannot change the implementation of sqlite3_snprintf() ** now without breaking compatibility. ** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first +** As long as the buffer size is greater than zero, sqlite3_snprintf() +** guarantees that the buffer is always zero-terminated. The first ** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely +** the zero terminator. So the longest string that can be completely ** written will be n-1 characters. ** ** These routines all implement some additional formatting @@ -1475,9 +1809,9 @@ SQLITE_API void sqlite3_free_table(char **result); ** All of the usual printf formatting options apply. In addition, there ** is are "%q", "%Q", and "%z" options. ** -** {F17410} The %q option works like %s in that it substitutes a null-terminated +** The %q option works like %s in that it substitutes a null-terminated ** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' +** %q is designed for use inside a string literal. By doubling each '\'' ** character it escapes that character and allows it to be inserted into ** the string. ** @@ -1513,7 +1847,7 @@ SQLITE_API void sqlite3_free_table(char **result); ** should always use %q instead of %s when inserting text into a string ** literal. ** -** {F17411} The %Q option works like %q except it also adds single quotes around +** The %Q option works like %q except it also adds single quotes around ** the outside of the total string. Or if the parameter in the argument ** list is a NULL pointer, %Q substitutes the text "NULL" (without single ** quotes) in place of the %Q option. {END} So, for example, one could say: @@ -1527,9 +1861,27 @@ SQLITE_API void sqlite3_free_table(char **result); ** The code above will render a correct SQL statement in the zSQL ** variable even if the zText variable is a NULL pointer. ** -** {F17412} The "%z" formatting option works exactly like "%s" with the +** The "%z" formatting option works exactly like "%s" with the ** addition that after the string has been read and copied into ** the result, [sqlite3_free()] is called on the input string. {END} +** +** INVARIANTS: +** +** {F17403} The [sqlite3_mprintf()] and [sqlite3_vmprintf()] interfaces +** return either pointers to zero-terminated UTF-8 strings held in +** memory obtained from [sqlite3_malloc()] or NULL pointers if +** a call to [sqlite3_malloc()] fails. +** +** {F17406} The [sqlite3_snprintf()] interface writes a zero-terminated +** UTF-8 string into the buffer pointed to by the second parameter +** provided that the first parameter is greater than zero. +** +** {F17407} The [sqlite3_snprintf()] interface does not writes slots of +** its output buffer (the second parameter) outside the range +** of 0 through N-1 (where N is the first parameter) +** regardless of the length of the string +** requested by the format specification. +** */ SQLITE_API char *sqlite3_mprintf(const char*,...); SQLITE_API char *sqlite3_vmprintf(const char*, va_list); @@ -1538,50 +1890,50 @@ SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...); /* ** CAPI3REF: Memory Allocation Subsystem {F17300} ** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence +** The SQLite core uses these three routines for all of its own +** internal memory allocation needs. "Core" in the previous sentence ** does not include operating-system specific VFS implementation. The ** windows VFS uses native malloc and free for some operations. ** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block +** The sqlite3_malloc() routine returns a pointer to a block ** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to +** If sqlite3_malloc() is unable to obtain sufficient free +** memory, it returns a NULL pointer. If the parameter N to ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns ** a NULL pointer. ** -** {F17305} Calling sqlite3_free() with a pointer previously returned +** Calling sqlite3_free() with a pointer previously returned ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is +** that it might be reused. The sqlite3_free() routine is ** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory +** to sqlite3_free() is harmless. After being freed, memory ** should neither be read nor written. Even reading previously freed ** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error +** Memory corruption, a segmentation fault, or other severe error ** might result if sqlite3_free() is called with a non-NULL pointer that ** was not obtained from sqlite3_malloc() or sqlite3_free(). ** -** {F17310} The sqlite3_realloc() interface attempts to resize a +** The sqlite3_realloc() interface attempts to resize a ** prior memory allocation to be at least N bytes, where N is the ** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() +** parameter. If the first parameter to sqlite3_realloc() ** is a NULL pointer then its behavior is identical to calling ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or +** If the second parameter to sqlite3_realloc() is zero or ** negative then the behavior is exactly the same as calling ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation +** Sqlite3_realloc() returns a pointer to a memory allocation ** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes +** If M is the size of the prior allocation, then min(N,M) bytes ** of the prior allocation are copied into the beginning of buffer returned ** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation +** If sqlite3_realloc() returns NULL, then the prior allocation ** is not freed. ** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() +** The memory returned by sqlite3_malloc() and sqlite3_realloc() ** is always aligned to at least an 8 byte boundary. {END} ** -** {F17381} The default implementation +** The default implementation ** of the memory allocation subsystem uses the malloc(), realloc() ** and free() provided by the standard C library. {F17382} However, if ** SQLite is compiled with the following C preprocessor macro @@ -1606,6 +1958,58 @@ SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...); ** installation. Memory allocation errors are detected, but ** they are reported back as [SQLITE_CANTOPEN] or ** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. +** +** INVARIANTS: +** +** {F17303} The [sqlite3_malloc(N)] interface returns either a pointer to +** newly checked-out block of at least N bytes of memory +** that is 8-byte aligned, +** or it returns NULL if it is unable to fulfill the request. +** +** {F17304} The [sqlite3_malloc(N)] interface returns a NULL pointer if +** N is less than or equal to zero. +** +** {F17305} The [sqlite3_free(P)] interface releases memory previously +** returned from [sqlite3_malloc()] or [sqlite3_realloc()], +** making it available for reuse. +** +** {F17306} A call to [sqlite3_free(NULL)] is a harmless no-op. +** +** {F17310} A call to [sqlite3_realloc(0,N)] is equivalent to a call +** to [sqlite3_malloc(N)]. +** +** {F17312} A call to [sqlite3_realloc(P,0)] is equivalent to a call +** to [sqlite3_free(P)]. +** +** {F17315} The SQLite core uses [sqlite3_malloc()], [sqlite3_realloc()], +** and [sqlite3_free()] for all of its memory allocation and +** deallocation needs. +** +** {F17318} The [sqlite3_realloc(P,N)] interface returns either a pointer +** to a block of checked-out memory of at least N bytes in size +** that is 8-byte aligned, or a NULL pointer. +** +** {F17321} When [sqlite3_realloc(P,N)] returns a non-NULL pointer, it first +** copies the first K bytes of content from P into the newly allocated +** where K is the lessor of N and the size of the buffer P. +** +** {F17322} When [sqlite3_realloc(P,N)] returns a non-NULL pointer, it first +** releases the buffer P. +** +** {F17323} When [sqlite3_realloc(P,N)] returns NULL, the buffer P is +** not modified or released. +** +** LIMITATIONS: +** +** {U17350} The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] +** must be either NULL or else a pointer obtained from a prior +** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that has +** not been released. +** +** {U17351} The application must not read or write any part of +** a block of memory after it has been released using +** [sqlite3_free()] or [sqlite3_realloc()]. +** */ SQLITE_API void *sqlite3_malloc(int); SQLITE_API void *sqlite3_realloc(void*, int); @@ -1614,29 +2018,31 @@ SQLITE_API void sqlite3_free(void*); /* ** CAPI3REF: Memory Allocator Statistics {F17370} ** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. +** SQLite provides these two interfaces for reporting on the status +** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()] +** the memory allocation subsystem included within the SQLite. +** +** INVARIANTS: +** +** {F17371} The [sqlite3_memory_used()] routine returns the +** number of bytes of memory currently outstanding +** (malloced but not freed). +** +** {F17373} The [sqlite3_memory_highwater()] routine returns the maximum +** value of [sqlite3_memory_used()] +** since the highwater mark was last reset. +** +** {F17374} The values returned by [sqlite3_memory_used()] and +** [sqlite3_memory_highwater()] include any overhead +** added by SQLite in its implementation of [sqlite3_malloc()], +** but not overhead added by the any underlying system library +** routines that [sqlite3_malloc()] may call. +** +** {F17375} The memory highwater mark is reset to the current value of +** [sqlite3_memory_used()] if and only if the parameter to +** [sqlite3_memory_highwater()] is true. The value returned +** by [sqlite3_memory_highwater(1)] is the highwater mark +** prior to the reset. */ SQLITE_API sqlite3_int64 sqlite3_memory_used(void); SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag); @@ -1644,44 +2050,41 @@ SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag); /* ** CAPI3REF: Compile-Time Authorization Callbacks {F12500} ** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} +** This routine registers a authorizer callback with a particular +** database connection, supplied in the first argument. ** The authorizer callback is invoked as SQL statements are being compiled ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various +** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. At various ** points during the compilation process, as logic is being created ** to perform various actions, the authorizer callback is invoked to ** see if those actions are allowed. The authorizer callback should ** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the ** specific action but allow the SQL statement to continue to be ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns +** rejected with an error. If the authorizer callback returns ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] ** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} +** the authorizer will fail with an error message. ** ** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the +** requested is ok. When the callback returns [SQLITE_DENY], the ** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of +** authorizer will fail with an error message explaining that +** access is denied. If the authorizer code is [SQLITE_READ] +** and the callback returns [SQLITE_IGNORE] then the prepared +** statement is constructed to insert a NULL value in place of +** the table column that would have +** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] +** return can be used to deny an untrusted user access to individual +** columns of a table. +** +** The first parameter to the authorizer callback is a copy of ** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer +** The second parameter to the callback is an integer ** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth +** to be authorized. The third through sixth ** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} +** additional details about the action to be authorized. ** ** An authorizer is used when preparing SQL statements from an untrusted ** source, to ensure that the SQL statements do not try to access data @@ -1694,14 +2097,66 @@ SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag); ** user-entered SQL is being prepared that disallows everything ** except SELECT statements. ** -** {F12520} Only a single authorizer can be in place on a database connection +** Only a single authorizer can be in place on a database connection ** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} +** previous call. Disable the authorizer by installing a NULL callback. +** The authorizer is disabled by default. ** ** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} +** [sqlite3_prepare()] or its variants. Authorization is not +** performed during statement evaluation in [sqlite3_step()]. +** +** INVARIANTS: +** +** {F12501} The [sqlite3_set_authorizer(D,...)] interface registers a +** authorizer callback with database connection D. +** +** {F12502} The authorizer callback is invoked as SQL statements are +** being compiled +** +** {F12503} If the authorizer callback returns any value other than +** [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] then +** the [sqlite3_prepare_v2()] or equivalent call that caused +** the authorizer callback to run shall fail with an +** [SQLITE_ERROR] error code and an appropriate error message. +** +** {F12504} When the authorizer callback returns [SQLITE_OK], the operation +** described is coded normally. +** +** {F12505} When the authorizer callback returns [SQLITE_DENY], the +** [sqlite3_prepare_v2()] or equivalent call that caused the +** authorizer callback to run shall fail +** with an [SQLITE_ERROR] error code and an error message +** explaining that access is denied. +** +** {F12506} If the authorizer code (the 2nd parameter to the authorizer +** callback) is [SQLITE_READ] and the authorizer callback returns +** [SQLITE_IGNORE] then the prepared statement is constructed to +** insert a NULL value in place of the table column that would have +** been read if [SQLITE_OK] had been returned. +** +** {F12507} If the authorizer code (the 2nd parameter to the authorizer +** callback) is anything other than [SQLITE_READ], then +** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. +** +** {F12510} The first parameter to the authorizer callback is a copy of +** the third parameter to the [sqlite3_set_authorizer()] interface. +** +** {F12511} The second parameter to the callback is an integer +** [SQLITE_COPY | action code] that specifies the particular action +** to be authorized. +** +** {F12512} The third through sixth parameters to the callback are +** zero-terminated strings that contain +** additional details about the action to be authorized. +** +** {F12520} Each call to [sqlite3_set_authorizer()] overrides the +** any previously installed authorizer. +** +** {F12521} A NULL authorizer means that no authorization +** callback is invoked. +** +** {F12522} The default authorizer is NULL. */ SQLITE_API int sqlite3_set_authorizer( sqlite3*, @@ -1725,20 +2180,42 @@ SQLITE_API int sqlite3_set_authorizer( ** CAPI3REF: Authorizer Action Codes {F12550} ** ** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The +** that is invoked to authorizer certain SQL statement actions. The ** second parameter to the callback is an integer code that specifies ** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} +** the authorizer callback may be passed. ** ** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization +** authorized. The 3rd and 4th parameters to the authorization ** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the +** codes is used as the second parameter. The 5th parameter to the ** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback +** etc.) if applicable. The 6th parameter to the authorizer callback ** is the name of the inner-most trigger or view that is responsible for ** the access attempt or NULL if this access attempt is directly from ** top-level SQL code. +** +** INVARIANTS: +** +** {F12551} The second parameter to an +** [sqlite3_set_authorizer | authorizer callback is always an integer +** [SQLITE_COPY | authorizer code] that specifies what action +** is being authorized. +** +** {F12552} The 3rd and 4th parameters to the +** [sqlite3_set_authorizer | authorization callback function] +** will be parameters or NULL depending on which +** [SQLITE_COPY | authorizer code] is used as the second parameter. +** +** {F12553} The 5th parameter to the +** [sqlite3_set_authorizer | authorizer callback] is the name +** of the database (example: "main", "temp", etc.) if applicable. +** +** {F12554} The 6th parameter to the +** [sqlite3_set_authorizer | authorizer callback] is the name +** of the inner-most trigger or view that is responsible for +** the access attempt or NULL if this access attempt is directly from +** top-level SQL code. */ /******************************************* 3rd ************ 4th ***********/ #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ @@ -1780,31 +2257,60 @@ SQLITE_API int sqlite3_set_authorizer( ** These routines register callback functions that can be used for ** tracing and profiling the execution of SQL statements. ** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} +** The callback function registered by sqlite3_trace() is invoked at +** various times when an SQL statement is being run by [sqlite3_step()]. +** The callback returns a UTF-8 rendering of the SQL statement text +** as the statement first begins executing. Additional callbacks occur +** as each triggersubprogram is entered. The callbacks for triggers +** contain a UTF-8 SQL comment that identifies the trigger. +** +** The callback function registered by sqlite3_profile() is invoked +** as each SQL statement finishes. The profile callback contains +** the original statement text and an estimate of wall-clock time +** of how long that statement took to run. ** ** The sqlite3_profile() API is currently considered experimental and -** is subject to change. +** is subject to change or removal in a future release. +** +** The trigger reporting feature of the trace callback is considered +** experimental and is subject to change or removal in future releases. +** Future versions of SQLite might also add new trace callback +** invocations. +** +** INVARIANTS: +** +** {F12281} The callback function registered by [sqlite3_trace()] is +** whenever an SQL statement first begins to execute and +** whenever a trigger subprogram first begins to run. +** +** {F12282} Each call to [sqlite3_trace()] overrides the previously +** registered trace callback. +** +** {F12283} A NULL trace callback disables tracing. +** +** {F12284} The first argument to the trace callback is a copy of +** the pointer which was the 3rd argument to [sqlite3_trace()]. +** +** {F12285} The second argument to the trace callback is a +** zero-terminated UTF8 string containing the original text +** of the SQL statement as it was passed into [sqlite3_prepare_v2()] +** or the equivalent, or an SQL comment indicating the beginning +** of a trigger subprogram. +** +** {F12287} The callback function registered by [sqlite3_profile()] is invoked +** as each SQL statement finishes. +** +** {F12288} The first parameter to the profile callback is a copy of +** the 3rd parameter to [sqlite3_profile()]. +** +** {F12289} The second parameter to the profile callback is a +** zero-terminated UTF-8 string that contains the complete text of +** the SQL statement as it was processed by [sqlite3_prepare_v2()] +** or the equivalent. +** +** {F12290} The third parameter to the profile callback is an estimate +** of the number of nanoseconds of wall-clock time required to +** run the SQL statement from start to finish. */ SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); SQLITE_API void *sqlite3_profile(sqlite3*, @@ -1813,68 +2319,78 @@ SQLITE_API void *sqlite3_profile(sqlite3*, /* ** CAPI3REF: Query Progress Callbacks {F12910} ** -** {F12911} This routine configures a callback function - the +** This routine configures a callback function - the ** progress callback - that is invoked periodically during long ** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this +** [sqlite3_get_table()]. An example use for this ** interface is to keep a GUI updated during a large query. ** +** If the progress callback returns non-zero, the opertion is +** interrupted. This feature can be used to implement a +** "Cancel" button on a GUI dialog box. +** +** INVARIANTS: +** +** {F12911} The callback function registered by [sqlite3_progress_handler()] +** is invoked periodically during long running calls to +** [sqlite3_step()]. +** ** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. +** machine opcodes, where N is the second argument to +** the [sqlite3_progress_handler()] call that registered +** the callback. What if N is less than 1? +** ** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} +** argument to [sqlite3_progress_handler()]. ** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} +** {F12914} The fourth argument [sqlite3_progress_handler()] is a +*** void pointer passed to the progress callback +** function each time it is invoked. +** +** {F12915} If a call to [sqlite3_step()] results in fewer than +** N opcodes being executed, +** then the progress callback is never invoked. {END} ** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} +** {F12916} Every call to [sqlite3_progress_handler()] +** overwrites any previously registere progress handler. +** +** {F12917} If the progress handler callback is NULL then no progress +** handler is invoked. ** ** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. +** the behavior is a if [sqlite3_interrupt()] had been called. */ SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); /* ** CAPI3REF: Opening A New Database Connection {F12700} ** -** {F12701} These routines open an SQLite database file whose name +** These routines open an SQLite database file whose name ** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 +** The filename argument is interpreted as UTF-8 ** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 ** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable +** An [sqlite3*] handle is usually returned in *ppDb, even +** if an error occurs. The only exception is if SQLite is unable ** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The +** be written into *ppDb instead of a pointer to the [sqlite3] object. +** If the database is opened (and/or created) +** successfully, then [SQLITE_OK] is returned. Otherwise an +** error code is returned. The ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain ** an English language description of the error. ** -** {F12707} The default encoding for the database will be UTF-8 if +** The default encoding for the database will be UTF-8 if ** [sqlite3_open()] or [sqlite3_open_v2()] is called and ** UTF-16 in the native byte order if [sqlite3_open16()] is used. ** -** {F12708} Whether or not an error occurs when it is opened, resources +** Whether or not an error occurs when it is opened, resources ** associated with the [sqlite3*] handle should be released by passing it ** to [sqlite3_close()] when it is no longer required. ** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] +** The [sqlite3_open_v2()] interface works like [sqlite3_open()] ** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be +** over the new database connection. The flags parameter can be ** one of: ** **
      @@ -1883,41 +2399,104 @@ SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); **
    1. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] **
    ** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens +** The first value opens the database read-only. +** If the database does not previously exist, an error is returned. +** The second option opens ** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option +** if the file is write protected. In either case the database +** must already exist or an error is returned. The third option ** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} +** not already exist. ** The third options is behavior that is always used for [sqlite3_open()] ** and [sqlite3_open16()]. ** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future +** If the filename is ":memory:", then an private +** in-memory database is created for the connection. This in-memory +** database will vanish when the database connection is closed. Future ** version of SQLite might make use of additional special filenames ** that begin with the ":" character. It is recommended that ** when a database filename really does begin with ** ":" that you prefix the filename with a pathname like "./" to ** avoid ambiguity. ** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be +** If the filename is an empty string, then a private temporary +** on-disk database will be created. This private database will be ** automatically deleted as soon as the database connection is closed. ** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the +** The fourth parameter to sqlite3_open_v2() is the name of the ** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the +** interface that the new database connection should use. If the ** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} +** object is used. ** ** Note to windows users: The encoding used for the filename argument ** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever ** codepage is currently defined. Filenames containing international ** characters must be converted to UTF-8 prior to passing them into ** [sqlite3_open()] or [sqlite3_open_v2()]. +** +** INVARIANTS: +** +** {F12701} The [sqlite3_open()], [sqlite3_open16()], and +** [sqlite3_open_v2()] interfaces create a new +** [database connection] associated with +** the database file given in their first parameter. +** +** {F12702} The filename argument is interpreted as UTF-8 +** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 +** in the native byte order for [sqlite3_open16()]. +** +** {F12703} A successful invocation of [sqlite3_open()], [sqlite3_open16()], +** or [sqlite3_open_v2()] writes a pointer to a new +** [database connection] into *ppDb. +** +** {F12704} The [sqlite3_open()], [sqlite3_open16()], and +** [sqlite3_open_v2()] interfaces return [SQLITE_OK] upon success, +** or an appropriate [error code] on failure. +** +** {F12706} The default text encoding for a new database created using +** [sqlite3_open()] or [sqlite3_open_v2()] will be UTF-8. +** +** {F12707} The default text encoding for a new database created using +** [sqlite3_open16()] will be UTF-16. +** +** {F12709} The [sqlite3_open(F,D)] interface is equivalent to +** [sqlite3_open_v2(F,D,G,0)] where the G parameter is +** [SQLITE_OPEN_READWRITE]|[SQLITE_OPEN_CREATE]. +** +** {F12711} If the G parameter to [sqlite3_open_v2(F,D,G,V)] contains the +** bit value [SQLITE_OPEN_READONLY] then the database is opened +** for reading only. +** +** {F12712} If the G parameter to [sqlite3_open_v2(F,D,G,V)] contains the +** bit value [SQLITE_OPEN_READWRITE] then the database is opened +** reading and writing if possible, or for reading only if the +** file is write protected by the operating system. +** +** {F12713} If the G parameter to [sqlite3_open(v2(F,D,G,V)] omits the +** bit value [SQLITE_OPEN_CREATE] and the database does not +** previously exist, an error is returned. +** +** {F12714} If the G parameter to [sqlite3_open(v2(F,D,G,V)] contains the +** bit value [SQLITE_OPEN_CREATE] and the database does not +** previously exist, then an attempt is made to create and +** initialize the database. +** +** {F12717} If the filename argument to [sqlite3_open()], [sqlite3_open16()], +** or [sqlite3_open_v2()] is ":memory:", then an private, +** ephemeral, in-memory database is created for the connection. +** Is SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE required +** in sqlite3_open_v2()? +** +** {F12719} If the filename is NULL or an empty string, then a private, +** ephermeral on-disk database will be created. +** Is SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE required +** in sqlite3_open_v2()? +** +** {F12721} The [database connection] created by +** [sqlite3_open_v2(F,D,G,V)] will use the +** [sqlite3_vfs] object identified by the V parameter, or +** the default [sqlite3_vfs] object is V is a NULL pointer. */ SQLITE_API int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ @@ -1937,35 +2516,46 @@ SQLITE_API int sqlite3_open_v2( /* ** CAPI3REF: Error Codes And Messages {F12800} ** -** {F12801} The sqlite3_errcode() interface returns the numeric +** The sqlite3_errcode() interface returns the numeric ** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] ** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the +** with [sqlite3] handle 'db'. If a prior API call failed but the ** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} +** is undefined. ** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language +** The sqlite3_errmsg() and sqlite3_errmsg16() return English-language ** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. +** Memory to hold the error message string is managed internally. +** The application does not need to worry with freeing the result. +** However, the error string might be overwritten or deallocated by +** subsequent calls to other SQLite interface functions. +** +** INVARIANTS: +** +** {F12801} The [sqlite3_errcode(D)] interface returns the numeric +** [SQLITE_OK | result code] or +** [SQLITE_IOERR_READ | extended result code] +** for the most recently failed interface call associated +** with [database connection] D. +** +** {F12803} The [sqlite3_errmsg(D)] and [sqlite3_errmsg16(D)] +** interfaces return English-language text that describes +** the error in the mostly recently failed interface call, +** encoded as either UTF8 or UTF16 respectively. +** +** {F12807} The strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()] +** are valid until the next SQLite interface call. +** +** {F12808} Calls to API routines that do not return an error code +** (example: [sqlite3_data_count()]) do not +** change the error code or message returned by +** [sqlite3_errcode()], [sqlite3_errmsg()], or [sqlite3_errmsg16()]. +** +** {F12809} Interfaces that are not associated with a specific +** [database connection] (examples: +** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] +** do not change the values returned by +** [sqlite3_errcode()], [sqlite3_errmsg()], or [sqlite3_errmsg16()]. */ SQLITE_API int sqlite3_errcode(sqlite3 *db); SQLITE_API const char *sqlite3_errmsg(sqlite3*); @@ -1973,6 +2563,7 @@ SQLITE_API const void *sqlite3_errmsg16(sqlite3*); /* ** CAPI3REF: SQL Statement Object {F13000} +** KEYWORDS: {prepared statement} {prepared statements} ** ** An instance of this object represent single SQL statements. This ** object is variously known as a "prepared statement" or a @@ -2002,72 +2593,105 @@ typedef struct sqlite3_stmt sqlite3_stmt; ** To execute an SQL query, it must first be compiled into a byte-code ** program using one of these routines. ** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] +** The first argument "db" is an [database connection] ** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} +** or [sqlite3_open16()]. ** The second argument "zSql" is the statement to be compiled, encoded ** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() ** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() ** use UTF-16. {END} ** -** {F13013} If the nByte argument is less +** If the nByte argument is less ** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of +** If nByte is non-negative, then it is the maximum number of ** bytes read from zSql. When nByte is non-negative, the ** zSql string ends at either the first '\000' or '\u0000' character or ** until the nByte-th byte, whichever comes first. {END} ** -** {F13015} *pzTail is made to point to the first byte past the end of the +** *pzTail is made to point to the first byte past the end of the ** first SQL statement in zSql. These routines only compiles the first ** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} +** uncompiled. ** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input +** *ppStmt is left pointing to a compiled [prepared statement] that can be +** executed using [sqlite3_step()]. Or if there is an error, *ppStmt is +** set to NULL. If the input text contains no SQL (if the input ** is and empty string or a comment) then *ppStmt is set to NULL. ** {U13018} The calling procedure is responsible for deleting the ** compiled SQL statement ** using [sqlite3_finalize()] after it has finished with it. ** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} +** On success, [SQLITE_OK] is returned. Otherwise an +** [error code] is returned. ** ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are ** recommended for all new programs. The two older interfaces are retained ** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement +** In the "v2" interfaces, the prepared statement ** that is returned (the [sqlite3_stmt] object) contains a copy of the ** original SQL text. {END} This causes the [sqlite3_step()] interface to ** behave a differently in two ways: ** **
      -**
    1. {F13022} +**
    2. ** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it ** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in +** statement and try to run it again. If the schema has changed in ** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling +** return [SQLITE_SCHEMA]. But unlike the legacy behavior, +** [SQLITE_SCHEMA] is now a fatal error. Calling ** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text +** error go away. Note: use [sqlite3_errmsg()] to find the text ** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} **
    3. ** **
    4. -** {F13030} When an error occurs, +** When an error occurs, ** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} +** [error codes] or [extended error codes]. ** The legacy behavior was that [sqlite3_step()] would only return a generic ** [SQLITE_ERROR] result code and you would have to make a second call to ** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} ** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} +** returned immediately. **
    5. **
    +** +** INVARIANTS: +** +** {F13011} The [sqlite3_prepare(db,zSql,...)] and +** [sqlite3_prepare_v2(db,zSql,...)] interfaces interpret the +** text in their zSql parameter as UTF-8. +** +** {F13012} The [sqlite3_prepare16(db,zSql,...)] and +** [sqlite3_prepare16_v2(db,zSql,...)] interfaces interpret the +** text in their zSql parameter as UTF-16 in the native byte order. +** +** {F13013} If the nByte argument to [sqlite3_prepare_v2(db,zSql,nByte,...)] +** and its variants is less than zero, then SQL text is +** read from zSql is read up to the first zero terminator. +** +** {F13014} If the nByte argument to [sqlite3_prepare_v2(db,zSql,nByte,...)] +** and its variants is non-negative, then nBytes bytes +** SQL text is read from zSql. +** +** {F13015} In [sqlite3_prepare_v2(db,zSql,N,P,pzTail)] and its variants +** if the zSql input text contains more than one SQL statement +** and pzTail is not NULL, then *pzTail is made to point to the +** first byte past the end of the first SQL statement in zSql. +** What does *pzTail point to if there is one statement? +** +** {F13016} A successful call to [sqlite3_prepare_v2(db,zSql,N,ppStmt,...)] +** or one of its variants writes into *ppStmt a pointer to a new +** [prepared statement] or a pointer to NULL +** if zSql contains nothing other than whitespace or comments. +** +** {F13019} The [sqlite3_prepare_v2()] interface and its variants return +** [SQLITE_OK] or an appropriate [error code] upon failure. +** +** {F13021} Before [sqlite3_prepare(db,zSql,nByte,ppStmt,pzTail)] or its +** variants returns an error (any value other than [SQLITE_OK]) +** it first sets *ppStmt to NULL. */ SQLITE_API int sqlite3_prepare( sqlite3 *db, /* Database handle */ @@ -2101,29 +2725,37 @@ SQLITE_API int sqlite3_prepare16_v2( /* ** CAPIREF: Retrieving Statement SQL {F13100} ** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. +** This intereface can be used to retrieve a saved copy of the original +** SQL text used to create a [prepared statement]. ** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. +** INVARIANTS: +** +** {F13101} If the [prepared statement] passed as +** the an argument to [sqlite3_sql()] was compiled +** compiled using either [sqlite3_prepare_v2()] or +** [sqlite3_prepare16_v2()], +** then [sqlite3_sql()] function returns a pointer to a +** zero-terminated string containing a UTF-8 rendering +** of the original SQL statement. +** +** {F13102} If the [prepared statement] passed as +** the an argument to [sqlite3_sql()] was compiled +** compiled using either [sqlite3_prepare()] or +** [sqlite3_prepare16()], +** then [sqlite3_sql()] function returns a NULL pointer. +** +** {F13103} The string returned by [sqlite3_sql(S)] is valid until the +** [prepared statement] S is deleted using [sqlite3_finalize(S)]. */ SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt); /* ** CAPI3REF: Dynamically Typed Value Object {F15000} ** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} +** SQLite uses the sqlite3_value object to represent all values +** that are or can be stored in a database table. ** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be +** Values stored in sqlite3_value objects can be ** be integers, floating point values, strings, BLOBs, or NULL. */ typedef struct Mem sqlite3_value; @@ -2132,7 +2764,7 @@ typedef struct Mem sqlite3_value; ** CAPI3REF: SQL Function Context Object {F16001} ** ** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context +** sqlite3_context object. A pointer to an sqlite3_context ** object is always first parameter to application-defined SQL functions. */ typedef struct sqlite3_context sqlite3_context; @@ -2140,78 +2772,152 @@ typedef struct sqlite3_context sqlite3_context; /* ** CAPI3REF: Binding Values To Prepared Statements {F13500} ** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its +** In the SQL strings input to [sqlite3_prepare_v2()] and its ** variants, literals may be replace by a parameter in one ** of these forms: ** **
      **
    • ? **
    • ?NNN -**
    • :AAA -**
    • @AAA +**
    • :VVV +**
    • @VVV **
    • $VVV **
    ** ** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") +** VVV alpha-numeric parameter name. +** The values of these parameters (also called "host parameter names" +** or "SQL parameters") ** can be set using the sqlite3_bind_*() routines defined here. ** -** {F13502} The first argument to the sqlite3_bind_*() routines always +** The first argument to the sqlite3_bind_*() routines always ** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named +** [sqlite3_prepare_v2()] or its variants. The second +** argument is the index of the parameter to be set. The +** first parameter has an index of 1. When the same named ** parameter is used more than once, second and subsequent ** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index +** The index for named parameters can be looked up using the +** [sqlite3_bind_parameter_name()] API if desired. The index ** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. +** The NNN value must be between 1 and the compile-time +** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). ** -** {F13509} The third argument is the value to bind to the parameter. {END} +** The third argument is the value to bind to the parameter. ** -** {F13510} In those +** In those ** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number +** in the parameter. To be clear: the value is the number of bytes +** in the value, not the number of characters. The number ** of bytes does not include the zero-terminator at the end of strings. -** {F13512} ** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} +** number of bytes up to the first zero terminator. ** -** {F13513} ** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the +** string after SQLite has finished with it. If the fifth argument is +** the special value [SQLITE_STATIC], then SQLite assumes that the ** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then +** If the fifth argument has the value [SQLITE_TRANSIENT], then ** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} +** the sqlite3_bind_*() routine returns. ** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} +** The sqlite3_bind_zeroblob() routine binds a BLOB of length N that +** is filled with zeros. A zeroblob uses a fixed amount of memory +** (just an integer to hold it size) while it is being processed. ** Zeroblobs are intended to serve as place-holders for BLOBs whose ** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} +** [sqlite3_blob_open | increment BLOB I/O] routines. A negative +** value for the zeroblob results in a zero-length BLOB. ** -** {F13530} The sqlite3_bind_*() routines must be called after +** The sqlite3_bind_*() routines must be called after ** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} +** before [sqlite3_step()]. ** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} +** Unbound parameters are interpreted as NULL. ** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a +** These routines return [SQLITE_OK] on success or an error code if +** anything goes wrong. [SQLITE_RANGE] is returned if the parameter +** index is out of range. [SQLITE_NOMEM] is returned if malloc fails. +** [SQLITE_MISUSE] might be returned if these routines are called on a ** virtual machine that is the wrong state or which has already been finalized. +** Detection of misuse is unreliable. Applications should not depend +** on SQLITE_MISUSE returns. SQLITE_MISUSE is intended to indicate a +** a logic error in the application. Future versions of SQLite might +** panic rather than return SQLITE_MISUSE. +** +** See also: [sqlite3_bind_parameter_count()], +** [sqlite3_bind_parameter_name()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13506} The [sqlite3_prepare | SQL statement compiler] recognizes +** tokens of the forms "?", "?NNN", "$VVV", ":VVV", and "@VVV" +** as SQL parameters, where NNN is any sequence of one or more +** digits and where VVV is any sequence of one or more +** alphanumeric characters or "::" optionally followed by +** a string containing no spaces and contained within parentheses. +** +** {F13509} The initial value of an SQL parameter is NULL. +** +** {F13512} The index of an "?" SQL parameter is one larger than the +** largest index of SQL parameter to the left, or 1 if +** the "?" is the leftmost SQL parameter. +** +** {F13515} The index of an "?NNN" SQL parameter is the integer NNN. +** +** {F13518} The index of an ":VVV", "$VVV", or "@VVV" SQL parameter is +** the same as the index of leftmost occurances of the same +** parameter, or one more than the largest index over all +** parameters to the left if this is the first occurrance +** of this parameter, or 1 if this is the leftmost parameter. +** +** {F13521} The [sqlite3_prepare | SQL statement compiler] fail with +** an [SQLITE_RANGE] error if the index of an SQL parameter +** is less than 1 or greater than SQLITE_MAX_VARIABLE_NUMBER. +** +** {F13524} Calls to [sqlite3_bind_text | sqlite3_bind(S,N,V,...)] +** associate the value V with all SQL parameters having an +** index of N in the [prepared statement] S. +** +** {F13527} Calls to [sqlite3_bind_text | sqlite3_bind(S,N,...)] +** override prior calls with the same values of S and N. +** +** {F13530} Bindings established by [sqlite3_bind_text | sqlite3_bind(S,...)] +** persist across calls to [sqlite3_reset(S)]. +** +** {F13533} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] SQLite binds the first L +** bytes of the blob or string pointed to by V, when L +** is non-negative. +** +** {F13536} In calls to [sqlite3_bind_text(S,N,V,L,D)] or +** [sqlite3_bind_text16(S,N,V,L,D)] SQLite binds characters +** from V through the first zero character when L is negative. +** +** {F13539} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] when D is the special +** constant [SQLITE_STATIC], SQLite assumes that the value V +** is held in static unmanaged space that will not change +** during the lifetime of the binding. +** +** {F13542} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] when D is the special +** constant [SQLITE_TRANSIENT], the routine makes a +** private copy of V value before it returns. +** +** {F13545} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] when D is a pointer to +** a function, SQLite invokes that function to destroy the +** V value after it has finished using the V value. +** +** {F13548} In calls to [sqlite3_bind_zeroblob(S,N,V,L)] the value bound +** is a blob of L bytes, or a zero-length blob if L is negative. */ SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double); @@ -2224,98 +2930,178 @@ SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); /* -** CAPI3REF: Number Of Host Parameters {F13600} +** CAPI3REF: Number Of SQL Parameters {F13600} +** +** This routine can be used to find the number of SQL parameters +** in a prepared statement. SQL parameters are tokens of the +** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as +** place-holders for values that are [sqlite3_bind_blob | bound] +** to the parameters at a later time. ** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} +** This routine actually returns the index of the largest parameter. +** For all forms except ?NNN, this will correspond to the number of +** unique parameters. If parameters of the ?NNN are used, there may +** be gaps in the list. ** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_name()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13601} The [sqlite3_bind_parameter_count(S)] interface returns +** the largest index of all SQL parameters in the +** [prepared statement] S, or 0 if S +** contains no SQL parameters. */ SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*); /* ** CAPI3REF: Name Of A Host Parameter {F13620} ** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name +** This routine returns a pointer to the name of the n-th +** SQL parameter in a [prepared statement]. +** SQL parameters of the form ":AAA" or "@AAA" or "$AAA" have a name ** which is the string ":AAA" or "@AAA" or "$VVV". ** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} +** is included as part of the name. ** Parameters of the form "?" or "?NNN" have no name. ** -** {F13623} The first host parameter has an index of 1, not 0. +** The first host parameter has an index of 1, not 0. ** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is +** If the value n is out of range or if the n-th parameter is +** nameless, then NULL is returned. The returned string is ** always in the UTF-8 encoding even if the named parameter was ** originally specified as UTF-16 in [sqlite3_prepare16()] or ** [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13621} The [sqlite3_bind_parameter_name(S,N)] interface returns +** a UTF-8 rendering of the name of the SQL parameter in +** [prepared statement] S having index N, or +** NULL if there is no SQL parameter with index N or if the +** parameter with index N is an anonymous parameter "?" or +** a numbered parameter "?NNN". */ SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); /* ** CAPI3REF: Index Of A Parameter With A Given Name {F13640} ** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. +** Return the index of an SQL parameter given its name. The +** index value returned is suitable for use as the second +** parameter to [sqlite3_bind_blob|sqlite3_bind()]. A zero +** is returned if no matching parameter is found. The parameter +** name must be given in UTF-8 even if the original statement +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13641} The [sqlite3_bind_parameter_index(S,N)] interface returns +** the index of SQL parameter in [prepared statement] +** S whose name matches the UTF-8 string N, or 0 if there is +** no match. */ SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); /* ** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} ** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not +** Contrary to the intuition of many, [sqlite3_reset()] does not ** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to +** [prepared statement]. Use this routine to ** reset all host parameters to NULL. +** +** INVARIANTS: +** +** {F13661} The [sqlite3_clear_bindings(S)] interface resets all +** SQL parameter bindings in [prepared statement] S +** back to NULL. */ SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*); /* ** CAPI3REF: Number Of Columns In A Result Set {F13710} ** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 +** Return the number of columns in the result set returned by the +** [prepared statement]. This routine returns 0 ** if pStmt is an SQL statement that does not return data (for ** example an UPDATE). +** +** INVARIANTS: +** +** {F13711} The [sqlite3_column_count(S)] interface returns the number of +** columns in the result set generated by the +** [prepared statement] S, or 0 if S does not generate +** a result set. */ SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Column Names In A Result Set {F13720} ** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() +** These routines return the name assigned to a particular column +** in the result set of a SELECT statement. The sqlite3_column_name() ** interface returns a pointer to a zero-terminated UTF8 string ** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. +** UTF16 string. The first parameter is the +** [prepared statement] that implements the SELECT statement. ** The second parameter is the column number. The left-most column is ** number 0. ** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] +** The returned string pointer is valid until either the +** [prepared statement] is destroyed by [sqlite3_finalize()] ** or until the next call sqlite3_column_name() or sqlite3_column_name16() ** on the same column. ** -** {F13725} If sqlite3_malloc() fails during the processing of either routine +** If sqlite3_malloc() fails during the processing of either routine ** (for example during a conversion from UTF-8 to UTF-16) then a ** NULL pointer is returned. +** +** The name of a result column is the value of the "AS" clause for +** that column, if there is an AS clause. If there is no AS clause +** then the name of the column is unspecified and may change from +** one release of SQLite to the next. +** +** INVARIANTS: +** +** {F13721} A successful invocation of the [sqlite3_column_name(S,N)] +** interface returns the name +** of the Nth column (where 0 is the left-most column) for the +** result set of [prepared statement] S as a +** zero-terminated UTF-8 string. +** +** {F13723} A successful invocation of the [sqlite3_column_name16(S,N)] +** interface returns the name +** of the Nth column (where 0 is the left-most column) for the +** result set of [prepared statement] S as a +** zero-terminated UTF-16 string in the native byte order. +** +** {F13724} The [sqlite3_column_name()] and [sqlite3_column_name16()] +** interfaces return a NULL pointer if they are unable to +** allocate memory memory to hold there normal return strings. +** +** {F13725} If the N parameter to [sqlite3_column_name(S,N)] or +** [sqlite3_column_name16(S,N)] is out of range, then the +** interfaces returns a NULL pointer. +** +** {F13726} The strings returned by [sqlite3_column_name(S,N)] and +** [sqlite3_column_name16(S,N)] are valid until the next +** call to either routine with the same S and N parameters +** or until [sqlite3_finalize(S)] is called. +** +** {F13727} When a result column of a [SELECT] statement contains +** an AS clause, the name of that column is the indentifier +** to the right of the AS keyword. */ SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N); SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); @@ -2323,32 +3109,32 @@ SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); /* ** CAPI3REF: Source Of Data In A Query Result {F13740} ** -** {F13741} These routines provide a means to determine what column of what +** These routines provide a means to determine what column of what ** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return +** The name of the database or table or column can be returned as +** either a UTF8 or UTF16 string. The _database_ routines return ** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} +** the origin_ routines return the column name. ** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using +** the [prepared statement] is destroyed using ** [sqlite3_finalize()] or until the same information is requested ** again in a different encoding. ** -** {F13745} The names returned are the original un-aliased names of the +** The names returned are the original un-aliased names of the ** database, table, and column. ** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by +** The first argument to the following calls is a [prepared statement]. +** These functions return information about the Nth column returned by ** the statement, where N is the second function argument. ** -** {F13748} If the Nth column returned by the statement is an expression +** If the Nth column returned by the statement is an expression ** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the +** return NULL. These routine might also return NULL if a memory +** allocation error occurs. Otherwise, they return the ** name of the attached database, table and column that query result ** column was extracted from. ** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return +** As with all other SQLite APIs, those postfixed with "16" return ** UTF-16 encoded strings, the other functions return UTF-8. {END} ** ** These APIs are only available if the library was compiled with the @@ -2358,6 +3144,67 @@ SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); ** If two or more threads call one or more of these routines against the same ** prepared statement and column at the same time then the results are ** undefined. +** +** INVARIANTS: +** +** {F13741} The [sqlite3_column_database_name(S,N)] interface returns either +** the UTF-8 zero-terminated name of the database from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13742} The [sqlite3_column_database_name16(S,N)] interface returns either +** the UTF-16 native byte order +** zero-terminated name of the database from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13743} The [sqlite3_column_table_name(S,N)] interface returns either +** the UTF-8 zero-terminated name of the table from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13744} The [sqlite3_column_table_name16(S,N)] interface returns either +** the UTF-16 native byte order +** zero-terminated name of the table from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13745} The [sqlite3_column_origin_name(S,N)] interface returns either +** the UTF-8 zero-terminated name of the table column from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13746} The [sqlite3_column_origin_name16(S,N)] interface returns either +** the UTF-16 native byte order +** zero-terminated name of the table column from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13748} The return values from +** [sqlite3_column_database_name|column metadata interfaces] +** are valid +** for the lifetime of the [prepared statement] +** or until the encoding is changed by another metadata +** interface call for the same prepared statement and column. +** +** LIMITATIONS: +** +** {U13751} If two or more threads call one or more +** [sqlite3_column_database_name|column metadata interfaces] +** the same [prepared statement] and result column +** at the same time then the results are undefined. */ SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int); SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int); @@ -2369,13 +3216,13 @@ SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); /* ** CAPI3REF: Declared Datatype Of A Query Result {F13760} ** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the +** The first parameter is a [prepared statement]. +** If this statement is a SELECT statement and the Nth column of the ** returned result set of that SELECT is a table column (not an ** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an +** column is returned. If the Nth column of the result set is an ** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} +** The returned string is always UTF-8 encoded. {END} ** For example, in the database schema: ** ** CREATE TABLE t1(c1 VARIANT); @@ -2394,14 +3241,36 @@ SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); ** strongly typed, but the typing is dynamic not static. Type ** is associated with individual values, not with the containers ** used to hold those values. -*/ -SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *, int i); +** +** INVARIANTS: +** +** {F13761} A successful call to [sqlite3_column_decltype(S,N)] +** returns a zero-terminated UTF-8 string containing the +** the declared datatype of the table column that appears +** as the Nth column (numbered from 0) of the result set to the +** [prepared statement] S. +** +** {F13762} A successful call to [sqlite3_column_decltype16(S,N)] +** returns a zero-terminated UTF-16 native byte order string +** containing the declared datatype of the table column that appears +** as the Nth column (numbered from 0) of the result set to the +** [prepared statement] S. +** +** {F13763} If N is less than 0 or N is greater than or equal to +** the number of columns in [prepared statement] S +** or if the Nth column of S is an expression or subquery rather +** than a table column or if a memory allocation failure +** occurs during encoding conversions, then +** calls to [sqlite3_column_decltype(S,N)] or +** [sqlite3_column_decltype16(S,N)] return NULL. +*/ +SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int); SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); /* ** CAPI3REF: Evaluate An SQL Statement {F13200} ** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call +** After an [prepared statement] has been prepared with a call ** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of ** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], ** then this function must be called one or more times to evaluate the @@ -2444,11 +3313,11 @@ SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** With the legacy interface, a more specific error code (example: ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) ** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, +** [prepared statement]. In the "v2" interface, ** the more specific error code is returned directly by sqlite3_step(). ** ** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has +** Perhaps it was called on a [prepared statement] that has ** already been [sqlite3_finalize | finalized] or on one that had ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could ** be the case that the same database connection is being used by two or @@ -2460,13 +3329,40 @@ SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] ** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or ** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. +** [error codes] that better describes the error. ** We admit that this is a goofy design. The problem has been fixed ** with the "v2" interface. If you prepare all of your SQL statements ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly +** more specific [error codes] are returned directly ** by sqlite3_step(). The use of the "v2" interface is recommended. +** +** INVARIANTS: +** +** {F13202} If [prepared statement] S is ready to be +** run, then [sqlite3_step(S)] advances that prepared statement +** until to completion or until it is ready to return another +** row of the result set or an interrupt or run-time error occurs. +** +** {F15304} When a call to [sqlite3_step(S)] causes the +** [prepared statement] S to run to completion, +** the function returns [SQLITE_DONE]. +** +** {F15306} When a call to [sqlite3_step(S)] stops because it is ready +** to return another row of the result set, it returns +** [SQLITE_ROW]. +** +** {F15308} If a call to [sqlite3_step(S)] encounters an +** [sqlite3_interrupt|interrupt] or a run-time error, +** it returns an appropraite error code that is not one of +** [SQLITE_OK], [SQLITE_ROW], or [SQLITE_DONE]. +** +** {F15310} If an [sqlite3_interrupt|interrupt] or run-time error +** occurs during a call to [sqlite3_step(S)] +** for a [prepared statement] S created using +** legacy interfaces [sqlite3_prepare()] or +** [sqlite3_prepare16()] then the function returns either +** [SQLITE_ERROR], [SQLITE_BUSY], or [SQLITE_MISUSE]. */ SQLITE_API int sqlite3_step(sqlite3_stmt*); @@ -2475,19 +3371,25 @@ SQLITE_API int sqlite3_step(sqlite3_stmt*); ** ** Return the number of values in the current row of the result set. ** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. +** INVARIANTS: +** +** {F13771} After a call to [sqlite3_step(S)] that returns +** [SQLITE_ROW], the [sqlite3_data_count(S)] routine +** will return the same value as the +** [sqlite3_column_count(S)] function. +** +** {F13772} After [sqlite3_step(S)] has returned any value other than +** [SQLITE_ROW] or before [sqlite3_step(S)] has been +** called on the [prepared statement] for +** the first time since it was [sqlite3_prepare|prepared] +** or [sqlite3_reset|reset], the [sqlite3_data_count(S)] +** routine returns zero. */ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Fundamental Datatypes {F10265} +** KEYWORDS: SQLITE_TEXT ** ** {F10266}Every value in SQLite has one of five fundamental datatypes: ** @@ -2520,10 +3422,12 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Results Values From A Query {F13800} ** +** These routines form the "result set query" interface. +** ** These routines return information about ** a single column of the current result row of a query. In every ** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being +** [prepared statement] that is being ** evaluated (the [sqlite3_stmt*] that was returned from ** [sqlite3_prepare_v2()] or one of its variants) and ** the second argument is the index of the column for which information @@ -2564,12 +3468,12 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); ** bytes in the string, not the number of characters. ** ** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return +** even empty strings, are always zero terminated. The return ** value from sqlite3_column_blob() for a zero-length blob is an arbitrary ** pointer, possibly even a NULL pointer. ** ** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. +** but leaves the result in UTF-16 in native byte order instead of UTF-8. ** The zero terminator is not included in this count. ** ** These routines attempt to convert the value where appropriate. For @@ -2661,6 +3565,61 @@ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); ** is either the integer 0, the floating point number 0.0, or a NULL ** pointer. Subsequent calls to [sqlite3_errcode()] will return ** [SQLITE_NOMEM]. +** +** INVARIANTS: +** +** {F13803} The [sqlite3_column_blob(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a blob and then returns a +** pointer to the converted value. +** +** {F13806} The [sqlite3_column_bytes(S,N)] interface returns the +** number of bytes in the blob or string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_column_blob(S,N)] or +** [sqlite3_column_text(S,N)]. +** +** {F13809} The [sqlite3_column_bytes16(S,N)] interface returns the +** number of bytes in the string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_column_text16(S,N)]. +** +** {F13812} The [sqlite3_column_double(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a floating point value and +** returns a copy of that value. +** +** {F13815} The [sqlite3_column_int(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a 64-bit signed integer and +** returns the lower 32 bits of that integer. +** +** {F13818} The [sqlite3_column_int64(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a 64-bit signed integer and +** returns a copy of that integer. +** +** {F13821} The [sqlite3_column_text(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a zero-terminated UTF-8 +** string and returns a pointer to that string. +** +** {F13824} The [sqlite3_column_text16(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a zero-terminated 2-byte +** aligned UTF-16 native byte order +** string and returns a pointer to that string. +** +** {F13827} The [sqlite3_column_type(S,N)] interface returns +** one of [SQLITE_NULL], [SQLITE_INTEGER], [SQLITE_FLOAT], +** [SQLITE_TEXT], or [SQLITE_BLOB] as appropriate for +** the Nth column in the current row of the result set for +** [prepared statement] S. +** +** {F13830} The [sqlite3_column_value(S,N)] interface returns a +** pointer to the [sqlite3_value] object that for the +** Nth column in the current row of the result set for +** [prepared statement] S. */ SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol); @@ -2677,19 +3636,29 @@ SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); ** CAPI3REF: Destroy A Prepared Statement Object {F13300} ** ** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was +** [prepared statement]. If the statement was ** executed successfully, or not executed at all, then SQLITE_OK is returned. ** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] +** [error code] or [extended error code] ** is returned. ** ** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not +** [prepared statement]. If the virtual machine has not ** completed execution when this routine is called, that is like ** encountering an error or an interrupt. (See [sqlite3_interrupt()].) ** Incomplete updates may be rolled back and transactions cancelled, ** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. +** [error code] returned will be [SQLITE_ABORT]. +** +** INVARIANTS: +** +** {F11302} The [sqlite3_finalize(S)] interface destroys the +** [prepared statement] S and releases all +** memory and file resources held by that object. +** +** {F11304} If the most recent call to [sqlite3_step(S)] for the +** [prepared statement] S returned an error, +** then [sqlite3_finalize(S)] returns that same error. */ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); @@ -2697,28 +3666,44 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); ** CAPI3REF: Reset A Prepared Statement Object {F13330} ** ** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. +** [prepared statement] object. ** back to its initial state, ready to be re-executed. ** Any SQL statement variables that had values bound to them using ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. ** Use [sqlite3_clear_bindings()] to reset the bindings. +** +** {F11332} The [sqlite3_reset(S)] interface resets the [prepared statement] S +** back to the beginning of its program. +** +** {F11334} If the most recent call to [sqlite3_step(S)] for +** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE], +** or if [sqlite3_step(S)] has never before been called on S, +** then [sqlite3_reset(S)] returns [SQLITE_OK]. +** +** {F11336} If the most recent call to [sqlite3_step(S)] for +** [prepared statement] S indicated an error, then +** [sqlite3_reset(S)] returns an appropriate [error code]. +** +** {F11338} The [sqlite3_reset(S)] interface does not change the values +** of any [sqlite3_bind_blob|bindings] on [prepared statement] S. */ SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt); /* ** CAPI3REF: Create Or Redefine SQL Functions {F16100} +** KEYWORDS: {function creation routines} ** -** The following two functions are used to add SQL functions or aggregates +** These two functions (collectively known as +** "function creation routines") are used to add SQL functions or aggregates ** or to redefine the behavior of existing SQL functions or aggregates. The ** difference only between the two is that the second parameter, the ** name of the (scalar) function or aggregate, is encoded in UTF-8 for ** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). ** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. +** The first parameter is the [database connection] to which the SQL +** function is to be added. If a single +** program uses more than one [database connection] internally, then SQL +** functions must be added individually to each [database connection]. ** ** The second parameter is the name of the SQL function to be created ** or redefined. @@ -2762,23 +3747,83 @@ SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt); ** arguments or differing perferred text encodings. SQLite will use ** the implementation most closely matches the way in which the ** SQL function is used. +** +** INVARIANTS: +** +** {F16103} The [sqlite3_create_function16()] interface behaves exactly +** like [sqlite3_create_function()] in every way except that it +** interprets the zFunctionName argument as +** zero-terminated UTF-16 native byte order instead of as a +** zero-terminated UTF-8. +** +** {F16106} A successful invocation of +** the [sqlite3_create_function(D,X,N,E,...)] interface registers +** or replaces callback functions in [database connection] D +** used to implement the SQL function named X with N parameters +** and having a perferred text encoding of E. +** +** {F16109} A successful call to [sqlite3_create_function(D,X,N,E,P,F,S,L)] +** replaces the P, F, S, and L values from any prior calls with +** the same D, X, N, and E values. +** +** {F16112} The [sqlite3_create_function(D,X,...)] interface fails with +** a return code of [SQLITE_ERROR] if the SQL function name X is +** longer than 255 bytes exclusive of the zero terminator. +** +** {F16118} Either F must be NULL and S and L are non-NULL or else F +** is non-NULL and S and L are NULL, otherwise +** [sqlite3_create_function(D,X,N,E,P,F,S,L)] returns [SQLITE_ERROR]. +** +** {F16121} The [sqlite3_create_function(D,...)] interface fails with an +** error code of [SQLITE_BUSY] if there exist [prepared statements] +** associated with the [database connection] D. +** +** {F16124} The [sqlite3_create_function(D,X,N,...)] interface fails with an +** error code of [SQLITE_ERROR] if parameter N (specifying the number +** of arguments to the SQL function being registered) is less +** than -1 or greater than 127. +** +** {F16127} When N is non-negative, the [sqlite3_create_function(D,X,N,...)] +** interface causes callbacks to be invoked for the SQL function +** named X when the number of arguments to the SQL function is +** exactly N. +** +** {F16130} When N is -1, the [sqlite3_create_function(D,X,N,...)] +** interface causes callbacks to be invoked for the SQL function +** named X with any number of arguments. +** +** {F16133} When calls to [sqlite3_create_function(D,X,N,...)] +** specify multiple implementations of the same function X +** and when one implementation has N>=0 and the other has N=(-1) +** the implementation with a non-zero N is preferred. +** +** {F16136} When calls to [sqlite3_create_function(D,X,N,E,...)] +** specify multiple implementations of the same function X with +** the same number of arguments N but with different +** encodings E, then the implementation where E matches the +** database encoding is preferred. +** +** {F16139} For an aggregate SQL function created using +** [sqlite3_create_function(D,X,N,E,P,0,S,L)] the finializer +** function L will always be invoked exactly once if the +** step function S is called one or more times. */ SQLITE_API int sqlite3_create_function( - sqlite3 *, + sqlite3 *db, const char *zFunctionName, int nArg, int eTextRep, - void*, + void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*) ); SQLITE_API int sqlite3_create_function16( - sqlite3*, + sqlite3 *db, const void *zFunctionName, int nArg, int eTextRep, - void*, + void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*) @@ -2858,6 +3903,68 @@ SQLITE_API int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlit ** interface, then these routines should be called from the same thread ** that ran [sqlite3_column_value()]. ** +** +** INVARIANTS: +** +** {F15103} The [sqlite3_value_blob(V)] interface converts the +** [sqlite3_value] object V into a blob and then returns a +** pointer to the converted value. +** +** {F15106} The [sqlite3_value_bytes(V)] interface returns the +** number of bytes in the blob or string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_value_blob(V)] or +** [sqlite3_value_text(V)]. +** +** {F15109} The [sqlite3_value_bytes16(V)] interface returns the +** number of bytes in the string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_value_text16(V)], +** [sqlite3_value_text16be(V)], or [sqlite3_value_text16le(V)]. +** +** {F15112} The [sqlite3_value_double(V)] interface converts the +** [sqlite3_value] object V into a floating point value and +** returns a copy of that value. +** +** {F15115} The [sqlite3_value_int(V)] interface converts the +** [sqlite3_value] object V into a 64-bit signed integer and +** returns the lower 32 bits of that integer. +** +** {F15118} The [sqlite3_value_int64(V)] interface converts the +** [sqlite3_value] object V into a 64-bit signed integer and +** returns a copy of that integer. +** +** {F15121} The [sqlite3_value_text(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated UTF-8 +** string and returns a pointer to that string. +** +** {F15124} The [sqlite3_value_text16(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated 2-byte +** aligned UTF-16 native byte order +** string and returns a pointer to that string. +** +** {F15127} The [sqlite3_value_text16be(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated 2-byte +** aligned UTF-16 big-endian +** string and returns a pointer to that string. +** +** {F15130} The [sqlite3_value_text16le(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated 2-byte +** aligned UTF-16 little-endian +** string and returns a pointer to that string. +** +** {F15133} The [sqlite3_value_type(V)] interface returns +** one of [SQLITE_NULL], [SQLITE_INTEGER], [SQLITE_FLOAT], +** [SQLITE_TEXT], or [SQLITE_BLOB] as appropriate for +** the [sqlite3_value] object V. +** +** {F15136} The [sqlite3_value_numeric_type(V)] interface converts +** the [sqlite3_value] object V into either an integer or +** a floating point value if it can do so without loss of +** information, and returns one of [SQLITE_NULL], +** [SQLITE_INTEGER], [SQLITE_FLOAT], [SQLITE_TEXT], or +** [SQLITE_BLOB] as appropriate for +** the [sqlite3_value] object V after the conversion attempt. */ SQLITE_API const void *sqlite3_value_blob(sqlite3_value*); SQLITE_API int sqlite3_value_bytes(sqlite3_value*); @@ -2877,16 +3984,16 @@ SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); ** ** The implementation of aggregate SQL functions use this routine to allocate ** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is +** The first time the sqlite3_aggregate_context() routine is ** is called for a particular aggregate, SQLite allocates nBytes of memory ** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} +** On second and subsequent calls to sqlite3_aggregate_context() +** for the same aggregate function index, the same buffer is returned. ** The implementation ** of the aggregate can use the returned buffer to accumulate data. ** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} +** SQLite automatically frees the allocated buffer when the aggregate +** query concludes. ** ** The first parameter should be a copy of the ** [sqlite3_context | SQL function context] that is the first @@ -2895,20 +4002,49 @@ SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); ** ** This routine must be called from the same thread in which ** the aggregate SQL function is running. +** +** INVARIANTS: +** +** {F16211} The first invocation of [sqlite3_aggregate_context(C,N)] for +** a particular instance of an aggregate function (for a particular +** context C) causes SQLite to allocation N bytes of memory, +** zero that memory, and return a pointer to the allocationed +** memory. +** +** {F16213} If a memory allocation error occurs during +** [sqlite3_aggregate_context(C,N)] then the function returns 0. +** +** {F16215} Second and subsequent invocations of +** [sqlite3_aggregate_context(C,N)] for the same context pointer C +** ignore the N parameter and return a pointer to the same +** block of memory returned by the first invocation. +** +** {F16217} The memory allocated by [sqlite3_aggregate_context(C,N)] is +** automatically freed on the next call to [sqlite3_reset()] +** or [sqlite3_finalize()] for the [prepared statement] containing +** the aggregate function associated with context C. */ SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); /* ** CAPI3REF: User Data For Functions {F16240} ** -** {F16241} The sqlite3_user_data() interface returns a copy of +** The sqlite3_user_data() interface returns a copy of ** the pointer that was the pUserData parameter (the 5th parameter) ** of the the [sqlite3_create_function()] ** and [sqlite3_create_function16()] routines that originally ** registered the application defined function. {END} ** -** {U16243} This routine must be called from the same thread in which +** This routine must be called from the same thread in which ** the application-defined function is running. +** +** INVARIANTS: +** +** {F16243} The [sqlite3_user_data(C)] interface returns a copy of the +** P pointer from the [sqlite3_create_function(D,X,N,E,P,F,S,L)] +** or [sqlite3_create_function16(D,X,N,E,P,F,S,L)] call that +** registered the SQL function associated with +** [sqlite3_context] C. */ SQLITE_API void *sqlite3_user_data(sqlite3_context*); @@ -2926,24 +4062,28 @@ SQLITE_API void *sqlite3_user_data(sqlite3_context*); ** invocations of the same function so that the original pattern string ** does not need to be recompiled on each invocation. ** -** {F16271} ** The sqlite3_get_auxdata() interface returns a pointer to the meta-data ** associated by the sqlite3_set_auxdata() function with the Nth argument ** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth +** If no meta-data has been ever been set for the Nth ** argument of the function, or if the cooresponding function parameter ** has changed since the meta-data was set, then sqlite3_get_auxdata() ** returns a NULL pointer. ** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data +** The sqlite3_set_auxdata() interface saves the meta-data ** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent +** argument of the application-defined function. Subsequent ** calls to sqlite3_get_auxdata() might return this data, if it has ** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor +** If it is not NULL, SQLite will invoke the destructor ** function given by the 4th parameter to sqlite3_set_auxdata() on ** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} +** or when the SQL statement completes, whichever comes first. +** +** SQLite is free to call the destructor and drop meta-data on +** any parameter of any function at any time. The only guarantee +** is that the destructor will be called before the metadata is +** dropped. ** ** In practice, meta-data is preserved between function calls for ** expressions that are constant at compile time. This includes literal @@ -2951,6 +4091,33 @@ SQLITE_API void *sqlite3_user_data(sqlite3_context*); ** ** These routines must be called from the same thread in which ** the SQL function is running. +** +** INVARIANTS: +** +** {F16272} The [sqlite3_get_auxdata(C,N)] interface returns a pointer +** to metadata associated with the Nth parameter of the SQL function +** whose context is C, or NULL if there is no metadata associated +** with that parameter. +** +** {F16274} The [sqlite3_set_auxdata(C,N,P,D)] interface assigns a metadata +** pointer P to the Nth parameter of the SQL function with context +** C. +** +** {F16276} SQLite will invoke the destructor D with a single argument +** which is the metadata pointer P following a call to +** [sqlite3_set_auxdata(C,N,P,D)] when SQLite ceases to hold +** the metadata. +** +** {F16277} SQLite ceases to hold metadata for an SQL function parameter +** when the value of that parameter changes. +** +** {F16278} When [sqlite3_set_auxdata(C,N,P,D)] is invoked, the destructor +** is called for any prior metadata associated with the same function +** context C and parameter N. +** +** {F16279} SQLite will call destructors for any metadata it is holding +** in a particular [prepared statement] S when either +** [sqlite3_reset(S)] or [sqlite3_finalize(S)] is called. */ SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N); SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); @@ -2989,90 +4156,188 @@ typedef void (*sqlite3_destructor_type)(void*); ** [sqlite3_bind_blob | sqlite3_bind_* documentation] for ** additional information. ** -** {F16402} The sqlite3_result_blob() interface sets the result from +** The sqlite3_result_blob() interface sets the result from ** an application defined function to be the BLOB whose content is pointed ** to by the second parameter and which is N bytes long where N is the ** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of +** The sqlite3_result_zeroblob() inerfaces set the result of ** the application defined function to be a BLOB containing all zero ** bytes and N bytes in size, where N is the value of the 2nd parameter. ** -** {F16407} The sqlite3_result_double() interface sets the result from +** The sqlite3_result_double() interface sets the result from ** an application defined function to be a floating point value specified ** by its 2nd argument. ** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions +** The sqlite3_result_error() and sqlite3_result_error16() functions ** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the +** SQLite uses the string pointed to by the ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite +** as the text of an error message. SQLite interprets the error +** message string from sqlite3_result_error() as UTF8. SQLite ** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() +** byte order. If the third parameter to sqlite3_result_error() ** or sqlite3_result_error16() is negative then SQLite takes as the error ** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or +** If the third parameter to sqlite3_result_error() or ** sqlite3_result_error16() is non-negative then SQLite takes that many ** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() +** The sqlite3_result_error() and sqlite3_result_error16() ** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or +** they return. Hence, the calling function can deallocate or ** modify the text after they return without harm. +** The sqlite3_result_error_code() function changes the error code +** returned by SQLite as a result of an error in a function. By default, +** the error code is SQLITE_ERROR. ** -** {F16421} The sqlite3_result_toobig() interface causes SQLite +** The sqlite3_result_toobig() interface causes SQLite ** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface +** to represent. The sqlite3_result_nomem() interface ** causes SQLite to throw an exception indicating that the a ** memory allocation failed. ** -** {F16431} The sqlite3_result_int() interface sets the return value +** The sqlite3_result_int() interface sets the return value ** of the application-defined function to be the 32-bit signed integer ** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value +** The sqlite3_result_int64() interface sets the return value ** of the application-defined function to be the 64-bit signed integer ** value given in the 2nd argument. ** -** {F16437} The sqlite3_result_null() interface sets the return value +** The sqlite3_result_null() interface sets the return value ** of the application-defined function to be NULL. ** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), +** The sqlite3_result_text(), sqlite3_result_text16(), ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces ** set the return value of the application-defined function to be ** a text string which is represented as UTF-8, UTF-16 native byte order, ** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from +** SQLite takes the text result from the application from ** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces +** If the 3rd parameter to the sqlite3_result_text* interfaces ** is negative, then SQLite takes result text from the 2nd parameter ** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces +** If the 3rd parameter to the sqlite3_result_text* interfaces ** is non-negative, then as many bytes (not characters) of the text ** pointed to by the 2nd parameter are taken as the application-defined ** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces +** If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that ** function as the destructor on the text or blob result when it has ** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces +** If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is the special constant SQLITE_STATIC, then ** SQLite assumes that the text or blob result is constant space and ** does not copy the space or call a destructor when it has ** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces +** If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT ** then SQLite makes a copy of the result into space obtained from ** from [sqlite3_malloc()] before it returns. ** -** {F16461} The sqlite3_result_value() interface sets the result of +** The sqlite3_result_value() interface sets the result of ** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The +** object specified by the 2nd parameter. The ** sqlite3_result_value() interface makes a copy of the [sqlite3_value] ** so that [sqlite3_value] specified in the parameter may change or ** be deallocated after sqlite3_result_value() returns without harm. ** -** {U16491} These routines are called from within the different thread +** If these routines are called from within the different thread ** than the one containing the application-defined function that recieved ** the [sqlite3_context] pointer, the results are undefined. +** +** INVARIANTS: +** +** {F16403} The default return value from any SQL function is NULL. +** +** {F16406} The [sqlite3_result_blob(C,V,N,D)] interface changes the +** return value of function C to be a blob that is N bytes +** in length and with content pointed to by V. +** +** {F16409} The [sqlite3_result_double(C,V)] interface changes the +** return value of function C to be the floating point value V. +** +** {F16412} The [sqlite3_result_error(C,V,N)] interface changes the return +** value of function C to be an exception with error code +** [SQLITE_ERROR] and a UTF8 error message copied from V up to the +** first zero byte or until N bytes are read if N is positive. +** +** {F16415} The [sqlite3_result_error16(C,V,N)] interface changes the return +** value of function C to be an exception with error code +** [SQLITE_ERROR] and a UTF16 native byte order error message +** copied from V up to the first zero terminator or until N bytes +** are read if N is positive. +** +** {F16418} The [sqlite3_result_error_toobig(C)] interface changes the return +** value of the function C to be an exception with error code +** [SQLITE_TOOBIG] and an appropriate error message. +** +** {F16421} The [sqlite3_result_error_nomem(C)] interface changes the return +** value of the function C to be an exception with error code +** [SQLITE_NOMEM] and an appropriate error message. +** +** {F16424} The [sqlite3_result_error_code(C,E)] interface changes the return +** value of the function C to be an exception with error code E. +** The error message text is unchanged. +** +** {F16427} The [sqlite3_result_int(C,V)] interface changes the +** return value of function C to be the 32-bit integer value V. +** +** {F16430} The [sqlite3_result_int64(C,V)] interface changes the +** return value of function C to be the 64-bit integer value V. +** +** {F16433} The [sqlite3_result_null(C)] interface changes the +** return value of function C to be NULL. +** +** {F16436} The [sqlite3_result_text(C,V,N,D)] interface changes the +** return value of function C to be the UTF8 string +** V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16439} The [sqlite3_result_text16(C,V,N,D)] interface changes the +** return value of function C to be the UTF16 native byte order +** string V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16442} The [sqlite3_result_text16be(C,V,N,D)] interface changes the +** return value of function C to be the UTF16 big-endian +** string V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16445} The [sqlite3_result_text16le(C,V,N,D)] interface changes the +** return value of function C to be the UTF16 little-endian +** string V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16448} The [sqlite3_result_value(C,V)] interface changes the +** return value of function C to be [sqlite3_value] object V. +** +** {F16451} The [sqlite3_result_zeroblob(C,N)] interface changes the +** return value of function C to be an N-byte blob of all zeros. +** +** {F16454} The [sqlite3_result_error()] and [sqlite3_result_error16()] +** interfaces make a copy of their error message strings before +** returning. +** +** {F16457} If the D destructor parameter to [sqlite3_result_blob(C,V,N,D)], +** [sqlite3_result_text(C,V,N,D)], [sqlite3_result_text16(C,V,N,D)], +** [sqlite3_result_text16be(C,V,N,D)], or +** [sqlite3_result_text16le(C,V,N,D)] is the constant [SQLITE_STATIC] +** then no destructor is ever called on the pointer V and SQLite +** assumes that V is immutable. +** +** {F16460} If the D destructor parameter to [sqlite3_result_blob(C,V,N,D)], +** [sqlite3_result_text(C,V,N,D)], [sqlite3_result_text16(C,V,N,D)], +** [sqlite3_result_text16be(C,V,N,D)], or +** [sqlite3_result_text16le(C,V,N,D)] is the constant +** [SQLITE_TRANSIENT] then the interfaces makes a copy of the +** content of V and retains the copy. +** +** {F16463} If the D destructor parameter to [sqlite3_result_blob(C,V,N,D)], +** [sqlite3_result_text(C,V,N,D)], [sqlite3_result_text16(C,V,N,D)], +** [sqlite3_result_text16be(C,V,N,D)], or +** [sqlite3_result_text16le(C,V,N,D)] is some value other than +** the constants [SQLITE_STATIC] and [SQLITE_TRANSIENT] then +** SQLite will invoke the destructor D with V as its only argument +** when it has finished with the V value. */ SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); SQLITE_API void sqlite3_result_double(sqlite3_context*, double); @@ -3080,6 +4345,7 @@ SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int); SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int); SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*); SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*); +SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int); SQLITE_API void sqlite3_result_int(sqlite3_context*, int); SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); SQLITE_API void sqlite3_result_null(sqlite3_context*); @@ -3093,52 +4359,95 @@ SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); /* ** CAPI3REF: Define New Collating Sequences {F16600} ** -** {F16601} ** These functions are used to add new collation sequences to the ** [sqlite3*] handle specified as the first argument. ** -** {F16602} ** The name of the new collation sequence is specified as a UTF-8 string ** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases +** and a UTF-16 string for sqlite3_create_collation16(). In all cases ** the name is passed as the second function argument. ** -** {F16604} ** The third argument may be one of the constants [SQLITE_UTF8], ** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied ** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The +** UTF-16 little-endian or UTF-16 big-endian respectively. The ** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that ** the routine expects pointers to 16-bit word aligned strings ** of UTF16 in the native byte order of the host computer. ** -** {F16607} ** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation +** argument. If it is NULL, this is the same as deleting the collation ** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application +** Each time the application ** supplied function is invoked, it is passed a copy of the void* passed as ** the fourth argument to sqlite3_create_collation() or ** sqlite3_create_collation16() as its first parameter. ** -** {F16612} ** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding +** each represented by a (length, data) pair and encoded in the encoding ** that was passed as the third argument when the collation sequence was ** registered. {END} The application defined collation routine should ** return negative, zero or positive if ** the first string is less than, equal to, or greater than the second ** string. i.e. (STRING1 - STRING2). ** -** {F16615} ** The sqlite3_create_collation_v2() works like sqlite3_create_collation() ** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is +** the collation. The destructor is called when the collation is ** destroyed and is passed a copy of the fourth parameter void* pointer ** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when +** Collations are destroyed when ** they are overridden by later calls to the collation creation functions ** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. +** +** INVARIANTS: +** +** {F16603} A successful call to the +** [sqlite3_create_collation_v2(B,X,E,P,F,D)] interface +** registers function F as the comparison function used to +** implement collation X on [database connection] B for +** databases having encoding E. +** +** {F16604} SQLite understands the X parameter to +** [sqlite3_create_collation_v2(B,X,E,P,F,D)] as a zero-terminated +** UTF-8 string in which case is ignored for ASCII characters and +** is significant for non-ASCII characters. +** +** {F16606} Successive calls to [sqlite3_create_collation_v2(B,X,E,P,F,D)] +** with the same values for B, X, and E, override prior values +** of P, F, and D. +** +** {F16609} The destructor D in [sqlite3_create_collation_v2(B,X,E,P,F,D)] +** is not NULL then it is called with argument P when the +** collating function is dropped by SQLite. +** +** {F16612} A collating function is dropped when it is overloaded. +** +** {F16615} A collating function is dropped when the database connection +** is closed using [sqlite3_close()]. +** +** {F16618} The pointer P in [sqlite3_create_collation_v2(B,X,E,P,F,D)] +** is passed through as the first parameter to the comparison +** function F for all subsequent invocations of F. +** +** {F16621} A call to [sqlite3_create_collation(B,X,E,P,F)] is exactly +** the same as a call to [sqlite3_create_collation_v2()] with +** the same parameters and a NULL destructor. +** +** {F16624} Following a [sqlite3_create_collation_v2(B,X,E,P,F,D)], +** SQLite uses the comparison function F for all text comparison +** operations on [database connection] B on text values that +** use the collating sequence name X. +** +** {F16627} The [sqlite3_create_collation16(B,X,E,P,F)] works the same +** as [sqlite3_create_collation(B,X,E,P,F)] except that the +** collation name X is understood as UTF-16 in native byte order +** instead of UTF-8. +** +** {F16630} When multiple comparison functions are available for the same +** collating sequence, SQLite chooses the one whose text encoding +** requires the least amount of conversion from the default +** text encoding of the database. */ SQLITE_API int sqlite3_create_collation( sqlite3*, @@ -3166,31 +4475,50 @@ SQLITE_API int sqlite3_create_collation16( /* ** CAPI3REF: Collation Needed Callbacks {F16700} ** -** {F16701} ** To avoid having to register all collation sequences before a database ** can be used, a single callback function may be registered with the ** database handle to be called whenever an undefined collation sequence is ** required. ** -** {F16702} ** If the function is registered using the sqlite3_collation_needed() API, ** then it is passed the names of undefined collation sequences as strings ** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either +** are passed as UTF-16 in machine native byte order. A call to either ** function replaces any existing callback. ** -** {F16705} When the callback is invoked, the first argument passed is a copy +** When the callback is invoked, the first argument passed is a copy ** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], +** sqlite3_collation_needed16(). The second argument is the database +** handle. The third argument is one of [SQLITE_UTF8], ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most ** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} +** The fourth parameter is the name of the +** required collation sequence. ** ** The callback function should register the desired collation using ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or ** [sqlite3_create_collation_v2()]. +** +** INVARIANTS: +** +** {F16702} A successful call to [sqlite3_collation_needed(D,P,F)] +** or [sqlite3_collation_needed16(D,P,F)] causes +** the [database connection] D to invoke callback F with first +** parameter P whenever it needs a comparison function for a +** collating sequence that it does not know about. +** +** {F16704} Each successful call to [sqlite3_collation_needed()] or +** [sqlite3_collation_needed16()] overrides the callback registered +** on the same [database connection] by prior calls to either +** interface. +** +** {F16706} The name of the requested collating function passed in the +** 4th parameter to the callback is in UTF-8 if the callback +** was registered using [sqlite3_collation_needed()] and +** is in UTF-16 native byte order if the callback was +** registered using [sqlite3_collation_needed16()]. +** +** */ SQLITE_API int sqlite3_collation_needed( sqlite3*, @@ -3231,17 +4559,28 @@ SQLITE_API int sqlite3_rekey( /* ** CAPI3REF: Suspend Execution For A Short Time {F10530} ** -** {F10531} The sqlite3_sleep() function +** The sqlite3_sleep() function ** causes the current thread to suspend execution ** for at least a number of milliseconds specified in its parameter. ** -** {F10532} If the operating system does not support sleep requests with +** If the operating system does not support sleep requests with ** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually +** the nearest second. The number of milliseconds of sleep actually ** requested from the operating system is returned. ** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} +** SQLite implements this interface by calling the xSleep() +** method of the default [sqlite3_vfs] object. +** +** INVARIANTS: +** +** {F10533} The [sqlite3_sleep(M)] interface invokes the xSleep +** method of the default [sqlite3_vfs|VFS] in order to +** suspend execution of the current thread for at least +** M milliseconds. +** +** {F10536} The [sqlite3_sleep(M)] interface returns the number of +** milliseconds of sleep actually requested of the operating +** system, which might be larger than the parameter M. */ SQLITE_API int sqlite3_sleep(int); @@ -3259,40 +4598,62 @@ SQLITE_API int sqlite3_sleep(int); ** as part of process initialization and before any SQLite interface ** routines have been call and remain unchanged thereafter. */ -SQLITE_EXTERN char *sqlite3_temp_directory; +SQLITE_API char *sqlite3_temp_directory; /* ** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} ** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or +** The sqlite3_get_autocommit() interfaces returns non-zero or ** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} +** respectively. Autocommit mode is on +** by default. Autocommit mode is disabled by a [BEGIN] statement. +** Autocommit mode is reenabled by a [COMMIT] or [ROLLBACK]. ** ** If certain kinds of errors occur on a statement within a multi-statement ** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to +** transaction might be rolled back automatically. The only way to ** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} +** an error is to use this function. +** +** INVARIANTS: ** +** {F12931} The [sqlite3_get_autocommit(D)] interface returns non-zero or +** zero if the [database connection] D is or is not in autocommit +** mode, respectively. +** +** {F12932} Autocommit mode is on by default. +** +** {F12933} Autocommit mode is disabled by a successful [BEGIN] statement. +** +** {F12934} Autocommit mode is enabled by a successful [COMMIT] or [ROLLBACK] +** statement. +** +** +** LIMITATIONS: +*** ** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} +** connection while this routine is running, then the return value +** is undefined. */ SQLITE_API int sqlite3_get_autocommit(sqlite3*); /* ** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} ** -** {F13121} The sqlite3_db_handle interface +** The sqlite3_db_handle interface ** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle +** [prepared statement] belongs. +** The database handle returned by sqlite3_db_handle ** is the same database handle that was ** the first argument to the [sqlite3_prepare_v2()] or its variants ** that was used to create the statement in the first place. +** +** INVARIANTS: +** +** {F13123} The [sqlite3_db_handle(S)] interface returns a pointer +** to the [database connection] associated with +** [prepared statement] S. */ SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*); @@ -3300,33 +4661,70 @@ SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*); /* ** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} ** -** {F12951} The sqlite3_commit_hook() interface registers a callback +** The sqlite3_commit_hook() interface registers a callback ** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() +** Any callback set by a previous call to sqlite3_commit_hook() ** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback +** The sqlite3_rollback_hook() interface registers a callback ** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() +** Any callback set by a previous call to sqlite3_commit_hook() ** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function +** The pArg argument is passed through +** to the callback. If the callback on a commit hook function ** returns non-zero, then the commit is converted into a rollback. ** -** {F12958} If another function was previously registered, its +** If another function was previously registered, its ** pArg value is returned. Otherwise NULL is returned. ** -** {F12959} Registering a NULL function disables the callback. +** Registering a NULL function disables the callback. ** -** {F12961} For the purposes of this API, a transaction is said to have been +** For the purposes of this API, a transaction is said to have been ** rolled back if an explicit "ROLLBACK" statement is executed, or ** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is +** The rollback callback is not invoked if a transaction is ** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is +** The rollback callback is not invoked if a transaction is ** rolled back because a commit callback returned non-zero. -** Check on this {END} +** Check on this ** ** These are experimental interfaces and are subject to change. +** +** INVARIANTS: +** +** {F12951} The [sqlite3_commit_hook(D,F,P)] interface registers the +** callback function F to be invoked with argument P whenever +** a transaction commits on [database connection] D. +** +** {F12952} The [sqlite3_commit_hook(D,F,P)] interface returns the P +** argument from the previous call with the same +** [database connection ] D , or NULL on the first call +** for a particular [database connection] D. +** +** {F12953} Each call to [sqlite3_commit_hook()] overwrites the callback +** registered by prior calls. +** +** {F12954} If the F argument to [sqlite3_commit_hook(D,F,P)] is NULL +** then the commit hook callback is cancelled and no callback +** is invoked when a transaction commits. +** +** {F12955} If the commit callback returns non-zero then the commit is +** converted into a rollback. +** +** {F12961} The [sqlite3_rollback_hook(D,F,P)] interface registers the +** callback function F to be invoked with argument P whenever +** a transaction rolls back on [database connection] D. +** +** {F12962} The [sqlite3_rollback_hook(D,F,P)] interface returns the P +** argument from the previous call with the same +** [database connection ] D , or NULL on the first call +** for a particular [database connection] D. +** +** {F12963} Each call to [sqlite3_rollback_hook()] overwrites the callback +** registered by prior calls. +** +** {F12964} If the F argument to [sqlite3_rollback_hook(D,F,P)] is NULL +** then the rollback hook callback is cancelled and no callback +** is invoked when a transaction rolls back. */ SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); @@ -3334,32 +4732,63 @@ SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); /* ** CAPI3REF: Data Change Notification Callbacks {F12970} ** -** {F12971} The sqlite3_update_hook() interface +** The sqlite3_update_hook() interface ** registers a callback function with the database connection identified by the ** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same +** Any callback set by a previous call to this function for the same ** database connection is overridden. ** -** {F12974} The second argument is a pointer to the function to invoke when a +** The second argument is a pointer to the function to invoke when a ** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is +** The first argument to the callback is ** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback +** The second callback ** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], ** depending on the operation that caused the callback to be invoked. -** {F12978} The third and +** The third and ** fourth arguments to the callback contain pointers to the database and ** table name containing the affected row. -** {F12979} The final callback parameter is +** The final callback parameter is ** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after +** In the case of an update, this is the rowid after ** the update takes place. ** -** {F12983} The update hook is not invoked when internal system tables are +** The update hook is not invoked when internal system tables are ** modified (i.e. sqlite_master and sqlite_sequence). ** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. +** If another function was previously registered, its pArg value +** is returned. Otherwise NULL is returned. +** +** INVARIANTS: +** +** {F12971} The [sqlite3_update_hook(D,F,P)] interface causes callback +** function F to be invoked with first parameter P whenever +** a table row is modified, inserted, or deleted on +** [database connection] D. +** +** {F12973} The [sqlite3_update_hook(D,F,P)] interface returns the value +** of P for the previous call on the same [database connection] D, +** or NULL for the first call. +** +** {F12975} If the update hook callback F in [sqlite3_update_hook(D,F,P)] +** is NULL then the no update callbacks are made. +** +** {F12977} Each call to [sqlite3_update_hook(D,F,P)] overrides prior calls +** to the same interface on the same [database connection] D. +** +** {F12979} The update hook callback is not invoked when internal system +** tables such as sqlite_master and sqlite_sequence are modified. +** +** {F12981} The second parameter to the update callback +** is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], +** depending on the operation that caused the callback to be invoked. +** +** {F12983} The third and fourth arguments to the callback contain pointers +** to zero-terminated UTF-8 strings which are the names of the +** database and table that is being updated. + +** {F12985} The final callback parameter is the rowid of the row after +** the change occurs. */ SQLITE_API void *sqlite3_update_hook( sqlite3*, @@ -3370,83 +4799,131 @@ SQLITE_API void *sqlite3_update_hook( /* ** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} ** -** {F10331} ** This routine enables or disables the sharing of the database cache ** and schema data structures between connections to the same database. -** {F10332} ** Sharing is enabled if the argument is true and disabled if the argument ** is false. ** -** {F10333} Cache sharing is enabled and disabled +** Cache sharing is enabled and disabled ** for an entire process. {END} This is a change as of SQLite version 3.5.0. ** In prior versions of SQLite, sharing was ** enabled or disabled for each thread separately. ** -** {F10334} ** The cache sharing mode set by this interface effects all subsequent ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} +** Existing database connections continue use the sharing mode +** that was in effect at the time they were opened. ** -** Virtual tables cannot be used with a shared cache. {F10336} When shared +** Virtual tables cannot be used with a shared cache. When shared ** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} +** virtual tables will always return an error. ** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} +** This routine returns [SQLITE_OK] if shared cache was +** enabled or disabled successfully. An [error code] +** is returned otherwise. ** -** {F10339} Shared cache is disabled by default. {END} But this might change in +** Shared cache is disabled by default. But this might change in ** future releases of SQLite. Applications that care about shared ** cache setting should set it explicitly. +** +** INVARIANTS: +** +** {F10331} A successful invocation of [sqlite3_enable_shared_cache(B)] +** will enable or disable shared cache mode for any subsequently +** created [database connection] in the same process. +** +** {F10336} When shared cache is enabled, the [sqlite3_create_module()] +** interface will always return an error. +** +** {F10337} The [sqlite3_enable_shared_cache(B)] interface returns +** [SQLITE_OK] if shared cache was enabled or disabled successfully. +** +** {F10339} Shared cache is disabled by default. */ SQLITE_API int sqlite3_enable_shared_cache(int); /* ** CAPI3REF: Attempt To Free Heap Memory {F17340} ** -** {F17341} The sqlite3_release_memory() interface attempts to +** The sqlite3_release_memory() interface attempts to ** free N bytes of heap memory by deallocating non-essential memory ** allocations held by the database labrary. {END} Memory used ** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns +** non-essential memory. Sqlite3_release_memory() returns ** the number of bytes actually freed, which might be more or less ** than the amount requested. +** +** INVARIANTS: +** +** {F17341} The [sqlite3_release_memory(N)] interface attempts to +** free N bytes of heap memory by deallocating non-essential +** memory allocations held by the database labrary. +** +** {F16342} The [sqlite3_release_memory(N)] returns the number +** of bytes actually freed, which might be more or less +** than the amount requested. */ SQLITE_API int sqlite3_release_memory(int); /* ** CAPI3REF: Impose A Limit On Heap Size {F17350} ** -** {F16351} The sqlite3_soft_heap_limit() interface +** The sqlite3_soft_heap_limit() interface ** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested +** by SQLite. If an internal allocation is requested ** that would exceed the soft heap limit, [sqlite3_release_memory()] is ** invoked one or more times to free up some space before the allocation -** is made. {END} +** is made. ** -** {F16353} The limit is called "soft", because if +** The limit is called "soft", because if ** [sqlite3_release_memory()] cannot ** free sufficient memory to prevent the limit from being exceeded, ** the memory is allocated anyway and the current operation proceeds. ** -** {F16354} ** A negative or zero value for N means that there is no soft heap limit and ** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. +** The default value for the soft heap limit is zero. ** ** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is +** But if the soft heap limit cannot honored, execution will +** continue without error or notification. This is why the limit is ** called a "soft" limit. It is advisory only. ** ** Prior to SQLite version 3.5.0, this routine only constrained the memory ** allocated by a single thread - the same thread in which this routine ** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In +** applied to all threads. The value specified for the soft heap limit +** is an upper bound on the total memory allocation for all threads. In ** version 3.5.0 there is no mechanism for limiting the heap usage for ** individual threads. +** +** INVARIANTS: +** +** {F16351} The [sqlite3_soft_heap_limit(N)] interface places a soft limit +** of N bytes on the amount of heap memory that may be allocated +** using [sqlite3_malloc()] or [sqlite3_realloc()] at any point +** in time. +** +** {F16352} If a call to [sqlite3_malloc()] or [sqlite3_realloc()] would +** cause the total amount of allocated memory to exceed the +** soft heap limit, then [sqlite3_release_memory()] is invoked +** in an attempt to reduce the memory usage prior to proceeding +** with the memory allocation attempt. +** +** {F16353} Calls to [sqlite3_malloc()] or [sqlite3_realloc()] that trigger +** attempts to reduce memory usage through the soft heap limit +** mechanism continue even if the attempt to reduce memory +** usage is unsuccessful. +** +** {F16354} A negative or zero value for N in a call to +** [sqlite3_soft_heap_limit(N)] means that there is no soft +** heap limit and [sqlite3_release_memory()] will only be +** called when memory is completely exhausted. +** +** {F16355} The default value for the soft heap limit is zero. +** +** {F16358} Each call to [sqlite3_soft_heap_limit(N)] overrides the +** values set by all prior calls. */ SQLITE_API void sqlite3_soft_heap_limit(int); @@ -3605,7 +5082,7 @@ SQLITE_API int sqlite3_auto_extension(void *xEntryPoint); ** ** {F12661} This function disables all previously registered ** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] +** routine undoes the effect of all prior [sqlite3_auto_extension()] ** calls. ** ** {F12662} This call disabled automatic extensions in all threads. {END} @@ -3636,6 +5113,9 @@ typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; typedef struct sqlite3_module sqlite3_module; /* +** CAPI3REF: Virtual Table Object {F18000} +** KEYWORDS: sqlite3_module +** ** A module is a class of virtual tables. Each module is defined ** by an instance of the following structure. This structure consists ** mostly of methods for the module. @@ -3672,6 +5152,9 @@ struct sqlite3_module { }; /* +** CAPI3REF: Virtual Table Indexing Information {F18100} +** KEYWORDS: sqlite3_index_info +** ** The sqlite3_index_info structure and its substructures is used to ** pass information into and receive the reply from the xBestIndex ** method of an sqlite3_module. The fields under **Inputs** are the @@ -3752,6 +5235,8 @@ struct sqlite3_index_info { #define SQLITE_INDEX_CONSTRAINT_MATCH 64 /* +** CAPI3REF: Register A Virtual Table Implementation {F18200} +** ** This routine is used to register a new module name with an SQLite ** connection. Module names must be registered before creating new ** virtual tables on the module, or before using preexisting virtual @@ -3765,6 +5250,8 @@ SQLITE_API int sqlite3_create_module( ); /* +** CAPI3REF: Register A Virtual Table Implementation {F18210} +** ** This routine is identical to the sqlite3_create_module() method above, ** except that it allows a destructor function to be specified. It is ** even more experimental than the rest of the virtual tables API. @@ -3778,6 +5265,9 @@ SQLITE_API int sqlite3_create_module_v2( ); /* +** CAPI3REF: Virtual Table Instance Object {F18010} +** KEYWORDS: sqlite3_vtab +** ** Every module implementation uses a subclass of the following structure ** to describe a particular instance of the module. Each subclass will ** be tailored to the specific needs of the module implementation. The @@ -3801,7 +5291,11 @@ struct sqlite3_vtab { /* Virtual table implementations will typically add additional fields */ }; -/* Every module implementation uses a subclass of the following structure +/* +** CAPI3REF: Virtual Table Cursor Object {F18020} +** KEYWORDS: sqlite3_vtab_cursor +** +** Every module implementation uses a subclass of the following structure ** to describe cursors that point into the virtual table and are used ** to loop through the virtual table. Cursors are created using the ** xOpen method of the module. Each module implementation will define @@ -3816,6 +5310,8 @@ struct sqlite3_vtab_cursor { }; /* +** CAPI3REF: Declare The Schema Of A Virtual Table {F18280} +** ** The xCreate and xConnect methods of a module use the following API ** to declare the format (the names and datatypes of the columns) of ** the virtual tables they implement. @@ -3823,6 +5319,8 @@ struct sqlite3_vtab_cursor { SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); /* +** CAPI3REF: Overload A Function For A Virtual Table {F18300} +** ** Virtual tables can provide alternative implementations of functions ** using the xFindFunction method. But global versions of those functions ** must exist in order to be overloaded. @@ -3855,8 +5353,9 @@ SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nA /* ** CAPI3REF: A Handle To An Open BLOB {F17800} ** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by +** An instance of this object represents an open BLOB on which +** incremental I/O can be preformed. +** Objects of this type are created by ** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. ** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces ** can be used to read or write small subsections of the blob. @@ -3868,7 +5367,7 @@ typedef struct sqlite3_blob sqlite3_blob; /* ** CAPI3REF: Open A BLOB For Incremental I/O {F17810} ** -** {F17811} This interfaces opens a handle to the blob located +** This interfaces opens a handle to the blob located ** in row iRow,, column zColumn, table zTable in database zDb; ** in other words, the same blob that would be selected by: ** @@ -3876,18 +5375,38 @@ typedef struct sqlite3_blob sqlite3_blob; ** SELECT zColumn FROM zDb.zTable WHERE rowid = iRow; ** {END} ** -** {F17812} If the flags parameter is non-zero, the blob is opened for +** If the flags parameter is non-zero, the blob is opened for ** read and write access. If it is zero, the blob is opened for read -** access. {END} +** access. ** -** {F17813} On success, [SQLITE_OK] is returned and the new +** On success, [SQLITE_OK] is returned and the new ** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and +** Otherwise an error code is returned and ** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message +** This function sets the database-handle error code and message ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement +** +** INVARIANTS: +** +** {F17813} A successful invocation of the [sqlite3_blob_open(D,B,T,C,R,F,P)] +** interface opens an [sqlite3_blob] object P on the blob +** in column C of table T in database B on [database connection] D. +** +** {F17814} A successful invocation of [sqlite3_blob_open(D,...)] starts +** a new transaction on [database connection] D if that connection +** is not already in a transaction. +** +** {F17816} The [sqlite3_blob_open(D,B,T,C,R,F,P)] interface opens the blob +** for read and write access if and only if the F parameter +** is non-zero. +** +** {F17819} The [sqlite3_blob_open()] interface returns [SQLITE_OK] on +** success and an appropriate [error code] on failure. +** +** {F17821} If an error occurs during evaluation of [sqlite3_blob_open(D,...)] +** then subsequent calls to [sqlite3_errcode(D)], +** [sqlite3_errmsg(D)], and [sqlite3_errmsg16(D)] will return +** information approprate for that error. */ SQLITE_API int sqlite3_blob_open( sqlite3*, @@ -3904,26 +5423,50 @@ SQLITE_API int sqlite3_blob_open( ** ** Close an open [sqlite3_blob | blob handle]. ** -** {F17831} Closing a BLOB shall cause the current transaction to commit +** Closing a BLOB shall cause the current transaction to commit ** if there are no other BLOBs, no pending prepared statements, and the ** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache +** If any writes were made to the BLOB, they might be held in cache ** until the close operation if they will fit. {END} ** Closing the BLOB often forces the changes ** out to disk and so if any I/O errors occur, they will likely occur ** at the time when the BLOB is closed. {F17833} Any errors that occur during ** closing are reported as a non-zero return value. ** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns +** The BLOB is closed unconditionally. Even if this routine returns ** an error code, the BLOB is still closed. +** +** INVARIANTS: +** +** {F17833} The [sqlite3_blob_close(P)] interface closes an +** [sqlite3_blob] object P previously opened using +** [sqlite3_blob_open()]. +** +** {F17836} Closing an [sqlite3_blob] object using +** [sqlite3_blob_close()] shall cause the current transaction to +** commit if there are no other open [sqlite3_blob] objects +** or [prepared statements] on the same [database connection] and +** the [database connection] is in +** [sqlite3_get_autocommit | autocommit mode]. +** +** {F17839} The [sqlite3_blob_close(P)] interfaces closes the +** [sqlite3_blob] object P unconditionally, even if +** [sqlite3_blob_close(P)] returns something other than [SQLITE_OK]. +** */ SQLITE_API int sqlite3_blob_close(sqlite3_blob *); /* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} +** CAPI3REF: Return The Size Of An Open BLOB {F17840} +** +** Return the size in bytes of the blob accessible via the open +** [sqlite3_blob] object in its only argument. +** +** INVARIANTS: ** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. +** {F17843} The [sqlite3_blob_bytes(P)] interface returns the size +** in bytes of the BLOB that the [sqlite3_blob] object P +** refers to. */ SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *); @@ -3932,40 +5475,96 @@ SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *); ** ** This function is used to read data from an open ** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. +** N bytes of data are copied into buffer +** Z from the open blob, starting at offset iOffset. ** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is +** If offset iOffset is less than N bytes from the end of the blob, +** [SQLITE_ERROR] is returned and no data is read. If N or iOffset is ** less than zero [SQLITE_ERROR] is returned and no data is read. ** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** On success, SQLITE_OK is returned. Otherwise, an +** [error code] or an [extended error code] is returned. +** +** INVARIANTS: +** +** {F17853} The [sqlite3_blob_read(P,Z,N,X)] interface reads N bytes +** beginning at offset X from +** the blob that [sqlite3_blob] object P refers to +** and writes those N bytes into buffer Z. +** +** {F17856} In [sqlite3_blob_read(P,Z,N,X)] if the size of the blob +** is less than N+X bytes, then the function returns [SQLITE_ERROR] +** and nothing is read from the blob. +** +** {F17859} In [sqlite3_blob_read(P,Z,N,X)] if X or N is less than zero +** then the function returns [SQLITE_ERROR] +** and nothing is read from the blob. +** +** {F17862} The [sqlite3_blob_read(P,Z,N,X)] interface returns [SQLITE_OK] +** if N bytes where successfully read into buffer Z. +** +** {F17865} If the requested read could not be completed, +** the [sqlite3_blob_read(P,Z,N,X)] interface returns an +** appropriate [error code] or [extended error code]. +** +** {F17868} If an error occurs during evaluation of [sqlite3_blob_read(D,...)] +** then subsequent calls to [sqlite3_errcode(D)], +** [sqlite3_errmsg(D)], and [sqlite3_errmsg16(D)] will return +** information approprate for that error. */ -SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); +SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); /* ** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} ** ** This function is used to write data into an open ** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer +** n bytes of data are copied from the buffer ** pointed to by z into the open blob, starting at offset iOffset. ** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument +** If the [sqlite3_blob | blob-handle] passed as the first argument ** was not opened for writing (the flags parameter to [sqlite3_blob_open()] *** was zero), this function returns [SQLITE_READONLY]. ** -** {F17873} This function may only modify the contents of the blob; it is +** This function may only modify the contents of the blob; it is ** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is +** If offset iOffset is less than n bytes from the end of the blob, +** [SQLITE_ERROR] is returned and no data is written. If n is ** less than zero [SQLITE_ERROR] is returned and no data is written. ** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** On success, SQLITE_OK is returned. Otherwise, an +** [error code] or an [extended error code] is returned. +** +** INVARIANTS: +** +** {F17873} The [sqlite3_blob_write(P,Z,N,X)] interface writes N bytes +** from buffer Z into +** the blob that [sqlite3_blob] object P refers to +** beginning at an offset of X into the blob. +** +** {F17875} The [sqlite3_blob_write(P,Z,N,X)] interface returns +** [SQLITE_READONLY] if the [sqlite3_blob] object P was +** [sqlite3_blob_open | opened] for reading only. +** +** {F17876} In [sqlite3_blob_write(P,Z,N,X)] if the size of the blob +** is less than N+X bytes, then the function returns [SQLITE_ERROR] +** and nothing is written into the blob. +** +** {F17879} In [sqlite3_blob_write(P,Z,N,X)] if X or N is less than zero +** then the function returns [SQLITE_ERROR] +** and nothing is written into the blob. +** +** {F17882} The [sqlite3_blob_write(P,Z,N,X)] interface returns [SQLITE_OK] +** if N bytes where successfully written into blob. +** +** {F17885} If the requested write could not be completed, +** the [sqlite3_blob_write(P,Z,N,X)] interface returns an +** appropriate [error code] or [extended error code]. +** +** {F17888} If an error occurs during evaluation of [sqlite3_blob_write(D,...)] +** then subsequent calls to [sqlite3_errcode(D)], +** [sqlite3_errmsg(D)], and [sqlite3_errmsg16(D)] will return +** information approprate for that error. */ SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); @@ -3974,30 +5573,57 @@ SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOff ** ** A virtual filesystem (VFS) is an [sqlite3_vfs] object ** that SQLite uses to interact -** with the underlying operating system. Most builds come with a +** with the underlying operating system. Most SQLite builds come with a ** single default VFS that is appropriate for the host computer. ** New VFSes can be registered and existing VFSes can be unregistered. ** The following interfaces are provided. ** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a +** The sqlite3_vfs_find() interface returns a pointer to +** a VFS given its name. Names are case sensitive. +** Names are zero-terminated UTF-8 strings. +** If there is no match, a NULL +** pointer is returned. If zVfsName is NULL then the default +** VFS is returned. +** +** New VFSes are registered with sqlite3_vfs_register(). +** Each new VFS becomes the default VFS if the makeDflt flag is set. +** The same VFS can be registered multiple times without injury. +** To make an existing VFS into the default VFS, register it again +** with the makeDflt flag set. If two different VFSes with the +** same name are registered, the behavior is undefined. If a ** VFS is registered with a name that is NULL or an empty string, ** then the behavior is undefined. ** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as +** Unregister a VFS with the sqlite3_vfs_unregister() interface. +** If the default VFS is unregistered, another VFS is chosen as ** the default. The choice for the new VFS is arbitrary. +** +** INVARIANTS: +** +** {F11203} The [sqlite3_vfs_find(N)] interface returns a pointer to the +** registered [sqlite3_vfs] object whose name exactly matches +** the zero-terminated UTF-8 string N, or it returns NULL if +** there is no match. +** +** {F11206} If the N parameter to [sqlite3_vfs_find(N)] is NULL then +** the function returns a pointer to the default [sqlite3_vfs] +** object if there is one, or NULL if there is no default +** [sqlite3_vfs] object. +** +** {F11209} The [sqlite3_vfs_register(P,F)] interface registers the +** well-formed [sqlite3_vfs] object P using the name given +** by the zName field of the object. +** +** {F11212} Using the [sqlite3_vfs_register(P,F)] interface to register +** the same [sqlite3_vfs] object multiple times is a harmless no-op. +** +** {F11215} The [sqlite3_vfs_register(P,F)] interface makes the +** the [sqlite3_vfs] object P the default [sqlite3_vfs] object +** if F is non-zero. +** +** {F11218} The [sqlite3_vfs_unregister(P)] interface unregisters the +** [sqlite3_vfs] object P so that it is no longer returned by +** subsequent calls to [sqlite3_vfs_find()]. */ SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); @@ -4192,6 +5818,45 @@ SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); /* +** CAPI3REF: Testing Interface {F11400} +** +** The sqlite3_test_control() interface is used to read out internal +** state of SQLite and to inject faults into SQLite for testing +** purposes. The first parameter a operation code that determines +** the number, meaning, and operation of all subsequent parameters. +** +** This interface is not for use by applications. It exists solely +** for verifying the correct operation of the SQLite library. Depending +** on how the SQLite library is compiled, this interface might not exist. +** +** The details of the operation codes, their meanings, the parameters +** they take, and what they do are all subject to change without notice. +** Unlike most of the SQLite API, this function is not guaranteed to +** operate consistently from one release to the next. +*/ +SQLITE_API int sqlite3_test_control(int op, ...); + +/* +** CAPI3REF: Testing Interface Operation Codes {F11410} +** +** These constants are the valid operation code parameters used +** as the first argument to [sqlite3_test_control()]. +** +** These parameters and their meansing are subject to change +** without notice. These values are for testing purposes only. +** Applications should not use any of these parameters or the +** [sqlite3_test_control()] interface. +*/ +#define SQLITE_TESTCTRL_FAULT_CONFIG 1 +#define SQLITE_TESTCTRL_FAULT_FAILURES 2 +#define SQLITE_TESTCTRL_FAULT_BENIGN_FAILURES 3 +#define SQLITE_TESTCTRL_FAULT_PENDING 4 + + + + + +/* ** Undo the hack that converts floating point types to integer for ** builds on processors without floating point support. */ @@ -4574,19 +6239,39 @@ SQLITE_PRIVATE void sqlite3HashClear(Hash*); ** cc '-DUINTPTR_TYPE=long long int' ... */ #ifndef UINT32_TYPE -# define UINT32_TYPE unsigned int +# ifdef HAVE_UINT32_T +# define UINT32_TYPE uint32_t +# else +# define UINT32_TYPE unsigned int +# endif #endif #ifndef UINT16_TYPE -# define UINT16_TYPE unsigned short int +# ifdef HAVE_UINT16_T +# define UINT16_TYPE uint16_t +# else +# define UINT16_TYPE unsigned short int +# endif #endif #ifndef INT16_TYPE -# define INT16_TYPE short int +# ifdef HAVE_INT16_T +# define INT16_TYPE int16_t +# else +# define INT16_TYPE short int +# endif #endif #ifndef UINT8_TYPE -# define UINT8_TYPE unsigned char +# ifdef HAVE_UINT8_T +# define UINT8_TYPE uint8_t +# else +# define UINT8_TYPE unsigned char +# endif #endif #ifndef INT8_TYPE -# define INT8_TYPE signed char +# ifdef HAVE_INT8_T +# define INT8_TYPE int8_t +# else +# define INT8_TYPE signed char +# endif #endif #ifndef LONGDOUBLE_TYPE # define LONGDOUBLE_TYPE long double @@ -4604,7 +6289,7 @@ typedef UINT8_TYPE i8; /* 1-byte signed integer */ ** evaluated at runtime. */ #ifdef SQLITE_AMALGAMATION -SQLITE_PRIVATE const int sqlite3One; +SQLITE_PRIVATE const int sqlite3one; #else SQLITE_PRIVATE const int sqlite3one; #endif @@ -4635,8 +6320,66 @@ struct BusyHandler { }; /* +** Name of the master database table. The master database table +** is a special table that holds the names and attributes of all +** user tables and indices. +*/ +#define MASTER_NAME "sqlite_master" +#define TEMP_MASTER_NAME "sqlite_temp_master" + +/* +** The root-page of the master database table. +*/ +#define MASTER_ROOT 1 + +/* +** The name of the schema table. +*/ +#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) + +/* +** A convenience macro that returns the number of elements in +** an array. +*/ +#define ArraySize(X) (sizeof(X)/sizeof(X[0])) + +/* +** Forward references to structures +*/ +typedef struct AggInfo AggInfo; +typedef struct AuthContext AuthContext; +typedef struct Bitvec Bitvec; +typedef struct CollSeq CollSeq; +typedef struct Column Column; +typedef struct Db Db; +typedef struct Schema Schema; +typedef struct Expr Expr; +typedef struct ExprList ExprList; +typedef struct FKey FKey; +typedef struct FuncDef FuncDef; +typedef struct IdList IdList; +typedef struct Index Index; +typedef struct KeyClass KeyClass; +typedef struct KeyInfo KeyInfo; +typedef struct Module Module; +typedef struct NameContext NameContext; +typedef struct Parse Parse; +typedef struct Select Select; +typedef struct SrcList SrcList; +typedef struct StrAccum StrAccum; +typedef struct Table Table; +typedef struct TableLock TableLock; +typedef struct Token Token; +typedef struct TriggerStack TriggerStack; +typedef struct TriggerStep TriggerStep; +typedef struct Trigger Trigger; +typedef struct WhereInfo WhereInfo; +typedef struct WhereLevel WhereLevel; + +/* ** Defer sourcing vdbe.h and btree.h until after the "u8" and -** "BusyHandler typedefs. +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque +** pointer types (i.e. FuncDef) defined above. */ /************** Include btree.h in the middle of sqliteInt.h *****************/ /************** Begin file btree.h *******************************************/ @@ -4865,7 +6608,7 @@ SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*); ** or VDBE. The VDBE implements an abstract machine that runs a ** simple program to access and modify the underlying database. ** -** $Id: vdbe.h,v 1.115 2007/11/14 06:48:48 danielk1977 Exp $ +** $Id: vdbe.h,v 1.125 2008/01/17 17:27:31 drh Exp $ */ #ifndef _SQLITE_VDBE_H_ #define _SQLITE_VDBE_H_ @@ -4878,16 +6621,41 @@ SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*); typedef struct Vdbe Vdbe; /* +** The names of the following types declared in vdbeInt.h are required +** for the VdbeOp definition. +*/ +typedef struct VdbeFunc VdbeFunc; +typedef struct Mem Mem; + +/* ** A single instruction of the virtual machine has an opcode ** and as many as three operands. The instruction is recorded ** as an instance of the following structure: */ struct VdbeOp { u8 opcode; /* What operation to perform */ + signed char p4type; /* One of the P4_xxx constants for p4 */ + u8 flags; /* Flags for internal use */ + u8 p5; /* Fifth parameter is an unsigned character */ int p1; /* First operand */ int p2; /* Second parameter (often the jump destination) */ - char *p3; /* Third parameter */ - int p3type; /* One of the P3_xxx constants defined below */ + int p3; /* The third parameter */ + union { /* forth parameter */ + int i; /* Integer value if p4type==P4_INT32 */ + void *p; /* Generic pointer */ + char *z; /* Pointer to data for string (char array) types */ + i64 *pI64; /* Used when p4type is P4_INT64 */ + double *pReal; /* Used when p4type is P4_REAL */ + FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */ + VdbeFunc *pVdbeFunc; /* Used when p4type is P4_VDBEFUNC */ + CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ + Mem *pMem; /* Used when p4type is P4_MEM */ + sqlite3_vtab *pVtab; /* Used when p4type is P4_VTAB */ + KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ + } p4; +#ifdef SQLITE_DEBUG + char *zComment; /* Comment to improve readability */ +#endif #ifdef VDBE_PROFILE int cnt; /* Number of times this instruction was executed */ long long cycles; /* Total time spend executing this instruction */ @@ -4902,36 +6670,37 @@ typedef struct VdbeOp VdbeOp; struct VdbeOpList { u8 opcode; /* What operation to perform */ signed char p1; /* First operand */ - short int p2; /* Second parameter (often the jump destination) */ - char *p3; /* Third parameter */ + signed char p2; /* Second parameter (often the jump destination) */ + signed char p3; /* Third parameter */ }; typedef struct VdbeOpList VdbeOpList; /* ** Allowed values of VdbeOp.p3type */ -#define P3_NOTUSED 0 /* The P3 parameter is not used */ -#define P3_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ -#define P3_STATIC (-2) /* Pointer to a static string */ -#define P3_COLLSEQ (-4) /* P3 is a pointer to a CollSeq structure */ -#define P3_FUNCDEF (-5) /* P3 is a pointer to a FuncDef structure */ -#define P3_KEYINFO (-6) /* P3 is a pointer to a KeyInfo structure */ -#define P3_VDBEFUNC (-7) /* P3 is a pointer to a VdbeFunc structure */ -#define P3_MEM (-8) /* P3 is a pointer to a Mem* structure */ -#define P3_TRANSIENT (-9) /* P3 is a pointer to a transient string */ -#define P3_VTAB (-10) /* P3 is a pointer to an sqlite3_vtab structure */ -#define P3_MPRINTF (-11) /* P3 is a string obtained from sqlite3_mprintf() */ -#define P3_REAL (-12) /* P3 is a 64-bit floating point value */ -#define P3_INT64 (-13) /* P3 is a 64-bit signed integer */ - -/* When adding a P3 argument using P3_KEYINFO, a copy of the KeyInfo structure +#define P4_NOTUSED 0 /* The P4 parameter is not used */ +#define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ +#define P4_STATIC (-2) /* Pointer to a static string */ +#define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */ +#define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */ +#define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */ +#define P4_VDBEFUNC (-7) /* P4 is a pointer to a VdbeFunc structure */ +#define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ +#define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */ +#define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */ +#define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */ +#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ +#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ +#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ + +/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure ** is made. That copy is freed when the Vdbe is finalized. But if the -** argument is P3_KEYINFO_HANDOFF, the passed in pointer is used. It still +** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still ** gets freed when the Vdbe is finalized so it still should be obtained ** from a single sqliteMalloc(). But no copy is made and the calling ** function should *not* try to free the KeyInfo. */ -#define P3_KEYINFO_HANDOFF (-9) +#define P4_KEYINFO_HANDOFF (-9) /* ** The Vdbe.aColName array contains 5n Mem structures, where n is the @@ -4960,41 +6729,40 @@ typedef struct VdbeOpList VdbeOpList; /************** Begin file opcodes.h *****************************************/ /* Automatically generated. Do not edit */ /* See the mkopcodeh.awk script for details */ -#define OP_MemLoad 1 -#define OP_VNext 2 -#define OP_HexBlob 126 /* same as TK_BLOB */ -#define OP_Column 3 -#define OP_SetCookie 4 -#define OP_IfMemPos 5 +#define OP_VNext 1 +#define OP_Column 2 +#define OP_SetCookie 3 #define OP_Real 125 /* same as TK_FLOAT */ -#define OP_Sequence 6 -#define OP_MoveGt 7 +#define OP_Sequence 4 +#define OP_MoveGt 5 #define OP_Ge 72 /* same as TK_GE */ -#define OP_RowKey 8 +#define OP_RowKey 6 +#define OP_SCopy 7 #define OP_Eq 68 /* same as TK_EQ */ -#define OP_OpenWrite 9 +#define OP_OpenWrite 8 #define OP_NotNull 66 /* same as TK_NOTNULL */ -#define OP_If 10 +#define OP_If 9 #define OP_ToInt 141 /* same as TK_TO_INT */ #define OP_String8 88 /* same as TK_STRING */ -#define OP_Pop 11 -#define OP_VRowid 12 -#define OP_CollSeq 13 -#define OP_OpenRead 14 -#define OP_Expire 15 -#define OP_AutoCommit 17 +#define OP_VRowid 10 +#define OP_CollSeq 11 +#define OP_OpenRead 12 +#define OP_Expire 13 +#define OP_AutoCommit 14 #define OP_Gt 69 /* same as TK_GT */ -#define OP_IntegrityCk 18 -#define OP_Sort 19 +#define OP_IntegrityCk 15 +#define OP_Sort 17 +#define OP_Copy 18 +#define OP_Trace 19 #define OP_Function 20 +#define OP_IfNeg 21 #define OP_And 61 /* same as TK_AND */ #define OP_Subtract 79 /* same as TK_MINUS */ -#define OP_Noop 21 -#define OP_Return 22 +#define OP_Noop 22 +#define OP_Return 23 #define OP_Remainder 82 /* same as TK_REM */ -#define OP_NewRowid 23 +#define OP_NewRowid 24 #define OP_Multiply 80 /* same as TK_STAR */ -#define OP_IfMemNeg 24 #define OP_Variable 25 #define OP_String 26 #define OP_RealAffinity 27 @@ -5014,108 +6782,128 @@ typedef struct VdbeOpList VdbeOpList; #define OP_Statement 41 #define OP_RowData 42 #define OP_MemMax 43 -#define OP_Push 44 #define OP_Or 60 /* same as TK_OR */ -#define OP_NotExists 45 -#define OP_MemIncr 46 -#define OP_Gosub 47 +#define OP_NotExists 44 +#define OP_Gosub 45 #define OP_Divide 81 /* same as TK_SLASH */ -#define OP_Integer 48 +#define OP_Integer 46 #define OP_ToNumeric 140 /* same as TK_TO_NUMERIC*/ -#define OP_MemInt 49 -#define OP_Prev 50 +#define OP_Prev 47 #define OP_Concat 83 /* same as TK_CONCAT */ #define OP_BitAnd 74 /* same as TK_BITAND */ -#define OP_VColumn 51 -#define OP_CreateTable 52 -#define OP_Last 53 +#define OP_VColumn 48 +#define OP_CreateTable 49 +#define OP_Last 50 #define OP_IsNull 65 /* same as TK_ISNULL */ -#define OP_IncrVacuum 54 -#define OP_IdxRowid 55 -#define OP_MakeIdxRec 56 +#define OP_IncrVacuum 51 +#define OP_IdxRowid 52 #define OP_ShiftRight 77 /* same as TK_RSHIFT */ -#define OP_ResetCount 57 -#define OP_FifoWrite 58 -#define OP_Callback 59 -#define OP_ContextPush 62 -#define OP_DropTrigger 63 -#define OP_DropIndex 64 -#define OP_IdxGE 73 -#define OP_IdxDelete 84 -#define OP_Vacuum 86 -#define OP_MoveLe 89 -#define OP_IfNot 90 -#define OP_DropTable 91 -#define OP_MakeRecord 92 +#define OP_ResetCount 53 +#define OP_FifoWrite 54 +#define OP_ContextPush 55 +#define OP_DropTrigger 56 +#define OP_DropIndex 57 +#define OP_IdxGE 58 +#define OP_IdxDelete 59 +#define OP_Vacuum 62 +#define OP_MoveLe 63 +#define OP_IfNot 64 +#define OP_DropTable 73 +#define OP_MakeRecord 84 #define OP_ToBlob 139 /* same as TK_TO_BLOB */ -#define OP_Delete 93 -#define OP_StackDepth 94 -#define OP_AggFinal 95 +#define OP_ResultRow 85 +#define OP_Delete 86 +#define OP_AggFinal 89 #define OP_ShiftLeft 76 /* same as TK_LSHIFT */ -#define OP_Dup 96 -#define OP_Goto 97 -#define OP_TableLock 98 -#define OP_FifoRead 99 -#define OP_Clear 100 -#define OP_IdxGT 101 -#define OP_MoveLt 102 +#define OP_Goto 90 +#define OP_TableLock 91 +#define OP_FifoRead 92 +#define OP_Clear 93 +#define OP_MoveLt 94 #define OP_Le 70 /* same as TK_LE */ -#define OP_VerifyCookie 103 -#define OP_AggStep 104 -#define OP_Pull 105 +#define OP_VerifyCookie 95 +#define OP_AggStep 96 #define OP_ToText 138 /* same as TK_TO_TEXT */ #define OP_Not 16 /* same as TK_NOT */ #define OP_ToReal 142 /* same as TK_TO_REAL */ -#define OP_SetNumColumns 106 -#define OP_AbsValue 107 -#define OP_Transaction 108 -#define OP_VFilter 109 -#define OP_Negative 85 /* same as TK_UMINUS */ +#define OP_SetNumColumns 97 +#define OP_Transaction 98 +#define OP_VFilter 99 #define OP_Ne 67 /* same as TK_NE */ -#define OP_VDestroy 110 -#define OP_ContextPop 111 +#define OP_VDestroy 100 +#define OP_ContextPop 101 #define OP_BitOr 75 /* same as TK_BITOR */ -#define OP_Next 112 -#define OP_IdxInsert 113 -#define OP_Distinct 114 +#define OP_Next 102 +#define OP_IdxInsert 103 #define OP_Lt 71 /* same as TK_LT */ -#define OP_Insert 115 -#define OP_Destroy 116 -#define OP_ReadCookie 117 -#define OP_ForceInt 118 -#define OP_LoadAnalysis 119 -#define OP_Explain 120 -#define OP_IfMemZero 121 -#define OP_OpenPseudo 122 -#define OP_OpenEphemeral 123 -#define OP_Null 124 -#define OP_Blob 127 +#define OP_Insert 104 +#define OP_Destroy 105 +#define OP_ReadCookie 106 +#define OP_ForceInt 107 +#define OP_LoadAnalysis 108 +#define OP_Explain 109 +#define OP_OpenPseudo 110 +#define OP_OpenEphemeral 111 +#define OP_Null 112 +#define OP_Move 113 +#define OP_Blob 114 #define OP_Add 78 /* same as TK_PLUS */ -#define OP_MemStore 128 -#define OP_Rewind 129 -#define OP_MoveGe 130 -#define OP_VBegin 131 -#define OP_VUpdate 132 +#define OP_Rewind 115 +#define OP_MoveGe 116 +#define OP_VBegin 117 +#define OP_VUpdate 118 +#define OP_IfZero 119 #define OP_BitNot 87 /* same as TK_BITNOT */ -#define OP_VCreate 133 -#define OP_MemMove 134 -#define OP_MemNull 135 -#define OP_Found 136 -#define OP_NullRow 137 - -/* Opcodes that are guaranteed to never push a value onto the stack -** contain a 1 their corresponding position of the following mask -** set. See the opcodeNoPush() function in vdbeaux.c */ -#define NOPUSH_MASK_0 0xeeb4 -#define NOPUSH_MASK_1 0xf96b -#define NOPUSH_MASK_2 0xfbb6 -#define NOPUSH_MASK_3 0xfe64 -#define NOPUSH_MASK_4 0xffff -#define NOPUSH_MASK_5 0xeef7 -#define NOPUSH_MASK_6 0xf7f6 -#define NOPUSH_MASK_7 0x0ecf -#define NOPUSH_MASK_8 0x7f3f -#define NOPUSH_MASK_9 0x0000 +#define OP_VCreate 120 +#define OP_Found 121 +#define OP_IfPos 122 +#define OP_NullRow 123 + +/* The following opcode values are never used */ +#define OP_NotUsed_124 124 +#define OP_NotUsed_126 126 +#define OP_NotUsed_127 127 +#define OP_NotUsed_128 128 +#define OP_NotUsed_129 129 +#define OP_NotUsed_130 130 +#define OP_NotUsed_131 131 +#define OP_NotUsed_132 132 +#define OP_NotUsed_133 133 +#define OP_NotUsed_134 134 +#define OP_NotUsed_135 135 +#define OP_NotUsed_136 136 +#define OP_NotUsed_137 137 + + +/* Properties such as "out2" or "jump" that are specified in +** comments following the "case" for each opcode in the vdbe.c +** are encoded into bitvectors as follows: +*/ +#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */ +#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */ +#define OPFLG_IN1 0x0004 /* in1: P1 is an input */ +#define OPFLG_IN2 0x0008 /* in2: P2 is an input */ +#define OPFLG_IN3 0x0010 /* in3: P3 is an input */ +#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */ +#define OPFLG_INITIALIZER {\ +/* 0 */ 0x00, 0x01, 0x00, 0x10, 0x02, 0x11, 0x00, 0x00,\ +/* 8 */ 0x00, 0x05, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\ +/* 16 */ 0x04, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00,\ +/* 24 */ 0x02, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00,\ +/* 32 */ 0x02, 0x11, 0x11, 0x02, 0x05, 0x00, 0x02, 0x11,\ +/* 40 */ 0x04, 0x00, 0x00, 0x0c, 0x11, 0x01, 0x02, 0x01,\ +/* 48 */ 0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x04, 0x00,\ +/* 56 */ 0x00, 0x00, 0x11, 0x08, 0x2c, 0x2c, 0x00, 0x11,\ +/* 64 */ 0x05, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ +/* 72 */ 0x15, 0x00, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c, 0x2c,\ +/* 80 */ 0x2c, 0x2c, 0x2c, 0x2c, 0x00, 0x00, 0x00, 0x04,\ +/* 88 */ 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x11, 0x00,\ +/* 96 */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x08,\ +/* 104 */ 0x00, 0x02, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00,\ +/* 112 */ 0x02, 0x00, 0x02, 0x01, 0x11, 0x00, 0x00, 0x05,\ +/* 120 */ 0x00, 0x11, 0x05, 0x00, 0x00, 0x02, 0x00, 0x00,\ +/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ +/* 136 */ 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04,} /************** End of opcodes.h *********************************************/ /************** Continuing where we left off in vdbe.h ***********************/ @@ -5125,14 +6913,19 @@ typedef struct VdbeOpList VdbeOpList; ** for a description of what each of these routines does. */ SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*); -SQLITE_PRIVATE int sqlite3VdbeAddOp(Vdbe*,int,int,int); -SQLITE_PRIVATE int sqlite3VdbeOp3(Vdbe*,int,int,int,const char *zP3,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp); SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1); SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2); +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3); +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N); -SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, const char *zP1, int N); +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*); @@ -5181,7 +6974,7 @@ SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...); ** subsystem. The page cache subsystem reads and writes a file a page ** at a time and provides a journal for rollback. ** -** @(#) $Id: pager.h,v 1.68 2007/11/28 16:19:56 drh Exp $ +** @(#) $Id: pager.h,v 1.69 2008/02/02 20:47:38 drh Exp $ */ #ifndef _PAGER_H_ @@ -5280,7 +7073,6 @@ SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*); #ifdef SQLITE_TEST SQLITE_PRIVATE int *sqlite3PagerStats(Pager*); SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*); - int pager3_refinfo_enable; #endif #ifdef SQLITE_TEST @@ -5296,63 +7088,6 @@ void enable_simulated_io_errors(void); /************** End of pager.h ***********************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ - -/* -** Name of the master database table. The master database table -** is a special table that holds the names and attributes of all -** user tables and indices. -*/ -#define MASTER_NAME "sqlite_master" -#define TEMP_MASTER_NAME "sqlite_temp_master" - -/* -** The root-page of the master database table. -*/ -#define MASTER_ROOT 1 - -/* -** The name of the schema table. -*/ -#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) - -/* -** A convenience macro that returns the number of elements in -** an array. -*/ -#define ArraySize(X) (sizeof(X)/sizeof(X[0])) - -/* -** Forward references to structures -*/ -typedef struct AggInfo AggInfo; -typedef struct AuthContext AuthContext; -typedef struct CollSeq CollSeq; -typedef struct Column Column; -typedef struct Db Db; -typedef struct Schema Schema; -typedef struct Expr Expr; -typedef struct ExprList ExprList; -typedef struct FKey FKey; -typedef struct FuncDef FuncDef; -typedef struct IdList IdList; -typedef struct Index Index; -typedef struct KeyClass KeyClass; -typedef struct KeyInfo KeyInfo; -typedef struct Module Module; -typedef struct NameContext NameContext; -typedef struct Parse Parse; -typedef struct Select Select; -typedef struct SrcList SrcList; -typedef struct StrAccum StrAccum; -typedef struct Table Table; -typedef struct TableLock TableLock; -typedef struct Token Token; -typedef struct TriggerStack TriggerStack; -typedef struct TriggerStep TriggerStep; -typedef struct Trigger Trigger; -typedef struct WhereInfo WhereInfo; -typedef struct WhereLevel WhereLevel; - /************** Include os.h in the middle of sqliteInt.h ********************/ /************** Begin file os.h **********************************************/ /* @@ -5721,6 +7456,7 @@ SQLITE_PRIVATE sqlite3_vfs *sqlite3OsDefaultVfs(void); /************** End of mutex.h ***********************************************/ /************** Continuing where we left off in sqliteInt.h ******************/ + /* ** Each database file to be accessed by the system is an instance ** of the following structure. There are normally two of these structures @@ -5826,7 +7562,7 @@ struct sqlite3 { u8 autoCommit; /* The auto-commit flag. */ u8 temp_store; /* 1: file 2: memory 0: default */ u8 mallocFailed; /* True if we have seen a malloc failure */ - char nextAutovac; /* Autovac setting after VACUUM if >=0 */ + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */ int nTable; /* Number of tables in the database */ CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ i64 lastRowid; /* ROWID of most recent insert (see above) */ @@ -5935,6 +7671,7 @@ struct sqlite3 { */ #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ +#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */ #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ @@ -6046,7 +7783,7 @@ struct CollSeq { ** ** But rather than start with 0 or 1, we begin with 'a'. That way, ** when multiple affinity types are concatenated into a string and -** used as the P3 operand, they will be more readable. +** used as the P4 operand, they will be more readable. ** ** Note also that the numeric types are grouped together so that testing ** for a numeric type is a single comparison. @@ -6060,6 +7797,20 @@ struct CollSeq { #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC) /* +** The SQLITE_AFF_MASK values masks off the significant bits of an +** affinity value. +*/ +#define SQLITE_AFF_MASK 0x67 + +/* +** Additional bit values that can be ORed with an affinity without +** changing the affinity. +*/ +#define SQLITE_JUMPIFNULL 0x08 /* jumps if either operand is NULL */ +#define SQLITE_NULLEQUAL 0x10 /* compare NULLs equal */ +#define SQLITE_STOREP2 0x80 /* Store result in reg[P2] rather than jump */ + +/* ** Each SQL table is represented in memory by an instance of the ** following structure. ** @@ -6574,6 +8325,10 @@ struct WhereLevel { sqlite3_index_info *pIdxInfo; /* Index info for n-th source table */ }; +#define ORDERBY_NORMAL 0 +#define ORDERBY_MIN 1 +#define ORDERBY_MAX 2 + /* ** The WHERE clause processing routine has two halves. The ** first part does the start of the WHERE loop and the second @@ -6639,7 +8394,7 @@ struct NameContext { ** ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes. ** These addresses must be stored so that we can go back and fill in -** the P3_KEYINFO and P2 parameters later. Neither the KeyInfo nor +** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor ** the number of columns in P2 can be computed at the same time ** as the OP_OpenEphm instruction is coded because not ** enough information about the compound query is known at that point. @@ -6675,18 +8430,30 @@ struct Select { */ #define SRT_Union 1 /* Store result as keys in an index */ #define SRT_Except 2 /* Remove result from a UNION index */ -#define SRT_Discard 3 /* Do not save the results anywhere */ +#define SRT_Exists 3 /* Store 1 if the result is not empty */ +#define SRT_Discard 4 /* Do not save the results anywhere */ /* The ORDER BY clause is ignored for all of the above */ -#define IgnorableOrderby(X) (X<=SRT_Discard) +#define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard) + +#define SRT_Callback 5 /* Invoke a callback with each row of result */ +#define SRT_Mem 6 /* Store result in a memory cell */ +#define SRT_Set 7 /* Store non-null results as keys in an index */ +#define SRT_Table 8 /* Store result as data with an automatic rowid */ +#define SRT_EphemTab 9 /* Create transient tab and store like SRT_Table */ +#define SRT_Subroutine 10 /* Call a subroutine to handle results */ -#define SRT_Callback 4 /* Invoke a callback with each row of result */ -#define SRT_Mem 5 /* Store result in a memory cell */ -#define SRT_Set 6 /* Store non-null results as keys in an index */ -#define SRT_Table 7 /* Store result as data with an automatic rowid */ -#define SRT_EphemTab 8 /* Create transient tab and store like SRT_Table */ -#define SRT_Subroutine 9 /* Call a subroutine to handle results */ -#define SRT_Exists 10 /* Store 1 if the result is not empty */ +/* +** A structure used to customize the behaviour of sqlite3Select(). See +** comments above sqlite3Select() for details. +*/ +typedef struct SelectDest SelectDest; +struct SelectDest { + u8 eDest; /* How to dispose of the results */ + u8 affinity; /* Affinity used when eDest==SRT_Set */ + int iParm; /* A parameter used by the eDest disposal method */ + int iMem; /* Base register where results are written */ +}; /* ** An SQL parser context. A copy of this structure is passed through @@ -6714,11 +8481,16 @@ struct Parse { u8 checkSchema; /* Causes schema cookie check after an error */ u8 nested; /* Number of nested calls to the parser/code generator */ u8 parseError; /* True after a parsing error. Ticket #1794 */ + u8 nTempReg; /* Number of temporary registers in aTempReg[] */ + u8 nTempInUse; /* Number of aTempReg[] currently checked out */ + int aTempReg[8]; /* Holding area for temporary registers */ + int nRangeReg; /* Size of the temporary register block */ + int iRangeReg; /* First register in temporary register block */ int nErr; /* Number of errors seen */ int nTab; /* Number of previously allocated VDBE cursors */ int nMem; /* Number of memory cells used so far */ int nSet; /* Number of sets used so far */ - int ckOffset; /* Stack offset to data used by CHECK constraints */ + int ckBase; /* Base register of data during check constraints */ u32 writeMask; /* Start a write transaction on these databases */ u32 cookieMask; /* Bitmask of schema verified databases */ int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */ @@ -6727,6 +8499,8 @@ struct Parse { int nTableLock; /* Number of locks in aTableLock */ TableLock *aTableLock; /* Required table locks for shared-cache mode */ #endif + int regRowid; /* Register holding rowid of CREATE TABLE entry */ + int regRoot; /* Register holding root page number for new objects */ /* Above is constant between recursions. Below is reset before and after ** each recursion */ @@ -6903,6 +8677,8 @@ struct TriggerStack { Table *pTab; /* Table that triggers are currently being coded on */ int newIdx; /* Index of vdbe cursor to "new" temp table */ int oldIdx; /* Index of vdbe cursor to "old" temp table */ + u32 newColMask; + u32 oldColMask; int orconf; /* Current orconf policy */ int ignoreJump; /* where to jump to for a RAISE(IGNORE) */ Trigger *pTrigger; /* The trigger currently being coded */ @@ -6988,6 +8764,7 @@ SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*); SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int); SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int); SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int); +SQLITE_PRIVATE int sqlite3MallocSize(void *); SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...); SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list); @@ -7005,6 +8782,10 @@ SQLITE_PRIVATE void sqlite3DequoteExpr(sqlite3*, Expr*); SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int); SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **); SQLITE_PRIVATE void sqlite3FinishCoding(Parse*); +SQLITE_PRIVATE int sqlite3GetTempReg(Parse*); +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int); +SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int); +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int); SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*, int, Expr*, Expr*, const Token*); SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*); SQLITE_PRIVATE Expr *sqlite3RegisterExpr(Parse*,Token*); @@ -7033,6 +8814,12 @@ SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,Expr*); SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*); SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*); +SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32); +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32); +SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32); +SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32); +SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*); + SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int); #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) @@ -7057,7 +8844,7 @@ SQLITE_PRIVATE void sqlite3SrcListDelete(SrcList*); SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*, Token*, int, int); SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int); -SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, int, int, Select*, int, int*, char *aff); +SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*, Select*, int, int*, char *aff); SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*, Expr*,ExprList*,int,Expr*,Expr*); SQLITE_PRIVATE void sqlite3SelectDelete(Select*); @@ -7066,16 +8853,17 @@ SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int); SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int); SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*); SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int); -SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**); +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u8); SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*); -SQLITE_PRIVATE void sqlite3ExprCodeGetColumn(Vdbe*, Table*, int, int); -SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*); -SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*); -SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*); +SQLITE_PRIVATE void sqlite3ExprCodeGetColumn(Vdbe*, Table*, int, int, int); +SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int); +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*); +SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int); +SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int); SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int); SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int); SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*); -SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,const char*, const char*); +SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*); SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*); SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*); SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*); @@ -7084,8 +8872,8 @@ SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*); SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*); SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*); SQLITE_PRIVATE int sqlite3ExprResolveNames(NameContext *, Expr *); -SQLITE_PRIVATE int sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); -SQLITE_PRIVATE int sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*); +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*); SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*); SQLITE_PRIVATE Expr *sqlite3CreateIdExpr(Parse *, const char*); SQLITE_PRIVATE void sqlite3Randomness(int, void*); @@ -7099,12 +8887,13 @@ SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*); SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*); SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*); SQLITE_PRIVATE int sqlite3IsRowid(const char*); -SQLITE_PRIVATE void sqlite3GenerateRowDelete(sqlite3*, Vdbe*, Table*, int, int); -SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Vdbe*, Table*, int, char*); -SQLITE_PRIVATE void sqlite3GenerateIndexKey(Vdbe*, Index*, int); -SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,char*,int,int,int,int); -SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, char*, int, int, int, int); -SQLITE_PRIVATE void sqlite3OpenTableAndIndices(Parse*, Table*, int, int); +SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int); +SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*); +SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int); +SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int, + int*,int,int,int,int); +SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*,int,int,int,int); +SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int); SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int); SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*); SQLITE_PRIVATE void sqlite3TokenCopy(sqlite3*,Token*, Token*); @@ -7115,10 +8904,17 @@ SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*); SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int); SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*); SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(sqlite3*); -SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*); -SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*); -SQLITE_PRIVATE int sqlite3SafetyCheck(sqlite3*); -SQLITE_PRIVATE void sqlite3ChangeCookie(sqlite3*, Vdbe*, int); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*); +SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*); +#else +# define sqlite3SafetyOn(A) 0 +# define sqlite3SafetyOff(A) 0 +#endif +SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*); +SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*); +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int); +SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Select*, Expr*, u32, int); #ifndef SQLITE_OMIT_TRIGGER SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*, @@ -7128,7 +8924,7 @@ SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int); SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*); SQLITE_PRIVATE int sqlite3TriggersExist(Parse*, Table*, int, ExprList*); SQLITE_PRIVATE int sqlite3CodeRowTrigger(Parse*, int, ExprList*, int, Table *, int, int, - int, int); + int, int, u32*, u32*); void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*); SQLITE_PRIVATE void sqlite3DeleteTriggerStep(TriggerStep*); SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*); @@ -7138,12 +8934,14 @@ SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList* SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*); SQLITE_PRIVATE void sqlite3DeleteTrigger(Trigger*); SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*); +SQLITE_PRIVATE void sqlite3SelectMask(Parse *, Select *, u32); #else # define sqlite3TriggersExist(A,B,C,D,E,F) 0 # define sqlite3DeleteTrigger(A) # define sqlite3DropTriggerPtr(A,B) # define sqlite3UnlinkAndDeleteTrigger(A,B,C) -# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I) 0 +# define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I,J,K) 0 +# define sqlite3SelectMask(A, B, C) #endif SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*); @@ -7188,7 +8986,7 @@ SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity); SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr); SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*); SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...); -SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z); +SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n); SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); SQLITE_PRIVATE const char *sqlite3ErrStr(int); SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); @@ -7248,7 +9046,7 @@ SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*); - +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int); /* ** The interface to the LEMON-generated parser @@ -7275,26 +9073,6 @@ SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, int, u8, const char *); SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*); #endif -/* -** The MallocDisallow() and MallocAllow() routines are like asserts. -** Call them around a section of code that you do not expect to do -** any memory allocation. -*/ -#ifdef SQLITE_MEMDEBUG -SQLITE_PRIVATE void sqlite3MallocDisallow(void); -SQLITE_PRIVATE void sqlite3MallocAllow(void); -SQLITE_PRIVATE void sqlite3MallocBenignFailure(int); -SQLITE_PRIVATE void sqlite3MallocEnterBenignBlock(int isBenign); -SQLITE_PRIVATE void sqlite3MallocLeaveBenignBlock(); -#else -# define sqlite3MallocDisallow() -# define sqlite3MallocAllow() -# define sqlite3MallocBenignFailure(x) -# define sqlite3MallocEnterBenignBlock(x); -# define sqlite3MallocLeaveBenignBlock(); -#endif - - #ifdef SQLITE_OMIT_VIRTUALTABLE # define sqlite3VtabClear(X) # define sqlite3VtabSync(X,Y) (Y) @@ -7322,6 +9100,36 @@ SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*); SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, int, const char*); SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *); + +/* +** Available fault injectors. Should be numbered beginning with 0. +*/ +#define SQLITE_FAULTINJECTOR_MALLOC 0 +#define SQLITE_FAULTINJECTOR_COUNT 1 + +/* +** The interface to the fault injector subsystem. If the fault injector +** mechanism is disabled at compile-time then set up macros so that no +** unnecessary code is generated. +*/ +#ifndef SQLITE_OMIT_FAULTINJECTOR +SQLITE_PRIVATE void sqlite3FaultConfig(int,int,int); +SQLITE_PRIVATE int sqlite3FaultFailures(int); +SQLITE_PRIVATE int sqlite3FaultBenignFailures(int); +SQLITE_PRIVATE int sqlite3FaultPending(int); +SQLITE_PRIVATE void sqlite3FaultBenign(int,int); +SQLITE_PRIVATE int sqlite3FaultStep(int); +#else +# define sqlite3FaultConfig(A,B,C) +# define sqlite3FaultFailures(A) 0 +# define sqlite3FaultBenignFailures(A) 0 +# define sqlite3FaultPending(A) (-1) +# define sqlite3FaultBenign(A,B) +# define sqlite3FaultStep(A) 0 +#endif + + + #define IN_INDEX_ROWID 1 #define IN_INDEX_EPH 2 #define IN_INDEX_INDEX 3 @@ -7355,17 +9163,17 @@ SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *); /* ** If the SQLITE_ENABLE IOTRACE exists then the global variable -** sqlite3_io_trace is a pointer to a printf-like routine used to +** sqlite3IoTrace is a pointer to a printf-like routine used to ** print I/O tracing messages. */ #ifdef SQLITE_ENABLE_IOTRACE -# define IOTRACE(A) if( sqlite3_io_trace ){ sqlite3_io_trace A; } +# define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; } SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*); #else # define IOTRACE(A) # define sqlite3VdbeIOTraceSql(X) #endif -SQLITE_EXTERN void (*sqlite3_io_trace)(const char*,...); +SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...); #endif @@ -7389,7 +9197,7 @@ SQLITE_EXTERN void (*sqlite3_io_trace)(const char*,...); ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: date.c,v 1.73 2007/09/12 17:01:45 danielk1977 Exp $ +** $Id: date.c,v 1.76 2008/02/21 20:40:44 drh Exp $ ** ** SQLite processes all times and dates as Julian Day numbers. The ** dates and times are stored as the number of days since noon @@ -7501,23 +9309,32 @@ end_getDigits: ** ** (+/-)HH:MM ** +** Or the "zulu" notation: +** +** Z +** ** If the parse is successful, write the number of minutes -** of change in *pnMin and return 0. If a parser error occurs, -** return 0. +** of change in p->tz and return 0. If a parser error occurs, +** return non-zero. ** ** A missing specifier is not considered an error. */ static int parseTimezone(const char *zDate, DateTime *p){ int sgn = 0; int nHr, nMn; + int c; while( isspace(*(u8*)zDate) ){ zDate++; } p->tz = 0; - if( *zDate=='-' ){ + c = *zDate; + if( c=='-' ){ sgn = -1; - }else if( *zDate=='+' ){ + }else if( c=='+' ){ sgn = +1; + }else if( c=='Z' || c=='z' ){ + zDate++; + goto zulu_time; }else{ - return *zDate!=0; + return c!=0; } zDate++; if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){ @@ -7525,6 +9342,7 @@ static int parseTimezone(const char *zDate, DateTime *p){ } zDate += 5; p->tz = sgn*(nMn + nHr*60); +zulu_time: while( isspace(*(u8*)zDate) ){ zDate++; } return *zDate!=0; } @@ -8023,6 +9841,9 @@ static int parseModifier(const char *zMod, DateTime *p){ ** argv[1] and following are modifiers. Parse them all and write ** the resulting time into the DateTime structure p. Return 0 ** on success and 1 if there are any errors. +** +** If there are zero parameters (if even argv[0] is undefined) +** then assume a default value of "now" for argv[0]. */ static int isDate( sqlite3_context *context, @@ -8032,8 +9853,12 @@ static int isDate( ){ int i; const unsigned char *z; - if( argc==0 ) return 1; - z = sqlite3_value_text(argv[0]); + static const unsigned char zDflt[] = "now"; + if( argc==0 ){ + z = zDflt; + }else{ + z = sqlite3_value_text(argv[0]); + } if( !z || parseDateOrTime(context, (char*)z, p) ){ return 1; } @@ -8197,7 +10022,10 @@ static void strftimeFunc( return; }else{ z = sqlite3_malloc( n ); - if( z==0 ) return; + if( z==0 ){ + sqlite3_result_error_nomem(context); + return; + } } computeJD(&x); computeYMD_HMS(&x); @@ -8252,15 +10080,13 @@ static void strftimeFunc( case 'S': sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break; case 'w': z[j++] = (((int)(x.rJD+1.5)) % 7) + '0'; break; case 'Y': sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=strlen(&z[j]);break; - case '%': z[j++] = '%'; break; + default: z[j++] = '%'; break; } } } z[j] = 0; - sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); - if( z!=zBuf ){ - sqlite3_free(z); - } + sqlite3_result_text(context, z, -1, + z==zBuf ? SQLITE_TRANSIENT : sqlite3_free); } /* @@ -8273,12 +10099,7 @@ static void ctimeFunc( int argc, sqlite3_value **argv ){ - sqlite3_value *pVal = sqlite3ValueNew(0); - if( pVal ){ - sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); - timeFunc(context, 1, &pVal); - sqlite3ValueFree(pVal); - } + timeFunc(context, 0, 0); } /* @@ -8291,12 +10112,7 @@ static void cdateFunc( int argc, sqlite3_value **argv ){ - sqlite3_value *pVal = sqlite3ValueNew(0); - if( pVal ){ - sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); - dateFunc(context, 1, &pVal); - sqlite3ValueFree(pVal); - } + dateFunc(context, 0, 0); } /* @@ -8309,12 +10125,7 @@ static void ctimestampFunc( int argc, sqlite3_value **argv ){ - sqlite3_value *pVal = sqlite3ValueNew(0); - if( pVal ){ - sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC); - datetimeFunc(context, 1, &pVal); - sqlite3ValueFree(pVal); - } + datetimeFunc(context, 0, 0); } #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */ @@ -8416,7 +10227,7 @@ SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(sqlite3 *db){ /************** End of date.c ************************************************/ /************** Begin file os.c **********************************************/ - /* +/* ** 2005 November 29 ** ** The author disclaims copyright to this source code. In place of @@ -8506,35 +10317,13 @@ SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id){ SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ return id->pMethods->xFileControl(id,op,pArg); } - -#ifdef SQLITE_TEST - /* The following two variables are used to override the values returned - ** by the xSectorSize() and xDeviceCharacteristics() vfs methods for - ** testing purposes. They are usually set by a test command implemented - ** in test6.c. - */ - int sqlite3_test_sector_size = 0; - int sqlite3_test_device_characteristics = 0; - int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ - int dc = id->pMethods->xDeviceCharacteristics(id); - return dc | sqlite3_test_device_characteristics; - } - int sqlite3OsSectorSize(sqlite3_file *id){ - if( sqlite3_test_sector_size==0 ){ - int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; - return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); - } - return sqlite3_test_sector_size; - } -#else - int sqlite3OsSectorSize(sqlite3_file *id){ - int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; - return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); - } - int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ - return id->pMethods->xDeviceCharacteristics(id); - } -#endif +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){ + int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; + return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); +} +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ + return id->pMethods->xDeviceCharacteristics(id); +} /* ** The next group of routines are convenience wrappers around the @@ -8630,7 +10419,9 @@ static sqlite3_vfs *vfsList = 0; ** first VFS on the list. */ SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){ +#ifndef SQLITE_MUTEX_NOOP sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); +#endif sqlite3_vfs *pVfs = 0; static int isInit = 0; sqlite3_mutex_enter(mutex); @@ -8672,7 +10463,9 @@ static void vfsUnlink(sqlite3_vfs *pVfs){ ** true. */ SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ +#ifndef SQLITE_MUTEX_NOOP sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); +#endif sqlite3_vfs_find(0); /* Make sure we are initialized */ sqlite3_mutex_enter(mutex); vfsUnlink(pVfs); @@ -8692,7 +10485,9 @@ SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ ** Unregister a VFS so that it is no longer accessible. */ SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ +#ifndef SQLITE_MUTEX_NOOP sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); +#endif sqlite3_mutex_enter(mutex); vfsUnlink(pVfs); sqlite3_mutex_leave(mutex); @@ -8700,9 +10495,9 @@ SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ } /************** End of os.c **************************************************/ -/************** Begin file mem1.c ********************************************/ +/************** Begin file fault.c *******************************************/ /* -** 2007 August 14 +** 2008 Jan 22 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -8712,231 +10507,146 @@ SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ ** May you share freely, never taking more than you give. ** ************************************************************************* -** This file contains the C functions that implement a memory -** allocation subsystem for use by SQLite. -** -** $Id: mem1.c,v 1.14 2007/11/29 18:36:49 drh Exp $ -*/ - -/* -** This version of the memory allocator is the default. It is -** used when no other memory allocator is specified using compile-time -** macros. -*/ -#if !defined(SQLITE_MEMDEBUG) && !defined(SQLITE_MEMORY_SIZE) \ - && !defined(SQLITE_MMAP_HEAP_SIZE) - -/* -** We will eventually construct multiple memory allocation subsystems -** suitable for use in various contexts: +** This file contains code to implement a fault-injector used for +** testing and verification of SQLite. ** -** * Normal multi-threaded builds -** * Normal single-threaded builds -** * Debugging builds +** Subsystems within SQLite can call sqlite3FaultStep() to see if +** they should simulate a fault. sqlite3FaultStep() normally returns +** zero but will return non-zero if a fault should be simulated. +** Fault injectors can be used, for example, to simulate memory +** allocation failures or I/O errors. ** -** This initial version is suitable for use in normal multi-threaded -** builds. We envision that alternative versions will be stored in -** separate source files. #ifdefs will be used to select the code from -** one of the various memN.c source files for use in any given build. +** The fault injector is omitted from the code if SQLite is +** compiled with -DSQLITE_OMIT_FAULTINJECTOR=1. There is a very +** small performance hit for leaving the fault injector in the code. +** Commerical products will probably want to omit the fault injector +** from production builds. But safety-critical systems who work +** under the motto "fly what you test and test what you fly" may +** choose to leave the fault injector enabled even in production. */ -/* -** All of the static variables used by this module are collected -** into a single structure named "mem". This is to keep the -** static variables organized and to reduce namespace pollution -** when this module is combined with other in the amalgamation. -*/ -static struct { - /* - ** The alarm callback and its arguments. The mem.mutex lock will - ** be held while the callback is running. Recursive calls into - ** the memory subsystem are allowed, but no new callbacks will be - ** issued. The alarmBusy variable is set to prevent recursive - ** callbacks. - */ - sqlite3_int64 alarmThreshold; - void (*alarmCallback)(void*, sqlite3_int64,int); - void *alarmArg; - int alarmBusy; - - /* - ** Mutex to control access to the memory allocation subsystem. - */ - sqlite3_mutex *mutex; - - /* - ** Current allocation and high-water mark. - */ - sqlite3_int64 nowUsed; - sqlite3_int64 mxUsed; - - -} mem; +#ifndef SQLITE_OMIT_FAULTINJECTOR /* -** Enter the mutex mem.mutex. Allocate it if it is not already allocated. +** There can be various kinds of faults. For example, there can be +** a memory allocation failure. Or an I/O failure. For each different +** fault type, there is a separate FaultInjector structure to keep track +** of the status of that fault. */ -static void enterMem(void){ - if( mem.mutex==0 ){ - mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); - } - sqlite3_mutex_enter(mem.mutex); -} +static struct FaultInjector { + int iCountdown; /* Number of pending successes before we hit a failure */ + int nRepeat; /* Number of times to repeat the failure */ + int nBenign; /* Number of benign failures seen since last config */ + int nFail; /* Number of failures seen since last config */ + u8 enable; /* True if enabled */ + u8 benign; /* Ture if next failure will be benign */ +} aFault[SQLITE_FAULTINJECTOR_COUNT]; /* -** Return the amount of memory currently checked out. +** This routine configures and enables a fault injector. After +** calling this routine, aFaultStep() will return false (zero) +** nDelay times, then it will return true nRepeat times, +** then it will again begin returning false. */ -SQLITE_API sqlite3_int64 sqlite3_memory_used(void){ - sqlite3_int64 n; - enterMem(); - n = mem.nowUsed; - sqlite3_mutex_leave(mem.mutex); - return n; +SQLITE_PRIVATE void sqlite3FaultConfig(int id, int nDelay, int nRepeat){ + assert( id>=0 && id=0; + aFault[id].benign = 0; } /* -** Return the maximum amount of memory that has ever been -** checked out since either the beginning of this process -** or since the most recent reset. +** Return the number of faults (both hard and benign faults) that have +** occurred since the injector was last configured. */ -SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ - sqlite3_int64 n; - enterMem(); - n = mem.mxUsed; - if( resetFlag ){ - mem.mxUsed = mem.nowUsed; - } - sqlite3_mutex_leave(mem.mutex); - return n; +SQLITE_PRIVATE int sqlite3FaultFailures(int id){ + assert( id>=0 && id=0 && id=0 && id0 ){ - enterMem(); - if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){ - sqlite3MemsysAlarm(nBytes); - } - p = malloc(nBytes+8); - if( p==0 ){ - sqlite3MemsysAlarm(nBytes); - p = malloc(nBytes+8); - } - if( p ){ - p[0] = nBytes; - p++; - mem.nowUsed += nBytes; - if( mem.nowUsed>mem.mxUsed ){ - mem.mxUsed = mem.nowUsed; - } - } - sqlite3_mutex_leave(mem.mutex); - } - return (void*)p; +SQLITE_PRIVATE void sqlite3FaultBenign(int id, int enable){ + assert( id>=0 && id=0 && id0 ){ + aFault[id].iCountdown--; return 0; } - p = pPrior; - p--; - nOld = (int)p[0]; - assert( mem.mutex!=0 ); - sqlite3_mutex_enter(mem.mutex); - if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){ - sqlite3MemsysAlarm(nBytes-nOld); + sqlite3Fault(); + aFault[id].nFail++; + if( aFault[id].benign ){ + aFault[id].nBenign++; } - p = realloc(p, nBytes+8); - if( p==0 ){ - sqlite3MemsysAlarm(nBytes); - p = pPrior; - p--; - p = realloc(p, nBytes+8); + aFault[id].nRepeat--; + if( aFault[id].nRepeat<=0 ){ + aFault[id].enable = 0; } - if( p ){ - p[0] = nBytes; - p++; - mem.nowUsed += nBytes-nOld; - if( mem.nowUsed>mem.mxUsed ){ - mem.mxUsed = mem.nowUsed; - } - } - sqlite3_mutex_leave(mem.mutex); - return (void*)p; + return 1; } -#endif /* !SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */ +#endif /* SQLITE_OMIT_FAULTINJECTOR */ -/************** End of mem1.c ************************************************/ -/************** Begin file mem2.c ********************************************/ +/************** End of fault.c ***********************************************/ +/************** Begin file mem1.c ********************************************/ /* -** 2007 August 15 +** 2007 August 14 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -8949,80 +10659,15 @@ SQLITE_API void *sqlite3_realloc(void *pPrior, int nBytes){ ** This file contains the C functions that implement a memory ** allocation subsystem for use by SQLite. ** -** $Id: mem2.c,v 1.18 2007/11/29 18:36:49 drh Exp $ -*/ - -/* -** This version of the memory allocator is used only if the -** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION -** is not defined. +** $Id: mem1.c,v 1.16 2008/02/14 23:26:56 drh Exp $ */ -#if defined(SQLITE_MEMDEBUG) /* -** We will eventually construct multiple memory allocation subsystems -** suitable for use in various contexts: -** -** * Normal multi-threaded builds -** * Normal single-threaded builds -** * Debugging builds -** -** This version is suitable for use in debugging builds. -** -** Features: -** -** * Every allocate has guards at both ends. -** * New allocations are initialized with randomness -** * Allocations are overwritten with randomness when freed -** * Optional logs of malloc activity generated -** * Summary of outstanding allocations with backtraces to the -** point of allocation. -** * The ability to simulate memory allocation failure -*/ - -/* -** The backtrace functionality is only available with GLIBC -*/ -#ifdef __GLIBC__ - extern int backtrace(void**,int); - extern void backtrace_symbols_fd(void*const*,int,int); -#else -# define backtrace(A,B) 0 -# define backtrace_symbols_fd(A,B,C) -#endif - -/* -** Each memory allocation looks like this: -** -** ------------------------------------------------------------------------ -** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard | -** ------------------------------------------------------------------------ -** -** The application code sees only a pointer to the allocation. We have -** to back up from the allocation pointer to find the MemBlockHdr. The -** MemBlockHdr tells us the size of the allocation and the number of -** backtrace pointers. There is also a guard word at the end of the -** MemBlockHdr. -*/ -struct MemBlockHdr { - struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ - int iSize; /* Size of this allocation */ - char nBacktrace; /* Number of backtraces on this alloc */ - char nBacktraceSlots; /* Available backtrace slots */ - short nTitle; /* Bytes of title; includes '\0' */ - int iForeGuard; /* Guard word for sanity */ -}; - -/* -** Guard words -*/ -#define FOREGUARD 0x80F5E153 -#define REARGUARD 0xE4676B53 - -/* -** Number of malloc size increments to track. +** This version of the memory allocator is the default. It is +** used when no other memory allocator is specified using compile-time +** macros. */ -#define NCSIZE 1000 +#ifdef SQLITE_SYSTEM_MALLOC /* ** All of the static variables used by this module are collected @@ -9039,7 +10684,7 @@ static struct { ** callbacks. */ sqlite3_int64 alarmThreshold; - void (*alarmCallback)(void*, sqlite3_int64, int); + void (*alarmCallback)(void*, sqlite3_int64,int); void *alarmArg; int alarmBusy; @@ -9054,52 +10699,312 @@ static struct { sqlite3_int64 nowUsed; sqlite3_int64 mxUsed; - /* - ** Head and tail of a linked list of all outstanding allocations - */ - struct MemBlockHdr *pFirst; - struct MemBlockHdr *pLast; - - /* - ** The number of levels of backtrace to save in new allocations. - */ - int nBacktrace; - - /* - ** Title text to insert in front of each block - */ - int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */ - char zTitle[100]; /* The title text */ - - /* - ** These values are used to simulate malloc failures. When - ** iFail is 1, simulate a malloc failures and reset the value - ** to iReset. - */ - int iFail; /* Decrement and fail malloc when this is 1 */ - int iReset; /* When malloc fails set iiFail to this value */ - int iFailCnt; /* Number of failures */ - int iBenignFailCnt; /* Number of benign failures */ - int iNextIsBenign; /* True if the next call to malloc may fail benignly */ - int iIsBenign; /* All malloc calls may fail benignly */ - - /* - ** sqlite3MallocDisallow() increments the following counter. - ** sqlite3MallocAllow() decrements it. - */ - int disallow; /* Do not allow memory allocation */ - - /* - ** Gather statistics on the sizes of memory allocations. - ** sizeCnt[i] is the number of allocation attempts of i*8 - ** bytes. i==NCSIZE is the number of allocation attempts for - ** sizes more than NCSIZE*8 bytes. - */ - int sizeCnt[NCSIZE]; - + } mem; - +/* +** Enter the mutex mem.mutex. Allocate it if it is not already allocated. +*/ +static void enterMem(void){ + if( mem.mutex==0 ){ + mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); + } + sqlite3_mutex_enter(mem.mutex); +} + +/* +** Return the amount of memory currently checked out. +*/ +SQLITE_API sqlite3_int64 sqlite3_memory_used(void){ + sqlite3_int64 n; + enterMem(); + n = mem.nowUsed; + sqlite3_mutex_leave(mem.mutex); + return n; +} + +/* +** Return the maximum amount of memory that has ever been +** checked out since either the beginning of this process +** or since the most recent reset. +*/ +SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){ + sqlite3_int64 n; + enterMem(); + n = mem.mxUsed; + if( resetFlag ){ + mem.mxUsed = mem.nowUsed; + } + sqlite3_mutex_leave(mem.mutex); + return n; +} + +/* +** Change the alarm callback +*/ +SQLITE_API int sqlite3_memory_alarm( + void(*xCallback)(void *pArg, sqlite3_int64 used,int N), + void *pArg, + sqlite3_int64 iThreshold +){ + enterMem(); + mem.alarmCallback = xCallback; + mem.alarmArg = pArg; + mem.alarmThreshold = iThreshold; + sqlite3_mutex_leave(mem.mutex); + return SQLITE_OK; +} + +/* +** Trigger the alarm +*/ +static void sqlite3MemsysAlarm(int nByte){ + void (*xCallback)(void*,sqlite3_int64,int); + sqlite3_int64 nowUsed; + void *pArg; + if( mem.alarmCallback==0 || mem.alarmBusy ) return; + mem.alarmBusy = 1; + xCallback = mem.alarmCallback; + nowUsed = mem.nowUsed; + pArg = mem.alarmArg; + sqlite3_mutex_leave(mem.mutex); + xCallback(pArg, nowUsed, nByte); + sqlite3_mutex_enter(mem.mutex); + mem.alarmBusy = 0; +} + +/* +** Allocate nBytes of memory +*/ +SQLITE_API void *sqlite3_malloc(int nBytes){ + sqlite3_int64 *p = 0; + if( nBytes>0 ){ + enterMem(); + if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){ + sqlite3MemsysAlarm(nBytes); + } + p = malloc(nBytes+8); + if( p==0 ){ + sqlite3MemsysAlarm(nBytes); + p = malloc(nBytes+8); + } + if( p ){ + p[0] = nBytes; + p++; + mem.nowUsed += nBytes; + if( mem.nowUsed>mem.mxUsed ){ + mem.mxUsed = mem.nowUsed; + } + } + sqlite3_mutex_leave(mem.mutex); + } + return (void*)p; +} + +/* +** Free memory. +*/ +SQLITE_API void sqlite3_free(void *pPrior){ + sqlite3_int64 *p; + int nByte; + if( pPrior==0 ){ + return; + } + assert( mem.mutex!=0 ); + p = pPrior; + p--; + nByte = (int)*p; + sqlite3_mutex_enter(mem.mutex); + mem.nowUsed -= nByte; + free(p); + sqlite3_mutex_leave(mem.mutex); +} + +/* +** Return the number of bytes allocated at p. +*/ +SQLITE_PRIVATE int sqlite3MallocSize(void *p){ + sqlite3_int64 *pInt; + if( !p ) return 0; + pInt = p; + return pInt[-1]; +} + +/* +** Change the size of an existing memory allocation +*/ +SQLITE_API void *sqlite3_realloc(void *pPrior, int nBytes){ + int nOld; + sqlite3_int64 *p; + if( pPrior==0 ){ + return sqlite3_malloc(nBytes); + } + if( nBytes<=0 ){ + sqlite3_free(pPrior); + return 0; + } + p = pPrior; + p--; + nOld = (int)p[0]; + assert( mem.mutex!=0 ); + sqlite3_mutex_enter(mem.mutex); + if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){ + sqlite3MemsysAlarm(nBytes-nOld); + } + p = realloc(p, nBytes+8); + if( p==0 ){ + sqlite3MemsysAlarm(nBytes); + p = pPrior; + p--; + p = realloc(p, nBytes+8); + } + if( p ){ + p[0] = nBytes; + p++; + mem.nowUsed += nBytes-nOld; + if( mem.nowUsed>mem.mxUsed ){ + mem.mxUsed = mem.nowUsed; + } + } + sqlite3_mutex_leave(mem.mutex); + return (void*)p; +} + +#endif /* SQLITE_SYSTEM_MALLOC */ + +/************** End of mem1.c ************************************************/ +/************** Begin file mem2.c ********************************************/ +/* +** 2007 August 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement a memory +** allocation subsystem for use by SQLite. +** +** $Id: mem2.c,v 1.22 2008/02/19 15:15:16 drh Exp $ +*/ + +/* +** This version of the memory allocator is used only if the +** SQLITE_MEMDEBUG macro is defined +*/ +#ifdef SQLITE_MEMDEBUG + +/* +** The backtrace functionality is only available with GLIBC +*/ +#ifdef __GLIBC__ + extern int backtrace(void**,int); + extern void backtrace_symbols_fd(void*const*,int,int); +#else +# define backtrace(A,B) 0 +# define backtrace_symbols_fd(A,B,C) +#endif + +/* +** Each memory allocation looks like this: +** +** ------------------------------------------------------------------------ +** | Title | backtrace pointers | MemBlockHdr | allocation | EndGuard | +** ------------------------------------------------------------------------ +** +** The application code sees only a pointer to the allocation. We have +** to back up from the allocation pointer to find the MemBlockHdr. The +** MemBlockHdr tells us the size of the allocation and the number of +** backtrace pointers. There is also a guard word at the end of the +** MemBlockHdr. +*/ +struct MemBlockHdr { + struct MemBlockHdr *pNext, *pPrev; /* Linked list of all unfreed memory */ + int iSize; /* Size of this allocation */ + char nBacktrace; /* Number of backtraces on this alloc */ + char nBacktraceSlots; /* Available backtrace slots */ + short nTitle; /* Bytes of title; includes '\0' */ + int iForeGuard; /* Guard word for sanity */ +}; + +/* +** Guard words +*/ +#define FOREGUARD 0x80F5E153 +#define REARGUARD 0xE4676B53 + +/* +** Number of malloc size increments to track. +*/ +#define NCSIZE 1000 + +/* +** All of the static variables used by this module are collected +** into a single structure named "mem". This is to keep the +** static variables organized and to reduce namespace pollution +** when this module is combined with other in the amalgamation. +*/ +static struct { + /* + ** The alarm callback and its arguments. The mem.mutex lock will + ** be held while the callback is running. Recursive calls into + ** the memory subsystem are allowed, but no new callbacks will be + ** issued. The alarmBusy variable is set to prevent recursive + ** callbacks. + */ + sqlite3_int64 alarmThreshold; + void (*alarmCallback)(void*, sqlite3_int64, int); + void *alarmArg; + int alarmBusy; + + /* + ** Mutex to control access to the memory allocation subsystem. + */ + sqlite3_mutex *mutex; + + /* + ** Current allocation and high-water mark. + */ + sqlite3_int64 nowUsed; + sqlite3_int64 mxUsed; + + /* + ** Head and tail of a linked list of all outstanding allocations + */ + struct MemBlockHdr *pFirst; + struct MemBlockHdr *pLast; + + /* + ** The number of levels of backtrace to save in new allocations. + */ + int nBacktrace; + + /* + ** Title text to insert in front of each block + */ + int nTitle; /* Bytes of zTitle to save. Includes '\0' and padding */ + char zTitle[100]; /* The title text */ + + /* + ** sqlite3MallocDisallow() increments the following counter. + ** sqlite3MallocAllow() decrements it. + */ + int disallow; /* Do not allow memory allocation */ + + /* + ** Gather statistics on the sizes of memory allocations. + ** sizeCnt[i] is the number of allocation attempts of i*8 + ** bytes. i==NCSIZE is the number of allocation attempts for + ** sizes more than NCSIZE*8 bytes. + */ + int sizeCnt[NCSIZE]; + +} mem; + + /* ** Enter the mutex mem.mutex. Allocate it if it is not already allocated. */ @@ -9191,14 +11096,15 @@ static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){ } /* -** This routine is called once the first time a simulated memory -** failure occurs. The sole purpose of this routine is to provide -** a convenient place to set a debugger breakpoint when debugging -** errors related to malloc() failures. +** Return the number of bytes currently allocated at address p. */ -static void sqlite3MemsysFailed(void){ - mem.iFailCnt = 0; - mem.iBenignFailCnt = 0; +SQLITE_PRIVATE int sqlite3MallocSize(void *p){ + struct MemBlockHdr *pHdr; + if( !p ){ + return 0; + } + pHdr = sqlite3MemsysGetHeader(p); + return pHdr->iSize; } /* @@ -9226,21 +11132,8 @@ SQLITE_API void *sqlite3_malloc(int nByte){ } totalSize = nByte + sizeof(*pHdr) + sizeof(int) + mem.nBacktrace*sizeof(void*) + mem.nTitle; - if( mem.iFail>0 ){ - if( mem.iFail==1 ){ - p = 0; - mem.iFail = mem.iReset; - if( mem.iFailCnt==0 ){ - sqlite3MemsysFailed(); /* A place to set a breakpoint */ - } - mem.iFailCnt++; - if( mem.iNextIsBenign || mem.iIsBenign ){ - mem.iBenignFailCnt++; - } - }else{ - p = malloc(totalSize); - mem.iFail--; - } + if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ + p = 0; }else{ p = malloc(totalSize); if( p==0 ){ @@ -9285,7 +11178,6 @@ SQLITE_API void *sqlite3_malloc(int nByte){ } sqlite3_mutex_leave(mem.mutex); } - mem.iNextIsBenign = 0; return p; } @@ -9364,7 +11256,7 @@ SQLITE_API void *sqlite3_realloc(void *pPrior, int nByte){ ** A value of zero turns of backtracing. The number is always rounded ** up to a multiple of 2. */ -SQLITE_API void sqlite3_memdebug_backtrace(int depth){ +SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){ if( depth<0 ){ depth = 0; } if( depth>20 ){ depth = 20; } depth = (depth+1)&0xfe; @@ -9374,7 +11266,7 @@ SQLITE_API void sqlite3_memdebug_backtrace(int depth){ /* ** Set the title string for subsequent allocations. */ -SQLITE_API void sqlite3_memdebug_settitle(const char *zTitle){ +SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){ int n = strlen(zTitle) + 1; enterMem(); if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; @@ -9388,7 +11280,7 @@ SQLITE_API void sqlite3_memdebug_settitle(const char *zTitle){ ** Open the file indicated and write a log of all unfreed memory ** allocations into that log. */ -SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ +SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){ FILE *out; struct MemBlockHdr *pHdr; void **pBt; @@ -9425,87 +11317,19 @@ SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ } /* -** This routine is used to simulate malloc failures. -** -** After calling this routine, there will be iFail successful -** memory allocations and then a failure. If iRepeat is 1 -** all subsequent memory allocations will fail. If iRepeat is -** 0, only a single allocation will fail. If iRepeat is negative -** then the previous setting for iRepeat is unchanged. -** -** Each call to this routine overrides the previous. To disable -** the simulated allocation failure mechanism, set iFail to -1. -** -** This routine returns the number of simulated failures that have -** occurred since the previous call. +** Return the number of times sqlite3_malloc() has been called. */ -SQLITE_API int sqlite3_memdebug_fail(int iFail, int iRepeat, int *piBenign){ - int n = mem.iFailCnt; - if( piBenign ){ - *piBenign = mem.iBenignFailCnt; - } - mem.iFail = iFail+1; - if( iRepeat>=0 ){ - mem.iReset = iRepeat; - } - mem.iFailCnt = 0; - mem.iBenignFailCnt = 0; - return n; -} - -SQLITE_API int sqlite3_memdebug_pending(){ - return (mem.iFail-1); -} - -/* -** The following three functions are used to indicate to the test -** infrastructure which malloc() calls may fail benignly without -** affecting functionality. This can happen when resizing hash tables -** (failing to resize a hash-table is a performance hit, but not an -** error) or sometimes during a rollback operation. -** -** If the argument is true, sqlite3MallocBenignFailure() indicates that the -** next call to allocate memory may fail benignly. -** -** If sqlite3MallocEnterBenignBlock() is called with a non-zero argument, -** then all memory allocations requested before the next call to -** sqlite3MallocLeaveBenignBlock() may fail benignly. -*/ -SQLITE_PRIVATE void sqlite3MallocBenignFailure(int isBenign){ - if( isBenign ){ - mem.iNextIsBenign = 1; - } -} -SQLITE_PRIVATE void sqlite3MallocEnterBenignBlock(int isBenign){ - if( isBenign ){ - mem.iIsBenign = 1; +SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){ + int i; + int nTotal = 0; + for(i=0; i0 ); - mem.disallow--; - sqlite3_mutex_leave(mem.mutex); -} -#endif /* SQLITE_MEMDEBUG && !SQLITE_OMIT_MEMORY_ALLOCATION */ +#endif /* SQLITE_MEMDEBUG */ /************** End of mem2.c ************************************************/ /************** Begin file mem3.c ********************************************/ @@ -9531,18 +11355,14 @@ SQLITE_PRIVATE void sqlite3MallocAllow(void){ ** This version of the memory allocation subsystem is used if ** and only if SQLITE_MEMORY_SIZE is defined. ** -** $Id: mem3.c,v 1.7 2007/11/29 18:36:49 drh Exp $ +** $Id: mem3.c,v 1.12 2008/02/19 15:15:16 drh Exp $ */ /* ** This version of the memory allocator is used only when ** SQLITE_MEMORY_SIZE is defined. */ -#if defined(SQLITE_MEMORY_SIZE) - -#ifdef SQLITE_MEMDEBUG -# error cannot define both SQLITE_MEMDEBUG and SQLITE_MEMORY_SIZE -#endif +#ifdef SQLITE_MEMORY_SIZE /* ** Maximum size (in Mem3Blocks) of a "small" chunk. @@ -9561,11 +11381,16 @@ SQLITE_PRIVATE void sqlite3MallocAllow(void){ ** a header that is not returned to the user. ** ** A chunk is two or more blocks that is either checked out or -** free. The first block has format u.hdr. u.hdr.size is the +** free. The first block has format u.hdr. u.hdr.size4x is 4 times the ** size of the allocation in blocks if the allocation is free. -** If the allocation is checked out, u.hdr.size is the negative -** of the size. Similarly, u.hdr.prevSize is the size of the -** immediately previous allocation. +** The u.hdr.size4x&1 bit is true if the chunk is checked out and +** false if the chunk is on the freelist. The u.hdr.size4x&2 bit +** is true if the previous chunk is checked out and false if the +** previous chunk is free. The u.hdr.prevSize field is the size of +** the previous chunk in blocks if the previous chunk is on the +** freelist. If the previous chunk is checked out, then +** u.hdr.prevSize can be part of the data for that chunk and should +** not be read or written. ** ** We often identify a chunk by its index in mem.aPool[]. When ** this is done, the chunk index refers to the second block of @@ -9579,18 +11404,19 @@ SQLITE_PRIVATE void sqlite3MallocAllow(void){ ** for smaller chunks and mem.aiHash[] for larger chunks. ** ** The second block of a chunk is user data if the chunk is checked -** out. +** out. If a chunk is checked out, the user data may extend into +** the u.hdr.prevSize value of the following chunk. */ typedef struct Mem3Block Mem3Block; struct Mem3Block { union { struct { - int prevSize; /* Size of previous chunk in Mem3Block elements */ - int size; /* Size of current chunk in Mem3Block elements */ + u32 prevSize; /* Size of previous chunk in Mem3Block elements */ + u32 size4x; /* 4x the size of current chunk in Mem3Block elements */ } hdr; struct { - int next; /* Index in mem.aPool[] of next free chunk */ - int prev; /* Index in mem.aPool[] of previous free chunk */ + u32 next; /* Index in mem.aPool[] of next free chunk */ + u32 prev; /* Index in mem.aPool[] of previous free chunk */ } list; } u; }; @@ -9615,7 +11441,7 @@ static struct { /* ** The minimum amount of free space that we have seen. */ - int mnMaster; + u32 mnMaster; /* ** iMaster is the index of the master chunk. Most new allocations @@ -9623,16 +11449,16 @@ static struct { ** of the current master. iMaster is 0 if there is not master chunk. ** The master chunk is not in either the aiHash[] or aiSmall[]. */ - int iMaster; - int szMaster; + u32 iMaster; + u32 szMaster; /* ** Array of lists of free blocks according to the block size ** for smaller chunks, or a hash on the block size for larger ** chunks. */ - int aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */ - int aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */ + u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */ + u32 aiHash[N_HASH]; /* For sizes MX_SMALL+1 and larger */ /* ** Memory available for allocation @@ -9644,9 +11470,9 @@ static struct { ** Unlink the chunk at mem.aPool[i] from list it is currently ** on. *pRoot is the list that i is a member of. */ -static void memsys3UnlinkFromList(int i, int *pRoot){ - int next = mem.aPool[i].u.list.next; - int prev = mem.aPool[i].u.list.prev; +static void memsys3UnlinkFromList(u32 i, u32 *pRoot){ + u32 next = mem.aPool[i].u.list.next; + u32 prev = mem.aPool[i].u.list.prev; assert( sqlite3_mutex_held(mem.mutex) ); if( prev==0 ){ *pRoot = next; @@ -9664,10 +11490,12 @@ static void memsys3UnlinkFromList(int i, int *pRoot){ ** Unlink the chunk at index i from ** whatever list is currently a member of. */ -static void memsys3Unlink(int i){ - int size, hash; +static void memsys3Unlink(u32 i){ + u32 size, hash; assert( sqlite3_mutex_held(mem.mutex) ); - size = mem.aPool[i-1].u.hdr.size; + assert( (mem.aPool[i-1].u.hdr.size4x & 1)==0 ); + assert( i>=1 ); + size = mem.aPool[i-1].u.hdr.size4x/4; assert( size==mem.aPool[i+size-1].u.hdr.prevSize ); assert( size>=2 ); if( size <= MX_SMALL ){ @@ -9682,7 +11510,7 @@ static void memsys3Unlink(int i){ ** Link the chunk at mem.aPool[i] so that is on the list rooted ** at *pRoot. */ -static void memsys3LinkIntoList(int i, int *pRoot){ +static void memsys3LinkIntoList(u32 i, u32 *pRoot){ assert( sqlite3_mutex_held(mem.mutex) ); mem.aPool[i].u.list.next = *pRoot; mem.aPool[i].u.list.prev = 0; @@ -9696,10 +11524,12 @@ static void memsys3LinkIntoList(int i, int *pRoot){ ** Link the chunk at index i into either the appropriate ** small chunk list, or into the large chunk hash table. */ -static void memsys3Link(int i){ - int size, hash; +static void memsys3Link(u32 i){ + u32 size, hash; assert( sqlite3_mutex_held(mem.mutex) ); - size = mem.aPool[i-1].u.hdr.size; + assert( i>=1 ); + assert( (mem.aPool[i-1].u.hdr.size4x & 1)==0 ); + size = mem.aPool[i-1].u.hdr.size4x/4; assert( size==mem.aPool[i+size-1].u.hdr.prevSize ); assert( size>=2 ); if( size <= MX_SMALL ){ @@ -9719,8 +11549,9 @@ static void memsys3Link(int i){ static void memsys3Enter(void){ if( mem.mutex==0 ){ mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); - mem.aPool[0].u.hdr.size = SQLITE_MEMORY_SIZE/8; + mem.aPool[0].u.hdr.size4x = SQLITE_MEMORY_SIZE/2 + 2; mem.aPool[SQLITE_MEMORY_SIZE/8].u.hdr.prevSize = SQLITE_MEMORY_SIZE/8; + mem.aPool[SQLITE_MEMORY_SIZE/8].u.hdr.size4x = 1; mem.iMaster = 1; mem.szMaster = SQLITE_MEMORY_SIZE/8; mem.mnMaster = mem.szMaster; @@ -9790,10 +11621,14 @@ static void memsys3OutOfMemory(int nByte){ ** size returned omits the 8-byte header overhead. This only ** works for chunks that are currently checked out. */ -static int memsys3Size(void *p){ - Mem3Block *pBlock = (Mem3Block*)p; - assert( pBlock[-1].u.hdr.size<0 ); - return (-1-pBlock[-1].u.hdr.size)*8; +SQLITE_PRIVATE int sqlite3MallocSize(void *p){ + int iSize = 0; + if( p ){ + Mem3Block *pBlock = (Mem3Block*)p; + assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); + iSize = (pBlock[-1].u.hdr.size4x&~3)*2 - 4; + } + return iSize; } /* @@ -9801,12 +11636,16 @@ static int memsys3Size(void *p){ ** size parameters for check-out and return a pointer to the ** user portion of the chunk. */ -static void *memsys3Checkout(int i, int nBlock){ +static void *memsys3Checkout(u32 i, int nBlock){ + u32 x; assert( sqlite3_mutex_held(mem.mutex) ); - assert( mem.aPool[i-1].u.hdr.size==nBlock ); + assert( i>=1 ); + assert( mem.aPool[i-1].u.hdr.size4x/4==nBlock ); assert( mem.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); - mem.aPool[i-1].u.hdr.size = -nBlock; - mem.aPool[i+nBlock-1].u.hdr.prevSize = -nBlock; + x = mem.aPool[i-1].u.hdr.size4x; + mem.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); + mem.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; + mem.aPool[i+nBlock-1].u.hdr.size4x |= 2; return &mem.aPool[i]; } @@ -9827,14 +11666,16 @@ static void *memsys3FromMaster(int nBlock){ return p; }else{ /* Split the master block. Return the tail. */ - int newi; + u32 newi, x; newi = mem.iMaster + mem.szMaster - nBlock; assert( newi > mem.iMaster+1 ); - mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.prevSize = -nBlock; - mem.aPool[newi-1].u.hdr.size = -nBlock; + mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.prevSize = nBlock; + mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.size4x |= 2; + mem.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; mem.szMaster -= nBlock; mem.aPool[newi-1].u.hdr.prevSize = mem.szMaster; - mem.aPool[mem.iMaster-1].u.hdr.size = mem.szMaster; + x = mem.aPool[mem.iMaster-1].u.hdr.size4x & 2; + mem.aPool[mem.iMaster-1].u.hdr.size4x = mem.szMaster*4 | x; if( mem.szMaster < mem.mnMaster ){ mem.mnMaster = mem.szMaster; } @@ -9858,27 +11699,30 @@ static void *memsys3FromMaster(int nBlock){ ** chunk before invoking this routine, then must unlink the (possibly ** changed) master chunk once this routine has finished. */ -static void memsys3Merge(int *pRoot){ - int iNext, prev, size, i; +static void memsys3Merge(u32 *pRoot){ + u32 iNext, prev, size, i, x; assert( sqlite3_mutex_held(mem.mutex) ); for(i=*pRoot; i>0; i=iNext){ iNext = mem.aPool[i].u.list.next; - size = mem.aPool[i-1].u.hdr.size; - assert( size>0 ); - if( mem.aPool[i-1].u.hdr.prevSize>0 ){ + size = mem.aPool[i-1].u.hdr.size4x; + assert( (size&1)==0 ); + if( (size&2)==0 ){ memsys3UnlinkFromList(i, pRoot); + assert( i > mem.aPool[i-1].u.hdr.prevSize ); prev = i - mem.aPool[i-1].u.hdr.prevSize; - assert( prev>=0 ); if( prev==iNext ){ iNext = mem.aPool[prev].u.list.next; } memsys3Unlink(prev); - size = i + size - prev; - mem.aPool[prev-1].u.hdr.size = size; + size = i + size/4 - prev; + x = mem.aPool[prev-1].u.hdr.size4x & 2; + mem.aPool[prev-1].u.hdr.size4x = size*4 | x; mem.aPool[prev+size-1].u.hdr.prevSize = size; memsys3Link(prev); i = prev; + }else{ + size /= 4; } if( size>mem.szMaster ){ mem.iMaster = i; @@ -9892,16 +11736,16 @@ static void memsys3Merge(int *pRoot){ ** Return NULL if unable. */ static void *memsys3Malloc(int nByte){ - int i; + u32 i; int nBlock; int toFree; assert( sqlite3_mutex_held(mem.mutex) ); assert( sizeof(Mem3Block)==8 ); - if( nByte<=0 ){ + if( nByte<=12 ){ nBlock = 2; }else{ - nBlock = (nByte + 15)/8; + nBlock = (nByte + 11)/8; } assert( nBlock >= 2 ); @@ -9919,7 +11763,7 @@ static void *memsys3Malloc(int nByte){ }else{ int hash = nBlock % N_HASH; for(i=mem.aiHash[hash]; i>0; i=mem.aPool[i].u.list.next){ - if( mem.aPool[i-1].u.hdr.size==nBlock ){ + if( mem.aPool[i-1].u.hdr.size4x/4==nBlock ){ memsys3UnlinkFromList(i, &mem.aiHash[hash]); return memsys3Checkout(i, nBlock); } @@ -9973,31 +11817,34 @@ static void *memsys3Malloc(int nByte){ void memsys3Free(void *pOld){ Mem3Block *p = (Mem3Block*)pOld; int i; - int size; + u32 size, x; assert( sqlite3_mutex_held(mem.mutex) ); assert( p>mem.aPool && p<&mem.aPool[SQLITE_MEMORY_SIZE/8] ); i = p - mem.aPool; - size = -mem.aPool[i-1].u.hdr.size; - assert( size>=2 ); - assert( mem.aPool[i+size-1].u.hdr.prevSize==-size ); - mem.aPool[i-1].u.hdr.size = size; + assert( (mem.aPool[i-1].u.hdr.size4x&1)==1 ); + size = mem.aPool[i-1].u.hdr.size4x/4; + assert( i+size<=SQLITE_MEMORY_SIZE/8+1 ); + mem.aPool[i-1].u.hdr.size4x &= ~1; mem.aPool[i+size-1].u.hdr.prevSize = size; + mem.aPool[i+size-1].u.hdr.size4x &= ~2; memsys3Link(i); /* Try to expand the master using the newly freed chunk */ if( mem.iMaster ){ - while( mem.aPool[mem.iMaster-1].u.hdr.prevSize>0 ){ + while( (mem.aPool[mem.iMaster-1].u.hdr.size4x&2)==0 ){ size = mem.aPool[mem.iMaster-1].u.hdr.prevSize; mem.iMaster -= size; mem.szMaster += size; memsys3Unlink(mem.iMaster); - mem.aPool[mem.iMaster-1].u.hdr.size = mem.szMaster; + x = mem.aPool[mem.iMaster-1].u.hdr.size4x & 2; + mem.aPool[mem.iMaster-1].u.hdr.size4x = mem.szMaster*4 | x; mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.prevSize = mem.szMaster; } - while( mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.size>0 ){ + x = mem.aPool[mem.iMaster-1].u.hdr.size4x & 2; + while( (mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.size4x&1)==0 ){ memsys3Unlink(mem.iMaster+mem.szMaster); - mem.szMaster += mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.size; - mem.aPool[mem.iMaster-1].u.hdr.size = mem.szMaster; + mem.szMaster += mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.size4x/4; + mem.aPool[mem.iMaster-1].u.hdr.size4x = mem.szMaster*4 | x; mem.aPool[mem.iMaster+mem.szMaster-1].u.hdr.prevSize = mem.szMaster; } } @@ -10043,7 +11890,7 @@ SQLITE_API void *sqlite3_realloc(void *pPrior, int nBytes){ return 0; } assert( mem.mutex!=0 ); - nOld = memsys3Size(pPrior); + nOld = sqlite3MallocSize(pPrior); if( nBytes<=nOld && nBytes>=nOld-128 ){ return pPrior; } @@ -10065,10 +11912,11 @@ SQLITE_API void *sqlite3_realloc(void *pPrior, int nBytes){ ** Open the file indicated and write a log of all unfreed memory ** allocations into that log. */ -SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ +SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){ #ifdef SQLITE_DEBUG FILE *out; - int i, j, size; + int i, j; + u32 size; if( zFilename==0 || zFilename[0]==0 ){ out = stdout; }else{ @@ -10081,23 +11929,27 @@ SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ } memsys3Enter(); fprintf(out, "CHUNKS:\n"); - for(i=1; i<=SQLITE_MEMORY_SIZE/8; i+=size){ - size = mem.aPool[i-1].u.hdr.size; - if( size>=-1 && size<=1 ){ + for(i=1; i<=SQLITE_MEMORY_SIZE/8; i+=size/4){ + size = mem.aPool[i-1].u.hdr.size4x; + if( size/4<=1 ){ fprintf(out, "%p size error\n", &mem.aPool[i]); assert( 0 ); break; } - if( mem.aPool[i+(size<0?-size:size)-1].u.hdr.prevSize!=size ){ + if( (size&1)==0 && mem.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ fprintf(out, "%p tail size does not match\n", &mem.aPool[i]); assert( 0 ); break; } - if( size<0 ){ - size = -size; - fprintf(out, "%p %6d bytes checked out\n", &mem.aPool[i], size*8-8); + if( ((mem.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ + fprintf(out, "%p tail checkout bit is incorrect\n", &mem.aPool[i]); + assert( 0 ); + break; + } + if( size&1 ){ + fprintf(out, "%p %6d bytes checked out\n", &mem.aPool[i], (size/4)*8-8); }else{ - fprintf(out, "%p %6d bytes free%s\n", &mem.aPool[i], size*8-8, + fprintf(out, "%p %6d bytes free%s\n", &mem.aPool[i], (size/4)*8-8, i==mem.iMaster ? " **master**" : ""); } } @@ -10105,7 +11957,8 @@ SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ if( mem.aiSmall[i]==0 ) continue; fprintf(out, "small(%2d):", i); for(j = mem.aiSmall[i]; j>0; j=mem.aPool[j].u.list.next){ - fprintf(out, " %p(%d)", &mem.aPool[j], mem.aPool[j-1].u.hdr.size*8-8); + fprintf(out, " %p(%d)", &mem.aPool[j], + (mem.aPool[j-1].u.hdr.size4x/4)*8-8); } fprintf(out, "\n"); } @@ -10113,7 +11966,8 @@ SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ if( mem.aiHash[i]==0 ) continue; fprintf(out, "hash(%2d):", i); for(j = mem.aiHash[i]; j>0; j=mem.aPool[j].u.list.next){ - fprintf(out, " %p(%d)", &mem.aPool[j], mem.aPool[j-1].u.hdr.size*8-8); + fprintf(out, " %p(%d)", &mem.aPool[j], + (mem.aPool[j-1].u.hdr.size4x/4)*8-8); } fprintf(out, "\n"); } @@ -10133,6 +11987,519 @@ SQLITE_API void sqlite3_memdebug_dump(const char *zFilename){ #endif /* !SQLITE_MEMORY_SIZE */ /************** End of mem3.c ************************************************/ +/************** Begin file mem5.c ********************************************/ +/* +** 2007 October 14 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement a memory +** allocation subsystem for use by SQLite. +** +** This version of the memory allocation subsystem omits all +** use of malloc(). All dynamically allocatable memory is +** contained in a static array, mem.aPool[]. The size of this +** fixed memory pool is SQLITE_POW2_MEMORY_SIZE bytes. +** +** This version of the memory allocation subsystem is used if +** and only if SQLITE_POW2_MEMORY_SIZE is defined. +** +** $Id: mem5.c,v 1.4 2008/02/19 15:15:16 drh Exp $ +*/ + +/* +** This version of the memory allocator is used only when +** SQLITE_POW2_MEMORY_SIZE is defined. +*/ +#ifdef SQLITE_POW2_MEMORY_SIZE + +/* +** Log2 of the minimum size of an allocation. For example, if +** 4 then all allocations will be rounded up to at least 16 bytes. +** If 5 then all allocations will be rounded up to at least 32 bytes. +*/ +#ifndef SQLITE_POW2_LOGMIN +# define SQLITE_POW2_LOGMIN 6 +#endif +#define POW2_MIN (1<=0 && i=0 && iLogsize=0 ){ + mem.aPool[next].u.list.prev = prev; + } +} + +/* +** Link the chunk at mem.aPool[i] so that is on the iLogsize +** free list. +*/ +static void memsys5Link(int i, int iLogsize){ + int x; + assert( sqlite3_mutex_held(mem.mutex) ); + assert( i>=0 && i=0 && iLogsize=0 ){ + assert( x=POW2_MAX ); + mem.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); + sqlite3_mutex_enter(mem.mutex); + for(i=0; i=0 && i=0 && iLogsize=0 ); + while( i>0 ){ + if( imem.maxRequest ){ + mem.maxRequest = nByte; + } + + /* Simulate a memory allocation fault */ + if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ) return 0; + + /* Round nByte up to the next valid power of two */ + if( nByte>POW2_MAX ) return 0; + for(iFullSz=POW2_MIN, iLogsize=0; iFullSz=mem.alarmThreshold ){ + memsys5Alarm(iFullSz); + } + + /* Make sure mem.aiFreelist[iLogsize] contains at least one free + ** block. If not, then split a block of the next larger power of + ** two in order to create a new free block of size iLogsize. + */ + for(iBin=iLogsize; mem.aiFreelist[iBin]<0 && iBin=NSIZE ) return 0; + i = memsys5UnlinkFirst(iBin); + while( iBin>iLogsize ){ + int newSize; + + iBin--; + newSize = 1 << iBin; + mem.aCtrl[i+newSize] = CTRL_FREE | iBin; + memsys5Link(i+newSize, iBin); + } + mem.aCtrl[i] = iLogsize; + + /* Update allocator performance statistics. */ + mem.nAlloc++; + mem.totalAlloc += iFullSz; + mem.totalExcess += iFullSz - nByte; + mem.currentCount++; + mem.currentOut += iFullSz; + if( mem.maxCount=0 && i0 ); + assert( mem.currentOut>=0 ); + mem.currentCount--; + mem.currentOut -= size*POW2_MIN; + assert( mem.currentOut>0 || mem.currentCount==0 ); + assert( mem.currentCount>0 || mem.currentOut==0 ); + + mem.aCtrl[i] = CTRL_FREE | iLogsize; + while( iLogsize>iLogsize) & 1 ){ + iBuddy = i - size; + }else{ + iBuddy = i + size; + } + assert( iBuddy>=0 && iBuddy0 ){ + memsys5Enter(); + p = memsys5Malloc(nBytes); + sqlite3_mutex_leave(mem.mutex); + } + return (void*)p; +} + +/* +** Free memory. +*/ +SQLITE_API void sqlite3_free(void *pPrior){ + if( pPrior==0 ){ + return; + } + assert( mem.mutex!=0 ); + sqlite3_mutex_enter(mem.mutex); + memsys5Free(pPrior); + sqlite3_mutex_leave(mem.mutex); +} + +/* +** Change the size of an existing memory allocation +*/ +SQLITE_API void *sqlite3_realloc(void *pPrior, int nBytes){ + int nOld; + void *p; + if( pPrior==0 ){ + return sqlite3_malloc(nBytes); + } + if( nBytes<=0 ){ + sqlite3_free(pPrior); + return 0; + } + assert( mem.mutex!=0 ); + nOld = sqlite3MallocSize(pPrior); + if( nBytes<=nOld ){ + return pPrior; + } + sqlite3_mutex_enter(mem.mutex); + p = memsys5Malloc(nBytes); + if( p ){ + memcpy(p, pPrior, nOld); + memsys5Free(pPrior); + } + sqlite3_mutex_leave(mem.mutex); + return p; +} + +/* +** Open the file indicated and write a log of all unfreed memory +** allocations into that log. +*/ +SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){ +#ifdef SQLITE_DEBUG + FILE *out; + int i, j, n; + + if( zFilename==0 || zFilename[0]==0 ){ + out = stdout; + }else{ + out = fopen(zFilename, "w"); + if( out==0 ){ + fprintf(stderr, "** Unable to output memory debug output log: %s **\n", + zFilename); + return; + } + } + memsys5Enter(); + for(i=0; i=0; j = mem.aPool[j].u.list.next, n++){} + fprintf(out, "freelist items of size %d: %d\n", POW2_MIN << i, n); + } + fprintf(out, "mem.nAlloc = %llu\n", mem.nAlloc); + fprintf(out, "mem.totalAlloc = %llu\n", mem.totalAlloc); + fprintf(out, "mem.totalExcess = %llu\n", mem.totalExcess); + fprintf(out, "mem.currentOut = %u\n", mem.currentOut); + fprintf(out, "mem.currentCount = %u\n", mem.currentCount); + fprintf(out, "mem.maxOut = %u\n", mem.maxOut); + fprintf(out, "mem.maxCount = %u\n", mem.maxCount); + fprintf(out, "mem.maxRequest = %u\n", mem.maxRequest); + sqlite3_mutex_leave(mem.mutex); + if( out==stdout ){ + fflush(stdout); + }else{ + fclose(out); + } +#endif +} + + +#endif /* !SQLITE_POW2_MEMORY_SIZE */ + +/************** End of mem5.c ************************************************/ /************** Begin file mutex.c *******************************************/ /* ** 2007 August 14 @@ -10275,7 +12642,7 @@ SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){ ************************************************************************* ** This file contains the C functions that implement mutexes for OS/2 ** -** $Id: mutex_os2.c,v 1.3 2007/10/02 19:56:04 pweilbacher Exp $ +** $Id: mutex_os2.c,v 1.5 2008/02/01 19:42:38 pweilbacher Exp $ */ /* @@ -10294,13 +12661,14 @@ SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){ ** Each recursive mutex is an instance of the following structure. */ struct sqlite3_mutex { - PSZ mutexName; /* Mutex name controlling the lock */ HMTX mutex; /* Mutex controlling the lock */ int id; /* Mutex type */ int nRef; /* Number of references */ TID owner; /* Thread holding this mutex */ }; +#define OS2_MUTEX_INITIALIZER 0,0,0,0 + /* ** The sqlite3_mutex_alloc() routine allocates a new ** mutex and returns a pointer to it. If it returns NULL @@ -10341,46 +12709,55 @@ struct sqlite3_mutex { ** the same type number. */ SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int iType){ - PSZ mutex_name = "\\SEM32\\SQLITE\\MUTEX"; - int mutex_name_len = strlen(mutex_name) + 1; /* name length + null byte */ - sqlite3_mutex *p; - + sqlite3_mutex *p = NULL; switch( iType ){ case SQLITE_MUTEX_FAST: case SQLITE_MUTEX_RECURSIVE: { p = sqlite3MallocZero( sizeof(*p) ); if( p ){ - p->mutexName = (PSZ)malloc(mutex_name_len); - sqlite3_snprintf(mutex_name_len, p->mutexName, "%s", mutex_name); p->id = iType; - DosCreateMutexSem(p->mutexName, &p->mutex, 0, FALSE); - DosOpenMutexSem(p->mutexName, &p->mutex); + if( DosCreateMutexSem( 0, &p->mutex, 0, FALSE ) != NO_ERROR ){ + sqlite3_free( p ); + p = NULL; + } } break; } default: { - static sqlite3_mutex staticMutexes[5]; - static int isInit = 0; - while( !isInit ) { - static long lock = 0; - DosEnterCritSec(); - lock++; - if( lock == 1 ) { - DosExitCritSec(); - int i; - for(i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++) { - staticMutexes[i].mutexName = (PSZ)malloc(mutex_name_len + 1); - sqlite3_snprintf(mutex_name_len + 1, /* one more for the number */ - staticMutexes[i].mutexName, "%s%1d", mutex_name, i); - DosCreateMutexSem(staticMutexes[i].mutexName, - &staticMutexes[i].mutex, 0, FALSE); - DosOpenMutexSem(staticMutexes[i].mutexName, - &staticMutexes[i].mutex); + static volatile int isInit = 0; + static sqlite3_mutex staticMutexes[] = { + { OS2_MUTEX_INITIALIZER, }, + { OS2_MUTEX_INITIALIZER, }, + { OS2_MUTEX_INITIALIZER, }, + { OS2_MUTEX_INITIALIZER, }, + { OS2_MUTEX_INITIALIZER, }, + }; + if ( !isInit ){ + APIRET rc; + PTIB ptib; + PPIB ppib; + HMTX mutex; + char name[32]; + DosGetInfoBlocks( &ptib, &ppib ); + sqlite3_snprintf( sizeof(name), name, "\\SEM32\\SQLITE%04x", + ppib->pib_ulpid ); + while( !isInit ){ + mutex = 0; + rc = DosCreateMutexSem( name, &mutex, 0, FALSE); + if( rc == NO_ERROR ){ + int i; + if( !isInit ){ + for( i = 0; i < sizeof(staticMutexes)/sizeof(staticMutexes[0]); i++ ){ + DosCreateMutexSem( 0, &staticMutexes[i].mutex, 0, FALSE ); + } + isInit = 1; + } + DosCloseMutexSem( mutex ); + }else if( rc == ERROR_DUPLICATE_NAME ){ + DosSleep( 1 ); + }else{ + return p; } - isInit = 1; - } else { - DosExitCritSec(); - DosSleep(1); } } assert( iType-2 >= 0 ); @@ -10402,9 +12779,8 @@ SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){ assert( p ); assert( p->nRef==0 ); assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ); - DosCloseMutexSem(p->mutex); - free(p->mutexName); - sqlite3_free(p); + DosCloseMutexSem( p->mutex ); + sqlite3_free( p ); } /* @@ -12183,10 +14559,19 @@ SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ ** Random numbers are used by some of the database backends in order ** to generate random integer keys for tables or random filenames. ** -** $Id: random.c,v 1.20 2007/08/21 13:51:23 drh Exp $ +** $Id: random.c,v 1.21 2008/01/16 17:46:38 drh Exp $ */ +/* All threads share a single random number generator. +** This structure is the current state of the generator. +*/ +static struct sqlite3PrngType { + unsigned char isInit; /* True if initialized */ + unsigned char i, j; /* State variables */ + unsigned char s[256]; /* State variables */ +} sqlite3Prng; + /* ** Get a single 8-bit random value from the RC4 PRNG. The Mutex ** must be held while executing this routine. @@ -12206,14 +14591,6 @@ SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ static int randomByte(void){ unsigned char t; - /* All threads share a single random number generator. - ** This structure is the current state of the generator. - */ - static struct { - unsigned char isInit; /* True if initialized */ - unsigned char i, j; /* State variables */ - unsigned char s[256]; /* State variables */ - } prng; /* Initialize the state of the random number generator once, ** the first time this routine is called. The seed value does @@ -12224,33 +14601,33 @@ static int randomByte(void){ ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random ** number generator) not as an encryption device. */ - if( !prng.isInit ){ + if( !sqlite3Prng.isInit ){ int i; char k[256]; - prng.j = 0; - prng.i = 0; + sqlite3Prng.j = 0; + sqlite3Prng.i = 0; sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); for(i=0; i<256; i++){ - prng.s[i] = i; + sqlite3Prng.s[i] = i; } for(i=0; i<256; i++){ - prng.j += prng.s[i] + k[i]; - t = prng.s[prng.j]; - prng.s[prng.j] = prng.s[i]; - prng.s[i] = t; + sqlite3Prng.j += sqlite3Prng.s[i] + k[i]; + t = sqlite3Prng.s[sqlite3Prng.j]; + sqlite3Prng.s[sqlite3Prng.j] = sqlite3Prng.s[i]; + sqlite3Prng.s[i] = t; } - prng.isInit = 1; + sqlite3Prng.isInit = 1; } /* Generate and return single random byte */ - prng.i++; - t = prng.s[prng.i]; - prng.j += t; - prng.s[prng.i] = prng.s[prng.j]; - prng.s[prng.j] = t; - t += prng.s[prng.i]; - return prng.s[t]; + sqlite3Prng.i++; + t = sqlite3Prng.s[sqlite3Prng.i]; + sqlite3Prng.j += t; + sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j]; + sqlite3Prng.s[sqlite3Prng.j] = t; + t += sqlite3Prng.s[sqlite3Prng.i]; + return sqlite3Prng.s[t]; } /* @@ -12269,6 +14646,23 @@ SQLITE_PRIVATE void sqlite3Randomness(int N, void *pBuf){ sqlite3_mutex_leave(mutex); } +#ifdef SQLITE_TEST +/* +** For testing purposes, we sometimes want to preserve the state of +** PRNG and restore the PRNG to its saved state at a later time. +*/ +static struct sqlite3PrngType sqlite3SavedPrng; +SQLITE_PRIVATE void sqlite3SavePrngState(void){ + memcpy(&sqlite3SavedPrng, &sqlite3Prng, sizeof(sqlite3Prng)); +} +SQLITE_PRIVATE void sqlite3RestorePrngState(void){ + memcpy(&sqlite3Prng, &sqlite3SavedPrng, sizeof(sqlite3Prng)); +} +SQLITE_PRIVATE void sqlite3ResetPrngState(void){ + sqlite3Prng.isInit = 0; +} +#endif /* SQLITE_TEST */ + /************** End of random.c **********************************************/ /************** Begin file utf.c *********************************************/ /* @@ -12285,7 +14679,7 @@ SQLITE_PRIVATE void sqlite3Randomness(int N, void *pBuf){ ** This file contains routines used to translate between UTF-8, ** UTF-16, UTF-16BE, and UTF-16LE. ** -** $Id: utf.c,v 1.59 2007/10/03 08:46:45 danielk1977 Exp $ +** $Id: utf.c,v 1.60 2008/02/13 18:25:27 danielk1977 Exp $ ** ** Notes on UTF-8: ** @@ -12407,13 +14801,6 @@ struct Cursor { typedef struct Cursor Cursor; /* -** Number of bytes of string storage space available to each stack -** layer without having to malloc. NBFS is short for Number of Bytes -** For Strings. -*/ -#define NBFS 32 - -/* ** A value for Cursor.cacheValid that means the cache is always invalid. */ #define CACHE_STALE 0 @@ -12437,14 +14824,12 @@ struct Mem { double r; /* Real value */ sqlite3 *db; /* The associated database connection */ char *z; /* String or BLOB value */ - int n; /* Number of characters in string value, including '\0' */ + int n; /* Number of characters in string value, excluding '\0' */ u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */ u8 type; /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */ u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */ void (*xDel)(void *); /* If not null, call this function to delete Mem.z */ - char zShort[NBFS]; /* Space for short strings */ }; -typedef struct Mem Mem; /* One or more of the following flags are set to indicate the validOK ** representations of the value stored in the Mem struct. @@ -12467,6 +14852,9 @@ typedef struct Mem Mem; #define MEM_Real 0x0008 /* Value is a real number */ #define MEM_Blob 0x0010 /* Value is a BLOB */ +#define MemSetTypeFlag(p, f) \ + ((p)->flags = ((p)->flags&~(MEM_Int|MEM_Real|MEM_Null|MEM_Blob|MEM_Str))|f) + /* Whenever Mem contains a valid string or blob representation, one of ** the following flags must be set to determine the memory management ** policy for Mem.z. The MEM_Term flag tells us whether or not the @@ -12476,7 +14864,6 @@ typedef struct Mem Mem; #define MEM_Dyn 0x0040 /* Need to call sqliteFree() on Mem.z */ #define MEM_Static 0x0080 /* Mem.z points to a static string */ #define MEM_Ephem 0x0100 /* Mem.z points to an ephemeral string */ -#define MEM_Short 0x0200 /* Mem.z points to Mem.zShort */ #define MEM_Agg 0x0400 /* Mem.z points to an agg function context */ #define MEM_Zero 0x0800 /* Mem.i contains count of 0s appended to blob */ @@ -12503,7 +14890,6 @@ struct VdbeFunc { void (*xDelete)(void *); /* Destructor for the aux data */ } apAux[1]; /* One slot for each function argument */ }; -typedef struct VdbeFunc VdbeFunc; /* ** The "context" argument for a installable function. A pointer to an @@ -12523,7 +14909,7 @@ struct sqlite3_context { VdbeFunc *pVdbeFunc; /* Auxilary data, if created. */ Mem s; /* The return value is stored here */ Mem *pMem; /* Memory cell used to store aggregate context */ - u8 isError; /* Set to true for an error */ + int isError; /* Error code returned by the function. */ CollSeq *pColl; /* Collating sequence */ }; @@ -12605,8 +14991,6 @@ struct Vdbe { int nLabel; /* Number of labels used */ int nLabelAlloc; /* Number of slots allocated in aLabel[] */ int *aLabel; /* Space to hold the labels */ - Mem *aStack; /* The operand stack, except string values */ - Mem *pTos; /* Top entry in the operand stack */ Mem **apArg; /* Arguments to currently executing user function */ Mem *aColName; /* Column names to return */ int nCursor; /* Number of slots in apCsr[] */ @@ -12633,9 +15017,8 @@ struct Vdbe { int returnDepth; /* Next unused element in returnStack[] */ int nResColumn; /* Number of columns in one row of the result set */ char **azResColumn; /* Values for one row of result */ - int popStack; /* Pop the stack this much on entry to VdbeExec() */ char *zErrMsg; /* Error message written here */ - u8 resOnStack; /* True if there are result values on the stack */ + Mem *pResultSet; /* Pointer to an array of results */ u8 explain; /* True if EXPLAIN present on SQL command */ u8 changeCntOn; /* True to update the change-counter */ u8 aborted; /* True if ROLLBACK in another VM causes an abort */ @@ -12694,7 +15077,7 @@ SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int); SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*); SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*); SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int); -SQLITE_PRIVATE int sqlite3VdbeMemMove(Mem*, Mem*); +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*); SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*); SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*)); SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64); @@ -12714,10 +15097,11 @@ SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*); SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p); SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*); SQLITE_PRIVATE const char *sqlite3OpcodeName(int); +SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int, int); +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve); #ifndef NDEBUG SQLITE_PRIVATE void sqlite3VdbeMemSanity(Mem*); -SQLITE_PRIVATE int sqlite3VdbeOpcodeNoPush(u8); #endif SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8); #ifdef SQLITE_DEBUG @@ -12890,7 +15274,6 @@ SQLITE_PRIVATE int sqlite3Utf8Read( ** encoding, or if *pMem does not contain a string value. */ SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ - unsigned char zShort[NBFS]; /* Temporary short output buffer */ int len; /* Maximum length of output string in bytes */ unsigned char *zOut; /* Output buffer */ unsigned char *zIn; /* Input iterator */ @@ -12956,19 +15339,14 @@ SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ /* Set zIn to point at the start of the input buffer and zTerm to point 1 ** byte past the end. ** - ** Variable zOut is set to point at the output buffer. This may be space - ** obtained from sqlite3_malloc(), or Mem.zShort, if it large enough and - ** not in use, or the zShort array on the stack (see above). + ** Variable zOut is set to point at the output buffer, space obtained + ** from sqlite3_malloc(). */ zIn = (u8*)pMem->z; zTerm = &zIn[pMem->n]; - if( len>NBFS ){ - zOut = sqlite3DbMallocRaw(pMem->db, len); - if( !zOut ){ - return SQLITE_NOMEM; - } - }else{ - zOut = zShort; + zOut = sqlite3DbMallocRaw(pMem->db, len); + if( !zOut ){ + return SQLITE_NOMEM; } z = zOut; @@ -13010,15 +15388,9 @@ SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){ assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); sqlite3VdbeMemRelease(pMem); - pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); + pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem); pMem->enc = desiredEnc; - if( zOut==zShort ){ - memcpy(pMem->zShort, zOut, len); - zOut = (u8*)pMem->zShort; - pMem->flags |= (MEM_Term|MEM_Short); - }else{ - pMem->flags |= (MEM_Term|MEM_Dyn); - } + pMem->flags |= (MEM_Term|MEM_Dyn); pMem->z = (char*)zOut; translate_out: @@ -13057,24 +15429,14 @@ SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){ } if( bom ){ - /* This function is called as soon as a string is stored in a Mem*, - ** from within sqlite3VdbeMemSetStr(). At that point it is not possible - ** for the string to be stored in Mem.zShort, or for it to be stored - ** in dynamic memory with no destructor. - */ - assert( !(pMem->flags&MEM_Short) ); - assert( !(pMem->flags&MEM_Dyn) || pMem->xDel ); - if( pMem->flags & MEM_Dyn ){ - void (*xDel)(void*) = pMem->xDel; - char *z = pMem->z; - pMem->z = 0; - pMem->xDel = 0; - rc = sqlite3VdbeMemSetStr(pMem, &z[2], pMem->n-2, bom, - SQLITE_TRANSIENT); - xDel(z); - }else{ - rc = sqlite3VdbeMemSetStr(pMem, &pMem->z[2], pMem->n-2, bom, - SQLITE_TRANSIENT); + rc = sqlite3VdbeMemMakeWriteable(pMem); + if( rc==SQLITE_OK ){ + pMem->n -= 2; + memmove(pMem->z, &pMem->z[2], pMem->n); + pMem->z[pMem->n] = '\0'; + pMem->z[pMem->n+1] = '\0'; + pMem->flags |= MEM_Term; + pMem->enc = bom; } } return rc; @@ -13264,7 +15626,7 @@ SQLITE_PRIVATE void sqlite3UtfSelfTest(){ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.213 2007/10/23 15:39:45 drh Exp $ +** $Id: util.c,v 1.216 2008/01/23 03:03:05 drh Exp $ */ @@ -13691,25 +16053,6 @@ SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){ } /* -** Check to make sure we have a valid db pointer. This test is not -** foolproof but it does provide some measure of protection against -** misuse of the interface such as passing in db pointers that are -** NULL or which have been previously closed. If this routine returns -** TRUE it means that the db pointer is invalid and should not be -** dereferenced for any reason. The calling function should invoke -** SQLITE_MISUSE immediately. -*/ -SQLITE_PRIVATE int sqlite3SafetyCheck(sqlite3 *db){ - int magic; - if( db==0 ) return 1; - magic = db->magic; - if( magic!=SQLITE_MAGIC_CLOSED && - magic!=SQLITE_MAGIC_OPEN && - magic!=SQLITE_MAGIC_BUSY ) return 1; - return 0; -} - -/* ** The variable-length integer encoding is as follows: ** ** KEY: @@ -13884,17 +16227,17 @@ static int hexToInt(int h){ ** binary value has been obtained from malloc and must be freed by ** the calling routine. */ -SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z){ +SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ char *zBlob; int i; - int n = strlen(z); - if( n%2 ) return 0; - zBlob = (char *)sqlite3DbMallocRaw(db, n/2); + zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); + n--; if( zBlob ){ for(i=0; imagic==SQLITE_MAGIC_OPEN ){ db->magic = SQLITE_MAGIC_BUSY; @@ -13936,22 +16280,57 @@ SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3 *db){ } return 1; } +#endif /* ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN. ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY ** when this routine is called. */ +#ifdef SQLITE_DEBUG SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3 *db){ if( db->magic==SQLITE_MAGIC_BUSY ){ db->magic = SQLITE_MAGIC_OPEN; return 0; - }else { + }else{ db->magic = SQLITE_MAGIC_ERROR; db->u1.isInterrupted = 1; return 1; } } +#endif + +/* +** Check to make sure we have a valid db pointer. This test is not +** foolproof but it does provide some measure of protection against +** misuse of the interface such as passing in db pointers that are +** NULL or which have been previously closed. If this routine returns +** 1 it means that the db pointer is valid and 0 if it should not be +** dereferenced for any reason. The calling function should invoke +** SQLITE_MISUSE immediately. +** +** sqlite3SafetyCheckOk() requires that the db pointer be valid for +** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to +** open properly and is not fit for general use but which can be +** used as an argument to sqlite3_errmsg() or sqlite3_close(). +*/ +SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){ + int magic; + if( db==0 ) return 0; + magic = db->magic; + if( magic!=SQLITE_MAGIC_OPEN && + magic!=SQLITE_MAGIC_BUSY ) return 0; + return 1; +} +SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ + int magic; + if( db==0 ) return 0; + magic = db->magic; + if( magic!=SQLITE_MAGIC_SICK && + magic!=SQLITE_MAGIC_OPEN && + magic!=SQLITE_MAGIC_BUSY ) return 0; + return 1; +} /************** End of util.c ************************************************/ /************** Begin file hash.c ********************************************/ @@ -13969,7 +16348,7 @@ SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3 *db){ ** This is the implementation of generic hash-tables ** used in SQLite. ** -** $Id: hash.c,v 1.24 2007/09/04 14:31:47 danielk1977 Exp $ +** $Id: hash.c,v 1.26 2008/02/18 22:24:58 drh Exp $ */ /* Turn bulk memory into a hash table object by initializing the @@ -14176,16 +16555,22 @@ static void rehash(Hash *pH, int new_size){ HashElem *elem, *next_elem; /* For looping over existing elements */ int (*xHash)(const void*,int); /* The hash function */ - assert( (new_size & (new_size-1))==0 ); +#ifdef SQLITE_MALLOC_SOFT_LIMIT + if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ + new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); + } + if( new_size==pH->htsize ) return; +#endif /* There is a call to sqlite3_malloc() inside rehash(). If there is ** already an allocation at pH->ht, then if this malloc() fails it ** is benign (since failing to resize a hash table is a performance ** hit only, not a fatal error). */ - sqlite3MallocBenignFailure(pH->htsize>0); - + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pH->htsize>0); new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) ); + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0); + if( new_ht==0 ) return; if( pH->ht ) sqlite3_free(pH->ht); pH->ht = new_ht; @@ -14278,8 +16663,7 @@ SQLITE_PRIVATE HashElem *sqlite3HashFindElem(const Hash *pH, const void *pKey, i xHash = hashFunction(pH->keyClass); assert( xHash!=0 ); h = (*xHash)(pKey,nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - elem = findElementGivenHash(pH,pKey,nKey, h & (pH->htsize-1)); + elem = findElementGivenHash(pH,pKey,nKey, h % pH->htsize); return elem; } @@ -14319,21 +16703,22 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, voi xHash = hashFunction(pH->keyClass); assert( xHash!=0 ); hraw = (*xHash)(pKey, nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - elem = findElementGivenHash(pH,pKey,nKey,h); - if( elem ){ - void *old_data = elem->data; - if( data==0 ){ - removeElementGivenHash(pH,elem,h); - }else{ - elem->data = data; - if( !pH->copyKey ){ - elem->pKey = (void *)pKey; + if( pH->htsize ){ + h = hraw % pH->htsize; + elem = findElementGivenHash(pH,pKey,nKey,h); + if( elem ){ + void *old_data = elem->data; + if( data==0 ){ + removeElementGivenHash(pH,elem,h); + }else{ + elem->data = data; + if( !pH->copyKey ){ + elem->pKey = (void *)pKey; + } + assert(nKey==elem->nKey); } - assert(nKey==elem->nKey); + return old_data; } - return old_data; } if( data==0 ) return 0; new_elem = (HashElem*)sqlite3_malloc( sizeof(HashElem) ); @@ -14351,7 +16736,7 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, voi new_elem->nKey = nKey; pH->count++; if( pH->htsize==0 ){ - rehash(pH,8); + rehash(pH, 128/sizeof(pH->ht[0])); if( pH->htsize==0 ){ pH->count = 0; if( pH->copyKey ){ @@ -14365,8 +16750,7 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, voi rehash(pH,pH->htsize*2); } assert( pH->htsize>0 ); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); + h = hraw % pH->htsize; insertElement(pH, &pH->ht[h], new_elem); new_elem->data = data; return 0; @@ -14379,30 +16763,30 @@ SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const void *pKey, int nKey, voi #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ static const char *const azName[] = { "?", - /* 1 */ "MemLoad", - /* 2 */ "VNext", - /* 3 */ "Column", - /* 4 */ "SetCookie", - /* 5 */ "IfMemPos", - /* 6 */ "Sequence", - /* 7 */ "MoveGt", - /* 8 */ "RowKey", - /* 9 */ "OpenWrite", - /* 10 */ "If", - /* 11 */ "Pop", - /* 12 */ "VRowid", - /* 13 */ "CollSeq", - /* 14 */ "OpenRead", - /* 15 */ "Expire", + /* 1 */ "VNext", + /* 2 */ "Column", + /* 3 */ "SetCookie", + /* 4 */ "Sequence", + /* 5 */ "MoveGt", + /* 6 */ "RowKey", + /* 7 */ "SCopy", + /* 8 */ "OpenWrite", + /* 9 */ "If", + /* 10 */ "VRowid", + /* 11 */ "CollSeq", + /* 12 */ "OpenRead", + /* 13 */ "Expire", + /* 14 */ "AutoCommit", + /* 15 */ "IntegrityCk", /* 16 */ "Not", - /* 17 */ "AutoCommit", - /* 18 */ "IntegrityCk", - /* 19 */ "Sort", + /* 17 */ "Sort", + /* 18 */ "Copy", + /* 19 */ "Trace", /* 20 */ "Function", - /* 21 */ "Noop", - /* 22 */ "Return", - /* 23 */ "NewRowid", - /* 24 */ "IfMemNeg", + /* 21 */ "IfNeg", + /* 22 */ "Noop", + /* 23 */ "Return", + /* 24 */ "NewRowid", /* 25 */ "Variable", /* 26 */ "String", /* 27 */ "RealAffinity", @@ -14422,27 +16806,27 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ /* 41 */ "Statement", /* 42 */ "RowData", /* 43 */ "MemMax", - /* 44 */ "Push", - /* 45 */ "NotExists", - /* 46 */ "MemIncr", - /* 47 */ "Gosub", - /* 48 */ "Integer", - /* 49 */ "MemInt", - /* 50 */ "Prev", - /* 51 */ "VColumn", - /* 52 */ "CreateTable", - /* 53 */ "Last", - /* 54 */ "IncrVacuum", - /* 55 */ "IdxRowid", - /* 56 */ "MakeIdxRec", - /* 57 */ "ResetCount", - /* 58 */ "FifoWrite", - /* 59 */ "Callback", + /* 44 */ "NotExists", + /* 45 */ "Gosub", + /* 46 */ "Integer", + /* 47 */ "Prev", + /* 48 */ "VColumn", + /* 49 */ "CreateTable", + /* 50 */ "Last", + /* 51 */ "IncrVacuum", + /* 52 */ "IdxRowid", + /* 53 */ "ResetCount", + /* 54 */ "FifoWrite", + /* 55 */ "ContextPush", + /* 56 */ "DropTrigger", + /* 57 */ "DropIndex", + /* 58 */ "IdxGE", + /* 59 */ "IdxDelete", /* 60 */ "Or", /* 61 */ "And", - /* 62 */ "ContextPush", - /* 63 */ "DropTrigger", - /* 64 */ "DropIndex", + /* 62 */ "Vacuum", + /* 63 */ "MoveLe", + /* 64 */ "IfNot", /* 65 */ "IsNull", /* 66 */ "NotNull", /* 67 */ "Ne", @@ -14451,7 +16835,7 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ /* 70 */ "Le", /* 71 */ "Lt", /* 72 */ "Ge", - /* 73 */ "IdxGE", + /* 73 */ "DropTable", /* 74 */ "BitAnd", /* 75 */ "BitOr", /* 76 */ "ShiftLeft", @@ -14462,60 +16846,60 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ /* 81 */ "Divide", /* 82 */ "Remainder", /* 83 */ "Concat", - /* 84 */ "IdxDelete", - /* 85 */ "Negative", - /* 86 */ "Vacuum", + /* 84 */ "MakeRecord", + /* 85 */ "ResultRow", + /* 86 */ "Delete", /* 87 */ "BitNot", /* 88 */ "String8", - /* 89 */ "MoveLe", - /* 90 */ "IfNot", - /* 91 */ "DropTable", - /* 92 */ "MakeRecord", - /* 93 */ "Delete", - /* 94 */ "StackDepth", - /* 95 */ "AggFinal", - /* 96 */ "Dup", - /* 97 */ "Goto", - /* 98 */ "TableLock", - /* 99 */ "FifoRead", - /* 100 */ "Clear", - /* 101 */ "IdxGT", - /* 102 */ "MoveLt", - /* 103 */ "VerifyCookie", - /* 104 */ "AggStep", - /* 105 */ "Pull", - /* 106 */ "SetNumColumns", - /* 107 */ "AbsValue", - /* 108 */ "Transaction", - /* 109 */ "VFilter", - /* 110 */ "VDestroy", - /* 111 */ "ContextPop", - /* 112 */ "Next", - /* 113 */ "IdxInsert", - /* 114 */ "Distinct", - /* 115 */ "Insert", - /* 116 */ "Destroy", - /* 117 */ "ReadCookie", - /* 118 */ "ForceInt", - /* 119 */ "LoadAnalysis", - /* 120 */ "Explain", - /* 121 */ "IfMemZero", - /* 122 */ "OpenPseudo", - /* 123 */ "OpenEphemeral", - /* 124 */ "Null", + /* 89 */ "AggFinal", + /* 90 */ "Goto", + /* 91 */ "TableLock", + /* 92 */ "FifoRead", + /* 93 */ "Clear", + /* 94 */ "MoveLt", + /* 95 */ "VerifyCookie", + /* 96 */ "AggStep", + /* 97 */ "SetNumColumns", + /* 98 */ "Transaction", + /* 99 */ "VFilter", + /* 100 */ "VDestroy", + /* 101 */ "ContextPop", + /* 102 */ "Next", + /* 103 */ "IdxInsert", + /* 104 */ "Insert", + /* 105 */ "Destroy", + /* 106 */ "ReadCookie", + /* 107 */ "ForceInt", + /* 108 */ "LoadAnalysis", + /* 109 */ "Explain", + /* 110 */ "OpenPseudo", + /* 111 */ "OpenEphemeral", + /* 112 */ "Null", + /* 113 */ "Move", + /* 114 */ "Blob", + /* 115 */ "Rewind", + /* 116 */ "MoveGe", + /* 117 */ "VBegin", + /* 118 */ "VUpdate", + /* 119 */ "IfZero", + /* 120 */ "VCreate", + /* 121 */ "Found", + /* 122 */ "IfPos", + /* 123 */ "NullRow", + /* 124 */ "NotUsed_124", /* 125 */ "Real", - /* 126 */ "HexBlob", - /* 127 */ "Blob", - /* 128 */ "MemStore", - /* 129 */ "Rewind", - /* 130 */ "MoveGe", - /* 131 */ "VBegin", - /* 132 */ "VUpdate", - /* 133 */ "VCreate", - /* 134 */ "MemMove", - /* 135 */ "MemNull", - /* 136 */ "Found", - /* 137 */ "NullRow", + /* 126 */ "NotUsed_126", + /* 127 */ "NotUsed_127", + /* 128 */ "NotUsed_128", + /* 129 */ "NotUsed_129", + /* 130 */ "NotUsed_130", + /* 131 */ "NotUsed_131", + /* 132 */ "NotUsed_132", + /* 133 */ "NotUsed_133", + /* 134 */ "NotUsed_134", + /* 135 */ "NotUsed_135", + /* 136 */ "NotUsed_136", + /* 137 */ "NotUsed_137", /* 138 */ "ToText", /* 139 */ "ToBlob", /* 140 */ "ToNumeric", @@ -14623,16 +17007,16 @@ SQLITE_API unsigned int sqlite3_pending_byte = 0x40000000; #endif #ifdef SQLITE_DEBUG -SQLITE_API int sqlite3_os_trace = 0; -#define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X) -#define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y) -#define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z) -#define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A) -#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B) +SQLITE_PRIVATE int sqlite3OSTrace = 0; +#define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X) +#define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y) +#define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z) +#define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A) +#define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B) #define OSTRACE6(X,Y,Z,A,B,C) \ - if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C) + if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C) #define OSTRACE7(X,Y,Z,A,B,C,D) \ - if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) + if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) #else #define OSTRACE1(X) #define OSTRACE2(X,Y) @@ -14672,19 +17056,22 @@ static unsigned int elapse; ** is used for testing the I/O recovery logic. */ #ifdef SQLITE_TEST -SQLITE_API int sqlite3_io_error_hit = 0; -SQLITE_API int sqlite3_io_error_pending = 0; -SQLITE_API int sqlite3_io_error_persist = 0; +SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ SQLITE_API int sqlite3_diskfull_pending = 0; SQLITE_API int sqlite3_diskfull = 0; +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) #define SimulateIOError(CODE) \ - if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \ - if( sqlite3_io_error_pending-- == 1 \ - || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \ - { local_ioerr(); CODE; } + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ + || sqlite3_io_error_pending-- == 1 ) \ + { local_ioerr(); CODE; } static void local_ioerr(){ IOTRACE(("IOERR\n")); - sqlite3_io_error_hit = 1; + sqlite3_io_error_hit++; + if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; } #define SimulateDiskfullError(CODE) \ if( sqlite3_diskfull_pending ){ \ @@ -14698,6 +17085,7 @@ static void local_ioerr(){ } \ } #else +#define SimulateIOErrorBenign(X) #define SimulateIOError(A) #define SimulateDiskfullError(A) #endif @@ -14825,15 +17213,10 @@ int os2Write( */ int os2Truncate( sqlite3_file *id, i64 nByte ){ APIRET rc = NO_ERROR; - ULONG filePosition = 0L; os2File *pFile = (os2File*)id; OSTRACE3( "TRUNCATE %d %lld\n", pFile->h, nByte ); SimulateIOError( return SQLITE_IOERR_TRUNCATE ); - rc = DosSetFilePtr( pFile->h, nByte, FILE_BEGIN, &filePosition ); - if( rc != NO_ERROR ){ - return SQLITE_IOERR; - } - rc = DosSetFilePtr( pFile->h, 0L, FILE_END, &filePosition ); + rc = DosSetFileSize( pFile->h, nByte ); return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR; } @@ -15087,15 +17470,16 @@ int os2Lock( sqlite3_file *id, int locktype ){ ** non-zero, otherwise zero. */ int os2CheckReservedLock( sqlite3_file *id ){ - APIRET rc = NO_ERROR; + int r = 0; os2File *pFile = (os2File*)id; assert( pFile!=0 ); if( pFile->locktype>=RESERVED_LOCK ){ - rc = 1; - OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc ); + r = 1; + OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, r ); }else{ FILELOCK LockArea, UnlockArea; + APIRET rc = NO_ERROR; memset(&LockArea, 0, sizeof(LockArea)); memset(&UnlockArea, 0, sizeof(UnlockArea)); LockArea.lOffset = RESERVED_BYTE; @@ -15105,17 +17489,18 @@ int os2CheckReservedLock( sqlite3_file *id ){ rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L ); OSTRACE3( "TEST WR-LOCK %d lock reserved byte rc=%d\n", pFile->h, rc ); if( rc == NO_ERROR ){ - int r; + APIRET rcu = NO_ERROR; /* return code for unlocking */ LockArea.lOffset = 0L; LockArea.lRange = 0L; UnlockArea.lOffset = RESERVED_BYTE; UnlockArea.lRange = 1L; - r = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L ); - OSTRACE3( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, r ); + rcu = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L ); + OSTRACE3( "TEST WR-LOCK %d unlock reserved byte r=%d\n", pFile->h, rcu ); } - OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc ); + r = !(rc == NO_ERROR); + OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, r ); } - return rc; + return r; } /* @@ -15306,15 +17691,8 @@ static int os2Open( OSTRACE1( "OPEN normal file attribute\n" ); } - //ulOpenMode |= flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ? - // OPEN_FLAGS_RANDOM : OPEN_FLAGS_SEQUENTIAL; - if( flags & (SQLITE_OPEN_MAIN_DB | SQLITE_OPEN_TEMP_DB) ){ - ulOpenMode |= OPEN_FLAGS_RANDOM; - OSTRACE1( "OPEN random access\n" ); - }else{ - ulOpenMode |= OPEN_FLAGS_SEQUENTIAL; - OSTRACE1( "OPEN sequential access\n" ); - } + /* always open in random access mode for possibly better speed */ + ulOpenMode |= OPEN_FLAGS_RANDOM; ulOpenMode |= OPEN_FLAGS_FAIL_ON_ERROR; rc = DosOpen( (PSZ)zName, @@ -15407,7 +17785,8 @@ static int os2GetTempname( sqlite3_vfs *pVfs, int nBuf, char *zBuf ){ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789"; int i, j; - PSZ zTempPath = ""; + char zTempPathBuf[3]; + PSZ zTempPath = (PSZ)&zTempPathBuf; if( DosScanEnv( (PSZ)"TEMP", &zTempPath ) ){ if( DosScanEnv( (PSZ)"TMP", &zTempPath ) ){ if( DosScanEnv( (PSZ)"TMPDIR", &zTempPath ) ){ @@ -15424,8 +17803,7 @@ static int os2GetTempname( sqlite3_vfs *pVfs, int nBuf, char *zBuf ){ j--; } zTempPath[j] = '\0'; - assert( nBuf>=pVfs->mxPathname ); - sqlite3_snprintf( pVfs->mxPathname-30, zBuf, + sqlite3_snprintf( nBuf-30, zBuf, "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath ); j = strlen( zBuf ); sqlite3Randomness( 20, &zBuf[j] ); @@ -15449,26 +17827,8 @@ static int os2FullPathname( int nFull, /* Size of output buffer in bytes */ char *zFull /* Output buffer */ ){ - if( strchr(zRelative, ':') ){ - sqlite3SetString( &zFull, zRelative, (char*)0 ); - }else{ - ULONG ulDriveNum = 0; - ULONG ulDriveMap = 0; - ULONG cbzBufLen = SQLITE_TEMPNAME_SIZE; - char zDrive[2]; - char *zBuff = (char*)malloc( cbzBufLen ); - if( zBuff == 0 ){ - return SQLITE_NOMEM; - } - DosQueryCurrentDisk( &ulDriveNum, &ulDriveMap ); - if( DosQueryCurrentDir( ulDriveNum, (PBYTE)zBuff, &cbzBufLen ) == NO_ERROR ){ - sprintf( zDrive, "%c", (char)('A' + ulDriveNum - 1) ); - sqlite3SetString( &zFull, zDrive, ":\\", zBuff, - "\\", zRelative, (char*)0 ); - } - free( zBuff ); - } - return SQLITE_OK; + APIRET rc = DosQueryPathInfo( zRelative, FIL_QUERYFULLNAME, zFull, nFull ); + return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR; } #ifndef SQLITE_OMIT_LOAD_EXTENSION @@ -15818,16 +18178,16 @@ SQLITE_API unsigned int sqlite3_pending_byte = 0x40000000; #endif #ifdef SQLITE_DEBUG -SQLITE_API int sqlite3_os_trace = 0; -#define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X) -#define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y) -#define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z) -#define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A) -#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B) +SQLITE_PRIVATE int sqlite3OSTrace = 0; +#define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X) +#define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y) +#define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z) +#define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A) +#define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B) #define OSTRACE6(X,Y,Z,A,B,C) \ - if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C) + if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C) #define OSTRACE7(X,Y,Z,A,B,C,D) \ - if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) + if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) #else #define OSTRACE1(X) #define OSTRACE2(X,Y) @@ -15867,19 +18227,22 @@ static unsigned int elapse; ** is used for testing the I/O recovery logic. */ #ifdef SQLITE_TEST -SQLITE_API int sqlite3_io_error_hit = 0; -SQLITE_API int sqlite3_io_error_pending = 0; -SQLITE_API int sqlite3_io_error_persist = 0; +SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ SQLITE_API int sqlite3_diskfull_pending = 0; SQLITE_API int sqlite3_diskfull = 0; +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) #define SimulateIOError(CODE) \ - if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \ - if( sqlite3_io_error_pending-- == 1 \ - || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \ - { local_ioerr(); CODE; } + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ + || sqlite3_io_error_pending-- == 1 ) \ + { local_ioerr(); CODE; } static void local_ioerr(){ IOTRACE(("IOERR\n")); - sqlite3_io_error_hit = 1; + sqlite3_io_error_hit++; + if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; } #define SimulateDiskfullError(CODE) \ if( sqlite3_diskfull_pending ){ \ @@ -15893,6 +18256,7 @@ static void local_ioerr(){ } \ } #else +#define SimulateIOErrorBenign(X) #define SimulateIOError(A) #define SimulateDiskfullError(A) #endif @@ -16155,12 +18519,12 @@ static Hash openHash = {SQLITE_HASH_BINARY, 0, 0, 0, 0, 0}; ** file systems that are known to be unsupported */ typedef enum { - posixLockingStyle = 0, /* standard posix-advisory locks */ - afpLockingStyle, /* use afp locks */ - flockLockingStyle, /* use flock() */ - dotlockLockingStyle, /* use .lock files */ - noLockingStyle, /* useful for read-only file system */ - unsupportedLockingStyle /* indicates unsupported file system */ + posixLockingStyle = 0, /* standard posix-advisory locks */ + afpLockingStyle, /* use afp locks */ + flockLockingStyle, /* use flock() */ + dotlockLockingStyle, /* use .lock files */ + noLockingStyle, /* useful for read-only file system */ + unsupportedLockingStyle /* indicates unsupported file system */ } sqlite3LockingStyle; #endif /* SQLITE_ENABLE_LOCKING_STYLE */ @@ -16384,31 +18748,31 @@ static sqlite3LockingStyle sqlite3DetectLockingStyle( #else struct statfs fsInfo; - if (statfs(filePath, &fsInfo) == -1) + if( statfs(filePath, &fsInfo) == -1 ){ return sqlite3TestLockingStyle(filePath, fd); - - if (fsInfo.f_flags & MNT_RDONLY) + } + if( fsInfo.f_flags & MNT_RDONLY ){ return noLockingStyle; - - if( (!strcmp(fsInfo.f_fstypename, "hfs")) || - (!strcmp(fsInfo.f_fstypename, "ufs")) ) - return posixLockingStyle; - - if(!strcmp(fsInfo.f_fstypename, "afpfs")) + } + if( strcmp(fsInfo.f_fstypename, "hfs")==0 || + strcmp(fsInfo.f_fstypename, "ufs")==0 ){ + return posixLockingStyle; + } + if( strcmp(fsInfo.f_fstypename, "afpfs")==0 ){ return afpLockingStyle; - - if(!strcmp(fsInfo.f_fstypename, "nfs")) + } + if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ return sqlite3TestLockingStyle(filePath, fd); - - if(!strcmp(fsInfo.f_fstypename, "smbfs")) + } + if( strcmp(fsInfo.f_fstypename, "smbfs")==0 ){ return flockLockingStyle; - - if(!strcmp(fsInfo.f_fstypename, "msdos")) + } + if( strcmp(fsInfo.f_fstypename, "msdos")==0 ){ return dotlockLockingStyle; - - if(!strcmp(fsInfo.f_fstypename, "webdav")) + } + if( strcmp(fsInfo.f_fstypename, "webdav")==0 ){ return unsupportedLockingStyle; - + } return sqlite3TestLockingStyle(filePath, fd); #endif /* SQLITE_FIXED_LOCKING_STYLE */ } @@ -16835,8 +19199,8 @@ static int unixSync(sqlite3_file *id, int flags){ static int unixTruncate(sqlite3_file *id, i64 nByte){ int rc; assert( id ); + SimulateIOError( return SQLITE_IOERR_TRUNCATE ); rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); - SimulateIOError( rc=1 ); if( rc ){ return SQLITE_IOERR_TRUNCATE; }else{ @@ -17129,6 +19493,7 @@ static int unixUnlock(sqlite3_file *id, int locktype){ struct flock lock; int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; + int h; assert( pFile ); OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype, @@ -17142,17 +19507,20 @@ static int unixUnlock(sqlite3_file *id, int locktype){ return SQLITE_MISUSE; } enterMutex(); + h = pFile->h; pLock = pFile->pLock; assert( pLock->cnt!=0 ); if( pFile->locktype>SHARED_LOCK ){ assert( pLock->locktype==pFile->locktype ); + SimulateIOErrorBenign(1); + SimulateIOError( h=(-1) ) + SimulateIOErrorBenign(0); if( locktype==SHARED_LOCK ){ lock.l_type = F_RDLCK; lock.l_whence = SEEK_SET; lock.l_start = SHARED_FIRST; lock.l_len = SHARED_SIZE; - if( fcntl(pFile->h, F_SETLK, &lock)==(-1) ){ - /* This should never happen */ + if( fcntl(h, F_SETLK, &lock)==(-1) ){ rc = SQLITE_IOERR_RDLOCK; } } @@ -17160,10 +19528,10 @@ static int unixUnlock(sqlite3_file *id, int locktype){ lock.l_whence = SEEK_SET; lock.l_start = PENDING_BYTE; lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); - if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){ + if( fcntl(h, F_SETLK, &lock)!=(-1) ){ pLock->locktype = SHARED_LOCK; }else{ - rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ + rc = SQLITE_IOERR_UNLOCK; } } if( locktype==NO_LOCK ){ @@ -17178,10 +19546,14 @@ static int unixUnlock(sqlite3_file *id, int locktype){ lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = lock.l_len = 0L; - if( fcntl(pFile->h, F_SETLK, &lock)!=(-1) ){ + SimulateIOErrorBenign(1); + SimulateIOError( h=(-1) ) + SimulateIOErrorBenign(0); + if( fcntl(h, F_SETLK, &lock)!=(-1) ){ pLock->locktype = NO_LOCK; }else{ - rc = SQLITE_IOERR_UNLOCK; /* This should never happen */ + rc = SQLITE_IOERR_UNLOCK; + pLock->cnt = 1; } } @@ -17189,21 +19561,23 @@ static int unixUnlock(sqlite3_file *id, int locktype){ ** count reaches zero, close any other file descriptors whose close ** was deferred because of outstanding locks. */ - pOpen = pFile->pOpen; - pOpen->nLock--; - assert( pOpen->nLock>=0 ); - if( pOpen->nLock==0 && pOpen->nPending>0 ){ - int i; - for(i=0; inPending; i++){ - close(pOpen->aPending[i]); + if( rc==SQLITE_OK ){ + pOpen = pFile->pOpen; + pOpen->nLock--; + assert( pOpen->nLock>=0 ); + if( pOpen->nLock==0 && pOpen->nPending>0 ){ + int i; + for(i=0; inPending; i++){ + close(pOpen->aPending[i]); + } + free(pOpen->aPending); + pOpen->nPending = 0; + pOpen->aPending = 0; } - free(pOpen->aPending); - pOpen->nPending = 0; - pOpen->aPending = 0; } } leaveMutex(); - pFile->locktype = locktype; + if( rc==SQLITE_OK ) pFile->locktype = locktype; return rc; } @@ -17258,7 +19632,7 @@ static int unixClose(sqlite3_file *id){ typedef struct afpLockingContext afpLockingContext; struct afpLockingContext { unsigned long long sharedLockByte; - char *filePath; + const char *filePath; }; struct ByteRangeLockPB2 @@ -17345,8 +19719,7 @@ static int afpUnixCheckReservedLock(sqlite3_file *id){ /* AFP-style locking following the behavior of unixLock, see the unixLock ** function comments for details of lock management. */ -static int afpUnixLock(sqlite3_file *id, int locktype) -{ +static int afpUnixLock(sqlite3_file *id, int locktype){ int rc = SQLITE_OK; unixFile *pFile = (unixFile*)id; afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; @@ -17354,11 +19727,12 @@ static int afpUnixLock(sqlite3_file *id, int locktype) assert( pFile ); OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h, - locktypeName(locktype), locktypeName(pFile->locktype), getpid()); + locktypeName(locktype), locktypeName(pFile->locktype), getpid()); + /* If there is already a lock of this type or more restrictive on the - ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as - ** enterMutex() hasn't been called yet. - */ + ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as + ** enterMutex() hasn't been called yet. + */ if( pFile->locktype>=locktype ){ OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, locktypeName(locktype)); @@ -17366,17 +19740,17 @@ static int afpUnixLock(sqlite3_file *id, int locktype) } /* Make sure the locking sequence is correct - */ + */ assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); assert( locktype!=PENDING_LOCK ); assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); /* This mutex is needed because pFile->pLock is shared across threads - */ + */ enterMutex(); /* Make sure the current thread owns the pFile. - */ + */ rc = transferOwnership(pFile); if( rc!=SQLITE_OK ){ leaveMutex(); @@ -17384,14 +19758,14 @@ static int afpUnixLock(sqlite3_file *id, int locktype) } /* A PENDING lock is needed before acquiring a SHARED lock and before - ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will - ** be released. - */ + ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will + ** be released. + */ if( locktype==SHARED_LOCK || (locktype==EXCLUSIVE_LOCK && pFile->locktypefilePath, pFile->h, - PENDING_BYTE, 1, 1); + ){ + int failed; + failed = _AFPFSSetLock(context->filePath, pFile->h, PENDING_BYTE, 1, 1); if (failed) { rc = SQLITE_BUSY; goto afp_end_lock; @@ -17399,8 +19773,8 @@ static int afpUnixLock(sqlite3_file *id, int locktype) } /* If control gets to this point, then actually go ahead and make - ** operating system calls for the specified lock. - */ + ** operating system calls for the specified lock. + */ if( locktype==SHARED_LOCK ){ int lk, failed; int tries = 0; @@ -17466,19 +19840,19 @@ static int afpUnixLock(sqlite3_file *id, int locktype) } afp_end_lock: - leaveMutex(); + leaveMutex(); OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), rc==SQLITE_OK ? "ok" : "failed"); return rc; } /* - ** Lower the locking level on file descriptor pFile to locktype. locktype - ** must be either NO_LOCK or SHARED_LOCK. - ** - ** If the locking level of the file descriptor is already at or below - ** the requested locking level, this routine is a no-op. - */ +** Lower the locking level on file descriptor pFile to locktype. locktype +** must be either NO_LOCK or SHARED_LOCK. +** +** If the locking level of the file descriptor is already at or below +** the requested locking level, this routine is a no-op. +*/ static int afpUnixUnlock(sqlite3_file *id, int locktype) { struct flock lock; int rc = SQLITE_OK; @@ -17545,25 +19919,22 @@ static int afpUnixUnlock(sqlite3_file *id, int locktype) { } /* - ** Close a file & cleanup AFP specific locking context - */ +** Close a file & cleanup AFP specific locking context +*/ static int afpUnixClose(sqlite3_file *id) { - unixFile *pFile = (unixFile*)pId; + unixFile *pFile = (unixFile*)id; if( !pFile ) return SQLITE_OK; - afpUnixUnlock(*pId, NO_LOCK); - /* free the AFP locking structure */ - if (pFile->lockingContext != NULL) { - if (((afpLockingContext *)pFile->lockingContext)->filePath != NULL) - sqlite3_free(((afpLockingContext*)pFile->lockingContext)->filePath); - sqlite3_free(pFile->lockingContext); - } - + afpUnixUnlock(id, NO_LOCK); + sqlite3_free(pFile->lockingContext); if( pFile->dirfd>=0 ) close(pFile->dirfd); pFile->dirfd = -1; + enterMutex(); close(pFile->h); + leaveMutex(); OSTRACE2("CLOSE %-3d\n", pFile->h); OpenCounter(-1); + memset(pFile, 0, sizeof(unixFile)); return SQLITE_OK; } @@ -17571,11 +19942,11 @@ static int afpUnixClose(sqlite3_file *id) { #pragma mark flock() style locking /* - ** The flockLockingContext is not used - */ +** The flockLockingContext is not used +*/ typedef void flockLockingContext; -static int flockUnixCheckReservedLock(sqlite3_file *id) { +static int flockUnixCheckReservedLock(sqlite3_file *id){ unixFile *pFile = (unixFile*)id; if (pFile->locktype == RESERVED_LOCK) { @@ -17641,31 +20012,32 @@ static int flockUnixUnlock(sqlite3_file *id, int locktype) { } /* - ** Close a file. - */ -static int flockUnixClose(sqlite3_file *pId) { - unixFile *pFile = (unixFile*)*pId; +** Close a file. +*/ +static int flockUnixClose(sqlite3_file *id) { + unixFile *pFile = (unixFile*)id; if( !pFile ) return SQLITE_OK; - flockUnixUnlock(*pId, NO_LOCK); + flockUnixUnlock(id, NO_LOCK); if( pFile->dirfd>=0 ) close(pFile->dirfd); pFile->dirfd = -1; + enterMutex(); - close(pFile->h); leaveMutex(); OSTRACE2("CLOSE %-3d\n", pFile->h); OpenCounter(-1); + memset(pFile, 0, sizeof(unixFile)); return SQLITE_OK; } #pragma mark Old-School .lock file based locking /* - ** The dotlockLockingContext structure contains all dotlock (.lock) lock - ** specific state - */ +** The dotlockLockingContext structure contains all dotlock (.lock) lock +** specific state +*/ typedef struct dotlockLockingContext dotlockLockingContext; struct dotlockLockingContext { char *lockPath; @@ -17674,26 +20046,29 @@ struct dotlockLockingContext { static int dotlockUnixCheckReservedLock(sqlite3_file *id) { unixFile *pFile = (unixFile*)id; - dotlockLockingContext *context = - (dotlockLockingContext *) pFile->lockingContext; - + dotlockLockingContext *context; + + context = (dotlockLockingContext*)pFile->lockingContext; if (pFile->locktype == RESERVED_LOCK) { return 1; /* already have a reserved lock */ } else { struct stat statBuf; - if (lstat(context->lockPath,&statBuf) == 0) + if (lstat(context->lockPath,&statBuf) == 0){ /* file exists, someone else has the lock */ return 1; - else + }else{ /* file does not exist, we could have it if we want it */ return 0; + } } } static int dotlockUnixLock(sqlite3_file *id, int locktype) { unixFile *pFile = (unixFile*)id; - dotlockLockingContext *context = - (dotlockLockingContext *) pFile->lockingContext; + dotlockLockingContext *context; + int fd; + + context = (dotlockLockingContext*)pFile->lockingContext; /* if we already have a lock, it is exclusive. ** Just adjust level and punt on outta here. */ @@ -17712,8 +20087,8 @@ static int dotlockUnixLock(sqlite3_file *id, int locktype) { } /* grab an exclusive lock */ - int fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600); - if (fd < 0) { + fd = open(context->lockPath,O_RDONLY|O_CREAT|O_EXCL,0600); + if( fd<0 ){ /* failed to open/create the file, someone else may have stolen the lock */ return SQLITE_BUSY; } @@ -17726,8 +20101,9 @@ static int dotlockUnixLock(sqlite3_file *id, int locktype) { static int dotlockUnixUnlock(sqlite3_file *id, int locktype) { unixFile *pFile = (unixFile*)id; - dotlockLockingContext *context = - (dotlockLockingContext *) pFile->lockingContext; + dotlockLockingContext *context; + + context = (dotlockLockingContext*)pFile->lockingContext; assert( locktype<=SHARED_LOCK ); @@ -17755,24 +20131,16 @@ static int dotlockUnixClose(sqlite3_file *id) { unixFile *pFile = (unixFile*)id; if( !pFile ) return SQLITE_OK; - dotlockUnixUnlock(*pId, NO_LOCK); - /* free the dotlock locking structure */ - if (pFile->lockingContext != NULL) { - if (((dotlockLockingContext *)pFile->lockingContext)->lockPath != NULL) - sqlite3_free( ( (dotlockLockingContext *) - pFile->lockingContext)->lockPath); - sqlite3_free(pFile->lockingContext); - } - + dotlockUnixUnlock(id, NO_LOCK); + sqlite3_free(pFile->lockingContext); if( pFile->dirfd>=0 ) close(pFile->dirfd); pFile->dirfd = -1; - enterMutex(); - + enterMutex(); close(pFile->h); - leaveMutex(); OSTRACE2("CLOSE %-3d\n", pFile->h); OpenCounter(-1); + memset(pFile, 0, sizeof(unixFile)); return SQLITE_OK; } @@ -17780,8 +20148,8 @@ static int dotlockUnixClose(sqlite3_file *id) { #pragma mark No locking /* - ** The nolockLockingContext is void - */ +** The nolockLockingContext is void +*/ typedef void nolockLockingContext; static int nolockUnixCheckReservedLock(sqlite3_file *id) { @@ -17797,8 +20165,8 @@ static int nolockUnixUnlock(sqlite3_file *id, int locktype) { } /* - ** Close a file. - */ +** Close a file. +*/ static int nolockUnixClose(sqlite3_file *id) { unixFile *pFile = (unixFile*)id; @@ -17806,12 +20174,11 @@ static int nolockUnixClose(sqlite3_file *id) { if( pFile->dirfd>=0 ) close(pFile->dirfd); pFile->dirfd = -1; enterMutex(); - close(pFile->h); - leaveMutex(); OSTRACE2("CLOSE %-3d\n", pFile->h); OpenCounter(-1); + memset(pFile, 0, sizeof(unixFile)); return SQLITE_OK; } @@ -17879,7 +20246,7 @@ static const sqlite3_io_methods sqlite3UnixIoMethod = { */ static const sqlite3_io_methods sqlite3AFPLockingUnixIoMethod = { 1, /* iVersion */ - unixClose, + afpUnixClose, unixRead, unixWrite, unixTruncate, @@ -17935,7 +20302,7 @@ static const sqlite3_io_methods sqlite3DotlockLockingUnixIoMethod = { /* ** This vector defines all the methods that can operate on an sqlite3_file -** for unix with dotlock style file locking. +** for unix with nolock style file locking. */ static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = { 1, /* iVersion */ @@ -17970,22 +20337,25 @@ static const sqlite3_io_methods sqlite3NolockLockingUnixIoMethod = { static int fillInUnixFile( int h, /* Open file descriptor of file being opened */ int dirfd, /* Directory file descriptor */ - sqlite3_file *pId, /* Write completed initialization here */ - const char *zFilename, /* Name of the file being opened */ + sqlite3_file *pId, /* Write to the unixFile structure here */ + const char *zFilename /* Name of the file being opened */ ){ sqlite3LockingStyle lockingStyle; unixFile *pNew = (unixFile *)pId; int rc; - memset(pNew, 0, sizeof(unixFile)); +#ifdef FD_CLOEXEC + fcntl(h, F_SETFD, fcntl(h, F_GETFD, 0) | FD_CLOEXEC); +#endif + lockingStyle = sqlite3DetectLockingStyle(zFilename, h); - if ( lockingStyle == posixLockingStyle ) { + if ( lockingStyle==posixLockingStyle ){ enterMutex(); rc = findLockInfo(h, &pNew->pLock, &pNew->pOpen); leaveMutex(); if( rc ){ + if( dirfd>=0 ) close(dirfd); close(h); - unlink(zFilename); return SQLITE_NOMEM; } } else { @@ -17993,65 +20363,67 @@ static int fillInUnixFile( pNew->pLock = NULL; pNew->pOpen = NULL; } + + OSTRACE3("OPEN %-3d %s\n", h, zFilename); pNew->dirfd = -1; pNew->h = h; + pNew->dirfd = dirfd; SET_THREADID(pNew); - pNew = sqlite3_malloc( sizeof(unixFile) ); - if( pNew==0 ){ - close(h); - enterMutex(); - releaseLockInfo(pNew->pLock); - releaseOpenCnt(pNew->pOpen); - leaveMutex(); - return SQLITE_NOMEM; - }else{ - switch(lockingStyle) { - case afpLockingStyle: { - /* afp locking uses the file path so it needs to be included in - ** the afpLockingContext */ - int nFilename; - pNew->pMethod = &sqlite3AFPLockingUnixIoMethod; - pNew->lockingContext = - sqlite3_malloc(sizeof(afpLockingContext)); - nFilename = strlen(zFilename)+1; - ((afpLockingContext *)pNew->lockingContext)->filePath = - sqlite3_malloc(nFilename); - memcpy(((afpLockingContext *)pNew->lockingContext)->filePath, - zFilename, nFilename); - srandomdev(); - break; + + switch(lockingStyle) { + case afpLockingStyle: { + /* afp locking uses the file path so it needs to be included in + ** the afpLockingContext */ + afpLockingContext *context; + pNew->pMethod = &sqlite3AFPLockingUnixIoMethod; + pNew->lockingContext = context = sqlite3_malloc( sizeof(*context) ); + if( context==0 ){ + close(h); + if( dirfd>=0 ) close(dirfd); + return SQLITE_NOMEM; } - case flockLockingStyle: - /* flock locking doesn't need additional lockingContext information */ - pNew->pMethod = &sqlite3FlockLockingUnixIoMethod; - break; - case dotlockLockingStyle: { - /* dotlock locking uses the file path so it needs to be included in - ** the dotlockLockingContext */ - int nFilename; - pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod; - pNew->lockingContext = sqlite3_malloc( - sizeof(dotlockLockingContext)); - nFilename = strlen(zFilename) + 6; - ((dotlockLockingContext *)pNew->lockingContext)->lockPath = - sqlite3_malloc( nFilename ); - sqlite3_snprintf(nFilename, - ((dotlockLockingContext *)pNew->lockingContext)->lockPath, - "%s.lock", zFilename); - break; + + /* NB: zFilename exists and remains valid until the file is closed + ** according to requirement F11141. So we do not need to make a + ** copy of the filename. */ + context->filePath = zFilename; + srandomdev(); + break; + } + case flockLockingStyle: + /* flock locking doesn't need additional lockingContext information */ + pNew->pMethod = &sqlite3FlockLockingUnixIoMethod; + break; + case dotlockLockingStyle: { + /* dotlock locking uses the file path so it needs to be included in + ** the dotlockLockingContext */ + dotlockLockingContext *context; + int nFilename; + nFilename = strlen(zFilename); + pNew->pMethod = &sqlite3DotlockLockingUnixIoMethod; + pNew->lockingContext = context = + sqlite3_malloc( sizeof(*context) + nFilename + 6 ); + if( context==0 ){ + close(h); + if( dirfd>=0 ) close(dirfd); + return SQLITE_NOMEM; } - case posixLockingStyle: - /* posix locking doesn't need additional lockingContext information */ - pNew->pMethod = &sqlite3UnixIoMethod; - break; - case noLockingStyle: - case unsupportedLockingStyle: - default: - pNew->pMethod = &sqlite3NolockLockingUnixIoMethod; + context->lockPath = (char*)&context[1]; + sqlite3_snprintf(nFilename, context->lockPath, + "%s.lock", zFilename); + break; } - OpenCounter(+1); - return SQLITE_OK; + case posixLockingStyle: + /* posix locking doesn't need additional lockingContext information */ + pNew->pMethod = &sqlite3UnixIoMethod; + break; + case noLockingStyle: + case unsupportedLockingStyle: + default: + pNew->pMethod = &sqlite3NolockLockingUnixIoMethod; } + OpenCounter(+1); + return SQLITE_OK; } #else /* SQLITE_ENABLE_LOCKING_STYLE */ static int fillInUnixFile( @@ -18662,16 +21034,16 @@ SQLITE_API unsigned int sqlite3_pending_byte = 0x40000000; #endif #ifdef SQLITE_DEBUG -SQLITE_API int sqlite3_os_trace = 0; -#define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X) -#define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y) -#define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z) -#define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A) -#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B) +SQLITE_PRIVATE int sqlite3OSTrace = 0; +#define OSTRACE1(X) if( sqlite3OSTrace ) sqlite3DebugPrintf(X) +#define OSTRACE2(X,Y) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y) +#define OSTRACE3(X,Y,Z) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z) +#define OSTRACE4(X,Y,Z,A) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A) +#define OSTRACE5(X,Y,Z,A,B) if( sqlite3OSTrace ) sqlite3DebugPrintf(X,Y,Z,A,B) #define OSTRACE6(X,Y,Z,A,B,C) \ - if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C) + if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C) #define OSTRACE7(X,Y,Z,A,B,C,D) \ - if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) + if(sqlite3OSTrace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D) #else #define OSTRACE1(X) #define OSTRACE2(X,Y) @@ -18711,19 +21083,22 @@ static unsigned int elapse; ** is used for testing the I/O recovery logic. */ #ifdef SQLITE_TEST -SQLITE_API int sqlite3_io_error_hit = 0; -SQLITE_API int sqlite3_io_error_pending = 0; -SQLITE_API int sqlite3_io_error_persist = 0; +SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */ +SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */ +SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */ +SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */ +SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */ SQLITE_API int sqlite3_diskfull_pending = 0; SQLITE_API int sqlite3_diskfull = 0; +#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) #define SimulateIOError(CODE) \ - if( sqlite3_io_error_pending || sqlite3_io_error_hit ) \ - if( sqlite3_io_error_pending-- == 1 \ - || (sqlite3_io_error_persist && sqlite3_io_error_hit) ) \ - { local_ioerr(); CODE; } + if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ + || sqlite3_io_error_pending-- == 1 ) \ + { local_ioerr(); CODE; } static void local_ioerr(){ IOTRACE(("IOERR\n")); - sqlite3_io_error_hit = 1; + sqlite3_io_error_hit++; + if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; } #define SimulateDiskfullError(CODE) \ if( sqlite3_diskfull_pending ){ \ @@ -18737,6 +21112,7 @@ static void local_ioerr(){ } \ } #else +#define SimulateIOErrorBenign(X) #define SimulateIOError(A) #define SimulateDiskfullError(A) #endif @@ -19299,9 +21675,17 @@ static int winClose(sqlite3_file *id){ rc = CloseHandle(pFile->h); }while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) ); #if OS_WINCE +#define WINCE_DELETION_ATTEMPTS 3 winceDestroyLock(pFile); if( pFile->zDeleteOnClose ){ - DeleteFileW(pFile->zDeleteOnClose); + int cnt = 0; + while( + DeleteFileW(pFile->zDeleteOnClose)==0 + && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff + && cnt++ < WINCE_DELETION_ATTEMPTS + ){ + Sleep(100); /* Wait a little before trying again */ + } free(pFile->zDeleteOnClose); } #endif @@ -20250,6 +22634,219 @@ SQLITE_PRIVATE sqlite3_vfs *sqlite3OsDefaultVfs(void){ #endif /* OS_WIN */ /************** End of os_win.c **********************************************/ +/************** Begin file bitvec.c ******************************************/ +/* +** 2008 February 16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements an object that represents a fixed-length +** bitmap. Bits are numbered starting with 1. +** +** A bitmap is used to record what pages a database file have been +** journalled during a transaction. Usually only a few pages are +** journalled. So the bitmap is usually sparse and has low cardinality. +** But sometimes (for example when during a DROP of a large table) most +** or all of the pages get journalled. In those cases, the bitmap becomes +** dense. The algorithm needs to handle both cases well. +** +** The size of the bitmap is fixed when the object is created. +** +** All bits are clear when the bitmap is created. Individual bits +** may be set or cleared one at a time. +** +** Test operations are about 100 times more common that set operations. +** Clear operations are exceedingly rare. There are usually between +** 5 and 500 set operations per Bitvec object, though the number of sets can +** sometimes grow into tens of thousands or larger. The size of the +** Bitvec object is the number of pages in the database file at the +** start of a transaction, and is thus usually less than a few thousand, +** but can be as large as 2 billion for a really big database. +** +** @(#) $Id: bitvec.c,v 1.2 2008/03/14 13:02:08 mlcreech Exp $ +*/ + +#define BITVEC_SZ 512 +/* Round the union size down to the nearest pointer boundary, since that's how +** it will be aligned within the Bitvec struct. */ +#define BITVEC_USIZE (((BITVEC_SZ-12)/sizeof(Bitvec *))*sizeof(Bitvec *)) +#define BITVEC_NCHAR BITVEC_USIZE +#define BITVEC_NBIT (BITVEC_NCHAR*8) +#define BITVEC_NINT (BITVEC_USIZE/4) +#define BITVEC_MXHASH (BITVEC_NINT/2) +#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *)) + +#define BITVEC_HASH(X) (((X)*37)%BITVEC_NINT) + +/* +** A bitmap is an instance of the following structure. +** +** This bitmap records the existance of zero or more bits +** with values between 1 and iSize, inclusive. +** +** There are three possible representations of the bitmap. +** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight +** bitmap. The least significant bit is bit 1. +** +** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is +** a hash table that will hold up to BITVEC_MXHASH distinct values. +** +** Otherwise, the value i is redirected into one of BITVEC_NPTR +** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap +** handles up to iDivisor separate values of i. apSub[0] holds +** values between 1 and iDivisor. apSub[1] holds values between +** iDivisor+1 and 2*iDivisor. apSub[N] holds values between +** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized +** to hold deal with values between 1 and iDivisor. +*/ +struct Bitvec { + u32 iSize; /* Maximum bit index */ + u32 nSet; /* Number of bits that are set */ + u32 iDivisor; /* Number of bits handled by each apSub[] entry */ + union { + u8 aBitmap[BITVEC_NCHAR]; /* Bitmap representation */ + u32 aHash[BITVEC_NINT]; /* Hash table representation */ + Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */ + } u; +}; + +/* +** Create a new bitmap object able to handle bits between 0 and iSize, +** inclusive. Return a pointer to the new object. Return NULL if +** malloc fails. +*/ +SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){ + Bitvec *p; + assert( sizeof(*p)==BITVEC_SZ ); + p = sqlite3MallocZero( sizeof(*p) ); + if( p ){ + p->iSize = iSize; + } + return p; +} + +/* +** Check to see if the i-th bit is set. Return true or false. +** If p is NULL (if the bitmap has not been created) or if +** i is out of range, then return false. +*/ +SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ + assert( i>0 ); + if( p==0 ) return 0; + if( i>p->iSize ) return 0; + if( p->iSize<=BITVEC_NBIT ){ + i--; + return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0; + } + if( p->iDivisor>0 ){ + u32 bin = (i-1)/p->iDivisor; + i = (i-1)%p->iDivisor + 1; + return sqlite3BitvecTest(p->u.apSub[bin], i); + }else{ + u32 h = BITVEC_HASH(i); + while( p->u.aHash[h] ){ + if( p->u.aHash[h]==i ) return 1; + h++; + if( h>=BITVEC_NINT ) h = 0; + } + return 0; + } +} + +/* +** Set the i-th bit. Return 0 on success and an error code if +** anything goes wrong. +*/ +SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){ + u32 h; + assert( p!=0 ); + if( p->iSize<=BITVEC_NBIT ){ + i--; + p->u.aBitmap[i/8] |= 1 << (i&7); + return SQLITE_OK; + } + if( p->iDivisor ){ + u32 bin = (i-1)/p->iDivisor; + i = (i-1)%p->iDivisor + 1; + if( p->u.apSub[bin]==0 ){ + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 1); + p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0); + if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM; + } + return sqlite3BitvecSet(p->u.apSub[bin], i); + } + h = BITVEC_HASH(i); + while( p->u.aHash[h] ){ + if( p->u.aHash[h]==i ) return SQLITE_OK; + h++; + if( h==BITVEC_NINT ) h = 0; + } + p->nSet++; + if( p->nSet>=BITVEC_MXHASH ){ + int j, rc; + u32 aiValues[BITVEC_NINT]; + memcpy(aiValues, p->u.aHash, sizeof(aiValues)); + memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR); + p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR; + sqlite3BitvecSet(p, i); + for(rc=j=0; ju.aHash[h] = i; + return SQLITE_OK; +} + +/* +** Clear the i-th bit. Return 0 on success and an error code if +** anything goes wrong. +*/ +SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i){ + assert( p!=0 ); + if( p->iSize<=BITVEC_NBIT ){ + i--; + p->u.aBitmap[i/8] &= ~(1 << (i&7)); + }else if( p->iDivisor ){ + u32 bin = (i-1)/p->iDivisor; + i = (i-1)%p->iDivisor + 1; + if( p->u.apSub[bin] ){ + sqlite3BitvecClear(p->u.apSub[bin], i); + } + }else{ + int j; + u32 aiValues[BITVEC_NINT]; + memcpy(aiValues, p->u.aHash, sizeof(aiValues)); + memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT); + p->nSet = 0; + for(j=0; jiDivisor ){ + int i; + for(i=0; iu.apSub[i]); + } + } + sqlite3_free(p); +} + +/************** End of bitvec.c **********************************************/ /************** Begin file pager.c *******************************************/ /* ** 2001 September 15 @@ -20271,7 +22868,7 @@ SQLITE_PRIVATE sqlite3_vfs *sqlite3OsDefaultVfs(void){ ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.400 2007/12/13 21:54:11 drh Exp $ +** @(#) $Id: pager.c,v 1.417 2008/03/17 13:50:58 drh Exp $ */ #ifndef SQLITE_OMIT_DISKIO @@ -20463,9 +23060,9 @@ struct PagerLruLink { ** has been synced to disk. For pages that are in the original ** database file, the following expression should always be true: ** -** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0 +** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno) ** -** The pPager->aInJournal[] array is only valid for the original +** The pPager->pInJournal object is only valid for the original ** pages of the database, not new pages that are added to the end ** of the database, so obviously the above expression cannot be ** valid for new pages. For new pages inJournal is always 0. @@ -20615,8 +23212,8 @@ struct Pager { int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ int mxPage; /* Maximum number of pages to hold in cache */ Pgno mxPgno; /* Maximum allowed size of the database */ - u8 *aInJournal; /* One bit for each page in the database file */ - u8 *aInStmt; /* One bit for each page in the database */ + Bitvec *pInJournal; /* One bit for each page in the database file */ + Bitvec *pInStmt; /* One bit for each page in the database */ char *zFilename; /* Name of the database file */ char *zJournal; /* Name of the journal file */ char *zDirectory; /* Directory hold database and journal files */ @@ -20783,25 +23380,6 @@ static const unsigned char aJournalMagic[] = { #endif /* -** Enable reference count tracking (for debugging) here: -*/ -#ifdef SQLITE_DEBUG - int pager3_refinfo_enable = 0; - static void pager_refinfo(PgHdr *p){ - static int cnt = 0; - if( !pager3_refinfo_enable ) return; - sqlite3DebugPrintf( - "REFCNT: %4d addr=%p nRef=%-3d total=%d\n", - p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef - ); - cnt++; /* Something to set a breakpoint on */ - } -# define REFINFO(X) pager_refinfo(X) -#else -# define REFINFO(X) -#endif - -/* ** Add page pPg to the end of the linked list managed by structure ** pList (pPg becomes the last entry in the list - the most recently ** used). Argument pLink should point to either pPg->free or pPg->gfree, @@ -20935,9 +23513,7 @@ static int pageInStatement(PgHdr *pPg){ if( MEMDB ){ return PGHDR_TO_HIST(pPg, pPager)->inStmt; }else{ - Pgno pgno = pPg->pgno; - u8 *a = pPager->aInStmt; - return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7)))); + return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno); } } @@ -20948,9 +23524,16 @@ static int pageInStatement(PgHdr *pPg){ static void pager_resize_hash_table(Pager *pPager, int N){ PgHdr **aHash, *pPg; assert( N>0 && (N&(N-1))==0 ); +#ifdef SQLITE_MALLOC_SOFT_LIMIT + if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){ + N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]); + } + if( N==pPager->nHash ) return; +#endif pagerLeave(pPager); - sqlite3MallocBenignFailure((int)pPager->aHash); + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0); aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0); pagerEnter(pPager); if( aHash==0 ){ /* Failure to rehash is not an error. It is only a performance hit. */ @@ -21477,6 +24060,7 @@ static void pager_reset(Pager *pPager){ PAGER_INCR(sqlite3_pager_pgfree_count); pNext = pPg->pNextAll; lruListRemove(pPg); + sqlite3_free(pPg->pData); sqlite3_free(pPg); } assert(pPager->lru.pFirst==0); @@ -21504,9 +24088,8 @@ static void pager_reset(Pager *pPager){ static void pager_unlock(Pager *pPager){ if( !pPager->exclusiveMode ){ if( !MEMDB ){ - if( pPager->fd->pMethods ){ - osUnlock(pPager->fd, NO_LOCK); - } + int rc = osUnlock(pPager->fd, NO_LOCK); + if( rc ) pPager->errCode = rc; pPager->dbSize = -1; IOTRACE(("UNLOCK %p\n", pPager)) @@ -21515,18 +24098,18 @@ static void pager_unlock(Pager *pPager){ ** cache can be discarded and the error code safely cleared. */ if( pPager->errCode ){ - pPager->errCode = SQLITE_OK; + if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK; pager_reset(pPager); if( pPager->stmtOpen ){ sqlite3OsClose(pPager->stfd); - sqlite3_free(pPager->aInStmt); - pPager->aInStmt = 0; + sqlite3BitvecDestroy(pPager->pInStmt); + pPager->pInStmt = 0; } if( pPager->journalOpen ){ sqlite3OsClose(pPager->jfd); pPager->journalOpen = 0; - sqlite3_free(pPager->aInJournal); - pPager->aInJournal = 0; + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; } pPager->stmtOpen = 0; pPager->stmtInUse = 0; @@ -21601,8 +24184,8 @@ static int pager_end_transaction(Pager *pPager){ rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0); } } - sqlite3_free( pPager->aInJournal ); - pPager->aInJournal = 0; + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ pPg->inJournal = 0; pPg->dirty = 0; @@ -21616,7 +24199,7 @@ static int pager_end_transaction(Pager *pPager){ pPager->dirtyCache = 0; pPager->nRec = 0; }else{ - assert( pPager->aInJournal==0 ); + assert( pPager->pInJournal==0 ); assert( pPager->dirtyCache==0 || pPager->useJournal==0 ); } @@ -22347,13 +24930,15 @@ SQLITE_PRIVATE int sqlite3PagerOpen( int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE; char *zPathname; int nPathname; + char *zStmtJrnl; + int nStmtJrnl; /* The default return is a NULL pointer */ *ppPager = 0; /* Compute the full pathname */ nPathname = pVfs->mxPathname+1; - zPathname = sqlite3_malloc(nPathname); + zPathname = sqlite3_malloc(nPathname*2); if( zPathname==0 ){ return SQLITE_NOMEM; } @@ -22376,12 +24961,26 @@ SQLITE_PRIVATE int sqlite3PagerOpen( } nPathname = strlen(zPathname); + /* Put the statement journal in temporary disk space since this is + ** sometimes RAM disk or other optimized storage. Unlikely the main + ** main journal file, the statement journal does not need to be + ** colocated with the database nor does it need to be persistent. + */ + zStmtJrnl = &zPathname[nPathname+1]; + rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl); + if( rc!=SQLITE_OK ){ + sqlite3_free(zPathname); + return rc; + } + nStmtJrnl = strlen(zStmtJrnl); + /* Allocate memory for the pager structure */ pPager = sqlite3MallocZero( sizeof(*pPager) + /* Pager structure */ journalFileSize + /* The journal file structure */ - pVfs->szOsFile * 2 + /* The db and stmt journal files */ - 4*nPathname + 40 /* zFilename, zDirectory, zJournal, zStmtJrnl */ + pVfs->szOsFile * 3 + /* The main db and two journal files */ + 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */ + nStmtJrnl /* zStmtJrnl */ ); if( !pPager ){ sqlite3_free(zPathname); @@ -22398,6 +24997,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen( pPager->zStmtJrnl = &pPager->zJournal[nPathname+10]; pPager->pVfs = pVfs; memcpy(pPager->zFilename, zPathname, nPathname+1); + memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1); sqlite3_free(zPathname); /* Open the pager file. @@ -22473,11 +25073,9 @@ SQLITE_PRIVATE int sqlite3PagerOpen( for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){} if( i>0 ) pPager->zDirectory[i-1] = 0; - /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */ + /* Fill in Pager.zJournal[] */ memcpy(pPager->zJournal, pPager->zFilename, nPathname); memcpy(&pPager->zJournal[nPathname], "-journal", 9); - memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname); - memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10); /* pPager->journalOpen = 0; */ pPager->useJournal = useJournal && !memDb; @@ -22684,7 +25282,7 @@ SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager){ int rc; assert( pPager!=0 ); if( pPager->errCode ){ - return 0; + return -1; } if( pPager->dbSize>=0 ){ n = pPager->dbSize; @@ -22695,7 +25293,7 @@ SQLITE_PRIVATE int sqlite3PagerPagecount(Pager *pPager){ pPager->nRef++; pager_error(pPager, rc); pPager->nRef--; - return 0; + return -1; } if( n>0 && npageSize ){ n = 1; @@ -22807,6 +25405,7 @@ static void pager_truncate_cache(Pager *pPager){ PAGER_INCR(sqlite3_pager_pgfree_count); unlinkPage(pPg); makeClean(pPg); + sqlite3_free(pPg->pData); sqlite3_free(pPg); pPager->nPage--; } @@ -22835,6 +25434,7 @@ static int pager_wait_on_lock(Pager *pPager, int locktype){ if( pPager->state>=locktype ){ rc = SQLITE_OK; }else{ + if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0; do { rc = sqlite3OsLock(pPager->fd, locktype); }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) ); @@ -22927,7 +25527,7 @@ SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){ if( pPager->journalOpen ){ sqlite3OsClose(pPager->jfd); } - sqlite3_free(pPager->aInJournal); + sqlite3BitvecDestroy(pPager->pInJournal); if( pPager->stmtOpen ){ sqlite3OsClose(pPager->stfd); } @@ -22969,7 +25569,6 @@ static void _page_ref(PgHdr *pPg){ pPg->pPager->nRef++; } pPg->nRef++; - REFINFO(pPg); } #ifdef SQLITE_DEBUG static void page_ref(PgHdr *pPg){ @@ -22977,7 +25576,6 @@ static void _page_ref(PgHdr *pPg){ _page_ref(pPg); }else{ pPg->nRef++; - REFINFO(pPg); } } #else @@ -23268,6 +25866,19 @@ static int pager_write_pagelist(PgHdr *pList){ ** collected even if they are still in use. */ static PgHdr *pager_get_all_dirty_pages(Pager *pPager){ + +#ifndef NDEBUG + /* Verify the sanity of the dirty list when we are running + ** in debugging mode. This is expensive, so do not + ** do this on a normal build. */ + int n1 = 0; + int n2 = 0; + PgHdr *p; + for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; } + for(p=pPager->pDirty; p; p=p->pDirty){ n2++; } + assert( n1==n2 ); +#endif + return pPager->pDirty; } @@ -23475,6 +26086,7 @@ SQLITE_PRIVATE int sqlite3PagerReleaseMemory(int nReq){ ); IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno)); PAGER_INCR(sqlite3_pager_pgfree_count); + sqlite3_free(pPg->pData); sqlite3_free(pPg); pPager->nPage--; }else{ @@ -23634,7 +26246,14 @@ static int pagerSharedLock(Pager *pPager){ } if( rc!=SQLITE_OK ){ pager_unlock(pPager); - return ((rc==SQLITE_NOMEM||rc==SQLITE_IOERR_NOMEM)?rc:SQLITE_BUSY); + switch( rc ){ + case SQLITE_NOMEM: + case SQLITE_IOERR_UNLOCK: + case SQLITE_IOERR_NOMEM: + return rc; + default: + return SQLITE_BUSY; + } } pPager->journalOpen = 1; pPager->journalStarted = 0; @@ -23746,6 +26365,7 @@ static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ || MEMDB || (pPager->lru.pFirstSynced==0 && pPager->doNotSync) ){ + void *pData; if( pPager->nPage>=pPager->nHash ){ pager_resize_hash_table(pPager, pPager->nHash<256 ? 256 : pPager->nHash*2); @@ -23757,14 +26377,21 @@ static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){ pagerLeave(pPager); nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra + MEMDB*sizeof(PgHistory); - pPg = sqlite3_malloc( nByteHdr + pPager->pageSize ); + pPg = sqlite3_malloc( nByteHdr ); + if( pPg ){ + pData = sqlite3_malloc( pPager->pageSize ); + if( pData==0 ){ + sqlite3_free(pPg); + pPg = 0; + } + } pagerEnter(pPager); if( pPg==0 ){ rc = SQLITE_NOMEM; goto pager_allocate_out; } memset(pPg, 0, nByteHdr); - pPg->pData = (void*)(nByteHdr + (char*)pPg); + pPg->pData = pData; pPg->pPager = pPager; pPg->pNextAll = pPager->pAll; pPager->pAll = pPg; @@ -23884,21 +26511,11 @@ static int pagerAcquire( pPg->pgno = pgno; assert( !MEMDB || pgno>pPager->stmtSize ); - if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ -#if 0 - sqlite3CheckMemory(pPager->aInJournal, pgno/8); -#endif - assert( pPager->journalOpen ); - pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; - pPg->needSync = 0; - }else{ - pPg->inJournal = 0; - pPg->needSync = 0; - } + pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno); + pPg->needSync = 0; makeClean(pPg); pPg->nRef = 1; - REFINFO(pPg); pPager->nRef++; if( pPager->nExtra>0 ){ @@ -24019,7 +26636,6 @@ SQLITE_PRIVATE int sqlite3PagerUnref(DbPage *pPg){ assert( pPg->nRef>0 ); pagerEnter(pPg->pPager); pPg->nRef--; - REFINFO(pPg); CHECK_PAGE(pPg); @@ -24062,12 +26678,12 @@ static int pager_open_journal(Pager *pPager){ assert( pPager->state>=PAGER_RESERVED ); assert( pPager->journalOpen==0 ); assert( pPager->useJournal ); - assert( pPager->aInJournal==0 ); + assert( pPager->pInJournal==0 ); sqlite3PagerPagecount(pPager); pagerLeave(pPager); - pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); + pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize); pagerEnter(pPager); - if( pPager->aInJournal==0 ){ + if( pPager->pInJournal==0 ){ rc = SQLITE_NOMEM; goto failed_to_open_journal; } @@ -24119,8 +26735,8 @@ static int pager_open_journal(Pager *pPager){ return rc; failed_to_open_journal: - sqlite3_free(pPager->aInJournal); - pPager->aInJournal = 0; + sqlite3BitvecDestroy(pPager->pInJournal); + pPager->pInJournal = 0; return rc; } @@ -24158,7 +26774,7 @@ SQLITE_PRIVATE int sqlite3PagerBegin(DbPage *pPg, int exFlag){ assert( pPg->nRef>0 ); assert( pPager->state!=PAGER_UNLOCK ); if( pPager->state==PAGER_SHARED ){ - assert( pPager->aInJournal==0 ); + assert( pPager->pInJournal==0 ); if( MEMDB ){ pPager->state = PAGER_EXCLUSIVE; pPager->origDbSize = pPager->dbSize; @@ -24188,12 +26804,12 @@ SQLITE_PRIVATE int sqlite3PagerBegin(DbPage *pPg, int exFlag){ */ assert( pPager->nRec==0 ); assert( pPager->origDbSize==0 ); - assert( pPager->aInJournal==0 ); + assert( pPager->pInJournal==0 ); sqlite3PagerPagecount(pPager); pagerLeave(pPager); - pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 ); + pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize ); pagerEnter(pPager); - if( !pPager->aInJournal ){ + if( !pPager->pInJournal ){ rc = SQLITE_NOMEM; }else{ pPager->origDbSize = pPager->dbSize; @@ -24369,11 +26985,11 @@ static int pager_write(PgHdr *pPg){ } pPager->nRec++; - assert( pPager->aInJournal!=0 ); - pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); + assert( pPager->pInJournal!=0 ); + sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); pPg->needSync = !pPager->noSync; if( pPager->stmtInUse ){ - pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); + sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); } } }else{ @@ -24418,8 +27034,8 @@ static int pager_write(PgHdr *pPg){ return rc; } pPager->stmtNRec++; - assert( pPager->aInStmt!=0 ); - pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); + assert( pPager->pInStmt!=0 ); + sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); } } } @@ -24488,9 +27104,7 @@ SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){ for(ii=0; iiaInJournal || pg==pPg->pgno || - pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7))) - ) { + if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){ if( pg!=PAGER_MJ_PGNO(pPager) ){ rc = sqlite3PagerGet(pPager, pg, &pPage); if( rc==SQLITE_OK ){ @@ -24501,7 +27115,7 @@ SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){ sqlite3PagerUnref(pPage); } } - }else if( (pPage = pager_lookup(pPager, pg)) ){ + }else if( (pPage = pager_lookup(pPager, pg))!=0 ){ if( pPage->needSync ){ needSync = 1; } @@ -24629,33 +27243,51 @@ SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage *pDbPage){ ** the PgHdr.needRead flag is set) then this routine acts as a promise ** that we will never need to read the page content in the future. ** so the needRead flag can be cleared at this point. +** +** This routine is only called from a single place in the sqlite btree +** code (when a leaf is removed from the free-list). This allows the +** following assumptions to be made about pPg: +** +** 1. PagerDontWrite() has been called on the page, OR +** PagerWrite() has not yet been called on the page. +** +** 2. The page existed when the transaction was started. +** +** Details: DontRollback() (this routine) is only called when a leaf is +** removed from the free list. DontWrite() is called whenever a page +** becomes a free-list leaf. */ SQLITE_PRIVATE void sqlite3PagerDontRollback(DbPage *pPg){ Pager *pPager = pPg->pPager; pagerEnter(pPager); assert( pPager->state>=PAGER_RESERVED ); - if( pPager->journalOpen==0 ) return; - if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return; - if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){ - assert( pPager->aInJournal!=0 ); - pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); - pPg->inJournal = 1; - pPg->needRead = 0; - if( pPager->stmtInUse ){ - pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); - } - PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); - IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) - } - if( pPager->stmtInUse - && !pageInStatement(pPg) - && (int)pPg->pgno<=pPager->stmtSize - ){ - assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize ); - assert( pPager->aInStmt!=0 ); - pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7); + + /* If the journal file is not open, or DontWrite() has been called on + ** this page (DontWrite() sets the alwaysRollback flag), then this + ** function is a no-op. + */ + if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){ + pagerLeave(pPager); + return; } + assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */ + + /* Check that PagerWrite() has not yet been called on this page, and + ** that the page existed when the transaction started. + */ + assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ); + + assert( pPager->pInJournal!=0 ); + sqlite3BitvecSet(pPager->pInJournal, pPg->pgno); + pPg->inJournal = 1; + pPg->needRead = 0; + if( pPager->stmtInUse ){ + assert( pPager->stmtSize <= pPager->origDbSize ); + sqlite3BitvecSet(pPager->pInStmt, pPg->pgno); + } + PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager)); + IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno)) pagerLeave(pPager); } @@ -24788,7 +27420,7 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster Pgno i; int iSkip = PAGER_MJ_PGNO(pPager); for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){ - if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){ + if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){ rc = sqlite3PagerGet(pPager, i, &pPg); if( rc!=SQLITE_OK ) goto sync_exit; rc = sqlite3PagerWrite(pPg); @@ -24815,8 +27447,14 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster pPg = pager_get_all_dirty_pages(pPager); rc = pager_write_pagelist(pPg); if( rc!=SQLITE_OK ){ - while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; } - pPager->pDirty = pPg; + assert( rc!=SQLITE_IOERR_BLOCKED ); + /* The error might have left the dirty list all fouled up here, + ** but that does not matter because if the if the dirty list did + ** get corrupted, then the transaction will roll back and + ** discard the dirty list. There is an assert in + ** pager_get_all_dirty_pages() that verifies that no attempt + ** is made to use an invalid dirty list. + */ goto sync_exit; } pPager->pDirty = 0; @@ -24888,6 +27526,7 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){ #endif pPager->pStmt = 0; pPager->state = PAGER_SHARED; + pagerLeave(pPager); return SQLITE_OK; } assert( pPager->journalOpen || !pPager->dirtyCache ); @@ -25045,10 +27684,10 @@ static int pagerStmtBegin(Pager *pPager){ } assert( pPager->journalOpen ); pagerLeave(pPager); - assert( pPager->aInStmt==0 ); - pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 ); + assert( pPager->pInStmt==0 ); + pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize); pagerEnter(pPager); - if( pPager->aInStmt==0 ){ + if( pPager->pInStmt==0 ){ /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */ return SQLITE_NOMEM; } @@ -25074,9 +27713,9 @@ static int pagerStmtBegin(Pager *pPager){ return SQLITE_OK; stmt_begin_failed: - if( pPager->aInStmt ){ - sqlite3_free(pPager->aInStmt); - pPager->aInStmt = 0; + if( pPager->pInStmt ){ + sqlite3BitvecDestroy(pPager->pInStmt); + pPager->pInStmt = 0; } return rc; } @@ -25098,8 +27737,8 @@ SQLITE_PRIVATE int sqlite3PagerStmtCommit(Pager *pPager){ PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager)); if( !MEMDB ){ /* sqlite3OsTruncate(pPager->stfd, 0); */ - sqlite3_free( pPager->aInStmt ); - pPager->aInStmt = 0; + sqlite3BitvecDestroy(pPager->pInStmt); + pPager->pInStmt = 0; }else{ for(pPg=pPager->pStmt; pPg; pPg=pNext){ PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager); @@ -25215,7 +27854,7 @@ SQLITE_PRIVATE void sqlite3PagerSetCodec( #ifndef SQLITE_OMIT_AUTOVACUUM /* -** Move the page pPg to location pgno in the file. +** Move the page pPg to location pgno in the file. ** ** There must be no references to the page previously located at ** pgno (which we call pPgOld) though that page is allowed to be @@ -25269,12 +27908,7 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ }else{ pPg->needSync = 0; } - if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){ - pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; - }else{ - pPg->inJournal = 0; - assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize ); - } + pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno); /* Change the page number for pPg and insert it into the new hash-chain. */ assert( pgno!=0 ); @@ -25295,9 +27929,16 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ /* If needSyncPgno is non-zero, then the journal file needs to be ** sync()ed before any data is written to database file page needSyncPgno. ** Currently, no such page exists in the page-cache and the - ** Pager.aInJournal bit has been set. This needs to be remedied by loading + ** Pager.pInJournal bit has been set. This needs to be remedied by loading ** the page into the pager-cache and setting the PgHdr.needSync flag. ** + ** If the attempt to load the page into the page-cache fails, (due + ** to a malloc() or IO failure), clear the bit in the pInJournal[] + ** array. Otherwise, if the page is loaded and written again in + ** this transaction, it may be written to the database file before + ** it is synced into the journal file. This way, it may end up in + ** the journal file twice, but that is not a problem. + ** ** The sqlite3PagerGet() call may cause the journal to sync. So make ** sure the Pager.needSync flag is set too. */ @@ -25305,7 +27946,13 @@ SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){ PgHdr *pPgHdr; assert( pPager->needSync ); rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr); - if( rc!=SQLITE_OK ) return rc; + if( rc!=SQLITE_OK ){ + if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){ + sqlite3BitvecClear(pPager->pInJournal, needSyncPgno); + } + pagerLeave(pPager); + return rc; + } pPager->needSync = 1; pPgHdr->needSync = 1; pPgHdr->inJournal = 1; @@ -25386,7 +28033,7 @@ SQLITE_PRIVATE void sqlite3PagerRefdump(Pager *pPager){ ** ************************************************************************* ** -** $Id: btmutex.c,v 1.8 2007/12/07 18:55:28 drh Exp $ +** $Id: btmutex.c,v 1.9 2008/01/23 12:52:41 drh Exp $ ** ** This file contains code used to implement mutexes on Btree objects. ** This code really belongs in btree.c. But btree.c is getting too @@ -25406,7 +28053,7 @@ SQLITE_PRIVATE void sqlite3PagerRefdump(Pager *pPager){ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btreeInt.h,v 1.14 2007/12/07 18:55:28 drh Exp $ +** $Id: btreeInt.h,v 1.17 2008/03/04 17:45:01 mlcreech Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to @@ -25613,10 +28260,11 @@ SQLITE_PRIVATE void sqlite3PagerRefdump(Pager *pPager){ #define MX_CELL_SIZE(pBt) (pBt->pageSize-8) /* The maximum number of cells on a single page of the database. This -** assumes a minimum cell size of 3 bytes. Such small cells will be -** exceedingly rare, but they are possible. +** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself +** plus 2 bytes for the index to the cell in the page header). Such +** small cells will be rare, but they are possible. */ -#define MX_CELL(pBt) ((pBt->pageSize-8)/3) +#define MX_CELL(pBt) ((pBt->pageSize-8)/6) /* Forward declarations */ typedef struct MemPage MemPage; @@ -25787,6 +28435,7 @@ struct BtShared { int nRef; /* Number of references to this structure */ BtShared *pNext; /* Next on a list of sharable BtShared structs */ BtLock *pLock; /* List of locks held on this shared-btree struct */ + Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */ #endif }; @@ -25874,11 +28523,11 @@ struct BtCursor { /* ** The TRACE macro will print high-level status information about the -** btree operation when the global variable sqlite3_btree_trace is +** btree operation when the global variable sqlite3BtreeTrace is ** enabled. */ #if SQLITE_TEST -# define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); } +# define TRACE(X) if( sqlite3BtreeTrace ){ printf X; fflush(stdout); } #else # define TRACE(X) #endif @@ -26085,6 +28734,7 @@ SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){ p->wantToLock++; if( p->locked ) return; +#ifndef SQLITE_MUTEX_NOOP /* In most cases, we should be able to acquire the lock we ** want without having to go throught the ascending lock ** procedure that follows. Just be sure not to block. @@ -26116,6 +28766,7 @@ SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){ pLater->locked = 1; } } +#endif /* SQLITE_MUTEX_NOOP */ } /* @@ -26351,7 +29002,7 @@ SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.433 2007/12/13 21:54:11 drh Exp $ +** $Id: btree.c,v 1.440 2008/03/04 17:45:01 mlcreech Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. @@ -26369,7 +29020,7 @@ static const char zMagicHeader[] = SQLITE_FILE_HEADER; ** macro. */ #if SQLITE_TEST -int sqlite3_btree_trace=0; /* True to enable tracing */ +int sqlite3BtreeTrace=0; /* True to enable tracing */ #endif @@ -26444,6 +29095,13 @@ static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ return SQLITE_OK; } + /* If some other connection is holding an exclusive lock, the + ** requested lock may not be obtained. + */ + if( pBt->pExclusive && pBt->pExclusive!=p ){ + return SQLITE_LOCKED; + } + /* This (along with lockTable()) is where the ReadUncommitted flag is ** dealt with. If the caller is querying for a read-lock and the flag is ** set, it is unconditionally granted - even if there are write-locks @@ -26553,13 +29211,15 @@ static int lockTable(Btree *p, Pgno iTable, u8 eLock){ ** procedure) held by Btree handle p. */ static void unlockAllTables(Btree *p){ - BtLock **ppIter = &p->pBt->pLock; + BtShared *pBt = p->pBt; + BtLock **ppIter = &pBt->pLock; assert( sqlite3BtreeHoldsMutex(p) ); assert( p->sharable || 0==*ppIter ); while( *ppIter ){ BtLock *pLock = *ppIter; + assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree ); if( pLock->pBtree==p ){ *ppIter = pLock->pNext; sqlite3_free(pLock); @@ -26567,6 +29227,10 @@ static void unlockAllTables(Btree *p){ ppIter = &pLock->pNext; } } + + if( pBt->pExclusive==p ){ + pBt->pExclusive = 0; + } } #endif /* SQLITE_OMIT_SHARED_CACHE */ @@ -26950,13 +29614,13 @@ SQLITE_PRIVATE void sqlite3BtreeParseCell( ** the space used by the cell pointer. */ #ifndef NDEBUG -static int cellSize(MemPage *pPage, int iCell){ +static u16 cellSize(MemPage *pPage, int iCell){ CellInfo info; sqlite3BtreeParseCell(pPage, iCell, &info); return info.nSize; } #endif -static int cellSizePtr(MemPage *pPage, u8 *pCell){ +static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ CellInfo info; sqlite3BtreeParseCellPtr(pPage, pCell, &info); return info.nSize; @@ -28075,12 +30739,15 @@ static void unlockBtreeIfUnused(BtShared *pBt){ assert( sqlite3_mutex_held(pBt->mutex) ); if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ + assert( pBt->pPage1->aData ); +#if 0 if( pBt->pPage1->aData==0 ){ MemPage *pPage = pBt->pPage1; pPage->aData = sqlite3PagerGetData(pPage->pDbPage); pPage->pBt = pBt; pPage->pgno = 1; } +#endif releasePage(pBt->pPage1); } pBt->pPage1 = 0; @@ -28191,6 +30858,18 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ goto trans_begun; } +#ifndef SQLITE_OMIT_SHARED_CACHE + if( wrflag>1 ){ + BtLock *pIter; + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + if( pIter->pBtree!=p ){ + rc = SQLITE_BUSY; + goto trans_begun; + } + } + } +#endif + do { if( pBt->pPage1==0 ){ rc = lockBtree(pBt); @@ -28223,6 +30902,12 @@ SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ if( p->inTrans>pBt->inTransaction ){ pBt->inTransaction = p->inTrans; } +#ifndef SQLITE_OMIT_SHARED_CACHE + if( wrflag>1 ){ + assert( !pBt->pExclusive ); + pBt->pExclusive = p; + } +#endif } @@ -30405,6 +33090,7 @@ static int allocateBtreePage( TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); (*pPgno)++; + if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } } if( pBt->nTrunc ){ pBt->nTrunc = *pPgno; @@ -30899,7 +33585,7 @@ static void assemblePage( MemPage *pPage, /* The page to be assemblied */ int nCell, /* The number of cells to add to this page */ u8 **apCell, /* Pointers to cell bodies */ - int *aSize /* Sizes of the cells */ + u16 *aSize /* Sizes of the cells */ ){ int i; /* Loop counter */ int totalSize; /* Total size of all cells */ @@ -30977,7 +33663,7 @@ static int balance_quick(MemPage *pPage, MemPage *pParent){ MemPage *pNew; Pgno pgnoNew; u8 *pCell; - int szCell; + u16 szCell; CellInfo info; BtShared *pBt = pPage->pBt; int parentIdx = pParent->nCell; /* pParent new divider cell index */ @@ -31103,7 +33789,7 @@ static int balance_nonroot(MemPage *pPage){ int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ int szNew[NB+2]; /* Combined size of cells place on i-th page */ u8 **apCell = 0; /* All cells begin balanced */ - int *szCell; /* Local size of all cells in apCell[] */ + u16 *szCell; /* Local size of all cells in apCell[] */ u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ u8 *aSpace; /* Space to hold copies of dividers cells */ #ifndef SQLITE_OMIT_AUTOVACUUM @@ -31216,25 +33902,25 @@ static int balance_nonroot(MemPage *pPage){ nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; } - /* Make nMaxCells a multiple of 2 in order to preserve 8-byte + /* Make nMaxCells a multiple of 4 in order to preserve 8-byte ** alignment */ - nMaxCells = (nMaxCells + 1)&~1; + nMaxCells = (nMaxCells + 3)&~3; /* ** Allocate space for memory structures */ apCell = sqlite3_malloc( - nMaxCells*sizeof(u8*) /* apCell */ - + nMaxCells*sizeof(int) /* szCell */ - + ROUND8(sizeof(MemPage))*NB /* aCopy */ - + pBt->pageSize*(5+NB) /* aSpace */ - + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ + nMaxCells*sizeof(u8*) /* apCell */ + + nMaxCells*sizeof(u16) /* szCell */ + + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ + + pBt->pageSize*5 /* aSpace */ + + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ ); if( apCell==0 ){ rc = SQLITE_NOMEM; goto balance_cleanup; } - szCell = (int*)&apCell[nMaxCells]; + szCell = (u16*)&apCell[nMaxCells]; aCopy[0] = (u8*)&szCell[nMaxCells]; assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ for(i=1; ipParent==0 ); assert( pPage->nCell==0 ); assert( sqlite3_mutex_held(pPage->pBt->mutex) ); pBt = pPage->pBt; mxCellPerPage = MX_CELL(pBt); - apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); + apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); if( apCell==0 ) return SQLITE_NOMEM; - szCell = (int*)&apCell[mxCellPerPage]; + szCell = (u16*)&apCell[mxCellPerPage]; if( pPage->leaf ){ /* The table is completely empty */ TRACE(("BALANCE: empty table %d\n", pPage->pgno)); @@ -31936,7 +34622,7 @@ SQLITE_PRIVATE int sqlite3BtreeInsert( assert( szNew==cellSizePtr(pPage, newCell) ); assert( szNew<=MX_CELL_SIZE(pBt) ); if( loc==0 && CURSOR_VALID==pCur->eState ){ - int szOld; + u16 szOld; assert( pCur->idx>=0 && pCur->idxnCell ); rc = sqlite3PagerWrite(pPage->pDbPage); if( rc ){ @@ -32039,9 +34725,6 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){ */ BtCursor leafCur; unsigned char *pNext; - int szNext; /* The compiler warning is wrong: szNext is always - ** initialized before use. Adding an extra initialization - ** to silence the compiler slows down the code. */ int notUsed; unsigned char *tempCell = 0; assert( !pPage->leafData ); @@ -32051,6 +34734,7 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){ rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); } if( rc==SQLITE_OK ){ + u16 szNext; TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); @@ -32061,17 +34745,17 @@ SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){ if( tempCell==0 ){ rc = SQLITE_NOMEM; } - } - if( rc==SQLITE_OK ){ - rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); - } - if( rc==SQLITE_OK ){ - put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); - rc = balance(pPage, 0); - } - if( rc==SQLITE_OK ){ - dropCell(leafCur.pPage, leafCur.idx, szNext); - rc = balance(leafCur.pPage, 0); + if( rc==SQLITE_OK ){ + rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); + } + if( rc==SQLITE_OK ){ + put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); + rc = balance(pPage, 0); + } + if( rc==SQLITE_OK ){ + dropCell(leafCur.pPage, leafCur.idx, szNext); + rc = balance(leafCur.pPage, 0); + } } sqlite3_free(tempCell); sqlite3BtreeReleaseTempCursor(&leafCur); @@ -32142,7 +34826,7 @@ static int btreeCreateTable(Btree *p, int *piTable, int flags){ /* The new root-page may not be allocated on a pointer-map page, or the ** PENDING_BYTE page. */ - if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || + while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ pgnoRoot++; } @@ -32541,7 +35225,9 @@ SQLITE_PRIVATE int sqlite3BtreeFlags(BtCursor *pCur){ /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call ** restoreOrClearCursorPosition() here. */ - MemPage *pPage = pCur->pPage; + MemPage *pPage; + restoreOrClearCursorPosition(pCur); + pPage = pCur->pPage; assert( cursorHoldsMutex(pCur) ); assert( pPage->pBt==pCur->pBt ); return pPage ? pPage->aData[pPage->hdrOffset] : 0; @@ -32833,7 +35519,7 @@ static int checkTreePage( cellStart = hdr + 12 - 4*pPage->leaf; for(i=0; i=usableSize || pc<0 ){ checkAppendMsg(pCheck, 0, @@ -33266,13 +35952,25 @@ SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){ */ /* +** Constants FIFOSIZE_FIRST and FIFOSIZE_MAX are the initial +** number of entries in a fifo page and the maximum number of +** entries in a fifo page. +*/ +#define FIFOSIZE_FIRST (((128-sizeof(FifoPage))/8)+1) +#ifdef SQLITE_MALLOC_SOFT_LIMIT +# define FIFOSIZE_MAX (((SQLITE_MALLOC_SOFT_LIMIT-sizeof(FifoPage))/8)+1) +#else +# define FIFOSIZE_MAX (((262144-sizeof(FifoPage))/8)+1) +#endif + +/* ** Allocate a new FifoPage and return a pointer to it. Return NULL if ** we run out of memory. Leave space on the page for nEntry entries. */ static FifoPage *allocateFifoPage(int nEntry){ FifoPage *pPage; - if( nEntry>32767 ){ - nEntry = 32767; + if( nEntry>FIFOSIZE_MAX ){ + nEntry = FIFOSIZE_MAX; } pPage = sqlite3_malloc( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) ); if( pPage ){ @@ -33300,7 +35998,7 @@ SQLITE_PRIVATE int sqlite3VdbeFifoPush(Fifo *pFifo, i64 val){ FifoPage *pPage; pPage = pFifo->pLast; if( pPage==0 ){ - pPage = pFifo->pLast = pFifo->pFirst = allocateFifoPage(20); + pPage = pFifo->pLast = pFifo->pFirst = allocateFifoPage(FIFOSIZE_FIRST); if( pPage==0 ){ return SQLITE_NOMEM; } @@ -33424,32 +36122,82 @@ SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ } /* +** Make sure pMem->z points to a writable allocation of at least +** n bytes. +** +** If the memory cell currently contains string or blob data +** and the third argument passed to this function is true, the +** current content of the cell is preserved. Otherwise, it may +** be discarded. +** +** This function sets the MEM_Dyn flag and clears any xDel callback. +** It also clears MEM_Ephem and MEM_Static. If the preserve flag is +** not set, Mem.n is zeroed. +*/ +SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ + int f = pMem->flags; + + assert( (f & (MEM_Dyn|MEM_Static|MEM_Ephem))==0 + || (f & (MEM_Dyn|MEM_Static|MEM_Ephem))==MEM_Dyn + || (f & (MEM_Dyn|MEM_Static|MEM_Ephem))==MEM_Ephem + || (f & (MEM_Dyn|MEM_Static|MEM_Ephem))==MEM_Static + ); + + if( ((f&MEM_Dyn)==0 || pMem->xDel || sqlite3MallocSize(pMem->z)0 ){ + if( preserve && (f&MEM_Dyn) && !pMem->xDel ){ + z = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); + pMem->z = 0; + preserve = 0; + }else{ + z = sqlite3DbMallocRaw(pMem->db, (n>32?n:32)); + } + if( !z ){ + return SQLITE_NOMEM; + } + } + + /* If the value is currently a string or blob and the preserve flag + ** is true, copy the content to the new buffer. + */ + if( pMem->flags&(MEM_Blob|MEM_Str) && preserve ){ + int nCopy = (pMem->n>n?n:pMem->n); + memcpy(z, pMem->z, nCopy); + } + + /* Release the old buffer. */ + sqlite3VdbeMemRelease(pMem); + + pMem->z = z; + pMem->flags |= MEM_Dyn; + pMem->flags &= ~(MEM_Ephem|MEM_Static); + pMem->xDel = 0; + } + return SQLITE_OK; +} + +/* ** Make the given Mem object MEM_Dyn. ** ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. */ SQLITE_PRIVATE int sqlite3VdbeMemDynamicify(Mem *pMem){ - int n; - u8 *z; + int f; assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); expandBlob(pMem); - if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){ - return SQLITE_OK; - } - assert( (pMem->flags & MEM_Dyn)==0 ); - n = pMem->n; - assert( pMem->flags & (MEM_Str|MEM_Blob) ); - z = sqlite3DbMallocRaw(pMem->db, n+2 ); - if( z==0 ){ - return SQLITE_NOMEM; + f = pMem->flags; + if( (f&(MEM_Str|MEM_Blob)) && ((f&MEM_Dyn)==0 || pMem->xDel) ){ + if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ + return SQLITE_NOMEM; + } + pMem->z[pMem->n] = 0; + pMem->z[pMem->n+1] = 0; + pMem->flags |= MEM_Term; } - pMem->flags |= MEM_Dyn|MEM_Term; - pMem->xDel = 0; - memcpy(z, pMem->z, n ); - z[n] = 0; - z[n+1] = 0; - pMem->z = (char*)z; - pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short); + return SQLITE_OK; } @@ -33460,24 +36208,22 @@ SQLITE_PRIVATE int sqlite3VdbeMemDynamicify(Mem *pMem){ #ifndef SQLITE_OMIT_INCRBLOB SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){ if( pMem->flags & MEM_Zero ){ - char *pNew; int nByte; - assert( (pMem->flags & MEM_Blob)!=0 ); - nByte = pMem->n + pMem->u.i; - if( nByte<=0 ) nByte = 1; + assert( pMem->flags&MEM_Blob ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - pNew = sqlite3DbMallocRaw(pMem->db, nByte); - if( pNew==0 ){ + + /* Set nByte to the number of bytes required to store the expanded blob. */ + nByte = pMem->n + pMem->u.i; + if( nByte<=0 ){ + nByte = 1; + } + if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ return SQLITE_NOMEM; } - memcpy(pNew, pMem->z, pMem->n); - memset(&pNew[pMem->n], 0, pMem->u.i); - sqlite3VdbeMemRelease(pMem); - pMem->z = pNew; + + memset(&pMem->z[pMem->n], 0, pMem->u.i); pMem->n += pMem->u.i; - pMem->u.i = 0; - pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term); - pMem->flags |= MEM_Dyn; + pMem->flags &= ~(MEM_Zero|MEM_Term); } return SQLITE_OK; } @@ -33491,33 +36237,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){ ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. */ SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){ - int n; - u8 *z; - assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - expandBlob(pMem); - if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){ - return SQLITE_OK; - } - assert( (pMem->flags & MEM_Dyn)==0 ); - assert( pMem->flags & (MEM_Str|MEM_Blob) ); - if( (n = pMem->n)+2zShort) ){ - z = (u8*)pMem->zShort; - pMem->flags |= MEM_Short|MEM_Term; - }else{ - z = sqlite3DbMallocRaw(pMem->db, n+2 ); - if( z==0 ){ - return SQLITE_NOMEM; - } - pMem->flags |= MEM_Dyn|MEM_Term; - pMem->xDel = 0; - } - memcpy(z, pMem->z, n ); - z[n] = 0; - z[n+1] = 0; - pMem->z = (char*)z; - pMem->flags &= ~(MEM_Ephem|MEM_Static); - assert(0==(1&(int)pMem->z)); - return SQLITE_OK; + return sqlite3VdbeMemDynamicify(pMem); } /* @@ -33528,27 +36248,12 @@ SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){ if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){ return SQLITE_OK; /* Nothing to do */ } - if( pMem->flags & (MEM_Static|MEM_Ephem) ){ - return sqlite3VdbeMemMakeWriteable(pMem); - }else{ - char *z; - sqlite3VdbeMemExpandBlob(pMem); - z = sqlite3DbMallocRaw(pMem->db, pMem->n+2); - if( !z ){ - return SQLITE_NOMEM; - } - memcpy(z, pMem->z, pMem->n); - z[pMem->n] = 0; - z[pMem->n+1] = 0; - if( pMem->xDel ){ - pMem->xDel(pMem->z); - }else{ - sqlite3_free(pMem->z); - } - pMem->xDel = 0; - pMem->z = z; - pMem->flags |= MEM_Term; + if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ + return SQLITE_NOMEM; } + pMem->z[pMem->n] = 0; + pMem->z[pMem->n+1] = 0; + pMem->flags |= MEM_Term; return SQLITE_OK; } @@ -33568,29 +36273,32 @@ SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){ SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){ int rc = SQLITE_OK; int fg = pMem->flags; - char *z = pMem->zShort; + const int nByte = 32; assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); assert( !(fg&MEM_Zero) ); assert( !(fg&(MEM_Str|MEM_Blob)) ); assert( fg&(MEM_Int|MEM_Real) ); - /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8 + if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ + return SQLITE_NOMEM; + } + + /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8 ** string representation of the value. Then, if the required encoding ** is UTF-16le or UTF-16be do a translation. ** ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. */ if( fg & MEM_Int ){ - sqlite3_snprintf(NBFS, z, "%lld", pMem->u.i); + sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); }else{ assert( fg & MEM_Real ); - sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r); + sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r); } - pMem->n = strlen(z); - pMem->z = z; + pMem->n = strlen(pMem->z); pMem->enc = SQLITE_UTF8; - pMem->flags |= MEM_Str | MEM_Short | MEM_Term; + pMem->flags |= MEM_Str|MEM_Term; sqlite3VdbeChangeEncoding(pMem, enc); return rc; } @@ -33610,19 +36318,15 @@ SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); ctx.s.flags = MEM_Null; - ctx.s.z = pMem->zShort; ctx.s.db = pMem->db; ctx.pMem = pMem; ctx.pFunc = pFunc; ctx.isError = 0; pFunc->xFinalize(&ctx); - if( pMem->z && pMem->z!=pMem->zShort ){ + if( pMem->z ){ sqlite3_free( pMem->z ); } *pMem = ctx.s; - if( pMem->flags & MEM_Short ){ - pMem->z = pMem->zShort; - } rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK); } return rc; @@ -33653,6 +36357,38 @@ SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){ } /* +** Convert a 64-bit IEEE double into a 64-bit signed integer. +** If the double is too large, return 0x8000000000000000. +** +** Most systems appear to do this simply by assigning +** variables and without the extra range tests. But +** there are reports that windows throws an expection +** if the floating point value is out of range. (See ticket #2880.) +** Because we do not completely understand the problem, we will +** take the conservative approach and always do range tests +** before attempting the conversion. +*/ +static i64 doubleToInt64(double r){ + /* + ** Many compilers we encounter do not define constants for the + ** minimum and maximum 64-bit integers, or they define them + ** inconsistently. And many do not understand the "LL" notation. + ** So we define our own static constants here using nothing + ** larger than a 32-bit integer constant. + */ + static const i64 maxInt = (((i64)0x7fffffff)<<32)|0xffffffff; + static const i64 minInt = ((i64)0x80000000)<<32; + + if( r<(double)minInt ){ + return minInt; + }else if( r>(double)maxInt ){ + return minInt; + }else{ + return (i64)r; + } +} + +/* ** Return some kind of integer value which is the best we can do ** at representing the value that *pMem describes as an integer. ** If pMem is an integer, then the value is exact. If pMem is @@ -33669,7 +36405,7 @@ SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){ if( flags & MEM_Int ){ return pMem->u.i; }else if( flags & MEM_Real ){ - return (i64)pMem->r; + return doubleToInt64(pMem->r); }else if( flags & (MEM_Str|MEM_Blob) ){ i64 value; pMem->flags |= MEM_Str; @@ -33719,20 +36455,24 @@ SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){ SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){ assert( pMem->flags & MEM_Real ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - pMem->u.i = pMem->r; - if( ((double)pMem->u.i)==pMem->r ){ + + pMem->u.i = doubleToInt64(pMem->r); + if( pMem->r==(double)pMem->u.i ){ pMem->flags |= MEM_Int; } } +static void setTypeFlag(Mem *pMem, int f){ + MemSetTypeFlag(pMem, f); +} + /* ** Convert pMem to type integer. Invalidate any prior representations. */ SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){ assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); pMem->u.i = sqlite3VdbeIntValue(pMem); - sqlite3VdbeMemRelease(pMem); - pMem->flags = MEM_Int; + setTypeFlag(pMem, MEM_Int); return SQLITE_OK; } @@ -33743,8 +36483,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){ SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){ assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); pMem->r = sqlite3VdbeRealValue(pMem); - sqlite3VdbeMemRelease(pMem); - pMem->flags = MEM_Real; + setTypeFlag(pMem, MEM_Real); return SQLITE_OK; } @@ -33759,14 +36498,13 @@ SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){ assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); r1 = sqlite3VdbeRealValue(pMem); - i = (i64)r1; + i = doubleToInt64(r1); r2 = (double)i; if( r1==r2 ){ sqlite3VdbeMemIntegerify(pMem); }else{ pMem->r = r1; - pMem->flags = MEM_Real; - sqlite3VdbeMemRelease(pMem); + setTypeFlag(pMem, MEM_Real); } return SQLITE_OK; } @@ -33775,10 +36513,8 @@ SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){ ** Delete any previous value and set the value stored in *pMem to NULL. */ SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){ - sqlite3VdbeMemRelease(pMem); - pMem->flags = MEM_Null; + setTypeFlag(pMem, MEM_Null); pMem->type = SQLITE_NULL; - pMem->n = 0; } /* @@ -33787,12 +36523,12 @@ SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){ */ SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ sqlite3VdbeMemRelease(pMem); - pMem->flags = MEM_Blob|MEM_Zero|MEM_Short; + setTypeFlag(pMem, MEM_Blob); + pMem->flags = MEM_Blob|MEM_Zero; pMem->type = SQLITE_BLOB; pMem->n = 0; if( n<0 ) n = 0; pMem->u.i = n; - pMem->z = pMem->zShort; pMem->enc = SQLITE_UTF8; } @@ -33839,15 +36575,16 @@ SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){ /* ** Make an shallow copy of pFrom into pTo. Prior contents of -** pTo are overwritten. The pFrom->z field is not duplicated. If +** pTo are freed. The pFrom->z field is not duplicated. If ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z ** and flags gets srcType (either MEM_Ephem or MEM_Static). */ SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ - memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort)); + sqlite3VdbeMemRelease(pTo); + memcpy(pTo, pFrom, sizeof(*pFrom)); pTo->xDel = 0; - if( pTo->flags & (MEM_Str|MEM_Blob) ){ - pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem); + if( pTo->flags&MEM_Dyn ){ + pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); assert( srcType==MEM_Ephem || srcType==MEM_Static ); pTo->flags |= srcType; } @@ -33858,15 +36595,48 @@ SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int sr ** freed before the copy is made. */ SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ - int rc; - if( pTo->flags & MEM_Dyn ){ - sqlite3VdbeMemRelease(pTo); + int rc = SQLITE_OK; + char *zBuf = 0; + + /* If cell pTo currently has a reusable buffer, save a pointer to it + ** in local variable zBuf. This function attempts to avoid freeing + ** this buffer. + */ + if( pTo->flags&MEM_Dyn ){ + if( pTo->xDel ){ + sqlite3VdbeMemRelease(pTo); + }else{ + zBuf = pTo->z; + } } - sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem); - if( pTo->flags & MEM_Ephem ){ - rc = sqlite3VdbeMemMakeWriteable(pTo); + + /* Copy the contents of *pFrom to *pTo */ + memcpy(pTo, pFrom, sizeof(*pFrom)); + + if( pTo->flags&(MEM_Str|MEM_Blob) && pTo->flags&MEM_Static ){ + /* pFrom contained a pointer to a static string. In this case, + ** free any dynamically allocated buffer associated with pTo. + */ + sqlite3_free(zBuf); }else{ - rc = SQLITE_OK; + char *zData = pTo->z; + + pTo->z = zBuf; + pTo->flags &= ~(MEM_Static|MEM_Ephem); + pTo->flags |= MEM_Dyn; + pTo->xDel = 0; + + if( pTo->flags&(MEM_Str|MEM_Blob) ){ + if( sqlite3VdbeMemGrow(pTo, pTo->n+2, 0) ){ + pTo->n = 0; + rc = SQLITE_NOMEM; + }else{ + memcpy(pTo->z, zData, pTo->n); + pTo->z[pTo->n] = '\0'; + pTo->z[pTo->n+1] = '\0'; + pTo->flags |= MEM_Term; + } + } } return rc; } @@ -33875,12 +36645,9 @@ SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ ** Transfer the contents of pFrom to pTo. Any existing value in pTo is ** freed. If pFrom contains ephemeral data, a copy is made. ** -** pFrom contains an SQL NULL when this routine returns. SQLITE_NOMEM -** might be returned if pFrom held ephemeral data and we were unable -** to allocate enough space to make a copy. +** pFrom contains an SQL NULL when this routine returns. */ -SQLITE_PRIVATE int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ - int rc; +SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); @@ -33888,21 +36655,18 @@ SQLITE_PRIVATE int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ sqlite3VdbeMemRelease(pTo); } memcpy(pTo, pFrom, sizeof(Mem)); - if( pFrom->flags & MEM_Short ){ - pTo->z = pTo->zShort; - } pFrom->flags = MEM_Null; pFrom->xDel = 0; - if( pTo->flags & MEM_Ephem ){ - rc = sqlite3VdbeMemMakeWriteable(pTo); - }else{ - rc = SQLITE_OK; - } - return rc; } /* ** Change the value of a Mem to be a string or a BLOB. +** +** The memory management strategy depends on the value of the xDel +** parameter. If the value passed is SQLITE_TRANSIENT, then the +** string is copied into a (possibly existing) buffer managed by the +** Mem structure. Otherwise, any existing buffer is freed and the +** pointer copied. */ SQLITE_PRIVATE int sqlite3VdbeMemSetStr( Mem *pMem, /* Memory cell to set to string value */ @@ -33911,59 +36675,60 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetStr( u8 enc, /* Encoding of z. 0 for BLOBs */ void (*xDel)(void*) /* Destructor function */ ){ + int nByte = n; /* New value for pMem->n */ + int flags = 0; /* New value for pMem->flags */ + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); - sqlite3VdbeMemRelease(pMem); + + /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ if( !z ){ - pMem->flags = MEM_Null; - pMem->type = SQLITE_NULL; + sqlite3VdbeMemSetNull(pMem); return SQLITE_OK; } - pMem->z = (char *)z; - if( xDel==SQLITE_STATIC ){ - pMem->flags = MEM_Static; - }else if( xDel==SQLITE_TRANSIENT ){ - pMem->flags = MEM_Ephem; + + flags = (enc==0?MEM_Blob:MEM_Str); + if( nByte<0 ){ + assert( enc!=0 ); + if( enc==SQLITE_UTF8 ){ + for(nByte=0; z[nByte]; nByte++){} + }else{ + for(nByte=0; z[nByte] | z[nByte+1]; nByte+=2){} + } + flags |= MEM_Term; + } + + /* The following block sets the new values of Mem.z and Mem.xDel. It + ** also sets a flag in local variable "flags" to indicate the memory + ** management (one of MEM_Dyn or MEM_Static). + */ + if( xDel==SQLITE_TRANSIENT ){ + int nAlloc = nByte; + if( flags&MEM_Term ){ + nAlloc += (enc==SQLITE_UTF8?1:2); + } + if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){ + return SQLITE_NOMEM; + } + memcpy(pMem->z, z, nAlloc); + flags |= MEM_Dyn; }else{ - pMem->flags = MEM_Dyn; + sqlite3VdbeMemRelease(pMem); + pMem->z = (char *)z; pMem->xDel = xDel; + flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); } - pMem->enc = enc; - pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT; - pMem->n = n; - - assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE - || enc==SQLITE_UTF16BE ); - switch( enc ){ - case 0: - pMem->flags |= MEM_Blob; - pMem->enc = SQLITE_UTF8; - break; - - case SQLITE_UTF8: - pMem->flags |= MEM_Str; - if( n<0 ){ - pMem->n = strlen(z); - pMem->flags |= MEM_Term; - } - break; + pMem->n = nByte; + pMem->flags = flags; + pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); + pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT); #ifndef SQLITE_OMIT_UTF16 - case SQLITE_UTF16LE: - case SQLITE_UTF16BE: - pMem->flags |= MEM_Str; - if( pMem->n<0 ){ - pMem->n = sqlite3Utf16ByteLen(pMem->z,-1); - pMem->flags |= MEM_Term; - } - if( sqlite3VdbeMemHandleBom(pMem) ){ - return SQLITE_NOMEM; - } -#endif /* SQLITE_OMIT_UTF16 */ - } - if( pMem->flags&MEM_Ephem ){ - return sqlite3VdbeMemMakeWriteable(pMem); + if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ + return SQLITE_NOMEM; } +#endif + return SQLITE_OK; } @@ -34111,6 +36876,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemFromBtree( char *zData; /* Data from the btree layer */ int available = 0; /* Number of bytes available on the local btree page */ sqlite3 *db; /* Database connection */ + int rc = SQLITE_OK; db = sqlite3BtreeCursorDb(pCur); assert( sqlite3_mutex_held(db->mutex) ); @@ -34121,52 +36887,31 @@ SQLITE_PRIVATE int sqlite3VdbeMemFromBtree( } assert( zData!=0 ); - pMem->db = db; - pMem->n = amt; - if( offset+amt<=available ){ + if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){ + sqlite3VdbeMemRelease(pMem); pMem->z = &zData[offset]; pMem->flags = MEM_Blob|MEM_Ephem; - }else{ - int rc; - if( amt>NBFS-2 ){ - zData = (char *)sqlite3DbMallocRaw(db, amt+2); - if( !zData ){ - return SQLITE_NOMEM; - } - pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; - pMem->xDel = 0; - }else{ - zData = &(pMem->zShort[0]); - pMem->flags = MEM_Blob|MEM_Short|MEM_Term; - } - pMem->z = zData; + }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ + pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; pMem->enc = 0; pMem->type = SQLITE_BLOB; - if( key ){ - rc = sqlite3BtreeKey(pCur, offset, amt, zData); + rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); }else{ - rc = sqlite3BtreeData(pCur, offset, amt, zData); + rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); } - zData[amt] = 0; - zData[amt+1] = 0; + pMem->z[amt] = 0; + pMem->z[amt+1] = 0; if( rc!=SQLITE_OK ){ - if( amt>NBFS-2 ){ - assert( zData!=pMem->zShort ); - assert( pMem->flags & MEM_Dyn ); - sqlite3_free(zData); - } else { - assert( zData==pMem->zShort ); - assert( pMem->flags & MEM_Short ); - } - return rc; + sqlite3VdbeMemRelease(pMem); } } + pMem->n = amt; - return SQLITE_OK; + return rc; } -#ifndef NDEBUG +#if 0 /* ** Perform various checks on the memory cell pMem. An assert() will ** fail if pMem is internally inconsistent. @@ -34239,7 +36984,7 @@ SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ expandBlob(pVal); if( pVal->flags&MEM_Str ){ sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); - if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){ + if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(sqlite3_intptr_t)pVal->z) ){ assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ return 0; @@ -34249,7 +36994,7 @@ SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ }else{ assert( (pVal->flags&MEM_Blob)==0 ); sqlite3VdbeMemStringify(pVal, enc); - assert( 0==(1&(int)pVal->z) ); + assert( 0==(1&(sqlite3_intptr_t)pVal->z) ); } assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 || pVal->db->mallocFailed ); @@ -34277,7 +37022,7 @@ SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){ ** Create a new sqlite3_value object, containing the value of pExpr. ** ** This only works for very simple expressions that consist of one constant -** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can +** token (i.e. "5", "5.1", "'a string'"). If the expression can ** be converted directly into a value, then the value is allocated and ** a pointer written to *ppVal. The caller is responsible for deallocating ** the value by passing it to sqlite3ValueFree() later on. If the expression @@ -34320,13 +37065,15 @@ SQLITE_PRIVATE int sqlite3ValueFromExpr( #ifndef SQLITE_OMIT_BLOB_LITERAL else if( op==TK_BLOB ){ int nVal; + assert( pExpr->token.n>=3 ); + assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); + assert( pExpr->token.z[1]=='\'' ); + assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); pVal = sqlite3ValueNew(db); - zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1); - if( !zVal || !pVal ) goto no_mem; - sqlite3Dequote(zVal); - nVal = strlen(zVal)/2; - sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal), nVal,0,sqlite3_free); - sqlite3_free(zVal); + nVal = pExpr->token.n - 3; + zVal = (char*)pExpr->token.z + 2; + sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, + 0, sqlite3_free); } #endif @@ -34359,7 +37106,7 @@ SQLITE_PRIVATE void sqlite3ValueSetStr( */ SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){ if( !v ) return; - sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC); + sqlite3VdbeMemRelease((Mem *)v); sqlite3_free(v); } @@ -34402,11 +37149,11 @@ SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ /* ** When debugging the code generator in a symbolic debugger, one can -** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed +** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed ** as they are added to the instruction stream. */ #ifdef SQLITE_DEBUG -SQLITE_API int sqlite3_vdbe_addop_trace = 0; +SQLITE_PRIVATE int sqlite3VdbeAddopTrace = 0; #endif @@ -34509,20 +37256,20 @@ static void resizeOpArray(Vdbe *p, int N){ ** ** op The opcode for this instruction ** -** p1, p2 First two of the three possible operands. +** p1, p2, p3 Operands ** ** Use the sqlite3VdbeResolveLabel() function to fix an address and -** the sqlite3VdbeChangeP3() function to change the value of the P3 +** the sqlite3VdbeChangeP4() function to change the value of the P4 ** operand. */ -SQLITE_PRIVATE int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ int i; VdbeOp *pOp; i = p->nOp; assert( p->magic==VDBE_MAGIC_INIT ); if( p->nOpAlloc<=i ){ - resizeOpArray(p, p->nOpAlloc*2 + 100); + resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); if( p->db->mallocFailed ){ return 0; } @@ -34532,21 +37279,40 @@ SQLITE_PRIVATE int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ pOp->opcode = op; pOp->p1 = p1; pOp->p2 = p2; - pOp->p3 = 0; - pOp->p3type = P3_NOTUSED; + pOp->p3 = p3; + pOp->p4.p = 0; + pOp->p4type = P4_NOTUSED; p->expired = 0; #ifdef SQLITE_DEBUG - if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); + if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); #endif return i; } +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){ + return sqlite3VdbeAddOp3(p, op, 0, 0, 0); +} +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ + return sqlite3VdbeAddOp3(p, op, p1, 0, 0); +} +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ + return sqlite3VdbeAddOp3(p, op, p1, p2, 0); +} + /* -** Add an opcode that includes the p3 value. +** Add an opcode that includes the p4 value as a pointer. */ -SQLITE_PRIVATE int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){ - int addr = sqlite3VdbeAddOp(p, op, p1, p2); - sqlite3VdbeChangeP3(p, addr, zP3, p3type); +SQLITE_PRIVATE int sqlite3VdbeAddOp4( + Vdbe *p, /* Add the opcode to this VM */ + int op, /* The new opcode */ + int p1, /* The P1 operand */ + int p2, /* The P2 operand */ + int p3, /* The P3 operand */ + const char *zP4, /* The P4 operand */ + int p4type /* P4 operand type */ +){ + int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); + sqlite3VdbeChangeP4(p, addr, zP4, p4type); return addr; } @@ -34594,51 +37360,9 @@ SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *p, int x){ } /* -** Return non-zero if opcode 'op' is guarenteed not to push more values -** onto the VDBE stack than it pops off. -*/ -static int opcodeNoPush(u8 op){ - /* The 10 NOPUSH_MASK_n constants are defined in the automatically - ** generated header file opcodes.h. Each is a 16-bit bitmask, one - ** bit corresponding to each opcode implemented by the virtual - ** machine in vdbe.c. The bit is true if the word "no-push" appears - ** in a comment on the same line as the "case OP_XXX:" in - ** sqlite3VdbeExec() in vdbe.c. - ** - ** If the bit is true, then the corresponding opcode is guarenteed not - ** to grow the stack when it is executed. Otherwise, it may grow the - ** stack by at most one entry. - ** - ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains - ** one bit for opcodes 16 to 31, and so on. - ** - ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h - ** because the file is generated by an awk program. Awk manipulates - ** all numbers as floating-point and we don't want to risk a rounding - ** error if someone builds with an awk that uses (for example) 32-bit - ** IEEE floats. - */ - static const u32 masks[5] = { - NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16), - NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16), - NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16), - NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16), - NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16) - }; - assert( op<32*5 ); - return (masks[op>>5] & (1<<(op&0x1F))); -} - -#ifndef NDEBUG -SQLITE_PRIVATE int sqlite3VdbeOpcodeNoPush(u8 op){ - return opcodeNoPush(op); -} -#endif - -/* -** Loop through the program looking for P2 values that are negative. -** Each such value is a label. Resolve the label by setting the P2 -** value to its correct non-zero value. +** Loop through the program looking for P2 values that are negative +** on jump instructions. Each such value is a label. Resolve the +** label by setting the P2 value to its correct non-zero value. ** ** This routine is called once after all opcodes have been inserted. ** @@ -34646,9 +37370,6 @@ SQLITE_PRIVATE int sqlite3VdbeOpcodeNoPush(u8 op){ ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. ** -** The integer *pMaxStack is set to the maximum number of vdbe stack -** entries that static analysis reveals this program might need. -** ** This routine also does the following optimization: It scans for ** instructions that might cause a statement rollback. Such instructions ** are: @@ -34662,10 +37383,9 @@ SQLITE_PRIVATE int sqlite3VdbeOpcodeNoPush(u8 op){ ** is changed to a Noop. In this way, we avoid creating the statement ** journal file unnecessarily. */ -static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ +static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ int i; int nMaxArgs = 0; - int nMaxStack = p->nOp; Op *pOp; int *aLabel = p->aLabel; int doesStatementRollback = 0; @@ -34673,7 +37393,9 @@ static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ u8 opcode = pOp->opcode; - if( opcode==OP_Function || opcode==OP_AggStep + if( opcode==OP_Function ){ + if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; + }else if( opcode==OP_AggStep #ifndef SQLITE_OMIT_VIRTUALTABLE || opcode==OP_VUpdate #endif @@ -34694,24 +37416,21 @@ static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ }else if( opcode==OP_VFilter ){ int n; assert( p->nOp - i >= 3 ); - assert( pOp[-2].opcode==OP_Integer ); - n = pOp[-2].p1; + assert( pOp[-1].opcode==OP_Integer ); + n = pOp[-1].p1; if( n>nMaxArgs ) nMaxArgs = n; #endif } - if( opcodeNoPush(opcode) ){ - nMaxStack--; - } - if( pOp->p2>=0 ) continue; - assert( -1-pOp->p2nLabel ); - pOp->p2 = aLabel[-1-pOp->p2]; + if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){ + assert( -1-pOp->p2nLabel ); + pOp->p2 = aLabel[-1-pOp->p2]; + } } sqlite3_free(p->aLabel); p->aLabel = 0; *pMaxFuncArgs = nMaxArgs; - *pMaxStack = nMaxStack; /* If we never rollback a statement transaction, then statement ** transactions are not needed. So change every OP_Statement @@ -34743,7 +37462,8 @@ SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp) int addr; assert( p->magic==VDBE_MAGIC_INIT ); if( p->nOp + nOp > p->nOpAlloc ){ - resizeOpArray(p, p->nOp*2 + nOp); + resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); + assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed ); } if( p->db->mallocFailed ){ return 0; @@ -34757,11 +37477,17 @@ SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp) VdbeOp *pOut = &p->aOp[i+addr]; pOut->opcode = pIn->opcode; pOut->p1 = pIn->p1; - pOut->p2 = p2<0 ? addr + ADDR(p2) : p2; + if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){ + pOut->p2 = addr + ADDR(p2); + }else{ + pOut->p2 = p2; + } pOut->p3 = pIn->p3; - pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED; + pOut->p4type = P4_NOTUSED; + pOut->p4.p = 0; + pOut->p5 = 0; #ifdef SQLITE_DEBUG - if( sqlite3_vdbe_addop_trace ){ + if( sqlite3VdbeAddopTrace ){ sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); } #endif @@ -34789,7 +37515,6 @@ SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ ** This routine is useful for setting a jump destination. */ SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ - assert( val>=0 ); assert( p==0 || p->magic==VDBE_MAGIC_INIT ); if( p && addr>=0 && p->nOp>addr && p->aOp ){ p->aOp[addr].p2 = val; @@ -34797,6 +37522,28 @@ SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ } /* +** Change the value of the P3 operand for a specific instruction. +*/ +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ + assert( p==0 || p->magic==VDBE_MAGIC_INIT ); + if( p && addr>=0 && p->nOp>addr && p->aOp ){ + p->aOp[addr].p3 = val; + } +} + +/* +** Change the value of the P5 operand for the most recently +** added operation. +*/ +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ + assert( p==0 || p->magic==VDBE_MAGIC_INIT ); + if( p && p->aOp ){ + assert( p->nOp>0 ); + p->aOp[p->nOp-1].p5 = val; + } +} + +/* ** Change the P2 operand of instruction addr so that it points to ** the address of the next instruction to be coded. */ @@ -34816,32 +37563,32 @@ static void freeEphemeralFunction(FuncDef *pDef){ } /* -** Delete a P3 value if necessary. +** Delete a P4 value if necessary. */ -static void freeP3(int p3type, void *p3){ +static void freeP4(int p4type, void *p3){ if( p3 ){ - switch( p3type ){ - case P3_REAL: - case P3_INT64: - case P3_MPRINTF: - case P3_DYNAMIC: - case P3_KEYINFO: - case P3_KEYINFO_HANDOFF: { + switch( p4type ){ + case P4_REAL: + case P4_INT64: + case P4_MPRINTF: + case P4_DYNAMIC: + case P4_KEYINFO: + case P4_KEYINFO_HANDOFF: { sqlite3_free(p3); break; } - case P3_VDBEFUNC: { + case P4_VDBEFUNC: { VdbeFunc *pVdbeFunc = (VdbeFunc *)p3; freeEphemeralFunction(pVdbeFunc->pFunc); sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); sqlite3_free(pVdbeFunc); break; } - case P3_FUNCDEF: { + case P4_FUNCDEF: { freeEphemeralFunction((FuncDef*)p3); break; } - case P3_MEM: { + case P4_MEM: { sqlite3ValueFree((sqlite3_value*)p3); break; } @@ -34857,7 +37604,7 @@ SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ if( p && p->aOp ){ VdbeOp *pOp = &p->aOp[addr]; while( N-- ){ - freeP3(pOp->p3type, pOp->p3); + freeP4(pOp->p4type, pOp->p4.p); memset(pOp, 0, sizeof(pOp[0])); pOp->opcode = OP_Noop; pOp++; @@ -34866,95 +37613,111 @@ SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ } /* -** Change the value of the P3 operand for a specific instruction. +** Change the value of the P4 operand for a specific instruction. ** This routine is useful when a large program is loaded from a ** static array using sqlite3VdbeAddOpList but we want to make a ** few minor changes to the program. ** -** If n>=0 then the P3 operand is dynamic, meaning that a copy of +** If n>=0 then the P4 operand is dynamic, meaning that a copy of ** the string is made into memory obtained from sqlite3_malloc(). -** A value of n==0 means copy bytes of zP3 up to and including the -** first null byte. If n>0 then copy n+1 bytes of zP3. +** A value of n==0 means copy bytes of zP4 up to and including the +** first null byte. If n>0 then copy n+1 bytes of zP4. ** -** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure. +** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. ** A copy is made of the KeyInfo structure into memory obtained from ** sqlite3_malloc, to be freed when the Vdbe is finalized. -** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure +** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure ** stored in memory that the caller has obtained from sqlite3_malloc. The ** caller should not free the allocation, it will be freed when the Vdbe is ** finalized. ** -** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points +** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points ** to a string or structure that is guaranteed to exist for the lifetime of ** the Vdbe. In these cases we can just copy the pointer. ** -** If addr<0 then change P3 on the most recently inserted instruction. +** If addr<0 then change P4 on the most recently inserted instruction. */ -SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ Op *pOp; - assert( p==0 || p->magic==VDBE_MAGIC_INIT ); - if( p==0 || p->aOp==0 || p->db->mallocFailed ){ - if (n != P3_KEYINFO) { - freeP3(n, (void*)*(char**)&zP3); + assert( p!=0 ); + assert( p->magic==VDBE_MAGIC_INIT ); + if( p->aOp==0 || p->db->mallocFailed ){ + if (n != P4_KEYINFO) { + freeP4(n, (void*)*(char**)&zP4); } return; } - if( addr<0 || addr>=p->nOp ){ + assert( addrnOp ); + if( addr<0 ){ addr = p->nOp - 1; if( addr<0 ) return; } pOp = &p->aOp[addr]; - freeP3(pOp->p3type, pOp->p3); - pOp->p3 = 0; - if( zP3==0 ){ - pOp->p3 = 0; - pOp->p3type = P3_NOTUSED; - }else if( n==P3_KEYINFO ){ + freeP4(pOp->p4type, pOp->p4.p); + pOp->p4.p = 0; + if( n==P4_INT32 ){ + /* Note: this cast is safe, because the origin data point was an int + ** that was cast to a (const char *). */ + pOp->p4.i = (int)(sqlite3_intptr_t)zP4; + pOp->p4type = n; + }else if( zP4==0 ){ + pOp->p4.p = 0; + pOp->p4type = P4_NOTUSED; + }else if( n==P4_KEYINFO ){ KeyInfo *pKeyInfo; int nField, nByte; - nField = ((KeyInfo*)zP3)->nField; + nField = ((KeyInfo*)zP4)->nField; nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; pKeyInfo = sqlite3_malloc( nByte ); - pOp->p3 = (char*)pKeyInfo; + pOp->p4.pKeyInfo = pKeyInfo; if( pKeyInfo ){ - unsigned char *aSortOrder; - memcpy(pKeyInfo, zP3, nByte); + memcpy(pKeyInfo, zP4, nByte); + /* In the current implementation, P4_KEYINFO is only ever used on + ** KeyInfo structures that have no aSortOrder component. Elements + ** with an aSortOrder always use P4_KEYINFO_HANDOFF. So we do not + ** need to bother with duplicating the aSortOrder. */ + assert( pKeyInfo->aSortOrder==0 ); +#if 0 aSortOrder = pKeyInfo->aSortOrder; if( aSortOrder ){ pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); } - pOp->p3type = P3_KEYINFO; +#endif + pOp->p4type = P4_KEYINFO; }else{ p->db->mallocFailed = 1; - pOp->p3type = P3_NOTUSED; + pOp->p4type = P4_NOTUSED; } - }else if( n==P3_KEYINFO_HANDOFF ){ - pOp->p3 = (char*)zP3; - pOp->p3type = P3_KEYINFO; + }else if( n==P4_KEYINFO_HANDOFF ){ + pOp->p4.p = (void*)zP4; + pOp->p4type = P4_KEYINFO; }else if( n<0 ){ - pOp->p3 = (char*)zP3; - pOp->p3type = n; + pOp->p4.p = (void*)zP4; + pOp->p4type = n; }else{ - if( n==0 ) n = strlen(zP3); - pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n); - pOp->p3type = P3_DYNAMIC; + if( n==0 ) n = strlen(zP4); + pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); + pOp->p4type = P4_DYNAMIC; } } #ifndef NDEBUG /* -** Replace the P3 field of the most recently coded instruction with -** comment text. +** Change the comment on the the most recently coded instruction. */ SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ va_list ap; assert( p->nOp>0 || p->aOp==0 ); - assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed ); - va_start(ap, zFormat); - sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC); - va_end(ap); + assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); + if( p->nOp ){ + char **pz = &p->aOp[p->nOp-1].zComment; + va_start(ap, zFormat); + sqlite3_free(*pz); + *pz = sqlite3VMPrintf(p->db, zFormat, ap); + va_end(ap); + } } #endif @@ -34970,16 +37733,16 @@ SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) /* -** Compute a string that describes the P3 parameter for an opcode. +** Compute a string that describes the P4 parameter for an opcode. ** Use zTemp for any required temporary buffer space. */ -static char *displayP3(Op *pOp, char *zTemp, int nTemp){ - char *zP3; +static char *displayP4(Op *pOp, char *zTemp, int nTemp){ + char *zP4 = zTemp; assert( nTemp>=20 ); - switch( pOp->p3type ){ - case P3_KEYINFO: { + switch( pOp->p4type ){ + case P4_KEYINFO: { int i, j; - KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3; + KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); i = strlen(zTemp); for(j=0; jnField; j++){ @@ -35004,48 +37767,59 @@ static char *displayP3(Op *pOp, char *zTemp, int nTemp){ zTemp[i++] = ')'; zTemp[i] = 0; assert( ip3; + case P4_COLLSEQ: { + CollSeq *pColl = pOp->p4.pColl; sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); - zP3 = zTemp; break; } - case P3_FUNCDEF: { - FuncDef *pDef = (FuncDef*)pOp->p3; + case P4_FUNCDEF: { + FuncDef *pDef = pOp->p4.pFunc; sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); - zP3 = zTemp; break; } - case P3_INT64: { - sqlite3_snprintf(nTemp, zTemp, "%lld", *(sqlite3_int64*)pOp->p3); - zP3 = zTemp; + case P4_INT64: { + sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); + break; + } + case P4_INT32: { + sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); break; } - case P3_REAL: { - sqlite3_snprintf(nTemp, zTemp, "%.16g", *(double*)pOp->p3); - zP3 = zTemp; + case P4_REAL: { + sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); + break; + } + case P4_MEM: { + Mem *pMem = pOp->p4.pMem; + assert( (pMem->flags & MEM_Null)==0 ); + if( pMem->flags & MEM_Str ){ + zP4 = pMem->z; + }else if( pMem->flags & MEM_Int ){ + sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); + }else if( pMem->flags & MEM_Real ){ + sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); + } break; } #ifndef SQLITE_OMIT_VIRTUALTABLE - case P3_VTAB: { - sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3; + case P4_VTAB: { + sqlite3_vtab *pVtab = pOp->p4.pVtab; sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); - zP3 = zTemp; break; } #endif default: { - zP3 = pOp->p3; - if( zP3==0 || pOp->opcode==OP_Noop ){ - zP3 = ""; + zP4 = pOp->p4.z; + if( zP4==0 ){ + zP4 = zTemp; + zTemp[0] = 0; } } } - assert( zP3!=0 ); - return zP3; + assert( zP4!=0 ); + return zP4; } #endif @@ -35070,13 +37844,19 @@ SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){ ** Print a single opcode. This routine is used for debugging only. */ SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ - char *zP3; + char *zP4; char zPtr[50]; - static const char *zFormat1 = "%4d %-13s %4d %4d %s\n"; + static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; if( pOut==0 ) pOut = stdout; - zP3 = displayP3(pOp, zPtr, sizeof(zPtr)); - fprintf(pOut, zFormat1, - pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3); + zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); + fprintf(pOut, zFormat1, pc, + sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, +#ifdef SQLITE_DEBUG + pOp->zComment ? pOp->zComment : "" +#else + "" +#endif + ); fflush(pOut); } #endif @@ -35085,11 +37865,15 @@ SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ ** Release an array of N Mem elements */ static void releaseMemArray(Mem *p, int N){ - if( p ){ + if( p && N ){ + sqlite3 *db = p->db; + int malloc_failed = db->mallocFailed; while( N-->0 ){ assert( N<2 || p[0].db==p[1].db ); - sqlite3VdbeMemRelease(p++); + sqlite3VdbeMemRelease(p); + p++->flags = MEM_Null; } + db->mallocFailed = malloc_failed; } } @@ -35100,6 +37884,11 @@ static void releaseMemArray(Mem *p, int N){ ** The interface is the same as sqlite3VdbeExec(). But instead of ** running the code, it invokes the callback once for each instruction. ** This feature is used to implement "EXPLAIN". +** +** When p->explain==1, each instruction is listed. When +** p->explain==2, only OP_Explain instructions are listed and these +** are shown in a different format. p->explain==2 is used to implement +** EXPLAIN QUERY PLAN. */ SQLITE_PRIVATE int sqlite3VdbeList( Vdbe *p /* The VDBE */ @@ -35107,20 +37896,18 @@ SQLITE_PRIVATE int sqlite3VdbeList( sqlite3 *db = p->db; int i; int rc = SQLITE_OK; + Mem *pMem = p->pResultSet = &p->aMem[1]; assert( p->explain ); if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; assert( db->magic==SQLITE_MAGIC_BUSY ); assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); - /* Even though this opcode does not put dynamic strings onto the - ** the stack, they may become dynamic if the user calls + /* Even though this opcode does not use dynamic strings for + ** the result, result columns may become dynamic if the user calls ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. */ - if( p->pTos==&p->aStack[4] ){ - releaseMemArray(p->aStack, 5); - } - p->resOnStack = 0; + releaseMemArray(pMem, p->nMem); do{ i = p->pc++; @@ -35133,20 +37920,22 @@ SQLITE_PRIVATE int sqlite3VdbeList( rc = SQLITE_ERROR; sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0); }else{ + char *z; Op *pOp = &p->aOp[i]; - Mem *pMem = p->aStack; - pMem->flags = MEM_Int; - pMem->type = SQLITE_INTEGER; - pMem->u.i = i; /* Program counter */ - pMem++; - - pMem->flags = MEM_Static|MEM_Str|MEM_Term; - pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ - assert( pMem->z!=0 ); - pMem->n = strlen(pMem->z); - pMem->type = SQLITE_TEXT; - pMem->enc = SQLITE_UTF8; - pMem++; + if( p->explain==1 ){ + pMem->flags = MEM_Int; + pMem->type = SQLITE_INTEGER; + pMem->u.i = i; /* Program counter */ + pMem++; + + pMem->flags = MEM_Static|MEM_Str|MEM_Term; + pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ + assert( pMem->z!=0 ); + pMem->n = strlen(pMem->z); + pMem->type = SQLITE_TEXT; + pMem->enc = SQLITE_UTF8; + pMem++; + } pMem->flags = MEM_Int; pMem->u.i = pOp->p1; /* P1 */ @@ -35158,17 +37947,57 @@ SQLITE_PRIVATE int sqlite3VdbeList( pMem->type = SQLITE_INTEGER; pMem++; - pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */ - pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort)); - assert( pMem->z!=0 ); - pMem->n = strlen(pMem->z); + if( p->explain==1 ){ + pMem->flags = MEM_Int; + pMem->u.i = pOp->p3; /* P3 */ + pMem->type = SQLITE_INTEGER; + pMem++; + } + + if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ + p->db->mallocFailed = 1; + return SQLITE_NOMEM; + } + pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; + z = displayP4(pOp, pMem->z, 32); + if( z!=pMem->z ){ + sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); + }else{ + assert( pMem->z!=0 ); + pMem->n = strlen(pMem->z); + pMem->enc = SQLITE_UTF8; + } pMem->type = SQLITE_TEXT; - pMem->enc = SQLITE_UTF8; + pMem++; - p->nResColumn = 5 - 2*(p->explain-1); - p->pTos = pMem; + if( p->explain==1 ){ + if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ + p->db->mallocFailed = 1; + return SQLITE_NOMEM; + } + pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; + pMem->n = 2; + sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ + pMem->type = SQLITE_TEXT; + pMem->enc = SQLITE_UTF8; + pMem++; + +#ifdef SQLITE_DEBUG + if( pOp->zComment ){ + pMem->flags = MEM_Str|MEM_Term; + pMem->z = pOp->zComment; + pMem->n = strlen(pMem->z); + pMem->enc = SQLITE_UTF8; + }else +#endif + { + pMem->flags = MEM_Null; /* Comment */ + pMem->type = SQLITE_NULL; + } + } + + p->nResColumn = 8 - 5*(p->explain-1); p->rc = SQLITE_OK; - p->resOnStack = 1; rc = SQLITE_ROW; } return rc; @@ -35183,9 +38012,9 @@ SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){ int nOp = p->nOp; VdbeOp *pOp; if( nOp<1 ) return; - pOp = &p->aOp[nOp-1]; - if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ - const char *z = pOp->p3; + pOp = &p->aOp[0]; + if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ + const char *z = pOp->p4.z; while( isspace(*(u8*)z) ) z++; printf("SQL: [%s]\n", z); } @@ -35199,13 +38028,13 @@ SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){ SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){ int nOp = p->nOp; VdbeOp *pOp; - if( sqlite3_io_trace==0 ) return; + if( sqlite3IoTrace==0 ) return; if( nOp<1 ) return; - pOp = &p->aOp[nOp-1]; - if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ + pOp = &p->aOp[0]; + if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ int i, j; char z[1000]; - sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3); + sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); for(i=0; isspace((unsigned char)z[i]); i++){} for(j=0; z[i]; i++){ if( isspace((unsigned char)z[i]) ){ @@ -35217,7 +38046,7 @@ SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){ } } z[j] = 0; - sqlite3_io_trace("SQL %s\n", z); + sqlite3IoTrace("SQL %s\n", z); } } #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ @@ -35256,37 +38085,28 @@ SQLITE_PRIVATE void sqlite3VdbeMakeReady( */ p->magic = VDBE_MAGIC_RUN; - /* No instruction ever pushes more than a single element onto the - ** stack. And the stack never grows on successive executions of the - ** same loop. So the total number of instructions is an upper bound - ** on the maximum stack depth required. (Added later:) The - ** resolveP2Values() call computes a tighter upper bound on the - ** stack size. - ** - ** Allocation all the stack space we will ever need. + /* + ** Allocation space for registers. */ - if( p->aStack==0 ){ + if( p->aMem==0 ){ int nArg; /* Maximum number of args passed to a user function. */ - int nStack; /* Maximum number of stack entries required */ - resolveP2Values(p, &nArg, &nStack); + resolveP2Values(p, &nArg); resizeOpArray(p, p->nOp); assert( nVar>=0 ); - assert( nStacknOp ); - if( isExplain ){ - nStack = 10; + if( isExplain && nMem<10 ){ + p->nMem = nMem = 10; } - p->aStack = sqlite3DbMallocZero(db, - nStack*sizeof(p->aStack[0]) /* aStack */ - + nArg*sizeof(Mem*) /* apArg */ + p->aMem = sqlite3DbMallocZero(db, + nMem*sizeof(Mem) /* aMem */ + nVar*sizeof(Mem) /* aVar */ + + nArg*sizeof(Mem*) /* apArg */ + nVar*sizeof(char*) /* azVar */ - + nMem*sizeof(Mem) /* aMem */ - + nCursor*sizeof(Cursor*) /* apCsr */ + + nCursor*sizeof(Cursor*) + 1 /* apCsr */ ); if( !db->mallocFailed ){ - p->aMem = &p->aStack[nStack]; - p->nMem = nMem; - p->aVar = &p->aMem[nMem]; + p->aMem--; /* aMem[] goes from 1..nMem */ + p->nMem = nMem; /* not from 0..nMem-1 */ + p->aVar = &p->aMem[nMem+1]; p->nVar = nVar; p->okVar = 0; p->apArg = (Mem**)&p->aVar[nVar]; @@ -35297,23 +38117,24 @@ SQLITE_PRIVATE void sqlite3VdbeMakeReady( p->aVar[n].flags = MEM_Null; p->aVar[n].db = db; } - for(n=0; naStack[n].db = db; + for(n=1; n<=nMem; n++){ + p->aMem[n].flags = MEM_Null; + p->aMem[n].db = db; } } } - for(n=0; nnMem; n++){ - p->aMem[n].flags = MEM_Null; - p->aMem[n].db = db; +#ifdef SQLITE_DEBUG + for(n=1; nnMem; n++){ + assert( p->aMem[n].db==db ); + assert( p->aMem[n].flags==MEM_Null ); } +#endif - p->pTos = &p->aStack[-1]; p->pc = -1; p->rc = SQLITE_OK; p->uniqueCnt = 0; p->returnDepth = 0; p->errorAction = OE_Abort; - p->popStack = 0; p->explain |= isExplain; p->magic = VDBE_MAGIC_RUN; p->nChange = 0; @@ -35350,9 +38171,9 @@ SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; const sqlite3_module *pModule = pCx->pModule; p->inVtabMethod = 1; - sqlite3SafetyOff(p->db); + (void)sqlite3SafetyOff(p->db); pModule->xClose(pVtabCursor); - sqlite3SafetyOn(p->db); + (void)sqlite3SafetyOn(p->db); p->inVtabMethod = 0; } #endif @@ -35386,12 +38207,11 @@ static void closeAllCursorsExceptActiveVtabs(Vdbe *p){ */ static void Cleanup(Vdbe *p){ int i; - if( p->aStack ){ - releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack)); - p->pTos = &p->aStack[-1]; - } closeAllCursorsExceptActiveVtabs(p); - releaseMemArray(p->aMem, p->nMem); + for(i=1; i<=p->nMem; i++){ + MemSetTypeFlag(&p->aMem[i], MEM_Null); + } + releaseMemArray(&p->aMem[1], p->nMem); sqlite3VdbeFifoClear(&p->sFifo); if( p->contextStack ){ for(i=0; icontextStackTop; i++){ @@ -35404,7 +38224,7 @@ static void Cleanup(Vdbe *p){ p->contextStackTop = 0; sqlite3_free(p->zErrMsg); p->zErrMsg = 0; - p->resOnStack = 0; + p->pResultSet = 0; } /* @@ -35436,8 +38256,8 @@ SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ ** ** This call must be made after a call to sqlite3VdbeSetNumCols(). ** -** If N==P3_STATIC it means that zName is a pointer to a constant static -** string and we can just copy the pointer. If it is P3_DYNAMIC, then +** If N==P4_STATIC it means that zName is a pointer to a constant static +** string and we can just copy the pointer. If it is P4_DYNAMIC, then ** the string is freed using sqlite3_free() when the vdbe is finished with ** it. Otherwise, N bytes of zName are copied. */ @@ -35449,12 +38269,12 @@ SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char * if( p->db->mallocFailed ) return SQLITE_NOMEM; assert( p->aColName!=0 ); pColName = &(p->aColName[idx+var*p->nResColumn]); - if( N==P3_DYNAMIC || N==P3_STATIC ){ + if( N==P4_DYNAMIC || N==P4_STATIC ){ rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC); }else{ rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT); } - if( rc==SQLITE_OK && N==P3_DYNAMIC ){ + if( rc==SQLITE_OK && N==P4_DYNAMIC ){ pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn; pColName->xDel = 0; } @@ -35500,9 +38320,9 @@ static int vdbeCommit(sqlite3 *db){ /* If there are any write-transactions at all, invoke the commit hook */ if( needXcommit && db->xCommitCallback ){ - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); rc = db->xCommitCallback(db->pCommitArg); - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); if( rc ){ return SQLITE_CONSTRAINT; } @@ -35950,9 +38770,9 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){ ** error, then it might not have been halted properly. So halt ** it now. */ - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); sqlite3VdbeHalt(p); - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); /* If the VDBE has be run even partially, then transfer the error code ** and error message from the VDBE into the main database structure. But @@ -35985,7 +38805,6 @@ SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){ /* Save profiling information from this VDBE run. */ - assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack ); #ifdef VDBE_PROFILE { FILE *out = fopen("vdbe_profile.out", "a"); @@ -36065,15 +38884,20 @@ SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){ p->pNext->pPrev = p->pPrev; } if( p->aOp ){ - for(i=0; inOp; i++){ - Op *pOp = &p->aOp[i]; - freeP3(pOp->p3type, pOp->p3); + Op *pOp = p->aOp; + for(i=0; inOp; i++, pOp++){ + freeP4(pOp->p4type, pOp->p4.p); +#ifdef SQLITE_DEBUG + sqlite3_free(pOp->zComment); +#endif } sqlite3_free(p->aOp); } releaseMemArray(p->aVar, p->nVar); sqlite3_free(p->aLabel); - sqlite3_free(p->aStack); + if( p->aMem ){ + sqlite3_free(&p->aMem[1]); + } releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); sqlite3_free(p->aColName); sqlite3_free(p->zSql); @@ -36439,6 +39263,9 @@ SQLITE_PRIVATE int sqlite3VdbeSerialGet( ** or positive integer if {nKey1, pKey1} is less than, equal to or ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings ** composed by the OP_MakeRecord opcode of the VDBE. +** +** Key1 and Key2 do not have to contain the same number of fields. +** But if the lengths differ, Key2 must be the shorter of the two. */ SQLITE_PRIVATE int sqlite3VdbeRecordCompare( void *userData, @@ -36459,8 +39286,10 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompare( Mem mem2; mem1.enc = pKeyInfo->enc; mem1.db = pKeyInfo->db; + mem1.flags = 0; mem2.enc = pKeyInfo->enc; mem2.db = pKeyInfo->db; + mem2.flags = 0; idx1 = GetVarint(aKey1, szHdr1); d1 = szHdr1; @@ -36485,8 +39314,8 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompare( /* Do the comparison */ rc = sqlite3MemCompare(&mem1, &mem2, iaColl[i] : 0); - if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1); - if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2); + if( mem1.flags&MEM_Dyn ) sqlite3VdbeMemRelease(&mem1); + if( mem2.flags&MEM_Dyn ) sqlite3VdbeMemRelease(&mem2); if( rc!=0 ){ break; } @@ -36504,7 +39333,7 @@ SQLITE_PRIVATE int sqlite3VdbeRecordCompare( if( d1aSortOrder && inField @@ -36548,6 +39377,8 @@ SQLITE_PRIVATE int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ if( nCellKey<=0 ){ return SQLITE_CORRUPT_BKPT; } + m.flags = 0; + m.db = 0; rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); if( rc ){ return rc; @@ -36587,6 +39418,8 @@ SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare( *res = 0; return SQLITE_OK; } + m.db = 0; + m.flags = 0; rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m); if( rc ){ return rc; @@ -36685,7 +39518,9 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){ rc = SQLITE_OK; }else{ Vdbe *v = (Vdbe*)pStmt; +#ifndef SQLITE_MUTEX_NOOP sqlite3_mutex *mutex = v->db->mutex; +#endif sqlite3_mutex_enter(mutex); rc = sqlite3VdbeFinalize(v); sqlite3_mutex_leave(mutex); @@ -36722,12 +39557,14 @@ SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){ SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ int i; int rc = SQLITE_OK; - Vdbe *v = (Vdbe*)pStmt; - sqlite3_mutex_enter(v->db->mutex); +#ifndef SQLITE_MUTEX_NOOP + sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; +#endif + sqlite3_mutex_enter(mutex); for(i=1; rc==SQLITE_OK && i<=sqlite3_bind_parameter_count(pStmt); i++){ rc = sqlite3_bind_null(pStmt, i); } - sqlite3_mutex_leave(v->db->mutex); + sqlite3_mutex_leave(mutex); return rc; } @@ -36800,13 +39637,13 @@ SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ } SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); - pCtx->isError = 1; + pCtx->isError = SQLITE_ERROR; sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); } #ifndef SQLITE_OMIT_UTF16 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); - pCtx->isError = 1; + pCtx->isError = SQLITE_ERROR; sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); } #endif @@ -36868,6 +39705,9 @@ SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); } +SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ + pCtx->isError = errCode; +} /* Force an SQLITE_TOOBIG error. */ SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){ @@ -36879,7 +39719,7 @@ SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){ SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){ assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); sqlite3VdbeMemSetNull(&pCtx->s); - pCtx->isError = 1; + pCtx->isError = SQLITE_NOMEM; pCtx->s.db->mallocFailed = 1; } @@ -36929,20 +39769,6 @@ static int sqlite3Step(Vdbe *p){ } #ifndef SQLITE_OMIT_TRACE - /* Invoke the trace callback if there is one - */ - if( db->xTrace && !db->init.busy ){ - assert( p->nOp>0 ); - assert( p->aOp[p->nOp-1].opcode==OP_Noop ); - assert( p->aOp[p->nOp-1].p3!=0 ); - assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); - sqlite3SafetyOff(db); - db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); - if( sqlite3SafetyOn(db) ){ - p->rc = SQLITE_MISUSE; - return SQLITE_MISUSE; - } - } if( db->xProfile && !db->init.busy ){ double rNow; sqlite3OsCurrentTime(db->pVfs, &rNow); @@ -36950,15 +39776,6 @@ static int sqlite3Step(Vdbe *p){ } #endif - /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned - ** on in debugging mode. - */ -#ifdef SQLITE_DEBUG - if( (db->flags & SQLITE_SqlTrace)!=0 ){ - sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); - } -#endif /* SQLITE_DEBUG */ - db->activeVdbeCnt++; p->pc = 0; } @@ -36978,17 +39795,14 @@ static int sqlite3Step(Vdbe *p){ #ifndef SQLITE_OMIT_TRACE /* Invoke the profile callback if there is one */ - if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ + if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->nOp>0 + && p->aOp[0].opcode==OP_Trace && p->aOp[0].p4.z!=0 ){ double rNow; u64 elapseTime; sqlite3OsCurrentTime(db->pVfs, &rNow); elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; - assert( p->nOp>0 ); - assert( p->aOp[p->nOp-1].opcode==OP_Noop ); - assert( p->aOp[p->nOp-1].p3!=0 ); - assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); - db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); + db->xProfile(db->pProfileArg, p->aOp[0].p4.z, elapseTime); } #endif @@ -37205,7 +40019,7 @@ SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){ */ SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){ Vdbe *pVm = (Vdbe *)pStmt; - if( pVm==0 || !pVm->resOnStack ) return 0; + if( pVm==0 || pVm->pResultSet==0 ) return 0; return pVm->nResColumn; } @@ -37222,10 +40036,10 @@ static Mem *columnMem(sqlite3_stmt *pStmt, int i){ Mem *pOut; pVm = (Vdbe *)pStmt; - if( pVm && pVm->resOnStack && inResColumn && i>=0 ){ + if( pVm && pVm->pResultSet!=0 && inResColumn && i>=0 ){ sqlite3_mutex_enter(pVm->db->mutex); vals = sqlite3_data_count(pStmt); - pOut = &pVm->pTos[(1-vals)+i]; + pOut = &pVm->pResultSet[i]; }else{ static const Mem nullMem = {{0}, 0.0, 0, "", 0, MEM_Null, SQLITE_NULL }; if( pVm->db ){ @@ -37604,6 +40418,7 @@ SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_valu if( rc==SQLITE_OK ){ rc = sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); } + rc = sqlite3ApiExit(p->db, rc); sqlite3_mutex_leave(p->db->mutex); return rc; } @@ -37642,7 +40457,7 @@ static void createVarMap(Vdbe *p){ for(j=0, pOp=p->aOp; jnOp; j++, pOp++){ if( pOp->opcode==OP_Variable ){ assert( pOp->p1>0 && pOp->p1<=p->nVar ); - p->azVar[pOp->p1-1] = pOp->p3; + p->azVar[pOp->p1-1] = pOp->p4.z; } } p->okVar = 1; @@ -37708,9 +40523,7 @@ SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt * } sqlite3_mutex_enter(pTo->db->mutex); for(i=0; rc==SQLITE_OK && inVar; i++){ - sqlite3MallocDisallow(); - rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); - sqlite3MallocAllow(); + sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); } sqlite3_mutex_leave(pTo->db->mutex); assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); @@ -37753,13 +40566,13 @@ SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ ** the VDBE to do the work of the SQL statement. VDBE programs are ** similar in form to assembly language. The program consists of ** a linear sequence of operations. Each operation has an opcode -** and 3 operands. Operands P1 and P2 are integers. Operand P3 -** is a null-terminated string. The P2 operand must be non-negative. -** Opcodes will typically ignore one or more operands. Many opcodes -** ignore all three operands. +** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4 +** is a null-terminated string. Operand P5 is an unsigned character. +** Few opcodes use all 5 operands. ** -** Computation results are stored on a stack. Each entry on the -** stack is either an integer, a null-terminated string, a floating point +** Computation results are stored on a set of registers numbered beginning +** with 1 and going up to Vdbe.nMem. Each register can store +** either an integer, a null-terminated string, a floating point ** number, or the SQL "NULL" value. An inplicit conversion from one ** type to the other occurs as necessary. ** @@ -37774,7 +40587,7 @@ SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.660 2007/12/13 21:54:11 drh Exp $ +** $Id: vdbe.c,v 1.711 2008/03/17 17:18:38 drh Exp $ */ /* @@ -37813,23 +40626,38 @@ SQLITE_API int sqlite3_sort_count = 0; /* ** The next global variable records the size of the largest MEM_Blob -** or MEM_Str that has appeared on the VDBE stack. The test procedures +** or MEM_Str that has been used by a VDBE opcode. The test procedures ** use this information to make sure that the zero-blob functionality ** is working correctly. This variable has no function other than to ** help verify the correct operation of the library. */ #ifdef SQLITE_TEST SQLITE_API int sqlite3_max_blobsize = 0; +static void updateMaxBlobsize(Mem *p){ + if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ + sqlite3_max_blobsize = p->n; + } +} #endif /* -** Release the memory associated with the given stack level. This +** Test a register to see if it exceeds the current maximum blob size. +** If it does, record the new maximum blob size. +*/ +#ifdef SQLITE_TEST +# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) +#else +# define UPDATE_MAX_BLOBSIZE(P) +#endif + +/* +** Release the memory associated with a register. This ** leaves the Mem.flags field in an inconsistent state. */ #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); } /* -** Convert the given stack entity into a string if it isn't one +** Convert the given register into a string if it isn't one ** already. Return non-zero if a malloc() fails. */ #define Stringify(P, enc) \ @@ -37856,12 +40684,12 @@ SQLITE_API int sqlite3_max_blobsize = 0; /* ** An ephemeral string value (signified by the MEM_Ephem flag) contains ** a pointer to a dynamically allocated string where some other entity -** is responsible for deallocating that string. Because the stack entry -** does not control the string, it might be deleted without the stack -** entry knowing it. +** is responsible for deallocating that string. Because the register +** does not control the string, it might be deleted without the register +** knowing it. ** ** This routine converts an ephemeral string into a dynamically allocated -** string that the stack entry itself controls. In other words, it +** string that the register itself controls. In other words, it ** converts an MEM_Ephem string into an MEM_Dyn string. */ #define Deephemeralize(P) \ @@ -37875,10 +40703,10 @@ SQLITE_API int sqlite3_max_blobsize = 0; #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) /* -** Argument pMem points at a memory cell that will be passed to a +** Argument pMem points at a regiser that will be passed to a ** user-defined function or returned to the user as the result of a query. ** The second argument, 'db_enc' is the text encoding used by the vdbe for -** stack variables. This routine sets the pMem->enc and pMem->type +** register variables. This routine sets the pMem->enc and pMem->type ** variables used by the sqlite3_value_*() routines. */ #define storeTypeInfo(A,B) _storeTypeInfo(A) @@ -37901,16 +40729,20 @@ static void _storeTypeInfo(Mem *pMem){ } /* -** Pop the stack N times. +** Properties of opcodes. The OPFLG_INITIALIZER macro is +** created by mkopcodeh.awk during compilation. Data is obtained +** from the comments following the "case OP_xxxx:" statements in +** this file. */ -static void popStack(Mem **ppTos, int N){ - Mem *pTos = *ppTos; - while( N>0 ){ - N--; - Release(pTos); - pTos--; - } - *ppTos = pTos; +static unsigned char opcodeProperty[] = OPFLG_INITIALIZER; + +/* +** Return true if an opcode has any of the OPFLG_xxx properties +** specified by mask. +*/ +SQLITE_PRIVATE int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){ + assert( opcode>0 && opcodez, &value) ){ - sqlite3VdbeMemRelease(pRec); pRec->u.i = value; - pRec->flags = MEM_Int; + MemSetTypeFlag(pRec, MEM_Int); }else{ sqlite3VdbeMemRealify(pRec); } @@ -38107,6 +40938,39 @@ SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ } #endif +#ifdef SQLITE_DEBUG +/* +** Print the value of a register for tracing purposes: +*/ +static void memTracePrint(FILE *out, Mem *p){ + if( p->flags & MEM_Null ){ + fprintf(out, " NULL"); + }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ + fprintf(out, " si:%lld", p->u.i); + }else if( p->flags & MEM_Int ){ + fprintf(out, " i:%lld", p->u.i); + }else if( p->flags & MEM_Real ){ + fprintf(out, " r:%g", p->r); + }else{ + char zBuf[200]; + sqlite3VdbeMemPrettyPrint(p, zBuf); + fprintf(out, " "); + fprintf(out, "%s", zBuf); + } +} +static void registerTrace(FILE *out, int iReg, Mem *p){ + fprintf(out, "REG[%d] = ", iReg); + memTracePrint(out, p); + fprintf(out, "\n"); +} +#endif + +#ifdef SQLITE_DEBUG +# define REGISTER_TRACE(R,M) if(p->trace&&R>0)registerTrace(p->trace,R,M) +#else +# define REGISTER_TRACE(R,M) +#endif + #ifdef VDBE_PROFILE /* @@ -38177,7 +41041,9 @@ SQLITE_PRIVATE int sqlite3VdbeExec( int rc = SQLITE_OK; /* Value to return */ sqlite3 *db = p->db; /* The database */ u8 encoding = ENC(db); /* The database encoding */ - Mem *pTos; /* Top entry in the operand stack */ + Mem *pIn1, *pIn2, *pIn3; /* Input operands */ + Mem *pOut; /* Output operand */ + u8 opProperty; #ifdef VDBE_PROFILE unsigned long long start; /* CPU clock count at start of opcode */ int origPc; /* Program counter at start of opcode */ @@ -38185,13 +41051,9 @@ SQLITE_PRIVATE int sqlite3VdbeExec( #ifndef SQLITE_OMIT_PROGRESS_CALLBACK int nProgressOps = 0; /* Opcodes executed since progress callback. */ #endif -#ifndef NDEBUG - Mem *pStackLimit; -#endif - if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; + assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ assert( db->magic==SQLITE_MAGIC_BUSY ); - pTos = p->pTos; sqlite3BtreeMutexArrayEnter(&p->aMutex); if( p->rc==SQLITE_NOMEM ){ /* This happens if a malloc() inside a call to sqlite3_column_text() or @@ -38201,17 +41063,13 @@ SQLITE_PRIVATE int sqlite3VdbeExec( assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); p->rc = SQLITE_OK; assert( p->explain==0 ); - if( p->popStack ){ - popStack(&pTos, p->popStack); - p->popStack = 0; - } - p->resOnStack = 0; + p->pResultSet = 0; db->busyHandler.nBusy = 0; CHECK_FOR_INTERRUPT; sqlite3VdbeIOTraceSql(p); #ifdef SQLITE_DEBUG - if( (p->db->flags & SQLITE_VdbeListing)!=0 - || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS) + if( p->pc==0 && ((p->db->flags & SQLITE_VdbeListing)!=0 + || sqlite3OsAccess(db->pVfs, "vdbe_explain", SQLITE_ACCESS_EXISTS)) ){ int i; printf("VDBE Program Listing:\n"); @@ -38226,7 +41084,6 @@ SQLITE_PRIVATE int sqlite3VdbeExec( #endif for(pc=p->pc; rc==SQLITE_OK; pc++){ assert( pc>=0 && pcnOp ); - assert( pTos<=&p->aStack[pc] ); if( db->mallocFailed ) goto no_mem; #ifdef VDBE_PROFILE origPc = pc; @@ -38278,7 +41135,7 @@ SQLITE_PRIVATE int sqlite3VdbeExec( if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; if( prc!=0 ){ rc = SQLITE_INTERRUPT; - goto vdbe_halt; + goto vdbe_error_halt; } nProgressOps = 0; } @@ -38286,22 +41143,63 @@ SQLITE_PRIVATE int sqlite3VdbeExec( } #endif -#ifndef NDEBUG - /* This is to check that the return value of static function - ** opcodeNoPush() (see vdbeaux.c) returns values that match the - ** implementation of the virtual machine in this file. If - ** opcodeNoPush() returns non-zero, then the stack is guarenteed - ** not to grow when the opcode is executed. If it returns zero, then - ** the stack may grow by at most 1. + /* Do common setup processing for any opcode that is marked + ** with the "out2-prerelease" tag. Such opcodes have a single + ** output which is specified by the P2 parameter. The P2 register + ** is initialized to a NULL. + */ + opProperty = opcodeProperty[pOp->opcode]; + if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=p->nMem ); + pOut = &p->aMem[pOp->p2]; + sqlite3VdbeMemRelease(pOut); + pOut->flags = MEM_Null; + }else + + /* Do common setup for opcodes marked with one of the following + ** combinations of properties. ** - ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not - ** available if NDEBUG is defined at build time. - */ - pStackLimit = pTos; - if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){ - pStackLimit++; + ** in1 + ** in1 in2 + ** in1 in2 out3 + ** in1 in3 + ** + ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate + ** registers for inputs. Variable pOut points to the output register. + */ + if( (opProperty & OPFLG_IN1)!=0 ){ + assert( pOp->p1>0 ); + assert( pOp->p1<=p->nMem ); + pIn1 = &p->aMem[pOp->p1]; + REGISTER_TRACE(pOp->p1, pIn1); + if( (opProperty & OPFLG_IN2)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=p->nMem ); + pIn2 = &p->aMem[pOp->p2]; + REGISTER_TRACE(pOp->p2, pIn2); + if( (opProperty & OPFLG_OUT3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=p->nMem ); + pOut = &p->aMem[pOp->p3]; + } + }else if( (opProperty & OPFLG_IN3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=p->nMem ); + pIn3 = &p->aMem[pOp->p3]; + REGISTER_TRACE(pOp->p3, pIn3); + } + }else if( (opProperty & OPFLG_IN2)!=0 ){ + assert( pOp->p2>0 ); + assert( pOp->p2<=p->nMem ); + pIn2 = &p->aMem[pOp->p2]; + REGISTER_TRACE(pOp->p2, pIn2); + }else if( (opProperty & OPFLG_IN3)!=0 ){ + assert( pOp->p3>0 ); + assert( pOp->p3<=p->nMem ); + pIn3 = &p->aMem[pOp->p3]; + REGISTER_TRACE(pOp->p3, pIn3); } -#endif switch( pOp->opcode ){ @@ -38323,10 +41221,10 @@ SQLITE_PRIVATE int sqlite3VdbeExec( ** case statement is followed by a comment of the form "/# same as ... #/" ** that comment is used to determine the particular value of the opcode. ** -** If a comment on the same line as the "case OP_" construction contains -** the word "no-push", then the opcode is guarenteed not to grow the -** vdbe stack when it is executed. See function opcode() in -** vdbeaux.c for details. +** Other keywords in the comment that follows each case are used to +** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. +** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See +** the mkopcodeh.awk script for additional information. ** ** Documentation about VDBE opcodes is generated by scanning this file ** for lines of that contain "Opcode:". That line and all subsequent @@ -38340,20 +41238,20 @@ SQLITE_PRIVATE int sqlite3VdbeExec( ** *****************************************************************************/ -/* Opcode: Goto * P2 * +/* Opcode: Goto * P2 * * * ** ** An unconditional jump to address P2. ** The next instruction executed will be ** the one at index P2 from the beginning of ** the program. */ -case OP_Goto: { /* no-push */ +case OP_Goto: { /* jump */ CHECK_FOR_INTERRUPT; pc = pOp->p2 - 1; break; } -/* Opcode: Gosub * P2 * +/* Opcode: Gosub * P2 * * * ** ** Push the current address plus 1 onto the return address stack ** and then jump to address P2. @@ -38363,27 +41261,27 @@ case OP_Goto: { /* no-push */ ** the return address stack will fill up and processing will abort ** with a fatal error. */ -case OP_Gosub: { /* no-push */ +case OP_Gosub: { /* jump */ assert( p->returnDepthreturnStack)/sizeof(p->returnStack[0]) ); p->returnStack[p->returnDepth++] = pc+1; pc = pOp->p2 - 1; break; } -/* Opcode: Return * * * +/* Opcode: Return * * * * * ** ** Jump immediately to the next instruction after the last unreturned ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then ** processing aborts with a fatal error. */ -case OP_Return: { /* no-push */ +case OP_Return: { assert( p->returnDepth>0 ); p->returnDepth--; pc = p->returnStack[p->returnDepth] - 1; break; } -/* Opcode: Halt P1 P2 P3 +/* Opcode: Halt P1 P2 * P4 * ** ** Exit immediately. All open cursors, Fifos, etc are closed ** automatically. @@ -38396,19 +41294,18 @@ case OP_Return: { /* no-push */ ** then back out all changes that have occurred during this execution of the ** VDBE, but do not rollback the transaction. ** -** If P3 is not null then it is an error message string. +** If P4 is not null then it is an error message string. ** ** There is an implied "Halt 0 0 0" instruction inserted at the very end of ** every program. So a jump past the last instruction of the program ** is the same as executing Halt. */ -case OP_Halt: { /* no-push */ - p->pTos = pTos; +case OP_Halt: { p->rc = pOp->p1; p->pc = pc; p->errorAction = pOp->p2; - if( pOp->p3 ){ - sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0); + if( pOp->p4.z ){ + sqlite3SetString(&p->zErrMsg, pOp->p4.z, (char*)0); } rc = sqlite3VdbeHalt(p); assert( rc==SQLITE_BUSY || rc==SQLITE_OK ); @@ -38420,186 +41317,127 @@ case OP_Halt: { /* no-push */ goto vdbe_return; } -/* Opcode: StackDepth P1 * * -** -** If P1 is less than zero, then store the current stack depth -** in P1. If P1 is zero or greater, verify that the current stack -** depth is equal to P1 and throw an exception if it is not. -** -** This opcode is used for internal consistency checking. -*/ -case OP_StackDepth: { /* no-push */ - int n = pTos - p->aStack + 1; - if( pOp->p1<0 ){ - pOp->p1 = n; - }else if( pOp->p1!=n ){ - p->pTos = pTos; - p->rc = rc = SQLITE_INTERNAL; - p->pc = pc; - p->errorAction = OE_Rollback; - sqlite3SetString(&p->zErrMsg, "internal error: VDBE stack leak", (char*)0); - goto vdbe_return; - } - break; -} - -/* Opcode: Integer P1 * * +/* Opcode: Integer P1 P2 * * * ** -** The 32-bit integer value P1 is pushed onto the stack. +** The 32-bit integer value P1 is written into register P2. */ -case OP_Integer: { - pTos++; - pTos->flags = MEM_Int; - pTos->u.i = pOp->p1; +case OP_Integer: { /* out2-prerelease */ + pOut->flags = MEM_Int; + pOut->u.i = pOp->p1; break; } -/* Opcode: Int64 * * P3 +/* Opcode: Int64 * P2 * P4 * ** -** P3 is a pointer to a 64-bit integer value. -** Push that value onto the stack. +** P4 is a pointer to a 64-bit integer value. +** Write that value into register P2. */ -case OP_Int64: { - pTos++; - assert( pOp->p3!=0 ); - pTos->flags = MEM_Int; - memcpy(&pTos->u.i, pOp->p3, 8); +case OP_Int64: { /* out2-prerelease */ + assert( pOp->p4.pI64!=0 ); + pOut->flags = MEM_Int; + pOut->u.i = *pOp->p4.pI64; break; } -/* Opcode: Real * * P3 +/* Opcode: Real * P2 * P4 * ** -** P3 is a pointer to a 64-bit floating point value. Push that value -** onto the stack. +** P4 is a pointer to a 64-bit floating point value. +** Write that value into register P2. */ -case OP_Real: { /* same as TK_FLOAT, */ - pTos++; - pTos->flags = MEM_Real; - memcpy(&pTos->r, pOp->p3, 8); +case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ + pOut->flags = MEM_Real; + pOut->r = *pOp->p4.pReal; break; } -/* Opcode: String8 * * P3 +/* Opcode: String8 * P2 * P4 * ** -** P3 points to a nul terminated UTF-8 string. This opcode is transformed +** P4 points to a nul terminated UTF-8 string. This opcode is transformed ** into an OP_String before it is executed for the first time. */ -case OP_String8: { /* same as TK_STRING */ - assert( pOp->p3!=0 ); +case OP_String8: { /* same as TK_STRING, out2-prerelease */ + assert( pOp->p4.z!=0 ); pOp->opcode = OP_String; - pOp->p1 = strlen(pOp->p3); - assert( SQLITE_MAX_SQL_LENGTH <= SQLITE_MAX_LENGTH ); - assert( pOp->p1 <= SQLITE_MAX_LENGTH ); + pOp->p1 = strlen(pOp->p4.z); #ifndef SQLITE_OMIT_UTF16 if( encoding!=SQLITE_UTF8 ){ - pTos++; - sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC); - if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem; - if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem; - pTos->flags &= ~(MEM_Dyn); - pTos->flags |= MEM_Static; - if( pOp->p3type==P3_DYNAMIC ){ - sqlite3_free(pOp->p3); - } - pOp->p3type = P3_DYNAMIC; - pOp->p3 = pTos->z; - pOp->p1 = pTos->n; - assert( pOp->p1 <= SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */ + sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); + if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; + if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pOut) ) goto no_mem; + pOut->flags &= ~(MEM_Dyn); + pOut->flags |= MEM_Static; + if( pOp->p4type==P4_DYNAMIC ){ + sqlite3_free(pOp->p4.z); + } + pOp->p4type = P4_DYNAMIC; + pOp->p4.z = pOut->z; + pOp->p1 = pOut->n; + if( pOp->p1>SQLITE_MAX_LENGTH ){ + goto too_big; + } + UPDATE_MAX_BLOBSIZE(pOut); break; } #endif - /* Otherwise fall through to the next case, OP_String */ + if( pOp->p1>SQLITE_MAX_LENGTH ){ + goto too_big; + } + /* Fall through to the next case, OP_String */ } -/* Opcode: String P1 * P3 -** -** The string value P3 of length P1 (bytes) is pushed onto the stack. -*/ -case OP_String: { - assert( pOp->p1 <= SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */ - pTos++; - assert( pOp->p3!=0 ); - pTos->flags = MEM_Str|MEM_Static|MEM_Term; - pTos->z = pOp->p3; - pTos->n = pOp->p1; - pTos->enc = encoding; +/* Opcode: String P1 P2 * P4 * +** +** The string value P4 of length P1 (bytes) is stored in register P2. +*/ +case OP_String: { /* out2-prerelease */ + assert( pOp->p4.z!=0 ); + pOut->flags = MEM_Str|MEM_Static|MEM_Term; + pOut->z = pOp->p4.z; + pOut->n = pOp->p1; + pOut->enc = encoding; + UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Null * * * +/* Opcode: Null * P2 * * * ** -** Push a NULL onto the stack. +** Write a NULL into register P2. */ -case OP_Null: { - pTos++; - pTos->flags = MEM_Null; - pTos->n = 0; +case OP_Null: { /* out2-prerelease */ break; } #ifndef SQLITE_OMIT_BLOB_LITERAL -/* Opcode: HexBlob * * P3 +/* Opcode: Blob P1 P2 * P4 ** -** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the -** vdbe stack. -** -** The first time this instruction executes, in transforms itself into a -** 'Blob' opcode with a binary blob as P3. -*/ -case OP_HexBlob: { /* same as TK_BLOB */ - pOp->opcode = OP_Blob; - pOp->p1 = strlen(pOp->p3)/2; - assert( SQLITE_MAX_SQL_LENGTH <= SQLITE_MAX_LENGTH ); - assert( pOp->p1 <= SQLITE_MAX_LENGTH ); - if( pOp->p1 ){ - char *zBlob = sqlite3HexToBlob(db, pOp->p3); - if( !zBlob ) goto no_mem; - if( pOp->p3type==P3_DYNAMIC ){ - sqlite3_free(pOp->p3); - } - pOp->p3 = zBlob; - pOp->p3type = P3_DYNAMIC; - }else{ - if( pOp->p3type==P3_DYNAMIC ){ - sqlite3_free(pOp->p3); - } - pOp->p3type = P3_STATIC; - pOp->p3 = ""; - } - - /* Fall through to the next case, OP_Blob. */ -} - -/* Opcode: Blob P1 * P3 -** -** P3 points to a blob of data P1 bytes long. Push this -** value onto the stack. This instruction is not coded directly +** P4 points to a blob of data P1 bytes long. Store this +** blob in register P2. This instruction is not coded directly ** by the compiler. Instead, the compiler layer specifies ** an OP_HexBlob opcode, with the hex string representation of -** the blob as P3. This opcode is transformed to an OP_Blob +** the blob as P4. This opcode is transformed to an OP_Blob ** the first time it is executed. */ -case OP_Blob: { - pTos++; - assert( pOp->p1 <= SQLITE_MAX_LENGTH ); /* Due to SQLITE_MAX_SQL_LENGTH */ - sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0); - pTos->enc = encoding; +case OP_Blob: { /* out2-prerelease */ + assert( pOp->p1 <= SQLITE_MAX_LENGTH ); + sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); + pOut->enc = encoding; + UPDATE_MAX_BLOBSIZE(pOut); break; } #endif /* SQLITE_OMIT_BLOB_LITERAL */ -/* Opcode: Variable P1 * * +/* Opcode: Variable P1 P2 * * * ** -** Push the value of variable P1 onto the stack. A variable is +** The value of variable P1 is written into register P2. A variable is ** an unknown in the original SQL string as handed to sqlite3_compile(). ** Any occurance of the '?' character in the original SQL is considered ** a variable. Variables in the SQL string are number from left to ** right beginning with 1. The values of variables are set using the ** sqlite3_bind() API. */ -case OP_Variable: { +case OP_Variable: { /* out2-prerelease */ int j = pOp->p1 - 1; Mem *pVar; assert( j>=0 && jnVar ); @@ -38608,124 +41446,73 @@ case OP_Variable: { if( sqlite3VdbeMemTooBig(pVar) ){ goto too_big; } - pTos++; - sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static); + sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static); + UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Pop P1 * * +/* Opcode: Move P1 P2 * * * ** -** P1 elements are popped off of the top of stack and discarded. +** Move the value in register P1 over into register P2. Register P1 +** is left holding a NULL. It is an error for P1 and P2 to be the +** same register. */ -case OP_Pop: { /* no-push */ - assert( pOp->p1>=0 ); - popStack(&pTos, pOp->p1); - assert( pTos>=&p->aStack[-1] ); - break; -} - -/* Opcode: Dup P1 P2 * -** -** A copy of the P1-th element of the stack -** is made and pushed onto the top of the stack. -** The top of the stack is element 0. So the -** instruction "Dup 0 0 0" will make a copy of the -** top of the stack. +/* Opcode: Copy P1 P2 * * * ** -** If the content of the P1-th element is a dynamically -** allocated string, then a new copy of that string -** is made if P2==0. If P2!=0, then just a pointer -** to the string is copied. +** Make a copy of register P1 into register P2. ** -** Also see the Pull instruction. +** This instruction makes a deep copy of the value. A duplicate +** is made of any string or blob constant. See also OP_SCopy. */ -case OP_Dup: { - Mem *pFrom = &pTos[-pOp->p1]; - assert( pFrom<=pTos && pFrom>=p->aStack ); - pTos++; - sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem); - if( pOp->p2 ){ - Deephemeralize(pTos); - } - break; -} - -/* Opcode: Pull P1 * * +/* Opcode: SCopy P1 P2 * * * ** -** The P1-th element is removed from its current location on -** the stack and pushed back on top of the stack. The -** top of the stack is element 0, so "Pull 0 0 0" is -** a no-op. "Pull 1 0 0" swaps the top two elements of -** the stack. +** Make a shallow copy of register P1 into register P2. ** -** See also the Dup instruction. +** This instruction makes a shallow copy of the value. If the value +** is a string or blob, then the copy is only a pointer to the +** original and hence if the original changes so will the copy. +** Worse, if the original is deallocated, the copy becomes invalid. +** Thus the program must guarantee that the original will not change +** during the lifetime of the copy. Use OP_Copy to make a complete +** copy. */ -case OP_Pull: { /* no-push */ - Mem *pFrom = &pTos[-pOp->p1]; - int i; - Mem ts; - - ts = *pFrom; - Deephemeralize(pTos); - for(i=0; ip1; i++, pFrom++){ - Deephemeralize(&pFrom[1]); - assert( (pFrom[1].flags & MEM_Ephem)==0 ); - *pFrom = pFrom[1]; - if( pFrom->flags & MEM_Short ){ - assert( pFrom->flags & (MEM_Str|MEM_Blob) ); - assert( pFrom->z==pFrom[1].zShort ); - pFrom->z = pFrom->zShort; - } - } - *pTos = ts; - if( pTos->flags & MEM_Short ){ - assert( pTos->flags & (MEM_Str|MEM_Blob) ); - assert( pTos->z==pTos[-pOp->p1].zShort ); - pTos->z = pTos->zShort; +case OP_Move: +case OP_Copy: +case OP_SCopy: { + assert( pOp->p1>0 ); + assert( pOp->p1<=p->nMem ); + pIn1 = &p->aMem[pOp->p1]; + REGISTER_TRACE(pOp->p1, pIn1); + assert( pOp->p2>0 ); + assert( pOp->p2<=p->nMem ); + pOut = &p->aMem[pOp->p2]; + assert( pOut!=pIn1 ); + if( pOp->opcode==OP_Move ){ + sqlite3VdbeMemMove(pOut, pIn1); + }else{ + sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); + if( pOp->opcode==OP_Copy ){ + Deephemeralize(pOut); + } } + REGISTER_TRACE(pOp->p2, pOut); break; } -/* Opcode: Push P1 * * +/* Opcode: ResultRow P1 P2 * * * ** -** Overwrite the value of the P1-th element down on the -** stack (P1==0 is the top of the stack) with the value -** of the top of the stack. Then pop the top of the stack. -*/ -case OP_Push: { /* no-push */ - Mem *pTo = &pTos[-pOp->p1]; - - assert( pTo>=p->aStack ); - sqlite3VdbeMemMove(pTo, pTos); - pTos--; - break; -} - -/* Opcode: Callback P1 * * -** -** The top P1 values on the stack represent a single result row from -** a query. This opcode causes the sqlite3_step() call to terminate +** The registers P1 throught P1+P2-1 contain a single row of +** results. This opcode causes the sqlite3_step() call to terminate ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt ** structure to provide access to the top P1 values as the result -** row. When the sqlite3_step() function is run again, the top P1 -** values will be automatically popped from the stack before the next -** instruction executes. +** row. */ -case OP_Callback: { /* no-push */ +case OP_ResultRow: { Mem *pMem; - Mem *pFirstColumn; - assert( p->nResColumn==pOp->p1 ); - - /* Data in the pager might be moved or changed out from under us - ** in between the return from this sqlite3_step() call and the - ** next call to sqlite3_step(). So deephermeralize everything on - ** the stack. Note that ephemeral data is never stored in memory - ** cells so we do not have to worry about them. - */ - pFirstColumn = &pTos[0-pOp->p1]; - for(pMem = p->aStack; pMemnResColumn==pOp->p2 ); + assert( pOp->p1>0 ); + assert( pOp->p1+pOp->p2<=p->nMem ); /* Invalidate all ephemeral cursor row caches */ p->cacheCtr = (p->cacheCtr + 2)|1; @@ -38734,169 +41521,117 @@ case OP_Callback: { /* no-push */ ** and have an assigned type. The results are deephemeralized as ** as side effect. */ - for(; pMem<=pTos; pMem++ ){ - sqlite3VdbeMemNulTerminate(pMem); - storeTypeInfo(pMem, encoding); + pMem = p->pResultSet = &p->aMem[pOp->p1]; + for(i=0; ip2; i++){ + sqlite3VdbeMemNulTerminate(&pMem[i]); + storeTypeInfo(&pMem[i], encoding); } + if( db->mallocFailed ) goto no_mem; - /* Set up the statement structure so that it will pop the current - ** results from the stack when the statement returns. + /* Return SQLITE_ROW */ - p->resOnStack = 1; p->nCallback++; - p->popStack = pOp->p1; p->pc = pc + 1; - p->pTos = pTos; rc = SQLITE_ROW; goto vdbe_return; } -/* Opcode: Concat P1 P2 * +/* Opcode: Concat P1 P2 P3 * * ** -** Look at the first P1+2 elements of the stack. Append them all -** together with the lowest element first. The original P1+2 elements -** are popped from the stack if P2==0 and retained if P2==1. If -** any element of the stack is NULL, then the result is NULL. +** Add the text in register P1 onto the end of the text in +** register P2 and store the result in register P3. +** If either the P1 or P2 text are NULL then store NULL in P3. ** -** When P1==1, this routine makes a copy of the top stack element -** into memory obtained from sqlite3_malloc(). +** P3 = P2 || P1 +** +** It is illegal for P1 and P3 to be the same register. Sometimes, +** if P3 is the same register as P2, the implementation is able +** to avoid a memcpy(). */ -case OP_Concat: { /* same as TK_CONCAT */ - char *zNew; +case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ i64 nByte; - int nField; - int i, j; - Mem *pTerm; - - /* Loop through the stack elements to see how long the result will be. */ - nField = pOp->p1 + 2; - pTerm = &pTos[1-nField]; - nByte = 0; - for(i=0; ip2==0 || (pTerm->flags&MEM_Str) ); - if( pTerm->flags&MEM_Null ){ - nByte = -1; - break; - } - ExpandBlob(pTerm); - Stringify(pTerm, encoding); - nByte += pTerm->n; - } - if( nByte<0 ){ - /* If nByte is less than zero, then there is a NULL value on the stack. - ** In this case just pop the values off the stack (if required) and - ** push on a NULL. - */ - if( pOp->p2==0 ){ - popStack(&pTos, nField); - } - pTos++; - pTos->flags = MEM_Null; - }else{ - /* Otherwise malloc() space for the result and concatenate all the - ** stack values. - */ - if( nByte+2>SQLITE_MAX_LENGTH ){ - goto too_big; - } - zNew = sqlite3DbMallocRaw(db, nByte+2 ); - if( zNew==0 ) goto no_mem; - j = 0; - pTerm = &pTos[1-nField]; - for(i=j=0; in; - assert( pTerm->flags & (MEM_Str|MEM_Blob) ); - memcpy(&zNew[j], pTerm->z, n); - j += n; - } - zNew[j] = 0; - zNew[j+1] = 0; - assert( j==nByte ); - - if( pOp->p2==0 ){ - popStack(&pTos, nField); - } - pTos++; - pTos->n = j; - pTos->flags = MEM_Str|MEM_Dyn|MEM_Term; - pTos->xDel = 0; - pTos->enc = encoding; - pTos->z = zNew; + assert( pIn1!=pOut ); + if( (pIn1->flags | pIn2->flags) & MEM_Null ){ + sqlite3VdbeMemSetNull(pOut); + break; } + ExpandBlob(pIn1); + Stringify(pIn1, encoding); + ExpandBlob(pIn2); + Stringify(pIn2, encoding); + nByte = pIn1->n + pIn2->n; + if( nByte>SQLITE_MAX_LENGTH ){ + goto too_big; + } + MemSetTypeFlag(pOut, MEM_Str); + if( sqlite3VdbeMemGrow(pOut, nByte+2, pOut==pIn2) ){ + goto no_mem; + } + if( pOut!=pIn2 ){ + memcpy(pOut->z, pIn2->z, pIn2->n); + } + memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); + pOut->z[nByte] = 0; + pOut->z[nByte+1] = 0; + pOut->flags |= MEM_Term; + pOut->n = nByte; + pOut->enc = encoding; + UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Add * * * +/* Opcode: Add P1 P2 P3 * * ** -** Pop the top two elements from the stack, add them together, -** and push the result back onto the stack. If either element -** is a string then it is converted to a double using the atof() -** function before the addition. -** If either operand is NULL, the result is NULL. +** Add the value in register P1 to the value in register P2 +** and store the result in regiser P3. +** If either input is NULL, the result is NULL. */ -/* Opcode: Multiply * * * +/* Opcode: Multiply P1 P2 P3 * * ** -** Pop the top two elements from the stack, multiply them together, -** and push the result back onto the stack. If either element -** is a string then it is converted to a double using the atof() -** function before the multiplication. -** If either operand is NULL, the result is NULL. +** +** Multiply the value in regiser P1 by the value in regiser P2 +** and store the result in register P3. +** If either input is NULL, the result is NULL. */ -/* Opcode: Subtract * * * +/* Opcode: Subtract P1 P2 P3 * * ** -** Pop the top two elements from the stack, subtract the -** first (what was on top of the stack) from the second (the -** next on stack) -** and push the result back onto the stack. If either element -** is a string then it is converted to a double using the atof() -** function before the subtraction. -** If either operand is NULL, the result is NULL. +** Subtract the value in register P1 from the value in register P2 +** and store the result in register P3. +** If either input is NULL, the result is NULL. */ -/* Opcode: Divide * * * +/* Opcode: Divide P1 P2 P3 * * ** -** Pop the top two elements from the stack, divide the -** first (what was on top of the stack) from the second (the -** next on stack) -** and push the result back onto the stack. If either element -** is a string then it is converted to a double using the atof() -** function before the division. Division by zero returns NULL. -** If either operand is NULL, the result is NULL. +** Divide the value in register P1 by the value in register P2 +** and store the result in register P3. If the value in register P2 +** is zero, then the result is NULL. +** If either input is NULL, the result is NULL. */ -/* Opcode: Remainder * * * +/* Opcode: Remainder P1 P2 P3 * * ** -** Pop the top two elements from the stack, divide the -** first (what was on top of the stack) from the second (the -** next on stack) -** and push the remainder after division onto the stack. If either element -** is a string then it is converted to a double using the atof() -** function before the division. Division by zero returns NULL. +** Compute the remainder after integer division of the value in +** register P1 by the value in register P2 and store the result in P3. +** If the value in register P2 is zero the result is NULL. ** If either operand is NULL, the result is NULL. */ -case OP_Add: /* same as TK_PLUS, no-push */ -case OP_Subtract: /* same as TK_MINUS, no-push */ -case OP_Multiply: /* same as TK_STAR, no-push */ -case OP_Divide: /* same as TK_SLASH, no-push */ -case OP_Remainder: { /* same as TK_REM, no-push */ - Mem *pNos = &pTos[-1]; +case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ +case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ +case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ +case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ +case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ int flags; - assert( pNos>=p->aStack ); - flags = pTos->flags | pNos->flags; - if( (flags & MEM_Null)!=0 ){ - Release(pTos); - pTos--; - Release(pTos); - pTos->flags = MEM_Null; - }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){ + flags = pIn1->flags | pIn2->flags; + if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; + if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){ i64 a, b; - a = pTos->u.i; - b = pNos->u.i; + a = pIn1->u.i; + b = pIn2->u.i; switch( pOp->opcode ){ case OP_Add: b += a; break; case OP_Subtract: b -= a; break; case OP_Multiply: b *= a; break; case OP_Divide: { - if( a==0 ) goto divide_by_zero; + if( a==0 ) goto arithmetic_result_is_null; /* Dividing the largest possible negative 64-bit integer (1<<63) by ** -1 returns an integer to large to store in a 64-bit data-type. On ** some architectures, the value overflows to (1<<63). On others, @@ -38909,64 +41644,55 @@ case OP_Remainder: { /* same as TK_REM, no-push */ break; } default: { - if( a==0 ) goto divide_by_zero; + if( a==0 ) goto arithmetic_result_is_null; if( a==-1 ) a = 1; b %= a; break; } } - Release(pTos); - pTos--; - Release(pTos); - pTos->u.i = b; - pTos->flags = MEM_Int; + pOut->u.i = b; + MemSetTypeFlag(pOut, MEM_Int); }else{ double a, b; - a = sqlite3VdbeRealValue(pTos); - b = sqlite3VdbeRealValue(pNos); + a = sqlite3VdbeRealValue(pIn1); + b = sqlite3VdbeRealValue(pIn2); switch( pOp->opcode ){ case OP_Add: b += a; break; case OP_Subtract: b -= a; break; case OP_Multiply: b *= a; break; case OP_Divide: { - if( a==0.0 ) goto divide_by_zero; + if( a==0.0 ) goto arithmetic_result_is_null; b /= a; break; } default: { i64 ia = (i64)a; i64 ib = (i64)b; - if( ia==0 ) goto divide_by_zero; + if( ia==0 ) goto arithmetic_result_is_null; if( ia==-1 ) ia = 1; b = ib % ia; break; } } if( sqlite3_isnan(b) ){ - goto divide_by_zero; + goto arithmetic_result_is_null; } - Release(pTos); - pTos--; - Release(pTos); - pTos->r = b; - pTos->flags = MEM_Real; + pOut->r = b; + MemSetTypeFlag(pOut, MEM_Real); if( (flags & MEM_Real)==0 ){ - sqlite3VdbeIntegerAffinity(pTos); + sqlite3VdbeIntegerAffinity(pOut); } } break; -divide_by_zero: - Release(pTos); - pTos--; - Release(pTos); - pTos->flags = MEM_Null; +arithmetic_result_is_null: + sqlite3VdbeMemSetNull(pOut); break; } -/* Opcode: CollSeq * * P3 +/* Opcode: CollSeq * * P4 ** -** P3 is a pointer to a CollSeq struct. If the next call to a user function +** P4 is a pointer to a CollSeq struct. If the next call to a user function ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will ** be returned. This is used by the built-in min(), max() and nullif() ** functions. @@ -38975,16 +41701,17 @@ divide_by_zero: ** to retrieve the collation sequence set by this opcode is not available ** publicly, only to user functions defined in func.c. */ -case OP_CollSeq: { /* no-push */ - assert( pOp->p3type==P3_COLLSEQ ); +case OP_CollSeq: { + assert( pOp->p4type==P4_COLLSEQ ); break; } -/* Opcode: Function P1 P2 P3 +/* Opcode: Function P1 P2 P3 P4 P5 ** -** Invoke a user function (P3 is a pointer to a Function structure that -** defines the function) with P2 arguments taken from the stack. Pop all -** arguments from the stack and push back the result. +** Invoke a user function (P4 is a pointer to a Function structure that +** defines the function) with P5 arguments taken from register P2 and +** successors. The result of the function is stored in register P3. +** Register P3 must not be one of the function inputs. ** ** P1 is a 32-bit bitmask indicating whether or not each argument to the ** function was determined to be constant at compile time. If the first @@ -39000,36 +41727,47 @@ case OP_Function: { Mem *pArg; sqlite3_context ctx; sqlite3_value **apVal; - int n = pOp->p2; + int n = pOp->p5; apVal = p->apArg; assert( apVal || n==0 ); - pArg = &pTos[1-n]; + assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) ); + assert( pOp->p3p2 || pOp->p3>=pOp->p2+n ); + pArg = &p->aMem[pOp->p2]; for(i=0; ip2, pArg); } - assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC ); - if( pOp->p3type==P3_FUNCDEF ){ - ctx.pFunc = (FuncDef*)pOp->p3; + assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC ); + if( pOp->p4type==P4_FUNCDEF ){ + ctx.pFunc = pOp->p4.pFunc; ctx.pVdbeFunc = 0; }else{ - ctx.pVdbeFunc = (VdbeFunc*)pOp->p3; + ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc; ctx.pFunc = ctx.pVdbeFunc->pFunc; } + assert( pOp->p3>0 && pOp->p3<=p->nMem ); + pOut = &p->aMem[pOp->p3]; ctx.s.flags = MEM_Null; - ctx.s.z = 0; - ctx.s.xDel = 0; - ctx.s.db = db; + ctx.s.db = 0; + + /* The output cell may already have a buffer allocated. Move + ** the pointer to ctx.s so in case the user-function can use + ** the already allocated buffer instead of allocating a new one. + */ + sqlite3VdbeMemMove(&ctx.s, pOut); + MemSetTypeFlag(&ctx.s, MEM_Null); + ctx.isError = 0; if( ctx.pFunc->needCollSeq ){ assert( pOp>p->aOp ); - assert( pOp[-1].p3type==P3_COLLSEQ ); + assert( pOp[-1].p4type==P4_COLLSEQ ); assert( pOp[-1].opcode==OP_CollSeq ); - ctx.pColl = (CollSeq *)pOp[-1].p3; + ctx.pColl = pOp[-1].p4.pColl; } if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; (*ctx.pFunc->xFunc)(&ctx, n, apVal); @@ -39047,234 +41785,209 @@ case OP_Function: { sqlite3VdbeMemRelease(&ctx.s); goto no_mem; } - popStack(&pTos, n); /* If any auxilary data functions have been called by this user function, ** immediately call the destructor for any non-static values. */ if( ctx.pVdbeFunc ){ sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1); - pOp->p3 = (char *)ctx.pVdbeFunc; - pOp->p3type = P3_VDBEFUNC; + pOp->p4.pVdbeFunc = ctx.pVdbeFunc; + pOp->p4type = P4_VDBEFUNC; } /* If the function returned an error, throw an exception */ if( ctx.isError ){ sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0); - rc = SQLITE_ERROR; + rc = ctx.isError; } - /* Copy the result of the function to the top of the stack */ + /* Copy the result of the function into register P3 */ sqlite3VdbeChangeEncoding(&ctx.s, encoding); - pTos++; - pTos->flags = 0; - sqlite3VdbeMemMove(pTos, &ctx.s); - if( sqlite3VdbeMemTooBig(pTos) ){ + sqlite3VdbeMemMove(pOut, &ctx.s); + if( sqlite3VdbeMemTooBig(pOut) ){ goto too_big; } + REGISTER_TRACE(pOp->p3, pOut); + UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: BitAnd * * * +/* Opcode: BitAnd P1 P2 P3 * * ** -** Pop the top two elements from the stack. Convert both elements -** to integers. Push back onto the stack the bit-wise AND of the -** two elements. -** If either operand is NULL, the result is NULL. +** Take the bit-wise AND of the values in register P1 and P2 and +** store the result in register P3. +** If either input is NULL, the result is NULL. */ -/* Opcode: BitOr * * * +/* Opcode: BitOr P1 P2 P3 * * ** -** Pop the top two elements from the stack. Convert both elements -** to integers. Push back onto the stack the bit-wise OR of the -** two elements. -** If either operand is NULL, the result is NULL. +** Take the bit-wise OR of the values in register P1 and P2 and +** store the result in register P3. +** If either input is NULL, the result is NULL. */ -/* Opcode: ShiftLeft * * * +/* Opcode: ShiftLeft P1 P2 P3 * * ** -** Pop the top two elements from the stack. Convert both elements -** to integers. Push back onto the stack the second element shifted -** left by N bits where N is the top element on the stack. -** If either operand is NULL, the result is NULL. +** Shift the integer value in register P2 to the left by the +** number of bits specified by the integer in regiser P1. +** Store the result in register P3. +** If either input is NULL, the result is NULL. */ -/* Opcode: ShiftRight * * * +/* Opcode: ShiftRight P1 P2 P3 * * ** -** Pop the top two elements from the stack. Convert both elements -** to integers. Push back onto the stack the second element shifted -** right by N bits where N is the top element on the stack. -** If either operand is NULL, the result is NULL. +** Shift the integer value in register P2 to the right by the +** number of bits specified by the integer in register P1. +** Store the result in register P3. +** If either input is NULL, the result is NULL. */ -case OP_BitAnd: /* same as TK_BITAND, no-push */ -case OP_BitOr: /* same as TK_BITOR, no-push */ -case OP_ShiftLeft: /* same as TK_LSHIFT, no-push */ -case OP_ShiftRight: { /* same as TK_RSHIFT, no-push */ - Mem *pNos = &pTos[-1]; +case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ +case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ +case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ +case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ i64 a, b; - assert( pNos>=p->aStack ); - if( (pTos->flags | pNos->flags) & MEM_Null ){ - popStack(&pTos, 2); - pTos++; - pTos->flags = MEM_Null; + if( (pIn1->flags | pIn2->flags) & MEM_Null ){ + sqlite3VdbeMemSetNull(pOut); break; } - a = sqlite3VdbeIntValue(pNos); - b = sqlite3VdbeIntValue(pTos); + a = sqlite3VdbeIntValue(pIn2); + b = sqlite3VdbeIntValue(pIn1); switch( pOp->opcode ){ case OP_BitAnd: a &= b; break; case OP_BitOr: a |= b; break; case OP_ShiftLeft: a <<= b; break; - case OP_ShiftRight: a >>= b; break; - default: /* CANT HAPPEN */ break; - } - Release(pTos); - pTos--; - Release(pTos); - pTos->u.i = a; - pTos->flags = MEM_Int; + default: assert( pOp->opcode==OP_ShiftRight ); + a >>= b; break; + } + pOut->u.i = a; + MemSetTypeFlag(pOut, MEM_Int); break; } -/* Opcode: AddImm P1 * * +/* Opcode: AddImm P1 P2 * * * ** -** Add the value P1 to whatever is on top of the stack. The result -** is always an integer. +** Add the constant P2 the value in register P1. +** The result is always an integer. ** -** To force the top of the stack to be an integer, just add 0. +** To force any register to be an integer, just add 0. */ -case OP_AddImm: { /* no-push */ - assert( pTos>=p->aStack ); - sqlite3VdbeMemIntegerify(pTos); - pTos->u.i += pOp->p1; +case OP_AddImm: { /* in1 */ + sqlite3VdbeMemIntegerify(pIn1); + pIn1->u.i += pOp->p2; break; } -/* Opcode: ForceInt P1 P2 * +/* Opcode: ForceInt P1 P2 P3 * * ** -** Convert the top of the stack into an integer. If the current top of -** the stack is not numeric (meaning that is is a NULL or a string that -** does not look like an integer or floating point number) then pop the -** stack and jump to P2. If the top of the stack is numeric then +** Convert value in register P1 into an integer. If the value +** in P1 is not numeric (meaning that is is a NULL or a string that +** does not look like an integer or floating point number) then +** jump to P2. If the value in P1 is numeric then ** convert it into the least integer that is greater than or equal to its -** current value if P1==0, or to the least integer that is strictly -** greater than its current value if P1==1. +** current value if P3==0, or to the least integer that is strictly +** greater than its current value if P3==1. */ -case OP_ForceInt: { /* no-push */ +case OP_ForceInt: { /* jump, in1 */ i64 v; - assert( pTos>=p->aStack ); - applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding); - if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){ - Release(pTos); - pTos--; + applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); + if( (pIn1->flags & (MEM_Int|MEM_Real))==0 ){ pc = pOp->p2 - 1; break; } - if( pTos->flags & MEM_Int ){ - v = pTos->u.i + (pOp->p1!=0); + if( pIn1->flags & MEM_Int ){ + v = pIn1->u.i + (pOp->p3!=0); }else{ - /* FIX ME: should this not be assert( pTos->flags & MEM_Real ) ??? */ - sqlite3VdbeMemRealify(pTos); - v = (int)pTos->r; - if( pTos->r>(double)v ) v++; - if( pOp->p1 && pTos->r==(double)v ) v++; - } - Release(pTos); - pTos->u.i = v; - pTos->flags = MEM_Int; + assert( pIn1->flags & MEM_Real ); + v = (sqlite3_int64)pIn1->r; + if( pIn1->r>(double)v ) v++; + if( pOp->p3 && pIn1->r==(double)v ) v++; + } + pIn1->u.i = v; + MemSetTypeFlag(pIn1, MEM_Int); break; } -/* Opcode: MustBeInt P1 P2 * +/* Opcode: MustBeInt P1 P2 * * * ** -** Force the top of the stack to be an integer. If the top of the -** stack is not an integer and cannot be converted into an integer +** Force the value in register P1 to be an integer. If the value +** in P1 is not an integer and cannot be converted into an integer ** without data loss, then jump immediately to P2, or if P2==0 ** raise an SQLITE_MISMATCH exception. -** -** If the top of the stack is not an integer and P2 is not zero and -** P1 is 1, then the stack is popped. In all other cases, the depth -** of the stack is unchanged. */ -case OP_MustBeInt: { /* no-push */ - assert( pTos>=p->aStack ); - applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding); - if( (pTos->flags & MEM_Int)==0 ){ +case OP_MustBeInt: { /* jump, in1 */ + applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); + if( (pIn1->flags & MEM_Int)==0 ){ if( pOp->p2==0 ){ rc = SQLITE_MISMATCH; goto abort_due_to_error; }else{ - if( pOp->p1 ) popStack(&pTos, 1); pc = pOp->p2 - 1; } }else{ - Release(pTos); - pTos->flags = MEM_Int; + MemSetTypeFlag(pIn1, MEM_Int); } break; } -/* Opcode: RealAffinity * * * +/* Opcode: RealAffinity P1 * * * * ** -** If the top of the stack is an integer, convert it to a real value. +** If register P1 holds an integer convert it to a real value. ** ** This opcode is used when extracting information from a column that ** has REAL affinity. Such column values may still be stored as ** integers, for space efficiency, but after extraction we want them ** to have only a real value. */ -case OP_RealAffinity: { /* no-push */ - assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Int ){ - sqlite3VdbeMemRealify(pTos); +case OP_RealAffinity: { /* in1 */ + if( pIn1->flags & MEM_Int ){ + sqlite3VdbeMemRealify(pIn1); } break; } #ifndef SQLITE_OMIT_CAST -/* Opcode: ToText * * * +/* Opcode: ToText P1 * * * * ** -** Force the value on the top of the stack to be text. +** Force the value in register P1 to be text. ** If the value is numeric, convert it to a string using the ** equivalent of printf(). Blob values are unchanged and ** are afterwards simply interpreted as text. ** ** A NULL value is not changed by this routine. It remains NULL. */ -case OP_ToText: { /* same as TK_TO_TEXT, no-push */ - assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Null ) break; +case OP_ToText: { /* same as TK_TO_TEXT, in1 */ + if( pIn1->flags & MEM_Null ) break; assert( MEM_Str==(MEM_Blob>>3) ); - pTos->flags |= (pTos->flags&MEM_Blob)>>3; - applyAffinity(pTos, SQLITE_AFF_TEXT, encoding); - rc = ExpandBlob(pTos); - assert( pTos->flags & MEM_Str ); - pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); + pIn1->flags |= (pIn1->flags&MEM_Blob)>>3; + applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); + rc = ExpandBlob(pIn1); + assert( pIn1->flags & MEM_Str || db->mallocFailed ); + pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob); + UPDATE_MAX_BLOBSIZE(pIn1); break; } -/* Opcode: ToBlob * * * +/* Opcode: ToBlob P1 * * * * ** -** Force the value on the top of the stack to be a BLOB. +** Force the value in register P1 to be a BLOB. ** If the value is numeric, convert it to a string first. ** Strings are simply reinterpreted as blobs with no change ** to the underlying data. ** ** A NULL value is not changed by this routine. It remains NULL. */ -case OP_ToBlob: { /* same as TK_TO_BLOB, no-push */ - assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Null ) break; - if( (pTos->flags & MEM_Blob)==0 ){ - applyAffinity(pTos, SQLITE_AFF_TEXT, encoding); - assert( pTos->flags & MEM_Str ); - pTos->flags |= MEM_Blob; +case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */ + if( pIn1->flags & MEM_Null ) break; + if( (pIn1->flags & MEM_Blob)==0 ){ + applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding); + assert( pIn1->flags & MEM_Str || db->mallocFailed ); } - pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str); + MemSetTypeFlag(pIn1, MEM_Blob); + UPDATE_MAX_BLOBSIZE(pIn1); break; } -/* Opcode: ToNumeric * * * +/* Opcode: ToNumeric P1 * * * * ** -** Force the value on the top of the stack to be numeric (either an +** Force the value in register P1 to be numeric (either an ** integer or a floating-point number.) ** If the value is text or blob, try to convert it to an using the ** equivalent of atoi() or atof() and store 0 if no such conversion @@ -39282,177 +41995,168 @@ case OP_ToBlob: { /* same as TK_TO_BLOB, no-push */ ** ** A NULL value is not changed by this routine. It remains NULL. */ -case OP_ToNumeric: { /* same as TK_TO_NUMERIC, no-push */ - assert( pTos>=p->aStack ); - if( (pTos->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ - sqlite3VdbeMemNumerify(pTos); +case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */ + if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){ + sqlite3VdbeMemNumerify(pIn1); } break; } #endif /* SQLITE_OMIT_CAST */ -/* Opcode: ToInt * * * +/* Opcode: ToInt P1 * * * * ** -** Force the value on the top of the stack to be an integer. If +** Force the value in register P1 be an integer. If ** The value is currently a real number, drop its fractional part. ** If the value is text or blob, try to convert it to an integer using the ** equivalent of atoi() and store 0 if no such conversion is possible. ** ** A NULL value is not changed by this routine. It remains NULL. */ -case OP_ToInt: { /* same as TK_TO_INT, no-push */ - assert( pTos>=p->aStack ); - if( (pTos->flags & MEM_Null)==0 ){ - sqlite3VdbeMemIntegerify(pTos); +case OP_ToInt: { /* same as TK_TO_INT, in1 */ + if( (pIn1->flags & MEM_Null)==0 ){ + sqlite3VdbeMemIntegerify(pIn1); } break; } #ifndef SQLITE_OMIT_CAST -/* Opcode: ToReal * * * +/* Opcode: ToReal P1 * * * * ** -** Force the value on the top of the stack to be a floating point number. +** Force the value in register P1 to be a floating point number. ** If The value is currently an integer, convert it. ** If the value is text or blob, try to convert it to an integer using the -** equivalent of atoi() and store 0 if no such conversion is possible. +** equivalent of atoi() and store 0.0 if no such conversion is possible. ** ** A NULL value is not changed by this routine. It remains NULL. */ -case OP_ToReal: { /* same as TK_TO_REAL, no-push */ - assert( pTos>=p->aStack ); - if( (pTos->flags & MEM_Null)==0 ){ - sqlite3VdbeMemRealify(pTos); +case OP_ToReal: { /* same as TK_TO_REAL, in1 */ + if( (pIn1->flags & MEM_Null)==0 ){ + sqlite3VdbeMemRealify(pIn1); } break; } #endif /* SQLITE_OMIT_CAST */ -/* Opcode: Eq P1 P2 P3 +/* Opcode: Lt P1 P2 P3 P4 P5 ** -** Pop the top two elements from the stack. If they are equal, then -** jump to instruction P2. Otherwise, continue to the next instruction. +** Compare the values in register P1 and P3. If reg(P3)flags|pNos->flags; + flags = pIn1->flags|pIn3->flags; - /* If either value is a NULL P2 is not zero, take the jump if the least - ** significant byte of P1 is true. If P2 is zero, then push a NULL onto - ** the stack. - */ if( flags&MEM_Null ){ - if( (pOp->p1 & 0x200)!=0 ){ - /* The 0x200 bit of P1 means, roughly "do not treat NULL as the - ** magic SQL value it normally is - treat it as if it were another - ** integer". - ** - ** With 0x200 set, if either operand is NULL then both operands - ** are converted to integers prior to being passed down into the - ** normal comparison logic below. NULL operands are converted to - ** zero and non-NULL operands are converted to 1. Thus, for example, - ** with 0x200 set, NULL==NULL is true whereas it would normally - ** be NULL. Similarly, NULL!=123 is true. + if( (pOp->p5 & SQLITE_NULLEQUAL)!=0 ){ + /* + ** When SQLITE_NULLEQUAL set and either operand is NULL + ** then both operands are converted to integers prior to being + ** passed down into the normal comparison logic below. + ** NULL operands are converted to zero and non-NULL operands + ** are converted to 1. Thus, for example, with SQLITE_NULLEQUAL + ** set, NULL==NULL is true whereas it would normally NULL. + ** Similarly, NULL!=123 is true. */ - sqlite3VdbeMemSetInt64(pTos, (pTos->flags & MEM_Null)==0); - sqlite3VdbeMemSetInt64(pNos, (pNos->flags & MEM_Null)==0); + x1.flags = MEM_Int; + x1.u.i = (pIn1->flags & MEM_Null)==0; + pIn1 = &x1; + x3.flags = MEM_Int; + x3.u.i = (pIn3->flags & MEM_Null)==0; + pIn3 = &x3; }else{ - /* If the 0x200 bit of P1 is clear and either operand is NULL then - ** the result is always NULL. The jump is taken if the 0x100 bit - ** of P1 is set. + /* If the SQLITE_NULLEQUAL bit is clear and either operand is NULL then + ** the result is always NULL. The jump is taken if the + ** SQLITE_JUMPIFNULL bit is set. */ - popStack(&pTos, 2); - if( pOp->p2 ){ - if( pOp->p1 & 0x100 ){ - pc = pOp->p2-1; - } - }else{ - pTos++; - pTos->flags = MEM_Null; + if( pOp->p5 & SQLITE_STOREP2 ){ + pOut = &p->aMem[pOp->p2]; + MemSetTypeFlag(pOut, MEM_Null); + REGISTER_TRACE(pOp->p2, pOut); + }else if( pOp->p5 & SQLITE_JUMPIFNULL ){ + pc = pOp->p2-1; } break; } } - affinity = pOp->p1 & 0xFF; + affinity = pOp->p5 & SQLITE_AFF_MASK; if( affinity ){ - applyAffinity(pNos, affinity, encoding); - applyAffinity(pTos, affinity, encoding); + applyAffinity(pIn1, affinity, encoding); + applyAffinity(pIn3, affinity, encoding); } - assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 ); - ExpandBlob(pNos); - ExpandBlob(pTos); - res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3); + assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); + ExpandBlob(pIn1); + ExpandBlob(pIn3); + res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); switch( pOp->opcode ){ case OP_Eq: res = res==0; break; case OP_Ne: res = res!=0; break; @@ -39462,229 +42166,155 @@ case OP_Ge: { /* same as TK_GE, no-push */ default: res = res>=0; break; } - popStack(&pTos, 2); - if( pOp->p2 ){ - if( res ){ - pc = pOp->p2-1; - } - }else{ - pTos++; - pTos->flags = MEM_Int; - pTos->u.i = res; + if( pOp->p5 & SQLITE_STOREP2 ){ + pOut = &p->aMem[pOp->p2]; + MemSetTypeFlag(pOut, MEM_Int); + pOut->u.i = res; + REGISTER_TRACE(pOp->p2, pOut); + }else if( res ){ + pc = pOp->p2-1; } break; } -/* Opcode: And * * * +/* Opcode: And P1 P2 P3 * * +** +** Take the logical AND of the values in registers P1 and P2 and +** write the result into register P3. ** -** Pop two values off the stack. Take the logical AND of the -** two values and push the resulting boolean value back onto the -** stack. +** If either P1 or P2 is 0 (false) then the result is 0 even if +** the other input is NULL. A NULL and true or two NULLs give +** a NULL output. */ -/* Opcode: Or * * * +/* Opcode: Or P1 P2 P3 * * ** -** Pop two values off the stack. Take the logical OR of the -** two values and push the resulting boolean value back onto the -** stack. +** Take the logical OR of the values in register P1 and P2 and +** store the answer in register P3. +** +** If either P1 or P2 is nonzero (true) then the result is 1 (true) +** even if the other input is NULL. A NULL and false or two NULLs +** give a NULL output. */ -case OP_And: /* same as TK_AND, no-push */ -case OP_Or: { /* same as TK_OR, no-push */ - Mem *pNos = &pTos[-1]; - int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */ +case OP_And: /* same as TK_AND, in1, in2, out3 */ +case OP_Or: { /* same as TK_OR, in1, in2, out3 */ + int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ - assert( pNos>=p->aStack ); - if( pTos->flags & MEM_Null ){ + if( pIn1->flags & MEM_Null ){ v1 = 2; }else{ - sqlite3VdbeMemIntegerify(pTos); - v1 = pTos->u.i==0; + v1 = sqlite3VdbeIntValue(pIn1)!=0; } - if( pNos->flags & MEM_Null ){ + if( pIn2->flags & MEM_Null ){ v2 = 2; }else{ - sqlite3VdbeMemIntegerify(pNos); - v2 = pNos->u.i==0; + v2 = sqlite3VdbeIntValue(pIn2)!=0; } if( pOp->opcode==OP_And ){ - static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; + static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; v1 = and_logic[v1*3+v2]; }else{ - static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; + static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; v1 = or_logic[v1*3+v2]; } - popStack(&pTos, 2); - pTos++; if( v1==2 ){ - pTos->flags = MEM_Null; + MemSetTypeFlag(pOut, MEM_Null); }else{ - pTos->u.i = v1==0; - pTos->flags = MEM_Int; + pOut->u.i = v1; + MemSetTypeFlag(pOut, MEM_Int); } break; } -/* Opcode: Negative * * * -** -** Treat the top of the stack as a numeric quantity. Replace it -** with its additive inverse. If the top of the stack is NULL -** its value is unchanged. -*/ -/* Opcode: AbsValue * * * +/* Opcode: Not P1 * * * * ** -** Treat the top of the stack as a numeric quantity. Replace it -** with its absolute value. If the top of the stack is NULL -** its value is unchanged. -*/ -case OP_Negative: /* same as TK_UMINUS, no-push */ -case OP_AbsValue: { - assert( pTos>=p->aStack ); - if( (pTos->flags & (MEM_Real|MEM_Int|MEM_Null))==0 ){ - sqlite3VdbeMemNumerify(pTos); - } - if( pTos->flags & MEM_Real ){ - Release(pTos); - if( pOp->opcode==OP_Negative || pTos->r<0.0 ){ - pTos->r = -pTos->r; - } - pTos->flags = MEM_Real; - }else if( pTos->flags & MEM_Int ){ - Release(pTos); - if( pOp->opcode==OP_Negative || pTos->u.i<0 ){ - pTos->u.i = -pTos->u.i; - } - pTos->flags = MEM_Int; - } - break; -} - -/* Opcode: Not * * * -** -** Interpret the top of the stack as a boolean value. Replace it -** with its complement. If the top of the stack is NULL its value +** Interpret the value in register P1 as a boolean value. Replace it +** with its complement. If the value in register P1 is NULL its value ** is unchanged. */ -case OP_Not: { /* same as TK_NOT, no-push */ - assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ - sqlite3VdbeMemIntegerify(pTos); - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos->u.i = !pTos->u.i; - pTos->flags = MEM_Int; +case OP_Not: { /* same as TK_NOT, in1 */ + if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */ + sqlite3VdbeMemIntegerify(pIn1); + pIn1->u.i = !pIn1->u.i; + assert( pIn1->flags&MEM_Int ); break; } -/* Opcode: BitNot * * * +/* Opcode: BitNot P1 * * * * ** -** Interpret the top of the stack as an value. Replace it -** with its ones-complement. If the top of the stack is NULL its -** value is unchanged. -*/ -case OP_BitNot: { /* same as TK_BITNOT, no-push */ - assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */ - sqlite3VdbeMemIntegerify(pTos); - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos->u.i = ~pTos->u.i; - pTos->flags = MEM_Int; - break; -} - -/* Opcode: Noop * * * -** -** Do nothing. This instruction is often useful as a jump -** destination. +** Interpret the content of register P1 as an integer. Replace it +** with its ones-complement. If the value is originally NULL, leave +** it unchanged. */ -/* -** The magic Explain opcode are only inserted when explain==2 (which -** is to say when the EXPLAIN QUERY PLAN syntax is used.) -** This opcode records information from the optimizer. It is the -** the same as a no-op. This opcodesnever appears in a real VM program. -*/ -case OP_Explain: -case OP_Noop: { /* no-push */ +case OP_BitNot: { /* same as TK_BITNOT, in1 */ + if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */ + sqlite3VdbeMemIntegerify(pIn1); + pIn1->u.i = ~pIn1->u.i; + assert( pIn1->flags&MEM_Int ); break; } -/* Opcode: If P1 P2 * -** -** Pop a single boolean from the stack. If the boolean popped is -** true, then jump to p2. Otherwise continue to the next instruction. -** An integer is false if zero and true otherwise. A string is -** false if it has zero length and true otherwise. +/* Opcode: If P1 P2 P3 * * ** -** If the value popped of the stack is NULL, then take the jump if P1 -** is true and fall through if P1 is false. +** Jump to P2 if the value in register P1 is true. The value is +** is considered true if it is numeric and non-zero. If the value +** in P1 is NULL then take the jump if P3 is true. */ -/* Opcode: IfNot P1 P2 * -** -** Pop a single boolean from the stack. If the boolean popped is -** false, then jump to p2. Otherwise continue to the next instruction. -** An integer is false if zero and true otherwise. A string is -** false if it has zero length and true otherwise. +/* Opcode: IfNot P1 P2 P3 * * ** -** If the value popped of the stack is NULL, then take the jump if P1 -** is true and fall through if P1 is false. +** Jump to P2 if the value in register P1 is False. The value is +** is considered true if it has a numeric value of zero. If the value +** in P1 is NULL then take the jump if P3 is true. */ -case OP_If: /* no-push */ -case OP_IfNot: { /* no-push */ +case OP_If: /* jump, in1 */ +case OP_IfNot: { /* jump, in1 */ int c; - assert( pTos>=p->aStack ); - if( pTos->flags & MEM_Null ){ - c = pOp->p1; + if( pIn1->flags & MEM_Null ){ + c = pOp->p3; }else{ #ifdef SQLITE_OMIT_FLOATING_POINT - c = sqlite3VdbeIntValue(pTos); + c = sqlite3VdbeIntValue(pIn1); #else - c = sqlite3VdbeRealValue(pTos)!=0.0; + c = sqlite3VdbeRealValue(pIn1)!=0.0; #endif if( pOp->opcode==OP_IfNot ) c = !c; } - Release(pTos); - pTos--; - if( c ) pc = pOp->p2-1; + if( c ){ + pc = pOp->p2-1; + } break; } -/* Opcode: IsNull P1 P2 * +/* Opcode: IsNull P1 P2 P3 * * ** -** Check the top of the stack and jump to P2 if the top of the stack -** is NULL. If P1 is positive, then pop P1 elements from the stack -** regardless of whether or not the jump is taken. If P1 is negative, -** pop -P1 elements from the stack only if the jump is taken and leave -** the stack unchanged if the jump is not taken. +** Jump to P2 if the value in register P1 is NULL. If P3 is greater +** than zero, then check all values reg(P1), reg(P1+1), +** reg(P1+2), ..., reg(P1+P3-1). */ -case OP_IsNull: { /* same as TK_ISNULL, no-push */ - if( pTos->flags & MEM_Null ){ - pc = pOp->p2-1; - if( pOp->p1<0 ){ - popStack(&pTos, -pOp->p1); +case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ + int n = pOp->p3; + assert( pOp->p3==0 || pOp->p1>0 ); + do{ + if( (pIn1->flags & MEM_Null)!=0 ){ + pc = pOp->p2 - 1; + break; } - } - if( pOp->p1>0 ){ - popStack(&pTos, pOp->p1); - } + pIn1++; + }while( --n > 0 ); break; } -/* Opcode: NotNull P1 P2 * +/* Opcode: NotNull P1 P2 * * * ** -** Jump to P2 if the top abs(P1) values on the stack are all not NULL. -** Regardless of whether or not the jump is taken, pop the stack -** P1 times if P1 is greater than zero. But if P1 is negative, -** leave the stack unchanged. +** Jump to P2 if the value in register P1 is not NULL. */ -case OP_NotNull: { /* same as TK_NOTNULL, no-push */ - int i, cnt; - cnt = pOp->p1; - if( cnt<0 ) cnt = -cnt; - assert( &pTos[1-cnt] >= p->aStack ); - for(i=0; i=cnt ) pc = pOp->p2-1; - if( pOp->p1>0 ) popStack(&pTos, cnt); +case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ + if( (pIn1->flags & MEM_Null)==0 ){ + pc = pOp->p2 - 1; + } break; } -/* Opcode: SetNumColumns P1 P2 * +/* Opcode: SetNumColumns P1 P2 * * * ** ** Before the OP_Column opcode can be executed on a cursor, this ** opcode must be called to set the number of fields in the table. @@ -39694,7 +42324,7 @@ case OP_NotNull: { /* same as TK_NOTNULL, no-push */ ** If OP_KeyAsData is to be applied to cursor P1, it must be executed ** before this op-code. */ -case OP_SetNumColumns: { /* no-push */ +case OP_SetNumColumns: { Cursor *pC; assert( (pOp->p1)nCursor ); assert( p->apCsr[pOp->p1]!=0 ); @@ -39703,23 +42333,22 @@ case OP_SetNumColumns: { /* no-push */ break; } -/* Opcode: Column P1 P2 P3 +/* Opcode: Column P1 P2 P3 P4 * ** ** Interpret the data that cursor P1 points to as a structure built using ** the MakeRecord instruction. (See the MakeRecord opcode for additional -** information about the format of the data.) Push onto the stack the value -** of the P2-th column contained in the data. If there are less that (P2+1) -** values in the record, push a NULL onto the stack. +** information about the format of the data.) Extract the P2-th column +** from this record. If there are less that (P2+1) +** values in the record, extract a NULL. +** +** The value extracted is stored in register P3. ** ** If the KeyAsData opcode has previously executed on this cursor, then the ** field might be extracted from the key rather than the data. ** -** If the column contains fewer than P2 fields, then push a NULL. Or -** if P3 is of type P3_MEM, then push the P3 value. The P3 value will -** be default value for a column that has been added using the ALTER TABLE -** ADD COLUMN command. If P3 is an ordinary string, just push a NULL. -** When P3 is a string it is really just a comment describing the value -** to be pushed, not a default value. +** If the column contains fewer than P2 fields, then extract a NULL. Or, +** if the P4 argument is a P4_MEM use the value of the P4 argument as +** the result. */ case OP_Column: { u32 payloadSize; /* Number of bytes in the record */ @@ -39734,12 +42363,15 @@ case OP_Column: { int len; /* The length of the serialized data for the column */ int i; /* Loop counter */ char *zData; /* Part of the record being decoded */ + Mem *pDest; /* Where to write the extracted value */ Mem sMem; /* For storing the record being decoded */ sMem.flags = 0; + sMem.db = 0; assert( p1nCursor ); - pTos++; - pTos->flags = MEM_Null; + assert( pOp->p3>0 && pOp->p3<=p->nMem ); + pDest = &p->aMem[pOp->p3]; + MemSetTypeFlag(pDest, MEM_Null); /* This block sets the variable payloadSize to be the total number of ** bytes in the record. @@ -39751,9 +42383,7 @@ case OP_Column: { ** If the data is unavailable, zRec is set to NULL. ** ** We also compute the number of columns in the record. For cursors, - ** the number of columns is stored in the Cursor.nField element. For - ** records on the stack, the next entry down on the stack is an integer - ** which is the number of records. + ** the number of columns is stored in the Cursor.nField element. */ pC = p->apCsr[p1]; assert( pC!=0 ); @@ -39779,7 +42409,8 @@ case OP_Column: { sqlite3BtreeDataSize(pCrsr, &payloadSize); } nField = pC->nField; - }else if( pC->pseudoTable ){ + }else{ + assert( pC->pseudoTable ); /* The record is the sole entry of a pseudo-table */ payloadSize = pC->nData; zRec = pC->pData; @@ -39787,17 +42418,12 @@ case OP_Column: { assert( payloadSize==0 || zRec!=0 ); nField = pC->nField; pCrsr = 0; - }else{ - zRec = 0; - payloadSize = 0; - pCrsr = 0; - nField = 0; } - /* If payloadSize is 0, then just push a NULL onto the stack. */ + /* If payloadSize is 0, then just store a NULL */ if( payloadSize==0 ){ - assert( pTos->flags==MEM_Null ); - break; + assert( pDest->flags&MEM_Null ); + goto op_column_out; } if( payloadSize>SQLITE_MAX_LENGTH ){ goto too_big; @@ -39808,7 +42434,7 @@ case OP_Column: { /* Read and parse the table header. Store the results of the parse ** into the record header cache fields of the cursor. */ - if( pC && pC->cacheStatus==p->cacheCtr ){ + if( pC->cacheStatus==p->cacheCtr ){ aType = pC->aType; aOffset = pC->aOffset; }else{ @@ -39862,6 +42488,8 @@ case OP_Column: { ** acquire the complete header text. */ if( !zRec && availisIndex, &sMem); if( rc!=SQLITE_OK ){ goto op_column_out; @@ -39885,8 +42513,8 @@ case OP_Column: { /* If i is less that nField, then there are less fields in this ** record than SetNumColumns indicated there are columns in the ** table. Set the offset for any extra columns not present in - ** the record to 0. This tells code below to push a NULL onto the - ** stack instead of deserializing a value from the record. + ** the record to 0. This tells code below to store a NULL + ** instead of deserializing a value from the record. */ aOffset[i] = 0; } @@ -39907,56 +42535,64 @@ case OP_Column: { /* Get the column information. If aOffset[p2] is non-zero, then ** deserialize the value from the record. If aOffset[p2] is zero, ** then there are not enough fields in the record to satisfy the - ** request. In this case, set the value NULL or to P3 if P3 is + ** request. In this case, set the value NULL or to P4 if P4 is ** a pointer to a Mem object. */ if( aOffset[p2] ){ assert( rc==SQLITE_OK ); if( zRec ){ - zData = &zRec[aOffset[p2]]; + if( pDest->flags&MEM_Dyn ){ + sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], &sMem); + sMem.db = db; + sqlite3VdbeMemCopy(pDest, &sMem); + assert( !(sMem.flags&MEM_Dyn) ); + }else{ + sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest); + } }else{ len = sqlite3VdbeSerialTypeLen(aType[p2]); + sqlite3VdbeMemMove(&sMem, pDest); rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem); if( rc!=SQLITE_OK ){ goto op_column_out; } zData = sMem.z; + sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest); } - sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos); - pTos->enc = encoding; + pDest->enc = encoding; }else{ - if( pOp->p3type==P3_MEM ){ - sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static); + if( pOp->p4type==P4_MEM ){ + sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); }else{ - pTos->flags = MEM_Null; + assert( pDest->flags&MEM_Null ); } } /* If we dynamically allocated space to hold the data (in the ** sqlite3VdbeMemFromBtree() call above) then transfer control of that - ** dynamically allocated space over to the pTos structure. + ** dynamically allocated space over to the pDest structure. ** This prevents a memory copy. */ if( (sMem.flags & MEM_Dyn)!=0 ){ - assert( pTos->flags & MEM_Ephem ); - assert( pTos->flags & (MEM_Str|MEM_Blob) ); - assert( pTos->z==sMem.z ); - assert( sMem.flags & MEM_Term ); - pTos->flags &= ~MEM_Ephem; - pTos->flags |= MEM_Dyn|MEM_Term; + assert( !sMem.xDel ); + assert( !(pDest->flags & MEM_Dyn) ); + assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); + pDest->flags &= ~(MEM_Ephem|MEM_Static); + pDest->flags |= MEM_Dyn|MEM_Term; + pDest->z = sMem.z; } - /* pTos->z might be pointing to sMem.zShort[]. Fix that so that we - ** can abandon sMem */ - rc = sqlite3VdbeMemMakeWriteable(pTos); + rc = sqlite3VdbeMemMakeWriteable(pDest); op_column_out: + UPDATE_MAX_BLOBSIZE(pDest); + REGISTER_TRACE(pOp->p3, pDest); break; } -/* Opcode: MakeRecord P1 P2 P3 +/* Opcode: MakeRecord P1 P2 P3 P4 * ** -** Convert the top abs(P1) entries of the stack into a single entry +** Convert P2 registers beginning with P1 into a single entry ** suitable for use as a data record in a database table or as a key ** in an index. The details of the format are irrelavant as long as ** the OP_Column opcode can decode the record later and as long as the @@ -39964,33 +42600,15 @@ op_column_out: ** records. Refer to source code comments for the details of the record ** format. ** -** The original stack entries are popped from the stack if P1>0 but -** remain on the stack if P1<0. -** -** If P2 is not zero and one or more of the entries are NULL, then jump -** to the address given by P2. This feature can be used to skip a -** uniqueness test on indices. -** -** P3 may be a string that is P1 characters long. The nth character of the +** P4 may be a string that is P1 characters long. The nth character of the ** string indicates the column affinity that should be used for the nth -** field of the index key (i.e. the first character of P3 corresponds to the -** lowest element on the stack). +** field of the index key. ** ** The mapping from character to affinity is given by the SQLITE_AFF_ ** macros defined in sqliteInt.h. ** -** If P3 is NULL then all index fields have the affinity NONE. -** -** See also OP_MakeIdxRec -*/ -/* Opcode: MakeIdxRec P1 P2 P3 -** -** This opcode works just OP_MakeRecord except that it reads an extra -** integer from the stack (thus reading a total of abs(P1+1) entries) -** and appends that extra integer to the end of the record as a varint. -** This results in an index key. +** If P4 is NULL then all index fields have the affinity NONE. */ -case OP_MakeIdxRec: case OP_MakeRecord: { /* Assuming the record contains N fields, the record format looks ** like this: @@ -39999,8 +42617,8 @@ case OP_MakeRecord: { ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | ** ------------------------------------------------------------------------ ** - ** Data(0) is taken from the lowest element of the stack and data(N-1) is - ** the top of the stack. + ** Data(0) is taken from register P1. Data(1) comes from register P1+1 + ** and so froth. ** ** Each type field is a varint representing the serial type of the ** corresponding data element (see sqlite3VdbeSerialType()). The @@ -40009,48 +42627,37 @@ case OP_MakeRecord: { */ u8 *zNewRecord; /* A buffer to hold the data for the new record */ Mem *pRec; /* The new record */ - Mem *pRowid = 0; /* Rowid appended to the new record */ u64 nData = 0; /* Number of bytes of data space */ int nHdr = 0; /* Number of bytes of header space */ u64 nByte = 0; /* Data space required for this record */ int nZero = 0; /* Number of zero bytes at the end of the record */ int nVarint; /* Number of bytes in a varint */ u32 serial_type; /* Type field */ - int containsNull = 0; /* True if any of the data fields are NULL */ - Mem *pData0; /* Bottom of the stack */ - int leaveOnStack; /* If true, leave the entries on the stack */ + Mem *pData0; /* First field to be combined into the record */ + Mem *pLast; /* Last field of the record */ int nField; /* Number of fields in the record */ - int jumpIfNull; /* Jump here if non-zero and any entries are NULL. */ - int addRowid; /* True to append a rowid column at the end */ char *zAffinity; /* The affinity string for the record */ int file_format; /* File format to use for encoding */ int i; /* Space used in zNewRecord[] */ - char zTemp[NBFS]; /* Space to hold small records */ - leaveOnStack = ((pOp->p1<0)?1:0); - nField = pOp->p1 * (leaveOnStack?-1:1); - jumpIfNull = pOp->p2; - addRowid = pOp->opcode==OP_MakeIdxRec; - zAffinity = pOp->p3; - - pData0 = &pTos[1-nField]; - assert( pData0>=p->aStack ); - containsNull = 0; + nField = pOp->p1; + zAffinity = pOp->p4.z; + assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem ); + pData0 = &p->aMem[nField]; + nField = pOp->p2; + pLast = &pData0[nField-1]; file_format = p->minWriteFileFormat; /* Loop through the elements that will make up the record to figure ** out how much space is required for the new record. */ - for(pRec=pData0; pRec<=pTos; pRec++){ + for(pRec=pData0; pRec<=pLast; pRec++){ int len; if( zAffinity ){ applyAffinity(pRec, zAffinity[pRec-pData0], encoding); } - if( pRec->flags&MEM_Null ){ - containsNull = 1; - } if( pRec->flags&MEM_Zero && pRec->n>0 ){ - ExpandBlob(pRec); + sqlite3VdbeMemExpandBlob(pRec); } serial_type = sqlite3VdbeSerialType(pRec, file_format); len = sqlite3VdbeSerialTypeLen(serial_type); @@ -40065,20 +42672,6 @@ case OP_MakeRecord: { } } - /* If we have to append a varint rowid to this record, set pRowid - ** to the value of the rowid and increase nByte by the amount of space - ** required to store it. - */ - if( addRowid ){ - pRowid = &pTos[0-nField]; - assert( pRowid>=p->aStack ); - sqlite3VdbeMemIntegerify(pRowid); - serial_type = sqlite3VdbeSerialType(pRowid, 0); - nData += sqlite3VdbeSerialTypeLen(serial_type); - nHdr += sqlite3VarintLen(serial_type); - nZero = 0; - } - /* Add the initial header varint and total the size */ nHdr += nVarint = sqlite3VarintLen(nHdr); if( nVarintsizeof(zTemp) ){ - zNewRecord = sqlite3DbMallocRaw(db, nByte); - if( !zNewRecord ){ - goto no_mem; - } - }else{ - zNewRecord = (u8*)zTemp; + /* Make sure the output register has a buffer large enough to store + ** the new record. The output register (pOp->p3) is not allowed to + ** be one of the input registers (because the following call to + ** sqlite3VdbeMemGrow() could clobber the value before it is used). + */ + assert( pOp->p3p1 || pOp->p3>=pOp->p1+pOp->p2 ); + pOut = &p->aMem[pOp->p3]; + if( sqlite3VdbeMemGrow(pOut, nByte, 0) ){ + goto no_mem; } + zNewRecord = (u8 *)pOut->z; /* Write the record */ i = sqlite3PutVarint(zNewRecord, nHdr); - for(pRec=pData0; pRec<=pTos; pRec++){ + for(pRec=pData0; pRec<=pLast; pRec++){ serial_type = sqlite3VdbeSerialType(pRec, file_format); i += sqlite3PutVarint(&zNewRecord[i], serial_type); /* serial type */ } - if( addRowid ){ - i += sqlite3PutVarint(&zNewRecord[i], sqlite3VdbeSerialType(pRowid, 0)); - } - for(pRec=pData0; pRec<=pTos; pRec++){ /* serial data */ + for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */ i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format); } - if( addRowid ){ - i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRowid, 0); - } assert( i==nByte ); - /* Pop entries off the stack if required. Push the new record on. */ - if( !leaveOnStack ){ - popStack(&pTos, nField+addRowid); - } - pTos++; - pTos->n = nByte; - if( nByte<=sizeof(zTemp) ){ - assert( zNewRecord==(unsigned char *)zTemp ); - pTos->z = pTos->zShort; - memcpy(pTos->zShort, zTemp, nByte); - pTos->flags = MEM_Blob | MEM_Short; - }else{ - assert( zNewRecord!=(unsigned char *)zTemp ); - pTos->z = (char*)zNewRecord; - pTos->flags = MEM_Blob | MEM_Dyn; - pTos->xDel = 0; - } + assert( pOp->p3>0 && pOp->p3<=p->nMem ); + pOut->n = nByte; + pOut->flags = MEM_Blob | MEM_Dyn; + pOut->xDel = 0; if( nZero ){ - pTos->u.i = nZero; - pTos->flags |= MEM_Zero; - } - pTos->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ - - /* If a NULL was encountered and jumpIfNull is non-zero, take the jump. */ - if( jumpIfNull && containsNull ){ - pc = jumpIfNull - 1; + pOut->u.i = nZero; + pOut->flags |= MEM_Zero; } + pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ + REGISTER_TRACE(pOp->p3, pOut); + UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Statement P1 * * +/* Opcode: Statement P1 * * * * ** ** Begin an individual statement transaction which is part of a larger ** BEGIN..COMMIT transaction. This is needed so that the statement @@ -40158,11 +42731,13 @@ case OP_MakeRecord: { ** database file has an index of 0 and the file used for temporary tables ** has an index of 1. */ -case OP_Statement: { /* no-push */ - int i = pOp->p1; - Btree *pBt; - if( i>=0 && inDb && (pBt = db->aDb[i].pBt)!=0 - && (db->autoCommit==0 || db->activeVdbeCnt>1) ){ +case OP_Statement: { + if( db->autoCommit==0 || db->activeVdbeCnt>1 ){ + int i = pOp->p1; + Btree *pBt; + assert( i>=0 && inDb ); + assert( db->aDb[i].pBt!=0 ); + pBt = db->aDb[i].pBt; assert( sqlite3BtreeIsInTrans(pBt) ); assert( (p->btreeMask & (1<p1; u8 rollback = pOp->p2; @@ -40206,7 +42781,6 @@ case OP_AutoCommit: { /* no-push */ }else{ db->autoCommit = i; if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ - p->pTos = pTos; p->pc = pc; db->autoCommit = 1-i; p->rc = rc = SQLITE_BUSY; @@ -40230,7 +42804,7 @@ case OP_AutoCommit: { /* no-push */ break; } -/* Opcode: Transaction P1 P2 * +/* Opcode: Transaction P1 P2 * * * ** ** Begin a transaction. The transaction ends when a Commit or Rollback ** opcode is encountered. Depending on the ON CONFLICT setting, the @@ -40238,7 +42812,8 @@ case OP_AutoCommit: { /* no-push */ ** ** P1 is the index of the database file on which the transaction is ** started. Index 0 is the main database file and index 1 is the -** file used for temporary tables. +** file used for temporary tables. Indices of 2 or more are used for +** attached databases. ** ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is ** obtained on the database file when a write-transaction is started. No @@ -40250,7 +42825,7 @@ case OP_AutoCommit: { /* no-push */ ** ** If P2 is zero, then a read-lock is obtained on the database file. */ -case OP_Transaction: { /* no-push */ +case OP_Transaction: { int i = pOp->p1; Btree *pBt; @@ -40263,7 +42838,6 @@ case OP_Transaction: { /* no-push */ if( rc==SQLITE_BUSY ){ p->pc = pc; p->rc = rc = SQLITE_BUSY; - p->pTos = pTos; goto vdbe_return; } if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){ @@ -40273,16 +42847,16 @@ case OP_Transaction: { /* no-push */ break; } -/* Opcode: ReadCookie P1 P2 * +/* Opcode: ReadCookie P1 P2 P3 * * ** -** Read cookie number P2 from database P1 and push it onto the stack. -** P2==0 is the schema version. P2==1 is the database format. -** P2==2 is the recommended pager cache size, and so forth. P1==0 is +** Read cookie number P3 from database P1 and write it into register P2. +** P3==0 is the schema version. P3==1 is the database format. +** P3==2 is the recommended pager cache size, and so forth. P1==0 is ** the main database file and P1==1 is the database file used to store ** temporary tables. ** ** If P1 is negative, then this is a request to read the size of a -** databases free-list. P2 must be set to 1 in this case. The actual +** databases free-list. P3 must be set to 1 in this case. The actual ** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1 ** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp"). ** @@ -40290,12 +42864,12 @@ case OP_Transaction: { /* no-push */ ** must be started or there must be an open cursor) before ** executing this instruction. */ -case OP_ReadCookie: { +case OP_ReadCookie: { /* out2-prerelease */ int iMeta; int iDb = pOp->p1; - int iCookie = pOp->p2; + int iCookie = pOp->p3; - assert( pOp->p2p3aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta); - pTos++; - pTos->u.i = iMeta; - pTos->flags = MEM_Int; + pOut->u.i = iMeta; + MemSetTypeFlag(pOut, MEM_Int); break; } -/* Opcode: SetCookie P1 P2 * +/* Opcode: SetCookie P1 P2 P3 * * ** -** Write the top of the stack into cookie number P2 of database P1. +** Write the content of register P3 (interpreted as an integer) +** into cookie number P2 of database P1. ** P2==0 is the schema version. P2==1 is the database format. ** P2==2 is the recommended pager cache size, and so forth. P1==0 is ** the main database file and P1==1 is the database file used to store @@ -40327,27 +42901,24 @@ case OP_ReadCookie: { ** ** A transaction must be started before executing this opcode. */ -case OP_SetCookie: { /* no-push */ +case OP_SetCookie: { /* in3 */ Db *pDb; assert( pOp->p2p1>=0 && pOp->p1nDb ); assert( (p->btreeMask & (1<p1))!=0 ); pDb = &db->aDb[pOp->p1]; assert( pDb->pBt!=0 ); - assert( pTos>=p->aStack ); - sqlite3VdbeMemIntegerify(pTos); + sqlite3VdbeMemIntegerify(pIn3); /* See note about index shifting on OP_ReadCookie */ - rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pTos->u.i); + rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i); if( pOp->p2==0 ){ /* When the schema cookie changes, record the new cookie internally */ - pDb->pSchema->schema_cookie = pTos->u.i; + pDb->pSchema->schema_cookie = pIn3->u.i; db->flags |= SQLITE_InternChanges; }else if( pOp->p2==1 ){ /* Record changes in the file format */ - pDb->pSchema->file_format = pTos->u.i; + pDb->pSchema->file_format = pIn3->u.i; } - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos--; if( pOp->p1==1 ){ /* Invalidate all prepared statements whenever the TEMP database ** schema is changed. Ticket #1644 */ @@ -40372,7 +42943,7 @@ case OP_SetCookie: { /* no-push */ ** to be executed (to establish a read lock) before this opcode is ** invoked. */ -case OP_VerifyCookie: { /* no-push */ +case OP_VerifyCookie: { int iMeta; Btree *pBt; assert( pOp->p1>=0 && pOp->p1nDb ); @@ -40410,17 +42981,18 @@ case OP_VerifyCookie: { /* no-push */ break; } -/* Opcode: OpenRead P1 P2 P3 +/* Opcode: OpenRead P1 P2 P3 P4 P5 ** ** Open a read-only cursor for the database table whose root page is -** P2 in a database file. The database file is determined by an -** integer from the top of the stack. 0 means the main database and -** 1 means the database used for temporary tables. Give the new -** cursor an identifier of P1. The P1 values need not be contiguous -** but all P1 values should be small integers. It is an error for -** P1 to be negative. +** P2 in a database file. The database file is determined by P3. +** P3==0 means the main database, P3==1 means the database used for +** temporary tables, and P3>1 means used the corresponding attached +** database. Give the new cursor an identifier of P1. The P1 +** values need not be contiguous but all P1 values should be small integers. +** It is an error for P1 to be negative. ** -** If P2==0 then take the root page number from the next of the stack. +** If P5!=0 then use the content of register P2 as the root page, not +** the value of P2 itself. ** ** There will be a read lock on the database whenever there is an ** open cursor. If the database was unlocked prior to this instruction @@ -40431,19 +43003,20 @@ case OP_VerifyCookie: { /* no-push */ ** to get a read lock but fails, the script terminates with an ** SQLITE_BUSY error code. ** -** The P3 value is a pointer to a KeyInfo structure that defines the -** content and collating sequence of indices. P3 is NULL for cursors +** The P4 value is a pointer to a KeyInfo structure that defines the +** content and collating sequence of indices. P4 is NULL for cursors ** that are not pointing to indices. ** ** See also OpenWrite. */ -/* Opcode: OpenWrite P1 P2 P3 +/* Opcode: OpenWrite P1 P2 P3 P4 P5 ** ** Open a read/write cursor named P1 on the table or index whose root -** page is P2. If P2==0 then take the root page number from the stack. +** page is P2. Or if P5!=0 use the content of register P2 to find the +** root page. ** -** The P3 value is a pointer to a KeyInfo structure that defines the -** content and collating sequence of indices. P3 is NULL for cursors +** The P4 value is a pointer to a KeyInfo structure that defines the +** content and collating sequence of indices. P4 is NULL for cursors ** that are not pointing to indices. ** ** This instruction works just like OpenRead except that it opens the cursor @@ -40452,21 +43025,16 @@ case OP_VerifyCookie: { /* no-push */ ** ** See also OpenRead. */ -case OP_OpenRead: /* no-push */ -case OP_OpenWrite: { /* no-push */ +case OP_OpenRead: +case OP_OpenWrite: { int i = pOp->p1; int p2 = pOp->p2; + int iDb = pOp->p3; int wrFlag; Btree *pX; - int iDb; Cursor *pCur; Db *pDb; - assert( pTos>=p->aStack ); - sqlite3VdbeMemIntegerify(pTos); - iDb = pTos->u.i; - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos--; assert( iDb>=0 && iDbnDb ); assert( (p->btreeMask & (1<aDb[iDb]; @@ -40480,26 +43048,25 @@ case OP_OpenWrite: { /* no-push */ }else{ wrFlag = 0; } - if( p2<=0 ){ - assert( pTos>=p->aStack ); - sqlite3VdbeMemIntegerify(pTos); - p2 = pTos->u.i; - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos--; + if( pOp->p5 ){ + assert( p2>0 ); + assert( p2<=p->nMem ); + pIn2 = &p->aMem[p2]; + sqlite3VdbeMemIntegerify(pIn2); + p2 = pIn2->u.i; assert( p2>=2 ); } assert( i>=0 ); pCur = allocateCursor(p, i, iDb); if( pCur==0 ) goto no_mem; pCur->nullRow = 1; - if( pX==0 ) break; /* We always provide a key comparison function. If the table being ** opened is of type INTKEY, the comparision function will be ignored. */ rc = sqlite3BtreeCursor(pX, p2, wrFlag, - sqlite3VdbeRecordCompare, pOp->p3, + sqlite3VdbeRecordCompare, pOp->p4.p, &pCur->pCursor); - if( pOp->p3type==P3_KEYINFO ){ - pCur->pKeyInfo = (KeyInfo*)pOp->p3; + if( pOp->p4type==P4_KEYINFO ){ + pCur->pKeyInfo = pOp->p4.pKeyInfo; pCur->pIncrKey = &pCur->pKeyInfo->incrKey; pCur->pKeyInfo->enc = ENC(p->db); }else{ @@ -40510,7 +43077,6 @@ case OP_OpenWrite: { /* no-push */ case SQLITE_BUSY: { p->pc = pc; p->rc = rc = SQLITE_BUSY; - p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */ goto vdbe_return; } case SQLITE_OK: { @@ -40527,19 +43093,19 @@ case OP_OpenWrite: { /* no-push */ } pCur->isTable = (flags & BTREE_INTKEY)!=0; pCur->isIndex = (flags & BTREE_ZERODATA)!=0; - /* If P3==0 it means we are expected to open a table. If P3!=0 then + /* If P4==0 it means we are expected to open a table. If P4!=0 then ** we expect to be opening an index. If this is not what happened, ** then the database is corrupt */ - if( (pCur->isTable && pOp->p3type==P3_KEYINFO) - || (pCur->isIndex && pOp->p3type!=P3_KEYINFO) ){ + if( (pCur->isTable && pOp->p4type==P4_KEYINFO) + || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){ rc = SQLITE_CORRUPT_BKPT; goto abort_due_to_error; } break; } case SQLITE_EMPTY: { - pCur->isTable = pOp->p3type!=P3_KEYINFO; + pCur->isTable = pOp->p4type!=P4_KEYINFO; pCur->isIndex = !pCur->isTable; rc = SQLITE_OK; break; @@ -40551,7 +43117,7 @@ case OP_OpenWrite: { /* no-push */ break; } -/* Opcode: OpenEphemeral P1 P2 P3 +/* Opcode: OpenEphemeral P1 P2 * P4 * ** ** Open a new cursor P1 to a transient table. ** The cursor is always opened read/write even if @@ -40559,8 +43125,8 @@ case OP_OpenWrite: { /* no-push */ ** table is deleted automatically when the cursor is closed. ** ** P2 is the number of columns in the virtual table. -** The cursor points to a BTree table if P3==0 and to a BTree index -** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure +** The cursor points to a BTree table if P4==0 and to a BTree index +** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure ** that defines the format of keys in the index. ** ** This opcode was once called OpenTemp. But that created @@ -40569,7 +43135,7 @@ case OP_OpenWrite: { /* no-push */ ** this opcode. Then this opcode was call OpenVirtual. But ** that created confusion with the whole virtual-table idea. */ -case OP_OpenEphemeral: { /* no-push */ +case OP_OpenEphemeral: { int i = pOp->p1; Cursor *pCx; static const int openFlags = @@ -40594,15 +43160,15 @@ case OP_OpenEphemeral: { /* no-push */ ** opening it. If a transient table is required, just use the ** automatically created table with root-page 1 (an INTKEY table). */ - if( pOp->p3 ){ + if( pOp->p4.pKeyInfo ){ int pgno; - assert( pOp->p3type==P3_KEYINFO ); + assert( pOp->p4type==P4_KEYINFO ); rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); if( rc==SQLITE_OK ){ assert( pgno==MASTER_ROOT+1 ); rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare, - pOp->p3, &pCx->pCursor); - pCx->pKeyInfo = (KeyInfo*)pOp->p3; + pOp->p4.z, &pCx->pCursor); + pCx->pKeyInfo = pOp->p4.pKeyInfo; pCx->pKeyInfo->enc = ENC(p->db); pCx->pIncrKey = &pCx->pKeyInfo->incrKey; } @@ -40618,7 +43184,7 @@ case OP_OpenEphemeral: { /* no-push */ break; } -/* Opcode: OpenPseudo P1 * * +/* Opcode: OpenPseudo P1 * * * * ** ** Open a new cursor that points to a fake table that contains a single ** row of data. Any attempt to write a second row of data causes the @@ -40630,7 +43196,7 @@ case OP_OpenEphemeral: { /* no-push */ ** row output from the sorter so that the row can be decomposed into ** individual columns using the OP_Column opcode. */ -case OP_OpenPseudo: { /* no-push */ +case OP_OpenPseudo: { int i = pOp->p1; Cursor *pCx; assert( i>=0 ); @@ -40644,68 +43210,73 @@ case OP_OpenPseudo: { /* no-push */ break; } -/* Opcode: Close P1 * * +/* Opcode: Close P1 * * * * ** ** Close a cursor previously opened as P1. If P1 is not ** currently open, this instruction is a no-op. */ -case OP_Close: { /* no-push */ +case OP_Close: { int i = pOp->p1; - if( i>=0 && inCursor ){ - sqlite3VdbeFreeCursor(p, p->apCsr[i]); - p->apCsr[i] = 0; - } + assert( i>=0 && inCursor ); + sqlite3VdbeFreeCursor(p, p->apCsr[i]); + p->apCsr[i] = 0; break; } -/* Opcode: MoveGe P1 P2 * +/* Opcode: MoveGe P1 P2 P3 * * ** -** Pop the top of the stack and use its value as a key. Reposition +** Use the value in register P3 as a key. Reposition ** cursor P1 so that it points to the smallest entry that is greater -** than or equal to the key that was popped ffrom the stack. +** than or equal to the key in register P3. ** If there are no records greater than or equal to the key and P2 ** is not zero, then jump to P2. ** +** A special feature of this opcode (and different from the +** related OP_MoveGt, OP_MoveLt, and OP_MoveLe) is that if P2 is +** zero and P1 is an SQL table (a b-tree with integer keys) then +** the seek is deferred until it is actually needed. It might be +** the case that the cursor is never accessed. By deferring the +** seek, we avoid unnecessary seeks. +** ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe */ -/* Opcode: MoveGt P1 P2 * +/* Opcode: MoveGt P1 P2 P3 * * ** -** Pop the top of the stack and use its value as a key. Reposition +** Use the value in register P3 as a key. Reposition ** cursor P1 so that it points to the smallest entry that is greater -** than the key from the stack. -** If there are no records greater than the key and P2 is not zero, +** than the key in register P3. +** If there are no records greater than the key ** then jump to P2. ** ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe */ -/* Opcode: MoveLt P1 P2 * +/* Opcode: MoveLt P1 P2 P3 * * ** -** Pop the top of the stack and use its value as a key. Reposition +** Use the value in register P3 as a key. Reposition ** cursor P1 so that it points to the largest entry that is less -** than the key from the stack. -** If there are no records less than the key and P2 is not zero, +** than the key in register P3. +** If there are no records less than the key ** then jump to P2. ** ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe */ -/* Opcode: MoveLe P1 P2 * +/* Opcode: MoveLe P1 P2 P3 * * ** -** Pop the top of the stack and use its value as a key. Reposition +** Use the value in register P3 as a key. Reposition ** cursor P1 so that it points to the largest entry that is less than -** or equal to the key that was popped from the stack. -** If there are no records less than or eqal to the key and P2 is not zero, +** or equal to the key. +** If there are no records less than or eqal to the key ** then jump to P2. ** ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt */ -case OP_MoveLt: /* no-push */ -case OP_MoveLe: /* no-push */ -case OP_MoveGe: /* no-push */ -case OP_MoveGt: { /* no-push */ +case OP_MoveLt: /* jump, in3 */ +case OP_MoveLe: /* jump, in3 */ +case OP_MoveGe: /* jump, in3 */ +case OP_MoveGt: { /* jump, in3 */ int i = pOp->p1; Cursor *pC; - assert( pTos>=p->aStack ); assert( i>=0 && inCursor ); pC = p->apCsr[i]; assert( pC!=0 ); @@ -40715,26 +43286,24 @@ case OP_MoveGt: { /* no-push */ pC->nullRow = 0; *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe; if( pC->isTable ){ - i64 iKey; - sqlite3VdbeMemIntegerify(pTos); - iKey = intToKey(pTos->u.i); - if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){ + i64 iKey = sqlite3VdbeIntValue(pIn3); + if( pOp->p2==0 ){ + assert( pOp->opcode==OP_MoveGe ); pC->movetoTarget = iKey; + pC->rowidIsValid = 0; pC->deferredMoveto = 1; - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos--; break; } rc = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, 0, &res); if( rc!=SQLITE_OK ){ goto abort_due_to_error; } - pC->lastRowid = pTos->u.i; + pC->lastRowid = iKey; pC->rowidIsValid = res==0; }else{ - assert( pTos->flags & MEM_Blob ); - ExpandBlob(pTos); - rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res); + assert( pIn3->flags & MEM_Blob ); + ExpandBlob(pIn3); + rc = sqlite3BtreeMoveto(pC->pCursor, pIn3->z, pIn3->n, 0, &res); if( rc!=SQLITE_OK ){ goto abort_due_to_error; } @@ -40767,88 +43336,59 @@ case OP_MoveGt: { /* no-push */ res = sqlite3BtreeEof(pC->pCursor); } } + assert( pOp->p2>0 ); if( res ){ - if( pOp->p2>0 ){ - pc = pOp->p2 - 1; - }else{ - pC->nullRow = 1; - } + pc = pOp->p2 - 1; } } - Release(pTos); - pTos--; break; } -/* Opcode: Distinct P1 P2 * -** -** Use the top of the stack as a record created using MakeRecord. P1 is a -** cursor on a table that declared as an index. If that table contains an -** entry that matches the top of the stack fall thru. If the top of the stack -** matches no entry in P1 then jump to P2. +/* Opcode: Found P1 P2 P3 * * ** -** The cursor is left pointing at the matching entry if it exists. The -** record on the top of the stack is not popped. -** -** This instruction is similar to NotFound except that this operation -** does not pop the key from the stack. -** -** The instruction is used to implement the DISTINCT operator on SELECT -** statements. The P1 table is not a true index but rather a record of -** all results that have produced so far. -** -** See also: Found, NotFound, MoveTo, IsUnique, NotExists -*/ -/* Opcode: Found P1 P2 * -** -** Top of the stack holds a blob constructed by MakeRecord. P1 is an index. -** If an entry that matches the top of the stack exists in P1 then -** jump to P2. If the top of the stack does not match any entry in P1 +** Register P3 holds a blob constructed by MakeRecord. P1 is an index. +** If an entry that matches the value in register p3 exists in P1 then +** jump to P2. If the P3 value does not match any entry in P1 ** then fall thru. The P1 cursor is left pointing at the matching entry -** if it exists. The blob is popped off the top of the stack. +** if it exists. ** ** This instruction is used to implement the IN operator where the ** left-hand side is a SELECT statement. P1 may be a true index, or it ** may be a temporary index that holds the results of the SELECT -** statement. +** statement. This instruction is also used to implement the +** DISTINCT keyword in SELECT statements. ** ** This instruction checks if index P1 contains a record for which ** the first N serialised values exactly match the N serialised values -** in the record on the stack, where N is the total number of values in -** the stack record (stack record is a prefix of the P1 record). +** in the record in register P3, where N is the total number of values in +** the P3 record (the P3 record is a prefix of the P1 record). ** -** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists +** See also: NotFound, MoveTo, IsUnique, NotExists */ -/* Opcode: NotFound P1 P2 * +/* Opcode: NotFound P1 P2 P3 * * ** -** The top of the stack holds a blob constructed by MakeRecord. P1 is +** Register P3 holds a blob constructed by MakeRecord. P1 is ** an index. If no entry exists in P1 that matches the blob then jump ** to P2. If an entry does existing, fall through. The cursor is left -** pointing to the entry that matches. The blob is popped from the stack. +** pointing to the entry that matches. ** -** The difference between this operation and Distinct is that -** Distinct does not pop the key from the stack. -** -** See also: Distinct, Found, MoveTo, NotExists, IsUnique +** See also: Found, MoveTo, NotExists, IsUnique */ -case OP_Distinct: /* no-push */ -case OP_NotFound: /* no-push */ -case OP_Found: { /* no-push */ +case OP_NotFound: /* jump, in3 */ +case OP_Found: { /* jump, in3 */ int i = pOp->p1; int alreadyExists = 0; Cursor *pC; - assert( pTos>=p->aStack ); assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); if( (pC = p->apCsr[i])->pCursor!=0 ){ int res; assert( pC->isTable==0 ); - assert( pTos->flags & MEM_Blob ); - Stringify(pTos, encoding); + assert( pIn3->flags & MEM_Blob ); if( pOp->opcode==OP_Found ){ pC->pKeyInfo->prefixIsEqual = 1; } - rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res); + rc = sqlite3BtreeMoveto(pC->pCursor, pIn3->z, pIn3->n, 0, &res); pC->pKeyInfo->prefixIsEqual = 0; if( rc!=SQLITE_OK ){ break; @@ -40862,19 +43402,14 @@ case OP_Found: { /* no-push */ }else{ if( !alreadyExists ) pc = pOp->p2 - 1; } - if( pOp->opcode!=OP_Distinct ){ - Release(pTos); - pTos--; - } break; } -/* Opcode: IsUnique P1 P2 * +/* Opcode: IsUnique P1 P2 P3 P4 * ** -** The top of the stack is an integer record number. Call this -** record number R. The next on the stack is an index key created -** using MakeIdxRec. Call it K. This instruction pops R from the -** stack but it leaves K unchanged. +** The P3 register contains an integer record number. Call this +** record number R. The P4 register contains an index key created +** using MakeIdxRec. Call it K. ** ** P1 is an index. So it has no data and its key consists of a ** record generated by OP_MakeRecord where the last field is the @@ -40885,25 +43420,25 @@ case OP_Found: { /* no-push */ ** If there is no such entry, then there is an immediate ** jump to P2. If any entry does exist where the index string ** matches K but the record number is not R, then the record -** number for that entry is pushed onto the stack and control +** number for that entry is written into P3 and control ** falls through to the next instruction. ** -** See also: Distinct, NotFound, NotExists, Found +** See also: NotFound, NotExists, Found */ -case OP_IsUnique: { /* no-push */ +case OP_IsUnique: { /* jump, in3 */ int i = pOp->p1; - Mem *pNos = &pTos[-1]; Cursor *pCx; BtCursor *pCrsr; + Mem *pK; i64 R; /* Pop the value R off the top of the stack */ - assert( pNos>=p->aStack ); - sqlite3VdbeMemIntegerify(pTos); - R = pTos->u.i; - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos--; + assert( pOp->p4type==P4_INT32 ); + assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem ); + pK = &p->aMem[pOp->p4.i]; + sqlite3VdbeMemIntegerify(pIn3); + R = pIn3->u.i; assert( i>=0 && inCursor ); pCx = p->apCsr[i]; assert( pCx!=0 ); @@ -40918,10 +43453,9 @@ case OP_IsUnique: { /* no-push */ /* Make sure K is a string and make zKey point to K */ - assert( pNos->flags & MEM_Blob ); - Stringify(pNos, encoding); - zKey = pNos->z; - nKey = pNos->n; + assert( pK->flags & MEM_Blob ); + zKey = pK->z; + nKey = pK->n; szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey); len = nKey-szRowid; @@ -40963,46 +43497,44 @@ case OP_IsUnique: { /* no-push */ break; } - /* The final varint of the key is different from R. Push it onto - ** the stack. (The record number of an entry that violates a UNIQUE - ** constraint.) + /* The final varint of the key is different from R. Store it back + ** into register R3. (The record number of an entry that violates + ** a UNIQUE constraint.) */ - pTos++; - pTos->u.i = v; - pTos->flags = MEM_Int; + pIn3->u.i = v; + assert( pIn3->flags&MEM_Int ); } break; } -/* Opcode: NotExists P1 P2 * +/* Opcode: NotExists P1 P2 P3 * * ** -** Use the top of the stack as a integer key. If a record with that key -** does not exist in table of P1, then jump to P2. If the record -** does exist, then fall thru. The cursor is left pointing to the -** record if it exists. The integer key is popped from the stack. +** Use the content of register P3 as a integer key. If a record +** with that key does not exist in table of P1, then jump to P2. +** If the record does exist, then fall thru. The cursor is left +** pointing to the record if it exists. ** ** The difference between this operation and NotFound is that this ** operation assumes the key is an integer and that P1 is a table whereas ** NotFound assumes key is a blob constructed from MakeRecord and ** P1 is an index. ** -** See also: Distinct, Found, MoveTo, NotFound, IsUnique +** See also: Found, MoveTo, NotFound, IsUnique */ -case OP_NotExists: { /* no-push */ +case OP_NotExists: { /* jump, in3 */ int i = pOp->p1; Cursor *pC; BtCursor *pCrsr; - assert( pTos>=p->aStack ); assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ int res; u64 iKey; - assert( pTos->flags & MEM_Int ); + assert( pIn3->flags & MEM_Int ); assert( p->apCsr[i]->isTable ); - iKey = intToKey(pTos->u.i); + iKey = intToKey(pIn3->u.i); rc = sqlite3BtreeMoveto(pCrsr, 0, iKey, 0,&res); - pC->lastRowid = pTos->u.i; + pC->lastRowid = pIn3->u.i; pC->rowidIsValid = res==0; pC->nullRow = 0; pC->cacheStatus = CACHE_STALE; @@ -41013,47 +43545,44 @@ case OP_NotExists: { /* no-push */ ** running ioerr.test and similar failure-recovery test scripts.) */ if( res!=0 ){ pc = pOp->p2 - 1; - pC->rowidIsValid = 0; + assert( pC->rowidIsValid==0 ); } } - Release(pTos); - pTos--; break; } -/* Opcode: Sequence P1 * * +/* Opcode: Sequence P1 P2 * * * ** -** Push an integer onto the stack which is the next available -** sequence number for cursor P1. The sequence number on the -** cursor is incremented after the push. +** Find the next available sequence number for cursor P1. +** Write the sequence number into register P2. +** The sequence number on the cursor is incremented after this +** instruction. */ -case OP_Sequence: { +case OP_Sequence: { /* out2-prerelease */ int i = pOp->p1; - assert( pTos>=p->aStack ); assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); - pTos++; - pTos->u.i = p->apCsr[i]->seqCount++; - pTos->flags = MEM_Int; + pOut->u.i = p->apCsr[i]->seqCount++; + MemSetTypeFlag(pOut, MEM_Int); break; } -/* Opcode: NewRowid P1 P2 * +/* Opcode: NewRowid P1 P2 P3 * * ** ** Get a new integer record number (a.k.a "rowid") used as the key to a table. ** The record number is not previously used as a key in the database -** table that cursor P1 points to. The new record number is pushed -** onto the stack. +** table that cursor P1 points to. The new record number is written +** written to register P2. ** -** If P2>0 then P2 is a memory cell that holds the largest previously +** If P3>0 then P3 is a register that holds the largest previously ** generated record number. No new record numbers are allowed to be less ** than this value. When this value reaches its maximum, a SQLITE_FULL -** error is generated. The P2 memory cell is updated with the generated -** record number. This P2 mechanism is used to help implement the +** error is generated. The P3 register is updated with the generated +** record number. This P3 mechanism is used to help implement the ** AUTOINCREMENT feature. */ -case OP_NewRowid: { +case OP_NewRowid: { /* out2-prerelease */ int i = pOp->p1; i64 v = 0; Cursor *pC; @@ -41136,12 +43665,13 @@ case OP_NewRowid: { } #ifndef SQLITE_OMIT_AUTOINCREMENT - if( pOp->p2 ){ + if( pOp->p3 ){ Mem *pMem; - assert( pOp->p2>0 && pOp->p2nMem ); /* P2 is a valid memory cell */ - pMem = &p->aMem[pOp->p2]; + assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */ + pMem = &p->aMem[pOp->p3]; + REGISTER_TRACE(pOp->p3, pMem); sqlite3VdbeMemIntegerify(pMem); - assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */ + assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ rc = SQLITE_FULL; goto abort_due_to_error; @@ -41161,23 +43691,21 @@ case OP_NewRowid: { } } if( pC->useRandomRowid ){ - assert( pOp->p2==0 ); /* SQLITE_FULL must have occurred prior to this */ + assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */ v = db->priorNewRowid; cnt = 0; do{ - if( v==0 || cnt>2 ){ + if( cnt==0 && (v&0xffffff)==v ){ + v++; + }else{ sqlite3Randomness(sizeof(v), &v); if( cnt<5 ) v &= 0xffffff; - }else{ - unsigned char r; - sqlite3Randomness(1, &r); - v += r + 1; } if( v==0 ) continue; x = intToKey(v); rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, 0, &res); cnt++; - }while( cnt<1000 && rx==SQLITE_OK && res==0 ); + }while( cnt<100 && rx==SQLITE_OK && res==0 ); db->priorNewRowid = v; if( rx==SQLITE_OK && res==0 ){ rc = SQLITE_FULL; @@ -41188,104 +43716,110 @@ case OP_NewRowid: { pC->deferredMoveto = 0; pC->cacheStatus = CACHE_STALE; } - pTos++; - pTos->u.i = v; - pTos->flags = MEM_Int; + MemSetTypeFlag(pOut, MEM_Int); + pOut->u.i = v; break; } -/* Opcode: Insert P1 P2 P3 +/* Opcode: Insert P1 P2 P3 P4 P5 ** ** Write an entry into the table of cursor P1. A new entry is ** created if it doesn't already exist or the data for an existing -** entry is overwritten. The data is the value on the top of the -** stack. The key is the next value down on the stack. The key must -** be an integer. The stack is popped twice by this instruction. +** entry is overwritten. The data is the value stored register +** number P2. The key is stored in register P3. The key must +** be an integer. ** -** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is -** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P2 is set, +** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is +** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, ** then rowid is stored for subsequent return by the ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). ** -** Parameter P3 may point to a string containing the table-name, or +** Parameter P4 may point to a string containing the table-name, or ** may be NULL. If it is not NULL, then the update-hook ** (sqlite3.xUpdateCallback) is invoked following a successful insert. ** +** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically +** allocated, then ownership of P2 is transferred to the pseudo-cursor +** and register P2 becomes ephemeral. If the cursor is changed, the +** value of register P2 will then change. Make sure this does not +** cause any problems.) +** ** This instruction only works on tables. The equivalent instruction ** for indices is OP_IdxInsert. */ -case OP_Insert: { /* no-push */ - Mem *pNos = &pTos[-1]; +case OP_Insert: { + Mem *pData = &p->aMem[pOp->p2]; + Mem *pKey = &p->aMem[pOp->p3]; + + i64 iKey; /* The integer ROWID or key for the record to be inserted */ int i = pOp->p1; Cursor *pC; - assert( pNos>=p->aStack ); assert( i>=0 && inCursor ); - assert( p->apCsr[i]!=0 ); - if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){ - i64 iKey; /* The integer ROWID or key for the record to be inserted */ - - assert( pNos->flags & MEM_Int ); - assert( pC->isTable ); - iKey = intToKey(pNos->u.i); - - if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; - if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->u.i; - if( pC->nextRowidValid && pNos->u.i>=pC->nextRowid ){ - pC->nextRowidValid = 0; - } - if( pTos->flags & MEM_Null ){ - pTos->z = 0; - pTos->n = 0; - }else{ - assert( pTos->flags & (MEM_Blob|MEM_Str) ); - } - if( pC->pseudoTable ){ - sqlite3_free(pC->pData); - pC->iKey = iKey; - pC->nData = pTos->n; - if( pTos->flags & MEM_Dyn ){ - pC->pData = pTos->z; - pTos->flags = MEM_Null; - }else{ - pC->pData = sqlite3_malloc( pC->nData+2 ); - if( !pC->pData ) goto no_mem; - memcpy(pC->pData, pTos->z, pC->nData); - pC->pData[pC->nData] = 0; - pC->pData[pC->nData+1] = 0; - } - pC->nullRow = 0; + pC = p->apCsr[i]; + assert( pC!=0 ); + assert( pC->pCursor!=0 || pC->pseudoTable ); + assert( pKey->flags & MEM_Int ); + assert( pC->isTable ); + REGISTER_TRACE(pOp->p2, pData); + REGISTER_TRACE(pOp->p3, pKey); + + iKey = intToKey(pKey->u.i); + if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; + if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i; + if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){ + pC->nextRowidValid = 0; + } + if( pData->flags & MEM_Null ){ + pData->z = 0; + pData->n = 0; + }else{ + assert( pData->flags & (MEM_Blob|MEM_Str) ); + } + if( pC->pseudoTable ){ + sqlite3_free(pC->pData); + pC->iKey = iKey; + pC->nData = pData->n; + if( pData->flags & MEM_Dyn ){ + pC->pData = pData->z; + pData->flags &= ~MEM_Dyn; + pData->flags |= MEM_Ephem; }else{ - int nZero; - if( pTos->flags & MEM_Zero ){ - nZero = pTos->u.i; - }else{ - nZero = 0; - } - rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, - pTos->z, pTos->n, nZero, - pOp->p2 & OPFLAG_APPEND); + pC->pData = sqlite3_malloc( pC->nData+2 ); + if( !pC->pData ) goto no_mem; + memcpy(pC->pData, pData->z, pC->nData); + pC->pData[pC->nData] = 0; + pC->pData[pC->nData+1] = 0; } - - pC->rowidIsValid = 0; - pC->deferredMoveto = 0; - pC->cacheStatus = CACHE_STALE; - - /* Invoke the update-hook if required. */ - if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){ - const char *zDb = db->aDb[pC->iDb].zName; - const char *zTbl = pOp->p3; - int op = ((pOp->p2 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); - assert( pC->isTable ); - db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); - assert( pC->iDb>=0 ); + pC->nullRow = 0; + }else{ + int nZero; + if( pData->flags & MEM_Zero ){ + nZero = pData->u.i; + }else{ + nZero = 0; } + rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, + pData->z, pData->n, nZero, + pOp->p5 & OPFLAG_APPEND); + } + + pC->rowidIsValid = 0; + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + + /* Invoke the update-hook if required. */ + if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ + const char *zDb = db->aDb[pC->iDb].zName; + const char *zTbl = pOp->p4.z; + int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); + assert( pC->isTable ); + db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); + assert( pC->iDb>=0 ); } - popStack(&pTos, 2); - break; } -/* Opcode: Delete P1 P2 P3 +/* Opcode: Delete P1 P2 * P4 * ** ** Delete the record at which the P1 cursor is currently pointing. ** @@ -41297,46 +43831,45 @@ case OP_Insert: { /* no-push */ ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is ** incremented (otherwise not). ** -** If P1 is a pseudo-table, then this instruction is a no-op. +** P1 must not be pseudo-table. It has to be a real table with +** multiple rows. +** +** If P4 is not NULL, then it is the name of the table that P1 is +** pointing to. The update hook will be invoked, if it exists. +** If P4 is not NULL then the P1 cursor must have been positioned +** using OP_NotFound prior to invoking this opcode. */ -case OP_Delete: { /* no-push */ +case OP_Delete: { int i = pOp->p1; + i64 iKey; Cursor *pC; + assert( i>=0 && inCursor ); pC = p->apCsr[i]; assert( pC!=0 ); - if( pC->pCursor!=0 ){ - i64 iKey; + assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ - /* If the update-hook will be invoked, set iKey to the rowid of the - ** row being deleted. - */ - if( db->xUpdateCallback && pOp->p3 ){ - assert( pC->isTable ); - if( pC->rowidIsValid ){ - iKey = pC->lastRowid; - }else{ - rc = sqlite3BtreeKeySize(pC->pCursor, &iKey); - if( rc ){ - goto abort_due_to_error; - } - iKey = keyToInt(iKey); - } - } + /* If the update-hook will be invoked, set iKey to the rowid of the + ** row being deleted. + */ + if( db->xUpdateCallback && pOp->p4.z ){ + assert( pC->isTable ); + assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */ + iKey = pC->lastRowid; + } - rc = sqlite3VdbeCursorMoveto(pC); - if( rc ) goto abort_due_to_error; - rc = sqlite3BtreeDelete(pC->pCursor); - pC->nextRowidValid = 0; - pC->cacheStatus = CACHE_STALE; + rc = sqlite3VdbeCursorMoveto(pC); + if( rc ) goto abort_due_to_error; + rc = sqlite3BtreeDelete(pC->pCursor); + pC->nextRowidValid = 0; + pC->cacheStatus = CACHE_STALE; - /* Invoke the update-hook if required. */ - if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){ - const char *zDb = db->aDb[pC->iDb].zName; - const char *zTbl = pOp->p3; - db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); - assert( pC->iDb>=0 ); - } + /* Invoke the update-hook if required. */ + if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ + const char *zDb = db->aDb[pC->iDb].zName; + const char *zTbl = pOp->p4.z; + db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); + assert( pC->iDb>=0 ); } if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; break; @@ -41349,7 +43882,7 @@ case OP_Delete: { /* no-push */ ** change counter (returned by subsequent calls to sqlite3_changes()) ** before it is reset. This is used by trigger programs. */ -case OP_ResetCount: { /* no-push */ +case OP_ResetCount: { if( pOp->p1 ){ sqlite3VdbeSetChanges(db, p->nChange); } @@ -41357,94 +43890,82 @@ case OP_ResetCount: { /* no-push */ break; } -/* Opcode: RowData P1 * * +/* Opcode: RowData P1 P2 * * * ** -** Push onto the stack the complete row data for cursor P1. -** There is no interpretation of the data. It is just copied -** onto the stack exactly as it is found in the database file. +** Write into register P2 the complete row data for cursor P1. +** There is no interpretation of the data. +** It is just copied onto the P2 register exactly as +** it is found in the database file. ** -** If the cursor is not pointing to a valid row, a NULL is pushed -** onto the stack. +** If the P1 cursor must be pointing to a valid row (not a NULL row) +** of a real table, not a pseudo-table. */ -/* Opcode: RowKey P1 * * +/* Opcode: RowKey P1 P2 * * * ** -** Push onto the stack the complete row key for cursor P1. -** There is no interpretation of the key. It is just copied -** onto the stack exactly as it is found in the database file. +** Write into register P2 the complete row key for cursor P1. +** There is no interpretation of the data. +** The key is copied onto the P3 register exactly as +** it is found in the database file. ** -** If the cursor is not pointing to a valid row, a NULL is pushed -** onto the stack. +** If the P1 cursor must be pointing to a valid row (not a NULL row) +** of a real table, not a pseudo-table. */ case OP_RowKey: case OP_RowData: { int i = pOp->p1; Cursor *pC; + BtCursor *pCrsr; u32 n; + pOut = &p->aMem[pOp->p2]; + /* Note that RowKey and RowData are really exactly the same instruction */ - pTos++; assert( i>=0 && inCursor ); pC = p->apCsr[i]; assert( pC->isTable || pOp->opcode==OP_RowKey ); assert( pC->isIndex || pOp->opcode==OP_RowData ); assert( pC!=0 ); - if( pC->nullRow ){ - pTos->flags = MEM_Null; - }else if( pC->pCursor!=0 ){ - BtCursor *pCrsr = pC->pCursor; - rc = sqlite3VdbeCursorMoveto(pC); - if( rc ) goto abort_due_to_error; - if( pC->nullRow ){ - pTos->flags = MEM_Null; - break; - }else if( pC->isIndex ){ - i64 n64; - assert( !pC->isTable ); - sqlite3BtreeKeySize(pCrsr, &n64); - if( n64>SQLITE_MAX_LENGTH ){ - goto too_big; - } - n = n64; - }else{ - sqlite3BtreeDataSize(pCrsr, &n); + assert( pC->nullRow==0 ); + assert( pC->pseudoTable==0 ); + assert( pC->pCursor!=0 ); + pCrsr = pC->pCursor; + rc = sqlite3VdbeCursorMoveto(pC); + if( rc ) goto abort_due_to_error; + if( pC->isIndex ){ + i64 n64; + assert( !pC->isTable ); + sqlite3BtreeKeySize(pCrsr, &n64); + if( n64>SQLITE_MAX_LENGTH ){ + goto too_big; } + n = n64; + }else{ + sqlite3BtreeDataSize(pCrsr, &n); if( n>SQLITE_MAX_LENGTH ){ goto too_big; } - pTos->n = n; - if( n<=NBFS ){ - pTos->flags = MEM_Blob | MEM_Short; - pTos->z = pTos->zShort; - }else{ - char *z = sqlite3_malloc( n ); - if( z==0 ) goto no_mem; - pTos->flags = MEM_Blob | MEM_Dyn; - pTos->xDel = 0; - pTos->z = z; - } - if( pC->isIndex ){ - rc = sqlite3BtreeKey(pCrsr, 0, n, pTos->z); - }else{ - rc = sqlite3BtreeData(pCrsr, 0, n, pTos->z); - } - }else if( pC->pseudoTable ){ - pTos->n = pC->nData; - assert( pC->nData<=SQLITE_MAX_LENGTH ); - pTos->z = pC->pData; - pTos->flags = MEM_Blob|MEM_Ephem; + } + if( sqlite3VdbeMemGrow(pOut, n, 0) ){ + goto no_mem; + } + pOut->n = n; + MemSetTypeFlag(pOut, MEM_Blob); + if( pC->isIndex ){ + rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); }else{ - pTos->flags = MEM_Null; + rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); } - pTos->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ + pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ + UPDATE_MAX_BLOBSIZE(pOut); break; } -/* Opcode: Rowid P1 * * +/* Opcode: Rowid P1 P2 * * * ** -** Push onto the stack an integer which is the key of the table entry that -** P1 is currently point to. +** Store in register P2 an integer which is the key of the table entry that +** P1 is currently point to. If p2==0 then push the integer. */ -case OP_Rowid: { +case OP_Rowid: { /* out2-prerelease */ int i = pOp->p1; Cursor *pC; i64 v; @@ -41454,31 +43975,30 @@ case OP_Rowid: { assert( pC!=0 ); rc = sqlite3VdbeCursorMoveto(pC); if( rc ) goto abort_due_to_error; - pTos++; if( pC->rowidIsValid ){ v = pC->lastRowid; }else if( pC->pseudoTable ){ v = keyToInt(pC->iKey); - }else if( pC->nullRow || pC->pCursor==0 ){ - pTos->flags = MEM_Null; + }else if( pC->nullRow ){ + /* Leave the rowid set to a NULL */ break; }else{ assert( pC->pCursor!=0 ); sqlite3BtreeKeySize(pC->pCursor, &v); v = keyToInt(v); } - pTos->u.i = v; - pTos->flags = MEM_Int; + pOut->u.i = v; + MemSetTypeFlag(pOut, MEM_Int); break; } -/* Opcode: NullRow P1 * * +/* Opcode: NullRow P1 * * * * ** ** Move the cursor P1 to a null row. Any OP_Column operations -** that occur while the cursor is on the null row will always push -** a NULL onto the stack. +** that occur while the cursor is on the null row will always +** write a NULL. */ -case OP_NullRow: { /* no-push */ +case OP_NullRow: { int i = pOp->p1; Cursor *pC; @@ -41490,7 +44010,7 @@ case OP_NullRow: { /* no-push */ break; } -/* Opcode: Last P1 P2 * +/* Opcode: Last P1 P2 * * * ** ** The next use of the Rowid or Column or Next instruction for P1 ** will refer to the last entry in the database table or index. @@ -41498,31 +44018,29 @@ case OP_NullRow: { /* no-push */ ** If P2 is 0 or if the table or index is not empty, fall through ** to the following instruction. */ -case OP_Last: { /* no-push */ +case OP_Last: { /* jump */ int i = pOp->p1; Cursor *pC; BtCursor *pCrsr; + int res; assert( i>=0 && inCursor ); pC = p->apCsr[i]; assert( pC!=0 ); - if( (pCrsr = pC->pCursor)!=0 ){ - int res; - rc = sqlite3BtreeLast(pCrsr, &res); - pC->nullRow = res; - pC->deferredMoveto = 0; - pC->cacheStatus = CACHE_STALE; - if( res && pOp->p2>0 ){ - pc = pOp->p2 - 1; - } - }else{ - pC->nullRow = 0; + pCrsr = pC->pCursor; + assert( pCrsr!=0 ); + rc = sqlite3BtreeLast(pCrsr, &res); + pC->nullRow = res; + pC->deferredMoveto = 0; + pC->cacheStatus = CACHE_STALE; + if( res && pOp->p2>0 ){ + pc = pOp->p2 - 1; } break; } -/* Opcode: Sort P1 P2 * +/* Opcode: Sort P1 P2 * * * ** ** This opcode does exactly the same thing as OP_Rewind except that ** it increments an undocumented global variable used for testing. @@ -41534,14 +44052,14 @@ case OP_Last: { /* no-push */ ** regression tests can determine whether or not the optimizer is ** correctly optimizing out sorts. */ -case OP_Sort: { /* no-push */ +case OP_Sort: { /* jump */ #ifdef SQLITE_TEST sqlite3_sort_count++; sqlite3_search_count--; #endif /* Fall through into OP_Rewind */ } -/* Opcode: Rewind P1 P2 * +/* Opcode: Rewind P1 P2 * * * ** ** The next use of the Rowid or Column or Next instruction for P1 ** will refer to the first entry in the database table or index. @@ -41549,7 +44067,7 @@ case OP_Sort: { /* no-push */ ** If P2 is 0 or if the table or index is not empty, fall through ** to the following instruction. */ -case OP_Rewind: { /* no-push */ +case OP_Rewind: { /* jump */ int i = pOp->p1; Cursor *pC; BtCursor *pCrsr; @@ -41567,30 +44085,35 @@ case OP_Rewind: { /* no-push */ res = 1; } pC->nullRow = res; - if( res && pOp->p2>0 ){ + assert( pOp->p2>0 && pOp->p2nOp ); + if( res ){ pc = pOp->p2 - 1; } break; } -/* Opcode: Next P1 P2 * +/* Opcode: Next P1 P2 * * * ** ** Advance cursor P1 so that it points to the next key/data pair in its ** table or index. If there are no more key/value pairs then fall through ** to the following instruction. But if the cursor advance was successful, ** jump immediately to P2. ** +** The P1 cursor must be for a real table, not a pseudo-table. +** ** See also: Prev */ -/* Opcode: Prev P1 P2 * +/* Opcode: Prev P1 P2 * * * ** ** Back up cursor P1 so that it points to the previous key/data pair in its ** table or index. If there is no previous key/value pairs then fall through ** to the following instruction. But if the cursor backup was successful, ** jump immediately to P2. +** +** The P1 cursor must be for a real table, not a pseudo-table. */ -case OP_Prev: /* no-push */ -case OP_Next: { /* no-push */ +case OP_Prev: /* jump */ +case OP_Next: { /* jump */ Cursor *pC; BtCursor *pCrsr; @@ -41600,231 +44123,200 @@ case OP_Next: { /* no-push */ if( pC==0 ){ break; /* See ticket #2273 */ } - if( (pCrsr = pC->pCursor)!=0 ){ - int res; - if( pC->nullRow ){ - res = 1; - }else{ - assert( pC->deferredMoveto==0 ); - rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : - sqlite3BtreePrevious(pCrsr, &res); - pC->nullRow = res; - pC->cacheStatus = CACHE_STALE; - } + pCrsr = pC->pCursor; + assert( pCrsr ); + if( pC->nullRow==0 ){ + int res = 1; + assert( pC->deferredMoveto==0 ); + rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) : + sqlite3BtreePrevious(pCrsr, &res); + pC->nullRow = res; + pC->cacheStatus = CACHE_STALE; if( res==0 ){ pc = pOp->p2 - 1; #ifdef SQLITE_TEST sqlite3_search_count++; #endif } - }else{ - pC->nullRow = 1; } pC->rowidIsValid = 0; break; } -/* Opcode: IdxInsert P1 P2 * +/* Opcode: IdxInsert P1 P2 P3 * * ** -** The top of the stack holds a SQL index key made using either the -** MakeIdxRec or MakeRecord instructions. This opcode writes that key +** Register P2 holds a SQL index key made using the +** MakeIdxRec instructions. This opcode writes that key ** into the index P1. Data for the entry is nil. ** -** P2 is a flag that provides a hint to the b-tree layer that this +** P3 is a flag that provides a hint to the b-tree layer that this ** insert is likely to be an append. ** ** This instruction only works for indices. The equivalent instruction ** for tables is OP_Insert. */ -case OP_IdxInsert: { /* no-push */ +case OP_IdxInsert: { /* in2 */ int i = pOp->p1; Cursor *pC; BtCursor *pCrsr; - assert( pTos>=p->aStack ); assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); - assert( pTos->flags & MEM_Blob ); + assert( pIn2->flags & MEM_Blob ); if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ assert( pC->isTable==0 ); - rc = ExpandBlob(pTos); + rc = ExpandBlob(pIn2); if( rc==SQLITE_OK ){ - int nKey = pTos->n; - const char *zKey = pTos->z; - rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p2); + int nKey = pIn2->n; + const char *zKey = pIn2->z; + rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3); assert( pC->deferredMoveto==0 ); pC->cacheStatus = CACHE_STALE; } } - Release(pTos); - pTos--; break; } -/* Opcode: IdxDelete P1 * * +/* Opcode: IdxDelete P1 P2 * * * ** -** The top of the stack is an index key built using the either the -** MakeIdxRec or MakeRecord opcodes. -** This opcode removes that entry from the index. +** The content of register P2 is an index key built using the +** MakeIdxRec opcode. This opcode removes that entry from the +** index opened by cursor P1. */ -case OP_IdxDelete: { /* no-push */ +case OP_IdxDelete: { /* in2 */ int i = pOp->p1; Cursor *pC; BtCursor *pCrsr; - assert( pTos>=p->aStack ); - assert( pTos->flags & MEM_Blob ); + assert( pIn2->flags & MEM_Blob ); assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ int res; - rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, 0, &res); + rc = sqlite3BtreeMoveto(pCrsr, pIn2->z, pIn2->n, 0, &res); if( rc==SQLITE_OK && res==0 ){ rc = sqlite3BtreeDelete(pCrsr); } assert( pC->deferredMoveto==0 ); pC->cacheStatus = CACHE_STALE; } - Release(pTos); - pTos--; break; } -/* Opcode: IdxRowid P1 * * +/* Opcode: IdxRowid P1 P2 * * * ** -** Push onto the stack an integer which is the last entry in the record at +** Write into register P2 an integer which is the last entry in the record at ** the end of the index key pointed to by cursor P1. This integer should be ** the rowid of the table entry to which this index entry points. ** ** See also: Rowid, MakeIdxRec. */ -case OP_IdxRowid: { +case OP_IdxRowid: { /* out2-prerelease */ int i = pOp->p1; BtCursor *pCrsr; Cursor *pC; assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); - pTos++; - pTos->flags = MEM_Null; if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){ i64 rowid; assert( pC->deferredMoveto==0 ); assert( pC->isTable==0 ); - if( pC->nullRow ){ - pTos->flags = MEM_Null; - }else{ + if( !pC->nullRow ){ rc = sqlite3VdbeIdxRowid(pCrsr, &rowid); if( rc!=SQLITE_OK ){ goto abort_due_to_error; } - pTos->flags = MEM_Int; - pTos->u.i = rowid; + MemSetTypeFlag(pOut, MEM_Int); + pOut->u.i = rowid; } } break; } -/* Opcode: IdxGT P1 P2 * +/* Opcode: IdxGE P1 P2 P3 * P5 ** -** The top of the stack is an index entry that omits the ROWID. Compare -** the top of stack against the index that P1 is currently pointing to. +** The value in register P3 is an index entry that omits the ROWID. Compare +** this value against the index that P1 is currently pointing to. ** Ignore the ROWID on the P1 index. ** -** The top of the stack might have fewer columns that P1. +** If the P1 index entry is greater than or equal to the value in +** register P3 then jump to P2. Otherwise fall through to the next +** instruction. ** -** If the P1 index entry is greater than the top of the stack -** then jump to P2. Otherwise fall through to the next instruction. -** In either case, the stack is popped once. -*/ -/* Opcode: IdxGE P1 P2 P3 -** -** The top of the stack is an index entry that omits the ROWID. Compare -** the top of stack against the index that P1 is currently pointing to. -** Ignore the ROWID on the P1 index. -** -** If the P1 index entry is greater than or equal to the top of the stack -** then jump to P2. Otherwise fall through to the next instruction. -** In either case, the stack is popped once. -** -** If P3 is the "+" string (or any other non-NULL string) then the -** index taken from the top of the stack is temporarily increased by -** an epsilon prior to the comparison. This make the opcode work -** like IdxGT except that if the key from the stack is a prefix of +** If P5 is non-zero then the value in register P3 is temporarily +** increased by an epsilon prior to the comparison. This make the opcode work +** like IdxGT except that if the key from register P3 is a prefix of ** the key in the cursor, the result is false whereas it would be ** true with IdxGT. */ -/* Opcode: IdxLT P1 P2 P3 +/* Opcode: IdxLT P1 P2 P3 * P5 ** -** The top of the stack is an index entry that omits the ROWID. Compare -** the top of stack against the index that P1 is currently pointing to. +** The value in register P3 is an index entry that omits the ROWID. Compare +** the this value against the index that P1 is currently pointing to. ** Ignore the ROWID on the P1 index. ** -** If the P1 index entry is less than the top of the stack +** If the P1 index entry is less than the register P3 value ** then jump to P2. Otherwise fall through to the next instruction. -** In either case, the stack is popped once. ** -** If P3 is the "+" string (or any other non-NULL string) then the -** index taken from the top of the stack is temporarily increased by +** If P5 is non-zero then the +** index taken from register P3 is temporarily increased by ** an epsilon prior to the comparison. This makes the opcode work ** like IdxLE. */ -case OP_IdxLT: /* no-push */ -case OP_IdxGT: /* no-push */ -case OP_IdxGE: { /* no-push */ +case OP_IdxLT: /* jump, in3 */ +case OP_IdxGE: { /* jump, in3 */ int i= pOp->p1; Cursor *pC; assert( i>=0 && inCursor ); assert( p->apCsr[i]!=0 ); - assert( pTos>=p->aStack ); if( (pC = p->apCsr[i])->pCursor!=0 ){ int res; - assert( pTos->flags & MEM_Blob ); /* Created using OP_MakeRecord */ + assert( pIn3->flags & MEM_Blob ); /* Created using OP_MakeRecord */ assert( pC->deferredMoveto==0 ); - ExpandBlob(pTos); - *pC->pIncrKey = pOp->p3!=0; - assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT ); - rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res); + ExpandBlob(pIn3); + assert( pOp->p5==0 || pOp->p5==1 ); + *pC->pIncrKey = pOp->p5; + rc = sqlite3VdbeIdxKeyCompare(pC, pIn3->n, (u8*)pIn3->z, &res); *pC->pIncrKey = 0; if( rc!=SQLITE_OK ){ break; } if( pOp->opcode==OP_IdxLT ){ res = -res; - }else if( pOp->opcode==OP_IdxGE ){ + }else{ + assert( pOp->opcode==OP_IdxGE ); res++; } if( res>0 ){ pc = pOp->p2 - 1 ; } } - Release(pTos); - pTos--; break; } -/* Opcode: Destroy P1 P2 * +/* Opcode: Destroy P1 P2 P3 * * ** ** Delete an entire database table or index whose root page in the database ** file is given by P1. ** -** The table being destroyed is in the main database file if P2==0. If -** P2==1 then the table to be clear is in the auxiliary database file +** The table being destroyed is in the main database file if P3==0. If +** P3==1 then the table to be clear is in the auxiliary database file ** that is used to store tables create using CREATE TEMPORARY TABLE. ** ** If AUTOVACUUM is enabled then it is possible that another root page ** might be moved into the newly deleted root page in order to keep all ** root pages contiguous at the beginning of the database. The former ** value of the root page that moved - its value before the move occurred - -** is pushed onto the stack. If no page movement was required (because -** the table being dropped was already the last one in the database) then -** a zero is pushed onto the stack. If AUTOVACUUM is disabled -** then a zero is pushed onto the stack. +** is stored in register P2. If no page +** movement was required (because the table being dropped was already +** the last one in the database) then a zero is stored in register P2. +** If AUTOVACUUM is disabled then a zero is stored in register P2. ** ** See also: Clear */ -case OP_Destroy: { +case OP_Destroy: { /* out2-prerelease */ int iMoved; int iCnt; #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -41842,15 +44334,15 @@ case OP_Destroy: { rc = SQLITE_LOCKED; p->errorAction = OE_Abort; }else{ + int iDb = pOp->p3; assert( iCnt==1 ); - assert( (p->btreeMask & (1<p2))!=0 ); - rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved); - pTos++; - pTos->flags = MEM_Int; - pTos->u.i = iMoved; + assert( (p->btreeMask & (1<aDb[iDb].pBt, pOp->p1, &iMoved); + MemSetTypeFlag(pOut, MEM_Int); + pOut->u.i = iMoved; #ifndef SQLITE_OMIT_AUTOVACUUM if( rc==SQLITE_OK && iMoved!=0 ){ - sqlite3RootPageMoved(&db->aDb[pOp->p2], iMoved, pOp->p1); + sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1); } #endif } @@ -41869,52 +44361,18 @@ case OP_Destroy: { ** ** See also: Destroy */ -case OP_Clear: { /* no-push */ - - /* For consistency with the way other features of SQLite operate - ** with a truncate, we will also skip the update callback. - */ -#if 0 - Btree *pBt = db->aDb[pOp->p2].pBt; - if( db->xUpdateCallback && pOp->p3 ){ - const char *zDb = db->aDb[pOp->p2].zName; - const char *zTbl = pOp->p3; - BtCursor *pCur = 0; - int fin = 0; - - rc = sqlite3BtreeCursor(pBt, pOp->p1, 0, 0, 0, &pCur); - if( rc!=SQLITE_OK ){ - goto abort_due_to_error; - } - for( - rc=sqlite3BtreeFirst(pCur, &fin); - rc==SQLITE_OK && !fin; - rc=sqlite3BtreeNext(pCur, &fin) - ){ - i64 iKey; - rc = sqlite3BtreeKeySize(pCur, &iKey); - if( rc ){ - break; - } - iKey = keyToInt(iKey); - db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey); - } - sqlite3BtreeCloseCursor(pCur); - if( rc!=SQLITE_OK ){ - goto abort_due_to_error; - } - } -#endif +case OP_Clear: { assert( (p->btreeMask & (1<p2))!=0 ); rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1); break; } -/* Opcode: CreateTable P1 * * +/* Opcode: CreateTable P1 P2 * * * ** -** Allocate a new table in the main database file if P2==0 or in the -** auxiliary database file if P2==1. Push the page number -** for the root page of the new table onto the stack. +** Allocate a new table in the main database file if P1==0 or in the +** auxiliary database file if P1==1 or in an attached database if +** P1>1. Write the root page number of the new table into +** register P2 ** ** The difference between a table and an index is this: A table must ** have a 4-byte integer key and can have arbitrary data. An index @@ -41922,16 +44380,17 @@ case OP_Clear: { /* no-push */ ** ** See also: CreateIndex */ -/* Opcode: CreateIndex P1 * * +/* Opcode: CreateIndex P1 P2 * * * ** -** Allocate a new index in the main database file if P2==0 or in the -** auxiliary database file if P2==1. Push the page number of the -** root page of the new index onto the stack. +** Allocate a new index in the main database file if P1==0 or in the +** auxiliary database file if P1==1 or in an attached database if +** P1>1. Write the root page number of the new table into +** register P2. ** ** See documentation on OP_CreateTable for additional information. */ -case OP_CreateIndex: -case OP_CreateTable: { +case OP_CreateIndex: /* out2-prerelease */ +case OP_CreateTable: { /* out2-prerelease */ int pgno; int flags; Db *pDb; @@ -41946,20 +44405,17 @@ case OP_CreateTable: { flags = BTREE_ZERODATA; } rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); - pTos++; if( rc==SQLITE_OK ){ - pTos->u.i = pgno; - pTos->flags = MEM_Int; - }else{ - pTos->flags = MEM_Null; + pOut->u.i = pgno; + MemSetTypeFlag(pOut, MEM_Int); } break; } -/* Opcode: ParseSchema P1 P2 P3 +/* Opcode: ParseSchema P1 P2 * P4 * ** ** Read and parse all entries from the SQLITE_MASTER table of database P1 -** that match the WHERE clause P3. P2 is the "force" flag. Always do +** that match the WHERE clause P4. P2 is the "force" flag. Always do ** the parsing if P2 is true. If P2 is false, then this routine is a ** no-op if the schema is not currently loaded. In other words, if P2 ** is false, the SQLITE_MASTER table is only parsed if the rest of the @@ -41968,7 +44424,7 @@ case OP_CreateTable: { ** This opcode invokes the parser to create a new virtual machine, ** then runs the new virtual machine. It is thus a reentrant opcode. */ -case OP_ParseSchema: { /* no-push */ +case OP_ParseSchema: { char *zSql; int iDb = pOp->p1; const char *zMaster; @@ -41984,9 +44440,9 @@ case OP_ParseSchema: { /* no-push */ initData.pzErrMsg = &p->zErrMsg; zSql = sqlite3MPrintf(db, "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s", - db->aDb[iDb].zName, zMaster, pOp->p3); + db->aDb[iDb].zName, zMaster, pOp->p4.z); if( zSql==0 ) goto no_mem; - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); assert( db->init.busy==0 ); db->init.busy = 1; assert( !db->mallocFailed ); @@ -41994,7 +44450,7 @@ case OP_ParseSchema: { /* no-push */ if( rc==SQLITE_ABORT ) rc = initData.rc; sqlite3_free(zSql); db->init.busy = 0; - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); if( rc==SQLITE_NOMEM ){ goto no_mem; } @@ -42002,13 +44458,13 @@ case OP_ParseSchema: { /* no-push */ } #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) -/* Opcode: LoadAnalysis P1 * * +/* Opcode: LoadAnalysis P1 * * * * ** ** Read the sqlite_stat1 table for database P1 and load the content ** of that table into the internal index hash table. This will cause ** the analysis to be used when preparing all subsequent queries. */ -case OP_LoadAnalysis: { /* no-push */ +case OP_LoadAnalysis: { int iDb = pOp->p1; assert( iDb>=0 && iDbnDb ); rc = sqlite3AnalysisLoad(db, iDb); @@ -42016,140 +44472,128 @@ case OP_LoadAnalysis: { /* no-push */ } #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */ -/* Opcode: DropTable P1 * P3 +/* Opcode: DropTable P1 * * P4 * ** ** Remove the internal (in-memory) data structures that describe -** the table named P3 in database P1. This is called after a table +** the table named P4 in database P1. This is called after a table ** is dropped in order to keep the internal representation of the ** schema consistent with what is on disk. */ -case OP_DropTable: { /* no-push */ - sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p3); +case OP_DropTable: { + sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); break; } -/* Opcode: DropIndex P1 * P3 +/* Opcode: DropIndex P1 * * P4 * ** ** Remove the internal (in-memory) data structures that describe -** the index named P3 in database P1. This is called after an index +** the index named P4 in database P1. This is called after an index ** is dropped in order to keep the internal representation of the ** schema consistent with what is on disk. */ -case OP_DropIndex: { /* no-push */ - sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p3); +case OP_DropIndex: { + sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); break; } -/* Opcode: DropTrigger P1 * P3 +/* Opcode: DropTrigger P1 * * P4 * ** ** Remove the internal (in-memory) data structures that describe -** the trigger named P3 in database P1. This is called after a trigger +** the trigger named P4 in database P1. This is called after a trigger ** is dropped in order to keep the internal representation of the ** schema consistent with what is on disk. */ -case OP_DropTrigger: { /* no-push */ - sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p3); +case OP_DropTrigger: { + sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); break; } #ifndef SQLITE_OMIT_INTEGRITY_CHECK -/* Opcode: IntegrityCk P1 P2 * +/* Opcode: IntegrityCk P1 P2 P3 * P5 ** -** Do an analysis of the currently open database. Push onto the -** stack the text of an error message describing any problems. -** If no problems are found, push a NULL onto the stack. +** Do an analysis of the currently open database. Store in +** register P1 the text of an error message describing any problems. +** If no problems are found, store a NULL in register P1. ** -** P1 is the address of a memory cell that contains the maximum -** number of allowed errors. At most mem[P1] errors will be reported. -** In other words, the analysis stops as soon as mem[P1] errors are -** seen. Mem[P1] is updated with the number of errors remaining. +** The register P3 contains the maximum number of allowed errors. +** At most reg(P3) errors will be reported. +** In other words, the analysis stops as soon as reg(P1) errors are +** seen. Reg(P1) is updated with the number of errors remaining. ** ** The root page numbers of all tables in the database are integer -** values on the stack. This opcode pulls as many integers as it -** can off of the stack and uses those numbers as the root pages. +** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables +** total. ** -** If P2 is not zero, the check is done on the auxiliary database +** If P5 is not zero, the check is done on the auxiliary database ** file, not the main database file. ** ** This opcode is used to implement the integrity_check pragma. */ case OP_IntegrityCk: { - int nRoot; - int *aRoot; - int j; - int nErr; - char *z; - Mem *pnErr; - - for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){ - if( (pTos[-nRoot].flags & MEM_Int)==0 ) break; - } + int nRoot; /* Number of tables to check. (Number of root pages.) */ + int *aRoot; /* Array of rootpage numbers for tables to be checked */ + int j; /* Loop counter */ + int nErr; /* Number of errors reported */ + char *z; /* Text of the error report */ + Mem *pnErr; /* Register keeping track of errors remaining */ + + nRoot = pOp->p2; assert( nRoot>0 ); aRoot = sqlite3_malloc( sizeof(int)*(nRoot+1) ); if( aRoot==0 ) goto no_mem; - j = pOp->p1; - assert( j>=0 && jnMem ); - pnErr = &p->aMem[j]; + assert( pOp->p3>0 && pOp->p3<=p->nMem ); + pnErr = &p->aMem[pOp->p3]; assert( (pnErr->flags & MEM_Int)!=0 ); + assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); + pIn1 = &p->aMem[pOp->p1]; for(j=0; ju.i; + aRoot[j] = sqlite3VdbeIntValue(&pIn1[j]); } aRoot[j] = 0; - popStack(&pTos, nRoot); - pTos++; - assert( pOp->p2>=0 && pOp->p2nDb ); - assert( (p->btreeMask & (1<p2))!=0 ); - z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot, + assert( pOp->p5nDb ); + assert( (p->btreeMask & (1<p5))!=0 ); + z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, pnErr->u.i, &nErr); pnErr->u.i -= nErr; + sqlite3VdbeMemSetNull(pIn1); if( nErr==0 ){ assert( z==0 ); - pTos->flags = MEM_Null; }else{ - pTos->z = z; - pTos->n = strlen(z); - pTos->flags = MEM_Str | MEM_Dyn | MEM_Term; - pTos->xDel = 0; + sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); } - pTos->enc = SQLITE_UTF8; - sqlite3VdbeChangeEncoding(pTos, encoding); + UPDATE_MAX_BLOBSIZE(pIn1); + sqlite3VdbeChangeEncoding(pIn1, encoding); sqlite3_free(aRoot); break; } #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ -/* Opcode: FifoWrite * * * +/* Opcode: FifoWrite P1 * * * * ** -** Write the integer on the top of the stack -** into the Fifo. +** Write the integer from register P1 into the Fifo. */ -case OP_FifoWrite: { /* no-push */ - assert( pTos>=p->aStack ); - sqlite3VdbeMemIntegerify(pTos); - if( sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i)==SQLITE_NOMEM ){ +case OP_FifoWrite: { /* in1 */ + if( sqlite3VdbeFifoPush(&p->sFifo, sqlite3VdbeIntValue(pIn1))==SQLITE_NOMEM ){ goto no_mem; } - assert( (pTos->flags & MEM_Dyn)==0 ); - pTos--; break; } -/* Opcode: FifoRead * P2 * +/* Opcode: FifoRead P1 P2 * * * ** -** Attempt to read a single integer from the Fifo -** and push it onto the stack. If the Fifo is empty -** push nothing but instead jump to P2. +** Attempt to read a single integer from the Fifo. Store that +** integer in register P1. +** +** If the Fifo is empty jump to P2. */ -case OP_FifoRead: { - i64 v; +case OP_FifoRead: { /* jump */ CHECK_FOR_INTERRUPT; - if( sqlite3VdbeFifoPop(&p->sFifo, &v)==SQLITE_DONE ){ + assert( pOp->p1>0 && pOp->p1<=p->nMem ); + pOut = &p->aMem[pOp->p1]; + MemSetTypeFlag(pOut, MEM_Int); + if( sqlite3VdbeFifoPop(&p->sFifo, &pOut->u.i)==SQLITE_DONE ){ pc = pOp->p2 - 1; - }else{ - pTos++; - pTos->u.i = v; - pTos->flags = MEM_Int; } break; } @@ -42161,7 +44605,7 @@ case OP_FifoRead: { ** opcode. The context stores the last insert row id, the last statement change ** count, and the current statement change count. */ -case OP_ContextPush: { /* no-push */ +case OP_ContextPush: { int i = p->contextStackTop++; Context *pContext; @@ -42187,7 +44631,7 @@ case OP_ContextPush: { /* no-push */ ** executed. The context stores the last insert row id, the last statement ** change count, and the current statement change count. */ -case OP_ContextPop: { /* no-push */ +case OP_ContextPop: { Context *pContext = &p->contextStack[--p->contextStackTop]; assert( p->contextStackTop>=0 ); db->lastRowid = pContext->lastRowid; @@ -42198,205 +44642,98 @@ case OP_ContextPop: { /* no-push */ } #endif /* #ifndef SQLITE_OMIT_TRIGGER */ -/* Opcode: MemStore P1 P2 * -** -** Write the top of the stack into memory location P1. -** P1 should be a small integer since space is allocated -** for all memory locations between 0 and P1 inclusive. -** -** After the data is stored in the memory location, the -** stack is popped once if P2 is 1. If P2 is zero, then -** the original data remains on the stack. -*/ -case OP_MemStore: { /* no-push */ - assert( pTos>=p->aStack ); - assert( pOp->p1>=0 && pOp->p1nMem ); - rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos); - pTos--; - - /* If P2 is 0 then fall thru to the next opcode, OP_MemLoad, that will - ** restore the top of the stack to its original value. - */ - if( pOp->p2 ){ - break; - } -} -/* Opcode: MemLoad P1 * * -** -** Push a copy of the value in memory location P1 onto the stack. -** -** If the value is a string, then the value pushed is a pointer to -** the string that is stored in the memory location. If the memory -** location is subsequently changed (using OP_MemStore) then the -** value pushed onto the stack will change too. -*/ -case OP_MemLoad: { - int i = pOp->p1; - assert( i>=0 && inMem ); - pTos++; - sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem); - break; -} - #ifndef SQLITE_OMIT_AUTOINCREMENT -/* Opcode: MemMax P1 * * +/* Opcode: MemMax P1 P2 * * * ** -** Set the value of memory cell P1 to the maximum of its current value -** and the value on the top of the stack. The stack is unchanged. +** Set the value of register P1 to the maximum of its current value +** and the value in register P2. ** ** This instruction throws an error if the memory cell is not initially ** an integer. */ -case OP_MemMax: { /* no-push */ - int i = pOp->p1; - Mem *pMem; - assert( pTos>=p->aStack ); - assert( i>=0 && inMem ); - pMem = &p->aMem[i]; - sqlite3VdbeMemIntegerify(pMem); - sqlite3VdbeMemIntegerify(pTos); - if( pMem->u.iu.i){ - pMem->u.i = pTos->u.i; +case OP_MemMax: { /* in1, in2 */ + sqlite3VdbeMemIntegerify(pIn1); + sqlite3VdbeMemIntegerify(pIn2); + if( pIn1->u.iu.i){ + pIn1->u.i = pIn2->u.i; } break; } #endif /* SQLITE_OMIT_AUTOINCREMENT */ -/* Opcode: MemIncr P1 P2 * -** -** Increment the integer valued memory cell P2 by the value in P1. -** -** It is illegal to use this instruction on a memory cell that does -** not contain an integer. An assertion fault will result if you try. -*/ -case OP_MemIncr: { /* no-push */ - int i = pOp->p2; - Mem *pMem; - assert( i>=0 && inMem ); - pMem = &p->aMem[i]; - assert( pMem->flags==MEM_Int ); - pMem->u.i += pOp->p1; - break; -} - -/* Opcode: IfMemPos P1 P2 * +/* Opcode: IfPos P1 P2 * * * ** -** If the value of memory cell P1 is 1 or greater, jump to P2. +** If the value of register P1 is 1 or greater, jump to P2. ** -** It is illegal to use this instruction on a memory cell that does +** It is illegal to use this instruction on a register that does ** not contain an integer. An assertion fault will result if you try. */ -case OP_IfMemPos: { /* no-push */ - int i = pOp->p1; - Mem *pMem; - assert( i>=0 && inMem ); - pMem = &p->aMem[i]; - assert( pMem->flags==MEM_Int ); - if( pMem->u.i>0 ){ +case OP_IfPos: { /* jump, in1 */ + assert( pIn1->flags&MEM_Int ); + if( pIn1->u.i>0 ){ pc = pOp->p2 - 1; } break; } -/* Opcode: IfMemNeg P1 P2 * +/* Opcode: IfNeg P1 P2 * * * ** -** If the value of memory cell P1 is less than zero, jump to P2. +** If the value of register P1 is less than zero, jump to P2. ** -** It is illegal to use this instruction on a memory cell that does +** It is illegal to use this instruction on a register that does ** not contain an integer. An assertion fault will result if you try. */ -case OP_IfMemNeg: { /* no-push */ - int i = pOp->p1; - Mem *pMem; - assert( i>=0 && inMem ); - pMem = &p->aMem[i]; - assert( pMem->flags==MEM_Int ); - if( pMem->u.i<0 ){ +case OP_IfNeg: { /* jump, in1 */ + assert( pIn1->flags&MEM_Int ); + if( pIn1->u.i<0 ){ pc = pOp->p2 - 1; } break; } -/* Opcode: IfMemZero P1 P2 * +/* Opcode: IfZero P1 P2 * * * ** -** If the value of memory cell P1 is exactly 0, jump to P2. +** If the value of register P1 is exactly 0, jump to P2. ** -** It is illegal to use this instruction on a memory cell that does +** It is illegal to use this instruction on a register that does ** not contain an integer. An assertion fault will result if you try. */ -case OP_IfMemZero: { /* no-push */ - int i = pOp->p1; - Mem *pMem; - assert( i>=0 && inMem ); - pMem = &p->aMem[i]; - assert( pMem->flags==MEM_Int ); - if( pMem->u.i==0 ){ +case OP_IfZero: { /* jump, in1 */ + assert( pIn1->flags&MEM_Int ); + if( pIn1->u.i==0 ){ pc = pOp->p2 - 1; } break; } -/* Opcode: MemNull P1 * * -** -** Store a NULL in memory cell P1 -*/ -case OP_MemNull: { - assert( pOp->p1>=0 && pOp->p1nMem ); - sqlite3VdbeMemSetNull(&p->aMem[pOp->p1]); - break; -} - -/* Opcode: MemInt P1 P2 * -** -** Store the integer value P1 in memory cell P2. -*/ -case OP_MemInt: { - assert( pOp->p2>=0 && pOp->p2nMem ); - sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1); - break; -} - -/* Opcode: MemMove P1 P2 * -** -** Move the content of memory cell P2 over to memory cell P1. -** Any prior content of P1 is erased. Memory cell P2 is left -** containing a NULL. -*/ -case OP_MemMove: { - assert( pOp->p1>=0 && pOp->p1nMem ); - assert( pOp->p2>=0 && pOp->p2nMem ); - rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]); - break; -} - -/* Opcode: AggStep P1 P2 P3 +/* Opcode: AggStep * P2 P3 P4 P5 ** ** Execute the step function for an aggregate. The -** function has P2 arguments. P3 is a pointer to the FuncDef -** structure that specifies the function. Use memory location -** P1 as the accumulator. +** function has P5 arguments. P4 is a pointer to the FuncDef +** structure that specifies the function. Use register +** P3 as the accumulator. ** -** The P2 arguments are popped from the stack. +** The P5 arguments are taken from register P2 and its +** successors. */ -case OP_AggStep: { /* no-push */ - int n = pOp->p2; +case OP_AggStep: { + int n = pOp->p5; int i; Mem *pMem, *pRec; sqlite3_context ctx; sqlite3_value **apVal; assert( n>=0 ); - pRec = &pTos[1-n]; - assert( pRec>=p->aStack ); + pRec = &p->aMem[pOp->p2]; apVal = p->apArg; assert( apVal || n==0 ); for(i=0; ip3; - assert( pOp->p1>=0 && pOp->p1nMem ); - ctx.pMem = pMem = &p->aMem[pOp->p1]; + ctx.pFunc = pOp->p4.pFunc; + assert( pOp->p3>0 && pOp->p3<=p->nMem ); + ctx.pMem = pMem = &p->aMem[pOp->p3]; pMem->n++; ctx.s.flags = MEM_Null; ctx.s.z = 0; @@ -42406,41 +44743,41 @@ case OP_AggStep: { /* no-push */ ctx.pColl = 0; if( ctx.pFunc->needCollSeq ){ assert( pOp>p->aOp ); - assert( pOp[-1].p3type==P3_COLLSEQ ); + assert( pOp[-1].p4type==P4_COLLSEQ ); assert( pOp[-1].opcode==OP_CollSeq ); - ctx.pColl = (CollSeq *)pOp[-1].p3; + ctx.pColl = pOp[-1].p4.pColl; } (ctx.pFunc->xStep)(&ctx, n, apVal); - popStack(&pTos, n); if( ctx.isError ){ sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0); - rc = SQLITE_ERROR; + rc = ctx.isError; } sqlite3VdbeMemRelease(&ctx.s); break; } -/* Opcode: AggFinal P1 P2 P3 +/* Opcode: AggFinal P1 P2 * P4 * ** ** Execute the finalizer function for an aggregate. P1 is ** the memory location that is the accumulator for the aggregate. ** ** P2 is the number of arguments that the step function takes and -** P3 is a pointer to the FuncDef for this function. The P2 +** P4 is a pointer to the FuncDef for this function. The P2 ** argument is not used by this opcode. It is only there to disambiguate ** functions that can take varying numbers of arguments. The -** P3 argument is only needed for the degenerate case where +** P4 argument is only needed for the degenerate case where ** the step function was not previously called. */ -case OP_AggFinal: { /* no-push */ +case OP_AggFinal: { Mem *pMem; - assert( pOp->p1>=0 && pOp->p1nMem ); + assert( pOp->p1>0 && pOp->p1<=p->nMem ); pMem = &p->aMem[pOp->p1]; assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); - rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3); + rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); if( rc==SQLITE_ERROR ){ sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0); } + UPDATE_MAX_BLOBSIZE(pMem); if( sqlite3VdbeMemTooBig(pMem) ){ goto too_big; } @@ -42449,13 +44786,13 @@ case OP_AggFinal: { /* no-push */ #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) -/* Opcode: Vacuum * * * +/* Opcode: Vacuum * * * * * ** ** Vacuum the entire database. This opcode will cause other virtual ** machines to be created and run. It may not be called from within ** a transaction. */ -case OP_Vacuum: { /* no-push */ +case OP_Vacuum: { if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; rc = sqlite3RunVacuum(&p->zErrMsg, db); if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; @@ -42464,13 +44801,13 @@ case OP_Vacuum: { /* no-push */ #endif #if !defined(SQLITE_OMIT_AUTOVACUUM) -/* Opcode: IncrVacuum P1 P2 * +/* Opcode: IncrVacuum P1 P2 * * * ** ** Perform a single step of the incremental vacuum procedure on ** the P1 database. If the vacuum has finished, jump to instruction ** P2. Otherwise, fall through to the next instruction. */ -case OP_IncrVacuum: { /* no-push */ +case OP_IncrVacuum: { /* jump */ Btree *pBt; assert( pOp->p1>=0 && pOp->p1nDb ); @@ -42485,7 +44822,7 @@ case OP_IncrVacuum: { /* no-push */ } #endif -/* Opcode: Expire P1 * * +/* Opcode: Expire P1 * * * * ** ** Cause precompiled statements to become expired. An expired statement ** fails with an error code of SQLITE_SCHEMA if it is ever executed @@ -42494,7 +44831,7 @@ case OP_IncrVacuum: { /* no-push */ ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, ** then only the currently executing statement is affected. */ -case OP_Expire: { /* no-push */ +case OP_Expire: { if( !pOp->p1 ){ sqlite3ExpirePreparedStatements(db); }else{ @@ -42504,7 +44841,7 @@ case OP_Expire: { /* no-push */ } #ifndef SQLITE_OMIT_SHARED_CACHE -/* Opcode: TableLock P1 P2 P3 +/* Opcode: TableLock P1 P2 * P4 * ** ** Obtain a lock on a particular table. This instruction is only used when ** the shared-cache feature is enabled. @@ -42517,10 +44854,10 @@ case OP_Expire: { /* no-push */ ** ** P2 contains the root-page of the table to lock. ** -** P3 contains a pointer to the name of the table being locked. This is only +** P4 contains a pointer to the name of the table being locked. This is only ** used to generate an error message if the lock cannot be obtained. */ -case OP_TableLock: { /* no-push */ +case OP_TableLock: { int p1 = pOp->p1; u8 isWriteLock = (p1<0); if( isWriteLock ){ @@ -42530,7 +44867,7 @@ case OP_TableLock: { /* no-push */ assert( (p->btreeMask & (1<aDb[p1].pBt, pOp->p2, isWriteLock); if( rc==SQLITE_LOCKED ){ - const char *z = (const char *)pOp->p3; + const char *z = pOp->p4.z; sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0); } break; @@ -42538,55 +44875,55 @@ case OP_TableLock: { /* no-push */ #endif /* SQLITE_OMIT_SHARED_CACHE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VBegin * * P3 +/* Opcode: VBegin * * * P4 * ** -** P3 a pointer to an sqlite3_vtab structure. Call the xBegin method +** P4 a pointer to an sqlite3_vtab structure. Call the xBegin method ** for that table. */ -case OP_VBegin: { /* no-push */ - rc = sqlite3VtabBegin(db, (sqlite3_vtab *)pOp->p3); +case OP_VBegin: { + rc = sqlite3VtabBegin(db, pOp->p4.pVtab); break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VCreate P1 * P3 +/* Opcode: VCreate P1 * * P4 * ** -** P3 is the name of a virtual table in database P1. Call the xCreate method +** P4 is the name of a virtual table in database P1. Call the xCreate method ** for that table. */ -case OP_VCreate: { /* no-push */ - rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p3, &p->zErrMsg); +case OP_VCreate: { + rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VDestroy P1 * P3 +/* Opcode: VDestroy P1 * * P4 * ** -** P3 is the name of a virtual table in database P1. Call the xDestroy method +** P4 is the name of a virtual table in database P1. Call the xDestroy method ** of that table. */ -case OP_VDestroy: { /* no-push */ +case OP_VDestroy: { p->inVtabMethod = 2; - rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p3); + rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); p->inVtabMethod = 0; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VOpen P1 * P3 +/* Opcode: VOpen P1 * * P4 * ** -** P3 is a pointer to a virtual table object, an sqlite3_vtab structure. +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. ** P1 is a cursor number. This opcode opens a cursor to the virtual ** table and stores that cursor in P1. */ -case OP_VOpen: { /* no-push */ +case OP_VOpen: { Cursor *pCur = 0; sqlite3_vtab_cursor *pVtabCursor = 0; - sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3); + sqlite3_vtab *pVtab = pOp->p4.pVtab; sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; assert(pVtab && pModule); @@ -42612,41 +44949,41 @@ case OP_VOpen: { /* no-push */ #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VFilter P1 P2 P3 +/* Opcode: VFilter P1 P2 P3 P4 * ** ** P1 is a cursor opened using VOpen. P2 is an address to jump to if ** the filtered result set is empty. ** -** P3 is either NULL or a string that was generated by the xBestIndex -** method of the module. The interpretation of the P3 string is left +** P4 is either NULL or a string that was generated by the xBestIndex +** method of the module. The interpretation of the P4 string is left ** to the module implementation. ** ** This opcode invokes the xFilter method on the virtual table specified -** by P1. The integer query plan parameter to xFilter is the top of the -** stack. Next down on the stack is the argc parameter. Beneath the -** next of stack are argc additional parameters which are passed to -** xFilter as argv. The topmost parameter (i.e. 3rd element popped from -** the stack) becomes argv[argc-1] when passed to xFilter. -** -** The integer query plan parameter, argc, and all argv stack values -** are popped from the stack before this instruction completes. +** by P1. The integer query plan parameter to xFilter is stored in register +** P3. Register P3+1 stores the argc parameter to be passed to the +** xFilter method. Registers P3+2..P3+1+argc are the argc additional +** parametersneath additional parameters which are passed to +** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. ** -** A jump is made to P2 if the result set after filtering would be -** empty. +** A jump is made to P2 if the result set after filtering would be empty. */ -case OP_VFilter: { /* no-push */ +case OP_VFilter: { /* jump */ int nArg; - + int iQuery; const sqlite3_module *pModule; + Mem *pQuery = &p->aMem[pOp->p3]; + Mem *pArgc = &pQuery[1]; Cursor *pCur = p->apCsr[pOp->p1]; + + REGISTER_TRACE(pOp->p3, pQuery); assert( pCur->pVtabCursor ); pModule = pCur->pVtabCursor->pVtab->pModule; - /* Grab the index number and argc parameters off the top of the stack. */ - assert( (&pTos[-1])>=p->aStack ); - assert( (pTos[0].flags&MEM_Int)!=0 && pTos[-1].flags==MEM_Int ); - nArg = pTos[-1].u.i; + /* Grab the index number and argc parameters */ + assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); + nArg = pArgc->u.i; + iQuery = pQuery->u.i; /* Invoke the xFilter method */ { @@ -42654,13 +44991,13 @@ case OP_VFilter: { /* no-push */ int i; Mem **apArg = p->apArg; for(i = 0; iinVtabMethod = 1; - rc = pModule->xFilter(pCur->pVtabCursor, pTos->u.i, pOp->p3, nArg, apArg); + rc = pModule->xFilter(pCur->pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); p->inVtabMethod = 0; if( rc==SQLITE_OK ){ res = pModule->xEof(pCur->pVtabCursor); @@ -42671,172 +45008,175 @@ case OP_VFilter: { /* no-push */ pc = pOp->p2 - 1; } } + pCur->nullRow = 0; - /* Pop the index number, argc value and parameters off the stack */ - popStack(&pTos, 2+nArg); break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VRowid P1 * * +/* Opcode: VRowid P1 P2 * * * ** -** Push an integer onto the stack which is the rowid of +** Store into register P2 the rowid of ** the virtual-table that the P1 cursor is pointing to. */ -case OP_VRowid: { +case OP_VRowid: { /* out2-prerelease */ const sqlite3_module *pModule; - + sqlite_int64 iRow; Cursor *pCur = p->apCsr[pOp->p1]; - assert( pCur->pVtabCursor ); - pModule = pCur->pVtabCursor->pVtab->pModule; - if( pModule->xRowid==0 ){ - sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0); - rc = SQLITE_ERROR; - } else { - sqlite_int64 iRow; - - if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; - rc = pModule->xRowid(pCur->pVtabCursor, &iRow); - if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; - pTos++; - pTos->flags = MEM_Int; - pTos->u.i = iRow; + assert( pCur->pVtabCursor ); + if( pCur->nullRow ){ + break; } - + pModule = pCur->pVtabCursor->pVtab->pModule; + assert( pModule->xRowid ); + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; + rc = pModule->xRowid(pCur->pVtabCursor, &iRow); + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; + MemSetTypeFlag(pOut, MEM_Int); + pOut->u.i = iRow; break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VColumn P1 P2 * +/* Opcode: VColumn P1 P2 P3 * * ** -** Push onto the stack the value of the P2-th column of -** the row of the virtual-table that the P1 cursor is pointing to. +** Store the value of the P2-th column of +** the row of the virtual-table that the +** P1 cursor is pointing to into register P3. */ case OP_VColumn: { const sqlite3_module *pModule; + Mem *pDest; + sqlite3_context sContext; Cursor *pCur = p->apCsr[pOp->p1]; assert( pCur->pVtabCursor ); + assert( pOp->p3>0 && pOp->p3<=p->nMem ); + pDest = &p->aMem[pOp->p3]; + if( pCur->nullRow ){ + sqlite3VdbeMemSetNull(pDest); + break; + } pModule = pCur->pVtabCursor->pVtab->pModule; - if( pModule->xColumn==0 ){ - sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0); - rc = SQLITE_ERROR; - } else { - sqlite3_context sContext; - memset(&sContext, 0, sizeof(sContext)); - sContext.s.flags = MEM_Null; - sContext.s.db = db; - if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; - rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); + assert( pModule->xColumn ); + memset(&sContext, 0, sizeof(sContext)); - /* Copy the result of the function to the top of the stack. We - ** do this regardless of whether or not an error occured to ensure any - ** dynamic allocation in sContext.s (a Mem struct) is released. - */ - sqlite3VdbeChangeEncoding(&sContext.s, encoding); - pTos++; - pTos->flags = 0; - sqlite3VdbeMemMove(pTos, &sContext.s); + /* The output cell may already have a buffer allocated. Move + ** the current contents to sContext.s so in case the user-function + ** can use the already allocated buffer instead of allocating a + ** new one. + */ + sqlite3VdbeMemMove(&sContext.s, pDest); + MemSetTypeFlag(&sContext.s, MEM_Null); - if( sqlite3SafetyOn(db) ){ - goto abort_due_to_misuse; - } - if( sqlite3VdbeMemTooBig(pTos) ){ - goto too_big; - } + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; + rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); + + /* Copy the result of the function to the P3 register. We + ** do this regardless of whether or not an error occured to ensure any + ** dynamic allocation in sContext.s (a Mem struct) is released. + */ + sqlite3VdbeChangeEncoding(&sContext.s, encoding); + REGISTER_TRACE(pOp->p3, pDest); + sqlite3VdbeMemMove(pDest, &sContext.s); + UPDATE_MAX_BLOBSIZE(pDest); + + if( sqlite3SafetyOn(db) ){ + goto abort_due_to_misuse; + } + if( sqlite3VdbeMemTooBig(pDest) ){ + goto too_big; } - break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VNext P1 P2 * +/* Opcode: VNext P1 P2 * * * ** ** Advance virtual table P1 to the next row in its result set and ** jump to instruction P2. Or, if the virtual table has reached ** the end of its result set, then fall through to the next instruction. */ -case OP_VNext: { /* no-push */ +case OP_VNext: { /* jump */ const sqlite3_module *pModule; int res = 0; Cursor *pCur = p->apCsr[pOp->p1]; assert( pCur->pVtabCursor ); + if( pCur->nullRow ){ + break; + } pModule = pCur->pVtabCursor->pVtab->pModule; - if( pModule->xNext==0 ){ - sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0); - rc = SQLITE_ERROR; - } else { - /* Invoke the xNext() method of the module. There is no way for the - ** underlying implementation to return an error if one occurs during - ** xNext(). Instead, if an error occurs, true is returned (indicating that - ** data is available) and the error code returned when xColumn or - ** some other method is next invoked on the save virtual table cursor. - */ - if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; - p->inVtabMethod = 1; - rc = pModule->xNext(pCur->pVtabCursor); - p->inVtabMethod = 0; - if( rc==SQLITE_OK ){ - res = pModule->xEof(pCur->pVtabCursor); - } - if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; + assert( pModule->xNext ); - if( !res ){ - /* If there is data, jump to P2 */ - pc = pOp->p2 - 1; - } + /* Invoke the xNext() method of the module. There is no way for the + ** underlying implementation to return an error if one occurs during + ** xNext(). Instead, if an error occurs, true is returned (indicating that + ** data is available) and the error code returned when xColumn or + ** some other method is next invoked on the save virtual table cursor. + */ + if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; + p->inVtabMethod = 1; + rc = pModule->xNext(pCur->pVtabCursor); + p->inVtabMethod = 0; + if( rc==SQLITE_OK ){ + res = pModule->xEof(pCur->pVtabCursor); } + if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; + if( !res ){ + /* If there is data, jump to P2 */ + pc = pOp->p2 - 1; + } break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VRename * * P3 +/* Opcode: VRename P1 * * P4 * ** -** P3 is a pointer to a virtual table object, an sqlite3_vtab structure. +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. ** This opcode invokes the corresponding xRename method. The value -** on the top of the stack is popped and passed as the zName argument -** to the xRename method. +** in register P1 is passed as the zName argument to the xRename method. */ -case OP_VRename: { /* no-push */ - sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3); +case OP_VRename: { + sqlite3_vtab *pVtab = pOp->p4.pVtab; + Mem *pName = &p->aMem[pOp->p1]; assert( pVtab->pModule->xRename ); + REGISTER_TRACE(pOp->p1, pName); - Stringify(pTos, encoding); + Stringify(pName, encoding); if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse; sqlite3VtabLock(pVtab); - rc = pVtab->pModule->xRename(pVtab, pTos->z); + rc = pVtab->pModule->xRename(pVtab, pName->z); sqlite3VtabUnlock(db, pVtab); if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; - popStack(&pTos, 1); break; } #endif #ifndef SQLITE_OMIT_VIRTUALTABLE -/* Opcode: VUpdate P1 P2 P3 +/* Opcode: VUpdate P1 P2 P3 P4 * ** -** P3 is a pointer to a virtual table object, an sqlite3_vtab structure. +** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. ** This opcode invokes the corresponding xUpdate method. P2 values -** are taken from the stack to pass to the xUpdate invocation. The -** value on the top of the stack corresponds to the p2th element -** of the argv array passed to xUpdate. +** are contiguous memory cells starting at P3 to pass to the xUpdate +** invocation. The value in register (P3+P2-1) corresponds to the +** p2th element of the argv array passed to xUpdate. ** ** The xUpdate method will do a DELETE or an INSERT or both. -** The argv[0] element (which corresponds to the P2-th element down -** on the stack) is the rowid of a row to delete. If argv[0] is -** NULL then no deletion occurs. The argv[1] element is the rowid -** of the new row. This can be NULL to have the virtual table -** select the new rowid for itself. The higher elements in the -** stack are the values of columns in the new row. +** The argv[0] element (which corresponds to memory cell P3) +** is the rowid of a row to delete. If argv[0] is NULL then no +** deletion occurs. The argv[1] element is the rowid of the new +** row. This can be NULL to have the virtual table select the new +** rowid for itself. The subsequent elements in the array are +** the values of columns in the new row. ** ** If P2==1 then no insert is performed. argv[0] is the rowid of ** a row to delete. @@ -42845,11 +45185,11 @@ case OP_VRename: { /* no-push */ ** is successful, then the value returned by sqlite3_last_insert_rowid() ** is set to the value of the rowid for the row just inserted. */ -case OP_VUpdate: { /* no-push */ - sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3); +case OP_VUpdate: { + sqlite3_vtab *pVtab = pOp->p4.pVtab; sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule; int nArg = pOp->p2; - assert( pOp->p3type==P3_VTAB ); + assert( pOp->p4type==P4_VTAB ); if( pModule->xUpdate==0 ){ sqlite3SetString(&p->zErrMsg, "read-only table", 0); rc = SQLITE_ERROR; @@ -42857,10 +45197,11 @@ case OP_VUpdate: { /* no-push */ int i; sqlite_int64 rowid; Mem **apArg = p->apArg; - Mem *pX = &pTos[1-nArg]; - for(i = 0; iaMem[pOp->p3]; + for(i=0; ilastRowid = rowid; } } - popStack(&pTos, nArg); break; } #endif /* SQLITE_OMIT_VIRTUALTABLE */ -/* An other opcode is illegal... +#ifndef SQLITE_OMIT_TRACE +/* Opcode: Trace * * * P4 * +** +** If tracing is enabled (by the sqlite3_trace()) interface, then +** the UTF-8 string contained in P4 is emitted on the trace callback. */ -default: { - assert( 0 ); +case OP_Trace: { + if( pOp->p4.z ){ + if( db->xTrace ){ + db->xTrace(db->pTraceArg, pOp->p4.z); + } +#ifdef SQLITE_DEBUG + if( (db->flags & SQLITE_SqlTrace)!=0 ){ + sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z); + } +#endif /* SQLITE_DEBUG */ + } + break; +} +#endif + + +/* Opcode: Noop * * * * * +** +** Do nothing. This instruction is often useful as a jump +** destination. +*/ +/* +** The magic Explain opcode are only inserted when explain==2 (which +** is to say when the EXPLAIN QUERY PLAN syntax is used.) +** This opcode records information from the optimizer. It is the +** the same as a no-op. This opcodesnever appears in a real VM program. +*/ +default: { /* This is really OP_Noop and OP_Explain */ break; } @@ -42892,9 +45262,6 @@ default: { *****************************************************************************/ } - /* Make sure the stack limit was not exceeded */ - assert( pTos<=pStackLimit ); - #ifdef VDBE_PROFILE { long long elapse = hwtime() - start; @@ -42907,73 +45274,36 @@ default: { } #endif -#ifdef SQLITE_TEST - /* Keep track of the size of the largest BLOB or STR that has appeared - ** on the top of the VDBE stack. - */ - if( pTos>=p->aStack && (pTos->flags & (MEM_Blob|MEM_Str))!=0 - && pTos->n>sqlite3_max_blobsize ){ - sqlite3_max_blobsize = pTos->n; - } -#endif - /* The following code adds nothing to the actual functionality ** of the program. It is only here for testing and debugging. ** On the other hand, it does burn CPU cycles every time through ** the evaluator loop. So we can leave it out when NDEBUG is defined. */ #ifndef NDEBUG - /* Sanity checking on the top element of the stack. If the previous - ** instruction was VNoChange, then the flags field of the top - ** of the stack is set to 0. This is technically invalid for a memory - ** cell, so avoid calling MemSanity() in this case. - */ - if( pTos>=p->aStack && pTos->flags ){ - assert( pTos->db==db ); - sqlite3VdbeMemSanity(pTos); - assert( !sqlite3VdbeMemTooBig(pTos) ); - } assert( pc>=-1 && pcnOp ); #ifdef SQLITE_DEBUG - /* Code for tracing the vdbe stack. */ - if( p->trace && pTos>=p->aStack ){ - int i; - fprintf(p->trace, "Stack:"); - for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){ - if( pTos[i].flags & MEM_Null ){ - fprintf(p->trace, " NULL"); - }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ - fprintf(p->trace, " si:%lld", pTos[i].u.i); - }else if( pTos[i].flags & MEM_Int ){ - fprintf(p->trace, " i:%lld", pTos[i].u.i); - }else if( pTos[i].flags & MEM_Real ){ - fprintf(p->trace, " r:%g", pTos[i].r); - }else{ - char zBuf[200]; - sqlite3VdbeMemPrettyPrint(&pTos[i], zBuf); - fprintf(p->trace, " "); - fprintf(p->trace, "%s", zBuf); - } + if( p->trace ){ + if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc); + if( opProperty & OPFLG_OUT2_PRERELEASE ){ + registerTrace(p->trace, pOp->p2, pOut); + } + if( opProperty & OPFLG_OUT3 ){ + registerTrace(p->trace, pOp->p3, pOut); } - if( rc!=0 ) fprintf(p->trace," rc=%d",rc); - fprintf(p->trace,"\n"); } #endif /* SQLITE_DEBUG */ #endif /* NDEBUG */ } /* The end of the for(;;) loop the loops through opcodes */ - /* If we reach this point, it means that execution is finished. + /* If we reach this point, it means that execution is finished with + ** an error of some kind. */ -vdbe_halt: - if( rc ){ - p->rc = rc; - rc = SQLITE_ERROR; - }else{ - rc = SQLITE_DONE; - } +vdbe_error_halt: + assert( rc ); + p->rc = rc; + rc = SQLITE_ERROR; sqlite3VdbeHalt(p); - p->pTos = pTos; /* This is the only way out of this procedure. We have to ** release the mutexes on btrees that were acquired at the @@ -42988,7 +45318,7 @@ vdbe_return: too_big: sqlite3SetString(&p->zErrMsg, "string or blob too big", (char*)0); rc = SQLITE_TOOBIG; - goto vdbe_halt; + goto vdbe_error_halt; /* Jump to here if a malloc() fails. */ @@ -42996,7 +45326,7 @@ no_mem: db->mallocFailed = 1; sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0); rc = SQLITE_NOMEM; - goto vdbe_halt; + goto vdbe_error_halt; /* Jump to here for an SQLITE_MISUSE error. */ @@ -43008,25 +45338,20 @@ abort_due_to_misuse: ** should hold the error number. */ abort_due_to_error: - if( p->zErrMsg==0 ){ - if( db->mallocFailed ) rc = SQLITE_NOMEM; - sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0); - } - goto vdbe_halt; + assert( p->zErrMsg==0 ); + if( db->mallocFailed ) rc = SQLITE_NOMEM; + sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0); + goto vdbe_error_halt; /* Jump to here if the sqlite3_interrupt() API sets the interrupt ** flag. */ abort_due_to_interrupt: assert( db->u1.isInterrupted ); - if( db->magic!=SQLITE_MAGIC_BUSY ){ - rc = SQLITE_MISUSE; - }else{ - rc = SQLITE_INTERRUPT; - } + rc = SQLITE_INTERRUPT; p->rc = rc; sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0); - goto vdbe_halt; + goto vdbe_error_halt; } /************** End of vdbe.c ************************************************/ @@ -43045,7 +45370,7 @@ abort_due_to_interrupt: ** ** This file contains code used to implement incremental BLOB I/O. ** -** $Id: vdbeblob.c,v 1.16 2007/08/30 01:19:59 drh Exp $ +** $Id: vdbeblob.c,v 1.20 2008/01/25 15:04:50 drh Exp $ */ @@ -43085,7 +45410,7 @@ SQLITE_API int sqlite3_blob_open( ** vdbe program will take advantage of the various transaction, ** locking and error handling infrastructure built into the vdbe. ** - ** After seeking the cursor, the vdbe executes an OP_Callback. + ** After seeking the cursor, the vdbe executes an OP_ResultRow. ** Code external to the Vdbe then "borrows" the b-tree cursor and ** uses it to implement the blob_read(), blob_write() and ** blob_bytes() functions. @@ -43097,21 +45422,20 @@ SQLITE_API int sqlite3_blob_open( static const VdbeOpList openBlob[] = { {OP_Transaction, 0, 0, 0}, /* 0: Start a transaction */ {OP_VerifyCookie, 0, 0, 0}, /* 1: Check the schema cookie */ - {OP_Integer, 0, 0, 0}, /* 2: Database number */ /* One of the following two instructions is replaced by an ** OP_Noop before exection. */ - {OP_OpenRead, 0, 0, 0}, /* 3: Open cursor 0 for reading */ - {OP_OpenWrite, 0, 0, 0}, /* 4: Open cursor 0 for read/write */ - {OP_SetNumColumns, 0, 0, 0}, /* 5: Num cols for cursor */ - - {OP_Variable, 1, 0, 0}, /* 6: Push the rowid to the stack */ - {OP_NotExists, 0, 10, 0}, /* 7: Seek the cursor */ - {OP_Column, 0, 0, 0}, /* 8 */ - {OP_Callback, 0, 0, 0}, /* 9 */ - {OP_Close, 0, 0, 0}, /* 10 */ - {OP_Halt, 0, 0, 0}, /* 11 */ + {OP_OpenRead, 0, 0, 0}, /* 2: Open cursor 0 for reading */ + {OP_OpenWrite, 0, 0, 0}, /* 3: Open cursor 0 for read/write */ + {OP_SetNumColumns, 0, 0, 0}, /* 4: Num cols for cursor */ + + {OP_Variable, 1, 1, 0}, /* 5: Push the rowid to the stack */ + {OP_NotExists, 0, 10, 1}, /* 6: Seek the cursor */ + {OP_Column, 0, 0, 1}, /* 7 */ + {OP_ResultRow, 1, 0, 0}, /* 8 */ + {OP_Close, 0, 0, 0}, /* 9 */ + {OP_Halt, 0, 0, 0}, /* 10 */ }; Vdbe *v = 0; @@ -43134,14 +45458,14 @@ SQLITE_API int sqlite3_blob_open( } sqlite3BtreeEnterAll(db); - pTab = sqlite3LocateTable(&sParse, zTable, zDb); + pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb); if( !pTab ){ if( sParse.zErrMsg ){ sqlite3_snprintf(sizeof(zErr), zErr, "%s", sParse.zErrMsg); } sqlite3_free(sParse.zErrMsg); rc = SQLITE_ERROR; - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); sqlite3BtreeLeaveAll(db); goto blob_open_out; } @@ -43155,7 +45479,7 @@ SQLITE_API int sqlite3_blob_open( if( iCol==pTab->nCol ){ sqlite3_snprintf(sizeof(zErr), zErr, "no such column: \"%s\"", zColumn); rc = SQLITE_ERROR; - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); sqlite3BtreeLeaveAll(db); goto blob_open_out; } @@ -43173,7 +45497,7 @@ SQLITE_API int sqlite3_blob_open( sqlite3_snprintf(sizeof(zErr), zErr, "cannot open indexed column for writing"); rc = SQLITE_ERROR; - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); sqlite3BtreeLeaveAll(db); goto blob_open_out; } @@ -43197,14 +45521,12 @@ SQLITE_API int sqlite3_blob_open( /* Make sure a mutex is held on the table to be accessed */ sqlite3VdbeUsesBtree(v, iDb); - /* Configure the db number pushed onto the stack */ - sqlite3VdbeChangeP1(v, 2, iDb); - /* Remove either the OP_OpenWrite or OpenRead. Set the P2 ** parameter of the other to pTab->tnum. */ - sqlite3VdbeChangeToNoop(v, (flags ? 3 : 4), 1); - sqlite3VdbeChangeP2(v, (flags ? 4 : 3), pTab->tnum); + sqlite3VdbeChangeToNoop(v, (flags ? 2 : 3), 1); + sqlite3VdbeChangeP2(v, (flags ? 3 : 2), pTab->tnum); + sqlite3VdbeChangeP3(v, (flags ? 3 : 2), iDb); /* Configure the OP_SetNumColumns. Configure the cursor to ** think that the table has one more column than it really @@ -43213,9 +45535,9 @@ SQLITE_API int sqlite3_blob_open( ** we can invoke OP_Column to fill in the vdbe cursors type ** and offset cache without causing any IO. */ - sqlite3VdbeChangeP2(v, 5, pTab->nCol+1); + sqlite3VdbeChangeP2(v, 4, pTab->nCol+1); if( !db->mallocFailed ){ - sqlite3VdbeMakeReady(v, 1, 0, 1, 0); + sqlite3VdbeMakeReady(v, 1, 1, 1, 0); } } @@ -43626,7 +45948,7 @@ SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){ ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** -** $Id: expr.c,v 1.320 2007/12/14 15:12:21 drh Exp $ +** $Id: expr.c,v 1.354 2008/03/12 10:39:00 danielk1977 Exp $ */ /* @@ -43770,15 +46092,13 @@ SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ } /* -** Return the P1 value that should be used for a binary comparison +** Return the P5 value that should be used for a binary comparison ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. -** If jumpIfNull is true, then set the low byte of the returned -** P1 value to tell the opcode to jump if either expression -** evaluates to NULL. */ -static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ - char aff = sqlite3ExprAffinity(pExpr2); - return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0); +static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ + u8 aff = (char)sqlite3ExprAffinity(pExpr2); + aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull; + return aff; } /* @@ -43823,12 +46143,20 @@ static int codeCompare( Expr *pLeft, /* The left operand */ Expr *pRight, /* The right operand */ int opcode, /* The comparison opcode */ + int in1, int in2, /* Register holding operands */ int dest, /* Jump here if true. */ int jumpIfNull /* If true, jump if either operand is NULL */ ){ - int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull); - CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); - return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ); + int p5; + int addr; + CollSeq *p4; + + p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); + p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); + addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, + (void*)p4, P4_COLLSEQ); + sqlite3VdbeChangeP5(pParse->pVdbe, p5); + return addr; } /* @@ -43895,9 +46223,8 @@ SQLITE_PRIVATE Expr *sqlite3PExpr( /* ** When doing a nested parse, you can include terms in an expression -** that look like this: #0 #1 #2 ... These terms refer to elements -** on the stack. "#0" means the top of the stack. -** "#1" means the next down on the stack. And so forth. +** that look like this: #1 #2 ... These terms refer to registers +** in the virtual machine. #N is the N-th register. ** ** This routine is called by the parser to deal with on of those terms. ** It immediately generates code to store the value in a memory location. @@ -43907,7 +46234,6 @@ SQLITE_PRIVATE Expr *sqlite3PExpr( SQLITE_PRIVATE Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ Vdbe *v = pParse->pVdbe; Expr *p; - int depth; if( pParse->nested==0 ){ sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); @@ -43917,10 +46243,7 @@ SQLITE_PRIVATE Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ if( p==0 ){ return 0; /* Malloc failed */ } - depth = atoi((char*)&pToken->z[1]); - p->iTable = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_Dup, depth, 0); - sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1); + p->iTable = atoi((char*)&pToken->z[1]); return p; } @@ -44719,14 +47042,17 @@ static int lookupName( if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ TriggerStack *pTriggerStack = pParse->trigStack; Table *pTab = 0; + u32 *piColMask; if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ pExpr->iTable = pTriggerStack->newIdx; assert( pTriggerStack->pTab ); pTab = pTriggerStack->pTab; + piColMask = &(pTriggerStack->newColMask); }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ pExpr->iTable = pTriggerStack->oldIdx; assert( pTriggerStack->pTab ); pTab = pTriggerStack->pTab; + piColMask = &(pTriggerStack->oldColMask); } if( pTab ){ @@ -44745,6 +47071,9 @@ static int lookupName( pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0); } pExpr->pTab = pTab; + if( iCol>=0 ){ + *piColMask |= ((u32)1<=32?0xffffffff:0); + } break; } } @@ -44832,23 +47161,16 @@ static int lookupName( ** more matches. Either way, we have an error. */ if( cnt!=1 ){ - char *z = 0; - char *zErr; - zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s"; + const char *zErr; + zErr = cnt==0 ? "no such column" : "ambiguous column name"; if( zDb ){ - sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0); + sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); }else if( zTab ){ - sqlite3SetString(&z, zTab, ".", zCol, (char*)0); - }else{ - z = sqlite3StrDup(zCol); - } - if( z ){ - sqlite3ErrorMsg(pParse, zErr, z); - sqlite3_free(z); - pTopNC->nErr++; + sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); }else{ - db->mallocFailed = 1; + sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); } + pTopNC->nErr++; } /* If a column from a table in pSrcList is referenced, then record @@ -45151,7 +47473,7 @@ struct QueryCoder { ** The returned value indicates the structure type, as follows: ** ** IN_INDEX_ROWID - The cursor was opened on a database table. -** IN_INDEX_INDEX - The cursor was opened on a database indec. +** IN_INDEX_INDEX - The cursor was opened on a database index. ** IN_INDEX_EPH - The cursor was opened on a specially created and ** populated epheremal table. ** @@ -45186,10 +47508,10 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique) ** or index instead of generating an epheremal table. */ if( sqlite3_enable_in_opt - && (p=pX->pSelect) && !p->pPrior + && (p=pX->pSelect)!=0 && !p->pPrior && !p->isDistinct && !p->isAgg && !p->pGroupBy && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect - && !p->pSrc->a[0].pTab->pSelect + && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN && !p->pLimit && !p->pOffset && !p->pWhere ){ @@ -45205,15 +47527,14 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique) */ assert(v); if( iCol<0 ){ - int iMem = pParse->nMem++; + int iMem = ++pParse->nMem; int iAddr; Table *pTab = p->pSrc->a[0].pTab; int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); sqlite3VdbeUsesBtree(v, iDb); - sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); - iAddr = sqlite3VdbeAddOp(v, OP_If, 0, iMem); - sqlite3VdbeAddOp(v, OP_MemInt, 1, iMem); + iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); eType = IN_INDEX_ROWID; @@ -45240,7 +47561,7 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique) && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) ){ int iDb; - int iMem = pParse->nMem++; + int iMem = ++pParse->nMem; int iAddr; char *pKey; @@ -45248,15 +47569,14 @@ SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique) iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); sqlite3VdbeUsesBtree(v, iDb); - sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); - iAddr = sqlite3VdbeAddOp(v, OP_If, 0, iMem); - sqlite3VdbeAddOp(v, OP_MemInt, 1, iMem); + iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - VdbeComment((v, "# %s", pIdx->zName)); - sqlite3VdbeOp3(v,OP_OpenRead,iTab,pIdx->tnum,pKey,P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, + pKey,P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIdx->zName)); eType = IN_INDEX_INDEX; - sqlite3VdbeAddOp(v, OP_SetNumColumns, iTab, pIdx->nColumn); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn); sqlite3VdbeJumpHere(v, iAddr); } @@ -45304,11 +47624,10 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ ** save the results, and reuse the same result on subsequent invocations. */ if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ - int mem = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0); - testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0); + int mem = ++pParse->nMem; + sqlite3VdbeAddOp1(v, OP_If, mem); + testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); assert( testAddr>0 || pParse->db->mallocFailed ); - sqlite3VdbeAddOp(v, OP_MemInt, 1, mem); } switch( pExpr->op ){ @@ -45333,10 +47652,10 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ ** is used. */ pExpr->iTable = pParse->nTab++; - addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0); + addr = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable); memset(&keyInfo, 0, sizeof(keyInfo)); keyInfo.nField = 1; - sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, pExpr->iTable, 1); if( pExpr->pSelect ){ /* Case 1: expr IN (SELECT ...) @@ -45344,10 +47663,13 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ ** Generate code to write the results of the select into the temporary ** table allocated and opened above. */ - int iParm = pExpr->iTable + (((int)affinity)<<16); + SelectDest dest; ExprList *pEList; + + sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); + dest.affinity = (int)affinity; assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); - if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){ + if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){ return; } pEList = pExpr->pSelect->pEList; @@ -45366,6 +47688,7 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ int i; ExprList *pList = pExpr->pList; struct ExprList_item *pItem; + int r1, r2; if( !affinity ){ affinity = SQLITE_AFF_NONE; @@ -45373,6 +47696,8 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ keyInfo.aColl[0] = pExpr->pLeft->pColl; /* Loop through each expression in . */ + r1 = sqlite3GetTempReg(pParse); + r2 = sqlite3GetTempReg(pParse); for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ Expr *pE2 = pItem->pExpr; @@ -45381,18 +47706,20 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ ** this code only executes once. Because for a non-constant ** expression we need to rerun this code each time. */ - if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){ - sqlite3VdbeChangeToNoop(v, testAddr-1, 3); + if( testAddr && !sqlite3ExprIsConstant(pE2) ){ + sqlite3VdbeChangeToNoop(v, testAddr-1, 2); testAddr = 0; } /* Evaluate the expression and insert it into the temp table */ - sqlite3ExprCode(pParse, pE2); - sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); - sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0); + sqlite3ExprCode(pParse, pE2, r1); + sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); } + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempReg(pParse, r2); } - sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO); + sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); break; } @@ -45404,31 +47731,31 @@ SQLITE_PRIVATE void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ */ static const Token one = { (u8*)"1", 0, 1 }; Select *pSel; - int iMem; - int sop; + SelectDest dest; - pExpr->iColumn = iMem = pParse->nMem++; pSel = pExpr->pSelect; + sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); if( pExpr->op==TK_SELECT ){ - sop = SRT_Mem; - sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0); - VdbeComment((v, "# Init subquery result")); + dest.eDest = SRT_Mem; + sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); + VdbeComment((v, "Init subquery result")); }else{ - sop = SRT_Exists; - sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem); - VdbeComment((v, "# Init EXISTS result")); + dest.eDest = SRT_Exists; + sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); + VdbeComment((v, "Init EXISTS result")); } sqlite3ExprDelete(pSel->pLimit); pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); - if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){ + if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){ return; } + pExpr->iColumn = dest.iParm; break; } } if( testAddr ){ - sqlite3VdbeJumpHere(v, testAddr); + sqlite3VdbeJumpHere(v, testAddr-1); } return; @@ -45448,13 +47775,13 @@ static char *dup8bytes(Vdbe *v, const char *in){ /* ** Generate an instruction that will put the floating point -** value described by z[0..n-1] on the stack. +** value described by z[0..n-1] into register iMem. ** ** The z[] string will probably not be zero-terminated. But the ** z[n] character is guaranteed to be something that does not look ** like the continuation of the number. */ -static void codeReal(Vdbe *v, const char *z, int n, int negateFlag){ +static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); if( z ){ double value; @@ -45463,36 +47790,36 @@ static void codeReal(Vdbe *v, const char *z, int n, int negateFlag){ sqlite3AtoF(z, &value); if( negateFlag ) value = -value; zV = dup8bytes(v, (char*)&value); - sqlite3VdbeOp3(v, OP_Real, 0, 0, zV, P3_REAL); + sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); } } /* ** Generate an instruction that will put the integer describe by -** text z[0..n-1] on the stack. +** text z[0..n-1] into register iMem. ** ** The z[] string will probably not be zero-terminated. But the ** z[n] character is guaranteed to be something that does not look ** like the continuation of the number. */ -static void codeInteger(Vdbe *v, const char *z, int n, int negateFlag){ +static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){ assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); if( z ){ int i; assert( !isdigit(z[n]) ); if( sqlite3GetInt32(z, &i) ){ - if( negateFlag ) i = -i; - sqlite3VdbeAddOp(v, OP_Integer, i, 0); - }else if( sqlite3FitsIn64Bits(z, negateFlag) ){ + if( negFlag ) i = -i; + sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); + }else if( sqlite3FitsIn64Bits(z, negFlag) ){ i64 value; char *zV; sqlite3Atoi64(z, &value); - if( negateFlag ) value = -value; + if( negFlag ) value = -value; zV = dup8bytes(v, (char*)&value); - sqlite3VdbeOp3(v, OP_Int64, 0, 0, zV, P3_INT64); + sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); }else{ - codeReal(v, z, n, negateFlag); + codeReal(v, z, n, negFlag, iMem); } } } @@ -45500,23 +47827,29 @@ static void codeInteger(Vdbe *v, const char *z, int n, int negateFlag){ /* ** Generate code that will extract the iColumn-th column from -** table pTab and push that column value on the stack. There -** is an open cursor to pTab in iTable. If iColumn<0 then -** code is generated that extracts the rowid. -*/ -SQLITE_PRIVATE void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){ +** table pTab and store the column value in register iReg. +** There is an open cursor to pTab in +** iTable. If iColumn<0 then code is generated that extracts the rowid. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeGetColumn( + Vdbe *v, /* The VM being created */ + Table *pTab, /* Description of the table we are reading from */ + int iColumn, /* Index of the table column */ + int iTable, /* The cursor pointing to the table */ + int iReg /* Store results here */ +){ if( iColumn<0 ){ int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; - sqlite3VdbeAddOp(v, op, iTable, 0); + sqlite3VdbeAddOp2(v, op, iTable, iReg); }else if( pTab==0 ){ - sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn); + sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); }else{ int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; - sqlite3VdbeAddOp(v, op, iTable, iColumn); + sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); sqlite3ColumnDefault(v, pTab, iColumn); #ifndef SQLITE_OMIT_FLOATING_POINT if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ - sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0); + sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); } #endif } @@ -45524,35 +47857,43 @@ SQLITE_PRIVATE void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, /* ** Generate code into the current Vdbe to evaluate the given -** expression and leave the result on the top of stack. -** -** This code depends on the fact that certain token values (ex: TK_EQ) -** are the same as opcode values (ex: OP_Eq) that implement the corresponding -** operation. Special comments in vdbe.c and the mkopcodeh.awk script in -** the make process cause these values to align. Assert()s in the code -** below verify that the numbers are aligned correctly. -*/ -SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ - Vdbe *v = pParse->pVdbe; - int op; - int stackChng = 1; /* Amount of change to stack depth */ +** expression. Attempt to store the results in register "target". +** Return the register where results are stored. +** +** With this routine, there is no guaranteed that results will +** be stored in target. The result might be stored in some other +** register if it is convenient to do so. The calling function +** must check the return code and move the results to the desired +** register. +*/ +static int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ + Vdbe *v = pParse->pVdbe; /* The VM under construction */ + int op; /* The opcode being coded */ + int inReg = target; /* Results stored in register inReg */ + int regFree1 = 0; /* If non-zero free this temporary register */ + int regFree2 = 0; /* If non-zero free this temporary register */ + int r1, r2, r3; /* Various register numbers */ + + assert( v!=0 || pParse->db->mallocFailed ); + assert( target>0 && target<=pParse->nMem ); + if( v==0 ) return 0; - if( v==0 ) return; if( pExpr==0 ){ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); - return; + op = TK_NULL; + }else{ + op = pExpr->op; } - op = pExpr->op; switch( op ){ case TK_AGG_COLUMN: { AggInfo *pAggInfo = pExpr->pAggInfo; struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; if( !pAggInfo->directMode ){ - sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0); + assert( pCol->iMem>0 ); + inReg = pCol->iMem; break; }else if( pAggInfo->useSortingIdx ){ - sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx, - pCol->iSorterColumn); + sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, + pCol->iSorterColumn, target); break; } /* Otherwise, fall thru into the TK_COLUMN case */ @@ -45560,61 +47901,64 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ case TK_COLUMN: { if( pExpr->iTable<0 ){ /* This only happens when coding check constraints */ - assert( pParse->ckOffset>0 ); - sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1); + assert( pParse->ckBase>0 ); + inReg = pExpr->iColumn + pParse->ckBase; }else{ - sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable); + sqlite3ExprCodeGetColumn(v, pExpr->pTab, + pExpr->iColumn, pExpr->iTable, target); } break; } case TK_INTEGER: { - codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0); + codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); break; } case TK_FLOAT: { - codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0); + codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); break; } case TK_STRING: { sqlite3DequoteExpr(pParse->db, pExpr); - sqlite3VdbeOp3(v,OP_String8, 0, 0, (char*)pExpr->token.z, pExpr->token.n); + sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, + (char*)pExpr->token.z, pExpr->token.n); break; } case TK_NULL: { - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, target); break; } #ifndef SQLITE_OMIT_BLOB_LITERAL case TK_BLOB: { int n; const char *z; - assert( TK_BLOB==OP_HexBlob ); + char *zBlob; + assert( pExpr->token.n>=3 ); + assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); + assert( pExpr->token.z[1]=='\'' ); + assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); n = pExpr->token.n - 3; z = (char*)pExpr->token.z + 2; - assert( n>=0 ); - if( n==0 ){ - z = ""; - } - sqlite3VdbeOp3(v, op, 0, 0, z, n); + zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); + sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); break; } #endif case TK_VARIABLE: { - sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0); + sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); if( pExpr->token.n>1 ){ - sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n); + sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); } break; } case TK_REGISTER: { - sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0); + inReg = pExpr->iTable; break; } #ifndef SQLITE_OMIT_CAST case TK_CAST: { /* Expressions of the form: CAST(pLeft AS token) */ int aff, to_op; - sqlite3ExprCode(pParse, pExpr->pLeft); + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); aff = sqlite3AffinityType(&pExpr->token); to_op = aff - SQLITE_AFF_TEXT + OP_ToText; assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); @@ -45622,8 +47966,7 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); - sqlite3VdbeAddOp(v, to_op, 0, 0); - stackChng = 0; + sqlite3VdbeAddOp1(v, to_op, inReg); break; } #endif /* SQLITE_OMIT_CAST */ @@ -45639,10 +47982,10 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ assert( TK_GE==OP_Ge ); assert( TK_EQ==OP_Eq ); assert( TK_NE==OP_Ne ); - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3ExprCode(pParse, pExpr->pRight); - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0); - stackChng = -1; + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, inReg, SQLITE_STOREP2); break; } case TK_AND: @@ -45668,10 +48011,9 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ assert( TK_LSHIFT==OP_ShiftLeft ); assert( TK_RSHIFT==OP_ShiftRight ); assert( TK_CONCAT==OP_Concat ); - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3ExprCode(pParse, pExpr->pRight); - sqlite3VdbeAddOp(v, op, 0, 0); - stackChng = -1; + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + sqlite3VdbeAddOp3(v, op, r2, r1, target); break; } case TK_UMINUS: { @@ -45680,34 +48022,37 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ Token *p = &pLeft->token; if( pLeft->op==TK_FLOAT ){ - codeReal(v, (char*)p->z, p->n, 1); + codeReal(v, (char*)p->z, p->n, 1, target); }else{ - codeInteger(v, (char*)p->z, p->n, 1); + codeInteger(v, (char*)p->z, p->n, 1, target); } - break; + }else{ + regFree1 = r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); + r2 = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); } - /* Fall through into TK_NOT */ + inReg = target; + break; } case TK_BITNOT: case TK_NOT: { assert( TK_BITNOT==OP_BitNot ); assert( TK_NOT==OP_Not ); - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3VdbeAddOp(v, op, 0, 0); - stackChng = 0; + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + sqlite3VdbeAddOp1(v, op, inReg); break; } case TK_ISNULL: case TK_NOTNULL: { - int dest; + int addr; assert( TK_ISNULL==OP_IsNull ); assert( TK_NOTNULL==OP_NotNull ); - sqlite3VdbeAddOp(v, OP_Integer, 1, 0); - sqlite3ExprCode(pParse, pExpr->pLeft); - dest = sqlite3VdbeCurrentAddr(v) + 2; - sqlite3VdbeAddOp(v, op, 1, dest); - sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); - stackChng = 0; + sqlite3VdbeAddOp2(v, OP_Integer, 1, target); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + addr = sqlite3VdbeAddOp1(v, op, r1); + sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); + sqlite3VdbeJumpHere(v, addr); break; } case TK_AGG_FUNCTION: { @@ -45716,7 +48061,7 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", &pExpr->span); }else{ - sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0); + inReg = pInfo->aFunc[pExpr->iAgg].iMem; } break; } @@ -45737,7 +48082,13 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ nId = pExpr->token.n; pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0); assert( pDef!=0 ); - nExpr = sqlite3ExprCodeExprList(pParse, pList); + if( pList ){ + nExpr = pList->nExpr; + r1 = sqlite3GetTempRange(pParse, nExpr); + sqlite3ExprCodeExprList(pParse, pList, r1); + }else{ + nExpr = r1 = 0; + } #ifndef SQLITE_OMIT_VIRTUALTABLE /* Possibly overload the function if the first argument is ** a virtual table column. @@ -45767,10 +48118,14 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ } if( pDef->needCollSeq ){ if( !pColl ) pColl = pParse->db->pDfltColl; - sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); + sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, + (char*)pDef, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, nExpr); + if( nExpr ){ + sqlite3ReleaseTempRange(pParse, r1, nExpr); } - sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF); - stackChng = 1-nExpr; break; } #ifndef SQLITE_OMIT_SUBQUERY @@ -45779,80 +48134,116 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ if( pExpr->iColumn==0 ){ sqlite3CodeSubselect(pParse, pExpr); } - sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); - VdbeComment((v, "# load subquery result")); + inReg = pExpr->iColumn; break; } case TK_IN: { - int addr; + int j1, j2, j3, j4, j5; char affinity; - int ckOffset = pParse->ckOffset; int eType; - int iLabel = sqlite3VdbeMakeLabel(v); eType = sqlite3FindInIndex(pParse, pExpr, 0); /* Figure out the affinity to use to create a key from the results ** of the expression. affinityStr stores a static string suitable for - ** P3 of OP_MakeRecord. + ** P4 of OP_MakeRecord. */ affinity = comparisonAffinity(pExpr); - sqlite3VdbeAddOp(v, OP_Integer, 1, 0); - pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0); + sqlite3VdbeAddOp2(v, OP_Integer, 1, target); /* Code the from " IN (...)". The temporary table ** pExpr->iTable contains the values that make up the (...) set. */ - sqlite3ExprCode(pParse, pExpr->pLeft); - addr = sqlite3VdbeCurrentAddr(v); - sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */ - sqlite3VdbeAddOp(v, OP_Pop, 2, 0); - sqlite3VdbeAddOp(v, OP_Null, 0, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, iLabel); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + j2 = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, j1); if( eType==IN_INDEX_ROWID ){ - int iAddr = sqlite3VdbeCurrentAddr(v)+3; - sqlite3VdbeAddOp(v, OP_MustBeInt, 1, iAddr); - sqlite3VdbeAddOp(v, OP_NotExists, pExpr->iTable, iAddr); - sqlite3VdbeAddOp(v, OP_Goto, pExpr->iTable, iLabel); + j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, r1, 0, 1); + j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1); + j5 = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, j3); + sqlite3VdbeJumpHere(v, j4); }else{ - sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */ - sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, iLabel); + r2 = regFree2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); + j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); } - sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */ - sqlite3VdbeResolveLabel(v, iLabel); - + sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); + sqlite3VdbeJumpHere(v, j2); + sqlite3VdbeJumpHere(v, j5); break; } #endif + /* + ** x BETWEEN y AND z + ** + ** This is equivalent to + ** + ** x>=y AND x<=z + ** + ** X is stored in pExpr->pLeft. + ** Y is stored in pExpr->pList->a[0].pExpr. + ** Z is stored in pExpr->pList->a[1].pExpr. + */ case TK_BETWEEN: { Expr *pLeft = pExpr->pLeft; struct ExprList_item *pLItem = pExpr->pList->a; Expr *pRight = pLItem->pExpr; - sqlite3ExprCode(pParse, pLeft); - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - sqlite3ExprCode(pParse, pRight); - codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0); - sqlite3VdbeAddOp(v, OP_Pull, 1, 0); + + r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); + r3 = sqlite3GetTempReg(pParse); + codeCompare(pParse, pLeft, pRight, OP_Ge, + r1, r2, r3, SQLITE_STOREP2); pLItem++; pRight = pLItem->pExpr; - sqlite3ExprCode(pParse, pRight); - codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0); - sqlite3VdbeAddOp(v, OP_And, 0, 0); + sqlite3ReleaseTempReg(pParse, regFree2); + r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); + codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r2, SQLITE_STOREP2); + sqlite3VdbeAddOp3(v, OP_And, r3, r2, target); + sqlite3ReleaseTempReg(pParse, r3); break; } case TK_UPLUS: { - sqlite3ExprCode(pParse, pExpr->pLeft); - stackChng = 0; + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); break; } + + /* + ** Form A: + ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END + ** + ** Form B: + ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END + ** + ** Form A is can be transformed into the equivalent form B as follows: + ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... + ** WHEN x=eN THEN rN ELSE y END + ** + ** X (if it exists) is in pExpr->pLeft. + ** Y is in pExpr->pRight. The Y is also optional. If there is no + ** ELSE clause and no other term matches, then the result of the + ** exprssion is NULL. + ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. + ** + ** The result of the expression is the Ri for the first matching Ei, + ** or if there is no matching Ei, the ELSE term Y, or if there is + ** no ELSE term, NULL. + */ case TK_CASE: { - int expr_end_label; - int jumpInst; - int nExpr; - int i; - ExprList *pEList; - struct ExprList_item *aListelem; + int endLabel; /* GOTO label for end of CASE stmt */ + int nextCase; /* GOTO label for next WHEN clause */ + int nExpr; /* 2x number of WHEN terms */ + int i; /* Loop counter */ + ExprList *pEList; /* List of WHEN terms */ + struct ExprList_item *aListelem; /* Array of WHEN terms */ + Expr opCompare; /* The X==Ei expression */ + Expr cacheX; /* Cached expression X */ + Expr *pX; /* The X expression */ + Expr *pTest; /* X==Ei (form A) or just Ei (form B) */ assert(pExpr->pList); assert((pExpr->pList->nExpr % 2) == 0); @@ -45860,33 +48251,33 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ pEList = pExpr->pList; aListelem = pEList->a; nExpr = pEList->nExpr; - expr_end_label = sqlite3VdbeMakeLabel(v); - if( pExpr->pLeft ){ - sqlite3ExprCode(pParse, pExpr->pLeft); + endLabel = sqlite3VdbeMakeLabel(v); + if( (pX = pExpr->pLeft)!=0 ){ + cacheX = *pX; + cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); + cacheX.op = TK_REGISTER; + opCompare.op = TK_EQ; + opCompare.pLeft = &cacheX; + pTest = &opCompare; } for(i=0; ipLeft ){ - sqlite3VdbeAddOp(v, OP_Dup, 1, 1); - jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr, - OP_Ne, 0, 1); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); + if( pX ){ + opCompare.pRight = aListelem[i].pExpr; }else{ - jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0); + pTest = aListelem[i].pExpr; } - sqlite3ExprCode(pParse, aListelem[i+1].pExpr); - sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label); - sqlite3VdbeJumpHere(v, jumpInst); - } - if( pExpr->pLeft ){ - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); + nextCase = sqlite3VdbeMakeLabel(v); + sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); + sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); + sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); + sqlite3VdbeResolveLabel(v, nextCase); } if( pExpr->pRight ){ - sqlite3ExprCode(pParse, pExpr->pRight); + sqlite3ExprCode(pParse, pExpr->pRight, target); }else{ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, target); } - sqlite3VdbeResolveLabel(v, expr_end_label); + sqlite3VdbeResolveLabel(v, endLabel); break; } #ifndef SQLITE_OMIT_TRIGGER @@ -45894,78 +48285,118 @@ SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr){ if( !pParse->trigStack ){ sqlite3ErrorMsg(pParse, "RAISE() may only be used within a trigger-program"); - return; + return 0; } if( pExpr->iColumn!=OE_Ignore ){ assert( pExpr->iColumn==OE_Rollback || pExpr->iColumn == OE_Abort || pExpr->iColumn == OE_Fail ); sqlite3DequoteExpr(pParse->db, pExpr); - sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, (char*)pExpr->token.z, pExpr->token.n); } else { assert( pExpr->iColumn == OE_Ignore ); - sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump); - VdbeComment((v, "# raise(IGNORE)")); + sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); + sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); + VdbeComment((v, "raise(IGNORE)")); } - stackChng = 0; break; } #endif } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); + return inReg; +} + +/* +** Generate code to evaluate an expression and store the results +** into a register. Return the register number where the results +** are stored. +** +** If the register is a temporary register that can be deallocated, +** then write its number into *pReg. If the result register is no +** a temporary, then set *pReg to zero. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ + int r1 = sqlite3GetTempReg(pParse); + int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); + if( r2==r1 ){ + *pReg = r1; + }else{ + sqlite3ReleaseTempReg(pParse, r1); + *pReg = 0; + } + return r2; +} - if( pParse->ckOffset ){ - pParse->ckOffset += stackChng; - assert( pParse->ckOffset ); +/* +** Generate code that will evaluate expression pExpr and store the +** results in register target. The results are guaranteed to appear +** in register target. +*/ +SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ + int inReg; + + assert( target>0 && target<=pParse->nMem ); + inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); + assert( pParse->pVdbe || pParse->db->mallocFailed ); + if( inReg!=target && pParse->pVdbe ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); } + return target; } -#ifndef SQLITE_OMIT_TRIGGER /* -** Generate code that evalutes the given expression and leaves the result -** on the stack. See also sqlite3ExprCode(). +** Generate code that evalutes the given expression and puts the result +** in register target. ** -** This routine might also cache the result and modify the pExpr tree -** so that it will make use of the cached result on subsequent evaluations -** rather than evaluate the whole expression again. Trivial expressions are -** not cached. If the expression is cached, its result is stored in a -** memory location. +** Also make a copy of the expression results into another "cache" register +** and modify the expression so that the next time it is evaluated, +** the result is a copy of the cache register. +** +** This routine is used for expressions that are used multiple +** times. They are evaluated once and the results of the expression +** are reused. */ -SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){ +SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ Vdbe *v = pParse->pVdbe; - VdbeOp *pOp; - int iMem; - int addr1, addr2; - if( v==0 ) return; - addr1 = sqlite3VdbeCurrentAddr(v); - sqlite3ExprCode(pParse, pExpr); - addr2 = sqlite3VdbeCurrentAddr(v); - if( addr2>addr1+1 - || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){ - iMem = pExpr->iTable = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); + int inReg; + inReg = sqlite3ExprCode(pParse, pExpr, target); + assert( target>0 ); + if( pExpr->op!=TK_REGISTER ){ + int iMem; + iMem = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); + pExpr->iTable = iMem; pExpr->op = TK_REGISTER; } + return inReg; } -#endif + /* ** Generate code that pushes the value of every element of the given -** expression list onto the stack. +** expression list into a sequence of registers beginning at target. ** -** Return the number of elements pushed onto the stack. +** Return the number of elements evaluated. */ SQLITE_PRIVATE int sqlite3ExprCodeExprList( Parse *pParse, /* Parsing context */ - ExprList *pList /* The expression list to be coded */ + ExprList *pList, /* The expression list to be coded */ + int target /* Where to write results */ ){ struct ExprList_item *pItem; int i, n; - if( pList==0 ) return 0; + assert( pList!=0 || pParse->db->mallocFailed ); + if( pList==0 ){ + return 0; + } + assert( target>0 ); n = pList->nExpr; for(pItem=pList->a, i=n; i>0; i--, pItem++){ - sqlite3ExprCode(pParse, pItem->pExpr); + sqlite3ExprCode(pParse, pItem->pExpr, target); + target++; } return n; } @@ -45976,7 +48407,7 @@ SQLITE_PRIVATE int sqlite3ExprCodeExprList( ** continues straight thru if the expression is false. ** ** If the expression evaluates to NULL (neither true nor false), then -** take the jump if the jumpIfNull flag is true. +** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. ** ** This code depends on the fact that certain token values (ex: TK_EQ) ** are the same as opcode values (ex: OP_Eq) that implement the corresponding @@ -45987,13 +48418,17 @@ SQLITE_PRIVATE int sqlite3ExprCodeExprList( SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ Vdbe *v = pParse->pVdbe; int op = 0; - int ckOffset = pParse->ckOffset; + int regFree1 = 0; + int regFree2 = 0; + int r1, r2; + + assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); if( v==0 || pExpr==0 ) return; op = pExpr->op; switch( op ){ case TK_AND: { int d2 = sqlite3VdbeMakeLabel(v); - sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); sqlite3VdbeResolveLabel(v, d2); break; @@ -46019,50 +48454,58 @@ SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int assert( TK_GE==OP_Ge ); assert( TK_EQ==OP_Eq ); assert( TK_NE==OP_Ne ); - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3ExprCode(pParse, pExpr->pRight); - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, jumpIfNull); break; } case TK_ISNULL: case TK_NOTNULL: { assert( TK_ISNULL==OP_IsNull ); assert( TK_NOTNULL==OP_NotNull ); - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3VdbeAddOp(v, op, 1, dest); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp2(v, op, r1, dest); break; } case TK_BETWEEN: { - /* The expression "x BETWEEN y AND z" is implemented as: + /* x BETWEEN y AND z + ** + ** Is equivalent to + ** + ** x>=y AND x<=z ** - ** 1 IF (x < y) GOTO 3 - ** 2 IF (x <= z) GOTO - ** 3 ... + ** Code it as such, taking care to do the common subexpression + ** elementation of x. */ - int addr; - Expr *pLeft = pExpr->pLeft; - Expr *pRight = pExpr->pList->a[0].pExpr; - sqlite3ExprCode(pParse, pLeft); - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - sqlite3ExprCode(pParse, pRight); - addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull); - - pRight = pExpr->pList->a[1].pExpr; - sqlite3ExprCode(pParse, pRight); - codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull); - - sqlite3VdbeAddOp(v, OP_Integer, 0, 0); - sqlite3VdbeJumpHere(v, addr); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); + Expr exprAnd; + Expr compLeft; + Expr compRight; + Expr exprX; + + exprX = *pExpr->pLeft; + exprAnd.op = TK_AND; + exprAnd.pLeft = &compLeft; + exprAnd.pRight = &compRight; + compLeft.op = TK_GE; + compLeft.pLeft = &exprX; + compLeft.pRight = pExpr->pList->a[0].pExpr; + compRight.op = TK_LE; + compRight.pLeft = &exprX; + compRight.pRight = pExpr->pList->a[1].pExpr; + exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); + exprX.op = TK_REGISTER; + sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); break; } default: { - sqlite3ExprCode(pParse, pExpr); - sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest); + r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); + sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); break; } } - pParse->ckOffset = ckOffset; + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); } /* @@ -46071,12 +48514,17 @@ SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int ** continues straight thru if the expression is true. ** ** If the expression evaluates to NULL (neither true nor false) then -** jump if jumpIfNull is true or fall through if jumpIfNull is false. +** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull +** is 0. */ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ Vdbe *v = pParse->pVdbe; int op = 0; - int ckOffset = pParse->ckOffset; + int regFree1 = 0; + int regFree2 = 0; + int r1, r2; + + assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); if( v==0 || pExpr==0 ) return; /* The value of pExpr->op and op are related as follows: @@ -46118,7 +48566,7 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int } case TK_OR: { int d2 = sqlite3VdbeMakeLabel(v); - sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); sqlite3VdbeResolveLabel(v, d2); break; @@ -46133,47 +48581,56 @@ SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int case TK_GE: case TK_NE: case TK_EQ: { - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3ExprCode(pParse, pExpr->pRight); - codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, jumpIfNull); break; } case TK_ISNULL: case TK_NOTNULL: { - sqlite3ExprCode(pParse, pExpr->pLeft); - sqlite3VdbeAddOp(v, op, 1, dest); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp2(v, op, r1, dest); break; } case TK_BETWEEN: { - /* The expression is "x BETWEEN y AND z". It is implemented as: + /* x BETWEEN y AND z + ** + ** Is equivalent to + ** + ** x>=y AND x<=z ** - ** 1 IF (x >= y) GOTO 3 - ** 2 GOTO - ** 3 IF (x > z) GOTO + ** Code it as such, taking care to do the common subexpression + ** elementation of x. */ - int addr; - Expr *pLeft = pExpr->pLeft; - Expr *pRight = pExpr->pList->a[0].pExpr; - sqlite3ExprCode(pParse, pLeft); - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - sqlite3ExprCode(pParse, pRight); - addr = sqlite3VdbeCurrentAddr(v); - codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull); - - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, dest); - pRight = pExpr->pList->a[1].pExpr; - sqlite3ExprCode(pParse, pRight); - codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull); + Expr exprAnd; + Expr compLeft; + Expr compRight; + Expr exprX; + + exprX = *pExpr->pLeft; + exprAnd.op = TK_AND; + exprAnd.pLeft = &compLeft; + exprAnd.pRight = &compRight; + compLeft.op = TK_GE; + compLeft.pLeft = &exprX; + compLeft.pRight = pExpr->pList->a[0].pExpr; + compRight.op = TK_LE; + compRight.pLeft = &exprX; + compRight.pRight = pExpr->pList->a[1].pExpr; + exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); + exprX.op = TK_REGISTER; + sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); break; } default: { - sqlite3ExprCode(pParse, pExpr); - sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest); + r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); + sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); break; } } - pParse->ckOffset = ckOffset; + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); } /* @@ -46304,7 +48761,7 @@ static int analyzeAggregate(void *pArg, Expr *pExpr){ pCol->pTab = pExpr->pTab; pCol->iTable = pExpr->iTable; pCol->iColumn = pExpr->iColumn; - pCol->iMem = pParse->nMem++; + pCol->iMem = ++pParse->nMem; pCol->iSorterColumn = -1; pCol->pExpr = pExpr; if( pAggInfo->pGroupBy ){ @@ -46360,7 +48817,7 @@ static int analyzeAggregate(void *pArg, Expr *pExpr){ if( i>=0 ){ pItem = &pAggInfo->aFunc[i]; pItem->pExpr = pExpr; - pItem->iMem = pParse->nMem++; + pItem->iMem = ++pParse->nMem; pItem->pFunc = sqlite3FindFunction(pParse->db, (char*)pExpr->token.z, pExpr->token.n, pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); @@ -46399,14 +48856,9 @@ static int analyzeAggregate(void *pArg, Expr *pExpr){ ** ** This routine should only be called after the expression has been ** analyzed by sqlite3ExprResolveNames(). -** -** If errors are seen, leave an error message in zErrMsg and return -** the number of errors. */ -SQLITE_PRIVATE int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ - int nErr = pNC->pParse->nErr; +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ walkExprTree(pExpr, analyzeAggregate, pNC); - return pNC->pParse->nErr - nErr; } /* @@ -46415,16 +48867,53 @@ SQLITE_PRIVATE int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ ** ** If an error is found, the analysis is cut short. */ -SQLITE_PRIVATE int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ struct ExprList_item *pItem; int i; - int nErr = 0; if( pList ){ - for(pItem=pList->a, i=0; nErr==0 && inExpr; i++, pItem++){ - nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); + for(pItem=pList->a, i=0; inExpr; i++, pItem++){ + sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); } } - return nErr; +} + +/* +** Allocate or deallocate temporary use registers during code generation. +*/ +SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){ + if( pParse->nTempReg ){ + return pParse->aTempReg[--pParse->nTempReg]; + }else{ + return ++pParse->nMem; + } +} +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ + if( iReg && pParse->nTempRegaTempReg) ){ + assert( iReg>0 ); + pParse->aTempReg[pParse->nTempReg++] = iReg; + } +} + +/* +** Allocate or deallocate a block of nReg consecutive registers +*/ +SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){ + int i; + if( nReg<=pParse->nRangeReg ){ + i = pParse->iRangeReg; + pParse->iRangeReg += nReg; + pParse->nRangeReg -= nReg; + }else{ + i = pParse->nMem+1; + pParse->nMem += nReg; + } + return i; +} +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ + if( nReg>pParse->nRangeReg ){ + pParse->nRangeReg = nReg; + pParse->iRangeReg = iReg; + } } /************** End of expr.c ************************************************/ @@ -46443,7 +48932,7 @@ SQLITE_PRIVATE int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ ** This file contains C code routines that used to generate VDBE code ** that implements the ALTER TABLE command. ** -** $Id: alter.c,v 1.35 2007/12/13 21:54:11 drh Exp $ +** $Id: alter.c,v 1.42 2008/02/09 14:30:30 drh Exp $ */ /* @@ -46507,7 +48996,7 @@ static void renameTableFunc( assert( len>0 ); } while( token!=TK_LP && token!=TK_USING ); - zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql, + zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, zTableName, tname.z+tname.n); sqlite3_result_text(context, zRet, -1, sqlite3_free); } @@ -46582,7 +49071,7 @@ static void renameTriggerFunc( /* Variable tname now contains the token that is the old table-name ** in the CREATE TRIGGER statement. */ - zRet = sqlite3MPrintf(db, "%.*s%Q%s", tname.z - zSql, zSql, + zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, zTableName, tname.z+tname.n); sqlite3_result_text(context, zRet, -1, sqlite3_free); } @@ -46672,24 +49161,24 @@ static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){ int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); assert( iTrigDb==iDb || iTrigDb==1 ); - sqlite3VdbeOp3(v, OP_DropTrigger, iTrigDb, 0, pTrig->name, 0); + sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->name, 0); } #endif /* Drop the table and index from the internal schema */ - sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0); + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); /* Reload the table, index and permanent trigger schemas. */ zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); if( !zWhere ) return; - sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, zWhere, P3_DYNAMIC); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); #ifndef SQLITE_OMIT_TRIGGER /* Now, if the table is not stored in the temp database, reload any temp ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. */ if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ - sqlite3VdbeOp3(v, OP_ParseSchema, 1, 0, zWhere, P3_DYNAMIC); + sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC); } #endif } @@ -46720,7 +49209,7 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable( assert( pSrc->nSrc==1 ); assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); - pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase); + pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); if( !pTab ) goto exit_rename_table; iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); zDb = db->aDb[iDb].zName; @@ -46782,7 +49271,7 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable( goto exit_rename_table; } sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb); - sqlite3ChangeCookie(db, v, iDb); + sqlite3ChangeCookie(pParse, iDb); /* If this is a virtual table, invoke the xRename() function if ** one is defined. The xRename() callback will modify the names @@ -46791,8 +49280,9 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable( */ #ifndef SQLITE_OMIT_VIRTUALTABLE if( isVirtualRename ){ - sqlite3VdbeOp3(v, OP_String8, 0, 0, zName, 0); - sqlite3VdbeOp3(v, OP_VRename, 0, 0, (const char*)pTab->pVtab, P3_VTAB); + int i = ++pParse->nMem; + sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0); + sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pTab->pVtab, P4_VTAB); } #endif @@ -46831,7 +49321,7 @@ SQLITE_PRIVATE void sqlite3AlterRenameTable( */ if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ sqlite3NestedParse(pParse, - "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q", + "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", zDb, zName, pTab->zName); } #endif @@ -46950,7 +49440,7 @@ SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ *zEnd-- = '\0'; } sqlite3NestedParse(pParse, - "UPDATE %Q.%s SET " + "UPDATE \"%w\".%s SET " "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " "WHERE type = 'table' AND name = %Q", zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, @@ -46997,7 +49487,7 @@ SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ assert( pParse->pNewTable==0 ); assert( sqlite3BtreeHoldsAllMutexes(db) ); if( db->mallocFailed ) goto exit_begin_add_column; - pTab = sqlite3LocateTable(pParse, pSrc->a[0].zName, pSrc->a[0].zDatabase); + pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); if( !pTab ) goto exit_begin_add_column; #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -47049,7 +49539,7 @@ SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ sqlite3BeginWriteOperation(pParse, 0, iDb); v = sqlite3GetVdbe(pParse); if( !v ) goto exit_begin_add_column; - sqlite3ChangeCookie(db, v, iDb); + sqlite3ChangeCookie(pParse, iDb); exit_begin_add_column: sqlite3SrcListDelete(pSrc); @@ -47072,7 +49562,7 @@ exit_begin_add_column: ************************************************************************* ** This file contains code associated with the ANALYZE command. ** -** @(#) $Id: analyze.c,v 1.24 2007/11/15 13:10:23 danielk1977 Exp $ +** @(#) $Id: analyze.c,v 1.41 2008/01/25 15:04:49 drh Exp $ */ #ifndef SQLITE_OMIT_ANALYZE @@ -47093,6 +49583,7 @@ static void openStatTable( sqlite3 *db = pParse->db; Db *pDb; int iRootPage; + int createStat1 = 0; Table *pStat; Vdbe *v = sqlite3GetVdbe(pParse); @@ -47103,13 +49594,14 @@ static void openStatTable( if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){ /* The sqlite_stat1 tables does not exist. Create it. ** Note that a side-effect of the CREATE TABLE statement is to leave - ** the rootpage of the new table on the top of the stack. This is + ** the rootpage of the new table in register pParse->regRoot. This is ** important because the OpenWrite opcode below will be needing it. */ sqlite3NestedParse(pParse, "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)", pDb->zName ); - iRootPage = 0; /* Cause rootpage to be taken from top of stack */ + iRootPage = pParse->regRoot; + createStat1 = 1; /* Cause rootpage to be taken from top of stack */ }else if( zWhere ){ /* The sqlite_stat1 table exists. Delete all entries associated with ** the table zWhere. */ @@ -47121,7 +49613,7 @@ static void openStatTable( }else{ /* The sqlite_stat1 table already exists. Delete all rows. */ iRootPage = pStat->tnum; - sqlite3VdbeAddOp(v, OP_Clear, pStat->tnum, iDb); + sqlite3VdbeAddOp2(v, OP_Clear, pStat->tnum, iDb); } /* Open the sqlite_stat1 table for writing. Unless it was created @@ -47129,12 +49621,12 @@ static void openStatTable( ** If this vdbe did create the sqlite_stat1 table, then it must have ** already obtained a schema-lock, making the write-lock redundant. */ - if( iRootPage>0 ){ + if( !createStat1 ){ sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1"); } - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - sqlite3VdbeAddOp(v, OP_OpenWrite, iStatCur, iRootPage); - sqlite3VdbeAddOp(v, OP_SetNumColumns, iStatCur, 3); + sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur, iRootPage, iDb); + sqlite3VdbeChangeP5(v, createStat1); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, iStatCur, 3); } /* @@ -47178,19 +49670,27 @@ static void analyzeOneTable( iIdxCur = pParse->nTab; for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + int regFields; /* Register block for building records */ + int regRec; /* Register holding completed record */ + int regTemp; /* Temporary use register */ + int regCol; /* Content of a column from the table being analyzed */ + int regRowid; /* Rowid for the inserted record */ + int regF2; /* Open a cursor to the index to be analyzed */ assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) ); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - VdbeComment((v, "# %s", pIdx->zName)); - sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, - (char *)pKey, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, + (char *)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIdx->zName)); nCol = pIdx->nColumn; - if( iMem+nCol*2>=pParse->nMem ){ - pParse->nMem = iMem+nCol*2+1; + regFields = iMem+nCol*2; + regTemp = regRowid = regCol = regFields+3; + regRec = regCol+1; + if( regRec>pParse->nMem ){ + pParse->nMem = regRec; } - sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, nCol+1); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, iIdxCur, nCol+1); /* Memory cells are used as follows: ** @@ -47206,33 +49706,33 @@ static void analyzeOneTable( ** are initialized to NULL. */ for(i=0; i<=nCol; i++){ - sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem+i); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i); } for(i=0; i0 then it is always the case the D>0 so division by zero ** is never possible. */ - sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); - addr = sqlite3VdbeAddOp(v, OP_IfNot, 0, 0); - sqlite3VdbeAddOp(v, OP_NewRowid, iStatCur, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); - sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, " ", 0); + addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); + sqlite3VdbeAddOp4(v, OP_String8, 0, regFields, 0, pTab->zName, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, regFields+1, 0, pIdx->zName, 0); + regF2 = regFields+2; + sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regF2); for(i=0; inTab++; openStatTable(pParse, iDb, iStatCur, 0); - iMem = pParse->nMem; + iMem = pParse->nMem+1; for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ Table *pTab = (Table*)sqliteHashData(k); analyzeOneTable(pParse, pTab, iStatCur, iMem); @@ -47325,7 +49820,7 @@ static void analyzeTable(Parse *pParse, Table *pTab){ sqlite3BeginWriteOperation(pParse, 0, iDb); iStatCur = pParse->nTab++; openStatTable(pParse, iDb, iStatCur, pTab->zName); - analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem); + analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1); loadAnalysis(pParse, iDb); } @@ -47370,7 +49865,7 @@ SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ }else{ z = sqlite3NameFromToken(db, pName1); if( z ){ - pTab = sqlite3LocateTable(pParse, z, 0); + pTab = sqlite3LocateTable(pParse, 0, z, 0); sqlite3_free(z); if( pTab ){ analyzeTable(pParse, pTab); @@ -47384,7 +49879,7 @@ SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ zDb = db->aDb[iDb].zName; z = sqlite3NameFromToken(db, pTableName); if( z ){ - pTab = sqlite3LocateTable(pParse, z, zDb); + pTab = sqlite3LocateTable(pParse, 0, z, zDb); sqlite3_free(z); if( pTab ){ analyzeTable(pParse, pTab); @@ -47469,9 +49964,9 @@ SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ /* Load new statistics out of the sqlite_stat1 table */ zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); sqlite3_free(zSql); return rc; } @@ -47494,7 +49989,7 @@ SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ ************************************************************************* ** This file contains code used to implement the ATTACH and DETACH commands. ** -** $Id: attach.c,v 1.63 2007/10/03 08:46:44 danielk1977 Exp $ +** $Id: attach.c,v 1.72 2008/02/13 18:25:27 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_ATTACH @@ -47668,9 +50163,11 @@ static void attachFunc( ** we found it. */ if( rc==SQLITE_OK ){ - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); + sqlite3BtreeEnterAll(db); rc = sqlite3Init(db, &zErrDyn); - sqlite3SafetyOff(db); + sqlite3BtreeLeaveAll(db); + (void)sqlite3SafetyOff(db); } if( rc ){ int iDb = db->nDb - 1; @@ -47702,6 +50199,7 @@ attach_error: zErr[sizeof(zErr)-1] = 0; sqlite3_result_error(context, zErr, -1); } + if( rc ) sqlite3_result_error_code(context, rc); } /* @@ -47777,6 +50275,7 @@ static void codeAttach( Vdbe *v; FuncDef *pFunc; sqlite3* db = pParse->db; + int regArgs; #ifndef SQLITE_OMIT_AUTHORIZATION assert( db->mallocFailed || pAuthArg ); @@ -47806,21 +50305,23 @@ static void codeAttach( } v = sqlite3GetVdbe(pParse); - sqlite3ExprCode(pParse, pFilename); - sqlite3ExprCode(pParse, pDbname); - sqlite3ExprCode(pParse, pKey); + regArgs = sqlite3GetTempRange(pParse, 4); + sqlite3ExprCode(pParse, pFilename, regArgs); + sqlite3ExprCode(pParse, pDbname, regArgs+1); + sqlite3ExprCode(pParse, pKey, regArgs+2); assert( v || db->mallocFailed ); if( v ){ - sqlite3VdbeAddOp(v, OP_Function, 0, nFunc); + sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-nFunc, regArgs+3); + sqlite3VdbeChangeP5(v, nFunc); pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0); - sqlite3VdbeChangeP3(v, -1, (char *)pFunc, P3_FUNCDEF); + sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF); /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this ** statement only). For DETACH, set it to false (expire all existing ** statements). */ - sqlite3VdbeAddOp(v, OP_Expire, (type==SQLITE_ATTACH), 0); + sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH)); } attach_end: @@ -48264,7 +50765,7 @@ SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){ ** COMMIT ** ROLLBACK ** -** $Id: build.c,v 1.450 2007/12/04 16:54:53 drh Exp $ +** $Id: build.c,v 1.474 2008/03/06 09:58:50 mlcreech Exp $ */ /* @@ -48354,7 +50855,7 @@ static void codeTableLocks(Parse *pParse){ if( p->isWriteLock ){ p1 = -1*(p1+1); } - sqlite3VdbeOp3(pVdbe, OP_TableLock, p1, p->iTab, p->zName, P3_STATIC); + sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, 0, p->zName, P4_STATIC); } } #else @@ -48378,6 +50879,7 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ db = pParse->db; if( db->mallocFailed ) return; if( pParse->nested ) return; + if( pParse->nErr ) return; if( !pParse->pVdbe ){ if( pParse->rc==SQLITE_OK && pParse->nErr ){ pParse->rc = SQLITE_ERROR; @@ -48390,7 +50892,7 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ */ v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp(v, OP_Halt, 0, 0); + sqlite3VdbeAddOp0(v, OP_Halt); /* The cookie mask contains one bit for each database file open. ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are @@ -48405,13 +50907,13 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ for(iDb=0, mask=1; iDbnDb; mask<<=1, iDb++){ if( (mask & pParse->cookieMask)==0 ) continue; sqlite3VdbeUsesBtree(v, iDb); - sqlite3VdbeAddOp(v, OP_Transaction, iDb, (mask & pParse->writeMask)!=0); - sqlite3VdbeAddOp(v, OP_VerifyCookie, iDb, pParse->cookieValue[iDb]); + sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0); + sqlite3VdbeAddOp2(v,OP_VerifyCookie, iDb, pParse->cookieValue[iDb]); } #ifndef SQLITE_OMIT_VIRTUALTABLE if( pParse->pVirtualLock ){ char *vtab = (char *)pParse->pVirtualLock->pVtab; - sqlite3VdbeOp3(v, OP_VBegin, 0, 0, vtab, P3_VTAB); + sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); } #endif @@ -48420,17 +50922,19 @@ SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ ** shared-cache feature is enabled. */ codeTableLocks(pParse); - sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->cookieGoto); + sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto); } #ifndef SQLITE_OMIT_TRACE - /* Add a No-op that contains the complete text of the compiled SQL - ** statement as its P3 argument. This does not change the functionality - ** of the program. - ** - ** This is used to implement sqlite3_trace(). - */ - sqlite3VdbeOp3(v, OP_Noop, 0, 0, pParse->zSql, pParse->zTail-pParse->zSql); + if( !db->init.busy ){ + /* Change the P4 argument of the first opcode (which will always be + ** an OP_Trace) to be the complete text of the current SQL statement. + */ + VdbeOp *pOp = sqlite3VdbeGetOp(v, 0); + if( pOp && pOp->opcode==OP_Trace ){ + sqlite3VdbeChangeP4(v, 0, pParse->zSql, pParse->zTail-pParse->zSql); + } + } #endif /* SQLITE_OMIT_TRACE */ } @@ -48528,7 +51032,12 @@ SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const cha ** routine leaves an error message in pParse->zErrMsg where ** sqlite3FindTable() does not. */ -SQLITE_PRIVATE Table *sqlite3LocateTable(Parse *pParse, const char *zName, const char *zDbase){ +SQLITE_PRIVATE Table *sqlite3LocateTable( + Parse *pParse, /* context in which to report errors */ + int isView, /* True if looking for a VIEW rather than a TABLE */ + const char *zName, /* Name of the table we are looking for */ + const char *zDbase /* Name of the database. Might be NULL */ +){ Table *p; /* Read the database schema. If an error occurs, leave an error message @@ -48539,10 +51048,11 @@ SQLITE_PRIVATE Table *sqlite3LocateTable(Parse *pParse, const char *zName, const p = sqlite3FindTable(pParse->db, zName, zDbase); if( p==0 ){ + const char *zMsg = isView ? "no such view" : "no such table"; if( zDbase ){ - sqlite3ErrorMsg(pParse, "no such table: %s.%s", zDbase, zName); + sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); }else{ - sqlite3ErrorMsg(pParse, "no such table: %s", zName); + sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); } pParse->checkSchema = 1; } @@ -48643,17 +51153,22 @@ SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char */ SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){ int i, j; - assert( iDb>=0 && iDbnDb ); + + if( iDb==0 ){ + sqlite3BtreeEnterAll(db); + } for(i=iDb; inDb; i++){ Db *pDb = &db->aDb[i]; if( pDb->pSchema ){ + assert(i==1 || (pDb->pBt && sqlite3BtreeHoldsMutex(pDb->pBt))); sqlite3SchemaFree(pDb->pSchema); } if( iDb>0 ) return; } assert( iDb==0 ); db->flags &= ~SQLITE_InternChanges; + sqlite3BtreeLeaveAll(db); /* If one or more of the auxiliary database files has been closed, ** then remove them from the auxiliary database list. We take the @@ -48834,9 +51349,8 @@ SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){ Vdbe *v = sqlite3GetVdbe(p); sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - sqlite3VdbeAddOp(v, OP_OpenWrite, 0, MASTER_ROOT); - sqlite3VdbeAddOp(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */ + sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, 5); /* sqlite_master has 5 columns */ } /* @@ -49073,30 +51587,33 @@ SQLITE_PRIVATE void sqlite3StartTable( ** now. */ if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ - int lbl; + int j1; int fileFormat; + int reg1, reg2, reg3; sqlite3BeginWriteOperation(pParse, 0, iDb); #ifndef SQLITE_OMIT_VIRTUALTABLE if( isVirtual ){ - sqlite3VdbeAddOp(v, OP_VBegin, 0, 0); + sqlite3VdbeAddOp0(v, OP_VBegin); } #endif /* If the file format and encoding in the database have not been set, ** set them now. */ - sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); /* file_format */ + reg1 = pParse->regRowid = ++pParse->nMem; + reg2 = pParse->regRoot = ++pParse->nMem; + reg3 = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, 1); /* file_format */ sqlite3VdbeUsesBtree(v, iDb); - lbl = sqlite3VdbeMakeLabel(v); - sqlite3VdbeAddOp(v, OP_If, 0, lbl); + j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? 1 : SQLITE_MAX_FILE_FORMAT; - sqlite3VdbeAddOp(v, OP_Integer, fileFormat, 0); - sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); - sqlite3VdbeAddOp(v, OP_Integer, ENC(db), 0); - sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 4); - sqlite3VdbeResolveLabel(v, lbl); + sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 1, reg3); + sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 4, reg3); + sqlite3VdbeJumpHere(v, j1); /* This just creates a place-holder record in the sqlite_master table. ** The record created does not contain anything yet. It will be replaced @@ -49108,19 +51625,18 @@ SQLITE_PRIVATE void sqlite3StartTable( */ #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) if( isView || isVirtual ){ - sqlite3VdbeAddOp(v, OP_Integer, 0, 0); + sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); }else #endif { - sqlite3VdbeAddOp(v, OP_CreateTable, iDb, 0); + sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); } sqlite3OpenMasterTable(pParse, iDb); - sqlite3VdbeAddOp(v, OP_NewRowid, 0, 0); - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - sqlite3VdbeAddOp(v, OP_Null, 0, 0); - sqlite3VdbeAddOp(v, OP_Insert, 0, OPFLAG_APPEND); - sqlite3VdbeAddOp(v, OP_Close, 0, 0); - sqlite3VdbeAddOp(v, OP_Pull, 1, 0); + sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); + sqlite3VdbeAddOp2(v, OP_Null, 0, reg3); + sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3VdbeAddOp0(v, OP_Close); } /* Normal (non-error) return. */ @@ -49509,9 +52025,13 @@ SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName, i ** and the probability of hitting the same cookie value is only ** 1 chance in 2^32. So we're safe enough. */ -SQLITE_PRIVATE void sqlite3ChangeCookie(sqlite3 *db, Vdbe *v, int iDb){ - sqlite3VdbeAddOp(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, 0); - sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 0); +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){ + int r1 = sqlite3GetTempReg(pParse); + sqlite3 *db = pParse->db; + Vdbe *v = pParse->pVdbe; + sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 0, r1); + sqlite3ReleaseTempReg(pParse, r1); } /* @@ -49558,7 +52078,7 @@ static void identPut(char *z, int *pIdx, char *zSignedIdent){ ** table. Memory to hold the text of the statement is obtained ** from sqliteMalloc() and must be freed by the calling function. */ -static char *createTableStmt(Table *p, int isTemp){ +static char *createTableStmt(sqlite3 *db, Table *p, int isTemp){ int i, k, n; char *zStmt; char *zSep, *zSep2, *zEnd, *z; @@ -49583,7 +52103,10 @@ static char *createTableStmt(Table *p, int isTemp){ } n += 35 + 6*p->nCol; zStmt = sqlite3_malloc( n ); - if( zStmt==0 ) return 0; + if( zStmt==0 ){ + db->mallocFailed = 1; + return 0; + } sqlite3_snprintf(n, zStmt, !OMIT_TEMPDB&&isTemp ? "CREATE TEMP TABLE ":"CREATE TABLE "); k = strlen(zStmt); @@ -49694,7 +52217,7 @@ SQLITE_PRIVATE void sqlite3EndTable( v = sqlite3GetVdbe(pParse); if( v==0 ) return; - sqlite3VdbeAddOp(v, OP_Close, 0, 0); + sqlite3VdbeAddOp1(v, OP_Close, 0); /* Create the rootpage for the new table and push it onto the stack. ** A view has no rootpage, so just push a zero onto the stack for @@ -49726,13 +52249,15 @@ SQLITE_PRIVATE void sqlite3EndTable( ** be redundant. */ if( pSelect ){ + SelectDest dest; Table *pSelTab; - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0); + + sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); + sqlite3VdbeChangeP5(v, 1); pParse->nTab = 2; - sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0); - sqlite3VdbeAddOp(v, OP_Close, 1, 0); + sqlite3SelectDestInit(&dest, SRT_Table, 1); + sqlite3Select(pParse, pSelect, &dest, 0, 0, 0, 0); + sqlite3VdbeAddOp1(v, OP_Close, 1); if( pParse->nErr==0 ){ pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect); if( pSelTab==0 ) return; @@ -49747,7 +52272,7 @@ SQLITE_PRIVATE void sqlite3EndTable( /* Compute the complete text of the CREATE statement */ if( pSelect ){ - zStmt = createTableStmt(p, p->pSchema==db->aDb[1].pSchema); + zStmt = createTableStmt(db, p, p->pSchema==db->aDb[1].pSchema); }else{ n = pEnd->z - pParse->sNameToken.z + 1; zStmt = sqlite3MPrintf(db, @@ -49763,16 +52288,18 @@ SQLITE_PRIVATE void sqlite3EndTable( */ sqlite3NestedParse(pParse, "UPDATE %Q.%s " - "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#0, sql=%Q " - "WHERE rowid=#1", + "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " + "WHERE rowid=#%d", db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zType, p->zName, p->zName, - zStmt + pParse->regRoot, + zStmt, + pParse->regRowid ); sqlite3_free(zStmt); - sqlite3ChangeCookie(db, v, iDb); + sqlite3ChangeCookie(pParse, iDb); #ifndef SQLITE_OMIT_AUTOINCREMENT /* Check to see if we need to create an sqlite_sequence table for @@ -49790,8 +52317,8 @@ SQLITE_PRIVATE void sqlite3EndTable( #endif /* Reparse everything to update our internal data structures */ - sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, - sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P3_DYNAMIC); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, + sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P4_DYNAMIC); } @@ -50067,20 +52594,22 @@ SQLITE_PRIVATE void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){ */ static void destroyRootPage(Parse *pParse, int iTable, int iDb){ Vdbe *v = sqlite3GetVdbe(pParse); - sqlite3VdbeAddOp(v, OP_Destroy, iTable, iDb); + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); #ifndef SQLITE_OMIT_AUTOVACUUM - /* OP_Destroy pushes an integer onto the stack. If this integer + /* OP_Destroy stores an in integer r1. If this integer ** is non-zero, then it is the root page number of a table moved to ** location iTable. The following code modifies the sqlite_master table to ** reflect this. ** - ** The "#0" in the SQL is a special constant that means whatever value + ** The "#%d" in the SQL is a special constant that means whatever value ** is on the top of the stack. See sqlite3RegisterExpr(). */ sqlite3NestedParse(pParse, - "UPDATE %Q.%s SET rootpage=%d WHERE #0 AND rootpage=#0", - pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable); + "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", + pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1); #endif + sqlite3ReleaseTempReg(pParse, r1); } /* @@ -50156,7 +52685,8 @@ SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, goto exit_drop_table; } assert( pName->nSrc==1 ); - pTab = sqlite3LocateTable(pParse, pName->a[0].zName, pName->a[0].zDatabase); + pTab = sqlite3LocateTable(pParse, isView, + pName->a[0].zName, pName->a[0].zDatabase); if( pTab==0 ){ if( noErr ){ @@ -50240,7 +52770,7 @@ SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, if( IsVirtual(pTab) ){ Vdbe *v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp(v, OP_VBegin, 0, 0); + sqlite3VdbeAddOp0(v, OP_VBegin); } } #endif @@ -50289,10 +52819,10 @@ SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, ** the schema cookie. */ if( IsVirtual(pTab) ){ - sqlite3VdbeOp3(v, OP_VDestroy, iDb, 0, pTab->zName, 0); + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); } - sqlite3VdbeOp3(v, OP_DropTable, iDb, 0, pTab->zName, 0); - sqlite3ChangeCookie(db, v, iDb); + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); + sqlite3ChangeCookie(pParse, iDb); } sqliteViewResetAll(db, iDb); @@ -50441,7 +52971,7 @@ SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ ** content of an index in response to a REINDEX command. ** ** if memRootPage is not negative, it means that the index is newly -** created. The memory cell specified by memRootPage contains the +** created. The register specified by memRootPage contains the ** root page number of the index. If memRootPage is negative, then ** the index already exists and must be cleared before being refilled and ** the root page number of the index is taken from pIndex->tnum. @@ -50454,6 +52984,8 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ int tnum; /* Root page of index */ Vdbe *v; /* Generate code into this virtual machine */ KeyInfo *pKey; /* KeyInfo for index */ + int regIdxKey; /* Registers containing the index key */ + int regRecord; /* Register holding assemblied index record */ sqlite3 *db = pParse->db; /* The database connection */ int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); @@ -50470,34 +53002,40 @@ static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ v = sqlite3GetVdbe(pParse); if( v==0 ) return; if( memRootPage>=0 ){ - sqlite3VdbeAddOp(v, OP_MemLoad, memRootPage, 0); - tnum = 0; + tnum = memRootPage; }else{ tnum = pIndex->tnum; - sqlite3VdbeAddOp(v, OP_Clear, tnum, iDb); + sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); } - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); pKey = sqlite3IndexKeyinfo(pParse, pIndex); - sqlite3VdbeOp3(v, OP_OpenWrite, iIdx, tnum, (char *)pKey, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, + (char *)pKey, P4_KEYINFO_HANDOFF); + if( memRootPage>=0 ){ + sqlite3VdbeChangeP5(v, 1); + } sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); - addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0); - sqlite3GenerateIndexKey(v, pIndex, iTab); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); + regRecord = sqlite3GetTempReg(pParse); + regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord); if( pIndex->onError!=OE_None ){ - int curaddr = sqlite3VdbeCurrentAddr(v); - int addr2 = curaddr+4; - sqlite3VdbeChangeP2(v, curaddr-1, addr2); - sqlite3VdbeAddOp(v, OP_Rowid, iTab, 0); - sqlite3VdbeAddOp(v, OP_AddImm, 1, 0); - sqlite3VdbeAddOp(v, OP_IsUnique, iIdx, addr2); - sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort, - "indexed columns are not unique", P3_STATIC); - assert( db->mallocFailed || addr2==sqlite3VdbeCurrentAddr(v) ); - } - sqlite3VdbeAddOp(v, OP_IdxInsert, iIdx, 0); - sqlite3VdbeAddOp(v, OP_Next, iTab, addr1+1); + int j1, j2; + int regRowid; + + regRowid = regIdxKey + pIndex->nColumn; + j1 = sqlite3VdbeAddOp3(v, OP_IsNull, regIdxKey, 0, pIndex->nColumn); + j2 = sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, + 0, regRowid, (char*)(sqlite3_intptr_t)regRecord, P4_INT32); + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, OE_Abort, 0, + "indexed columns are not unique", P4_STATIC); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeJumpHere(v, j2); + } + sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); sqlite3VdbeJumpHere(v, addr1); - sqlite3VdbeAddOp(v, OP_Close, iTab, 0); - sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); + sqlite3VdbeAddOp1(v, OP_Close, iTab); + sqlite3VdbeAddOp1(v, OP_Close, iIdx); } /* @@ -50578,7 +53116,7 @@ SQLITE_PRIVATE void sqlite3CreateIndex( ** sqlite3FixSrcList can never fail. */ assert(0); } - pTab = sqlite3LocateTable(pParse, pTblName->a[0].zName, + pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, pTblName->a[0].zDatabase); if( !pTab ) goto exit_create_index; assert( db->aDb[iDb].pSchema==pTab->pSchema ); @@ -50863,7 +53401,7 @@ SQLITE_PRIVATE void sqlite3CreateIndex( else if( db->init.busy==0 ){ Vdbe *v; char *zStmt; - int iMem = pParse->nMem++; + int iMem = ++pParse->nMem; v = sqlite3GetVdbe(pParse); if( v==0 ) goto exit_create_index; @@ -50872,8 +53410,7 @@ SQLITE_PRIVATE void sqlite3CreateIndex( /* Create the rootpage for the index */ sqlite3BeginWriteOperation(pParse, 1, iDb); - sqlite3VdbeAddOp(v, OP_CreateIndex, iDb, 0); - sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0); + sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); /* Gather the complete text of the CREATE INDEX statement into ** the zStmt variable @@ -50893,13 +53430,13 @@ SQLITE_PRIVATE void sqlite3CreateIndex( /* Add an entry in sqlite_master for this index */ sqlite3NestedParse(pParse, - "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#0,%Q);", + "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName, pTab->zName, + iMem, zStmt ); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); sqlite3_free(zStmt); /* Fill the index with data and reparse the schema. Code an OP_Expire @@ -50907,10 +53444,10 @@ SQLITE_PRIVATE void sqlite3CreateIndex( */ if( pTblName ){ sqlite3RefillIndex(pParse, pIndex, iMem); - sqlite3ChangeCookie(db, v, iDb); - sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, - sqlite3MPrintf(db, "name='%q'", pIndex->zName), P3_DYNAMIC); - sqlite3VdbeAddOp(v, OP_Expire, 0, 0); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, + sqlite3MPrintf(db, "name='%q'", pIndex->zName), P4_DYNAMIC); + sqlite3VdbeAddOp1(v, OP_Expire, 0); } } @@ -50954,12 +53491,17 @@ SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minForm Vdbe *v; v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 1); + int r1 = sqlite3GetTempReg(pParse); + int r2 = sqlite3GetTempReg(pParse); + int j1; + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, 1); sqlite3VdbeUsesBtree(v, iDb); - sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0); - sqlite3VdbeAddOp(v, OP_Ge, 0, sqlite3VdbeCurrentAddr(v)+3); - sqlite3VdbeAddOp(v, OP_Integer, minFormat, 0); - sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 1); + sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2); + j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 1, r2); + sqlite3VdbeJumpHere(v, j1); + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempReg(pParse, r2); } } @@ -51054,9 +53596,9 @@ SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName ); - sqlite3ChangeCookie(db, v, iDb); + sqlite3ChangeCookie(pParse, iDb); destroyRootPage(pParse, pIndex->tnum, iDb); - sqlite3VdbeOp3(v, OP_DropIndex, iDb, 0, pIndex->zName, 0); + sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); } exit_drop_index: @@ -51352,11 +53894,11 @@ SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){ if( !v ) return; if( type!=TK_DEFERRED ){ for(i=0; inDb; i++){ - sqlite3VdbeAddOp(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); + sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); sqlite3VdbeUsesBtree(v, i); } } - sqlite3VdbeAddOp(v, OP_AutoCommit, 0, 0); + sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0); } /* @@ -51372,7 +53914,7 @@ SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){ v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 0); + sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0); } } @@ -51389,7 +53931,7 @@ SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){ v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp(v, OP_AutoCommit, 1, 1); + sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1); } } @@ -51416,15 +53958,7 @@ SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){ pParse->rc = rc; return 1; } - if( db->flags & !db->autoCommit ){ - rc = sqlite3BtreeBeginTrans(db->aDb[1].pBt, 1); - if( rc!=SQLITE_OK ){ - sqlite3ErrorMsg(pParse, "unable to get a write lock on " - "the temporary database file"); - pParse->rc = rc; - return 1; - } - } + assert( (db->flags & SQLITE_InTrans)==0 || db->autoCommit ); assert( db->aDb[1].pSchema ); } return 0; @@ -51461,7 +53995,7 @@ SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ if( v==0 ) return; /* This only happens if there was a prior error */ db = pParse->db; if( pParse->cookieGoto==0 ){ - pParse->cookieGoto = sqlite3VdbeAddOp(v, OP_Goto, 0, 0)+1; + pParse->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1; } if( iDb>=0 ){ assert( iDbnDb ); @@ -51502,7 +54036,7 @@ SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, sqlite3CodeVerifySchema(pParse, iDb); pParse->writeMask |= 1<nested==0 ){ - sqlite3VdbeAddOp(v, OP_Statement, iDb, 0); + sqlite3VdbeAddOp1(v, OP_Statement, iDb); } if( (OMIT_TEMPDB || iDb!=1) && pParse->db->aDb[1].pBt!=0 ){ sqlite3BeginWriteOperation(pParse, setStatement, 1); @@ -52069,7 +54603,7 @@ SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ ** This file contains C code routines that are called by the parser ** in order to generate code for DELETE FROM statements. ** -** $Id: delete.c,v 1.134 2007/12/12 17:42:53 danielk1977 Exp $ +** $Id: delete.c,v 1.161 2008/02/12 16:52:14 drh Exp $ */ /* @@ -52082,7 +54616,7 @@ SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ int i; struct SrcList_item *pItem; for(i=0, pItem=pSrc->a; inSrc; i++, pItem++){ - pTab = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase); + pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); sqlite3DeleteTable(pItem->pTab); pItem->pTab = pTab; if( pTab ){ @@ -52131,11 +54665,43 @@ SQLITE_PRIVATE void sqlite3OpenTable( v = sqlite3GetVdbe(p); assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite), pTab->zName); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - VdbeComment((v, "# %s", pTab->zName)); - sqlite3VdbeAddOp(v, opcode, iCur, pTab->tnum); - sqlite3VdbeAddOp(v, OP_SetNumColumns, iCur, pTab->nCol); + sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); + VdbeComment((v, "%s", pTab->zName)); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, iCur, pTab->nCol); +} + + +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) +/* +** Evaluate a view and store its result in an ephemeral table. The +** pWhere argument is an optional WHERE clause that restricts the +** set of rows in the view that are to be added to the ephemeral table. +*/ +SQLITE_PRIVATE void sqlite3MaterializeView( + Parse *pParse, /* Parsing context */ + Select *pView, /* View definition */ + Expr *pWhere, /* Optional WHERE clause to be added */ + u32 col_mask, /* Render only the columns in this mask. */ + int iCur /* Cursor number for ephemerial table */ +){ + SelectDest dest; + Select *pDup; + sqlite3 *db = pParse->db; + + pDup = sqlite3SelectDup(db, pView); + if( pWhere ){ + SrcList *pFrom; + + pWhere = sqlite3ExprDup(db, pWhere); + pFrom = sqlite3SrcListAppendFromTerm(pParse, 0, 0, 0, 0, pDup, 0, 0); + pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0); + } + sqlite3SelectMask(pParse, pDup, col_mask); + sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); + sqlite3Select(pParse, pDup, &dest, 0, 0, 0, 0); + sqlite3SelectDelete(pDup); } +#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */ /* @@ -52169,6 +54735,11 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( int isView; /* True if attempting to delete from a view */ int triggers_exist = 0; /* True if any triggers exist */ #endif + int iBeginAfterTrigger; /* Address of after trigger program */ + int iEndAfterTrigger; /* Exit of after trigger program */ + int iBeginBeforeTrigger; /* Address of before trigger program */ + int iEndBeforeTrigger; /* Exit of before trigger program */ + u32 old_col_mask = 0; /* Mask of OLD.* columns in use */ sContext.pParse = 0; db = pParse->db; @@ -52222,15 +54793,12 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( oldIdx = pParse->nTab++; } - /* Resolve the column names in the WHERE clause. + /* Assign cursor number to the table and all its indices. */ assert( pTabList->nSrc==1 ); iCur = pTabList->a[0].iCursor = pParse->nTab++; - memset(&sNC, 0, sizeof(sNC)); - sNC.pParse = pParse; - sNC.pSrcList = pTabList; - if( sqlite3ExprResolveNames(&sNC, pWhere) ){ - goto delete_from_cleanup; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + pParse->nTab++; } /* Start the view context @@ -52248,21 +54816,46 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); sqlite3BeginWriteOperation(pParse, triggers_exist, iDb); + if( triggers_exist ){ + int orconf = ((pParse->trigStack)?pParse->trigStack->orconf:OE_Default); + int iGoto = sqlite3VdbeAddOp0(v, OP_Goto); + addr = sqlite3VdbeMakeLabel(v); + + iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v); + (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab, + -1, oldIdx, orconf, addr, &old_col_mask, 0); + iEndBeforeTrigger = sqlite3VdbeAddOp0(v, OP_Goto); + + iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v); + (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1, + oldIdx, orconf, addr, &old_col_mask, 0); + iEndAfterTrigger = sqlite3VdbeAddOp0(v, OP_Goto); + + sqlite3VdbeJumpHere(v, iGoto); + } + /* If we are trying to delete from a view, realize that view into ** a ephemeral table. */ if( isView ){ - Select *pView = sqlite3SelectDup(db, pTab->pSelect); - sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0); - sqlite3SelectDelete(pView); + sqlite3MaterializeView(pParse, pTab->pSelect, pWhere, old_col_mask, iCur); + } + + /* Resolve the column names in the WHERE clause. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + if( sqlite3ExprResolveNames(&sNC, pWhere) ){ + goto delete_from_cleanup; } /* Initialize the counter of the number of rows deleted, if ** we are counting rows. */ if( db->flags & SQLITE_CountRows ){ - memCnt = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt); + memCnt = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); } /* Special case: A DELETE without a WHERE clause deletes everything. @@ -52277,19 +54870,19 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( if( !isView ){ sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); } - sqlite3VdbeAddOp(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2); - addr2 = sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt); - sqlite3VdbeAddOp(v, OP_Next, iCur, addr2); - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); + sqlite3VdbeAddOp2(v, OP_Rewind, iCur, sqlite3VdbeCurrentAddr(v)+2); + addr2 = sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); + sqlite3VdbeAddOp2(v, OP_Next, iCur, addr2); + sqlite3VdbeAddOp1(v, OP_Close, iCur); } if( !isView ){ - sqlite3VdbeAddOp(v, OP_Clear, pTab->tnum, iDb); + sqlite3VdbeAddOp2(v, OP_Clear, pTab->tnum, iDb); if( !pParse->nested ){ - sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC); + sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); } for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ assert( pIdx->pSchema==pTab->pSchema ); - sqlite3VdbeAddOp(v, OP_Clear, pIdx->tnum, iDb); + sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); } } } @@ -52297,17 +54890,19 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( ** the table and pick which records to delete. */ else{ + int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ + /* Begin the database scan */ - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0); if( pWInfo==0 ) goto delete_from_cleanup; /* Remember the rowid of every item to be deleted. */ - sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0); - sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0); + sqlite3VdbeAddOp2(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, iRowid); + sqlite3VdbeAddOp1(v, OP_FifoWrite, iRowid); if( db->flags & SQLITE_CountRows ){ - sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt); + sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); } /* End the database scan loop. @@ -52317,8 +54912,8 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( /* Open the pseudo-table used to store OLD if there are triggers. */ if( triggers_exist ){ - sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); - sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); + sqlite3VdbeAddOp1(v, OP_OpenPseudo, oldIdx); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, oldIdx, pTab->nCol); } /* Delete every item whose key was written to the list during the @@ -52327,58 +54922,53 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( */ end = sqlite3VdbeMakeLabel(v); - /* This is the beginning of the delete loop when there are - ** row triggers. + if( !isView ){ + /* Open cursors for the table we are deleting from and + ** all its indices. + */ + sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); + } + + /* This is the beginning of the delete loop. If a trigger encounters + ** an IGNORE constraint, it jumps back to here. */ if( triggers_exist ){ - int mem1 = pParse->nMem++; - addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, end); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); - sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0); - if( !isView ){ - sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); - } - sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); - sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); - sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); - sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0); - if( !isView ){ - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); - } - - (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_BEFORE, pTab, - -1, oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default, - addr); - if( !isView ){ - sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0); - } + sqlite3VdbeResolveLabel(v, addr); } + addr = sqlite3VdbeAddOp2(v, OP_FifoRead, iRowid, end); - if( !isView ){ - /* Open cursors for the table we are deleting from and all its - ** indices. If there are row triggers, this happens inside the - ** OP_FifoRead loop because the cursor have to all be closed - ** before the trigger fires. If there are no row triggers, the - ** cursors are opened only once on the outside the loop. + if( triggers_exist ){ + int iData = ++pParse->nMem; /* For storing row data of OLD table */ + + /* If the record is no longer present in the table, jump to the + ** next iteration of the loop through the contents of the fifo. */ - sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, iRowid); - /* This is the beginning of the delete loop when there are no - ** row triggers */ - if( !triggers_exist ){ - addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, end); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); + /* Populate the OLD.* pseudo-table */ + if( old_col_mask ){ + sqlite3VdbeAddOp2(v, OP_RowData, iCur, iData); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, iData); } + sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, iData, iRowid); + + /* Jump back and run the BEFORE triggers */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); + sqlite3VdbeJumpHere(v, iEndBeforeTrigger); + } + if( !isView ){ /* Delete the row */ #ifndef SQLITE_OMIT_VIRTUALTABLE if( IsVirtual(pTab) ){ + const char *pVtab = (const char *)pTab->pVtab; pParse->pVirtualLock = pTab; - sqlite3VdbeOp3(v, OP_VUpdate, 0, 1, (const char*)pTab->pVtab, P3_VTAB); + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVtab, P4_VTAB); }else #endif { - sqlite3GenerateRowDelete(db, v, pTab, iCur, pParse->nested==0); + sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, pParse->nested==0); } } @@ -52386,27 +54976,21 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( ** the AFTER triggers */ if( triggers_exist ){ - if( !isView ){ - for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ - sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum); - } - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); - } - (void)sqlite3CodeRowTrigger(pParse, TK_DELETE, 0, TRIGGER_AFTER, pTab, -1, - oldIdx, (pParse->trigStack)?pParse->trigStack->orconf:OE_Default, - addr); + /* Jump back and run the AFTER triggers */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); + sqlite3VdbeJumpHere(v, iEndAfterTrigger); } /* End of the delete loop */ - sqlite3VdbeAddOp(v, OP_Goto, 0, addr); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); sqlite3VdbeResolveLabel(v, end); /* Close the cursors after the loop if there are no row triggers */ - if( !triggers_exist && !IsVirtual(pTab) ){ + if( !isView && !IsVirtual(pTab) ){ for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ - sqlite3VdbeAddOp(v, OP_Close, iCur + i, pIdx->tnum); + sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum); } - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); + sqlite3VdbeAddOp1(v, OP_Close, iCur); } } @@ -52416,10 +55000,9 @@ SQLITE_PRIVATE void sqlite3DeleteFrom( ** invoke the callback function. */ if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ - sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0); - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P3_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", P4_STATIC); } delete_from_cleanup: @@ -52442,26 +55025,29 @@ delete_from_cleanup: ** 2. Read/write cursors for all indices of pTab must be open as ** cursor number base+i for the i-th index. ** -** 3. The record number of the row to be deleted must be on the top -** of the stack. +** 3. The record number of the row to be deleted must be stored in +** memory cell iRowid. ** ** This routine pops the top of the stack to remove the record number ** and then generates code to remove both the table record and all index ** entries that point to that record. */ SQLITE_PRIVATE void sqlite3GenerateRowDelete( - sqlite3 *db, /* The database containing the index */ - Vdbe *v, /* Generate code into this VDBE */ + Parse *pParse, /* Parsing context */ Table *pTab, /* Table containing the row to be deleted */ int iCur, /* Cursor number for the table */ + int iRowid, /* Memory cell that contains the rowid to delete */ int count /* Increment the row change counter */ ){ int addr; - addr = sqlite3VdbeAddOp(v, OP_NotExists, iCur, 0); - sqlite3GenerateRowIndexDelete(v, pTab, iCur, 0); - sqlite3VdbeAddOp(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); + Vdbe *v; + + v = pParse->pVdbe; + addr = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowid); + sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0); + sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); if( count ){ - sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC); + sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); } sqlite3VdbeJumpHere(v, addr); } @@ -52483,19 +55069,22 @@ SQLITE_PRIVATE void sqlite3GenerateRowDelete( ** deleted. */ SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete( - Vdbe *v, /* Generate code into this VDBE */ + Parse *pParse, /* Parsing and code generating context */ Table *pTab, /* Table containing the row to be deleted */ int iCur, /* Cursor number for the table */ - char *aIdxUsed /* Only delete if aIdxUsed!=0 && aIdxUsed[i]!=0 */ + int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ ){ int i; Index *pIdx; + int r1; + r1 = sqlite3GetTempReg(pParse); for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ - if( aIdxUsed!=0 && aIdxUsed[i-1]==0 ) continue; - sqlite3GenerateIndexKey(v, pIdx, iCur); - sqlite3VdbeAddOp(v, OP_IdxDelete, iCur+i, 0); + if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue; + sqlite3GenerateIndexKey(pParse, pIdx, iCur, r1); + sqlite3VdbeAddOp2(pParse->pVdbe, OP_IdxDelete, iCur+i, r1); } + sqlite3ReleaseTempReg(pParse, r1); } /* @@ -52503,27 +55092,40 @@ SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete( ** of the tack. The key with be for index pIdx which is an index on pTab. ** iCur is the index of a cursor open on the pTab table and pointing to ** the entry that needs indexing. +** +** Return a register number which is the first in a block of +** registers that holds the elements of the index key. The +** block of registers has already been deallocated by the time +** this routine returns. */ -SQLITE_PRIVATE void sqlite3GenerateIndexKey( - Vdbe *v, /* Generate code into this VDBE */ +SQLITE_PRIVATE int sqlite3GenerateIndexKey( + Parse *pParse, /* Parsing context */ Index *pIdx, /* The index for which to generate a key */ - int iCur /* Cursor number for the pIdx->pTable table */ + int iCur, /* Cursor number for the pIdx->pTable table */ + int regOut /* Write the new index key to this register */ ){ + Vdbe *v = pParse->pVdbe; int j; Table *pTab = pIdx->pTable; + int regBase; + int nCol; - sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); - for(j=0; jnColumn; j++){ + nCol = pIdx->nColumn; + regBase = sqlite3GetTempRange(pParse, nCol+1); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol); + for(j=0; jaiColumn[j]; if( idx==pTab->iPKey ){ - sqlite3VdbeAddOp(v, OP_Dup, j, 0); + sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j); }else{ - sqlite3VdbeAddOp(v, OP_Column, iCur, idx); + sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j); sqlite3ColumnDefault(v, pTab, idx); } } - sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut); sqlite3IndexAffinityStr(v, pIdx); + sqlite3ReleaseTempRange(pParse, regBase, nCol+1); + return regBase; } /************** End of delete.c **********************************************/ @@ -52546,7 +55148,7 @@ SQLITE_PRIVATE void sqlite3GenerateIndexKey( ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.181 2007/12/13 21:54:11 drh Exp $ +** $Id: func.c,v 1.186 2008/03/06 09:58:50 mlcreech Exp $ */ @@ -53400,7 +56002,7 @@ static void trimFunc( const unsigned char *zIn; /* Input string */ const unsigned char *zCharSet; /* Set of characters to trim */ int nIn; /* Number of bytes in input */ - int flags; /* 1: trimleft 2: trimright 3: trim */ + sqlite3_intptr_t flags; /* 1: trimleft 2: trimright 3: trim */ int i; /* Loop counter */ unsigned char *aLen; /* Length of each character in zCharSet */ unsigned char **azChar; /* Individual characters in zCharSet */ @@ -53441,7 +56043,7 @@ static void trimFunc( } } if( nChar>0 ){ - flags = (int)sqlite3_user_data(context); + flags = (sqlite3_intptr_t)sqlite3_user_data(context); if( flags & 1 ){ while( nIn>0 ){ int len; @@ -53671,20 +56273,18 @@ static void test_auxdata( for(i=0; izColAff ){ @@ -54144,21 +56747,22 @@ SQLITE_PRIVATE void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ int n; Table *pTab = pIdx->pTable; sqlite3 *db = sqlite3VdbeDb(v); - pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+1); + pIdx->zColAff = (char *)sqlite3DbMallocZero(db, pIdx->nColumn+2); if( !pIdx->zColAff ){ return; } for(n=0; nnColumn; n++){ pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; } - pIdx->zColAff[pIdx->nColumn] = '\0'; + pIdx->zColAff[n++] = SQLITE_AFF_NONE; + pIdx->zColAff[n] = 0; } - sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0); + sqlite3VdbeChangeP4(v, -1, pIdx->zColAff, 0); } /* -** Set P3 of the most recently inserted opcode to a column affinity +** Set P4 of the most recently inserted opcode to a column affinity ** string for table pTab. A column affinity string has one character ** for each column indexed by the index, according to the affinity of the ** column: @@ -54197,7 +56801,7 @@ SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ pTab->zColAff = zColAff; } - sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0); + sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0); } /* @@ -54213,27 +56817,22 @@ static int readsTable(Vdbe *v, int iStartAddr, int iDb, Table *pTab){ for(i=iStartAddr; iopcode==OP_OpenRead ){ - VdbeOp *pPrior = &pOp[-1]; + if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){ + Index *pIndex; int tnum = pOp->p2; - assert( i>iStartAddr ); - assert( pPrior->opcode==OP_Integer ); - if( pPrior->p1==iDb ){ - Index *pIndex; - if( tnum==pTab->tnum ){ + if( tnum==pTab->tnum ){ + return 1; + } + for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ + if( tnum==pIndex->tnum ){ return 1; } - for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ - if( tnum==pIndex->tnum ){ - return 1; - } - } } } #ifndef SQLITE_OMIT_VIRTUALTABLE - if( pOp->opcode==OP_VOpen && pOp->p3==(const char*)pTab->pVtab ){ - assert( pOp->p3!=0 ); - assert( pOp->p3type==P3_VTAB ); + if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pTab->pVtab ){ + assert( pOp->p4.pVtab!=0 ); + assert( pOp->p4type==P4_VTAB ); return 1; } #endif @@ -54245,45 +56844,48 @@ static int readsTable(Vdbe *v, int iStartAddr, int iDb, Table *pTab){ /* ** Write out code to initialize the autoincrement logic. This code ** looks up the current autoincrement value in the sqlite_sequence -** table and stores that value in a memory cell. Code generated by -** autoIncStep() will keep that memory cell holding the largest +** table and stores that value in a register. Code generated by +** autoIncStep() will keep that register holding the largest ** rowid value. Code generated by autoIncEnd() will write the new ** largest value of the counter back into the sqlite_sequence table. ** ** This routine returns the index of the mem[] cell that contains ** the maximum rowid counter. ** -** Two memory cells are allocated. The next memory cell after the -** one returned holds the rowid in sqlite_sequence where we will -** write back the revised maximum rowid. +** Three consecutive registers are allocated by this routine. The +** first two hold the name of the target table and the maximum rowid +** inserted into the target table, respectively. +** The third holds the rowid in sqlite_sequence where we will +** write back the revised maximum rowid. This routine returns the +** index of the second of these three registers. */ static int autoIncBegin( Parse *pParse, /* Parsing context */ int iDb, /* Index of the database holding pTab */ Table *pTab /* The table we are writing to */ ){ - int memId = 0; + int memId = 0; /* Register holding maximum rowid */ if( pTab->autoInc ){ Vdbe *v = pParse->pVdbe; Db *pDb = &pParse->db->aDb[iDb]; int iCur = pParse->nTab; - int addr; + int addr; /* Address of the top of the loop */ assert( v ); - addr = sqlite3VdbeCurrentAddr(v); - memId = pParse->nMem+1; - pParse->nMem += 2; + pParse->nMem++; /* Holds name of table */ + memId = ++pParse->nMem; + pParse->nMem++; sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead); - sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13); - sqlite3VdbeAddOp(v, OP_Column, iCur, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); - sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12); - sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); - sqlite3VdbeAddOp(v, OP_MemStore, memId-1, 1); - sqlite3VdbeAddOp(v, OP_Column, iCur, 1); - sqlite3VdbeAddOp(v, OP_MemStore, memId, 1); - sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13); - sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4); - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); + addr = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, pTab->zName, 0); + sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+8); + sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, memId); + sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); + sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, memId+1); + sqlite3VdbeAddOp3(v, OP_Column, iCur, 1, memId); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+8); + sqlite3VdbeAddOp2(v, OP_Next, iCur, addr+2); + sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); } return memId; } @@ -54296,15 +56898,15 @@ static int autoIncBegin( ** larger than the maximum rowid in the memId memory cell, then the ** memory cell is updated. The stack is unchanged. */ -static void autoIncStep(Parse *pParse, int memId){ +static void autoIncStep(Parse *pParse, int memId, int regRowid){ if( memId>0 ){ - sqlite3VdbeAddOp(pParse->pVdbe, OP_MemMax, memId, 0); + sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); } } /* ** After doing one or more inserts, the maximum rowid is stored -** in mem[memId]. Generate code to write this value back into the +** in reg[memId]. Generate code to write this value back into the ** the sqlite_sequence table. */ static void autoIncEnd( @@ -54317,19 +56919,18 @@ static void autoIncEnd( int iCur = pParse->nTab; Vdbe *v = pParse->pVdbe; Db *pDb = &pParse->db->aDb[iDb]; - int addr; + int j1; + int iRec = ++pParse->nMem; /* Memory cell used for record */ + assert( v ); - addr = sqlite3VdbeCurrentAddr(v); sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); - sqlite3VdbeAddOp(v, OP_MemLoad, memId-1, 0); - sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0); - sqlite3VdbeAddOp(v, OP_MemLoad, memId, 0); - sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0); - sqlite3VdbeAddOp(v, OP_Insert, iCur, OPFLAG_APPEND); - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); + sqlite3VdbeAddOp2(v, OP_NewRowid, iCur, memId+1); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); + sqlite3VdbeAddOp3(v, OP_Insert, iCur, iRec, memId+1); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3VdbeAddOp1(v, OP_Close, iCur); } } #else @@ -54338,7 +56939,7 @@ static void autoIncEnd( ** above are all no-ops */ # define autoIncBegin(A,B,C) (0) -# define autoIncStep(A,B) +# define autoIncStep(A,B,C) # define autoIncEnd(A,B,C,D) #endif /* SQLITE_OMIT_AUTOINCREMENT */ @@ -54448,31 +57049,39 @@ SQLITE_PRIVATE void sqlite3Insert( IdList *pColumn, /* Column names corresponding to IDLIST. */ int onError /* How to handle constraint errors */ ){ - Table *pTab; /* The table to insert into */ + sqlite3 *db; /* The main database structure */ + Table *pTab; /* The table to insert into. aka TABLE */ char *zTab; /* Name of the table into which we are inserting */ const char *zDb; /* Name of the database holding this table */ int i, j, idx; /* Loop counters */ Vdbe *v; /* Generate code into this virtual machine */ Index *pIdx; /* For looping over indices of the table */ int nColumn; /* Number of columns in the data */ - int base = 0; /* VDBE Cursor number for pTab */ - int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ - sqlite3 *db; /* The main database structure */ + int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ + int baseCur = 0; /* VDBE Cursor number for pTab */ int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ int endOfLoop; /* Label for the end of the insertion loop */ int useTempTable = 0; /* Store SELECT results in intermediate table */ int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ + int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */ int iSelectLoop = 0; /* Address of code that implements the SELECT */ int iCleanup = 0; /* Address of the cleanup code */ int iInsertBlock = 0; /* Address of the subroutine used to insert data */ - int iCntMem = 0; /* Memory cell used for the row counter */ - int newIdx = -1; /* Cursor for the NEW table */ + int newIdx = -1; /* Cursor for the NEW pseudo-table */ + int iDb; /* Index of database holding TABLE */ Db *pDb; /* The database containing table being inserted into */ - int counterMem = 0; /* Memory cell holding AUTOINCREMENT counter */ int appendFlag = 0; /* True if the insert is likely to be an append */ - int iDb; - int nHidden = 0; + /* Register allocations */ + int regFromSelect; /* Base register for data coming from SELECT */ + int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ + int regRowCount = 0; /* Memory cell used for the row counter */ + int regIns; /* Block of regs holding rowid+data being inserted */ + int regRowid; /* registers holding insert rowid */ + int regData; /* register holding first column to insert */ + int regRecord; /* Holds the assemblied row record */ + int *aRegIdx = 0; /* One register allocated to each index */ + #ifndef SQLITE_OMIT_TRIGGER int isView; /* True if attempting to insert into a view */ @@ -54561,11 +57170,9 @@ SQLITE_PRIVATE void sqlite3Insert( #endif /* SQLITE_OMIT_XFER_OPT */ /* If this is an AUTOINCREMENT table, look up the sequence number in the - ** sqlite_sequence table and store it in memory cell counterMem. Also - ** remember the rowid of the sqlite_sequence table entry in memory cell - ** counterRowid. + ** sqlite_sequence table and store it in memory cell regAutoinc. */ - counterMem = autoIncBegin(pParse, iDb, pTab); + regAutoinc = autoIncBegin(pParse, iDb, pTab); /* Figure out how many columns of data are supplied. If the data ** is coming from a SELECT statement, then this step also generates @@ -54578,19 +57185,23 @@ SQLITE_PRIVATE void sqlite3Insert( if( pSelect ){ /* Data is coming from a SELECT. Generate code to implement that SELECT */ + SelectDest dest; int rc, iInitCode; - iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); + + iInitCode = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); iSelectLoop = sqlite3VdbeCurrentAddr(v); iInsertBlock = sqlite3VdbeMakeLabel(v); + sqlite3SelectDestInit(&dest, SRT_Subroutine, iInsertBlock); /* Resolve the expressions in the SELECT statement and execute it. */ - rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0); + rc = sqlite3Select(pParse, pSelect, &dest, 0, 0, 0, 0); if( rc || pParse->nErr || db->mallocFailed ){ goto insert_cleanup; } + regFromSelect = dest.iMem; iCleanup = sqlite3VdbeMakeLabel(v); - sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup); + sqlite3VdbeAddOp2(v, OP_Goto, 0, iCleanup); assert( pSelect->pEList ); nColumn = pSelect->pEList->nExpr; @@ -54610,23 +57221,27 @@ SQLITE_PRIVATE void sqlite3Insert( /* Generate the subroutine that SELECT calls to process each row of ** the result. Store the result in a temporary table */ + int regRec, regRowid; + srcTab = pParse->nTab++; + regRec = sqlite3GetTempReg(pParse); + regRowid = sqlite3GetTempReg(pParse); sqlite3VdbeResolveLabel(v, iInsertBlock); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); - sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); - sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0); - sqlite3VdbeAddOp(v, OP_Pull, 1, 0); - sqlite3VdbeAddOp(v, OP_Insert, srcTab, OPFLAG_APPEND); - sqlite3VdbeAddOp(v, OP_Return, 0, 0); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); + sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regRowid); + sqlite3VdbeAddOp2(v, OP_Return, 0, 0); + sqlite3ReleaseTempReg(pParse, regRec); + sqlite3ReleaseTempReg(pParse, regRowid); /* The following code runs first because the GOTO at the very top ** of the program jumps to it. Create the temporary table, then jump ** back up and execute the SELECT code above. */ sqlite3VdbeJumpHere(v, iInitCode); - sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0); - sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn); - sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, 0); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, srcTab, nColumn); + sqlite3VdbeAddOp2(v, OP_Goto, 0, iSelectLoop); sqlite3VdbeResolveLabel(v, iCleanup); }else{ sqlite3VdbeJumpHere(v, iInitCode); @@ -54716,21 +57331,31 @@ SQLITE_PRIVATE void sqlite3Insert( /* Open the temp table for FOR EACH ROW triggers */ if( triggers_exist ){ - sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); - sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); + sqlite3VdbeAddOp2(v, OP_OpenPseudo, newIdx, 0); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, newIdx, pTab->nCol); } /* Initialize the count of rows to be inserted */ if( db->flags & SQLITE_CountRows ){ - iCntMem = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem); + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); } - /* Open tables and indices if there are no row triggers */ - if( !triggers_exist ){ - base = pParse->nTab; - sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); + /* If this is not a view, open the table and and all indices */ + if( !isView ){ + int nIdx; + int i; + + baseCur = pParse->nTab; + nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite); + aRegIdx = sqlite3DbMallocZero(db, sizeof(int)*(nIdx+1)); + if( aRegIdx==0 ){ + goto insert_cleanup; + } + for(i=0; inMem; + } } /* If the data source is a temporary table, then we have to create @@ -54740,18 +57365,32 @@ SQLITE_PRIVATE void sqlite3Insert( */ if( useTempTable ){ iBreak = sqlite3VdbeMakeLabel(v); - sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, srcTab, iBreak); iCont = sqlite3VdbeCurrentAddr(v); }else if( pSelect ){ - sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop); + sqlite3VdbeAddOp2(v, OP_Goto, 0, iSelectLoop); sqlite3VdbeResolveLabel(v, iInsertBlock); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); } + /* Allocate registers for holding the rowid of the new row, + ** the content of the new row, and the assemblied row record. + */ + regRecord = ++pParse->nMem; + regRowid = regIns = pParse->nMem+1; + pParse->nMem += pTab->nCol + 1; + if( IsVirtual(pTab) ){ + regRowid++; + pParse->nMem++; + } + regData = regRowid+1; + /* Run the BEFORE and INSTEAD OF triggers, if there are any */ endOfLoop = sqlite3VdbeMakeLabel(v); if( triggers_exist & TRIGGER_BEFORE ){ + int regRowid; + int regCols; + int regRec; /* build the NEW.* reference row. Note that if there is an INTEGER ** PRIMARY KEY into which a NULL is being inserted, that NULL will be @@ -54759,17 +57398,19 @@ SQLITE_PRIVATE void sqlite3Insert( ** we do not know what the unique ID will be (because the insert has ** not happened yet) so we substitute a rowid of -1 */ + regRowid = sqlite3GetTempReg(pParse); if( keyColumn<0 ){ - sqlite3VdbeAddOp(v, OP_Integer, -1, 0); + sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid); }else if( useTempTable ){ - sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); + sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid); }else{ + int j1; assert( pSelect==0 ); /* Otherwise useTempTable is true */ - sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); - sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_Integer, -1, 0); - sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); + sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid); + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); + sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); } /* Cannot have triggers on a virtual table. If it were possible, @@ -54779,6 +57420,7 @@ SQLITE_PRIVATE void sqlite3Insert( /* Create the new column data */ + regCols = sqlite3GetTempRange(pParse, pTab->nCol); for(i=0; inCol; i++){ if( pColumn==0 ){ j = i; @@ -54788,15 +57430,16 @@ SQLITE_PRIVATE void sqlite3Insert( } } if( pColumn && j>=pColumn->nId ){ - sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i); }else if( useTempTable ){ - sqlite3VdbeAddOp(v, OP_Column, srcTab, j); + sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i); }else{ assert( pSelect==0 ); /* Otherwise useTempTable is true */ - sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr); + sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i); } } - sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); + regRec = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRec); /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, ** do not attempt any conversions before assembling the record. @@ -54806,23 +57449,18 @@ SQLITE_PRIVATE void sqlite3Insert( if( !isView ){ sqlite3TableAffinityStr(v, pTab); } - sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); + sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid); + sqlite3ReleaseTempReg(pParse, regRec); + sqlite3ReleaseTempReg(pParse, regRowid); + sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol); /* Fire BEFORE or INSTEAD OF triggers */ if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, - newIdx, -1, onError, endOfLoop) ){ + newIdx, -1, onError, endOfLoop, 0, 0) ){ goto insert_cleanup; } } - /* If any triggers exists, the opening of tables and indices is deferred - ** until now. - */ - if( triggers_exist && !isView ){ - base = pParse->nTab; - sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite); - } - /* Push the record number for the new entry onto the stack. The ** record number is a randomly generate integer created by NewRowid ** except when the table has an INTEGER PRIMARY KEY column, in which @@ -54830,53 +57468,56 @@ SQLITE_PRIVATE void sqlite3Insert( */ if( !isView ){ if( IsVirtual(pTab) ){ - /* The row that the VUpdate opcode will delete: none */ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + /* The row that the VUpdate opcode will delete: none */ + sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); } if( keyColumn>=0 ){ if( useTempTable ){ - sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn); + sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid); }else if( pSelect ){ - sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1); + sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid); }else{ VdbeOp *pOp; - sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr); + sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid); pOp = sqlite3VdbeGetOp(v, sqlite3VdbeCurrentAddr(v) - 1); if( pOp && pOp->opcode==OP_Null ){ appendFlag = 1; pOp->opcode = OP_NewRowid; - pOp->p1 = base; - pOp->p2 = counterMem; + pOp->p1 = baseCur; + pOp->p2 = regRowid; + pOp->p3 = regAutoinc; } } /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid ** to generate a unique primary key value. */ if( !appendFlag ){ - sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); - sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); + int j1; + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); + sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); } }else if( IsVirtual(pTab) ){ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); }else{ - sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem); + sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); appendFlag = 1; } - autoIncStep(pParse, counterMem); + autoIncStep(pParse, regAutoinc, regRowid); /* Push onto the stack, data for all columns of the new entry, beginning ** with the first column. */ nHidden = 0; for(i=0; inCol; i++){ + int iRegStore = regRowid+1+i; if( i==pTab->iPKey ){ /* The value of the INTEGER PRIMARY KEY column is always a NULL. ** Whenever this column is read, the record number will be substituted ** in its place. So will fill this column with a NULL to avoid ** taking up data space with information that will never be used. */ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore); continue; } if( pColumn==0 ){ @@ -54893,13 +57534,13 @@ SQLITE_PRIVATE void sqlite3Insert( } } if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ - sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore); }else if( useTempTable ){ - sqlite3VdbeAddOp(v, OP_Column, srcTab, j); + sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); }else if( pSelect ){ - sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j+IsVirtual(pTab), 1); + sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); }else{ - sqlite3ExprCode(pParse, pList->a[j].pExpr); + sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); } } @@ -54909,37 +57550,46 @@ SQLITE_PRIVATE void sqlite3Insert( #ifndef SQLITE_OMIT_VIRTUALTABLE if( IsVirtual(pTab) ){ pParse->pVirtualLock = pTab; - sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2, - (const char*)pTab->pVtab, P3_VTAB); + sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, + (const char*)pTab->pVtab, P4_VTAB); }else #endif { - sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0, - 0, onError, endOfLoop); - sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0, - (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1, - appendFlag); + sqlite3GenerateConstraintChecks( + pParse, + pTab, + baseCur, + regIns, + aRegIdx, + keyColumn>=0, + 0, + onError, + endOfLoop + ); + sqlite3CompleteInsertion( + pParse, + pTab, + baseCur, + regIns, + aRegIdx, + 0, + 0, + (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1, + appendFlag + ); } } /* Update the count of rows that are inserted */ if( (db->flags & SQLITE_CountRows)!=0 ){ - sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem); + sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); } if( triggers_exist ){ - /* Close all tables opened */ - if( !isView ){ - sqlite3VdbeAddOp(v, OP_Close, base, 0); - for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ - sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); - } - } - /* Code AFTER triggers */ if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab, - newIdx, -1, onError, endOfLoop) ){ + newIdx, -1, onError, endOfLoop, 0, 0) ){ goto insert_cleanup; } } @@ -54948,28 +57598,27 @@ SQLITE_PRIVATE void sqlite3Insert( */ sqlite3VdbeResolveLabel(v, endOfLoop); if( useTempTable ){ - sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont); + sqlite3VdbeAddOp2(v, OP_Next, srcTab, iCont); sqlite3VdbeResolveLabel(v, iBreak); - sqlite3VdbeAddOp(v, OP_Close, srcTab, 0); + sqlite3VdbeAddOp2(v, OP_Close, srcTab, 0); }else if( pSelect ){ - sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); - sqlite3VdbeAddOp(v, OP_Return, 0, 0); + sqlite3VdbeAddOp2(v, OP_Return, 0, 0); sqlite3VdbeResolveLabel(v, iCleanup); } - if( !triggers_exist && !IsVirtual(pTab) ){ + if( !IsVirtual(pTab) && !isView ){ /* Close all tables opened */ - sqlite3VdbeAddOp(v, OP_Close, base, 0); + sqlite3VdbeAddOp2(v, OP_Close, baseCur, 0); for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ - sqlite3VdbeAddOp(v, OP_Close, idx+base, 0); + sqlite3VdbeAddOp2(v, OP_Close, idx+baseCur, 0); } } /* Update the sqlite_sequence table by storing the content of the - ** counter value in memory counterMem back into the sqlite_sequence + ** counter value in memory regAutoinc back into the sqlite_sequence ** table. */ - autoIncEnd(pParse, iDb, pTab, counterMem); + autoIncEnd(pParse, iDb, pTab, regAutoinc); /* ** Return the number of rows inserted. If this routine is @@ -54977,10 +57626,9 @@ SQLITE_PRIVATE void sqlite3Insert( ** invoke the callback function. */ if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){ - sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0); - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P4_STATIC); } insert_cleanup: @@ -54988,17 +57636,17 @@ insert_cleanup: sqlite3ExprListDelete(pList); sqlite3SelectDelete(pSelect); sqlite3IdListDelete(pColumn); + sqlite3_free(aRegIdx); } /* -** Generate code to do a constraint check prior to an INSERT or an UPDATE. +** Generate code to do constraint checks prior to an INSERT or an UPDATE. ** -** When this routine is called, the stack contains (from bottom to top) -** the following values: +** The input is a range of consecutive registers as follows: ** ** 1. The rowid of the row to be updated before the update. This ** value is omitted unless we are doing an UPDATE that involves a -** change to the record number. +** change to the record number or writing to a virtual table. ** ** 2. The rowid of the row after the update. ** @@ -55008,15 +57656,20 @@ insert_cleanup: ** ** N. The data in the last column of the entry after the update. ** +** The regRowid parameter is the index of the register containing (2). +** ** The old rowid shown as entry (1) above is omitted unless both isUpdate ** and rowidChng are 1. isUpdate is true for UPDATEs and false for -** INSERTs and rowidChng is true if the record number is being changed. +** INSERTs. RowidChng means that the new rowid is explicitly specified by +** the update or insert statement. If rowidChng is false, it means that +** the rowid is computed automatically in an insert or that the rowid value +** is not modified by the update. ** -** The code generated by this routine pushes additional entries onto -** the stack which are the keys for new index entries for the new record. -** The order of index keys is the same as the order of the indices on -** the pTable->pIndex list. A key is only created for index i if -** aIdxUsed!=0 and aIdxUsed[i]!=0. +** The code generated by this routine store new index entries into +** registers identified by aRegIdx[]. No index entry is created for +** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is +** the same as the order of indices on the linked list of indices +** attached to the table. ** ** This routine also generates code to check constraints. NOT NULL, ** CHECK, and UNIQUE constraints are all checked. If a constraint fails, @@ -55058,23 +57711,18 @@ insert_cleanup: ** for the constraint is used. ** ** The calling routine must open a read/write cursor for pTab with -** cursor number "base". All indices of pTab must also have open -** read/write cursors with cursor number base+i for the i-th cursor. +** cursor number "baseCur". All indices of pTab must also have open +** read/write cursors with cursor number baseCur+i for the i-th cursor. ** Except, if there is no possibility of a REPLACE action then -** cursors do not need to be open for indices where aIdxUsed[i]==0. -** -** If the isUpdate flag is true, it means that the "base" cursor is -** initially pointing to an entry that is being updated. The isUpdate -** flag causes extra code to be generated so that the "base" cursor -** is still pointing at the same entry after the routine returns. -** Without the isUpdate flag, the "base" cursor might be moved. +** cursors do not need to be open for indices where aRegIdx[i]==0. */ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( Parse *pParse, /* The parser context */ Table *pTab, /* the table into which we are inserting */ - int base, /* Index of a read/write cursor pointing at pTab */ - char *aIdxUsed, /* Which indices are used. NULL means all are used */ - int rowidChng, /* True if the record number will change */ + int baseCur, /* Index of a read/write cursor pointing at pTab */ + int regRowid, /* Index of the range of input registers */ + int *aRegIdx, /* Register used by each index. 0 for unused indices */ + int rowidChng, /* True if the rowid might collide with existing entry */ int isUpdate, /* True for UPDATE, False for INSERT */ int overrideError, /* Override onError to this if not OE_Default */ int ignoreDest /* Jump to this label on an OE_Ignore resolution */ @@ -55083,18 +57731,19 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( Vdbe *v; int nCol; int onError; - int addr; - int extra; + int j1, j2, j3; /* Addresses of jump instructions */ + int regData; /* Register containing first data column */ int iCur; Index *pIdx; int seenReplace = 0; - int jumpInst1=0, jumpInst2; int hasTwoRowids = (isUpdate && rowidChng); v = sqlite3GetVdbe(pParse); assert( v!=0 ); assert( pTab->pSelect==0 ); /* This table is not a VIEW */ nCol = pTab->nCol; + regData = regRowid + 1; + /* Test all NOT NULL constraints. */ @@ -55112,8 +57761,7 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ onError = OE_Abort; } - sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1); - addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0); + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i); assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail || onError==OE_Ignore || onError==OE_Replace ); switch( onError ){ @@ -55121,24 +57769,22 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( case OE_Abort: case OE_Fail: { char *zMsg = 0; - sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError); sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName, " may not be NULL", (char*)0); - sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC); + sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC); break; } case OE_Ignore: { - sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); break; } case OE_Replace: { - sqlite3ExprCode(pParse, pTab->aCol[i].pDflt); - sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0); + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i); break; } } - sqlite3VdbeJumpHere(v, addr); + sqlite3VdbeJumpHere(v, j1); } /* Test all CHECK constraints @@ -55146,17 +57792,13 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( #ifndef SQLITE_OMIT_CHECK if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ int allOk = sqlite3VdbeMakeLabel(v); - assert( pParse->ckOffset==0 ); - pParse->ckOffset = nCol; - sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1); - assert( pParse->ckOffset==nCol ); - pParse->ckOffset = 0; + pParse->ckBase = regData; + sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); onError = overrideError!=OE_Default ? overrideError : OE_Abort; if( onError==OE_Ignore ){ - sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); }else{ - sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError); + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_CONSTRAINT, onError); } sqlite3VdbeResolveLabel(v, allOk); } @@ -55174,70 +57816,65 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( onError = OE_Abort; } - if( isUpdate ){ - sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); - sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); - jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0); - } - sqlite3VdbeAddOp(v, OP_Dup, nCol, 1); - jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0); - switch( onError ){ - default: { - onError = OE_Abort; - /* Fall thru into the next case */ - } - case OE_Rollback: - case OE_Abort: - case OE_Fail: { - sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, - "PRIMARY KEY must be unique", P3_STATIC); - break; - } - case OE_Replace: { - sqlite3GenerateRowIndexDelete(v, pTab, base, 0); - if( isUpdate ){ - sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1); - sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); + if( onError!=OE_Replace || pTab->pIndex ){ + if( isUpdate ){ + j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, regRowid-1); + } + j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); + switch( onError ){ + default: { + onError = OE_Abort; + /* Fall thru into the next case */ + } + case OE_Rollback: + case OE_Abort: + case OE_Fail: { + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, + "PRIMARY KEY must be unique", P4_STATIC); + break; + } + case OE_Replace: { + sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); + seenReplace = 1; + break; + } + case OE_Ignore: { + assert( seenReplace==0 ); + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); + break; } - seenReplace = 1; - break; } - case OE_Ignore: { - assert( seenReplace==0 ); - sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); - break; + sqlite3VdbeJumpHere(v, j3); + if( isUpdate ){ + sqlite3VdbeJumpHere(v, j2); } } - sqlite3VdbeJumpHere(v, jumpInst2); - if( isUpdate ){ - sqlite3VdbeJumpHere(v, jumpInst1); - sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1); - sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); - } } /* Test all UNIQUE constraints by creating entries for each UNIQUE ** index and making sure that duplicate entries do not already exist. ** Add the new records to the indices as we go. */ - extra = -1; for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ - if( aIdxUsed && aIdxUsed[iCur]==0 ) continue; /* Skip unused indices */ - extra++; + int regIdx; + int regR; + + if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */ /* Create a key for accessing the index entry */ - sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1); + regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1); for(i=0; inColumn; i++){ int idx = pIdx->aiColumn[i]; if( idx==pTab->iPKey ){ - sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1); + sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); }else{ - sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1); + sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i); } } - jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0); + sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]); sqlite3IndexAffinityStr(v, pIdx); + sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); /* Find out what action to take in case there is an indexing conflict */ onError = pIdx->onError; @@ -55254,8 +57891,12 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( /* Check to see if the new index entry will be unique */ - sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1); - jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0); + j2 = sqlite3VdbeAddOp3(v, OP_IsNull, regIdx, 0, pIdx->nColumn); + regR = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_SCopy, regRowid-hasTwoRowids, regR); + j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0, + regR, (char*)(sqlite3_intptr_t)aRegIdx[iCur], + P4_INT32); /* Generate code that executes if the new index entry is not unique */ assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail @@ -55287,38 +57928,31 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( } sqlite3_snprintf(sizeof(zErrMsg)-n1, &zErrMsg[n1], pIdx->nColumn>1 ? " are not unique" : " is not unique"); - sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0); + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, zErrMsg,0); break; } case OE_Ignore: { assert( seenReplace==0 ); - sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest); + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); break; } case OE_Replace: { - sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0); - if( isUpdate ){ - sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1); - sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); - } + sqlite3GenerateRowDelete(pParse, pTab, baseCur, regR, 0); seenReplace = 1; break; } } -#if NULL_DISTINCT_FOR_UNIQUE - sqlite3VdbeJumpHere(v, jumpInst1); -#endif - sqlite3VdbeJumpHere(v, jumpInst2); + sqlite3VdbeJumpHere(v, j2); + sqlite3VdbeJumpHere(v, j3); + sqlite3ReleaseTempReg(pParse, regR); } } /* ** This routine generates code to finish the INSERT or UPDATE operation ** that was started by a prior call to sqlite3GenerateConstraintChecks. -** The stack must contain keys for all active indices followed by data -** and the rowid for the new entry. This routine creates the new -** entries in all indices and in the main table. +** A consecutive range of registers starting at regRowid contains the +** rowid and the content to be inserted. ** ** The arguments to this routine should be the same as the first six ** arguments to sqlite3GenerateConstraintChecks. @@ -55326,8 +57960,9 @@ SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( SQLITE_PRIVATE void sqlite3CompleteInsertion( Parse *pParse, /* The parser context */ Table *pTab, /* the table into which we are inserting */ - int base, /* Index of a read/write cursor pointing at pTab */ - char *aIdxUsed, /* Which indices are used. NULL means all are used */ + int baseCur, /* Index of a read/write cursor pointing at pTab */ + int regRowid, /* Range of content */ + int *aRegIdx, /* Register used by each index. 0 for unused indices */ int rowidChng, /* True if the record number will change */ int isUpdate, /* True for UPDATE, False for INSERT */ int newIdx, /* Index of NEW table for triggers. -1 if none */ @@ -55338,22 +57973,24 @@ SQLITE_PRIVATE void sqlite3CompleteInsertion( int nIdx; Index *pIdx; int pik_flags; + int regData; + int regRec; v = sqlite3GetVdbe(pParse); assert( v!=0 ); assert( pTab->pSelect==0 ); /* This table is not a VIEW */ for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} for(i=nIdx-1; i>=0; i--){ - if( aIdxUsed && aIdxUsed[i]==0 ) continue; - sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0); + if( aRegIdx[i]==0 ) continue; + sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]); } - sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); + regData = regRowid + 1; + regRec = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); sqlite3TableAffinityStr(v, pTab); #ifndef SQLITE_OMIT_TRIGGER if( newIdx>=0 ){ - sqlite3VdbeAddOp(v, OP_Dup, 1, 0); - sqlite3VdbeAddOp(v, OP_Dup, 1, 0); - sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); + sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRec, regRowid); } #endif if( pParse->nested ){ @@ -55365,25 +58002,24 @@ SQLITE_PRIVATE void sqlite3CompleteInsertion( if( appendBias ){ pik_flags |= OPFLAG_APPEND; } - sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags); + sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid); if( !pParse->nested ){ - sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC); - } - - if( isUpdate && rowidChng ){ - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); + sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); } + sqlite3VdbeChangeP5(v, pik_flags); } /* ** Generate code that will open cursors for a table and for all -** indices of that table. The "base" parameter is the cursor number used +** indices of that table. The "baseCur" parameter is the cursor number used ** for the table. Indices are opened on subsequent cursors. +** +** Return the number of indices on the table. */ -SQLITE_PRIVATE void sqlite3OpenTableAndIndices( +SQLITE_PRIVATE int sqlite3OpenTableAndIndices( Parse *pParse, /* Parsing context */ Table *pTab, /* Table to be opened */ - int base, /* Cursor number assigned to the table */ + int baseCur, /* Cursor number assigned to the table */ int op /* OP_OpenRead or OP_OpenWrite */ ){ int i; @@ -55391,21 +58027,22 @@ SQLITE_PRIVATE void sqlite3OpenTableAndIndices( Index *pIdx; Vdbe *v; - if( IsVirtual(pTab) ) return; + if( IsVirtual(pTab) ) return 0; iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); v = sqlite3GetVdbe(pParse); assert( v!=0 ); - sqlite3OpenTable(pParse, base, iDb, pTab, op); + sqlite3OpenTable(pParse, baseCur, iDb, pTab, op); for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); assert( pIdx->pSchema==pTab->pSchema ); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - VdbeComment((v, "# %s", pIdx->zName)); - sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIdx->zName)); } - if( pParse->nTab<=base+i ){ - pParse->nTab = base+i; + if( pParse->nTab<=baseCur+i ){ + pParse->nTab = baseCur+i; } + return i-1; } @@ -55463,7 +58100,7 @@ static int xferCompatibleIndex(Index *pDest, Index *pSrc){ return 0; /* Different sort orders */ } if( pSrc->azColl[i]!=pDest->azColl[i] ){ - return 0; /* Different sort orders */ + return 0; /* Different collating sequences */ } } @@ -55521,8 +58158,9 @@ static int xferOptimization( int emptySrcTest; /* Address of test for empty pSrc */ Vdbe *v; /* The VDBE we are building */ KeyInfo *pKey; /* Key information for an index */ - int counterMem; /* Memory register used by AUTOINC */ + int regAutoinc; /* Memory register used by AUTOINC */ int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ + int regData, regRowid; /* Registers holding data and rowid */ if( pSelect==0 ){ return 0; /* Must be of the form INSERT INTO ... SELECT ... */ @@ -55584,7 +58222,7 @@ static int xferOptimization( ** we have to check the semantics. */ pItem = pSelect->pSrc->a; - pSrc = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase); + pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); if( pSrc==0 ){ return 0; /* FROM clause does not contain a real table */ } @@ -55649,7 +58287,7 @@ static int xferOptimization( sqlite3CodeVerifySchema(pParse, iDbSrc); iSrc = pParse->nTab++; iDest = pParse->nTab++; - counterMem = autoIncBegin(pParse, iDbDest, pDest); + regAutoinc = autoIncBegin(pParse, iDbDest, pDest); sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){ /* If tables do not have an INTEGER PRIMARY KEY and there @@ -55662,64 +58300,65 @@ static int xferOptimization( ** insure that all entries in the union of DEST and SRC will be ** unique. */ - addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iDest, 0); - emptyDestTest = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); + emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); sqlite3VdbeJumpHere(v, addr1); }else{ emptyDestTest = 0; } sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); - emptySrcTest = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0); + emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); + regData = sqlite3GetTempReg(pParse); + regRowid = sqlite3GetTempReg(pParse); if( pDest->iPKey>=0 ){ - addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0); - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - addr2 = sqlite3VdbeAddOp(v, OP_NotExists, iDest, 0); - sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, - "PRIMARY KEY must be unique", P3_STATIC); + addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); + addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, + "PRIMARY KEY must be unique", P4_STATIC); sqlite3VdbeJumpHere(v, addr2); - autoIncStep(pParse, counterMem); + autoIncStep(pParse, regAutoinc, regRowid); }else if( pDest->pIndex==0 ){ - addr1 = sqlite3VdbeAddOp(v, OP_NewRowid, iDest, 0); + addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); }else{ - addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0); + addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); assert( pDest->autoInc==0 ); } - sqlite3VdbeAddOp(v, OP_RowData, iSrc, 0); - sqlite3VdbeOp3(v, OP_Insert, iDest, - OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND, - pDest->zName, 0); - sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1); - autoIncEnd(pParse, iDbDest, pDest, counterMem); + sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); + sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); + sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); + sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); + autoIncEnd(pParse, iDbDest, pDest, regAutoinc); for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; } assert( pSrcIdx ); - sqlite3VdbeAddOp(v, OP_Close, iSrc, 0); - sqlite3VdbeAddOp(v, OP_Close, iDest, 0); - sqlite3VdbeAddOp(v, OP_Integer, iDbSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx); - VdbeComment((v, "# %s", pSrcIdx->zName)); - sqlite3VdbeOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, - (char*)pKey, P3_KEYINFO_HANDOFF); - sqlite3VdbeAddOp(v, OP_Integer, iDbDest, 0); + sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pSrcIdx->zName)); pKey = sqlite3IndexKeyinfo(pParse, pDestIdx); - VdbeComment((v, "# %s", pDestIdx->zName)); - sqlite3VdbeOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, - (char*)pKey, P3_KEYINFO_HANDOFF); - addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0); - sqlite3VdbeAddOp(v, OP_RowKey, iSrc, 0); - sqlite3VdbeAddOp(v, OP_IdxInsert, iDest, 1); - sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1+1); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pDestIdx->zName)); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); + sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); sqlite3VdbeJumpHere(v, addr1); } sqlite3VdbeJumpHere(v, emptySrcTest); - sqlite3VdbeAddOp(v, OP_Close, iSrc, 0); - sqlite3VdbeAddOp(v, OP_Close, iDest, 0); + sqlite3ReleaseTempReg(pParse, regRowid); + sqlite3ReleaseTempReg(pParse, regData); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); if( emptyDestTest ){ - sqlite3VdbeAddOp(v, OP_Halt, SQLITE_OK, 0); + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); sqlite3VdbeJumpHere(v, emptyDestTest); - sqlite3VdbeAddOp(v, OP_Close, iDest, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); return 0; }else{ return 1; @@ -55745,7 +58384,7 @@ static int xferOptimization( ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: legacy.c,v 1.22 2007/08/29 12:31:26 danielk1977 Exp $ +** $Id: legacy.c,v 1.23 2008/02/13 18:25:27 danielk1977 Exp $ */ @@ -55811,6 +58450,10 @@ SQLITE_API int sqlite3_exec( if( 0==nCallback ){ for(i=0; imallocFailed = 1; + goto exec_out; + } } nCallback++; } @@ -55818,6 +58461,10 @@ SQLITE_API int sqlite3_exec( azVals = &azCols[nCol]; for(i=0; imallocFailed = 1; + goto exec_out; + } } } if( xCallback(pArg, nCol, azVals, azCols) ){ @@ -55880,7 +58527,9 @@ exec_out: */ #ifndef SQLITE_OMIT_LOAD_EXTENSION -#define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ +#ifndef SQLITE_CORE + #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ +#endif /************** Include sqlite3ext.h in the middle of loadext.c **************/ /************** Begin file sqlite3ext.h **************************************/ /* @@ -55900,7 +58549,7 @@ exec_out: ** as extensions by SQLite should #include this file instead of ** sqlite3.h. ** -** @(#) $Id: sqlite3ext.h,v 1.17 2007/08/31 16:11:36 drh Exp $ +** @(#) $Id: sqlite3ext.h,v 1.18 2008/03/02 03:32:05 mlcreech Exp $ */ #ifndef _SQLITE3EXT_H_ #define _SQLITE3EXT_H_ @@ -55908,13 +58557,13 @@ exec_out: typedef struct sqlite3_api_routines sqlite3_api_routines; /* -** The following structure hold pointers to all of the SQLite API +** The following structure holds pointers to all of the SQLite API ** routines. ** ** WARNING: In order to maintain backwards compatibility, add new ** interfaces to the end of this structure only. If you insert new ** interfaces in the middle of this structure, then older different -** versions of SQLite will not be able to load each others shared +** versions of SQLite will not be able to load each others' shared ** libraries! */ struct sqlite3_api_routines { @@ -56333,7 +58982,7 @@ struct sqlite3_api_routines { ** also check to make sure that the pointer to the function is ** not NULL before calling it. */ -SQLITE_API const sqlite3_api_routines sqlite3_apis = { +SQLITE_PRIVATE const sqlite3_api_routines sqlite3Apis = { sqlite3_aggregate_context, sqlite3_aggregate_count, sqlite3_bind_blob, @@ -56574,7 +59223,7 @@ static int sqlite3LoadExtension( sqlite3OsDlClose(pVfs, handle); } return SQLITE_ERROR; - }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){ + }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){ if( pzErrMsg ){ *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); } @@ -56718,7 +59367,7 @@ SQLITE_PRIVATE int sqlite3AutoLoadExtensions(sqlite3 *db){ autoext.aExt[i]; } sqlite3_mutex_leave(mutex); - if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){ + if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){ sqlite3Error(db, SQLITE_ERROR, "automatic extension loading failed: %s", zErrmsg); go = 0; @@ -56746,7 +59395,7 @@ SQLITE_PRIVATE int sqlite3AutoLoadExtensions(sqlite3 *db){ ************************************************************************* ** This file contains code used to implement the PRAGMA command. ** -** $Id: pragma.c,v 1.152 2007/12/13 21:54:11 drh Exp $ +** $Id: pragma.c,v 1.170 2008/02/13 18:25:27 danielk1977 Exp $ */ /* Ignore this whole file if pragmas are disabled @@ -56880,12 +59529,13 @@ static int changeTempStorage(Parse *pParse, const char *zStorageType){ */ static void returnSingleInt(Parse *pParse, const char *zLabel, int value){ Vdbe *v = sqlite3GetVdbe(pParse); - sqlite3VdbeAddOp(v, OP_Integer, value, 0); + int mem = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, value, mem); if( pParse->explain==0 ){ sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P3_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, P4_STATIC); } - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1); } #ifndef SQLITE_OMIT_FLAG_PRAGMAS @@ -56942,7 +59592,7 @@ static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ ** compiler (eg. count_changes). So add an opcode to expire all ** compiled SQL statements after modifying a pragma value. */ - sqlite3VdbeAddOp(v, OP_Expire, 0, 0); + sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); } } @@ -56982,8 +59632,9 @@ SQLITE_PRIVATE void sqlite3Pragma( int iDb; /* Database index for */ sqlite3 *db = pParse->db; Db *pDb; - Vdbe *v = sqlite3GetVdbe(pParse); + Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); if( v==0 ) return; + pParse->nMem = 2; /* Interpret the [database.] part of the pragma statement. iDb is the ** index of the database this pragma is being applied to in db.aDb[]. */ @@ -57030,20 +59681,21 @@ SQLITE_PRIVATE void sqlite3Pragma( */ if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ static const VdbeOpList getCacheSize[] = { - { OP_ReadCookie, 0, 2, 0}, /* 0 */ - { OP_AbsValue, 0, 0, 0}, - { OP_Dup, 0, 0, 0}, - { OP_Integer, 0, 0, 0}, - { OP_Ne, 0, 6, 0}, - { OP_Integer, 0, 0, 0}, /* 5 */ - { OP_Callback, 1, 0, 0}, + { OP_ReadCookie, 0, 1, 2}, /* 0 */ + { OP_IfPos, 1, 6, 0}, + { OP_Integer, 0, 2, 0}, + { OP_Subtract, 1, 2, 1}, + { OP_IfPos, 1, 6, 0}, + { OP_Integer, 0, 1, 0}, /* 5 */ + { OP_ResultRow, 1, 1, 0}, }; int addr; if( sqlite3ReadSchema(pParse) ) goto pragma_out; sqlite3VdbeUsesBtree(v, iDb); if( !zRight ){ sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P3_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", P4_STATIC); + pParse->nMem += 2; addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); sqlite3VdbeChangeP1(v, addr, iDb); sqlite3VdbeChangeP1(v, addr+5, SQLITE_DEFAULT_CACHE_SIZE); @@ -57051,12 +59703,12 @@ SQLITE_PRIVATE void sqlite3Pragma( int size = atoi(zRight); if( size<0 ) size = -size; sqlite3BeginWriteOperation(pParse, 0, iDb); - sqlite3VdbeAddOp(v, OP_Integer, size, 0); - sqlite3VdbeAddOp(v, OP_ReadCookie, iDb, 2); - addr = sqlite3VdbeAddOp(v, OP_Integer, 0, 0); - sqlite3VdbeAddOp(v, OP_Ge, 0, addr+3); - sqlite3VdbeAddOp(v, OP_Negative, 0, 0); - sqlite3VdbeAddOp(v, OP_SetCookie, iDb, 2); + sqlite3VdbeAddOp2(v, OP_Integer, size, 1); + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, 2, 2); + addr = sqlite3VdbeAddOp2(v, OP_IfPos, 2, 0); + sqlite3VdbeAddOp2(v, OP_Integer, -size, 1); + sqlite3VdbeJumpHere(v, addr); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 2, 1); pDb->pSchema->cache_size = size; sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); } @@ -57149,9 +59801,9 @@ SQLITE_PRIVATE void sqlite3Pragma( zRet = "exclusive"; } sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P3_STATIC); - sqlite3VdbeOp3(v, OP_String8, 0, 0, zRet, 0); - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", P4_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); }else #endif /* SQLITE_OMIT_PAGER_PRAGMAS */ @@ -57189,11 +59841,11 @@ SQLITE_PRIVATE void sqlite3Pragma( */ static const VdbeOpList setMeta6[] = { { OP_Transaction, 0, 1, 0}, /* 0 */ - { OP_ReadCookie, 0, 3, 0}, /* 1 */ - { OP_If, 0, 0, 0}, /* 2 */ + { OP_ReadCookie, 0, 1, 3}, /* 1 */ + { OP_If, 1, 0, 0}, /* 2 */ { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ - { OP_Integer, 0, 0, 0}, /* 4 */ - { OP_SetCookie, 0, 6, 0}, /* 5 */ + { OP_Integer, 0, 1, 0}, /* 4 */ + { OP_SetCookie, 0, 6, 1}, /* 5 */ }; int iAddr; iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6); @@ -57224,11 +59876,11 @@ SQLITE_PRIVATE void sqlite3Pragma( iLimit = 0x7fffffff; } sqlite3BeginWriteOperation(pParse, 0, iDb); - sqlite3VdbeAddOp(v, OP_MemInt, iLimit, 0); - addr = sqlite3VdbeAddOp(v, OP_IncrVacuum, iDb, 0); - sqlite3VdbeAddOp(v, OP_Callback, 0, 0); - sqlite3VdbeAddOp(v, OP_MemIncr, -1, 0); - sqlite3VdbeAddOp(v, OP_IfMemPos, 0, addr); + sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); + addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); + sqlite3VdbeAddOp1(v, OP_ResultRow, 1); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); + sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); sqlite3VdbeJumpHere(v, addr); }else #endif @@ -57294,9 +59946,9 @@ SQLITE_PRIVATE void sqlite3Pragma( if( sqlite3_temp_directory ){ sqlite3VdbeSetNumCols(v, 1); sqlite3VdbeSetColName(v, 0, COLNAME_NAME, - "temp_store_directory", P3_STATIC); - sqlite3VdbeOp3(v, OP_String8, 0, 0, sqlite3_temp_directory, 0); - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + "temp_store_directory", P4_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); } }else{ if( zRight[0] @@ -57374,12 +60026,13 @@ SQLITE_PRIVATE void sqlite3Pragma( int nHidden = 0; Column *pCol; sqlite3VdbeSetNumCols(v, 6); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); - sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P3_STATIC); - sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P3_STATIC); - sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P3_STATIC); - sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P3_STATIC); + pParse->nMem = 6; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", P4_STATIC); + sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", P4_STATIC); + sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", P4_STATIC); + sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", P4_STATIC); sqlite3ViewGetColumnNames(pParse, pTab); for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ const Token *pDflt; @@ -57387,18 +60040,18 @@ SQLITE_PRIVATE void sqlite3Pragma( nHidden++; continue; } - sqlite3VdbeAddOp(v, OP_Integer, i-nHidden, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pCol->zName, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, + sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pCol->zType ? pCol->zType : "", 0); - sqlite3VdbeAddOp(v, OP_Integer, pCol->notNull, 0); + sqlite3VdbeAddOp2(v, OP_Integer, pCol->notNull, 4); if( pCol->pDflt && (pDflt = &pCol->pDflt->span)->z ){ - sqlite3VdbeOp3(v, OP_String8, 0, 0, (char*)pDflt->z, pDflt->n); + sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pDflt->z, pDflt->n); }else{ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, 5); } - sqlite3VdbeAddOp(v, OP_Integer, pCol->isPrimKey, 0); - sqlite3VdbeAddOp(v, OP_Callback, 6, 0); + sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); } } }else @@ -57412,16 +60065,17 @@ SQLITE_PRIVATE void sqlite3Pragma( int i; pTab = pIdx->pTable; sqlite3VdbeSetNumCols(v, 3); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P3_STATIC); - sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P3_STATIC); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", P4_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", P4_STATIC); for(i=0; inColumn; i++){ int cnum = pIdx->aiColumn[i]; - sqlite3VdbeAddOp(v, OP_Integer, i, 0); - sqlite3VdbeAddOp(v, OP_Integer, cnum, 0); + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2); assert( pTab->nCol>cnum ); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->aCol[cnum].zName, 0); - sqlite3VdbeAddOp(v, OP_Callback, 3, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); } } }else @@ -57437,14 +60091,15 @@ SQLITE_PRIVATE void sqlite3Pragma( if( pIdx ){ int i = 0; sqlite3VdbeSetNumCols(v, 3); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); - sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P3_STATIC); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", P4_STATIC); while(pIdx){ - sqlite3VdbeAddOp(v, OP_Integer, i, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pIdx->zName, 0); - sqlite3VdbeAddOp(v, OP_Integer, pIdx->onError!=OE_None, 0); - sqlite3VdbeAddOp(v, OP_Callback, 3, 0); + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0); + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); ++i; pIdx = pIdx->pNext; } @@ -57456,17 +60111,18 @@ SQLITE_PRIVATE void sqlite3Pragma( int i; if( sqlite3ReadSchema(pParse) ) goto pragma_out; sqlite3VdbeSetNumCols(v, 3); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); - sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P3_STATIC); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", P4_STATIC); for(i=0; inDb; i++){ if( db->aDb[i].pBt==0 ) continue; assert( db->aDb[i].zName!=0 ); - sqlite3VdbeAddOp(v, OP_Integer, i, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); - sqlite3VdbeAddOp(v, OP_Callback, 3, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); } }else @@ -57474,13 +60130,14 @@ SQLITE_PRIVATE void sqlite3Pragma( int i = 0; HashElem *p; sqlite3VdbeSetNumCols(v, 2); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P3_STATIC); + pParse->nMem = 2; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", P4_STATIC); for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ CollSeq *pColl = (CollSeq *)sqliteHashData(p); - sqlite3VdbeAddOp(v, OP_Integer, i++, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pColl->zName, 0); - sqlite3VdbeAddOp(v, OP_Callback, 2, 0); + sqlite3VdbeAddOp2(v, OP_Integer, i++, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); } }else #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ @@ -57497,22 +60154,23 @@ SQLITE_PRIVATE void sqlite3Pragma( if( pFK ){ int i = 0; sqlite3VdbeSetNumCols(v, 5); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P3_STATIC); - sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P3_STATIC); - sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P3_STATIC); - sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P3_STATIC); + pParse->nMem = 5; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", P4_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", P4_STATIC); + sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", P4_STATIC); + sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", P4_STATIC); while(pFK){ int j; for(j=0; jnCol; j++){ char *zCol = pFK->aCol[j].zCol; - sqlite3VdbeAddOp(v, OP_Integer, i, 0); - sqlite3VdbeAddOp(v, OP_Integer, j, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, pFK->zTo, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, - pTab->aCol[pFK->aCol[j].iFrom].zName, 0); - sqlite3VdbeOp3(v, zCol ? OP_String8 : OP_Null, 0, 0, zCol, 0); - sqlite3VdbeAddOp(v, OP_Callback, 5, 0); + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp2(v, OP_Integer, j, 2); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, + pTab->aCol[pFK->aCol[j].iFrom].zName, 0); + sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 5); } ++i; pFK = pFK->pNextFrom; @@ -57548,7 +60206,13 @@ SQLITE_PRIVATE void sqlite3Pragma( #endif #ifndef SQLITE_OMIT_INTEGRITY_CHECK - if( sqlite3StrICmp(zLeft, "integrity_check")==0 ){ + /* Pragma "quick_check" is an experimental reduced version of + ** integrity_check designed to detect most database corruption + ** without most of the overhead of a full integrity-check. + */ + if( sqlite3StrICmp(zLeft, "integrity_check")==0 + || sqlite3StrICmp(zLeft, "quick_check")==0 + ){ int i, j, addr, mxErr; /* Code that appears at the end of the integrity check. If no error @@ -57556,17 +60220,19 @@ SQLITE_PRIVATE void sqlite3Pragma( ** error message */ static const VdbeOpList endCode[] = { - { OP_MemLoad, 0, 0, 0}, - { OP_Integer, 0, 0, 0}, - { OP_Ne, 0, 0, 0}, /* 2 */ - { OP_String8, 0, 0, "ok"}, - { OP_Callback, 1, 0, 0}, + { OP_AddImm, 1, 0, 0}, /* 0 */ + { OP_IfNeg, 1, 0, 0}, /* 1 */ + { OP_String8, 0, 3, 0}, /* 2 */ + { OP_ResultRow, 3, 1, 0}, }; + int isQuick = (zLeft[0]=='q'); + /* Initialize the VDBE program */ if( sqlite3ReadSchema(pParse) ) goto pragma_out; + pParse->nMem = 6; sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P3_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", P4_STATIC); /* Set the maximum error count */ mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; @@ -57576,7 +60242,7 @@ SQLITE_PRIVATE void sqlite3Pragma( mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; } } - sqlite3VdbeAddOp(v, OP_MemInt, mxErr, 0); + sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ /* Do an integrity check on each database file */ for(i=0; inDb; i++){ @@ -57587,100 +60253,116 @@ SQLITE_PRIVATE void sqlite3Pragma( if( OMIT_TEMPDB && i==1 ) continue; sqlite3CodeVerifySchema(pParse, i); - addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0); - sqlite3VdbeAddOp(v, OP_Halt, 0, 0); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); sqlite3VdbeJumpHere(v, addr); /* Do an integrity check of the B-Tree + ** + ** Begin by filling registers 2, 3, ... with the root pages numbers + ** for all tables and indices in the database. */ pTbls = &db->aDb[i].pSchema->tblHash; for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ Table *pTab = sqliteHashData(x); Index *pIdx; - sqlite3VdbeAddOp(v, OP_Integer, pTab->tnum, 0); + sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt); cnt++; for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - sqlite3VdbeAddOp(v, OP_Integer, pIdx->tnum, 0); + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt); cnt++; } } if( cnt==0 ) continue; - sqlite3VdbeAddOp(v, OP_IntegrityCk, 0, i); - addr = sqlite3VdbeAddOp(v, OP_IsNull, -1, 0); - sqlite3VdbeOp3(v, OP_String8, 0, 0, + + /* Make sure sufficient number of registers have been allocated */ + if( pParse->nMem < cnt+4 ){ + pParse->nMem = cnt+4; + } + + /* Do the b-tree integrity checks */ + sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1); + sqlite3VdbeChangeP5(v, i); + addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), - P3_DYNAMIC); - sqlite3VdbeAddOp(v, OP_Pull, 1, 0); - sqlite3VdbeAddOp(v, OP_Concat, 0, 0); - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_Move, 2, 4); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); + sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); sqlite3VdbeJumpHere(v, addr); /* Make sure all the indices are constructed correctly. */ - for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ Table *pTab = sqliteHashData(x); Index *pIdx; int loopTop; if( pTab->pIndex==0 ) continue; - addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0); - sqlite3VdbeAddOp(v, OP_Halt, 0, 0); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); sqlite3VdbeJumpHere(v, addr); sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead); - sqlite3VdbeAddOp(v, OP_MemInt, 0, 1); - loopTop = sqlite3VdbeAddOp(v, OP_Rewind, 1, 0); - sqlite3VdbeAddOp(v, OP_MemIncr, 1, 1); + sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */ + loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0); + sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */ for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ int jmp2; static const VdbeOpList idxErr[] = { - { OP_MemIncr, -1, 0, 0}, - { OP_String8, 0, 0, "rowid "}, - { OP_Rowid, 1, 0, 0}, - { OP_String8, 0, 0, " missing from index "}, - { OP_String8, 0, 0, 0}, /* 4 */ - { OP_Concat, 2, 0, 0}, - { OP_Callback, 1, 0, 0}, + { OP_AddImm, 1, -1, 0}, + { OP_String8, 0, 3, 0}, /* 1 */ + { OP_Rowid, 1, 4, 0}, + { OP_String8, 0, 5, 0}, /* 3 */ + { OP_String8, 0, 6, 0}, /* 4 */ + { OP_Concat, 4, 3, 3}, + { OP_Concat, 5, 3, 3}, + { OP_Concat, 6, 3, 3}, + { OP_ResultRow, 3, 1, 0}, }; - sqlite3GenerateIndexKey(v, pIdx, 1); - jmp2 = sqlite3VdbeAddOp(v, OP_Found, j+2, 0); + sqlite3GenerateIndexKey(pParse, pIdx, 1, 3); + jmp2 = sqlite3VdbeAddOp3(v, OP_Found, j+2, 0, 3); addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); - sqlite3VdbeChangeP3(v, addr+4, pIdx->zName, P3_STATIC); + sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC); + sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC); + sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC); sqlite3VdbeJumpHere(v, jmp2); } - sqlite3VdbeAddOp(v, OP_Next, 1, loopTop+1); + sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1); sqlite3VdbeJumpHere(v, loopTop); for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ static const VdbeOpList cntIdx[] = { - { OP_MemInt, 0, 2, 0}, + { OP_Integer, 0, 3, 0}, { OP_Rewind, 0, 0, 0}, /* 1 */ - { OP_MemIncr, 1, 2, 0}, + { OP_AddImm, 3, 1, 0}, { OP_Next, 0, 0, 0}, /* 3 */ - { OP_MemLoad, 1, 0, 0}, - { OP_MemLoad, 2, 0, 0}, - { OP_Eq, 0, 0, 0}, /* 6 */ - { OP_MemIncr, -1, 0, 0}, - { OP_String8, 0, 0, "wrong # of entries in index "}, - { OP_String8, 0, 0, 0}, /* 9 */ - { OP_Concat, 0, 0, 0}, - { OP_Callback, 1, 0, 0}, + { OP_Eq, 2, 0, 3}, /* 4 */ + { OP_AddImm, 1, -1, 0}, + { OP_String8, 0, 2, 0}, /* 6 */ + { OP_String8, 0, 3, 0}, /* 7 */ + { OP_Concat, 3, 2, 2}, + { OP_ResultRow, 2, 1, 0}, }; if( pIdx->tnum==0 ) continue; - addr = sqlite3VdbeAddOp(v, OP_IfMemPos, 0, 0); - sqlite3VdbeAddOp(v, OP_Halt, 0, 0); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); sqlite3VdbeJumpHere(v, addr); addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); sqlite3VdbeChangeP1(v, addr+1, j+2); sqlite3VdbeChangeP2(v, addr+1, addr+4); sqlite3VdbeChangeP1(v, addr+3, j+2); sqlite3VdbeChangeP2(v, addr+3, addr+2); - sqlite3VdbeJumpHere(v, addr+6); - sqlite3VdbeChangeP3(v, addr+9, pIdx->zName, P3_STATIC); + sqlite3VdbeJumpHere(v, addr+4); + sqlite3VdbeChangeP4(v, addr+6, + "wrong # of entries in index ", P4_STATIC); + sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC); } } } addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); - sqlite3VdbeChangeP1(v, addr+1, mxErr); - sqlite3VdbeJumpHere(v, addr+2); + sqlite3VdbeChangeP2(v, addr, -mxErr); + sqlite3VdbeJumpHere(v, addr+1); + sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC); }else #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ @@ -57726,15 +60408,15 @@ SQLITE_PRIVATE void sqlite3Pragma( if( !zRight ){ /* "PRAGMA encoding" */ if( sqlite3ReadSchema(pParse) ) goto pragma_out; sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P3_STATIC); - sqlite3VdbeAddOp(v, OP_String8, 0, 0); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", P4_STATIC); + sqlite3VdbeAddOp2(v, OP_String8, 0, 1); for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ if( pEnc->enc==ENC(pParse->db) ){ - sqlite3VdbeChangeP3(v, -1, pEnc->zName, P3_STATIC); + sqlite3VdbeChangeP4(v, -1, pEnc->zName, P4_STATIC); break; } } - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); }else{ /* "PRAGMA encoding = XXX" */ /* Only change the value of sqlite.enc if the database handle is not ** initialized. If the main database exists, the new sqlite.enc value @@ -57810,8 +60492,8 @@ SQLITE_PRIVATE void sqlite3Pragma( /* Write the specified cookie value */ static const VdbeOpList setCookie[] = { { OP_Transaction, 0, 1, 0}, /* 0 */ - { OP_Integer, 0, 0, 0}, /* 1 */ - { OP_SetCookie, 0, 0, 0}, /* 2 */ + { OP_Integer, 0, 1, 0}, /* 1 */ + { OP_SetCookie, 0, 0, 1}, /* 2 */ }; int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); sqlite3VdbeChangeP1(v, addr, iDb); @@ -57821,14 +60503,14 @@ SQLITE_PRIVATE void sqlite3Pragma( }else{ /* Read the specified cookie value */ static const VdbeOpList readCookie[] = { - { OP_ReadCookie, 0, 0, 0}, /* 0 */ - { OP_Callback, 1, 0, 0} + { OP_ReadCookie, 0, 1, 0}, /* 0 */ + { OP_ResultRow, 1, 1, 0} }; int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie); sqlite3VdbeChangeP1(v, addr, iDb); - sqlite3VdbeChangeP2(v, addr, iCookie); + sqlite3VdbeChangeP3(v, addr, iCookie); sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P3_TRANSIENT); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, P4_TRANSIENT); } }else #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ @@ -57844,24 +60526,25 @@ SQLITE_PRIVATE void sqlite3Pragma( int i; Vdbe *v = sqlite3GetVdbe(pParse); sqlite3VdbeSetNumCols(v, 2); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P3_STATIC); - sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P3_STATIC); + pParse->nMem = 2; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", P4_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", P4_STATIC); for(i=0; inDb; i++){ Btree *pBt; Pager *pPager; const char *zState = "unknown"; int j; if( db->aDb[i].zName==0 ) continue; - sqlite3VdbeOp3(v, OP_String8, 0, 0, db->aDb[i].zName, P3_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC); pBt = db->aDb[i].pBt; if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ zState = "closed"; - }else if( sqlite3_file_control(db, db->aDb[i].zName, + }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ zState = azLockName[j]; } - sqlite3VdbeOp3(v, OP_String8, 0, 0, zState, P3_STATIC); - sqlite3VdbeAddOp(v, OP_Callback, 2, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); } }else #endif @@ -57906,7 +60589,7 @@ SQLITE_PRIVATE void sqlite3Pragma( ** the VDBE implementing the pragma to expire. Most (all?) pragmas ** are only valid for a single execution. */ - sqlite3VdbeAddOp(v, OP_Expire, 1, 0); + sqlite3VdbeAddOp2(v, OP_Expire, 1, 0); /* ** Reset the safety level, in case the fullfsync flag or synchronous @@ -57943,7 +60626,7 @@ pragma_out: ** interface, and routines that contribute to loading the database schema ** from disk. ** -** $Id: prepare.c,v 1.66 2007/12/13 21:54:11 drh Exp $ +** $Id: prepare.c,v 1.78 2008/03/08 12:23:31 drh Exp $ */ /* @@ -58014,6 +60697,8 @@ SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char sqlite3_free(zErr); return 1; } + }else if( argv[0]==0 ){ + corruptSchema(pData, 0); }else{ /* If the SQL column is blank it means this is an index that ** was created to be the PRIMARY KEY or to fulfill a UNIQUE @@ -58086,6 +60771,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ assert( iDb>=0 && iDbnDb ); assert( db->aDb[iDb].pSchema ); assert( sqlite3_mutex_held(db->mutex) ); + assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); /* zMasterSchema and zInitScript are set to point at the master schema ** and initialisation script appropriate for the database being @@ -58099,7 +60785,6 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ zMasterName = SCHEMA_TABLE(iDb); /* Construct the schema tables. */ - sqlite3SafetyOff(db); azArg[0] = zMasterName; azArg[1] = "1"; azArg[2] = zMasterSchema; @@ -58107,9 +60792,10 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ initData.db = db; initData.iDb = iDb; initData.pzErrMsg = pzErrMsg; + (void)sqlite3SafetyOff(db); rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0); + (void)sqlite3SafetyOn(db); if( rc ){ - sqlite3SafetyOn(db); rc = initData.rc; goto error_out; } @@ -58117,7 +60803,6 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ if( pTab ){ pTab->readOnly = 1; } - sqlite3SafetyOn(db); /* Create a cursor to hold the database open */ @@ -58196,6 +60881,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ size = meta[2]; if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } + if( size<0 ) size = -size; pDb->pSchema->cache_size = size; sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); @@ -58236,7 +60922,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ zSql = sqlite3MPrintf(db, "SELECT name, rootpage, sql FROM '%q'.%s", db->aDb[iDb].zName, zMasterName); - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); #ifndef SQLITE_OMIT_AUTHORIZATION { int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); @@ -58249,7 +60935,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ } #endif if( rc==SQLITE_ABORT ) rc = initData.rc; - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); sqlite3_free(zSql); #ifndef SQLITE_OMIT_ANALYZE if( rc==SQLITE_OK ){ @@ -58416,7 +61102,7 @@ SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ /* ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. */ -SQLITE_PRIVATE int sqlite3Prepare( +static int sqlite3Prepare( sqlite3 *db, /* Database handle. */ const char *zSql, /* UTF-8 encoded SQL statement. */ int nBytes, /* Length of zSql in bytes. */ @@ -58448,7 +61134,7 @@ SQLITE_PRIVATE int sqlite3Prepare( if( rc ){ const char *zDb = db->aDb[i].zName; sqlite3Error(db, SQLITE_LOCKED, "database schema is locked: %s", zDb); - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); return SQLITE_LOCKED; } } @@ -58458,7 +61144,9 @@ SQLITE_PRIVATE int sqlite3Prepare( sParse.db = db; if( nBytes>=0 && zSql[nBytes]!=0 ){ char *zSqlCopy; - if( nBytes>SQLITE_MAX_SQL_LENGTH ){ + if( SQLITE_MAX_SQL_LENGTH>0 && nBytes>SQLITE_MAX_SQL_LENGTH ){ + sqlite3Error(db, SQLITE_TOOBIG, "statement too long"); + (void)sqlite3SafetyOff(db); return SQLITE_TOOBIG; } zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); @@ -58493,16 +61181,19 @@ SQLITE_PRIVATE int sqlite3Prepare( if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ if( sParse.explain==2 ){ sqlite3VdbeSetNumCols(sParse.pVdbe, 3); - sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P3_STATIC); - sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P3_STATIC); - sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P3_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "order", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "from", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "detail", P4_STATIC); }else{ - sqlite3VdbeSetNumCols(sParse.pVdbe, 5); - sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P3_STATIC); - sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P3_STATIC); - sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P3_STATIC); - sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P3_STATIC); - sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P3_STATIC); + sqlite3VdbeSetNumCols(sParse.pVdbe, 8); + sqlite3VdbeSetColName(sParse.pVdbe, 0, COLNAME_NAME, "addr", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 1, COLNAME_NAME, "opcode", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 2, COLNAME_NAME, "p1", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 3, COLNAME_NAME, "p2", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 4, COLNAME_NAME, "p3", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 5, COLNAME_NAME, "p4", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 6, COLNAME_NAME, "p5", P4_STATIC); + sqlite3VdbeSetColName(sParse.pVdbe, 7, COLNAME_NAME, "comment",P4_STATIC); } } #endif @@ -58529,7 +61220,6 @@ SQLITE_PRIVATE int sqlite3Prepare( } rc = sqlite3ApiExit(db, rc); - /* sqlite3ReleaseThreadData(); */ assert( (rc&db->errMask)==rc ); return rc; } @@ -58542,7 +61232,7 @@ static int sqlite3LockAndPrepare( const char **pzTail /* OUT: End of parsed string */ ){ int rc; - if( sqlite3SafetyCheck(db) ){ + if( !sqlite3SafetyCheckOk(db) ){ return SQLITE_MISUSE; } sqlite3_mutex_enter(db->mutex); @@ -58566,9 +61256,7 @@ SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){ assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); zSql = sqlite3_sql((sqlite3_stmt *)p); - if( zSql==0 ){ - return 0; - } + assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ db = sqlite3VdbeDb(p); assert( sqlite3_mutex_held(db->mutex) ); rc = sqlite3LockAndPrepare(db, zSql, -1, 0, &pNew, 0); @@ -58604,7 +61292,10 @@ SQLITE_API int sqlite3_prepare( sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const char **pzTail /* OUT: End of parsed string */ ){ - return sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail); + int rc; + rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; } SQLITE_API int sqlite3_prepare_v2( sqlite3 *db, /* Database handle. */ @@ -58613,7 +61304,10 @@ SQLITE_API int sqlite3_prepare_v2( sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const char **pzTail /* OUT: End of parsed string */ ){ - return sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail); + int rc; + rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; } @@ -58637,7 +61331,7 @@ static int sqlite3Prepare16( const char *zTail8 = 0; int rc = SQLITE_OK; - if( sqlite3SafetyCheck(db) ){ + if( !sqlite3SafetyCheckOk(db) ){ return SQLITE_MISUSE; } sqlite3_mutex_enter(db->mutex); @@ -58676,7 +61370,10 @@ SQLITE_API int sqlite3_prepare16( sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const void **pzTail /* OUT: End of parsed string */ ){ - return sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; } SQLITE_API int sqlite3_prepare16_v2( sqlite3 *db, /* Database handle. */ @@ -58685,7 +61382,10 @@ SQLITE_API int sqlite3_prepare16_v2( sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ const void **pzTail /* OUT: End of parsed string */ ){ - return sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; } #endif /* SQLITE_OMIT_UTF16 */ @@ -58706,7 +61406,7 @@ SQLITE_API int sqlite3_prepare16_v2( ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** -** $Id: select.c,v 1.372 2007/12/14 17:24:40 drh Exp $ +** $Id: select.c,v 1.415 2008/03/04 17:45:01 mlcreech Exp $ */ @@ -58726,6 +61426,16 @@ static void clearSelect(Select *p){ sqlite3ExprDelete(p->pOffset); } +/* +** Initialize a SelectDest structure. +*/ +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ + pDest->eDest = eDest; + pDest->iParm = iParm; + pDest->affinity = 0; + pDest->iMem = 0; +} + /* ** Allocate a new Select structure and return a pointer to that @@ -59077,22 +61787,34 @@ static int sqliteProcessJoin(Parse *pParse, Select *p){ static void pushOntoSorter( Parse *pParse, /* Parser context */ ExprList *pOrderBy, /* The ORDER BY clause */ - Select *pSelect /* The whole SELECT statement */ + Select *pSelect, /* The whole SELECT statement */ + int regData /* Register holding data to be sorted */ ){ Vdbe *v = pParse->pVdbe; - sqlite3ExprCodeExprList(pParse, pOrderBy); - sqlite3VdbeAddOp(v, OP_Sequence, pOrderBy->iECursor, 0); - sqlite3VdbeAddOp(v, OP_Pull, pOrderBy->nExpr + 1, 0); - sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr + 2, 0); - sqlite3VdbeAddOp(v, OP_IdxInsert, pOrderBy->iECursor, 0); + int nExpr = pOrderBy->nExpr; + int regBase = sqlite3GetTempRange(pParse, nExpr+2); + int regRecord = sqlite3GetTempReg(pParse); + sqlite3ExprCodeExprList(pParse, pOrderBy, regBase); + sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr); + sqlite3VdbeAddOp2(v, OP_Move, regData, regBase+nExpr+1); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3ReleaseTempRange(pParse, regBase, nExpr+2); if( pSelect->iLimit>=0 ){ int addr1, addr2; - addr1 = sqlite3VdbeAddOp(v, OP_IfMemZero, pSelect->iLimit+1, 0); - sqlite3VdbeAddOp(v, OP_MemIncr, -1, pSelect->iLimit+1); - addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); + int iLimit; + if( pSelect->pOffset ){ + iLimit = pSelect->iOffset+1; + }else{ + iLimit = pSelect->iLimit; + } + addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); + sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1); + addr2 = sqlite3VdbeAddOp0(v, OP_Goto); sqlite3VdbeJumpHere(v, addr1); - sqlite3VdbeAddOp(v, OP_Last, pOrderBy->iECursor, 0); - sqlite3VdbeAddOp(v, OP_Delete, pOrderBy->iECursor, 0); + sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor); + sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor); sqlite3VdbeJumpHere(v, addr2); pSelect->iLimit = -1; } @@ -59104,25 +61826,21 @@ static void pushOntoSorter( static void codeOffset( Vdbe *v, /* Generate code into this VM */ Select *p, /* The SELECT statement being coded */ - int iContinue, /* Jump here to skip the current record */ - int nPop /* Number of times to pop stack when jumping */ + int iContinue /* Jump here to skip the current record */ ){ if( p->iOffset>=0 && iContinue!=0 ){ int addr; - sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iOffset); - addr = sqlite3VdbeAddOp(v, OP_IfMemNeg, p->iOffset, 0); - if( nPop>0 ){ - sqlite3VdbeAddOp(v, OP_Pop, nPop, 0); - } - sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); - VdbeComment((v, "# skip OFFSET records")); + sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1); + addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset); + sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue); + VdbeComment((v, "skip OFFSET records")); sqlite3VdbeJumpHere(v, addr); } } /* -** Add code that will check to make sure the top N elements of the -** stack are distinct. iTab is a sorting index that holds previously +** Add code that will check to make sure the N registers starting at iMem +** form a distinct entry. iTab is a sorting index that holds previously ** seen combinations of the N values. A new entry is made in iTab ** if the current N values are new. ** @@ -59130,17 +61848,21 @@ static void codeOffset( ** stack if the top N elements are not distinct. */ static void codeDistinct( - Vdbe *v, /* Generate code into this VM */ + Parse *pParse, /* Parsing and code generating context */ int iTab, /* A sorting index used to test for distinctness */ int addrRepeat, /* Jump to here if not distinct */ - int N /* The top N elements of the stack must be distinct */ + int N, /* Number of elements */ + int iMem /* First element */ ){ - sqlite3VdbeAddOp(v, OP_MakeRecord, -N, 0); - sqlite3VdbeAddOp(v, OP_Distinct, iTab, sqlite3VdbeCurrentAddr(v)+3); - sqlite3VdbeAddOp(v, OP_Pop, N+1, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, addrRepeat); - VdbeComment((v, "# skip indistinct records")); - sqlite3VdbeAddOp(v, OP_IdxInsert, iTab, 0); + Vdbe *v; + int r1; + + v = pParse->pVdbe; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); + sqlite3VdbeAddOp3(v, OP_Found, iTab, addrRepeat, r1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); + sqlite3ReleaseTempReg(pParse, r1); } /* @@ -59149,7 +61871,12 @@ static void codeDistinct( ** column. We do this in a subroutine because the error occurs in multiple ** places. */ -static int checkForMultiColumnSelectError(Parse *pParse, int eDest, int nExpr){ +static int checkForMultiColumnSelectError( + Parse *pParse, /* Parse context. */ + SelectDest *pDest, /* Destination of SELECT results */ + int nExpr /* Number of result columns returned by SELECT */ +){ + int eDest = pDest->eDest; if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){ sqlite3ErrorMsg(pParse, "only a single result allowed for " "a SELECT that is part of an expression"); @@ -59168,7 +61895,7 @@ static int checkForMultiColumnSelectError(Parse *pParse, int eDest, int nExpr){ ** then data is pulled from srcTab and pEList is used only to get the ** datatypes for each column. */ -static int selectInnerLoop( +static void selectInnerLoop( Parse *pParse, /* The parser context */ Select *p, /* The complete select statement being coded */ ExprList *pEList, /* List of values being extracted */ @@ -59176,8 +61903,7 @@ static int selectInnerLoop( int nColumn, /* Number of columns in the source table */ ExprList *pOrderBy, /* If not NULL, sort results using this key */ int distinct, /* If >=0, make sure results are distinct */ - int eDest, /* How to dispose of the results */ - int iParm, /* An argument to the disposal method */ + SelectDest *pDest, /* How to dispose of the results */ int iContinue, /* Jump here to continue with next row */ int iBreak, /* Jump here to break out of the inner loop */ char *aff /* affinity string if eDest is SRT_Union */ @@ -59185,8 +61911,12 @@ static int selectInnerLoop( Vdbe *v = pParse->pVdbe; int i; int hasDistinct; /* True if the DISTINCT keyword is present */ + int regResult; /* Start of memory holding result set */ + int eDest = pDest->eDest; /* How to dispose of results */ + int iParm = pDest->iParm; /* First argument to disposal method */ + int nResultCol; /* Number of result columns */ - if( v==0 ) return 0; + if( v==0 ) return; assert( pEList!=0 ); /* If there was a LIMIT clause on the SELECT statement, then do the check @@ -59194,19 +61924,33 @@ static int selectInnerLoop( */ hasDistinct = distinct>=0 && pEList->nExpr>0; if( pOrderBy==0 && !hasDistinct ){ - codeOffset(v, p, iContinue, 0); + codeOffset(v, p, iContinue); } /* Pull the requested columns. */ if( nColumn>0 ){ + nResultCol = nColumn; + }else{ + nResultCol = pEList->nExpr; + } + if( pDest->iMem==0 ){ + pDest->iMem = sqlite3GetTempRange(pParse, nResultCol); + } + regResult = pDest->iMem; + if( nColumn>0 ){ for(i=0; ia[i].pExpr, regResult+i); } - }else{ - nColumn = pEList->nExpr; - sqlite3ExprCodeExprList(pParse, pEList); } + nColumn = nResultCol; /* If the DISTINCT keyword was present on the SELECT statement ** and this row has been seen before, then do not make this row @@ -59215,14 +61959,14 @@ static int selectInnerLoop( if( hasDistinct ){ assert( pEList!=0 ); assert( pEList->nExpr==nColumn ); - codeDistinct(v, distinct, iContinue, nColumn); + codeDistinct(pParse, distinct, iContinue, nColumn, regResult); if( pOrderBy==0 ){ - codeOffset(v, p, iContinue, nColumn); + codeOffset(v, p, iContinue); } } - if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){ - return 0; + if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ + return; } switch( eDest ){ @@ -59231,11 +61975,14 @@ static int selectInnerLoop( */ #ifndef SQLITE_OMIT_COMPOUND_SELECT case SRT_Union: { - sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); + int r1; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); if( aff ){ - sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); + sqlite3VdbeChangeP4(v, -1, aff, P4_STATIC); } - sqlite3VdbeAddOp(v, OP_IdxInsert, iParm, 0); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); break; } @@ -59244,11 +61991,12 @@ static int selectInnerLoop( ** the temporary table iParm. */ case SRT_Except: { - int addr; - addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); - sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); - sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3); - sqlite3VdbeAddOp(v, OP_Delete, iParm, 0); + int r1; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); + sqlite3VdbeChangeP4(v, -1, aff, P4_STATIC); + sqlite3VdbeAddOp2(v, OP_IdxDelete, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); break; } #endif @@ -59257,14 +62005,18 @@ static int selectInnerLoop( */ case SRT_Table: case SRT_EphemTab: { - sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); if( pOrderBy ){ - pushOntoSorter(pParse, pOrderBy, p); + pushOntoSorter(pParse, pOrderBy, p, r1); }else{ - sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0); - sqlite3VdbeAddOp(v, OP_Pull, 1, 0); - sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND); + int r2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3ReleaseTempReg(pParse, r2); } + sqlite3ReleaseTempReg(pParse, r1); break; } @@ -59274,23 +62026,22 @@ static int selectInnerLoop( ** item into the set table with bogus data. */ case SRT_Set: { - int addr1 = sqlite3VdbeCurrentAddr(v); int addr2; assert( nColumn==1 ); - sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); - p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr,(iParm>>16)&0xff); + addr2 = sqlite3VdbeAddOp1(v, OP_IsNull, regResult); + p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity); if( pOrderBy ){ /* At first glance you would think we could optimize out the ** ORDER BY in this case since the order of entries in the set ** does not matter. But there might be a LIMIT clause, in which ** case the order does matter */ - pushOntoSorter(pParse, pOrderBy, p); + pushOntoSorter(pParse, pOrderBy, p, regResult); }else{ - sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1); - sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0); + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); } sqlite3VdbeJumpHere(v, addr2); break; @@ -59299,8 +62050,7 @@ static int selectInnerLoop( /* If any row exist in the result set, record that fact and abort. */ case SRT_Exists: { - sqlite3VdbeAddOp(v, OP_MemInt, 1, iParm); - sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm); /* The LIMIT clause will terminate the loop for us */ break; } @@ -59312,9 +62062,9 @@ static int selectInnerLoop( case SRT_Mem: { assert( nColumn==1 ); if( pOrderBy ){ - pushOntoSorter(pParse, pOrderBy, p); + pushOntoSorter(pParse, pOrderBy, p, regResult); }else{ - sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); + sqlite3VdbeAddOp2(v, OP_Move, regResult, iParm); /* The LIMIT clause will jump out of the loop for us */ } break; @@ -59328,12 +62078,14 @@ static int selectInnerLoop( case SRT_Subroutine: case SRT_Callback: { if( pOrderBy ){ - sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); - pushOntoSorter(pParse, pOrderBy, p); + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); + pushOntoSorter(pParse, pOrderBy, p, r1); + sqlite3ReleaseTempReg(pParse, r1); }else if( eDest==SRT_Subroutine ){ - sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); + sqlite3VdbeAddOp2(v, OP_Gosub, 0, iParm); }else{ - sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn); } break; } @@ -59346,7 +62098,6 @@ static int selectInnerLoop( */ default: { assert( eDest==SRT_Discard ); - sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); break; } #endif @@ -59355,10 +62106,9 @@ static int selectInnerLoop( /* Jump to the end of the loop if the LIMIT is reached. */ if( p->iLimit>=0 && pOrderBy==0 ){ - sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit); - sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, iBreak); + sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1); + sqlite3VdbeAddOp2(v, OP_IfZero, p->iLimit, iBreak); } - return 0; } /* @@ -59373,8 +62123,8 @@ static int selectInnerLoop( ** ** Space to hold the KeyInfo structure is obtain from malloc. The calling ** function is responsible for seeing that this structure is eventually -** freed. Add the KeyInfo structure to the P3 field of an opcode using -** P3_KEYINFO_HANDOFF is the usual way of dealing with this. +** freed. Add the KeyInfo structure to the P4 field of an opcode using +** P4_KEYINFO_HANDOFF is the usual way of dealing with this. */ static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){ sqlite3 *db = pParse->db; @@ -59410,12 +62160,11 @@ static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){ ** routine generates the code needed to do that. */ static void generateSortTail( - Parse *pParse, /* Parsing context */ - Select *p, /* The SELECT statement */ - Vdbe *v, /* Generate code into this VDBE */ - int nColumn, /* Number of columns of data */ - int eDest, /* Write the sorted results here */ - int iParm /* Optional parameter associated with eDest */ + Parse *pParse, /* Parsing context */ + Select *p, /* The SELECT statement */ + Vdbe *v, /* Generate code into this VDBE */ + int nColumn, /* Number of columns of data */ + SelectDest *pDest /* Write the sorted results here */ ){ int brk = sqlite3VdbeMakeLabel(v); int cont = sqlite3VdbeMakeLabel(v); @@ -59424,39 +62173,44 @@ static void generateSortTail( int pseudoTab = 0; ExprList *pOrderBy = p->pOrderBy; + int eDest = pDest->eDest; + int iParm = pDest->iParm; + + int regRow; + int regRowid; + iTab = pOrderBy->iECursor; if( eDest==SRT_Callback || eDest==SRT_Subroutine ){ pseudoTab = pParse->nTab++; - sqlite3VdbeAddOp(v, OP_OpenPseudo, pseudoTab, 0); - sqlite3VdbeAddOp(v, OP_SetNumColumns, pseudoTab, nColumn); - } - addr = 1 + sqlite3VdbeAddOp(v, OP_Sort, iTab, brk); - codeOffset(v, p, cont, 0); - if( eDest==SRT_Callback || eDest==SRT_Subroutine ){ - sqlite3VdbeAddOp(v, OP_Integer, 1, 0); - } - sqlite3VdbeAddOp(v, OP_Column, iTab, pOrderBy->nExpr + 1); + sqlite3VdbeAddOp2(v, OP_OpenPseudo, pseudoTab, 0); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, pseudoTab, nColumn); + } + addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, brk); + codeOffset(v, p, cont); + regRow = sqlite3GetTempReg(pParse); + regRowid = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow); switch( eDest ){ case SRT_Table: case SRT_EphemTab: { - sqlite3VdbeAddOp(v, OP_NewRowid, iParm, 0); - sqlite3VdbeAddOp(v, OP_Pull, 1, 0); - sqlite3VdbeAddOp(v, OP_Insert, iParm, OPFLAG_APPEND); + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); break; } #ifndef SQLITE_OMIT_SUBQUERY case SRT_Set: { + int j1; assert( nColumn==1 ); - sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3); - sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &p->affinity, 1); - sqlite3VdbeAddOp(v, OP_IdxInsert, (iParm&0x0000FFFF), 0); + j1 = sqlite3VdbeAddOp1(v, OP_IsNull, regRow); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid); + sqlite3VdbeJumpHere(v, j1); break; } case SRT_Mem: { assert( nColumn==1 ); - sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); + sqlite3VdbeAddOp2(v, OP_Move, regRow, iParm); /* The LIMIT clause will terminate the loop for us */ break; } @@ -59464,14 +62218,15 @@ static void generateSortTail( case SRT_Callback: case SRT_Subroutine: { int i; - sqlite3VdbeAddOp(v, OP_Insert, pseudoTab, 0); + sqlite3VdbeAddOp2(v, OP_Integer, 1, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, pseudoTab, regRow, regRowid); for(i=0; iiMem+i); } if( eDest==SRT_Callback ){ - sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn); }else{ - sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); + sqlite3VdbeAddOp2(v, OP_Gosub, 0, iParm); } break; } @@ -59480,21 +62235,23 @@ static void generateSortTail( break; } } + sqlite3ReleaseTempReg(pParse, regRow); + sqlite3ReleaseTempReg(pParse, regRowid); /* Jump to the end of the loop when the LIMIT is reached */ if( p->iLimit>=0 ){ - sqlite3VdbeAddOp(v, OP_MemIncr, -1, p->iLimit); - sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, brk); + sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1); + sqlite3VdbeAddOp2(v, OP_IfZero, p->iLimit, brk); } /* The bottom of the loop */ sqlite3VdbeResolveLabel(v, cont); - sqlite3VdbeAddOp(v, OP_Next, iTab, addr); + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); sqlite3VdbeResolveLabel(v, brk); if( eDest==SRT_Callback || eDest==SRT_Subroutine ){ - sqlite3VdbeAddOp(v, OP_Close, pseudoTab, 0); + sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0); } } @@ -59657,10 +62414,10 @@ static void generateColumnTypes( ** column specific strings, in case the schema is reset before this ** virtual machine is deleted. */ - sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P3_TRANSIENT); - sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P3_TRANSIENT); - sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P3_TRANSIENT); - sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P3_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P4_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P4_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P4_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P4_TRANSIENT); } } @@ -59724,7 +62481,7 @@ static void generateColumnNames( zTab = pTabList->a[j].zAlias; if( fullNames || zTab==0 ) zTab = pTab->zName; sqlite3SetString(&zName, zTab, ".", zCol, (char*)0); - sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P3_DYNAMIC); + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P4_DYNAMIC); }else{ sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol)); } @@ -59936,7 +62693,7 @@ static int prepSelectStmt(Parse *pParse, Select *p){ /* An ordinary table or view name in the FROM clause */ assert( pFrom->pTab==0 ); pFrom->pTab = pTab = - sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); + sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase); if( pTab==0 ){ return 1; } @@ -60224,7 +62981,7 @@ static int processOrderGroupBy( sqlite3 *db = pParse->db; ExprList *pEList; - if( pOrderBy==0 ) return 0; + if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){ const char *zType = isOrder ? "ORDER" : "GROUP"; sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); @@ -60254,7 +63011,7 @@ static int processOrderGroupBy( sqlite3ExprDelete(pE); pE = sqlite3ExprDup(db, pEList->a[iCol-1].pExpr); pOrderBy->a[i].pExpr = pE; - if( pColl && flags ){ + if( pE && pColl && flags ){ pE->pColl = pColl; pE->flags |= flags; } @@ -60306,15 +63063,15 @@ static int processCompoundOrderBy( while( pSelect && moreToDo ){ moreToDo = 0; for(i=0; inExpr; i++){ - int iCol; + int iCol = -1; Expr *pE, *pDup; if( pOrderBy->a[i].done ) continue; pE = pOrderBy->a[i].pExpr; pDup = sqlite3ExprDup(db, pE); - if( pDup==0 ){ - return 1; + if( !db->mallocFailed ){ + assert(pDup); + iCol = matchOrderByTermToExprList(pParse, pSelect, pDup, i+1, 1, 0); } - iCol = matchOrderByTermToExprList(pParse, pSelect, pDup, i+1, 1, 0); sqlite3ExprDelete(pDup); if( iCol<0 ){ return 1; @@ -60360,6 +63117,11 @@ SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){ Vdbe *v = pParse->pVdbe; if( v==0 ){ v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); +#ifndef SQLITE_OMIT_TRACE + if( v ){ + sqlite3VdbeAddOp0(v, OP_Trace); + } +#endif } return v; } @@ -60387,7 +63149,7 @@ static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ Vdbe *v = 0; int iLimit = 0; int iOffset; - int addr1, addr2; + int addr1; /* ** "LIMIT -1" always shows all rows. There is some @@ -60396,43 +63158,35 @@ static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ ** no rows. */ if( p->pLimit ){ - p->iLimit = iLimit = pParse->nMem; - pParse->nMem += 2; + p->iLimit = iLimit = ++pParse->nMem; v = sqlite3GetVdbe(pParse); if( v==0 ) return; - sqlite3ExprCode(pParse, p->pLimit); - sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); - sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 1); - VdbeComment((v, "# LIMIT counter")); - sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak); - sqlite3VdbeAddOp(v, OP_MemLoad, iLimit, 0); + sqlite3ExprCode(pParse, p->pLimit, iLimit); + sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); + VdbeComment((v, "LIMIT counter")); + sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); } if( p->pOffset ){ - p->iOffset = iOffset = pParse->nMem++; + p->iOffset = iOffset = ++pParse->nMem; + if( p->pLimit ){ + pParse->nMem++; /* Allocate an extra register for limit+offset */ + } v = sqlite3GetVdbe(pParse); if( v==0 ) return; - sqlite3ExprCode(pParse, p->pOffset); - sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); - sqlite3VdbeAddOp(v, OP_MemStore, iOffset, p->pLimit==0); - VdbeComment((v, "# OFFSET counter")); - addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iOffset, 0); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_Integer, 0, 0); + sqlite3ExprCode(pParse, p->pOffset, iOffset); + sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); + VdbeComment((v, "OFFSET counter")); + addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset); sqlite3VdbeJumpHere(v, addr1); if( p->pLimit ){ - sqlite3VdbeAddOp(v, OP_Add, 0, 0); + sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1); + VdbeComment((v, "LIMIT+OFFSET")); + addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); + sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1); + sqlite3VdbeJumpHere(v, addr1); } } - if( p->pLimit ){ - addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iLimit, 0); - sqlite3VdbeAddOp(v, OP_Pop, 1, 0); - sqlite3VdbeAddOp(v, OP_MemInt, -1, iLimit+1); - addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); - sqlite3VdbeJumpHere(v, addr1); - sqlite3VdbeAddOp(v, OP_MemStore, iLimit+1, 1); - VdbeComment((v, "# LIMIT+OFFSET")); - sqlite3VdbeJumpHere(v, addr2); - } } /* @@ -60443,7 +63197,7 @@ static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){ int addr; assert( pOrderBy->iECursor==0 ); pOrderBy->iECursor = pParse->nTab++; - addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenEphemeral, + addr = sqlite3VdbeAddOp2(pParse->pVdbe, OP_OpenEphemeral, pOrderBy->iECursor, pOrderBy->nExpr+1); assert( p->addrOpenEphm[2] == -1 ); p->addrOpenEphm[2] = addr; @@ -60507,8 +63261,7 @@ static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ static int multiSelect( Parse *pParse, /* Parsing context */ Select *p, /* The right-most of SELECTs to be coded */ - int eDest, /* \___ Store query results as specified */ - int iParm, /* / by these two parameters. */ + SelectDest *pDest, /* What to do with query results */ char *aff /* If eDest is SRT_Union, the affinity string */ ){ int rc = SQLITE_OK; /* Success code from a subroutine */ @@ -60518,6 +63271,9 @@ static int multiSelect( ExprList *pOrderBy; /* The ORDER BY clause on p */ int aSetP2[2]; /* Set P2 value of these op to number of columns */ int nSetP2 = 0; /* Number of slots in aSetP2[] used */ + SelectDest dest; /* Alternative data destination */ + + dest = *pDest; /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. @@ -60552,11 +63308,11 @@ static int multiSelect( /* Create the destination temporary table if necessary */ - if( eDest==SRT_EphemTab ){ + if( dest.eDest==SRT_EphemTab ){ assert( p->pEList ); assert( nSetP2pLimit ); pPrior->pLimit = p->pLimit; pPrior->pOffset = p->pOffset; - rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); + rc = sqlite3Select(pParse, pPrior, &dest, 0, 0, 0, aff); p->pLimit = 0; p->pOffset = 0; if( rc ){ @@ -60579,10 +63335,10 @@ static int multiSelect( p->iLimit = pPrior->iLimit; p->iOffset = pPrior->iOffset; if( p->iLimit>=0 ){ - addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0); - VdbeComment((v, "# Jump ahead if LIMIT reached")); + addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); + VdbeComment((v, "Jump ahead if LIMIT reached")); } - rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); + rc = sqlite3Select(pParse, p, &dest, 0, 0, 0, aff); p->pPrior = pPrior; if( rc ){ goto multi_select_end; @@ -60601,13 +63357,14 @@ static int multiSelect( int priorOp; /* The SRT_ operation to apply to prior selects */ Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ int addr; + SelectDest uniondest; priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; - if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){ + if( dest.eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){ /* We can reuse a temporary table generated by a SELECT to our ** right. */ - unionTab = iParm; + unionTab = dest.iParm; }else{ /* We will need to create our own temporary table to hold the ** intermediate results. @@ -60617,7 +63374,7 @@ static int multiSelect( rc = 1; goto multi_select_end; } - addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, unionTab, 0); + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); if( priorOp==SRT_Table ){ assert( nSetP2pOrderBy ); - rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); + sqlite3SelectDestInit(&uniondest, priorOp, unionTab); + rc = sqlite3Select(pParse, pPrior, &uniondest, 0, 0, 0, aff); if( rc ){ goto multi_select_end; } @@ -60652,7 +63410,8 @@ static int multiSelect( p->pLimit = 0; pOffset = p->pOffset; p->pOffset = 0; - rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); + uniondest.eDest = op; + rc = sqlite3Select(pParse, p, &uniondest, 0, 0, 0, aff); /* Query flattening in sqlite3Select() might refill p->pOrderBy. ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ sqlite3ExprListDelete(p->pOrderBy); @@ -60671,10 +63430,10 @@ static int multiSelect( /* Convert the data in the temporary table into whatever form ** it is that we currently need. */ - if( eDest!=priorOp || unionTab!=iParm ){ + if( dest.eDest!=priorOp || unionTab!=dest.iParm ){ int iCont, iBreak, iStart; assert( p->pEList ); - if( eDest==SRT_Callback ){ + if( dest.eDest==SRT_Callback ){ Select *pFirst = p; while( pFirst->pPrior ) pFirst = pFirst->pPrior; generateColumnNames(pParse, 0, pFirst->pEList); @@ -60682,19 +63441,14 @@ static int multiSelect( iBreak = sqlite3VdbeMakeLabel(v); iCont = sqlite3VdbeMakeLabel(v); computeLimitRegisters(pParse, p, iBreak); - sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); iStart = sqlite3VdbeCurrentAddr(v); - rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, - pOrderBy, -1, eDest, iParm, - iCont, iBreak, 0); - if( rc ){ - rc = 1; - goto multi_select_end; - } + selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, + pOrderBy, -1, &dest, iCont, iBreak, 0); sqlite3VdbeResolveLabel(v, iCont); - sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); sqlite3VdbeResolveLabel(v, iBreak); - sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); } break; } @@ -60703,6 +63457,8 @@ static int multiSelect( int iCont, iBreak, iStart; Expr *pLimit, *pOffset; int addr; + SelectDest intersectdest; + int r1; /* INTERSECT is different from the others since it requires ** two temporary tables. Hence it has its own case. Begin @@ -60716,7 +63472,7 @@ static int multiSelect( } createSortingIndex(pParse, p, pOrderBy); - addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab1, 0); + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); assert( p->addrOpenEphm[0] == -1 ); p->addrOpenEphm[0] = addr; p->pRightmost->usesEphm = 1; @@ -60724,14 +63480,15 @@ static int multiSelect( /* Code the SELECTs to our left into temporary table "tab1". */ - rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); + rc = sqlite3Select(pParse, pPrior, &intersectdest, 0, 0, 0, aff); if( rc ){ goto multi_select_end; } /* Code the current SELECT into temporary table "tab2" */ - addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab2, 0); + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); assert( p->addrOpenEphm[1] == -1 ); p->addrOpenEphm[1] = addr; p->pPrior = 0; @@ -60739,7 +63496,8 @@ static int multiSelect( p->pLimit = 0; pOffset = p->pOffset; p->pOffset = 0; - rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); + intersectdest.iParm = tab2; + rc = sqlite3Select(pParse, p, &intersectdest, 0, 0, 0, aff); p->pPrior = pPrior; sqlite3ExprDelete(p->pLimit); p->pLimit = pLimit; @@ -60752,7 +63510,7 @@ static int multiSelect( ** tables. */ assert( p->pEList ); - if( eDest==SRT_Callback ){ + if( dest.eDest==SRT_Callback ){ Select *pFirst = p; while( pFirst->pPrior ) pFirst = pFirst->pPrior; generateColumnNames(pParse, 0, pFirst->pEList); @@ -60760,21 +63518,18 @@ static int multiSelect( iBreak = sqlite3VdbeMakeLabel(v); iCont = sqlite3VdbeMakeLabel(v); computeLimitRegisters(pParse, p, iBreak); - sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); - iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0); - sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); - rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, - pOrderBy, -1, eDest, iParm, - iCont, iBreak, 0); - if( rc ){ - rc = 1; - goto multi_select_end; - } + sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); + r1 = sqlite3GetTempReg(pParse); + iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1); + sqlite3VdbeAddOp3(v, OP_NotFound, tab2, iCont, r1); + sqlite3ReleaseTempReg(pParse, r1); + selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, + pOrderBy, -1, &dest, iCont, iBreak, 0); sqlite3VdbeResolveLabel(v, iCont); - sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); sqlite3VdbeResolveLabel(v, iBreak); - sqlite3VdbeAddOp(v, OP_Close, tab2, 0); - sqlite3VdbeAddOp(v, OP_Close, tab1, 0); + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); break; } } @@ -60844,7 +63599,7 @@ static int multiSelect( break; } sqlite3VdbeChangeP2(v, addr, nCol); - sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO); + sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO); pLoop->addrOpenEphm[i] = -1; } } @@ -60888,15 +63643,16 @@ static int multiSelect( addr = p->addrOpenEphm[2]; sqlite3VdbeChangeP2(v, addr, p->pOrderBy->nExpr+2); pKeyInfo->nField = nOrderByExpr; - sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); + sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO_HANDOFF); pKeyInfo = 0; - generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); + generateSortTail(pParse, p, v, p->pEList->nExpr, &dest); } sqlite3_free(pKeyInfo); } multi_select_end: + pDest->iMem = dest.iMem; return rc; } #endif /* SQLITE_OMIT_COMPOUND_SELECT */ @@ -61054,6 +63810,10 @@ static void substSelect( ** subquery does not have both an ORDER BY and a LIMIT clause. ** (See ticket #2339) ** +** (16) The outer query is not an aggregate or the subquery does +** not contain ORDER BY. (Ticket #2942) This used to not matter +** until we introduced the group_concat() function. +** ** In this routine, the "p" parameter is a pointer to the outer query. ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. @@ -61111,6 +63871,7 @@ static int flattenSubquery( if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){ return 0; /* Restriction (11) */ } + if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */ /* Restriction 3: If the subquery is a join, make sure the subquery is ** not used as the right operand of an outer join. Examples of why this @@ -61267,161 +64028,32 @@ static int flattenSubquery( #endif /* SQLITE_OMIT_VIEW */ /* -** Analyze the SELECT statement passed in as an argument to see if it -** is a simple min() or max() query. If it is and this query can be -** satisfied using a single seek to the beginning or end of an index, -** then generate the code for this SELECT and return 1. If this is not a -** simple min() or max() query, then return 0; +** Analyze the SELECT statement passed as an argument to see if it +** is a min() or max() query. Return ORDERBY_MIN or ORDERBY_MAX if +** it is, or 0 otherwise. At present, a query is considered to be +** a min()/max() query if: ** -** A simply min() or max() query looks like this: +** 1. There is a single object in the FROM clause. ** -** SELECT min(a) FROM table; -** SELECT max(a) FROM table; -** -** The query may have only a single table in its FROM argument. There -** can be no GROUP BY or HAVING or WHERE clauses. The result set must -** be the min() or max() of a single column of the table. The column -** in the min() or max() function must be indexed. -** -** The parameters to this routine are the same as for sqlite3Select(). -** See the header comment on that routine for additional information. +** 2. There is a single expression in the result set, and it is +** either min(x) or max(x), where x is a column reference. */ -static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ +static int minMaxQuery(Parse *pParse, Select *p){ Expr *pExpr; - int iCol; - Table *pTab; - Index *pIdx; - int base; - Vdbe *v; - int seekOp; - ExprList *pEList, *pList, eList; - struct ExprList_item eListItem; - SrcList *pSrc; - int brk; - int iDb; + ExprList *pEList = p->pEList; - /* Check to see if this query is a simple min() or max() query. Return - ** zero if it is not. - */ - if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; - pSrc = p->pSrc; - if( pSrc->nSrc!=1 ) return 0; - pEList = p->pEList; - if( pEList->nExpr!=1 ) return 0; + if( pEList->nExpr!=1 ) return ORDERBY_NORMAL; pExpr = pEList->a[0].pExpr; - if( pExpr->op!=TK_AGG_FUNCTION ) return 0; - pList = pExpr->pList; - if( pList==0 || pList->nExpr!=1 ) return 0; - if( pExpr->token.n!=3 ) return 0; + pEList = pExpr->pList; + if( pExpr->op!=TK_AGG_FUNCTION || pEList==0 || pEList->nExpr!=1 ) return 0; + if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return ORDERBY_NORMAL; + if( pExpr->token.n!=3 ) return ORDERBY_NORMAL; if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){ - seekOp = OP_Rewind; + return ORDERBY_MIN; }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){ - seekOp = OP_Last; - }else{ - return 0; - } - pExpr = pList->a[0].pExpr; - if( pExpr->op!=TK_COLUMN ) return 0; - iCol = pExpr->iColumn; - pTab = pSrc->a[0].pTab; - - /* This optimization cannot be used with virtual tables. */ - if( IsVirtual(pTab) ) return 0; - - /* If we get to here, it means the query is of the correct form. - ** Check to make sure we have an index and make pIdx point to the - ** appropriate index. If the min() or max() is on an INTEGER PRIMARY - ** key column, no index is necessary so set pIdx to NULL. If no - ** usable index is found, return 0. - */ - if( iCol<0 ){ - pIdx = 0; - }else{ - CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); - if( pColl==0 ) return 0; - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - assert( pIdx->nColumn>=1 ); - if( pIdx->aiColumn[0]==iCol && - 0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){ - break; - } - } - if( pIdx==0 ) return 0; - } - - /* Identify column types if we will be using the callback. This - ** step is skipped if the output is going to a table or a memory cell. - ** The column names have already been generated in the calling function. - */ - v = sqlite3GetVdbe(pParse); - if( v==0 ) return 0; - - /* If the output is destined for a temporary table, open that table. - */ - if( eDest==SRT_EphemTab ){ - sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 1); - } - - /* Generating code to find the min or the max. Basically all we have - ** to do is find the first or the last entry in the chosen index. If - ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first - ** or last entry in the main table. - */ - iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); - assert( iDb>=0 || pTab->isEphem ); - sqlite3CodeVerifySchema(pParse, iDb); - sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); - base = pSrc->a[0].iCursor; - brk = sqlite3VdbeMakeLabel(v); - computeLimitRegisters(pParse, p, brk); - if( pSrc->a[0].pSelect==0 ){ - sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead); - } - if( pIdx==0 ){ - sqlite3VdbeAddOp(v, seekOp, base, 0); - }else{ - /* Even though the cursor used to open the index here is closed - ** as soon as a single value has been read from it, allocate it - ** using (pParse->nTab++) to prevent the cursor id from being - ** reused. This is important for statements of the form - ** "INSERT INTO x SELECT max() FROM x". - */ - int iIdx; - KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); - iIdx = pParse->nTab++; - assert( pIdx->pSchema==pTab->pSchema ); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, - (char*)pKey, P3_KEYINFO_HANDOFF); - if( seekOp==OP_Rewind ){ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); - sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); - seekOp = OP_MoveGt; - } - if( pIdx->aSortOrder[0]==SQLITE_SO_DESC ){ - /* Ticket #2514: invert the seek operator if we are using - ** a descending index. */ - if( seekOp==OP_Last ){ - seekOp = OP_Rewind; - }else{ - assert( seekOp==OP_MoveGt ); - seekOp = OP_MoveLt; - } - } - sqlite3VdbeAddOp(v, seekOp, iIdx, 0); - sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0); - sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); - sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); + return ORDERBY_MAX; } - eList.nExpr = 1; - memset(&eListItem, 0, sizeof(eListItem)); - eList.a = &eListItem; - eList.a[0].pExpr = pExpr; - selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, brk, brk, 0); - sqlite3VdbeResolveLabel(v, brk); - sqlite3VdbeAddOp(v, OP_Close, base, 0); - - return 1; + return ORDERBY_NORMAL; } /* @@ -61569,10 +64201,10 @@ static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ return; } for(i=0; inColumn; i++){ - sqlite3VdbeAddOp(v, OP_MemNull, pAggInfo->aCol[i].iMem, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem); } for(pFunc=pAggInfo->aFunc, i=0; inFunc; i++, pFunc++){ - sqlite3VdbeAddOp(v, OP_MemNull, pFunc->iMem, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem); if( pFunc->iDistinct>=0 ){ Expr *pE = pFunc->pExpr; if( pE->pList==0 || pE->pList->nExpr!=1 ){ @@ -61581,8 +64213,8 @@ static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ pFunc->iDistinct = -1; }else{ KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->pList); - sqlite3VdbeOp3(v, OP_OpenEphemeral, pFunc->iDistinct, 0, - (char*)pKeyInfo, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); } } } @@ -61598,8 +64230,8 @@ static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ struct AggInfo_func *pF; for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ ExprList *pList = pF->pExpr->pList; - sqlite3VdbeOp3(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, - (void*)pF->pFunc, P3_FUNCDEF); + sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0, + (void*)pF->pFunc, P4_FUNCDEF); } } @@ -61617,17 +64249,20 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ int nArg; int addrNext = 0; + int regAgg; ExprList *pList = pF->pExpr->pList; if( pList ){ nArg = pList->nExpr; - sqlite3ExprCodeExprList(pParse, pList); + regAgg = sqlite3GetTempRange(pParse, nArg); + sqlite3ExprCodeExprList(pParse, pList, regAgg); }else{ nArg = 0; + regAgg = 0; } if( pF->iDistinct>=0 ){ addrNext = sqlite3VdbeMakeLabel(v); assert( nArg==1 ); - codeDistinct(v, pF->iDistinct, addrNext, 1); + codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg); } if( pF->pFunc->needCollSeq ){ CollSeq *pColl = 0; @@ -61640,44 +64275,86 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ if( !pColl ){ pColl = pParse->db->pDfltColl; } - sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); + sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); } - sqlite3VdbeOp3(v, OP_AggStep, pF->iMem, nArg, (void*)pF->pFunc, P3_FUNCDEF); + sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem, + (void*)pF->pFunc, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, nArg); + sqlite3ReleaseTempRange(pParse, regAgg, nArg); if( addrNext ){ sqlite3VdbeResolveLabel(v, addrNext); } } for(i=0, pC=pAggInfo->aCol; inAccumulator; i++, pC++){ - sqlite3ExprCode(pParse, pC->pExpr); - sqlite3VdbeAddOp(v, OP_MemStore, pC->iMem, 1); + sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); } pAggInfo->directMode = 0; } +#ifndef SQLITE_OMIT_TRIGGER +/* +** This function is used when a SELECT statement is used to create a +** temporary table for iterating through when running an INSTEAD OF +** UPDATE or INSTEAD OF DELETE trigger. +** +** If possible, the SELECT statement is modified so that NULL values +** are stored in the temporary table for all columns for which the +** corresponding bit in argument mask is not set. If mask takes the +** special value 0xffffffff, then all columns are populated. +*/ +SQLITE_PRIVATE void sqlite3SelectMask(Parse *pParse, Select *p, u32 mask){ + if( p && !p->pPrior && !p->isDistinct && mask!=0xffffffff ){ + ExprList *pEList; + int i; + sqlite3SelectResolve(pParse, p, 0); + pEList = p->pEList; + for(i=0; pEList && inExpr && i<32; i++){ + if( !(mask&((u32)1<a[i].pExpr); + pEList->a[i].pExpr = sqlite3Expr(pParse->db, TK_NULL, 0, 0, 0); + } + } + } +} +#endif /* ** Generate code for the given SELECT statement. ** ** The results are distributed in various ways depending on the -** value of eDest and iParm. +** contents of the SelectDest structure pointed to by argument pDest +** as follows: ** -** eDest Value Result +** pDest->eDest Result ** ------------ ------------------------------------------- ** SRT_Callback Invoke the callback for each row of the result. ** -** SRT_Mem Store first result in memory cell iParm +** SRT_Mem Store first result in memory cell pDest->iParm +** +** SRT_Set Store non-null results as keys of table pDest->iParm. +** Apply the affinity pDest->affinity before storing them. +** +** SRT_Union Store results as a key in a temporary table pDest->iParm. +** +** SRT_Except Remove results from the temporary table pDest->iParm. +** +** SRT_Table Store results in temporary table pDest->iParm ** -** SRT_Set Store results as keys of table iParm. +** SRT_EphemTab Create an temporary table pDest->iParm and store +** the result there. The cursor is left open after +** returning. ** -** SRT_Union Store results as a key in a temporary table iParm +** SRT_Subroutine For each row returned, push the results onto the +** vdbe stack and call the subroutine (via OP_Gosub) +** at address pDest->iParm. ** -** SRT_Except Remove results from the temporary table iParm. +** SRT_Exists Store a 1 in memory cell pDest->iParm if the result +** set is not empty. ** -** SRT_Table Store results in temporary table iParm +** SRT_Discard Throw the results away. ** -** The table above is incomplete. Additional eDist value have be added -** since this comment was written. See the selectInnerLoop() function for -** a complete listing of the allowed values of eDest and their meanings. +** See the selectInnerLoop() function for a canonical listing of the +** allowed values of eDest and their meanings. ** ** This routine returns the number of errors. If any errors are ** encountered, then an appropriate error message is left in @@ -61710,8 +64387,7 @@ static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ SQLITE_PRIVATE int sqlite3Select( Parse *pParse, /* The parser context */ Select *p, /* The SELECT statement being coded. */ - int eDest, /* How to dispose of the results */ - int iParm, /* A parameter used by the eDest disposal method */ + SelectDest *pDest, /* What to do with the query results */ Select *pParent, /* Another SELECT for which this is a sub-query */ int parentTab, /* Index in pParent->pSrc of this query */ int *pParentAgg, /* True if pParent uses aggregate functions */ @@ -61743,8 +64419,15 @@ SQLITE_PRIVATE int sqlite3Select( memset(&sAggInfo, 0, sizeof(sAggInfo)); pOrderBy = p->pOrderBy; - if( IgnorableOrderby(eDest) ){ + if( IgnorableOrderby(pDest) ){ p->pOrderBy = 0; + + /* In these cases the DISTINCT operator makes no difference to the + ** results, so remove it if it were specified. + */ + assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || + pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard); + p->isDistinct = 0; } if( sqlite3SelectResolve(pParse, p, 0) ){ goto select_end; @@ -61768,7 +64451,7 @@ SQLITE_PRIVATE int sqlite3Select( return 1; } } - return multiSelect(pParse, p, eDest, iParm, aff); + return multiSelect(pParse, p, pDest, aff); } #endif @@ -61793,14 +64476,14 @@ SQLITE_PRIVATE int sqlite3Select( ** only a single column may be output. */ #ifndef SQLITE_OMIT_SUBQUERY - if( checkForMultiColumnSelectError(pParse, eDest, pEList->nExpr) ){ + if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ goto select_end; } #endif /* ORDER BY is ignored for some destinations. */ - if( IgnorableOrderby(eDest) ){ + if( IgnorableOrderby(pDest) ){ pOrderBy = 0; } @@ -61816,6 +64499,7 @@ SQLITE_PRIVATE int sqlite3Select( const char *zSavedAuthContext = 0; int needRestoreContext; struct SrcList_item *pItem = &pTabList->a[i]; + SelectDest dest; if( pItem->pSelect==0 || pItem->isPopulated ) continue; if( pItem->zName!=0 ){ @@ -61835,8 +64519,8 @@ SQLITE_PRIVATE int sqlite3Select( */ pParse->nHeight += sqlite3SelectExprHeight(p); #endif - sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab, - pItem->iCursor, p, i, &isAgg, 0); + sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); + sqlite3Select(pParse, pItem->pSelect, &dest, p, i, &isAgg, 0); if( db->mallocFailed ){ goto select_end; } @@ -61848,7 +64532,7 @@ SQLITE_PRIVATE int sqlite3Select( } pTabList = p->pSrc; pWhere = p->pWhere; - if( !IgnorableOrderby(eDest) ){ + if( !IgnorableOrderby(pDest) ){ pOrderBy = p->pOrderBy; } pGroupBy = p->pGroupBy; @@ -61857,14 +64541,6 @@ SQLITE_PRIVATE int sqlite3Select( } #endif - /* Check for the special case of a min() or max() function by itself - ** in the result set. - */ - if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ - rc = 0; - goto select_end; - } - /* Check to see if this is a subquery that can be "flattened" into its parent. ** If flattening is a possiblity, do so and return immediately. */ @@ -61895,21 +64571,20 @@ SQLITE_PRIVATE int sqlite3Select( */ if( pOrderBy ){ KeyInfo *pKeyInfo; - if( pParse->nErr ){ - goto select_end; - } pKeyInfo = keyInfoFromExprList(pParse, pOrderBy); pOrderBy->iECursor = pParse->nTab++; p->addrOpenEphm[2] = addrSortIndex = - sqlite3VdbeOp3(v, OP_OpenEphemeral, pOrderBy->iECursor, pOrderBy->nExpr+2, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + pOrderBy->iECursor, pOrderBy->nExpr+2, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); }else{ addrSortIndex = -1; } /* If the output is destined for a temporary table, open that table. */ - if( eDest==SRT_EphemTab ){ - sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, pEList->nExpr); + if( pDest->eDest==SRT_EphemTab ){ + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr); } /* Set the limiter. @@ -61924,8 +64599,8 @@ SQLITE_PRIVATE int sqlite3Select( assert( isAgg || pGroupBy ); distinct = pParse->nTab++; pKeyInfo = keyInfoFromExprList(pParse, p->pEList); - sqlite3VdbeOp3(v, OP_OpenEphemeral, distinct, 0, - (char*)pKeyInfo, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); }else{ distinct = -1; } @@ -61935,7 +64610,7 @@ SQLITE_PRIVATE int sqlite3Select( /* This case is for non-aggregate queries ** Begin the database scan */ - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, 0); if( pWInfo==0 ) goto select_end; /* If sorting index that was created by a prior OP_OpenEphemeral @@ -61950,10 +64625,8 @@ SQLITE_PRIVATE int sqlite3Select( /* Use the standard inner loop */ assert(!isDistinct); - if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, eDest, - iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ - goto select_end; - } + selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, pDest, + pWInfo->iContinue, pWInfo->iBreak, aff); /* End the database scan loop. */ @@ -61994,20 +64667,14 @@ SQLITE_PRIVATE int sqlite3Select( sNC.pAggInfo = &sAggInfo; sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0; sAggInfo.pGroupBy = pGroupBy; - if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){ - goto select_end; - } - if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){ - goto select_end; - } - if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ - goto select_end; + sqlite3ExprAnalyzeAggList(&sNC, pEList); + sqlite3ExprAnalyzeAggList(&sNC, pOrderBy); + if( pHaving ){ + sqlite3ExprAnalyzeAggregates(&sNC, pHaving); } sAggInfo.nAccumulator = sAggInfo.nColumn; for(i=0; ipList) ){ - goto select_end; - } + sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList); } if( db->mallocFailed ) goto select_end; @@ -62032,23 +64699,23 @@ SQLITE_PRIVATE int sqlite3Select( sAggInfo.sortingIdx = pParse->nTab++; pKeyInfo = keyInfoFromExprList(pParse, pGroupBy); addrSortingIdx = - sqlite3VdbeOp3(v, OP_OpenEphemeral, sAggInfo.sortingIdx, - sAggInfo.nSortingColumn, - (char*)pKeyInfo, P3_KEYINFO_HANDOFF); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, sAggInfo.sortingIdx, + sAggInfo.nSortingColumn, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); /* Initialize memory locations used by GROUP BY aggregate processing */ - iUseFlag = pParse->nMem++; - iAbortFlag = pParse->nMem++; - iAMem = pParse->nMem; + iUseFlag = ++pParse->nMem; + iAbortFlag = ++pParse->nMem; + iAMem = pParse->nMem + 1; pParse->nMem += pGroupBy->nExpr; - iBMem = pParse->nMem; + iBMem = pParse->nMem + 1; pParse->nMem += pGroupBy->nExpr; - sqlite3VdbeAddOp(v, OP_MemInt, 0, iAbortFlag); - VdbeComment((v, "# clear abort flag")); - sqlite3VdbeAddOp(v, OP_MemInt, 0, iUseFlag); - VdbeComment((v, "# indicate accumulator empty")); - sqlite3VdbeAddOp(v, OP_Goto, 0, addrInitializeLoop); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag); + VdbeComment((v, "clear abort flag")); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); + VdbeComment((v, "indicate accumulator empty")); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrInitializeLoop); /* Generate a subroutine that outputs a single row of the result ** set. This subroutine first looks at the iUseFlag. If iUseFlag @@ -62058,31 +64725,28 @@ SQLITE_PRIVATE int sqlite3Select( ** order to signal the caller to abort. */ addrSetAbort = sqlite3VdbeCurrentAddr(v); - sqlite3VdbeAddOp(v, OP_MemInt, 1, iAbortFlag); - VdbeComment((v, "# set abort flag")); - sqlite3VdbeAddOp(v, OP_Return, 0, 0); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag); + VdbeComment((v, "set abort flag")); + sqlite3VdbeAddOp2(v, OP_Return, 0, 0); addrOutputRow = sqlite3VdbeCurrentAddr(v); - sqlite3VdbeAddOp(v, OP_IfMemPos, iUseFlag, addrOutputRow+2); - VdbeComment((v, "# Groupby result generator entry point")); - sqlite3VdbeAddOp(v, OP_Return, 0, 0); + sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); + VdbeComment((v, "Groupby result generator entry point")); + sqlite3VdbeAddOp2(v, OP_Return, 0, 0); finalizeAggFunctions(pParse, &sAggInfo); if( pHaving ){ - sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, 1); + sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL); } - rc = selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy, - distinct, eDest, iParm, - addrOutputRow+1, addrSetAbort, aff); - if( rc ){ - goto select_end; - } - sqlite3VdbeAddOp(v, OP_Return, 0, 0); - VdbeComment((v, "# end groupby result generator")); + selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy, + distinct, pDest, + addrOutputRow+1, addrSetAbort, aff); + sqlite3VdbeAddOp2(v, OP_Return, 0, 0); + VdbeComment((v, "end groupby result generator")); /* Generate a subroutine that will reset the group-by accumulator */ addrReset = sqlite3VdbeCurrentAddr(v); resetAccumulator(pParse, &sAggInfo); - sqlite3VdbeAddOp(v, OP_Return, 0, 0); + sqlite3VdbeAddOp2(v, OP_Return, 0, 0); /* Begin a loop that will extract all source rows in GROUP BY order. ** This might involve two separate loops with an OP_Sort in between, or @@ -62090,8 +64754,8 @@ SQLITE_PRIVATE int sqlite3Select( ** in the right order to begin with. */ sqlite3VdbeResolveLabel(v, addrInitializeLoop); - sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset); - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy); + sqlite3VdbeAddOp2(v, OP_Gosub, 0, addrReset); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0); if( pWInfo==0 ) goto select_end; if( pGroupBy==0 ){ /* The optimizer is able to deliver rows in group by order so @@ -62106,21 +64770,41 @@ SQLITE_PRIVATE int sqlite3Select( ** then loop over the sorting index in order to get the output ** in sorted order */ + int regBase; + int regRecord; + int nCol; + int nGroupBy; + groupBySort = 1; - sqlite3ExprCodeExprList(pParse, pGroupBy); - sqlite3VdbeAddOp(v, OP_Sequence, sAggInfo.sortingIdx, 0); - j = pGroupBy->nExpr+1; + nGroupBy = pGroupBy->nExpr; + nCol = nGroupBy + 1; + j = nGroupBy+1; + for(i=0; i=j ){ + nCol++; + j++; + } + } + regBase = sqlite3GetTempRange(pParse, nCol); + sqlite3ExprCodeExprList(pParse, pGroupBy, regBase); + sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy); + j = nGroupBy+1; for(i=0; iiSorterColumnpTab, pCol->iColumn, pCol->iTable); - j++; + if( pCol->iSorterColumn>=j ){ + sqlite3ExprCodeGetColumn(v, pCol->pTab, pCol->iColumn, pCol->iTable, + j + regBase); + j++; + } } - sqlite3VdbeAddOp(v, OP_MakeRecord, j, 0); - sqlite3VdbeAddOp(v, OP_IdxInsert, sAggInfo.sortingIdx, 0); + regRecord = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); + sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3ReleaseTempRange(pParse, regBase, nCol); sqlite3WhereEnd(pWInfo); - sqlite3VdbeAddOp(v, OP_Sort, sAggInfo.sortingIdx, addrEnd); - VdbeComment((v, "# GROUP BY sort")); + sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd); + VdbeComment((v, "GROUP BY sort")); sAggInfo.useSortingIdx = 1; } @@ -62132,24 +64816,20 @@ SQLITE_PRIVATE int sqlite3Select( addrTopOfLoop = sqlite3VdbeCurrentAddr(v); for(j=0; jnExpr; j++){ if( groupBySort ){ - sqlite3VdbeAddOp(v, OP_Column, sAggInfo.sortingIdx, j); + sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j); }else{ sAggInfo.directMode = 1; - sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr); + sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); } - sqlite3VdbeAddOp(v, OP_MemStore, iBMem+j, jnExpr-1); } for(j=pGroupBy->nExpr-1; j>=0; j--){ - if( jnExpr-1 ){ - sqlite3VdbeAddOp(v, OP_MemLoad, iBMem+j, 0); - } - sqlite3VdbeAddOp(v, OP_MemLoad, iAMem+j, 0); if( j==0 ){ - sqlite3VdbeAddOp(v, OP_Eq, 0x200, addrProcessRow); + sqlite3VdbeAddOp3(v, OP_Eq, iAMem+j, addrProcessRow, iBMem+j); }else{ - sqlite3VdbeAddOp(v, OP_Ne, 0x200, addrGroupByChange); + sqlite3VdbeAddOp3(v, OP_Ne, iAMem+j, addrGroupByChange, iBMem+j); } - sqlite3VdbeChangeP3(v, -1, (void*)pKeyInfo->aColl[j], P3_COLLSEQ); + sqlite3VdbeChangeP4(v, -1, (void*)pKeyInfo->aColl[j], P4_COLLSEQ); + sqlite3VdbeChangeP5(v, SQLITE_NULLEQUAL); } /* Generate code that runs whenever the GROUP BY changes. @@ -62163,27 +64843,27 @@ SQLITE_PRIVATE int sqlite3Select( */ sqlite3VdbeResolveLabel(v, addrGroupByChange); for(j=0; jnExpr; j++){ - sqlite3VdbeAddOp(v, OP_MemMove, iAMem+j, iBMem+j); + sqlite3VdbeAddOp2(v, OP_Move, iBMem+j, iAMem+j); } - sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow); - VdbeComment((v, "# output one row")); - sqlite3VdbeAddOp(v, OP_IfMemPos, iAbortFlag, addrEnd); - VdbeComment((v, "# check abort flag")); - sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset); - VdbeComment((v, "# reset accumulator")); + sqlite3VdbeAddOp2(v, OP_Gosub, 0, addrOutputRow); + VdbeComment((v, "output one row")); + sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); + VdbeComment((v, "check abort flag")); + sqlite3VdbeAddOp2(v, OP_Gosub, 0, addrReset); + VdbeComment((v, "reset accumulator")); /* Update the aggregate accumulators based on the content of ** the current row */ sqlite3VdbeResolveLabel(v, addrProcessRow); updateAccumulator(pParse, &sAggInfo); - sqlite3VdbeAddOp(v, OP_MemInt, 1, iUseFlag); - VdbeComment((v, "# indicate data in accumulator")); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag); + VdbeComment((v, "indicate data in accumulator")); /* End of the loop */ if( groupBySort ){ - sqlite3VdbeAddOp(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop); + sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop); }else{ sqlite3WhereEnd(pWInfo); sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1); @@ -62191,27 +64871,74 @@ SQLITE_PRIVATE int sqlite3Select( /* Output the final row of result */ - sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow); - VdbeComment((v, "# output final row")); + sqlite3VdbeAddOp2(v, OP_Gosub, 0, addrOutputRow); + VdbeComment((v, "output final row")); } /* endif pGroupBy */ else { + ExprList *pMinMax = 0; + ExprList *pDel = 0; + u8 flag; + + /* Check if the query is of one of the following forms: + ** + ** SELECT min(x) FROM ... + ** SELECT max(x) FROM ... + ** + ** If it is, then ask the code in where.c to attempt to sort results + ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. + ** If where.c is able to produce results sorted in this order, then + ** add vdbe code to break out of the processing loop after the + ** first iteration (since the first iteration of the loop is + ** guaranteed to operate on the row with the minimum or maximum + ** value of x, the only row required). + ** + ** A special flag must be passed to sqlite3WhereBegin() to slightly + ** modify behaviour as follows: + ** + ** + If the query is a "SELECT min(x)", then the loop coded by + ** where.c should not iterate over any values with a NULL value + ** for x. + ** + ** + The optimizer code in where.c (the thing that decides which + ** index or indices to use) should place a different priority on + ** satisfying the 'ORDER BY' clause than it does in other cases. + ** Refer to code and comments in where.c for details. + */ + flag = minMaxQuery(pParse, p); + if( flag ){ + pDel = pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->pList); + if( pMinMax && !db->mallocFailed ){ + pMinMax->a[0].sortOrder = ((flag==ORDERBY_MIN)?0:1); + pMinMax->a[0].pExpr->op = TK_COLUMN; + } + } + /* This case runs if the aggregate has no GROUP BY clause. The ** processing is much simpler since there is only a single row ** of output. */ resetAccumulator(pParse, &sAggInfo); - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); - if( pWInfo==0 ) goto select_end; + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, flag); + if( pWInfo==0 ){ + sqlite3ExprListDelete(pDel); + goto select_end; + } updateAccumulator(pParse, &sAggInfo); + if( !pMinMax && flag ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak); + VdbeComment((v, "%s() by index", (flag==ORDERBY_MIN?"min":"max"))); + } sqlite3WhereEnd(pWInfo); finalizeAggFunctions(pParse, &sAggInfo); pOrderBy = 0; if( pHaving ){ - sqlite3ExprIfFalse(pParse, pHaving, addrEnd, 1); + sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL); } selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, - eDest, iParm, addrEnd, addrEnd, aff); + pDest, addrEnd, addrEnd, aff); + + sqlite3ExprListDelete(pDel); } sqlite3VdbeResolveLabel(v, addrEnd); @@ -62221,7 +64948,7 @@ SQLITE_PRIVATE int sqlite3Select( ** and send them to the callback one by one. */ if( pOrderBy ){ - generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); + generateSortTail(pParse, p, v, pEList->nExpr, pDest); } #ifndef SQLITE_OMIT_SUBQUERY @@ -62254,7 +64981,7 @@ select_end: /* Identify column names if we will be using them in a callback. This ** step is skipped if the output is going to some other destination. */ - if( rc==SQLITE_OK && eDest==SRT_Callback ){ + if( rc==SQLITE_OK && pDest->eDest==SRT_Callback ){ generateColumnNames(pParse, pTabList, pEList); } @@ -62278,7 +65005,7 @@ select_end: ** code base. Then are intended to be called from within the debugger ** or from temporary "printf" statements inserted for debugging. */ -SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){ +static void sqlite3PrintExpr(Expr *p){ if( p->token.z && p->token.n>0 ){ sqlite3DebugPrintf("(%.*s", p->token.n, p->token.z); }else{ @@ -62294,7 +65021,7 @@ SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){ } sqlite3DebugPrintf(")"); } -SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){ +static void sqlite3PrintExprList(ExprList *pList){ int i; for(i=0; inExpr; i++){ sqlite3PrintExpr(pList->a[i].pExpr); @@ -62303,7 +65030,7 @@ SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){ } } } -SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){ +static void sqlite3PrintSelect(Select *p, int indent){ sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p); sqlite3PrintExprList(p->pEList); sqlite3DebugPrintf("\n"); @@ -62430,17 +65157,15 @@ static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ if( p->nRow==0 ){ p->nColumn = nCol; for(i=0; iazResult[p->nData++] = z; } }else if( p->nColumn!=nCol ){ - sqlite3SetString(&p->zErrMsg, - "sqlite3_get_table() called with two or more incompatible queries", - (char*)0); + sqlite3_free(p->zErrMsg); + p->zErrMsg = sqlite3_mprintf( + "sqlite3_get_table() called with two or more incompatible queries" + ); p->rc = SQLITE_ERROR; return 1; } @@ -62488,7 +65213,7 @@ SQLITE_API int sqlite3_get_table( ){ int rc; TabResult res; - if( pazResult==0 ){ return SQLITE_ERROR; } + *pazResult = 0; if( pnColumn ) *pnColumn = 0; if( pnRow ) *pnRow = 0; @@ -62499,19 +65224,15 @@ SQLITE_API int sqlite3_get_table( res.nData = 1; res.nAlloc = 20; res.rc = SQLITE_OK; - res.azResult = sqlite3_malloc( sizeof(char*)*res.nAlloc ); - if( res.azResult==0 ) return SQLITE_NOMEM; + res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc ); + if( res.azResult==0 ){ + db->errCode = SQLITE_NOMEM; + return SQLITE_NOMEM; + } res.azResult[0] = 0; rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); -#ifndef NDEBUG - sqlite3_mutex_enter(db->mutex); - assert((rc&db->errMask)==rc && (res.rc&db->errMask)==res.rc); - sqlite3_mutex_leave(db->mutex); -#endif - if( res.azResult ){ - assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); - res.azResult[0] = (char*)res.nData; - } + assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); + res.azResult[0] = (char*)(sqlite3_intptr_t)res.nData; if( (rc&0xff)==SQLITE_ABORT ){ sqlite3_free_table(&res.azResult[1]); if( res.zErrMsg ){ @@ -62521,9 +65242,7 @@ SQLITE_API int sqlite3_get_table( } sqlite3_free(res.zErrMsg); } - sqlite3_mutex_enter(db->mutex); - db->errCode = res.rc; - sqlite3_mutex_leave(db->mutex); + db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ return res.rc; } sqlite3_free(res.zErrMsg); @@ -62536,6 +65255,7 @@ SQLITE_API int sqlite3_get_table( azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) ); if( azNew==0 ){ sqlite3_free_table(&res.azResult[1]); + db->errCode = SQLITE_NOMEM; return SQLITE_NOMEM; } res.nAlloc = res.nData+1; @@ -62554,10 +65274,10 @@ SQLITE_API void sqlite3_free_table( char **azResult /* Result returned from from sqlite3_get_table() */ ){ if( azResult ){ - int i, n; + sqlite3_intptr_t i, n; azResult--; - if( azResult==0 ) return; - n = (int)azResult[0]; + assert( azResult!=0 ); + n = (sqlite3_intptr_t)azResult[0]; for(i=1; iinit.busy ){ - static const VdbeOpList insertTrig[] = { - { OP_NewRowid, 0, 0, 0 }, - { OP_String8, 0, 0, "trigger" }, - { OP_String8, 0, 0, 0 }, /* 2: trigger name */ - { OP_String8, 0, 0, 0 }, /* 3: table name */ - { OP_Integer, 0, 0, 0 }, - { OP_String8, 0, 0, "CREATE TRIGGER "}, - { OP_String8, 0, 0, 0 }, /* 6: SQL */ - { OP_Concat, 0, 0, 0 }, - { OP_MakeRecord, 5, 0, "aaada" }, - { OP_Insert, 0, 0, 0 }, - }; - int addr; Vdbe *v; + char *z; /* Make an entry in the sqlite_master table */ v = sqlite3GetVdbe(pParse); if( v==0 ) goto triggerfinish_cleanup; sqlite3BeginWriteOperation(pParse, 0, iDb); - sqlite3OpenMasterTable(pParse, iDb); - addr = sqlite3VdbeAddOpList(v, ArraySize(insertTrig), insertTrig); - sqlite3VdbeChangeP3(v, addr+2, pTrig->name, 0); - sqlite3VdbeChangeP3(v, addr+3, pTrig->table, 0); - sqlite3VdbeChangeP3(v, addr+6, (char*)pAll->z, pAll->n); - sqlite3ChangeCookie(db, v, iDb); - sqlite3VdbeAddOp(v, OP_Close, 0, 0); - sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 0, sqlite3MPrintf( - db, "type='trigger' AND name='%q'", pTrig->name), P3_DYNAMIC + z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); + sqlite3NestedParse(pParse, + "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTrig->name, + pTrig->table, z); + sqlite3_free(z); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf( + db, "type='trigger' AND name='%q'", pTrig->name), P4_DYNAMIC ); } @@ -63103,12 +65811,12 @@ SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ int base; static const VdbeOpList dropTrigger[] = { { OP_Rewind, 0, ADDR(9), 0}, - { OP_String8, 0, 0, 0}, /* 1 */ - { OP_Column, 0, 1, 0}, - { OP_Ne, 0, ADDR(8), 0}, - { OP_String8, 0, 0, "trigger"}, - { OP_Column, 0, 0, 0}, - { OP_Ne, 0, ADDR(8), 0}, + { OP_String8, 0, 1, 0}, /* 1 */ + { OP_Column, 0, 1, 2}, + { OP_Ne, 2, ADDR(8), 1}, + { OP_String8, 0, 1, 0}, /* 4: "trigger" */ + { OP_Column, 0, 0, 2}, + { OP_Ne, 2, ADDR(8), 1}, { OP_Delete, 0, 0, 0}, { OP_Next, 0, ADDR(1), 0}, /* 8 */ }; @@ -63116,10 +65824,11 @@ SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ sqlite3BeginWriteOperation(pParse, 0, iDb); sqlite3OpenMasterTable(pParse, iDb); base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger); - sqlite3VdbeChangeP3(v, base+1, pTrigger->name, 0); - sqlite3ChangeCookie(db, v, iDb); - sqlite3VdbeAddOp(v, OP_Close, 0, 0); - sqlite3VdbeOp3(v, OP_DropTrigger, iDb, 0, pTrigger->name, 0); + sqlite3VdbeChangeP4(v, base+1, pTrigger->name, 0); + sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp2(v, OP_Close, 0, 0); + sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->name, 0); } } @@ -63244,8 +65953,8 @@ static int codeTriggerProgram( assert( pTriggerStep!=0 ); assert( v!=0 ); - sqlite3VdbeAddOp(v, OP_ContextPush, 0, 0); - VdbeComment((v, "# begin trigger %s", pStepList->pTrig->name)); + sqlite3VdbeAddOp2(v, OP_ContextPush, 0, 0); + VdbeComment((v, "begin trigger %s", pStepList->pTrig->name)); while( pTriggerStep ){ orconf = (orconfin == OE_Default)?pTriggerStep->orconf:orconfin; pParse->trigStack->orconf = orconf; @@ -63253,8 +65962,11 @@ static int codeTriggerProgram( case TK_SELECT: { Select *ss = sqlite3SelectDup(db, pTriggerStep->pSelect); if( ss ){ + SelectDest dest; + + sqlite3SelectDestInit(&dest, SRT_Discard, 0); sqlite3SelectResolve(pParse, ss, 0); - sqlite3Select(pParse, ss, SRT_Discard, 0, 0, 0, 0, 0); + sqlite3Select(pParse, ss, &dest, 0, 0, 0, 0); sqlite3SelectDelete(ss); } break; @@ -63262,31 +65974,31 @@ static int codeTriggerProgram( case TK_UPDATE: { SrcList *pSrc; pSrc = targetSrcList(pParse, pTriggerStep); - sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); + sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0); sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db, pTriggerStep->pExprList), sqlite3ExprDup(db, pTriggerStep->pWhere), orconf); - sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0); break; } case TK_INSERT: { SrcList *pSrc; pSrc = targetSrcList(pParse, pTriggerStep); - sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); + sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0); sqlite3Insert(pParse, pSrc, sqlite3ExprListDup(db, pTriggerStep->pExprList), sqlite3SelectDup(db, pTriggerStep->pSelect), sqlite3IdListDup(db, pTriggerStep->pIdList), orconf); - sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0); break; } case TK_DELETE: { SrcList *pSrc; - sqlite3VdbeAddOp(v, OP_ResetCount, 0, 0); + sqlite3VdbeAddOp2(v, OP_ResetCount, 0, 0); pSrc = targetSrcList(pParse, pTriggerStep); sqlite3DeleteFrom(pParse, pSrc, sqlite3ExprDup(db, pTriggerStep->pWhere)); - sqlite3VdbeAddOp(v, OP_ResetCount, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResetCount, 1, 0); break; } default: @@ -63294,8 +66006,8 @@ static int codeTriggerProgram( } pTriggerStep = pTriggerStep->pNext; } - sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0); - VdbeComment((v, "# end trigger %s", pStepList->pTrig->name)); + sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); + VdbeComment((v, "end trigger %s", pStepList->pTrig->name)); return 0; } @@ -63319,6 +66031,13 @@ static int codeTriggerProgram( ** a row containing values to be substituted for old.* expressions in the ** trigger program(s). ** +** If they are not NULL, the piOldColMask and piNewColMask output variables +** are set to values that describe the columns used by the trigger program +** in the OLD.* and NEW.* tables respectively. If column N of the +** pseudo-table is read at least once, the corresponding bit of the output +** mask is set. If a column with an index greater than 32 is read, the +** output mask is set to the special value 0xffffffff. +** */ SQLITE_PRIVATE int sqlite3CodeRowTrigger( Parse *pParse, /* Parse context */ @@ -63329,11 +66048,17 @@ SQLITE_PRIVATE int sqlite3CodeRowTrigger( int newIdx, /* The indice of the "new" row to access */ int oldIdx, /* The indice of the "old" row to access */ int orconf, /* ON CONFLICT policy */ - int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ + int ignoreJump, /* Instruction to jump to for RAISE(IGNORE) */ + u32 *piOldColMask, /* OUT: Mask of columns used from the OLD.* table */ + u32 *piNewColMask /* OUT: Mask of columns used from the NEW.* table */ ){ Trigger *p; + sqlite3 *db = pParse->db; TriggerStack trigStackEntry; + trigStackEntry.oldColMask = 0; + trigStackEntry.newColMask = 0; + assert(op == TK_UPDATE || op == TK_INSERT || op == TK_DELETE); assert(tr_tm == TRIGGER_BEFORE || tr_tm == TRIGGER_AFTER ); @@ -63341,7 +66066,6 @@ SQLITE_PRIVATE int sqlite3CodeRowTrigger( for(p=pTab->pTrigger; p; p=p->pNext){ int fire_this = 0; - sqlite3 *db = pParse->db; /* Determine whether we should code this trigger */ if( @@ -63370,6 +66094,11 @@ SQLITE_PRIVATE int sqlite3CodeRowTrigger( AuthContext sContext; NameContext sNC; +#ifndef SQLITE_OMIT_TRACE + sqlite3VdbeAddOp4(pParse->pVdbe, OP_Trace, 0, 0, 0, + sqlite3MPrintf(db, "-- TRIGGER %s", p->name), + P4_DYNAMIC); +#endif memset(&sNC, 0, sizeof(sNC)); sNC.pParse = pParse; @@ -63391,7 +66120,7 @@ SQLITE_PRIVATE int sqlite3CodeRowTrigger( sqlite3ExprDelete(whenExpr); return 1; } - sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, 1); + sqlite3ExprIfFalse(pParse, whenExpr, endTrigger, SQLITE_JUMPIFNULL); sqlite3ExprDelete(whenExpr); codeTriggerProgram(pParse, p->step_list, orconf); @@ -63403,6 +66132,8 @@ SQLITE_PRIVATE int sqlite3CodeRowTrigger( sqlite3VdbeResolveLabel(pParse->pVdbe, endTrigger); } } + if( piOldColMask ) *piOldColMask |= trigStackEntry.oldColMask; + if( piNewColMask ) *piNewColMask |= trigStackEntry.newColMask; return 0; } #endif /* !defined(SQLITE_OMIT_TRIGGER) */ @@ -63423,7 +66154,7 @@ SQLITE_PRIVATE int sqlite3CodeRowTrigger( ** This file contains C code routines that are called by the parser ** to handle UPDATE statements. ** -** $Id: update.c,v 1.144 2007/12/12 16:06:23 danielk1977 Exp $ +** $Id: update.c,v 1.171 2008/02/12 16:52:14 drh Exp $ */ #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -63441,7 +66172,7 @@ static void updateVirtualTable( /* ** The most recently coded instruction was an OP_Column to retrieve the -** i-th column of table pTab. This routine sets the P3 parameter of the +** i-th column of table pTab. This routine sets the P4 parameter of the ** OP_Column to the default value, if any. ** ** The default value of a column is specified by a DEFAULT clause in the @@ -63449,9 +66180,9 @@ static void updateVirtualTable( ** was created, or added later to the table definition by an ALTER TABLE ** command. If the latter, then the row-records in the table btree on disk ** may not contain a value for the column and the default value, taken -** from the P3 parameter of the OP_Column instruction, is returned instead. +** from the P4 parameter of the OP_Column instruction, is returned instead. ** If the former, then all row-records are guaranteed to include a value -** for the column and the P3 value is not required. +** for the column and the P4 value is not required. ** ** Column definitions created by an ALTER TABLE command may only have ** literal default values specified: a number, null or a string. (If a more @@ -63459,7 +66190,7 @@ static void updateVirtualTable( ** when the ALTER TABLE is executed and one of the literal values written ** into the sqlite_master table.) ** -** Therefore, the P3 parameter is only required if the default value for +** Therefore, the P4 parameter is only required if the default value for ** the column is a literal number, string or null. The sqlite3ValueFromExpr() ** function is capable of transforming these types of expressions into ** sqlite3_value objects. @@ -63469,12 +66200,12 @@ SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i){ sqlite3_value *pValue; u8 enc = ENC(sqlite3VdbeDb(v)); Column *pCol = &pTab->aCol[i]; + VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); assert( inCol ); - sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, pCol->affinity, &pValue); + sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, + pCol->affinity, &pValue); if( pValue ){ - sqlite3VdbeChangeP3(v, -1, (const char *)pValue, P3_MEM); - }else{ - VdbeComment((v, "# %s.%s", pTab->zName, pCol->zName)); + sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); } } } @@ -63500,11 +66231,9 @@ SQLITE_PRIVATE void sqlite3Update( Vdbe *v; /* The virtual database engine */ Index *pIdx; /* For looping over indices */ int nIdx; /* Number of indices that need updating */ - int nIdxTotal; /* Total number of indices */ int iCur; /* VDBE Cursor number of pTab */ sqlite3 *db; /* The database structure */ - Index **apIdx = 0; /* An array of indices that need updating too */ - char *aIdxUsed = 0; /* aIdxUsed[i]==1 if the i-th index is used */ + int *aRegIdx = 0; /* One register assigned to each index to be updated */ int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the ** an expression for the i-th column of the table. ** aXRef[i]==-1 if the i-th column is not changed. */ @@ -63514,17 +66243,28 @@ SQLITE_PRIVATE void sqlite3Update( AuthContext sContext; /* The authorization context */ NameContext sNC; /* The name-context to resolve expressions in */ int iDb; /* Database containing the table being updated */ - int memCnt = 0; /* Memory cell used for counting rows changed */ - int mem1; /* Memory address storing the rowid for next row to update */ + int j1; /* Addresses of jump instructions */ #ifndef SQLITE_OMIT_TRIGGER int isView; /* Trying to update a view */ int triggers_exist = 0; /* True if any row triggers exist */ #endif + int iBeginAfterTrigger; /* Address of after trigger program */ + int iEndAfterTrigger; /* Exit of after trigger program */ + int iBeginBeforeTrigger; /* Address of before trigger program */ + int iEndBeforeTrigger; /* Exit of before trigger program */ + u32 old_col_mask = 0; /* Mask of OLD.* columns in use */ + u32 new_col_mask = 0; /* Mask of NEW.* columns in use */ int newIdx = -1; /* index of trigger "new" temp table */ int oldIdx = -1; /* index of trigger "old" temp table */ + /* Register Allocations */ + int regRowCount = 0; /* A count of rows changed */ + int regOldRowid; /* The old rowid */ + int regNewRowid; /* The new rowid */ + int regData; /* New data for the row */ + sContext.pParse = 0; db = pParse->db; if( pParse->nErr || db->mallocFailed ){ @@ -63630,41 +66370,47 @@ SQLITE_PRIVATE void sqlite3Update( #endif } - /* Allocate memory for the array apIdx[] and fill it with pointers to every - ** index that needs to be updated. Indices only need updating if their - ** key includes one of the columns named in pChanges or if the record - ** number of the original table entry is changing. + /* Allocate memory for the array aRegIdx[]. There is one entry in the + ** array for each index associated with table being updated. Fill in + ** the value with a register number for indices that are to be used + ** and with zero for unused indices. */ - for(nIdx=nIdxTotal=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdxTotal++){ - if( chngRowid ){ - i = 0; - }else { - for(i=0; inColumn; i++){ - if( aXRef[pIdx->aiColumn[i]]>=0 ) break; - } - } - if( inColumn ) nIdx++; - } - if( nIdxTotal>0 ){ - apIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx + nIdxTotal ); - if( apIdx==0 ) goto update_cleanup; - aIdxUsed = (char*)&apIdx[nIdx]; + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} + if( nIdx>0 ){ + aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx ); + if( aRegIdx==0 ) goto update_cleanup; } - for(nIdx=j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + int reg; if( chngRowid ){ - i = 0; + reg = ++pParse->nMem; }else{ + reg = 0; for(i=0; inColumn; i++){ - if( aXRef[pIdx->aiColumn[i]]>=0 ) break; + if( aXRef[pIdx->aiColumn[i]]>=0 ){ + reg = ++pParse->nMem; + break; + } } } - if( inColumn ){ - apIdx[nIdx++] = pIdx; - aIdxUsed[j] = 1; - }else{ - aIdxUsed[j] = 0; - } + aRegIdx[j] = reg; + } + + /* Allocate a block of register used to store the change record + ** sent to sqlite3GenerateConstraintChecks(). There are either + ** one or two registers for holding the rowid. One rowid register + ** is used if chngRowid is false and two are used if chngRowid is + ** true. Following these are pTab->nCol register holding column + ** data. + */ + regOldRowid = regNewRowid = pParse->nMem + 1; + pParse->nMem += pTab->nCol + 1; + if( chngRowid ){ + regNewRowid++; + pParse->nMem++; } + regData = regNewRowid+1; + /* Begin generating code. */ @@ -63672,7 +66418,6 @@ SQLITE_PRIVATE void sqlite3Update( if( v==0 ) goto update_cleanup; if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); sqlite3BeginWriteOperation(pParse, 1, iDb); - mem1 = pParse->nMem++; #ifndef SQLITE_OMIT_VIRTUALTABLE /* Virtual tables must be handled separately */ @@ -63685,38 +66430,65 @@ SQLITE_PRIVATE void sqlite3Update( } #endif - /* Resolve the column names in all the expressions in the - ** WHERE clause. - */ - if( sqlite3ExprResolveNames(&sNC, pWhere) ){ - goto update_cleanup; - } - /* Start the view context */ if( isView ){ sqlite3AuthContextPush(pParse, &sContext, pTab->zName); } + /* Generate the code for triggers. + */ + if( triggers_exist ){ + int iGoto; + + /* Create pseudo-tables for NEW and OLD + */ + sqlite3VdbeAddOp2(v, OP_OpenPseudo, oldIdx, 0); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, oldIdx, pTab->nCol); + sqlite3VdbeAddOp2(v, OP_OpenPseudo, newIdx, 0); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, newIdx, pTab->nCol); + + iGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); + addr = sqlite3VdbeMakeLabel(v); + iBeginBeforeTrigger = sqlite3VdbeCurrentAddr(v); + if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab, + newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){ + goto update_cleanup; + } + iEndBeforeTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); + iBeginAfterTrigger = sqlite3VdbeCurrentAddr(v); + if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab, + newIdx, oldIdx, onError, addr, &old_col_mask, &new_col_mask) ){ + goto update_cleanup; + } + iEndAfterTrigger = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); + sqlite3VdbeJumpHere(v, iGoto); + } + /* If we are trying to update a view, realize that view into ** a ephemeral table. */ if( isView ){ - Select *pView; - pView = sqlite3SelectDup(db, pTab->pSelect); - sqlite3Select(pParse, pView, SRT_EphemTab, iCur, 0, 0, 0, 0); - sqlite3SelectDelete(pView); + sqlite3MaterializeView(pParse, pTab->pSelect, pWhere, + old_col_mask|new_col_mask, iCur); + } + + /* Resolve the column names in all the expressions in the + ** WHERE clause. + */ + if( sqlite3ExprResolveNames(&sNC, pWhere) ){ + goto update_cleanup; } /* Begin the database scan */ - pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0); if( pWInfo==0 ) goto update_cleanup; /* Remember the rowid of every item to be updated. */ - sqlite3VdbeAddOp(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid, iCur, 0); - sqlite3VdbeAddOp(v, OP_FifoWrite, 0, 0); + sqlite3VdbeAddOp2(v, IsVirtual(pTab) ? OP_VRowid : OP_Rowid,iCur,regOldRowid); + sqlite3VdbeAddOp2(v, OP_FifoWrite, regOldRowid, 0); /* End the database scan loop. */ @@ -63725,209 +66497,195 @@ SQLITE_PRIVATE void sqlite3Update( /* Initialize the count of updated rows */ if( db->flags & SQLITE_CountRows && !pParse->trigStack ){ - memCnt = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemInt, 0, memCnt); + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); } - if( triggers_exist ){ - - /* Create pseudo-tables for NEW and OLD + if( !isView && !IsVirtual(pTab) ){ + /* + ** Open every index that needs updating. Note that if any + ** index could potentially invoke a REPLACE conflict resolution + ** action, then we need to open all indices because we might need + ** to be deleting some records. */ - sqlite3VdbeAddOp(v, OP_OpenPseudo, oldIdx, 0); - sqlite3VdbeAddOp(v, OP_SetNumColumns, oldIdx, pTab->nCol); - sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0); - sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol); + sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); + if( onError==OE_Replace ){ + openAll = 1; + }else{ + openAll = 0; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->onError==OE_Replace ){ + openAll = 1; + break; + } + } + } + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + if( openAll || aRegIdx[i]>0 ){ + KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb, + (char*)pKey, P4_KEYINFO_HANDOFF); + assert( pParse->nTab>iCur+i+1 ); + } + } + } + + /* Jump back to this point if a trigger encounters an IGNORE constraint. */ + if( triggers_exist ){ + sqlite3VdbeResolveLabel(v, addr); + } + + /* Top of the update loop */ + addr = sqlite3VdbeAddOp2(v, OP_FifoRead, regOldRowid, 0); - /* The top of the update loop for when there are triggers. + if( triggers_exist ){ + int regRowid; + int regRow; + int regCols; + + /* Make cursor iCur point to the record that is being updated. */ - addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); - sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0); - - if( !isView ){ - /* Open a cursor and make it point to the record that is - ** being updated. - */ - sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); - } - sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); /* Generate the OLD table */ - sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); - sqlite3VdbeAddOp(v, OP_RowData, iCur, 0); - sqlite3VdbeAddOp(v, OP_Insert, oldIdx, 0); + regRowid = sqlite3GetTempReg(pParse); + regRow = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid); + if( !old_col_mask ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regRow); + }else{ + sqlite3VdbeAddOp2(v, OP_RowData, iCur, regRow); + } + sqlite3VdbeAddOp3(v, OP_Insert, oldIdx, regRow, regRowid); /* Generate the NEW table */ if( chngRowid ){ - sqlite3ExprCodeAndCache(pParse, pRowidExpr); + sqlite3ExprCodeAndCache(pParse, pRowidExpr, regRowid); }else{ - sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regRowid); } + regCols = sqlite3GetTempRange(pParse, pTab->nCol); for(i=0; inCol; i++){ if( i==pTab->iPKey ){ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i); continue; } j = aXRef[i]; - if( j<0 ){ - sqlite3VdbeAddOp(v, OP_Column, iCur, i); - sqlite3ColumnDefault(v, pTab, i); + if( new_col_mask&((u32)1<a[j].pExpr, regCols+i); + } }else{ - sqlite3ExprCodeAndCache(pParse, pChanges->a[j].pExpr); + sqlite3VdbeAddOp2(v, OP_Null, 0, regCols+i); } } - sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regCols, pTab->nCol, regRow); if( !isView ){ sqlite3TableAffinityStr(v, pTab); } + sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol); if( pParse->nErr ) goto update_cleanup; - sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0); - if( !isView ){ - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); - } + sqlite3VdbeAddOp3(v, OP_Insert, newIdx, regRow, regRowid); + sqlite3ReleaseTempReg(pParse, regRowid); + sqlite3ReleaseTempReg(pParse, regRow); - /* Fire the BEFORE and INSTEAD OF triggers - */ - if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_BEFORE, pTab, - newIdx, oldIdx, onError, addr) ){ - goto update_cleanup; - } - - if( !isView ){ - sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0); - } + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginBeforeTrigger); + sqlite3VdbeJumpHere(v, iEndBeforeTrigger); } if( !isView && !IsVirtual(pTab) ){ - /* - ** Open every index that needs updating. Note that if any - ** index could potentially invoke a REPLACE conflict resolution - ** action, then we need to open all indices because we might need - ** to be deleting some records. - */ - sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); - if( onError==OE_Replace ){ - openAll = 1; - }else{ - openAll = 0; - for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ - if( pIdx->onError==OE_Replace ){ - openAll = 1; - break; - } - } - } - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ - if( openAll || aIdxUsed[i] ){ - KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - sqlite3VdbeOp3(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, - (char*)pKey, P3_KEYINFO_HANDOFF); - assert( pParse->nTab>iCur+i+1 ); - } - } - /* Loop over every record that needs updating. We have to load ** the old data for each record to be updated because some columns ** might not change and we will need to copy the old value. ** Also, the old data is needed to delete the old index entries. ** So make the cursor point at the old record. */ - if( !triggers_exist ){ - addr = sqlite3VdbeAddOp(v, OP_FifoRead, 0, 0); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); - sqlite3VdbeAddOp(v, OP_MemStore, mem1, 0); - } - sqlite3VdbeAddOp(v, OP_NotExists, iCur, addr); - sqlite3VdbeAddOp(v, OP_MemLoad, mem1, 0); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); /* If the record number will change, push the record number as it ** will be after the update. (The old record number is currently ** on top of the stack.) */ if( chngRowid ){ - sqlite3ExprCode(pParse, pRowidExpr); - sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); + sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); } /* Compute new data for this record. */ for(i=0; inCol; i++){ if( i==pTab->iPKey ){ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, regData+i); continue; } j = aXRef[i]; if( j<0 ){ - sqlite3VdbeAddOp(v, OP_Column, iCur, i); + sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regData+i); sqlite3ColumnDefault(v, pTab, i); }else{ - sqlite3ExprCode(pParse, pChanges->a[j].pExpr); + sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regData+i); } } /* Do constraint checks */ - sqlite3GenerateConstraintChecks(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, - onError, addr); + sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid, + aRegIdx, chngRowid, 1, + onError, addr); /* Delete the old indices for the current record. */ - sqlite3GenerateRowIndexDelete(v, pTab, iCur, aIdxUsed); + j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid); + sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx); /* If changing the record number, delete the old record. */ if( chngRowid ){ - sqlite3VdbeAddOp(v, OP_Delete, iCur, 0); + sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0); } + sqlite3VdbeJumpHere(v, j1); /* Create the new index entries and the new record. */ - sqlite3CompleteInsertion(pParse, pTab, iCur, aIdxUsed, chngRowid, 1, -1, 0); + sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, + aRegIdx, chngRowid, 1, -1, 0); } /* Increment the row counter */ if( db->flags & SQLITE_CountRows && !pParse->trigStack){ - sqlite3VdbeAddOp(v, OP_MemIncr, 1, memCnt); + sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); } /* If there are triggers, close all the cursors after each iteration ** through the loop. The fire the after triggers. */ if( triggers_exist ){ - if( !isView ){ - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ - if( openAll || aIdxUsed[i] ) - sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); - } - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); - } - if( sqlite3CodeRowTrigger(pParse, TK_UPDATE, pChanges, TRIGGER_AFTER, pTab, - newIdx, oldIdx, onError, addr) ){ - goto update_cleanup; - } + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBeginAfterTrigger); + sqlite3VdbeJumpHere(v, iEndAfterTrigger); } /* Repeat the above with the next record to be updated, until ** all record selected by the WHERE clause have been updated. */ - sqlite3VdbeAddOp(v, OP_Goto, 0, addr); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); sqlite3VdbeJumpHere(v, addr); - /* Close all tables if there were no FOR EACH ROW triggers */ - if( !triggers_exist ){ - for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ - if( openAll || aIdxUsed[i] ){ - sqlite3VdbeAddOp(v, OP_Close, iCur+i+1, 0); - } + /* Close all tables */ + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + if( openAll || aRegIdx[i]>0 ){ + sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0); } - sqlite3VdbeAddOp(v, OP_Close, iCur, 0); - }else{ - sqlite3VdbeAddOp(v, OP_Close, newIdx, 0); - sqlite3VdbeAddOp(v, OP_Close, oldIdx, 0); + } + sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); + if( triggers_exist ){ + sqlite3VdbeAddOp2(v, OP_Close, newIdx, 0); + sqlite3VdbeAddOp2(v, OP_Close, oldIdx, 0); } /* @@ -63936,15 +66694,14 @@ SQLITE_PRIVATE void sqlite3Update( ** invoke the callback function. */ if( db->flags & SQLITE_CountRows && !pParse->trigStack && pParse->nested==0 ){ - sqlite3VdbeAddOp(v, OP_MemLoad, memCnt, 0); - sqlite3VdbeAddOp(v, OP_Callback, 1, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); sqlite3VdbeSetNumCols(v, 1); - sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P3_STATIC); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", P4_STATIC); } update_cleanup: sqlite3AuthContextPop(&sContext); - sqlite3_free(apIdx); + sqlite3_free(aRegIdx); sqlite3_free(aXRef); sqlite3SrcListDelete(pTabList); sqlite3ExprListDelete(pChanges); @@ -63988,7 +66745,10 @@ static void updateVirtualTable( int ephemTab; /* Table holding the result of the SELECT */ int i; /* Loop counter */ int addr; /* Address of top of loop */ + int iReg; /* First register in set passed to OP_VUpdate */ sqlite3 *db = pParse->db; /* Database connection */ + const char *pVtab = (const char*)pTab->pVtab; + SelectDest dest; /* Construct the SELECT statement that will find the new values for ** all updated rows. @@ -64015,33 +66775,28 @@ static void updateVirtualTable( */ assert( v ); ephemTab = pParse->nTab++; - sqlite3VdbeAddOp(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); /* fill the ephemeral table */ - sqlite3Select(pParse, pSelect, SRT_Table, ephemTab, 0, 0, 0, 0); + sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); + sqlite3Select(pParse, pSelect, &dest, 0, 0, 0, 0); - /* - ** Generate code to scan the ephemeral table and call VDelete and - ** VInsert - */ - sqlite3VdbeAddOp(v, OP_Rewind, ephemTab, 0); + /* Generate code to scan the ephemeral table and call VUpdate. */ + iReg = ++pParse->nMem; + pParse->nMem += pTab->nCol+1; + sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); addr = sqlite3VdbeCurrentAddr(v); - sqlite3VdbeAddOp(v, OP_Column, ephemTab, 0); - if( pRowid ){ - sqlite3VdbeAddOp(v, OP_Column, ephemTab, 1); - }else{ - sqlite3VdbeAddOp(v, OP_Dup, 0, 0); - } + sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg); + sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1); for(i=0; inCol; i++){ - sqlite3VdbeAddOp(v, OP_Column, ephemTab, i+1+(pRowid!=0)); + sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i); } pParse->pVirtualLock = pTab; - sqlite3VdbeOp3(v, OP_VUpdate, 0, pTab->nCol+2, - (const char*)pTab->pVtab, P3_VTAB); - sqlite3VdbeAddOp(v, OP_Next, ephemTab, addr); + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVtab, P4_VTAB); + sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr); sqlite3VdbeJumpHere(v, addr-1); - sqlite3VdbeAddOp(v, OP_Close, ephemTab, 0); + sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); /* Cleanup */ sqlite3SelectDelete(pSelect); @@ -64066,7 +66821,7 @@ static void updateVirtualTable( ** Most of the code in this file may be omitted by defining the ** SQLITE_OMIT_VACUUM macro. ** -** $Id: vacuum.c,v 1.75 2007/12/05 01:38:24 drh Exp $ +** $Id: vacuum.c,v 1.76 2008/01/03 00:01:25 drh Exp $ */ #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) @@ -64120,7 +66875,7 @@ static int execExecSql(sqlite3 *db, const char *zSql){ SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){ Vdbe *v = sqlite3GetVdbe(pParse); if( v ){ - sqlite3VdbeAddOp(v, OP_Vacuum, 0, 0); + sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0); } return; } @@ -64327,7 +67082,7 @@ end_of_vacuum: ************************************************************************* ** This file contains code used to help implement virtual tables. ** -** $Id: vtab.c,v 1.59 2007/09/20 11:32:18 rse Exp $ +** $Id: vtab.c,v 1.65 2008/03/06 09:58:50 mlcreech Exp $ */ #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -64408,12 +67163,12 @@ SQLITE_PRIVATE void sqlite3VtabLock(sqlite3_vtab *pVtab){ SQLITE_PRIVATE void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){ pVtab->nRef--; assert(db); - assert(!sqlite3SafetyCheck(db)); + assert( sqlite3SafetyCheckOk(db) ); if( pVtab->nRef==0 ){ if( db->magic==SQLITE_MAGIC_BUSY ){ - sqlite3SafetyOff(db); + (void)sqlite3SafetyOff(db); pVtab->pModule->xDisconnect(pVtab); - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); } else { pVtab->pModule->xDisconnect(pVtab); } @@ -64574,29 +67329,30 @@ SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ ** SQLITE_MASTER table. We just need to update that slot with all ** the information we've collected. ** - ** The top of the stack is the rootpage allocated by sqlite3StartTable(). - ** This value is always 0 and is ignored, a virtual table does not have a - ** rootpage. The next entry on the stack is the rowid of the record - ** in the sqlite_master table. + ** The VM register number pParse->regRowid holds the rowid of an + ** entry in the sqlite_master table tht was created for this vtab + ** by sqlite3StartTable(). */ iDb = sqlite3SchemaToIndex(db, pTab->pSchema); sqlite3NestedParse(pParse, "UPDATE %Q.%s " "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " - "WHERE rowid=#1", + "WHERE rowid=#%d", db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pTab->zName, pTab->zName, - zStmt + zStmt, + pParse->regRowid ); sqlite3_free(zStmt); v = sqlite3GetVdbe(pParse); - sqlite3ChangeCookie(db, v, iDb); + sqlite3ChangeCookie(pParse, iDb); - sqlite3VdbeAddOp(v, OP_Expire, 0, 0); + sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); zWhere = sqlite3MPrintf(db, "name='%q'", pTab->zName); - sqlite3VdbeOp3(v, OP_ParseSchema, iDb, 1, zWhere, P3_DYNAMIC); - sqlite3VdbeOp3(v, OP_VCreate, iDb, 0, pTab->zName, strlen(pTab->zName) + 1); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); + sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, + pTab->zName, strlen(pTab->zName) + 1); } /* If we are rereading the sqlite_master table create the in-memory @@ -64908,7 +67664,7 @@ SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab if( xDestroy ){ rc = xDestroy(pTab->pVtab); } - sqlite3SafetyOn(db); + (void)sqlite3SafetyOn(db); if( rc==SQLITE_OK ){ pTab->pVtab = 0; } @@ -64925,7 +67681,7 @@ SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab ** ** The array is cleared after invoking the callbacks. */ -static void callFinaliser(sqlite3 *db, int offset){ +static void callFinaliser(sqlite3 *db, sqlite3_intptr_t offset){ int i; if( db->aVTrans ){ for(i=0; inVTrans && db->aVTrans[i]; i++){ @@ -64978,7 +67734,7 @@ SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, int rc2){ ** sqlite3.aVTrans array. Then clear the array itself. */ SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){ - callFinaliser(db, (int)(&((sqlite3_module *)0)->xRollback)); + callFinaliser(db, (sqlite3_intptr_t)(&((sqlite3_module *)0)->xRollback)); return SQLITE_OK; } @@ -64987,7 +67743,7 @@ SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){ ** sqlite3.aVTrans array. Then clear the array itself. */ SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){ - callFinaliser(db, (int)(&((sqlite3_module *)0)->xCommit)); + callFinaliser(db, (sqlite3_intptr_t)(&((sqlite3_module *)0)->xCommit)); return SQLITE_OK; } @@ -65131,7 +67887,7 @@ SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction( ** so is applicable. Because this module is responsible for selecting ** indices, you might also think of this module as the "query optimizer". ** -** $Id: where.c,v 1.266 2007/12/12 17:42:53 danielk1977 Exp $ +** $Id: where.c,v 1.290 2008/03/17 17:08:33 drh Exp $ */ /* @@ -65143,8 +67899,8 @@ SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction( ** Trace output macros */ #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) -SQLITE_API int sqlite3_where_trace = 0; -# define WHERETRACE(X) if(sqlite3_where_trace) sqlite3DebugPrintf X +SQLITE_PRIVATE int sqlite3WhereTrace = 0; +# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X #else # define WHERETRACE(X) #endif @@ -65631,19 +68387,22 @@ static int isLikeOrGlob( sqlite3 *db, /* The database */ Expr *pExpr, /* Test this expression */ int *pnPattern, /* Number of non-wildcard prefix characters */ - int *pisComplete /* True if the only wildcard is % in the last character */ + int *pisComplete, /* True if the only wildcard is % in the last character */ + int *pnoCase /* True if uppercase is equivalent to lowercase */ ){ const char *z; Expr *pRight, *pLeft; ExprList *pList; int c, cnt; - int noCase; char wc[3]; CollSeq *pColl; - if( !sqlite3IsLikeFunction(db, pExpr, &noCase, wc) ){ + if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ return 0; } +#ifdef SQLITE_EBCDIC + if( *pnoCase ) return 0; +#endif pList = pExpr->pList; pRight = pList->a[0].pExpr; if( pRight->op!=TK_STRING ){ @@ -65654,15 +68413,13 @@ static int isLikeOrGlob( return 0; } pColl = pLeft->pColl; + assert( pColl!=0 || pLeft->iColumn==-1 ); if( pColl==0 ){ - /* TODO: Coverage testing doesn't get this case. Is it actually possible - ** for an expression of type TK_COLUMN to not have an assigned collation - ** sequence at this point? - */ + /* No collation is defined for the ROWID. Use the default. */ pColl = db->pDfltColl; } - if( (pColl->type!=SQLITE_COLL_BINARY || noCase) && - (pColl->type!=SQLITE_COLL_NOCASE || !noCase) ){ + if( (pColl->type!=SQLITE_COLL_BINARY || *pnoCase) && + (pColl->type!=SQLITE_COLL_NOCASE || !*pnoCase) ){ return 0; } sqlite3DequoteExpr(db, pRight); @@ -65832,6 +68589,7 @@ static void exprAnalyze( Bitmask prereqAll; int nPattern; int isComplete; + int noCase; int op; Parse *pParse = pWC->pParse; sqlite3 *db = pParse->db; @@ -65950,6 +68708,7 @@ static void exprAnalyze( exprAnalyzeAll(pSrc, &sOr); assert( sOr.nTerm>=2 ); j = 0; + if( db->mallocFailed ) goto or_not_possible; do{ assert( j='abc' AND x<'abd' AND x LIKE 'abc%' + ** + ** The last character of the prefix "abc" is incremented to form the + ** termination condidtion "abd". This trick of incrementing the last + ** is not 255 and if the character set is not EBCDIC. */ - if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete) ){ + if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete, &noCase) ){ Expr *pLeft, *pRight; Expr *pStr1, *pStr2; Expr *pNewExpr1, *pNewExpr2; @@ -66019,8 +68786,12 @@ or_not_possible: } pStr2 = sqlite3ExprDup(db, pStr1); if( !db->mallocFailed ){ + u8 c, *pC; assert( pStr2->token.dyn ); - ++*(u8*)&pStr2->token.z[nPattern-1]; + pC = (u8*)&pStr2->token.z[nPattern-1]; + c = *pC; + if( noCase ) c = sqlite3UpperToLower[c]; + *pC = c + 1; } pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0); idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); @@ -66276,7 +69047,7 @@ static double estLog(double N){ #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG) static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ int i; - if( !sqlite3_where_trace ) return; + if( !sqlite3WhereTrace ) return; for(i=0; inConstraint; i++){ sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", i, @@ -66294,7 +69065,7 @@ static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ } static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ int i; - if( !sqlite3_where_trace ) return; + if( !sqlite3WhereTrace ) return; for(i=0; inConstraint; i++){ sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", i, @@ -66495,20 +69266,27 @@ static double bestVirtualIndex( *(int*)&pIdxInfo->nOrderBy = 0; } - sqlite3SafetyOff(pParse->db); + (void)sqlite3SafetyOff(pParse->db); WHERETRACE(("xBestIndex for %s\n", pTab->zName)); TRACE_IDX_INPUTS(pIdxInfo); rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo); TRACE_IDX_OUTPUTS(pIdxInfo); + (void)sqlite3SafetyOn(pParse->db); + + for(i=0; inConstraint; i++){ + if( !pIdxInfo->aConstraint[i].usable && pUsage[i].argvIndex>0 ){ + sqlite3ErrorMsg(pParse, + "table %s: xBestIndex returned an invalid plan", pTab->zName); + return 0.0; + } + } + if( rc!=SQLITE_OK ){ if( rc==SQLITE_NOMEM ){ pParse->db->mallocFailed = 1; }else { sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); } - sqlite3SafetyOn(pParse->db); - }else{ - rc = sqlite3SafetyOn(pParse->db); } *(int*)&pIdxInfo->nOrderBy = nOrderBy; @@ -66819,9 +69597,13 @@ static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ static void buildIndexProbe( Vdbe *v, /* Generate code into this VM */ int nColumn, /* The number of columns to check for NULL */ - Index *pIdx /* Index that we will be searching */ + Index *pIdx, /* Index that we will be searching */ + int regSrc, /* Take values from this register */ + int regDest /* Write the result into this register */ ){ - sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); + assert( regSrc>0 ); + assert( regDest>0 ); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regSrc, nColumn, regDest); sqlite3IndexAffinityStr(v, pIdx); } @@ -66831,7 +69613,7 @@ static void buildIndexProbe( ** term can be either X=expr or X IN (...). pTerm is the term to be ** coded. ** -** The current value for the constraint is left on the top of the stack. +** The current value for the constraint is left in register iReg. ** ** For a constraint of the form X=expr, the expression is evaluated and its ** result is left on the stack. For constraints of the form X IN (...) @@ -66840,14 +69622,17 @@ static void buildIndexProbe( static void codeEqualityTerm( Parse *pParse, /* The parsing context */ WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ - WhereLevel *pLevel /* When level of the FROM clause we are working on */ + WhereLevel *pLevel, /* When level of the FROM clause we are working on */ + int iReg /* Leave results in this register */ ){ Expr *pX = pTerm->pExpr; Vdbe *v = pParse->pVdbe; + + assert( iReg>0 && iReg<=pParse->nMem ); if( pX->op==TK_EQ ){ - sqlite3ExprCode(pParse, pX->pRight); + sqlite3ExprCode(pParse, pX->pRight, iReg); }else if( pX->op==TK_ISNULL ){ - sqlite3VdbeAddOp(v, OP_Null, 0, 0); + sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); #ifndef SQLITE_OMIT_SUBQUERY }else{ int eType; @@ -66857,8 +69642,8 @@ static void codeEqualityTerm( assert( pX->op==TK_IN ); eType = sqlite3FindInIndex(pParse, pX, 1); iTab = pX->iTable; - sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0); - VdbeComment((v, "# %.*s", pX->span.n, pX->span.z)); + sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); + VdbeComment((v, "%.*s", pX->span.n, pX->span.z)); if( pLevel->nIn==0 ){ pLevel->nxt = sqlite3VdbeMakeLabel(v); } @@ -66867,11 +69652,14 @@ static void codeEqualityTerm( sizeof(pLevel->aInLoop[0])*pLevel->nIn); pIn = pLevel->aInLoop; if( pIn ){ - int op = ((eType==IN_INDEX_ROWID)?OP_Rowid:OP_Column); pIn += pLevel->nIn - 1; pIn->iCur = iTab; - pIn->topAddr = sqlite3VdbeAddOp(v, op, iTab, 0); - sqlite3VdbeAddOp(v, OP_IsNull, -1, 0); + if( eType==IN_INDEX_ROWID ){ + pIn->topAddr = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); + }else{ + pIn->topAddr = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); + } + sqlite3VdbeAddOp1(v, OP_IsNull, iReg); }else{ pLevel->nIn = 0; } @@ -66903,30 +69691,29 @@ static void codeEqualityTerm( ** this routine allocates an additional nEq memory cells for internal ** use. */ -static void codeAllEqualityTerms( +static int codeAllEqualityTerms( Parse *pParse, /* Parsing context */ WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ WhereClause *pWC, /* The WHERE clause */ - Bitmask notReady /* Which parts of FROM have not yet been coded */ + Bitmask notReady, /* Which parts of FROM have not yet been coded */ + int nExtraReg /* Number of extra registers to allocate */ ){ int nEq = pLevel->nEq; /* The number of == or IN constraints to code */ - int termsInMem = 0; /* If true, store value in mem[] cells */ Vdbe *v = pParse->pVdbe; /* The virtual machine under construction */ Index *pIdx = pLevel->pIdx; /* The index being used for this loop */ int iCur = pLevel->iTabCur; /* The cursor of the table */ WhereTerm *pTerm; /* A single constraint term */ int j; /* Loop counter */ + int regBase; /* Base register */ /* Figure out how many memory cells we will need then allocate them. ** We always need at least one used to store the loop terminator ** value. If there are IN operators we'll need one for each == or ** IN constraint. */ - pLevel->iMem = pParse->nMem++; - if( pLevel->flags & WHERE_COLUMN_IN ){ - pParse->nMem += pLevel->nEq; - termsInMem = 1; - } + pLevel->iMem = pParse->nMem + 1; + regBase = pParse->nMem + 2; + pParse->nMem += pLevel->nEq + 2 + nExtraReg; /* Evaluate the equality constraints */ @@ -66936,22 +69723,12 @@ static void codeAllEqualityTerms( pTerm = findTerm(pWC, iCur, k, notReady, pLevel->flags, pIdx); if( pTerm==0 ) break; assert( (pTerm->flags & TERM_CODED)==0 ); - codeEqualityTerm(pParse, pTerm, pLevel); + codeEqualityTerm(pParse, pTerm, pLevel, regBase+j); if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ - sqlite3VdbeAddOp(v, OP_IsNull, termsInMem ? -1 : -(j+1), pLevel->brk); - } - if( termsInMem ){ - sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem+j+1, 1); - } - } - - /* Make sure all the constraint values are on the top of the stack - */ - if( termsInMem ){ - for(j=0; jiMem+j+1, 0); + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->brk); } } + return regBase; } #if defined(SQLITE_TEST) @@ -66976,13 +69753,7 @@ static void whereInfoFree(WhereInfo *pWInfo){ for(i=0; inLevel; i++){ sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo; if( pInfo ){ - if( pInfo->needToFreeIdxStr ){ - /* Coverage: Don't think this can be reached. By the time this - ** function is called, the index-strings have been passed - ** to the vdbe layer for deletion. - */ - sqlite3_free(pInfo->idxStr); - } + assert( pInfo->needToFreeIdxStr==0 ); sqlite3_free(pInfo); } } @@ -67083,7 +69854,8 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( Parse *pParse, /* The parser context */ SrcList *pTabList, /* A list of all tables to be scanned */ Expr *pWhere, /* The WHERE clause */ - ExprList **ppOrderBy /* An ORDER BY clause, or NULL */ + ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */ + u8 obflag /* One of ORDERBY_MIN, ORDERBY_MAX or ORDERBY_NORMAL */ ){ int i; /* Loop counter */ WhereInfo *pWInfo; /* Will become the return value of this function */ @@ -67098,6 +69870,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( int iFrom; /* First unused FROM clause element */ int andFlags; /* AND-ed combination of all wc.a[].flags */ sqlite3 *db; /* Database connection */ + ExprList *pOrderBy = 0; /* The number of tables in the FROM clause is limited by the number of ** bits in a Bitmask @@ -67107,6 +69880,10 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( return 0; } + if( ppOrderBy ){ + pOrderBy = *ppOrderBy; + } + /* Split the WHERE clause into separate subexpressions where each ** subexpression is separated by an AND operator. */ @@ -67132,7 +69909,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( ** expression and either jump over all of the code or fall thru. */ if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ - sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1); + sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); pWhere = 0; } @@ -67296,7 +70073,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( if( pLevel->flags & WHERE_ORDERBY ){ zMsg = sqlite3MPrintf(db, "%z ORDER BY", zMsg); } - sqlite3VdbeOp3(v, OP_Explain, i, pLevel->iFrom, zMsg, P3_DYNAMIC); + sqlite3VdbeAddOp4(v, OP_Explain, i, pLevel->iFrom, 0, zMsg, P4_DYNAMIC); } #endif /* SQLITE_OMIT_EXPLAIN */ pTabItem = &pTabList->a[pLevel->iFrom]; @@ -67306,7 +70083,8 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( #ifndef SQLITE_OMIT_VIRTUALTABLE if( pLevel->pBestIdx ){ int iCur = pTabItem->iCursor; - sqlite3VdbeOp3(v, OP_VOpen, iCur, 0, (const char*)pTab->pVtab, P3_VTAB); + sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, + (const char*)pTab->pVtab, P4_VTAB); }else #endif if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){ @@ -67325,11 +70103,10 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( if( (pIx = pLevel->pIdx)!=0 ){ KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); assert( pIx->pSchema==pTab->pSchema ); - sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); - VdbeComment((v, "# %s", pIx->zName)); - sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIx->tnum, - (char*)pKey, P3_KEYINFO_HANDOFF); - sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1); + sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIx->zName)); + sqlite3VdbeAddOp2(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1); } sqlite3CodeVerifySchema(pParse, iDb); } @@ -67374,10 +70151,9 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( ** row of the left table of the join. */ if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ - if( !pParse->nMem ) pParse->nMem++; - pLevel->iLeftJoin = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemInt, 0, pLevel->iLeftJoin); - VdbeComment((v, "# init LEFT JOIN no-match flag")); + pLevel->iLeftJoin = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); + VdbeComment((v, "init LEFT JOIN no-match flag")); } #ifndef SQLITE_OMIT_VIRTUALTABLE @@ -67386,6 +70162,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( ** to access the data. */ int j; + int iReg; /* P3 Value for OP_VFilter */ sqlite3_index_info *pBestIdx = pLevel->pBestIdx; int nConstraint = pBestIdx->nConstraint; struct sqlite3_index_constraint_usage *aUsage = @@ -67393,21 +70170,23 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( const struct sqlite3_index_constraint *aConstraint = pBestIdx->aConstraint; + iReg = sqlite3GetTempRange(pParse, nConstraint+2); for(j=1; j<=nConstraint; j++){ int k; for(k=0; kpRight); + sqlite3ExprCode(pParse, wc.a[iTerm].pExpr->pRight, iReg+j+1); break; } } if( k==nConstraint ) break; } - sqlite3VdbeAddOp(v, OP_Integer, j-1, 0); - sqlite3VdbeAddOp(v, OP_Integer, pBestIdx->idxNum, 0); - sqlite3VdbeOp3(v, OP_VFilter, iCur, brk, pBestIdx->idxStr, - pBestIdx->needToFreeIdxStr ? P3_MPRINTF : P3_STATIC); + sqlite3VdbeAddOp2(v, OP_Integer, pBestIdx->idxNum, iReg); + sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1); + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, brk, iReg, pBestIdx->idxStr, + pBestIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC); + sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); pBestIdx->needToFreeIdxStr = 0; for(j=0; jnConstraint; j++){ if( aUsage[j].omit ){ @@ -67427,16 +70206,19 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( ** we reference multiple rows using a "rowid IN (...)" ** construct. */ + int r1; pTerm = findTerm(&wc, iCur, -1, notReady, WO_EQ|WO_IN, 0); assert( pTerm!=0 ); assert( pTerm->pExpr!=0 ); assert( pTerm->leftCursor==iCur ); assert( omitTable==0 ); - codeEqualityTerm(pParse, pTerm, pLevel); + r1 = sqlite3GetTempReg(pParse); + codeEqualityTerm(pParse, pTerm, pLevel, r1); nxt = pLevel->nxt; - sqlite3VdbeAddOp(v, OP_MustBeInt, 1, nxt); - sqlite3VdbeAddOp(v, OP_NotExists, iCur, nxt); + sqlite3VdbeAddOp3(v, OP_MustBeInt, r1, nxt, 1); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, nxt, r1); VdbeComment((v, "pk")); + sqlite3ReleaseTempReg(pParse, r1); pLevel->op = OP_Noop; }else if( pLevel->flags & WHERE_ROWID_RANGE ){ /* Case 2: We have an inequality comparison against the ROWID field. @@ -67455,25 +70237,27 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( } if( pStart ){ Expr *pX; + int r1, regFree1; pX = pStart->pExpr; assert( pX!=0 ); assert( pStart->leftCursor==iCur ); - sqlite3ExprCode(pParse, pX->pRight); - sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LE || pX->op==TK_GT, brk); - sqlite3VdbeAddOp(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk); + r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, ®Free1); + sqlite3VdbeAddOp3(v, OP_ForceInt, r1, brk, + pX->op==TK_LE || pX->op==TK_GT); + sqlite3VdbeAddOp3(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk, r1); VdbeComment((v, "pk")); + sqlite3ReleaseTempReg(pParse, regFree1); disableTerm(pLevel, pStart); }else{ - sqlite3VdbeAddOp(v, bRev ? OP_Last : OP_Rewind, iCur, brk); + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, brk); } if( pEnd ){ Expr *pX; pX = pEnd->pExpr; assert( pX!=0 ); assert( pEnd->leftCursor==iCur ); - sqlite3ExprCode(pParse, pX->pRight); - pLevel->iMem = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); + pLevel->iMem = ++pParse->nMem; + sqlite3ExprCode(pParse, pX->pRight, pLevel->iMem); if( pX->op==TK_LT || pX->op==TK_GT ){ testOp = bRev ? OP_Le : OP_Ge; }else{ @@ -67486,9 +70270,12 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( pLevel->p1 = iCur; pLevel->p2 = start; if( testOp!=OP_Noop ){ - sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0); - sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); - sqlite3VdbeAddOp(v, testOp, SQLITE_AFF_NUMERIC|0x100, brk); + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, r1); + /* sqlite3VdbeAddOp2(v, OP_SCopy, pLevel->iMem, 0); */ + sqlite3VdbeAddOp3(v, testOp, pLevel->iMem, brk, r1); + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); + sqlite3ReleaseTempReg(pParse, r1); } }else if( pLevel->flags & WHERE_COLUMN_RANGE ){ /* Case 3: The WHERE clause term that refers to the right-most @@ -67510,19 +70297,14 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( int testOp; int topLimit = (pLevel->flags & WHERE_TOP_LIMIT)!=0; int btmLimit = (pLevel->flags & WHERE_BTM_LIMIT)!=0; + int isMinQuery = 0; /* If this is an optimized SELECT min(x) ... */ + int regBase; /* Base register holding constraint values */ + int r1; /* Temp register */ /* Generate code to evaluate all constraint terms using == or IN ** and level the values of those terms on the stack. */ - codeAllEqualityTerms(pParse, pLevel, &wc, notReady); - - /* Duplicate the equality term values because they will all be - ** used twice: once to make the termination key and once to make the - ** start key. - */ - for(j=0; j or >= @@ -67538,6 +70320,22 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( SWAP(int, topLimit, btmLimit); } + /* If this loop satisfies a sort order (pOrderBy) request that + ** was passed to this function to implement a "SELECT min(x) ..." + ** query, then the caller will only allow the loop to run for + ** a single iteration. This means that the first row returned + ** should not have a NULL value stored in 'x'. If column 'x' is + ** the first one after the nEq equality constraints in the index, + ** this requires some special handling. + */ + if( (obflag==ORDERBY_MIN) + && (pLevel->flags&WHERE_ORDERBY) + && (pIdx->nColumn>nEq) + && (pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq]) + ){ + isMinQuery = 1; + } + /* Generate the termination key. This is the key value that ** will end the search. There is no termination key if there ** are no equality terms and no "X<..." term. @@ -67548,13 +70346,13 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( nxt = pLevel->nxt; if( topLimit ){ Expr *pX; - int k = pIdx->aiColumn[j]; + int k = pIdx->aiColumn[nEq]; pTerm = findTerm(&wc, iCur, k, notReady, topOp, pIdx); assert( pTerm!=0 ); pX = pTerm->pExpr; assert( (pTerm->flags & TERM_CODED)==0 ); - sqlite3ExprCode(pParse, pX->pRight); - sqlite3VdbeAddOp(v, OP_IsNull, -(nEq*2+1), nxt); + sqlite3ExprCode(pParse, pX->pRight, regBase+nEq); + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, nxt); topEq = pTerm->eOperator & (WO_LE|WO_GE); disableTerm(pLevel, pTerm); testOp = OP_IdxGE; @@ -67562,20 +70360,22 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( testOp = nEq>0 ? OP_IdxGE : OP_Noop; topEq = 1; } - if( testOp!=OP_Noop ){ + if( testOp!=OP_Noop || (isMinQuery&&bRev) ){ int nCol = nEq + topLimit; - pLevel->iMem = pParse->nMem++; - buildIndexProbe(v, nCol, pIdx); + if( isMinQuery && !topLimit ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nCol); + nCol++; + topEq = 0; + } + buildIndexProbe(v, nCol, pIdx, regBase, pLevel->iMem); if( bRev ){ int op = topEq ? OP_MoveLe : OP_MoveLt; - sqlite3VdbeAddOp(v, op, iIdxCur, nxt); - }else{ - sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); + sqlite3VdbeAddOp3(v, op, iIdxCur, nxt, pLevel->iMem); } }else if( bRev ){ - sqlite3VdbeAddOp(v, OP_Last, iIdxCur, brk); + sqlite3VdbeAddOp2(v, OP_Last, iIdxCur, brk); } - + /* Generate the start key. This is the key that defines the lower ** bound on the search. There is no start key if there are no ** equality terms and if there is no "X>..." term. In @@ -67587,33 +70387,41 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( */ if( btmLimit ){ Expr *pX; - int k = pIdx->aiColumn[j]; + int k = pIdx->aiColumn[nEq]; pTerm = findTerm(&wc, iCur, k, notReady, btmOp, pIdx); assert( pTerm!=0 ); pX = pTerm->pExpr; assert( (pTerm->flags & TERM_CODED)==0 ); - sqlite3ExprCode(pParse, pX->pRight); - sqlite3VdbeAddOp(v, OP_IsNull, -(nEq+1), nxt); + sqlite3ExprCode(pParse, pX->pRight, regBase+nEq); + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, nxt); btmEq = pTerm->eOperator & (WO_LE|WO_GE); disableTerm(pLevel, pTerm); }else{ btmEq = 1; } - if( nEq>0 || btmLimit ){ + if( nEq>0 || btmLimit || (isMinQuery&&!bRev) ){ int nCol = nEq + btmLimit; - buildIndexProbe(v, nCol, pIdx); + if( isMinQuery && !btmLimit ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nCol); + nCol++; + btmEq = 0; + } if( bRev ){ - pLevel->iMem = pParse->nMem++; - sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); + r1 = pLevel->iMem; testOp = OP_IdxLT; }else{ + r1 = sqlite3GetTempReg(pParse); + } + buildIndexProbe(v, nCol, pIdx, regBase, r1); + if( !bRev ){ int op = btmEq ? OP_MoveGe : OP_MoveGt; - sqlite3VdbeAddOp(v, op, iIdxCur, nxt); + sqlite3VdbeAddOp3(v, op, iIdxCur, nxt, r1); + sqlite3ReleaseTempReg(pParse, r1); } }else if( bRev ){ testOp = OP_Noop; }else{ - sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, brk); + sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, brk); } /* Generate the the top of the loop. If there is a termination @@ -67622,20 +70430,21 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( */ start = sqlite3VdbeCurrentAddr(v); if( testOp!=OP_Noop ){ - sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); - sqlite3VdbeAddOp(v, testOp, iIdxCur, nxt); + sqlite3VdbeAddOp3(v, testOp, iIdxCur, nxt, pLevel->iMem); if( (topEq && !bRev) || (!btmEq && bRev) ){ - sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC); + sqlite3VdbeChangeP5(v, 1); } } + r1 = sqlite3GetTempReg(pParse); if( topLimit | btmLimit ){ - sqlite3VdbeAddOp(v, OP_Column, iIdxCur, nEq); - sqlite3VdbeAddOp(v, OP_IsNull, 1, cont); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); + sqlite3VdbeAddOp2(v, OP_IsNull, r1, cont); } if( !omitTable ){ - sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0); - sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, r1); + sqlite3VdbeAddOp3(v, OP_MoveGe, iCur, 0, r1); /* Deferred seek */ } + sqlite3ReleaseTempReg(pParse, r1); /* Record the instruction used to terminate the loop. */ @@ -67648,18 +70457,33 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( */ int start; int nEq = pLevel->nEq; + int isMinQuery = 0; /* If this is an optimized SELECT min(x) ... */ + int regBase; /* Base register of array holding constraints */ + int r1; /* Generate code to evaluate all constraint terms using == or IN ** and leave the values of those terms on the stack. */ - codeAllEqualityTerms(pParse, pLevel, &wc, notReady); + regBase = codeAllEqualityTerms(pParse, pLevel, &wc, notReady, 1); nxt = pLevel->nxt; - /* Generate a single key that will be used to both start and terminate - ** the search - */ - buildIndexProbe(v, nEq, pIdx); - sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); + if( (obflag==ORDERBY_MIN) + && (pLevel->flags&WHERE_ORDERBY) + && (pIdx->nColumn>nEq) + && (pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq]) + ){ + isMinQuery = 1; + buildIndexProbe(v, nEq, pIdx, regBase, pLevel->iMem); + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + r1 = ++pParse->nMem; + buildIndexProbe(v, nEq+1, pIdx, regBase, r1); + }else{ + /* Generate a single key that will be used to both start and + ** terminate the search + */ + r1 = pLevel->iMem; + buildIndexProbe(v, nEq, pIdx, regBase, r1); + } /* Generate code (1) to move to the first matching element of the table. ** Then generate code (2) that jumps to "nxt" after the cursor is past @@ -67668,20 +70492,33 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( ** iteration of the scan to see if the scan has finished. */ if( bRev ){ /* Scan in reverse order */ - sqlite3VdbeAddOp(v, OP_MoveLe, iIdxCur, nxt); - start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); - sqlite3VdbeAddOp(v, OP_IdxLT, iIdxCur, nxt); + int op; + if( isMinQuery ){ + op = OP_MoveLt; + }else{ + op = OP_MoveLe; + } + sqlite3VdbeAddOp3(v, op, iIdxCur, nxt, r1); + start = sqlite3VdbeAddOp3(v, OP_IdxLT, iIdxCur, nxt, pLevel->iMem); pLevel->op = OP_Prev; }else{ /* Scan in the forward order */ - sqlite3VdbeAddOp(v, OP_MoveGe, iIdxCur, nxt); - start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); - sqlite3VdbeOp3(v, OP_IdxGE, iIdxCur, nxt, "+", P3_STATIC); + int op; + if( isMinQuery ){ + op = OP_MoveGt; + }else{ + op = OP_MoveGe; + } + sqlite3VdbeAddOp3(v, op, iIdxCur, nxt, r1); + start = sqlite3VdbeAddOp3(v, OP_IdxGE, iIdxCur, nxt, pLevel->iMem); + sqlite3VdbeChangeP5(v, 1); pLevel->op = OP_Next; } if( !omitTable ){ - sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0); - sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0); + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, r1); + sqlite3VdbeAddOp3(v, OP_MoveGe, iCur, 0, r1); /* Deferred seek */ + sqlite3ReleaseTempReg(pParse, r1); } pLevel->p1 = iIdxCur; pLevel->p2 = start; @@ -67693,10 +70530,9 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( assert( bRev==0 ); pLevel->op = OP_Next; pLevel->p1 = iCur; - pLevel->p2 = 1 + sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk); + pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, OP_Rewind, iCur, brk); } notReady &= ~getMask(&maskSet, iCur); - sqlite3VdbeAddOp(v, OP_StackDepth, -1, 0); /* Insert code to test every subexpression that can be completely ** computed using the current set of tables. @@ -67710,7 +70546,7 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ continue; } - sqlite3ExprIfFalse(pParse, pE, cont, 1); + sqlite3ExprIfFalse(pParse, pE, cont, SQLITE_JUMPIFNULL); pTerm->flags |= TERM_CODED; } @@ -67719,13 +70555,13 @@ SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( */ if( pLevel->iLeftJoin ){ pLevel->top = sqlite3VdbeCurrentAddr(v); - sqlite3VdbeAddOp(v, OP_MemInt, 1, pLevel->iLeftJoin); - VdbeComment((v, "# record LEFT JOIN hit")); + sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); + VdbeComment((v, "record LEFT JOIN hit")); for(pTerm=wc.a, j=0; jflags & (TERM_VIRTUAL|TERM_CODED) ) continue; if( (pTerm->prereqAll & notReady)!=0 ) continue; assert( pTerm->pExpr ); - sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, 1); + sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, SQLITE_JUMPIFNULL); pTerm->flags |= TERM_CODED; } } @@ -67808,7 +70644,7 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ pLevel = &pWInfo->a[i]; sqlite3VdbeResolveLabel(v, pLevel->cont); if( pLevel->op!=OP_Noop ){ - sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); + sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); } if( pLevel->nIn ){ struct InLoop *pIn; @@ -67816,7 +70652,7 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ sqlite3VdbeResolveLabel(v, pLevel->nxt); for(j=pLevel->nIn, pIn=&pLevel->aInLoop[j-1]; j>0; j--, pIn--){ sqlite3VdbeJumpHere(v, pIn->topAddr+1); - sqlite3VdbeAddOp(v, OP_Next, pIn->iCur, pIn->topAddr); + sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->topAddr); sqlite3VdbeJumpHere(v, pIn->topAddr-1); } sqlite3_free(pLevel->aInLoop); @@ -67824,12 +70660,12 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ sqlite3VdbeResolveLabel(v, pLevel->brk); if( pLevel->iLeftJoin ){ int addr; - addr = sqlite3VdbeAddOp(v, OP_IfMemPos, pLevel->iLeftJoin, 0); - sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); + sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); if( pLevel->iIdxCur>=0 ){ - sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iIdxCur, 0); + sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); } - sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top); + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->top); sqlite3VdbeJumpHere(v, addr); } } @@ -67847,10 +70683,10 @@ SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ assert( pTab!=0 ); if( pTab->isEphem || pTab->pSelect ) continue; if( (pLevel->flags & WHERE_IDX_ONLY)==0 ){ - sqlite3VdbeAddOp(v, OP_Close, pTabItem->iCursor, 0); + sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); } if( pLevel->pIdx!=0 ){ - sqlite3VdbeAddOp(v, OP_Close, pLevel->iIdxCur, 0); + sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); } /* If this scan uses an index, make code substitutions to read data @@ -68010,11 +70846,9 @@ typedef union { TriggerStep* yy243; struct TrigEvent yy370; SrcList* yy373; - Expr * yy386; struct {int value; int mask;} yy405; Token yy410; IdList* yy432; - int yy495; } YYMINORTYPE; #ifndef YYSTACKDEPTH #define YYSTACKDEPTH 100 @@ -68025,8 +70859,6 @@ typedef union { #define sqlite3ParserARG_STORE yypParser->pParse = pParse #define YYNSTATE 588 #define YYNRULE 312 -#define YYERRORSYMBOL 138 -#define YYERRSYMDT yy495 #define YYFALLBACK 1 #define YY_NO_ACTION (YYNSTATE+YYNRULE+2) #define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1) @@ -68080,154 +70912,141 @@ typedef union { ** yy_default[] Default action for each state. */ static const YYACTIONTYPE yy_action[] = { - /* 0 */ 299, 901, 124, 587, 416, 174, 2, 425, 61, 61, - /* 10 */ 61, 61, 297, 63, 63, 63, 63, 64, 64, 65, - /* 20 */ 65, 65, 66, 212, 454, 214, 432, 438, 68, 63, - /* 30 */ 63, 63, 63, 64, 64, 65, 65, 65, 66, 212, - /* 40 */ 398, 395, 403, 458, 60, 59, 304, 442, 443, 439, - /* 50 */ 439, 62, 62, 61, 61, 61, 61, 265, 63, 63, - /* 60 */ 63, 63, 64, 64, 65, 65, 65, 66, 212, 299, - /* 70 */ 500, 501, 425, 496, 210, 82, 67, 427, 69, 156, + /* 0 */ 292, 901, 124, 587, 409, 172, 2, 418, 61, 61, + /* 10 */ 61, 61, 519, 63, 63, 63, 63, 64, 64, 65, + /* 20 */ 65, 65, 66, 210, 447, 212, 425, 431, 68, 63, + /* 30 */ 63, 63, 63, 64, 64, 65, 65, 65, 66, 210, + /* 40 */ 391, 388, 396, 451, 60, 59, 297, 435, 436, 432, + /* 50 */ 432, 62, 62, 61, 61, 61, 61, 263, 63, 63, + /* 60 */ 63, 63, 64, 64, 65, 65, 65, 66, 210, 292, + /* 70 */ 493, 494, 418, 489, 208, 82, 67, 420, 69, 154, /* 80 */ 63, 63, 63, 63, 64, 64, 65, 65, 65, 66, - /* 90 */ 212, 67, 469, 69, 156, 432, 438, 573, 266, 58, - /* 100 */ 64, 64, 65, 65, 65, 66, 212, 404, 405, 429, - /* 110 */ 429, 429, 299, 60, 59, 304, 442, 443, 439, 439, - /* 120 */ 62, 62, 61, 61, 61, 61, 324, 63, 63, 63, - /* 130 */ 63, 64, 64, 65, 65, 65, 66, 212, 432, 438, - /* 140 */ 94, 65, 65, 65, 66, 212, 403, 212, 421, 34, - /* 150 */ 56, 305, 449, 450, 417, 481, 60, 59, 304, 442, - /* 160 */ 443, 439, 439, 62, 62, 61, 61, 61, 61, 495, + /* 90 */ 210, 67, 462, 69, 154, 425, 431, 573, 264, 58, + /* 100 */ 64, 64, 65, 65, 65, 66, 210, 397, 398, 422, + /* 110 */ 422, 422, 292, 60, 59, 297, 435, 436, 432, 432, + /* 120 */ 62, 62, 61, 61, 61, 61, 317, 63, 63, 63, + /* 130 */ 63, 64, 64, 65, 65, 65, 66, 210, 425, 431, + /* 140 */ 94, 65, 65, 65, 66, 210, 396, 210, 414, 34, + /* 150 */ 56, 298, 442, 443, 410, 488, 60, 59, 297, 435, + /* 160 */ 436, 432, 432, 62, 62, 61, 61, 61, 61, 490, /* 170 */ 63, 63, 63, 63, 64, 64, 65, 65, 65, 66, - /* 180 */ 212, 299, 259, 524, 203, 571, 113, 415, 522, 458, - /* 190 */ 338, 324, 414, 20, 241, 347, 297, 403, 471, 531, - /* 200 */ 292, 454, 214, 570, 569, 472, 530, 432, 438, 151, - /* 210 */ 152, 404, 405, 421, 41, 213, 153, 533, 422, 496, - /* 220 */ 263, 568, 261, 427, 299, 60, 59, 304, 442, 443, - /* 230 */ 439, 439, 62, 62, 61, 61, 61, 61, 324, 63, - /* 240 */ 63, 63, 63, 64, 64, 65, 65, 65, 66, 212, - /* 250 */ 432, 438, 454, 340, 217, 429, 429, 429, 219, 550, - /* 260 */ 421, 41, 404, 405, 490, 567, 213, 299, 60, 59, - /* 270 */ 304, 442, 443, 439, 439, 62, 62, 61, 61, 61, - /* 280 */ 61, 652, 63, 63, 63, 63, 64, 64, 65, 65, - /* 290 */ 65, 66, 212, 432, 438, 103, 652, 549, 524, 519, - /* 300 */ 652, 216, 652, 67, 231, 69, 156, 534, 20, 66, - /* 310 */ 212, 60, 59, 304, 442, 443, 439, 439, 62, 62, - /* 320 */ 61, 61, 61, 61, 265, 63, 63, 63, 63, 64, - /* 330 */ 64, 65, 65, 65, 66, 212, 654, 324, 288, 77, - /* 340 */ 299, 456, 523, 170, 491, 155, 232, 380, 271, 270, - /* 350 */ 327, 654, 445, 445, 485, 654, 485, 654, 210, 421, - /* 360 */ 27, 456, 330, 170, 652, 391, 432, 438, 497, 425, - /* 370 */ 652, 652, 652, 652, 652, 652, 652, 252, 654, 422, - /* 380 */ 581, 291, 80, 652, 60, 59, 304, 442, 443, 439, - /* 390 */ 439, 62, 62, 61, 61, 61, 61, 210, 63, 63, - /* 400 */ 63, 63, 64, 64, 65, 65, 65, 66, 212, 299, - /* 410 */ 379, 585, 892, 494, 892, 306, 393, 368, 324, 654, - /* 420 */ 21, 324, 307, 324, 425, 654, 654, 654, 654, 654, - /* 430 */ 654, 654, 571, 654, 425, 432, 438, 532, 654, 654, - /* 440 */ 421, 49, 485, 421, 35, 421, 49, 329, 449, 450, - /* 450 */ 570, 582, 348, 60, 59, 304, 442, 443, 439, 439, - /* 460 */ 62, 62, 61, 61, 61, 61, 655, 63, 63, 63, - /* 470 */ 63, 64, 64, 65, 65, 65, 66, 212, 299, 420, - /* 480 */ 198, 655, 509, 419, 324, 655, 315, 655, 653, 425, - /* 490 */ 223, 316, 653, 525, 653, 238, 166, 118, 245, 350, - /* 500 */ 250, 351, 178, 314, 432, 438, 421, 34, 655, 254, - /* 510 */ 239, 213, 389, 213, 422, 653, 588, 398, 395, 406, - /* 520 */ 407, 408, 60, 59, 304, 442, 443, 439, 439, 62, - /* 530 */ 62, 61, 61, 61, 61, 335, 63, 63, 63, 63, - /* 540 */ 64, 64, 65, 65, 65, 66, 212, 299, 342, 655, - /* 550 */ 307, 257, 463, 547, 501, 655, 655, 655, 655, 655, - /* 560 */ 655, 655, 653, 655, 464, 653, 653, 653, 655, 655, - /* 570 */ 653, 161, 498, 432, 438, 653, 653, 465, 1, 502, - /* 580 */ 544, 418, 403, 585, 891, 176, 891, 343, 174, 503, - /* 590 */ 425, 60, 59, 304, 442, 443, 439, 439, 62, 62, - /* 600 */ 61, 61, 61, 61, 240, 63, 63, 63, 63, 64, - /* 610 */ 64, 65, 65, 65, 66, 212, 299, 381, 223, 422, - /* 620 */ 9, 93, 377, 582, 403, 118, 245, 350, 250, 351, - /* 630 */ 178, 177, 162, 325, 403, 183, 345, 254, 352, 355, - /* 640 */ 356, 227, 432, 438, 446, 320, 399, 404, 405, 357, - /* 650 */ 459, 209, 540, 367, 540, 425, 546, 302, 202, 299, - /* 660 */ 60, 59, 304, 442, 443, 439, 439, 62, 62, 61, - /* 670 */ 61, 61, 61, 402, 63, 63, 63, 63, 64, 64, - /* 680 */ 65, 65, 65, 66, 212, 432, 438, 225, 524, 404, - /* 690 */ 405, 489, 422, 397, 18, 824, 2, 578, 20, 404, - /* 700 */ 405, 194, 299, 60, 59, 304, 442, 443, 439, 439, - /* 710 */ 62, 62, 61, 61, 61, 61, 386, 63, 63, 63, - /* 720 */ 63, 64, 64, 65, 65, 65, 66, 212, 432, 438, - /* 730 */ 327, 370, 445, 445, 481, 422, 327, 373, 445, 445, - /* 740 */ 275, 519, 519, 8, 394, 299, 60, 70, 304, 442, - /* 750 */ 443, 439, 439, 62, 62, 61, 61, 61, 61, 378, + /* 180 */ 210, 292, 257, 524, 295, 571, 113, 408, 522, 451, + /* 190 */ 331, 317, 407, 20, 418, 340, 519, 396, 532, 531, + /* 200 */ 505, 447, 212, 570, 569, 208, 530, 425, 431, 149, + /* 210 */ 150, 397, 398, 414, 41, 211, 151, 533, 372, 489, + /* 220 */ 261, 568, 259, 420, 292, 60, 59, 297, 435, 436, + /* 230 */ 432, 432, 62, 62, 61, 61, 61, 61, 317, 63, + /* 240 */ 63, 63, 63, 64, 64, 65, 65, 65, 66, 210, + /* 250 */ 425, 431, 447, 333, 215, 422, 422, 422, 363, 418, + /* 260 */ 414, 41, 397, 398, 366, 567, 211, 292, 60, 59, + /* 270 */ 297, 435, 436, 432, 432, 62, 62, 61, 61, 61, + /* 280 */ 61, 396, 63, 63, 63, 63, 64, 64, 65, 65, + /* 290 */ 65, 66, 210, 425, 431, 491, 300, 524, 474, 66, + /* 300 */ 210, 214, 474, 229, 411, 286, 534, 20, 449, 523, + /* 310 */ 168, 60, 59, 297, 435, 436, 432, 432, 62, 62, + /* 320 */ 61, 61, 61, 61, 474, 63, 63, 63, 63, 64, + /* 330 */ 64, 65, 65, 65, 66, 210, 209, 480, 317, 77, + /* 340 */ 292, 239, 300, 55, 484, 230, 397, 398, 181, 547, + /* 350 */ 494, 345, 348, 349, 67, 152, 69, 154, 339, 524, + /* 360 */ 414, 35, 350, 241, 221, 370, 425, 431, 578, 20, + /* 370 */ 164, 118, 243, 343, 248, 344, 176, 322, 442, 443, + /* 380 */ 414, 3, 80, 252, 60, 59, 297, 435, 436, 432, + /* 390 */ 432, 62, 62, 61, 61, 61, 61, 174, 63, 63, + /* 400 */ 63, 63, 64, 64, 65, 65, 65, 66, 210, 292, + /* 410 */ 221, 550, 236, 487, 510, 353, 317, 118, 243, 343, + /* 420 */ 248, 344, 176, 181, 317, 525, 345, 348, 349, 252, + /* 430 */ 223, 415, 155, 464, 511, 425, 431, 350, 414, 34, + /* 440 */ 465, 211, 177, 175, 160, 237, 414, 34, 338, 549, + /* 450 */ 449, 323, 168, 60, 59, 297, 435, 436, 432, 432, + /* 460 */ 62, 62, 61, 61, 61, 61, 415, 63, 63, 63, + /* 470 */ 63, 64, 64, 65, 65, 65, 66, 210, 292, 542, + /* 480 */ 335, 517, 504, 541, 456, 571, 302, 19, 331, 144, + /* 490 */ 317, 390, 317, 330, 2, 362, 457, 294, 483, 373, + /* 500 */ 269, 268, 252, 570, 425, 431, 588, 391, 388, 458, + /* 510 */ 208, 495, 414, 49, 414, 49, 303, 585, 892, 159, + /* 520 */ 892, 496, 60, 59, 297, 435, 436, 432, 432, 62, + /* 530 */ 62, 61, 61, 61, 61, 201, 63, 63, 63, 63, + /* 540 */ 64, 64, 65, 65, 65, 66, 210, 292, 317, 181, + /* 550 */ 439, 255, 345, 348, 349, 370, 153, 582, 308, 251, + /* 560 */ 309, 452, 76, 350, 78, 382, 211, 426, 427, 415, + /* 570 */ 414, 27, 319, 425, 431, 440, 1, 22, 585, 891, + /* 580 */ 396, 891, 544, 478, 320, 263, 438, 438, 429, 430, + /* 590 */ 415, 60, 59, 297, 435, 436, 432, 432, 62, 62, + /* 600 */ 61, 61, 61, 61, 328, 63, 63, 63, 63, 64, + /* 610 */ 64, 65, 65, 65, 66, 210, 292, 428, 582, 374, + /* 620 */ 224, 93, 517, 9, 336, 396, 557, 396, 456, 67, + /* 630 */ 396, 69, 154, 399, 400, 401, 320, 238, 438, 438, + /* 640 */ 457, 318, 425, 431, 299, 397, 398, 320, 433, 438, + /* 650 */ 438, 581, 291, 458, 225, 327, 5, 222, 546, 292, + /* 660 */ 60, 59, 297, 435, 436, 432, 432, 62, 62, 61, + /* 670 */ 61, 61, 61, 395, 63, 63, 63, 63, 64, 64, + /* 680 */ 65, 65, 65, 66, 210, 425, 431, 482, 313, 392, + /* 690 */ 397, 398, 397, 398, 207, 397, 398, 824, 273, 517, + /* 700 */ 251, 200, 292, 60, 59, 297, 435, 436, 432, 432, + /* 710 */ 62, 62, 61, 61, 61, 61, 470, 63, 63, 63, + /* 720 */ 63, 64, 64, 65, 65, 65, 66, 210, 425, 431, + /* 730 */ 171, 160, 263, 263, 304, 415, 276, 119, 274, 263, + /* 740 */ 517, 517, 263, 517, 192, 292, 60, 70, 297, 435, + /* 750 */ 436, 432, 432, 62, 62, 61, 61, 61, 61, 379, /* 760 */ 63, 63, 63, 63, 64, 64, 65, 65, 65, 66, - /* 770 */ 212, 432, 438, 243, 211, 167, 310, 224, 278, 196, - /* 780 */ 276, 55, 374, 519, 180, 181, 182, 519, 299, 119, - /* 790 */ 59, 304, 442, 443, 439, 439, 62, 62, 61, 61, - /* 800 */ 61, 61, 646, 63, 63, 63, 63, 64, 64, 65, - /* 810 */ 65, 65, 66, 212, 432, 438, 403, 646, 311, 253, - /* 820 */ 253, 646, 312, 646, 327, 5, 445, 445, 481, 542, - /* 830 */ 154, 519, 474, 541, 304, 442, 443, 439, 439, 62, - /* 840 */ 62, 61, 61, 61, 61, 369, 63, 63, 63, 63, - /* 850 */ 64, 64, 65, 65, 65, 66, 212, 72, 331, 277, - /* 860 */ 4, 253, 377, 428, 303, 253, 313, 487, 622, 173, - /* 870 */ 162, 455, 328, 72, 331, 265, 4, 265, 84, 158, - /* 880 */ 303, 404, 405, 265, 67, 646, 69, 156, 328, 333, - /* 890 */ 360, 646, 646, 646, 646, 646, 646, 646, 183, 458, - /* 900 */ 185, 352, 355, 356, 646, 333, 388, 477, 188, 253, - /* 910 */ 433, 434, 357, 422, 463, 458, 557, 179, 559, 75, - /* 920 */ 74, 336, 403, 147, 560, 210, 464, 226, 73, 322, - /* 930 */ 323, 436, 437, 427, 422, 75, 74, 488, 387, 465, - /* 940 */ 475, 334, 422, 512, 73, 322, 323, 72, 331, 427, - /* 950 */ 4, 210, 467, 324, 303, 318, 123, 19, 480, 144, - /* 960 */ 435, 157, 328, 513, 484, 429, 429, 429, 430, 431, - /* 970 */ 11, 346, 301, 452, 452, 421, 34, 254, 324, 333, - /* 980 */ 251, 429, 429, 429, 430, 431, 11, 404, 405, 458, - /* 990 */ 265, 164, 293, 421, 3, 422, 228, 229, 230, 104, - /* 1000 */ 421, 28, 324, 403, 294, 324, 265, 265, 265, 75, - /* 1010 */ 74, 656, 207, 478, 283, 309, 179, 338, 73, 322, - /* 1020 */ 323, 284, 337, 427, 421, 23, 656, 421, 32, 324, - /* 1030 */ 656, 561, 656, 205, 420, 549, 326, 526, 419, 204, - /* 1040 */ 324, 128, 206, 324, 476, 511, 510, 279, 385, 281, - /* 1050 */ 514, 421, 53, 656, 515, 429, 429, 429, 430, 431, - /* 1060 */ 11, 649, 421, 52, 258, 421, 98, 324, 404, 405, - /* 1070 */ 183, 301, 260, 352, 355, 356, 649, 76, 650, 78, - /* 1080 */ 649, 246, 649, 262, 357, 384, 280, 270, 264, 421, - /* 1090 */ 96, 300, 247, 650, 656, 324, 210, 650, 191, 650, - /* 1100 */ 656, 656, 656, 656, 656, 656, 656, 653, 656, 324, - /* 1110 */ 364, 160, 440, 656, 656, 324, 295, 421, 101, 324, - /* 1120 */ 390, 583, 653, 324, 269, 324, 653, 447, 653, 22, - /* 1130 */ 372, 421, 102, 412, 375, 324, 476, 421, 112, 376, - /* 1140 */ 272, 421, 114, 324, 649, 421, 16, 421, 99, 653, - /* 1150 */ 649, 649, 649, 649, 649, 649, 649, 421, 33, 324, - /* 1160 */ 584, 650, 324, 649, 273, 421, 97, 650, 650, 650, - /* 1170 */ 650, 650, 650, 650, 483, 274, 175, 506, 507, 556, - /* 1180 */ 650, 421, 24, 324, 421, 54, 566, 516, 324, 128, - /* 1190 */ 653, 324, 256, 359, 128, 128, 653, 653, 653, 653, - /* 1200 */ 653, 653, 653, 324, 653, 421, 115, 146, 324, 653, - /* 1210 */ 421, 116, 282, 421, 117, 324, 545, 324, 128, 285, - /* 1220 */ 553, 324, 175, 324, 233, 421, 25, 554, 324, 91, - /* 1230 */ 421, 36, 324, 286, 324, 577, 426, 421, 37, 421, - /* 1240 */ 26, 324, 451, 421, 38, 421, 39, 324, 332, 324, - /* 1250 */ 421, 40, 324, 453, 421, 42, 421, 43, 564, 292, - /* 1260 */ 91, 324, 470, 421, 44, 324, 580, 324, 290, 421, - /* 1270 */ 29, 421, 30, 324, 421, 45, 324, 518, 298, 324, - /* 1280 */ 473, 248, 517, 421, 46, 324, 354, 421, 47, 421, - /* 1290 */ 48, 520, 552, 563, 165, 421, 31, 401, 421, 10, - /* 1300 */ 7, 421, 50, 409, 410, 411, 321, 421, 51, 84, - /* 1310 */ 423, 341, 237, 83, 339, 57, 234, 79, 235, 215, - /* 1320 */ 236, 172, 85, 424, 349, 344, 468, 125, 505, 308, - /* 1330 */ 295, 242, 499, 482, 244, 504, 486, 249, 508, 296, - /* 1340 */ 105, 221, 521, 149, 361, 150, 365, 527, 528, 529, - /* 1350 */ 186, 88, 121, 535, 187, 132, 363, 189, 142, 220, - /* 1360 */ 222, 383, 141, 190, 537, 192, 548, 371, 195, 267, - /* 1370 */ 382, 538, 133, 555, 562, 317, 134, 135, 136, 92, - /* 1380 */ 574, 138, 95, 575, 576, 579, 111, 100, 400, 319, - /* 1390 */ 122, 17, 413, 623, 624, 168, 169, 441, 444, 71, - /* 1400 */ 460, 448, 457, 143, 159, 171, 461, 6, 462, 479, - /* 1410 */ 466, 13, 126, 81, 12, 127, 163, 492, 493, 218, - /* 1420 */ 86, 353, 106, 358, 255, 107, 120, 87, 108, 184, - /* 1430 */ 247, 362, 145, 536, 175, 129, 366, 193, 109, 268, - /* 1440 */ 289, 551, 131, 14, 130, 197, 89, 539, 199, 201, - /* 1450 */ 543, 200, 139, 558, 137, 565, 110, 15, 287, 572, - /* 1460 */ 140, 208, 148, 396, 392, 586, 902, 902, 902, 902, - /* 1470 */ 90, + /* 770 */ 210, 425, 431, 384, 559, 305, 306, 251, 415, 320, + /* 780 */ 560, 438, 438, 561, 540, 360, 540, 387, 292, 196, + /* 790 */ 59, 297, 435, 436, 432, 432, 62, 62, 61, 61, + /* 800 */ 61, 61, 371, 63, 63, 63, 63, 64, 64, 65, + /* 810 */ 65, 65, 66, 210, 425, 431, 396, 275, 251, 251, + /* 820 */ 172, 250, 418, 415, 386, 367, 178, 179, 180, 469, + /* 830 */ 311, 123, 156, 128, 297, 435, 436, 432, 432, 62, + /* 840 */ 62, 61, 61, 61, 61, 317, 63, 63, 63, 63, + /* 850 */ 64, 64, 65, 65, 65, 66, 210, 72, 324, 177, + /* 860 */ 4, 317, 263, 317, 296, 263, 415, 414, 28, 317, + /* 870 */ 263, 317, 321, 72, 324, 317, 4, 421, 445, 445, + /* 880 */ 296, 397, 398, 414, 23, 414, 32, 418, 321, 326, + /* 890 */ 329, 414, 53, 414, 52, 317, 158, 414, 98, 451, + /* 900 */ 317, 194, 317, 277, 317, 326, 378, 471, 502, 317, + /* 910 */ 478, 279, 478, 165, 294, 451, 317, 414, 96, 75, + /* 920 */ 74, 469, 414, 101, 414, 102, 414, 112, 73, 315, + /* 930 */ 316, 414, 114, 420, 448, 75, 74, 481, 414, 16, + /* 940 */ 381, 317, 183, 467, 73, 315, 316, 72, 324, 420, + /* 950 */ 4, 208, 317, 186, 296, 317, 499, 500, 476, 208, + /* 960 */ 173, 341, 321, 414, 99, 422, 422, 422, 423, 424, + /* 970 */ 11, 361, 380, 307, 414, 33, 415, 414, 97, 326, + /* 980 */ 460, 422, 422, 422, 423, 424, 11, 415, 413, 451, + /* 990 */ 413, 162, 412, 317, 412, 468, 226, 227, 228, 104, + /* 1000 */ 84, 473, 317, 509, 508, 317, 622, 477, 317, 75, + /* 1010 */ 74, 249, 205, 21, 281, 414, 24, 418, 73, 315, + /* 1020 */ 316, 282, 317, 420, 414, 54, 507, 414, 115, 317, + /* 1030 */ 414, 116, 506, 203, 147, 549, 244, 512, 526, 202, + /* 1040 */ 317, 513, 204, 317, 414, 117, 317, 245, 317, 18, + /* 1050 */ 317, 414, 25, 317, 256, 422, 422, 422, 423, 424, + /* 1060 */ 11, 258, 414, 36, 260, 414, 37, 317, 414, 26, + /* 1070 */ 414, 38, 414, 39, 262, 414, 40, 317, 514, 317, + /* 1080 */ 128, 317, 418, 317, 189, 377, 278, 268, 267, 414, + /* 1090 */ 42, 293, 317, 254, 317, 128, 208, 365, 8, 414, + /* 1100 */ 43, 414, 44, 414, 29, 414, 30, 352, 368, 128, + /* 1110 */ 317, 545, 317, 128, 414, 45, 414, 46, 317, 583, + /* 1120 */ 383, 553, 317, 173, 554, 317, 91, 317, 564, 369, + /* 1130 */ 91, 357, 414, 47, 414, 48, 580, 270, 290, 271, + /* 1140 */ 414, 31, 272, 556, 414, 10, 566, 414, 50, 414, + /* 1150 */ 51, 280, 283, 284, 577, 146, 463, 405, 584, 231, + /* 1160 */ 325, 419, 444, 466, 446, 246, 505, 552, 563, 515, + /* 1170 */ 516, 520, 163, 518, 394, 347, 7, 402, 403, 404, + /* 1180 */ 314, 84, 232, 334, 332, 83, 79, 416, 170, 57, + /* 1190 */ 213, 461, 125, 85, 337, 342, 492, 301, 233, 498, + /* 1200 */ 497, 105, 502, 219, 354, 247, 521, 234, 501, 235, + /* 1210 */ 287, 417, 503, 218, 527, 528, 529, 358, 240, 535, + /* 1220 */ 475, 242, 288, 479, 356, 184, 185, 121, 187, 132, + /* 1230 */ 188, 548, 537, 88, 190, 193, 364, 142, 375, 376, + /* 1240 */ 555, 133, 220, 562, 134, 310, 135, 138, 136, 574, + /* 1250 */ 575, 141, 576, 265, 579, 100, 538, 217, 393, 92, + /* 1260 */ 103, 95, 406, 623, 624, 166, 434, 167, 437, 71, + /* 1270 */ 453, 441, 450, 17, 143, 157, 169, 6, 111, 13, + /* 1280 */ 454, 455, 459, 472, 126, 81, 12, 127, 161, 485, + /* 1290 */ 486, 216, 86, 122, 106, 182, 253, 346, 312, 107, + /* 1300 */ 120, 87, 351, 108, 245, 355, 145, 536, 359, 129, + /* 1310 */ 173, 266, 191, 109, 289, 551, 130, 539, 195, 543, + /* 1320 */ 131, 14, 197, 199, 198, 558, 137, 139, 140, 110, + /* 1330 */ 15, 285, 572, 206, 389, 565, 385, 148, 586, 902, + /* 1340 */ 902, 902, 902, 902, 902, 89, 90, }; static const YYCODETYPE yy_lookahead[] = { /* 0 */ 16, 139, 140, 141, 168, 21, 144, 23, 69, 70, @@ -68245,146 +71064,133 @@ static const YYCODETYPE yy_lookahead[] = { /* 120 */ 67, 68, 69, 70, 71, 72, 147, 74, 75, 76, /* 130 */ 77, 78, 79, 80, 81, 82, 83, 84, 42, 43, /* 140 */ 44, 80, 81, 82, 83, 84, 23, 84, 169, 170, - /* 150 */ 19, 164, 165, 166, 23, 161, 60, 61, 62, 63, + /* 150 */ 19, 164, 165, 166, 23, 169, 60, 61, 62, 63, /* 160 */ 64, 65, 66, 67, 68, 69, 70, 71, 72, 169, /* 170 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, - /* 180 */ 84, 16, 14, 147, 155, 147, 21, 167, 168, 58, - /* 190 */ 211, 147, 156, 157, 200, 216, 176, 23, 27, 176, - /* 200 */ 177, 78, 79, 165, 166, 34, 183, 42, 43, 78, - /* 210 */ 79, 88, 89, 169, 170, 228, 180, 181, 189, 88, + /* 180 */ 84, 16, 14, 147, 150, 147, 21, 167, 168, 58, + /* 190 */ 211, 147, 156, 157, 23, 216, 176, 23, 181, 176, + /* 200 */ 177, 78, 79, 165, 166, 110, 183, 42, 43, 78, + /* 210 */ 79, 88, 89, 169, 170, 228, 180, 181, 123, 88, /* 220 */ 52, 98, 54, 92, 16, 60, 61, 62, 63, 64, /* 230 */ 65, 66, 67, 68, 69, 70, 71, 72, 147, 74, /* 240 */ 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, - /* 250 */ 42, 43, 78, 209, 210, 124, 125, 126, 175, 11, - /* 260 */ 169, 170, 88, 89, 20, 227, 228, 16, 60, 61, + /* 250 */ 42, 43, 78, 209, 210, 124, 125, 126, 224, 88, + /* 260 */ 169, 170, 88, 89, 230, 227, 228, 16, 60, 61, /* 270 */ 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, - /* 280 */ 72, 1, 74, 75, 76, 77, 78, 79, 80, 81, - /* 290 */ 82, 83, 84, 42, 43, 175, 16, 49, 147, 147, - /* 300 */ 20, 210, 22, 217, 153, 219, 220, 156, 157, 83, - /* 310 */ 84, 60, 61, 62, 63, 64, 65, 66, 67, 68, - /* 320 */ 69, 70, 71, 72, 147, 74, 75, 76, 77, 78, - /* 330 */ 79, 80, 81, 82, 83, 84, 1, 147, 158, 131, - /* 340 */ 16, 161, 162, 163, 20, 155, 190, 99, 100, 101, - /* 350 */ 106, 16, 108, 109, 147, 20, 147, 22, 110, 169, - /* 360 */ 170, 161, 162, 163, 84, 188, 42, 43, 169, 23, - /* 370 */ 90, 91, 92, 93, 94, 95, 96, 225, 43, 189, - /* 380 */ 244, 245, 131, 103, 60, 61, 62, 63, 64, 65, - /* 390 */ 66, 67, 68, 69, 70, 71, 72, 110, 74, 75, + /* 280 */ 72, 23, 74, 75, 76, 77, 78, 79, 80, 81, + /* 290 */ 82, 83, 84, 42, 43, 160, 16, 147, 161, 83, + /* 300 */ 84, 210, 161, 153, 169, 158, 156, 157, 161, 162, + /* 310 */ 163, 60, 61, 62, 63, 64, 65, 66, 67, 68, + /* 320 */ 69, 70, 71, 72, 161, 74, 75, 76, 77, 78, + /* 330 */ 79, 80, 81, 82, 83, 84, 192, 200, 147, 131, + /* 340 */ 16, 200, 16, 199, 20, 190, 88, 89, 90, 185, + /* 350 */ 186, 93, 94, 95, 217, 22, 219, 220, 147, 147, + /* 360 */ 169, 170, 104, 200, 84, 147, 42, 43, 156, 157, + /* 370 */ 90, 91, 92, 93, 94, 95, 96, 164, 165, 166, + /* 380 */ 169, 170, 131, 103, 60, 61, 62, 63, 64, 65, + /* 390 */ 66, 67, 68, 69, 70, 71, 72, 155, 74, 75, /* 400 */ 76, 77, 78, 79, 80, 81, 82, 83, 84, 16, - /* 410 */ 123, 19, 20, 20, 22, 208, 239, 208, 147, 84, - /* 420 */ 19, 147, 16, 147, 23, 90, 91, 92, 93, 94, - /* 430 */ 95, 96, 147, 98, 88, 42, 43, 181, 103, 104, - /* 440 */ 169, 170, 147, 169, 170, 169, 170, 164, 165, 166, - /* 450 */ 165, 59, 80, 60, 61, 62, 63, 64, 65, 66, - /* 460 */ 67, 68, 69, 70, 71, 72, 1, 74, 75, 76, - /* 470 */ 77, 78, 79, 80, 81, 82, 83, 84, 16, 107, - /* 480 */ 155, 16, 20, 111, 147, 20, 215, 22, 16, 88, - /* 490 */ 84, 215, 20, 181, 22, 221, 90, 91, 92, 93, - /* 500 */ 94, 95, 96, 208, 42, 43, 169, 170, 43, 103, - /* 510 */ 147, 228, 227, 228, 189, 43, 0, 1, 2, 7, - /* 520 */ 8, 9, 60, 61, 62, 63, 64, 65, 66, 67, - /* 530 */ 68, 69, 70, 71, 72, 186, 74, 75, 76, 77, - /* 540 */ 78, 79, 80, 81, 82, 83, 84, 16, 211, 84, - /* 550 */ 16, 20, 12, 185, 186, 90, 91, 92, 93, 94, - /* 560 */ 95, 96, 90, 98, 24, 93, 94, 95, 103, 104, - /* 570 */ 98, 147, 160, 42, 43, 103, 104, 37, 19, 39, - /* 580 */ 18, 169, 23, 19, 20, 155, 22, 147, 21, 49, - /* 590 */ 23, 60, 61, 62, 63, 64, 65, 66, 67, 68, - /* 600 */ 69, 70, 71, 72, 147, 74, 75, 76, 77, 78, - /* 610 */ 79, 80, 81, 82, 83, 84, 16, 55, 84, 189, - /* 620 */ 19, 21, 147, 59, 23, 91, 92, 93, 94, 95, - /* 630 */ 96, 201, 202, 147, 23, 90, 206, 103, 93, 94, - /* 640 */ 95, 145, 42, 43, 20, 142, 143, 88, 89, 104, - /* 650 */ 20, 148, 99, 100, 101, 88, 94, 150, 155, 16, + /* 410 */ 84, 11, 221, 20, 30, 16, 147, 91, 92, 93, + /* 420 */ 94, 95, 96, 90, 147, 181, 93, 94, 95, 103, + /* 430 */ 212, 189, 155, 27, 50, 42, 43, 104, 169, 170, + /* 440 */ 34, 228, 43, 201, 202, 147, 169, 170, 206, 49, + /* 450 */ 161, 162, 163, 60, 61, 62, 63, 64, 65, 66, + /* 460 */ 67, 68, 69, 70, 71, 72, 189, 74, 75, 76, + /* 470 */ 77, 78, 79, 80, 81, 82, 83, 84, 16, 25, + /* 480 */ 211, 147, 20, 29, 12, 147, 102, 19, 211, 21, + /* 490 */ 147, 141, 147, 216, 144, 41, 24, 98, 20, 99, + /* 500 */ 100, 101, 103, 165, 42, 43, 0, 1, 2, 37, + /* 510 */ 110, 39, 169, 170, 169, 170, 182, 19, 20, 147, + /* 520 */ 22, 49, 60, 61, 62, 63, 64, 65, 66, 67, + /* 530 */ 68, 69, 70, 71, 72, 155, 74, 75, 76, 77, + /* 540 */ 78, 79, 80, 81, 82, 83, 84, 16, 147, 90, + /* 550 */ 20, 20, 93, 94, 95, 147, 155, 59, 215, 225, + /* 560 */ 215, 20, 130, 104, 132, 227, 228, 42, 43, 189, + /* 570 */ 169, 170, 16, 42, 43, 20, 19, 22, 19, 20, + /* 580 */ 23, 22, 18, 147, 106, 147, 108, 109, 63, 64, + /* 590 */ 189, 60, 61, 62, 63, 64, 65, 66, 67, 68, + /* 600 */ 69, 70, 71, 72, 186, 74, 75, 76, 77, 78, + /* 610 */ 79, 80, 81, 82, 83, 84, 16, 92, 59, 55, + /* 620 */ 212, 21, 147, 19, 147, 23, 188, 23, 12, 217, + /* 630 */ 23, 219, 220, 7, 8, 9, 106, 147, 108, 109, + /* 640 */ 24, 147, 42, 43, 208, 88, 89, 106, 92, 108, + /* 650 */ 109, 244, 245, 37, 145, 39, 191, 182, 94, 16, /* 660 */ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, /* 670 */ 70, 71, 72, 147, 74, 75, 76, 77, 78, 79, - /* 680 */ 80, 81, 82, 83, 84, 42, 43, 212, 147, 88, - /* 690 */ 89, 80, 189, 141, 19, 133, 144, 156, 157, 88, - /* 700 */ 89, 155, 16, 60, 61, 62, 63, 64, 65, 66, - /* 710 */ 67, 68, 69, 70, 71, 72, 213, 74, 75, 76, + /* 680 */ 80, 81, 82, 83, 84, 42, 43, 80, 142, 143, + /* 690 */ 88, 89, 88, 89, 148, 88, 89, 133, 14, 147, + /* 700 */ 225, 155, 16, 60, 61, 62, 63, 64, 65, 66, + /* 710 */ 67, 68, 69, 70, 71, 72, 114, 74, 75, 76, /* 720 */ 77, 78, 79, 80, 81, 82, 83, 84, 42, 43, - /* 730 */ 106, 224, 108, 109, 161, 189, 106, 230, 108, 109, - /* 740 */ 14, 147, 147, 68, 241, 16, 60, 61, 62, 63, + /* 730 */ 201, 202, 147, 147, 182, 189, 52, 147, 54, 147, + /* 740 */ 147, 147, 147, 147, 155, 16, 60, 61, 62, 63, /* 750 */ 64, 65, 66, 67, 68, 69, 70, 71, 72, 213, /* 760 */ 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, - /* 770 */ 84, 42, 43, 200, 192, 19, 182, 182, 52, 22, - /* 780 */ 54, 199, 236, 147, 99, 100, 101, 147, 16, 147, + /* 770 */ 84, 42, 43, 188, 188, 182, 182, 225, 189, 106, + /* 780 */ 188, 108, 109, 188, 99, 100, 101, 241, 16, 155, /* 790 */ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, - /* 800 */ 71, 72, 1, 74, 75, 76, 77, 78, 79, 80, - /* 810 */ 81, 82, 83, 84, 42, 43, 23, 16, 182, 225, - /* 820 */ 225, 20, 182, 22, 106, 191, 108, 109, 161, 25, - /* 830 */ 22, 147, 22, 29, 62, 63, 64, 65, 66, 67, - /* 840 */ 68, 69, 70, 71, 72, 41, 74, 75, 76, 77, - /* 850 */ 78, 79, 80, 81, 82, 83, 84, 16, 17, 133, - /* 860 */ 19, 225, 147, 147, 23, 225, 182, 200, 112, 201, - /* 870 */ 202, 161, 31, 16, 17, 147, 19, 147, 121, 155, - /* 880 */ 23, 88, 89, 147, 217, 84, 219, 220, 31, 48, - /* 890 */ 16, 90, 91, 92, 93, 94, 95, 96, 90, 58, - /* 900 */ 155, 93, 94, 95, 103, 48, 91, 114, 155, 225, - /* 910 */ 42, 43, 104, 189, 12, 58, 188, 43, 188, 78, - /* 920 */ 79, 147, 23, 113, 188, 110, 24, 212, 87, 88, - /* 930 */ 89, 63, 64, 92, 189, 78, 79, 80, 123, 37, - /* 940 */ 203, 39, 189, 30, 87, 88, 89, 16, 17, 92, - /* 950 */ 19, 110, 147, 147, 23, 242, 243, 19, 147, 21, - /* 960 */ 92, 155, 31, 50, 147, 124, 125, 126, 127, 128, - /* 970 */ 129, 147, 98, 124, 125, 169, 170, 103, 147, 48, - /* 980 */ 147, 124, 125, 126, 127, 128, 129, 88, 89, 58, - /* 990 */ 147, 5, 147, 169, 170, 189, 10, 11, 12, 13, - /* 1000 */ 169, 170, 147, 23, 178, 147, 147, 147, 147, 78, - /* 1010 */ 79, 1, 26, 114, 28, 102, 43, 211, 87, 88, - /* 1020 */ 89, 35, 216, 92, 169, 170, 16, 169, 170, 147, - /* 1030 */ 20, 188, 22, 47, 107, 49, 16, 147, 111, 53, - /* 1040 */ 147, 22, 56, 147, 22, 91, 92, 188, 188, 188, - /* 1050 */ 178, 169, 170, 43, 178, 124, 125, 126, 127, 128, - /* 1060 */ 129, 1, 169, 170, 147, 169, 170, 147, 88, 89, - /* 1070 */ 90, 98, 147, 93, 94, 95, 16, 130, 1, 132, - /* 1080 */ 20, 92, 22, 147, 104, 99, 100, 101, 147, 169, - /* 1090 */ 170, 105, 103, 16, 84, 147, 110, 20, 232, 22, - /* 1100 */ 90, 91, 92, 93, 94, 95, 96, 1, 98, 147, - /* 1110 */ 233, 89, 92, 103, 104, 147, 97, 169, 170, 147, - /* 1120 */ 134, 20, 16, 147, 147, 147, 20, 20, 22, 22, - /* 1130 */ 147, 169, 170, 149, 147, 147, 114, 169, 170, 147, - /* 1140 */ 147, 169, 170, 147, 84, 169, 170, 169, 170, 43, - /* 1150 */ 90, 91, 92, 93, 94, 95, 96, 169, 170, 147, - /* 1160 */ 59, 84, 147, 103, 147, 169, 170, 90, 91, 92, - /* 1170 */ 93, 94, 95, 96, 20, 147, 22, 7, 8, 147, - /* 1180 */ 103, 169, 170, 147, 169, 170, 147, 20, 147, 22, - /* 1190 */ 84, 147, 20, 20, 22, 22, 90, 91, 92, 93, - /* 1200 */ 94, 95, 96, 147, 98, 169, 170, 191, 147, 103, - /* 1210 */ 169, 170, 147, 169, 170, 147, 20, 147, 22, 147, - /* 1220 */ 20, 147, 22, 147, 193, 169, 170, 20, 147, 22, - /* 1230 */ 169, 170, 147, 147, 147, 147, 161, 169, 170, 169, - /* 1240 */ 170, 147, 229, 169, 170, 169, 170, 147, 223, 147, - /* 1250 */ 169, 170, 147, 229, 169, 170, 169, 170, 20, 177, - /* 1260 */ 22, 147, 172, 169, 170, 147, 20, 147, 22, 169, - /* 1270 */ 170, 169, 170, 147, 169, 170, 147, 161, 161, 147, - /* 1280 */ 172, 172, 172, 169, 170, 147, 173, 169, 170, 169, - /* 1290 */ 170, 172, 194, 194, 6, 169, 170, 146, 169, 170, - /* 1300 */ 22, 169, 170, 146, 146, 146, 154, 169, 170, 121, - /* 1310 */ 189, 118, 197, 119, 116, 120, 194, 130, 195, 222, - /* 1320 */ 196, 112, 98, 198, 98, 115, 152, 152, 179, 40, - /* 1330 */ 97, 204, 171, 205, 204, 171, 205, 171, 173, 171, - /* 1340 */ 19, 84, 179, 174, 15, 174, 38, 171, 171, 171, - /* 1350 */ 151, 130, 60, 152, 151, 19, 152, 151, 214, 226, - /* 1360 */ 226, 15, 214, 152, 152, 151, 184, 152, 184, 234, - /* 1370 */ 152, 235, 187, 194, 194, 152, 187, 187, 187, 237, - /* 1380 */ 33, 184, 237, 152, 152, 137, 240, 159, 1, 246, - /* 1390 */ 243, 231, 20, 112, 112, 112, 112, 92, 107, 19, - /* 1400 */ 11, 20, 20, 19, 19, 22, 20, 117, 20, 114, - /* 1410 */ 20, 117, 19, 22, 22, 20, 112, 20, 20, 44, - /* 1420 */ 19, 44, 19, 44, 20, 19, 32, 19, 19, 96, - /* 1430 */ 103, 16, 21, 17, 22, 98, 36, 98, 19, 133, - /* 1440 */ 5, 1, 102, 19, 45, 122, 68, 51, 113, 115, - /* 1450 */ 45, 14, 102, 17, 113, 123, 14, 19, 136, 20, - /* 1460 */ 122, 135, 19, 3, 57, 4, 247, 247, 247, 247, - /* 1470 */ 68, + /* 800 */ 71, 72, 213, 74, 75, 76, 77, 78, 79, 80, + /* 810 */ 81, 82, 83, 84, 42, 43, 23, 133, 225, 225, + /* 820 */ 21, 225, 23, 189, 239, 236, 99, 100, 101, 22, + /* 830 */ 242, 243, 155, 22, 62, 63, 64, 65, 66, 67, + /* 840 */ 68, 69, 70, 71, 72, 147, 74, 75, 76, 77, + /* 850 */ 78, 79, 80, 81, 82, 83, 84, 16, 17, 43, + /* 860 */ 19, 147, 147, 147, 23, 147, 189, 169, 170, 147, + /* 870 */ 147, 147, 31, 16, 17, 147, 19, 147, 124, 125, + /* 880 */ 23, 88, 89, 169, 170, 169, 170, 88, 31, 48, + /* 890 */ 147, 169, 170, 169, 170, 147, 89, 169, 170, 58, + /* 900 */ 147, 22, 147, 188, 147, 48, 188, 114, 97, 147, + /* 910 */ 147, 188, 147, 19, 98, 58, 147, 169, 170, 78, + /* 920 */ 79, 114, 169, 170, 169, 170, 169, 170, 87, 88, + /* 930 */ 89, 169, 170, 92, 161, 78, 79, 80, 169, 170, + /* 940 */ 91, 147, 155, 22, 87, 88, 89, 16, 17, 92, + /* 950 */ 19, 110, 147, 155, 23, 147, 7, 8, 20, 110, + /* 960 */ 22, 80, 31, 169, 170, 124, 125, 126, 127, 128, + /* 970 */ 129, 208, 123, 208, 169, 170, 189, 169, 170, 48, + /* 980 */ 147, 124, 125, 126, 127, 128, 129, 189, 107, 58, + /* 990 */ 107, 5, 111, 147, 111, 203, 10, 11, 12, 13, + /* 1000 */ 121, 147, 147, 91, 92, 147, 112, 147, 147, 78, + /* 1010 */ 79, 147, 26, 19, 28, 169, 170, 23, 87, 88, + /* 1020 */ 89, 35, 147, 92, 169, 170, 178, 169, 170, 147, + /* 1030 */ 169, 170, 147, 47, 113, 49, 92, 178, 147, 53, + /* 1040 */ 147, 178, 56, 147, 169, 170, 147, 103, 147, 19, + /* 1050 */ 147, 169, 170, 147, 147, 124, 125, 126, 127, 128, + /* 1060 */ 129, 147, 169, 170, 147, 169, 170, 147, 169, 170, + /* 1070 */ 169, 170, 169, 170, 147, 169, 170, 147, 20, 147, + /* 1080 */ 22, 147, 88, 147, 232, 99, 100, 101, 147, 169, + /* 1090 */ 170, 105, 147, 20, 147, 22, 110, 147, 68, 169, + /* 1100 */ 170, 169, 170, 169, 170, 169, 170, 20, 147, 22, + /* 1110 */ 147, 20, 147, 22, 169, 170, 169, 170, 147, 20, + /* 1120 */ 134, 20, 147, 22, 20, 147, 22, 147, 20, 147, + /* 1130 */ 22, 233, 169, 170, 169, 170, 20, 147, 22, 147, + /* 1140 */ 169, 170, 147, 147, 169, 170, 147, 169, 170, 169, + /* 1150 */ 170, 147, 147, 147, 147, 191, 172, 149, 59, 193, + /* 1160 */ 223, 161, 229, 172, 229, 172, 177, 194, 194, 172, + /* 1170 */ 161, 161, 6, 172, 146, 173, 22, 146, 146, 146, + /* 1180 */ 154, 121, 194, 118, 116, 119, 130, 189, 112, 120, + /* 1190 */ 222, 152, 152, 98, 115, 98, 171, 40, 195, 179, + /* 1200 */ 171, 19, 97, 84, 15, 171, 179, 196, 173, 197, + /* 1210 */ 174, 198, 171, 226, 171, 171, 171, 38, 204, 152, + /* 1220 */ 205, 204, 174, 205, 152, 151, 151, 60, 151, 19, + /* 1230 */ 152, 184, 152, 130, 151, 184, 152, 214, 152, 15, + /* 1240 */ 194, 187, 226, 194, 187, 152, 187, 184, 187, 33, + /* 1250 */ 152, 214, 152, 234, 137, 159, 235, 175, 1, 237, + /* 1260 */ 175, 237, 20, 112, 112, 112, 92, 112, 107, 19, + /* 1270 */ 11, 20, 20, 231, 19, 19, 22, 117, 240, 117, + /* 1280 */ 20, 20, 20, 114, 19, 22, 22, 20, 112, 20, + /* 1290 */ 20, 44, 19, 243, 19, 96, 20, 44, 246, 19, + /* 1300 */ 32, 19, 44, 19, 103, 16, 21, 17, 36, 98, + /* 1310 */ 22, 133, 98, 19, 5, 1, 45, 51, 122, 45, + /* 1320 */ 102, 19, 113, 115, 14, 17, 113, 102, 122, 14, + /* 1330 */ 19, 136, 20, 135, 3, 123, 57, 19, 4, 247, + /* 1340 */ 247, 247, 247, 247, 247, 68, 68, }; #define YY_SHIFT_USE_DFLT (-62) -#define YY_SHIFT_MAX 396 +#define YY_SHIFT_MAX 389 static const short yy_shift_ofst[] = { - /* 0 */ 39, 841, 986, -16, 841, 931, 931, 980, 123, -36, - /* 10 */ 96, 931, 931, 931, 931, 931, -45, 248, 174, 19, - /* 20 */ 346, -54, -54, 53, 165, 208, 251, 324, 393, 462, + /* 0 */ 39, 841, 986, -16, 841, 931, 931, 258, 123, -36, + /* 10 */ 96, 931, 931, 931, 931, 931, -45, 400, 174, 19, + /* 20 */ 171, -54, -54, 53, 165, 208, 251, 324, 393, 462, /* 30 */ 531, 600, 643, 686, 643, 643, 643, 643, 643, 643, /* 40 */ 643, 643, 643, 643, 643, 643, 643, 643, 643, 643, /* 50 */ 643, 643, 729, 772, 772, 857, 931, 931, 931, 931, @@ -68392,70 +71198,69 @@ static const short yy_shift_ofst[] = { /* 70 */ 931, 931, 931, 931, 931, 931, 931, 931, 931, 931, /* 80 */ 931, 931, 931, 931, 931, 931, 931, 931, 931, 931, /* 90 */ 931, 931, 931, 931, 931, 931, -61, -61, 6, 6, - /* 100 */ 406, 22, 61, 874, 562, 19, 19, 19, 19, 19, - /* 110 */ 19, 19, 226, 346, 63, -62, -62, -62, 131, 534, - /* 120 */ 540, 540, 392, 564, 516, 567, 19, 567, 19, 19, + /* 100 */ 280, 22, 61, 399, 564, 19, 19, 19, 19, 19, + /* 110 */ 19, 19, 216, 171, 63, -62, -62, -62, 131, 326, + /* 120 */ 472, 472, 498, 559, 506, 799, 19, 799, 19, 19, /* 130 */ 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, - /* 140 */ 19, 815, 287, -36, -36, -36, -62, -62, -62, 1106, - /* 150 */ 472, -15, -15, 808, 545, 244, 559, 624, 630, 902, - /* 160 */ 793, 899, 601, 611, 512, 19, 19, 372, 19, 19, - /* 170 */ 401, 19, 19, 1022, 19, 19, 718, 1022, 19, 19, - /* 180 */ 913, 913, 913, 19, 19, 718, 19, 19, 718, 19, - /* 190 */ 804, 553, 19, 19, 718, 19, 19, 19, 718, 19, - /* 200 */ 19, 19, 718, 718, 19, 19, 19, 19, 19, 938, - /* 210 */ 927, 810, 346, 849, 849, 947, 171, 171, 171, 973, - /* 220 */ 171, 346, 171, 346, 1019, 757, 757, 1288, 1288, 1288, - /* 230 */ 1288, 1278, -36, 1188, 1193, 1194, 1198, 1195, 1187, 1209, - /* 240 */ 1209, 1224, 1210, 1224, 1210, 1226, 1226, 1289, 1226, 1233, - /* 250 */ 1226, 1321, 1257, 1257, 1289, 1226, 1226, 1226, 1321, 1329, - /* 260 */ 1209, 1329, 1209, 1329, 1209, 1209, 1308, 1221, 1329, 1209, - /* 270 */ 1292, 1292, 1336, 1188, 1209, 1346, 1346, 1346, 1346, 1188, - /* 280 */ 1292, 1336, 1209, 1347, 1347, 1209, 1209, 1248, -62, -62, - /* 290 */ -62, -62, 335, 465, 1010, 280, 801, 1060, 1077, 868, - /* 300 */ 726, 685, 168, 756, 1020, 1107, 1154, 989, 1170, 954, - /* 310 */ 1167, 1172, 1173, 1196, 1200, 1207, 1238, 675, 1246, 1101, - /* 320 */ 1387, 1372, 1281, 1282, 1283, 1284, 1305, 1291, 1380, 1381, - /* 330 */ 1382, 1384, 1389, 1385, 1386, 1383, 1388, 1390, 1391, 1290, - /* 340 */ 1392, 1294, 1391, 1295, 1393, 1395, 1304, 1397, 1398, 1394, - /* 350 */ 1375, 1401, 1377, 1403, 1404, 1406, 1408, 1379, 1409, 1333, - /* 360 */ 1327, 1415, 1416, 1411, 1337, 1400, 1396, 1399, 1412, 1405, - /* 370 */ 1306, 1339, 1419, 1435, 1440, 1340, 1378, 1402, 1323, 1424, - /* 380 */ 1335, 1437, 1334, 1436, 1341, 1350, 1338, 1438, 1332, 1439, - /* 390 */ 1442, 1407, 1326, 1322, 1443, 1460, 1461, + /* 140 */ 19, 849, 95, -36, -36, -36, -62, -62, -62, -15, + /* 150 */ -15, 333, 459, 478, 557, 530, 541, 616, 602, 793, + /* 160 */ 604, 607, 626, 19, 19, 881, 19, 19, 994, 19, + /* 170 */ 19, 807, 19, 19, 673, 807, 19, 19, 384, 384, + /* 180 */ 384, 19, 19, 673, 19, 19, 673, 19, 454, 685, + /* 190 */ 19, 19, 673, 19, 19, 19, 673, 19, 19, 19, + /* 200 */ 673, 673, 19, 19, 19, 19, 19, 468, 883, 921, + /* 210 */ 171, 754, 754, 432, 406, 406, 406, 816, 406, 171, + /* 220 */ 406, 171, 811, 879, 879, 1166, 1166, 1166, 1166, 1154, + /* 230 */ -36, 1060, 1065, 1066, 1068, 1069, 1056, 1076, 1076, 1095, + /* 240 */ 1079, 1095, 1079, 1097, 1097, 1157, 1097, 1105, 1097, 1182, + /* 250 */ 1119, 1119, 1157, 1097, 1097, 1097, 1182, 1189, 1076, 1189, + /* 260 */ 1076, 1189, 1076, 1076, 1179, 1103, 1189, 1076, 1167, 1167, + /* 270 */ 1210, 1060, 1076, 1224, 1224, 1224, 1224, 1060, 1167, 1210, + /* 280 */ 1076, 1216, 1216, 1076, 1076, 1117, -62, -62, -62, -62, + /* 290 */ -62, -62, 525, 684, 727, 168, 894, 556, 555, 938, + /* 300 */ 944, 949, 912, 1058, 1073, 1087, 1091, 1101, 1104, 1108, + /* 310 */ 1030, 1116, 1099, 1257, 1242, 1151, 1152, 1153, 1155, 1174, + /* 320 */ 1161, 1250, 1251, 1252, 1255, 1259, 1256, 1260, 1254, 1261, + /* 330 */ 1262, 1263, 1160, 1264, 1162, 1263, 1169, 1265, 1267, 1176, + /* 340 */ 1269, 1270, 1268, 1247, 1273, 1253, 1275, 1276, 1280, 1282, + /* 350 */ 1258, 1284, 1199, 1201, 1289, 1290, 1285, 1211, 1272, 1266, + /* 360 */ 1271, 1288, 1274, 1178, 1214, 1294, 1309, 1314, 1218, 1277, + /* 370 */ 1278, 1196, 1302, 1209, 1310, 1208, 1308, 1213, 1225, 1206, + /* 380 */ 1311, 1212, 1312, 1315, 1279, 1198, 1195, 1318, 1331, 1334, }; #define YY_REDUCE_USE_DFLT (-165) #define YY_REDUCE_MAX 291 static const short yy_reduce_ofst[] = { - /* 0 */ -138, 806, 503, 667, 190, -21, 44, 36, 38, 430, - /* 10 */ -141, 274, 91, 337, 271, 276, -126, 546, 285, 151, - /* 20 */ 180, -13, 283, 86, 86, 86, 86, 86, 86, 86, - /* 30 */ 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, - /* 40 */ 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, - /* 50 */ 86, 86, 86, 86, 86, 824, 831, 855, 858, 882, - /* 60 */ 893, 896, 920, 948, 962, 968, 972, 976, 978, 988, - /* 70 */ 996, 1012, 1015, 1036, 1041, 1044, 1056, 1061, 1068, 1070, - /* 80 */ 1074, 1076, 1081, 1085, 1087, 1094, 1100, 1102, 1105, 1114, - /* 90 */ 1118, 1120, 1126, 1129, 1132, 1138, 86, 86, 86, 86, - /* 100 */ 20, 86, 86, 23, 507, 594, 595, 636, 640, 684, - /* 110 */ 177, 541, 86, 200, 86, 86, 86, 86, 412, -164, - /* 120 */ -115, 368, 136, 136, 552, -6, 207, 573, 152, -90, - /* 130 */ 209, 475, 295, 728, 730, 736, 843, 859, 860, 715, - /* 140 */ 861, 29, 325, 724, 745, 753, 582, 668, 713, 83, - /* 150 */ 120, 0, 199, 256, 312, 156, 363, 156, 156, 349, - /* 160 */ 424, 440, 457, 486, 496, 526, 642, 634, 486, 716, - /* 170 */ 710, 774, 805, 737, 811, 817, 156, 737, 833, 845, - /* 180 */ 826, 872, 876, 890, 917, 156, 925, 936, 156, 941, - /* 190 */ 866, 877, 977, 983, 156, 987, 992, 993, 156, 1017, - /* 200 */ 1028, 1032, 156, 156, 1039, 1065, 1072, 1086, 1088, 984, - /* 210 */ 1016, 1031, 1075, 1013, 1024, 1025, 1090, 1108, 1109, 1082, - /* 220 */ 1110, 1116, 1119, 1117, 1113, 1098, 1099, 1151, 1157, 1158, - /* 230 */ 1159, 1152, 1121, 1122, 1123, 1124, 1115, 1125, 1097, 1174, - /* 240 */ 1175, 1127, 1128, 1130, 1131, 1161, 1164, 1149, 1166, 1165, - /* 250 */ 1168, 1169, 1133, 1134, 1163, 1176, 1177, 1178, 1171, 1199, - /* 260 */ 1201, 1203, 1204, 1206, 1211, 1212, 1135, 1136, 1214, 1215, - /* 270 */ 1182, 1184, 1144, 1179, 1218, 1185, 1189, 1190, 1191, 1180, - /* 280 */ 1197, 1148, 1223, 1142, 1145, 1231, 1232, 1146, 1228, 1160, - /* 290 */ 1147, 1143, + /* 0 */ -138, 277, 546, 137, 401, -21, 44, 36, 38, 242, + /* 10 */ -141, 191, 91, 269, 343, 345, -126, 589, 338, 150, + /* 20 */ 147, -13, 213, 412, 412, 412, 412, 412, 412, 412, + /* 30 */ 412, 412, 412, 412, 412, 412, 412, 412, 412, 412, + /* 40 */ 412, 412, 412, 412, 412, 412, 412, 412, 412, 412, + /* 50 */ 412, 412, 412, 412, 412, 211, 698, 714, 716, 722, + /* 60 */ 724, 728, 748, 753, 755, 757, 762, 769, 794, 805, + /* 70 */ 808, 846, 855, 858, 861, 875, 882, 893, 896, 899, + /* 80 */ 901, 903, 906, 920, 930, 932, 934, 936, 945, 947, + /* 90 */ 963, 965, 971, 975, 978, 980, 412, 412, 412, 412, + /* 100 */ 20, 412, 412, 23, 34, 334, 475, 552, 593, 594, + /* 110 */ 585, 212, 412, 289, 412, 412, 412, 412, 135, -164, + /* 120 */ -115, 164, 407, 407, 350, 141, 436, 163, 596, -90, + /* 130 */ 763, 218, 765, 438, 586, 592, 595, 715, 718, 408, + /* 140 */ 723, 380, 634, 677, 787, 798, 144, 529, 588, -14, + /* 150 */ 0, 17, 244, 155, 298, 155, 155, 418, 372, 477, + /* 160 */ 490, 494, 509, 526, 590, 465, 494, 730, 773, 743, + /* 170 */ 833, 792, 854, 860, 155, 792, 864, 885, 848, 859, + /* 180 */ 863, 891, 907, 155, 914, 917, 155, 927, 852, 898, + /* 190 */ 941, 950, 155, 961, 982, 990, 155, 992, 995, 996, + /* 200 */ 155, 155, 999, 1004, 1005, 1006, 1007, 1008, 964, 966, + /* 210 */ 1000, 933, 935, 937, 984, 991, 993, 989, 997, 1009, + /* 220 */ 1001, 1010, 1002, 973, 974, 1028, 1031, 1032, 1033, 1026, + /* 230 */ 998, 988, 1003, 1011, 1012, 1013, 968, 1039, 1040, 1014, + /* 240 */ 1015, 1017, 1018, 1025, 1029, 1020, 1034, 1035, 1041, 1036, + /* 250 */ 987, 1016, 1027, 1043, 1044, 1045, 1048, 1074, 1067, 1075, + /* 260 */ 1072, 1077, 1078, 1080, 1019, 1021, 1083, 1084, 1047, 1051, + /* 270 */ 1023, 1046, 1086, 1054, 1057, 1059, 1061, 1049, 1063, 1037, + /* 280 */ 1093, 1022, 1024, 1098, 1100, 1038, 1096, 1082, 1085, 1042, + /* 290 */ 1050, 1052, }; static const YYACTIONTYPE yy_default[] = { /* 0 */ 594, 819, 900, 709, 900, 819, 900, 900, 846, 713, @@ -68473,44 +71278,44 @@ static const YYACTIONTYPE yy_default[] = { /* 120 */ 900, 900, 900, 900, 594, 709, 900, 709, 900, 900, /* 130 */ 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, /* 140 */ 900, 900, 900, 900, 900, 900, 703, 713, 893, 900, - /* 150 */ 900, 900, 900, 669, 900, 900, 900, 900, 900, 900, - /* 160 */ 900, 900, 900, 900, 601, 599, 900, 701, 900, 900, - /* 170 */ 627, 900, 900, 711, 900, 900, 716, 717, 900, 900, - /* 180 */ 900, 900, 900, 900, 900, 615, 900, 900, 690, 900, - /* 190 */ 852, 900, 900, 900, 866, 900, 900, 900, 864, 900, - /* 200 */ 900, 900, 692, 752, 833, 900, 879, 881, 900, 900, - /* 210 */ 701, 710, 900, 900, 900, 815, 736, 736, 736, 648, - /* 220 */ 736, 900, 736, 900, 651, 746, 746, 598, 598, 598, - /* 230 */ 598, 668, 900, 746, 737, 739, 729, 741, 900, 718, - /* 240 */ 718, 726, 728, 726, 728, 680, 680, 665, 680, 651, - /* 250 */ 680, 825, 830, 830, 665, 680, 680, 680, 825, 610, - /* 260 */ 718, 610, 718, 610, 718, 718, 856, 858, 610, 718, - /* 270 */ 682, 682, 758, 746, 718, 689, 689, 689, 689, 746, - /* 280 */ 682, 758, 718, 878, 878, 718, 718, 886, 635, 861, - /* 290 */ 893, 898, 900, 900, 900, 900, 900, 900, 900, 900, - /* 300 */ 900, 900, 900, 765, 900, 900, 900, 900, 900, 900, - /* 310 */ 900, 900, 900, 900, 900, 900, 900, 839, 900, 900, - /* 320 */ 900, 900, 770, 766, 900, 767, 900, 695, 900, 900, - /* 330 */ 900, 900, 900, 900, 900, 900, 900, 900, 818, 900, - /* 340 */ 730, 900, 738, 900, 900, 900, 900, 900, 900, 900, + /* 150 */ 900, 669, 900, 900, 900, 900, 900, 900, 900, 900, + /* 160 */ 900, 900, 601, 599, 900, 701, 900, 900, 627, 900, + /* 170 */ 900, 711, 900, 900, 716, 717, 900, 900, 900, 900, + /* 180 */ 900, 900, 900, 615, 900, 900, 690, 900, 852, 900, + /* 190 */ 900, 900, 866, 900, 900, 900, 864, 900, 900, 900, + /* 200 */ 692, 752, 833, 900, 879, 881, 900, 900, 701, 710, + /* 210 */ 900, 900, 900, 815, 736, 736, 736, 648, 736, 900, + /* 220 */ 736, 900, 651, 746, 746, 598, 598, 598, 598, 668, + /* 230 */ 900, 746, 737, 739, 729, 741, 900, 718, 718, 726, + /* 240 */ 728, 726, 728, 680, 680, 665, 680, 651, 680, 825, + /* 250 */ 830, 830, 665, 680, 680, 680, 825, 610, 718, 610, + /* 260 */ 718, 610, 718, 718, 856, 858, 610, 718, 682, 682, + /* 270 */ 758, 746, 718, 689, 689, 689, 689, 746, 682, 758, + /* 280 */ 718, 878, 878, 718, 718, 886, 635, 653, 653, 861, + /* 290 */ 893, 898, 900, 900, 900, 900, 765, 900, 900, 900, + /* 300 */ 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, + /* 310 */ 839, 900, 900, 900, 900, 770, 766, 900, 767, 900, + /* 320 */ 695, 900, 900, 900, 900, 900, 900, 900, 900, 900, + /* 330 */ 900, 818, 900, 730, 900, 738, 900, 900, 900, 900, + /* 340 */ 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, /* 350 */ 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, - /* 360 */ 900, 900, 900, 900, 900, 900, 900, 854, 855, 900, + /* 360 */ 854, 855, 900, 900, 900, 900, 900, 900, 900, 900, /* 370 */ 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, - /* 380 */ 900, 900, 900, 900, 900, 900, 900, 900, 900, 900, - /* 390 */ 900, 885, 900, 900, 888, 595, 900, 589, 592, 591, - /* 400 */ 593, 597, 600, 622, 623, 624, 602, 603, 604, 605, - /* 410 */ 606, 607, 608, 614, 616, 634, 636, 620, 638, 699, - /* 420 */ 700, 762, 693, 694, 698, 621, 773, 764, 768, 769, - /* 430 */ 771, 772, 786, 787, 789, 795, 802, 805, 788, 793, - /* 440 */ 794, 796, 801, 804, 696, 697, 808, 628, 629, 632, - /* 450 */ 633, 842, 844, 843, 845, 631, 630, 774, 777, 810, - /* 460 */ 811, 867, 868, 869, 870, 871, 806, 719, 809, 792, - /* 470 */ 731, 734, 735, 732, 702, 712, 721, 722, 723, 724, - /* 480 */ 707, 708, 714, 727, 760, 761, 715, 704, 705, 706, - /* 490 */ 807, 763, 775, 776, 639, 640, 770, 641, 642, 643, - /* 500 */ 681, 684, 685, 686, 644, 663, 666, 667, 645, 647, - /* 510 */ 659, 660, 661, 662, 657, 658, 826, 827, 831, 829, - /* 520 */ 828, 664, 637, 626, 619, 670, 673, 674, 675, 676, + /* 380 */ 900, 900, 900, 900, 885, 900, 900, 888, 595, 900, + /* 390 */ 589, 592, 591, 593, 597, 600, 622, 623, 624, 602, + /* 400 */ 603, 604, 605, 606, 607, 608, 614, 616, 634, 636, + /* 410 */ 620, 638, 699, 700, 762, 693, 694, 698, 621, 773, + /* 420 */ 764, 768, 769, 771, 772, 786, 787, 789, 795, 802, + /* 430 */ 805, 788, 793, 794, 796, 801, 804, 696, 697, 808, + /* 440 */ 628, 629, 632, 633, 842, 844, 843, 845, 631, 630, + /* 450 */ 774, 777, 810, 811, 867, 868, 869, 870, 871, 806, + /* 460 */ 719, 809, 792, 731, 734, 735, 732, 702, 712, 721, + /* 470 */ 722, 723, 724, 707, 708, 714, 727, 760, 761, 715, + /* 480 */ 704, 705, 706, 807, 763, 775, 776, 639, 640, 770, + /* 490 */ 641, 642, 643, 681, 684, 685, 686, 644, 663, 666, + /* 500 */ 667, 645, 652, 646, 647, 654, 655, 656, 659, 660, + /* 510 */ 661, 662, 657, 658, 826, 827, 831, 829, 828, 649, + /* 520 */ 650, 664, 637, 626, 619, 670, 673, 674, 675, 676, /* 530 */ 677, 679, 671, 672, 617, 609, 611, 720, 848, 857, /* 540 */ 853, 849, 850, 851, 612, 822, 823, 683, 754, 755, /* 550 */ 847, 860, 862, 759, 863, 865, 890, 687, 688, 691, @@ -69193,58 +71998,56 @@ static void yy_destructor(YYCODETYPE yymajor, YYMINORTYPE *yypminor){ ** which appear on the RHS of the rule, but which are not used ** inside the C code. */ - case 155: - case 189: - case 206: + case 155: /* select */ + case 189: /* oneselect */ + case 206: /* seltablist_paren */ {sqlite3SelectDelete((yypminor->yy219));} break; - case 169: - case 170: - case 194: - case 196: - case 204: - case 210: - case 218: - case 221: - case 223: - case 235: + case 169: /* term */ + case 170: /* expr */ + case 194: /* where_opt */ + case 196: /* having_opt */ + case 204: /* on_opt */ + case 210: /* sortitem */ + case 218: /* escape */ + case 221: /* case_operand */ + case 223: /* case_else */ + case 235: /* when_clause */ + case 238: /* key_opt */ {sqlite3ExprDelete((yypminor->yy172));} break; - case 174: - case 182: - case 192: - case 195: - case 197: - case 199: - case 209: - case 211: - case 212: - case 215: - case 216: - case 222: + case 174: /* idxlist_opt */ + case 182: /* idxlist */ + case 192: /* selcollist */ + case 195: /* groupby_opt */ + case 197: /* orderby_opt */ + case 199: /* sclp */ + case 209: /* sortlist */ + case 211: /* nexprlist */ + case 212: /* setlist */ + case 215: /* itemlist */ + case 216: /* exprlist */ + case 222: /* case_exprlist */ {sqlite3ExprListDelete((yypminor->yy174));} break; - case 188: - case 193: - case 201: - case 202: + case 188: /* fullname */ + case 193: /* from */ + case 201: /* seltablist */ + case 202: /* stl_prefix */ {sqlite3SrcListDelete((yypminor->yy373));} break; - case 205: - case 208: - case 214: + case 205: /* using_opt */ + case 208: /* inscollist */ + case 214: /* inscollist_opt */ {sqlite3IdListDelete((yypminor->yy432));} break; - case 231: - case 236: + case 231: /* trigger_cmd_list */ + case 236: /* trigger_cmd */ {sqlite3DeleteTriggerStep((yypminor->yy243));} break; - case 233: + case 233: /* trigger_event */ {sqlite3IdListDelete((yypminor->yy370).b);} break; - case 238: -{sqlite3ExprDelete((yypminor->yy386));} - break; default: break; /* If no destructor action specified: do nothing */ } } @@ -69318,9 +72121,7 @@ static int yy_find_shift_action( if( stateno>YY_SHIFT_MAX || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){ return yy_default[stateno]; } - if( iLookAhead==YYNOCODE ){ - return YY_NO_ACTION; - } + assert( iLookAhead!=YYNOCODE ); i += iLookAhead; if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){ if( iLookAhead>0 ){ @@ -69371,21 +72172,14 @@ static int yy_find_reduce_action( YYCODETYPE iLookAhead /* The look-ahead token */ ){ int i; - /* int stateno = pParser->yystack[pParser->yyidx].stateno; */ - - if( stateno>YY_REDUCE_MAX || - (i = yy_reduce_ofst[stateno])==YY_REDUCE_USE_DFLT ){ - return yy_default[stateno]; - } - if( iLookAhead==YYNOCODE ){ - return YY_NO_ACTION; - } + assert( stateno<=YY_REDUCE_MAX ); + i = yy_reduce_ofst[stateno]; + assert( i!=YY_REDUCE_USE_DFLT ); + assert( iLookAhead!=YYNOCODE ); i += iLookAhead; - if( i<0 || i>=YY_SZ_ACTTAB || yy_lookahead[i]!=iLookAhead ){ - return yy_default[stateno]; - }else{ - return yy_action[i]; - } + assert( i>=0 && i ** break; */ - case 0: - case 1: - case 2: - case 4: - case 5: - case 10: - case 11: - case 12: - case 20: - case 28: - case 29: - case 37: - case 44: - case 45: - case 46: - case 47: - case 48: - case 49: - case 55: - case 82: - case 83: - case 84: - case 85: - case 257: - case 258: - case 268: - case 269: - case 289: - case 290: - case 298: - case 299: - case 303: - case 304: - case 306: - case 310: + case 0: /* input ::= cmdlist */ + case 1: /* cmdlist ::= cmdlist ecmd */ + case 2: /* cmdlist ::= ecmd */ + case 4: /* ecmd ::= SEMI */ + case 5: /* ecmd ::= explain cmdx SEMI */ + case 10: /* trans_opt ::= */ + case 11: /* trans_opt ::= TRANSACTION */ + case 12: /* trans_opt ::= TRANSACTION nm */ + case 20: /* cmd ::= create_table create_table_args */ + case 28: /* columnlist ::= columnlist COMMA column */ + case 29: /* columnlist ::= column */ + case 37: /* type ::= */ + case 44: /* signed ::= plus_num */ + case 45: /* signed ::= minus_num */ + case 46: /* carglist ::= carglist carg */ + case 47: /* carglist ::= */ + case 48: /* carg ::= CONSTRAINT nm ccons */ + case 49: /* carg ::= ccons */ + case 55: /* ccons ::= NULL onconf */ + case 82: /* conslist ::= conslist COMMA tcons */ + case 83: /* conslist ::= conslist tcons */ + case 84: /* conslist ::= tcons */ + case 85: /* tcons ::= CONSTRAINT nm */ + case 257: /* plus_opt ::= PLUS */ + case 258: /* plus_opt ::= */ + case 268: /* foreach_clause ::= */ + case 269: /* foreach_clause ::= FOR EACH ROW */ + case 289: /* database_kw_opt ::= DATABASE */ + case 290: /* database_kw_opt ::= */ + case 298: /* kwcolumn_opt ::= */ + case 299: /* kwcolumn_opt ::= COLUMNKW */ + case 303: /* vtabarglist ::= vtabarg */ + case 304: /* vtabarglist ::= vtabarglist COMMA vtabarg */ + case 306: /* vtabarg ::= vtabarg vtabargtoken */ + case 310: /* anylist ::= */ { } break; - case 3: + case 3: /* cmdx ::= cmd */ { sqlite3FinishCoding(pParse); } break; - case 6: + case 6: /* explain ::= */ { sqlite3BeginParse(pParse, 0); } break; - case 7: + case 7: /* explain ::= EXPLAIN */ { sqlite3BeginParse(pParse, 1); } break; - case 8: + case 8: /* explain ::= EXPLAIN QUERY PLAN */ { sqlite3BeginParse(pParse, 2); } break; - case 9: + case 9: /* cmd ::= BEGIN transtype trans_opt */ {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy46);} break; - case 13: + case 13: /* transtype ::= */ {yygotominor.yy46 = TK_DEFERRED;} break; - case 14: - case 15: - case 16: - case 107: - case 109: + case 14: /* transtype ::= DEFERRED */ + case 15: /* transtype ::= IMMEDIATE */ + case 16: /* transtype ::= EXCLUSIVE */ + case 107: /* multiselect_op ::= UNION */ + case 109: /* multiselect_op ::= EXCEPT|INTERSECT */ {yygotominor.yy46 = yymsp[0].major;} break; - case 17: - case 18: + case 17: /* cmd ::= COMMIT trans_opt */ + case 18: /* cmd ::= END trans_opt */ {sqlite3CommitTransaction(pParse);} break; - case 19: + case 19: /* cmd ::= ROLLBACK trans_opt */ {sqlite3RollbackTransaction(pParse);} break; - case 21: + case 21: /* create_table ::= CREATE temp TABLE ifnotexists nm dbnm */ { sqlite3StartTable(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410,yymsp[-4].minor.yy46,0,0,yymsp[-2].minor.yy46); } break; - case 22: - case 25: - case 63: - case 77: - case 79: - case 90: - case 101: - case 112: - case 113: - case 213: - case 216: + case 22: /* ifnotexists ::= */ + case 25: /* temp ::= */ + case 63: /* autoinc ::= */ + case 77: /* init_deferred_pred_opt ::= */ + case 79: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ + case 90: /* defer_subclause_opt ::= */ + case 101: /* ifexists ::= */ + case 112: /* distinct ::= ALL */ + case 113: /* distinct ::= */ + case 213: /* between_op ::= BETWEEN */ + case 216: /* in_op ::= IN */ {yygotominor.yy46 = 0;} break; - case 23: - case 24: - case 64: - case 78: - case 100: - case 111: - case 214: - case 217: + case 23: /* ifnotexists ::= IF NOT EXISTS */ + case 24: /* temp ::= TEMP */ + case 64: /* autoinc ::= AUTOINCR */ + case 78: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ + case 100: /* ifexists ::= IF EXISTS */ + case 111: /* distinct ::= DISTINCT */ + case 214: /* between_op ::= NOT BETWEEN */ + case 217: /* in_op ::= NOT IN */ {yygotominor.yy46 = 1;} break; - case 26: + case 26: /* create_table_args ::= LP columnlist conslist_opt RP */ { sqlite3EndTable(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy0,0); } break; - case 27: + case 27: /* create_table_args ::= AS select */ { sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy219); sqlite3SelectDelete(yymsp[0].minor.yy219); } break; - case 30: + case 30: /* column ::= columnid type carglist */ { yygotominor.yy410.z = yymsp[-2].minor.yy410.z; yygotominor.yy410.n = (pParse->sLastToken.z-yymsp[-2].minor.yy410.z) + pParse->sLastToken.n; } break; - case 31: + case 31: /* columnid ::= nm */ { sqlite3AddColumn(pParse,&yymsp[0].minor.yy410); yygotominor.yy410 = yymsp[0].minor.yy410; } break; - case 32: - case 33: - case 34: - case 35: - case 36: - case 256: + case 32: /* id ::= ID */ + case 33: /* ids ::= ID|STRING */ + case 34: /* nm ::= ID */ + case 35: /* nm ::= STRING */ + case 36: /* nm ::= JOIN_KW */ + case 256: /* number ::= INTEGER|FLOAT */ {yygotominor.yy410 = yymsp[0].minor.yy0;} break; - case 38: + case 38: /* type ::= typetoken */ {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy410);} break; - case 39: - case 42: - case 119: - case 120: - case 131: - case 241: - case 243: - case 252: - case 253: - case 254: - case 255: + case 39: /* typetoken ::= typename */ + case 42: /* typename ::= ids */ + case 119: /* as ::= AS nm */ + case 120: /* as ::= ids */ + case 131: /* dbnm ::= DOT nm */ + case 241: /* idxitem ::= nm */ + case 243: /* collate ::= COLLATE ids */ + case 252: /* nmnum ::= plus_num */ + case 253: /* nmnum ::= nm */ + case 254: /* plus_num ::= plus_opt number */ + case 255: /* minus_num ::= MINUS number */ {yygotominor.yy410 = yymsp[0].minor.yy410;} break; - case 40: + case 40: /* typetoken ::= typename LP signed RP */ { yygotominor.yy410.z = yymsp[-3].minor.yy410.z; yygotominor.yy410.n = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy410.z; } break; - case 41: + case 41: /* typetoken ::= typename LP signed COMMA signed RP */ { yygotominor.yy410.z = yymsp[-5].minor.yy410.z; yygotominor.yy410.n = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy410.z; } break; - case 43: + case 43: /* typename ::= typename ids */ {yygotominor.yy410.z=yymsp[-1].minor.yy410.z; yygotominor.yy410.n=yymsp[0].minor.yy410.n+(yymsp[0].minor.yy410.z-yymsp[-1].minor.yy410.z);} break; - case 50: - case 52: + case 50: /* ccons ::= DEFAULT term */ + case 52: /* ccons ::= DEFAULT PLUS term */ {sqlite3AddDefaultValue(pParse,yymsp[0].minor.yy172);} break; - case 51: + case 51: /* ccons ::= DEFAULT LP expr RP */ {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy172);} break; - case 53: + case 53: /* ccons ::= DEFAULT MINUS term */ { Expr *p = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy172, 0, 0); sqlite3AddDefaultValue(pParse,p); } break; - case 54: + case 54: /* ccons ::= DEFAULT id */ { Expr *p = sqlite3PExpr(pParse, TK_STRING, 0, 0, &yymsp[0].minor.yy410); sqlite3AddDefaultValue(pParse,p); } break; - case 56: + case 56: /* ccons ::= NOT NULL onconf */ {sqlite3AddNotNull(pParse, yymsp[0].minor.yy46);} break; - case 57: + case 57: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */ {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy46,yymsp[0].minor.yy46,yymsp[-2].minor.yy46);} break; - case 58: + case 58: /* ccons ::= UNIQUE onconf */ {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy46,0,0,0,0);} break; - case 59: + case 59: /* ccons ::= CHECK LP expr RP */ {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy172);} break; - case 60: + case 60: /* ccons ::= REFERENCES nm idxlist_opt refargs */ {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy410,yymsp[-1].minor.yy174,yymsp[0].minor.yy46);} break; - case 61: + case 61: /* ccons ::= defer_subclause */ {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy46);} break; - case 62: + case 62: /* ccons ::= COLLATE ids */ {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy410);} break; - case 65: + case 65: /* refargs ::= */ { yygotominor.yy46 = OE_Restrict * 0x010101; } break; - case 66: + case 66: /* refargs ::= refargs refarg */ { yygotominor.yy46 = (yymsp[-1].minor.yy46 & yymsp[0].minor.yy405.mask) | yymsp[0].minor.yy405.value; } break; - case 67: + case 67: /* refarg ::= MATCH nm */ { yygotominor.yy405.value = 0; yygotominor.yy405.mask = 0x000000; } break; - case 68: + case 68: /* refarg ::= ON DELETE refact */ { yygotominor.yy405.value = yymsp[0].minor.yy46; yygotominor.yy405.mask = 0x0000ff; } break; - case 69: + case 69: /* refarg ::= ON UPDATE refact */ { yygotominor.yy405.value = yymsp[0].minor.yy46<<8; yygotominor.yy405.mask = 0x00ff00; } break; - case 70: + case 70: /* refarg ::= ON INSERT refact */ { yygotominor.yy405.value = yymsp[0].minor.yy46<<16; yygotominor.yy405.mask = 0xff0000; } break; - case 71: + case 71: /* refact ::= SET NULL */ { yygotominor.yy46 = OE_SetNull; } break; - case 72: + case 72: /* refact ::= SET DEFAULT */ { yygotominor.yy46 = OE_SetDflt; } break; - case 73: + case 73: /* refact ::= CASCADE */ { yygotominor.yy46 = OE_Cascade; } break; - case 74: + case 74: /* refact ::= RESTRICT */ { yygotominor.yy46 = OE_Restrict; } break; - case 75: - case 76: - case 91: - case 93: - case 95: - case 96: - case 166: + case 75: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ + case 76: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ + case 91: /* defer_subclause_opt ::= defer_subclause */ + case 93: /* onconf ::= ON CONFLICT resolvetype */ + case 95: /* orconf ::= OR resolvetype */ + case 96: /* resolvetype ::= raisetype */ + case 166: /* insert_cmd ::= INSERT orconf */ {yygotominor.yy46 = yymsp[0].minor.yy46;} break; - case 80: + case 80: /* conslist_opt ::= */ {yygotominor.yy410.n = 0; yygotominor.yy410.z = 0;} break; - case 81: + case 81: /* conslist_opt ::= COMMA conslist */ {yygotominor.yy410 = yymsp[-1].minor.yy0;} break; - case 86: + case 86: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */ {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy174,yymsp[0].minor.yy46,yymsp[-2].minor.yy46,0);} break; - case 87: + case 87: /* tcons ::= UNIQUE LP idxlist RP onconf */ {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy174,yymsp[0].minor.yy46,0,0,0,0);} break; - case 88: + case 88: /* tcons ::= CHECK LP expr RP onconf */ {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy172);} break; - case 89: + case 89: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */ { sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy174, &yymsp[-3].minor.yy410, yymsp[-2].minor.yy174, yymsp[-1].minor.yy46); sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy46); } break; - case 92: - case 94: + case 92: /* onconf ::= */ + case 94: /* orconf ::= */ {yygotominor.yy46 = OE_Default;} break; - case 97: + case 97: /* resolvetype ::= IGNORE */ {yygotominor.yy46 = OE_Ignore;} break; - case 98: - case 167: + case 98: /* resolvetype ::= REPLACE */ + case 167: /* insert_cmd ::= REPLACE */ {yygotominor.yy46 = OE_Replace;} break; - case 99: + case 99: /* cmd ::= DROP TABLE ifexists fullname */ { sqlite3DropTable(pParse, yymsp[0].minor.yy373, 0, yymsp[-1].minor.yy46); } break; - case 102: + case 102: /* cmd ::= CREATE temp VIEW ifnotexists nm dbnm AS select */ { sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy410, &yymsp[-2].minor.yy410, yymsp[0].minor.yy219, yymsp[-6].minor.yy46, yymsp[-4].minor.yy46); } break; - case 103: + case 103: /* cmd ::= DROP VIEW ifexists fullname */ { sqlite3DropTable(pParse, yymsp[0].minor.yy373, 1, yymsp[-1].minor.yy46); } break; - case 104: + case 104: /* cmd ::= select */ { - sqlite3Select(pParse, yymsp[0].minor.yy219, SRT_Callback, 0, 0, 0, 0, 0); + SelectDest dest = {SRT_Callback, 0, 0}; + sqlite3Select(pParse, yymsp[0].minor.yy219, &dest, 0, 0, 0, 0); sqlite3SelectDelete(yymsp[0].minor.yy219); } break; - case 105: - case 128: + case 105: /* select ::= oneselect */ + case 128: /* seltablist_paren ::= select */ {yygotominor.yy219 = yymsp[0].minor.yy219;} break; - case 106: + case 106: /* select ::= select multiselect_op oneselect */ { if( yymsp[0].minor.yy219 ){ yymsp[0].minor.yy219->op = yymsp[-1].minor.yy46; @@ -70128,37 +72923,37 @@ static void yy_reduce( yygotominor.yy219 = yymsp[0].minor.yy219; } break; - case 108: + case 108: /* multiselect_op ::= UNION ALL */ {yygotominor.yy46 = TK_ALL;} break; - case 110: + case 110: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ { yygotominor.yy219 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy174,yymsp[-5].minor.yy373,yymsp[-4].minor.yy172,yymsp[-3].minor.yy174,yymsp[-2].minor.yy172,yymsp[-1].minor.yy174,yymsp[-7].minor.yy46,yymsp[0].minor.yy234.pLimit,yymsp[0].minor.yy234.pOffset); } break; - case 114: - case 238: + case 114: /* sclp ::= selcollist COMMA */ + case 238: /* idxlist_opt ::= LP idxlist RP */ {yygotominor.yy174 = yymsp[-1].minor.yy174;} break; - case 115: - case 141: - case 149: - case 231: - case 237: + case 115: /* sclp ::= */ + case 141: /* orderby_opt ::= */ + case 149: /* groupby_opt ::= */ + case 231: /* exprlist ::= */ + case 237: /* idxlist_opt ::= */ {yygotominor.yy174 = 0;} break; - case 116: + case 116: /* selcollist ::= sclp expr as */ { yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy174,yymsp[-1].minor.yy172,yymsp[0].minor.yy410.n?&yymsp[0].minor.yy410:0); } break; - case 117: + case 117: /* selcollist ::= sclp STAR */ { Expr *p = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0); yygotominor.yy174 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy174, p, 0); } break; - case 118: + case 118: /* selcollist ::= sclp nm DOT STAR */ { Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, 0); Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410); @@ -70166,181 +72961,181 @@ static void yy_reduce( yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy174, pDot, 0); } break; - case 121: + case 121: /* as ::= */ {yygotominor.yy410.n = 0;} break; - case 122: + case 122: /* from ::= */ {yygotominor.yy373 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy373));} break; - case 123: + case 123: /* from ::= FROM seltablist */ { yygotominor.yy373 = yymsp[0].minor.yy373; sqlite3SrcListShiftJoinType(yygotominor.yy373); } break; - case 124: + case 124: /* stl_prefix ::= seltablist joinop */ { yygotominor.yy373 = yymsp[-1].minor.yy373; if( yygotominor.yy373 && yygotominor.yy373->nSrc>0 ) yygotominor.yy373->a[yygotominor.yy373->nSrc-1].jointype = yymsp[0].minor.yy46; } break; - case 125: + case 125: /* stl_prefix ::= */ {yygotominor.yy373 = 0;} break; - case 126: + case 126: /* seltablist ::= stl_prefix nm dbnm as on_opt using_opt */ { yygotominor.yy373 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy373,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,0,yymsp[-1].minor.yy172,yymsp[0].minor.yy432); } break; - case 127: + case 127: /* seltablist ::= stl_prefix LP seltablist_paren RP as on_opt using_opt */ { yygotominor.yy373 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy373,0,0,&yymsp[-2].minor.yy410,yymsp[-4].minor.yy219,yymsp[-1].minor.yy172,yymsp[0].minor.yy432); } break; - case 129: + case 129: /* seltablist_paren ::= seltablist */ { sqlite3SrcListShiftJoinType(yymsp[0].minor.yy373); yygotominor.yy219 = sqlite3SelectNew(pParse,0,yymsp[0].minor.yy373,0,0,0,0,0,0,0); } break; - case 130: + case 130: /* dbnm ::= */ {yygotominor.yy410.z=0; yygotominor.yy410.n=0;} break; - case 132: + case 132: /* fullname ::= nm dbnm */ {yygotominor.yy373 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410);} break; - case 133: + case 133: /* joinop ::= COMMA|JOIN */ { yygotominor.yy46 = JT_INNER; } break; - case 134: + case 134: /* joinop ::= JOIN_KW JOIN */ { yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); } break; - case 135: + case 135: /* joinop ::= JOIN_KW nm JOIN */ { yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy410,0); } break; - case 136: + case 136: /* joinop ::= JOIN_KW nm nm JOIN */ { yygotominor.yy46 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy410,&yymsp[-1].minor.yy410); } break; - case 137: - case 145: - case 152: - case 159: - case 174: - case 202: - case 226: - case 228: + case 137: /* on_opt ::= ON expr */ + case 145: /* sortitem ::= expr */ + case 152: /* having_opt ::= HAVING expr */ + case 159: /* where_opt ::= WHERE expr */ + case 174: /* expr ::= term */ + case 202: /* escape ::= ESCAPE expr */ + case 226: /* case_else ::= ELSE expr */ + case 228: /* case_operand ::= expr */ {yygotominor.yy172 = yymsp[0].minor.yy172;} break; - case 138: - case 151: - case 158: - case 203: - case 227: - case 229: + case 138: /* on_opt ::= */ + case 151: /* having_opt ::= */ + case 158: /* where_opt ::= */ + case 203: /* escape ::= */ + case 227: /* case_else ::= */ + case 229: /* case_operand ::= */ {yygotominor.yy172 = 0;} break; - case 139: - case 171: + case 139: /* using_opt ::= USING LP inscollist RP */ + case 171: /* inscollist_opt ::= LP inscollist RP */ {yygotominor.yy432 = yymsp[-1].minor.yy432;} break; - case 140: - case 170: + case 140: /* using_opt ::= */ + case 170: /* inscollist_opt ::= */ {yygotominor.yy432 = 0;} break; - case 142: - case 150: - case 230: + case 142: /* orderby_opt ::= ORDER BY sortlist */ + case 150: /* groupby_opt ::= GROUP BY nexprlist */ + case 230: /* exprlist ::= nexprlist */ {yygotominor.yy174 = yymsp[0].minor.yy174;} break; - case 143: + case 143: /* sortlist ::= sortlist COMMA sortitem sortorder */ { yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy174,yymsp[-1].minor.yy172,0); if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46; } break; - case 144: + case 144: /* sortlist ::= sortitem sortorder */ { yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy172,0); if( yygotominor.yy174 && yygotominor.yy174->a ) yygotominor.yy174->a[0].sortOrder = yymsp[0].minor.yy46; } break; - case 146: - case 148: + case 146: /* sortorder ::= ASC */ + case 148: /* sortorder ::= */ {yygotominor.yy46 = SQLITE_SO_ASC;} break; - case 147: + case 147: /* sortorder ::= DESC */ {yygotominor.yy46 = SQLITE_SO_DESC;} break; - case 153: + case 153: /* limit_opt ::= */ {yygotominor.yy234.pLimit = 0; yygotominor.yy234.pOffset = 0;} break; - case 154: + case 154: /* limit_opt ::= LIMIT expr */ {yygotominor.yy234.pLimit = yymsp[0].minor.yy172; yygotominor.yy234.pOffset = 0;} break; - case 155: + case 155: /* limit_opt ::= LIMIT expr OFFSET expr */ {yygotominor.yy234.pLimit = yymsp[-2].minor.yy172; yygotominor.yy234.pOffset = yymsp[0].minor.yy172;} break; - case 156: + case 156: /* limit_opt ::= LIMIT expr COMMA expr */ {yygotominor.yy234.pOffset = yymsp[-2].minor.yy172; yygotominor.yy234.pLimit = yymsp[0].minor.yy172;} break; - case 157: + case 157: /* cmd ::= DELETE FROM fullname where_opt */ {sqlite3DeleteFrom(pParse,yymsp[-1].minor.yy373,yymsp[0].minor.yy172);} break; - case 160: + case 160: /* cmd ::= UPDATE orconf fullname SET setlist where_opt */ { sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy174,SQLITE_MAX_COLUMN,"set list"); sqlite3Update(pParse,yymsp[-3].minor.yy373,yymsp[-1].minor.yy174,yymsp[0].minor.yy172,yymsp[-4].minor.yy46); } break; - case 161: + case 161: /* setlist ::= setlist COMMA nm EQ expr */ {yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);} break; - case 162: + case 162: /* setlist ::= nm EQ expr */ {yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy172,&yymsp[-2].minor.yy410);} break; - case 163: + case 163: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */ {sqlite3Insert(pParse, yymsp[-5].minor.yy373, yymsp[-1].minor.yy174, 0, yymsp[-4].minor.yy432, yymsp[-7].minor.yy46);} break; - case 164: + case 164: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */ {sqlite3Insert(pParse, yymsp[-2].minor.yy373, 0, yymsp[0].minor.yy219, yymsp[-1].minor.yy432, yymsp[-4].minor.yy46);} break; - case 165: + case 165: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */ {sqlite3Insert(pParse, yymsp[-3].minor.yy373, 0, 0, yymsp[-2].minor.yy432, yymsp[-5].minor.yy46);} break; - case 168: - case 232: + case 168: /* itemlist ::= itemlist COMMA expr */ + case 232: /* nexprlist ::= nexprlist COMMA expr */ {yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy174,yymsp[0].minor.yy172,0);} break; - case 169: - case 233: + case 169: /* itemlist ::= expr */ + case 233: /* nexprlist ::= expr */ {yygotominor.yy174 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy172,0);} break; - case 172: + case 172: /* inscollist ::= inscollist COMMA nm */ {yygotominor.yy432 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy432,&yymsp[0].minor.yy410);} break; - case 173: + case 173: /* inscollist ::= nm */ {yygotominor.yy432 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy410);} break; - case 175: + case 175: /* expr ::= LP expr RP */ {yygotominor.yy172 = yymsp[-1].minor.yy172; sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); } break; - case 176: - case 181: - case 182: + case 176: /* term ::= NULL */ + case 181: /* term ::= INTEGER|FLOAT|BLOB */ + case 182: /* term ::= STRING */ {yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[0].major, 0, 0, &yymsp[0].minor.yy0);} break; - case 177: - case 178: + case 177: /* expr ::= ID */ + case 178: /* expr ::= JOIN_KW */ {yygotominor.yy172 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);} break; - case 179: + case 179: /* expr ::= nm DOT nm */ { Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410); Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy410); yygotominor.yy172 = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0); } break; - case 180: + case 180: /* expr ::= nm DOT nm DOT nm */ { Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy410); Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy410); @@ -70349,28 +73144,28 @@ static void yy_reduce( yygotominor.yy172 = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0); } break; - case 183: + case 183: /* expr ::= REGISTER */ {yygotominor.yy172 = sqlite3RegisterExpr(pParse, &yymsp[0].minor.yy0);} break; - case 184: + case 184: /* expr ::= VARIABLE */ { Token *pToken = &yymsp[0].minor.yy0; Expr *pExpr = yygotominor.yy172 = sqlite3PExpr(pParse, TK_VARIABLE, 0, 0, pToken); sqlite3ExprAssignVarNumber(pParse, pExpr); } break; - case 185: + case 185: /* expr ::= expr COLLATE ids */ { yygotominor.yy172 = sqlite3ExprSetColl(pParse, yymsp[-2].minor.yy172, &yymsp[0].minor.yy410); } break; - case 186: + case 186: /* expr ::= CAST LP expr AS typetoken RP */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy172, 0, &yymsp[-1].minor.yy410); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); } break; - case 187: + case 187: /* expr ::= ID LP distinct exprlist RP */ { if( yymsp[-1].minor.yy174 && yymsp[-1].minor.yy174->nExpr>SQLITE_MAX_FUNCTION_ARG ){ sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0); @@ -70382,13 +73177,13 @@ static void yy_reduce( } } break; - case 188: + case 188: /* expr ::= ID LP STAR RP */ { yygotominor.yy172 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); } break; - case 189: + case 189: /* term ::= CTIME_KW */ { /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are ** treated as functions that return constants */ @@ -70399,25 +73194,25 @@ static void yy_reduce( } } break; - case 190: - case 191: - case 192: - case 193: - case 194: - case 195: - case 196: - case 197: + case 190: /* expr ::= expr AND expr */ + case 191: /* expr ::= expr OR expr */ + case 192: /* expr ::= expr LT|GT|GE|LE expr */ + case 193: /* expr ::= expr EQ|NE expr */ + case 194: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ + case 195: /* expr ::= expr PLUS|MINUS expr */ + case 196: /* expr ::= expr STAR|SLASH|REM expr */ + case 197: /* expr ::= expr CONCAT expr */ {yygotominor.yy172 = sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy172,yymsp[0].minor.yy172,0);} break; - case 198: - case 200: + case 198: /* likeop ::= LIKE_KW */ + case 200: /* likeop ::= MATCH */ {yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 0;} break; - case 199: - case 201: + case 199: /* likeop ::= NOT LIKE_KW */ + case 201: /* likeop ::= NOT MATCH */ {yygotominor.yy72.eOperator = yymsp[0].minor.yy0; yygotominor.yy72.not = 1;} break; - case 204: + case 204: /* expr ::= expr likeop expr escape */ { ExprList *pList; pList = sqlite3ExprListAppend(pParse,0, yymsp[-1].minor.yy172, 0); @@ -70431,50 +73226,50 @@ static void yy_reduce( if( yygotominor.yy172 ) yygotominor.yy172->flags |= EP_InfixFunc; } break; - case 205: + case 205: /* expr ::= expr ISNULL|NOTNULL */ { yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[0].major, yymsp[-1].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy172->span,&yymsp[0].minor.yy0); } break; - case 206: + case 206: /* expr ::= expr IS NULL */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_ISNULL, yymsp[-2].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0); } break; - case 207: + case 207: /* expr ::= expr NOT NULL */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOTNULL, yymsp[-2].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy172->span,&yymsp[0].minor.yy0); } break; - case 208: + case 208: /* expr ::= expr IS NOT NULL */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_NOTNULL, yymsp[-3].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,&yymsp[0].minor.yy0); } break; - case 209: - case 210: + case 209: /* expr ::= NOT expr */ + case 210: /* expr ::= BITNOT expr */ { yygotominor.yy172 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span); } break; - case 211: + case 211: /* expr ::= MINUS expr */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span); } break; - case 212: + case 212: /* expr ::= PLUS expr */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_UPLUS, yymsp[0].minor.yy172, 0, 0); sqlite3ExprSpan(yygotominor.yy172,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy172->span); } break; - case 215: + case 215: /* expr ::= expr between_op expr AND expr */ { ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy172, 0); pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy172, 0); @@ -70488,7 +73283,7 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy172->span); } break; - case 218: + case 218: /* expr ::= expr in_op LP exprlist RP */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy172, 0, 0); if( yygotominor.yy172 ){ @@ -70501,7 +73296,7 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0); } break; - case 219: + case 219: /* expr ::= LP select RP */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0); if( yygotominor.yy172 ){ @@ -70513,7 +73308,7 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); } break; - case 220: + case 220: /* expr ::= expr in_op LP select RP */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy172, 0, 0); if( yygotominor.yy172 ){ @@ -70526,7 +73321,7 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172,&yymsp[-4].minor.yy172->span,&yymsp[0].minor.yy0); } break; - case 221: + case 221: /* expr ::= expr in_op nm dbnm */ { SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410); yygotominor.yy172 = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy172, 0, 0); @@ -70540,7 +73335,7 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172,&yymsp[-3].minor.yy172->span,yymsp[0].minor.yy410.z?&yymsp[0].minor.yy410:&yymsp[-1].minor.yy410); } break; - case 222: + case 222: /* expr ::= EXISTS LP select RP */ { Expr *p = yygotominor.yy172 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0); if( p ){ @@ -70552,7 +73347,7 @@ static void yy_reduce( } } break; - case 223: + case 223: /* expr ::= CASE case_operand case_exprlist case_else END */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, 0); if( yygotominor.yy172 ){ @@ -70564,33 +73359,33 @@ static void yy_reduce( sqlite3ExprSpan(yygotominor.yy172, &yymsp[-4].minor.yy0, &yymsp[0].minor.yy0); } break; - case 224: + case 224: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */ { yygotominor.yy174 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy174, yymsp[-2].minor.yy172, 0); yygotominor.yy174 = sqlite3ExprListAppend(pParse,yygotominor.yy174, yymsp[0].minor.yy172, 0); } break; - case 225: + case 225: /* case_exprlist ::= WHEN expr THEN expr */ { yygotominor.yy174 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy172, 0); yygotominor.yy174 = sqlite3ExprListAppend(pParse,yygotominor.yy174, yymsp[0].minor.yy172, 0); } break; - case 234: + case 234: /* cmd ::= CREATE uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */ { sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy410, &yymsp[-5].minor.yy410, sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy410,0), yymsp[-1].minor.yy174, yymsp[-9].minor.yy46, &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy46); } break; - case 235: - case 282: + case 235: /* uniqueflag ::= UNIQUE */ + case 282: /* raisetype ::= ABORT */ {yygotominor.yy46 = OE_Abort;} break; - case 236: + case 236: /* uniqueflag ::= */ {yygotominor.yy46 = OE_None;} break; - case 239: + case 239: /* idxlist ::= idxlist COMMA idxitem collate sortorder */ { Expr *p = 0; if( yymsp[-1].minor.yy410.n>0 ){ @@ -70602,7 +73397,7 @@ static void yy_reduce( if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46; } break; - case 240: + case 240: /* idxlist ::= idxitem collate sortorder */ { Expr *p = 0; if( yymsp[-1].minor.yy410.n>0 ){ @@ -70614,34 +73409,34 @@ static void yy_reduce( if( yygotominor.yy174 ) yygotominor.yy174->a[yygotominor.yy174->nExpr-1].sortOrder = yymsp[0].minor.yy46; } break; - case 242: + case 242: /* collate ::= */ {yygotominor.yy410.z = 0; yygotominor.yy410.n = 0;} break; - case 244: + case 244: /* cmd ::= DROP INDEX ifexists fullname */ {sqlite3DropIndex(pParse, yymsp[0].minor.yy373, yymsp[-1].minor.yy46);} break; - case 245: - case 246: + case 245: /* cmd ::= VACUUM */ + case 246: /* cmd ::= VACUUM nm */ {sqlite3Vacuum(pParse);} break; - case 247: + case 247: /* cmd ::= PRAGMA nm dbnm EQ nmnum */ {sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy410,0);} break; - case 248: + case 248: /* cmd ::= PRAGMA nm dbnm EQ ON */ {sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy0,0);} break; - case 249: + case 249: /* cmd ::= PRAGMA nm dbnm EQ minus_num */ { sqlite3Pragma(pParse,&yymsp[-3].minor.yy410,&yymsp[-2].minor.yy410,&yymsp[0].minor.yy410,1); } break; - case 250: + case 250: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */ {sqlite3Pragma(pParse,&yymsp[-4].minor.yy410,&yymsp[-3].minor.yy410,&yymsp[-1].minor.yy410,0);} break; - case 251: + case 251: /* cmd ::= PRAGMA nm dbnm */ {sqlite3Pragma(pParse,&yymsp[-1].minor.yy410,&yymsp[0].minor.yy410,0,0);} break; - case 259: + case 259: /* cmd ::= CREATE trigger_decl BEGIN trigger_cmd_list END */ { Token all; all.z = yymsp[-3].minor.yy410.z; @@ -70649,36 +73444,38 @@ static void yy_reduce( sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy243, &all); } break; - case 260: + case 260: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */ { sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy410, &yymsp[-6].minor.yy410, yymsp[-5].minor.yy46, yymsp[-4].minor.yy370.a, yymsp[-4].minor.yy370.b, yymsp[-2].minor.yy373, yymsp[0].minor.yy172, yymsp[-10].minor.yy46, yymsp[-8].minor.yy46); yygotominor.yy410 = (yymsp[-6].minor.yy410.n==0?yymsp[-7].minor.yy410:yymsp[-6].minor.yy410); } break; - case 261: - case 264: + case 261: /* trigger_time ::= BEFORE */ + case 264: /* trigger_time ::= */ { yygotominor.yy46 = TK_BEFORE; } break; - case 262: + case 262: /* trigger_time ::= AFTER */ { yygotominor.yy46 = TK_AFTER; } break; - case 263: + case 263: /* trigger_time ::= INSTEAD OF */ { yygotominor.yy46 = TK_INSTEAD;} break; - case 265: - case 266: + case 265: /* trigger_event ::= DELETE|INSERT */ + case 266: /* trigger_event ::= UPDATE */ {yygotominor.yy370.a = yymsp[0].major; yygotominor.yy370.b = 0;} break; - case 267: + case 267: /* trigger_event ::= UPDATE OF inscollist */ {yygotominor.yy370.a = TK_UPDATE; yygotominor.yy370.b = yymsp[0].minor.yy432;} break; - case 270: + case 270: /* when_clause ::= */ + case 287: /* key_opt ::= */ { yygotominor.yy172 = 0; } break; - case 271: + case 271: /* when_clause ::= WHEN expr */ + case 288: /* key_opt ::= KEY expr */ { yygotominor.yy172 = yymsp[0].minor.yy172; } break; - case 272: + case 272: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */ { if( yymsp[-2].minor.yy243 ){ yymsp[-2].minor.yy243->pLast->pNext = yymsp[-1].minor.yy243; @@ -70689,25 +73486,25 @@ static void yy_reduce( yygotominor.yy243 = yymsp[-2].minor.yy243; } break; - case 273: + case 273: /* trigger_cmd_list ::= */ { yygotominor.yy243 = 0; } break; - case 274: + case 274: /* trigger_cmd ::= UPDATE orconf nm SET setlist where_opt */ { yygotominor.yy243 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-3].minor.yy410, yymsp[-1].minor.yy174, yymsp[0].minor.yy172, yymsp[-4].minor.yy46); } break; - case 275: + case 275: /* trigger_cmd ::= insert_cmd INTO nm inscollist_opt VALUES LP itemlist RP */ {yygotominor.yy243 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy410, yymsp[-4].minor.yy432, yymsp[-1].minor.yy174, 0, yymsp[-7].minor.yy46);} break; - case 276: + case 276: /* trigger_cmd ::= insert_cmd INTO nm inscollist_opt select */ {yygotominor.yy243 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy410, yymsp[-1].minor.yy432, 0, yymsp[0].minor.yy219, yymsp[-4].minor.yy46);} break; - case 277: + case 277: /* trigger_cmd ::= DELETE FROM nm where_opt */ {yygotominor.yy243 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-1].minor.yy410, yymsp[0].minor.yy172);} break; - case 278: + case 278: /* trigger_cmd ::= select */ {yygotominor.yy243 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy219); } break; - case 279: + case 279: /* expr ::= RAISE LP IGNORE RP */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); if( yygotominor.yy172 ){ @@ -70716,7 +73513,7 @@ static void yy_reduce( } } break; - case 280: + case 280: /* expr ::= RAISE LP raisetype COMMA nm RP */ { yygotominor.yy172 = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy410); if( yygotominor.yy172 ) { @@ -70725,78 +73522,72 @@ static void yy_reduce( } } break; - case 281: + case 281: /* raisetype ::= ROLLBACK */ {yygotominor.yy46 = OE_Rollback;} break; - case 283: + case 283: /* raisetype ::= FAIL */ {yygotominor.yy46 = OE_Fail;} break; - case 284: + case 284: /* cmd ::= DROP TRIGGER ifexists fullname */ { sqlite3DropTrigger(pParse,yymsp[0].minor.yy373,yymsp[-1].minor.yy46); } break; - case 285: + case 285: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */ { - sqlite3Attach(pParse, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, yymsp[0].minor.yy386); + sqlite3Attach(pParse, yymsp[-3].minor.yy172, yymsp[-1].minor.yy172, yymsp[0].minor.yy172); } break; - case 286: + case 286: /* cmd ::= DETACH database_kw_opt expr */ { sqlite3Detach(pParse, yymsp[0].minor.yy172); } break; - case 287: -{ yygotominor.yy386 = 0; } - break; - case 288: -{ yygotominor.yy386 = yymsp[0].minor.yy172; } - break; - case 291: + case 291: /* cmd ::= REINDEX */ {sqlite3Reindex(pParse, 0, 0);} break; - case 292: + case 292: /* cmd ::= REINDEX nm dbnm */ {sqlite3Reindex(pParse, &yymsp[-1].minor.yy410, &yymsp[0].minor.yy410);} break; - case 293: + case 293: /* cmd ::= ANALYZE */ {sqlite3Analyze(pParse, 0, 0);} break; - case 294: + case 294: /* cmd ::= ANALYZE nm dbnm */ {sqlite3Analyze(pParse, &yymsp[-1].minor.yy410, &yymsp[0].minor.yy410);} break; - case 295: + case 295: /* cmd ::= ALTER TABLE fullname RENAME TO nm */ { sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy373,&yymsp[0].minor.yy410); } break; - case 296: + case 296: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */ { sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy410); } break; - case 297: + case 297: /* add_column_fullname ::= fullname */ { sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy373); } break; - case 300: + case 300: /* cmd ::= create_vtab */ {sqlite3VtabFinishParse(pParse,0);} break; - case 301: + case 301: /* cmd ::= create_vtab LP vtabarglist RP */ {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);} break; - case 302: + case 302: /* create_vtab ::= CREATE VIRTUAL TABLE nm dbnm USING nm */ { sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy410, &yymsp[-2].minor.yy410, &yymsp[0].minor.yy410); } break; - case 305: + case 305: /* vtabarg ::= */ {sqlite3VtabArgInit(pParse);} break; - case 307: - case 308: - case 309: - case 311: + case 307: /* vtabargtoken ::= ANY */ + case 308: /* vtabargtoken ::= lp anylist RP */ + case 309: /* lp ::= LP */ + case 311: /* anylist ::= anylist ANY */ {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);} break; }; @@ -70821,7 +73612,8 @@ static void yy_reduce( { yy_shift(yypParser,yyact,yygoto,&yygotominor); } - }else if( yyact == YYNSTATE + YYNRULE + 1 ){ + }else{ + assert( yyact == YYNSTATE + YYNRULE + 1 ); yy_accept(yypParser); } } @@ -70855,14 +73647,9 @@ static void yy_syntax_error( sqlite3ParserARG_FETCH; #define TOKEN (yyminor.yy0) - if( !pParse->parseError ){ - if( TOKEN.z[0] ){ - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); - }else{ - sqlite3ErrorMsg(pParse, "incomplete SQL statement"); - } - pParse->parseError = 1; - } + assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); + pParse->parseError = 1; sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ } @@ -70912,7 +73699,9 @@ SQLITE_PRIVATE void sqlite3Parser( YYMINORTYPE yyminorunion; int yyact; /* The parser action. */ int yyendofinput; /* True if we are at the end of input */ +#ifdef YYERRORSYMBOL int yyerrorhit = 0; /* True if yymajor has invoked an error */ +#endif yyParser *yypParser; /* The parser */ /* (re)initialize the parser, if necessary */ @@ -70943,17 +73732,17 @@ SQLITE_PRIVATE void sqlite3Parser( do{ yyact = yy_find_shift_action(yypParser,yymajor); if( yyactyyerrcnt--; - if( yyendofinput && yypParser->yyidx>=0 ){ - yymajor = 0; - }else{ - yymajor = YYNOCODE; - } + yymajor = YYNOCODE; }else if( yyact < YYNSTATE + YYNRULE ){ yy_reduce(yypParser,yyact-YYNSTATE); - }else if( yyact == YY_ERROR_ACTION ){ + }else{ + assert( yyact == YY_ERROR_ACTION ); +#ifdef YYERRORSYMBOL int yymx; +#endif #ifndef NDEBUG if( yyTraceFILE ){ fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); @@ -71034,9 +73823,6 @@ SQLITE_PRIVATE void sqlite3Parser( } yymajor = YYNOCODE; #endif - }else{ - yy_accept(yypParser); - yymajor = YYNOCODE; } }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); return; @@ -71061,7 +73847,7 @@ SQLITE_PRIVATE void sqlite3Parser( ** individual tokens and sends those tokens one-by-one over to the ** parser for analysis. ** -** $Id: tokenize.c,v 1.136 2007/08/27 23:26:59 drh Exp $ +** $Id: tokenize.c,v 1.138 2008/01/22 23:37:10 drh Exp $ */ /* @@ -71451,7 +74237,7 @@ static int getToken(const unsigned char *z, int *tokenType){ } case '[': { for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} - *tokenType = TK_ID; + *tokenType = c==']' ? TK_ID : TK_ILLEGAL; return i; } case '?': { @@ -71503,19 +74289,14 @@ static int getToken(const unsigned char *z, int *tokenType){ } #ifndef SQLITE_OMIT_BLOB_LITERAL case 'x': case 'X': { - if( (c=z[1])=='\'' || c=='"' ){ - int delim = c; + if( z[1]=='\'' ){ *tokenType = TK_BLOB; - for(i=2; (c=z[i])!=0; i++){ - if( c==delim ){ - if( i%2 ) *tokenType = TK_ILLEGAL; - break; - } + for(i=2; (c=z[i])!=0 && c!='\''; i++){ if( !isxdigit(c) ){ *tokenType = TK_ILLEGAL; - return i; } } + if( i%2 || !c ) *tokenType = TK_ILLEGAL; if( c ) i++; return i; } @@ -71577,7 +74358,7 @@ SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzEr assert( pParse->sLastToken.dyn==0 ); pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType); i += pParse->sLastToken.n; - if( i>SQLITE_MAX_SQL_LENGTH ){ + if( SQLITE_MAX_SQL_LENGTH>0 && i>SQLITE_MAX_SQL_LENGTH ){ pParse->rc = SQLITE_TOOBIG; break; } @@ -71636,7 +74417,7 @@ abort_parse: sqlite3_free(pParse->zErrMsg); } pParse->zErrMsg = 0; - if( !nErr ) nErr++; + nErr++; } if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ sqlite3VdbeDelete(pParse->pVdbe); @@ -71957,8 +74738,40 @@ SQLITE_API int sqlite3_complete16(const void *zSql){ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.409 2007/12/07 18:55:28 drh Exp $ +** $Id: main.c,v 1.421 2008/03/07 21:37:19 drh Exp $ */ +#ifdef SQLITE_ENABLE_FTS3 +/************** Include fts3.h in the middle of main.c ***********************/ +/************** Begin file fts3.h ********************************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** FTS3 library. All it does is declare the sqlite3Fts3Init() interface. +*/ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + +/************** End of fts3.h ************************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif /* ** The version of the library @@ -71974,7 +74787,7 @@ SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } ** I/O active are written using this function. These messages ** are intended for debugging activity only. */ -SQLITE_API void (*sqlite3_io_trace)(const char*, ...) = 0; +SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0; /* ** If the following global variable points to a string which is the @@ -71987,11 +74800,22 @@ SQLITE_API char *sqlite3_temp_directory = 0; /* +** Return true if the buffer z[0..n-1] contains all spaces. +*/ +static int allSpaces(const char *z, int n){ + while( n>0 && z[--n]==' ' ){} + return n==0; +} + +/* ** This is the default collating function named "BINARY" which is always ** available. +** +** If the padFlag argument is not NULL then space padding at the end +** of strings is ignored. This implements the RTRIM collation. */ static int binCollFunc( - void *NotUsed, + void *padFlag, int nKey1, const void *pKey1, int nKey2, const void *pKey2 ){ @@ -71999,7 +74823,14 @@ static int binCollFunc( n = nKey1mutex); @@ -72087,21 +74918,7 @@ SQLITE_API int sqlite3_close(sqlite3 *db){ sqlite3_mutex_leave(db->mutex); return SQLITE_BUSY; } - assert( !sqlite3SafetyCheck(db) ); - - /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database - ** cannot be opened for some reason. So this routine needs to run in - ** that case. But maybe there should be an extra magic value for the - ** "failed to open" state. - ** - ** TODO: Coverage tests do not test the case where this condition is - ** true. It's hard to see how to cause it without messing with threads. - */ - if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ - /* printf("DID NOT CLOSE\n"); fflush(stdout); */ - sqlite3_mutex_leave(db->mutex); - return SQLITE_ERROR; - } + assert( sqlite3SafetyCheckSickOrOk(db) ); for(j=0; jnDb; j++){ struct Db *pDb = &db->aDb[j]; @@ -72163,6 +74980,7 @@ SQLITE_API int sqlite3_close(sqlite3 *db){ */ sqlite3_free(db->aDb[1].pSchema); sqlite3_mutex_leave(db->mutex); + db->magic = SQLITE_MAGIC_CLOSED; sqlite3_mutex_free(db->mutex); sqlite3_free(db); return SQLITE_OK; @@ -72175,7 +74993,7 @@ SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){ int i; int inTrans = 0; assert( sqlite3_mutex_held(db->mutex) ); - sqlite3MallocEnterBenignBlock(1); /* Enter benign region */ + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 1); for(i=0; inDb; i++){ if( db->aDb[i].pBt ){ if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ @@ -72186,7 +75004,7 @@ SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){ } } sqlite3VtabRollback(db); - sqlite3MallocLeaveBenignBlock(); /* Leave benign region */ + sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0); if( db->flags&SQLITE_InternChanges ){ sqlite3ExpirePreparedStatements(db); @@ -72310,9 +75128,6 @@ SQLITE_API int sqlite3_busy_handler( int (*xBusy)(void*,int), void *pArg ){ - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } sqlite3_mutex_enter(db->mutex); db->busyHandler.xFunc = xBusy; db->busyHandler.pArg = pArg; @@ -72333,7 +75148,7 @@ SQLITE_API void sqlite3_progress_handler( int (*xProgress)(void*), void *pArg ){ - if( !sqlite3SafetyCheck(db) ){ + if( sqlite3SafetyCheckOk(db) ){ sqlite3_mutex_enter(db->mutex); if( nOps>0 ){ db->xProgress = xProgress; @@ -72355,9 +75170,6 @@ SQLITE_API void sqlite3_progress_handler( ** specified number of milliseconds before returning 0. */ SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } if( ms>0 ){ db->busyTimeout = ms; sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); @@ -72371,7 +75183,7 @@ SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ ** Cause any pending operation to stop at its earliest opportunity. */ SQLITE_API void sqlite3_interrupt(sqlite3 *db){ - if( db && (db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_BUSY) ){ + if( sqlite3SafetyCheckOk(db) ){ db->u1.isInterrupted = 1; } } @@ -72397,9 +75209,6 @@ SQLITE_PRIVATE int sqlite3CreateFunc( int nName; assert( sqlite3_mutex_held(db->mutex) ); - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } if( zFunctionName==0 || (xFunc && (xFinal || xStep)) || (!xFunc && (xFinal && !xStep)) || @@ -72721,7 +75530,7 @@ SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){ if( !db ){ return sqlite3ErrStr(SQLITE_NOMEM); } - if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ + if( !sqlite3SafetyCheckSickOrOk(db) || db->errCode==SQLITE_MISUSE ){ return sqlite3ErrStr(SQLITE_MISUSE); } sqlite3_mutex_enter(db->mutex); @@ -72763,7 +75572,7 @@ SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){ if( !db ){ return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); } - if( sqlite3SafetyCheck(db) || db->errCode==SQLITE_MISUSE ){ + if( !sqlite3SafetyCheckSickOrOk(db) || db->errCode==SQLITE_MISUSE ){ return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); } sqlite3_mutex_enter(db->mutex); @@ -72785,12 +75594,12 @@ SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){ ** passed to this function, we assume a malloc() failed during sqlite3_open(). */ SQLITE_API int sqlite3_errcode(sqlite3 *db){ + if( db && !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE; + } if( !db || db->mallocFailed ){ return SQLITE_NOMEM; } - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } return db->errCode & db->errMask; } @@ -72809,9 +75618,6 @@ static int createCollation( CollSeq *pColl; int enc2; - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } assert( sqlite3_mutex_held(db->mutex) ); /* If SQLITE_UTF16 is specified as the encoding type, transform this @@ -72889,6 +75695,17 @@ static int openDatabase( int rc; CollSeq *pColl; + /* Remove harmful bits from the flags parameter */ + flags &= ~( SQLITE_OPEN_DELETEONCLOSE | + SQLITE_OPEN_MAIN_DB | + SQLITE_OPEN_TEMP_DB | + SQLITE_OPEN_TRANSIENT_DB | + SQLITE_OPEN_MAIN_JOURNAL | + SQLITE_OPEN_TEMP_JOURNAL | + SQLITE_OPEN_SUBJOURNAL | + SQLITE_OPEN_MASTER_JOURNAL + ); + /* Allocate the sqlite data structure */ db = sqlite3MallocZero( sizeof(sqlite3) ); if( db==0 ) goto opendb_out; @@ -72923,8 +75740,8 @@ static int openDatabase( db->pVfs = sqlite3_vfs_find(zVfs); if( !db->pVfs ){ rc = SQLITE_ERROR; - db->magic = SQLITE_MAGIC_CLOSED; - sqlite3Error(db, rc, "no such vfs: %s", (zVfs?zVfs:"(null)")); + db->magic = SQLITE_MAGIC_SICK; + sqlite3Error(db, rc, "no such vfs: %s", zVfs); goto opendb_out; } @@ -72932,13 +75749,15 @@ static int openDatabase( ** and UTF-16, so add a version for each to avoid any unnecessary ** conversions. The only error that can occur here is a malloc() failure. */ - if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) || - createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) || - createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) || + createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0); + createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0); + createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0); + createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); + if( db->mallocFailed || (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 ){ assert( db->mallocFailed ); - db->magic = SQLITE_MAGIC_CLOSED; + db->magic = SQLITE_MAGIC_SICK; goto opendb_out; } @@ -72959,7 +75778,7 @@ static int openDatabase( &db->aDb[0].pBt); if( rc!=SQLITE_OK ){ sqlite3Error(db, rc, 0); - db->magic = SQLITE_MAGIC_CLOSED; + db->magic = SQLITE_MAGIC_SICK; goto opendb_out; } db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); @@ -73012,7 +75831,6 @@ static int openDatabase( #ifdef SQLITE_ENABLE_FTS3 if( !db->mallocFailed && rc==SQLITE_OK ){ - extern int sqlite3Fts3Init(sqlite3*); rc = sqlite3Fts3Init(db); } #endif @@ -73036,7 +75854,8 @@ static int openDatabase( #endif opendb_out: - if( db && db->mutex ){ + if( db ){ + assert( db->mutex!=0 ); sqlite3_mutex_leave(db->mutex); } if( SQLITE_NOMEM==(rc = sqlite3_errcode(db)) ){ @@ -73087,7 +75906,8 @@ SQLITE_API int sqlite3_open16( if( zFilename8 ){ rc = openDatabase(zFilename8, ppDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); - if( rc==SQLITE_OK && *ppDb ){ + assert( *ppDb || rc==SQLITE_NOMEM ); + if( rc==SQLITE_OK ){ rc = sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); if( rc!=SQLITE_OK ){ sqlite3_close(*ppDb); @@ -73152,7 +75972,7 @@ SQLITE_API int sqlite3_create_collation16( int(*xCompare)(void*,int,const void*,int,const void*) ){ int rc = SQLITE_OK; - char *zName8; + char *zName8; sqlite3_mutex_enter(db->mutex); assert( !db->mallocFailed ); zName8 = sqlite3Utf16to8(db, zName, -1); @@ -73175,9 +75995,6 @@ SQLITE_API int sqlite3_collation_needed( void *pCollNeededArg, void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) ){ - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } sqlite3_mutex_enter(db->mutex); db->xCollNeeded = xCollNeeded; db->xCollNeeded16 = 0; @@ -73196,9 +76013,6 @@ SQLITE_API int sqlite3_collation_needed16( void *pCollNeededArg, void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) ){ - if( sqlite3SafetyCheck(db) ){ - return SQLITE_MISUSE; - } sqlite3_mutex_enter(db->mutex); db->xCollNeeded = 0; db->xCollNeeded16 = xCollNeeded16; @@ -73280,11 +76094,11 @@ SQLITE_API int sqlite3_table_column_metadata( int autoinc = 0; /* Ensure the database schema has been loaded */ - if( sqlite3SafetyOn(db) ){ - return SQLITE_MISUSE; - } + (void)sqlite3SafetyOn(db); sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); rc = sqlite3Init(db, &zErrMsg); + sqlite3BtreeLeaveAll(db); if( SQLITE_OK!=rc ){ goto error_out; } @@ -73340,9 +76154,7 @@ SQLITE_API int sqlite3_table_column_metadata( } error_out: - if( sqlite3SafetyOff(db) ){ - rc = SQLITE_MISUSE; - } + (void)sqlite3SafetyOff(db); /* Whether the function call succeeded or failed, set the output parameters ** to whatever their local counterparts contain. If an error did occur, @@ -73410,13 +76222,14 @@ SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, vo Btree *pBtree = db->aDb[iDb].pBt; if( pBtree ){ Pager *pPager; + sqlite3_file *fd; sqlite3BtreeEnter(pBtree); pPager = sqlite3BtreePager(pBtree); - if( pPager ){ - sqlite3_file *fd = sqlite3PagerFile(pPager); - if( fd ){ - rc = sqlite3OsFileControl(fd, op, pArg); - } + assert( pPager!=0 ); + fd = sqlite3PagerFile(pPager); + assert( fd!=0 ); + if( fd->pMethods ){ + rc = sqlite3OsFileControl(fd, op, pArg); } sqlite3BtreeLeave(pBtree); } @@ -73425,17 +76238,44 @@ SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, vo return rc; } -/************** End of main.c ************************************************/ -/****************************************************************************** -** This file is an amalgamation of separate C source files from the SQLite -** Full Text Search extension 2 (fts3). By combining all the individual C -** code files into this single large file, the entire code can be compiled -** as a one translation unit. This allows many compilers to do optimizations -** that would not be possible if the files were compiled separately. It also -** makes the code easier to import into other projects. -** -** This amalgamation was generated on 2007-12-14 17:40:57 UTC. +/* +** Interface to the testing logic. */ +SQLITE_API int sqlite3_test_control(int op, ...){ + va_list ap; + int rc = 0; + va_start(ap, op); + switch( op ){ +#ifndef SQLITE_OMIT_FAULTINJECTOR + case SQLITE_TESTCTRL_FAULT_CONFIG: { + int id = va_arg(ap, int); + int nDelay = va_arg(ap, int); + int nRepeat = va_arg(ap, int); + sqlite3FaultConfig(id, nDelay, nRepeat); + break; + } + case SQLITE_TESTCTRL_FAULT_FAILURES: { + int id = va_arg(ap, int); + rc = sqlite3FaultFailures(id); + break; + } + case SQLITE_TESTCTRL_FAULT_BENIGN_FAILURES: { + int id = va_arg(ap, int); + rc = sqlite3FaultBenignFailures(id); + break; + } + case SQLITE_TESTCTRL_FAULT_PENDING: { + int id = va_arg(ap, int); + rc = sqlite3FaultPending(id); + break; + } +#endif /* SQLITE_OMIT_FAULTINJECTOR */ + } + va_end(ap); + return rc; +} + +/************** End of main.c ************************************************/ /************** Begin file fts3.c ********************************************/ /* ** 2006 Oct 10 @@ -73717,16 +76557,11 @@ SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, vo # define SQLITE_CORE 1 #endif -#include -#include -#include -#include -#include -/************** Include fts3.h in the middle of fts3.c ***********************/ -/************** Begin file fts3.h ********************************************/ +/************** Include fts3_hash.h in the middle of fts3.c ******************/ +/************** Begin file fts3_hash.h ***************************************/ /* -** 2006 Oct 10 +** 2001 September 22 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: @@ -73735,40026 +76570,7851 @@ SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, vo ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** -****************************************************************************** +************************************************************************* +** This is the header file for the generic hash-table implemenation +** used in SQLite. We've modified it slightly to serve as a standalone +** hash table implementation for the full-text indexing module. ** -** This header file is used by programs that want to link against the -** FTS3 library. All it does is declare the sqlite3Fts3Init() interface. */ -/************** Include sqlite3.h in the middle of fts3.h ********************/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. +#ifndef _FTS3_HASH_H_ +#define _FTS3_HASH_H_ + +/* Forward declarations of structures. */ +typedef struct fts3Hash fts3Hash; +typedef struct fts3HashElem fts3HashElem; + +/* A complete hash table is an instance of the following structure. +** The internals of this structure are intended to be opaque -- client +** code should not attempt to access or modify the fields of this structure +** directly. Change this structure only by using the routines below. +** However, many of the "procedures" and "functions" for modifying and +** accessing this structure are really macros, so we can't really make +** this structure opaque. +*/ +struct fts3Hash { + char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ + char copyKey; /* True if copy of key made on insert */ + int count; /* Number of entries in this table */ + fts3HashElem *first; /* The first element of the array */ + int htsize; /* Number of buckets in the hash table */ + struct _fts3ht { /* the hash table */ + int count; /* Number of entries with this hash */ + fts3HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; +}; + +/* Each element in the hash table is an instance of the following +** structure. All elements are stored on a single doubly-linked list. ** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. +** Again, this structure is intended to be opaque, but it can't really +** be opaque because it is used by macros. +*/ +struct fts3HashElem { + fts3HashElem *next, *prev; /* Next and previous elements in the table */ + void *data; /* Data associated with this element */ + void *pKey; int nKey; /* Key associated with this element */ +}; + +/* +** There are 2 different modes of operation for a hash table: ** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. +** FTS3_HASH_STRING pKey points to a string that is nKey bytes long +** (including the null-terminator, if any). Case +** is respected in comparisons. ** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. +** FTS3_HASH_BINARY pKey points to binary data nKey bytes long. +** memcmp() is used to compare keys. ** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ +** A copy of the key is made if the copyKey parameter to fts3HashInit is 1. */ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ -#include /* Needed for the definition of va_list */ +#define FTS3_HASH_STRING 1 +#define FTS3_HASH_BINARY 2 /* -** Make sure we can call this stuff from C++. +** Access routines. To delete, insert a NULL pointer. */ -#if 0 -extern "C" { -#endif +SQLITE_PRIVATE void sqlite3Fts3HashInit(fts3Hash*, int keytype, int copyKey); +SQLITE_PRIVATE void *sqlite3Fts3HashInsert(fts3Hash*, const void *pKey, int nKey, void *pData); +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const fts3Hash*, const void *pKey, int nKey); +SQLITE_PRIVATE void sqlite3Fts3HashClear(fts3Hash*); +/* +** Shorthand for the functions above +*/ +#define fts3HashInit sqlite3Fts3HashInit +#define fts3HashInsert sqlite3Fts3HashInsert +#define fts3HashFind sqlite3Fts3HashFind +#define fts3HashClear sqlite3Fts3HashClear /* -** Add the ability to override 'extern' +** Macros for looping over all elements of a hash table. The idiom is +** like this: +** +** fts3Hash h; +** fts3HashElem *p; +** ... +** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){ +** SomeStructure *pData = fts3HashData(p); +** // do something with pData +** } */ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif +#define fts3HashFirst(H) ((H)->first) +#define fts3HashNext(E) ((E)->next) +#define fts3HashData(E) ((E)->data) +#define fts3HashKey(E) ((E)->pKey) +#define fts3HashKeysize(E) ((E)->nKey) /* -** Make sure these symbols where not defined by some previous header -** file. +** Number of entries in a hash table */ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif +#define fts3HashCount(H) ((H)->count) + +#endif /* _FTS3_HASH_H_ */ +/************** End of fts3_hash.h *******************************************/ +/************** Continuing where we left off in fts3.c ***********************/ +/************** Include fts3_tokenizer.h in the middle of fts3.c *************/ +/************** Begin file fts3_tokenizer.h **********************************/ /* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". +** 2006 July 10 ** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. +** The author disclaims copyright to this source code. ** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). +************************************************************************* +** Defines the interface to tokenizers used by fulltext-search. There +** are three basic components: ** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 - -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} +** sqlite3_tokenizer_module is a singleton defining the tokenizer +** interface functions. This is essentially the class structure for +** tokenizers. ** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. +** sqlite3_tokenizer is used to define a particular tokenizer, perhaps +** including customization information defined at creation time. ** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. +** sqlite3_tokenizer_cursor is generated by a tokenizer to generate +** tokens from a particular input. */ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); +#ifndef _FTS3_TOKENIZER_H_ +#define _FTS3_TOKENIZER_H_ -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. +/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. +** If tokenizers are to be allowed to call sqlite3_*() functions, then +** we will need a way to register the API consistently. */ -int sqlite3_threadsafe(void); /* -** CAPI3REF: Database Connection Handle {F12000} +** Structures used by the tokenizer interface. When a new tokenizer +** implementation is registered, the caller provides a pointer to +** an sqlite3_tokenizer_module containing pointers to the callback +** functions that make up an implementation. ** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this +** When an fts3 table is created, it passes any arguments passed to +** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the +** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer +** implementation. The xCreate() function in turn returns an +** sqlite3_tokenizer structure representing the specific tokenizer to +** be used for the fts3 table (customized by the tokenizer clause arguments). +** +** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() +** method is called. It returns an sqlite3_tokenizer_cursor object +** that may be used to tokenize a specific input buffer based on +** the tokenization rules supplied by a specific sqlite3_tokenizer ** object. */ -typedef struct sqlite3 sqlite3; +typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; +typedef struct sqlite3_tokenizer sqlite3_tokenizer; +typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; +struct sqlite3_tokenizer_module { -/* -** CAPI3REF: 64-Bit Integer Types {F10200} + /* + ** Structure version. Should always be set to 0. + */ + int iVersion; + + /* + ** Create a new tokenizer. The values in the argv[] array are the + ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL + ** TABLE statement that created the fts3 table. For example, if + ** the following SQL is executed: + ** + ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) + ** + ** then argc is set to 2, and the argv[] array contains pointers + ** to the strings "arg1" and "arg2". + ** + ** This method should return either SQLITE_OK (0), or an SQLite error + ** code. If SQLITE_OK is returned, then *ppTokenizer should be set + ** to point at the newly created tokenizer structure. The generic + ** sqlite3_tokenizer.pModule variable should not be initialised by + ** this callback. The caller will do so. + */ + int (*xCreate)( + int argc, /* Size of argv array */ + const char *const*argv, /* Tokenizer argument strings */ + sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ + ); + + /* + ** Destroy an existing tokenizer. The fts3 module calls this method + ** exactly once for each successful call to xCreate(). + */ + int (*xDestroy)(sqlite3_tokenizer *pTokenizer); + + /* + ** Create a tokenizer cursor to tokenize an input buffer. The caller + ** is responsible for ensuring that the input buffer remains valid + ** until the cursor is closed (using the xClose() method). + */ + int (*xOpen)( + sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ + const char *pInput, int nBytes, /* Input buffer */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ + ); + + /* + ** Destroy an existing tokenizer cursor. The fts3 module calls this + ** method exactly once for each successful call to xOpen(). + */ + int (*xClose)(sqlite3_tokenizer_cursor *pCursor); + + /* + ** Retrieve the next token from the tokenizer cursor pCursor. This + ** method should either return SQLITE_OK and set the values of the + ** "OUT" variables identified below, or SQLITE_DONE to indicate that + ** the end of the buffer has been reached, or an SQLite error code. + ** + ** *ppToken should be set to point at a buffer containing the + ** normalized version of the token (i.e. after any case-folding and/or + ** stemming has been performed). *pnBytes should be set to the length + ** of this buffer in bytes. The input text that generated the token is + ** identified by the byte offsets returned in *piStartOffset and + ** *piEndOffset. + ** + ** The buffer *ppToken is set to point at is managed by the tokenizer + ** implementation. It is only required to be valid until the next call + ** to xNext() or xClose(). + */ + /* TODO(shess) current implementation requires pInput to be + ** nul-terminated. This should either be fixed, or pInput/nBytes + ** should be converted to zInput. + */ + int (*xNext)( + sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ + const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ + int *piStartOffset, /* OUT: Byte offset of token in input buffer */ + int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ + int *piPosition /* OUT: Number of tokens returned before this one */ + ); +}; + +struct sqlite3_tokenizer { + const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ + /* Tokenizer implementations will typically add additional fields */ +}; + +struct sqlite3_tokenizer_cursor { + sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ + /* Tokenizer implementations will typically add additional fields */ +}; + +#endif /* _FTS3_TOKENIZER_H_ */ + +/************** End of fts3_tokenizer.h **************************************/ +/************** Continuing where we left off in fts3.c ***********************/ +#ifndef SQLITE_CORE + SQLITE_EXTENSION_INIT1 +#endif + + +/* TODO(shess) MAN, this thing needs some refactoring. At minimum, it +** would be nice to order the file better, perhaps something along the +** lines of: ** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} +** - utility functions +** - table setup functions +** - table update functions +** - table query functions ** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. +** Put the query functions last because they're likely to reference +** typedefs or functions from the table update section. */ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; + +#if 0 +# define FTSTRACE(A) printf A; fflush(stdout) #else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; +# define FTSTRACE(A) #endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; /* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point +** Default span for NEAR operators. */ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif +#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 + +/* It is not safe to call isspace(), tolower(), or isalnum() on +** hi-bit-set characters. This is the same solution used in the +** tokenizer. +*/ +/* TODO(shess) The snippet-generation code should be using the +** tokenizer-generated tokens rather than doing its own local +** tokenization. +*/ +/* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */ +static int safe_isspace(char c){ + return (c&0x80)==0 ? isspace(c) : 0; +} +static int safe_tolower(char c){ + return (c&0x80)==0 ? tolower(c) : c; +} +static int safe_isalnum(char c){ + return (c&0x80)==0 ? isalnum(c) : 0; +} + +typedef enum DocListType { + DL_DOCIDS, /* docids only */ + DL_POSITIONS, /* docids + positions */ + DL_POSITIONS_OFFSETS /* docids + positions + offsets */ +} DocListType; /* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. +** By default, only positions and not offsets are stored in the doclists. +** To change this so that offsets are stored too, compile with ** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} +** -DDL_DEFAULT=DL_POSITIONS_OFFSETS ** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. +** If DL_DEFAULT is set to DL_DOCIDS, your table can only be inserted +** into (no deletes or updates). */ -int sqlite3_close(sqlite3 *); +#ifndef DL_DEFAULT +# define DL_DEFAULT DL_POSITIONS +#endif -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. +enum { + POS_END = 0, /* end of this position list */ + POS_COLUMN, /* followed by new column number */ + POS_BASE +}; + +/* MERGE_COUNT controls how often we merge segments (see comment at +** top of file). */ -typedef int (*sqlite3_callback)(void*,int,char**, char**); +#define MERGE_COUNT 16 -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); +/* utility functions */ -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] +/* CLEAR() and SCRAMBLE() abstract memset() on a pointer to a single +** record to prevent errors of the form: ** +** my_function(SomeType *b){ +** memset(b, '\0', sizeof(b)); // sizeof(b)!=sizeof(*b) +** } */ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ +/* TODO(shess) Obvious candidates for a header file. */ +#define CLEAR(b) memset(b, '\0', sizeof(*(b))) -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) +#ifndef NDEBUG +# define SCRAMBLE(b) memset(b, 0x55, sizeof(*(b))) +#else +# define SCRAMBLE(b) +#endif -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 +/* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */ +#define VARINT_MAX 10 -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 +/* Write a 64-bit variable-length integer to memory starting at p[0]. + * The length of data written will be between 1 and VARINT_MAX bytes. + * The number of bytes written is returned. */ +static int fts3PutVarint(char *p, sqlite_int64 v){ + unsigned char *q = (unsigned char *) p; + sqlite_uint64 vu = v; + do{ + *q++ = (unsigned char) ((vu & 0x7f) | 0x80); + vu >>= 7; + }while( vu!=0 ); + q[-1] &= 0x7f; /* turn off high bit in final byte */ + assert( q - (unsigned char *)p <= VARINT_MAX ); + return (int) (q - (unsigned char *)p); +} -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} -*/ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 +/* Read a 64-bit variable-length integer from memory starting at p[0]. + * Return the number of bytes read, or 0 on error. + * The value is stored in *v. */ +static int fts3GetVarint(const char *p, sqlite_int64 *v){ + const unsigned char *q = (const unsigned char *) p; + sqlite_uint64 x = 0, y = 1; + while( (*q & 0x80) == 0x80 ){ + x += y * (*q++ & 0x7f); + y <<= 7; + if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */ + assert( 0 ); + return 0; + } + } + x += y * (*q++); + *v = (sqlite_int64) x; + return (int) (q - (unsigned char *)p); +} -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. +static int fts3GetVarint32(const char *p, int *pi){ + sqlite_int64 i; + int ret = fts3GetVarint(p, &i); + *pi = (int) i; + assert( *pi==i ); + return ret; +} + +/*******************************************************************/ +/* DataBuffer is used to collect data into a buffer in piecemeal +** fashion. It implements the usual distinction between amount of +** data currently stored (nData) and buffer capacity (nCapacity). ** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). +** dataBufferInit - create a buffer with given initial capacity. +** dataBufferReset - forget buffer's data, retaining capacity. +** dataBufferDestroy - free buffer's data. +** dataBufferSwap - swap contents of two buffers. +** dataBufferExpand - expand capacity without adding data. +** dataBufferAppend - append data. +** dataBufferAppend2 - append two pieces of data at once. +** dataBufferReplace - replace buffer's data. */ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 +typedef struct DataBuffer { + char *pData; /* Pointer to malloc'ed buffer. */ + int nCapacity; /* Size of pData buffer. */ + int nData; /* End of data loaded into pData. */ +} DataBuffer; + +static void dataBufferInit(DataBuffer *pBuffer, int nCapacity){ + assert( nCapacity>=0 ); + pBuffer->nData = 0; + pBuffer->nCapacity = nCapacity; + pBuffer->pData = nCapacity==0 ? NULL : sqlite3_malloc(nCapacity); +} +static void dataBufferReset(DataBuffer *pBuffer){ + pBuffer->nData = 0; +} +static void dataBufferDestroy(DataBuffer *pBuffer){ + if( pBuffer->pData!=NULL ) sqlite3_free(pBuffer->pData); + SCRAMBLE(pBuffer); +} +static void dataBufferSwap(DataBuffer *pBuffer1, DataBuffer *pBuffer2){ + DataBuffer tmp = *pBuffer1; + *pBuffer1 = *pBuffer2; + *pBuffer2 = tmp; +} +static void dataBufferExpand(DataBuffer *pBuffer, int nAddCapacity){ + assert( nAddCapacity>0 ); + /* TODO(shess) Consider expanding more aggressively. Note that the + ** underlying malloc implementation may take care of such things for + ** us already. + */ + if( pBuffer->nData+nAddCapacity>pBuffer->nCapacity ){ + pBuffer->nCapacity = pBuffer->nData+nAddCapacity; + pBuffer->pData = sqlite3_realloc(pBuffer->pData, pBuffer->nCapacity); + } +} +static void dataBufferAppend(DataBuffer *pBuffer, + const char *pSource, int nSource){ + assert( nSource>0 && pSource!=NULL ); + dataBufferExpand(pBuffer, nSource); + memcpy(pBuffer->pData+pBuffer->nData, pSource, nSource); + pBuffer->nData += nSource; +} +static void dataBufferAppend2(DataBuffer *pBuffer, + const char *pSource1, int nSource1, + const char *pSource2, int nSource2){ + assert( nSource1>0 && pSource1!=NULL ); + assert( nSource2>0 && pSource2!=NULL ); + dataBufferExpand(pBuffer, nSource1+nSource2); + memcpy(pBuffer->pData+pBuffer->nData, pSource1, nSource1); + memcpy(pBuffer->pData+pBuffer->nData+nSource1, pSource2, nSource2); + pBuffer->nData += nSource1+nSource2; +} +static void dataBufferReplace(DataBuffer *pBuffer, + const char *pSource, int nSource){ + dataBufferReset(pBuffer); + dataBufferAppend(pBuffer, pSource, nSource); +} +/* StringBuffer is a null-terminated version of DataBuffer. */ +typedef struct StringBuffer { + DataBuffer b; /* Includes null terminator. */ +} StringBuffer; -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. +static void initStringBuffer(StringBuffer *sb){ + dataBufferInit(&sb->b, 100); + dataBufferReplace(&sb->b, "", 1); +} +static int stringBufferLength(StringBuffer *sb){ + return sb->b.nData-1; +} +static char *stringBufferData(StringBuffer *sb){ + return sb->b.pData; +} +static void stringBufferDestroy(StringBuffer *sb){ + dataBufferDestroy(&sb->b); +} + +static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){ + assert( sb->b.nData>0 ); + if( nFrom>0 ){ + sb->b.nData--; + dataBufferAppend2(&sb->b, zFrom, nFrom, "", 1); + } +} +static void append(StringBuffer *sb, const char *zFrom){ + nappend(sb, zFrom, strlen(zFrom)); +} + +/* Append a list of strings separated by commas. */ +static void appendList(StringBuffer *sb, int nString, char **azString){ + int i; + for(i=0; i0 ) append(sb, ", "); + append(sb, azString[i]); + } +} + +static int endsInWhiteSpace(StringBuffer *p){ + return stringBufferLength(p)>0 && + safe_isspace(stringBufferData(p)[stringBufferLength(p)-1]); +} + +/* If the StringBuffer ends in something other than white space, add a +** single space character to the end. */ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; +static void appendWhiteSpace(StringBuffer *p){ + if( stringBufferLength(p)==0 ) return; + if( !endsInWhiteSpace(p) ) append(p, " "); +} -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. +/* Remove white space from the end of the StringBuffer */ +static void trimWhiteSpace(StringBuffer *p){ + while( endsInWhiteSpace(p) ){ + p->b.pData[--p->b.nData-1] = '\0'; + } +} + +/*******************************************************************/ +/* DLReader is used to read document elements from a doclist. The +** current docid is cached, so dlrDocid() is fast. DLReader does not +** own the doclist buffer. ** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: +** dlrAtEnd - true if there's no more data to read. +** dlrDocid - docid of current document. +** dlrDocData - doclist data for current document (including docid). +** dlrDocDataBytes - length of same. +** dlrAllDataBytes - length of all remaining data. +** dlrPosData - position data for current document. +** dlrPosDataLen - length of pos data for current document (incl POS_END). +** dlrStep - step to current document. +** dlrInit - initial for doclist of given type against given data. +** dlrDestroy - clean up. ** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    +** Expected usage is something like: ** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). +** DLReader reader; +** dlrInit(&reader, pData, nData); +** while( !dlrAtEnd(&reader) ){ +** // calls to dlrDocid() and kin. +** dlrStep(&reader); +** } +** dlrDestroy(&reader); */ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; +typedef struct DLReader { + DocListType iType; + const char *pData; + int nData; -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 + sqlite_int64 iDocid; + int nElement; +} DLReader; -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. +static int dlrAtEnd(DLReader *pReader){ + assert( pReader->nData>=0 ); + return pReader->nData==0; +} +static sqlite_int64 dlrDocid(DLReader *pReader){ + assert( !dlrAtEnd(pReader) ); + return pReader->iDocid; +} +static const char *dlrDocData(DLReader *pReader){ + assert( !dlrAtEnd(pReader) ); + return pReader->pData; +} +static int dlrDocDataBytes(DLReader *pReader){ + assert( !dlrAtEnd(pReader) ); + return pReader->nElement; +} +static int dlrAllDataBytes(DLReader *pReader){ + assert( !dlrAtEnd(pReader) ); + return pReader->nData; +} +/* TODO(shess) Consider adding a field to track iDocid varint length +** to make these two functions faster. This might matter (a tiny bit) +** for queries. */ -typedef struct sqlite3_mutex sqlite3_mutex; +static const char *dlrPosData(DLReader *pReader){ + sqlite_int64 iDummy; + int n = fts3GetVarint(pReader->pData, &iDummy); + assert( !dlrAtEnd(pReader) ); + return pReader->pData+n; +} +static int dlrPosDataLen(DLReader *pReader){ + sqlite_int64 iDummy; + int n = fts3GetVarint(pReader->pData, &iDummy); + assert( !dlrAtEnd(pReader) ); + return pReader->nElement-n; +} +static void dlrStep(DLReader *pReader){ + assert( !dlrAtEnd(pReader) ); -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; + /* Skip past current doclist element. */ + assert( pReader->nElement<=pReader->nData ); + pReader->pData += pReader->nElement; + pReader->nData -= pReader->nElement; -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 + /* If there is more data, read the next doclist element. */ + if( pReader->nData!=0 ){ + sqlite_int64 iDocidDelta; + int iDummy, n = fts3GetVarint(pReader->pData, &iDocidDelta); + pReader->iDocid += iDocidDelta; + if( pReader->iType>=DL_POSITIONS ){ + assert( nnData ); + while( 1 ){ + n += fts3GetVarint32(pReader->pData+n, &iDummy); + assert( n<=pReader->nData ); + if( iDummy==POS_END ) break; + if( iDummy==POS_COLUMN ){ + n += fts3GetVarint32(pReader->pData+n, &iDummy); + assert( nnData ); + }else if( pReader->iType==DL_POSITIONS_OFFSETS ){ + n += fts3GetVarint32(pReader->pData+n, &iDummy); + n += fts3GetVarint32(pReader->pData+n, &iDummy); + assert( nnData ); + } + } + } + pReader->nElement = n; + assert( pReader->nElement<=pReader->nData ); + } +} +static void dlrInit(DLReader *pReader, DocListType iType, + const char *pData, int nData){ + assert( pData!=NULL && nData!=0 ); + pReader->iType = iType; + pReader->pData = pData; + pReader->nData = nData; + pReader->nElement = 0; + pReader->iDocid = 0; -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); + /* Load the first element's data. There must be a first element. */ + dlrStep(pReader); +} +static void dlrDestroy(DLReader *pReader){ + SCRAMBLE(pReader); +} -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. +#ifndef NDEBUG +/* Verify that the doclist can be validly decoded. Also returns the +** last docid found because it is convenient in other assertions for +** DLWriter. */ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); +static void docListValidate(DocListType iType, const char *pData, int nData, + sqlite_int64 *pLastDocid){ + sqlite_int64 iPrevDocid = 0; + assert( nData>0 ); + assert( pData!=0 ); + assert( pData+nData>pData ); + while( nData!=0 ){ + sqlite_int64 iDocidDelta; + int n = fts3GetVarint(pData, &iDocidDelta); + iPrevDocid += iDocidDelta; + if( iType>DL_DOCIDS ){ + int iDummy; + while( 1 ){ + n += fts3GetVarint32(pData+n, &iDummy); + if( iDummy==POS_END ) break; + if( iDummy==POS_COLUMN ){ + n += fts3GetVarint32(pData+n, &iDummy); + }else if( iType>DL_POSITIONS ){ + n += fts3GetVarint32(pData+n, &iDummy); + n += fts3GetVarint32(pData+n, &iDummy); + } + assert( n<=nData ); + } + } + assert( n<=nData ); + pData += n; + nData -= n; + } + if( pLastDocid ) *pLastDocid = iPrevDocid; +} +#define ASSERT_VALID_DOCLIST(i, p, n, o) docListValidate(i, p, n, o) +#else +#define ASSERT_VALID_DOCLIST(i, p, n, o) assert( 1 ) +#endif -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. +/*******************************************************************/ +/* DLWriter is used to write doclist data to a DataBuffer. DLWriter +** always appends to the buffer and does not own it. ** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. +** dlwInit - initialize to write a given type doclistto a buffer. +** dlwDestroy - clear the writer's memory. Does not free buffer. +** dlwAppend - append raw doclist data to buffer. +** dlwCopy - copy next doclist from reader to writer. +** dlwAdd - construct doclist element and append to buffer. +** Only apply dlwAdd() to DL_DOCIDS doclists (else use PLWriter). */ -int sqlite3_changes(sqlite3*); +typedef struct DLWriter { + DocListType iType; + DataBuffer *b; + sqlite_int64 iPrevDocid; +#ifndef NDEBUG + int has_iPrevDocid; +#endif +} DLWriter; -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. +static void dlwInit(DLWriter *pWriter, DocListType iType, DataBuffer *b){ + pWriter->b = b; + pWriter->iType = iType; + pWriter->iPrevDocid = 0; +#ifndef NDEBUG + pWriter->has_iPrevDocid = 0; +#endif +} +static void dlwDestroy(DLWriter *pWriter){ + SCRAMBLE(pWriter); +} +/* iFirstDocid is the first docid in the doclist in pData. It is +** needed because pData may point within a larger doclist, in which +** case the first item would be delta-encoded. ** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} +** iLastDocid is the final docid in the doclist in pData. It is +** needed to create the new iPrevDocid for future delta-encoding. The +** code could decode the passed doclist to recreate iLastDocid, but +** the only current user (docListMerge) already has decoded this +** information. */ -int sqlite3_total_changes(sqlite3*); - -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. +/* TODO(shess) This has become just a helper for docListMerge. +** Consider a refactor to make this cleaner. */ -void sqlite3_interrupt(sqlite3*); +static void dlwAppend(DLWriter *pWriter, + const char *pData, int nData, + sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ + sqlite_int64 iDocid = 0; + char c[VARINT_MAX]; + int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */ +#ifndef NDEBUG + sqlite_int64 iLastDocidDelta; +#endif -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. + /* Recode the initial docid as delta from iPrevDocid. */ + nFirstOld = fts3GetVarint(pData, &iDocid); + assert( nFirstOldiType==DL_DOCIDS) ); + nFirstNew = fts3PutVarint(c, iFirstDocid-pWriter->iPrevDocid); + + /* Verify that the incoming doclist is valid AND that it ends with + ** the expected docid. This is essential because we'll trust this + ** docid in future delta-encoding. + */ + ASSERT_VALID_DOCLIST(pWriter->iType, pData, nData, &iLastDocidDelta); + assert( iLastDocid==iFirstDocid-iDocid+iLastDocidDelta ); + + /* Append recoded initial docid and everything else. Rest of docids + ** should have been delta-encoded from previous initial docid. + */ + if( nFirstOldb, c, nFirstNew, + pData+nFirstOld, nData-nFirstOld); + }else{ + dataBufferAppend(pWriter->b, c, nFirstNew); + } + pWriter->iPrevDocid = iLastDocid; +} +static void dlwCopy(DLWriter *pWriter, DLReader *pReader){ + dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), + dlrDocid(pReader), dlrDocid(pReader)); +} +static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){ + char c[VARINT_MAX]; + int n = fts3PutVarint(c, iDocid-pWriter->iPrevDocid); + + /* Docids must ascend. */ + assert( !pWriter->has_iPrevDocid || iDocid>pWriter->iPrevDocid ); + assert( pWriter->iType==DL_DOCIDS ); + + dataBufferAppend(pWriter->b, c, n); + pWriter->iPrevDocid = iDocid; +#ifndef NDEBUG + pWriter->has_iPrevDocid = 1; +#endif +} + +/*******************************************************************/ +/* PLReader is used to read data from a document's position list. As +** the caller steps through the list, data is cached so that varints +** only need to be decoded once. ** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} +** plrInit, plrDestroy - create/destroy a reader. +** plrColumn, plrPosition, plrStartOffset, plrEndOffset - accessors +** plrAtEnd - at end of stream, only call plrDestroy once true. +** plrStep - step to the next element. */ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); +typedef struct PLReader { + /* These refer to the next position's data. nData will reach 0 when + ** reading the last position, so plrStep() signals EOF by setting + ** pData to NULL. + */ + const char *pData; + int nData; -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. + DocListType iType; + int iColumn; /* the last column read */ + int iPosition; /* the last position read */ + int iStartOffset; /* the last start offset read */ + int iEndOffset; /* the last end offset read */ +} PLReader; + +static int plrAtEnd(PLReader *pReader){ + return pReader->pData==NULL; +} +static int plrColumn(PLReader *pReader){ + assert( !plrAtEnd(pReader) ); + return pReader->iColumn; +} +static int plrPosition(PLReader *pReader){ + assert( !plrAtEnd(pReader) ); + return pReader->iPosition; +} +static int plrStartOffset(PLReader *pReader){ + assert( !plrAtEnd(pReader) ); + return pReader->iStartOffset; +} +static int plrEndOffset(PLReader *pReader){ + assert( !plrAtEnd(pReader) ); + return pReader->iEndOffset; +} +static void plrStep(PLReader *pReader){ + int i, n; + + assert( !plrAtEnd(pReader) ); + + if( pReader->nData==0 ){ + pReader->pData = NULL; + return; + } + + n = fts3GetVarint32(pReader->pData, &i); + if( i==POS_COLUMN ){ + n += fts3GetVarint32(pReader->pData+n, &pReader->iColumn); + pReader->iPosition = 0; + pReader->iStartOffset = 0; + n += fts3GetVarint32(pReader->pData+n, &i); + } + /* Should never see adjacent column changes. */ + assert( i!=POS_COLUMN ); + + if( i==POS_END ){ + pReader->nData = 0; + pReader->pData = NULL; + return; + } + + pReader->iPosition += i-POS_BASE; + if( pReader->iType==DL_POSITIONS_OFFSETS ){ + n += fts3GetVarint32(pReader->pData+n, &i); + pReader->iStartOffset += i; + n += fts3GetVarint32(pReader->pData+n, &i); + pReader->iEndOffset = pReader->iStartOffset+i; + } + assert( n<=pReader->nData ); + pReader->pData += n; + pReader->nData -= n; +} + +static void plrInit(PLReader *pReader, DLReader *pDLReader){ + pReader->pData = dlrPosData(pDLReader); + pReader->nData = dlrPosDataLen(pDLReader); + pReader->iType = pDLReader->iType; + pReader->iColumn = 0; + pReader->iPosition = 0; + pReader->iStartOffset = 0; + pReader->iEndOffset = 0; + plrStep(pReader); +} +static void plrDestroy(PLReader *pReader){ + SCRAMBLE(pReader); +} + +/*******************************************************************/ +/* PLWriter is used in constructing a document's position list. As a +** convenience, if iType is DL_DOCIDS, PLWriter becomes a no-op. +** PLWriter writes to the associated DLWriter's buffer. ** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. +** plwInit - init for writing a document's poslist. +** plwDestroy - clear a writer. +** plwAdd - append position and offset information. +** plwCopy - copy next position's data from reader to writer. +** plwTerminate - add any necessary doclist terminator. ** -** {F12321} The default busy callback is NULL. {END} +** Calling plwAdd() after plwTerminate() may result in a corrupt +** doclist. +*/ +/* TODO(shess) Until we've written the second item, we can cache the +** first item's information. Then we'd have three states: ** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. +** - initialized with docid, no positions. +** - docid and one position. +** - docid and multiple positions. ** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. +** Only the last state needs to actually write to dlw->b, which would +** be an improvement in the DLCollector case. */ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); +typedef struct PLWriter { + DLWriter *dlw; -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. + int iColumn; /* the last column written */ + int iPos; /* the last position written */ + int iOffset; /* the last start offset written */ +} PLWriter; + +/* TODO(shess) In the case where the parent is reading these values +** from a PLReader, we could optimize to a copy if that PLReader has +** the same type as pWriter. */ -int sqlite3_busy_timeout(sqlite3*, int ms); +static void plwAdd(PLWriter *pWriter, int iColumn, int iPos, + int iStartOffset, int iEndOffset){ + /* Worst-case space for POS_COLUMN, iColumn, iPosDelta, + ** iStartOffsetDelta, and iEndOffsetDelta. + */ + char c[5*VARINT_MAX]; + int n = 0; -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); + /* Ban plwAdd() after plwTerminate(). */ + assert( pWriter->iPos!=-1 ); -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); + if( pWriter->dlw->iType==DL_DOCIDS ) return; -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); + if( iColumn!=pWriter->iColumn ){ + n += fts3PutVarint(c+n, POS_COLUMN); + n += fts3PutVarint(c+n, iColumn); + pWriter->iColumn = iColumn; + pWriter->iPos = 0; + pWriter->iOffset = 0; + } + assert( iPos>=pWriter->iPos ); + n += fts3PutVarint(c+n, POS_BASE+(iPos-pWriter->iPos)); + pWriter->iPos = iPos; + if( pWriter->dlw->iType==DL_POSITIONS_OFFSETS ){ + assert( iStartOffset>=pWriter->iOffset ); + n += fts3PutVarint(c+n, iStartOffset-pWriter->iOffset); + pWriter->iOffset = iStartOffset; + assert( iEndOffset>=iStartOffset ); + n += fts3PutVarint(c+n, iEndOffset-iStartOffset); + } + dataBufferAppend(pWriter->dlw->b, c, n); +} +static void plwCopy(PLWriter *pWriter, PLReader *pReader){ + plwAdd(pWriter, plrColumn(pReader), plrPosition(pReader), + plrStartOffset(pReader), plrEndOffset(pReader)); +} +static void plwInit(PLWriter *pWriter, DLWriter *dlw, sqlite_int64 iDocid){ + char c[VARINT_MAX]; + int n; -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); + pWriter->dlw = dlw; -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} -*/ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); + /* Docids must ascend. */ + assert( !pWriter->dlw->has_iPrevDocid || iDocid>pWriter->dlw->iPrevDocid ); + n = fts3PutVarint(c, iDocid-pWriter->dlw->iPrevDocid); + dataBufferAppend(pWriter->dlw->b, c, n); + pWriter->dlw->iPrevDocid = iDocid; +#ifndef NDEBUG + pWriter->dlw->has_iPrevDocid = 1; +#endif -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. + pWriter->iColumn = 0; + pWriter->iPos = 0; + pWriter->iOffset = 0; +} +/* TODO(shess) Should plwDestroy() also terminate the doclist? But +** then plwDestroy() would no longer be just a destructor, it would +** also be doing work, which isn't consistent with the overall idiom. +** Another option would be for plwAdd() to always append any necessary +** terminator, so that the output is always correct. But that would +** add incremental work to the common case with the only benefit being +** API elegance. Punt for now. */ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ +static void plwTerminate(PLWriter *pWriter){ + if( pWriter->dlw->iType>DL_DOCIDS ){ + char c[VARINT_MAX]; + int n = fts3PutVarint(c, POS_END); + dataBufferAppend(pWriter->dlw->b, c, n); + } +#ifndef NDEBUG + /* Mark as terminated for assert in plwAdd(). */ + pWriter->iPos = -1; +#endif +} +static void plwDestroy(PLWriter *pWriter){ + SCRAMBLE(pWriter); +} -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} +/*******************************************************************/ +/* DLCollector wraps PLWriter and DLWriter to provide a +** dynamically-allocated doclist area to use during tokenization. ** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. +** dlcNew - malloc up and initialize a collector. +** dlcDelete - destroy a collector and all contained items. +** dlcAddPos - append position and offset information. +** dlcAddDoclist - add the collected doclist to the given buffer. +** dlcNext - terminate the current document and open another. */ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ +typedef struct DLCollector { + DataBuffer b; + DLWriter dlw; + PLWriter plw; +} DLCollector; -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. +/* TODO(shess) This could also be done by calling plwTerminate() and +** dataBufferAppend(). I tried that, expecting nominal performance +** differences, but it seemed to pretty reliably be worth 1% to code +** it this way. I suspect it is the incremental malloc overhead (some +** percentage of the plwTerminate() calls will cause a realloc), so +** this might be worth revisiting if the DataBuffer implementation +** changes. */ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); +static void dlcAddDoclist(DLCollector *pCollector, DataBuffer *b){ + if( pCollector->dlw.iType>DL_DOCIDS ){ + char c[VARINT_MAX]; + int n = fts3PutVarint(c, POS_END); + dataBufferAppend2(b, pCollector->b.pData, pCollector->b.nData, c, n); + }else{ + dataBufferAppend(b, pCollector->b.pData, pCollector->b.nData); + } +} +static void dlcNext(DLCollector *pCollector, sqlite_int64 iDocid){ + plwTerminate(&pCollector->plw); + plwDestroy(&pCollector->plw); + plwInit(&pCollector->plw, &pCollector->dlw, iDocid); +} +static void dlcAddPos(DLCollector *pCollector, int iColumn, int iPos, + int iStartOffset, int iEndOffset){ + plwAdd(&pCollector->plw, iColumn, iPos, iStartOffset, iEndOffset); +} -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); +static DLCollector *dlcNew(sqlite_int64 iDocid, DocListType iType){ + DLCollector *pCollector = sqlite3_malloc(sizeof(DLCollector)); + dataBufferInit(&pCollector->b, 0); + dlwInit(&pCollector->dlw, iType, &pCollector->b); + plwInit(&pCollector->plw, &pCollector->dlw, iDocid); + return pCollector; +} +static void dlcDelete(DLCollector *pCollector){ + plwDestroy(&pCollector->plw); + dlwDestroy(&pCollector->dlw); + dataBufferDestroy(&pCollector->b); + SCRAMBLE(pCollector); + sqlite3_free(pCollector); +} -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. + +/* Copy the doclist data of iType in pData/nData into *out, trimming +** unnecessary data as we go. Only columns matching iColumn are +** copied, all columns copied if iColumn is -1. Elements with no +** matching columns are dropped. The output is an iOutType doclist. */ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); +/* NOTE(shess) This code is only valid after all doclists are merged. +** If this is run before merges, then doclist items which represent +** deletion will be trimmed, and will thus not effect a deletion +** during the merge. +*/ +static void docListTrim(DocListType iType, const char *pData, int nData, + int iColumn, DocListType iOutType, DataBuffer *out){ + DLReader dlReader; + DLWriter dlWriter; -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); + assert( iOutType<=iType ); -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; + dlrInit(&dlReader, iType, pData, nData); + dlwInit(&dlWriter, iOutType, out); -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    + while( !dlrAtEnd(&dlReader) ){ + PLReader plReader; + PLWriter plWriter; + int match = 0; + + plrInit(&plReader, &dlReader); + + while( !plrAtEnd(&plReader) ){ + if( iColumn==-1 || plrColumn(&plReader)==iColumn ){ + if( !match ){ + plwInit(&plWriter, &dlWriter, dlrDocid(&dlReader)); + match = 1; + } + plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader), + plrStartOffset(&plReader), plrEndOffset(&plReader)); + } + plrStep(&plReader); + } + if( match ){ + plwTerminate(&plWriter); + plwDestroy(&plWriter); + } + + plrDestroy(&plReader); + dlrStep(&dlReader); + } + dlwDestroy(&dlWriter); + dlrDestroy(&dlReader); +} + +/* Used by docListMerge() to keep doclists in the ascending order by +** docid, then ascending order by age (so the newest comes first). */ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); +typedef struct OrderedDLReader { + DLReader *pReader; -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. + /* TODO(shess) If we assume that docListMerge pReaders is ordered by + ** age (which we do), then we could use pReader comparisons to break + ** ties. + */ + int idx; +} OrderedDLReader; + +/* Order eof to end, then by docid asc, idx desc. */ +static int orderedDLReaderCmp(OrderedDLReader *r1, OrderedDLReader *r2){ + if( dlrAtEnd(r1->pReader) ){ + if( dlrAtEnd(r2->pReader) ) return 0; /* Both atEnd(). */ + return 1; /* Only r1 atEnd(). */ + } + if( dlrAtEnd(r2->pReader) ) return -1; /* Only r2 atEnd(). */ + + if( dlrDocid(r1->pReader)pReader) ) return -1; + if( dlrDocid(r1->pReader)>dlrDocid(r2->pReader) ) return 1; + + /* Descending on idx. */ + return r2->idx-r1->idx; +} + +/* Bubble p[0] to appropriate place in p[1..n-1]. Assumes that +** p[1..n-1] is already sorted. +*/ +/* TODO(shess) Is this frequent enough to warrant a binary search? +** Before implementing that, instrument the code to check. In most +** current usage, I expect that p[0] will be less than p[1] a very +** high proportion of the time. */ -const char *sqlite3_sql(sqlite3_stmt *pStmt); +static void orderedDLReaderReorder(OrderedDLReader *p, int n){ + while( n>1 && orderedDLReaderCmp(p, p+1)>0 ){ + OrderedDLReader tmp = p[0]; + p[0] = p[1]; + p[1] = tmp; + n--; + p++; + } +} -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. +/* Given an array of doclist readers, merge their doclist elements +** into out in sorted order (by docid), dropping elements from older +** readers when there is a duplicate docid. pReaders is assumed to be +** ordered by age, oldest first. */ -typedef struct Mem sqlite3_value; +/* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably +** be fixed. +*/ +static void docListMerge(DataBuffer *out, + DLReader *pReaders, int nReaders){ + OrderedDLReader readers[MERGE_COUNT]; + DLWriter writer; + int i, n; + const char *pStart = 0; + int nStart = 0; + sqlite_int64 iFirstDocid = 0, iLastDocid = 0; -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. + assert( nReaders>0 ); + if( nReaders==1 ){ + dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders)); + return; + } + + assert( nReaders<=MERGE_COUNT ); + n = 0; + for(i=0; i0 ){ + orderedDLReaderReorder(readers+i, nReaders-i); + } + + dlwInit(&writer, pReaders[0].iType, out); + while( !dlrAtEnd(readers[0].pReader) ){ + sqlite_int64 iDocid = dlrDocid(readers[0].pReader); + + /* If this is a continuation of the current buffer to copy, extend + ** that buffer. memcpy() seems to be more efficient if it has a + ** lots of data to copy. + */ + if( dlrDocData(readers[0].pReader)==pStart+nStart ){ + nStart += dlrDocDataBytes(readers[0].pReader); + }else{ + if( pStart!=0 ){ + dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); + } + pStart = dlrDocData(readers[0].pReader); + nStart = dlrDocDataBytes(readers[0].pReader); + iFirstDocid = iDocid; + } + iLastDocid = iDocid; + dlrStep(readers[0].pReader); + + /* Drop all of the older elements with the same docid. */ + for(i=1; i0 ){ + orderedDLReaderReorder(readers+i, nReaders-i); + } + } + + /* Copy over any remaining elements. */ + if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); + dlwDestroy(&writer); +} + +/* Helper function for posListUnion(). Compares the current position +** between left and right, returning as standard C idiom of <0 if +** left0 if left>right, and 0 if left==right. "End" always +** compares greater. */ -typedef struct sqlite3_context sqlite3_context; +static int posListCmp(PLReader *pLeft, PLReader *pRight){ + assert( pLeft->iType==pRight->iType ); + if( pLeft->iType==DL_DOCIDS ) return 0; -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. + if( plrAtEnd(pLeft) ) return plrAtEnd(pRight) ? 0 : 1; + if( plrAtEnd(pRight) ) return -1; + + if( plrColumn(pLeft)plrColumn(pRight) ) return 1; + + if( plrPosition(pLeft)plrPosition(pRight) ) return 1; + if( pLeft->iType==DL_POSITIONS ) return 0; + + if( plrStartOffset(pLeft)plrStartOffset(pRight) ) return 1; + + if( plrEndOffset(pLeft)plrEndOffset(pRight) ) return 1; + + return 0; +} + +/* Write the union of position lists in pLeft and pRight to pOut. +** "Union" in this case meaning "All unique position tuples". Should +** work with any doclist type, though both inputs and the output +** should be the same type. */ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); +static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ + PLReader left, right; + PLWriter writer; -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. + assert( dlrDocid(pLeft)==dlrDocid(pRight) ); + assert( pLeft->iType==pRight->iType ); + assert( pLeft->iType==pOut->iType ); + + plrInit(&left, pLeft); + plrInit(&right, pRight); + plwInit(&writer, pOut, dlrDocid(pLeft)); + + while( !plrAtEnd(&left) || !plrAtEnd(&right) ){ + int c = posListCmp(&left, &right); + if( c<0 ){ + plwCopy(&writer, &left); + plrStep(&left); + }else if( c>0 ){ + plwCopy(&writer, &right); + plrStep(&right); + }else{ + plwCopy(&writer, &left); + plrStep(&left); + plrStep(&right); + } + } + + plwTerminate(&writer); + plwDestroy(&writer); + plrDestroy(&left); + plrDestroy(&right); +} + +/* Write the union of doclists in pLeft and pRight to pOut. For +** docids in common between the inputs, the union of the position +** lists is written. Inputs and outputs are always type DL_DEFAULT. */ -int sqlite3_bind_parameter_count(sqlite3_stmt*); +static void docListUnion( + const char *pLeft, int nLeft, + const char *pRight, int nRight, + DataBuffer *pOut /* Write the combined doclist here */ +){ + DLReader left, right; + DLWriter writer; -/* -** CAPI3REF: Name Of A Host Parameter {F13620} + if( nLeft==0 ){ + if( nRight!=0) dataBufferAppend(pOut, pRight, nRight); + return; + } + if( nRight==0 ){ + dataBufferAppend(pOut, pLeft, nLeft); + return; + } + + dlrInit(&left, DL_DEFAULT, pLeft, nLeft); + dlrInit(&right, DL_DEFAULT, pRight, nRight); + dlwInit(&writer, DL_DEFAULT, pOut); + + while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ + if( dlrAtEnd(&right) ){ + dlwCopy(&writer, &left); + dlrStep(&left); + }else if( dlrAtEnd(&left) ){ + dlwCopy(&writer, &right); + dlrStep(&right); + }else if( dlrDocid(&left)dlrDocid(&right) ){ + dlwCopy(&writer, &right); + dlrStep(&right); + }else{ + posListUnion(&left, &right, &writer); + dlrStep(&left); + dlrStep(&right); + } + } + + dlrDestroy(&left); + dlrDestroy(&right); + dlwDestroy(&writer); +} + +/* +** This function is used as part of the implementation of phrase and +** NEAR matching. ** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. +** pLeft and pRight are DLReaders positioned to the same docid in +** lists of type DL_POSITION. This function writes an entry to the +** DLWriter pOut for each position in pRight that is less than +** (nNear+1) greater (but not equal to or smaller) than a position +** in pLeft. For example, if nNear is 0, and the positions contained +** by pLeft and pRight are: ** -** {F13623} The first host parameter has an index of 1, not 0. +** pLeft: 5 10 15 20 +** pRight: 6 9 17 21 ** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); - -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} +** then the docid is added to pOut. If pOut is of type DL_POSITIONS, +** then a positionids "6" and "21" are also added to pOut. ** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. +** If boolean argument isSaveLeft is true, then positionids are copied +** from pLeft instead of pRight. In the example above, the positions "5" +** and "20" would be added instead of "6" and "21". */ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); +static void posListPhraseMerge( + DLReader *pLeft, + DLReader *pRight, + int nNear, + int isSaveLeft, + DLWriter *pOut +){ + PLReader left, right; + PLWriter writer; + int match = 0; -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); + assert( dlrDocid(pLeft)==dlrDocid(pRight) ); + assert( pOut->iType!=DL_POSITIONS_OFFSETS ); -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); + plrInit(&left, pLeft); + plrInit(&right, pRight); + + while( !plrAtEnd(&left) && !plrAtEnd(&right) ){ + if( plrColumn(&left)plrColumn(&right) ){ + plrStep(&right); + }else if( plrPosition(&left)>=plrPosition(&right) ){ + plrStep(&right); + }else{ + if( (plrPosition(&right)-plrPosition(&left))<=(nNear+1) ){ + if( !match ){ + plwInit(&writer, pOut, dlrDocid(pLeft)); + match = 1; + } + if( !isSaveLeft ){ + plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0); + }else{ + plwAdd(&writer, plrColumn(&left), plrPosition(&left), 0, 0); + } + plrStep(&right); + }else{ + plrStep(&left); + } + } + } + + if( match ){ + plwTerminate(&writer); + plwDestroy(&writer); + } + + plrDestroy(&left); + plrDestroy(&right); +} /* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. +** Compare the values pointed to by the PLReaders passed as arguments. +** Return -1 if the value pointed to by pLeft is considered less than +** the value pointed to by pRight, +1 if it is considered greater +** than it, or 0 if it is equal. i.e. ** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. +** (*pLeft - *pRight) ** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. +** A PLReader that is in the EOF condition is considered greater than +** any other. If neither argument is in EOF state, the return value of +** plrColumn() is used. If the plrColumn() values are equal, the +** comparison is on the basis of plrPosition(). */ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); +static int plrCompare(PLReader *pLeft, PLReader *pRight){ + assert(!plrAtEnd(pLeft) || !plrAtEnd(pRight)); -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} + if( plrAtEnd(pRight) || plrAtEnd(pLeft) ){ + return (plrAtEnd(pRight) ? -1 : 1); + } + if( plrColumn(pLeft)!=plrColumn(pRight) ){ + return ((plrColumn(pLeft)0) +** and write the results into pOut. ** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. +** A phrase intersection means that two documents only match +** if pLeft.iPos+1==pRight.iPos. ** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. +** A NEAR intersection means that two documents only match if +** (abs(pLeft.iPos-pRight.iPos)Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. + assert( iType!=DL_POSITIONS_OFFSETS ); + + dlrInit(&left, DL_POSITIONS, pLeft, nLeft); + dlrInit(&right, DL_POSITIONS, pRight, nRight); + dlwInit(&writer, iType, pOut); + + while( !dlrAtEnd(&left) && !dlrAtEnd(&right) ){ + if( dlrDocid(&left) -**
  • 64-bit signed integer -**
  • 64-bit IEEE floating point number -**
  • string -**
  • BLOB -**
  • NULL -**
{END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. + if( nLeft==0 ){ + if( nRight!=0 ) dataBufferAppend(pOut, pRight, nRight); + return; + } + if( nRight==0 ){ + dataBufferAppend(pOut, pLeft, nLeft); + return; + } + + dlrInit(&left, DL_DOCIDS, pLeft, nLeft); + dlrInit(&right, DL_DOCIDS, pRight, nRight); + dlwInit(&writer, DL_DOCIDS, pOut); + + while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ + if( dlrAtEnd(&right) ){ + dlwAdd(&writer, dlrDocid(&left)); + dlrStep(&left); + }else if( dlrAtEnd(&left) ){ + dlwAdd(&writer, dlrDocid(&right)); + dlrStep(&right); + }else if( dlrDocid(&left) -** -**
Internal
Type
Requested
Type
Conversion +** {nPhrase=4, iPhrase=1, nNear=6, pTerm="sqlite"}, +** {nPhrase=0, iPhrase=1, nNear=3, pTerm="google"}, +** {nPhrase=0, iPhrase=1, nNear=0, pTerm="search"}, +** {nPhrase=0, iPhrase=2, nNear=0, pTerm="engine"}, ** -**
NULL INTEGER Result is 0 -**
NULL FLOAT Result is 0.0 -**
NULL TEXT Result is NULL pointer -**
NULL BLOB Result is NULL pointer -**
INTEGER FLOAT Convert from integer to float -**
INTEGER TEXT ASCII rendering of the integer -**
INTEGER BLOB Same as for INTEGER->TEXT -**
FLOAT INTEGER Convert from float to integer -**
FLOAT TEXT ASCII rendering of the float -**
FLOAT BLOB Same as FLOAT->TEXT -**
TEXT INTEGER Use atoi() -**
TEXT FLOAT Use atof() -**
TEXT BLOB No change -**
BLOB INTEGER Convert to TEXT then use atoi() -**
BLOB FLOAT Convert to TEXT then use atof() -**
BLOB TEXT Add a zero terminator if needed -**
-** -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
    -**
  • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

  • -** -**
  • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

  • -** -**
  • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

  • -**
-** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
    -**
  • sqlite3_column_text() followed by sqlite3_column_bytes()
  • -**
  • sqlite3_column_blob() followed by sqlite3_column_bytes()
  • -**
  • sqlite3_column_text16() followed by sqlite3_column_bytes16()
  • -**
-** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. +** compiling the FTS3 syntax to Query structures is done by the parseQuery() +** function. */ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); +typedef struct QueryTerm { + short int nPhrase; /* How many following terms are part of the same phrase */ + short int iPhrase; /* This is the i-th term of a phrase. */ + short int iColumn; /* Column of the index that must match this term */ + signed char nNear; /* term followed by a NEAR operator with span=(nNear-1) */ + signed char isOr; /* this term is preceded by "OR" */ + signed char isNot; /* this term is preceded by "-" */ + signed char isPrefix; /* this term is followed by "*" */ + char *pTerm; /* text of the term. '\000' terminated. malloced */ + int nTerm; /* Number of bytes in pTerm[] */ +} QueryTerm; -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); +/* A query string is parsed into a Query structure. + * + * We could, in theory, allow query strings to be complicated + * nested expressions with precedence determined by parentheses. + * But none of the major search engines do this. (Perhaps the + * feeling is that an parenthesized expression is two complex of + * an idea for the average user to grasp.) Taking our lead from + * the major search engines, we will allow queries to be a list + * of terms (with an implied AND operator) or phrases in double-quotes, + * with a single optional "-" before each non-phrase term to designate + * negation and an optional OR connector. + * + * OR binds more tightly than the implied AND, which is what the + * major search engines seem to do. So, for example: + * + * [one two OR three] ==> one AND (two OR three) + * [one OR two three] ==> (one OR two) AND three + * + * A "-" before a term matches all entries that lack that term. + * The "-" must occur immediately before the term with in intervening + * space. This is how the search engines do it. + * + * A NOT term cannot be the right-hand operand of an OR. If this + * occurs in the query string, the NOT is ignored: + * + * [one OR -two] ==> one OR two + * + */ +typedef struct Query { + fulltext_vtab *pFts; /* The full text index */ + int nTerms; /* Number of terms in the query */ + QueryTerm *pTerms; /* Array of terms. Space obtained from malloc() */ + int nextIsOr; /* Set the isOr flag on the next inserted term */ + int nextIsNear; /* Set the isOr flag on the next inserted term */ + int nextColumn; /* Next word parsed must be in this column */ + int dfltColumn; /* The default column */ +} Query; -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); /* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. +** An instance of the following structure keeps track of generated +** matching-word offset information and snippets. */ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ +typedef struct Snippet { + int nMatch; /* Total number of matches */ + int nAlloc; /* Space allocated for aMatch[] */ + struct snippetMatch { /* One entry for each matching term */ + char snStatus; /* Status flag for use while constructing snippets */ + short int iCol; /* The column that contains the match */ + short int iTerm; /* The index in Query.pTerms[] of the matching term */ + int iToken; /* The index of the matching document token */ + short int nByte; /* Number of bytes in the term */ + int iStart; /* The offset to the first character of the term */ + } *aMatch; /* Points to space obtained from malloc */ + char *zOffset; /* Text rendering of aMatch[] */ + int nOffset; /* strlen(zOffset) */ + char *zSnippet; /* Snippet text */ + int nSnippet; /* strlen(zSnippet) */ +} Snippet; -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); +typedef enum QueryType { + QUERY_GENERIC, /* table scan */ + QUERY_DOCID, /* lookup by docid */ + QUERY_FULLTEXT /* QUERY_FULLTEXT + [i] is a full-text search for column i*/ +} QueryType; -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. -*/ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); +typedef enum fulltext_statement { + CONTENT_INSERT_STMT, + CONTENT_SELECT_STMT, + CONTENT_UPDATE_STMT, + CONTENT_DELETE_STMT, -/* -** CAPI3REF: User Data For Functions {F16240} -** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} -** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. + BLOCK_INSERT_STMT, + BLOCK_SELECT_STMT, + BLOCK_DELETE_STMT, + + SEGDIR_MAX_INDEX_STMT, + SEGDIR_SET_STMT, + SEGDIR_SELECT_STMT, + SEGDIR_SPAN_STMT, + SEGDIR_DELETE_STMT, + SEGDIR_SELECT_ALL_STMT, + + MAX_STMT /* Always at end! */ +} fulltext_statement; + +/* These must exactly match the enum above. */ +/* TODO(shess): Is there some risk that a statement will be used in two +** cursors at once, e.g. if a query joins a virtual table to itself? +** If so perhaps we should move some of these to the cursor object. */ -void *sqlite3_user_data(sqlite3_context*); +static const char *const fulltext_zStatement[MAX_STMT] = { + /* CONTENT_INSERT */ NULL, /* generated in contentInsertStatement() */ + /* CONTENT_SELECT */ NULL, /* generated in contentSelectStatement() */ + /* CONTENT_UPDATE */ NULL, /* generated in contentUpdateStatement() */ + /* CONTENT_DELETE */ "delete from %_content where docid = ?", + + /* BLOCK_INSERT */ + "insert into %_segments (blockid, block) values (null, ?)", + /* BLOCK_SELECT */ "select block from %_segments where blockid = ?", + /* BLOCK_DELETE */ "delete from %_segments where blockid between ? and ?", + + /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?", + /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)", + /* SEGDIR_SELECT */ + "select start_block, leaves_end_block, root from %_segdir " + " where level = ? order by idx", + /* SEGDIR_SPAN */ + "select min(start_block), max(end_block) from %_segdir " + " where level = ? and start_block <> 0", + /* SEGDIR_DELETE */ "delete from %_segdir where level = ?", + /* SEGDIR_SELECT_ALL */ + "select root, leaves_end_block from %_segdir order by level desc, idx", +}; /* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. -** -** These routines must be called from the same thread in which -** the SQL function is running. +** A connection to a fulltext index is an instance of the following +** structure. The xCreate and xConnect methods create an instance +** of this structure and xDestroy and xDisconnect free that instance. +** All other methods receive a pointer to the structure as one of their +** arguments. */ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); +struct fulltext_vtab { + sqlite3_vtab base; /* Base class used by SQLite core */ + sqlite3 *db; /* The database connection */ + const char *zDb; /* logical database name */ + const char *zName; /* virtual table name */ + int nColumn; /* number of columns in virtual table */ + char **azColumn; /* column names. malloced */ + char **azContentColumn; /* column names in content table; malloced */ + sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ + + /* Precompiled statements which we keep as long as the table is + ** open. + */ + sqlite3_stmt *pFulltextStatements[MAX_STMT]; + + /* Precompiled statements used for segment merges. We run a + ** separate select across the leaf level of each tree being merged. + */ + sqlite3_stmt *pLeafSelectStmts[MERGE_COUNT]; + /* The statement used to prepare pLeafSelectStmts. */ +#define LEAF_SELECT \ + "select block from %_segments where blockid between ? and ? order by blockid" + /* These buffer pending index updates during transactions. + ** nPendingData estimates the memory size of the pending data. It + ** doesn't include the hash-bucket overhead, nor any malloc + ** overhead. When nPendingData exceeds kPendingThreshold, the + ** buffer is flushed even before the transaction closes. + ** pendingTerms stores the data, and is only valid when nPendingData + ** is >=0 (nPendingData<0 means pendingTerms has not been + ** initialized). iPrevDocid is the last docid written, used to make + ** certain we're inserting in sorted order. + */ + int nPendingData; +#define kPendingThreshold (1*1024*1024) + sqlite_int64 iPrevDocid; + fts3Hash pendingTerms; +}; /* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. +** When the core wants to do a query, it create a cursor using a +** call to xOpen. This structure is an instance of a cursor. It +** is destroyed by xClose. */ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) +typedef struct fulltext_cursor { + sqlite3_vtab_cursor base; /* Base class used by SQLite core */ + QueryType iCursorType; /* Copy of sqlite3_index_info.idxNum */ + sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ + int eof; /* True if at End Of Results */ + Query q; /* Parsed query string */ + Snippet snippet; /* Cached snippet for the current row */ + int iColumn; /* Column being searched */ + DataBuffer result; /* Doclist results from fulltextQuery */ + DLReader reader; /* Result reader if result not empty */ +} fulltext_cursor; -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); +static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){ + return (fulltext_vtab *) c->base.pVtab; +} -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); +static const sqlite3_module fts3Module; /* forward declaration */ -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. -** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. -** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} -** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. -*/ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); +/* Return a dynamically generated statement of the form + * insert into %_content (docid, ...) values (?, ...) + */ +static const char *contentInsertStatement(fulltext_vtab *v){ + StringBuffer sb; + int i; -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); + initStringBuffer(&sb); + append(&sb, "insert into %_content (docid, "); + appendList(&sb, v->nColumn, v->azContentColumn); + append(&sb, ") values (?"); + for(i=0; inColumn; ++i) + append(&sb, ", ?"); + append(&sb, ")"); + return stringBufferData(&sb); +} -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); +/* Return a dynamically generated statement of the form + * select from %_content where docid = ? + */ +static const char *contentSelectStatement(fulltext_vtab *v){ + StringBuffer sb; + initStringBuffer(&sb); + append(&sb, "SELECT "); + appendList(&sb, v->nColumn, v->azContentColumn); + append(&sb, " FROM %_content WHERE docid = ?"); + return stringBufferData(&sb); +} -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); +/* Return a dynamically generated statement of the form + * update %_content set [col_0] = ?, [col_1] = ?, ... + * where docid = ? + */ +static const char *contentUpdateStatement(fulltext_vtab *v){ + StringBuffer sb; + int i; -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. + initStringBuffer(&sb); + append(&sb, "update %_content set "); + for(i=0; inColumn; ++i) { + if( i>0 ){ + append(&sb, ", "); + } + append(&sb, v->azContentColumn[i]); + append(&sb, " = ?"); + } + append(&sb, " where docid = ?"); + return stringBufferData(&sb); +} + +/* Puts a freshly-prepared statement determined by iStmt in *ppStmt. +** If the indicated statement has never been prepared, it is prepared +** and cached, otherwise the cached version is reset. */ -SQLITE_EXTERN char *sqlite3_temp_directory; +static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt, + sqlite3_stmt **ppStmt){ + assert( iStmtpFulltextStatements[iStmt]==NULL ){ + const char *zStmt; + int rc; + switch( iStmt ){ + case CONTENT_INSERT_STMT: + zStmt = contentInsertStatement(v); break; + case CONTENT_SELECT_STMT: + zStmt = contentSelectStatement(v); break; + case CONTENT_UPDATE_STMT: + zStmt = contentUpdateStatement(v); break; + default: + zStmt = fulltext_zStatement[iStmt]; + } + rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt], + zStmt); + if( zStmt != fulltext_zStatement[iStmt]) sqlite3_free((void *) zStmt); + if( rc!=SQLITE_OK ) return rc; + } else { + int rc = sqlite3_reset(v->pFulltextStatements[iStmt]); + if( rc!=SQLITE_OK ) return rc; + } -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} + *ppStmt = v->pFulltextStatements[iStmt]; + return SQLITE_OK; +} + +/* Like sqlite3_step(), but convert SQLITE_DONE to SQLITE_OK and +** SQLITE_ROW to SQLITE_ERROR. Useful for statements like UPDATE, +** where we expect no results. */ -int sqlite3_get_autocommit(sqlite3*); +static int sql_single_step(sqlite3_stmt *s){ + int rc = sqlite3_step(s); + return (rc==SQLITE_DONE) ? SQLITE_OK : rc; +} -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. +/* Like sql_get_statement(), but for special replicated LEAF_SELECT +** statements. +*/ +/* TODO(shess) Write version for generic statements and then share +** that between the cached-statement functions. */ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); +static int sql_get_leaf_statement(fulltext_vtab *v, int idx, + sqlite3_stmt **ppStmt){ + assert( idx>=0 && idxpLeafSelectStmts[idx]==NULL ){ + int rc = sql_prepare(v->db, v->zDb, v->zName, &v->pLeafSelectStmts[idx], + LEAF_SELECT); + if( rc!=SQLITE_OK ) return rc; + }else{ + int rc = sqlite3_reset(v->pLeafSelectStmts[idx]); + if( rc!=SQLITE_OK ) return rc; + } + *ppStmt = v->pLeafSelectStmts[idx]; + return SQLITE_OK; +} -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. +/* insert into %_content (docid, ...) values ([docid], [pValues]) +** If the docid contains SQL NULL, then a unique docid will be +** generated. */ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); +static int content_insert(fulltext_vtab *v, sqlite3_value *docid, + sqlite3_value **pValues){ + sqlite3_stmt *s; + int i; + int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); + rc = sqlite3_bind_value(s, 1, docid); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. + for(i=0; inColumn; ++i){ + rc = sqlite3_bind_value(s, 2+i, pValues[i]); + if( rc!=SQLITE_OK ) return rc; + } + + return sql_single_step(s); +} + +/* update %_content set col0 = pValues[0], col1 = pValues[1], ... + * where docid = [iDocid] */ +static int content_update(fulltext_vtab *v, sqlite3_value **pValues, + sqlite_int64 iDocid){ + sqlite3_stmt *s; + int i; + int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + for(i=0; inColumn; ++i){ + rc = sqlite3_bind_value(s, 1+i, pValues[i]); + if( rc!=SQLITE_OK ) return rc; + } + + rc = sqlite3_bind_int64(s, 1+v->nColumn, iDocid); + if( rc!=SQLITE_OK ) return rc; + + return sql_single_step(s); +} + +static void freeStringArray(int nString, const char **pString){ + int i; + + for (i=0 ; i < nString ; ++i) { + if( pString[i]!=NULL ) sqlite3_free((void *) pString[i]); + } + sqlite3_free((void *) pString); +} + +/* select * from %_content where docid = [iDocid] + * The caller must delete the returned array and all strings in it. + * null fields will be NULL in the returned array. + * + * TODO: Perhaps we should return pointer/length strings here for consistency + * with other code which uses pointer/length. */ +static int content_select(fulltext_vtab *v, sqlite_int64 iDocid, + const char ***pValues){ + sqlite3_stmt *s; + const char **values; + int i; + int rc; + + *pValues = NULL; + + rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 1, iDocid); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_step(s); + if( rc!=SQLITE_ROW ) return rc; + + values = (const char **) sqlite3_malloc(v->nColumn * sizeof(const char *)); + for(i=0; inColumn; ++i){ + if( sqlite3_column_type(s, i)==SQLITE_NULL ){ + values[i] = NULL; + }else{ + values[i] = string_dup((char*)sqlite3_column_text(s, i)); + } + } + + /* We expect only one row. We must execute another sqlite3_step() + * to complete the iteration; otherwise the table will remain locked. */ + rc = sqlite3_step(s); + if( rc==SQLITE_DONE ){ + *pValues = values; + return SQLITE_OK; + } + + freeStringArray(v->nColumn, values); + return rc; +} + +/* delete from %_content where docid = [iDocid ] */ +static int content_delete(fulltext_vtab *v, sqlite_int64 iDocid){ + sqlite3_stmt *s; + int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 1, iDocid); + if( rc!=SQLITE_OK ) return rc; + + return sql_single_step(s); +} + +/* insert into %_segments values ([pData]) +** returns assigned blockid in *piBlockid */ -int sqlite3_enable_shared_cache(int); +static int block_insert(fulltext_vtab *v, const char *pData, int nData, + sqlite_int64 *piBlockid){ + sqlite3_stmt *s; + int rc = sql_get_statement(v, BLOCK_INSERT_STMT, &s); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} + rc = sqlite3_bind_blob(s, 1, pData, nData, SQLITE_STATIC); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_step(s); + if( rc==SQLITE_ROW ) return SQLITE_ERROR; + if( rc!=SQLITE_DONE ) return rc; + + /* blockid column is an alias for rowid. */ + *piBlockid = sqlite3_last_insert_rowid(v->db); + return SQLITE_OK; +} + +/* delete from %_segments +** where blockid between [iStartBlockid] and [iEndBlockid] ** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. +** Deletes the range of blocks, inclusive, used to delete the blocks +** which form a segment. */ -int sqlite3_release_memory(int); +static int block_delete(fulltext_vtab *v, + sqlite_int64 iStartBlockid, sqlite_int64 iEndBlockid){ + sqlite3_stmt *s; + int rc = sql_get_statement(v, BLOCK_DELETE_STMT, &s); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. + rc = sqlite3_bind_int64(s, 1, iStartBlockid); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 2, iEndBlockid); + if( rc!=SQLITE_OK ) return rc; + + return sql_single_step(s); +} + +/* Returns SQLITE_ROW with *pidx set to the maximum segment idx found +** at iLevel. Returns SQLITE_DONE if there are no segments at +** iLevel. Otherwise returns an error. */ -void sqlite3_soft_heap_limit(int); +static int segdir_max_index(fulltext_vtab *v, int iLevel, int *pidx){ + sqlite3_stmt *s; + int rc = sql_get_statement(v, SEGDIR_MAX_INDEX_STMT, &s); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
-** Parameter     Output Type      Description
-** -----------------------------------
-**
-**   5th         const char*      Data type
-**   6th         const char*      Name of the default collation sequence 
-**   7th         int              True if the column has a NOT NULL constraint
-**   8th         int              True if the column is part of the PRIMARY KEY
-**   9th         int              True if the column is AUTOINCREMENT
-** 
-** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
-**     data type: "INTEGER"
-**     collation sequence: "BINARY"
-**     not null: 0
-**     primary key: 1
-**     auto increment: 0
-** 
-** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. + rc = sqlite3_bind_int(s, 1, iLevel); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_step(s); + /* Should always get at least one row due to how max() works. */ + if( rc==SQLITE_DONE ) return SQLITE_DONE; + if( rc!=SQLITE_ROW ) return rc; + + /* NULL means that there were no inputs to max(). */ + if( SQLITE_NULL==sqlite3_column_type(s, 0) ){ + rc = sqlite3_step(s); + if( rc==SQLITE_ROW ) return SQLITE_ERROR; + return rc; + } + + *pidx = sqlite3_column_int(s, 0); + + /* We expect only one row. We must execute another sqlite3_step() + * to complete the iteration; otherwise the table will remain locked. */ + rc = sqlite3_step(s); + if( rc==SQLITE_ROW ) return SQLITE_ERROR; + if( rc!=SQLITE_DONE ) return rc; + return SQLITE_ROW; +} + +/* insert into %_segdir values ( +** [iLevel], [idx], +** [iStartBlockid], [iLeavesEndBlockid], [iEndBlockid], +** [pRootData] +** ) */ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); +static int segdir_set(fulltext_vtab *v, int iLevel, int idx, + sqlite_int64 iStartBlockid, + sqlite_int64 iLeavesEndBlockid, + sqlite_int64 iEndBlockid, + const char *pRootData, int nRootData){ + sqlite3_stmt *s; + int rc = sql_get_statement(v, SEGDIR_SET_STMT, &s); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. + rc = sqlite3_bind_int(s, 1, iLevel); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int(s, 2, idx); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 3, iStartBlockid); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 4, iLeavesEndBlockid); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 5, iEndBlockid); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_blob(s, 6, pRootData, nRootData, SQLITE_STATIC); + if( rc!=SQLITE_OK ) return rc; + + return sql_single_step(s); +} + +/* Queries %_segdir for the block span of the segments in level +** iLevel. Returns SQLITE_DONE if there are no blocks for iLevel, +** SQLITE_ROW if there are blocks, else an error. */ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); +static int segdir_span(fulltext_vtab *v, int iLevel, + sqlite_int64 *piStartBlockid, + sqlite_int64 *piEndBlockid){ + sqlite3_stmt *s; + int rc = sql_get_statement(v, SEGDIR_SPAN_STMT, &s); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} + rc = sqlite3_bind_int(s, 1, iLevel); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_step(s); + if( rc==SQLITE_DONE ) return SQLITE_DONE; /* Should never happen */ + if( rc!=SQLITE_ROW ) return rc; + + /* This happens if all segments at this level are entirely inline. */ + if( SQLITE_NULL==sqlite3_column_type(s, 0) ){ + /* We expect only one row. We must execute another sqlite3_step() + * to complete the iteration; otherwise the table will remain locked. */ + int rc2 = sqlite3_step(s); + if( rc2==SQLITE_ROW ) return SQLITE_ERROR; + return rc2; + } + + *piStartBlockid = sqlite3_column_int64(s, 0); + *piEndBlockid = sqlite3_column_int64(s, 1); + + /* We expect only one row. We must execute another sqlite3_step() + * to complete the iteration; otherwise the table will remain locked. */ + rc = sqlite3_step(s); + if( rc==SQLITE_ROW ) return SQLITE_ERROR; + if( rc!=SQLITE_DONE ) return rc; + return SQLITE_ROW; +} + +/* Delete the segment blocks and segment directory records for all +** segments at iLevel. +*/ +static int segdir_delete(fulltext_vtab *v, int iLevel){ + sqlite3_stmt *s; + sqlite_int64 iStartBlockid, iEndBlockid; + int rc = segdir_span(v, iLevel, &iStartBlockid, &iEndBlockid); + if( rc!=SQLITE_ROW && rc!=SQLITE_DONE ) return rc; + + if( rc==SQLITE_ROW ){ + rc = block_delete(v, iStartBlockid, iEndBlockid); + if( rc!=SQLITE_OK ) return rc; + } + + /* Delete the segment directory itself. */ + rc = sql_get_statement(v, SEGDIR_DELETE_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 1, iLevel); + if( rc!=SQLITE_OK ) return rc; + + return sql_single_step(s); +} + +/* TODO(shess) clearPendingTerms() is far down the file because +** writeZeroSegment() is far down the file because LeafWriter is far +** down the file. Consider refactoring the code to move the non-vtab +** code above the vtab code so that we don't need this forward +** reference. */ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); +static int clearPendingTerms(fulltext_vtab *v); /* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. +** Free the memory used to contain a fulltext_vtab structure. */ -int sqlite3_auto_extension(void *xEntryPoint); +static void fulltext_vtab_destroy(fulltext_vtab *v){ + int iStmt, i; + + FTSTRACE(("FTS3 Destroy %p\n", v)); + for( iStmt=0; iStmtpFulltextStatements[iStmt]!=NULL ){ + sqlite3_finalize(v->pFulltextStatements[iStmt]); + v->pFulltextStatements[iStmt] = NULL; + } + } + + for( i=0; ipLeafSelectStmts[i]!=NULL ){ + sqlite3_finalize(v->pLeafSelectStmts[i]); + v->pLeafSelectStmts[i] = NULL; + } + } + + if( v->pTokenizer!=NULL ){ + v->pTokenizer->pModule->xDestroy(v->pTokenizer); + v->pTokenizer = NULL; + } + + clearPendingTerms(v); + sqlite3_free(v->azColumn); + for(i = 0; i < v->nColumn; ++i) { + sqlite3_free(v->azContentColumn[i]); + } + sqlite3_free(v->azContentColumn); + sqlite3_free(v); +} /* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. +** Token types for parsing the arguments to xConnect or xCreate. */ -void sqlite3_reset_auto_extension(void); - +#define TOKEN_EOF 0 /* End of file */ +#define TOKEN_SPACE 1 /* Any kind of whitespace */ +#define TOKEN_ID 2 /* An identifier */ +#define TOKEN_STRING 3 /* A string literal */ +#define TOKEN_PUNCT 4 /* A single punctuation character */ /* -****** EXPERIMENTAL - subject to change without notice ************** +** If X is a character that can be used in an identifier then +** ftsIdChar(X) will be true. Otherwise it is false. ** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. +** For ASCII, any character with the high-order bit set is +** allowed in an identifier. For 7-bit characters, +** isFtsIdChar[X] must be 1. ** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. +** Ticket #1066. the SQL standard does not allow '$' in the +** middle of identfiers. But many SQL implementations do. +** SQLite will allow '$' in identifiers for compatibility. +** But the feature is undocumented. */ +static const char isFtsIdChar[] = { +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ +}; +#define ftsIdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isFtsIdChar[c-0x20])) + /* -** Structures used by the virtual table interface +** Return the length of the token that begins at z[0]. +** Store the token type in *tokenType before returning. */ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; +static int ftsGetToken(const char *z, int *tokenType){ + int i, c; + switch( *z ){ + case 0: { + *tokenType = TOKEN_EOF; + return 0; + } + case ' ': case '\t': case '\n': case '\f': case '\r': { + for(i=1; safe_isspace(z[i]); i++){} + *tokenType = TOKEN_SPACE; + return i; + } + case '`': + case '\'': + case '"': { + int delim = z[0]; + for(i=1; (c=z[i])!=0; i++){ + if( c==delim ){ + if( z[i+1]==delim ){ + i++; + }else{ + break; + } + } + } + *tokenType = TOKEN_STRING; + return i + (c!=0); + } + case '[': { + for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} + *tokenType = TOKEN_ID; + return i; + } + default: { + if( !ftsIdChar(*z) ){ + break; + } + for(i=1; ftsIdChar(z[i]); i++){} + *tokenType = TOKEN_ID; + return i; + } + } + *tokenType = TOKEN_PUNCT; + return 1; +} /* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. +** A token extracted from a string is an instance of the following +** structure. */ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); - - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; +typedef struct FtsToken { + const char *z; /* Pointer to token text. Not '\000' terminated */ + short int n; /* Length of the token text in bytes. */ +} FtsToken; /* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. +** Given a input string (which is really one of the argv[] parameters +** passed into xConnect or xCreate) split the string up into tokens. +** Return an array of pointers to '\000' terminated strings, one string +** for each non-whitespace token. ** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. +** The returned array is terminated by a single NULL pointer. ** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). +** Space to hold the returned array is obtained from a single +** malloc and should be freed by passing the return value to free(). +** The individual strings within the token list are all a part of +** the single memory allocation and will all be freed at once. */ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ - - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 - -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); - -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); +static char **tokenizeString(const char *z, int *pnToken){ + int nToken = 0; + FtsToken *aToken = sqlite3_malloc( strlen(z) * sizeof(aToken[0]) ); + int n = 1; + int e, i; + int totalSize = 0; + char **azToken; + char *zCopy; + while( n>0 ){ + n = ftsGetToken(z, &e); + if( e!=TOKEN_SPACE ){ + aToken[nToken].z = z; + aToken[nToken].n = n; + nToken++; + totalSize += n+1; + } + z += n; + } + azToken = (char**)sqlite3_malloc( nToken*sizeof(char*) + totalSize ); + zCopy = (char*)&azToken[nToken]; + nToken--; + for(i=0; i=0 ){ + azIn[j] = azIn[i]; + } + j++; + } + } + azIn[j] = 0; + } +} -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; /* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
-**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
-** 
{END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement +** Find the first alphanumeric token in the string zIn. Null-terminate +** this token. Remove any quotation marks. And return a pointer to +** the result. */ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); +static char *firstToken(char *zIn, char **pzTail){ + int n, ttype; + while(1){ + n = ftsGetToken(zIn, &ttype); + if( ttype==TOKEN_SPACE ){ + zIn += n; + }else if( ttype==TOKEN_EOF ){ + *pzTail = zIn; + return 0; + }else{ + zIn[n] = 0; + *pzTail = &zIn[1]; + dequoteString(zIn); + return zIn; + } + } + /*NOTREACHED*/ +} -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. +/* Return true if... ** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. +** * s begins with the string t, ignoring case +** * s is longer than t +** * The first character of s beyond t is not a alphanumeric +** +** Ignore leading space in *s. ** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. +** To put it another way, return true if the first token of +** s[] is t[]. */ -int sqlite3_blob_close(sqlite3_blob *); +static int startsWith(const char *s, const char *t){ + while( safe_isspace(*s) ){ s++; } + while( *t ){ + if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0; + } + return *s!='_' && !safe_isalnum(*s); +} /* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. +** An instance of this structure defines the "spec" of a +** full text index. This structure is populated by parseSpec +** and use by fulltextConnect and fulltextCreate. */ -int sqlite3_blob_bytes(sqlite3_blob *); +typedef struct TableSpec { + const char *zDb; /* Logical database name */ + const char *zName; /* Name of the full-text index */ + int nColumn; /* Number of columns to be indexed */ + char **azColumn; /* Original names of columns to be indexed */ + char **azContentColumn; /* Column names for %_content */ + char **azTokenizer; /* Name of tokenizer and its arguments */ +} TableSpec; /* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** Reclaim all of the memory used by a TableSpec */ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); +static void clearTableSpec(TableSpec *p) { + sqlite3_free(p->azColumn); + sqlite3_free(p->azContentColumn); + sqlite3_free(p->azTokenizer); +} + +/* Parse a CREATE VIRTUAL TABLE statement, which looks like this: + * + * CREATE VIRTUAL TABLE email + * USING fts3(subject, body, tokenize mytokenizer(myarg)) + * + * We return parsed information in a TableSpec structure. + * + */ +static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv, + char**pzErr){ + int i, n; + char *z, *zDummy; + char **azArg; + const char *zTokenizer = 0; /* argv[] entry describing the tokenizer */ + + assert( argc>=3 ); + /* Current interface: + ** argv[0] - module name + ** argv[1] - database name + ** argv[2] - table name + ** argv[3..] - columns, optionally followed by tokenizer specification + ** and snippet delimiters specification. + */ + + /* Make a copy of the complete argv[][] array in a single allocation. + ** The argv[][] array is read-only and transient. We can write to the + ** copy in order to modify things and the copy is persistent. + */ + CLEAR(pSpec); + for(i=n=0; izDb = azArg[1]; + pSpec->zName = azArg[2]; + pSpec->nColumn = 0; + pSpec->azColumn = azArg; + zTokenizer = "tokenize simple"; + for(i=3; inColumn] = firstToken(azArg[i], &zDummy); + pSpec->nColumn++; + } + } + if( pSpec->nColumn==0 ){ + azArg[0] = "content"; + pSpec->nColumn = 1; + } + + /* + ** Construct the list of content column names. + ** + ** Each content column name will be of the form cNNAAAA + ** where NN is the column number and AAAA is the sanitized + ** column name. "sanitized" means that special characters are + ** converted to "_". The cNN prefix guarantees that all column + ** names are unique. + ** + ** The AAAA suffix is not strictly necessary. It is included + ** for the convenience of people who might examine the generated + ** %_content table and wonder what the columns are used for. + */ + pSpec->azContentColumn = sqlite3_malloc( pSpec->nColumn * sizeof(char *) ); + if( pSpec->azContentColumn==0 ){ + clearTableSpec(pSpec); + return SQLITE_NOMEM; + } + for(i=0; inColumn; i++){ + char *p; + pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]); + for (p = pSpec->azContentColumn[i]; *p ; ++p) { + if( !safe_isalnum(*p) ) *p = '_'; + } + } + + /* + ** Parse the tokenizer specification string. + */ + pSpec->azTokenizer = tokenizeString(zTokenizer, &n); + tokenListToIdList(pSpec->azTokenizer); + + return SQLITE_OK; +} /* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. +** Generate a CREATE TABLE statement that describes the schema of +** the virtual table. Return a pointer to this schema string. ** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** Space is obtained from sqlite3_mprintf() and should be freed +** using sqlite3_free(). */ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); +static char *fulltextSchema( + int nColumn, /* Number of columns */ + const char *const* azColumn, /* List of columns */ + const char *zTableName /* Name of the table */ +){ + int i; + char *zSchema, *zNext; + const char *zSep = "("; + zSchema = sqlite3_mprintf("CREATE TABLE x"); + for(i=0; i -**
  • SQLITE_MUTEX_OS2 -**
  • SQLITE_MUTEX_PTHREAD -**
  • SQLITE_MUTEX_W32 -**
  • SQLITE_MUTEX_NOOP -** -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); + char const *zTok; /* Name of tokenizer to use for this fts table */ + int nTok; /* Length of zTok, including nul terminator */ -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); + v = (fulltext_vtab *) sqlite3_malloc(sizeof(fulltext_vtab)); + if( v==0 ) return SQLITE_NOMEM; + CLEAR(v); + /* sqlite will initialize v->base */ + v->db = db; + v->zDb = spec->zDb; /* Freed when azColumn is freed */ + v->zName = spec->zName; /* Freed when azColumn is freed */ + v->nColumn = spec->nColumn; + v->azContentColumn = spec->azContentColumn; + spec->azContentColumn = 0; + v->azColumn = spec->azColumn; + spec->azColumn = 0; -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ + if( spec->azTokenizer==0 ){ + return SQLITE_NOMEM; + } -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] -*/ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); + zTok = spec->azTokenizer[0]; + if( !zTok ){ + zTok = "simple"; + } + nTok = strlen(zTok)+1; -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif + m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zTok, nTok); + if( !m ){ + *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]); + rc = SQLITE_ERROR; + goto err; + } -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif + for(n=0; spec->azTokenizer[n]; n++){} + if( n ){ + rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1], + &v->pTokenizer); + }else{ + rc = m->xCreate(0, 0, &v->pTokenizer); + } + if( rc!=SQLITE_OK ) goto err; + v->pTokenizer->pModule = m; -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3.h ***********************/ + /* TODO: verify the existence of backing tables foo_content, foo_term */ -#if 0 -extern "C" { -#endif /* __cplusplus */ + schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn, + spec->zName); + rc = sqlite3_declare_vtab(db, schema); + sqlite3_free(schema); + if( rc!=SQLITE_OK ) goto err; -int sqlite3Fts3Init(sqlite3 *db); + memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements)); -#if 0 -} /* extern "C" */ -#endif /* __cplusplus */ + /* Indicate that the buffer is not live. */ + v->nPendingData = -1; -/************** End of fts3.h ************************************************/ -/************** Continuing where we left off in fts3.c ***********************/ -/************** Include fts3_hash.h in the middle of fts3.c ******************/ -/************** Begin file fts3_hash.h ***************************************/ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the header file for the generic hash-table implemenation -** used in SQLite. We've modified it slightly to serve as a standalone -** hash table implementation for the full-text indexing module. -** -*/ -#ifndef _FTS3_HASH_H_ -#define _FTS3_HASH_H_ + *ppVTab = &v->base; + FTSTRACE(("FTS3 Connect %p\n", v)); -/* Forward declarations of structures. */ -typedef struct fts3Hash fts3Hash; -typedef struct fts3HashElem fts3HashElem; + return rc; -/* A complete hash table is an instance of the following structure. -** The internals of this structure are intended to be opaque -- client -** code should not attempt to access or modify the fields of this structure -** directly. Change this structure only by using the routines below. -** However, many of the "procedures" and "functions" for modifying and -** accessing this structure are really macros, so we can't really make -** this structure opaque. -*/ -struct fts3Hash { - char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ - char copyKey; /* True if copy of key made on insert */ - int count; /* Number of entries in this table */ - fts3HashElem *first; /* The first element of the array */ - int htsize; /* Number of buckets in the hash table */ - struct _fts3ht { /* the hash table */ - int count; /* Number of entries with this hash */ - fts3HashElem *chain; /* Pointer to first entry with this hash */ - } *ht; -}; +err: + fulltext_vtab_destroy(v); + return rc; +} -/* Each element in the hash table is an instance of the following -** structure. All elements are stored on a single doubly-linked list. -** -** Again, this structure is intended to be opaque, but it can't really -** be opaque because it is used by macros. -*/ -struct fts3HashElem { - fts3HashElem *next, *prev; /* Next and previous elements in the table */ - void *data; /* Data associated with this element */ - void *pKey; int nKey; /* Key associated with this element */ -}; +static int fulltextConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVTab, + char **pzErr +){ + TableSpec spec; + int rc = parseSpec(&spec, argc, argv, pzErr); + if( rc!=SQLITE_OK ) return rc; -/* -** There are 2 different modes of operation for a hash table: -** -** FTS3_HASH_STRING pKey points to a string that is nKey bytes long -** (including the null-terminator, if any). Case -** is respected in comparisons. -** -** FTS3_HASH_BINARY pKey points to binary data nKey bytes long. -** memcmp() is used to compare keys. -** -** A copy of the key is made if the copyKey parameter to fts3HashInit is 1. -*/ -#define FTS3_HASH_STRING 1 -#define FTS3_HASH_BINARY 2 + rc = constructVtab(db, (fts3Hash *)pAux, &spec, ppVTab, pzErr); + clearTableSpec(&spec); + return rc; +} -/* -** Access routines. To delete, insert a NULL pointer. +/* The %_content table holds the text of each document, with +** the docid column exposed as the SQLite rowid for the table. */ -void sqlite3Fts3HashInit(fts3Hash*, int keytype, int copyKey); -void *sqlite3Fts3HashInsert(fts3Hash*, const void *pKey, int nKey, void *pData); -void *sqlite3Fts3HashFind(const fts3Hash*, const void *pKey, int nKey); -void sqlite3Fts3HashClear(fts3Hash*); - -/* -** Shorthand for the functions above +/* TODO(shess) This comment needs elaboration to match the updated +** code. Work it into the top-of-file comment at that time. */ -#define fts3HashInit sqlite3Fts3HashInit -#define fts3HashInsert sqlite3Fts3HashInsert -#define fts3HashFind sqlite3Fts3HashFind -#define fts3HashClear sqlite3Fts3HashClear +static int fulltextCreate(sqlite3 *db, void *pAux, + int argc, const char * const *argv, + sqlite3_vtab **ppVTab, char **pzErr){ + int rc; + TableSpec spec; + StringBuffer schema; + FTSTRACE(("FTS3 Create\n")); -/* -** Macros for looping over all elements of a hash table. The idiom is -** like this: -** -** fts3Hash h; -** fts3HashElem *p; -** ... -** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){ -** SomeStructure *pData = fts3HashData(p); -** // do something with pData -** } -*/ -#define fts3HashFirst(H) ((H)->first) -#define fts3HashNext(E) ((E)->next) -#define fts3HashData(E) ((E)->data) -#define fts3HashKey(E) ((E)->pKey) -#define fts3HashKeysize(E) ((E)->nKey) + rc = parseSpec(&spec, argc, argv, pzErr); + if( rc!=SQLITE_OK ) return rc; -/* -** Number of entries in a hash table -*/ -#define fts3HashCount(H) ((H)->count) + initStringBuffer(&schema); + append(&schema, "CREATE TABLE %_content("); + append(&schema, " docid INTEGER PRIMARY KEY,"); + appendList(&schema, spec.nColumn, spec.azContentColumn); + append(&schema, ")"); + rc = sql_exec(db, spec.zDb, spec.zName, stringBufferData(&schema)); + stringBufferDestroy(&schema); + if( rc!=SQLITE_OK ) goto out; -#endif /* _FTS3_HASH_H_ */ + rc = sql_exec(db, spec.zDb, spec.zName, + "create table %_segments(" + " blockid INTEGER PRIMARY KEY," + " block blob" + ");" + ); + if( rc!=SQLITE_OK ) goto out; -/************** End of fts3_hash.h *******************************************/ -/************** Continuing where we left off in fts3.c ***********************/ -/************** Include fts3_tokenizer.h in the middle of fts3.c *************/ -/************** Begin file fts3_tokenizer.h **********************************/ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _FTS3_TOKENIZER_H_ -#define _FTS3_TOKENIZER_H_ + rc = sql_exec(db, spec.zDb, spec.zName, + "create table %_segdir(" + " level integer," + " idx integer," + " start_block integer," + " leaves_end_block integer," + " end_block integer," + " root blob," + " primary key(level, idx)" + ");"); + if( rc!=SQLITE_OK ) goto out; -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -/************** Include sqlite3.h in the middle of fts3_tokenizer.h **********/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. -** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. -** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. -** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. -** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ -*/ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ + rc = constructVtab(db, (fts3Hash *)pAux, &spec, ppVTab, pzErr); -/* -** Make sure we can call this stuff from C++. -*/ -#if 0 -extern "C" { -#endif +out: + clearTableSpec(&spec); + return rc; +} +/* Decide how to handle an SQL query. */ +static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ + fulltext_vtab *v = (fulltext_vtab *)pVTab; + int i; + FTSTRACE(("FTS3 BestIndex\n")); -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif + for(i=0; inConstraint; ++i){ + const struct sqlite3_index_constraint *pConstraint; + pConstraint = &pInfo->aConstraint[i]; + if( pConstraint->usable ) { + if( (pConstraint->iColumn==-1 || pConstraint->iColumn==v->nColumn+1) && + pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + pInfo->idxNum = QUERY_DOCID; /* lookup by docid */ + FTSTRACE(("FTS3 QUERY_DOCID\n")); + } else if( pConstraint->iColumn>=0 && pConstraint->iColumn<=v->nColumn && + pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ + /* full-text search */ + pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn; + FTSTRACE(("FTS3 QUERY_FULLTEXT %d\n", pConstraint->iColumn)); + } else continue; -/* -** Make sure these symbols where not defined by some previous header -** file. -*/ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif + pInfo->aConstraintUsage[i].argvIndex = 1; + pInfo->aConstraintUsage[i].omit = 1; -/* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. -** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 + /* An arbitrary value for now. + * TODO: Perhaps docid matches should be considered cheaper than + * full-text searches. */ + pInfo->estimatedCost = 1.0; -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); + return SQLITE_OK; + } + } + pInfo->idxNum = QUERY_GENERIC; + return SQLITE_OK; +} -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); +static int fulltextDisconnect(sqlite3_vtab *pVTab){ + FTSTRACE(("FTS3 Disconnect %p\n", pVTab)); + fulltext_vtab_destroy((fulltext_vtab *)pVTab); + return SQLITE_OK; +} -/* -** CAPI3REF: Database Connection Handle {F12000} -** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this -** object. -*/ -typedef struct sqlite3 sqlite3; +static int fulltextDestroy(sqlite3_vtab *pVTab){ + fulltext_vtab *v = (fulltext_vtab *)pVTab; + int rc; + + FTSTRACE(("FTS3 Destroy %p\n", pVTab)); + rc = sql_exec(v->db, v->zDb, v->zName, + "drop table if exists %_content;" + "drop table if exists %_segments;" + "drop table if exists %_segdir;" + ); + if( rc!=SQLITE_OK ) return rc; + fulltext_vtab_destroy((fulltext_vtab *)pVTab); + return SQLITE_OK; +} -/* -** CAPI3REF: 64-Bit Integer Types {F10200} -** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} -** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. +static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + fulltext_cursor *c; + + c = (fulltext_cursor *) sqlite3_malloc(sizeof(fulltext_cursor)); + if( c ){ + memset(c, 0, sizeof(fulltext_cursor)); + /* sqlite will initialize c->base */ + *ppCursor = &c->base; + FTSTRACE(("FTS3 Open %p: %p\n", pVTab, c)); + return SQLITE_OK; + }else{ + return SQLITE_NOMEM; + } +} + + +/* Free all of the dynamically allocated memory held by *q */ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; -#else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; -#endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; +static void queryClear(Query *q){ + int i; + for(i = 0; i < q->nTerms; ++i){ + sqlite3_free(q->pTerms[i].pTerm); + } + sqlite3_free(q->pTerms); + CLEAR(q); +} +/* Free all of the dynamically allocated memory held by the +** Snippet +*/ +static void snippetClear(Snippet *p){ + sqlite3_free(p->aMatch); + sqlite3_free(p->zOffset); + sqlite3_free(p->zSnippet); + CLEAR(p); +} /* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point +** Append a single entry to the p->aMatch[] log. */ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif +static void snippetAppendMatch( + Snippet *p, /* Append the entry to this snippet */ + int iCol, int iTerm, /* The column and query term */ + int iToken, /* Matching token in document */ + int iStart, int nByte /* Offset and size of the match */ +){ + int i; + struct snippetMatch *pMatch; + if( p->nMatch+1>=p->nAlloc ){ + p->nAlloc = p->nAlloc*2 + 10; + p->aMatch = sqlite3_realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) ); + if( p->aMatch==0 ){ + p->nMatch = 0; + p->nAlloc = 0; + return; + } + } + i = p->nMatch++; + pMatch = &p->aMatch[i]; + pMatch->iCol = iCol; + pMatch->iTerm = iTerm; + pMatch->iToken = iToken; + pMatch->iStart = iStart; + pMatch->nByte = nByte; +} /* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. +** Sizing information for the circular buffer used in snippetOffsetsOfColumn() */ -int sqlite3_close(sqlite3 *); +#define FTS3_ROTOR_SZ (32) +#define FTS3_ROTOR_MASK (FTS3_ROTOR_SZ-1) /* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. +** Add entries to pSnippet->aMatch[] for every match that occurs against +** document zDoc[0..nDoc-1] which is stored in column iColumn. */ -typedef int (*sqlite3_callback)(void*,int,char**, char**); +static void snippetOffsetsOfColumn( + Query *pQuery, + Snippet *pSnippet, + int iColumn, + const char *zDoc, + int nDoc +){ + const sqlite3_tokenizer_module *pTModule; /* The tokenizer module */ + sqlite3_tokenizer *pTokenizer; /* The specific tokenizer */ + sqlite3_tokenizer_cursor *pTCursor; /* Tokenizer cursor */ + fulltext_vtab *pVtab; /* The full text index */ + int nColumn; /* Number of columns in the index */ + const QueryTerm *aTerm; /* Query string terms */ + int nTerm; /* Number of query string terms */ + int i, j; /* Loop counters */ + int rc; /* Return code */ + unsigned int match, prevMatch; /* Phrase search bitmasks */ + const char *zToken; /* Next token from the tokenizer */ + int nToken; /* Size of zToken */ + int iBegin, iEnd, iPos; /* Offsets of beginning and end */ -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); + /* The following variables keep a circular buffer of the last + ** few tokens */ + unsigned int iRotor = 0; /* Index of current token */ + int iRotorBegin[FTS3_ROTOR_SZ]; /* Beginning offset of token */ + int iRotorLen[FTS3_ROTOR_SZ]; /* Length of token */ + + pVtab = pQuery->pFts; + nColumn = pVtab->nColumn; + pTokenizer = pVtab->pTokenizer; + pTModule = pTokenizer->pModule; + rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor); + if( rc ) return; + pTCursor->pTokenizer = pTokenizer; + aTerm = pQuery->pTerms; + nTerm = pQuery->nTerms; + if( nTerm>=FTS3_ROTOR_SZ ){ + nTerm = FTS3_ROTOR_SZ - 1; + } + prevMatch = 0; + while(1){ + rc = pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos); + if( rc ) break; + iRotorBegin[iRotor&FTS3_ROTOR_MASK] = iBegin; + iRotorLen[iRotor&FTS3_ROTOR_MASK] = iEnd-iBegin; + match = 0; + for(i=0; i=0 && iColnToken ) continue; + if( !aTerm[i].isPrefix && aTerm[i].nTerm1 && (prevMatch & (1<=0; j--){ + int k = (iRotor-j) & FTS3_ROTOR_MASK; + snippetAppendMatch(pSnippet, iColumn, i-j, iPos-j, + iRotorBegin[k], iRotorLen[k]); + } + } + } + prevMatch = match<<1; + iRotor++; + } + pTModule->xClose(pTCursor); +} /* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. +** Remove entries from the pSnippet structure to account for the NEAR +** operator. When this is called, pSnippet contains the list of token +** offsets produced by treating all NEAR operators as AND operators. +** This function removes any entries that should not be present after +** accounting for the NEAR restriction. For example, if the queried +** document is: ** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} +** "A B C D E A" ** -** See also: [SQLITE_IOERR_READ | extended result codes] +** and the query is: +** +** A NEAR/0 E ** +** then when this function is called the Snippet contains token offsets +** 0, 4 and 5. This function removes the "0" entry (because the first A +** is not near enough to an E). */ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ +static void trimSnippetOffsetsForNear(Query *pQuery, Snippet *pSnippet){ + int ii; + int iDir = 1; + + while(iDir>-2) { + assert( iDir==1 || iDir==-1 ); + for(ii=0; iinMatch; ii++){ + int jj; + int nNear; + struct snippetMatch *pMatch = &pSnippet->aMatch[ii]; + QueryTerm *pQueryTerm = &pQuery->pTerms[pMatch->iTerm]; + + if( (pMatch->iTerm+iDir)<0 + || (pMatch->iTerm+iDir)>=pQuery->nTerms + ){ + continue; + } + + nNear = pQueryTerm->nNear; + if( iDir<0 ){ + nNear = pQueryTerm[-1].nNear; + } + + if( pMatch->iTerm>=0 && nNear ){ + int isOk = 0; + int iNextTerm = pMatch->iTerm+iDir; + int iPrevTerm = iNextTerm; + + int iEndToken; + int iStartToken; + + if( iDir<0 ){ + int nPhrase = 1; + iStartToken = pMatch->iToken; + while( (pMatch->iTerm+nPhrase)nTerms + && pQuery->pTerms[pMatch->iTerm+nPhrase].iPhrase>1 + ){ + nPhrase++; + } + iEndToken = iStartToken + nPhrase - 1; + }else{ + iEndToken = pMatch->iToken; + iStartToken = pMatch->iToken+1-pQueryTerm->iPhrase; + } + + while( pQuery->pTerms[iNextTerm].iPhrase>1 ){ + iNextTerm--; + } + while( (iPrevTerm+1)nTerms && + pQuery->pTerms[iPrevTerm+1].iPhrase>1 + ){ + iPrevTerm++; + } + + for(jj=0; isOk==0 && jjnMatch; jj++){ + struct snippetMatch *p = &pSnippet->aMatch[jj]; + if( p->iCol==pMatch->iCol && (( + p->iTerm==iNextTerm && + p->iToken>iEndToken && + p->iToken<=iEndToken+nNear + ) || ( + p->iTerm==iPrevTerm && + p->iTokeniToken>=iStartToken-nNear + ))){ + isOk = 1; + } + } + if( !isOk ){ + for(jj=1-pQueryTerm->iPhrase; jj<=0; jj++){ + pMatch[jj].iTerm = -1; + } + ii = -1; + iDir = 1; + } + } + } + iDir -= 2; + } +} /* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. +** Compute all offsets for the current row of the query. +** If the offsets have already been computed, this routine is a no-op. */ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) +static void snippetAllOffsets(fulltext_cursor *p){ + int nColumn; + int iColumn, i; + int iFirst, iLast; + fulltext_vtab *pFts; + + if( p->snippet.nMatch ) return; + if( p->q.nTerms==0 ) return; + pFts = p->q.pFts; + nColumn = pFts->nColumn; + iColumn = (p->iCursorType - QUERY_FULLTEXT); + if( iColumn<0 || iColumn>=nColumn ){ + iFirst = 0; + iLast = nColumn-1; + }else{ + iFirst = iColumn; + iLast = iColumn; + } + for(i=iFirst; i<=iLast; i++){ + const char *zDoc; + int nDoc; + zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1); + nDoc = sqlite3_column_bytes(p->pStmt, i+1); + snippetOffsetsOfColumn(&p->q, &p->snippet, i, zDoc, nDoc); + } + + trimSnippetOffsetsForNear(&p->q, &p->snippet); +} /* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. +** Convert the information in the aMatch[] array of the snippet +** into the string zOffset[0..nOffset-1]. */ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 +static void snippetOffsetText(Snippet *p){ + int i; + int cnt = 0; + StringBuffer sb; + char zBuf[200]; + if( p->zOffset ) return; + initStringBuffer(&sb); + for(i=0; inMatch; i++){ + struct snippetMatch *pMatch = &p->aMatch[i]; + if( pMatch->iTerm>=0 ){ + /* If snippetMatch.iTerm is less than 0, then the match was + ** discarded as part of processing the NEAR operator (see the + ** trimSnippetOffsetsForNear() function for details). Ignore + ** it in this case + */ + zBuf[0] = ' '; + sprintf(&zBuf[cnt>0], "%d %d %d %d", pMatch->iCol, + pMatch->iTerm, pMatch->iStart, pMatch->nByte); + append(&sb, zBuf); + cnt++; + } + } + p->zOffset = stringBufferData(&sb); + p->nOffset = stringBufferLength(&sb); +} /* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} +** zDoc[0..nDoc-1] is phrase of text. aMatch[0..nMatch-1] are a set +** of matching words some of which might be in zDoc. zDoc is column +** number iCol. ** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). +** iBreak is suggested spot in zDoc where we could begin or end an +** excerpt. Return a value similar to iBreak but possibly adjusted +** to be a little left or right so that the break point is better. */ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 +static int wordBoundary( + int iBreak, /* The suggested break point */ + const char *zDoc, /* Document text */ + int nDoc, /* Number of bytes in zDoc[] */ + struct snippetMatch *aMatch, /* Matching words */ + int nMatch, /* Number of entries in aMatch[] */ + int iCol /* The column number for zDoc[] */ +){ + int i; + if( iBreak<=10 ){ + return 0; + } + if( iBreak>=nDoc-10 ){ + return nDoc; + } + for(i=0; i0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){ + return aMatch[i-1].iStart; + } + } + for(i=1; i<=10; i++){ + if( safe_isspace(zDoc[iBreak-i]) ){ + return iBreak - i + 1; + } + if( safe_isspace(zDoc[iBreak+i]) ){ + return iBreak + i + 1; + } + } + return iBreak; +} + + /* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} +** Allowed values for Snippet.aMatch[].snStatus */ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 +#define SNIPPET_IGNORE 0 /* It is ok to omit this match from the snippet */ +#define SNIPPET_DESIRED 1 /* We want to include this match in the snippet */ /* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). +** Generate the text of a snippet. */ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 +static void snippetText( + fulltext_cursor *pCursor, /* The cursor we need the snippet for */ + const char *zStartMark, /* Markup to appear before each match */ + const char *zEndMark, /* Markup to appear after each match */ + const char *zEllipsis /* Ellipsis mark */ +){ + int i, j; + struct snippetMatch *aMatch; + int nMatch; + int nDesired; + StringBuffer sb; + int tailCol; + int tailOffset; + int iCol; + int nDoc; + const char *zDoc; + int iStart, iEnd; + int tailEllipsis = 0; + int iMatch; + + sqlite3_free(pCursor->snippet.zSnippet); + pCursor->snippet.zSnippet = 0; + aMatch = pCursor->snippet.aMatch; + nMatch = pCursor->snippet.nMatch; + initStringBuffer(&sb); -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; + for(i=0; iq.nTerms; i++){ + for(j=0; j -**
  • [SQLITE_LOCK_NONE], -**
  • [SQLITE_LOCK_SHARED], -**
  • [SQLITE_LOCK_RESERVED], -**
  • [SQLITE_LOCK_PENDING], or -**
  • [SQLITE_LOCK_EXCLUSIVE]. -** -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; + iMatch = 0; + tailCol = -1; + tailOffset = 0; + for(i=0; i0; i++){ + if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue; + nDesired--; + iCol = aMatch[i].iCol; + zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1); + nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1); + iStart = aMatch[i].iStart - 40; + iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol); + if( iStart<=10 ){ + iStart = 0; + } + if( iCol==tailCol && iStart<=tailOffset+20 ){ + iStart = tailOffset; + } + if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){ + trimWhiteSpace(&sb); + appendWhiteSpace(&sb); + append(&sb, zEllipsis); + appendWhiteSpace(&sb); + } + iEnd = aMatch[i].iStart + aMatch[i].nByte + 40; + iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol); + if( iEnd>=nDoc-10 ){ + iEnd = nDoc; + tailEllipsis = 0; + }else{ + tailEllipsis = 1; + } + while( iMatchsnippet.zSnippet = stringBufferData(&sb); + pCursor->snippet.nSnippet = stringBufferLength(&sb); +} -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 /* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. +** Close the cursor. For additional information see the documentation +** on the xClose method of the virtual table interface. */ -typedef struct sqlite3_mutex sqlite3_mutex; +static int fulltextClose(sqlite3_vtab_cursor *pCursor){ + fulltext_cursor *c = (fulltext_cursor *) pCursor; + FTSTRACE(("FTS3 Close %p\n", c)); + sqlite3_finalize(c->pStmt); + queryClear(&c->q); + snippetClear(&c->snippet); + if( c->result.nData!=0 ) dlrDestroy(&c->reader); + dataBufferDestroy(&c->result); + sqlite3_free(c); + return SQLITE_OK; +} -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; +static int fulltextNext(sqlite3_vtab_cursor *pCursor){ + fulltext_cursor *c = (fulltext_cursor *) pCursor; + int rc; -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 + FTSTRACE(("FTS3 Next %p\n", pCursor)); + snippetClear(&c->snippet); + if( c->iCursorType < QUERY_FULLTEXT ){ + /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ + rc = sqlite3_step(c->pStmt); + switch( rc ){ + case SQLITE_ROW: + c->eof = 0; + return SQLITE_OK; + case SQLITE_DONE: + c->eof = 1; + return SQLITE_OK; + default: + c->eof = 1; + return rc; + } + } else { /* full-text query */ + rc = sqlite3_reset(c->pStmt); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); + if( c->result.nData==0 || dlrAtEnd(&c->reader) ){ + c->eof = 1; + return SQLITE_OK; + } + rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader)); + dlrStep(&c->reader); + if( rc!=SQLITE_OK ) return rc; + /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ + rc = sqlite3_step(c->pStmt); + if( rc==SQLITE_ROW ){ /* the case we expect */ + c->eof = 0; + return SQLITE_OK; + } + /* an error occurred; abort */ + return rc==SQLITE_DONE ? SQLITE_ERROR : rc; + } +} -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. -*/ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. +/* TODO(shess) If we pushed LeafReader to the top of the file, or to +** another file, term_select() could be pushed above +** docListOfTerm(). */ -int sqlite3_changes(sqlite3*); +static int termSelect(fulltext_vtab *v, int iColumn, + const char *pTerm, int nTerm, int isPrefix, + DocListType iType, DataBuffer *out); -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. +/* Return a DocList corresponding to the query term *pTerm. If *pTerm +** is the first term of a phrase query, go ahead and evaluate the phrase +** query and return the doclist for the entire phrase query. ** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} +** The resulting DL_DOCIDS doclist is stored in pResult, which is +** overwritten. */ -int sqlite3_total_changes(sqlite3*); +static int docListOfTerm( + fulltext_vtab *v, /* The full text index */ + int iColumn, /* column to restrict to. No restriction if >=nColumn */ + QueryTerm *pQTerm, /* Term we are looking for, or 1st term of a phrase */ + DataBuffer *pResult /* Write the result here */ +){ + DataBuffer left, right, new; + int i, rc; -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. -*/ -void sqlite3_interrupt(sqlite3*); + /* No phrase search if no position info. */ + assert( pQTerm->nPhrase==0 || DL_DEFAULT!=DL_DOCIDS ); -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} + /* This code should never be called with buffered updates. */ + assert( v->nPendingData<0 ); + + dataBufferInit(&left, 0); + rc = termSelect(v, iColumn, pQTerm->pTerm, pQTerm->nTerm, pQTerm->isPrefix, + (0nPhrase ? DL_POSITIONS : DL_DOCIDS), &left); + if( rc ) return rc; + for(i=1; i<=pQTerm->nPhrase && left.nData>0; i++){ + /* If this token is connected to the next by a NEAR operator, and + ** the next token is the start of a phrase, then set nPhraseRight + ** to the number of tokens in the phrase. Otherwise leave it at 1. + */ + int nPhraseRight = 1; + while( (i+nPhraseRight)<=pQTerm->nPhrase + && pQTerm[i+nPhraseRight].nNear==0 + ){ + nPhraseRight++; + } + + dataBufferInit(&right, 0); + rc = termSelect(v, iColumn, pQTerm[i].pTerm, pQTerm[i].nTerm, + pQTerm[i].isPrefix, DL_POSITIONS, &right); + if( rc ){ + dataBufferDestroy(&left); + return rc; + } + dataBufferInit(&new, 0); + docListPhraseMerge(left.pData, left.nData, right.pData, right.nData, + pQTerm[i-1].nNear, pQTerm[i-1].iPhrase + nPhraseRight, + ((inPhrase) ? DL_POSITIONS : DL_DOCIDS), + &new); + dataBufferDestroy(&left); + dataBufferDestroy(&right); + left = new; + } + *pResult = left; + return SQLITE_OK; +} + +/* Add a new term pTerm[0..nTerm-1] to the query *q. */ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); +static void queryAdd(Query *q, const char *pTerm, int nTerm){ + QueryTerm *t; + ++q->nTerms; + q->pTerms = sqlite3_realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0])); + if( q->pTerms==0 ){ + q->nTerms = 0; + return; + } + t = &q->pTerms[q->nTerms - 1]; + CLEAR(t); + t->pTerm = sqlite3_malloc(nTerm+1); + memcpy(t->pTerm, pTerm, nTerm); + t->pTerm[nTerm] = 0; + t->nTerm = nTerm; + t->isOr = q->nextIsOr; + t->isPrefix = 0; + q->nextIsOr = 0; + t->iColumn = q->nextColumn; + q->nextColumn = q->dfltColumn; +} /* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. +** Check to see if the string zToken[0...nToken-1] matches any +** column name in the virtual table. If it does, +** return the zero-indexed column number. If not, return -1. */ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); +static int checkColumnSpecifier( + fulltext_vtab *pVtab, /* The virtual table */ + const char *zToken, /* Text of the token */ + int nToken /* Number of characters in the token */ +){ + int i; + for(i=0; inColumn; i++){ + if( memcmp(pVtab->azColumn[i], zToken, nToken)==0 + && pVtab->azColumn[i][nToken]==0 ){ + return i; + } + } + return -1; +} /* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. +** Parse the text at pSegment[0..nSegment-1]. Add additional terms +** to the query being assemblied in pQuery. ** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. +** inPhrase is true if pSegment[0..nSegement-1] is contained within +** double-quotes. If inPhrase is true, then the first term +** is marked with the number of terms in the phrase less one and +** OR and "-" syntax is ignored. If inPhrase is false, then every +** term found is marked with nPhrase=0 and OR and "-" syntax is significant. */ -int sqlite3_busy_timeout(sqlite3*, int ms); +static int tokenizeSegment( + sqlite3_tokenizer *pTokenizer, /* The tokenizer to use */ + const char *pSegment, int nSegment, /* Query expression being parsed */ + int inPhrase, /* True if within "..." */ + Query *pQuery /* Append results here */ +){ + const sqlite3_tokenizer_module *pModule = pTokenizer->pModule; + sqlite3_tokenizer_cursor *pCursor; + int firstIndex = pQuery->nTerms; + int iCol; + int nTerm = 1; + + int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor); + if( rc!=SQLITE_OK ) return rc; + pCursor->pTokenizer = pTokenizer; -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); + while( 1 ){ + const char *pToken; + int nToken, iBegin, iEnd, iPos; -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); + rc = pModule->xNext(pCursor, + &pToken, &nToken, + &iBegin, &iEnd, &iPos); + if( rc!=SQLITE_OK ) break; + if( !inPhrase && + pSegment[iEnd]==':' && + (iCol = checkColumnSpecifier(pQuery->pFts, pToken, nToken))>=0 ){ + pQuery->nextColumn = iCol; + continue; + } + if( !inPhrase && pQuery->nTerms>0 && nToken==2 + && pSegment[iBegin+0]=='O' + && pSegment[iBegin+1]=='R' + ){ + pQuery->nextIsOr = 1; + continue; + } + if( !inPhrase && pQuery->nTerms>0 && !pQuery->nextIsOr && nToken==4 + && pSegment[iBegin+0]=='N' + && pSegment[iBegin+1]=='E' + && pSegment[iBegin+2]=='A' + && pSegment[iBegin+3]=='R' + ){ + QueryTerm *pTerm = &pQuery->pTerms[pQuery->nTerms-1]; + if( (iBegin+6)='0' && pSegment[iBegin+5]<='9' + ){ + pTerm->nNear = (pSegment[iBegin+5] - '0'); + nToken += 2; + if( pSegment[iBegin+6]>='0' && pSegment[iBegin+6]<=9 ){ + pTerm->nNear = pTerm->nNear * 10 + (pSegment[iBegin+6] - '0'); + iEnd++; + } + pModule->xNext(pCursor, &pToken, &nToken, &iBegin, &iEnd, &iPos); + } else { + pTerm->nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM; + } + pTerm->nNear++; + continue; + } -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); + queryAdd(pQuery, pToken, nToken); + if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){ + pQuery->pTerms[pQuery->nTerms-1].isNot = 1; + } + if( iEndpTerms[pQuery->nTerms-1].isPrefix = 1; + } + pQuery->pTerms[pQuery->nTerms-1].iPhrase = nTerm; + if( inPhrase ){ + nTerm++; + } + } -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); + if( inPhrase && pQuery->nTerms>firstIndex ){ + pQuery->pTerms[firstIndex].nPhrase = pQuery->nTerms - firstIndex - 1; + } -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} + return pModule->xClose(pCursor); +} + +/* Parse a query string, yielding a Query object pQuery. ** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} +** The calling function will need to queryClear() to clean up +** the dynamically allocated memory held by pQuery. */ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); +static int parseQuery( + fulltext_vtab *v, /* The fulltext index */ + const char *zInput, /* Input text of the query string */ + int nInput, /* Size of the input text */ + int dfltColumn, /* Default column of the index to match against */ + Query *pQuery /* Write the parse results here. */ +){ + int iInput, inPhrase = 0; + int ii; + QueryTerm *aTerm; -/* -** CAPI3REF: Authorizer Return Codes {F12590} + if( zInput==0 ) nInput = 0; + if( nInput<0 ) nInput = strlen(zInput); + pQuery->nTerms = 0; + pQuery->pTerms = NULL; + pQuery->nextIsOr = 0; + pQuery->nextColumn = dfltColumn; + pQuery->dfltColumn = dfltColumn; + pQuery->pFts = v; + + for(iInput=0; iInputiInput ){ + tokenizeSegment(v->pTokenizer, zInput+iInput, i-iInput, inPhrase, + pQuery); + } + iInput = i; + if( ipTerms; + for(ii=0; iinTerms; ii++){ + if( aTerm[ii].nNear || aTerm[ii].nPhrase ){ + while (aTerm[ii+aTerm[ii].nPhrase].nNear) { + aTerm[ii].nPhrase += (1 + aTerm[ii+aTerm[ii].nPhrase+1].nPhrase); + } + } + } + + return SQLITE_OK; +} + +/* TODO(shess) Refactor the code to remove this forward decl. */ +static int flushPendingTerms(fulltext_vtab *v); + +/* Perform a full-text query using the search expression in +** zInput[0..nInput-1]. Return a list of matching documents +** in pResult. ** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. +** Queries must match column iColumn. Or if iColumn>=nColumn +** they are allowed to match against any column. */ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ - -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. -*/ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ - -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. -*/ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); - -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); - -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. -*/ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); - -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); - -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; - -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    -*/ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); - -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. -*/ -const char *sqlite3_sql(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. -*/ -typedef struct Mem sqlite3_value; - -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. -*/ -typedef struct sqlite3_context sqlite3_context; - -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. -*/ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); - -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. -*/ -int sqlite3_bind_parameter_count(sqlite3_stmt*); - -/* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); - -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. -*/ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); - -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); - -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. -** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. -*/ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); - -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. -*/ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); - -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. -*/ -int sqlite3_data_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 - -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); - -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); - -/* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ - -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); - -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); - -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. -*/ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); - -/* -** CAPI3REF: User Data For Functions {F16240} -** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} -** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. -*/ -void *sqlite3_user_data(sqlite3_context*); - -/* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. -** -** These routines must be called from the same thread in which -** the SQL function is running. -*/ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); - - -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. -*/ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) - -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); - -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); - -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. -** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. -** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} -** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. -*/ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); - -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); - -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); - -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); - -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. -*/ -SQLITE_EXTERN char *sqlite3_temp_directory; - -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); - -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); - - -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); - -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); - -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. -*/ -int sqlite3_enable_shared_cache(int); - -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. -*/ -int sqlite3_release_memory(int); - -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. -*/ -void sqlite3_soft_heap_limit(int); - -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -*/ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); - -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. -*/ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); - -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} -*/ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); - -/* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -int sqlite3_auto_extension(void *xEntryPoint); - - -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -void sqlite3_reset_auto_extension(void); - - -/* -****** EXPERIMENTAL - subject to change without notice ************** -** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - -/* -** Structures used by the virtual table interface -*/ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; - -/* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. -*/ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); - - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; - -/* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. -** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. -** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). -*/ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ - - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 - -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); - -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); - -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. -*/ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. -*/ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); - -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); - -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** -*/ - -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; - -/* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement -*/ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); - -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. -** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. -** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. -*/ -int sqlite3_blob_close(sqlite3_blob *); - -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); - -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); - -/* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. -** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); - -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); - -/* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ - -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] -*/ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); - -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif - -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif - -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3_tokenizer.h *************/ - -/* -** Structures used by the tokenizer interface. When a new tokenizer -** implementation is registered, the caller provides a pointer to -** an sqlite3_tokenizer_module containing pointers to the callback -** functions that make up an implementation. -** -** When an fts3 table is created, it passes any arguments passed to -** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the -** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer -** implementation. The xCreate() function in turn returns an -** sqlite3_tokenizer structure representing the specific tokenizer to -** be used for the fts3 table (customized by the tokenizer clause arguments). -** -** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() -** method is called. It returns an sqlite3_tokenizer_cursor object -** that may be used to tokenize a specific input buffer based on -** the tokenization rules supplied by a specific sqlite3_tokenizer -** object. -*/ -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; - -struct sqlite3_tokenizer_module { - - /* - ** Structure version. Should always be set to 0. - */ - int iVersion; - - /* - ** Create a new tokenizer. The values in the argv[] array are the - ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL - ** TABLE statement that created the fts3 table. For example, if - ** the following SQL is executed: - ** - ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) - ** - ** then argc is set to 2, and the argv[] array contains pointers - ** to the strings "arg1" and "arg2". - ** - ** This method should return either SQLITE_OK (0), or an SQLite error - ** code. If SQLITE_OK is returned, then *ppTokenizer should be set - ** to point at the newly created tokenizer structure. The generic - ** sqlite3_tokenizer.pModule variable should not be initialised by - ** this callback. The caller will do so. - */ - int (*xCreate)( - int argc, /* Size of argv array */ - const char *const*argv, /* Tokenizer argument strings */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ - ); - - /* - ** Destroy an existing tokenizer. The fts3 module calls this method - ** exactly once for each successful call to xCreate(). - */ - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Create a tokenizer cursor to tokenize an input buffer. The caller - ** is responsible for ensuring that the input buffer remains valid - ** until the cursor is closed (using the xClose() method). - */ - int (*xOpen)( - sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ - const char *pInput, int nBytes, /* Input buffer */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ - ); - - /* - ** Destroy an existing tokenizer cursor. The fts3 module calls this - ** method exactly once for each successful call to xOpen(). - */ - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - - /* - ** Retrieve the next token from the tokenizer cursor pCursor. This - ** method should either return SQLITE_OK and set the values of the - ** "OUT" variables identified below, or SQLITE_DONE to indicate that - ** the end of the buffer has been reached, or an SQLite error code. - ** - ** *ppToken should be set to point at a buffer containing the - ** normalized version of the token (i.e. after any case-folding and/or - ** stemming has been performed). *pnBytes should be set to the length - ** of this buffer in bytes. The input text that generated the token is - ** identified by the byte offsets returned in *piStartOffset and - ** *piEndOffset. - ** - ** The buffer *ppToken is set to point at is managed by the tokenizer - ** implementation. It is only required to be valid until the next call - ** to xNext() or xClose(). - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xNext)( - sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ - const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ - int *piStartOffset, /* OUT: Byte offset of token in input buffer */ - int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ - int *piPosition /* OUT: Number of tokens returned before this one */ - ); -}; - -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; - -#endif /* _FTS3_TOKENIZER_H_ */ - -/************** End of fts3_tokenizer.h **************************************/ -/************** Continuing where we left off in fts3.c ***********************/ -#ifndef SQLITE_CORE - #include "sqlite3ext.h" - SQLITE_EXTENSION_INIT1 -#endif - - -/* TODO(shess) MAN, this thing needs some refactoring. At minimum, it -** would be nice to order the file better, perhaps something along the -** lines of: -** -** - utility functions -** - table setup functions -** - table update functions -** - table query functions -** -** Put the query functions last because they're likely to reference -** typedefs or functions from the table update section. -*/ - -#if 0 -# define FTSTRACE(A) printf A; fflush(stdout) -#else -# define FTSTRACE(A) -#endif - -/* -** Default span for NEAR operators. -*/ -#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 - -/* It is not safe to call isspace(), tolower(), or isalnum() on -** hi-bit-set characters. This is the same solution used in the -** tokenizer. -*/ -/* TODO(shess) The snippet-generation code should be using the -** tokenizer-generated tokens rather than doing its own local -** tokenization. -*/ -/* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */ -static int safe_isspace(char c){ - return (c&0x80)==0 ? isspace(c) : 0; -} -static int safe_tolower(char c){ - return (c&0x80)==0 ? tolower(c) : c; -} -static int safe_isalnum(char c){ - return (c&0x80)==0 ? isalnum(c) : 0; -} - -typedef enum DocListType { - DL_DOCIDS, /* docids only */ - DL_POSITIONS, /* docids + positions */ - DL_POSITIONS_OFFSETS /* docids + positions + offsets */ -} DocListType; - -/* -** By default, only positions and not offsets are stored in the doclists. -** To change this so that offsets are stored too, compile with -** -** -DDL_DEFAULT=DL_POSITIONS_OFFSETS -** -** If DL_DEFAULT is set to DL_DOCIDS, your table can only be inserted -** into (no deletes or updates). -*/ -#ifndef DL_DEFAULT -# define DL_DEFAULT DL_POSITIONS -#endif - -enum { - POS_END = 0, /* end of this position list */ - POS_COLUMN, /* followed by new column number */ - POS_BASE -}; - -/* MERGE_COUNT controls how often we merge segments (see comment at -** top of file). -*/ -#define MERGE_COUNT 16 - -/* utility functions */ - -/* CLEAR() and SCRAMBLE() abstract memset() on a pointer to a single -** record to prevent errors of the form: -** -** my_function(SomeType *b){ -** memset(b, '\0', sizeof(b)); // sizeof(b)!=sizeof(*b) -** } -*/ -/* TODO(shess) Obvious candidates for a header file. */ -#define CLEAR(b) memset(b, '\0', sizeof(*(b))) - -#ifndef NDEBUG -# define SCRAMBLE(b) memset(b, 0x55, sizeof(*(b))) -#else -# define SCRAMBLE(b) -#endif - -/* We may need up to VARINT_MAX bytes to store an encoded 64-bit integer. */ -#define VARINT_MAX 10 - -/* Write a 64-bit variable-length integer to memory starting at p[0]. - * The length of data written will be between 1 and VARINT_MAX bytes. - * The number of bytes written is returned. */ -static int fts3PutVarint(char *p, sqlite_int64 v){ - unsigned char *q = (unsigned char *) p; - sqlite_uint64 vu = v; - do{ - *q++ = (unsigned char) ((vu & 0x7f) | 0x80); - vu >>= 7; - }while( vu!=0 ); - q[-1] &= 0x7f; /* turn off high bit in final byte */ - assert( q - (unsigned char *)p <= VARINT_MAX ); - return (int) (q - (unsigned char *)p); -} - -/* Read a 64-bit variable-length integer from memory starting at p[0]. - * Return the number of bytes read, or 0 on error. - * The value is stored in *v. */ -static int fts3GetVarint(const char *p, sqlite_int64 *v){ - const unsigned char *q = (const unsigned char *) p; - sqlite_uint64 x = 0, y = 1; - while( (*q & 0x80) == 0x80 ){ - x += y * (*q++ & 0x7f); - y <<= 7; - if( q - (unsigned char *)p >= VARINT_MAX ){ /* bad data */ - assert( 0 ); - return 0; - } - } - x += y * (*q++); - *v = (sqlite_int64) x; - return (int) (q - (unsigned char *)p); -} - -static int fts3GetVarint32(const char *p, int *pi){ - sqlite_int64 i; - int ret = fts3GetVarint(p, &i); - *pi = (int) i; - assert( *pi==i ); - return ret; -} - -/*******************************************************************/ -/* DataBuffer is used to collect data into a buffer in piecemeal -** fashion. It implements the usual distinction between amount of -** data currently stored (nData) and buffer capacity (nCapacity). -** -** dataBufferInit - create a buffer with given initial capacity. -** dataBufferReset - forget buffer's data, retaining capacity. -** dataBufferDestroy - free buffer's data. -** dataBufferSwap - swap contents of two buffers. -** dataBufferExpand - expand capacity without adding data. -** dataBufferAppend - append data. -** dataBufferAppend2 - append two pieces of data at once. -** dataBufferReplace - replace buffer's data. -*/ -typedef struct DataBuffer { - char *pData; /* Pointer to malloc'ed buffer. */ - int nCapacity; /* Size of pData buffer. */ - int nData; /* End of data loaded into pData. */ -} DataBuffer; - -static void dataBufferInit(DataBuffer *pBuffer, int nCapacity){ - assert( nCapacity>=0 ); - pBuffer->nData = 0; - pBuffer->nCapacity = nCapacity; - pBuffer->pData = nCapacity==0 ? NULL : sqlite3_malloc(nCapacity); -} -static void dataBufferReset(DataBuffer *pBuffer){ - pBuffer->nData = 0; -} -static void dataBufferDestroy(DataBuffer *pBuffer){ - if( pBuffer->pData!=NULL ) sqlite3_free(pBuffer->pData); - SCRAMBLE(pBuffer); -} -static void dataBufferSwap(DataBuffer *pBuffer1, DataBuffer *pBuffer2){ - DataBuffer tmp = *pBuffer1; - *pBuffer1 = *pBuffer2; - *pBuffer2 = tmp; -} -static void dataBufferExpand(DataBuffer *pBuffer, int nAddCapacity){ - assert( nAddCapacity>0 ); - /* TODO(shess) Consider expanding more aggressively. Note that the - ** underlying malloc implementation may take care of such things for - ** us already. - */ - if( pBuffer->nData+nAddCapacity>pBuffer->nCapacity ){ - pBuffer->nCapacity = pBuffer->nData+nAddCapacity; - pBuffer->pData = sqlite3_realloc(pBuffer->pData, pBuffer->nCapacity); - } -} -static void dataBufferAppend(DataBuffer *pBuffer, - const char *pSource, int nSource){ - assert( nSource>0 && pSource!=NULL ); - dataBufferExpand(pBuffer, nSource); - memcpy(pBuffer->pData+pBuffer->nData, pSource, nSource); - pBuffer->nData += nSource; -} -static void dataBufferAppend2(DataBuffer *pBuffer, - const char *pSource1, int nSource1, - const char *pSource2, int nSource2){ - assert( nSource1>0 && pSource1!=NULL ); - assert( nSource2>0 && pSource2!=NULL ); - dataBufferExpand(pBuffer, nSource1+nSource2); - memcpy(pBuffer->pData+pBuffer->nData, pSource1, nSource1); - memcpy(pBuffer->pData+pBuffer->nData+nSource1, pSource2, nSource2); - pBuffer->nData += nSource1+nSource2; -} -static void dataBufferReplace(DataBuffer *pBuffer, - const char *pSource, int nSource){ - dataBufferReset(pBuffer); - dataBufferAppend(pBuffer, pSource, nSource); -} - -/* StringBuffer is a null-terminated version of DataBuffer. */ -typedef struct StringBuffer { - DataBuffer b; /* Includes null terminator. */ -} StringBuffer; - -static void initStringBuffer(StringBuffer *sb){ - dataBufferInit(&sb->b, 100); - dataBufferReplace(&sb->b, "", 1); -} -static int stringBufferLength(StringBuffer *sb){ - return sb->b.nData-1; -} -static char *stringBufferData(StringBuffer *sb){ - return sb->b.pData; -} -static void stringBufferDestroy(StringBuffer *sb){ - dataBufferDestroy(&sb->b); -} - -static void nappend(StringBuffer *sb, const char *zFrom, int nFrom){ - assert( sb->b.nData>0 ); - if( nFrom>0 ){ - sb->b.nData--; - dataBufferAppend2(&sb->b, zFrom, nFrom, "", 1); - } -} -static void append(StringBuffer *sb, const char *zFrom){ - nappend(sb, zFrom, strlen(zFrom)); -} - -/* Append a list of strings separated by commas. */ -static void appendList(StringBuffer *sb, int nString, char **azString){ - int i; - for(i=0; i0 ) append(sb, ", "); - append(sb, azString[i]); - } -} - -static int endsInWhiteSpace(StringBuffer *p){ - return stringBufferLength(p)>0 && - safe_isspace(stringBufferData(p)[stringBufferLength(p)-1]); -} - -/* If the StringBuffer ends in something other than white space, add a -** single space character to the end. -*/ -static void appendWhiteSpace(StringBuffer *p){ - if( stringBufferLength(p)==0 ) return; - if( !endsInWhiteSpace(p) ) append(p, " "); -} - -/* Remove white space from the end of the StringBuffer */ -static void trimWhiteSpace(StringBuffer *p){ - while( endsInWhiteSpace(p) ){ - p->b.pData[--p->b.nData-1] = '\0'; - } -} - -/*******************************************************************/ -/* DLReader is used to read document elements from a doclist. The -** current docid is cached, so dlrDocid() is fast. DLReader does not -** own the doclist buffer. -** -** dlrAtEnd - true if there's no more data to read. -** dlrDocid - docid of current document. -** dlrDocData - doclist data for current document (including docid). -** dlrDocDataBytes - length of same. -** dlrAllDataBytes - length of all remaining data. -** dlrPosData - position data for current document. -** dlrPosDataLen - length of pos data for current document (incl POS_END). -** dlrStep - step to current document. -** dlrInit - initial for doclist of given type against given data. -** dlrDestroy - clean up. -** -** Expected usage is something like: -** -** DLReader reader; -** dlrInit(&reader, pData, nData); -** while( !dlrAtEnd(&reader) ){ -** // calls to dlrDocid() and kin. -** dlrStep(&reader); -** } -** dlrDestroy(&reader); -*/ -typedef struct DLReader { - DocListType iType; - const char *pData; - int nData; - - sqlite_int64 iDocid; - int nElement; -} DLReader; - -static int dlrAtEnd(DLReader *pReader){ - assert( pReader->nData>=0 ); - return pReader->nData==0; -} -static sqlite_int64 dlrDocid(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->iDocid; -} -static const char *dlrDocData(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->pData; -} -static int dlrDocDataBytes(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->nElement; -} -static int dlrAllDataBytes(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - return pReader->nData; -} -/* TODO(shess) Consider adding a field to track iDocid varint length -** to make these two functions faster. This might matter (a tiny bit) -** for queries. -*/ -static const char *dlrPosData(DLReader *pReader){ - sqlite_int64 iDummy; - int n = fts3GetVarint(pReader->pData, &iDummy); - assert( !dlrAtEnd(pReader) ); - return pReader->pData+n; -} -static int dlrPosDataLen(DLReader *pReader){ - sqlite_int64 iDummy; - int n = fts3GetVarint(pReader->pData, &iDummy); - assert( !dlrAtEnd(pReader) ); - return pReader->nElement-n; -} -static void dlrStep(DLReader *pReader){ - assert( !dlrAtEnd(pReader) ); - - /* Skip past current doclist element. */ - assert( pReader->nElement<=pReader->nData ); - pReader->pData += pReader->nElement; - pReader->nData -= pReader->nElement; - - /* If there is more data, read the next doclist element. */ - if( pReader->nData!=0 ){ - sqlite_int64 iDocidDelta; - int iDummy, n = fts3GetVarint(pReader->pData, &iDocidDelta); - pReader->iDocid += iDocidDelta; - if( pReader->iType>=DL_POSITIONS ){ - assert( nnData ); - while( 1 ){ - n += fts3GetVarint32(pReader->pData+n, &iDummy); - assert( n<=pReader->nData ); - if( iDummy==POS_END ) break; - if( iDummy==POS_COLUMN ){ - n += fts3GetVarint32(pReader->pData+n, &iDummy); - assert( nnData ); - }else if( pReader->iType==DL_POSITIONS_OFFSETS ){ - n += fts3GetVarint32(pReader->pData+n, &iDummy); - n += fts3GetVarint32(pReader->pData+n, &iDummy); - assert( nnData ); - } - } - } - pReader->nElement = n; - assert( pReader->nElement<=pReader->nData ); - } -} -static void dlrInit(DLReader *pReader, DocListType iType, - const char *pData, int nData){ - assert( pData!=NULL && nData!=0 ); - pReader->iType = iType; - pReader->pData = pData; - pReader->nData = nData; - pReader->nElement = 0; - pReader->iDocid = 0; - - /* Load the first element's data. There must be a first element. */ - dlrStep(pReader); -} -static void dlrDestroy(DLReader *pReader){ - SCRAMBLE(pReader); -} - -#ifndef NDEBUG -/* Verify that the doclist can be validly decoded. Also returns the -** last docid found because it is convenient in other assertions for -** DLWriter. -*/ -static void docListValidate(DocListType iType, const char *pData, int nData, - sqlite_int64 *pLastDocid){ - sqlite_int64 iPrevDocid = 0; - assert( nData>0 ); - assert( pData!=0 ); - assert( pData+nData>pData ); - while( nData!=0 ){ - sqlite_int64 iDocidDelta; - int n = fts3GetVarint(pData, &iDocidDelta); - iPrevDocid += iDocidDelta; - if( iType>DL_DOCIDS ){ - int iDummy; - while( 1 ){ - n += fts3GetVarint32(pData+n, &iDummy); - if( iDummy==POS_END ) break; - if( iDummy==POS_COLUMN ){ - n += fts3GetVarint32(pData+n, &iDummy); - }else if( iType>DL_POSITIONS ){ - n += fts3GetVarint32(pData+n, &iDummy); - n += fts3GetVarint32(pData+n, &iDummy); - } - assert( n<=nData ); - } - } - assert( n<=nData ); - pData += n; - nData -= n; - } - if( pLastDocid ) *pLastDocid = iPrevDocid; -} -#define ASSERT_VALID_DOCLIST(i, p, n, o) docListValidate(i, p, n, o) -#else -#define ASSERT_VALID_DOCLIST(i, p, n, o) assert( 1 ) -#endif - -/*******************************************************************/ -/* DLWriter is used to write doclist data to a DataBuffer. DLWriter -** always appends to the buffer and does not own it. -** -** dlwInit - initialize to write a given type doclistto a buffer. -** dlwDestroy - clear the writer's memory. Does not free buffer. -** dlwAppend - append raw doclist data to buffer. -** dlwCopy - copy next doclist from reader to writer. -** dlwAdd - construct doclist element and append to buffer. -** Only apply dlwAdd() to DL_DOCIDS doclists (else use PLWriter). -*/ -typedef struct DLWriter { - DocListType iType; - DataBuffer *b; - sqlite_int64 iPrevDocid; -#ifndef NDEBUG - int has_iPrevDocid; -#endif -} DLWriter; - -static void dlwInit(DLWriter *pWriter, DocListType iType, DataBuffer *b){ - pWriter->b = b; - pWriter->iType = iType; - pWriter->iPrevDocid = 0; -#ifndef NDEBUG - pWriter->has_iPrevDocid = 0; -#endif -} -static void dlwDestroy(DLWriter *pWriter){ - SCRAMBLE(pWriter); -} -/* iFirstDocid is the first docid in the doclist in pData. It is -** needed because pData may point within a larger doclist, in which -** case the first item would be delta-encoded. -** -** iLastDocid is the final docid in the doclist in pData. It is -** needed to create the new iPrevDocid for future delta-encoding. The -** code could decode the passed doclist to recreate iLastDocid, but -** the only current user (docListMerge) already has decoded this -** information. -*/ -/* TODO(shess) This has become just a helper for docListMerge. -** Consider a refactor to make this cleaner. -*/ -static void dlwAppend(DLWriter *pWriter, - const char *pData, int nData, - sqlite_int64 iFirstDocid, sqlite_int64 iLastDocid){ - sqlite_int64 iDocid = 0; - char c[VARINT_MAX]; - int nFirstOld, nFirstNew; /* Old and new varint len of first docid. */ -#ifndef NDEBUG - sqlite_int64 iLastDocidDelta; -#endif - - /* Recode the initial docid as delta from iPrevDocid. */ - nFirstOld = fts3GetVarint(pData, &iDocid); - assert( nFirstOldiType==DL_DOCIDS) ); - nFirstNew = fts3PutVarint(c, iFirstDocid-pWriter->iPrevDocid); - - /* Verify that the incoming doclist is valid AND that it ends with - ** the expected docid. This is essential because we'll trust this - ** docid in future delta-encoding. - */ - ASSERT_VALID_DOCLIST(pWriter->iType, pData, nData, &iLastDocidDelta); - assert( iLastDocid==iFirstDocid-iDocid+iLastDocidDelta ); - - /* Append recoded initial docid and everything else. Rest of docids - ** should have been delta-encoded from previous initial docid. - */ - if( nFirstOldb, c, nFirstNew, - pData+nFirstOld, nData-nFirstOld); - }else{ - dataBufferAppend(pWriter->b, c, nFirstNew); - } - pWriter->iPrevDocid = iLastDocid; -} -static void dlwCopy(DLWriter *pWriter, DLReader *pReader){ - dlwAppend(pWriter, dlrDocData(pReader), dlrDocDataBytes(pReader), - dlrDocid(pReader), dlrDocid(pReader)); -} -static void dlwAdd(DLWriter *pWriter, sqlite_int64 iDocid){ - char c[VARINT_MAX]; - int n = fts3PutVarint(c, iDocid-pWriter->iPrevDocid); - - /* Docids must ascend. */ - assert( !pWriter->has_iPrevDocid || iDocid>pWriter->iPrevDocid ); - assert( pWriter->iType==DL_DOCIDS ); - - dataBufferAppend(pWriter->b, c, n); - pWriter->iPrevDocid = iDocid; -#ifndef NDEBUG - pWriter->has_iPrevDocid = 1; -#endif -} - -/*******************************************************************/ -/* PLReader is used to read data from a document's position list. As -** the caller steps through the list, data is cached so that varints -** only need to be decoded once. -** -** plrInit, plrDestroy - create/destroy a reader. -** plrColumn, plrPosition, plrStartOffset, plrEndOffset - accessors -** plrAtEnd - at end of stream, only call plrDestroy once true. -** plrStep - step to the next element. -*/ -typedef struct PLReader { - /* These refer to the next position's data. nData will reach 0 when - ** reading the last position, so plrStep() signals EOF by setting - ** pData to NULL. - */ - const char *pData; - int nData; - - DocListType iType; - int iColumn; /* the last column read */ - int iPosition; /* the last position read */ - int iStartOffset; /* the last start offset read */ - int iEndOffset; /* the last end offset read */ -} PLReader; - -static int plrAtEnd(PLReader *pReader){ - return pReader->pData==NULL; -} -static int plrColumn(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iColumn; -} -static int plrPosition(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iPosition; -} -static int plrStartOffset(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iStartOffset; -} -static int plrEndOffset(PLReader *pReader){ - assert( !plrAtEnd(pReader) ); - return pReader->iEndOffset; -} -static void plrStep(PLReader *pReader){ - int i, n; - - assert( !plrAtEnd(pReader) ); - - if( pReader->nData==0 ){ - pReader->pData = NULL; - return; - } - - n = fts3GetVarint32(pReader->pData, &i); - if( i==POS_COLUMN ){ - n += fts3GetVarint32(pReader->pData+n, &pReader->iColumn); - pReader->iPosition = 0; - pReader->iStartOffset = 0; - n += fts3GetVarint32(pReader->pData+n, &i); - } - /* Should never see adjacent column changes. */ - assert( i!=POS_COLUMN ); - - if( i==POS_END ){ - pReader->nData = 0; - pReader->pData = NULL; - return; - } - - pReader->iPosition += i-POS_BASE; - if( pReader->iType==DL_POSITIONS_OFFSETS ){ - n += fts3GetVarint32(pReader->pData+n, &i); - pReader->iStartOffset += i; - n += fts3GetVarint32(pReader->pData+n, &i); - pReader->iEndOffset = pReader->iStartOffset+i; - } - assert( n<=pReader->nData ); - pReader->pData += n; - pReader->nData -= n; -} - -static void plrInit(PLReader *pReader, DLReader *pDLReader){ - pReader->pData = dlrPosData(pDLReader); - pReader->nData = dlrPosDataLen(pDLReader); - pReader->iType = pDLReader->iType; - pReader->iColumn = 0; - pReader->iPosition = 0; - pReader->iStartOffset = 0; - pReader->iEndOffset = 0; - plrStep(pReader); -} -static void plrDestroy(PLReader *pReader){ - SCRAMBLE(pReader); -} - -/*******************************************************************/ -/* PLWriter is used in constructing a document's position list. As a -** convenience, if iType is DL_DOCIDS, PLWriter becomes a no-op. -** PLWriter writes to the associated DLWriter's buffer. -** -** plwInit - init for writing a document's poslist. -** plwDestroy - clear a writer. -** plwAdd - append position and offset information. -** plwCopy - copy next position's data from reader to writer. -** plwTerminate - add any necessary doclist terminator. -** -** Calling plwAdd() after plwTerminate() may result in a corrupt -** doclist. -*/ -/* TODO(shess) Until we've written the second item, we can cache the -** first item's information. Then we'd have three states: -** -** - initialized with docid, no positions. -** - docid and one position. -** - docid and multiple positions. -** -** Only the last state needs to actually write to dlw->b, which would -** be an improvement in the DLCollector case. -*/ -typedef struct PLWriter { - DLWriter *dlw; - - int iColumn; /* the last column written */ - int iPos; /* the last position written */ - int iOffset; /* the last start offset written */ -} PLWriter; - -/* TODO(shess) In the case where the parent is reading these values -** from a PLReader, we could optimize to a copy if that PLReader has -** the same type as pWriter. -*/ -static void plwAdd(PLWriter *pWriter, int iColumn, int iPos, - int iStartOffset, int iEndOffset){ - /* Worst-case space for POS_COLUMN, iColumn, iPosDelta, - ** iStartOffsetDelta, and iEndOffsetDelta. - */ - char c[5*VARINT_MAX]; - int n = 0; - - /* Ban plwAdd() after plwTerminate(). */ - assert( pWriter->iPos!=-1 ); - - if( pWriter->dlw->iType==DL_DOCIDS ) return; - - if( iColumn!=pWriter->iColumn ){ - n += fts3PutVarint(c+n, POS_COLUMN); - n += fts3PutVarint(c+n, iColumn); - pWriter->iColumn = iColumn; - pWriter->iPos = 0; - pWriter->iOffset = 0; - } - assert( iPos>=pWriter->iPos ); - n += fts3PutVarint(c+n, POS_BASE+(iPos-pWriter->iPos)); - pWriter->iPos = iPos; - if( pWriter->dlw->iType==DL_POSITIONS_OFFSETS ){ - assert( iStartOffset>=pWriter->iOffset ); - n += fts3PutVarint(c+n, iStartOffset-pWriter->iOffset); - pWriter->iOffset = iStartOffset; - assert( iEndOffset>=iStartOffset ); - n += fts3PutVarint(c+n, iEndOffset-iStartOffset); - } - dataBufferAppend(pWriter->dlw->b, c, n); -} -static void plwCopy(PLWriter *pWriter, PLReader *pReader){ - plwAdd(pWriter, plrColumn(pReader), plrPosition(pReader), - plrStartOffset(pReader), plrEndOffset(pReader)); -} -static void plwInit(PLWriter *pWriter, DLWriter *dlw, sqlite_int64 iDocid){ - char c[VARINT_MAX]; - int n; - - pWriter->dlw = dlw; - - /* Docids must ascend. */ - assert( !pWriter->dlw->has_iPrevDocid || iDocid>pWriter->dlw->iPrevDocid ); - n = fts3PutVarint(c, iDocid-pWriter->dlw->iPrevDocid); - dataBufferAppend(pWriter->dlw->b, c, n); - pWriter->dlw->iPrevDocid = iDocid; -#ifndef NDEBUG - pWriter->dlw->has_iPrevDocid = 1; -#endif - - pWriter->iColumn = 0; - pWriter->iPos = 0; - pWriter->iOffset = 0; -} -/* TODO(shess) Should plwDestroy() also terminate the doclist? But -** then plwDestroy() would no longer be just a destructor, it would -** also be doing work, which isn't consistent with the overall idiom. -** Another option would be for plwAdd() to always append any necessary -** terminator, so that the output is always correct. But that would -** add incremental work to the common case with the only benefit being -** API elegance. Punt for now. -*/ -static void plwTerminate(PLWriter *pWriter){ - if( pWriter->dlw->iType>DL_DOCIDS ){ - char c[VARINT_MAX]; - int n = fts3PutVarint(c, POS_END); - dataBufferAppend(pWriter->dlw->b, c, n); - } -#ifndef NDEBUG - /* Mark as terminated for assert in plwAdd(). */ - pWriter->iPos = -1; -#endif -} -static void plwDestroy(PLWriter *pWriter){ - SCRAMBLE(pWriter); -} - -/*******************************************************************/ -/* DLCollector wraps PLWriter and DLWriter to provide a -** dynamically-allocated doclist area to use during tokenization. -** -** dlcNew - malloc up and initialize a collector. -** dlcDelete - destroy a collector and all contained items. -** dlcAddPos - append position and offset information. -** dlcAddDoclist - add the collected doclist to the given buffer. -** dlcNext - terminate the current document and open another. -*/ -typedef struct DLCollector { - DataBuffer b; - DLWriter dlw; - PLWriter plw; -} DLCollector; - -/* TODO(shess) This could also be done by calling plwTerminate() and -** dataBufferAppend(). I tried that, expecting nominal performance -** differences, but it seemed to pretty reliably be worth 1% to code -** it this way. I suspect it is the incremental malloc overhead (some -** percentage of the plwTerminate() calls will cause a realloc), so -** this might be worth revisiting if the DataBuffer implementation -** changes. -*/ -static void dlcAddDoclist(DLCollector *pCollector, DataBuffer *b){ - if( pCollector->dlw.iType>DL_DOCIDS ){ - char c[VARINT_MAX]; - int n = fts3PutVarint(c, POS_END); - dataBufferAppend2(b, pCollector->b.pData, pCollector->b.nData, c, n); - }else{ - dataBufferAppend(b, pCollector->b.pData, pCollector->b.nData); - } -} -static void dlcNext(DLCollector *pCollector, sqlite_int64 iDocid){ - plwTerminate(&pCollector->plw); - plwDestroy(&pCollector->plw); - plwInit(&pCollector->plw, &pCollector->dlw, iDocid); -} -static void dlcAddPos(DLCollector *pCollector, int iColumn, int iPos, - int iStartOffset, int iEndOffset){ - plwAdd(&pCollector->plw, iColumn, iPos, iStartOffset, iEndOffset); -} - -static DLCollector *dlcNew(sqlite_int64 iDocid, DocListType iType){ - DLCollector *pCollector = sqlite3_malloc(sizeof(DLCollector)); - dataBufferInit(&pCollector->b, 0); - dlwInit(&pCollector->dlw, iType, &pCollector->b); - plwInit(&pCollector->plw, &pCollector->dlw, iDocid); - return pCollector; -} -static void dlcDelete(DLCollector *pCollector){ - plwDestroy(&pCollector->plw); - dlwDestroy(&pCollector->dlw); - dataBufferDestroy(&pCollector->b); - SCRAMBLE(pCollector); - sqlite3_free(pCollector); -} - - -/* Copy the doclist data of iType in pData/nData into *out, trimming -** unnecessary data as we go. Only columns matching iColumn are -** copied, all columns copied if iColumn is -1. Elements with no -** matching columns are dropped. The output is an iOutType doclist. -*/ -/* NOTE(shess) This code is only valid after all doclists are merged. -** If this is run before merges, then doclist items which represent -** deletion will be trimmed, and will thus not effect a deletion -** during the merge. -*/ -static void docListTrim(DocListType iType, const char *pData, int nData, - int iColumn, DocListType iOutType, DataBuffer *out){ - DLReader dlReader; - DLWriter dlWriter; - - assert( iOutType<=iType ); - - dlrInit(&dlReader, iType, pData, nData); - dlwInit(&dlWriter, iOutType, out); - - while( !dlrAtEnd(&dlReader) ){ - PLReader plReader; - PLWriter plWriter; - int match = 0; - - plrInit(&plReader, &dlReader); - - while( !plrAtEnd(&plReader) ){ - if( iColumn==-1 || plrColumn(&plReader)==iColumn ){ - if( !match ){ - plwInit(&plWriter, &dlWriter, dlrDocid(&dlReader)); - match = 1; - } - plwAdd(&plWriter, plrColumn(&plReader), plrPosition(&plReader), - plrStartOffset(&plReader), plrEndOffset(&plReader)); - } - plrStep(&plReader); - } - if( match ){ - plwTerminate(&plWriter); - plwDestroy(&plWriter); - } - - plrDestroy(&plReader); - dlrStep(&dlReader); - } - dlwDestroy(&dlWriter); - dlrDestroy(&dlReader); -} - -/* Used by docListMerge() to keep doclists in the ascending order by -** docid, then ascending order by age (so the newest comes first). -*/ -typedef struct OrderedDLReader { - DLReader *pReader; - - /* TODO(shess) If we assume that docListMerge pReaders is ordered by - ** age (which we do), then we could use pReader comparisons to break - ** ties. - */ - int idx; -} OrderedDLReader; - -/* Order eof to end, then by docid asc, idx desc. */ -static int orderedDLReaderCmp(OrderedDLReader *r1, OrderedDLReader *r2){ - if( dlrAtEnd(r1->pReader) ){ - if( dlrAtEnd(r2->pReader) ) return 0; /* Both atEnd(). */ - return 1; /* Only r1 atEnd(). */ - } - if( dlrAtEnd(r2->pReader) ) return -1; /* Only r2 atEnd(). */ - - if( dlrDocid(r1->pReader)pReader) ) return -1; - if( dlrDocid(r1->pReader)>dlrDocid(r2->pReader) ) return 1; - - /* Descending on idx. */ - return r2->idx-r1->idx; -} - -/* Bubble p[0] to appropriate place in p[1..n-1]. Assumes that -** p[1..n-1] is already sorted. -*/ -/* TODO(shess) Is this frequent enough to warrant a binary search? -** Before implementing that, instrument the code to check. In most -** current usage, I expect that p[0] will be less than p[1] a very -** high proportion of the time. -*/ -static void orderedDLReaderReorder(OrderedDLReader *p, int n){ - while( n>1 && orderedDLReaderCmp(p, p+1)>0 ){ - OrderedDLReader tmp = p[0]; - p[0] = p[1]; - p[1] = tmp; - n--; - p++; - } -} - -/* Given an array of doclist readers, merge their doclist elements -** into out in sorted order (by docid), dropping elements from older -** readers when there is a duplicate docid. pReaders is assumed to be -** ordered by age, oldest first. -*/ -/* TODO(shess) nReaders must be <= MERGE_COUNT. This should probably -** be fixed. -*/ -static void docListMerge(DataBuffer *out, - DLReader *pReaders, int nReaders){ - OrderedDLReader readers[MERGE_COUNT]; - DLWriter writer; - int i, n; - const char *pStart = 0; - int nStart = 0; - sqlite_int64 iFirstDocid = 0, iLastDocid = 0; - - assert( nReaders>0 ); - if( nReaders==1 ){ - dataBufferAppend(out, dlrDocData(pReaders), dlrAllDataBytes(pReaders)); - return; - } - - assert( nReaders<=MERGE_COUNT ); - n = 0; - for(i=0; i0 ){ - orderedDLReaderReorder(readers+i, nReaders-i); - } - - dlwInit(&writer, pReaders[0].iType, out); - while( !dlrAtEnd(readers[0].pReader) ){ - sqlite_int64 iDocid = dlrDocid(readers[0].pReader); - - /* If this is a continuation of the current buffer to copy, extend - ** that buffer. memcpy() seems to be more efficient if it has a - ** lots of data to copy. - */ - if( dlrDocData(readers[0].pReader)==pStart+nStart ){ - nStart += dlrDocDataBytes(readers[0].pReader); - }else{ - if( pStart!=0 ){ - dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); - } - pStart = dlrDocData(readers[0].pReader); - nStart = dlrDocDataBytes(readers[0].pReader); - iFirstDocid = iDocid; - } - iLastDocid = iDocid; - dlrStep(readers[0].pReader); - - /* Drop all of the older elements with the same docid. */ - for(i=1; i0 ){ - orderedDLReaderReorder(readers+i, nReaders-i); - } - } - - /* Copy over any remaining elements. */ - if( nStart>0 ) dlwAppend(&writer, pStart, nStart, iFirstDocid, iLastDocid); - dlwDestroy(&writer); -} - -/* Helper function for posListUnion(). Compares the current position -** between left and right, returning as standard C idiom of <0 if -** left0 if left>right, and 0 if left==right. "End" always -** compares greater. -*/ -static int posListCmp(PLReader *pLeft, PLReader *pRight){ - assert( pLeft->iType==pRight->iType ); - if( pLeft->iType==DL_DOCIDS ) return 0; - - if( plrAtEnd(pLeft) ) return plrAtEnd(pRight) ? 0 : 1; - if( plrAtEnd(pRight) ) return -1; - - if( plrColumn(pLeft)plrColumn(pRight) ) return 1; - - if( plrPosition(pLeft)plrPosition(pRight) ) return 1; - if( pLeft->iType==DL_POSITIONS ) return 0; - - if( plrStartOffset(pLeft)plrStartOffset(pRight) ) return 1; - - if( plrEndOffset(pLeft)plrEndOffset(pRight) ) return 1; - - return 0; -} - -/* Write the union of position lists in pLeft and pRight to pOut. -** "Union" in this case meaning "All unique position tuples". Should -** work with any doclist type, though both inputs and the output -** should be the same type. -*/ -static void posListUnion(DLReader *pLeft, DLReader *pRight, DLWriter *pOut){ - PLReader left, right; - PLWriter writer; - - assert( dlrDocid(pLeft)==dlrDocid(pRight) ); - assert( pLeft->iType==pRight->iType ); - assert( pLeft->iType==pOut->iType ); - - plrInit(&left, pLeft); - plrInit(&right, pRight); - plwInit(&writer, pOut, dlrDocid(pLeft)); - - while( !plrAtEnd(&left) || !plrAtEnd(&right) ){ - int c = posListCmp(&left, &right); - if( c<0 ){ - plwCopy(&writer, &left); - plrStep(&left); - }else if( c>0 ){ - plwCopy(&writer, &right); - plrStep(&right); - }else{ - plwCopy(&writer, &left); - plrStep(&left); - plrStep(&right); - } - } - - plwTerminate(&writer); - plwDestroy(&writer); - plrDestroy(&left); - plrDestroy(&right); -} - -/* Write the union of doclists in pLeft and pRight to pOut. For -** docids in common between the inputs, the union of the position -** lists is written. Inputs and outputs are always type DL_DEFAULT. -*/ -static void docListUnion( - const char *pLeft, int nLeft, - const char *pRight, int nRight, - DataBuffer *pOut /* Write the combined doclist here */ -){ - DLReader left, right; - DLWriter writer; - - if( nLeft==0 ){ - if( nRight!=0) dataBufferAppend(pOut, pRight, nRight); - return; - } - if( nRight==0 ){ - dataBufferAppend(pOut, pLeft, nLeft); - return; - } - - dlrInit(&left, DL_DEFAULT, pLeft, nLeft); - dlrInit(&right, DL_DEFAULT, pRight, nRight); - dlwInit(&writer, DL_DEFAULT, pOut); - - while( !dlrAtEnd(&left) || !dlrAtEnd(&right) ){ - if( dlrAtEnd(&right) ){ - dlwCopy(&writer, &left); - dlrStep(&left); - }else if( dlrAtEnd(&left) ){ - dlwCopy(&writer, &right); - dlrStep(&right); - }else if( dlrDocid(&left)dlrDocid(&right) ){ - dlwCopy(&writer, &right); - dlrStep(&right); - }else{ - posListUnion(&left, &right, &writer); - dlrStep(&left); - dlrStep(&right); - } - } - - dlrDestroy(&left); - dlrDestroy(&right); - dlwDestroy(&writer); -} - -/* -** This function is used as part of the implementation of phrase and -** NEAR matching. -** -** pLeft and pRight are DLReaders positioned to the same docid in -** lists of type DL_POSITION. This function writes an entry to the -** DLWriter pOut for each position in pRight that is less than -** (nNear+1) greater (but not equal to or smaller) than a position -** in pLeft. For example, if nNear is 0, and the positions contained -** by pLeft and pRight are: -** -** pLeft: 5 10 15 20 -** pRight: 6 9 17 21 -** -** then the docid is added to pOut. If pOut is of type DL_POSITIONS, -** then a positionids "6" and "21" are also added to pOut. -** -** If boolean argument isSaveLeft is true, then positionids are copied -** from pLeft instead of pRight. In the example above, the positions "5" -** and "20" would be added instead of "6" and "21". -*/ -static void posListPhraseMerge( - DLReader *pLeft, - DLReader *pRight, - int nNear, - int isSaveLeft, - DLWriter *pOut -){ - PLReader left, right; - PLWriter writer; - int match = 0; - - assert( dlrDocid(pLeft)==dlrDocid(pRight) ); - assert( pOut->iType!=DL_POSITIONS_OFFSETS ); - - plrInit(&left, pLeft); - plrInit(&right, pRight); - - while( !plrAtEnd(&left) && !plrAtEnd(&right) ){ - if( plrColumn(&left)plrColumn(&right) ){ - plrStep(&right); - }else if( plrPosition(&left)>=plrPosition(&right) ){ - plrStep(&right); - }else{ - if( (plrPosition(&right)-plrPosition(&left))<=(nNear+1) ){ - if( !match ){ - plwInit(&writer, pOut, dlrDocid(pLeft)); - match = 1; - } - if( !isSaveLeft ){ - plwAdd(&writer, plrColumn(&right), plrPosition(&right), 0, 0); - }else{ - plwAdd(&writer, plrColumn(&left), plrPosition(&left), 0, 0); - } - plrStep(&right); - }else{ - plrStep(&left); - } - } - } - - if( match ){ - plwTerminate(&writer); - plwDestroy(&writer); - } - - plrDestroy(&left); - plrDestroy(&right); -} - -/* -** Compare the values pointed to by the PLReaders passed as arguments. -** Return -1 if the value pointed to by pLeft is considered less than -** the value pointed to by pRight, +1 if it is considered greater -** than it, or 0 if it is equal. i.e. -** -** (*pLeft - *pRight) -** -** A PLReader that is in the EOF condition is considered greater than -** any other. If neither argument is in EOF state, the return value of -** plrColumn() is used. If the plrColumn() values are equal, the -** comparison is on the basis of plrPosition(). -*/ -static int plrCompare(PLReader *pLeft, PLReader *pRight){ - assert(!plrAtEnd(pLeft) || !plrAtEnd(pRight)); - - if( plrAtEnd(pRight) || plrAtEnd(pLeft) ){ - return (plrAtEnd(pRight) ? -1 : 1); - } - if( plrColumn(pLeft)!=plrColumn(pRight) ){ - return ((plrColumn(pLeft)0) -** and write the results into pOut. -** -** A phrase intersection means that two documents only match -** if pLeft.iPos+1==pRight.iPos. -** -** A NEAR intersection means that two documents only match if -** (abs(pLeft.iPos-pRight.iPos) one AND (two OR three) - * [one OR two three] ==> (one OR two) AND three - * - * A "-" before a term matches all entries that lack that term. - * The "-" must occur immediately before the term with in intervening - * space. This is how the search engines do it. - * - * A NOT term cannot be the right-hand operand of an OR. If this - * occurs in the query string, the NOT is ignored: - * - * [one OR -two] ==> one OR two - * - */ -typedef struct Query { - fulltext_vtab *pFts; /* The full text index */ - int nTerms; /* Number of terms in the query */ - QueryTerm *pTerms; /* Array of terms. Space obtained from malloc() */ - int nextIsOr; /* Set the isOr flag on the next inserted term */ - int nextIsNear; /* Set the isOr flag on the next inserted term */ - int nextColumn; /* Next word parsed must be in this column */ - int dfltColumn; /* The default column */ -} Query; - - -/* -** An instance of the following structure keeps track of generated -** matching-word offset information and snippets. -*/ -typedef struct Snippet { - int nMatch; /* Total number of matches */ - int nAlloc; /* Space allocated for aMatch[] */ - struct snippetMatch { /* One entry for each matching term */ - char snStatus; /* Status flag for use while constructing snippets */ - short int iCol; /* The column that contains the match */ - short int iTerm; /* The index in Query.pTerms[] of the matching term */ - int iToken; /* The index of the matching document token */ - short int nByte; /* Number of bytes in the term */ - int iStart; /* The offset to the first character of the term */ - } *aMatch; /* Points to space obtained from malloc */ - char *zOffset; /* Text rendering of aMatch[] */ - int nOffset; /* strlen(zOffset) */ - char *zSnippet; /* Snippet text */ - int nSnippet; /* strlen(zSnippet) */ -} Snippet; - - -typedef enum QueryType { - QUERY_GENERIC, /* table scan */ - QUERY_DOCID, /* lookup by docid */ - QUERY_FULLTEXT /* QUERY_FULLTEXT + [i] is a full-text search for column i*/ -} QueryType; - -typedef enum fulltext_statement { - CONTENT_INSERT_STMT, - CONTENT_SELECT_STMT, - CONTENT_UPDATE_STMT, - CONTENT_DELETE_STMT, - - BLOCK_INSERT_STMT, - BLOCK_SELECT_STMT, - BLOCK_DELETE_STMT, - - SEGDIR_MAX_INDEX_STMT, - SEGDIR_SET_STMT, - SEGDIR_SELECT_STMT, - SEGDIR_SPAN_STMT, - SEGDIR_DELETE_STMT, - SEGDIR_SELECT_ALL_STMT, - - MAX_STMT /* Always at end! */ -} fulltext_statement; - -/* These must exactly match the enum above. */ -/* TODO(shess): Is there some risk that a statement will be used in two -** cursors at once, e.g. if a query joins a virtual table to itself? -** If so perhaps we should move some of these to the cursor object. -*/ -static const char *const fulltext_zStatement[MAX_STMT] = { - /* CONTENT_INSERT */ NULL, /* generated in contentInsertStatement() */ - /* CONTENT_SELECT */ NULL, /* generated in contentSelectStatement() */ - /* CONTENT_UPDATE */ NULL, /* generated in contentUpdateStatement() */ - /* CONTENT_DELETE */ "delete from %_content where docid = ?", - - /* BLOCK_INSERT */ - "insert into %_segments (blockid, block) values (null, ?)", - /* BLOCK_SELECT */ "select block from %_segments where blockid = ?", - /* BLOCK_DELETE */ "delete from %_segments where blockid between ? and ?", - - /* SEGDIR_MAX_INDEX */ "select max(idx) from %_segdir where level = ?", - /* SEGDIR_SET */ "insert into %_segdir values (?, ?, ?, ?, ?, ?)", - /* SEGDIR_SELECT */ - "select start_block, leaves_end_block, root from %_segdir " - " where level = ? order by idx", - /* SEGDIR_SPAN */ - "select min(start_block), max(end_block) from %_segdir " - " where level = ? and start_block <> 0", - /* SEGDIR_DELETE */ "delete from %_segdir where level = ?", - /* SEGDIR_SELECT_ALL */ - "select root, leaves_end_block from %_segdir order by level desc, idx", -}; - -/* -** A connection to a fulltext index is an instance of the following -** structure. The xCreate and xConnect methods create an instance -** of this structure and xDestroy and xDisconnect free that instance. -** All other methods receive a pointer to the structure as one of their -** arguments. -*/ -struct fulltext_vtab { - sqlite3_vtab base; /* Base class used by SQLite core */ - sqlite3 *db; /* The database connection */ - const char *zDb; /* logical database name */ - const char *zName; /* virtual table name */ - int nColumn; /* number of columns in virtual table */ - char **azColumn; /* column names. malloced */ - char **azContentColumn; /* column names in content table; malloced */ - sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ - - /* Precompiled statements which we keep as long as the table is - ** open. - */ - sqlite3_stmt *pFulltextStatements[MAX_STMT]; - - /* Precompiled statements used for segment merges. We run a - ** separate select across the leaf level of each tree being merged. - */ - sqlite3_stmt *pLeafSelectStmts[MERGE_COUNT]; - /* The statement used to prepare pLeafSelectStmts. */ -#define LEAF_SELECT \ - "select block from %_segments where blockid between ? and ? order by blockid" - - /* These buffer pending index updates during transactions. - ** nPendingData estimates the memory size of the pending data. It - ** doesn't include the hash-bucket overhead, nor any malloc - ** overhead. When nPendingData exceeds kPendingThreshold, the - ** buffer is flushed even before the transaction closes. - ** pendingTerms stores the data, and is only valid when nPendingData - ** is >=0 (nPendingData<0 means pendingTerms has not been - ** initialized). iPrevDocid is the last docid written, used to make - ** certain we're inserting in sorted order. - */ - int nPendingData; -#define kPendingThreshold (1*1024*1024) - sqlite_int64 iPrevDocid; - fts3Hash pendingTerms; -}; - -/* -** When the core wants to do a query, it create a cursor using a -** call to xOpen. This structure is an instance of a cursor. It -** is destroyed by xClose. -*/ -typedef struct fulltext_cursor { - sqlite3_vtab_cursor base; /* Base class used by SQLite core */ - QueryType iCursorType; /* Copy of sqlite3_index_info.idxNum */ - sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ - int eof; /* True if at End Of Results */ - Query q; /* Parsed query string */ - Snippet snippet; /* Cached snippet for the current row */ - int iColumn; /* Column being searched */ - DataBuffer result; /* Doclist results from fulltextQuery */ - DLReader reader; /* Result reader if result not empty */ -} fulltext_cursor; - -static struct fulltext_vtab *cursor_vtab(fulltext_cursor *c){ - return (fulltext_vtab *) c->base.pVtab; -} - -static const sqlite3_module fts3Module; /* forward declaration */ - -/* Return a dynamically generated statement of the form - * insert into %_content (docid, ...) values (?, ...) - */ -static const char *contentInsertStatement(fulltext_vtab *v){ - StringBuffer sb; - int i; - - initStringBuffer(&sb); - append(&sb, "insert into %_content (docid, "); - appendList(&sb, v->nColumn, v->azContentColumn); - append(&sb, ") values (?"); - for(i=0; inColumn; ++i) - append(&sb, ", ?"); - append(&sb, ")"); - return stringBufferData(&sb); -} - -/* Return a dynamically generated statement of the form - * select from %_content where docid = ? - */ -static const char *contentSelectStatement(fulltext_vtab *v){ - StringBuffer sb; - initStringBuffer(&sb); - append(&sb, "SELECT "); - appendList(&sb, v->nColumn, v->azContentColumn); - append(&sb, " FROM %_content WHERE docid = ?"); - return stringBufferData(&sb); -} - -/* Return a dynamically generated statement of the form - * update %_content set [col_0] = ?, [col_1] = ?, ... - * where docid = ? - */ -static const char *contentUpdateStatement(fulltext_vtab *v){ - StringBuffer sb; - int i; - - initStringBuffer(&sb); - append(&sb, "update %_content set "); - for(i=0; inColumn; ++i) { - if( i>0 ){ - append(&sb, ", "); - } - append(&sb, v->azContentColumn[i]); - append(&sb, " = ?"); - } - append(&sb, " where docid = ?"); - return stringBufferData(&sb); -} - -/* Puts a freshly-prepared statement determined by iStmt in *ppStmt. -** If the indicated statement has never been prepared, it is prepared -** and cached, otherwise the cached version is reset. -*/ -static int sql_get_statement(fulltext_vtab *v, fulltext_statement iStmt, - sqlite3_stmt **ppStmt){ - assert( iStmtpFulltextStatements[iStmt]==NULL ){ - const char *zStmt; - int rc; - switch( iStmt ){ - case CONTENT_INSERT_STMT: - zStmt = contentInsertStatement(v); break; - case CONTENT_SELECT_STMT: - zStmt = contentSelectStatement(v); break; - case CONTENT_UPDATE_STMT: - zStmt = contentUpdateStatement(v); break; - default: - zStmt = fulltext_zStatement[iStmt]; - } - rc = sql_prepare(v->db, v->zDb, v->zName, &v->pFulltextStatements[iStmt], - zStmt); - if( zStmt != fulltext_zStatement[iStmt]) sqlite3_free((void *) zStmt); - if( rc!=SQLITE_OK ) return rc; - } else { - int rc = sqlite3_reset(v->pFulltextStatements[iStmt]); - if( rc!=SQLITE_OK ) return rc; - } - - *ppStmt = v->pFulltextStatements[iStmt]; - return SQLITE_OK; -} - -/* Like sqlite3_step(), but convert SQLITE_DONE to SQLITE_OK and -** SQLITE_ROW to SQLITE_ERROR. Useful for statements like UPDATE, -** where we expect no results. -*/ -static int sql_single_step(sqlite3_stmt *s){ - int rc = sqlite3_step(s); - return (rc==SQLITE_DONE) ? SQLITE_OK : rc; -} - -/* Like sql_get_statement(), but for special replicated LEAF_SELECT -** statements. -*/ -/* TODO(shess) Write version for generic statements and then share -** that between the cached-statement functions. -*/ -static int sql_get_leaf_statement(fulltext_vtab *v, int idx, - sqlite3_stmt **ppStmt){ - assert( idx>=0 && idxpLeafSelectStmts[idx]==NULL ){ - int rc = sql_prepare(v->db, v->zDb, v->zName, &v->pLeafSelectStmts[idx], - LEAF_SELECT); - if( rc!=SQLITE_OK ) return rc; - }else{ - int rc = sqlite3_reset(v->pLeafSelectStmts[idx]); - if( rc!=SQLITE_OK ) return rc; - } - - *ppStmt = v->pLeafSelectStmts[idx]; - return SQLITE_OK; -} - -/* insert into %_content (docid, ...) values ([docid], [pValues]) -** If the docid contains SQL NULL, then a unique docid will be -** generated. -*/ -static int content_insert(fulltext_vtab *v, sqlite3_value *docid, - sqlite3_value **pValues){ - sqlite3_stmt *s; - int i; - int rc = sql_get_statement(v, CONTENT_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_value(s, 1, docid); - if( rc!=SQLITE_OK ) return rc; - - for(i=0; inColumn; ++i){ - rc = sqlite3_bind_value(s, 2+i, pValues[i]); - if( rc!=SQLITE_OK ) return rc; - } - - return sql_single_step(s); -} - -/* update %_content set col0 = pValues[0], col1 = pValues[1], ... - * where docid = [iDocid] */ -static int content_update(fulltext_vtab *v, sqlite3_value **pValues, - sqlite_int64 iDocid){ - sqlite3_stmt *s; - int i; - int rc = sql_get_statement(v, CONTENT_UPDATE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - for(i=0; inColumn; ++i){ - rc = sqlite3_bind_value(s, 1+i, pValues[i]); - if( rc!=SQLITE_OK ) return rc; - } - - rc = sqlite3_bind_int64(s, 1+v->nColumn, iDocid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -static void freeStringArray(int nString, const char **pString){ - int i; - - for (i=0 ; i < nString ; ++i) { - if( pString[i]!=NULL ) sqlite3_free((void *) pString[i]); - } - sqlite3_free((void *) pString); -} - -/* select * from %_content where docid = [iDocid] - * The caller must delete the returned array and all strings in it. - * null fields will be NULL in the returned array. - * - * TODO: Perhaps we should return pointer/length strings here for consistency - * with other code which uses pointer/length. */ -static int content_select(fulltext_vtab *v, sqlite_int64 iDocid, - const char ***pValues){ - sqlite3_stmt *s; - const char **values; - int i; - int rc; - - *pValues = NULL; - - rc = sql_get_statement(v, CONTENT_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iDocid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc!=SQLITE_ROW ) return rc; - - values = (const char **) sqlite3_malloc(v->nColumn * sizeof(const char *)); - for(i=0; inColumn; ++i){ - if( sqlite3_column_type(s, i)==SQLITE_NULL ){ - values[i] = NULL; - }else{ - values[i] = string_dup((char*)sqlite3_column_text(s, i)); - } - } - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ){ - *pValues = values; - return SQLITE_OK; - } - - freeStringArray(v->nColumn, values); - return rc; -} - -/* delete from %_content where docid = [iDocid ] */ -static int content_delete(fulltext_vtab *v, sqlite_int64 iDocid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, CONTENT_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iDocid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* insert into %_segments values ([pData]) -** returns assigned blockid in *piBlockid -*/ -static int block_insert(fulltext_vtab *v, const char *pData, int nData, - sqlite_int64 *piBlockid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, BLOCK_INSERT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 1, pData, nData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - - /* blockid column is an alias for rowid. */ - *piBlockid = sqlite3_last_insert_rowid(v->db); - return SQLITE_OK; -} - -/* delete from %_segments -** where blockid between [iStartBlockid] and [iEndBlockid] -** -** Deletes the range of blocks, inclusive, used to delete the blocks -** which form a segment. -*/ -static int block_delete(fulltext_vtab *v, - sqlite_int64 iStartBlockid, sqlite_int64 iEndBlockid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, BLOCK_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iStartBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Returns SQLITE_ROW with *pidx set to the maximum segment idx found -** at iLevel. Returns SQLITE_DONE if there are no segments at -** iLevel. Otherwise returns an error. -*/ -static int segdir_max_index(fulltext_vtab *v, int iLevel, int *pidx){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_MAX_INDEX_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - /* Should always get at least one row due to how max() works. */ - if( rc==SQLITE_DONE ) return SQLITE_DONE; - if( rc!=SQLITE_ROW ) return rc; - - /* NULL means that there were no inputs to max(). */ - if( SQLITE_NULL==sqlite3_column_type(s, 0) ){ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - return rc; - } - - *pidx = sqlite3_column_int(s, 0); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - return SQLITE_ROW; -} - -/* insert into %_segdir values ( -** [iLevel], [idx], -** [iStartBlockid], [iLeavesEndBlockid], [iEndBlockid], -** [pRootData] -** ) -*/ -static int segdir_set(fulltext_vtab *v, int iLevel, int idx, - sqlite_int64 iStartBlockid, - sqlite_int64 iLeavesEndBlockid, - sqlite_int64 iEndBlockid, - const char *pRootData, int nRootData){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_SET_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 2, idx); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 3, iStartBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 4, iLeavesEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 5, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_blob(s, 6, pRootData, nRootData, SQLITE_STATIC); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* Queries %_segdir for the block span of the segments in level -** iLevel. Returns SQLITE_DONE if there are no blocks for iLevel, -** SQLITE_ROW if there are blocks, else an error. -*/ -static int segdir_span(fulltext_vtab *v, int iLevel, - sqlite_int64 *piStartBlockid, - sqlite_int64 *piEndBlockid){ - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_SPAN_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_DONE; /* Should never happen */ - if( rc!=SQLITE_ROW ) return rc; - - /* This happens if all segments at this level are entirely inline. */ - if( SQLITE_NULL==sqlite3_column_type(s, 0) ){ - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - int rc2 = sqlite3_step(s); - if( rc2==SQLITE_ROW ) return SQLITE_ERROR; - return rc2; - } - - *piStartBlockid = sqlite3_column_int64(s, 0); - *piEndBlockid = sqlite3_column_int64(s, 1); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - return SQLITE_ROW; -} - -/* Delete the segment blocks and segment directory records for all -** segments at iLevel. -*/ -static int segdir_delete(fulltext_vtab *v, int iLevel){ - sqlite3_stmt *s; - sqlite_int64 iStartBlockid, iEndBlockid; - int rc = segdir_span(v, iLevel, &iStartBlockid, &iEndBlockid); - if( rc!=SQLITE_ROW && rc!=SQLITE_DONE ) return rc; - - if( rc==SQLITE_ROW ){ - rc = block_delete(v, iStartBlockid, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - } - - /* Delete the segment directory itself. */ - rc = sql_get_statement(v, SEGDIR_DELETE_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - return sql_single_step(s); -} - -/* TODO(shess) clearPendingTerms() is far down the file because -** writeZeroSegment() is far down the file because LeafWriter is far -** down the file. Consider refactoring the code to move the non-vtab -** code above the vtab code so that we don't need this forward -** reference. -*/ -static int clearPendingTerms(fulltext_vtab *v); - -/* -** Free the memory used to contain a fulltext_vtab structure. -*/ -static void fulltext_vtab_destroy(fulltext_vtab *v){ - int iStmt, i; - - FTSTRACE(("FTS3 Destroy %p\n", v)); - for( iStmt=0; iStmtpFulltextStatements[iStmt]!=NULL ){ - sqlite3_finalize(v->pFulltextStatements[iStmt]); - v->pFulltextStatements[iStmt] = NULL; - } - } - - for( i=0; ipLeafSelectStmts[i]!=NULL ){ - sqlite3_finalize(v->pLeafSelectStmts[i]); - v->pLeafSelectStmts[i] = NULL; - } - } - - if( v->pTokenizer!=NULL ){ - v->pTokenizer->pModule->xDestroy(v->pTokenizer); - v->pTokenizer = NULL; - } - - clearPendingTerms(v); - - sqlite3_free(v->azColumn); - for(i = 0; i < v->nColumn; ++i) { - sqlite3_free(v->azContentColumn[i]); - } - sqlite3_free(v->azContentColumn); - sqlite3_free(v); -} - -/* -** Token types for parsing the arguments to xConnect or xCreate. -*/ -#define TOKEN_EOF 0 /* End of file */ -#define TOKEN_SPACE 1 /* Any kind of whitespace */ -#define TOKEN_ID 2 /* An identifier */ -#define TOKEN_STRING 3 /* A string literal */ -#define TOKEN_PUNCT 4 /* A single punctuation character */ - -/* -** If X is a character that can be used in an identifier then -** ftsIdChar(X) will be true. Otherwise it is false. -** -** For ASCII, any character with the high-order bit set is -** allowed in an identifier. For 7-bit characters, -** isFtsIdChar[X] must be 1. -** -** Ticket #1066. the SQL standard does not allow '$' in the -** middle of identfiers. But many SQL implementations do. -** SQLite will allow '$' in identifiers for compatibility. -** But the feature is undocumented. -*/ -static const char isFtsIdChar[] = { -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ - 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ -}; -#define ftsIdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isFtsIdChar[c-0x20])) - - -/* -** Return the length of the token that begins at z[0]. -** Store the token type in *tokenType before returning. -*/ -static int ftsGetToken(const char *z, int *tokenType){ - int i, c; - switch( *z ){ - case 0: { - *tokenType = TOKEN_EOF; - return 0; - } - case ' ': case '\t': case '\n': case '\f': case '\r': { - for(i=1; safe_isspace(z[i]); i++){} - *tokenType = TOKEN_SPACE; - return i; - } - case '`': - case '\'': - case '"': { - int delim = z[0]; - for(i=1; (c=z[i])!=0; i++){ - if( c==delim ){ - if( z[i+1]==delim ){ - i++; - }else{ - break; - } - } - } - *tokenType = TOKEN_STRING; - return i + (c!=0); - } - case '[': { - for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} - *tokenType = TOKEN_ID; - return i; - } - default: { - if( !ftsIdChar(*z) ){ - break; - } - for(i=1; ftsIdChar(z[i]); i++){} - *tokenType = TOKEN_ID; - return i; - } - } - *tokenType = TOKEN_PUNCT; - return 1; -} - -/* -** A token extracted from a string is an instance of the following -** structure. -*/ -typedef struct FtsToken { - const char *z; /* Pointer to token text. Not '\000' terminated */ - short int n; /* Length of the token text in bytes. */ -} FtsToken; - -/* -** Given a input string (which is really one of the argv[] parameters -** passed into xConnect or xCreate) split the string up into tokens. -** Return an array of pointers to '\000' terminated strings, one string -** for each non-whitespace token. -** -** The returned array is terminated by a single NULL pointer. -** -** Space to hold the returned array is obtained from a single -** malloc and should be freed by passing the return value to free(). -** The individual strings within the token list are all a part of -** the single memory allocation and will all be freed at once. -*/ -static char **tokenizeString(const char *z, int *pnToken){ - int nToken = 0; - FtsToken *aToken = sqlite3_malloc( strlen(z) * sizeof(aToken[0]) ); - int n = 1; - int e, i; - int totalSize = 0; - char **azToken; - char *zCopy; - while( n>0 ){ - n = ftsGetToken(z, &e); - if( e!=TOKEN_SPACE ){ - aToken[nToken].z = z; - aToken[nToken].n = n; - nToken++; - totalSize += n+1; - } - z += n; - } - azToken = (char**)sqlite3_malloc( nToken*sizeof(char*) + totalSize ); - zCopy = (char*)&azToken[nToken]; - nToken--; - for(i=0; i=0 ){ - azIn[j] = azIn[i]; - } - j++; - } - } - azIn[j] = 0; - } -} - - -/* -** Find the first alphanumeric token in the string zIn. Null-terminate -** this token. Remove any quotation marks. And return a pointer to -** the result. -*/ -static char *firstToken(char *zIn, char **pzTail){ - int n, ttype; - while(1){ - n = ftsGetToken(zIn, &ttype); - if( ttype==TOKEN_SPACE ){ - zIn += n; - }else if( ttype==TOKEN_EOF ){ - *pzTail = zIn; - return 0; - }else{ - zIn[n] = 0; - *pzTail = &zIn[1]; - dequoteString(zIn); - return zIn; - } - } - /*NOTREACHED*/ -} - -/* Return true if... -** -** * s begins with the string t, ignoring case -** * s is longer than t -** * The first character of s beyond t is not a alphanumeric -** -** Ignore leading space in *s. -** -** To put it another way, return true if the first token of -** s[] is t[]. -*/ -static int startsWith(const char *s, const char *t){ - while( safe_isspace(*s) ){ s++; } - while( *t ){ - if( safe_tolower(*s++)!=safe_tolower(*t++) ) return 0; - } - return *s!='_' && !safe_isalnum(*s); -} - -/* -** An instance of this structure defines the "spec" of a -** full text index. This structure is populated by parseSpec -** and use by fulltextConnect and fulltextCreate. -*/ -typedef struct TableSpec { - const char *zDb; /* Logical database name */ - const char *zName; /* Name of the full-text index */ - int nColumn; /* Number of columns to be indexed */ - char **azColumn; /* Original names of columns to be indexed */ - char **azContentColumn; /* Column names for %_content */ - char **azTokenizer; /* Name of tokenizer and its arguments */ -} TableSpec; - -/* -** Reclaim all of the memory used by a TableSpec -*/ -static void clearTableSpec(TableSpec *p) { - sqlite3_free(p->azColumn); - sqlite3_free(p->azContentColumn); - sqlite3_free(p->azTokenizer); -} - -/* Parse a CREATE VIRTUAL TABLE statement, which looks like this: - * - * CREATE VIRTUAL TABLE email - * USING fts3(subject, body, tokenize mytokenizer(myarg)) - * - * We return parsed information in a TableSpec structure. - * - */ -static int parseSpec(TableSpec *pSpec, int argc, const char *const*argv, - char**pzErr){ - int i, n; - char *z, *zDummy; - char **azArg; - const char *zTokenizer = 0; /* argv[] entry describing the tokenizer */ - - assert( argc>=3 ); - /* Current interface: - ** argv[0] - module name - ** argv[1] - database name - ** argv[2] - table name - ** argv[3..] - columns, optionally followed by tokenizer specification - ** and snippet delimiters specification. - */ - - /* Make a copy of the complete argv[][] array in a single allocation. - ** The argv[][] array is read-only and transient. We can write to the - ** copy in order to modify things and the copy is persistent. - */ - CLEAR(pSpec); - for(i=n=0; izDb = azArg[1]; - pSpec->zName = azArg[2]; - pSpec->nColumn = 0; - pSpec->azColumn = azArg; - zTokenizer = "tokenize simple"; - for(i=3; inColumn] = firstToken(azArg[i], &zDummy); - pSpec->nColumn++; - } - } - if( pSpec->nColumn==0 ){ - azArg[0] = "content"; - pSpec->nColumn = 1; - } - - /* - ** Construct the list of content column names. - ** - ** Each content column name will be of the form cNNAAAA - ** where NN is the column number and AAAA is the sanitized - ** column name. "sanitized" means that special characters are - ** converted to "_". The cNN prefix guarantees that all column - ** names are unique. - ** - ** The AAAA suffix is not strictly necessary. It is included - ** for the convenience of people who might examine the generated - ** %_content table and wonder what the columns are used for. - */ - pSpec->azContentColumn = sqlite3_malloc( pSpec->nColumn * sizeof(char *) ); - if( pSpec->azContentColumn==0 ){ - clearTableSpec(pSpec); - return SQLITE_NOMEM; - } - for(i=0; inColumn; i++){ - char *p; - pSpec->azContentColumn[i] = sqlite3_mprintf("c%d%s", i, azArg[i]); - for (p = pSpec->azContentColumn[i]; *p ; ++p) { - if( !safe_isalnum(*p) ) *p = '_'; - } - } - - /* - ** Parse the tokenizer specification string. - */ - pSpec->azTokenizer = tokenizeString(zTokenizer, &n); - tokenListToIdList(pSpec->azTokenizer); - - return SQLITE_OK; -} - -/* -** Generate a CREATE TABLE statement that describes the schema of -** the virtual table. Return a pointer to this schema string. -** -** Space is obtained from sqlite3_mprintf() and should be freed -** using sqlite3_free(). -*/ -static char *fulltextSchema( - int nColumn, /* Number of columns */ - const char *const* azColumn, /* List of columns */ - const char *zTableName /* Name of the table */ -){ - int i; - char *zSchema, *zNext; - const char *zSep = "("; - zSchema = sqlite3_mprintf("CREATE TABLE x"); - for(i=0; ibase */ - v->db = db; - v->zDb = spec->zDb; /* Freed when azColumn is freed */ - v->zName = spec->zName; /* Freed when azColumn is freed */ - v->nColumn = spec->nColumn; - v->azContentColumn = spec->azContentColumn; - spec->azContentColumn = 0; - v->azColumn = spec->azColumn; - spec->azColumn = 0; - - if( spec->azTokenizer==0 ){ - return SQLITE_NOMEM; - } - - zTok = spec->azTokenizer[0]; - if( !zTok ){ - zTok = "simple"; - } - nTok = strlen(zTok)+1; - - m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zTok, nTok); - if( !m ){ - *pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]); - rc = SQLITE_ERROR; - goto err; - } - - for(n=0; spec->azTokenizer[n]; n++){} - if( n ){ - rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1], - &v->pTokenizer); - }else{ - rc = m->xCreate(0, 0, &v->pTokenizer); - } - if( rc!=SQLITE_OK ) goto err; - v->pTokenizer->pModule = m; - - /* TODO: verify the existence of backing tables foo_content, foo_term */ - - schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn, - spec->zName); - rc = sqlite3_declare_vtab(db, schema); - sqlite3_free(schema); - if( rc!=SQLITE_OK ) goto err; - - memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements)); - - /* Indicate that the buffer is not live. */ - v->nPendingData = -1; - - *ppVTab = &v->base; - FTSTRACE(("FTS3 Connect %p\n", v)); - - return rc; - -err: - fulltext_vtab_destroy(v); - return rc; -} - -static int fulltextConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, - char **pzErr -){ - TableSpec spec; - int rc = parseSpec(&spec, argc, argv, pzErr); - if( rc!=SQLITE_OK ) return rc; - - rc = constructVtab(db, (fts3Hash *)pAux, &spec, ppVTab, pzErr); - clearTableSpec(&spec); - return rc; -} - -/* The %_content table holds the text of each document, with -** the docid column exposed as the SQLite rowid for the table. -*/ -/* TODO(shess) This comment needs elaboration to match the updated -** code. Work it into the top-of-file comment at that time. -*/ -static int fulltextCreate(sqlite3 *db, void *pAux, - int argc, const char * const *argv, - sqlite3_vtab **ppVTab, char **pzErr){ - int rc; - TableSpec spec; - StringBuffer schema; - FTSTRACE(("FTS3 Create\n")); - - rc = parseSpec(&spec, argc, argv, pzErr); - if( rc!=SQLITE_OK ) return rc; - - initStringBuffer(&schema); - append(&schema, "CREATE TABLE %_content("); - append(&schema, " docid INTEGER PRIMARY KEY,"); - appendList(&schema, spec.nColumn, spec.azContentColumn); - append(&schema, ")"); - rc = sql_exec(db, spec.zDb, spec.zName, stringBufferData(&schema)); - stringBufferDestroy(&schema); - if( rc!=SQLITE_OK ) goto out; - - rc = sql_exec(db, spec.zDb, spec.zName, - "create table %_segments(" - " blockid INTEGER PRIMARY KEY," - " block blob" - ");" - ); - if( rc!=SQLITE_OK ) goto out; - - rc = sql_exec(db, spec.zDb, spec.zName, - "create table %_segdir(" - " level integer," - " idx integer," - " start_block integer," - " leaves_end_block integer," - " end_block integer," - " root blob," - " primary key(level, idx)" - ");"); - if( rc!=SQLITE_OK ) goto out; - - rc = constructVtab(db, (fts3Hash *)pAux, &spec, ppVTab, pzErr); - -out: - clearTableSpec(&spec); - return rc; -} - -/* Decide how to handle an SQL query. */ -static int fulltextBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ - fulltext_vtab *v = (fulltext_vtab *)pVTab; - int i; - FTSTRACE(("FTS3 BestIndex\n")); - - for(i=0; inConstraint; ++i){ - const struct sqlite3_index_constraint *pConstraint; - pConstraint = &pInfo->aConstraint[i]; - if( pConstraint->usable ) { - if( (pConstraint->iColumn==-1 || pConstraint->iColumn==v->nColumn+1) && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - pInfo->idxNum = QUERY_DOCID; /* lookup by docid */ - FTSTRACE(("FTS3 QUERY_DOCID\n")); - } else if( pConstraint->iColumn>=0 && pConstraint->iColumn<=v->nColumn && - pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){ - /* full-text search */ - pInfo->idxNum = QUERY_FULLTEXT + pConstraint->iColumn; - FTSTRACE(("FTS3 QUERY_FULLTEXT %d\n", pConstraint->iColumn)); - } else continue; - - pInfo->aConstraintUsage[i].argvIndex = 1; - pInfo->aConstraintUsage[i].omit = 1; - - /* An arbitrary value for now. - * TODO: Perhaps docid matches should be considered cheaper than - * full-text searches. */ - pInfo->estimatedCost = 1.0; - - return SQLITE_OK; - } - } - pInfo->idxNum = QUERY_GENERIC; - return SQLITE_OK; -} - -static int fulltextDisconnect(sqlite3_vtab *pVTab){ - FTSTRACE(("FTS3 Disconnect %p\n", pVTab)); - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextDestroy(sqlite3_vtab *pVTab){ - fulltext_vtab *v = (fulltext_vtab *)pVTab; - int rc; - - FTSTRACE(("FTS3 Destroy %p\n", pVTab)); - rc = sql_exec(v->db, v->zDb, v->zName, - "drop table if exists %_content;" - "drop table if exists %_segments;" - "drop table if exists %_segdir;" - ); - if( rc!=SQLITE_OK ) return rc; - - fulltext_vtab_destroy((fulltext_vtab *)pVTab); - return SQLITE_OK; -} - -static int fulltextOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ - fulltext_cursor *c; - - c = (fulltext_cursor *) sqlite3_malloc(sizeof(fulltext_cursor)); - if( c ){ - memset(c, 0, sizeof(fulltext_cursor)); - /* sqlite will initialize c->base */ - *ppCursor = &c->base; - FTSTRACE(("FTS3 Open %p: %p\n", pVTab, c)); - return SQLITE_OK; - }else{ - return SQLITE_NOMEM; - } -} - - -/* Free all of the dynamically allocated memory held by *q -*/ -static void queryClear(Query *q){ - int i; - for(i = 0; i < q->nTerms; ++i){ - sqlite3_free(q->pTerms[i].pTerm); - } - sqlite3_free(q->pTerms); - CLEAR(q); -} - -/* Free all of the dynamically allocated memory held by the -** Snippet -*/ -static void snippetClear(Snippet *p){ - sqlite3_free(p->aMatch); - sqlite3_free(p->zOffset); - sqlite3_free(p->zSnippet); - CLEAR(p); -} -/* -** Append a single entry to the p->aMatch[] log. -*/ -static void snippetAppendMatch( - Snippet *p, /* Append the entry to this snippet */ - int iCol, int iTerm, /* The column and query term */ - int iToken, /* Matching token in document */ - int iStart, int nByte /* Offset and size of the match */ -){ - int i; - struct snippetMatch *pMatch; - if( p->nMatch+1>=p->nAlloc ){ - p->nAlloc = p->nAlloc*2 + 10; - p->aMatch = sqlite3_realloc(p->aMatch, p->nAlloc*sizeof(p->aMatch[0]) ); - if( p->aMatch==0 ){ - p->nMatch = 0; - p->nAlloc = 0; - return; - } - } - i = p->nMatch++; - pMatch = &p->aMatch[i]; - pMatch->iCol = iCol; - pMatch->iTerm = iTerm; - pMatch->iToken = iToken; - pMatch->iStart = iStart; - pMatch->nByte = nByte; -} - -/* -** Sizing information for the circular buffer used in snippetOffsetsOfColumn() -*/ -#define FTS3_ROTOR_SZ (32) -#define FTS3_ROTOR_MASK (FTS3_ROTOR_SZ-1) - -/* -** Add entries to pSnippet->aMatch[] for every match that occurs against -** document zDoc[0..nDoc-1] which is stored in column iColumn. -*/ -static void snippetOffsetsOfColumn( - Query *pQuery, - Snippet *pSnippet, - int iColumn, - const char *zDoc, - int nDoc -){ - const sqlite3_tokenizer_module *pTModule; /* The tokenizer module */ - sqlite3_tokenizer *pTokenizer; /* The specific tokenizer */ - sqlite3_tokenizer_cursor *pTCursor; /* Tokenizer cursor */ - fulltext_vtab *pVtab; /* The full text index */ - int nColumn; /* Number of columns in the index */ - const QueryTerm *aTerm; /* Query string terms */ - int nTerm; /* Number of query string terms */ - int i, j; /* Loop counters */ - int rc; /* Return code */ - unsigned int match, prevMatch; /* Phrase search bitmasks */ - const char *zToken; /* Next token from the tokenizer */ - int nToken; /* Size of zToken */ - int iBegin, iEnd, iPos; /* Offsets of beginning and end */ - - /* The following variables keep a circular buffer of the last - ** few tokens */ - unsigned int iRotor = 0; /* Index of current token */ - int iRotorBegin[FTS3_ROTOR_SZ]; /* Beginning offset of token */ - int iRotorLen[FTS3_ROTOR_SZ]; /* Length of token */ - - pVtab = pQuery->pFts; - nColumn = pVtab->nColumn; - pTokenizer = pVtab->pTokenizer; - pTModule = pTokenizer->pModule; - rc = pTModule->xOpen(pTokenizer, zDoc, nDoc, &pTCursor); - if( rc ) return; - pTCursor->pTokenizer = pTokenizer; - aTerm = pQuery->pTerms; - nTerm = pQuery->nTerms; - if( nTerm>=FTS3_ROTOR_SZ ){ - nTerm = FTS3_ROTOR_SZ - 1; - } - prevMatch = 0; - while(1){ - rc = pTModule->xNext(pTCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos); - if( rc ) break; - iRotorBegin[iRotor&FTS3_ROTOR_MASK] = iBegin; - iRotorLen[iRotor&FTS3_ROTOR_MASK] = iEnd-iBegin; - match = 0; - for(i=0; i=0 && iColnToken ) continue; - if( !aTerm[i].isPrefix && aTerm[i].nTerm1 && (prevMatch & (1<=0; j--){ - int k = (iRotor-j) & FTS3_ROTOR_MASK; - snippetAppendMatch(pSnippet, iColumn, i-j, iPos-j, - iRotorBegin[k], iRotorLen[k]); - } - } - } - prevMatch = match<<1; - iRotor++; - } - pTModule->xClose(pTCursor); -} - -/* -** Remove entries from the pSnippet structure to account for the NEAR -** operator. When this is called, pSnippet contains the list of token -** offsets produced by treating all NEAR operators as AND operators. -** This function removes any entries that should not be present after -** accounting for the NEAR restriction. For example, if the queried -** document is: -** -** "A B C D E A" -** -** and the query is: -** -** A NEAR/0 E -** -** then when this function is called the Snippet contains token offsets -** 0, 4 and 5. This function removes the "0" entry (because the first A -** is not near enough to an E). -*/ -static void trimSnippetOffsetsForNear(Query *pQuery, Snippet *pSnippet){ - int ii; - int iDir = 1; - - while(iDir>-2) { - assert( iDir==1 || iDir==-1 ); - for(ii=0; iinMatch; ii++){ - int jj; - int nNear; - struct snippetMatch *pMatch = &pSnippet->aMatch[ii]; - QueryTerm *pQueryTerm = &pQuery->pTerms[pMatch->iTerm]; - - if( (pMatch->iTerm+iDir)<0 - || (pMatch->iTerm+iDir)>=pQuery->nTerms - ){ - continue; - } - - nNear = pQueryTerm->nNear; - if( iDir<0 ){ - nNear = pQueryTerm[-1].nNear; - } - - if( pMatch->iTerm>=0 && nNear ){ - int isOk = 0; - int iNextTerm = pMatch->iTerm+iDir; - int iPrevTerm = iNextTerm; - - int iEndToken; - int iStartToken; - - if( iDir<0 ){ - int nPhrase = 1; - iStartToken = pMatch->iToken; - while( (pMatch->iTerm+nPhrase)nTerms - && pQuery->pTerms[pMatch->iTerm+nPhrase].iPhrase>1 - ){ - nPhrase++; - } - iEndToken = iStartToken + nPhrase - 1; - }else{ - iEndToken = pMatch->iToken; - iStartToken = pMatch->iToken+1-pQueryTerm->iPhrase; - } - - while( pQuery->pTerms[iNextTerm].iPhrase>1 ){ - iNextTerm--; - } - while( (iPrevTerm+1)nTerms && - pQuery->pTerms[iPrevTerm+1].iPhrase>1 - ){ - iPrevTerm++; - } - - for(jj=0; isOk==0 && jjnMatch; jj++){ - struct snippetMatch *p = &pSnippet->aMatch[jj]; - if( p->iCol==pMatch->iCol && (( - p->iTerm==iNextTerm && - p->iToken>iEndToken && - p->iToken<=iEndToken+nNear - ) || ( - p->iTerm==iPrevTerm && - p->iTokeniToken>=iStartToken-nNear - ))){ - isOk = 1; - } - } - if( !isOk ){ - for(jj=1-pQueryTerm->iPhrase; jj<=0; jj++){ - pMatch[jj].iTerm = -1; - } - ii = -1; - iDir = 1; - } - } - } - iDir -= 2; - } -} - -/* -** Compute all offsets for the current row of the query. -** If the offsets have already been computed, this routine is a no-op. -*/ -static void snippetAllOffsets(fulltext_cursor *p){ - int nColumn; - int iColumn, i; - int iFirst, iLast; - fulltext_vtab *pFts; - - if( p->snippet.nMatch ) return; - if( p->q.nTerms==0 ) return; - pFts = p->q.pFts; - nColumn = pFts->nColumn; - iColumn = (p->iCursorType - QUERY_FULLTEXT); - if( iColumn<0 || iColumn>=nColumn ){ - iFirst = 0; - iLast = nColumn-1; - }else{ - iFirst = iColumn; - iLast = iColumn; - } - for(i=iFirst; i<=iLast; i++){ - const char *zDoc; - int nDoc; - zDoc = (const char*)sqlite3_column_text(p->pStmt, i+1); - nDoc = sqlite3_column_bytes(p->pStmt, i+1); - snippetOffsetsOfColumn(&p->q, &p->snippet, i, zDoc, nDoc); - } - - trimSnippetOffsetsForNear(&p->q, &p->snippet); -} - -/* -** Convert the information in the aMatch[] array of the snippet -** into the string zOffset[0..nOffset-1]. -*/ -static void snippetOffsetText(Snippet *p){ - int i; - int cnt = 0; - StringBuffer sb; - char zBuf[200]; - if( p->zOffset ) return; - initStringBuffer(&sb); - for(i=0; inMatch; i++){ - struct snippetMatch *pMatch = &p->aMatch[i]; - if( pMatch->iTerm>=0 ){ - /* If snippetMatch.iTerm is less than 0, then the match was - ** discarded as part of processing the NEAR operator (see the - ** trimSnippetOffsetsForNear() function for details). Ignore - ** it in this case - */ - zBuf[0] = ' '; - sprintf(&zBuf[cnt>0], "%d %d %d %d", pMatch->iCol, - pMatch->iTerm, pMatch->iStart, pMatch->nByte); - append(&sb, zBuf); - cnt++; - } - } - p->zOffset = stringBufferData(&sb); - p->nOffset = stringBufferLength(&sb); -} - -/* -** zDoc[0..nDoc-1] is phrase of text. aMatch[0..nMatch-1] are a set -** of matching words some of which might be in zDoc. zDoc is column -** number iCol. -** -** iBreak is suggested spot in zDoc where we could begin or end an -** excerpt. Return a value similar to iBreak but possibly adjusted -** to be a little left or right so that the break point is better. -*/ -static int wordBoundary( - int iBreak, /* The suggested break point */ - const char *zDoc, /* Document text */ - int nDoc, /* Number of bytes in zDoc[] */ - struct snippetMatch *aMatch, /* Matching words */ - int nMatch, /* Number of entries in aMatch[] */ - int iCol /* The column number for zDoc[] */ -){ - int i; - if( iBreak<=10 ){ - return 0; - } - if( iBreak>=nDoc-10 ){ - return nDoc; - } - for(i=0; i0 && aMatch[i-1].iStart+aMatch[i-1].nByte>=iBreak ){ - return aMatch[i-1].iStart; - } - } - for(i=1; i<=10; i++){ - if( safe_isspace(zDoc[iBreak-i]) ){ - return iBreak - i + 1; - } - if( safe_isspace(zDoc[iBreak+i]) ){ - return iBreak + i + 1; - } - } - return iBreak; -} - - - -/* -** Allowed values for Snippet.aMatch[].snStatus -*/ -#define SNIPPET_IGNORE 0 /* It is ok to omit this match from the snippet */ -#define SNIPPET_DESIRED 1 /* We want to include this match in the snippet */ - -/* -** Generate the text of a snippet. -*/ -static void snippetText( - fulltext_cursor *pCursor, /* The cursor we need the snippet for */ - const char *zStartMark, /* Markup to appear before each match */ - const char *zEndMark, /* Markup to appear after each match */ - const char *zEllipsis /* Ellipsis mark */ -){ - int i, j; - struct snippetMatch *aMatch; - int nMatch; - int nDesired; - StringBuffer sb; - int tailCol; - int tailOffset; - int iCol; - int nDoc; - const char *zDoc; - int iStart, iEnd; - int tailEllipsis = 0; - int iMatch; - - - sqlite3_free(pCursor->snippet.zSnippet); - pCursor->snippet.zSnippet = 0; - aMatch = pCursor->snippet.aMatch; - nMatch = pCursor->snippet.nMatch; - initStringBuffer(&sb); - - for(i=0; iq.nTerms; i++){ - for(j=0; j0; i++){ - if( aMatch[i].snStatus!=SNIPPET_DESIRED ) continue; - nDesired--; - iCol = aMatch[i].iCol; - zDoc = (const char*)sqlite3_column_text(pCursor->pStmt, iCol+1); - nDoc = sqlite3_column_bytes(pCursor->pStmt, iCol+1); - iStart = aMatch[i].iStart - 40; - iStart = wordBoundary(iStart, zDoc, nDoc, aMatch, nMatch, iCol); - if( iStart<=10 ){ - iStart = 0; - } - if( iCol==tailCol && iStart<=tailOffset+20 ){ - iStart = tailOffset; - } - if( (iCol!=tailCol && tailCol>=0) || iStart!=tailOffset ){ - trimWhiteSpace(&sb); - appendWhiteSpace(&sb); - append(&sb, zEllipsis); - appendWhiteSpace(&sb); - } - iEnd = aMatch[i].iStart + aMatch[i].nByte + 40; - iEnd = wordBoundary(iEnd, zDoc, nDoc, aMatch, nMatch, iCol); - if( iEnd>=nDoc-10 ){ - iEnd = nDoc; - tailEllipsis = 0; - }else{ - tailEllipsis = 1; - } - while( iMatchsnippet.zSnippet = stringBufferData(&sb); - pCursor->snippet.nSnippet = stringBufferLength(&sb); -} - - -/* -** Close the cursor. For additional information see the documentation -** on the xClose method of the virtual table interface. -*/ -static int fulltextClose(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - FTSTRACE(("FTS3 Close %p\n", c)); - sqlite3_finalize(c->pStmt); - queryClear(&c->q); - snippetClear(&c->snippet); - if( c->result.nData!=0 ) dlrDestroy(&c->reader); - dataBufferDestroy(&c->result); - sqlite3_free(c); - return SQLITE_OK; -} - -static int fulltextNext(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - int rc; - - FTSTRACE(("FTS3 Next %p\n", pCursor)); - snippetClear(&c->snippet); - if( c->iCursorType < QUERY_FULLTEXT ){ - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - switch( rc ){ - case SQLITE_ROW: - c->eof = 0; - return SQLITE_OK; - case SQLITE_DONE: - c->eof = 1; - return SQLITE_OK; - default: - c->eof = 1; - return rc; - } - } else { /* full-text query */ - rc = sqlite3_reset(c->pStmt); - if( rc!=SQLITE_OK ) return rc; - - if( c->result.nData==0 || dlrAtEnd(&c->reader) ){ - c->eof = 1; - return SQLITE_OK; - } - rc = sqlite3_bind_int64(c->pStmt, 1, dlrDocid(&c->reader)); - dlrStep(&c->reader); - if( rc!=SQLITE_OK ) return rc; - /* TODO(shess) Handle SQLITE_SCHEMA AND SQLITE_BUSY. */ - rc = sqlite3_step(c->pStmt); - if( rc==SQLITE_ROW ){ /* the case we expect */ - c->eof = 0; - return SQLITE_OK; - } - /* an error occurred; abort */ - return rc==SQLITE_DONE ? SQLITE_ERROR : rc; - } -} - - -/* TODO(shess) If we pushed LeafReader to the top of the file, or to -** another file, term_select() could be pushed above -** docListOfTerm(). -*/ -static int termSelect(fulltext_vtab *v, int iColumn, - const char *pTerm, int nTerm, int isPrefix, - DocListType iType, DataBuffer *out); - -/* Return a DocList corresponding to the query term *pTerm. If *pTerm -** is the first term of a phrase query, go ahead and evaluate the phrase -** query and return the doclist for the entire phrase query. -** -** The resulting DL_DOCIDS doclist is stored in pResult, which is -** overwritten. -*/ -static int docListOfTerm( - fulltext_vtab *v, /* The full text index */ - int iColumn, /* column to restrict to. No restriction if >=nColumn */ - QueryTerm *pQTerm, /* Term we are looking for, or 1st term of a phrase */ - DataBuffer *pResult /* Write the result here */ -){ - DataBuffer left, right, new; - int i, rc; - - /* No phrase search if no position info. */ - assert( pQTerm->nPhrase==0 || DL_DEFAULT!=DL_DOCIDS ); - - /* This code should never be called with buffered updates. */ - assert( v->nPendingData<0 ); - - dataBufferInit(&left, 0); - rc = termSelect(v, iColumn, pQTerm->pTerm, pQTerm->nTerm, pQTerm->isPrefix, - (0nPhrase ? DL_POSITIONS : DL_DOCIDS), &left); - if( rc ) return rc; - for(i=1; i<=pQTerm->nPhrase && left.nData>0; i++){ - /* If this token is connected to the next by a NEAR operator, and - ** the next token is the start of a phrase, then set nPhraseRight - ** to the number of tokens in the phrase. Otherwise leave it at 1. - */ - int nPhraseRight = 1; - while( (i+nPhraseRight)<=pQTerm->nPhrase - && pQTerm[i+nPhraseRight].nNear==0 - ){ - nPhraseRight++; - } - - dataBufferInit(&right, 0); - rc = termSelect(v, iColumn, pQTerm[i].pTerm, pQTerm[i].nTerm, - pQTerm[i].isPrefix, DL_POSITIONS, &right); - if( rc ){ - dataBufferDestroy(&left); - return rc; - } - dataBufferInit(&new, 0); - docListPhraseMerge(left.pData, left.nData, right.pData, right.nData, - pQTerm[i-1].nNear, pQTerm[i-1].iPhrase + nPhraseRight, - ((inPhrase) ? DL_POSITIONS : DL_DOCIDS), - &new); - dataBufferDestroy(&left); - dataBufferDestroy(&right); - left = new; - } - *pResult = left; - return SQLITE_OK; -} - -/* Add a new term pTerm[0..nTerm-1] to the query *q. -*/ -static void queryAdd(Query *q, const char *pTerm, int nTerm){ - QueryTerm *t; - ++q->nTerms; - q->pTerms = sqlite3_realloc(q->pTerms, q->nTerms * sizeof(q->pTerms[0])); - if( q->pTerms==0 ){ - q->nTerms = 0; - return; - } - t = &q->pTerms[q->nTerms - 1]; - CLEAR(t); - t->pTerm = sqlite3_malloc(nTerm+1); - memcpy(t->pTerm, pTerm, nTerm); - t->pTerm[nTerm] = 0; - t->nTerm = nTerm; - t->isOr = q->nextIsOr; - t->isPrefix = 0; - q->nextIsOr = 0; - t->iColumn = q->nextColumn; - q->nextColumn = q->dfltColumn; -} - -/* -** Check to see if the string zToken[0...nToken-1] matches any -** column name in the virtual table. If it does, -** return the zero-indexed column number. If not, return -1. -*/ -static int checkColumnSpecifier( - fulltext_vtab *pVtab, /* The virtual table */ - const char *zToken, /* Text of the token */ - int nToken /* Number of characters in the token */ -){ - int i; - for(i=0; inColumn; i++){ - if( memcmp(pVtab->azColumn[i], zToken, nToken)==0 - && pVtab->azColumn[i][nToken]==0 ){ - return i; - } - } - return -1; -} - -/* -** Parse the text at pSegment[0..nSegment-1]. Add additional terms -** to the query being assemblied in pQuery. -** -** inPhrase is true if pSegment[0..nSegement-1] is contained within -** double-quotes. If inPhrase is true, then the first term -** is marked with the number of terms in the phrase less one and -** OR and "-" syntax is ignored. If inPhrase is false, then every -** term found is marked with nPhrase=0 and OR and "-" syntax is significant. -*/ -static int tokenizeSegment( - sqlite3_tokenizer *pTokenizer, /* The tokenizer to use */ - const char *pSegment, int nSegment, /* Query expression being parsed */ - int inPhrase, /* True if within "..." */ - Query *pQuery /* Append results here */ -){ - const sqlite3_tokenizer_module *pModule = pTokenizer->pModule; - sqlite3_tokenizer_cursor *pCursor; - int firstIndex = pQuery->nTerms; - int iCol; - int nTerm = 1; - - int rc = pModule->xOpen(pTokenizer, pSegment, nSegment, &pCursor); - if( rc!=SQLITE_OK ) return rc; - pCursor->pTokenizer = pTokenizer; - - while( 1 ){ - const char *pToken; - int nToken, iBegin, iEnd, iPos; - - rc = pModule->xNext(pCursor, - &pToken, &nToken, - &iBegin, &iEnd, &iPos); - if( rc!=SQLITE_OK ) break; - if( !inPhrase && - pSegment[iEnd]==':' && - (iCol = checkColumnSpecifier(pQuery->pFts, pToken, nToken))>=0 ){ - pQuery->nextColumn = iCol; - continue; - } - if( !inPhrase && pQuery->nTerms>0 && nToken==2 - && pSegment[iBegin+0]=='O' - && pSegment[iBegin+1]=='R' - ){ - pQuery->nextIsOr = 1; - continue; - } - if( !inPhrase && pQuery->nTerms>0 && !pQuery->nextIsOr && nToken==4 - && pSegment[iBegin+0]=='N' - && pSegment[iBegin+1]=='E' - && pSegment[iBegin+2]=='A' - && pSegment[iBegin+3]=='R' - ){ - QueryTerm *pTerm = &pQuery->pTerms[pQuery->nTerms-1]; - if( (iBegin+6)='0' && pSegment[iBegin+5]<='9' - ){ - pTerm->nNear = (pSegment[iBegin+5] - '0'); - nToken += 2; - if( pSegment[iBegin+6]>='0' && pSegment[iBegin+6]<=9 ){ - pTerm->nNear = pTerm->nNear * 10 + (pSegment[iBegin+6] - '0'); - iEnd++; - } - pModule->xNext(pCursor, &pToken, &nToken, &iBegin, &iEnd, &iPos); - } else { - pTerm->nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM; - } - pTerm->nNear++; - continue; - } - - queryAdd(pQuery, pToken, nToken); - if( !inPhrase && iBegin>0 && pSegment[iBegin-1]=='-' ){ - pQuery->pTerms[pQuery->nTerms-1].isNot = 1; - } - if( iEndpTerms[pQuery->nTerms-1].isPrefix = 1; - } - pQuery->pTerms[pQuery->nTerms-1].iPhrase = nTerm; - if( inPhrase ){ - nTerm++; - } - } - - if( inPhrase && pQuery->nTerms>firstIndex ){ - pQuery->pTerms[firstIndex].nPhrase = pQuery->nTerms - firstIndex - 1; - } - - return pModule->xClose(pCursor); -} - -/* Parse a query string, yielding a Query object pQuery. -** -** The calling function will need to queryClear() to clean up -** the dynamically allocated memory held by pQuery. -*/ -static int parseQuery( - fulltext_vtab *v, /* The fulltext index */ - const char *zInput, /* Input text of the query string */ - int nInput, /* Size of the input text */ - int dfltColumn, /* Default column of the index to match against */ - Query *pQuery /* Write the parse results here. */ -){ - int iInput, inPhrase = 0; - int ii; - QueryTerm *aTerm; - - if( zInput==0 ) nInput = 0; - if( nInput<0 ) nInput = strlen(zInput); - pQuery->nTerms = 0; - pQuery->pTerms = NULL; - pQuery->nextIsOr = 0; - pQuery->nextColumn = dfltColumn; - pQuery->dfltColumn = dfltColumn; - pQuery->pFts = v; - - for(iInput=0; iInputiInput ){ - tokenizeSegment(v->pTokenizer, zInput+iInput, i-iInput, inPhrase, - pQuery); - } - iInput = i; - if( ipTerms; - for(ii=0; iinTerms; ii++){ - if( aTerm[ii].nNear || aTerm[ii].nPhrase ){ - while (aTerm[ii+aTerm[ii].nPhrase].nNear) { - aTerm[ii].nPhrase += (1 + aTerm[ii+aTerm[ii].nPhrase+1].nPhrase); - } - } - } - - return SQLITE_OK; -} - -/* TODO(shess) Refactor the code to remove this forward decl. */ -static int flushPendingTerms(fulltext_vtab *v); - -/* Perform a full-text query using the search expression in -** zInput[0..nInput-1]. Return a list of matching documents -** in pResult. -** -** Queries must match column iColumn. Or if iColumn>=nColumn -** they are allowed to match against any column. -*/ -static int fulltextQuery( - fulltext_vtab *v, /* The full text index */ - int iColumn, /* Match against this column by default */ - const char *zInput, /* The query string */ - int nInput, /* Number of bytes in zInput[] */ - DataBuffer *pResult, /* Write the result doclist here */ - Query *pQuery /* Put parsed query string here */ -){ - int i, iNext, rc; - DataBuffer left, right, or, new; - int nNot = 0; - QueryTerm *aTerm; - - /* TODO(shess) Instead of flushing pendingTerms, we could query for - ** the relevant term and merge the doclist into what we receive from - ** the database. Wait and see if this is a common issue, first. - ** - ** A good reason not to flush is to not generate update-related - ** error codes from here. - */ - - /* Flush any buffered updates before executing the query. */ - rc = flushPendingTerms(v); - if( rc!=SQLITE_OK ) return rc; - - /* TODO(shess) I think that the queryClear() calls below are not - ** necessary, because fulltextClose() already clears the query. - */ - rc = parseQuery(v, zInput, nInput, iColumn, pQuery); - if( rc!=SQLITE_OK ) return rc; - - /* Empty or NULL queries return no results. */ - if( pQuery->nTerms==0 ){ - dataBufferInit(pResult, 0); - return SQLITE_OK; - } - - /* Merge AND terms. */ - /* TODO(shess) I think we can early-exit if( i>nNot && left.nData==0 ). */ - aTerm = pQuery->pTerms; - for(i = 0; inTerms; i=iNext){ - if( aTerm[i].isNot ){ - /* Handle all NOT terms in a separate pass */ - nNot++; - iNext = i + aTerm[i].nPhrase+1; - continue; - } - iNext = i + aTerm[i].nPhrase + 1; - rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &right); - if( rc ){ - if( i!=nNot ) dataBufferDestroy(&left); - queryClear(pQuery); - return rc; - } - while( iNextnTerms && aTerm[iNext].isOr ){ - rc = docListOfTerm(v, aTerm[iNext].iColumn, &aTerm[iNext], &or); - iNext += aTerm[iNext].nPhrase + 1; - if( rc ){ - if( i!=nNot ) dataBufferDestroy(&left); - dataBufferDestroy(&right); - queryClear(pQuery); - return rc; - } - dataBufferInit(&new, 0); - docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new); - dataBufferDestroy(&right); - dataBufferDestroy(&or); - right = new; - } - if( i==nNot ){ /* first term processed. */ - left = right; - }else{ - dataBufferInit(&new, 0); - docListAndMerge(left.pData, left.nData, right.pData, right.nData, &new); - dataBufferDestroy(&right); - dataBufferDestroy(&left); - left = new; - } - } - - if( nNot==pQuery->nTerms ){ - /* We do not yet know how to handle a query of only NOT terms */ - return SQLITE_ERROR; - } - - /* Do the EXCEPT terms */ - for(i=0; inTerms; i += aTerm[i].nPhrase + 1){ - if( !aTerm[i].isNot ) continue; - rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &right); - if( rc ){ - queryClear(pQuery); - dataBufferDestroy(&left); - return rc; - } - dataBufferInit(&new, 0); - docListExceptMerge(left.pData, left.nData, right.pData, right.nData, &new); - dataBufferDestroy(&right); - dataBufferDestroy(&left); - left = new; - } - - *pResult = left; - return rc; -} - -/* -** This is the xFilter interface for the virtual table. See -** the virtual table xFilter method documentation for additional -** information. -** -** If idxNum==QUERY_GENERIC then do a full table scan against -** the %_content table. -** -** If idxNum==QUERY_DOCID then do a docid lookup for a single entry -** in the %_content table. -** -** If idxNum>=QUERY_FULLTEXT then use the full text index. The -** column on the left-hand side of the MATCH operator is column -** number idxNum-QUERY_FULLTEXT, 0 indexed. argv[0] is the right-hand -** side of the MATCH operator. -*/ -/* TODO(shess) Upgrade the cursor initialization and destruction to -** account for fulltextFilter() being called multiple times on the -** same cursor. The current solution is very fragile. Apply fix to -** fts3 as appropriate. -*/ -static int fulltextFilter( - sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ - int idxNum, const char *idxStr, /* Which indexing scheme to use */ - int argc, sqlite3_value **argv /* Arguments for the indexing scheme */ -){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - fulltext_vtab *v = cursor_vtab(c); - int rc; - StringBuffer sb; - - FTSTRACE(("FTS3 Filter %p\n",pCursor)); - - initStringBuffer(&sb); - append(&sb, "SELECT docid, "); - appendList(&sb, v->nColumn, v->azContentColumn); - append(&sb, " FROM %_content"); - if( idxNum!=QUERY_GENERIC ) append(&sb, " WHERE docid = ?"); - sqlite3_finalize(c->pStmt); - rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, stringBufferData(&sb)); - stringBufferDestroy(&sb); - if( rc!=SQLITE_OK ) return rc; - - c->iCursorType = idxNum; - switch( idxNum ){ - case QUERY_GENERIC: - break; - - case QUERY_DOCID: - rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0])); - if( rc!=SQLITE_OK ) return rc; - break; - - default: /* full-text search */ - { - const char *zQuery = (const char *)sqlite3_value_text(argv[0]); - assert( idxNum<=QUERY_FULLTEXT+v->nColumn); - assert( argc==1 ); - queryClear(&c->q); - if( c->result.nData!=0 ){ - /* This case happens if the same cursor is used repeatedly. */ - dlrDestroy(&c->reader); - dataBufferReset(&c->result); - }else{ - dataBufferInit(&c->result, 0); - } - rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &c->result, &c->q); - if( rc!=SQLITE_OK ) return rc; - if( c->result.nData!=0 ){ - dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData); - } - break; - } - } - - return fulltextNext(pCursor); -} - -/* This is the xEof method of the virtual table. The SQLite core -** calls this routine to find out if it has reached the end of -** a query's results set. -*/ -static int fulltextEof(sqlite3_vtab_cursor *pCursor){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - return c->eof; -} - -/* This is the xColumn method of the virtual table. The SQLite -** core calls this method during a query when it needs the value -** of a column from the virtual table. This method needs to use -** one of the sqlite3_result_*() routines to store the requested -** value back in the pContext. -*/ -static int fulltextColumn(sqlite3_vtab_cursor *pCursor, - sqlite3_context *pContext, int idxCol){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - fulltext_vtab *v = cursor_vtab(c); - - if( idxColnColumn ){ - sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1); - sqlite3_result_value(pContext, pVal); - }else if( idxCol==v->nColumn ){ - /* The extra column whose name is the same as the table. - ** Return a blob which is a pointer to the cursor - */ - sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT); - }else if( idxCol==v->nColumn+1 ){ - /* The docid column, which is an alias for rowid. */ - sqlite3_value *pVal = sqlite3_column_value(c->pStmt, 0); - sqlite3_result_value(pContext, pVal); - } - return SQLITE_OK; -} - -/* This is the xRowid method. The SQLite core calls this routine to -** retrieve the rowid for the current row of the result set. fts3 -** exposes %_content.docid as the rowid for the virtual table. The -** rowid should be written to *pRowid. -*/ -static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ - fulltext_cursor *c = (fulltext_cursor *) pCursor; - - *pRowid = sqlite3_column_int64(c->pStmt, 0); - return SQLITE_OK; -} - -/* Add all terms in [zText] to pendingTerms table. If [iColumn] > 0, -** we also store positions and offsets in the hash table using that -** column number. -*/ -static int buildTerms(fulltext_vtab *v, sqlite_int64 iDocid, - const char *zText, int iColumn){ - sqlite3_tokenizer *pTokenizer = v->pTokenizer; - sqlite3_tokenizer_cursor *pCursor; - const char *pToken; - int nTokenBytes; - int iStartOffset, iEndOffset, iPosition; - int rc; - - rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor); - if( rc!=SQLITE_OK ) return rc; - - pCursor->pTokenizer = pTokenizer; - while( SQLITE_OK==(rc=pTokenizer->pModule->xNext(pCursor, - &pToken, &nTokenBytes, - &iStartOffset, &iEndOffset, - &iPosition)) ){ - DLCollector *p; - int nData; /* Size of doclist before our update. */ - - /* Positions can't be negative; we use -1 as a terminator - * internally. Token can't be NULL or empty. */ - if( iPosition<0 || pToken == NULL || nTokenBytes == 0 ){ - rc = SQLITE_ERROR; - break; - } - - p = fts3HashFind(&v->pendingTerms, pToken, nTokenBytes); - if( p==NULL ){ - nData = 0; - p = dlcNew(iDocid, DL_DEFAULT); - fts3HashInsert(&v->pendingTerms, pToken, nTokenBytes, p); - - /* Overhead for our hash table entry, the key, and the value. */ - v->nPendingData += sizeof(struct fts3HashElem)+sizeof(*p)+nTokenBytes; - }else{ - nData = p->b.nData; - if( p->dlw.iPrevDocid!=iDocid ) dlcNext(p, iDocid); - } - if( iColumn>=0 ){ - dlcAddPos(p, iColumn, iPosition, iStartOffset, iEndOffset); - } - - /* Accumulate data added by dlcNew or dlcNext, and dlcAddPos. */ - v->nPendingData += p->b.nData-nData; - } - - /* TODO(shess) Check return? Should this be able to cause errors at - ** this point? Actually, same question about sqlite3_finalize(), - ** though one could argue that failure there means that the data is - ** not durable. *ponder* - */ - pTokenizer->pModule->xClose(pCursor); - if( SQLITE_DONE == rc ) return SQLITE_OK; - return rc; -} - -/* Add doclists for all terms in [pValues] to pendingTerms table. */ -static int insertTerms(fulltext_vtab *v, sqlite_int64 iDocid, - sqlite3_value **pValues){ - int i; - for(i = 0; i < v->nColumn ; ++i){ - char *zText = (char*)sqlite3_value_text(pValues[i]); - int rc = buildTerms(v, iDocid, zText, i); - if( rc!=SQLITE_OK ) return rc; - } - return SQLITE_OK; -} - -/* Add empty doclists for all terms in the given row's content to -** pendingTerms. -*/ -static int deleteTerms(fulltext_vtab *v, sqlite_int64 iDocid){ - const char **pValues; - int i, rc; - - /* TODO(shess) Should we allow such tables at all? */ - if( DL_DEFAULT==DL_DOCIDS ) return SQLITE_ERROR; - - rc = content_select(v, iDocid, &pValues); - if( rc!=SQLITE_OK ) return rc; - - for(i = 0 ; i < v->nColumn; ++i) { - rc = buildTerms(v, iDocid, pValues[i], -1); - if( rc!=SQLITE_OK ) break; - } - - freeStringArray(v->nColumn, pValues); - return SQLITE_OK; -} - -/* TODO(shess) Refactor the code to remove this forward decl. */ -static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid); - -/* Insert a row into the %_content table; set *piDocid to be the ID of the -** new row. Add doclists for terms to pendingTerms. -*/ -static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestDocid, - sqlite3_value **pValues, sqlite_int64 *piDocid){ - int rc; - - rc = content_insert(v, pRequestDocid, pValues); /* execute an SQL INSERT */ - if( rc!=SQLITE_OK ) return rc; - - /* docid column is an alias for rowid. */ - *piDocid = sqlite3_last_insert_rowid(v->db); - rc = initPendingTerms(v, *piDocid); - if( rc!=SQLITE_OK ) return rc; - - return insertTerms(v, *piDocid, pValues); -} - -/* Delete a row from the %_content table; add empty doclists for terms -** to pendingTerms. -*/ -static int index_delete(fulltext_vtab *v, sqlite_int64 iRow){ - int rc = initPendingTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = deleteTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - return content_delete(v, iRow); /* execute an SQL DELETE */ -} - -/* Update a row in the %_content table; add delete doclists to -** pendingTerms for old terms not in the new data, add insert doclists -** to pendingTerms for terms in the new data. -*/ -static int index_update(fulltext_vtab *v, sqlite_int64 iRow, - sqlite3_value **pValues){ - int rc = initPendingTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - /* Generate an empty doclist for each term that previously appeared in this - * row. */ - rc = deleteTerms(v, iRow); - if( rc!=SQLITE_OK ) return rc; - - rc = content_update(v, pValues, iRow); /* execute an SQL UPDATE */ - if( rc!=SQLITE_OK ) return rc; - - /* Now add positions for terms which appear in the updated row. */ - return insertTerms(v, iRow, pValues); -} - -/*******************************************************************/ -/* InteriorWriter is used to collect terms and block references into -** interior nodes in %_segments. See commentary at top of file for -** format. -*/ - -/* How large interior nodes can grow. */ -#define INTERIOR_MAX 2048 - -/* Minimum number of terms per interior node (except the root). This -** prevents large terms from making the tree too skinny - must be >0 -** so that the tree always makes progress. Note that the min tree -** fanout will be INTERIOR_MIN_TERMS+1. -*/ -#define INTERIOR_MIN_TERMS 7 -#if INTERIOR_MIN_TERMS<1 -# error INTERIOR_MIN_TERMS must be greater than 0. -#endif - -/* ROOT_MAX controls how much data is stored inline in the segment -** directory. -*/ -/* TODO(shess) Push ROOT_MAX down to whoever is writing things. It's -** only here so that interiorWriterRootInfo() and leafWriterRootInfo() -** can both see it, but if the caller passed it in, we wouldn't even -** need a define. -*/ -#define ROOT_MAX 1024 -#if ROOT_MAXterm, 0); - dataBufferReplace(&block->term, pTerm, nTerm); - - n = fts3PutVarint(c, iHeight); - n += fts3PutVarint(c+n, iChildBlock); - dataBufferInit(&block->data, INTERIOR_MAX); - dataBufferReplace(&block->data, c, n); - } - return block; -} - -#ifndef NDEBUG -/* Verify that the data is readable as an interior node. */ -static void interiorBlockValidate(InteriorBlock *pBlock){ - const char *pData = pBlock->data.pData; - int nData = pBlock->data.nData; - int n, iDummy; - sqlite_int64 iBlockid; - - assert( nData>0 ); - assert( pData!=0 ); - assert( pData+nData>pData ); - - /* Must lead with height of node as a varint(n), n>0 */ - n = fts3GetVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>0 ); - assert( n0 ); - assert( n<=nData ); - pData += n; - nData -= n; - - /* Zero or more terms of positive length */ - if( nData!=0 ){ - /* First term is not delta-encoded. */ - n = fts3GetVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>0 ); - assert( n+iDummy>0); - assert( n+iDummy<=nData ); - pData += n+iDummy; - nData -= n+iDummy; - - /* Following terms delta-encoded. */ - while( nData!=0 ){ - /* Length of shared prefix. */ - n = fts3GetVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>=0 ); - assert( n0 ); - assert( iDummy>0 ); - assert( n+iDummy>0); - assert( n+iDummy<=nData ); - pData += n+iDummy; - nData -= n+iDummy; - } - } -} -#define ASSERT_VALID_INTERIOR_BLOCK(x) interiorBlockValidate(x) -#else -#define ASSERT_VALID_INTERIOR_BLOCK(x) assert( 1 ) -#endif - -typedef struct InteriorWriter { - int iHeight; /* from 0 at leaves. */ - InteriorBlock *first, *last; - struct InteriorWriter *parentWriter; - - DataBuffer term; /* Last term written to block "last". */ - sqlite_int64 iOpeningChildBlock; /* First child block in block "last". */ -#ifndef NDEBUG - sqlite_int64 iLastChildBlock; /* for consistency checks. */ -#endif -} InteriorWriter; - -/* Initialize an interior node where pTerm[nTerm] marks the leftmost -** term in the tree. iChildBlock is the leftmost child block at the -** next level down the tree. -*/ -static void interiorWriterInit(int iHeight, const char *pTerm, int nTerm, - sqlite_int64 iChildBlock, - InteriorWriter *pWriter){ - InteriorBlock *block; - assert( iHeight>0 ); - CLEAR(pWriter); - - pWriter->iHeight = iHeight; - pWriter->iOpeningChildBlock = iChildBlock; -#ifndef NDEBUG - pWriter->iLastChildBlock = iChildBlock; -#endif - block = interiorBlockNew(iHeight, iChildBlock, pTerm, nTerm); - pWriter->last = pWriter->first = block; - ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); - dataBufferInit(&pWriter->term, 0); -} - -/* Append the child node rooted at iChildBlock to the interior node, -** with pTerm[nTerm] as the leftmost term in iChildBlock's subtree. -*/ -static void interiorWriterAppend(InteriorWriter *pWriter, - const char *pTerm, int nTerm, - sqlite_int64 iChildBlock){ - char c[VARINT_MAX+VARINT_MAX]; - int n, nPrefix = 0; - - ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); - - /* The first term written into an interior node is actually - ** associated with the second child added (the first child was added - ** in interiorWriterInit, or in the if clause at the bottom of this - ** function). That term gets encoded straight up, with nPrefix left - ** at 0. - */ - if( pWriter->term.nData==0 ){ - n = fts3PutVarint(c, nTerm); - }else{ - while( nPrefixterm.nData && - pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){ - nPrefix++; - } - - n = fts3PutVarint(c, nPrefix); - n += fts3PutVarint(c+n, nTerm-nPrefix); - } - -#ifndef NDEBUG - pWriter->iLastChildBlock++; -#endif - assert( pWriter->iLastChildBlock==iChildBlock ); - - /* Overflow to a new block if the new term makes the current block - ** too big, and the current block already has enough terms. - */ - if( pWriter->last->data.nData+n+nTerm-nPrefix>INTERIOR_MAX && - iChildBlock-pWriter->iOpeningChildBlock>INTERIOR_MIN_TERMS ){ - pWriter->last->next = interiorBlockNew(pWriter->iHeight, iChildBlock, - pTerm, nTerm); - pWriter->last = pWriter->last->next; - pWriter->iOpeningChildBlock = iChildBlock; - dataBufferReset(&pWriter->term); - }else{ - dataBufferAppend2(&pWriter->last->data, c, n, - pTerm+nPrefix, nTerm-nPrefix); - dataBufferReplace(&pWriter->term, pTerm, nTerm); - } - ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); -} - -/* Free the space used by pWriter, including the linked-list of -** InteriorBlocks, and parentWriter, if present. -*/ -static int interiorWriterDestroy(InteriorWriter *pWriter){ - InteriorBlock *block = pWriter->first; - - while( block!=NULL ){ - InteriorBlock *b = block; - block = block->next; - dataBufferDestroy(&b->term); - dataBufferDestroy(&b->data); - sqlite3_free(b); - } - if( pWriter->parentWriter!=NULL ){ - interiorWriterDestroy(pWriter->parentWriter); - sqlite3_free(pWriter->parentWriter); - } - dataBufferDestroy(&pWriter->term); - SCRAMBLE(pWriter); - return SQLITE_OK; -} - -/* If pWriter can fit entirely in ROOT_MAX, return it as the root info -** directly, leaving *piEndBlockid unchanged. Otherwise, flush -** pWriter to %_segments, building a new layer of interior nodes, and -** recursively ask for their root into. -*/ -static int interiorWriterRootInfo(fulltext_vtab *v, InteriorWriter *pWriter, - char **ppRootInfo, int *pnRootInfo, - sqlite_int64 *piEndBlockid){ - InteriorBlock *block = pWriter->first; - sqlite_int64 iBlockid = 0; - int rc; - - /* If we can fit the segment inline */ - if( block==pWriter->last && block->data.nDatadata.pData; - *pnRootInfo = block->data.nData; - return SQLITE_OK; - } - - /* Flush the first block to %_segments, and create a new level of - ** interior node. - */ - ASSERT_VALID_INTERIOR_BLOCK(block); - rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid); - if( rc!=SQLITE_OK ) return rc; - *piEndBlockid = iBlockid; - - pWriter->parentWriter = sqlite3_malloc(sizeof(*pWriter->parentWriter)); - interiorWriterInit(pWriter->iHeight+1, - block->term.pData, block->term.nData, - iBlockid, pWriter->parentWriter); - - /* Flush additional blocks and append to the higher interior - ** node. - */ - for(block=block->next; block!=NULL; block=block->next){ - ASSERT_VALID_INTERIOR_BLOCK(block); - rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid); - if( rc!=SQLITE_OK ) return rc; - *piEndBlockid = iBlockid; - - interiorWriterAppend(pWriter->parentWriter, - block->term.pData, block->term.nData, iBlockid); - } - - /* Parent node gets the chance to be the root. */ - return interiorWriterRootInfo(v, pWriter->parentWriter, - ppRootInfo, pnRootInfo, piEndBlockid); -} - -/****************************************************************/ -/* InteriorReader is used to read off the data from an interior node -** (see comment at top of file for the format). -*/ -typedef struct InteriorReader { - const char *pData; - int nData; - - DataBuffer term; /* previous term, for decoding term delta. */ - - sqlite_int64 iBlockid; -} InteriorReader; - -static void interiorReaderDestroy(InteriorReader *pReader){ - dataBufferDestroy(&pReader->term); - SCRAMBLE(pReader); -} - -/* TODO(shess) The assertions are great, but what if we're in NDEBUG -** and the blob is empty or otherwise contains suspect data? -*/ -static void interiorReaderInit(const char *pData, int nData, - InteriorReader *pReader){ - int n, nTerm; - - /* Require at least the leading flag byte */ - assert( nData>0 ); - assert( pData[0]!='\0' ); - - CLEAR(pReader); - - /* Decode the base blockid, and set the cursor to the first term. */ - n = fts3GetVarint(pData+1, &pReader->iBlockid); - assert( 1+n<=nData ); - pReader->pData = pData+1+n; - pReader->nData = nData-(1+n); - - /* A single-child interior node (such as when a leaf node was too - ** large for the segment directory) won't have any terms. - ** Otherwise, decode the first term. - */ - if( pReader->nData==0 ){ - dataBufferInit(&pReader->term, 0); - }else{ - n = fts3GetVarint32(pReader->pData, &nTerm); - dataBufferInit(&pReader->term, nTerm); - dataBufferReplace(&pReader->term, pReader->pData+n, nTerm); - assert( n+nTerm<=pReader->nData ); - pReader->pData += n+nTerm; - pReader->nData -= n+nTerm; - } -} - -static int interiorReaderAtEnd(InteriorReader *pReader){ - return pReader->term.nData==0; -} - -static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){ - return pReader->iBlockid; -} - -static int interiorReaderTermBytes(InteriorReader *pReader){ - assert( !interiorReaderAtEnd(pReader) ); - return pReader->term.nData; -} -static const char *interiorReaderTerm(InteriorReader *pReader){ - assert( !interiorReaderAtEnd(pReader) ); - return pReader->term.pData; -} - -/* Step forward to the next term in the node. */ -static void interiorReaderStep(InteriorReader *pReader){ - assert( !interiorReaderAtEnd(pReader) ); - - /* If the last term has been read, signal eof, else construct the - ** next term. - */ - if( pReader->nData==0 ){ - dataBufferReset(&pReader->term); - }else{ - int n, nPrefix, nSuffix; - - n = fts3GetVarint32(pReader->pData, &nPrefix); - n += fts3GetVarint32(pReader->pData+n, &nSuffix); - - /* Truncate the current term and append suffix data. */ - pReader->term.nData = nPrefix; - dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); - - assert( n+nSuffix<=pReader->nData ); - pReader->pData += n+nSuffix; - pReader->nData -= n+nSuffix; - } - pReader->iBlockid++; -} - -/* Compare the current term to pTerm[nTerm], returning strcmp-style -** results. If isPrefix, equality means equal through nTerm bytes. -*/ -static int interiorReaderTermCmp(InteriorReader *pReader, - const char *pTerm, int nTerm, int isPrefix){ - const char *pReaderTerm = interiorReaderTerm(pReader); - int nReaderTerm = interiorReaderTermBytes(pReader); - int c, n = nReaderTerm0 ) return -1; - if( nTerm>0 ) return 1; - return 0; - } - - c = memcmp(pReaderTerm, pTerm, n); - if( c!=0 ) return c; - if( isPrefix && n==nTerm ) return 0; - return nReaderTerm - nTerm; -} - -/****************************************************************/ -/* LeafWriter is used to collect terms and associated doclist data -** into leaf blocks in %_segments (see top of file for format info). -** Expected usage is: -** -** LeafWriter writer; -** leafWriterInit(0, 0, &writer); -** while( sorted_terms_left_to_process ){ -** // data is doclist data for that term. -** rc = leafWriterStep(v, &writer, pTerm, nTerm, pData, nData); -** if( rc!=SQLITE_OK ) goto err; -** } -** rc = leafWriterFinalize(v, &writer); -**err: -** leafWriterDestroy(&writer); -** return rc; -** -** leafWriterStep() may write a collected leaf out to %_segments. -** leafWriterFinalize() finishes writing any buffered data and stores -** a root node in %_segdir. leafWriterDestroy() frees all buffers and -** InteriorWriters allocated as part of writing this segment. -** -** TODO(shess) Document leafWriterStepMerge(). -*/ - -/* Put terms with data this big in their own block. */ -#define STANDALONE_MIN 1024 - -/* Keep leaf blocks below this size. */ -#define LEAF_MAX 2048 - -typedef struct LeafWriter { - int iLevel; - int idx; - sqlite_int64 iStartBlockid; /* needed to create the root info */ - sqlite_int64 iEndBlockid; /* when we're done writing. */ - - DataBuffer term; /* previous encoded term */ - DataBuffer data; /* encoding buffer */ - - /* bytes of first term in the current node which distinguishes that - ** term from the last term of the previous node. - */ - int nTermDistinct; - - InteriorWriter parentWriter; /* if we overflow */ - int has_parent; -} LeafWriter; - -static void leafWriterInit(int iLevel, int idx, LeafWriter *pWriter){ - CLEAR(pWriter); - pWriter->iLevel = iLevel; - pWriter->idx = idx; - - dataBufferInit(&pWriter->term, 32); - - /* Start out with a reasonably sized block, though it can grow. */ - dataBufferInit(&pWriter->data, LEAF_MAX); -} - -#ifndef NDEBUG -/* Verify that the data is readable as a leaf node. */ -static void leafNodeValidate(const char *pData, int nData){ - int n, iDummy; - - if( nData==0 ) return; - assert( nData>0 ); - assert( pData!=0 ); - assert( pData+nData>pData ); - - /* Must lead with a varint(0) */ - n = fts3GetVarint32(pData, &iDummy); - assert( iDummy==0 ); - assert( n>0 ); - assert( n0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy<=nData ); - ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL); - pData += n+iDummy; - nData -= n+iDummy; - - /* Verify that trailing terms and doclists also are readable. */ - while( nData!=0 ){ - n = fts3GetVarint32(pData, &iDummy); - assert( n>0 ); - assert( iDummy>=0 ); - assert( n0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy0 ); - assert( iDummy>0 ); - assert( n+iDummy>0 ); - assert( n+iDummy<=nData ); - ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL); - pData += n+iDummy; - nData -= n+iDummy; - } -} -#define ASSERT_VALID_LEAF_NODE(p, n) leafNodeValidate(p, n) -#else -#define ASSERT_VALID_LEAF_NODE(p, n) assert( 1 ) -#endif - -/* Flush the current leaf node to %_segments, and adding the resulting -** blockid and the starting term to the interior node which will -** contain it. -*/ -static int leafWriterInternalFlush(fulltext_vtab *v, LeafWriter *pWriter, - int iData, int nData){ - sqlite_int64 iBlockid = 0; - const char *pStartingTerm; - int nStartingTerm, rc, n; - - /* Must have the leading varint(0) flag, plus at least some - ** valid-looking data. - */ - assert( nData>2 ); - assert( iData>=0 ); - assert( iData+nData<=pWriter->data.nData ); - ASSERT_VALID_LEAF_NODE(pWriter->data.pData+iData, nData); - - rc = block_insert(v, pWriter->data.pData+iData, nData, &iBlockid); - if( rc!=SQLITE_OK ) return rc; - assert( iBlockid!=0 ); - - /* Reconstruct the first term in the leaf for purposes of building - ** the interior node. - */ - n = fts3GetVarint32(pWriter->data.pData+iData+1, &nStartingTerm); - pStartingTerm = pWriter->data.pData+iData+1+n; - assert( pWriter->data.nData>iData+1+n+nStartingTerm ); - assert( pWriter->nTermDistinct>0 ); - assert( pWriter->nTermDistinct<=nStartingTerm ); - nStartingTerm = pWriter->nTermDistinct; - - if( pWriter->has_parent ){ - interiorWriterAppend(&pWriter->parentWriter, - pStartingTerm, nStartingTerm, iBlockid); - }else{ - interiorWriterInit(1, pStartingTerm, nStartingTerm, iBlockid, - &pWriter->parentWriter); - pWriter->has_parent = 1; - } - - /* Track the span of this segment's leaf nodes. */ - if( pWriter->iEndBlockid==0 ){ - pWriter->iEndBlockid = pWriter->iStartBlockid = iBlockid; - }else{ - pWriter->iEndBlockid++; - assert( iBlockid==pWriter->iEndBlockid ); - } - - return SQLITE_OK; -} -static int leafWriterFlush(fulltext_vtab *v, LeafWriter *pWriter){ - int rc = leafWriterInternalFlush(v, pWriter, 0, pWriter->data.nData); - if( rc!=SQLITE_OK ) return rc; - - /* Re-initialize the output buffer. */ - dataBufferReset(&pWriter->data); - - return SQLITE_OK; -} - -/* Fetch the root info for the segment. If the entire leaf fits -** within ROOT_MAX, then it will be returned directly, otherwise it -** will be flushed and the root info will be returned from the -** interior node. *piEndBlockid is set to the blockid of the last -** interior or leaf node written to disk (0 if none are written at -** all). -*/ -static int leafWriterRootInfo(fulltext_vtab *v, LeafWriter *pWriter, - char **ppRootInfo, int *pnRootInfo, - sqlite_int64 *piEndBlockid){ - /* we can fit the segment entirely inline */ - if( !pWriter->has_parent && pWriter->data.nDatadata.pData; - *pnRootInfo = pWriter->data.nData; - *piEndBlockid = 0; - return SQLITE_OK; - } - - /* Flush remaining leaf data. */ - if( pWriter->data.nData>0 ){ - int rc = leafWriterFlush(v, pWriter); - if( rc!=SQLITE_OK ) return rc; - } - - /* We must have flushed a leaf at some point. */ - assert( pWriter->has_parent ); - - /* Tenatively set the end leaf blockid as the end blockid. If the - ** interior node can be returned inline, this will be the final - ** blockid, otherwise it will be overwritten by - ** interiorWriterRootInfo(). - */ - *piEndBlockid = pWriter->iEndBlockid; - - return interiorWriterRootInfo(v, &pWriter->parentWriter, - ppRootInfo, pnRootInfo, piEndBlockid); -} - -/* Collect the rootInfo data and store it into the segment directory. -** This has the effect of flushing the segment's leaf data to -** %_segments, and also flushing any interior nodes to %_segments. -*/ -static int leafWriterFinalize(fulltext_vtab *v, LeafWriter *pWriter){ - sqlite_int64 iEndBlockid; - char *pRootInfo; - int rc, nRootInfo; - - rc = leafWriterRootInfo(v, pWriter, &pRootInfo, &nRootInfo, &iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - /* Don't bother storing an entirely empty segment. */ - if( iEndBlockid==0 && nRootInfo==0 ) return SQLITE_OK; - - return segdir_set(v, pWriter->iLevel, pWriter->idx, - pWriter->iStartBlockid, pWriter->iEndBlockid, - iEndBlockid, pRootInfo, nRootInfo); -} - -static void leafWriterDestroy(LeafWriter *pWriter){ - if( pWriter->has_parent ) interiorWriterDestroy(&pWriter->parentWriter); - dataBufferDestroy(&pWriter->term); - dataBufferDestroy(&pWriter->data); -} - -/* Encode a term into the leafWriter, delta-encoding as appropriate. -** Returns the length of the new term which distinguishes it from the -** previous term, which can be used to set nTermDistinct when a node -** boundary is crossed. -*/ -static int leafWriterEncodeTerm(LeafWriter *pWriter, - const char *pTerm, int nTerm){ - char c[VARINT_MAX+VARINT_MAX]; - int n, nPrefix = 0; - - assert( nTerm>0 ); - while( nPrefixterm.nData && - pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){ - nPrefix++; - /* Failing this implies that the terms weren't in order. */ - assert( nPrefixdata.nData==0 ){ - /* Encode the node header and leading term as: - ** varint(0) - ** varint(nTerm) - ** char pTerm[nTerm] - */ - n = fts3PutVarint(c, '\0'); - n += fts3PutVarint(c+n, nTerm); - dataBufferAppend2(&pWriter->data, c, n, pTerm, nTerm); - }else{ - /* Delta-encode the term as: - ** varint(nPrefix) - ** varint(nSuffix) - ** char pTermSuffix[nSuffix] - */ - n = fts3PutVarint(c, nPrefix); - n += fts3PutVarint(c+n, nTerm-nPrefix); - dataBufferAppend2(&pWriter->data, c, n, pTerm+nPrefix, nTerm-nPrefix); - } - dataBufferReplace(&pWriter->term, pTerm, nTerm); - - return nPrefix+1; -} - -/* Used to avoid a memmove when a large amount of doclist data is in -** the buffer. This constructs a node and term header before -** iDoclistData and flushes the resulting complete node using -** leafWriterInternalFlush(). -*/ -static int leafWriterInlineFlush(fulltext_vtab *v, LeafWriter *pWriter, - const char *pTerm, int nTerm, - int iDoclistData){ - char c[VARINT_MAX+VARINT_MAX]; - int iData, n = fts3PutVarint(c, 0); - n += fts3PutVarint(c+n, nTerm); - - /* There should always be room for the header. Even if pTerm shared - ** a substantial prefix with the previous term, the entire prefix - ** could be constructed from earlier data in the doclist, so there - ** should be room. - */ - assert( iDoclistData>=n+nTerm ); - - iData = iDoclistData-(n+nTerm); - memcpy(pWriter->data.pData+iData, c, n); - memcpy(pWriter->data.pData+iData+n, pTerm, nTerm); - - return leafWriterInternalFlush(v, pWriter, iData, pWriter->data.nData-iData); -} - -/* Push pTerm[nTerm] along with the doclist data to the leaf layer of -** %_segments. -*/ -static int leafWriterStepMerge(fulltext_vtab *v, LeafWriter *pWriter, - const char *pTerm, int nTerm, - DLReader *pReaders, int nReaders){ - char c[VARINT_MAX+VARINT_MAX]; - int iTermData = pWriter->data.nData, iDoclistData; - int i, nData, n, nActualData, nActual, rc, nTermDistinct; - - ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData); - nTermDistinct = leafWriterEncodeTerm(pWriter, pTerm, nTerm); - - /* Remember nTermDistinct if opening a new node. */ - if( iTermData==0 ) pWriter->nTermDistinct = nTermDistinct; - - iDoclistData = pWriter->data.nData; - - /* Estimate the length of the merged doclist so we can leave space - ** to encode it. - */ - for(i=0, nData=0; idata, c, n); - - docListMerge(&pWriter->data, pReaders, nReaders); - ASSERT_VALID_DOCLIST(DL_DEFAULT, - pWriter->data.pData+iDoclistData+n, - pWriter->data.nData-iDoclistData-n, NULL); - - /* The actual amount of doclist data at this point could be smaller - ** than the length we encoded. Additionally, the space required to - ** encode this length could be smaller. For small doclists, this is - ** not a big deal, we can just use memmove() to adjust things. - */ - nActualData = pWriter->data.nData-(iDoclistData+n); - nActual = fts3PutVarint(c, nActualData); - assert( nActualData<=nData ); - assert( nActual<=n ); - - /* If the new doclist is big enough for force a standalone leaf - ** node, we can immediately flush it inline without doing the - ** memmove(). - */ - /* TODO(shess) This test matches leafWriterStep(), which does this - ** test before it knows the cost to varint-encode the term and - ** doclist lengths. At some point, change to - ** pWriter->data.nData-iTermData>STANDALONE_MIN. - */ - if( nTerm+nActualData>STANDALONE_MIN ){ - /* Push leaf node from before this term. */ - if( iTermData>0 ){ - rc = leafWriterInternalFlush(v, pWriter, 0, iTermData); - if( rc!=SQLITE_OK ) return rc; - - pWriter->nTermDistinct = nTermDistinct; - } - - /* Fix the encoded doclist length. */ - iDoclistData += n - nActual; - memcpy(pWriter->data.pData+iDoclistData, c, nActual); - - /* Push the standalone leaf node. */ - rc = leafWriterInlineFlush(v, pWriter, pTerm, nTerm, iDoclistData); - if( rc!=SQLITE_OK ) return rc; - - /* Leave the node empty. */ - dataBufferReset(&pWriter->data); - - return rc; - } - - /* At this point, we know that the doclist was small, so do the - ** memmove if indicated. - */ - if( nActualdata.pData+iDoclistData+nActual, - pWriter->data.pData+iDoclistData+n, - pWriter->data.nData-(iDoclistData+n)); - pWriter->data.nData -= n-nActual; - } - - /* Replace written length with actual length. */ - memcpy(pWriter->data.pData+iDoclistData, c, nActual); - - /* If the node is too large, break things up. */ - /* TODO(shess) This test matches leafWriterStep(), which does this - ** test before it knows the cost to varint-encode the term and - ** doclist lengths. At some point, change to - ** pWriter->data.nData>LEAF_MAX. - */ - if( iTermData+nTerm+nActualData>LEAF_MAX ){ - /* Flush out the leading data as a node */ - rc = leafWriterInternalFlush(v, pWriter, 0, iTermData); - if( rc!=SQLITE_OK ) return rc; - - pWriter->nTermDistinct = nTermDistinct; - - /* Rebuild header using the current term */ - n = fts3PutVarint(pWriter->data.pData, 0); - n += fts3PutVarint(pWriter->data.pData+n, nTerm); - memcpy(pWriter->data.pData+n, pTerm, nTerm); - n += nTerm; - - /* There should always be room, because the previous encoding - ** included all data necessary to construct the term. - */ - assert( ndata.nData-iDoclistDatadata.pData+n, - pWriter->data.pData+iDoclistData, - pWriter->data.nData-iDoclistData); - pWriter->data.nData -= iDoclistData-n; - } - ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData); - - return SQLITE_OK; -} - -/* Push pTerm[nTerm] along with the doclist data to the leaf layer of -** %_segments. -*/ -/* TODO(shess) Revise writeZeroSegment() so that doclists are -** constructed directly in pWriter->data. -*/ -static int leafWriterStep(fulltext_vtab *v, LeafWriter *pWriter, - const char *pTerm, int nTerm, - const char *pData, int nData){ - int rc; - DLReader reader; - - dlrInit(&reader, DL_DEFAULT, pData, nData); - rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1); - dlrDestroy(&reader); - - return rc; -} - - -/****************************************************************/ -/* LeafReader is used to iterate over an individual leaf node. */ -typedef struct LeafReader { - DataBuffer term; /* copy of current term. */ - - const char *pData; /* data for current term. */ - int nData; -} LeafReader; - -static void leafReaderDestroy(LeafReader *pReader){ - dataBufferDestroy(&pReader->term); - SCRAMBLE(pReader); -} - -static int leafReaderAtEnd(LeafReader *pReader){ - return pReader->nData<=0; -} - -/* Access the current term. */ -static int leafReaderTermBytes(LeafReader *pReader){ - return pReader->term.nData; -} -static const char *leafReaderTerm(LeafReader *pReader){ - assert( pReader->term.nData>0 ); - return pReader->term.pData; -} - -/* Access the doclist data for the current term. */ -static int leafReaderDataBytes(LeafReader *pReader){ - int nData; - assert( pReader->term.nData>0 ); - fts3GetVarint32(pReader->pData, &nData); - return nData; -} -static const char *leafReaderData(LeafReader *pReader){ - int n, nData; - assert( pReader->term.nData>0 ); - n = fts3GetVarint32(pReader->pData, &nData); - return pReader->pData+n; -} - -static void leafReaderInit(const char *pData, int nData, - LeafReader *pReader){ - int nTerm, n; - - assert( nData>0 ); - assert( pData[0]=='\0' ); - - CLEAR(pReader); - - /* Read the first term, skipping the header byte. */ - n = fts3GetVarint32(pData+1, &nTerm); - dataBufferInit(&pReader->term, nTerm); - dataBufferReplace(&pReader->term, pData+1+n, nTerm); - - /* Position after the first term. */ - assert( 1+n+nTermpData = pData+1+n+nTerm; - pReader->nData = nData-1-n-nTerm; -} - -/* Step the reader forward to the next term. */ -static void leafReaderStep(LeafReader *pReader){ - int n, nData, nPrefix, nSuffix; - assert( !leafReaderAtEnd(pReader) ); - - /* Skip previous entry's data block. */ - n = fts3GetVarint32(pReader->pData, &nData); - assert( n+nData<=pReader->nData ); - pReader->pData += n+nData; - pReader->nData -= n+nData; - - if( !leafReaderAtEnd(pReader) ){ - /* Construct the new term using a prefix from the old term plus a - ** suffix from the leaf data. - */ - n = fts3GetVarint32(pReader->pData, &nPrefix); - n += fts3GetVarint32(pReader->pData+n, &nSuffix); - assert( n+nSuffixnData ); - pReader->term.nData = nPrefix; - dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); - - pReader->pData += n+nSuffix; - pReader->nData -= n+nSuffix; - } -} - -/* strcmp-style comparison of pReader's current term against pTerm. -** If isPrefix, equality means equal through nTerm bytes. -*/ -static int leafReaderTermCmp(LeafReader *pReader, - const char *pTerm, int nTerm, int isPrefix){ - int c, n = pReader->term.nDataterm.nData : nTerm; - if( n==0 ){ - if( pReader->term.nData>0 ) return -1; - if(nTerm>0 ) return 1; - return 0; - } - - c = memcmp(pReader->term.pData, pTerm, n); - if( c!=0 ) return c; - if( isPrefix && n==nTerm ) return 0; - return pReader->term.nData - nTerm; -} - - -/****************************************************************/ -/* LeavesReader wraps LeafReader to allow iterating over the entire -** leaf layer of the tree. -*/ -typedef struct LeavesReader { - int idx; /* Index within the segment. */ - - sqlite3_stmt *pStmt; /* Statement we're streaming leaves from. */ - int eof; /* we've seen SQLITE_DONE from pStmt. */ - - LeafReader leafReader; /* reader for the current leaf. */ - DataBuffer rootData; /* root data for inline. */ -} LeavesReader; - -/* Access the current term. */ -static int leavesReaderTermBytes(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderTermBytes(&pReader->leafReader); -} -static const char *leavesReaderTerm(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderTerm(&pReader->leafReader); -} - -/* Access the doclist data for the current term. */ -static int leavesReaderDataBytes(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderDataBytes(&pReader->leafReader); -} -static const char *leavesReaderData(LeavesReader *pReader){ - assert( !pReader->eof ); - return leafReaderData(&pReader->leafReader); -} - -static int leavesReaderAtEnd(LeavesReader *pReader){ - return pReader->eof; -} - -/* loadSegmentLeaves() may not read all the way to SQLITE_DONE, thus -** leaving the statement handle open, which locks the table. -*/ -/* TODO(shess) This "solution" is not satisfactory. Really, there -** should be check-in function for all statement handles which -** arranges to call sqlite3_reset(). This most likely will require -** modification to control flow all over the place, though, so for now -** just punt. -** -** Note the the current system assumes that segment merges will run to -** completion, which is why this particular probably hasn't arisen in -** this case. Probably a brittle assumption. -*/ -static int leavesReaderReset(LeavesReader *pReader){ - return sqlite3_reset(pReader->pStmt); -} - -static void leavesReaderDestroy(LeavesReader *pReader){ - leafReaderDestroy(&pReader->leafReader); - dataBufferDestroy(&pReader->rootData); - SCRAMBLE(pReader); -} - -/* Initialize pReader with the given root data (if iStartBlockid==0 -** the leaf data was entirely contained in the root), or from the -** stream of blocks between iStartBlockid and iEndBlockid, inclusive. -*/ -static int leavesReaderInit(fulltext_vtab *v, - int idx, - sqlite_int64 iStartBlockid, - sqlite_int64 iEndBlockid, - const char *pRootData, int nRootData, - LeavesReader *pReader){ - CLEAR(pReader); - pReader->idx = idx; - - dataBufferInit(&pReader->rootData, 0); - if( iStartBlockid==0 ){ - /* Entire leaf level fit in root data. */ - dataBufferReplace(&pReader->rootData, pRootData, nRootData); - leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, - &pReader->leafReader); - }else{ - sqlite3_stmt *s; - int rc = sql_get_leaf_statement(v, idx, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iStartBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 2, iEndBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ){ - pReader->eof = 1; - return SQLITE_OK; - } - if( rc!=SQLITE_ROW ) return rc; - - pReader->pStmt = s; - leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), - sqlite3_column_bytes(pReader->pStmt, 0), - &pReader->leafReader); - } - return SQLITE_OK; -} - -/* Step the current leaf forward to the next term. If we reach the -** end of the current leaf, step forward to the next leaf block. -*/ -static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){ - assert( !leavesReaderAtEnd(pReader) ); - leafReaderStep(&pReader->leafReader); - - if( leafReaderAtEnd(&pReader->leafReader) ){ - int rc; - if( pReader->rootData.pData ){ - pReader->eof = 1; - return SQLITE_OK; - } - rc = sqlite3_step(pReader->pStmt); - if( rc!=SQLITE_ROW ){ - pReader->eof = 1; - return rc==SQLITE_DONE ? SQLITE_OK : rc; - } - leafReaderDestroy(&pReader->leafReader); - leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), - sqlite3_column_bytes(pReader->pStmt, 0), - &pReader->leafReader); - } - return SQLITE_OK; -} - -/* Order LeavesReaders by their term, ignoring idx. Readers at eof -** always sort to the end. -*/ -static int leavesReaderTermCmp(LeavesReader *lr1, LeavesReader *lr2){ - if( leavesReaderAtEnd(lr1) ){ - if( leavesReaderAtEnd(lr2) ) return 0; - return 1; - } - if( leavesReaderAtEnd(lr2) ) return -1; - - return leafReaderTermCmp(&lr1->leafReader, - leavesReaderTerm(lr2), leavesReaderTermBytes(lr2), - 0); -} - -/* Similar to leavesReaderTermCmp(), with additional ordering by idx -** so that older segments sort before newer segments. -*/ -static int leavesReaderCmp(LeavesReader *lr1, LeavesReader *lr2){ - int c = leavesReaderTermCmp(lr1, lr2); - if( c!=0 ) return c; - return lr1->idx-lr2->idx; -} - -/* Assume that pLr[1]..pLr[nLr] are sorted. Bubble pLr[0] into its -** sorted position. -*/ -static void leavesReaderReorder(LeavesReader *pLr, int nLr){ - while( nLr>1 && leavesReaderCmp(pLr, pLr+1)>0 ){ - LeavesReader tmp = pLr[0]; - pLr[0] = pLr[1]; - pLr[1] = tmp; - nLr--; - pLr++; - } -} - -/* Initializes pReaders with the segments from level iLevel, returning -** the number of segments in *piReaders. Leaves pReaders in sorted -** order. -*/ -static int leavesReadersInit(fulltext_vtab *v, int iLevel, - LeavesReader *pReaders, int *piReaders){ - sqlite3_stmt *s; - int i, rc = sql_get_statement(v, SEGDIR_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int(s, 1, iLevel); - if( rc!=SQLITE_OK ) return rc; - - i = 0; - while( (rc = sqlite3_step(s))==SQLITE_ROW ){ - sqlite_int64 iStart = sqlite3_column_int64(s, 0); - sqlite_int64 iEnd = sqlite3_column_int64(s, 1); - const char *pRootData = sqlite3_column_blob(s, 2); - int nRootData = sqlite3_column_bytes(s, 2); - - assert( i0 ){ - leavesReaderDestroy(&pReaders[i]); - } - return rc; - } - - *piReaders = i; - - /* Leave our results sorted by term, then age. */ - while( i-- ){ - leavesReaderReorder(pReaders+i, *piReaders-i); - } - return SQLITE_OK; -} - -/* Merge doclists from pReaders[nReaders] into a single doclist, which -** is written to pWriter. Assumes pReaders is ordered oldest to -** newest. -*/ -/* TODO(shess) Consider putting this inline in segmentMerge(). */ -static int leavesReadersMerge(fulltext_vtab *v, - LeavesReader *pReaders, int nReaders, - LeafWriter *pWriter){ - DLReader dlReaders[MERGE_COUNT]; - const char *pTerm = leavesReaderTerm(pReaders); - int i, nTerm = leavesReaderTermBytes(pReaders); - - assert( nReaders<=MERGE_COUNT ); - - for(i=0; i0 ){ - rc = leavesReaderStep(v, lrs+i); - if( rc!=SQLITE_OK ) goto err; - - /* Reorder by term, then by age. */ - leavesReaderReorder(lrs+i, MERGE_COUNT-i); - } - } - - for(i=0; i0 ); - - for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader); - rc=leavesReaderStep(v, pReader)){ - /* TODO(shess) Really want leavesReaderTermCmp(), but that name is - ** already taken to compare the terms of two LeavesReaders. Think - ** on a better name. [Meanwhile, break encapsulation rather than - ** use a confusing name.] - */ - int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix); - if( c>0 ) break; /* Past any possible matches. */ - if( c==0 ){ - const char *pData = leavesReaderData(pReader); - int iBuffer, nData = leavesReaderDataBytes(pReader); - - /* Find the first empty buffer. */ - for(iBuffer=0; iBuffer0 ){ - assert(pBuffers!=NULL); - memcpy(p, pBuffers, nBuffers*sizeof(*pBuffers)); - sqlite3_free(pBuffers); - } - pBuffers = p; - } - dataBufferInit(&(pBuffers[nBuffers]), 0); - nBuffers++; - } - - /* At this point, must have an empty at iBuffer. */ - assert(iBufferpData, p->nData); - - /* dataBufferReset() could allow a large doclist to blow up - ** our memory requirements. - */ - if( p->nCapacity<1024 ){ - dataBufferReset(p); - }else{ - dataBufferDestroy(p); - dataBufferInit(p, 0); - } - } - } - } - } - - /* Union all the doclists together into *out. */ - /* TODO(shess) What if *out is big? Sigh. */ - if( rc==SQLITE_OK && nBuffers>0 ){ - int iBuffer; - for(iBuffer=0; iBuffer0 ){ - if( out->nData==0 ){ - dataBufferSwap(out, &(pBuffers[iBuffer])); - }else{ - docListAccumulateUnion(out, pBuffers[iBuffer].pData, - pBuffers[iBuffer].nData); - } - } - } - } - - while( nBuffers-- ){ - dataBufferDestroy(&(pBuffers[nBuffers])); - } - if( pBuffers!=NULL ) sqlite3_free(pBuffers); - - return rc; -} - -/* Call loadSegmentLeavesInt() with pData/nData as input. */ -static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - LeavesReader reader; - int rc; - - assert( nData>1 ); - assert( *pData=='\0' ); - rc = leavesReaderInit(v, 0, 0, 0, pData, nData, &reader); - if( rc!=SQLITE_OK ) return rc; - - rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out); - leavesReaderReset(&reader); - leavesReaderDestroy(&reader); - return rc; -} - -/* Call loadSegmentLeavesInt() with the leaf nodes from iStartLeaf to -** iEndLeaf (inclusive) as input, and merge the resulting doclist into -** out. -*/ -static int loadSegmentLeaves(fulltext_vtab *v, - sqlite_int64 iStartLeaf, sqlite_int64 iEndLeaf, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - int rc; - LeavesReader reader; - - assert( iStartLeaf<=iEndLeaf ); - rc = leavesReaderInit(v, 0, iStartLeaf, iEndLeaf, NULL, 0, &reader); - if( rc!=SQLITE_OK ) return rc; - - rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out); - leavesReaderReset(&reader); - leavesReaderDestroy(&reader); - return rc; -} - -/* Taking pData/nData as an interior node, find the sequence of child -** nodes which could include pTerm/nTerm/isPrefix. Note that the -** interior node terms logically come between the blocks, so there is -** one more blockid than there are terms (that block contains terms >= -** the last interior-node term). -*/ -/* TODO(shess) The calling code may already know that the end child is -** not worth calculating, because the end may be in a later sibling -** node. Consider whether breaking symmetry is worthwhile. I suspect -** it is not worthwhile. -*/ -static void getChildrenContaining(const char *pData, int nData, - const char *pTerm, int nTerm, int isPrefix, - sqlite_int64 *piStartChild, - sqlite_int64 *piEndChild){ - InteriorReader reader; - - assert( nData>1 ); - assert( *pData!='\0' ); - interiorReaderInit(pData, nData, &reader); - - /* Scan for the first child which could contain pTerm/nTerm. */ - while( !interiorReaderAtEnd(&reader) ){ - if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break; - interiorReaderStep(&reader); - } - *piStartChild = interiorReaderCurrentBlockid(&reader); - - /* Keep scanning to find a term greater than our term, using prefix - ** comparison if indicated. If isPrefix is false, this will be the - ** same blockid as the starting block. - */ - while( !interiorReaderAtEnd(&reader) ){ - if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break; - interiorReaderStep(&reader); - } - *piEndChild = interiorReaderCurrentBlockid(&reader); - - interiorReaderDestroy(&reader); - - /* Children must ascend, and if !prefix, both must be the same. */ - assert( *piEndChild>=*piStartChild ); - assert( isPrefix || *piStartChild==*piEndChild ); -} - -/* Read block at iBlockid and pass it with other params to -** getChildrenContaining(). -*/ -static int loadAndGetChildrenContaining( - fulltext_vtab *v, - sqlite_int64 iBlockid, - const char *pTerm, int nTerm, int isPrefix, - sqlite_int64 *piStartChild, sqlite_int64 *piEndChild -){ - sqlite3_stmt *s = NULL; - int rc; - - assert( iBlockid!=0 ); - assert( pTerm!=NULL ); - assert( nTerm!=0 ); /* TODO(shess) Why not allow this? */ - assert( piStartChild!=NULL ); - assert( piEndChild!=NULL ); - - rc = sql_get_statement(v, BLOCK_SELECT_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_bind_int64(s, 1, iBlockid); - if( rc!=SQLITE_OK ) return rc; - - rc = sqlite3_step(s); - if( rc==SQLITE_DONE ) return SQLITE_ERROR; - if( rc!=SQLITE_ROW ) return rc; - - getChildrenContaining(sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0), - pTerm, nTerm, isPrefix, piStartChild, piEndChild); - - /* We expect only one row. We must execute another sqlite3_step() - * to complete the iteration; otherwise the table will remain - * locked. */ - rc = sqlite3_step(s); - if( rc==SQLITE_ROW ) return SQLITE_ERROR; - if( rc!=SQLITE_DONE ) return rc; - - return SQLITE_OK; -} - -/* Traverse the tree represented by pData[nData] looking for -** pTerm[nTerm], placing its doclist into *out. This is internal to -** loadSegment() to make error-handling cleaner. -*/ -static int loadSegmentInt(fulltext_vtab *v, const char *pData, int nData, - sqlite_int64 iLeavesEnd, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - /* Special case where root is a leaf. */ - if( *pData=='\0' ){ - return loadSegmentLeaf(v, pData, nData, pTerm, nTerm, isPrefix, out); - }else{ - int rc; - sqlite_int64 iStartChild, iEndChild; - - /* Process pData as an interior node, then loop down the tree - ** until we find the set of leaf nodes to scan for the term. - */ - getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, - &iStartChild, &iEndChild); - while( iStartChild>iLeavesEnd ){ - sqlite_int64 iNextStart, iNextEnd; - rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix, - &iNextStart, &iNextEnd); - if( rc!=SQLITE_OK ) return rc; - - /* If we've branched, follow the end branch, too. */ - if( iStartChild!=iEndChild ){ - sqlite_int64 iDummy; - rc = loadAndGetChildrenContaining(v, iEndChild, pTerm, nTerm, isPrefix, - &iDummy, &iNextEnd); - if( rc!=SQLITE_OK ) return rc; - } - - assert( iNextStart<=iNextEnd ); - iStartChild = iNextStart; - iEndChild = iNextEnd; - } - assert( iStartChild<=iLeavesEnd ); - assert( iEndChild<=iLeavesEnd ); - - /* Scan through the leaf segments for doclists. */ - return loadSegmentLeaves(v, iStartChild, iEndChild, - pTerm, nTerm, isPrefix, out); - } -} - -/* Call loadSegmentInt() to collect the doclist for pTerm/nTerm, then -** merge its doclist over *out (any duplicate doclists read from the -** segment rooted at pData will overwrite those in *out). -*/ -/* TODO(shess) Consider changing this to determine the depth of the -** leaves using either the first characters of interior nodes (when -** ==1, we're one level above the leaves), or the first character of -** the root (which will describe the height of the tree directly). -** Either feels somewhat tricky to me. -*/ -/* TODO(shess) The current merge is likely to be slow for large -** doclists (though it should process from newest/smallest to -** oldest/largest, so it may not be that bad). It might be useful to -** modify things to allow for N-way merging. This could either be -** within a segment, with pairwise merges across segments, or across -** all segments at once. -*/ -static int loadSegment(fulltext_vtab *v, const char *pData, int nData, - sqlite_int64 iLeavesEnd, - const char *pTerm, int nTerm, int isPrefix, - DataBuffer *out){ - DataBuffer result; - int rc; - - assert( nData>1 ); - - /* This code should never be called with buffered updates. */ - assert( v->nPendingData<0 ); - - dataBufferInit(&result, 0); - rc = loadSegmentInt(v, pData, nData, iLeavesEnd, - pTerm, nTerm, isPrefix, &result); - if( rc==SQLITE_OK && result.nData>0 ){ - if( out->nData==0 ){ - DataBuffer tmp = *out; - *out = result; - result = tmp; - }else{ - DataBuffer merged; - DLReader readers[2]; - - dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); - dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); - dataBufferInit(&merged, out->nData+result.nData); - docListMerge(&merged, readers, 2); - dataBufferDestroy(out); - *out = merged; - dlrDestroy(&readers[0]); - dlrDestroy(&readers[1]); - } - } - dataBufferDestroy(&result); - return rc; -} - -/* Scan the database and merge together the posting lists for the term -** into *out. -*/ -static int termSelect(fulltext_vtab *v, int iColumn, - const char *pTerm, int nTerm, int isPrefix, - DocListType iType, DataBuffer *out){ - DataBuffer doclist; - sqlite3_stmt *s; - int rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s); - if( rc!=SQLITE_OK ) return rc; - - /* This code should never be called with buffered updates. */ - assert( v->nPendingData<0 ); - - dataBufferInit(&doclist, 0); - - /* Traverse the segments from oldest to newest so that newer doclist - ** elements for given docids overwrite older elements. - */ - while( (rc = sqlite3_step(s))==SQLITE_ROW ){ - const char *pData = sqlite3_column_blob(s, 0); - const int nData = sqlite3_column_bytes(s, 0); - const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1); - rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix, - &doclist); - if( rc!=SQLITE_OK ) goto err; - } - if( rc==SQLITE_DONE ){ - if( doclist.nData!=0 ){ - /* TODO(shess) The old term_select_all() code applied the column - ** restrict as we merged segments, leading to smaller buffers. - ** This is probably worthwhile to bring back, once the new storage - ** system is checked in. - */ - if( iColumn==v->nColumn) iColumn = -1; - docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, - iColumn, iType, out); - } - rc = SQLITE_OK; - } - - err: - dataBufferDestroy(&doclist); - return rc; -} - -/****************************************************************/ -/* Used to hold hashtable data for sorting. */ -typedef struct TermData { - const char *pTerm; - int nTerm; - DLCollector *pCollector; -} TermData; - -/* Orders TermData elements in strcmp fashion ( <0 for less-than, 0 -** for equal, >0 for greater-than). -*/ -static int termDataCmp(const void *av, const void *bv){ - const TermData *a = (const TermData *)av; - const TermData *b = (const TermData *)bv; - int n = a->nTermnTerm ? a->nTerm : b->nTerm; - int c = memcmp(a->pTerm, b->pTerm, n); - if( c!=0 ) return c; - return a->nTerm-b->nTerm; -} - -/* Order pTerms data by term, then write a new level 0 segment using -** LeafWriter. -*/ -static int writeZeroSegment(fulltext_vtab *v, fts3Hash *pTerms){ - fts3HashElem *e; - int idx, rc, i, n; - TermData *pData; - LeafWriter writer; - DataBuffer dl; - - /* Determine the next index at level 0, merging as necessary. */ - rc = segdirNextIndex(v, 0, &idx); - if( rc!=SQLITE_OK ) return rc; - - n = fts3HashCount(pTerms); - pData = sqlite3_malloc(n*sizeof(TermData)); - - for(i = 0, e = fts3HashFirst(pTerms); e; i++, e = fts3HashNext(e)){ - assert( i1 ) qsort(pData, n, sizeof(*pData), termDataCmp); - - /* TODO(shess) Refactor so that we can write directly to the segment - ** DataBuffer, as happens for segment merges. - */ - leafWriterInit(0, idx, &writer); - dataBufferInit(&dl, 0); - for(i=0; inPendingData>=0 ){ - fts3HashElem *e; - for(e=fts3HashFirst(&v->pendingTerms); e; e=fts3HashNext(e)){ - dlcDelete(fts3HashData(e)); - } - fts3HashClear(&v->pendingTerms); - v->nPendingData = -1; - } - return SQLITE_OK; -} - -/* If pendingTerms has data, flush it to a level-zero segment, and -** free it. -*/ -static int flushPendingTerms(fulltext_vtab *v){ - if( v->nPendingData>=0 ){ - int rc = writeZeroSegment(v, &v->pendingTerms); - if( rc==SQLITE_OK ) clearPendingTerms(v); - return rc; - } - return SQLITE_OK; -} - -/* If pendingTerms is "too big", or docid is out of order, flush it. -** Regardless, be certain that pendingTerms is initialized for use. -*/ -static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid){ - /* TODO(shess) Explore whether partially flushing the buffer on - ** forced-flush would provide better performance. I suspect that if - ** we ordered the doclists by size and flushed the largest until the - ** buffer was half empty, that would let the less frequent terms - ** generate longer doclists. - */ - if( iDocid<=v->iPrevDocid || v->nPendingData>kPendingThreshold ){ - int rc = flushPendingTerms(v); - if( rc!=SQLITE_OK ) return rc; - } - if( v->nPendingData<0 ){ - fts3HashInit(&v->pendingTerms, FTS3_HASH_STRING, 1); - v->nPendingData = 0; - } - v->iPrevDocid = iDocid; - return SQLITE_OK; -} - -/* This function implements the xUpdate callback; it is the top-level entry - * point for inserting, deleting or updating a row in a full-text table. */ -static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg, - sqlite_int64 *pRowid){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - int rc; - - FTSTRACE(("FTS3 Update %p\n", pVtab)); - - if( nArg<2 ){ - rc = index_delete(v, sqlite3_value_int64(ppArg[0])); - } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){ - /* An update: - * ppArg[0] = old rowid - * ppArg[1] = new rowid - * ppArg[2..2+v->nColumn-1] = values - * ppArg[2+v->nColumn] = value for magic column (we ignore this) - * ppArg[2+v->nColumn+1] = value for docid - */ - sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]); - if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER || - sqlite3_value_int64(ppArg[1]) != rowid ){ - rc = SQLITE_ERROR; /* we don't allow changing the rowid */ - }else if( sqlite3_value_type(ppArg[2+v->nColumn+1]) != SQLITE_INTEGER || - sqlite3_value_int64(ppArg[2+v->nColumn+1]) != rowid ){ - rc = SQLITE_ERROR; /* we don't allow changing the docid */ - }else{ - assert( nArg==2+v->nColumn+2); - rc = index_update(v, rowid, &ppArg[2]); - } - } else { - /* An insert: - * ppArg[1] = requested rowid - * ppArg[2..2+v->nColumn-1] = values - * ppArg[2+v->nColumn] = value for magic column (we ignore this) - * ppArg[2+v->nColumn+1] = value for docid - */ - sqlite3_value *pRequestDocid = ppArg[2+v->nColumn+1]; - assert( nArg==2+v->nColumn+2); - if( SQLITE_NULL != sqlite3_value_type(pRequestDocid) && - SQLITE_NULL != sqlite3_value_type(ppArg[1]) ){ - /* TODO(shess) Consider allowing this to work if the values are - ** identical. I'm inclined to discourage that usage, though, - ** given that both rowid and docid are special columns. Better - ** would be to define one or the other as the default winner, - ** but should it be fts3-centric (docid) or SQLite-centric - ** (rowid)? - */ - rc = SQLITE_ERROR; - }else{ - if( SQLITE_NULL == sqlite3_value_type(pRequestDocid) ){ - pRequestDocid = ppArg[1]; - } - rc = index_insert(v, pRequestDocid, &ppArg[2], pRowid); - } - } - - return rc; -} - -static int fulltextSync(sqlite3_vtab *pVtab){ - FTSTRACE(("FTS3 xSync()\n")); - return flushPendingTerms((fulltext_vtab *)pVtab); -} - -static int fulltextBegin(sqlite3_vtab *pVtab){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - FTSTRACE(("FTS3 xBegin()\n")); - - /* Any buffered updates should have been cleared by the previous - ** transaction. - */ - assert( v->nPendingData<0 ); - return clearPendingTerms(v); -} - -static int fulltextCommit(sqlite3_vtab *pVtab){ - fulltext_vtab *v = (fulltext_vtab *) pVtab; - FTSTRACE(("FTS3 xCommit()\n")); - - /* Buffered updates should have been cleared by fulltextSync(). */ - assert( v->nPendingData<0 ); - return clearPendingTerms(v); -} - -static int fulltextRollback(sqlite3_vtab *pVtab){ - FTSTRACE(("FTS3 xRollback()\n")); - return clearPendingTerms((fulltext_vtab *)pVtab); -} - -/* -** Implementation of the snippet() function for FTS3 -*/ -static void snippetFunc( - sqlite3_context *pContext, - int argc, - sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc<1 ) return; - if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1); - }else{ - const char *zStart = ""; - const char *zEnd = ""; - const char *zEllipsis = "..."; - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - if( argc>=2 ){ - zStart = (const char*)sqlite3_value_text(argv[1]); - if( argc>=3 ){ - zEnd = (const char*)sqlite3_value_text(argv[2]); - if( argc>=4 ){ - zEllipsis = (const char*)sqlite3_value_text(argv[3]); - } - } - } - snippetAllOffsets(pCursor); - snippetText(pCursor, zStart, zEnd, zEllipsis); - sqlite3_result_text(pContext, pCursor->snippet.zSnippet, - pCursor->snippet.nSnippet, SQLITE_STATIC); - } -} - -/* -** Implementation of the offsets() function for FTS3 -*/ -static void snippetOffsetsFunc( - sqlite3_context *pContext, - int argc, - sqlite3_value **argv -){ - fulltext_cursor *pCursor; - if( argc<1 ) return; - if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || - sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ - sqlite3_result_error(pContext, "illegal first argument to offsets",-1); - }else{ - memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); - snippetAllOffsets(pCursor); - snippetOffsetText(&pCursor->snippet); - sqlite3_result_text(pContext, - pCursor->snippet.zOffset, pCursor->snippet.nOffset, - SQLITE_STATIC); - } -} - -/* -** This routine implements the xFindFunction method for the FTS3 -** virtual table. -*/ -static int fulltextFindFunction( - sqlite3_vtab *pVtab, - int nArg, - const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg -){ - if( strcmp(zName,"snippet")==0 ){ - *pxFunc = snippetFunc; - return 1; - }else if( strcmp(zName,"offsets")==0 ){ - *pxFunc = snippetOffsetsFunc; - return 1; - } - return 0; -} - -/* -** Rename an fts3 table. -*/ -static int fulltextRename( - sqlite3_vtab *pVtab, - const char *zName -){ - fulltext_vtab *p = (fulltext_vtab *)pVtab; - int rc = SQLITE_NOMEM; - char *zSql = sqlite3_mprintf( - "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';" - "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';" - "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';" - , p->zDb, p->zName, zName - , p->zDb, p->zName, zName - , p->zDb, p->zName, zName - ); - if( zSql ){ - rc = sqlite3_exec(p->db, zSql, 0, 0, 0); - sqlite3_free(zSql); - } - return rc; -} - -static const sqlite3_module fts3Module = { - /* iVersion */ 0, - /* xCreate */ fulltextCreate, - /* xConnect */ fulltextConnect, - /* xBestIndex */ fulltextBestIndex, - /* xDisconnect */ fulltextDisconnect, - /* xDestroy */ fulltextDestroy, - /* xOpen */ fulltextOpen, - /* xClose */ fulltextClose, - /* xFilter */ fulltextFilter, - /* xNext */ fulltextNext, - /* xEof */ fulltextEof, - /* xColumn */ fulltextColumn, - /* xRowid */ fulltextRowid, - /* xUpdate */ fulltextUpdate, - /* xBegin */ fulltextBegin, - /* xSync */ fulltextSync, - /* xCommit */ fulltextCommit, - /* xRollback */ fulltextRollback, - /* xFindFunction */ fulltextFindFunction, - /* xRename */ fulltextRename, -}; - -static void hashDestroy(void *p){ - fts3Hash *pHash = (fts3Hash *)p; - sqlite3Fts3HashClear(pHash); - sqlite3_free(pHash); -} - -/* -** The fts3 built-in tokenizers - "simple" and "porter" - are implemented -** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following -** two forward declarations are for functions declared in these files -** used to retrieve the respective implementations. -** -** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed -** to by the argument to point a the "simple" tokenizer implementation. -** Function ...PorterTokenizerModule() sets *pModule to point to the -** porter tokenizer/stemmer implementation. -*/ -void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); -void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); -void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); - -int sqlite3Fts3InitHashTable(sqlite3 *, fts3Hash *, const char *); - -/* -** Initialise the fts3 extension. If this extension is built as part -** of the sqlite library, then this function is called directly by -** SQLite. If fts3 is built as a dynamically loadable extension, this -** function is called by the sqlite3_extension_init() entry point. -*/ -int sqlite3Fts3Init(sqlite3 *db){ - int rc = SQLITE_OK; - fts3Hash *pHash = 0; - const sqlite3_tokenizer_module *pSimple = 0; - const sqlite3_tokenizer_module *pPorter = 0; - const sqlite3_tokenizer_module *pIcu = 0; - - sqlite3Fts3SimpleTokenizerModule(&pSimple); - sqlite3Fts3PorterTokenizerModule(&pPorter); -#ifdef SQLITE_ENABLE_ICU - sqlite3Fts3IcuTokenizerModule(&pIcu); -#endif - - /* Allocate and initialise the hash-table used to store tokenizers. */ - pHash = sqlite3_malloc(sizeof(fts3Hash)); - if( !pHash ){ - rc = SQLITE_NOMEM; - }else{ - sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); - } - - /* Load the built-in tokenizers into the hash table */ - if( rc==SQLITE_OK ){ - if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) - || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) - || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) - ){ - rc = SQLITE_NOMEM; - } - } - - /* Create the virtual table wrapper around the hash-table and overload - ** the two scalar functions. If this is successful, register the - ** module with sqlite. - */ - if( SQLITE_OK==rc - && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) - && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) - && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1)) - ){ - return sqlite3_create_module_v2( - db, "fts3", &fts3Module, (void *)pHash, hashDestroy - ); - } - - /* An error has occured. Delete the hash table and return the error code. */ - assert( rc!=SQLITE_OK ); - if( pHash ){ - sqlite3Fts3HashClear(pHash); - sqlite3_free(pHash); - } - return rc; -} - -#if !SQLITE_CORE -int sqlite3_extension_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - SQLITE_EXTENSION_INIT2(pApi) - return sqlite3Fts3Init(db); -} -#endif - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ - -/************** End of fts3.c ************************************************/ -/************** Begin file fts3_hash.c ***************************************/ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the implementation of generic hash-tables used in SQLite. -** We've modified it slightly to serve as a standalone hash table -** implementation for the full-text indexing module. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS3 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS3 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) - - -/************** Include sqlite3.h in the middle of fts3_hash.c ***************/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. -** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. -** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. -** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. -** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ -*/ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ - -/* -** Make sure we can call this stuff from C++. -*/ -#if 0 -extern "C" { -#endif - - -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif - -/* -** Make sure these symbols where not defined by some previous header -** file. -*/ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif - -/* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. -** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 - -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); - -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); - -/* -** CAPI3REF: Database Connection Handle {F12000} -** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this -** object. -*/ -typedef struct sqlite3 sqlite3; - - -/* -** CAPI3REF: 64-Bit Integer Types {F10200} -** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} -** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. -*/ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; -#else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; -#endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; - -/* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif - -/* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. -*/ -int sqlite3_close(sqlite3 *); - -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. -*/ -typedef int (*sqlite3_callback)(void*,int,char**, char**); - -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); - -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] -** -*/ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ - -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) - -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 - -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 - -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} -*/ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 - -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). -*/ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 - - -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; - -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; - -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 - -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. -*/ -typedef struct sqlite3_mutex sqlite3_mutex; - -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; - -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 - -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); - -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. -*/ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); - -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. -*/ -int sqlite3_changes(sqlite3*); - -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} -*/ -int sqlite3_total_changes(sqlite3*); - -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. -*/ -void sqlite3_interrupt(sqlite3*); - -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} -*/ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); - -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. -*/ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); - -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. -*/ -int sqlite3_busy_timeout(sqlite3*, int ms); - -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); - -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); - -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); - -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); - -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} -*/ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); - -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. -*/ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ - -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. -*/ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ - -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. -*/ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); - -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); - -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. -*/ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); - -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); - -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; - -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    -*/ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); - -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. -*/ -const char *sqlite3_sql(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. -*/ -typedef struct Mem sqlite3_value; - -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. -*/ -typedef struct sqlite3_context sqlite3_context; - -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. -*/ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); - -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. -*/ -int sqlite3_bind_parameter_count(sqlite3_stmt*); - -/* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); - -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. -*/ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); - -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); - -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. -** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. -*/ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); - -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. -*/ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); - -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. -*/ -int sqlite3_data_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 - -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); - -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); - -/* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ - -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); - -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); - -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. -*/ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); - -/* -** CAPI3REF: User Data For Functions {F16240} -** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} -** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. -*/ -void *sqlite3_user_data(sqlite3_context*); - -/* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. -** -** These routines must be called from the same thread in which -** the SQL function is running. -*/ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); - - -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. -*/ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) - -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); - -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); - -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. -** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. -** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} -** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. -*/ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); - -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); - -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); - -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); - -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. -*/ -SQLITE_EXTERN char *sqlite3_temp_directory; - -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); - -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); - - -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); - -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); - -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. -*/ -int sqlite3_enable_shared_cache(int); - -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. -*/ -int sqlite3_release_memory(int); - -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. -*/ -void sqlite3_soft_heap_limit(int); - -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -*/ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); - -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. -*/ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); - -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} -*/ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); - -/* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -int sqlite3_auto_extension(void *xEntryPoint); - - -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -void sqlite3_reset_auto_extension(void); - - -/* -****** EXPERIMENTAL - subject to change without notice ************** -** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - -/* -** Structures used by the virtual table interface -*/ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; - -/* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. -*/ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); - - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; - -/* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. -** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. -** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). -*/ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ - - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 - -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); - -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); - -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. -*/ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. -*/ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); - -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); - -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** -*/ - -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; - -/* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement -*/ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); - -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. -** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. -** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. -*/ -int sqlite3_blob_close(sqlite3_blob *); - -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); - -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); - -/* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. -** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); - -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); - -/* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ - -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] -*/ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); - -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif - -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif - -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3_hash.c ******************/ -/************** Include fts3_hash.h in the middle of fts3_hash.c *************/ -/************** Begin file fts3_hash.h ***************************************/ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the header file for the generic hash-table implemenation -** used in SQLite. We've modified it slightly to serve as a standalone -** hash table implementation for the full-text indexing module. -** -*/ -#ifndef _FTS3_HASH_H_ -#define _FTS3_HASH_H_ - -/* Forward declarations of structures. */ -typedef struct fts3Hash fts3Hash; -typedef struct fts3HashElem fts3HashElem; - -/* A complete hash table is an instance of the following structure. -** The internals of this structure are intended to be opaque -- client -** code should not attempt to access or modify the fields of this structure -** directly. Change this structure only by using the routines below. -** However, many of the "procedures" and "functions" for modifying and -** accessing this structure are really macros, so we can't really make -** this structure opaque. -*/ -struct fts3Hash { - char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ - char copyKey; /* True if copy of key made on insert */ - int count; /* Number of entries in this table */ - fts3HashElem *first; /* The first element of the array */ - int htsize; /* Number of buckets in the hash table */ - struct _fts3ht { /* the hash table */ - int count; /* Number of entries with this hash */ - fts3HashElem *chain; /* Pointer to first entry with this hash */ - } *ht; -}; - -/* Each element in the hash table is an instance of the following -** structure. All elements are stored on a single doubly-linked list. -** -** Again, this structure is intended to be opaque, but it can't really -** be opaque because it is used by macros. -*/ -struct fts3HashElem { - fts3HashElem *next, *prev; /* Next and previous elements in the table */ - void *data; /* Data associated with this element */ - void *pKey; int nKey; /* Key associated with this element */ -}; - -/* -** There are 2 different modes of operation for a hash table: -** -** FTS3_HASH_STRING pKey points to a string that is nKey bytes long -** (including the null-terminator, if any). Case -** is respected in comparisons. -** -** FTS3_HASH_BINARY pKey points to binary data nKey bytes long. -** memcmp() is used to compare keys. -** -** A copy of the key is made if the copyKey parameter to fts3HashInit is 1. -*/ -#define FTS3_HASH_STRING 1 -#define FTS3_HASH_BINARY 2 - -/* -** Access routines. To delete, insert a NULL pointer. -*/ -void sqlite3Fts3HashInit(fts3Hash*, int keytype, int copyKey); -void *sqlite3Fts3HashInsert(fts3Hash*, const void *pKey, int nKey, void *pData); -void *sqlite3Fts3HashFind(const fts3Hash*, const void *pKey, int nKey); -void sqlite3Fts3HashClear(fts3Hash*); - -/* -** Shorthand for the functions above -*/ -#define fts3HashInit sqlite3Fts3HashInit -#define fts3HashInsert sqlite3Fts3HashInsert -#define fts3HashFind sqlite3Fts3HashFind -#define fts3HashClear sqlite3Fts3HashClear - -/* -** Macros for looping over all elements of a hash table. The idiom is -** like this: -** -** fts3Hash h; -** fts3HashElem *p; -** ... -** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){ -** SomeStructure *pData = fts3HashData(p); -** // do something with pData -** } -*/ -#define fts3HashFirst(H) ((H)->first) -#define fts3HashNext(E) ((E)->next) -#define fts3HashData(E) ((E)->data) -#define fts3HashKey(E) ((E)->pKey) -#define fts3HashKeysize(E) ((E)->nKey) - -/* -** Number of entries in a hash table -*/ -#define fts3HashCount(H) ((H)->count) - -#endif /* _FTS3_HASH_H_ */ - -/************** End of fts3_hash.h *******************************************/ -/************** Continuing where we left off in fts3_hash.c ******************/ - -/* -** Malloc and Free functions -*/ -static void *fts3HashMalloc(int n){ - void *p = sqlite3_malloc(n); - if( p ){ - memset(p, 0, n); - } - return p; -} -static void fts3HashFree(void *p){ - sqlite3_free(p); -} - -/* Turn bulk memory into a hash table object by initializing the -** fields of the Hash structure. -** -** "pNew" is a pointer to the hash table that is to be initialized. -** keyClass is one of the constants -** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass -** determines what kind of key the hash table will use. "copyKey" is -** true if the hash table should make its own private copy of keys and -** false if it should just use the supplied pointer. -*/ -void sqlite3Fts3HashInit(fts3Hash *pNew, int keyClass, int copyKey){ - assert( pNew!=0 ); - assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY ); - pNew->keyClass = keyClass; - pNew->copyKey = copyKey; - pNew->first = 0; - pNew->count = 0; - pNew->htsize = 0; - pNew->ht = 0; -} - -/* Remove all entries from a hash table. Reclaim all memory. -** Call this routine to delete a hash table or to reset a hash table -** to the empty state. -*/ -void sqlite3Fts3HashClear(fts3Hash *pH){ - fts3HashElem *elem; /* For looping over all elements of the table */ - - assert( pH!=0 ); - elem = pH->first; - pH->first = 0; - fts3HashFree(pH->ht); - pH->ht = 0; - pH->htsize = 0; - while( elem ){ - fts3HashElem *next_elem = elem->next; - if( pH->copyKey && elem->pKey ){ - fts3HashFree(elem->pKey); - } - fts3HashFree(elem); - elem = next_elem; - } - pH->count = 0; -} - -/* -** Hash and comparison functions when the mode is FTS3_HASH_STRING -*/ -static int fts3StrHash(const void *pKey, int nKey){ - const char *z = (const char *)pKey; - int h = 0; - if( nKey<=0 ) nKey = (int) strlen(z); - while( nKey > 0 ){ - h = (h<<3) ^ h ^ *z++; - nKey--; - } - return h & 0x7fffffff; -} -static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return strncmp((const char*)pKey1,(const char*)pKey2,n1); -} - -/* -** Hash and comparison functions when the mode is FTS3_HASH_BINARY -*/ -static int fts3BinHash(const void *pKey, int nKey){ - int h = 0; - const char *z = (const char *)pKey; - while( nKey-- > 0 ){ - h = (h<<3) ^ h ^ *(z++); - } - return h & 0x7fffffff; -} -static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){ - if( n1!=n2 ) return 1; - return memcmp(pKey1,pKey2,n1); -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** The C syntax in this function definition may be unfamilar to some -** programmers, so we provide the following additional explanation: -** -** The name of the function is "ftsHashFunction". The function takes a -** single parameter "keyClass". The return value of ftsHashFunction() -** is a pointer to another function. Specifically, the return value -** of ftsHashFunction() is a pointer to a function that takes two parameters -** with types "const void*" and "int" and returns an "int". -*/ -static int (*ftsHashFunction(int keyClass))(const void*,int){ - if( keyClass==FTS3_HASH_STRING ){ - return &fts3StrHash; - }else{ - assert( keyClass==FTS3_HASH_BINARY ); - return &fts3BinHash; - } -} - -/* -** Return a pointer to the appropriate hash function given the key class. -** -** For help in interpreted the obscure C code in the function definition, -** see the header comment on the previous function. -*/ -static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){ - if( keyClass==FTS3_HASH_STRING ){ - return &fts3StrCompare; - }else{ - assert( keyClass==FTS3_HASH_BINARY ); - return &fts3BinCompare; - } -} - -/* Link an element into the hash table -*/ -static void fts3HashInsertElement( - fts3Hash *pH, /* The complete hash table */ - struct _fts3ht *pEntry, /* The entry into which pNew is inserted */ - fts3HashElem *pNew /* The element to be inserted */ -){ - fts3HashElem *pHead; /* First element already in pEntry */ - pHead = pEntry->chain; - if( pHead ){ - pNew->next = pHead; - pNew->prev = pHead->prev; - if( pHead->prev ){ pHead->prev->next = pNew; } - else { pH->first = pNew; } - pHead->prev = pNew; - }else{ - pNew->next = pH->first; - if( pH->first ){ pH->first->prev = pNew; } - pNew->prev = 0; - pH->first = pNew; - } - pEntry->count++; - pEntry->chain = pNew; -} - - -/* Resize the hash table so that it cantains "new_size" buckets. -** "new_size" must be a power of 2. The hash table might fail -** to resize if sqliteMalloc() fails. -*/ -static void fts3Rehash(fts3Hash *pH, int new_size){ - struct _fts3ht *new_ht; /* The new hash table */ - fts3HashElem *elem, *next_elem; /* For looping over existing elements */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( (new_size & (new_size-1))==0 ); - new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) ); - if( new_ht==0 ) return; - fts3HashFree(pH->ht); - pH->ht = new_ht; - pH->htsize = new_size; - xHash = ftsHashFunction(pH->keyClass); - for(elem=pH->first, pH->first=0; elem; elem = next_elem){ - int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); - next_elem = elem->next; - fts3HashInsertElement(pH, &new_ht[h], elem); - } -} - -/* This function (for internal use only) locates an element in an -** hash table that matches the given key. The hash for this key has -** already been computed and is passed as the 4th parameter. -*/ -static fts3HashElem *fts3FindElementByHash( - const fts3Hash *pH, /* The pH to be searched */ - const void *pKey, /* The key we are searching for */ - int nKey, - int h /* The hash for this key. */ -){ - fts3HashElem *elem; /* Used to loop thru the element list */ - int count; /* Number of elements left to test */ - int (*xCompare)(const void*,int,const void*,int); /* comparison function */ - - if( pH->ht ){ - struct _fts3ht *pEntry = &pH->ht[h]; - elem = pEntry->chain; - count = pEntry->count; - xCompare = ftsCompareFunction(pH->keyClass); - while( count-- && elem ){ - if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ - return elem; - } - elem = elem->next; - } - } - return 0; -} - -/* Remove a single entry from the hash table given a pointer to that -** element and a hash on the element's key. -*/ -static void fts3RemoveElementByHash( - fts3Hash *pH, /* The pH containing "elem" */ - fts3HashElem* elem, /* The element to be removed from the pH */ - int h /* Hash value for the element */ -){ - struct _fts3ht *pEntry; - if( elem->prev ){ - elem->prev->next = elem->next; - }else{ - pH->first = elem->next; - } - if( elem->next ){ - elem->next->prev = elem->prev; - } - pEntry = &pH->ht[h]; - if( pEntry->chain==elem ){ - pEntry->chain = elem->next; - } - pEntry->count--; - if( pEntry->count<=0 ){ - pEntry->chain = 0; - } - if( pH->copyKey && elem->pKey ){ - fts3HashFree(elem->pKey); - } - fts3HashFree( elem ); - pH->count--; - if( pH->count<=0 ){ - assert( pH->first==0 ); - assert( pH->count==0 ); - fts3HashClear(pH); - } -} - -/* Attempt to locate an element of the hash table pH with a key -** that matches pKey,nKey. Return the data for this element if it is -** found, or NULL if there is no match. -*/ -void *sqlite3Fts3HashFind(const fts3Hash *pH, const void *pKey, int nKey){ - int h; /* A hash on key */ - fts3HashElem *elem; /* The element that matches key */ - int (*xHash)(const void*,int); /* The hash function */ - - if( pH==0 || pH->ht==0 ) return 0; - xHash = ftsHashFunction(pH->keyClass); - assert( xHash!=0 ); - h = (*xHash)(pKey,nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - elem = fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1)); - return elem ? elem->data : 0; -} - -/* Insert an element into the hash table pH. The key is pKey,nKey -** and the data is "data". -** -** If no element exists with a matching key, then a new -** element is created. A copy of the key is made if the copyKey -** flag is set. NULL is returned. -** -** If another element already exists with the same key, then the -** new data replaces the old data and the old data is returned. -** The key is not copied in this instance. If a malloc fails, then -** the new data is returned and the hash table is unchanged. -** -** If the "data" parameter to this function is NULL, then the -** element corresponding to "key" is removed from the hash table. -*/ -void *sqlite3Fts3HashInsert( - fts3Hash *pH, /* The hash table to insert into */ - const void *pKey, /* The key */ - int nKey, /* Number of bytes in the key */ - void *data /* The data */ -){ - int hraw; /* Raw hash value of the key */ - int h; /* the hash of the key modulo hash table size */ - fts3HashElem *elem; /* Used to loop thru the element list */ - fts3HashElem *new_elem; /* New element added to the pH */ - int (*xHash)(const void*,int); /* The hash function */ - - assert( pH!=0 ); - xHash = ftsHashFunction(pH->keyClass); - assert( xHash!=0 ); - hraw = (*xHash)(pKey, nKey); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - elem = fts3FindElementByHash(pH,pKey,nKey,h); - if( elem ){ - void *old_data = elem->data; - if( data==0 ){ - fts3RemoveElementByHash(pH,elem,h); - }else{ - elem->data = data; - } - return old_data; - } - if( data==0 ) return 0; - new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) ); - if( new_elem==0 ) return data; - if( pH->copyKey && pKey!=0 ){ - new_elem->pKey = fts3HashMalloc( nKey ); - if( new_elem->pKey==0 ){ - fts3HashFree(new_elem); - return data; - } - memcpy((void*)new_elem->pKey, pKey, nKey); - }else{ - new_elem->pKey = (void*)pKey; - } - new_elem->nKey = nKey; - pH->count++; - if( pH->htsize==0 ){ - fts3Rehash(pH,8); - if( pH->htsize==0 ){ - pH->count = 0; - fts3HashFree(new_elem); - return data; - } - } - if( pH->count > pH->htsize ){ - fts3Rehash(pH,pH->htsize*2); - } - assert( pH->htsize>0 ); - assert( (pH->htsize & (pH->htsize-1))==0 ); - h = hraw & (pH->htsize-1); - fts3HashInsertElement(pH, &pH->ht[h], new_elem); - new_elem->data = data; - return 0; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ - -/************** End of fts3_hash.c *******************************************/ -/************** Begin file fts3_porter.c *************************************/ -/* -** 2006 September 30 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** Implementation of the full-text-search tokenizer that implements -** a Porter stemmer. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS3 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS3 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) - - - -/************** Include fts3_tokenizer.h in the middle of fts3_porter.c ******/ -/************** Begin file fts3_tokenizer.h **********************************/ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _FTS3_TOKENIZER_H_ -#define _FTS3_TOKENIZER_H_ - -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -/************** Include sqlite3.h in the middle of fts3_tokenizer.h **********/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. -** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. -** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. -** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. -** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ -*/ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ - -/* -** Make sure we can call this stuff from C++. -*/ -#if 0 -extern "C" { -#endif - - -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif - -/* -** Make sure these symbols where not defined by some previous header -** file. -*/ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif - -/* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. -** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 - -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); - -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); - -/* -** CAPI3REF: Database Connection Handle {F12000} -** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this -** object. -*/ -typedef struct sqlite3 sqlite3; - - -/* -** CAPI3REF: 64-Bit Integer Types {F10200} -** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} -** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. -*/ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; -#else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; -#endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; - -/* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif - -/* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. -*/ -int sqlite3_close(sqlite3 *); - -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. -*/ -typedef int (*sqlite3_callback)(void*,int,char**, char**); - -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); - -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] -** -*/ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ - -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) - -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 - -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 - -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} -*/ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 - -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). -*/ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 - - -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; - -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; - -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 - -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. -*/ -typedef struct sqlite3_mutex sqlite3_mutex; - -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; - -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 - -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); - -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. -*/ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); - -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. -*/ -int sqlite3_changes(sqlite3*); - -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} -*/ -int sqlite3_total_changes(sqlite3*); - -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. -*/ -void sqlite3_interrupt(sqlite3*); - -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} -*/ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); - -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. -*/ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); - -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. -*/ -int sqlite3_busy_timeout(sqlite3*, int ms); - -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); - -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); - -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); - -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); - -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} -*/ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); - -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. -*/ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ - -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. -*/ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ - -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. -*/ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); - -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); - -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. -*/ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); - -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); - -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; - -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    -*/ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); - -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. -*/ -const char *sqlite3_sql(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. -*/ -typedef struct Mem sqlite3_value; - -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. -*/ -typedef struct sqlite3_context sqlite3_context; - -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. -*/ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); - -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. -*/ -int sqlite3_bind_parameter_count(sqlite3_stmt*); - -/* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); - -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. -*/ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); - -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); - -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. -** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. -*/ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); - -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. -*/ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); - -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. -*/ -int sqlite3_data_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 - -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); - -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); - -/* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ - -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); - -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); - -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. -*/ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); - -/* -** CAPI3REF: User Data For Functions {F16240} -** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} -** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. -*/ -void *sqlite3_user_data(sqlite3_context*); - -/* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. -** -** These routines must be called from the same thread in which -** the SQL function is running. -*/ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); - - -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. -*/ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) - -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); - -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); - -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. -** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. -** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} -** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. -*/ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); - -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); - -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); - -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); - -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. -*/ -SQLITE_EXTERN char *sqlite3_temp_directory; - -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); - -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); - - -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); - -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); - -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. -*/ -int sqlite3_enable_shared_cache(int); - -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. -*/ -int sqlite3_release_memory(int); - -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. -*/ -void sqlite3_soft_heap_limit(int); - -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -*/ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); - -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. -*/ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); - -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} -*/ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); - -/* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -int sqlite3_auto_extension(void *xEntryPoint); - - -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -void sqlite3_reset_auto_extension(void); - - -/* -****** EXPERIMENTAL - subject to change without notice ************** -** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - -/* -** Structures used by the virtual table interface -*/ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; - -/* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. -*/ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); - - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; - -/* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. -** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. -** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). -*/ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ - - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 - -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); - -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); - -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. -*/ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. -*/ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); - -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); - -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** -*/ - -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; - -/* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement -*/ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); - -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. -** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. -** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. -*/ -int sqlite3_blob_close(sqlite3_blob *); - -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); - -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); - -/* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. -** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); - -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); - -/* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ - -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] -*/ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); - -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif - -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif - -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3_tokenizer.h *************/ - -/* -** Structures used by the tokenizer interface. When a new tokenizer -** implementation is registered, the caller provides a pointer to -** an sqlite3_tokenizer_module containing pointers to the callback -** functions that make up an implementation. -** -** When an fts3 table is created, it passes any arguments passed to -** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the -** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer -** implementation. The xCreate() function in turn returns an -** sqlite3_tokenizer structure representing the specific tokenizer to -** be used for the fts3 table (customized by the tokenizer clause arguments). -** -** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() -** method is called. It returns an sqlite3_tokenizer_cursor object -** that may be used to tokenize a specific input buffer based on -** the tokenization rules supplied by a specific sqlite3_tokenizer -** object. -*/ -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; - -struct sqlite3_tokenizer_module { - - /* - ** Structure version. Should always be set to 0. - */ - int iVersion; - - /* - ** Create a new tokenizer. The values in the argv[] array are the - ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL - ** TABLE statement that created the fts3 table. For example, if - ** the following SQL is executed: - ** - ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) - ** - ** then argc is set to 2, and the argv[] array contains pointers - ** to the strings "arg1" and "arg2". - ** - ** This method should return either SQLITE_OK (0), or an SQLite error - ** code. If SQLITE_OK is returned, then *ppTokenizer should be set - ** to point at the newly created tokenizer structure. The generic - ** sqlite3_tokenizer.pModule variable should not be initialised by - ** this callback. The caller will do so. - */ - int (*xCreate)( - int argc, /* Size of argv array */ - const char *const*argv, /* Tokenizer argument strings */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ - ); - - /* - ** Destroy an existing tokenizer. The fts3 module calls this method - ** exactly once for each successful call to xCreate(). - */ - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Create a tokenizer cursor to tokenize an input buffer. The caller - ** is responsible for ensuring that the input buffer remains valid - ** until the cursor is closed (using the xClose() method). - */ - int (*xOpen)( - sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ - const char *pInput, int nBytes, /* Input buffer */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ - ); - - /* - ** Destroy an existing tokenizer cursor. The fts3 module calls this - ** method exactly once for each successful call to xOpen(). - */ - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - - /* - ** Retrieve the next token from the tokenizer cursor pCursor. This - ** method should either return SQLITE_OK and set the values of the - ** "OUT" variables identified below, or SQLITE_DONE to indicate that - ** the end of the buffer has been reached, or an SQLite error code. - ** - ** *ppToken should be set to point at a buffer containing the - ** normalized version of the token (i.e. after any case-folding and/or - ** stemming has been performed). *pnBytes should be set to the length - ** of this buffer in bytes. The input text that generated the token is - ** identified by the byte offsets returned in *piStartOffset and - ** *piEndOffset. - ** - ** The buffer *ppToken is set to point at is managed by the tokenizer - ** implementation. It is only required to be valid until the next call - ** to xNext() or xClose(). - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xNext)( - sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ - const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ - int *piStartOffset, /* OUT: Byte offset of token in input buffer */ - int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ - int *piPosition /* OUT: Number of tokens returned before this one */ - ); -}; - -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; - -#endif /* _FTS3_TOKENIZER_H_ */ - -/************** End of fts3_tokenizer.h **************************************/ -/************** Continuing where we left off in fts3_porter.c ****************/ - -/* -** Class derived from sqlite3_tokenizer -*/ -typedef struct porter_tokenizer { - sqlite3_tokenizer base; /* Base class */ -} porter_tokenizer; - -/* -** Class derived from sqlit3_tokenizer_cursor -*/ -typedef struct porter_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *zInput; /* input we are tokenizing */ - int nInput; /* size of the input */ - int iOffset; /* current position in zInput */ - int iToken; /* index of next token to be returned */ - char *zToken; /* storage for current token */ - int nAllocated; /* space allocated to zToken buffer */ -} porter_tokenizer_cursor; - - -/* Forward declaration */ -static const sqlite3_tokenizer_module porterTokenizerModule; - - -/* -** Create a new tokenizer instance. -*/ -static int porterCreate( - int argc, const char * const *argv, - sqlite3_tokenizer **ppTokenizer -){ - porter_tokenizer *t; - t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t)); - if( t==NULL ) return SQLITE_NOMEM; - memset(t, 0, sizeof(*t)); - *ppTokenizer = &t->base; - return SQLITE_OK; -} - -/* -** Destroy a tokenizer -*/ -static int porterDestroy(sqlite3_tokenizer *pTokenizer){ - sqlite3_free(pTokenizer); - return SQLITE_OK; -} - -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is zInput[0..nInput-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. -*/ -static int porterOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *zInput, int nInput, /* String to be tokenized */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - porter_tokenizer_cursor *c; - - c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); - if( c==NULL ) return SQLITE_NOMEM; - - c->zInput = zInput; - if( zInput==0 ){ - c->nInput = 0; - }else if( nInput<0 ){ - c->nInput = (int)strlen(zInput); - }else{ - c->nInput = nInput; - } - c->iOffset = 0; /* start tokenizing at the beginning */ - c->iToken = 0; - c->zToken = NULL; /* no space allocated, yet. */ - c->nAllocated = 0; - - *ppCursor = &c->base; - return SQLITE_OK; -} - -/* -** Close a tokenization cursor previously opened by a call to -** porterOpen() above. -*/ -static int porterClose(sqlite3_tokenizer_cursor *pCursor){ - porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; - sqlite3_free(c->zToken); - sqlite3_free(c); - return SQLITE_OK; -} -/* -** Vowel or consonant -*/ -static const char cType[] = { - 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, - 1, 1, 1, 2, 1 -}; - -/* -** isConsonant() and isVowel() determine if their first character in -** the string they point to is a consonant or a vowel, according -** to Porter ruls. -** -** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'. -** 'Y' is a consonant unless it follows another consonant, -** in which case it is a vowel. -** -** In these routine, the letters are in reverse order. So the 'y' rule -** is that 'y' is a consonant unless it is followed by another -** consonent. -*/ -static int isVowel(const char*); -static int isConsonant(const char *z){ - int j; - char x = *z; - if( x==0 ) return 0; - assert( x>='a' && x<='z' ); - j = cType[x-'a']; - if( j<2 ) return j; - return z[1]==0 || isVowel(z + 1); -} -static int isVowel(const char *z){ - int j; - char x = *z; - if( x==0 ) return 0; - assert( x>='a' && x<='z' ); - j = cType[x-'a']; - if( j<2 ) return 1-j; - return isConsonant(z + 1); -} - -/* -** Let any sequence of one or more vowels be represented by V and let -** C be sequence of one or more consonants. Then every word can be -** represented as: -** -** [C] (VC){m} [V] -** -** In prose: A word is an optional consonant followed by zero or -** vowel-consonant pairs followed by an optional vowel. "m" is the -** number of vowel consonant pairs. This routine computes the value -** of m for the first i bytes of a word. -** -** Return true if the m-value for z is 1 or more. In other words, -** return true if z contains at least one vowel that is followed -** by a consonant. -** -** In this routine z[] is in reverse order. So we are really looking -** for an instance of of a consonant followed by a vowel. -*/ -static int m_gt_0(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* Like mgt0 above except we are looking for a value of m which is -** exactly 1 -*/ -static int m_eq_1(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - if( *z==0 ) return 0; - while( isVowel(z) ){ z++; } - if( *z==0 ) return 1; - while( isConsonant(z) ){ z++; } - return *z==0; -} - -/* Like mgt0 above except we are looking for a value of m>1 instead -** or m>0 -*/ -static int m_gt_1(const char *z){ - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - if( *z==0 ) return 0; - while( isVowel(z) ){ z++; } - if( *z==0 ) return 0; - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* -** Return TRUE if there is a vowel anywhere within z[0..n-1] -*/ -static int hasVowel(const char *z){ - while( isConsonant(z) ){ z++; } - return *z!=0; -} - -/* -** Return TRUE if the word ends in a double consonant. -** -** The text is reversed here. So we are really looking at -** the first two characters of z[]. -*/ -static int doubleConsonant(const char *z){ - return isConsonant(z) && z[0]==z[1] && isConsonant(z+1); -} - -/* -** Return TRUE if the word ends with three letters which -** are consonant-vowel-consonent and where the final consonant -** is not 'w', 'x', or 'y'. -** -** The word is reversed here. So we are really checking the -** first three letters and the first one cannot be in [wxy]. -*/ -static int star_oh(const char *z){ - return - z[0]!=0 && isConsonant(z) && - z[0]!='w' && z[0]!='x' && z[0]!='y' && - z[1]!=0 && isVowel(z+1) && - z[2]!=0 && isConsonant(z+2); -} - -/* -** If the word ends with zFrom and xCond() is true for the stem -** of the word that preceeds the zFrom ending, then change the -** ending to zTo. -** -** The input word *pz and zFrom are both in reverse order. zTo -** is in normal order. -** -** Return TRUE if zFrom matches. Return FALSE if zFrom does not -** match. Not that TRUE is returned even if xCond() fails and -** no substitution occurs. -*/ -static int stem( - char **pz, /* The word being stemmed (Reversed) */ - const char *zFrom, /* If the ending matches this... (Reversed) */ - const char *zTo, /* ... change the ending to this (not reversed) */ - int (*xCond)(const char*) /* Condition that must be true */ -){ - char *z = *pz; - while( *zFrom && *zFrom==*z ){ z++; zFrom++; } - if( *zFrom!=0 ) return 0; - if( xCond && !xCond(z) ) return 1; - while( *zTo ){ - *(--z) = *(zTo++); - } - *pz = z; - return 1; -} - -/* -** This is the fallback stemmer used when the porter stemmer is -** inappropriate. The input word is copied into the output with -** US-ASCII case folding. If the input word is too long (more -** than 20 bytes if it contains no digits or more than 6 bytes if -** it contains digits) then word is truncated to 20 or 6 bytes -** by taking 10 or 3 bytes from the beginning and end. -*/ -static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ - int i, mx, j; - int hasDigit = 0; - for(i=0; i='A' && c<='Z' ){ - zOut[i] = c - 'A' + 'a'; - }else{ - if( c>='0' && c<='9' ) hasDigit = 1; - zOut[i] = c; - } - } - mx = hasDigit ? 3 : 10; - if( nIn>mx*2 ){ - for(j=mx, i=nIn-mx; i=sizeof(zReverse)-7 ){ - /* The word is too big or too small for the porter stemmer. - ** Fallback to the copy stemmer */ - copy_stemmer(zIn, nIn, zOut, pnOut); - return; - } - for(i=0, j=sizeof(zReverse)-6; i='A' && c<='Z' ){ - zReverse[j] = c + 'a' - 'A'; - }else if( c>='a' && c<='z' ){ - zReverse[j] = c; - }else{ - /* The use of a character not in [a-zA-Z] means that we fallback - ** to the copy stemmer */ - copy_stemmer(zIn, nIn, zOut, pnOut); - return; - } - } - memset(&zReverse[sizeof(zReverse)-5], 0, 5); - z = &zReverse[j+1]; - - - /* Step 1a */ - if( z[0]=='s' ){ - if( - !stem(&z, "sess", "ss", 0) && - !stem(&z, "sei", "i", 0) && - !stem(&z, "ss", "ss", 0) - ){ - z++; - } - } - - /* Step 1b */ - z2 = z; - if( stem(&z, "dee", "ee", m_gt_0) ){ - /* Do nothing. The work was all in the test */ - }else if( - (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel)) - && z!=z2 - ){ - if( stem(&z, "ta", "ate", 0) || - stem(&z, "lb", "ble", 0) || - stem(&z, "zi", "ize", 0) ){ - /* Do nothing. The work was all in the test */ - }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){ - z++; - }else if( m_eq_1(z) && star_oh(z) ){ - *(--z) = 'e'; - } - } - - /* Step 1c */ - if( z[0]=='y' && hasVowel(z+1) ){ - z[0] = 'i'; - } - - /* Step 2 */ - switch( z[1] ){ - case 'a': - stem(&z, "lanoita", "ate", m_gt_0) || - stem(&z, "lanoit", "tion", m_gt_0); - break; - case 'c': - stem(&z, "icne", "ence", m_gt_0) || - stem(&z, "icna", "ance", m_gt_0); - break; - case 'e': - stem(&z, "rezi", "ize", m_gt_0); - break; - case 'g': - stem(&z, "igol", "log", m_gt_0); - break; - case 'l': - stem(&z, "ilb", "ble", m_gt_0) || - stem(&z, "illa", "al", m_gt_0) || - stem(&z, "iltne", "ent", m_gt_0) || - stem(&z, "ile", "e", m_gt_0) || - stem(&z, "ilsuo", "ous", m_gt_0); - break; - case 'o': - stem(&z, "noitazi", "ize", m_gt_0) || - stem(&z, "noita", "ate", m_gt_0) || - stem(&z, "rota", "ate", m_gt_0); - break; - case 's': - stem(&z, "msila", "al", m_gt_0) || - stem(&z, "ssenevi", "ive", m_gt_0) || - stem(&z, "ssenluf", "ful", m_gt_0) || - stem(&z, "ssensuo", "ous", m_gt_0); - break; - case 't': - stem(&z, "itila", "al", m_gt_0) || - stem(&z, "itivi", "ive", m_gt_0) || - stem(&z, "itilib", "ble", m_gt_0); - break; - } - - /* Step 3 */ - switch( z[0] ){ - case 'e': - stem(&z, "etaci", "ic", m_gt_0) || - stem(&z, "evita", "", m_gt_0) || - stem(&z, "ezila", "al", m_gt_0); - break; - case 'i': - stem(&z, "itici", "ic", m_gt_0); - break; - case 'l': - stem(&z, "laci", "ic", m_gt_0) || - stem(&z, "luf", "", m_gt_0); - break; - case 's': - stem(&z, "ssen", "", m_gt_0); - break; - } - - /* Step 4 */ - switch( z[1] ){ - case 'a': - if( z[0]=='l' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'c': - if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){ - z += 4; - } - break; - case 'e': - if( z[0]=='r' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'i': - if( z[0]=='c' && m_gt_1(z+2) ){ - z += 2; - } - break; - case 'l': - if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){ - z += 4; - } - break; - case 'n': - if( z[0]=='t' ){ - if( z[2]=='a' ){ - if( m_gt_1(z+3) ){ - z += 3; - } - }else if( z[2]=='e' ){ - stem(&z, "tneme", "", m_gt_1) || - stem(&z, "tnem", "", m_gt_1) || - stem(&z, "tne", "", m_gt_1); - } - } - break; - case 'o': - if( z[0]=='u' ){ - if( m_gt_1(z+2) ){ - z += 2; - } - }else if( z[3]=='s' || z[3]=='t' ){ - stem(&z, "noi", "", m_gt_1); - } - break; - case 's': - if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){ - z += 3; - } - break; - case 't': - stem(&z, "eta", "", m_gt_1) || - stem(&z, "iti", "", m_gt_1); - break; - case 'u': - if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){ - z += 3; - } - break; - case 'v': - case 'z': - if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){ - z += 3; - } - break; - } - - /* Step 5a */ - if( z[0]=='e' ){ - if( m_gt_1(z+1) ){ - z++; - }else if( m_eq_1(z+1) && !star_oh(z+1) ){ - z++; - } - } - - /* Step 5b */ - if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){ - z++; - } - - /* z[] is now the stemmed word in reverse order. Flip it back - ** around into forward order and return. - */ - *pnOut = i = strlen(z); - zOut[i] = 0; - while( *z ){ - zOut[--i] = *(z++); - } -} - -/* -** Characters that can be part of a token. We assume any character -** whose value is greater than 0x80 (any UTF character) can be -** part of a token. In other words, delimiters all must have -** values of 0x7f or lower. -*/ -static const char porterIdChar[] = { -/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ -}; -#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30])) - -/* -** Extract the next token from a tokenization cursor. The cursor must -** have been opened by a prior call to porterOpen(). -*/ -static int porterNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */ - const char **pzToken, /* OUT: *pzToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; - const char *z = c->zInput; - - while( c->iOffsetnInput ){ - int iStartOffset, ch; - - /* Scan past delimiter characters */ - while( c->iOffsetnInput && isDelim(z[c->iOffset]) ){ - c->iOffset++; - } - - /* Count non-delimiter characters. */ - iStartOffset = c->iOffset; - while( c->iOffsetnInput && !isDelim(z[c->iOffset]) ){ - c->iOffset++; - } - - if( c->iOffset>iStartOffset ){ - int n = c->iOffset-iStartOffset; - if( n>c->nAllocated ){ - c->nAllocated = n+20; - c->zToken = sqlite3_realloc(c->zToken, c->nAllocated); - if( c->zToken==NULL ) return SQLITE_NOMEM; - } - porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); - *pzToken = c->zToken; - *piStartOffset = iStartOffset; - *piEndOffset = c->iOffset; - *piPosition = c->iToken++; - return SQLITE_OK; - } - } - return SQLITE_DONE; -} - -/* -** The set of routines that implement the porter-stemmer tokenizer -*/ -static const sqlite3_tokenizer_module porterTokenizerModule = { - 0, - porterCreate, - porterDestroy, - porterOpen, - porterClose, - porterNext, -}; - -/* -** Allocate a new porter tokenizer. Return a pointer to the new -** tokenizer in *ppModule -*/ -void sqlite3Fts3PorterTokenizerModule( - sqlite3_tokenizer_module const**ppModule -){ - *ppModule = &porterTokenizerModule; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ - -/************** End of fts3_porter.c *****************************************/ -/************** Begin file fts3_tokenizer.c **********************************/ -/* -** 2007 June 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This is part of an SQLite module implementing full-text search. -** This particular file implements the generic tokenizer interface. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS3 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS3 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) - -/************** Include sqlite3ext.h in the middle of fts3_tokenizer.c *******/ -/************** Begin file sqlite3ext.h **************************************/ -/* -** 2006 June 7 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the SQLite interface for use by -** shared libraries that want to be imported as extensions into -** an SQLite instance. Shared libraries that intend to be loaded -** as extensions by SQLite should #include this file instead of -** sqlite3.h. -** -** @(#) $Id: sqlite3ext.h,v 1.17 2007/08/31 16:11:36 drh Exp $ -*/ -#ifndef _SQLITE3EXT_H_ -#define _SQLITE3EXT_H_ -/************** Include sqlite3.h in the middle of sqlite3ext.h **************/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. -** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. -** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. -** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. -** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ -*/ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ - -/* -** Make sure we can call this stuff from C++. -*/ -#if 0 -extern "C" { -#endif - - -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif - -/* -** Make sure these symbols where not defined by some previous header -** file. -*/ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif - -/* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. -** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 - -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); - -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); - -/* -** CAPI3REF: Database Connection Handle {F12000} -** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this -** object. -*/ -typedef struct sqlite3 sqlite3; - - -/* -** CAPI3REF: 64-Bit Integer Types {F10200} -** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} -** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. -*/ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; -#else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; -#endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; - -/* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif - -/* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. -*/ -int sqlite3_close(sqlite3 *); - -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. -*/ -typedef int (*sqlite3_callback)(void*,int,char**, char**); - -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); - -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] -** -*/ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ - -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) - -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 - -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 - -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} -*/ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 - -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). -*/ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 - - -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; - -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; - -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 - -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. -*/ -typedef struct sqlite3_mutex sqlite3_mutex; - -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; - -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 - -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); - -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. -*/ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); - -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. -*/ -int sqlite3_changes(sqlite3*); - -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} -*/ -int sqlite3_total_changes(sqlite3*); - -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. -*/ -void sqlite3_interrupt(sqlite3*); - -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} -*/ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); - -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. -*/ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); - -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. -*/ -int sqlite3_busy_timeout(sqlite3*, int ms); - -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); - -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); - -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); - -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); - -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} -*/ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); - -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. -*/ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ - -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. -*/ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ - -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. -*/ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); - -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); - -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. -*/ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); - -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); - -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; - -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    -*/ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); - -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. -*/ -const char *sqlite3_sql(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. -*/ -typedef struct Mem sqlite3_value; - -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. -*/ -typedef struct sqlite3_context sqlite3_context; - -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. -*/ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); - -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. -*/ -int sqlite3_bind_parameter_count(sqlite3_stmt*); - -/* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); - -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. -*/ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); - -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); - -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. -** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. -*/ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); - -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. -*/ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); - -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. -*/ -int sqlite3_data_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 - -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); - -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); - -/* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ - -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); - -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); - -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. -*/ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); - -/* -** CAPI3REF: User Data For Functions {F16240} -** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} -** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. -*/ -void *sqlite3_user_data(sqlite3_context*); - -/* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. -** -** These routines must be called from the same thread in which -** the SQL function is running. -*/ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); - - -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. -*/ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) - -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); - -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); - -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. -** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. -** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} -** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. -*/ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); - -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); - -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); - -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); - -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. -*/ -SQLITE_EXTERN char *sqlite3_temp_directory; - -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); - -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); - - -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); - -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); - -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. -*/ -int sqlite3_enable_shared_cache(int); - -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. -*/ -int sqlite3_release_memory(int); - -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. -*/ -void sqlite3_soft_heap_limit(int); - -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -*/ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); - -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. -*/ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); - -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} -*/ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); - -/* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -int sqlite3_auto_extension(void *xEntryPoint); - - -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -void sqlite3_reset_auto_extension(void); - - -/* -****** EXPERIMENTAL - subject to change without notice ************** -** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - -/* -** Structures used by the virtual table interface -*/ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; - -/* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. -*/ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); - - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; - -/* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. -** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. -** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). -*/ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ - - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 - -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); - -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); - -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. -*/ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. -*/ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); - -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); - -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** -*/ - -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; - -/* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement -*/ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); - -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. -** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. -** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. -*/ -int sqlite3_blob_close(sqlite3_blob *); - -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); - -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); - -/* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. -** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); - -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); - -/* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ - -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] -*/ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); - -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif - -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif - -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in sqlite3ext.h *****************/ - -typedef struct sqlite3_api_routines sqlite3_api_routines; - -/* -** The following structure hold pointers to all of the SQLite API -** routines. -** -** WARNING: In order to maintain backwards compatibility, add new -** interfaces to the end of this structure only. If you insert new -** interfaces in the middle of this structure, then older different -** versions of SQLite will not be able to load each others shared -** libraries! -*/ -struct sqlite3_api_routines { - void * (*aggregate_context)(sqlite3_context*,int nBytes); - int (*aggregate_count)(sqlite3_context*); - int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); - int (*bind_double)(sqlite3_stmt*,int,double); - int (*bind_int)(sqlite3_stmt*,int,int); - int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); - int (*bind_null)(sqlite3_stmt*,int); - int (*bind_parameter_count)(sqlite3_stmt*); - int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); - const char * (*bind_parameter_name)(sqlite3_stmt*,int); - int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); - int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); - int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); - int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); - int (*busy_timeout)(sqlite3*,int ms); - int (*changes)(sqlite3*); - int (*close)(sqlite3*); - int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*)); - int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*)); - const void * (*column_blob)(sqlite3_stmt*,int iCol); - int (*column_bytes)(sqlite3_stmt*,int iCol); - int (*column_bytes16)(sqlite3_stmt*,int iCol); - int (*column_count)(sqlite3_stmt*pStmt); - const char * (*column_database_name)(sqlite3_stmt*,int); - const void * (*column_database_name16)(sqlite3_stmt*,int); - const char * (*column_decltype)(sqlite3_stmt*,int i); - const void * (*column_decltype16)(sqlite3_stmt*,int); - double (*column_double)(sqlite3_stmt*,int iCol); - int (*column_int)(sqlite3_stmt*,int iCol); - sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); - const char * (*column_name)(sqlite3_stmt*,int); - const void * (*column_name16)(sqlite3_stmt*,int); - const char * (*column_origin_name)(sqlite3_stmt*,int); - const void * (*column_origin_name16)(sqlite3_stmt*,int); - const char * (*column_table_name)(sqlite3_stmt*,int); - const void * (*column_table_name16)(sqlite3_stmt*,int); - const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); - const void * (*column_text16)(sqlite3_stmt*,int iCol); - int (*column_type)(sqlite3_stmt*,int iCol); - sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); - void * (*commit_hook)(sqlite3*,int(*)(void*),void*); - int (*complete)(const char*sql); - int (*complete16)(const void*sql); - int (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*)); - int (*create_collation16)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*)); - int (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*)); - int (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*)); - int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); - int (*data_count)(sqlite3_stmt*pStmt); - sqlite3 * (*db_handle)(sqlite3_stmt*); - int (*declare_vtab)(sqlite3*,const char*); - int (*enable_shared_cache)(int); - int (*errcode)(sqlite3*db); - const char * (*errmsg)(sqlite3*); - const void * (*errmsg16)(sqlite3*); - int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); - int (*expired)(sqlite3_stmt*); - int (*finalize)(sqlite3_stmt*pStmt); - void (*free)(void*); - void (*free_table)(char**result); - int (*get_autocommit)(sqlite3*); - void * (*get_auxdata)(sqlite3_context*,int); - int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); - int (*global_recover)(void); - void (*interruptx)(sqlite3*); - sqlite_int64 (*last_insert_rowid)(sqlite3*); - const char * (*libversion)(void); - int (*libversion_number)(void); - void *(*malloc)(int); - char * (*mprintf)(const char*,...); - int (*open)(const char*,sqlite3**); - int (*open16)(const void*,sqlite3**); - int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); - int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); - void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); - void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); - void *(*realloc)(void*,int); - int (*reset)(sqlite3_stmt*pStmt); - void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_double)(sqlite3_context*,double); - void (*result_error)(sqlite3_context*,const char*,int); - void (*result_error16)(sqlite3_context*,const void*,int); - void (*result_int)(sqlite3_context*,int); - void (*result_int64)(sqlite3_context*,sqlite_int64); - void (*result_null)(sqlite3_context*); - void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); - void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_value)(sqlite3_context*,sqlite3_value*); - void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); - int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*); - void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); - char * (*snprintf)(int,char*,const char*,...); - int (*step)(sqlite3_stmt*); - int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*); - void (*thread_cleanup)(void); - int (*total_changes)(sqlite3*); - void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); - int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); - void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*); - void * (*user_data)(sqlite3_context*); - const void * (*value_blob)(sqlite3_value*); - int (*value_bytes)(sqlite3_value*); - int (*value_bytes16)(sqlite3_value*); - double (*value_double)(sqlite3_value*); - int (*value_int)(sqlite3_value*); - sqlite_int64 (*value_int64)(sqlite3_value*); - int (*value_numeric_type)(sqlite3_value*); - const unsigned char * (*value_text)(sqlite3_value*); - const void * (*value_text16)(sqlite3_value*); - const void * (*value_text16be)(sqlite3_value*); - const void * (*value_text16le)(sqlite3_value*); - int (*value_type)(sqlite3_value*); - char *(*vmprintf)(const char*,va_list); - /* Added ??? */ - int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); - /* Added by 3.3.13 */ - int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); - int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); - int (*clear_bindings)(sqlite3_stmt*); - /* Added by 3.4.1 */ - int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *)); - /* Added by 3.5.0 */ - int (*bind_zeroblob)(sqlite3_stmt*,int,int); - int (*blob_bytes)(sqlite3_blob*); - int (*blob_close)(sqlite3_blob*); - int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**); - int (*blob_read)(sqlite3_blob*,void*,int,int); - int (*blob_write)(sqlite3_blob*,const void*,int,int); - int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*)); - int (*file_control)(sqlite3*,const char*,int,void*); - sqlite3_int64 (*memory_highwater)(int); - sqlite3_int64 (*memory_used)(void); - sqlite3_mutex *(*mutex_alloc)(int); - void (*mutex_enter)(sqlite3_mutex*); - void (*mutex_free)(sqlite3_mutex*); - void (*mutex_leave)(sqlite3_mutex*); - int (*mutex_try)(sqlite3_mutex*); - int (*open_v2)(const char*,sqlite3**,int,const char*); - int (*release_memory)(int); - void (*result_error_nomem)(sqlite3_context*); - void (*result_error_toobig)(sqlite3_context*); - int (*sleep)(int); - void (*soft_heap_limit)(int); - sqlite3_vfs *(*vfs_find)(const char*); - int (*vfs_register)(sqlite3_vfs*,int); - int (*vfs_unregister)(sqlite3_vfs*); -}; - -/* -** The following macros redefine the API routines so that they are -** redirected throught the global sqlite3_api structure. -** -** This header file is also used by the loadext.c source file -** (part of the main SQLite library - not an extension) so that -** it can get access to the sqlite3_api_routines structure -** definition. But the main library does not want to redefine -** the API. So the redefinition macros are only valid if the -** SQLITE_CORE macros is undefined. -*/ -#ifndef SQLITE_CORE -#define sqlite3_aggregate_context sqlite3_api->aggregate_context -#define sqlite3_aggregate_count sqlite3_api->aggregate_count -#define sqlite3_bind_blob sqlite3_api->bind_blob -#define sqlite3_bind_double sqlite3_api->bind_double -#define sqlite3_bind_int sqlite3_api->bind_int -#define sqlite3_bind_int64 sqlite3_api->bind_int64 -#define sqlite3_bind_null sqlite3_api->bind_null -#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count -#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index -#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name -#define sqlite3_bind_text sqlite3_api->bind_text -#define sqlite3_bind_text16 sqlite3_api->bind_text16 -#define sqlite3_bind_value sqlite3_api->bind_value -#define sqlite3_busy_handler sqlite3_api->busy_handler -#define sqlite3_busy_timeout sqlite3_api->busy_timeout -#define sqlite3_changes sqlite3_api->changes -#define sqlite3_close sqlite3_api->close -#define sqlite3_collation_needed sqlite3_api->collation_needed -#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 -#define sqlite3_column_blob sqlite3_api->column_blob -#define sqlite3_column_bytes sqlite3_api->column_bytes -#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 -#define sqlite3_column_count sqlite3_api->column_count -#define sqlite3_column_database_name sqlite3_api->column_database_name -#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 -#define sqlite3_column_decltype sqlite3_api->column_decltype -#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 -#define sqlite3_column_double sqlite3_api->column_double -#define sqlite3_column_int sqlite3_api->column_int -#define sqlite3_column_int64 sqlite3_api->column_int64 -#define sqlite3_column_name sqlite3_api->column_name -#define sqlite3_column_name16 sqlite3_api->column_name16 -#define sqlite3_column_origin_name sqlite3_api->column_origin_name -#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 -#define sqlite3_column_table_name sqlite3_api->column_table_name -#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 -#define sqlite3_column_text sqlite3_api->column_text -#define sqlite3_column_text16 sqlite3_api->column_text16 -#define sqlite3_column_type sqlite3_api->column_type -#define sqlite3_column_value sqlite3_api->column_value -#define sqlite3_commit_hook sqlite3_api->commit_hook -#define sqlite3_complete sqlite3_api->complete -#define sqlite3_complete16 sqlite3_api->complete16 -#define sqlite3_create_collation sqlite3_api->create_collation -#define sqlite3_create_collation16 sqlite3_api->create_collation16 -#define sqlite3_create_function sqlite3_api->create_function -#define sqlite3_create_function16 sqlite3_api->create_function16 -#define sqlite3_create_module sqlite3_api->create_module -#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 -#define sqlite3_data_count sqlite3_api->data_count -#define sqlite3_db_handle sqlite3_api->db_handle -#define sqlite3_declare_vtab sqlite3_api->declare_vtab -#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache -#define sqlite3_errcode sqlite3_api->errcode -#define sqlite3_errmsg sqlite3_api->errmsg -#define sqlite3_errmsg16 sqlite3_api->errmsg16 -#define sqlite3_exec sqlite3_api->exec -#define sqlite3_expired sqlite3_api->expired -#define sqlite3_finalize sqlite3_api->finalize -#define sqlite3_free sqlite3_api->free -#define sqlite3_free_table sqlite3_api->free_table -#define sqlite3_get_autocommit sqlite3_api->get_autocommit -#define sqlite3_get_auxdata sqlite3_api->get_auxdata -#define sqlite3_get_table sqlite3_api->get_table -#define sqlite3_global_recover sqlite3_api->global_recover -#define sqlite3_interrupt sqlite3_api->interruptx -#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid -#define sqlite3_libversion sqlite3_api->libversion -#define sqlite3_libversion_number sqlite3_api->libversion_number -#define sqlite3_malloc sqlite3_api->malloc -#define sqlite3_mprintf sqlite3_api->mprintf -#define sqlite3_open sqlite3_api->open -#define sqlite3_open16 sqlite3_api->open16 -#define sqlite3_prepare sqlite3_api->prepare -#define sqlite3_prepare16 sqlite3_api->prepare16 -#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 -#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 -#define sqlite3_profile sqlite3_api->profile -#define sqlite3_progress_handler sqlite3_api->progress_handler -#define sqlite3_realloc sqlite3_api->realloc -#define sqlite3_reset sqlite3_api->reset -#define sqlite3_result_blob sqlite3_api->result_blob -#define sqlite3_result_double sqlite3_api->result_double -#define sqlite3_result_error sqlite3_api->result_error -#define sqlite3_result_error16 sqlite3_api->result_error16 -#define sqlite3_result_int sqlite3_api->result_int -#define sqlite3_result_int64 sqlite3_api->result_int64 -#define sqlite3_result_null sqlite3_api->result_null -#define sqlite3_result_text sqlite3_api->result_text -#define sqlite3_result_text16 sqlite3_api->result_text16 -#define sqlite3_result_text16be sqlite3_api->result_text16be -#define sqlite3_result_text16le sqlite3_api->result_text16le -#define sqlite3_result_value sqlite3_api->result_value -#define sqlite3_rollback_hook sqlite3_api->rollback_hook -#define sqlite3_set_authorizer sqlite3_api->set_authorizer -#define sqlite3_set_auxdata sqlite3_api->set_auxdata -#define sqlite3_snprintf sqlite3_api->snprintf -#define sqlite3_step sqlite3_api->step -#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata -#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup -#define sqlite3_total_changes sqlite3_api->total_changes -#define sqlite3_trace sqlite3_api->trace -#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings -#define sqlite3_update_hook sqlite3_api->update_hook -#define sqlite3_user_data sqlite3_api->user_data -#define sqlite3_value_blob sqlite3_api->value_blob -#define sqlite3_value_bytes sqlite3_api->value_bytes -#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 -#define sqlite3_value_double sqlite3_api->value_double -#define sqlite3_value_int sqlite3_api->value_int -#define sqlite3_value_int64 sqlite3_api->value_int64 -#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type -#define sqlite3_value_text sqlite3_api->value_text -#define sqlite3_value_text16 sqlite3_api->value_text16 -#define sqlite3_value_text16be sqlite3_api->value_text16be -#define sqlite3_value_text16le sqlite3_api->value_text16le -#define sqlite3_value_type sqlite3_api->value_type -#define sqlite3_vmprintf sqlite3_api->vmprintf -#define sqlite3_overload_function sqlite3_api->overload_function -#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 -#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 -#define sqlite3_clear_bindings sqlite3_api->clear_bindings -#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob -#define sqlite3_blob_bytes sqlite3_api->blob_bytes -#define sqlite3_blob_close sqlite3_api->blob_close -#define sqlite3_blob_open sqlite3_api->blob_open -#define sqlite3_blob_read sqlite3_api->blob_read -#define sqlite3_blob_write sqlite3_api->blob_write -#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 -#define sqlite3_file_control sqlite3_api->file_control -#define sqlite3_memory_highwater sqlite3_api->memory_highwater -#define sqlite3_memory_used sqlite3_api->memory_used -#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc -#define sqlite3_mutex_enter sqlite3_api->mutex_enter -#define sqlite3_mutex_free sqlite3_api->mutex_free -#define sqlite3_mutex_leave sqlite3_api->mutex_leave -#define sqlite3_mutex_try sqlite3_api->mutex_try -#define sqlite3_open_v2 sqlite3_api->open_v2 -#define sqlite3_release_memory sqlite3_api->release_memory -#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem -#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig -#define sqlite3_sleep sqlite3_api->sleep -#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit -#define sqlite3_vfs_find sqlite3_api->vfs_find -#define sqlite3_vfs_register sqlite3_api->vfs_register -#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister -#endif /* SQLITE_CORE */ - -#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api; -#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v; - -#endif /* _SQLITE3EXT_H_ */ - -/************** End of sqlite3ext.h ******************************************/ -/************** Continuing where we left off in fts3_tokenizer.c *************/ -SQLITE_EXTENSION_INIT1 - -/************** Include fts3_hash.h in the middle of fts3_tokenizer.c ********/ -/************** Begin file fts3_hash.h ***************************************/ -/* -** 2001 September 22 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This is the header file for the generic hash-table implemenation -** used in SQLite. We've modified it slightly to serve as a standalone -** hash table implementation for the full-text indexing module. -** -*/ -#ifndef _FTS3_HASH_H_ -#define _FTS3_HASH_H_ - -/* Forward declarations of structures. */ -typedef struct fts3Hash fts3Hash; -typedef struct fts3HashElem fts3HashElem; - -/* A complete hash table is an instance of the following structure. -** The internals of this structure are intended to be opaque -- client -** code should not attempt to access or modify the fields of this structure -** directly. Change this structure only by using the routines below. -** However, many of the "procedures" and "functions" for modifying and -** accessing this structure are really macros, so we can't really make -** this structure opaque. -*/ -struct fts3Hash { - char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ - char copyKey; /* True if copy of key made on insert */ - int count; /* Number of entries in this table */ - fts3HashElem *first; /* The first element of the array */ - int htsize; /* Number of buckets in the hash table */ - struct _fts3ht { /* the hash table */ - int count; /* Number of entries with this hash */ - fts3HashElem *chain; /* Pointer to first entry with this hash */ - } *ht; -}; - -/* Each element in the hash table is an instance of the following -** structure. All elements are stored on a single doubly-linked list. -** -** Again, this structure is intended to be opaque, but it can't really -** be opaque because it is used by macros. -*/ -struct fts3HashElem { - fts3HashElem *next, *prev; /* Next and previous elements in the table */ - void *data; /* Data associated with this element */ - void *pKey; int nKey; /* Key associated with this element */ -}; - -/* -** There are 2 different modes of operation for a hash table: -** -** FTS3_HASH_STRING pKey points to a string that is nKey bytes long -** (including the null-terminator, if any). Case -** is respected in comparisons. -** -** FTS3_HASH_BINARY pKey points to binary data nKey bytes long. -** memcmp() is used to compare keys. -** -** A copy of the key is made if the copyKey parameter to fts3HashInit is 1. -*/ -#define FTS3_HASH_STRING 1 -#define FTS3_HASH_BINARY 2 - -/* -** Access routines. To delete, insert a NULL pointer. -*/ -void sqlite3Fts3HashInit(fts3Hash*, int keytype, int copyKey); -void *sqlite3Fts3HashInsert(fts3Hash*, const void *pKey, int nKey, void *pData); -void *sqlite3Fts3HashFind(const fts3Hash*, const void *pKey, int nKey); -void sqlite3Fts3HashClear(fts3Hash*); - -/* -** Shorthand for the functions above -*/ -#define fts3HashInit sqlite3Fts3HashInit -#define fts3HashInsert sqlite3Fts3HashInsert -#define fts3HashFind sqlite3Fts3HashFind -#define fts3HashClear sqlite3Fts3HashClear - -/* -** Macros for looping over all elements of a hash table. The idiom is -** like this: -** -** fts3Hash h; -** fts3HashElem *p; -** ... -** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){ -** SomeStructure *pData = fts3HashData(p); -** // do something with pData -** } -*/ -#define fts3HashFirst(H) ((H)->first) -#define fts3HashNext(E) ((E)->next) -#define fts3HashData(E) ((E)->data) -#define fts3HashKey(E) ((E)->pKey) -#define fts3HashKeysize(E) ((E)->nKey) - -/* -** Number of entries in a hash table -*/ -#define fts3HashCount(H) ((H)->count) - -#endif /* _FTS3_HASH_H_ */ - -/************** End of fts3_hash.h *******************************************/ -/************** Continuing where we left off in fts3_tokenizer.c *************/ -/************** Include fts3_tokenizer.h in the middle of fts3_tokenizer.c ***/ -/************** Begin file fts3_tokenizer.h **********************************/ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _FTS3_TOKENIZER_H_ -#define _FTS3_TOKENIZER_H_ - -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -/************** Include sqlite3.h in the middle of fts3_tokenizer.h **********/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. -** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. -** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. -** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. -** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ -*/ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ - -/* -** Make sure we can call this stuff from C++. -*/ -#if 0 -extern "C" { -#endif - - -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif - -/* -** Make sure these symbols where not defined by some previous header -** file. -*/ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif - -/* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. -** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 - -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); - -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); - -/* -** CAPI3REF: Database Connection Handle {F12000} -** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this -** object. -*/ -typedef struct sqlite3 sqlite3; - - -/* -** CAPI3REF: 64-Bit Integer Types {F10200} -** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} -** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. -*/ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; -#else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; -#endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; - -/* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif - -/* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. -*/ -int sqlite3_close(sqlite3 *); - -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. -*/ -typedef int (*sqlite3_callback)(void*,int,char**, char**); - -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); - -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] -** -*/ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ - -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) - -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 - -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 - -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} -*/ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 - -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). -*/ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 - - -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; - -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; - -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 - -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. -*/ -typedef struct sqlite3_mutex sqlite3_mutex; - -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; - -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 - -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); - -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. -*/ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); - -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. -*/ -int sqlite3_changes(sqlite3*); - -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} -*/ -int sqlite3_total_changes(sqlite3*); - -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. -*/ -void sqlite3_interrupt(sqlite3*); - -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} -*/ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); - -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. -*/ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); - -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. -*/ -int sqlite3_busy_timeout(sqlite3*, int ms); - -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); - -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); - -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); - -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); - -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} -*/ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); - -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. -*/ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ - -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. -*/ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ - -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. -*/ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); - -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); - -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. -*/ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); - -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); - -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; - -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    -*/ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); - -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. -*/ -const char *sqlite3_sql(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. -*/ -typedef struct Mem sqlite3_value; - -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. -*/ -typedef struct sqlite3_context sqlite3_context; - -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. -*/ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); - -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. -*/ -int sqlite3_bind_parameter_count(sqlite3_stmt*); - -/* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); - -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. -*/ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); - -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); - -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. -** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. -*/ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); - -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. -*/ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); - -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); - -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. -*/ -int sqlite3_data_count(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 - -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); - -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); - -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); - -/* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ - -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); - -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); - -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. -*/ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); - -/* -** CAPI3REF: User Data For Functions {F16240} -** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} -** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. -*/ -void *sqlite3_user_data(sqlite3_context*); - -/* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. -** -** These routines must be called from the same thread in which -** the SQL function is running. -*/ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); - - -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. -*/ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) - -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); - -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); - -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. -** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. -** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} -** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. -*/ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); - -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); - -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); - -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); - -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. -*/ -SQLITE_EXTERN char *sqlite3_temp_directory; - -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); - -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); - - -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); - -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); - -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. -*/ -int sqlite3_enable_shared_cache(int); - -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. -*/ -int sqlite3_release_memory(int); - -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. -*/ -void sqlite3_soft_heap_limit(int); - -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -*/ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); - -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. -*/ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); - -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} -*/ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); - -/* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -int sqlite3_auto_extension(void *xEntryPoint); - - -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -void sqlite3_reset_auto_extension(void); - - -/* -****** EXPERIMENTAL - subject to change without notice ************** -** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -*/ - -/* -** Structures used by the virtual table interface -*/ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; - -/* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. -*/ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); - - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; - -/* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. -** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. -** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). -*/ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ - - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 - -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); - -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); - -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. -*/ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ -}; - -/* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. -*/ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); - -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); - -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** -*/ - -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; - -/* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement -*/ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); - -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. -** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. -** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. -*/ -int sqlite3_blob_close(sqlite3_blob *); - -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); - -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); - -/* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. -** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); - -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); - -/* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); - -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ - -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] -*/ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); - -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif - -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif - -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3_tokenizer.h *************/ - -/* -** Structures used by the tokenizer interface. When a new tokenizer -** implementation is registered, the caller provides a pointer to -** an sqlite3_tokenizer_module containing pointers to the callback -** functions that make up an implementation. -** -** When an fts3 table is created, it passes any arguments passed to -** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the -** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer -** implementation. The xCreate() function in turn returns an -** sqlite3_tokenizer structure representing the specific tokenizer to -** be used for the fts3 table (customized by the tokenizer clause arguments). -** -** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() -** method is called. It returns an sqlite3_tokenizer_cursor object -** that may be used to tokenize a specific input buffer based on -** the tokenization rules supplied by a specific sqlite3_tokenizer -** object. -*/ -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; - -struct sqlite3_tokenizer_module { - - /* - ** Structure version. Should always be set to 0. - */ - int iVersion; - - /* - ** Create a new tokenizer. The values in the argv[] array are the - ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL - ** TABLE statement that created the fts3 table. For example, if - ** the following SQL is executed: - ** - ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) - ** - ** then argc is set to 2, and the argv[] array contains pointers - ** to the strings "arg1" and "arg2". - ** - ** This method should return either SQLITE_OK (0), or an SQLite error - ** code. If SQLITE_OK is returned, then *ppTokenizer should be set - ** to point at the newly created tokenizer structure. The generic - ** sqlite3_tokenizer.pModule variable should not be initialised by - ** this callback. The caller will do so. - */ - int (*xCreate)( - int argc, /* Size of argv array */ - const char *const*argv, /* Tokenizer argument strings */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ - ); - - /* - ** Destroy an existing tokenizer. The fts3 module calls this method - ** exactly once for each successful call to xCreate(). - */ - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Create a tokenizer cursor to tokenize an input buffer. The caller - ** is responsible for ensuring that the input buffer remains valid - ** until the cursor is closed (using the xClose() method). - */ - int (*xOpen)( - sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ - const char *pInput, int nBytes, /* Input buffer */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ - ); - - /* - ** Destroy an existing tokenizer cursor. The fts3 module calls this - ** method exactly once for each successful call to xOpen(). - */ - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - - /* - ** Retrieve the next token from the tokenizer cursor pCursor. This - ** method should either return SQLITE_OK and set the values of the - ** "OUT" variables identified below, or SQLITE_DONE to indicate that - ** the end of the buffer has been reached, or an SQLite error code. - ** - ** *ppToken should be set to point at a buffer containing the - ** normalized version of the token (i.e. after any case-folding and/or - ** stemming has been performed). *pnBytes should be set to the length - ** of this buffer in bytes. The input text that generated the token is - ** identified by the byte offsets returned in *piStartOffset and - ** *piEndOffset. - ** - ** The buffer *ppToken is set to point at is managed by the tokenizer - ** implementation. It is only required to be valid until the next call - ** to xNext() or xClose(). - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xNext)( - sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ - const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ - int *piStartOffset, /* OUT: Byte offset of token in input buffer */ - int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ - int *piPosition /* OUT: Number of tokens returned before this one */ - ); -}; - -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; - -#endif /* _FTS3_TOKENIZER_H_ */ - -/************** End of fts3_tokenizer.h **************************************/ -/************** Continuing where we left off in fts3_tokenizer.c *************/ - -/* -** Implementation of the SQL scalar function for accessing the underlying -** hash table. This function may be called as follows: -** -** SELECT (); -** SELECT (, ); -** -** where is the name passed as the second argument -** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). -** -** If the argument is specified, it must be a blob value -** containing a pointer to be stored as the hash data corresponding -** to the string . If is not specified, then -** the string must already exist in the has table. Otherwise, -** an error is returned. -** -** Whether or not the argument is specified, the value returned -** is a blob containing the pointer stored as the hash data corresponding -** to string (after the hash-table is updated, if applicable). -*/ -static void scalarFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - fts3Hash *pHash; - void *pPtr = 0; - const unsigned char *zName; - int nName; - - assert( argc==1 || argc==2 ); - - pHash = (fts3Hash *)sqlite3_user_data(context); - - zName = sqlite3_value_text(argv[0]); - nName = sqlite3_value_bytes(argv[0])+1; - - if( argc==2 ){ - void *pOld; - int n = sqlite3_value_bytes(argv[1]); - if( n!=sizeof(pPtr) ){ - sqlite3_result_error(context, "argument type mismatch", -1); - return; - } - pPtr = *(void **)sqlite3_value_blob(argv[1]); - pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); - if( pOld==pPtr ){ - sqlite3_result_error(context, "out of memory", -1); - return; - } - }else{ - pPtr = sqlite3Fts3HashFind(pHash, zName, nName); - if( !pPtr ){ - char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); - return; - } - } - - sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); -} - -#ifdef SQLITE_TEST - -#include - -/* -** Implementation of a special SQL scalar function for testing tokenizers -** designed to be used in concert with the Tcl testing framework. This -** function must be called with two arguments: -** -** SELECT (, ); -** SELECT (, ); -** -** where is the name passed as the second argument -** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') -** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). -** -** The return value is a string that may be interpreted as a Tcl -** list. For each token in the , three elements are -** added to the returned list. The first is the token position, the -** second is the token text (folded, stemmed, etc.) and the third is the -** substring of associated with the token. For example, -** using the built-in "simple" tokenizer: -** -** SELECT fts_tokenizer_test('simple', 'I don't see how'); -** -** will return the string: -** -** "{0 i I 1 dont don't 2 see see 3 how how}" -** -*/ -static void testFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - fts3Hash *pHash; - sqlite3_tokenizer_module *p; - sqlite3_tokenizer *pTokenizer = 0; - sqlite3_tokenizer_cursor *pCsr = 0; - - const char *zErr = 0; - - const char *zName; - int nName; - const char *zInput; - int nInput; - - const char *zArg = 0; - - const char *zToken; - int nToken; - int iStart; - int iEnd; - int iPos; - - Tcl_Obj *pRet; - - assert( argc==2 || argc==3 ); - - nName = sqlite3_value_bytes(argv[0]); - zName = (const char *)sqlite3_value_text(argv[0]); - nInput = sqlite3_value_bytes(argv[argc-1]); - zInput = (const char *)sqlite3_value_text(argv[argc-1]); - - if( argc==3 ){ - zArg = (const char *)sqlite3_value_text(argv[1]); - } - - pHash = (fts3Hash *)sqlite3_user_data(context); - p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); - - if( !p ){ - char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); - sqlite3_result_error(context, zErr, -1); - sqlite3_free(zErr); - return; - } - - pRet = Tcl_NewObj(); - Tcl_IncrRefCount(pRet); - - if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){ - zErr = "error in xCreate()"; - goto finish; - } - pTokenizer->pModule = p; - if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){ - zErr = "error in xOpen()"; - goto finish; - } - pCsr->pTokenizer = pTokenizer; - - while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ - Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); - zToken = &zInput[iStart]; - nToken = iEnd-iStart; - Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); - } - - if( SQLITE_OK!=p->xClose(pCsr) ){ - zErr = "error in xClose()"; - goto finish; - } - if( SQLITE_OK!=p->xDestroy(pTokenizer) ){ - zErr = "error in xDestroy()"; - goto finish; - } - -finish: - if( zErr ){ - sqlite3_result_error(context, zErr, -1); - }else{ - sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); - } - Tcl_DecrRefCount(pRet); -} - -static -int registerTokenizer( - sqlite3 *db, - char *zName, - const sqlite3_tokenizer_module *p -){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts3_tokenizer(?, ?)"; - - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); - sqlite3_step(pStmt); - - return sqlite3_finalize(pStmt); -} - -static -int queryTokenizer( - sqlite3 *db, - char *zName, - const sqlite3_tokenizer_module **pp -){ - int rc; - sqlite3_stmt *pStmt; - const char zSql[] = "SELECT fts3_tokenizer(?)"; - - *pp = 0; - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc!=SQLITE_OK ){ - return rc; - } - - sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ - memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); - } - } - - return sqlite3_finalize(pStmt); -} - -void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); - -/* -** Implementation of the scalar function fts3_tokenizer_internal_test(). -** This function is used for testing only, it is not included in the -** build unless SQLITE_TEST is defined. -** -** The purpose of this is to test that the fts3_tokenizer() function -** can be used as designed by the C-code in the queryTokenizer and -** registerTokenizer() functions above. These two functions are repeated -** in the README.tokenizer file as an example, so it is important to -** test them. -** -** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar -** function with no arguments. An assert() will fail if a problem is -** detected. i.e.: -** -** SELECT fts3_tokenizer_internal_test(); -** -*/ -static void intTestFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - int rc; - const sqlite3_tokenizer_module *p1; - const sqlite3_tokenizer_module *p2; - sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); - - /* Test the query function */ - sqlite3Fts3SimpleTokenizerModule(&p1); - rc = queryTokenizer(db, "simple", &p2); - assert( rc==SQLITE_OK ); - assert( p1==p2 ); - rc = queryTokenizer(db, "nosuchtokenizer", &p2); - assert( rc==SQLITE_ERROR ); - assert( p2==0 ); - assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); - - /* Test the storage function */ - rc = registerTokenizer(db, "nosuchtokenizer", p1); - assert( rc==SQLITE_OK ); - rc = queryTokenizer(db, "nosuchtokenizer", &p2); - assert( rc==SQLITE_OK ); - assert( p2==p1 ); - - sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); -} - -#endif - -/* -** Set up SQL objects in database db used to access the contents of -** the hash table pointed to by argument pHash. The hash table must -** been initialised to use string keys, and to take a private copy -** of the key when a value is inserted. i.e. by a call similar to: -** -** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); -** -** This function adds a scalar function (see header comment above -** scalarFunc() in this file for details) and, if ENABLE_TABLE is -** defined at compilation time, a temporary virtual table (see header -** comment above struct HashTableVtab) to the database schema. Both -** provide read/write access to the contents of *pHash. -** -** The third argument to this function, zName, is used as the name -** of both the scalar and, if created, the virtual table. -*/ -int sqlite3Fts3InitHashTable( - sqlite3 *db, - fts3Hash *pHash, - const char *zName -){ - int rc = SQLITE_OK; - void *p = (void *)pHash; - const int any = SQLITE_ANY; - char *zTest = 0; - char *zTest2 = 0; - -#ifdef SQLITE_TEST - void *pdb = (void *)db; - zTest = sqlite3_mprintf("%s_test", zName); - zTest2 = sqlite3_mprintf("%s_internal_test", zName); - if( !zTest || !zTest2 ){ - rc = SQLITE_NOMEM; - } -#endif - - if( rc!=SQLITE_OK - || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0)) - || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0)) -#ifdef SQLITE_TEST - || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0)) - || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0)) - || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0)) -#endif - ); - - sqlite3_free(zTest); - sqlite3_free(zTest2); - return rc; -} - -#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ - -/************** End of fts3_tokenizer.c **************************************/ -/************** Begin file fts3_tokenizer1.c *********************************/ -/* -** 2006 Oct 10 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** Implementation of the "simple" full-text-search tokenizer. -*/ - -/* -** The code in this file is only compiled if: -** -** * The FTS3 module is being built as an extension -** (in which case SQLITE_CORE is not defined), or -** -** * The FTS3 module is being built into the core of -** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). -*/ -#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) - - - -/************** Include fts3_tokenizer.h in the middle of fts3_tokenizer1.c **/ -/************** Begin file fts3_tokenizer.h **********************************/ -/* -** 2006 July 10 -** -** The author disclaims copyright to this source code. -** -************************************************************************* -** Defines the interface to tokenizers used by fulltext-search. There -** are three basic components: -** -** sqlite3_tokenizer_module is a singleton defining the tokenizer -** interface functions. This is essentially the class structure for -** tokenizers. -** -** sqlite3_tokenizer is used to define a particular tokenizer, perhaps -** including customization information defined at creation time. -** -** sqlite3_tokenizer_cursor is generated by a tokenizer to generate -** tokens from a particular input. -*/ -#ifndef _FTS3_TOKENIZER_H_ -#define _FTS3_TOKENIZER_H_ - -/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. -** If tokenizers are to be allowed to call sqlite3_*() functions, then -** we will need a way to register the API consistently. -*/ -/************** Include sqlite3.h in the middle of fts3_tokenizer.h **********/ -/************** Begin file sqlite3.h *****************************************/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the interface that the SQLite library -** presents to client programs. If a C-function, structure, datatype, -** or constant definition does not appear in this file, then it is -** not a published API of SQLite, is subject to change without -** notice, and should not be referenced by programs that use SQLite. -** -** Some of the definitions that are in this file are marked as -** "experimental". Experimental interfaces are normally new -** features recently added to SQLite. We do not anticipate changes -** to experimental interfaces but reserve to make minor changes if -** experience from use "in the wild" suggest such changes are prudent. -** -** The official C-language API documentation for SQLite is derived -** from comments in this file. This file is the authoritative source -** on how SQLite interfaces are suppose to operate. -** -** The name of this file under configuration management is "sqlite.h.in". -** The makefile makes some minor changes to this file (such as inserting -** the version number) and changes its name to "sqlite3.h" as -** part of the build process. -** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ -*/ -#ifndef _SQLITE3_H_ -#define _SQLITE3_H_ - -/* -** Make sure we can call this stuff from C++. -*/ -#if 0 -extern "C" { -#endif - - -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif - -/* -** Make sure these symbols where not defined by some previous header -** file. -*/ -#ifdef SQLITE_VERSION -# undef SQLITE_VERSION -#endif -#ifdef SQLITE_VERSION_NUMBER -# undef SQLITE_VERSION_NUMBER -#endif - -/* -** CAPI3REF: Compile-Time Library Version Numbers {F10010} -** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when -** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with -** each release but resets back to 0 when Y is incremented. -** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 - -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); - -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); - -/* -** CAPI3REF: Database Connection Handle {F12000} -** -** Each open SQLite database is represented by pointer to an instance of the -** opaque structure named "sqlite3". It is useful to think of an sqlite3 -** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and -** [sqlite3_open_v2()] interfaces are its constructors -** and [sqlite3_close()] is its destructor. There are many other interfaces -** (such as [sqlite3_prepare_v2()], [sqlite3_create_function()], and -** [sqlite3_busy_timeout()] to name but three) that are methods on this -** object. -*/ -typedef struct sqlite3 sqlite3; - - -/* -** CAPI3REF: 64-Bit Integer Types {F10200} -** -** Because there is no cross-platform way to specify such types -** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} -** -** The sqlite3_int64 and sqlite3_uint64 are the preferred type -** definitions. The sqlite_int64 and sqlite_uint64 types are -** supported for backwards compatibility only. -*/ -#ifdef SQLITE_INT64_TYPE - typedef SQLITE_INT64_TYPE sqlite_int64; - typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; -#elif defined(_MSC_VER) || defined(__BORLANDC__) - typedef __int64 sqlite_int64; - typedef unsigned __int64 sqlite_uint64; -#else - typedef long long int sqlite_int64; - typedef unsigned long long int sqlite_uint64; -#endif -typedef sqlite_int64 sqlite3_int64; -typedef sqlite_uint64 sqlite3_uint64; - -/* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif - -/* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. -*/ -int sqlite3_close(sqlite3 *); - -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. -*/ -typedef int (*sqlite3_callback)(void*,int,char**, char**); - -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); - -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] -** -*/ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ - -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) - -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 - -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 - -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} -*/ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 - -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). -*/ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 +static int fulltextQuery( + fulltext_vtab *v, /* The full text index */ + int iColumn, /* Match against this column by default */ + const char *zInput, /* The query string */ + int nInput, /* Number of bytes in zInput[] */ + DataBuffer *pResult, /* Write the result doclist here */ + Query *pQuery /* Put parsed query string here */ +){ + int i, iNext, rc; + DataBuffer left, right, or, new; + int nNot = 0; + QueryTerm *aTerm; + /* TODO(shess) Instead of flushing pendingTerms, we could query for + ** the relevant term and merge the doclist into what we receive from + ** the database. Wait and see if this is a common issue, first. + ** + ** A good reason not to flush is to not generate update-related + ** error codes from here. + */ -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; + /* Flush any buffered updates before executing the query. */ + rc = flushPendingTerms(v); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; + /* TODO(shess) I think that the queryClear() calls below are not + ** necessary, because fulltextClose() already clears the query. + */ + rc = parseQuery(v, zInput, nInput, iColumn, pQuery); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 + /* Empty or NULL queries return no results. */ + if( pQuery->nTerms==0 ){ + dataBufferInit(pResult, 0); + return SQLITE_OK; + } -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. -*/ -typedef struct sqlite3_mutex sqlite3_mutex; + /* Merge AND terms. */ + /* TODO(shess) I think we can early-exit if( i>nNot && left.nData==0 ). */ + aTerm = pQuery->pTerms; + for(i = 0; inTerms; i=iNext){ + if( aTerm[i].isNot ){ + /* Handle all NOT terms in a separate pass */ + nNot++; + iNext = i + aTerm[i].nPhrase+1; + continue; + } + iNext = i + aTerm[i].nPhrase + 1; + rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &right); + if( rc ){ + if( i!=nNot ) dataBufferDestroy(&left); + queryClear(pQuery); + return rc; + } + while( iNextnTerms && aTerm[iNext].isOr ){ + rc = docListOfTerm(v, aTerm[iNext].iColumn, &aTerm[iNext], &or); + iNext += aTerm[iNext].nPhrase + 1; + if( rc ){ + if( i!=nNot ) dataBufferDestroy(&left); + dataBufferDestroy(&right); + queryClear(pQuery); + return rc; + } + dataBufferInit(&new, 0); + docListOrMerge(right.pData, right.nData, or.pData, or.nData, &new); + dataBufferDestroy(&right); + dataBufferDestroy(&or); + right = new; + } + if( i==nNot ){ /* first term processed. */ + left = right; + }else{ + dataBufferInit(&new, 0); + docListAndMerge(left.pData, left.nData, right.pData, right.nData, &new); + dataBufferDestroy(&right); + dataBufferDestroy(&left); + left = new; + } + } -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; + if( nNot==pQuery->nTerms ){ + /* We do not yet know how to handle a query of only NOT terms */ + return SQLITE_ERROR; + } -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 + /* Do the EXCEPT terms */ + for(i=0; inTerms; i += aTerm[i].nPhrase + 1){ + if( !aTerm[i].isNot ) continue; + rc = docListOfTerm(v, aTerm[i].iColumn, &aTerm[i], &right); + if( rc ){ + queryClear(pQuery); + dataBufferDestroy(&left); + return rc; + } + dataBufferInit(&new, 0); + docListExceptMerge(left.pData, left.nData, right.pData, right.nData, &new); + dataBufferDestroy(&right); + dataBufferDestroy(&left); + left = new; + } -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); + *pResult = left; + return rc; +} /* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. +** This is the xFilter interface for the virtual table. See +** the virtual table xFilter method documentation for additional +** information. ** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. +** If idxNum==QUERY_GENERIC then do a full table scan against +** the %_content table. ** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. +** If idxNum==QUERY_DOCID then do a docid lookup for a single entry +** in the %_content table. ** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. +** If idxNum>=QUERY_FULLTEXT then use the full text index. The +** column on the left-hand side of the MATCH operator is column +** number idxNum-QUERY_FULLTEXT, 0 indexed. argv[0] is the right-hand +** side of the MATCH operator. */ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); - -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. +/* TODO(shess) Upgrade the cursor initialization and destruction to +** account for fulltextFilter() being called multiple times on the +** same cursor. The current solution is very fragile. Apply fix to +** fts3 as appropriate. */ -int sqlite3_changes(sqlite3*); +static int fulltextFilter( + sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ + int idxNum, const char *idxStr, /* Which indexing scheme to use */ + int argc, sqlite3_value **argv /* Arguments for the indexing scheme */ +){ + fulltext_cursor *c = (fulltext_cursor *) pCursor; + fulltext_vtab *v = cursor_vtab(c); + int rc; + StringBuffer sb; -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} -*/ -int sqlite3_total_changes(sqlite3*); + FTSTRACE(("FTS3 Filter %p\n",pCursor)); -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. + initStringBuffer(&sb); + append(&sb, "SELECT docid, "); + appendList(&sb, v->nColumn, v->azContentColumn); + append(&sb, " FROM %_content"); + if( idxNum!=QUERY_GENERIC ) append(&sb, " WHERE docid = ?"); + sqlite3_finalize(c->pStmt); + rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, stringBufferData(&sb)); + stringBufferDestroy(&sb); + if( rc!=SQLITE_OK ) return rc; + + c->iCursorType = idxNum; + switch( idxNum ){ + case QUERY_GENERIC: + break; + + case QUERY_DOCID: + rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0])); + if( rc!=SQLITE_OK ) return rc; + break; + + default: /* full-text search */ + { + const char *zQuery = (const char *)sqlite3_value_text(argv[0]); + assert( idxNum<=QUERY_FULLTEXT+v->nColumn); + assert( argc==1 ); + queryClear(&c->q); + if( c->result.nData!=0 ){ + /* This case happens if the same cursor is used repeatedly. */ + dlrDestroy(&c->reader); + dataBufferReset(&c->result); + }else{ + dataBufferInit(&c->result, 0); + } + rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &c->result, &c->q); + if( rc!=SQLITE_OK ) return rc; + if( c->result.nData!=0 ){ + dlrInit(&c->reader, DL_DOCIDS, c->result.pData, c->result.nData); + } + break; + } + } + + return fulltextNext(pCursor); +} + +/* This is the xEof method of the virtual table. The SQLite core +** calls this routine to find out if it has reached the end of +** a query's results set. */ -void sqlite3_interrupt(sqlite3*); +static int fulltextEof(sqlite3_vtab_cursor *pCursor){ + fulltext_cursor *c = (fulltext_cursor *) pCursor; + return c->eof; +} -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} +/* This is the xColumn method of the virtual table. The SQLite +** core calls this method during a query when it needs the value +** of a column from the virtual table. This method needs to use +** one of the sqlite3_result_*() routines to store the requested +** value back in the pContext. */ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); +static int fulltextColumn(sqlite3_vtab_cursor *pCursor, + sqlite3_context *pContext, int idxCol){ + fulltext_cursor *c = (fulltext_cursor *) pCursor; + fulltext_vtab *v = cursor_vtab(c); -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. + if( idxColnColumn ){ + sqlite3_value *pVal = sqlite3_column_value(c->pStmt, idxCol+1); + sqlite3_result_value(pContext, pVal); + }else if( idxCol==v->nColumn ){ + /* The extra column whose name is the same as the table. + ** Return a blob which is a pointer to the cursor + */ + sqlite3_result_blob(pContext, &c, sizeof(c), SQLITE_TRANSIENT); + }else if( idxCol==v->nColumn+1 ){ + /* The docid column, which is an alias for rowid. */ + sqlite3_value *pVal = sqlite3_column_value(c->pStmt, 0); + sqlite3_result_value(pContext, pVal); + } + return SQLITE_OK; +} + +/* This is the xRowid method. The SQLite core calls this routine to +** retrieve the rowid for the current row of the result set. fts3 +** exposes %_content.docid as the rowid for the virtual table. The +** rowid should be written to *pRowid. */ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); +static int fulltextRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ + fulltext_cursor *c = (fulltext_cursor *) pCursor; -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. + *pRowid = sqlite3_column_int64(c->pStmt, 0); + return SQLITE_OK; +} + +/* Add all terms in [zText] to pendingTerms table. If [iColumn] > 0, +** we also store positions and offsets in the hash table using that +** column number. */ -int sqlite3_busy_timeout(sqlite3*, int ms); +static int buildTerms(fulltext_vtab *v, sqlite_int64 iDocid, + const char *zText, int iColumn){ + sqlite3_tokenizer *pTokenizer = v->pTokenizer; + sqlite3_tokenizer_cursor *pCursor; + const char *pToken; + int nTokenBytes; + int iStartOffset, iEndOffset, iPosition; + int rc; -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); + rc = pTokenizer->pModule->xOpen(pTokenizer, zText, -1, &pCursor); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} + pCursor->pTokenizer = pTokenizer; + while( SQLITE_OK==(rc=pTokenizer->pModule->xNext(pCursor, + &pToken, &nTokenBytes, + &iStartOffset, &iEndOffset, + &iPosition)) ){ + DLCollector *p; + int nData; /* Size of doclist before our update. */ + + /* Positions can't be negative; we use -1 as a terminator + * internally. Token can't be NULL or empty. */ + if( iPosition<0 || pToken == NULL || nTokenBytes == 0 ){ + rc = SQLITE_ERROR; + break; + } + + p = fts3HashFind(&v->pendingTerms, pToken, nTokenBytes); + if( p==NULL ){ + nData = 0; + p = dlcNew(iDocid, DL_DEFAULT); + fts3HashInsert(&v->pendingTerms, pToken, nTokenBytes, p); + + /* Overhead for our hash table entry, the key, and the value. */ + v->nPendingData += sizeof(struct fts3HashElem)+sizeof(*p)+nTokenBytes; + }else{ + nData = p->b.nData; + if( p->dlw.iPrevDocid!=iDocid ) dlcNext(p, iDocid); + } + if( iColumn>=0 ){ + dlcAddPos(p, iColumn, iPosition, iStartOffset, iEndOffset); + } + + /* Accumulate data added by dlcNew or dlcNext, and dlcAddPos. */ + v->nPendingData += p->b.nData-nData; + } + + /* TODO(shess) Check return? Should this be able to cause errors at + ** this point? Actually, same question about sqlite3_finalize(), + ** though one could argue that failure there means that the data is + ** not durable. *ponder* + */ + pTokenizer->pModule->xClose(pCursor); + if( SQLITE_DONE == rc ) return SQLITE_OK; + return rc; +} + +/* Add doclists for all terms in [pValues] to pendingTerms table. */ +static int insertTerms(fulltext_vtab *v, sqlite_int64 iDocid, + sqlite3_value **pValues){ + int i; + for(i = 0; i < v->nColumn ; ++i){ + char *zText = (char*)sqlite3_value_text(pValues[i]); + int rc = buildTerms(v, iDocid, zText, i); + if( rc!=SQLITE_OK ) return rc; + } + return SQLITE_OK; +} + +/* Add empty doclists for all terms in the given row's content to +** pendingTerms. */ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); +static int deleteTerms(fulltext_vtab *v, sqlite_int64 iDocid){ + const char **pValues; + int i, rc; -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. + /* TODO(shess) Should we allow such tables at all? */ + if( DL_DEFAULT==DL_DOCIDS ) return SQLITE_ERROR; + + rc = content_select(v, iDocid, &pValues); + if( rc!=SQLITE_OK ) return rc; + + for(i = 0 ; i < v->nColumn; ++i) { + rc = buildTerms(v, iDocid, pValues[i], -1); + if( rc!=SQLITE_OK ) break; + } + + freeStringArray(v->nColumn, pValues); + return SQLITE_OK; +} + +/* TODO(shess) Refactor the code to remove this forward decl. */ +static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid); + +/* Insert a row into the %_content table; set *piDocid to be the ID of the +** new row. Add doclists for terms to pendingTerms. */ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); +static int index_insert(fulltext_vtab *v, sqlite3_value *pRequestDocid, + sqlite3_value **pValues, sqlite_int64 *piDocid){ + int rc; + + rc = content_insert(v, pRequestDocid, pValues); /* execute an SQL INSERT */ + if( rc!=SQLITE_OK ) return rc; + + /* docid column is an alias for rowid. */ + *piDocid = sqlite3_last_insert_rowid(v->db); + rc = initPendingTerms(v, *piDocid); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); + return insertTerms(v, *piDocid, pValues); +} -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} +/* Delete a row from the %_content table; add empty doclists for terms +** to pendingTerms. */ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); +static int index_delete(fulltext_vtab *v, sqlite_int64 iRow){ + int rc = initPendingTerms(v, iRow); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. + rc = deleteTerms(v, iRow); + if( rc!=SQLITE_OK ) return rc; + + return content_delete(v, iRow); /* execute an SQL DELETE */ +} + +/* Update a row in the %_content table; add delete doclists to +** pendingTerms for old terms not in the new data, add insert doclists +** to pendingTerms for terms in the new data. */ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ +static int index_update(fulltext_vtab *v, sqlite_int64 iRow, + sqlite3_value **pValues){ + int rc = initPendingTerms(v, iRow); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. + /* Generate an empty doclist for each term that previously appeared in this + * row. */ + rc = deleteTerms(v, iRow); + if( rc!=SQLITE_OK ) return rc; + + rc = content_update(v, pValues, iRow); /* execute an SQL UPDATE */ + if( rc!=SQLITE_OK ) return rc; + + /* Now add positions for terms which appear in the updated row. */ + return insertTerms(v, iRow, pValues); +} + +/*******************************************************************/ +/* InteriorWriter is used to collect terms and block references into +** interior nodes in %_segments. See commentary at top of file for +** format. */ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. +/* How large interior nodes can grow. */ +#define INTERIOR_MAX 2048 + +/* Minimum number of terms per interior node (except the root). This +** prevents large terms from making the tree too skinny - must be >0 +** so that the tree always makes progress. Note that the min tree +** fanout will be INTERIOR_MIN_TERMS+1. */ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); +#define INTERIOR_MIN_TERMS 7 +#if INTERIOR_MIN_TERMS<1 +# error INTERIOR_MIN_TERMS must be greater than 0. +#endif -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. +/* ROOT_MAX controls how much data is stored inline in the segment +** directory. +*/ +/* TODO(shess) Push ROOT_MAX down to whoever is writing things. It's +** only here so that interiorWriterRootInfo() and leafWriterRootInfo() +** can both see it, but if the caller passed it in, we wouldn't even +** need a define. */ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); +#define ROOT_MAX 1024 +#if ROOT_MAX -**
  • [SQLITE_OPEN_READONLY] -**
  • [SQLITE_OPEN_READWRITE] -**
  • [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -** -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. +/* InteriorBlock stores a linked-list of interior blocks while a lower +** layer is being constructed. */ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); +typedef struct InteriorBlock { + DataBuffer term; /* Leftmost term in block's subtree. */ + DataBuffer data; /* Accumulated data for the block. */ + struct InteriorBlock *next; +} InteriorBlock; -/* -** CAPI3REF: Error Codes And Messages {F12800} -** -** {F12801} The sqlite3_errcode() interface returns the numeric -** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] -** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the -** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} -** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language -** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. -*/ -int sqlite3_errcode(sqlite3 *db); -const char *sqlite3_errmsg(sqlite3*); -const void *sqlite3_errmsg16(sqlite3*); +static InteriorBlock *interiorBlockNew(int iHeight, sqlite_int64 iChildBlock, + const char *pTerm, int nTerm){ + InteriorBlock *block = sqlite3_malloc(sizeof(InteriorBlock)); + char c[VARINT_MAX+VARINT_MAX]; + int n; -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. + if( block ){ + memset(block, 0, sizeof(*block)); + dataBufferInit(&block->term, 0); + dataBufferReplace(&block->term, pTerm, nTerm); + + n = fts3PutVarint(c, iHeight); + n += fts3PutVarint(c+n, iChildBlock); + dataBufferInit(&block->data, INTERIOR_MAX); + dataBufferReplace(&block->data, c, n); + } + return block; +} + +#ifndef NDEBUG +/* Verify that the data is readable as an interior node. */ +static void interiorBlockValidate(InteriorBlock *pBlock){ + const char *pData = pBlock->data.pData; + int nData = pBlock->data.nData; + int n, iDummy; + sqlite_int64 iBlockid; + + assert( nData>0 ); + assert( pData!=0 ); + assert( pData+nData>pData ); + + /* Must lead with height of node as a varint(n), n>0 */ + n = fts3GetVarint32(pData, &iDummy); + assert( n>0 ); + assert( iDummy>0 ); + assert( n0 ); + assert( n<=nData ); + pData += n; + nData -= n; + + /* Zero or more terms of positive length */ + if( nData!=0 ){ + /* First term is not delta-encoded. */ + n = fts3GetVarint32(pData, &iDummy); + assert( n>0 ); + assert( iDummy>0 ); + assert( n+iDummy>0); + assert( n+iDummy<=nData ); + pData += n+iDummy; + nData -= n+iDummy; + + /* Following terms delta-encoded. */ + while( nData!=0 ){ + /* Length of shared prefix. */ + n = fts3GetVarint32(pData, &iDummy); + assert( n>0 ); + assert( iDummy>=0 ); + assert( n0 ); + assert( iDummy>0 ); + assert( n+iDummy>0); + assert( n+iDummy<=nData ); + pData += n+iDummy; + nData -= n+iDummy; + } + } +} +#define ASSERT_VALID_INTERIOR_BLOCK(x) interiorBlockValidate(x) +#else +#define ASSERT_VALID_INTERIOR_BLOCK(x) assert( 1 ) +#endif + +typedef struct InteriorWriter { + int iHeight; /* from 0 at leaves. */ + InteriorBlock *first, *last; + struct InteriorWriter *parentWriter; + + DataBuffer term; /* Last term written to block "last". */ + sqlite_int64 iOpeningChildBlock; /* First child block in block "last". */ +#ifndef NDEBUG + sqlite_int64 iLastChildBlock; /* for consistency checks. */ +#endif +} InteriorWriter; + +/* Initialize an interior node where pTerm[nTerm] marks the leftmost +** term in the tree. iChildBlock is the leftmost child block at the +** next level down the tree. */ -typedef struct sqlite3_stmt sqlite3_stmt; +static void interiorWriterInit(int iHeight, const char *pTerm, int nTerm, + sqlite_int64 iChildBlock, + InteriorWriter *pWriter){ + InteriorBlock *block; + assert( iHeight>0 ); + CLEAR(pWriter); -/* -** CAPI3REF: Compiling An SQL Statement {F13010} -** -** To execute an SQL query, it must first be compiled into a byte-code -** program using one of these routines. -** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] -** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} -** The second argument "zSql" is the statement to be compiled, encoded -** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() -** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() -** use UTF-16. {END} -** -** {F13013} If the nByte argument is less -** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of -** bytes read from zSql. When nByte is non-negative, the -** zSql string ends at either the first '\000' or '\u0000' character or -** until the nByte-th byte, whichever comes first. {END} -** -** {F13015} *pzTail is made to point to the first byte past the end of the -** first SQL statement in zSql. These routines only compiles the first -** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} -** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input -** is and empty string or a comment) then *ppStmt is set to NULL. -** {U13018} The calling procedure is responsible for deleting the -** compiled SQL statement -** using [sqlite3_finalize()] after it has finished with it. -** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} -** -** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are -** recommended for all new programs. The two older interfaces are retained -** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement -** that is returned (the [sqlite3_stmt] object) contains a copy of the -** original SQL text. {END} This causes the [sqlite3_step()] interface to -** behave a differently in two ways: -** -**
      -**
    1. {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
    2. -** -**
    3. -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
    4. -**
    + pWriter->iHeight = iHeight; + pWriter->iOpeningChildBlock = iChildBlock; +#ifndef NDEBUG + pWriter->iLastChildBlock = iChildBlock; +#endif + block = interiorBlockNew(iHeight, iChildBlock, pTerm, nTerm); + pWriter->last = pWriter->first = block; + ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); + dataBufferInit(&pWriter->term, 0); +} + +/* Append the child node rooted at iChildBlock to the interior node, +** with pTerm[nTerm] as the leftmost term in iChildBlock's subtree. */ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); +static void interiorWriterAppend(InteriorWriter *pWriter, + const char *pTerm, int nTerm, + sqlite_int64 iChildBlock){ + char c[VARINT_MAX+VARINT_MAX]; + int n, nPrefix = 0; + + ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); + + /* The first term written into an interior node is actually + ** associated with the second child added (the first child was added + ** in interiorWriterInit, or in the if clause at the bottom of this + ** function). That term gets encoded straight up, with nPrefix left + ** at 0. + */ + if( pWriter->term.nData==0 ){ + n = fts3PutVarint(c, nTerm); + }else{ + while( nPrefixterm.nData && + pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){ + nPrefix++; + } + + n = fts3PutVarint(c, nPrefix); + n += fts3PutVarint(c+n, nTerm-nPrefix); + } + +#ifndef NDEBUG + pWriter->iLastChildBlock++; +#endif + assert( pWriter->iLastChildBlock==iChildBlock ); + + /* Overflow to a new block if the new term makes the current block + ** too big, and the current block already has enough terms. + */ + if( pWriter->last->data.nData+n+nTerm-nPrefix>INTERIOR_MAX && + iChildBlock-pWriter->iOpeningChildBlock>INTERIOR_MIN_TERMS ){ + pWriter->last->next = interiorBlockNew(pWriter->iHeight, iChildBlock, + pTerm, nTerm); + pWriter->last = pWriter->last->next; + pWriter->iOpeningChildBlock = iChildBlock; + dataBufferReset(&pWriter->term); + }else{ + dataBufferAppend2(&pWriter->last->data, c, n, + pTerm+nPrefix, nTerm-nPrefix); + dataBufferReplace(&pWriter->term, pTerm, nTerm); + } + ASSERT_VALID_INTERIOR_BLOCK(pWriter->last); +} -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. +/* Free the space used by pWriter, including the linked-list of +** InteriorBlocks, and parentWriter, if present. */ -const char *sqlite3_sql(sqlite3_stmt *pStmt); +static int interiorWriterDestroy(InteriorWriter *pWriter){ + InteriorBlock *block = pWriter->first; -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. -*/ -typedef struct Mem sqlite3_value; + while( block!=NULL ){ + InteriorBlock *b = block; + block = block->next; + dataBufferDestroy(&b->term); + dataBufferDestroy(&b->data); + sqlite3_free(b); + } + if( pWriter->parentWriter!=NULL ){ + interiorWriterDestroy(pWriter->parentWriter); + sqlite3_free(pWriter->parentWriter); + } + dataBufferDestroy(&pWriter->term); + SCRAMBLE(pWriter); + return SQLITE_OK; +} -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. +/* If pWriter can fit entirely in ROOT_MAX, return it as the root info +** directly, leaving *piEndBlockid unchanged. Otherwise, flush +** pWriter to %_segments, building a new layer of interior nodes, and +** recursively ask for their root into. */ -typedef struct sqlite3_context sqlite3_context; +static int interiorWriterRootInfo(fulltext_vtab *v, InteriorWriter *pWriter, + char **ppRootInfo, int *pnRootInfo, + sqlite_int64 *piEndBlockid){ + InteriorBlock *block = pWriter->first; + sqlite_int64 iBlockid = 0; + int rc; -/* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. -*/ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); + /* If we can fit the segment inline */ + if( block==pWriter->last && block->data.nDatadata.pData; + *pnRootInfo = block->data.nData; + return SQLITE_OK; + } -/* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. -*/ -int sqlite3_bind_parameter_count(sqlite3_stmt*); + /* Flush the first block to %_segments, and create a new level of + ** interior node. + */ + ASSERT_VALID_INTERIOR_BLOCK(block); + rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid); + if( rc!=SQLITE_OK ) return rc; + *piEndBlockid = iBlockid; -/* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. -*/ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); + pWriter->parentWriter = sqlite3_malloc(sizeof(*pWriter->parentWriter)); + interiorWriterInit(pWriter->iHeight+1, + block->term.pData, block->term.nData, + iBlockid, pWriter->parentWriter); -/* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. -*/ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); + /* Flush additional blocks and append to the higher interior + ** node. + */ + for(block=block->next; block!=NULL; block=block->next){ + ASSERT_VALID_INTERIOR_BLOCK(block); + rc = block_insert(v, block->data.pData, block->data.nData, &iBlockid); + if( rc!=SQLITE_OK ) return rc; + *piEndBlockid = iBlockid; -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); + interiorWriterAppend(pWriter->parentWriter, + block->term.pData, block->term.nData, iBlockid); + } -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); + /* Parent node gets the chance to be the root. */ + return interiorWriterRootInfo(v, pWriter->parentWriter, + ppRootInfo, pnRootInfo, piEndBlockid); +} -/* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. -** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. +/****************************************************************/ +/* InteriorReader is used to read off the data from an interior node +** (see comment at top of file for the format). */ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); +typedef struct InteriorReader { + const char *pData; + int nData; -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); + DataBuffer term; /* previous term, for decoding term delta. */ -/* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. -*/ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); + sqlite_int64 iBlockid; +} InteriorReader; -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); +static void interiorReaderDestroy(InteriorReader *pReader){ + dataBufferDestroy(&pReader->term); + SCRAMBLE(pReader); +} -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. +/* TODO(shess) The assertions are great, but what if we're in NDEBUG +** and the blob is empty or otherwise contains suspect data? */ -int sqlite3_data_count(sqlite3_stmt *pStmt); +static void interiorReaderInit(const char *pData, int nData, + InteriorReader *pReader){ + int n, nTerm; -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 + /* Require at least the leading flag byte */ + assert( nData>0 ); + assert( pData[0]!='\0' ); -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); + CLEAR(pReader); + + /* Decode the base blockid, and set the cursor to the first term. */ + n = fts3GetVarint(pData+1, &pReader->iBlockid); + assert( 1+n<=nData ); + pReader->pData = pData+1+n; + pReader->nData = nData-(1+n); + + /* A single-child interior node (such as when a leaf node was too + ** large for the segment directory) won't have any terms. + ** Otherwise, decode the first term. + */ + if( pReader->nData==0 ){ + dataBufferInit(&pReader->term, 0); + }else{ + n = fts3GetVarint32(pReader->pData, &nTerm); + dataBufferInit(&pReader->term, nTerm); + dataBufferReplace(&pReader->term, pReader->pData+n, nTerm); + assert( n+nTerm<=pReader->nData ); + pReader->pData += n+nTerm; + pReader->nData -= n+nTerm; + } +} + +static int interiorReaderAtEnd(InteriorReader *pReader){ + return pReader->term.nData==0; +} + +static sqlite_int64 interiorReaderCurrentBlockid(InteriorReader *pReader){ + return pReader->iBlockid; +} -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); +static int interiorReaderTermBytes(InteriorReader *pReader){ + assert( !interiorReaderAtEnd(pReader) ); + return pReader->term.nData; +} +static const char *interiorReaderTerm(InteriorReader *pReader){ + assert( !interiorReaderAtEnd(pReader) ); + return pReader->term.pData; +} -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); +/* Step forward to the next term in the node. */ +static void interiorReaderStep(InteriorReader *pReader){ + assert( !interiorReaderAtEnd(pReader) ); -/* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. -** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. -** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. -** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. -*/ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); + /* If the last term has been read, signal eof, else construct the + ** next term. + */ + if( pReader->nData==0 ){ + dataBufferReset(&pReader->term); + }else{ + int n, nPrefix, nSuffix; -/* -** CAPI3REF: Text Encodings {F10267} -** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. -*/ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ + n = fts3GetVarint32(pReader->pData, &nPrefix); + n += fts3GetVarint32(pReader->pData+n, &nSuffix); -/* -** CAPI3REF: Obsolete Functions -** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. -*/ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); + /* Truncate the current term and append suffix data. */ + pReader->term.nData = nPrefix; + dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); -/* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** -*/ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); + assert( n+nSuffix<=pReader->nData ); + pReader->pData += n+nSuffix; + pReader->nData -= n+nSuffix; + } + pReader->iBlockid++; +} -/* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. +/* Compare the current term to pTerm[nTerm], returning strcmp-style +** results. If isPrefix, equality means equal through nTerm bytes. */ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); +static int interiorReaderTermCmp(InteriorReader *pReader, + const char *pTerm, int nTerm, int isPrefix){ + const char *pReaderTerm = interiorReaderTerm(pReader); + int nReaderTerm = interiorReaderTermBytes(pReader); + int c, n = nReaderTerm0 ) return -1; + if( nTerm>0 ) return 1; + return 0; + } -/* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. + c = memcmp(pReaderTerm, pTerm, n); + if( c!=0 ) return c; + if( isPrefix && n==nTerm ) return 0; + return nReaderTerm - nTerm; +} + +/****************************************************************/ +/* LeafWriter is used to collect terms and associated doclist data +** into leaf blocks in %_segments (see top of file for format info). +** Expected usage is: ** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} +** LeafWriter writer; +** leafWriterInit(0, 0, &writer); +** while( sorted_terms_left_to_process ){ +** // data is doclist data for that term. +** rc = leafWriterStep(v, &writer, pTerm, nTerm, pData, nData); +** if( rc!=SQLITE_OK ) goto err; +** } +** rc = leafWriterFinalize(v, &writer); +**err: +** leafWriterDestroy(&writer); +** return rc; ** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. +** leafWriterStep() may write a collected leaf out to %_segments. +** leafWriterFinalize() finishes writing any buffered data and stores +** a root node in %_segdir. leafWriterDestroy() frees all buffers and +** InteriorWriters allocated as part of writing this segment. ** -** These routines must be called from the same thread in which -** the SQL function is running. +** TODO(shess) Document leafWriterStepMerge(). */ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); +/* Put terms with data this big in their own block. */ +#define STANDALONE_MIN 1024 -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. -*/ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) +/* Keep leaf blocks below this size. */ +#define LEAF_MAX 2048 -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. -*/ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); +typedef struct LeafWriter { + int iLevel; + int idx; + sqlite_int64 iStartBlockid; /* needed to create the root info */ + sqlite_int64 iEndBlockid; /* when we're done writing. */ -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); + DataBuffer term; /* previous encoded term */ + DataBuffer data; /* encoding buffer */ + + /* bytes of first term in the current node which distinguishes that + ** term from the last term of the previous node. + */ + int nTermDistinct; + + InteriorWriter parentWriter; /* if we overflow */ + int has_parent; +} LeafWriter; + +static void leafWriterInit(int iLevel, int idx, LeafWriter *pWriter){ + CLEAR(pWriter); + pWriter->iLevel = iLevel; + pWriter->idx = idx; + + dataBufferInit(&pWriter->term, 32); + + /* Start out with a reasonably sized block, though it can grow. */ + dataBufferInit(&pWriter->data, LEAF_MAX); +} + +#ifndef NDEBUG +/* Verify that the data is readable as a leaf node. */ +static void leafNodeValidate(const char *pData, int nData){ + int n, iDummy; + + if( nData==0 ) return; + assert( nData>0 ); + assert( pData!=0 ); + assert( pData+nData>pData ); + + /* Must lead with a varint(0) */ + n = fts3GetVarint32(pData, &iDummy); + assert( iDummy==0 ); + assert( n>0 ); + assert( n0 ); + assert( iDummy>0 ); + assert( n+iDummy>0 ); + assert( n+iDummy0 ); + assert( iDummy>0 ); + assert( n+iDummy>0 ); + assert( n+iDummy<=nData ); + ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL); + pData += n+iDummy; + nData -= n+iDummy; + + /* Verify that trailing terms and doclists also are readable. */ + while( nData!=0 ){ + n = fts3GetVarint32(pData, &iDummy); + assert( n>0 ); + assert( iDummy>=0 ); + assert( n0 ); + assert( iDummy>0 ); + assert( n+iDummy>0 ); + assert( n+iDummy0 ); + assert( iDummy>0 ); + assert( n+iDummy>0 ); + assert( n+iDummy<=nData ); + ASSERT_VALID_DOCLIST(DL_DEFAULT, pData+n, iDummy, NULL); + pData += n+iDummy; + nData -= n+iDummy; + } +} +#define ASSERT_VALID_LEAF_NODE(p, n) leafNodeValidate(p, n) +#else +#define ASSERT_VALID_LEAF_NODE(p, n) assert( 1 ) +#endif -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. +/* Flush the current leaf node to %_segments, and adding the resulting +** blockid and the starting term to the interior node which will +** contain it. */ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); +static int leafWriterInternalFlush(fulltext_vtab *v, LeafWriter *pWriter, + int iData, int nData){ + sqlite_int64 iBlockid = 0; + const char *pStartingTerm; + int nStartingTerm, rc, n; -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); + /* Must have the leading varint(0) flag, plus at least some + ** valid-looking data. + */ + assert( nData>2 ); + assert( iData>=0 ); + assert( iData+nData<=pWriter->data.nData ); + ASSERT_VALID_LEAF_NODE(pWriter->data.pData+iData, nData); -/* -** CAPI3REF: Suspend Execution For A Short Time {F10530} -** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. -** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. -** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} -*/ -int sqlite3_sleep(int); + rc = block_insert(v, pWriter->data.pData+iData, nData, &iBlockid); + if( rc!=SQLITE_OK ) return rc; + assert( iBlockid!=0 ); -/* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} -** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. -** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. -*/ -SQLITE_EXTERN char *sqlite3_temp_directory; + /* Reconstruct the first term in the leaf for purposes of building + ** the interior node. + */ + n = fts3GetVarint32(pWriter->data.pData+iData+1, &nStartingTerm); + pStartingTerm = pWriter->data.pData+iData+1+n; + assert( pWriter->data.nData>iData+1+n+nStartingTerm ); + assert( pWriter->nTermDistinct>0 ); + assert( pWriter->nTermDistinct<=nStartingTerm ); + nStartingTerm = pWriter->nTermDistinct; -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); + if( pWriter->has_parent ){ + interiorWriterAppend(&pWriter->parentWriter, + pStartingTerm, nStartingTerm, iBlockid); + }else{ + interiorWriterInit(1, pStartingTerm, nStartingTerm, iBlockid, + &pWriter->parentWriter); + pWriter->has_parent = 1; + } -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); + /* Track the span of this segment's leaf nodes. */ + if( pWriter->iEndBlockid==0 ){ + pWriter->iEndBlockid = pWriter->iStartBlockid = iBlockid; + }else{ + pWriter->iEndBlockid++; + assert( iBlockid==pWriter->iEndBlockid ); + } + return SQLITE_OK; +} +static int leafWriterFlush(fulltext_vtab *v, LeafWriter *pWriter){ + int rc = leafWriterInternalFlush(v, pWriter, 0, pWriter->data.nData); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. -*/ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); + /* Re-initialize the output buffer. */ + dataBufferReset(&pWriter->data); -/* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. -*/ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); + return SQLITE_OK; +} -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. +/* Fetch the root info for the segment. If the entire leaf fits +** within ROOT_MAX, then it will be returned directly, otherwise it +** will be flushed and the root info will be returned from the +** interior node. *piEndBlockid is set to the blockid of the last +** interior or leaf node written to disk (0 if none are written at +** all). */ -int sqlite3_enable_shared_cache(int); +static int leafWriterRootInfo(fulltext_vtab *v, LeafWriter *pWriter, + char **ppRootInfo, int *pnRootInfo, + sqlite_int64 *piEndBlockid){ + /* we can fit the segment entirely inline */ + if( !pWriter->has_parent && pWriter->data.nDatadata.pData; + *pnRootInfo = pWriter->data.nData; + *piEndBlockid = 0; + return SQLITE_OK; + } -/* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. -*/ -int sqlite3_release_memory(int); + /* Flush remaining leaf data. */ + if( pWriter->data.nData>0 ){ + int rc = leafWriterFlush(v, pWriter); + if( rc!=SQLITE_OK ) return rc; + } -/* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. -*/ -void sqlite3_soft_heap_limit(int); + /* We must have flushed a leaf at some point. */ + assert( pWriter->has_parent ); -/* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -*/ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); + /* Tenatively set the end leaf blockid as the end blockid. If the + ** interior node can be returned inline, this will be the final + ** blockid, otherwise it will be overwritten by + ** interiorWriterRootInfo(). + */ + *piEndBlockid = pWriter->iEndBlockid; -/* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. -*/ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); + return interiorWriterRootInfo(v, &pWriter->parentWriter, + ppRootInfo, pnRootInfo, piEndBlockid); +} -/* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} +/* Collect the rootInfo data and store it into the segment directory. +** This has the effect of flushing the segment's leaf data to +** %_segments, and also flushing any interior nodes to %_segments. */ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); +static int leafWriterFinalize(fulltext_vtab *v, LeafWriter *pWriter){ + sqlite_int64 iEndBlockid; + char *pRootInfo; + int rc, nRootInfo; -/* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} -** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} -** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. -** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. -** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. -** -** {F12644} Automatic extensions apply across all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. -*/ -int sqlite3_auto_extension(void *xEntryPoint); + rc = leafWriterRootInfo(v, pWriter, &pRootInfo, &nRootInfo, &iEndBlockid); + if( rc!=SQLITE_OK ) return rc; + /* Don't bother storing an entirely empty segment. */ + if( iEndBlockid==0 && nRootInfo==0 ) return SQLITE_OK; -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. + return segdir_set(v, pWriter->iLevel, pWriter->idx, + pWriter->iStartBlockid, pWriter->iEndBlockid, + iEndBlockid, pRootInfo, nRootInfo); +} + +static void leafWriterDestroy(LeafWriter *pWriter){ + if( pWriter->has_parent ) interiorWriterDestroy(&pWriter->parentWriter); + dataBufferDestroy(&pWriter->term); + dataBufferDestroy(&pWriter->data); +} + +/* Encode a term into the leafWriter, delta-encoding as appropriate. +** Returns the length of the new term which distinguishes it from the +** previous term, which can be used to set nTermDistinct when a node +** boundary is crossed. */ -void sqlite3_reset_auto_extension(void); +static int leafWriterEncodeTerm(LeafWriter *pWriter, + const char *pTerm, int nTerm){ + char c[VARINT_MAX+VARINT_MAX]; + int n, nPrefix = 0; + assert( nTerm>0 ); + while( nPrefixterm.nData && + pTerm[nPrefix]==pWriter->term.pData[nPrefix] ){ + nPrefix++; + /* Failing this implies that the terms weren't in order. */ + assert( nPrefixdata.nData==0 ){ + /* Encode the node header and leading term as: + ** varint(0) + ** varint(nTerm) + ** char pTerm[nTerm] + */ + n = fts3PutVarint(c, '\0'); + n += fts3PutVarint(c+n, nTerm); + dataBufferAppend2(&pWriter->data, c, n, pTerm, nTerm); + }else{ + /* Delta-encode the term as: + ** varint(nPrefix) + ** varint(nSuffix) + ** char pTermSuffix[nSuffix] + */ + n = fts3PutVarint(c, nPrefix); + n += fts3PutVarint(c+n, nTerm-nPrefix); + dataBufferAppend2(&pWriter->data, c, n, pTerm+nPrefix, nTerm-nPrefix); + } + dataBufferReplace(&pWriter->term, pTerm, nTerm); + + return nPrefix+1; +} + +/* Used to avoid a memmove when a large amount of doclist data is in +** the buffer. This constructs a node and term header before +** iDoclistData and flushes the resulting complete node using +** leafWriterInternalFlush(). */ +static int leafWriterInlineFlush(fulltext_vtab *v, LeafWriter *pWriter, + const char *pTerm, int nTerm, + int iDoclistData){ + char c[VARINT_MAX+VARINT_MAX]; + int iData, n = fts3PutVarint(c, 0); + n += fts3PutVarint(c+n, nTerm); -/* -** Structures used by the virtual table interface -*/ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; + /* There should always be room for the header. Even if pTerm shared + ** a substantial prefix with the previous term, the entire prefix + ** could be constructed from earlier data in the doclist, so there + ** should be room. + */ + assert( iDoclistData>=n+nTerm ); -/* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. -*/ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); + iData = iDoclistData-(n+nTerm); + memcpy(pWriter->data.pData+iData, c, n); + memcpy(pWriter->data.pData+iData+n, pTerm, nTerm); - int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); -}; + return leafWriterInternalFlush(v, pWriter, iData, pWriter->data.nData-iData); +} -/* -** The sqlite3_index_info structure and its substructures is used to -** pass information into and receive the reply from the xBestIndex -** method of an sqlite3_module. The fields under **Inputs** are the -** inputs to xBestIndex and are read-only. xBestIndex inserts its -** results into the **Outputs** fields. -** -** The aConstraint[] array records WHERE clause constraints of the -** form: -** -** column OP expr -** -** Where OP is =, <, <=, >, or >=. -** The particular operator is stored -** in aConstraint[].op. The index of the column is stored in -** aConstraint[].iColumn. aConstraint[].usable is TRUE if the -** expr on the right-hand side can be evaluated (and thus the constraint -** is usable) and false if it cannot. -** -** The optimizer automatically inverts terms of the form "expr OP column" -** and makes other simplifications to the WHERE clause in an attempt to -** get as many WHERE clause terms into the form shown above as possible. -** The aConstraint[] array only reports WHERE clause terms in the correct -** form that refer to the particular virtual table being queried. -** -** Information about the ORDER BY clause is stored in aOrderBy[]. -** Each term of aOrderBy records a column of the ORDER BY clause. -** -** The xBestIndex method must fill aConstraintUsage[] with information -** about what parameters to pass to xFilter. If argvIndex>0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. -** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. -** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. -** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). +/* Push pTerm[nTerm] along with the doclist data to the leaf layer of +** %_segments. */ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ +static int leafWriterStepMerge(fulltext_vtab *v, LeafWriter *pWriter, + const char *pTerm, int nTerm, + DLReader *pReaders, int nReaders){ + char c[VARINT_MAX+VARINT_MAX]; + int iTermData = pWriter->data.nData, iDoclistData; + int i, nData, n, nActualData, nActual, rc, nTermDistinct; - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 + ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData); + nTermDistinct = leafWriterEncodeTerm(pWriter, pTerm, nTerm); -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); + /* Remember nTermDistinct if opening a new node. */ + if( iTermData==0 ) pWriter->nTermDistinct = nTermDistinct; -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); + iDoclistData = pWriter->data.nData; -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; + /* Estimate the length of the merged doclist so we can leave space + ** to encode it. + */ + for(i=0, nData=0; idata, c, n); -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. -*/ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ -}; + docListMerge(&pWriter->data, pReaders, nReaders); + ASSERT_VALID_DOCLIST(DL_DEFAULT, + pWriter->data.pData+iDoclistData+n, + pWriter->data.nData-iDoclistData-n, NULL); -/* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. -*/ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); + /* The actual amount of doclist data at this point could be smaller + ** than the length we encoded. Additionally, the space required to + ** encode this length could be smaller. For small doclists, this is + ** not a big deal, we can just use memmove() to adjust things. + */ + nActualData = pWriter->data.nData-(iDoclistData+n); + nActual = fts3PutVarint(c, nActualData); + assert( nActualData<=nData ); + assert( nActual<=n ); -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); + /* If the new doclist is big enough for force a standalone leaf + ** node, we can immediately flush it inline without doing the + ** memmove(). + */ + /* TODO(shess) This test matches leafWriterStep(), which does this + ** test before it knows the cost to varint-encode the term and + ** doclist lengths. At some point, change to + ** pWriter->data.nData-iTermData>STANDALONE_MIN. + */ + if( nTerm+nActualData>STANDALONE_MIN ){ + /* Push leaf node from before this term. */ + if( iTermData>0 ){ + rc = leafWriterInternalFlush(v, pWriter, 0, iTermData); + if( rc!=SQLITE_OK ) return rc; -/* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** -*/ + pWriter->nTermDistinct = nTermDistinct; + } -/* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. -*/ -typedef struct sqlite3_blob sqlite3_blob; + /* Fix the encoded doclist length. */ + iDoclistData += n - nActual; + memcpy(pWriter->data.pData+iDoclistData, c, nActual); -/* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} -** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: -** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} -** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} -** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement -*/ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); + /* Push the standalone leaf node. */ + rc = leafWriterInlineFlush(v, pWriter, pTerm, nTerm, iDoclistData); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. -** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. -** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. -*/ -int sqlite3_blob_close(sqlite3_blob *); + /* Leave the node empty. */ + dataBufferReset(&pWriter->data); -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); + return rc; + } -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); + /* At this point, we know that the doclist was small, so do the + ** memmove if indicated. + */ + if( nActualdata.pData+iDoclistData+nActual, + pWriter->data.pData+iDoclistData+n, + pWriter->data.nData-(iDoclistData+n)); + pWriter->data.nData -= n-nActual; + } -/* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} -** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. -** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. -** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. -** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); + /* Replace written length with actual length. */ + memcpy(pWriter->data.pData+iDoclistData, c, nActual); -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); + /* If the node is too large, break things up. */ + /* TODO(shess) This test matches leafWriterStep(), which does this + ** test before it knows the cost to varint-encode the term and + ** doclist lengths. At some point, change to + ** pWriter->data.nData>LEAF_MAX. + */ + if( iTermData+nTerm+nActualData>LEAF_MAX ){ + /* Flush out the leading data as a node */ + rc = leafWriterInternalFlush(v, pWriter, 0, iTermData); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. -** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} -** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} -** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} -** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} -** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} -** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. -*/ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); + pWriter->nTermDistinct = nTermDistinct; + + /* Rebuild header using the current term */ + n = fts3PutVarint(pWriter->data.pData, 0); + n += fts3PutVarint(pWriter->data.pData+n, nTerm); + memcpy(pWriter->data.pData+n, pTerm, nTerm); + n += nTerm; + + /* There should always be room, because the previous encoding + ** included all data necessary to construct the term. + */ + assert( ndata.nData-iDoclistDatadata.pData+n, + pWriter->data.pData+iDoclistData, + pWriter->data.nData-iDoclistData); + pWriter->data.nData -= iDoclistData-n; + } + ASSERT_VALID_LEAF_NODE(pWriter->data.pData, pWriter->data.nData); -/* -** CAPI3REF: Mutex Verifcation Routines {F17080} -** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. -** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} -** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} -** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. -*/ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); + return SQLITE_OK; +} -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} +/* Push pTerm[nTerm] along with the doclist data to the leaf layer of +** %_segments. */ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ - -/* -** CAPI3REF: Low-Level Control Of Database Files {F11300} -** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. -** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} -** -** See also: [SQLITE_FCNTL_LOCKSTATE] +/* TODO(shess) Revise writeZeroSegment() so that doclists are +** constructed directly in pWriter->data. */ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); +static int leafWriterStep(fulltext_vtab *v, LeafWriter *pWriter, + const char *pTerm, int nTerm, + const char *pData, int nData){ + int rc; + DLReader reader; -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. + dlrInit(&reader, DL_DEFAULT, pData, nData); + rc = leafWriterStepMerge(v, pWriter, pTerm, nTerm, &reader, 1); + dlrDestroy(&reader); + + return rc; +} + + +/****************************************************************/ +/* LeafReader is used to iterate over an individual leaf node. */ +typedef struct LeafReader { + DataBuffer term; /* copy of current term. */ + + const char *pData; /* data for current term. */ + int nData; +} LeafReader; + +static void leafReaderDestroy(LeafReader *pReader){ + dataBufferDestroy(&pReader->term); + SCRAMBLE(pReader); +} + +static int leafReaderAtEnd(LeafReader *pReader){ + return pReader->nData<=0; +} + +/* Access the current term. */ +static int leafReaderTermBytes(LeafReader *pReader){ + return pReader->term.nData; +} +static const char *leafReaderTerm(LeafReader *pReader){ + assert( pReader->term.nData>0 ); + return pReader->term.pData; +} + +/* Access the doclist data for the current term. */ +static int leafReaderDataBytes(LeafReader *pReader){ + int nData; + assert( pReader->term.nData>0 ); + fts3GetVarint32(pReader->pData, &nData); + return nData; +} +static const char *leafReaderData(LeafReader *pReader){ + int n, nData; + assert( pReader->term.nData>0 ); + n = fts3GetVarint32(pReader->pData, &nData); + return pReader->pData+n; +} + +static void leafReaderInit(const char *pData, int nData, + LeafReader *pReader){ + int nTerm, n; + + assert( nData>0 ); + assert( pData[0]=='\0' ); + + CLEAR(pReader); + + /* Read the first term, skipping the header byte. */ + n = fts3GetVarint32(pData+1, &nTerm); + dataBufferInit(&pReader->term, nTerm); + dataBufferReplace(&pReader->term, pData+1+n, nTerm); + + /* Position after the first term. */ + assert( 1+n+nTermpData = pData+1+n+nTerm; + pReader->nData = nData-1-n-nTerm; +} + +/* Step the reader forward to the next term. */ +static void leafReaderStep(LeafReader *pReader){ + int n, nData, nPrefix, nSuffix; + assert( !leafReaderAtEnd(pReader) ); + + /* Skip previous entry's data block. */ + n = fts3GetVarint32(pReader->pData, &nData); + assert( n+nData<=pReader->nData ); + pReader->pData += n+nData; + pReader->nData -= n+nData; + + if( !leafReaderAtEnd(pReader) ){ + /* Construct the new term using a prefix from the old term plus a + ** suffix from the leaf data. + */ + n = fts3GetVarint32(pReader->pData, &nPrefix); + n += fts3GetVarint32(pReader->pData+n, &nSuffix); + assert( n+nSuffixnData ); + pReader->term.nData = nPrefix; + dataBufferAppend(&pReader->term, pReader->pData+n, nSuffix); + + pReader->pData += n+nSuffix; + pReader->nData -= n+nSuffix; + } +} + +/* strcmp-style comparison of pReader's current term against pTerm. +** If isPrefix, equality means equal through nTerm bytes. */ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double -#endif +static int leafReaderTermCmp(LeafReader *pReader, + const char *pTerm, int nTerm, int isPrefix){ + int c, n = pReader->term.nDataterm.nData : nTerm; + if( n==0 ){ + if( pReader->term.nData>0 ) return -1; + if(nTerm>0 ) return 1; + return 0; + } -#if 0 -} /* End of the 'extern "C"' block */ -#endif -#endif + c = memcmp(pReader->term.pData, pTerm, n); + if( c!=0 ) return c; + if( isPrefix && n==nTerm ) return 0; + return pReader->term.nData - nTerm; +} -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3_tokenizer.h *************/ -/* -** Structures used by the tokenizer interface. When a new tokenizer -** implementation is registered, the caller provides a pointer to -** an sqlite3_tokenizer_module containing pointers to the callback -** functions that make up an implementation. -** -** When an fts3 table is created, it passes any arguments passed to -** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the -** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer -** implementation. The xCreate() function in turn returns an -** sqlite3_tokenizer structure representing the specific tokenizer to -** be used for the fts3 table (customized by the tokenizer clause arguments). -** -** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() -** method is called. It returns an sqlite3_tokenizer_cursor object -** that may be used to tokenize a specific input buffer based on -** the tokenization rules supplied by a specific sqlite3_tokenizer -** object. +/****************************************************************/ +/* LeavesReader wraps LeafReader to allow iterating over the entire +** leaf layer of the tree. */ -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; +typedef struct LeavesReader { + int idx; /* Index within the segment. */ -struct sqlite3_tokenizer_module { + sqlite3_stmt *pStmt; /* Statement we're streaming leaves from. */ + int eof; /* we've seen SQLITE_DONE from pStmt. */ - /* - ** Structure version. Should always be set to 0. - */ - int iVersion; + LeafReader leafReader; /* reader for the current leaf. */ + DataBuffer rootData; /* root data for inline. */ +} LeavesReader; - /* - ** Create a new tokenizer. The values in the argv[] array are the - ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL - ** TABLE statement that created the fts3 table. For example, if - ** the following SQL is executed: - ** - ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) - ** - ** then argc is set to 2, and the argv[] array contains pointers - ** to the strings "arg1" and "arg2". - ** - ** This method should return either SQLITE_OK (0), or an SQLite error - ** code. If SQLITE_OK is returned, then *ppTokenizer should be set - ** to point at the newly created tokenizer structure. The generic - ** sqlite3_tokenizer.pModule variable should not be initialised by - ** this callback. The caller will do so. - */ - int (*xCreate)( - int argc, /* Size of argv array */ - const char *const*argv, /* Tokenizer argument strings */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ - ); +/* Access the current term. */ +static int leavesReaderTermBytes(LeavesReader *pReader){ + assert( !pReader->eof ); + return leafReaderTermBytes(&pReader->leafReader); +} +static const char *leavesReaderTerm(LeavesReader *pReader){ + assert( !pReader->eof ); + return leafReaderTerm(&pReader->leafReader); +} - /* - ** Destroy an existing tokenizer. The fts3 module calls this method - ** exactly once for each successful call to xCreate(). - */ - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); +/* Access the doclist data for the current term. */ +static int leavesReaderDataBytes(LeavesReader *pReader){ + assert( !pReader->eof ); + return leafReaderDataBytes(&pReader->leafReader); +} +static const char *leavesReaderData(LeavesReader *pReader){ + assert( !pReader->eof ); + return leafReaderData(&pReader->leafReader); +} - /* - ** Create a tokenizer cursor to tokenize an input buffer. The caller - ** is responsible for ensuring that the input buffer remains valid - ** until the cursor is closed (using the xClose() method). - */ - int (*xOpen)( - sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ - const char *pInput, int nBytes, /* Input buffer */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ - ); +static int leavesReaderAtEnd(LeavesReader *pReader){ + return pReader->eof; +} - /* - ** Destroy an existing tokenizer cursor. The fts3 module calls this - ** method exactly once for each successful call to xOpen(). - */ - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); +/* loadSegmentLeaves() may not read all the way to SQLITE_DONE, thus +** leaving the statement handle open, which locks the table. +*/ +/* TODO(shess) This "solution" is not satisfactory. Really, there +** should be check-in function for all statement handles which +** arranges to call sqlite3_reset(). This most likely will require +** modification to control flow all over the place, though, so for now +** just punt. +** +** Note the the current system assumes that segment merges will run to +** completion, which is why this particular probably hasn't arisen in +** this case. Probably a brittle assumption. +*/ +static int leavesReaderReset(LeavesReader *pReader){ + return sqlite3_reset(pReader->pStmt); +} - /* - ** Retrieve the next token from the tokenizer cursor pCursor. This - ** method should either return SQLITE_OK and set the values of the - ** "OUT" variables identified below, or SQLITE_DONE to indicate that - ** the end of the buffer has been reached, or an SQLite error code. - ** - ** *ppToken should be set to point at a buffer containing the - ** normalized version of the token (i.e. after any case-folding and/or - ** stemming has been performed). *pnBytes should be set to the length - ** of this buffer in bytes. The input text that generated the token is - ** identified by the byte offsets returned in *piStartOffset and - ** *piEndOffset. - ** - ** The buffer *ppToken is set to point at is managed by the tokenizer - ** implementation. It is only required to be valid until the next call - ** to xNext() or xClose(). - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xNext)( - sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ - const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ - int *piStartOffset, /* OUT: Byte offset of token in input buffer */ - int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ - int *piPosition /* OUT: Number of tokens returned before this one */ - ); -}; +static void leavesReaderDestroy(LeavesReader *pReader){ + leafReaderDestroy(&pReader->leafReader); + dataBufferDestroy(&pReader->rootData); + SCRAMBLE(pReader); +} -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; +/* Initialize pReader with the given root data (if iStartBlockid==0 +** the leaf data was entirely contained in the root), or from the +** stream of blocks between iStartBlockid and iEndBlockid, inclusive. +*/ +static int leavesReaderInit(fulltext_vtab *v, + int idx, + sqlite_int64 iStartBlockid, + sqlite_int64 iEndBlockid, + const char *pRootData, int nRootData, + LeavesReader *pReader){ + CLEAR(pReader); + pReader->idx = idx; -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; + dataBufferInit(&pReader->rootData, 0); + if( iStartBlockid==0 ){ + /* Entire leaf level fit in root data. */ + dataBufferReplace(&pReader->rootData, pRootData, nRootData); + leafReaderInit(pReader->rootData.pData, pReader->rootData.nData, + &pReader->leafReader); + }else{ + sqlite3_stmt *s; + int rc = sql_get_leaf_statement(v, idx, &s); + if( rc!=SQLITE_OK ) return rc; -#endif /* _FTS3_TOKENIZER_H_ */ + rc = sqlite3_bind_int64(s, 1, iStartBlockid); + if( rc!=SQLITE_OK ) return rc; -/************** End of fts3_tokenizer.h **************************************/ -/************** Continuing where we left off in fts3_tokenizer1.c ************/ + rc = sqlite3_bind_int64(s, 2, iEndBlockid); + if( rc!=SQLITE_OK ) return rc; -typedef struct simple_tokenizer { - sqlite3_tokenizer base; - char delim[128]; /* flag ASCII delimiters */ -} simple_tokenizer; + rc = sqlite3_step(s); + if( rc==SQLITE_DONE ){ + pReader->eof = 1; + return SQLITE_OK; + } + if( rc!=SQLITE_ROW ) return rc; -typedef struct simple_tokenizer_cursor { - sqlite3_tokenizer_cursor base; - const char *pInput; /* input we are tokenizing */ - int nBytes; /* size of the input */ - int iOffset; /* current position in pInput */ - int iToken; /* index of next token to be returned */ - char *pToken; /* storage for current token */ - int nTokenAllocated; /* space allocated to zToken buffer */ -} simple_tokenizer_cursor; + pReader->pStmt = s; + leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), + sqlite3_column_bytes(pReader->pStmt, 0), + &pReader->leafReader); + } + return SQLITE_OK; +} +/* Step the current leaf forward to the next term. If we reach the +** end of the current leaf, step forward to the next leaf block. +*/ +static int leavesReaderStep(fulltext_vtab *v, LeavesReader *pReader){ + assert( !leavesReaderAtEnd(pReader) ); + leafReaderStep(&pReader->leafReader); -/* Forward declaration */ -static const sqlite3_tokenizer_module simpleTokenizerModule; + if( leafReaderAtEnd(&pReader->leafReader) ){ + int rc; + if( pReader->rootData.pData ){ + pReader->eof = 1; + return SQLITE_OK; + } + rc = sqlite3_step(pReader->pStmt); + if( rc!=SQLITE_ROW ){ + pReader->eof = 1; + return rc==SQLITE_DONE ? SQLITE_OK : rc; + } + leafReaderDestroy(&pReader->leafReader); + leafReaderInit(sqlite3_column_blob(pReader->pStmt, 0), + sqlite3_column_bytes(pReader->pStmt, 0), + &pReader->leafReader); + } + return SQLITE_OK; +} -static int simpleDelim(simple_tokenizer *t, unsigned char c){ - return c<0x80 && t->delim[c]; +/* Order LeavesReaders by their term, ignoring idx. Readers at eof +** always sort to the end. +*/ +static int leavesReaderTermCmp(LeavesReader *lr1, LeavesReader *lr2){ + if( leavesReaderAtEnd(lr1) ){ + if( leavesReaderAtEnd(lr2) ) return 0; + return 1; + } + if( leavesReaderAtEnd(lr2) ) return -1; + + return leafReaderTermCmp(&lr1->leafReader, + leavesReaderTerm(lr2), leavesReaderTermBytes(lr2), + 0); } -/* -** Create a new tokenizer instance. +/* Similar to leavesReaderTermCmp(), with additional ordering by idx +** so that older segments sort before newer segments. */ -static int simpleCreate( - int argc, const char * const *argv, - sqlite3_tokenizer **ppTokenizer -){ - simple_tokenizer *t; +static int leavesReaderCmp(LeavesReader *lr1, LeavesReader *lr2){ + int c = leavesReaderTermCmp(lr1, lr2); + if( c!=0 ) return c; + return lr1->idx-lr2->idx; +} + +/* Assume that pLr[1]..pLr[nLr] are sorted. Bubble pLr[0] into its +** sorted position. +*/ +static void leavesReaderReorder(LeavesReader *pLr, int nLr){ + while( nLr>1 && leavesReaderCmp(pLr, pLr+1)>0 ){ + LeavesReader tmp = pLr[0]; + pLr[0] = pLr[1]; + pLr[1] = tmp; + nLr--; + pLr++; + } +} + +/* Initializes pReaders with the segments from level iLevel, returning +** the number of segments in *piReaders. Leaves pReaders in sorted +** order. +*/ +static int leavesReadersInit(fulltext_vtab *v, int iLevel, + LeavesReader *pReaders, int *piReaders){ + sqlite3_stmt *s; + int i, rc = sql_get_statement(v, SEGDIR_SELECT_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int(s, 1, iLevel); + if( rc!=SQLITE_OK ) return rc; + + i = 0; + while( (rc = sqlite3_step(s))==SQLITE_ROW ){ + sqlite_int64 iStart = sqlite3_column_int64(s, 0); + sqlite_int64 iEnd = sqlite3_column_int64(s, 1); + const char *pRootData = sqlite3_column_blob(s, 2); + int nRootData = sqlite3_column_bytes(s, 2); - t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); - if( t==NULL ) return SQLITE_NOMEM; - memset(t, 0, sizeof(*t)); + assert( i1 ){ - int i, n = strlen(argv[1]); - for(i=0; i=0x80 ){ - sqlite3_free(t); - return SQLITE_ERROR; - } - t->delim[ch] = 1; - } - } else { - /* Mark non-alphanumeric ASCII characters as delimiters */ - int i; - for(i=1; i<0x80; i++){ - t->delim[i] = !isalnum(i); + i++; + } + if( rc!=SQLITE_DONE ){ + while( i-->0 ){ + leavesReaderDestroy(&pReaders[i]); } + return rc; } - *ppTokenizer = &t->base; - return SQLITE_OK; -} + *piReaders = i; -/* -** Destroy a tokenizer -*/ -static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ - sqlite3_free(pTokenizer); + /* Leave our results sorted by term, then age. */ + while( i-- ){ + leavesReaderReorder(pReaders+i, *piReaders-i); + } return SQLITE_OK; } -/* -** Prepare to begin tokenizing a particular string. The input -** string to be tokenized is pInput[0..nBytes-1]. A cursor -** used to incrementally tokenize this string is returned in -** *ppCursor. +/* Merge doclists from pReaders[nReaders] into a single doclist, which +** is written to pWriter. Assumes pReaders is ordered oldest to +** newest. */ -static int simpleOpen( - sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *pInput, int nBytes, /* String to be tokenized */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ -){ - simple_tokenizer_cursor *c; +/* TODO(shess) Consider putting this inline in segmentMerge(). */ +static int leavesReadersMerge(fulltext_vtab *v, + LeavesReader *pReaders, int nReaders, + LeafWriter *pWriter){ + DLReader dlReaders[MERGE_COUNT]; + const char *pTerm = leavesReaderTerm(pReaders); + int i, nTerm = leavesReaderTermBytes(pReaders); - c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); - if( c==NULL ) return SQLITE_NOMEM; + assert( nReaders<=MERGE_COUNT ); - c->pInput = pInput; - if( pInput==0 ){ - c->nBytes = 0; - }else if( nBytes<0 ){ - c->nBytes = (int)strlen(pInput); - }else{ - c->nBytes = nBytes; + for(i=0; iiOffset = 0; /* start tokenizing at the beginning */ - c->iToken = 0; - c->pToken = NULL; /* no space allocated, yet. */ - c->nTokenAllocated = 0; - *ppCursor = &c->base; - return SQLITE_OK; + return leafWriterStepMerge(v, pWriter, pTerm, nTerm, dlReaders, nReaders); } -/* -** Close a tokenization cursor previously opened by a call to -** simpleOpen() above. +/* Forward ref due to mutual recursion with segdirNextIndex(). */ +static int segmentMerge(fulltext_vtab *v, int iLevel); + +/* Put the next available index at iLevel into *pidx. If iLevel +** already has MERGE_COUNT segments, they are merged to a higher +** level to make room. */ -static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - sqlite3_free(c->pToken); - sqlite3_free(c); +static int segdirNextIndex(fulltext_vtab *v, int iLevel, int *pidx){ + int rc = segdir_max_index(v, iLevel, pidx); + if( rc==SQLITE_DONE ){ /* No segments at iLevel. */ + *pidx = 0; + }else if( rc==SQLITE_ROW ){ + if( *pidx==(MERGE_COUNT-1) ){ + rc = segmentMerge(v, iLevel); + if( rc!=SQLITE_OK ) return rc; + *pidx = 0; + }else{ + (*pidx)++; + } + }else{ + return rc; + } return SQLITE_OK; } -/* -** Extract the next token from a tokenization cursor. The cursor must -** have been opened by a prior call to simpleOpen(). +/* Merge MERGE_COUNT segments at iLevel into a new segment at +** iLevel+1. If iLevel+1 is already full of segments, those will be +** merged to make room. */ -static int simpleNext( - sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ - const char **ppToken, /* OUT: *ppToken is the token text */ - int *pnBytes, /* OUT: Number of bytes in token */ - int *piStartOffset, /* OUT: Starting offset of token */ - int *piEndOffset, /* OUT: Ending offset of token */ - int *piPosition /* OUT: Position integer of token */ -){ - simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; - simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; - unsigned char *p = (unsigned char *)c->pInput; +static int segmentMerge(fulltext_vtab *v, int iLevel){ + LeafWriter writer; + LeavesReader lrs[MERGE_COUNT]; + int i, rc, idx = 0; - while( c->iOffsetnBytes ){ - int iStartOffset; + /* Determine the next available segment index at the next level, + ** merging as necessary. + */ + rc = segdirNextIndex(v, iLevel+1, &idx); + if( rc!=SQLITE_OK ) return rc; - /* Scan past delimiter characters */ - while( c->iOffsetnBytes && simpleDelim(t, p[c->iOffset]) ){ - c->iOffset++; - } + /* TODO(shess) This assumes that we'll always see exactly + ** MERGE_COUNT segments to merge at a given level. That will be + ** broken if we allow the developer to request preemptive or + ** deferred merging. + */ + memset(&lrs, '\0', sizeof(lrs)); + rc = leavesReadersInit(v, iLevel, lrs, &i); + if( rc!=SQLITE_OK ) return rc; + assert( i==MERGE_COUNT ); - /* Count non-delimiter characters. */ - iStartOffset = c->iOffset; - while( c->iOffsetnBytes && !simpleDelim(t, p[c->iOffset]) ){ - c->iOffset++; + leafWriterInit(iLevel+1, idx, &writer); + + /* Since leavesReaderReorder() pushes readers at eof to the end, + ** when the first reader is empty, all will be empty. + */ + while( !leavesReaderAtEnd(lrs) ){ + /* Figure out how many readers share their next term. */ + for(i=1; iiOffset>iStartOffset ){ - int i, n = c->iOffset-iStartOffset; - if( n>c->nTokenAllocated ){ - c->nTokenAllocated = n+20; - c->pToken = sqlite3_realloc(c->pToken, c->nTokenAllocated); - if( c->pToken==NULL ) return SQLITE_NOMEM; - } - for(i=0; ipToken[i] = ch<0x80 ? tolower(ch) : ch; - } - *ppToken = c->pToken; - *pnBytes = n; - *piStartOffset = iStartOffset; - *piEndOffset = c->iOffset; - *piPosition = c->iToken++; + rc = leavesReadersMerge(v, lrs, i, &writer); + if( rc!=SQLITE_OK ) goto err; - return SQLITE_OK; + /* Step forward those that were merged. */ + while( i-->0 ){ + rc = leavesReaderStep(v, lrs+i); + if( rc!=SQLITE_OK ) goto err; + + /* Reorder by term, then by age. */ + leavesReaderReorder(lrs+i, MERGE_COUNT-i); } } - return SQLITE_DONE; -} -/* -** The set of routines that implement the simple tokenizer -*/ -static const sqlite3_tokenizer_module simpleTokenizerModule = { - 0, - simpleCreate, - simpleDestroy, - simpleOpen, - simpleClose, - simpleNext, -}; + for(i=0; i0 ); + for(rc=SQLITE_OK; rc==SQLITE_OK && !leavesReaderAtEnd(pReader); + rc=leavesReaderStep(v, pReader)){ + /* TODO(shess) Really want leavesReaderTermCmp(), but that name is + ** already taken to compare the terms of two LeavesReaders. Think + ** on a better name. [Meanwhile, break encapsulation rather than + ** use a confusing name.] + */ + int c = leafReaderTermCmp(&pReader->leafReader, pTerm, nTerm, isPrefix); + if( c>0 ) break; /* Past any possible matches. */ + if( c==0 ){ + const char *pData = leavesReaderData(pReader); + int iBuffer, nData = leavesReaderDataBytes(pReader); -/* -** Add the ability to override 'extern' -*/ -#ifndef SQLITE_EXTERN -# define SQLITE_EXTERN extern -#endif + /* Find the first empty buffer. */ + for(iBuffer=0; iBuffer=3001001). -** -** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. -*/ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 + /* Manual realloc so we can handle NULL appropriately. */ + p = sqlite3_malloc(nMaxBuffers*sizeof(*pBuffers)); + if( p==NULL ){ + rc = SQLITE_NOMEM; + break; + } -/* -** CAPI3REF: Run-Time Library Version Numbers {F10020} -** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might -** include a check in their application to verify that -** sqlite3_libversion_number() always returns the value -** [SQLITE_VERSION_NUMBER]. -** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not -** constants within the DLL. -*/ -SQLITE_EXTERN const char sqlite3_version[]; -const char *sqlite3_libversion(void); -int sqlite3_libversion_number(void); + if( nBuffers>0 ){ + assert(pBuffers!=NULL); + memcpy(p, pBuffers, nBuffers*sizeof(*pBuffers)); + sqlite3_free(pBuffers); + } + pBuffers = p; + } + dataBufferInit(&(pBuffers[nBuffers]), 0); + nBuffers++; + } -/* -** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} -** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. -*/ -int sqlite3_threadsafe(void); + /* At this point, must have an empty at iBuffer. */ + assert(iBufferpData, p->nData); + + /* dataBufferReset() could allow a large doclist to blow up + ** our memory requirements. + */ + if( p->nCapacity<1024 ){ + dataBufferReset(p); + }else{ + dataBufferDestroy(p); + dataBufferInit(p, 0); + } + } + } + } + } + + /* Union all the doclists together into *out. */ + /* TODO(shess) What if *out is big? Sigh. */ + if( rc==SQLITE_OK && nBuffers>0 ){ + int iBuffer; + for(iBuffer=0; iBuffer0 ){ + if( out->nData==0 ){ + dataBufferSwap(out, &(pBuffers[iBuffer])); + }else{ + docListAccumulateUnion(out, pBuffers[iBuffer].pData, + pBuffers[iBuffer].nData); + } + } + } + } + + while( nBuffers-- ){ + dataBufferDestroy(&(pBuffers[nBuffers])); + } + if( pBuffers!=NULL ) sqlite3_free(pBuffers); -/* -** If compiling for a processor that lacks floating point support, -** substitute integer for floating-point -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# define double sqlite3_int64 -#endif + return rc; +} -/* -** CAPI3REF: Closing A Database Connection {F12010} -** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. -** -** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} -** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. -*/ -int sqlite3_close(sqlite3 *); +/* Call loadSegmentLeavesInt() with pData/nData as input. */ +static int loadSegmentLeaf(fulltext_vtab *v, const char *pData, int nData, + const char *pTerm, int nTerm, int isPrefix, + DataBuffer *out){ + LeavesReader reader; + int rc; -/* -** The type for a callback function. -** This is legacy and deprecated. It is included for historical -** compatibility and is not documented. -*/ -typedef int (*sqlite3_callback)(void*,int,char**, char**); + assert( nData>1 ); + assert( *pData=='\0' ); + rc = leavesReaderInit(v, 0, 0, 0, pData, nData, &reader); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: One-Step Query Execution Interface {F12100} -** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} -*/ -int sqlite3_exec( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be evaluted */ - int (*callback)(void*,int,char**,char**), /* Callback function */ - void *, /* 1st argument to callback */ - char **errmsg /* Error msg written here */ -); + rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out); + leavesReaderReset(&reader); + leavesReaderDestroy(&reader); + return rc; +} -/* -** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK -** -** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} -** -** See also: [SQLITE_IOERR_READ | extended result codes] -** +/* Call loadSegmentLeavesInt() with the leaf nodes from iStartLeaf to +** iEndLeaf (inclusive) as input, and merge the resulting doclist into +** out. */ -#define SQLITE_OK 0 /* Successful result */ -/* beginning-of-error-codes */ -#define SQLITE_ERROR 1 /* SQL error or missing database */ -#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ -#define SQLITE_PERM 3 /* Access permission denied */ -#define SQLITE_ABORT 4 /* Callback routine requested an abort */ -#define SQLITE_BUSY 5 /* The database file is locked */ -#define SQLITE_LOCKED 6 /* A table in the database is locked */ -#define SQLITE_NOMEM 7 /* A malloc() failed */ -#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ -#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ -#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ -#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ -#define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */ -#define SQLITE_FULL 13 /* Insertion failed because database is full */ -#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ -#define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */ -#define SQLITE_EMPTY 16 /* Database is empty */ -#define SQLITE_SCHEMA 17 /* The database schema changed */ -#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ -#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ -#define SQLITE_MISMATCH 20 /* Data type mismatch */ -#define SQLITE_MISUSE 21 /* Library used incorrectly */ -#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ -#define SQLITE_AUTH 23 /* Authorization denied */ -#define SQLITE_FORMAT 24 /* Auxiliary database format error */ -#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ -#define SQLITE_NOTADB 26 /* File opened that is not a database file */ -#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ -#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ -/* end-of-error-codes */ +static int loadSegmentLeaves(fulltext_vtab *v, + sqlite_int64 iStartLeaf, sqlite_int64 iEndLeaf, + const char *pTerm, int nTerm, int isPrefix, + DataBuffer *out){ + int rc; + LeavesReader reader; -/* -** CAPI3REF: Extended Result Codes {F10220} -** -** In its default configuration, SQLite API routines return one of 26 integer -** [SQLITE_OK | result codes]. However, experience has shown that -** many of these result codes are too course-grained. They do not provide as -** much information about problems as programmers might like. In an effort to -** address this, newer versions of SQLite (version 3.3.8 and later) include -** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled -** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} -** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} -** -** The SQLITE_OK result code will never be extended. It will always -** be exactly zero. -*/ -#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) -#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) -#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) -#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) -#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) -#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) -#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) -#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) -#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) -#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) -#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) -#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) + assert( iStartLeaf<=iEndLeaf ); + rc = leavesReaderInit(v, 0, iStartLeaf, iEndLeaf, NULL, 0, &reader); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Flags For File Open Operations {F10230} -** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the -** [sqlite3_vfs] object. -*/ -#define SQLITE_OPEN_READONLY 0x00000001 -#define SQLITE_OPEN_READWRITE 0x00000002 -#define SQLITE_OPEN_CREATE 0x00000004 -#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 -#define SQLITE_OPEN_EXCLUSIVE 0x00000010 -#define SQLITE_OPEN_MAIN_DB 0x00000100 -#define SQLITE_OPEN_TEMP_DB 0x00000200 -#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 -#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 -#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 -#define SQLITE_OPEN_SUBJOURNAL 0x00002000 -#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 + rc = loadSegmentLeavesInt(v, &reader, pTerm, nTerm, isPrefix, out); + leavesReaderReset(&reader); + leavesReaderDestroy(&reader); + return rc; +} -/* -** CAPI3REF: Device Characteristics {F10240} -** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] -** object returns an integer which is a vector of the these -** bit values expressing I/O characteristics of the mass storage -** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} -** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). +/* Taking pData/nData as an interior node, find the sequence of child +** nodes which could include pTerm/nTerm/isPrefix. Note that the +** interior node terms logically come between the blocks, so there is +** one more blockid than there are terms (that block contains terms >= +** the last interior-node term). */ -#define SQLITE_IOCAP_ATOMIC 0x00000001 -#define SQLITE_IOCAP_ATOMIC512 0x00000002 -#define SQLITE_IOCAP_ATOMIC1K 0x00000004 -#define SQLITE_IOCAP_ATOMIC2K 0x00000008 -#define SQLITE_IOCAP_ATOMIC4K 0x00000010 -#define SQLITE_IOCAP_ATOMIC8K 0x00000020 -#define SQLITE_IOCAP_ATOMIC16K 0x00000040 -#define SQLITE_IOCAP_ATOMIC32K 0x00000080 -#define SQLITE_IOCAP_ATOMIC64K 0x00000100 -#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 -#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 - -/* -** CAPI3REF: File Locking Levels {F10250} -** -** {F10251} SQLite uses one of the following integer values as the second -** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} +/* TODO(shess) The calling code may already know that the end child is +** not worth calculating, because the end may be in a later sibling +** node. Consider whether breaking symmetry is worthwhile. I suspect +** it is not worthwhile. */ -#define SQLITE_LOCK_NONE 0 -#define SQLITE_LOCK_SHARED 1 -#define SQLITE_LOCK_RESERVED 2 -#define SQLITE_LOCK_PENDING 3 -#define SQLITE_LOCK_EXCLUSIVE 4 +static void getChildrenContaining(const char *pData, int nData, + const char *pTerm, int nTerm, int isPrefix, + sqlite_int64 *piStartChild, + sqlite_int64 *piEndChild){ + InteriorReader reader; -/* -** CAPI3REF: Synchronization Type Flags {F10260} -** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the -** these integer values as the second argument. -** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the -** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means -** to use Mac OS-X style fullsync instead of fsync(). -*/ -#define SQLITE_SYNC_NORMAL 0x00002 -#define SQLITE_SYNC_FULL 0x00003 -#define SQLITE_SYNC_DATAONLY 0x00010 + assert( nData>1 ); + assert( *pData!='\0' ); + interiorReaderInit(pData, nData, &reader); + /* Scan for the first child which could contain pTerm/nTerm. */ + while( !interiorReaderAtEnd(&reader) ){ + if( interiorReaderTermCmp(&reader, pTerm, nTerm, 0)>0 ) break; + interiorReaderStep(&reader); + } + *piStartChild = interiorReaderCurrentBlockid(&reader); -/* -** CAPI3REF: OS Interface Open File Handle {F11110} -** -** An [sqlite3_file] object represents an open file in the OS -** interface layer. Individual OS interface implementations will -** want to subclass this object by appending additional fields -** for their own use. The pMethods entry is a pointer to an -** [sqlite3_io_methods] object that defines methods for performing -** I/O operations on the open file. -*/ -typedef struct sqlite3_file sqlite3_file; -struct sqlite3_file { - const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ -}; + /* Keep scanning to find a term greater than our term, using prefix + ** comparison if indicated. If isPrefix is false, this will be the + ** same blockid as the starting block. + */ + while( !interiorReaderAtEnd(&reader) ){ + if( interiorReaderTermCmp(&reader, pTerm, nTerm, isPrefix)>0 ) break; + interiorReaderStep(&reader); + } + *piEndChild = interiorReaderCurrentBlockid(&reader); -/* -** CAPI3REF: OS Interface File Virtual Methods Object {F11120} -** -** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the -** methods used to perform various operations against the open file. -** -** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or -** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). -* The second choice is an -** OS-X style fullsync. The SQLITE_SYNC_DATA flag may be ORed in to -** indicate that only the data of the file and not its inode needs to be -** synced. -** -** The integer values to xLock() and xUnlock() are one of -**
      -**
    • [SQLITE_LOCK_NONE], -**
    • [SQLITE_LOCK_SHARED], -**
    • [SQLITE_LOCK_RESERVED], -**
    • [SQLITE_LOCK_PENDING], or -**
    • [SQLITE_LOCK_EXCLUSIVE]. -**
    -** xLock() increases the lock. xUnlock() decreases the lock. -** The xCheckReservedLock() method looks -** to see if any database connection, either in this -** process or in some other process, is holding an RESERVED, -** PENDING, or EXCLUSIVE lock on the file. It returns true -** if such a lock exists and false if not. -** -** The xFileControl() method is a generic interface that allows custom -** VFS implementations to directly control an open file using the -** [sqlite3_file_control()] interface. The second "op" argument -** is an integer opcode. The third -** argument is a generic pointer which is intended to be a pointer -** to a structure that may contain arguments or space in which to -** write return values. Potential uses for xFileControl() might be -** functions to enable blocking locks with timeouts, to change the -** locking strategy (for example to use dot-file locks), to inquire -** about the status of a lock, or to break stale locks. The SQLite -** core reserves opcodes less than 100 for its own use. -** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. -** Applications that define a custom xFileControl method should use opcodes -** greater than 100 to avoid conflicts. -** -** The xSectorSize() method returns the sector size of the -** device that underlies the file. The sector size is the -** minimum write that can be performed without disturbing -** other bytes in the file. The xDeviceCharacteristics() -** method returns a bit vector describing behaviors of the -** underlying device: -** -**
      -**
    • [SQLITE_IOCAP_ATOMIC] -**
    • [SQLITE_IOCAP_ATOMIC512] -**
    • [SQLITE_IOCAP_ATOMIC1K] -**
    • [SQLITE_IOCAP_ATOMIC2K] -**
    • [SQLITE_IOCAP_ATOMIC4K] -**
    • [SQLITE_IOCAP_ATOMIC8K] -**
    • [SQLITE_IOCAP_ATOMIC16K] -**
    • [SQLITE_IOCAP_ATOMIC32K] -**
    • [SQLITE_IOCAP_ATOMIC64K] -**
    • [SQLITE_IOCAP_SAFE_APPEND] -**
    • [SQLITE_IOCAP_SEQUENTIAL] -**
    -** -** The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values -** mean that writes of blocks that are nnn bytes in size and -** are aligned to an address which is an integer multiple of -** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means -** that when data is appended to a file, the data is appended -** first then the size of the file is extended, never the other -** way around. The SQLITE_IOCAP_SEQUENTIAL property means that -** information is written to disk in the same order as calls -** to xWrite(). -*/ -typedef struct sqlite3_io_methods sqlite3_io_methods; -struct sqlite3_io_methods { - int iVersion; - int (*xClose)(sqlite3_file*); - int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); - int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); - int (*xLock)(sqlite3_file*, int); - int (*xUnlock)(sqlite3_file*, int); - int (*xCheckReservedLock)(sqlite3_file*); - int (*xFileControl)(sqlite3_file*, int op, void *pArg); - int (*xSectorSize)(sqlite3_file*); - int (*xDeviceCharacteristics)(sqlite3_file*); - /* Additional methods may be added in future releases */ -}; + interiorReaderDestroy(&reader); -/* -** CAPI3REF: Standard File Control Opcodes {F11310} -** -** These integer constants are opcodes for the xFileControl method -** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] -** interface. -** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of -** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], -** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability -** is used during testing and only needs to be supported when SQLITE_TEST -** is defined. -*/ -#define SQLITE_FCNTL_LOCKSTATE 1 + /* Children must ascend, and if !prefix, both must be the same. */ + assert( *piEndChild>=*piStartChild ); + assert( isPrefix || *piStartChild==*piEndChild ); +} -/* -** CAPI3REF: Mutex Handle {F17110} -** -** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only -** deals with pointers to the [sqlite3_mutex] object. -** -** Mutexes are created using [sqlite3_mutex_alloc()]. +/* Read block at iBlockid and pass it with other params to +** getChildrenContaining(). */ -typedef struct sqlite3_mutex sqlite3_mutex; +static int loadAndGetChildrenContaining( + fulltext_vtab *v, + sqlite_int64 iBlockid, + const char *pTerm, int nTerm, int isPrefix, + sqlite_int64 *piStartChild, sqlite_int64 *piEndChild +){ + sqlite3_stmt *s = NULL; + int rc; -/* -** CAPI3REF: OS Interface Object {F11140} -** -** An instance of this object defines the interface between the -** SQLite core and the underlying operating system. The "vfs" -** in the name of the object stands for "virtual file system". -** -** The iVersion field is initially 1 but may be larger for future -** versions of SQLite. Additional fields may be appended to this -** object when the iVersion value is increased. -** -** The szOsFile field is the size of the subclassed [sqlite3_file] -** structure used by this VFS. mxPathname is the maximum length of -** a pathname in this VFS. -** -** Registered vfs modules are kept on a linked list formed by -** the pNext pointer. The [sqlite3_vfs_register()] -** and [sqlite3_vfs_unregister()] interfaces manage this list -** in a thread-safe way. The [sqlite3_vfs_find()] interface -** searches the list. -** -** The pNext field is the only fields in the sqlite3_vfs -** structure that SQLite will ever modify. SQLite will only access -** or modify this field while holding a particular static mutex. -** The application should never modify anything within the sqlite3_vfs -** object once the object has been registered. -** -** The zName field holds the name of the VFS module. The name must -** be unique across all VFS modules. -** -** {F11141} SQLite will guarantee that the zFilename string passed to -** xOpen() is a full pathname as generated by xFullPathname() and -** that the string will be valid and unchanged until xClose() is -** called. {END} So the [sqlite3_file] can store a pointer to the -** filename if it needs to remember the filename for some reason. -** -** {F11142} The flags argument to xOpen() includes all bits set in -** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] -** or [sqlite3_open16()] is used, then flags includes at least -** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. {END} -** If xOpen() opens a file read-only then it sets *pOutFlags to -** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be -** set. -** -** {F11143} SQLite will also add one of the following flags to the xOpen() -** call, depending on the object being opened: -** -**
      -**
    • [SQLITE_OPEN_MAIN_DB] -**
    • [SQLITE_OPEN_MAIN_JOURNAL] -**
    • [SQLITE_OPEN_TEMP_DB] -**
    • [SQLITE_OPEN_TEMP_JOURNAL] -**
    • [SQLITE_OPEN_TRANSIENT_DB] -**
    • [SQLITE_OPEN_SUBJOURNAL] -**
    • [SQLITE_OPEN_MASTER_JOURNAL] -**
    {END} -** -** The file I/O implementation can use the object type flags to -** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. -** -** {F11144} SQLite might also add one of the following flags to the xOpen -** method: -** -**
      -**
    • [SQLITE_OPEN_DELETEONCLOSE] -**
    • [SQLITE_OPEN_EXCLUSIVE] -**
    -** -** {F11145} The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be -** deleted when it is closed. {F11146} The [SQLITE_OPEN_DELETEONCLOSE] -** will be set for TEMP databases, journals and for subjournals. -** {F11147} The [SQLITE_OPEN_EXCLUSIVE] flag means the file should be opened -** for exclusive access. This flag is set for all files except -** for the main database file. {END} -** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite -** to hold the [sqlite3_file] structure passed as the third -** argument to xOpen. {END} The xOpen method does not have to -** allocate the structure; it should just fill it in. -** -** {F11149} The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] -** to test for the existance of a file, -** or [SQLITE_ACCESS_READWRITE] to test to see -** if a file is readable and writable, or [SQLITE_ACCESS_READ] -** to test to see if a file is at least readable. {END} The file can be a -** directory. -** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for -** the output buffers for xGetTempname and xFullPathname. {F11151} The exact -** size of the output buffer is also passed as a parameter to both -** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN -** should be returned. As this is handled as a fatal error by SQLite, -** vfs implementations should endeavor to prevent this by setting -** mxPathname to a sufficiently large value. -** -** The xRandomness(), xSleep(), and xCurrentTime() interfaces -** are not strictly a part of the filesystem, but they are -** included in the VFS structure for completeness. -** The xRandomness() function attempts to return nBytes bytes -** of good-quality randomness into zOut. The return value is -** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at -** least the number of microseconds given. The xCurrentTime() -** method returns a Julian Day Number for the current date and -** time. -*/ -typedef struct sqlite3_vfs sqlite3_vfs; -struct sqlite3_vfs { - int iVersion; /* Structure version number */ - int szOsFile; /* Size of subclassed sqlite3_file */ - int mxPathname; /* Maximum file pathname length */ - sqlite3_vfs *pNext; /* Next registered VFS */ - const char *zName; /* Name of this virtual file system */ - void *pAppData; /* Pointer to application-specific data */ - int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, - int flags, int *pOutFlags); - int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); - int (*xAccess)(sqlite3_vfs*, const char *zName, int flags); - int (*xGetTempname)(sqlite3_vfs*, int nOut, char *zOut); - int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); - void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); - void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); - void *(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol); - void (*xDlClose)(sqlite3_vfs*, void*); - int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); - int (*xSleep)(sqlite3_vfs*, int microseconds); - int (*xCurrentTime)(sqlite3_vfs*, double*); - /* New fields may be appended in figure versions. The iVersion - ** value will increment whenever this happens. */ -}; + assert( iBlockid!=0 ); + assert( pTerm!=NULL ); + assert( nTerm!=0 ); /* TODO(shess) Why not allow this? */ + assert( piStartChild!=NULL ); + assert( piEndChild!=NULL ); + + rc = sql_get_statement(v, BLOCK_SELECT_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_bind_int64(s, 1, iBlockid); + if( rc!=SQLITE_OK ) return rc; + + rc = sqlite3_step(s); + if( rc==SQLITE_DONE ) return SQLITE_ERROR; + if( rc!=SQLITE_ROW ) return rc; + + getChildrenContaining(sqlite3_column_blob(s, 0), sqlite3_column_bytes(s, 0), + pTerm, nTerm, isPrefix, piStartChild, piEndChild); -/* -** CAPI3REF: Flags for the xAccess VFS method {F11190} -** -** {F11191} These integer constants can be used as the third parameter to -** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is -** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method -** simply checks to see if the file exists. {F11193} With -** SQLITE_ACCESS_READWRITE, the xAccess method checks to see -** if the file is both readable and writable. {F11194} With -** SQLITE_ACCESS_READ the xAccess method -** checks to see if the file is readable. -*/ -#define SQLITE_ACCESS_EXISTS 0 -#define SQLITE_ACCESS_READWRITE 1 -#define SQLITE_ACCESS_READ 2 + /* We expect only one row. We must execute another sqlite3_step() + * to complete the iteration; otherwise the table will remain + * locked. */ + rc = sqlite3_step(s); + if( rc==SQLITE_ROW ) return SQLITE_ERROR; + if( rc!=SQLITE_DONE ) return rc; -/* -** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} -** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. -** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. -*/ -int sqlite3_extended_result_codes(sqlite3*, int onoff); + return SQLITE_OK; +} -/* -** CAPI3REF: Last Insert Rowid {F12220} -** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available -** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If -** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. -** -** {F12224} This routine returns the rowid of the most recent -** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts -** have ever occurred on this database connection, zero is returned. -** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the -** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned -** by this routine reverts to the last value inserted before the -** trigger fired. -** -** {F12228} An INSERT that fails due to a constraint violation is not a -** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, -** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE -** encounters a constraint violation, it does not fail. The -** INSERT continues to completion after deleting rows that caused -** the constraint problem so INSERT OR REPLACE will always change -** the return value of this interface. -** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. +/* Traverse the tree represented by pData[nData] looking for +** pTerm[nTerm], placing its doclist into *out. This is internal to +** loadSegment() to make error-handling cleaner. */ -sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); +static int loadSegmentInt(fulltext_vtab *v, const char *pData, int nData, + sqlite_int64 iLeavesEnd, + const char *pTerm, int nTerm, int isPrefix, + DataBuffer *out){ + /* Special case where root is a leaf. */ + if( *pData=='\0' ){ + return loadSegmentLeaf(v, pData, nData, pTerm, nTerm, isPrefix, out); + }else{ + int rc; + sqlite_int64 iStartChild, iEndChild; -/* -** CAPI3REF: Count The Number Of Rows Modified {F12240} -** -** {F12241} This function returns the number of database rows that were changed -** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only -** changes that are directly specified by the INSERT, UPDATE, or -** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function -** to find the total number of changes including changes caused by triggers. -** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface -** can be called to find the number of -** changes in the most recently completed INSERT, UPDATE, or DELETE -** statement within the body of the same trigger. -** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. -*/ -int sqlite3_changes(sqlite3*); + /* Process pData as an interior node, then loop down the tree + ** until we find the set of leaf nodes to scan for the term. + */ + getChildrenContaining(pData, nData, pTerm, nTerm, isPrefix, + &iStartChild, &iEndChild); + while( iStartChild>iLeavesEnd ){ + sqlite_int64 iNextStart, iNextEnd; + rc = loadAndGetChildrenContaining(v, iStartChild, pTerm, nTerm, isPrefix, + &iNextStart, &iNextEnd); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Total Number Of Rows Modified {F12260} -*** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes -** are counted as soon as the statement that makes them is completed -** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. -** -** {F12265} SQLite implements the command "DELETE FROM table" without -** a WHERE clause by dropping and recreating the table. (This is much -** faster than going -** through and deleting individual elements form the table.) Because of -** this optimization, the change count for "DELETE FROM table" will be -** zero regardless of the number of elements that were originally in the -** table. To get an accurate count of the number of rows deleted, use -** "DELETE FROM table WHERE 1" instead. -** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} -*/ -int sqlite3_total_changes(sqlite3*); + /* If we've branched, follow the end branch, too. */ + if( iStartChild!=iEndChild ){ + sqlite_int64 iDummy; + rc = loadAndGetChildrenContaining(v, iEndChild, pTerm, nTerm, isPrefix, + &iDummy, &iNextEnd); + if( rc!=SQLITE_OK ) return rc; + } -/* -** CAPI3REF: Interrupt A Long-Running Query {F12270} -** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically -** called in response to a user action such as pressing "Cancel" -** or Ctrl-C where the user wants a long query operation to halt -** immediately. -** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it -** is not safe to call this routine with a database connection that -** is closed or might close before sqlite3_interrupt() returns. -** -** If an SQL is very nearly finished at the time when sqlite3_interrupt() -** is called, then it might not have an opportunity to be interrupted. -** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an -** INSERT, UPDATE, or DELETE that is inside an explicit transaction, -** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements -** that are started after sqlite3_interrupt() returns. -*/ -void sqlite3_interrupt(sqlite3*); + assert( iNextStart<=iNextEnd ); + iStartChild = iNextStart; + iEndChild = iNextEnd; + } + assert( iStartChild<=iLeavesEnd ); + assert( iEndChild<=iLeavesEnd ); -/* -** CAPI3REF: Determine If An SQL Statement Is Complete {F10510} -** -** These routines are useful for command-line input to determine if the -** currently entered text seems to form complete a SQL statement or -** if additional input is needed before sending the text into -** SQLite for parsing. These routines return true if the input string -** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and -** so will not detect syntactically incorrect SQL. -** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} -*/ -int sqlite3_complete(const char *sql); -int sqlite3_complete16(const void *sql); + /* Scan through the leaf segments for doclists. */ + return loadSegmentLeaves(v, iStartChild, iEndChild, + pTerm, nTerm, isPrefix, out); + } +} -/* -** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} -** -** {F12311} This routine identifies a callback function that might be -** invoked whenever an attempt is made to open a database table -** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] -** or [SQLITE_IOERR_BLOCKED] -** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The -** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to -** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the -** busy callback returns 0, then no additional attempts are made to -** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt -** is made to open the database for reading and the cycle repeats. -** -** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} -** If SQLite determines that invoking the busy handler could result in -** a deadlock, it will go ahead and return [SQLITE_BUSY] or -** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} -** Consider a scenario where one process is holding a read lock that -** it is trying to promote to a reserved lock and -** a second process is holding a reserved lock that it is trying -** to promote to an exclusive lock. The first process cannot proceed -** because it is blocked by the second and the second process cannot -** proceed because it is blocked by the first. If both processes -** invoke the busy handlers, neither will make any progress. Therefore, -** SQLite returns [SQLITE_BUSY] for the first process, hoping that this -** will induce the first process to release its read lock and allow -** the second process to proceed. -** -** {F12321} The default busy callback is NULL. {END} -** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] -** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will -** already hold a RESERVED lock on the database file, but it needs -** to promote this lock to EXCLUSIVE so that it can spill cache -** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory -** cache will be left in an inconsistent state and so the error -** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the -** -** CorruptionFollowingBusyError wiki page for a discussion of why -** this is important. -** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database -** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear -** the busy handler. -** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. +/* Call loadSegmentInt() to collect the doclist for pTerm/nTerm, then +** merge its doclist over *out (any duplicate doclists read from the +** segment rooted at pData will overwrite those in *out). */ -int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); - -/* -** CAPI3REF: Set A Busy Timeout {F12340} -** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] -** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until -** at least "ms" milliseconds of sleeping have been done. {F12343} After -** "ms" milliseconds of sleeping, the handler returns 0 which -** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. -** -** {F12344} Calling this routine with an argument less than or equal to zero -** turns off all busy handlers. -** -** {F12345} There can only be a single busy handler for a particular database -** connection. If another busy handler was defined -** (using [sqlite3_busy_handler()]) prior to calling -** this routine, that other busy handler is cleared. +/* TODO(shess) Consider changing this to determine the depth of the +** leaves using either the first characters of interior nodes (when +** ==1, we're one level above the leaves), or the first character of +** the root (which will describe the height of the tree directly). +** Either feels somewhat tricky to me. */ -int sqlite3_busy_timeout(sqlite3*, int ms); +/* TODO(shess) The current merge is likely to be slow for large +** doclists (though it should process from newest/smallest to +** oldest/largest, so it may not be that bad). It might be useful to +** modify things to allow for N-way merging. This could either be +** within a segment, with pairwise merges across segments, or across +** all segments at once. +*/ +static int loadSegment(fulltext_vtab *v, const char *pData, int nData, + sqlite_int64 iLeavesEnd, + const char *pTerm, int nTerm, int isPrefix, + DataBuffer *out){ + DataBuffer result; + int rc; -/* -** CAPI3REF: Convenience Routines For Running Queries {F12370} -** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} -** -** As an example, suppose the query result where this table: -** -**
    -**        Name        | Age
    -**        -----------------------
    -**        Alice       | 43
    -**        Bob         | 28
    -**        Cindy       | 21
    -** 
    -** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: -** -**
    -**        azResult[0] = "Name";
    -**        azResult[1] = "Age";
    -**        azResult[2] = "Alice";
    -**        azResult[3] = "43";
    -**        azResult[4] = "Bob";
    -**        azResult[5] = "28";
    -**        azResult[6] = "Cindy";
    -**        azResult[7] = "21";
    -** 
    -** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). -** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to -** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. -*/ -int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ -); -void sqlite3_free_table(char **result); + assert( nData>1 ); -/* -** CAPI3REF: Formatted String Printing Functions {F17400} -** -** These routines are workalikes of the "printf()" family of functions -** from the standard C library. -** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their -** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough -** memory to hold the resulting string. -** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from -** the standard C library. The result is written into the -** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the -** first two parameters is reversed from snprintf(). This is an -** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() -** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that -** the number of characters written would be a more useful return -** value but we cannot change the implementation of sqlite3_snprintf() -** now without breaking compatibility. -** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first -** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely -** written will be n-1 characters. -** -** These routines all implement some additional formatting -** options that are useful for constructing SQL statements. -** All of the usual printf formatting options apply. In addition, there -** is are "%q", "%Q", and "%z" options. -** -** {F17410} The %q option works like %s in that it substitutes a null-terminated -** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' -** character it escapes that character and allows it to be inserted into -** the string. -** -** For example, so some string variable contains text as follows: -** -**
    -**  char *zText = "It's a happy day!";
    -** 
    -** -** One can use this text in an SQL statement as follows: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** Because the %q format string is used, the '\'' character in zText -** is escaped and the SQL generated is as follows: -** -**
    -**  INSERT INTO table1 VALUES('It''s a happy day!')
    -** 
    -** -** This is correct. Had we used %s instead of %q, the generated SQL -** would have looked like this: -** -**
    -**  INSERT INTO table1 VALUES('It's a happy day!');
    -** 
    -** -** This second example is an SQL syntax error. As a general rule you -** should always use %q instead of %s when inserting text into a string -** literal. -** -** {F17411} The %Q option works like %q except it also adds single quotes around -** the outside of the total string. Or if the parameter in the argument -** list is a NULL pointer, %Q substitutes the text "NULL" (without single -** quotes) in place of the %Q option. {END} So, for example, one could say: -** -**
    -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    -**  sqlite3_exec(db, zSQL, 0, 0, 0);
    -**  sqlite3_free(zSQL);
    -** 
    -** -** The code above will render a correct SQL statement in the zSQL -** variable even if the zText variable is a NULL pointer. -** -** {F17412} The "%z" formatting option works exactly like "%s" with the -** addition that after the string has been read and copied into -** the result, [sqlite3_free()] is called on the input string. {END} -*/ -char *sqlite3_mprintf(const char*,...); -char *sqlite3_vmprintf(const char*, va_list); -char *sqlite3_snprintf(int,char*,const char*, ...); + /* This code should never be called with buffered updates. */ + assert( v->nPendingData<0 ); -/* -** CAPI3REF: Memory Allocation Subsystem {F17300} -** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence -** does not include operating-system specific VFS implementation. The -** windows VFS uses native malloc and free for some operations. -** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block -** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to -** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns -** a NULL pointer. -** -** {F17305} Calling sqlite3_free() with a pointer previously returned -** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is -** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory -** should neither be read nor written. Even reading previously freed -** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error -** might result if sqlite3_free() is called with a non-NULL pointer that -** was not obtained from sqlite3_malloc() or sqlite3_free(). -** -** {F17310} The sqlite3_realloc() interface attempts to resize a -** prior memory allocation to be at least N bytes, where N is the -** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() -** is a NULL pointer then its behavior is identical to calling -** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or -** negative then the behavior is exactly the same as calling -** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation -** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes -** of the prior allocation are copied into the beginning of buffer returned -** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation -** is not freed. -** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() -** is always aligned to at least an 8 byte boundary. {END} -** -** {F17381} The default implementation -** of the memory allocation subsystem uses the malloc(), realloc() -** and free() provided by the standard C library. {F17382} However, if -** SQLite is compiled with the following C preprocessor macro -** -**
    SQLITE_MEMORY_SIZE=NNN
    -** -** where NNN is an integer, then SQLite create a static -** array of at least NNN bytes in size and use that array -** for all of its dynamic memory allocation needs. {END} Additional -** memory allocator options may be added in future releases. -** -** In SQLite version 3.5.0 and 3.5.1, it was possible to define -** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in -** implementation of these routines to be omitted. That capability -** is no longer provided. Only built-in memory allocators can be -** used. -** -** The windows OS interface layer calls -** the system malloc() and free() directly when converting -** filenames between the UTF-8 encoding used by SQLite -** and whatever filename encoding is used by the particular windows -** installation. Memory allocation errors are detected, but -** they are reported back as [SQLITE_CANTOPEN] or -** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. -*/ -void *sqlite3_malloc(int); -void *sqlite3_realloc(void*, int); -void sqlite3_free(void*); + dataBufferInit(&result, 0); + rc = loadSegmentInt(v, pData, nData, iLeavesEnd, + pTerm, nTerm, isPrefix, &result); + if( rc==SQLITE_OK && result.nData>0 ){ + if( out->nData==0 ){ + DataBuffer tmp = *out; + *out = result; + result = tmp; + }else{ + DataBuffer merged; + DLReader readers[2]; -/* -** CAPI3REF: Memory Allocator Statistics {F17370} -** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. -*/ -sqlite3_int64 sqlite3_memory_used(void); -sqlite3_int64 sqlite3_memory_highwater(int resetFlag); + dlrInit(&readers[0], DL_DEFAULT, out->pData, out->nData); + dlrInit(&readers[1], DL_DEFAULT, result.pData, result.nData); + dataBufferInit(&merged, out->nData+result.nData); + docListMerge(&merged, readers, 2); + dataBufferDestroy(out); + *out = merged; + dlrDestroy(&readers[0]); + dlrDestroy(&readers[1]); + } + } + dataBufferDestroy(&result); + return rc; +} -/* -** CAPI3REF: Compile-Time Authorization Callbacks {F12500} -** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} -** The authorizer callback is invoked as SQL statements are being compiled -** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various -** points during the compilation process, as logic is being created -** to perform various actions, the authorizer callback is invoked to -** see if those actions are allowed. The authorizer callback should -** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the -** specific action but allow the SQL statement to continue to be -** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns -** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] -** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} -** -** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the -** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of -** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer -** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth -** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} -** -** An authorizer is used when preparing SQL statements from an untrusted -** source, to ensure that the SQL statements do not try to access data -** that they are not allowed to see, or that they do not try to -** execute malicious statements that damage the database. For -** example, an application may allow a user to enter arbitrary -** SQL queries for evaluation by a database. But the application does -** not want the user to be able to make arbitrary changes to the -** database. An authorizer could then be put in place while the -** user-entered SQL is being prepared that disallows everything -** except SELECT statements. -** -** {F12520} Only a single authorizer can be in place on a database connection -** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} -** -** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} +/* Scan the database and merge together the posting lists for the term +** into *out. */ -int sqlite3_set_authorizer( - sqlite3*, - int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), - void *pUserData -); +static int termSelect(fulltext_vtab *v, int iColumn, + const char *pTerm, int nTerm, int isPrefix, + DocListType iType, DataBuffer *out){ + DataBuffer doclist; + sqlite3_stmt *s; + int rc = sql_get_statement(v, SEGDIR_SELECT_ALL_STMT, &s); + if( rc!=SQLITE_OK ) return rc; + + /* This code should never be called with buffered updates. */ + assert( v->nPendingData<0 ); + + dataBufferInit(&doclist, 0); + + /* Traverse the segments from oldest to newest so that newer doclist + ** elements for given docids overwrite older elements. + */ + while( (rc = sqlite3_step(s))==SQLITE_ROW ){ + const char *pData = sqlite3_column_blob(s, 0); + const int nData = sqlite3_column_bytes(s, 0); + const sqlite_int64 iLeavesEnd = sqlite3_column_int64(s, 1); + rc = loadSegment(v, pData, nData, iLeavesEnd, pTerm, nTerm, isPrefix, + &doclist); + if( rc!=SQLITE_OK ) goto err; + } + if( rc==SQLITE_DONE ){ + if( doclist.nData!=0 ){ + /* TODO(shess) The old term_select_all() code applied the column + ** restrict as we merged segments, leading to smaller buffers. + ** This is probably worthwhile to bring back, once the new storage + ** system is checked in. + */ + if( iColumn==v->nColumn) iColumn = -1; + docListTrim(DL_DEFAULT, doclist.pData, doclist.nData, + iColumn, iType, out); + } + rc = SQLITE_OK; + } + + err: + dataBufferDestroy(&doclist); + return rc; +} + +/****************************************************************/ +/* Used to hold hashtable data for sorting. */ +typedef struct TermData { + const char *pTerm; + int nTerm; + DLCollector *pCollector; +} TermData; -/* -** CAPI3REF: Authorizer Return Codes {F12590} -** -** The [sqlite3_set_authorizer | authorizer callback function] must -** return either [SQLITE_OK] or one of these two constants in order -** to signal SQLite whether or not the action is permitted. See the -** [sqlite3_set_authorizer | authorizer documentation] for additional -** information. +/* Orders TermData elements in strcmp fashion ( <0 for less-than, 0 +** for equal, >0 for greater-than). */ -#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ -#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ +static int termDataCmp(const void *av, const void *bv){ + const TermData *a = (const TermData *)av; + const TermData *b = (const TermData *)bv; + int n = a->nTermnTerm ? a->nTerm : b->nTerm; + int c = memcmp(a->pTerm, b->pTerm, n); + if( c!=0 ) return c; + return a->nTerm-b->nTerm; +} -/* -** CAPI3REF: Authorizer Action Codes {F12550} -** -** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The -** second parameter to the callback is an integer code that specifies -** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} -** -** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization -** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the -** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback -** is the name of the inner-most trigger or view that is responsible for -** the access attempt or NULL if this access attempt is directly from -** top-level SQL code. +/* Order pTerms data by term, then write a new level 0 segment using +** LeafWriter. */ -/******************************************* 3rd ************ 4th ***********/ -#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ -#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ -#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ -#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ -#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ -#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ -#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ -#define SQLITE_DELETE 9 /* Table Name NULL */ -#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ -#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ -#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ -#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ -#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ -#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ -#define SQLITE_DROP_VIEW 17 /* View Name NULL */ -#define SQLITE_INSERT 18 /* Table Name NULL */ -#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ -#define SQLITE_READ 20 /* Table Name Column Name */ -#define SQLITE_SELECT 21 /* NULL NULL */ -#define SQLITE_TRANSACTION 22 /* NULL NULL */ -#define SQLITE_UPDATE 23 /* Table Name Column Name */ -#define SQLITE_ATTACH 24 /* Filename NULL */ -#define SQLITE_DETACH 25 /* Database Name NULL */ -#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ -#define SQLITE_REINDEX 27 /* Index Name NULL */ -#define SQLITE_ANALYZE 28 /* Table Name NULL */ -#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ -#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ -#define SQLITE_FUNCTION 31 /* Function Name NULL */ -#define SQLITE_COPY 0 /* No longer used */ +static int writeZeroSegment(fulltext_vtab *v, fts3Hash *pTerms){ + fts3HashElem *e; + int idx, rc, i, n; + TermData *pData; + LeafWriter writer; + DataBuffer dl; -/* -** CAPI3REF: Tracing And Profiling Functions {F12280} -** -** These routines register callback functions that can be used for -** tracing and profiling the execution of SQL statements. -** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} -** -** The sqlite3_profile() API is currently considered experimental and -** is subject to change. -*/ -void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); -void *sqlite3_profile(sqlite3*, - void(*xProfile)(void*,const char*,sqlite3_uint64), void*); + /* Determine the next index at level 0, merging as necessary. */ + rc = segdirNextIndex(v, 0, &idx); + if( rc!=SQLITE_OK ) return rc; -/* -** CAPI3REF: Query Progress Callbacks {F12910} -** -** {F12911} This routine configures a callback function - the -** progress callback - that is invoked periodically during long -** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this -** interface is to keep a GUI updated during a large query. -** -** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. -** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} -** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} -** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} -** -** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. -*/ -void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); + n = fts3HashCount(pTerms); + pData = sqlite3_malloc(n*sizeof(TermData)); -/* -** CAPI3REF: Opening A New Database Connection {F12700} -** -** {F12701} These routines open an SQLite database file whose name -** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 -** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 -** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable -** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The -** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain -** an English language description of the error. -** -** {F12707} The default encoding for the database will be UTF-8 if -** [sqlite3_open()] or [sqlite3_open_v2()] is called and -** UTF-16 in the native byte order if [sqlite3_open16()] is used. -** -** {F12708} Whether or not an error occurs when it is opened, resources -** associated with the [sqlite3*] handle should be released by passing it -** to [sqlite3_close()] when it is no longer required. -** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] -** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be -** one of: -** -**
      -**
    1. [SQLITE_OPEN_READONLY] -**
    2. [SQLITE_OPEN_READWRITE] -**
    3. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] -**
    -** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens -** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option -** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} -** The third options is behavior that is always used for [sqlite3_open()] -** and [sqlite3_open16()]. -** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future -** version of SQLite might make use of additional special filenames -** that begin with the ":" character. It is recommended that -** when a database filename really does begin with -** ":" that you prefix the filename with a pathname like "./" to -** avoid ambiguity. -** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be -** automatically deleted as soon as the database connection is closed. -** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the -** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the -** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} -** -** Note to windows users: The encoding used for the filename argument -** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever -** codepage is currently defined. Filenames containing international -** characters must be converted to UTF-8 prior to passing them into -** [sqlite3_open()] or [sqlite3_open_v2()]. -*/ -int sqlite3_open( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open16( - const void *filename, /* Database filename (UTF-16) */ - sqlite3 **ppDb /* OUT: SQLite db handle */ -); -int sqlite3_open_v2( - const char *filename, /* Database filename (UTF-8) */ - sqlite3 **ppDb, /* OUT: SQLite db handle */ - int flags, /* Flags */ - const char *zVfs /* Name of VFS module to use */ -); + for(i = 0, e = fts3HashFirst(pTerms); e; i++, e = fts3HashNext(e)){ + assert( i1 ) qsort(pData, n, sizeof(*pData), termDataCmp); -/* -** CAPI3REF: SQL Statement Object {F13000} -** -** An instance of this object represent single SQL statements. This -** object is variously known as a "prepared statement" or a -** "compiled SQL statement" or simply as a "statement". -** -** The life of a statement object goes something like this: -** -**
      -**
    1. Create the object using [sqlite3_prepare_v2()] or a related -** function. -**
    2. Bind values to host parameters using -** [sqlite3_bind_blob | sqlite3_bind_* interfaces]. -**
    3. Run the SQL by calling [sqlite3_step()] one or more times. -**
    4. Reset the statement using [sqlite3_reset()] then go back -** to step 2. Do this zero or more times. -**
    5. Destroy the object using [sqlite3_finalize()]. -**
    -** -** Refer to documentation on individual methods above for additional -** information. -*/ -typedef struct sqlite3_stmt sqlite3_stmt; + /* TODO(shess) Refactor so that we can write directly to the segment + ** DataBuffer, as happens for segment merges. + */ + leafWriterInit(0, idx, &writer); + dataBufferInit(&dl, 0); + for(i=0; i -**
  • {F13022} -** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it -** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in -** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling -** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text -** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} -**
  • -** -**
  • -** {F13030} When an error occurs, -** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} -** The legacy behavior was that [sqlite3_step()] would only return a generic -** [SQLITE_ERROR] result code and you would have to make a second call to -** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} -** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} -**
  • -** -*/ -int sqlite3_prepare( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare_v2( - sqlite3 *db, /* Database handle */ - const char *zSql, /* SQL statement, UTF-8 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const char **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); -int sqlite3_prepare16_v2( - sqlite3 *db, /* Database handle */ - const void *zSql, /* SQL statement, UTF-16 encoded */ - int nByte, /* Maximum length of zSql in bytes. */ - sqlite3_stmt **ppStmt, /* OUT: Statement handle */ - const void **pzTail /* OUT: Pointer to unused portion of zSql */ -); + err: + dataBufferDestroy(&dl); + sqlite3_free(pData); + leafWriterDestroy(&writer); + return rc; +} -/* -** CAPIREF: Retrieving Statement SQL {F13100} -** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. -** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. -*/ -const char *sqlite3_sql(sqlite3_stmt *pStmt); +/* If pendingTerms has data, free it. */ +static int clearPendingTerms(fulltext_vtab *v){ + if( v->nPendingData>=0 ){ + fts3HashElem *e; + for(e=fts3HashFirst(&v->pendingTerms); e; e=fts3HashNext(e)){ + dlcDelete(fts3HashData(e)); + } + fts3HashClear(&v->pendingTerms); + v->nPendingData = -1; + } + return SQLITE_OK; +} -/* -** CAPI3REF: Dynamically Typed Value Object {F15000} -** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} -** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be -** be integers, floating point values, strings, BLOBs, or NULL. +/* If pendingTerms has data, flush it to a level-zero segment, and +** free it. */ -typedef struct Mem sqlite3_value; +static int flushPendingTerms(fulltext_vtab *v){ + if( v->nPendingData>=0 ){ + int rc = writeZeroSegment(v, &v->pendingTerms); + if( rc==SQLITE_OK ) clearPendingTerms(v); + return rc; + } + return SQLITE_OK; +} -/* -** CAPI3REF: SQL Function Context Object {F16001} -** -** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context -** object is always first parameter to application-defined SQL functions. +/* If pendingTerms is "too big", or docid is out of order, flush it. +** Regardless, be certain that pendingTerms is initialized for use. */ -typedef struct sqlite3_context sqlite3_context; +static int initPendingTerms(fulltext_vtab *v, sqlite_int64 iDocid){ + /* TODO(shess) Explore whether partially flushing the buffer on + ** forced-flush would provide better performance. I suspect that if + ** we ordered the doclists by size and flushed the largest until the + ** buffer was half empty, that would let the less frequent terms + ** generate longer doclists. + */ + if( iDocid<=v->iPrevDocid || v->nPendingData>kPendingThreshold ){ + int rc = flushPendingTerms(v); + if( rc!=SQLITE_OK ) return rc; + } + if( v->nPendingData<0 ){ + fts3HashInit(&v->pendingTerms, FTS3_HASH_STRING, 1); + v->nPendingData = 0; + } + v->iPrevDocid = iDocid; + return SQLITE_OK; +} + +/* This function implements the xUpdate callback; it is the top-level entry + * point for inserting, deleting or updating a row in a full-text table. */ +static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg, + sqlite_int64 *pRowid){ + fulltext_vtab *v = (fulltext_vtab *) pVtab; + int rc; + + FTSTRACE(("FTS3 Update %p\n", pVtab)); + + if( nArg<2 ){ + rc = index_delete(v, sqlite3_value_int64(ppArg[0])); + } else if( sqlite3_value_type(ppArg[0]) != SQLITE_NULL ){ + /* An update: + * ppArg[0] = old rowid + * ppArg[1] = new rowid + * ppArg[2..2+v->nColumn-1] = values + * ppArg[2+v->nColumn] = value for magic column (we ignore this) + * ppArg[2+v->nColumn+1] = value for docid + */ + sqlite_int64 rowid = sqlite3_value_int64(ppArg[0]); + if( sqlite3_value_type(ppArg[1]) != SQLITE_INTEGER || + sqlite3_value_int64(ppArg[1]) != rowid ){ + rc = SQLITE_ERROR; /* we don't allow changing the rowid */ + }else if( sqlite3_value_type(ppArg[2+v->nColumn+1]) != SQLITE_INTEGER || + sqlite3_value_int64(ppArg[2+v->nColumn+1]) != rowid ){ + rc = SQLITE_ERROR; /* we don't allow changing the docid */ + }else{ + assert( nArg==2+v->nColumn+2); + rc = index_update(v, rowid, &ppArg[2]); + } + } else { + /* An insert: + * ppArg[1] = requested rowid + * ppArg[2..2+v->nColumn-1] = values + * ppArg[2+v->nColumn] = value for magic column (we ignore this) + * ppArg[2+v->nColumn+1] = value for docid + */ + sqlite3_value *pRequestDocid = ppArg[2+v->nColumn+1]; + assert( nArg==2+v->nColumn+2); + if( SQLITE_NULL != sqlite3_value_type(pRequestDocid) && + SQLITE_NULL != sqlite3_value_type(ppArg[1]) ){ + /* TODO(shess) Consider allowing this to work if the values are + ** identical. I'm inclined to discourage that usage, though, + ** given that both rowid and docid are special columns. Better + ** would be to define one or the other as the default winner, + ** but should it be fts3-centric (docid) or SQLite-centric + ** (rowid)? + */ + rc = SQLITE_ERROR; + }else{ + if( SQLITE_NULL == sqlite3_value_type(pRequestDocid) ){ + pRequestDocid = ppArg[1]; + } + rc = index_insert(v, pRequestDocid, &ppArg[2], pRowid); + } + } + + return rc; +} + +static int fulltextSync(sqlite3_vtab *pVtab){ + FTSTRACE(("FTS3 xSync()\n")); + return flushPendingTerms((fulltext_vtab *)pVtab); +} + +static int fulltextBegin(sqlite3_vtab *pVtab){ + fulltext_vtab *v = (fulltext_vtab *) pVtab; + FTSTRACE(("FTS3 xBegin()\n")); + + /* Any buffered updates should have been cleared by the previous + ** transaction. + */ + assert( v->nPendingData<0 ); + return clearPendingTerms(v); +} + +static int fulltextCommit(sqlite3_vtab *pVtab){ + fulltext_vtab *v = (fulltext_vtab *) pVtab; + FTSTRACE(("FTS3 xCommit()\n")); + + /* Buffered updates should have been cleared by fulltextSync(). */ + assert( v->nPendingData<0 ); + return clearPendingTerms(v); +} + +static int fulltextRollback(sqlite3_vtab *pVtab){ + FTSTRACE(("FTS3 xRollback()\n")); + return clearPendingTerms((fulltext_vtab *)pVtab); +} /* -** CAPI3REF: Binding Values To Prepared Statements {F13500} -** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its -** variants, literals may be replace by a parameter in one -** of these forms: -** -**
      -**
    • ? -**
    • ?NNN -**
    • :AAA -**
    • @AAA -**
    • $VVV -**
    -** -** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") -** can be set using the sqlite3_bind_*() routines defined here. -** -** {F13502} The first argument to the sqlite3_bind_*() routines always -** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named -** parameter is used more than once, second and subsequent -** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index -** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. -** -** {F13509} The third argument is the value to bind to the parameter. {END} -** -** {F13510} In those -** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number -** of bytes does not include the zero-terminator at the end of strings. -** {F13512} -** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} -** -** {F13513} -** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the -** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then -** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} -** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} -** Zeroblobs are intended to serve as place-holders for BLOBs whose -** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} -** -** {F13530} The sqlite3_bind_*() routines must be called after -** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} -** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} -** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a -** virtual machine that is the wrong state or which has already been finalized. +** Implementation of the snippet() function for FTS3 */ -int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); -int sqlite3_bind_double(sqlite3_stmt*, int, double); -int sqlite3_bind_int(sqlite3_stmt*, int, int); -int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); -int sqlite3_bind_null(sqlite3_stmt*, int); -int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); -int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); -int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); -int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); +static void snippetFunc( + sqlite3_context *pContext, + int argc, + sqlite3_value **argv +){ + fulltext_cursor *pCursor; + if( argc<1 ) return; + if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || + sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ + sqlite3_result_error(pContext, "illegal first argument to html_snippet",-1); + }else{ + const char *zStart = ""; + const char *zEnd = ""; + const char *zEllipsis = "..."; + memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); + if( argc>=2 ){ + zStart = (const char*)sqlite3_value_text(argv[1]); + if( argc>=3 ){ + zEnd = (const char*)sqlite3_value_text(argv[2]); + if( argc>=4 ){ + zEllipsis = (const char*)sqlite3_value_text(argv[3]); + } + } + } + snippetAllOffsets(pCursor); + snippetText(pCursor, zStart, zEnd, zEllipsis); + sqlite3_result_text(pContext, pCursor->snippet.zSnippet, + pCursor->snippet.nSnippet, SQLITE_STATIC); + } +} /* -** CAPI3REF: Number Of Host Parameters {F13600} -** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} -** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. +** Implementation of the offsets() function for FTS3 */ -int sqlite3_bind_parameter_count(sqlite3_stmt*); +static void snippetOffsetsFunc( + sqlite3_context *pContext, + int argc, + sqlite3_value **argv +){ + fulltext_cursor *pCursor; + if( argc<1 ) return; + if( sqlite3_value_type(argv[0])!=SQLITE_BLOB || + sqlite3_value_bytes(argv[0])!=sizeof(pCursor) ){ + sqlite3_result_error(pContext, "illegal first argument to offsets",-1); + }else{ + memcpy(&pCursor, sqlite3_value_blob(argv[0]), sizeof(pCursor)); + snippetAllOffsets(pCursor); + snippetOffsetText(&pCursor->snippet); + sqlite3_result_text(pContext, + pCursor->snippet.zOffset, pCursor->snippet.nOffset, + SQLITE_STATIC); + } +} /* -** CAPI3REF: Name Of A Host Parameter {F13620} -** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name -** which is the string ":AAA" or "@AAA" or "$VVV". -** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} -** Parameters of the form "?" or "?NNN" have no name. -** -** {F13623} The first host parameter has an index of 1, not 0. -** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is -** always in the UTF-8 encoding even if the named parameter was -** originally specified as UTF-16 in [sqlite3_prepare16()] or -** [sqlite3_prepare16_v2()]. +** This routine implements the xFindFunction method for the FTS3 +** virtual table. */ -const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); +static int fulltextFindFunction( + sqlite3_vtab *pVtab, + int nArg, + const char *zName, + void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), + void **ppArg +){ + if( strcmp(zName,"snippet")==0 ){ + *pxFunc = snippetFunc; + return 1; + }else if( strcmp(zName,"offsets")==0 ){ + *pxFunc = snippetOffsetsFunc; + return 1; + } + return 0; +} /* -** CAPI3REF: Index Of A Parameter With A Given Name {F13640} -** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. +** Rename an fts3 table. */ -int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); +static int fulltextRename( + sqlite3_vtab *pVtab, + const char *zName +){ + fulltext_vtab *p = (fulltext_vtab *)pVtab; + int rc = SQLITE_NOMEM; + char *zSql = sqlite3_mprintf( + "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';" + "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';" + "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';" + , p->zDb, p->zName, zName + , p->zDb, p->zName, zName + , p->zDb, p->zName, zName + ); + if( zSql ){ + rc = sqlite3_exec(p->db, zSql, 0, 0, 0); + sqlite3_free(zSql); + } + return rc; +} -/* -** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} -** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not -** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to -** reset all host parameters to NULL. -*/ -int sqlite3_clear_bindings(sqlite3_stmt*); +static const sqlite3_module fts3Module = { + /* iVersion */ 0, + /* xCreate */ fulltextCreate, + /* xConnect */ fulltextConnect, + /* xBestIndex */ fulltextBestIndex, + /* xDisconnect */ fulltextDisconnect, + /* xDestroy */ fulltextDestroy, + /* xOpen */ fulltextOpen, + /* xClose */ fulltextClose, + /* xFilter */ fulltextFilter, + /* xNext */ fulltextNext, + /* xEof */ fulltextEof, + /* xColumn */ fulltextColumn, + /* xRowid */ fulltextRowid, + /* xUpdate */ fulltextUpdate, + /* xBegin */ fulltextBegin, + /* xSync */ fulltextSync, + /* xCommit */ fulltextCommit, + /* xRollback */ fulltextRollback, + /* xFindFunction */ fulltextFindFunction, + /* xRename */ fulltextRename, +}; -/* -** CAPI3REF: Number Of Columns In A Result Set {F13710} -** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 -** if pStmt is an SQL statement that does not return data (for -** example an UPDATE). -*/ -int sqlite3_column_count(sqlite3_stmt *pStmt); +static void hashDestroy(void *p){ + fts3Hash *pHash = (fts3Hash *)p; + sqlite3Fts3HashClear(pHash); + sqlite3_free(pHash); +} /* -** CAPI3REF: Column Names In A Result Set {F13720} -** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() -** interface returns a pointer to a zero-terminated UTF8 string -** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. -** The second parameter is the column number. The left-most column is -** number 0. -** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] -** or until the next call sqlite3_column_name() or sqlite3_column_name16() -** on the same column. +** The fts3 built-in tokenizers - "simple" and "porter" - are implemented +** in files fts3_tokenizer1.c and fts3_porter.c respectively. The following +** two forward declarations are for functions declared in these files +** used to retrieve the respective implementations. ** -** {F13725} If sqlite3_malloc() fails during the processing of either routine -** (for example during a conversion from UTF-8 to UTF-16) then a -** NULL pointer is returned. +** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed +** to by the argument to point a the "simple" tokenizer implementation. +** Function ...PorterTokenizerModule() sets *pModule to point to the +** porter tokenizer/stemmer implementation. */ -const char *sqlite3_column_name(sqlite3_stmt*, int N); -const void *sqlite3_column_name16(sqlite3_stmt*, int N); +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); -/* -** CAPI3REF: Source Of Data In A Query Result {F13740} -** -** {F13741} These routines provide a means to determine what column of what -** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return -** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} -** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using -** [sqlite3_finalize()] or until the same information is requested -** again in a different encoding. -** -** {F13745} The names returned are the original un-aliased names of the -** database, table, and column. -** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by -** the statement, where N is the second function argument. -** -** {F13748} If the Nth column returned by the statement is an expression -** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the -** name of the attached database, table and column that query result -** column was extracted from. -** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return -** UTF-16 encoded strings, the other functions return UTF-8. {END} -** -** These APIs are only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. -** -** {U13751} -** If two or more threads call one or more of these routines against the same -** prepared statement and column at the same time then the results are -** undefined. -*/ -const char *sqlite3_column_database_name(sqlite3_stmt*,int); -const void *sqlite3_column_database_name16(sqlite3_stmt*,int); -const char *sqlite3_column_table_name(sqlite3_stmt*,int); -const void *sqlite3_column_table_name16(sqlite3_stmt*,int); -const char *sqlite3_column_origin_name(sqlite3_stmt*,int); -const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); +SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, fts3Hash *, const char *); /* -** CAPI3REF: Declared Datatype Of A Query Result {F13760} -** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the -** returned result set of that SELECT is a table column (not an -** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an -** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} -** For example, in the database schema: -** -** CREATE TABLE t1(c1 VARIANT); -** -** And the following statement compiled: -** -** SELECT c1 + 1, c1 FROM t1; -** -** Then this routine would return the string "VARIANT" for the second -** result column (i==1), and a NULL pointer for the first result column -** (i==0). -** -** SQLite uses dynamic run-time typing. So just because a column -** is declared to contain a particular type does not mean that the -** data stored in that column is of the declared type. SQLite is -** strongly typed, but the typing is dynamic not static. Type -** is associated with individual values, not with the containers -** used to hold those values. +** Initialise the fts3 extension. If this extension is built as part +** of the sqlite library, then this function is called directly by +** SQLite. If fts3 is built as a dynamically loadable extension, this +** function is called by the sqlite3_extension_init() entry point. */ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); -const void *sqlite3_column_decltype16(sqlite3_stmt*,int); +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){ + int rc = SQLITE_OK; + fts3Hash *pHash = 0; + const sqlite3_tokenizer_module *pSimple = 0; + const sqlite3_tokenizer_module *pPorter = 0; + const sqlite3_tokenizer_module *pIcu = 0; -/* -** CAPI3REF: Evaluate An SQL Statement {F13200} -** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call -** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of -** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], -** then this function must be called one or more times to evaluate the -** statement. -** -** The details of the behavior of this sqlite3_step() interface depend -** on whether the statement was prepared using the newer "v2" interface -** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy -** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the -** new "v2" interface is recommended for new applications but the legacy -** interface will continue to be supported. -** -** In the lagacy interface, the return value will be either [SQLITE_BUSY], -** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. -** With the "v2" interface, any of the other [SQLITE_OK | result code] -** or [SQLITE_IOERR_READ | extended result code] might be returned as -** well. -** -** [SQLITE_BUSY] means that the database engine was unable to acquire the -** database locks it needs to do its job. If the statement is a COMMIT -** or occurs outside of an explicit transaction, then you can retry the -** statement. If the statement is not a COMMIT and occurs within a -** explicit transaction then you should rollback the transaction before -** continuing. -** -** [SQLITE_DONE] means that the statement has finished executing -** successfully. sqlite3_step() should not be called again on this virtual -** machine without first calling [sqlite3_reset()] to reset the virtual -** machine back to its initial state. -** -** If the SQL statement being executed returns any data, then -** [SQLITE_ROW] is returned each time a new row of data is ready -** for processing by the caller. The values may be accessed using -** the [sqlite3_column_int | column access functions]. -** sqlite3_step() is called again to retrieve the next row of data. -** -** [SQLITE_ERROR] means that a run-time error (such as a constraint -** violation) has occurred. sqlite3_step() should not be called again on -** the VM. More information may be found by calling [sqlite3_errmsg()]. -** With the legacy interface, a more specific error code (example: -** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) -** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, -** the more specific error code is returned directly by sqlite3_step(). -** -** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has -** already been [sqlite3_finalize | finalized] or on one that had -** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could -** be the case that the same database connection is being used by two or -** more threads at the same moment in time. -** -** Goofy Interface Alert: -** In the legacy interface, -** the sqlite3_step() API always returns a generic error code, -** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] -** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or -** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. -** We admit that this is a goofy design. The problem has been fixed -** with the "v2" interface. If you prepare all of your SQL statements -** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead -** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly -** by sqlite3_step(). The use of the "v2" interface is recommended. -*/ -int sqlite3_step(sqlite3_stmt*); + sqlite3Fts3SimpleTokenizerModule(&pSimple); + sqlite3Fts3PorterTokenizerModule(&pPorter); +#ifdef SQLITE_ENABLE_ICU + sqlite3Fts3IcuTokenizerModule(&pIcu); +#endif -/* -** CAPI3REF: Number of columns in a result set {F13770} -** -** Return the number of values in the current row of the result set. -** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. -*/ -int sqlite3_data_count(sqlite3_stmt *pStmt); + /* Allocate and initialise the hash-table used to store tokenizers. */ + pHash = sqlite3_malloc(sizeof(fts3Hash)); + if( !pHash ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); + } -/* -** CAPI3REF: Fundamental Datatypes {F10265} -** -** {F10266}Every value in SQLite has one of five fundamental datatypes: -** -**
      -**
    • 64-bit signed integer -**
    • 64-bit IEEE floating point number -**
    • string -**
    • BLOB -**
    • NULL -**
    {END} -** -** These constants are codes for each of those types. -** -** Note that the SQLITE_TEXT constant was also used in SQLite version 2 -** for a completely different meaning. Software that links against both -** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT not -** SQLITE_TEXT. -*/ -#define SQLITE_INTEGER 1 -#define SQLITE_FLOAT 2 -#define SQLITE_BLOB 4 -#define SQLITE_NULL 5 -#ifdef SQLITE_TEXT -# undef SQLITE_TEXT -#else -# define SQLITE_TEXT 3 -#endif -#define SQLITE3_TEXT 3 + /* Load the built-in tokenizers into the hash table */ + if( rc==SQLITE_OK ){ + if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) + || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) + || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) + ){ + rc = SQLITE_NOMEM; + } + } -/* -** CAPI3REF: Results Values From A Query {F13800} -** -** These routines return information about -** a single column of the current result row of a query. In every -** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being -** evaluated (the [sqlite3_stmt*] that was returned from -** [sqlite3_prepare_v2()] or one of its variants) and -** the second argument is the index of the column for which information -** should be returned. The left-most column of the result set -** has an index of 0. -** -** If the SQL statement is not currently point to a valid row, or if the -** the column index is out of range, the result is undefined. -** These routines may only be called when the most recent call to -** [sqlite3_step()] has returned [SQLITE_ROW] and neither -** [sqlite3_reset()] nor [sqlite3_finalize()] has been call subsequently. -** If any of these routines are called after [sqlite3_reset()] or -** [sqlite3_finalize()] or after [sqlite3_step()] has returned -** something other than [SQLITE_ROW], the results are undefined. -** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] -** are called from a different thread while any of these routines -** are pending, then the results are undefined. -** -** The sqlite3_column_type() routine returns -** [SQLITE_INTEGER | datatype code] for the initial data type -** of the result column. The returned value is one of [SQLITE_INTEGER], -** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value -** returned by sqlite3_column_type() is only meaningful if no type -** conversions have occurred as described below. After a type conversion, -** the value returned by sqlite3_column_type() is undefined. Future -** versions of SQLite may change the behavior of sqlite3_column_type() -** following a type conversion. -** -** If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() -** routine returns the number of bytes in that BLOB or string. -** If the result is a UTF-16 string, then sqlite3_column_bytes() converts -** the string to UTF-8 and then returns the number of bytes. -** If the result is a numeric value then sqlite3_column_bytes() uses -** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns -** the number of bytes in that string. -** The value returned does not include the zero terminator at the end -** of the string. For clarity: the value returned is the number of -** bytes in the string, not the number of characters. -** -** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return -** value from sqlite3_column_blob() for a zero-length blob is an arbitrary -** pointer, possibly even a NULL pointer. -** -** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. -** The zero terminator is not included in this count. -** -** These routines attempt to convert the value where appropriate. For -** example, if the internal representation is FLOAT and a text result -** is requested, [sqlite3_snprintf()] is used internally to do the conversion -** automatically. The following table details the conversions that -** are applied: -** -**
    -** -**
    Internal
    Type
    Requested
    Type
    Conversion -** -**
    NULL INTEGER Result is 0 -**
    NULL FLOAT Result is 0.0 -**
    NULL TEXT Result is NULL pointer -**
    NULL BLOB Result is NULL pointer -**
    INTEGER FLOAT Convert from integer to float -**
    INTEGER TEXT ASCII rendering of the integer -**
    INTEGER BLOB Same as for INTEGER->TEXT -**
    FLOAT INTEGER Convert from float to integer -**
    FLOAT TEXT ASCII rendering of the float -**
    FLOAT BLOB Same as FLOAT->TEXT -**
    TEXT INTEGER Use atoi() -**
    TEXT FLOAT Use atof() -**
    TEXT BLOB No change -**
    BLOB INTEGER Convert to TEXT then use atoi() -**
    BLOB FLOAT Convert to TEXT then use atof() -**
    BLOB TEXT Add a zero terminator if needed -**
    -**
    -** -** The table above makes reference to standard C library functions atoi() -** and atof(). SQLite does not really use these functions. It has its -** on equavalent internal routines. The atoi() and atof() names are -** used in the table for brevity and because they are familiar to most -** C programmers. -** -** Note that when type conversions occur, pointers returned by prior -** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or -** sqlite3_column_text16() may be invalidated. -** Type conversions and pointer invalidations might occur -** in the following cases: -** -**
      -**
    • The initial content is a BLOB and sqlite3_column_text() -** or sqlite3_column_text16() is called. A zero-terminator might -** need to be added to the string.

    • -** -**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or -** sqlite3_column_text16() is called. The content must be converted -** to UTF-16.

    • -** -**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or -** sqlite3_column_text() is called. The content must be converted -** to UTF-8.

    • -**
    -** -** Conversions between UTF-16be and UTF-16le are always done in place and do -** not invalidate a prior pointer, though of course the content of the buffer -** that the prior pointer points to will have been modified. Other kinds -** of conversion are done in place when it is possible, but sometime it is -** not possible and in those cases prior pointers are invalidated. -** -** The safest and easiest to remember policy is to invoke these routines -** in one of the following ways: -** -**
      -**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • -**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • -**
    -** -** In other words, you should call sqlite3_column_text(), sqlite3_column_blob(), -** or sqlite3_column_text16() first to force the result into the desired -** format, then invoke sqlite3_column_bytes() or sqlite3_column_bytes16() to -** find the size of the result. Do not mix call to sqlite3_column_text() or -** sqlite3_column_blob() with calls to sqlite3_column_bytes16(). And do not -** mix calls to sqlite3_column_text16() with calls to sqlite3_column_bytes(). -** -** The pointers returned are valid until a type conversion occurs as -** described above, or until [sqlite3_step()] or [sqlite3_reset()] or -** [sqlite3_finalize()] is called. The memory space used to hold strings -** and blobs is freed automatically. Do not pass the pointers returned -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into -** [sqlite3_free()]. -** -** If a memory allocation error occurs during the evaluation of any -** of these routines, a default value is returned. The default value -** is either the integer 0, the floating point number 0.0, or a NULL -** pointer. Subsequent calls to [sqlite3_errcode()] will return -** [SQLITE_NOMEM]. -*/ -const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes(sqlite3_stmt*, int iCol); -int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); -double sqlite3_column_double(sqlite3_stmt*, int iCol); -int sqlite3_column_int(sqlite3_stmt*, int iCol); -sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); -const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); -const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); -int sqlite3_column_type(sqlite3_stmt*, int iCol); -sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); + /* Create the virtual table wrapper around the hash-table and overload + ** the two scalar functions. If this is successful, register the + ** module with sqlite. + */ + if( SQLITE_OK==rc + && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", -1)) + ){ + return sqlite3_create_module_v2( + db, "fts3", &fts3Module, (void *)pHash, hashDestroy + ); + } -/* -** CAPI3REF: Destroy A Prepared Statement Object {F13300} -** -** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was -** executed successfully, or not executed at all, then SQLITE_OK is returned. -** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] -** is returned. -** -** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not -** completed execution when this routine is called, that is like -** encountering an error or an interrupt. (See [sqlite3_interrupt()].) -** Incomplete updates may be rolled back and transactions cancelled, -** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. -*/ -int sqlite3_finalize(sqlite3_stmt *pStmt); + /* An error has occured. Delete the hash table and return the error code. */ + assert( rc!=SQLITE_OK ); + if( pHash ){ + sqlite3Fts3HashClear(pHash); + sqlite3_free(pHash); + } + return rc; +} -/* -** CAPI3REF: Reset A Prepared Statement Object {F13330} -** -** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. -** back to its initial state, ready to be re-executed. -** Any SQL statement variables that had values bound to them using -** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. -** Use [sqlite3_clear_bindings()] to reset the bindings. -*/ -int sqlite3_reset(sqlite3_stmt *pStmt); +#if !SQLITE_CORE +SQLITE_API int sqlite3_extension_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3Fts3Init(db); +} +#endif + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ +/************** End of fts3.c ************************************************/ +/************** Begin file fts3_hash.c ***************************************/ /* -** CAPI3REF: Create Or Redefine SQL Functions {F16100} -** -** The following two functions are used to add SQL functions or aggregates -** or to redefine the behavior of existing SQL functions or aggregates. The -** difference only between the two is that the second parameter, the -** name of the (scalar) function or aggregate, is encoded in UTF-8 for -** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). -** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. -** -** The second parameter is the name of the SQL function to be created -** or redefined. -** The length of the name is limited to 255 bytes, exclusive of the -** zero-terminator. Note that the name length limit is in bytes, not -** characters. Any attempt to create a function with a longer name -** will result in an SQLITE_ERROR error. -** -** The third parameter is the number of arguments that the SQL function or -** aggregate takes. If this parameter is negative, then the SQL function or -** aggregate may take any number of arguments. -** -** The fourth parameter, eTextRep, specifies what -** [SQLITE_UTF8 | text encoding] this SQL function prefers for -** its parameters. Any SQL function implementation should be able to work -** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be -** more efficient with one encoding than another. It is allowed to -** invoke sqlite3_create_function() or sqlite3_create_function16() multiple -** times with the same function but with different values of eTextRep. -** When multiple implementations of the same function are available, SQLite -** will pick the one that involves the least amount of data conversion. -** If there is only a single implementation which does not care what -** text encoding is used, then the fourth argument should be -** [SQLITE_ANY]. +** 2001 September 22 ** -** The fifth parameter is an arbitrary pointer. The implementation -** of the function can gain access to this pointer using -** [sqlite3_user_data()]. +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: ** -** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are -** pointers to C-language functions that implement the SQL -** function or aggregate. A scalar SQL function requires an implementation of -** the xFunc callback only, NULL pointers should be passed as the xStep -** and xFinal parameters. An aggregate SQL function requires an implementation -** of xStep and xFinal and NULL should be passed for xFunc. To delete an -** existing SQL function or aggregate, pass NULL for all three function -** callback. +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. ** -** It is permitted to register multiple implementations of the same -** functions with the same name but with either differing numbers of -** arguments or differing perferred text encodings. SQLite will use -** the implementation most closely matches the way in which the -** SQL function is used. +************************************************************************* +** This is the implementation of generic hash-tables used in SQLite. +** We've modified it slightly to serve as a standalone hash table +** implementation for the full-text indexing module. */ -int sqlite3_create_function( - sqlite3 *, - const char *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); -int sqlite3_create_function16( - sqlite3*, - const void *zFunctionName, - int nArg, - int eTextRep, - void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*) -); /* -** CAPI3REF: Text Encodings {F10267} +** The code in this file is only compiled if: ** -** These constant define integer codes that represent the various -** text encodings supported by SQLite. +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ -#define SQLITE_UTF8 1 -#define SQLITE_UTF16LE 2 -#define SQLITE_UTF16BE 3 -#define SQLITE_UTF16 4 /* Use native byte order */ -#define SQLITE_ANY 5 /* sqlite3_create_function only */ -#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + /* -** CAPI3REF: Obsolete Functions +** Malloc and Free functions +*/ +static void *fts3HashMalloc(int n){ + void *p = sqlite3_malloc(n); + if( p ){ + memset(p, 0, n); + } + return p; +} +static void fts3HashFree(void *p){ + sqlite3_free(p); +} + +/* Turn bulk memory into a hash table object by initializing the +** fields of the Hash structure. ** -** These functions are all now obsolete. In order to maintain -** backwards compatibility with older code, we continue to support -** these functions. However, new development projects should avoid -** the use of these functions. To help encourage people to avoid -** using these functions, we are not going to tell you want they do. +** "pNew" is a pointer to the hash table that is to be initialized. +** keyClass is one of the constants +** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass +** determines what kind of key the hash table will use. "copyKey" is +** true if the hash table should make its own private copy of keys and +** false if it should just use the supplied pointer. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashInit(fts3Hash *pNew, int keyClass, int copyKey){ + assert( pNew!=0 ); + assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY ); + pNew->keyClass = keyClass; + pNew->copyKey = copyKey; + pNew->first = 0; + pNew->count = 0; + pNew->htsize = 0; + pNew->ht = 0; +} + +/* Remove all entries from a hash table. Reclaim all memory. +** Call this routine to delete a hash table or to reset a hash table +** to the empty state. */ -int sqlite3_aggregate_count(sqlite3_context*); -int sqlite3_expired(sqlite3_stmt*); -int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); -int sqlite3_global_recover(void); -void sqlite3_thread_cleanup(void); -int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); +SQLITE_PRIVATE void sqlite3Fts3HashClear(fts3Hash *pH){ + fts3HashElem *elem; /* For looping over all elements of the table */ + + assert( pH!=0 ); + elem = pH->first; + pH->first = 0; + fts3HashFree(pH->ht); + pH->ht = 0; + pH->htsize = 0; + while( elem ){ + fts3HashElem *next_elem = elem->next; + if( pH->copyKey && elem->pKey ){ + fts3HashFree(elem->pKey); + } + fts3HashFree(elem); + elem = next_elem; + } + pH->count = 0; +} /* -** CAPI3REF: Obtaining SQL Function Parameter Values {F15100} -** -** The C-language implementation of SQL functions and aggregates uses -** this set of interface routines to access the parameter values on -** the function or aggregate. -** -** The xFunc (for scalar functions) or xStep (for aggregates) parameters -** to [sqlite3_create_function()] and [sqlite3_create_function16()] -** define callbacks that implement the SQL functions and aggregates. -** The 4th parameter to these callbacks is an array of pointers to -** [sqlite3_value] objects. There is one [sqlite3_value] object for -** each parameter to the SQL function. These routines are used to -** extract values from the [sqlite3_value] objects. -** -** These routines work just like the corresponding -** [sqlite3_column_blob | sqlite3_column_* routines] except that -** these routines take a single [sqlite3_value*] pointer instead -** of an [sqlite3_stmt*] pointer and an integer column number. -** -** The sqlite3_value_text16() interface extracts a UTF16 string -** in the native byte-order of the host machine. The -** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces -** extract UTF16 strings as big-endian and little-endian respectively. -** -** The sqlite3_value_numeric_type() interface attempts to apply -** numeric affinity to the value. This means that an attempt is -** made to convert the value to an integer or floating point. If -** such a conversion is possible without loss of information (in other -** words if the value is a string that looks like a number) -** then the conversion is done. Otherwise no conversion occurs. The -** [SQLITE_INTEGER | datatype] after conversion is returned. -** -** Please pay particular attention to the fact that the pointer that -** is returned from [sqlite3_value_blob()], [sqlite3_value_text()], or -** [sqlite3_value_text16()] can be invalidated by a subsequent call to -** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], -** or [sqlite3_value_text16()]. -** -** These routines must be called from the same thread as -** the SQL function that supplied the sqlite3_value* parameters. -** Or, if the sqlite3_value* argument comes from the [sqlite3_column_value()] -** interface, then these routines should be called from the same thread -** that ran [sqlite3_column_value()]. -** +** Hash and comparison functions when the mode is FTS3_HASH_STRING */ -const void *sqlite3_value_blob(sqlite3_value*); -int sqlite3_value_bytes(sqlite3_value*); -int sqlite3_value_bytes16(sqlite3_value*); -double sqlite3_value_double(sqlite3_value*); -int sqlite3_value_int(sqlite3_value*); -sqlite3_int64 sqlite3_value_int64(sqlite3_value*); -const unsigned char *sqlite3_value_text(sqlite3_value*); -const void *sqlite3_value_text16(sqlite3_value*); -const void *sqlite3_value_text16le(sqlite3_value*); -const void *sqlite3_value_text16be(sqlite3_value*); -int sqlite3_value_type(sqlite3_value*); -int sqlite3_value_numeric_type(sqlite3_value*); +static int fts3StrHash(const void *pKey, int nKey){ + const char *z = (const char *)pKey; + int h = 0; + if( nKey<=0 ) nKey = (int) strlen(z); + while( nKey > 0 ){ + h = (h<<3) ^ h ^ *z++; + nKey--; + } + return h & 0x7fffffff; +} +static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ + if( n1!=n2 ) return 1; + return strncmp((const char*)pKey1,(const char*)pKey2,n1); +} /* -** CAPI3REF: Obtain Aggregate Function Context {F16210} -** -** The implementation of aggregate SQL functions use this routine to allocate -** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is -** is called for a particular aggregate, SQLite allocates nBytes of memory -** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} -** The implementation -** of the aggregate can use the returned buffer to accumulate data. -** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} -** -** The first parameter should be a copy of the -** [sqlite3_context | SQL function context] that is the first -** parameter to the callback routine that implements the aggregate -** function. -** -** This routine must be called from the same thread in which -** the aggregate SQL function is running. +** Hash and comparison functions when the mode is FTS3_HASH_BINARY */ -void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); +static int fts3BinHash(const void *pKey, int nKey){ + int h = 0; + const char *z = (const char *)pKey; + while( nKey-- > 0 ){ + h = (h<<3) ^ h ^ *(z++); + } + return h & 0x7fffffff; +} +static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){ + if( n1!=n2 ) return 1; + return memcmp(pKey1,pKey2,n1); +} /* -** CAPI3REF: User Data For Functions {F16240} +** Return a pointer to the appropriate hash function given the key class. ** -** {F16241} The sqlite3_user_data() interface returns a copy of -** the pointer that was the pUserData parameter (the 5th parameter) -** of the the [sqlite3_create_function()] -** and [sqlite3_create_function16()] routines that originally -** registered the application defined function. {END} +** The C syntax in this function definition may be unfamilar to some +** programmers, so we provide the following additional explanation: ** -** {U16243} This routine must be called from the same thread in which -** the application-defined function is running. +** The name of the function is "ftsHashFunction". The function takes a +** single parameter "keyClass". The return value of ftsHashFunction() +** is a pointer to another function. Specifically, the return value +** of ftsHashFunction() is a pointer to a function that takes two parameters +** with types "const void*" and "int" and returns an "int". */ -void *sqlite3_user_data(sqlite3_context*); +static int (*ftsHashFunction(int keyClass))(const void*,int){ + if( keyClass==FTS3_HASH_STRING ){ + return &fts3StrHash; + }else{ + assert( keyClass==FTS3_HASH_BINARY ); + return &fts3BinHash; + } +} /* -** CAPI3REF: Function Auxiliary Data {F16270} -** -** The following two functions may be used by scalar SQL functions to -** associate meta-data with argument values. If the same value is passed to -** multiple invocations of the same SQL function during query execution, under -** some circumstances the associated meta-data may be preserved. This may -** be used, for example, to add a regular-expression matching scalar -** function. The compiled version of the regular expression is stored as -** meta-data associated with the SQL value passed as the regular expression -** pattern. The compiled regular expression can be reused on multiple -** invocations of the same function so that the original pattern string -** does not need to be recompiled on each invocation. -** -** {F16271} -** The sqlite3_get_auxdata() interface returns a pointer to the meta-data -** associated by the sqlite3_set_auxdata() function with the Nth argument -** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth -** argument of the function, or if the cooresponding function parameter -** has changed since the meta-data was set, then sqlite3_get_auxdata() -** returns a NULL pointer. -** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data -** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent -** calls to sqlite3_get_auxdata() might return this data, if it has -** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor -** function given by the 4th parameter to sqlite3_set_auxdata() on -** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} -** -** In practice, meta-data is preserved between function calls for -** expressions that are constant at compile time. This includes literal -** values and SQL variables. +** Return a pointer to the appropriate hash function given the key class. ** -** These routines must be called from the same thread in which -** the SQL function is running. +** For help in interpreted the obscure C code in the function definition, +** see the header comment on the previous function. +*/ +static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){ + if( keyClass==FTS3_HASH_STRING ){ + return &fts3StrCompare; + }else{ + assert( keyClass==FTS3_HASH_BINARY ); + return &fts3BinCompare; + } +} + +/* Link an element into the hash table */ -void *sqlite3_get_auxdata(sqlite3_context*, int N); -void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); +static void fts3HashInsertElement( + fts3Hash *pH, /* The complete hash table */ + struct _fts3ht *pEntry, /* The entry into which pNew is inserted */ + fts3HashElem *pNew /* The element to be inserted */ +){ + fts3HashElem *pHead; /* First element already in pEntry */ + pHead = pEntry->chain; + if( pHead ){ + pNew->next = pHead; + pNew->prev = pHead->prev; + if( pHead->prev ){ pHead->prev->next = pNew; } + else { pH->first = pNew; } + pHead->prev = pNew; + }else{ + pNew->next = pH->first; + if( pH->first ){ pH->first->prev = pNew; } + pNew->prev = 0; + pH->first = pNew; + } + pEntry->count++; + pEntry->chain = pNew; +} -/* -** CAPI3REF: Constants Defining Special Destructor Behavior {F10280} -** -** These are special value for the destructor that is passed in as the -** final argument to routines like [sqlite3_result_blob()]. If the destructor -** argument is SQLITE_STATIC, it means that the content pointer is constant -** and will never change. It does not need to be destroyed. The -** SQLITE_TRANSIENT value means that the content will likely change in -** the near future and that SQLite should make its own private copy of -** the content before returning. -** -** The typedef is necessary to work around problems in certain -** C++ compilers. See ticket #2191. +/* Resize the hash table so that it cantains "new_size" buckets. +** "new_size" must be a power of 2. The hash table might fail +** to resize if sqliteMalloc() fails. */ -typedef void (*sqlite3_destructor_type)(void*); -#define SQLITE_STATIC ((sqlite3_destructor_type)0) -#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) +static void fts3Rehash(fts3Hash *pH, int new_size){ + struct _fts3ht *new_ht; /* The new hash table */ + fts3HashElem *elem, *next_elem; /* For looping over existing elements */ + int (*xHash)(const void*,int); /* The hash function */ -/* -** CAPI3REF: Setting The Result Of An SQL Function {F16400} -** -** These routines are used by the xFunc or xFinal callbacks that -** implement SQL functions and aggregates. See -** [sqlite3_create_function()] and [sqlite3_create_function16()] -** for additional information. -** -** These functions work very much like the -** [sqlite3_bind_blob | sqlite3_bind_*] family of functions used -** to bind values to host parameters in prepared statements. -** Refer to the -** [sqlite3_bind_blob | sqlite3_bind_* documentation] for -** additional information. -** -** {F16402} The sqlite3_result_blob() interface sets the result from -** an application defined function to be the BLOB whose content is pointed -** to by the second parameter and which is N bytes long where N is the -** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of -** the application defined function to be a BLOB containing all zero -** bytes and N bytes in size, where N is the value of the 2nd parameter. -** -** {F16407} The sqlite3_result_double() interface sets the result from -** an application defined function to be a floating point value specified -** by its 2nd argument. -** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions -** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the -** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite -** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() -** or sqlite3_result_error16() is negative then SQLite takes as the error -** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or -** sqlite3_result_error16() is non-negative then SQLite takes that many -** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() -** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or -** modify the text after they return without harm. -** -** {F16421} The sqlite3_result_toobig() interface causes SQLite -** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface -** causes SQLite to throw an exception indicating that the a -** memory allocation failed. -** -** {F16431} The sqlite3_result_int() interface sets the return value -** of the application-defined function to be the 32-bit signed integer -** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value -** of the application-defined function to be the 64-bit signed integer -** value given in the 2nd argument. -** -** {F16437} The sqlite3_result_null() interface sets the return value -** of the application-defined function to be NULL. -** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), -** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces -** set the return value of the application-defined function to be -** a text string which is represented as UTF-8, UTF-16 native byte order, -** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from -** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces -** is negative, then SQLite takes result text from the 2nd parameter -** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces -** is non-negative, then as many bytes (not characters) of the text -** pointed to by the 2nd parameter are taken as the application-defined -** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that -** function as the destructor on the text or blob result when it has -** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_STATIC, then -** SQLite assumes that the text or blob result is constant space and -** does not copy the space or call a destructor when it has -** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces -** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT -** then SQLite makes a copy of the result into space obtained from -** from [sqlite3_malloc()] before it returns. -** -** {F16461} The sqlite3_result_value() interface sets the result of -** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The -** sqlite3_result_value() interface makes a copy of the [sqlite3_value] -** so that [sqlite3_value] specified in the parameter may change or -** be deallocated after sqlite3_result_value() returns without harm. -** -** {U16491} These routines are called from within the different thread -** than the one containing the application-defined function that recieved -** the [sqlite3_context] pointer, the results are undefined. + assert( (new_size & (new_size-1))==0 ); + new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) ); + if( new_ht==0 ) return; + fts3HashFree(pH->ht); + pH->ht = new_ht; + pH->htsize = new_size; + xHash = ftsHashFunction(pH->keyClass); + for(elem=pH->first, pH->first=0; elem; elem = next_elem){ + int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); + next_elem = elem->next; + fts3HashInsertElement(pH, &new_ht[h], elem); + } +} + +/* This function (for internal use only) locates an element in an +** hash table that matches the given key. The hash for this key has +** already been computed and is passed as the 4th parameter. +*/ +static fts3HashElem *fts3FindElementByHash( + const fts3Hash *pH, /* The pH to be searched */ + const void *pKey, /* The key we are searching for */ + int nKey, + int h /* The hash for this key. */ +){ + fts3HashElem *elem; /* Used to loop thru the element list */ + int count; /* Number of elements left to test */ + int (*xCompare)(const void*,int,const void*,int); /* comparison function */ + + if( pH->ht ){ + struct _fts3ht *pEntry = &pH->ht[h]; + elem = pEntry->chain; + count = pEntry->count; + xCompare = ftsCompareFunction(pH->keyClass); + while( count-- && elem ){ + if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ + return elem; + } + elem = elem->next; + } + } + return 0; +} + +/* Remove a single entry from the hash table given a pointer to that +** element and a hash on the element's key. +*/ +static void fts3RemoveElementByHash( + fts3Hash *pH, /* The pH containing "elem" */ + fts3HashElem* elem, /* The element to be removed from the pH */ + int h /* Hash value for the element */ +){ + struct _fts3ht *pEntry; + if( elem->prev ){ + elem->prev->next = elem->next; + }else{ + pH->first = elem->next; + } + if( elem->next ){ + elem->next->prev = elem->prev; + } + pEntry = &pH->ht[h]; + if( pEntry->chain==elem ){ + pEntry->chain = elem->next; + } + pEntry->count--; + if( pEntry->count<=0 ){ + pEntry->chain = 0; + } + if( pH->copyKey && elem->pKey ){ + fts3HashFree(elem->pKey); + } + fts3HashFree( elem ); + pH->count--; + if( pH->count<=0 ){ + assert( pH->first==0 ); + assert( pH->count==0 ); + fts3HashClear(pH); + } +} + +/* Attempt to locate an element of the hash table pH with a key +** that matches pKey,nKey. Return the data for this element if it is +** found, or NULL if there is no match. */ -void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_double(sqlite3_context*, double); -void sqlite3_result_error(sqlite3_context*, const char*, int); -void sqlite3_result_error16(sqlite3_context*, const void*, int); -void sqlite3_result_error_toobig(sqlite3_context*); -void sqlite3_result_error_nomem(sqlite3_context*); -void sqlite3_result_int(sqlite3_context*, int); -void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); -void sqlite3_result_null(sqlite3_context*); -void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); -void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); -void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); -void sqlite3_result_value(sqlite3_context*, sqlite3_value*); -void sqlite3_result_zeroblob(sqlite3_context*, int n); +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const fts3Hash *pH, const void *pKey, int nKey){ + int h; /* A hash on key */ + fts3HashElem *elem; /* The element that matches key */ + int (*xHash)(const void*,int); /* The hash function */ -/* -** CAPI3REF: Define New Collating Sequences {F16600} -** -** {F16601} -** These functions are used to add new collation sequences to the -** [sqlite3*] handle specified as the first argument. -** -** {F16602} -** The name of the new collation sequence is specified as a UTF-8 string -** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases -** the name is passed as the second function argument. -** -** {F16604} -** The third argument may be one of the constants [SQLITE_UTF8], -** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied -** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The -** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that -** the routine expects pointers to 16-bit word aligned strings -** of UTF16 in the native byte order of the host computer. -** -** {F16607} -** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation -** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application -** supplied function is invoked, it is passed a copy of the void* passed as -** the fourth argument to sqlite3_create_collation() or -** sqlite3_create_collation16() as its first parameter. -** -** {F16612} -** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding -** that was passed as the third argument when the collation sequence was -** registered. {END} The application defined collation routine should -** return negative, zero or positive if -** the first string is less than, equal to, or greater than the second -** string. i.e. (STRING1 - STRING2). -** -** {F16615} -** The sqlite3_create_collation_v2() works like sqlite3_create_collation() -** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is -** destroyed and is passed a copy of the fourth parameter void* pointer -** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when -** they are overridden by later calls to the collation creation functions -** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. -*/ -int sqlite3_create_collation( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); -int sqlite3_create_collation_v2( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*), - void(*xDestroy)(void*) -); -int sqlite3_create_collation16( - sqlite3*, - const char *zName, - int eTextRep, - void*, - int(*xCompare)(void*,int,const void*,int,const void*) -); + if( pH==0 || pH->ht==0 ) return 0; + xHash = ftsHashFunction(pH->keyClass); + assert( xHash!=0 ); + h = (*xHash)(pKey,nKey); + assert( (pH->htsize & (pH->htsize-1))==0 ); + elem = fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1)); + return elem ? elem->data : 0; +} -/* -** CAPI3REF: Collation Needed Callbacks {F16700} -** -** {F16701} -** To avoid having to register all collation sequences before a database -** can be used, a single callback function may be registered with the -** database handle to be called whenever an undefined collation sequence is -** required. +/* Insert an element into the hash table pH. The key is pKey,nKey +** and the data is "data". ** -** {F16702} -** If the function is registered using the sqlite3_collation_needed() API, -** then it is passed the names of undefined collation sequences as strings -** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either -** function replaces any existing callback. +** If no element exists with a matching key, then a new +** element is created. A copy of the key is made if the copyKey +** flag is set. NULL is returned. ** -** {F16705} When the callback is invoked, the first argument passed is a copy -** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], -** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most -** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} +** If another element already exists with the same key, then the +** new data replaces the old data and the old data is returned. +** The key is not copied in this instance. If a malloc fails, then +** the new data is returned and the hash table is unchanged. ** -** The callback function should register the desired collation using -** [sqlite3_create_collation()], [sqlite3_create_collation16()], or -** [sqlite3_create_collation_v2()]. +** If the "data" parameter to this function is NULL, then the +** element corresponding to "key" is removed from the hash table. */ -int sqlite3_collation_needed( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const char*) -); -int sqlite3_collation_needed16( - sqlite3*, - void*, - void(*)(void*,sqlite3*,int eTextRep,const void*) -); +SQLITE_PRIVATE void *sqlite3Fts3HashInsert( + fts3Hash *pH, /* The hash table to insert into */ + const void *pKey, /* The key */ + int nKey, /* Number of bytes in the key */ + void *data /* The data */ +){ + int hraw; /* Raw hash value of the key */ + int h; /* the hash of the key modulo hash table size */ + fts3HashElem *elem; /* Used to loop thru the element list */ + fts3HashElem *new_elem; /* New element added to the pH */ + int (*xHash)(const void*,int); /* The hash function */ -/* -** Specify the key for an encrypted database. This routine should be -** called right after sqlite3_open(). -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_key( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The key */ -); + assert( pH!=0 ); + xHash = ftsHashFunction(pH->keyClass); + assert( xHash!=0 ); + hraw = (*xHash)(pKey, nKey); + assert( (pH->htsize & (pH->htsize-1))==0 ); + h = hraw & (pH->htsize-1); + elem = fts3FindElementByHash(pH,pKey,nKey,h); + if( elem ){ + void *old_data = elem->data; + if( data==0 ){ + fts3RemoveElementByHash(pH,elem,h); + }else{ + elem->data = data; + } + return old_data; + } + if( data==0 ) return 0; + new_elem = (fts3HashElem*)fts3HashMalloc( sizeof(fts3HashElem) ); + if( new_elem==0 ) return data; + if( pH->copyKey && pKey!=0 ){ + new_elem->pKey = fts3HashMalloc( nKey ); + if( new_elem->pKey==0 ){ + fts3HashFree(new_elem); + return data; + } + memcpy((void*)new_elem->pKey, pKey, nKey); + }else{ + new_elem->pKey = (void*)pKey; + } + new_elem->nKey = nKey; + pH->count++; + if( pH->htsize==0 ){ + fts3Rehash(pH,8); + if( pH->htsize==0 ){ + pH->count = 0; + fts3HashFree(new_elem); + return data; + } + } + if( pH->count > pH->htsize ){ + fts3Rehash(pH,pH->htsize*2); + } + assert( pH->htsize>0 ); + assert( (pH->htsize & (pH->htsize-1))==0 ); + h = hraw & (pH->htsize-1); + fts3HashInsertElement(pH, &pH->ht[h], new_elem); + new_elem->data = data; + return 0; +} -/* -** Change the key on an open database. If the current database is not -** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the -** database is decrypted. -** -** The code to implement this API is not available in the public release -** of SQLite. -*/ -int sqlite3_rekey( - sqlite3 *db, /* Database to be rekeyed */ - const void *pKey, int nKey /* The new key */ -); +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ +/************** End of fts3_hash.c *******************************************/ +/************** Begin file fts3_porter.c *************************************/ /* -** CAPI3REF: Suspend Execution For A Short Time {F10530} +** 2006 September 30 ** -** {F10531} The sqlite3_sleep() function -** causes the current thread to suspend execution -** for at least a number of milliseconds specified in its parameter. +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: ** -** {F10532} If the operating system does not support sleep requests with -** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually -** requested from the operating system is returned. +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. ** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} +************************************************************************* +** Implementation of the full-text-search tokenizer that implements +** a Porter stemmer. */ -int sqlite3_sleep(int); /* -** CAPI3REF: Name Of The Folder Holding Temporary Files {F10310} +** The code in this file is only compiled if: ** -** If this global variable is made to point to a string which is -** the name of a folder (a.ka. directory), then all temporary files -** created by SQLite will be placed in that directory. If this variable -** is NULL pointer, then SQLite does a search for an appropriate temporary -** file directory. +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or ** -** It is not safe to modify this variable once a database connection -** has been opened. It is intended that this variable be set once -** as part of process initialization and before any SQLite interface -** routines have been call and remain unchanged thereafter. +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ -SQLITE_EXTERN char *sqlite3_temp_directory; +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) -/* -** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} -** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or -** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} -** -** If certain kinds of errors occur on a statement within a multi-statement -** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], -** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to -** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} -** -** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} -*/ -int sqlite3_get_autocommit(sqlite3*); -/* -** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} -** -** {F13121} The sqlite3_db_handle interface -** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle -** is the same database handle that was -** the first argument to the [sqlite3_prepare_v2()] or its variants -** that was used to create the statement in the first place. -*/ -sqlite3 *sqlite3_db_handle(sqlite3_stmt*); /* -** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} -** -** {F12951} The sqlite3_commit_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback -** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() -** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function -** returns non-zero, then the commit is converted into a rollback. -** -** {F12958} If another function was previously registered, its -** pArg value is returned. Otherwise NULL is returned. -** -** {F12959} Registering a NULL function disables the callback. -** -** {F12961} For the purposes of this API, a transaction is said to have been -** rolled back if an explicit "ROLLBACK" statement is executed, or -** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is -** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is -** rolled back because a commit callback returned non-zero. -** Check on this {END} -** -** These are experimental interfaces and are subject to change. +** Class derived from sqlite3_tokenizer */ -void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); -void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); +typedef struct porter_tokenizer { + sqlite3_tokenizer base; /* Base class */ +} porter_tokenizer; /* -** CAPI3REF: Data Change Notification Callbacks {F12970} -** -** {F12971} The sqlite3_update_hook() interface -** registers a callback function with the database connection identified by the -** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same -** database connection is overridden. -** -** {F12974} The second argument is a pointer to the function to invoke when a -** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is -** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback -** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], -** depending on the operation that caused the callback to be invoked. -** {F12978} The third and -** fourth arguments to the callback contain pointers to the database and -** table name containing the affected row. -** {F12979} The final callback parameter is -** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after -** the update takes place. -** -** {F12983} The update hook is not invoked when internal system tables are -** modified (i.e. sqlite_master and sqlite_sequence). -** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. +** Class derived from sqlit3_tokenizer_cursor */ -void *sqlite3_update_hook( - sqlite3*, - void(*)(void *,int ,char const *,char const *,sqlite3_int64), - void* -); +typedef struct porter_tokenizer_cursor { + sqlite3_tokenizer_cursor base; + const char *zInput; /* input we are tokenizing */ + int nInput; /* size of the input */ + int iOffset; /* current position in zInput */ + int iToken; /* index of next token to be returned */ + char *zToken; /* storage for current token */ + int nAllocated; /* space allocated to zToken buffer */ +} porter_tokenizer_cursor; + + +/* Forward declaration */ +static const sqlite3_tokenizer_module porterTokenizerModule; -/* -** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} -** -** {F10331} -** This routine enables or disables the sharing of the database cache -** and schema data structures between connections to the same database. -** {F10332} -** Sharing is enabled if the argument is true and disabled if the argument -** is false. -** -** {F10333} Cache sharing is enabled and disabled -** for an entire process. {END} This is a change as of SQLite version 3.5.0. -** In prior versions of SQLite, sharing was -** enabled or disabled for each thread separately. -** -** {F10334} -** The cache sharing mode set by this interface effects all subsequent -** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} -** -** Virtual tables cannot be used with a shared cache. {F10336} When shared -** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} -** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} -** -** {F10339} Shared cache is disabled by default. {END} But this might change in -** future releases of SQLite. Applications that care about shared -** cache setting should set it explicitly. -*/ -int sqlite3_enable_shared_cache(int); /* -** CAPI3REF: Attempt To Free Heap Memory {F17340} -** -** {F17341} The sqlite3_release_memory() interface attempts to -** free N bytes of heap memory by deallocating non-essential memory -** allocations held by the database labrary. {END} Memory used -** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns -** the number of bytes actually freed, which might be more or less -** than the amount requested. +** Create a new tokenizer instance. */ -int sqlite3_release_memory(int); +static int porterCreate( + int argc, const char * const *argv, + sqlite3_tokenizer **ppTokenizer +){ + porter_tokenizer *t; + t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t)); + if( t==NULL ) return SQLITE_NOMEM; + memset(t, 0, sizeof(*t)); + *ppTokenizer = &t->base; + return SQLITE_OK; +} /* -** CAPI3REF: Impose A Limit On Heap Size {F17350} -** -** {F16351} The sqlite3_soft_heap_limit() interface -** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested -** that would exceed the soft heap limit, [sqlite3_release_memory()] is -** invoked one or more times to free up some space before the allocation -** is made. {END} -** -** {F16353} The limit is called "soft", because if -** [sqlite3_release_memory()] cannot -** free sufficient memory to prevent the limit from being exceeded, -** the memory is allocated anyway and the current operation proceeds. -** -** {F16354} -** A negative or zero value for N means that there is no soft heap limit and -** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. -** -** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is -** called a "soft" limit. It is advisory only. -** -** Prior to SQLite version 3.5.0, this routine only constrained the memory -** allocated by a single thread - the same thread in which this routine -** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In -** version 3.5.0 there is no mechanism for limiting the heap usage for -** individual threads. +** Destroy a tokenizer */ -void sqlite3_soft_heap_limit(int); +static int porterDestroy(sqlite3_tokenizer *pTokenizer){ + sqlite3_free(pTokenizer); + return SQLITE_OK; +} /* -** CAPI3REF: Extract Metadata About A Column Of A Table {F12850} -** -** This routine -** returns meta-data about a specific column of a specific database -** table accessible using the connection handle passed as the first function -** argument. -** -** The column is identified by the second, third and fourth parameters to -** this function. The second parameter is either the name of the database -** (i.e. "main", "temp" or an attached database) containing the specified -** table or NULL. If it is NULL, then all attached databases are searched -** for the table using the same algorithm as the database engine uses to -** resolve unqualified table references. -** -** The third and fourth parameters to this function are the table and column -** name of the desired column, respectively. Neither of these parameters -** may be NULL. -** -** Meta information is returned by writing to the memory locations passed as -** the 5th and subsequent parameters to this function. Any of these -** arguments may be NULL, in which case the corresponding element of meta -** information is ommitted. -** -**
    -** Parameter     Output Type      Description
    -** -----------------------------------
    -**
    -**   5th         const char*      Data type
    -**   6th         const char*      Name of the default collation sequence 
    -**   7th         int              True if the column has a NOT NULL constraint
    -**   8th         int              True if the column is part of the PRIMARY KEY
    -**   9th         int              True if the column is AUTOINCREMENT
    -** 
    -** -** -** The memory pointed to by the character pointers returned for the -** declaration type and collation sequence is valid only until the next -** call to any sqlite API function. -** -** If the specified table is actually a view, then an error is returned. -** -** If the specified column is "rowid", "oid" or "_rowid_" and an -** INTEGER PRIMARY KEY column has been explicitly declared, then the output -** parameters are set for the explicitly declared column. If there is no -** explicitly declared IPK column, then the output parameters are set as -** follows: -** -**
    -**     data type: "INTEGER"
    -**     collation sequence: "BINARY"
    -**     not null: 0
    -**     primary key: 1
    -**     auto increment: 0
    -** 
    -** -** This function may load one or more schemas from database files. If an -** error occurs during this process, or if the requested table or column -** cannot be found, an SQLITE error code is returned and an error message -** left in the database handle (to be retrieved using sqlite3_errmsg()). -** -** This API is only available if the library was compiled with the -** SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol defined. +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is zInput[0..nInput-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. */ -int sqlite3_table_column_metadata( - sqlite3 *db, /* Connection handle */ - const char *zDbName, /* Database name or NULL */ - const char *zTableName, /* Table name */ - const char *zColumnName, /* Column name */ - char const **pzDataType, /* OUTPUT: Declared data type */ - char const **pzCollSeq, /* OUTPUT: Collation sequence name */ - int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ - int *pPrimaryKey, /* OUTPUT: True if column part of PK */ - int *pAutoinc /* OUTPUT: True if column is auto-increment */ -); +static int porterOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *zInput, int nInput, /* String to be tokenized */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + porter_tokenizer_cursor *c; + + c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + + c->zInput = zInput; + if( zInput==0 ){ + c->nInput = 0; + }else if( nInput<0 ){ + c->nInput = (int)strlen(zInput); + }else{ + c->nInput = nInput; + } + c->iOffset = 0; /* start tokenizing at the beginning */ + c->iToken = 0; + c->zToken = NULL; /* no space allocated, yet. */ + c->nAllocated = 0; + + *ppCursor = &c->base; + return SQLITE_OK; +} /* -** CAPI3REF: Load An Extension {F12600} -** -** {F12601} The sqlite3_load_extension() interface -** attempts to load an SQLite extension library contained in the file -** zFile. {F12602} The entry point is zProc. {F12603} zProc may be 0 -** in which case the name of the entry point defaults -** to "sqlite3_extension_init". -** -** {F12604} The sqlite3_load_extension() interface shall -** return [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. -** -** {F12605} -** If an error occurs and pzErrMsg is not 0, then the -** sqlite3_load_extension() interface shall attempt to fill *pzErrMsg with -** error message text stored in memory obtained from [sqlite3_malloc()]. -** {END} The calling function should free this memory -** by calling [sqlite3_free()]. -** -** {F12606} -** Extension loading must be enabled using [sqlite3_enable_load_extension()] -** prior to calling this API or an error will be returned. +** Close a tokenization cursor previously opened by a call to +** porterOpen() above. */ -int sqlite3_load_extension( - sqlite3 *db, /* Load the extension into this database connection */ - const char *zFile, /* Name of the shared library containing extension */ - const char *zProc, /* Entry point. Derived from zFile if 0 */ - char **pzErrMsg /* Put error message here if not 0 */ -); - +static int porterClose(sqlite3_tokenizer_cursor *pCursor){ + porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; + sqlite3_free(c->zToken); + sqlite3_free(c); + return SQLITE_OK; +} /* -** CAPI3REF: Enable Or Disable Extension Loading {F12620} -** -** So as not to open security holes in older applications that are -** unprepared to deal with extension loading, and as a means of disabling -** extension loading while evaluating user-entered SQL, the following -** API is provided to turn the [sqlite3_load_extension()] mechanism on and -** off. {F12622} It is off by default. {END} See ticket #1863. -** -** {F12621} Call the sqlite3_enable_load_extension() routine -** with onoff==1 to turn extension loading on -** and call it with onoff==0 to turn it back off again. {END} +** Vowel or consonant */ -int sqlite3_enable_load_extension(sqlite3 *db, int onoff); +static const char cType[] = { + 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 2, 1 +}; /* -** CAPI3REF: Make Arrangements To Automatically Load An Extension {F12640} +** isConsonant() and isVowel() determine if their first character in +** the string they point to is a consonant or a vowel, according +** to Porter ruls. ** -** {F12641} This function -** registers an extension entry point that is automatically invoked -** whenever a new database connection is opened using -** [sqlite3_open()], [sqlite3_open16()], or [sqlite3_open_v2()]. {END} +** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'. +** 'Y' is a consonant unless it follows another consonant, +** in which case it is a vowel. ** -** This API can be invoked at program startup in order to register -** one or more statically linked extensions that will be available -** to all new database connections. +** In these routine, the letters are in reverse order. So the 'y' rule +** is that 'y' is a consonant unless it is followed by another +** consonent. +*/ +static int isVowel(const char*); +static int isConsonant(const char *z){ + int j; + char x = *z; + if( x==0 ) return 0; + assert( x>='a' && x<='z' ); + j = cType[x-'a']; + if( j<2 ) return j; + return z[1]==0 || isVowel(z + 1); +} +static int isVowel(const char *z){ + int j; + char x = *z; + if( x==0 ) return 0; + assert( x>='a' && x<='z' ); + j = cType[x-'a']; + if( j<2 ) return 1-j; + return isConsonant(z + 1); +} + +/* +** Let any sequence of one or more vowels be represented by V and let +** C be sequence of one or more consonants. Then every word can be +** represented as: ** -** {F12642} Duplicate extensions are detected so calling this routine multiple -** times with the same extension is harmless. +** [C] (VC){m} [V] ** -** {F12643} This routine stores a pointer to the extension in an array -** that is obtained from sqlite_malloc(). {END} If you run a memory leak -** checker on your program and it reports a leak because of this -** array, then invoke [sqlite3_reset_auto_extension()] prior -** to shutdown to free the memory. +** In prose: A word is an optional consonant followed by zero or +** vowel-consonant pairs followed by an optional vowel. "m" is the +** number of vowel consonant pairs. This routine computes the value +** of m for the first i bytes of a word. ** -** {F12644} Automatic extensions apply across all threads. {END} +** Return true if the m-value for z is 1 or more. In other words, +** return true if z contains at least one vowel that is followed +** by a consonant. ** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. +** In this routine z[] is in reverse order. So we are really looking +** for an instance of of a consonant followed by a vowel. */ -int sqlite3_auto_extension(void *xEntryPoint); +static int m_gt_0(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + return *z!=0; +} +/* Like mgt0 above except we are looking for a value of m which is +** exactly 1 +*/ +static int m_eq_1(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + if( *z==0 ) return 0; + while( isVowel(z) ){ z++; } + if( *z==0 ) return 1; + while( isConsonant(z) ){ z++; } + return *z==0; +} -/* -** CAPI3REF: Reset Automatic Extension Loading {F12660} -** -** {F12661} This function disables all previously registered -** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] -** calls. -** -** {F12662} This call disabled automatic extensions in all threads. {END} -** -** This interface is experimental and is subject to change or -** removal in future releases of SQLite. +/* Like mgt0 above except we are looking for a value of m>1 instead +** or m>0 */ -void sqlite3_reset_auto_extension(void); +static int m_gt_1(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + if( *z==0 ) return 0; + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + return *z!=0; +} +/* +** Return TRUE if there is a vowel anywhere within z[0..n-1] +*/ +static int hasVowel(const char *z){ + while( isConsonant(z) ){ z++; } + return *z!=0; +} /* -****** EXPERIMENTAL - subject to change without notice ************** +** Return TRUE if the word ends in a double consonant. ** -** The interface to the virtual-table mechanism is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. +** The text is reversed here. So we are really looking at +** the first two characters of z[]. +*/ +static int doubleConsonant(const char *z){ + return isConsonant(z) && z[0]==z[1] && isConsonant(z+1); +} + +/* +** Return TRUE if the word ends with three letters which +** are consonant-vowel-consonent and where the final consonant +** is not 'w', 'x', or 'y'. ** -** When the virtual-table mechanism stablizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. +** The word is reversed here. So we are really checking the +** first three letters and the first one cannot be in [wxy]. */ +static int star_oh(const char *z){ + return + z[0]!=0 && isConsonant(z) && + z[0]!='w' && z[0]!='x' && z[0]!='y' && + z[1]!=0 && isVowel(z+1) && + z[2]!=0 && isConsonant(z+2); +} /* -** Structures used by the virtual table interface +** If the word ends with zFrom and xCond() is true for the stem +** of the word that preceeds the zFrom ending, then change the +** ending to zTo. +** +** The input word *pz and zFrom are both in reverse order. zTo +** is in normal order. +** +** Return TRUE if zFrom matches. Return FALSE if zFrom does not +** match. Not that TRUE is returned even if xCond() fails and +** no substitution occurs. */ -typedef struct sqlite3_vtab sqlite3_vtab; -typedef struct sqlite3_index_info sqlite3_index_info; -typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; -typedef struct sqlite3_module sqlite3_module; +static int stem( + char **pz, /* The word being stemmed (Reversed) */ + const char *zFrom, /* If the ending matches this... (Reversed) */ + const char *zTo, /* ... change the ending to this (not reversed) */ + int (*xCond)(const char*) /* Condition that must be true */ +){ + char *z = *pz; + while( *zFrom && *zFrom==*z ){ z++; zFrom++; } + if( *zFrom!=0 ) return 0; + if( xCond && !xCond(z) ) return 1; + while( *zTo ){ + *(--z) = *(zTo++); + } + *pz = z; + return 1; +} /* -** A module is a class of virtual tables. Each module is defined -** by an instance of the following structure. This structure consists -** mostly of methods for the module. +** This is the fallback stemmer used when the porter stemmer is +** inappropriate. The input word is copied into the output with +** US-ASCII case folding. If the input word is too long (more +** than 20 bytes if it contains no digits or more than 6 bytes if +** it contains digits) then word is truncated to 20 or 6 bytes +** by taking 10 or 3 bytes from the beginning and end. */ -struct sqlite3_module { - int iVersion; - int (*xCreate)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xConnect)(sqlite3*, void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVTab, char**); - int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); - int (*xDisconnect)(sqlite3_vtab *pVTab); - int (*xDestroy)(sqlite3_vtab *pVTab); - int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); - int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, - int argc, sqlite3_value **argv); - int (*xNext)(sqlite3_vtab_cursor*); - int (*xEof)(sqlite3_vtab_cursor*); - int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); - int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); - int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); - int (*xBegin)(sqlite3_vtab *pVTab); - int (*xSync)(sqlite3_vtab *pVTab); - int (*xCommit)(sqlite3_vtab *pVTab); - int (*xRollback)(sqlite3_vtab *pVTab); - int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), - void **ppArg); +static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ + int i, mx, j; + int hasDigit = 0; + for(i=0; i='A' && c<='Z' ){ + zOut[i] = c - 'A' + 'a'; + }else{ + if( c>='0' && c<='9' ) hasDigit = 1; + zOut[i] = c; + } + } + mx = hasDigit ? 3 : 10; + if( nIn>mx*2 ){ + for(j=mx, i=nIn-mx; i0 then -** the right-hand side of the corresponding aConstraint[] is evaluated -** and becomes the argvIndex-th entry in argv. If aConstraintUsage[].omit -** is true, then the constraint is assumed to be fully handled by the -** virtual table and is not checked again by SQLite. +** Stem the input word zIn[0..nIn-1]. Store the output in zOut. +** zOut is at least big enough to hold nIn bytes. Write the actual +** size of the output word (exclusive of the '\0' terminator) into *pnOut. ** -** The idxNum and idxPtr values are recorded and passed into xFilter. -** sqlite3_free() is used to free idxPtr if needToFreeIdxPtr is true. +** Any upper-case characters in the US-ASCII character set ([A-Z]) +** are converted to lower case. Upper-case UTF characters are +** unchanged. ** -** The orderByConsumed means that output from xFilter will occur in -** the correct order to satisfy the ORDER BY clause so that no separate -** sorting step is required. +** Words that are longer than about 20 bytes are stemmed by retaining +** a few bytes from the beginning and the end of the word. If the +** word contains digits, 3 bytes are taken from the beginning and +** 3 bytes from the end. For long words without digits, 10 bytes +** are taken from each end. US-ASCII case folding still applies. +** +** If the input word contains not digits but does characters not +** in [a-zA-Z] then no stemming is attempted and this routine just +** copies the input into the input into the output with US-ASCII +** case folding. ** -** The estimatedCost value is an estimate of the cost of doing the -** particular lookup. A full scan of a table with N entries should have -** a cost of N. A binary search of a table of N entries should have a -** cost of approximately log(N). +** Stemming never increases the length of the word. So there is +** no chance of overflowing the zOut buffer. */ -struct sqlite3_index_info { - /* Inputs */ - int nConstraint; /* Number of entries in aConstraint */ - struct sqlite3_index_constraint { - int iColumn; /* Column on left-hand side of constraint */ - unsigned char op; /* Constraint operator */ - unsigned char usable; /* True if this constraint is usable */ - int iTermOffset; /* Used internally - xBestIndex should ignore */ - } *aConstraint; /* Table of WHERE clause constraints */ - int nOrderBy; /* Number of terms in the ORDER BY clause */ - struct sqlite3_index_orderby { - int iColumn; /* Column number */ - unsigned char desc; /* True for DESC. False for ASC. */ - } *aOrderBy; /* The ORDER BY clause */ +static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ + int i, j, c; + char zReverse[28]; + char *z, *z2; + if( nIn<3 || nIn>=sizeof(zReverse)-7 ){ + /* The word is too big or too small for the porter stemmer. + ** Fallback to the copy stemmer */ + copy_stemmer(zIn, nIn, zOut, pnOut); + return; + } + for(i=0, j=sizeof(zReverse)-6; i='A' && c<='Z' ){ + zReverse[j] = c + 'a' - 'A'; + }else if( c>='a' && c<='z' ){ + zReverse[j] = c; + }else{ + /* The use of a character not in [a-zA-Z] means that we fallback + ** to the copy stemmer */ + copy_stemmer(zIn, nIn, zOut, pnOut); + return; + } + } + memset(&zReverse[sizeof(zReverse)-5], 0, 5); + z = &zReverse[j+1]; - /* Outputs */ - struct sqlite3_index_constraint_usage { - int argvIndex; /* if >0, constraint is part of argv to xFilter */ - unsigned char omit; /* Do not code a test for this constraint */ - } *aConstraintUsage; - int idxNum; /* Number used to identify the index */ - char *idxStr; /* String, possibly obtained from sqlite3_malloc */ - int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ - int orderByConsumed; /* True if output is already ordered */ - double estimatedCost; /* Estimated cost of using this index */ -}; -#define SQLITE_INDEX_CONSTRAINT_EQ 2 -#define SQLITE_INDEX_CONSTRAINT_GT 4 -#define SQLITE_INDEX_CONSTRAINT_LE 8 -#define SQLITE_INDEX_CONSTRAINT_LT 16 -#define SQLITE_INDEX_CONSTRAINT_GE 32 -#define SQLITE_INDEX_CONSTRAINT_MATCH 64 -/* -** This routine is used to register a new module name with an SQLite -** connection. Module names must be registered before creating new -** virtual tables on the module, or before using preexisting virtual -** tables of the module. -*/ -int sqlite3_create_module( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void * /* Client data for xCreate/xConnect */ -); + /* Step 1a */ + if( z[0]=='s' ){ + if( + !stem(&z, "sess", "ss", 0) && + !stem(&z, "sei", "i", 0) && + !stem(&z, "ss", "ss", 0) + ){ + z++; + } + } + + /* Step 1b */ + z2 = z; + if( stem(&z, "dee", "ee", m_gt_0) ){ + /* Do nothing. The work was all in the test */ + }else if( + (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel)) + && z!=z2 + ){ + if( stem(&z, "ta", "ate", 0) || + stem(&z, "lb", "ble", 0) || + stem(&z, "zi", "ize", 0) ){ + /* Do nothing. The work was all in the test */ + }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){ + z++; + }else if( m_eq_1(z) && star_oh(z) ){ + *(--z) = 'e'; + } + } + + /* Step 1c */ + if( z[0]=='y' && hasVowel(z+1) ){ + z[0] = 'i'; + } + + /* Step 2 */ + switch( z[1] ){ + case 'a': + stem(&z, "lanoita", "ate", m_gt_0) || + stem(&z, "lanoit", "tion", m_gt_0); + break; + case 'c': + stem(&z, "icne", "ence", m_gt_0) || + stem(&z, "icna", "ance", m_gt_0); + break; + case 'e': + stem(&z, "rezi", "ize", m_gt_0); + break; + case 'g': + stem(&z, "igol", "log", m_gt_0); + break; + case 'l': + stem(&z, "ilb", "ble", m_gt_0) || + stem(&z, "illa", "al", m_gt_0) || + stem(&z, "iltne", "ent", m_gt_0) || + stem(&z, "ile", "e", m_gt_0) || + stem(&z, "ilsuo", "ous", m_gt_0); + break; + case 'o': + stem(&z, "noitazi", "ize", m_gt_0) || + stem(&z, "noita", "ate", m_gt_0) || + stem(&z, "rota", "ate", m_gt_0); + break; + case 's': + stem(&z, "msila", "al", m_gt_0) || + stem(&z, "ssenevi", "ive", m_gt_0) || + stem(&z, "ssenluf", "ful", m_gt_0) || + stem(&z, "ssensuo", "ous", m_gt_0); + break; + case 't': + stem(&z, "itila", "al", m_gt_0) || + stem(&z, "itivi", "ive", m_gt_0) || + stem(&z, "itilib", "ble", m_gt_0); + break; + } + + /* Step 3 */ + switch( z[0] ){ + case 'e': + stem(&z, "etaci", "ic", m_gt_0) || + stem(&z, "evita", "", m_gt_0) || + stem(&z, "ezila", "al", m_gt_0); + break; + case 'i': + stem(&z, "itici", "ic", m_gt_0); + break; + case 'l': + stem(&z, "laci", "ic", m_gt_0) || + stem(&z, "luf", "", m_gt_0); + break; + case 's': + stem(&z, "ssen", "", m_gt_0); + break; + } + + /* Step 4 */ + switch( z[1] ){ + case 'a': + if( z[0]=='l' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'c': + if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){ + z += 4; + } + break; + case 'e': + if( z[0]=='r' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'i': + if( z[0]=='c' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'l': + if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){ + z += 4; + } + break; + case 'n': + if( z[0]=='t' ){ + if( z[2]=='a' ){ + if( m_gt_1(z+3) ){ + z += 3; + } + }else if( z[2]=='e' ){ + stem(&z, "tneme", "", m_gt_1) || + stem(&z, "tnem", "", m_gt_1) || + stem(&z, "tne", "", m_gt_1); + } + } + break; + case 'o': + if( z[0]=='u' ){ + if( m_gt_1(z+2) ){ + z += 2; + } + }else if( z[3]=='s' || z[3]=='t' ){ + stem(&z, "noi", "", m_gt_1); + } + break; + case 's': + if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){ + z += 3; + } + break; + case 't': + stem(&z, "eta", "", m_gt_1) || + stem(&z, "iti", "", m_gt_1); + break; + case 'u': + if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){ + z += 3; + } + break; + case 'v': + case 'z': + if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){ + z += 3; + } + break; + } + + /* Step 5a */ + if( z[0]=='e' ){ + if( m_gt_1(z+1) ){ + z++; + }else if( m_eq_1(z+1) && !star_oh(z+1) ){ + z++; + } + } -/* -** This routine is identical to the sqlite3_create_module() method above, -** except that it allows a destructor function to be specified. It is -** even more experimental than the rest of the virtual tables API. -*/ -int sqlite3_create_module_v2( - sqlite3 *db, /* SQLite connection to register module with */ - const char *zName, /* Name of the module */ - const sqlite3_module *, /* Methods for the module */ - void *, /* Client data for xCreate/xConnect */ - void(*xDestroy)(void*) /* Module destructor function */ -); + /* Step 5b */ + if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){ + z++; + } -/* -** Every module implementation uses a subclass of the following structure -** to describe a particular instance of the module. Each subclass will -** be tailored to the specific needs of the module implementation. The -** purpose of this superclass is to define certain fields that are common -** to all module implementations. -** -** Virtual tables methods can set an error message by assigning a -** string obtained from sqlite3_mprintf() to zErrMsg. The method should -** take care that any prior string is freed by a call to sqlite3_free() -** prior to assigning a new string to zErrMsg. After the error message -** is delivered up to the client application, the string will be automatically -** freed by sqlite3_free() and the zErrMsg field will be zeroed. Note -** that sqlite3_mprintf() and sqlite3_free() are used on the zErrMsg field -** since virtual tables are commonly implemented in loadable extensions which -** do not have access to sqlite3MPrintf() or sqlite3Free(). -*/ -struct sqlite3_vtab { - const sqlite3_module *pModule; /* The module for this virtual table */ - int nRef; /* Used internally */ - char *zErrMsg; /* Error message from sqlite3_mprintf() */ - /* Virtual table implementations will typically add additional fields */ -}; + /* z[] is now the stemmed word in reverse order. Flip it back + ** around into forward order and return. + */ + *pnOut = i = strlen(z); + zOut[i] = 0; + while( *z ){ + zOut[--i] = *(z++); + } +} -/* Every module implementation uses a subclass of the following structure -** to describe cursors that point into the virtual table and are used -** to loop through the virtual table. Cursors are created using the -** xOpen method of the module. Each module implementation will define -** the content of a cursor structure to suit its own needs. -** -** This superclass exists in order to define fields of the cursor that -** are common to all implementations. +/* +** Characters that can be part of a token. We assume any character +** whose value is greater than 0x80 (any UTF character) can be +** part of a token. In other words, delimiters all must have +** values of 0x7f or lower. */ -struct sqlite3_vtab_cursor { - sqlite3_vtab *pVtab; /* Virtual table of this cursor */ - /* Virtual table implementations will typically add additional fields */ +static const char porterIdChar[] = { +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ }; +#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30])) /* -** The xCreate and xConnect methods of a module use the following API -** to declare the format (the names and datatypes of the columns) of -** the virtual tables they implement. +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to porterOpen(). */ -int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); +static int porterNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */ + const char **pzToken, /* OUT: *pzToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; + const char *z = c->zInput; -/* -** Virtual tables can provide alternative implementations of functions -** using the xFindFunction method. But global versions of those functions -** must exist in order to be overloaded. -** -** This API makes sure a global version of a function with a particular -** name and number of parameters exists. If no such function exists -** before this API is called, a new function is created. The implementation -** of the new function always causes an exception to be thrown. So -** the new function is not good for anything by itself. Its only -** purpose is to be a place-holder function that can be overloaded -** by virtual tables. -** -** This API should be considered part of the virtual table interface, -** which is experimental and subject to change. -*/ -int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); + while( c->iOffsetnInput ){ + int iStartOffset, ch; + + /* Scan past delimiter characters */ + while( c->iOffsetnInput && isDelim(z[c->iOffset]) ){ + c->iOffset++; + } + + /* Count non-delimiter characters. */ + iStartOffset = c->iOffset; + while( c->iOffsetnInput && !isDelim(z[c->iOffset]) ){ + c->iOffset++; + } + + if( c->iOffset>iStartOffset ){ + int n = c->iOffset-iStartOffset; + if( n>c->nAllocated ){ + c->nAllocated = n+20; + c->zToken = sqlite3_realloc(c->zToken, c->nAllocated); + if( c->zToken==NULL ) return SQLITE_NOMEM; + } + porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); + *pzToken = c->zToken; + *piStartOffset = iStartOffset; + *piEndOffset = c->iOffset; + *piPosition = c->iToken++; + return SQLITE_OK; + } + } + return SQLITE_DONE; +} /* -** The interface to the virtual-table mechanism defined above (back up -** to a comment remarkably similar to this one) is currently considered -** to be experimental. The interface might change in incompatible ways. -** If this is a problem for you, do not use the interface at this time. -** -** When the virtual-table mechanism stabilizes, we will declare the -** interface fixed, support it indefinitely, and remove this comment. -** -****** EXPERIMENTAL - subject to change without notice ************** +** The set of routines that implement the porter-stemmer tokenizer */ +static const sqlite3_tokenizer_module porterTokenizerModule = { + 0, + porterCreate, + porterDestroy, + porterOpen, + porterClose, + porterNext, +}; /* -** CAPI3REF: A Handle To An Open BLOB {F17800} -** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by -** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. -** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces -** can be used to read or write small subsections of the blob. -** The [sqlite3_blob_bytes()] interface returns the size of the -** blob in bytes. +** Allocate a new porter tokenizer. Return a pointer to the new +** tokenizer in *ppModule */ -typedef struct sqlite3_blob sqlite3_blob; +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &porterTokenizerModule; +} +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_porter.c *****************************************/ +/************** Begin file fts3_tokenizer.c **********************************/ /* -** CAPI3REF: Open A BLOB For Incremental I/O {F17810} +** 2007 June 22 ** -** {F17811} This interfaces opens a handle to the blob located -** in row iRow,, column zColumn, table zTable in database zDb; -** in other words, the same blob that would be selected by: +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: ** -**
    -**     SELECT zColumn FROM zDb.zTable WHERE rowid = iRow;
    -** 
    {END} +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. ** -** {F17812} If the flags parameter is non-zero, the blob is opened for -** read and write access. If it is zero, the blob is opened for read -** access. {END} +****************************************************************************** ** -** {F17813} On success, [SQLITE_OK] is returned and the new -** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and -** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message -** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement +** This is part of an SQLite module implementing full-text search. +** This particular file implements the generic tokenizer interface. */ -int sqlite3_blob_open( - sqlite3*, - const char *zDb, - const char *zTable, - const char *zColumn, - sqlite3_int64 iRow, - int flags, - sqlite3_blob **ppBlob -); /* -** CAPI3REF: Close A BLOB Handle {F17830} -** -** Close an open [sqlite3_blob | blob handle]. +** The code in this file is only compiled if: ** -** {F17831} Closing a BLOB shall cause the current transaction to commit -** if there are no other BLOBs, no pending prepared statements, and the -** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache -** until the close operation if they will fit. {END} -** Closing the BLOB often forces the changes -** out to disk and so if any I/O errors occur, they will likely occur -** at the time when the BLOB is closed. {F17833} Any errors that occur during -** closing are reported as a non-zero return value. +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or ** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns -** an error code, the BLOB is still closed. +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). */ -int sqlite3_blob_close(sqlite3_blob *); +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) -/* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} -** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. -*/ -int sqlite3_blob_bytes(sqlite3_blob *); +#ifndef SQLITE_CORE + SQLITE_EXTENSION_INIT1 +#endif -/* -** CAPI3REF: Read Data From A BLOB Incrementally {F17850} -** -** This function is used to read data from an open -** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. -** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is -** less than zero [SQLITE_ERROR] is returned and no data is read. -** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. -*/ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); /* -** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} +** Implementation of the SQL scalar function for accessing the underlying +** hash table. This function may be called as follows: ** -** This function is used to write data into an open -** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer -** pointed to by z into the open blob, starting at offset iOffset. +** SELECT (); +** SELECT (, ); ** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument -** was not opened for writing (the flags parameter to [sqlite3_blob_open()] -*** was zero), this function returns [SQLITE_READONLY]. +** where is the name passed as the second argument +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). ** -** {F17873} This function may only modify the contents of the blob; it is -** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is -** less than zero [SQLITE_ERROR] is returned and no data is written. +** If the argument is specified, it must be a blob value +** containing a pointer to be stored as the hash data corresponding +** to the string . If is not specified, then +** the string must already exist in the has table. Otherwise, +** an error is returned. ** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** Whether or not the argument is specified, the value returned +** is a blob containing the pointer stored as the hash data corresponding +** to string (after the hash-table is updated, if applicable). */ -int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); +static void scalarFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + fts3Hash *pHash; + void *pPtr = 0; + const unsigned char *zName; + int nName; + + assert( argc==1 || argc==2 ); + + pHash = (fts3Hash *)sqlite3_user_data(context); + + zName = sqlite3_value_text(argv[0]); + nName = sqlite3_value_bytes(argv[0])+1; + + if( argc==2 ){ + void *pOld; + int n = sqlite3_value_bytes(argv[1]); + if( n!=sizeof(pPtr) ){ + sqlite3_result_error(context, "argument type mismatch", -1); + return; + } + pPtr = *(void **)sqlite3_value_blob(argv[1]); + pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); + if( pOld==pPtr ){ + sqlite3_result_error(context, "out of memory", -1); + return; + } + }else{ + pPtr = sqlite3Fts3HashFind(pHash, zName, nName); + if( !pPtr ){ + char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + } + + sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); +} + +#ifdef SQLITE_TEST -/* -** CAPI3REF: Virtual File System Objects {F11200} -** -** A virtual filesystem (VFS) is an [sqlite3_vfs] object -** that SQLite uses to interact -** with the underlying operating system. Most builds come with a -** single default VFS that is appropriate for the host computer. -** New VFSes can be registered and existing VFSes can be unregistered. -** The following interfaces are provided. -** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a -** VFS is registered with a name that is NULL or an empty string, -** then the behavior is undefined. -** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as -** the default. The choice for the new VFS is arbitrary. -*/ -sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); -int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); -int sqlite3_vfs_unregister(sqlite3_vfs*); /* -** CAPI3REF: Mutexes {F17000} -** -** The SQLite core uses these routines for thread -** synchronization. Though they are intended for internal -** use by SQLite, code that links against SQLite is -** permitted to use any of these routines. -** -** The SQLite source code contains multiple implementations -** of these mutex routines. An appropriate implementation -** is selected automatically at compile-time. The following -** implementations are available in the SQLite core: -** -**
      -**
    • SQLITE_MUTEX_OS2 -**
    • SQLITE_MUTEX_PTHREAD -**
    • SQLITE_MUTEX_W32 -**
    • SQLITE_MUTEX_NOOP -**
    -** -** The SQLITE_MUTEX_NOOP implementation is a set of routines -** that does no real locking and is appropriate for use in -** a single-threaded application. The SQLITE_MUTEX_OS2, -** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations -** are appropriate for use on os/2, unix, and windows. -** -** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor -** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex -** implementation is included with the library. The -** mutex interface routines defined here become external -** references in the SQLite library for which implementations -** must be provided by the application. This facility allows an -** application that links against SQLite to provide its own mutex -** implementation without having to modify the SQLite core. -** -** {F17011} The sqlite3_mutex_alloc() routine allocates a new -** mutex and returns a pointer to it. {F17012} If it returns NULL -** that means that a mutex could not be allocated. {F17013} SQLite -** will unwind its stack and return an error. {F17014} The argument -** to sqlite3_mutex_alloc() is one of these integer constants: -** -**
      -**
    • SQLITE_MUTEX_FAST -**
    • SQLITE_MUTEX_RECURSIVE -**
    • SQLITE_MUTEX_STATIC_MASTER -**
    • SQLITE_MUTEX_STATIC_MEM -**
    • SQLITE_MUTEX_STATIC_MEM2 -**
    • SQLITE_MUTEX_STATIC_PRNG -**
    • SQLITE_MUTEX_STATIC_LRU -**
    {END} -** -** {F17015} The first two constants cause sqlite3_mutex_alloc() to create -** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE -** is used but not necessarily so when SQLITE_MUTEX_FAST is used. {END} -** The mutex implementation does not need to make a distinction -** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does -** not want to. {F17016} But SQLite will only request a recursive mutex in -** cases where it really needs one. {END} If a faster non-recursive mutex -** implementation is available on the host platform, the mutex subsystem -** might return such a mutex in response to SQLITE_MUTEX_FAST. -** -** {F17017} The other allowed parameters to sqlite3_mutex_alloc() each return -** a pointer to a static preexisting mutex. {END} Four static mutexes are -** used by the current version of SQLite. Future versions of SQLite -** may add additional static mutexes. Static mutexes are for internal -** use by SQLite only. Applications that use SQLite mutexes should -** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or -** SQLITE_MUTEX_RECURSIVE. +** Implementation of a special SQL scalar function for testing tokenizers +** designed to be used in concert with the Tcl testing framework. This +** function must be called with two arguments: ** -** {F17018} Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST -** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() -** returns a different mutex on every call. {F17034} But for the static -** mutex types, the same mutex is returned on every call that has -** the same type number. {END} +** SELECT (, ); +** SELECT (, ); ** -** {F17019} The sqlite3_mutex_free() routine deallocates a previously -** allocated dynamic mutex. {F17020} SQLite is careful to deallocate every -** dynamic mutex that it allocates. {U17021} The dynamic mutexes must not be in -** use when they are deallocated. {U17022} Attempting to deallocate a static -** mutex results in undefined behavior. {F17023} SQLite never deallocates -** a static mutex. {END} +** where is the name passed as the second argument +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') +** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). ** -** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt -** to enter a mutex. {F17024} If another thread is already within the mutex, -** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return -** SQLITE_BUSY. {F17025} The sqlite3_mutex_try() interface returns SQLITE_OK -** upon successful entry. {F17026} Mutexes created using -** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. -** {F17027} In such cases the, -** mutex must be exited an equal number of times before another thread -** can enter. {U17028} If the same thread tries to enter any other -** kind of mutex more than once, the behavior is undefined. -** {F17029} SQLite will never exhibit -** such behavior in its own use of mutexes. {END} +** The return value is a string that may be interpreted as a Tcl +** list. For each token in the , three elements are +** added to the returned list. The first is the token position, the +** second is the token text (folded, stemmed, etc.) and the third is the +** substring of associated with the token. For example, +** using the built-in "simple" tokenizer: ** -** Some systems (ex: windows95) do not the operation implemented by -** sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() will -** always return SQLITE_BUSY. {F17030} The SQLite core only ever uses -** sqlite3_mutex_try() as an optimization so this is acceptable behavior. {END} +** SELECT fts_tokenizer_test('simple', 'I don't see how'); ** -** {F17031} The sqlite3_mutex_leave() routine exits a mutex that was -** previously entered by the same thread. {U17032} The behavior -** is undefined if the mutex is not currently entered by the -** calling thread or is not currently allocated. {F17033} SQLite will -** never do either. {END} +** will return the string: ** -** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. +** "{0 i I 1 dont don't 2 see see 3 how how}" +** */ -sqlite3_mutex *sqlite3_mutex_alloc(int); -void sqlite3_mutex_free(sqlite3_mutex*); -void sqlite3_mutex_enter(sqlite3_mutex*); -int sqlite3_mutex_try(sqlite3_mutex*); -void sqlite3_mutex_leave(sqlite3_mutex*); +static void testFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + fts3Hash *pHash; + sqlite3_tokenizer_module *p; + sqlite3_tokenizer *pTokenizer = 0; + sqlite3_tokenizer_cursor *pCsr = 0; + + const char *zErr = 0; + + const char *zName; + int nName; + const char *zInput; + int nInput; + + const char *zArg = 0; + + const char *zToken; + int nToken; + int iStart; + int iEnd; + int iPos; + + Tcl_Obj *pRet; + + assert( argc==2 || argc==3 ); + + nName = sqlite3_value_bytes(argv[0]); + zName = (const char *)sqlite3_value_text(argv[0]); + nInput = sqlite3_value_bytes(argv[argc-1]); + zInput = (const char *)sqlite3_value_text(argv[argc-1]); + + if( argc==3 ){ + zArg = (const char *)sqlite3_value_text(argv[1]); + } + + pHash = (fts3Hash *)sqlite3_user_data(context); + p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); + + if( !p ){ + char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + + pRet = Tcl_NewObj(); + Tcl_IncrRefCount(pRet); + + if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){ + zErr = "error in xCreate()"; + goto finish; + } + pTokenizer->pModule = p; + if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){ + zErr = "error in xOpen()"; + goto finish; + } + pCsr->pTokenizer = pTokenizer; + + while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); + zToken = &zInput[iStart]; + nToken = iEnd-iStart; + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); + } + + if( SQLITE_OK!=p->xClose(pCsr) ){ + zErr = "error in xClose()"; + goto finish; + } + if( SQLITE_OK!=p->xDestroy(pTokenizer) ){ + zErr = "error in xDestroy()"; + goto finish; + } + +finish: + if( zErr ){ + sqlite3_result_error(context, zErr, -1); + }else{ + sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); + } + Tcl_DecrRefCount(pRet); +} + +static +int registerTokenizer( + sqlite3 *db, + char *zName, + const sqlite3_tokenizer_module *p +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?, ?)"; + + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); + sqlite3_step(pStmt); + + return sqlite3_finalize(pStmt); +} + +static +int queryTokenizer( + sqlite3 *db, + char *zName, + const sqlite3_tokenizer_module **pp +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?)"; + + *pp = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ + memcpy(pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); + } + } + + return sqlite3_finalize(pStmt); +} + +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); /* -** CAPI3REF: Mutex Verifcation Routines {F17080} +** Implementation of the scalar function fts3_tokenizer_internal_test(). +** This function is used for testing only, it is not included in the +** build unless SQLITE_TEST is defined. ** -** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines -** are intended for use inside assert() statements. {F17081} The SQLite core -** never uses these routines except inside an assert() and applications -** are advised to follow the lead of the core. {F17082} The core only -** provides implementations for these routines when it is compiled -** with the SQLITE_DEBUG flag. {U17087} External mutex implementations -** are only required to provide these routines if SQLITE_DEBUG is -** defined and if NDEBUG is not defined. +** The purpose of this is to test that the fts3_tokenizer() function +** can be used as designed by the C-code in the queryTokenizer and +** registerTokenizer() functions above. These two functions are repeated +** in the README.tokenizer file as an example, so it is important to +** test them. ** -** {F17083} These routines should return true if the mutex in their argument -** is held or not held, respectively, by the calling thread. {END} +** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar +** function with no arguments. An assert() will fail if a problem is +** detected. i.e.: ** -** {X17084} The implementation is not required to provided versions of these -** routines that actually work. -** If the implementation does not provide working -** versions of these routines, it should at least provide stubs -** that always return true so that one does not get spurious -** assertion failures. {END} +** SELECT fts3_tokenizer_internal_test(); ** -** {F17085} If the argument to sqlite3_mutex_held() is a NULL pointer then -** the routine should return 1. {END} This seems counter-intuitive since -** clearly the mutex cannot be held if it does not exist. But the -** the reason the mutex does not exist is because the build is not -** using mutexes. And we do not want the assert() containing the -** call to sqlite3_mutex_held() to fail, so a non-zero return is -** the appropriate thing to do. {F17086} The sqlite3_mutex_notheld() -** interface should also return 1 when given a NULL pointer. */ -int sqlite3_mutex_held(sqlite3_mutex*); -int sqlite3_mutex_notheld(sqlite3_mutex*); +static void intTestFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int rc; + const sqlite3_tokenizer_module *p1; + const sqlite3_tokenizer_module *p2; + sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); -/* -** CAPI3REF: Mutex Types {F17001} -** -** {F17002} The [sqlite3_mutex_alloc()] interface takes a single argument -** which is one of these integer constants. {END} -*/ -#define SQLITE_MUTEX_FAST 0 -#define SQLITE_MUTEX_RECURSIVE 1 -#define SQLITE_MUTEX_STATIC_MASTER 2 -#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ -#define SQLITE_MUTEX_STATIC_MEM2 4 /* sqlite3_release_memory() */ -#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ -#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ + /* Test the query function */ + sqlite3Fts3SimpleTokenizerModule(&p1); + rc = queryTokenizer(db, "simple", &p2); + assert( rc==SQLITE_OK ); + assert( p1==p2 ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_ERROR ); + assert( p2==0 ); + assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); + + /* Test the storage function */ + rc = registerTokenizer(db, "nosuchtokenizer", p1); + assert( rc==SQLITE_OK ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_OK ); + assert( p2==p1 ); + + sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); +} + +#endif /* -** CAPI3REF: Low-Level Control Of Database Files {F11300} +** Set up SQL objects in database db used to access the contents of +** the hash table pointed to by argument pHash. The hash table must +** been initialised to use string keys, and to take a private copy +** of the key when a value is inserted. i.e. by a call similar to: ** -** {F11301} The [sqlite3_file_control()] interface makes a direct call to the -** xFileControl method for the [sqlite3_io_methods] object associated -** with a particular database identified by the second argument. {F11302} The -** name of the database is the name assigned to the database by the -** ATTACH SQL command that opened the -** database. {F11303} To control the main database file, use the name "main" -** or a NULL pointer. {F11304} The third and fourth parameters to this routine -** are passed directly through to the second and third parameters of -** the xFileControl method. {F11305} The return value of the xFileControl -** method becomes the return value of this routine. +** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); ** -** {F11306} If the second parameter (zDbName) does not match the name of any -** open database file, then SQLITE_ERROR is returned. {F11307} This error -** code is not remembered and will not be recalled by [sqlite3_errcode()] -** or [sqlite3_errmsg()]. {U11308} The underlying xFileControl method might -** also return SQLITE_ERROR. {U11309} There is no way to distinguish between -** an incorrect zDbName and an SQLITE_ERROR return from the underlying -** xFileControl method. {END} +** This function adds a scalar function (see header comment above +** scalarFunc() in this file for details) and, if ENABLE_TABLE is +** defined at compilation time, a temporary virtual table (see header +** comment above struct HashTableVtab) to the database schema. Both +** provide read/write access to the contents of *pHash. ** -** See also: [SQLITE_FCNTL_LOCKSTATE] +** The third argument to this function, zName, is used as the name +** of both the scalar and, if created, the virtual table. */ -int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); +SQLITE_PRIVATE int sqlite3Fts3InitHashTable( + sqlite3 *db, + fts3Hash *pHash, + const char *zName +){ + int rc = SQLITE_OK; + void *p = (void *)pHash; + const int any = SQLITE_ANY; + char *zTest = 0; + char *zTest2 = 0; -/* -** Undo the hack that converts floating point types to integer for -** builds on processors without floating point support. -*/ -#ifdef SQLITE_OMIT_FLOATING_POINT -# undef double +#ifdef SQLITE_TEST + void *pdb = (void *)db; + zTest = sqlite3_mprintf("%s_test", zName); + zTest2 = sqlite3_mprintf("%s_internal_test", zName); + if( !zTest || !zTest2 ){ + rc = SQLITE_NOMEM; + } #endif -#if 0 -} /* End of the 'extern "C"' block */ -#endif + if( rc!=SQLITE_OK + || (rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0)) + || (rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0)) +#ifdef SQLITE_TEST + || (rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0)) + || (rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0)) + || (rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0)) #endif + ); -/************** End of sqlite3.h *********************************************/ -/************** Continuing where we left off in fts3_tokenizer.h *************/ + sqlite3_free(zTest); + sqlite3_free(zTest2); + return rc; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ +/************** End of fts3_tokenizer.c **************************************/ +/************** Begin file fts3_tokenizer1.c *********************************/ /* -** Structures used by the tokenizer interface. When a new tokenizer -** implementation is registered, the caller provides a pointer to -** an sqlite3_tokenizer_module containing pointers to the callback -** functions that make up an implementation. +** 2006 Oct 10 ** -** When an fts3 table is created, it passes any arguments passed to -** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the -** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer -** implementation. The xCreate() function in turn returns an -** sqlite3_tokenizer structure representing the specific tokenizer to -** be used for the fts3 table (customized by the tokenizer clause arguments). +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: ** -** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() -** method is called. It returns an sqlite3_tokenizer_cursor object -** that may be used to tokenize a specific input buffer based on -** the tokenization rules supplied by a specific sqlite3_tokenizer -** object. +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Implementation of the "simple" full-text-search tokenizer. */ -typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; -typedef struct sqlite3_tokenizer sqlite3_tokenizer; -typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; - -struct sqlite3_tokenizer_module { - - /* - ** Structure version. Should always be set to 0. - */ - int iVersion; - - /* - ** Create a new tokenizer. The values in the argv[] array are the - ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL - ** TABLE statement that created the fts3 table. For example, if - ** the following SQL is executed: - ** - ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) - ** - ** then argc is set to 2, and the argv[] array contains pointers - ** to the strings "arg1" and "arg2". - ** - ** This method should return either SQLITE_OK (0), or an SQLite error - ** code. If SQLITE_OK is returned, then *ppTokenizer should be set - ** to point at the newly created tokenizer structure. The generic - ** sqlite3_tokenizer.pModule variable should not be initialised by - ** this callback. The caller will do so. - */ - int (*xCreate)( - int argc, /* Size of argv array */ - const char *const*argv, /* Tokenizer argument strings */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ - ); - - /* - ** Destroy an existing tokenizer. The fts3 module calls this method - ** exactly once for each successful call to xCreate(). - */ - int (*xDestroy)(sqlite3_tokenizer *pTokenizer); - - /* - ** Create a tokenizer cursor to tokenize an input buffer. The caller - ** is responsible for ensuring that the input buffer remains valid - ** until the cursor is closed (using the xClose() method). - */ - int (*xOpen)( - sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ - const char *pInput, int nBytes, /* Input buffer */ - sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ - ); - - /* - ** Destroy an existing tokenizer cursor. The fts3 module calls this - ** method exactly once for each successful call to xOpen(). - */ - int (*xClose)(sqlite3_tokenizer_cursor *pCursor); - - /* - ** Retrieve the next token from the tokenizer cursor pCursor. This - ** method should either return SQLITE_OK and set the values of the - ** "OUT" variables identified below, or SQLITE_DONE to indicate that - ** the end of the buffer has been reached, or an SQLite error code. - ** - ** *ppToken should be set to point at a buffer containing the - ** normalized version of the token (i.e. after any case-folding and/or - ** stemming has been performed). *pnBytes should be set to the length - ** of this buffer in bytes. The input text that generated the token is - ** identified by the byte offsets returned in *piStartOffset and - ** *piEndOffset. - ** - ** The buffer *ppToken is set to point at is managed by the tokenizer - ** implementation. It is only required to be valid until the next call - ** to xNext() or xClose(). - */ - /* TODO(shess) current implementation requires pInput to be - ** nul-terminated. This should either be fixed, or pInput/nBytes - ** should be converted to zInput. - */ - int (*xNext)( - sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ - const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ - int *piStartOffset, /* OUT: Byte offset of token in input buffer */ - int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ - int *piPosition /* OUT: Number of tokens returned before this one */ - ); -}; - -struct sqlite3_tokenizer { - const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ - /* Tokenizer implementations will typically add additional fields */ -}; - -struct sqlite3_tokenizer_cursor { - sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ - /* Tokenizer implementations will typically add additional fields */ -}; -#endif /* _FTS3_TOKENIZER_H_ */ +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) -/************** End of fts3_tokenizer.h **************************************/ -/************** Continuing where we left off in fts3_icu.c *******************/ -#include -#include -#include -#include -typedef struct IcuTokenizer IcuTokenizer; -typedef struct IcuCursor IcuCursor; -struct IcuTokenizer { +typedef struct simple_tokenizer { sqlite3_tokenizer base; - char *zLocale; -}; + char delim[128]; /* flag ASCII delimiters */ +} simple_tokenizer; -struct IcuCursor { +typedef struct simple_tokenizer_cursor { sqlite3_tokenizer_cursor base; + const char *pInput; /* input we are tokenizing */ + int nBytes; /* size of the input */ + int iOffset; /* current position in pInput */ + int iToken; /* index of next token to be returned */ + char *pToken; /* storage for current token */ + int nTokenAllocated; /* space allocated to zToken buffer */ +} simple_tokenizer_cursor; - UBreakIterator *pIter; /* ICU break-iterator object */ - int nChar; /* Number of UChar elements in pInput */ - UChar *aChar; /* Copy of input using utf-16 encoding */ - int *aOffset; /* Offsets of each character in utf-8 input */ - int nBuffer; - char *zBuffer; +/* Forward declaration */ +static const sqlite3_tokenizer_module simpleTokenizerModule; - int iToken; -}; +static int simpleDelim(simple_tokenizer *t, unsigned char c){ + return c<0x80 && t->delim[c]; +} /* ** Create a new tokenizer instance. */ -static int icuCreate( - int argc, /* Number of entries in argv[] */ - const char * const *argv, /* Tokenizer creation arguments */ - sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ +static int simpleCreate( + int argc, const char * const *argv, + sqlite3_tokenizer **ppTokenizer ){ - IcuTokenizer *p; - int n = 0; + simple_tokenizer *t; - if( argc>0 ){ - n = strlen(argv[0])+1; - } - p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n); - if( !p ){ - return SQLITE_NOMEM; - } - memset(p, 0, sizeof(IcuTokenizer)); + t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); + if( t==NULL ) return SQLITE_NOMEM; + memset(t, 0, sizeof(*t)); - if( n ){ - p->zLocale = (char *)&p[1]; - memcpy(p->zLocale, argv[0], n); + /* TODO(shess) Delimiters need to remain the same from run to run, + ** else we need to reindex. One solution would be a meta-table to + ** track such information in the database, then we'd only want this + ** information on the initial create. + */ + if( argc>1 ){ + int i, n = strlen(argv[1]); + for(i=0; i=0x80 ){ + sqlite3_free(t); + return SQLITE_ERROR; + } + t->delim[ch] = 1; + } + } else { + /* Mark non-alphanumeric ASCII characters as delimiters */ + int i; + for(i=1; i<0x80; i++){ + t->delim[i] = !isalnum(i); + } } - *ppTokenizer = (sqlite3_tokenizer *)p; - + *ppTokenizer = &t->base; return SQLITE_OK; } /* ** Destroy a tokenizer */ -static int icuDestroy(sqlite3_tokenizer *pTokenizer){ - IcuTokenizer *p = (IcuTokenizer *)pTokenizer; - sqlite3_free(p); +static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ + sqlite3_free(pTokenizer); return SQLITE_OK; } @@ -113764,85 +84424,49 @@ static int icuDestroy(sqlite3_tokenizer *pTokenizer){ ** used to incrementally tokenize this string is returned in ** *ppCursor. */ -static int icuOpen( +static int simpleOpen( sqlite3_tokenizer *pTokenizer, /* The tokenizer */ - const char *zInput, /* Input string */ - int nInput, /* Length of zInput in bytes */ + const char *pInput, int nBytes, /* String to be tokenized */ sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ ){ - IcuTokenizer *p = (IcuTokenizer *)pTokenizer; - IcuCursor *pCsr; - - const int32_t opt = U_FOLD_CASE_DEFAULT; - UErrorCode status = U_ZERO_ERROR; - int nChar; - - UChar32 c; - int iInput = 0; - int iOut = 0; - - *ppCursor = 0; - - if( -1 == nInput ) nInput = strlen(nInput); - nChar = nInput+1; - pCsr = (IcuCursor *)sqlite3_malloc( - sizeof(IcuCursor) + /* IcuCursor */ - nChar * sizeof(UChar) + /* IcuCursor.aChar[] */ - (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */ - ); - if( !pCsr ){ - return SQLITE_NOMEM; - } - memset(pCsr, 0, sizeof(IcuCursor)); - pCsr->aChar = (UChar *)&pCsr[1]; - pCsr->aOffset = (int *)&pCsr->aChar[nChar]; - - pCsr->aOffset[iOut] = iInput; - U8_NEXT(zInput, iInput, nInput, c); - while( c>0 ){ - int isError = 0; - c = u_foldCase(c, opt); - U16_APPEND(pCsr->aChar, iOut, nChar, c, isError); - if( isError ){ - sqlite3_free(pCsr); - return SQLITE_ERROR; - } - pCsr->aOffset[iOut] = iInput; + simple_tokenizer_cursor *c; - if( iInputpIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status); - if( !U_SUCCESS(status) ){ - sqlite3_free(pCsr); - return SQLITE_ERROR; + c->pInput = pInput; + if( pInput==0 ){ + c->nBytes = 0; + }else if( nBytes<0 ){ + c->nBytes = (int)strlen(pInput); + }else{ + c->nBytes = nBytes; } - pCsr->nChar = iOut; + c->iOffset = 0; /* start tokenizing at the beginning */ + c->iToken = 0; + c->pToken = NULL; /* no space allocated, yet. */ + c->nTokenAllocated = 0; - ubrk_first(pCsr->pIter); - *ppCursor = (sqlite3_tokenizer_cursor *)pCsr; + *ppCursor = &c->base; return SQLITE_OK; } /* -** Close a tokenization cursor previously opened by a call to icuOpen(). +** Close a tokenization cursor previously opened by a call to +** simpleOpen() above. */ -static int icuClose(sqlite3_tokenizer_cursor *pCursor){ - IcuCursor *pCsr = (IcuCursor *)pCursor; - ubrk_close(pCsr->pIter); - sqlite3_free(pCsr->zBuffer); - sqlite3_free(pCsr); +static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ + simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; + sqlite3_free(c->pToken); + sqlite3_free(c); return SQLITE_OK; } /* -** Extract the next token from a tokenization cursor. +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to simpleOpen(). */ -static int icuNext( +static int simpleNext( sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ const char **ppToken, /* OUT: *ppToken is the token text */ int *pnBytes, /* OUT: Number of bytes in token */ @@ -113850,82 +84474,72 @@ static int icuNext( int *piEndOffset, /* OUT: Ending offset of token */ int *piPosition /* OUT: Position integer of token */ ){ - IcuCursor *pCsr = (IcuCursor *)pCursor; - - int iStart = 0; - int iEnd = 0; - int nByte = 0; + simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; + simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; + unsigned char *p = (unsigned char *)c->pInput; - while( iStart==iEnd ){ - UChar32 c; + while( c->iOffsetnBytes ){ + int iStartOffset; - iStart = ubrk_current(pCsr->pIter); - iEnd = ubrk_next(pCsr->pIter); - if( iEnd==UBRK_DONE ){ - return SQLITE_DONE; + /* Scan past delimiter characters */ + while( c->iOffsetnBytes && simpleDelim(t, p[c->iOffset]) ){ + c->iOffset++; } - while( iStartaChar, iWhite, pCsr->nChar, c); - if( u_isspace(c) ){ - iStart = iWhite; - }else{ - break; - } + /* Count non-delimiter characters. */ + iStartOffset = c->iOffset; + while( c->iOffsetnBytes && !simpleDelim(t, p[c->iOffset]) ){ + c->iOffset++; } - assert(iStart<=iEnd); - } - do { - UErrorCode status = U_ZERO_ERROR; - if( nByte ){ - char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte); - if( !zNew ){ - return SQLITE_NOMEM; + if( c->iOffset>iStartOffset ){ + int i, n = c->iOffset-iStartOffset; + if( n>c->nTokenAllocated ){ + c->nTokenAllocated = n+20; + c->pToken = sqlite3_realloc(c->pToken, c->nTokenAllocated); + if( c->pToken==NULL ) return SQLITE_NOMEM; } - pCsr->zBuffer = zNew; - pCsr->nBuffer = nByte; - } - - u_strToUTF8( - pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */ - &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */ - &status /* Output success/failure */ - ); - } while( nByte>pCsr->nBuffer ); - - *ppToken = pCsr->zBuffer; - *pnBytes = nByte; - *piStartOffset = pCsr->aOffset[iStart]; - *piEndOffset = pCsr->aOffset[iEnd]; - *piPosition = pCsr->iToken++; + for(i=0; ipToken[i] = ch<0x80 ? tolower(ch) : ch; + } + *ppToken = c->pToken; + *pnBytes = n; + *piStartOffset = iStartOffset; + *piEndOffset = c->iOffset; + *piPosition = c->iToken++; - return SQLITE_OK; + return SQLITE_OK; + } + } + return SQLITE_DONE; } /* ** The set of routines that implement the simple tokenizer */ -static const sqlite3_tokenizer_module icuTokenizerModule = { - 0, /* iVersion */ - icuCreate, /* xCreate */ - icuDestroy, /* xCreate */ - icuOpen, /* xOpen */ - icuClose, /* xClose */ - icuNext, /* xNext */ +static const sqlite3_tokenizer_module simpleTokenizerModule = { + 0, + simpleCreate, + simpleDestroy, + simpleOpen, + simpleClose, + simpleNext, }; /* -** Set *ppModule to point at the implementation of the ICU tokenizer. +** Allocate a new simple tokenizer. Return a pointer to the new +** tokenizer in *ppModule */ -void sqlite3Fts3IcuTokenizerModule( +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule( sqlite3_tokenizer_module const**ppModule ){ - *ppModule = &icuTokenizerModule; + *ppModule = &simpleTokenizerModule; } -#endif /* defined(SQLITE_ENABLE_ICU) */ #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ -/************** End of fts3_icu.c ********************************************/ +/************** End of fts3_tokenizer1.c *************************************/ diff --git a/libs/sqlite3/sqlite3.h b/libs/sqlite3/sqlite3.h index 955dea0..3d4c513 100644 --- a/libs/sqlite3/sqlite3.h +++ b/libs/sqlite3/sqlite3.h @@ -30,7 +30,7 @@ ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. ** -** @(#) $Id: sqlite.h.in,v 1.278 2007/12/13 21:54:11 drh Exp $ +** @(#) $Id: sqlite.h.in,v 1.291 2008/03/08 12:37:31 drh Exp $ */ #ifndef _SQLITE3_H_ #define _SQLITE3_H_ @@ -65,50 +65,63 @@ extern "C" { /* ** CAPI3REF: Compile-Time Library Version Numbers {F10010} ** -** {F10011} The #define in the sqlite3.h header file named -** SQLITE_VERSION resolves to a string literal that identifies -** the version of the SQLite library in the format "X.Y.Z", where -** X is the major version number, Y is the minor version number and Z -** is the release number. The X.Y.Z might be followed by "alpha" or "beta". -** {END} For example "3.1.1beta". -** -** The X value is always 3 in SQLite. The X value only changes when -** backwards compatibility is broken and we intend to never break -** backwards compatibility. The Y value only changes when +** The SQLITE_VERSION and SQLITE_VERSION_NUMBER #defines in +** the sqlite3.h file specify the version of SQLite with which +** that header file is associated. +** +** The "version" of SQLite is a string of the form "X.Y.Z". +** The phrase "alpha" or "beta" might be appended after the Z. +** The X value is major version number always 3 in SQLite3. +** The X value only changes when backwards compatibility is +** broken and we intend to never break +** backwards compatibility. The Y value is the minor version +** number and only changes when ** there are major feature enhancements that are forwards compatible -** but not backwards compatible. The Z value is incremented with +** but not backwards compatible. The Z value is release number +** and is incremented with ** each release but resets back to 0 when Y is incremented. ** -** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer -** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are as -** with SQLITE_VERSION. {END} For example, for version "3.1.1beta", -** SQLITE_VERSION_NUMBER is set to 3001001. To detect if they are using -** version 3.1.1 or greater at compile time, programs may use the test -** (SQLITE_VERSION_NUMBER>=3001001). -** ** See also: [sqlite3_libversion()] and [sqlite3_libversion_number()]. +** +** INVARIANTS: +** +** {F10011} The SQLITE_VERSION #define in the sqlite3.h header file +** evaluates to a string literal that is the SQLite version +** with which the header file is associated. +** +** {F10014} The SQLITE_VERSION_NUMBER #define resolves to an integer +** with the value (X*1000000 + Y*1000 + Z) where X, Y, and +** Z are the major version, minor version, and release number. */ -#define SQLITE_VERSION "3.5.4" -#define SQLITE_VERSION_NUMBER 3005004 +#define SQLITE_VERSION "3.5.7" +#define SQLITE_VERSION_NUMBER 3005007 /* ** CAPI3REF: Run-Time Library Version Numbers {F10020} +** KEYWORDS: sqlite3_version ** -** {F10021} The sqlite3_libversion_number() interface returns an integer -** equal to [SQLITE_VERSION_NUMBER]. {END} The value returned -** by this routine should only be different from the header values -** if the application is compiled using an sqlite3.h header from a -** different version of SQLite than library. Cautious programmers might +** These features provide the same information as the [SQLITE_VERSION] +** and [SQLITE_VERSION_NUMBER] #defines in the header, but are associated +** with the library instead of the header file. Cautious programmers might ** include a check in their application to verify that ** sqlite3_libversion_number() always returns the value ** [SQLITE_VERSION_NUMBER]. ** -** {F10022} The sqlite3_version[] string constant contains the text of the -** [SQLITE_VERSION] string. {F10023} The sqlite3_libversion() function returns -** a pointer to the sqlite3_version[] string constant. {END} The -** sqlite3_libversion() function -** is provided for DLL users who can only access functions and not +** The sqlite3_libversion() function returns the same information as is +** in the sqlite3_version[] string constant. The function is provided +** for use in DLLs since DLL users usually do not have direct access to string ** constants within the DLL. +** +** INVARIANTS: +** +** {F10021} The [sqlite3_libversion_number()] interface returns an integer +** equal to [SQLITE_VERSION_NUMBER]. +** +** {F10022} The [sqlite3_version] string constant contains the text of the +** [SQLITE_VERSION] string. +** +** {F10023} The [sqlite3_libversion()] function returns +** a pointer to the [sqlite3_version] string constant. */ SQLITE_EXTERN const char sqlite3_version[]; const char *sqlite3_libversion(void); @@ -117,27 +130,32 @@ int sqlite3_libversion_number(void); /* ** CAPI3REF: Test To See If The Library Is Threadsafe {F10100} ** -** {F10101} The sqlite3_threadsafe() routine returns nonzero -** if SQLite was compiled with its mutexes enabled or zero if -** SQLite was compiled with mutexes disabled. {END} If this -** routine returns false, then it is not safe for simultaneously -** running threads to both invoke SQLite interfaces. -** -** Really all this routine does is return true if SQLite was -** compiled with the -DSQLITE_THREADSAFE=1 option and false if -** compiled with -DSQLITE_THREADSAFE=0. If SQLite uses an -** application-defined mutex subsystem, malloc subsystem, collating -** sequence, VFS, SQL function, progress callback, commit hook, -** extension, or other accessories and these add-ons are not -** threadsafe, then clearly the combination will not be threadsafe -** either. Hence, this routine never reports that the library -** is guaranteed to be threadsafe, only when it is guaranteed not -** to be. +** SQLite can be compiled with or without mutexes. When +** the SQLITE_THREADSAFE C preprocessor macro is true, mutexes +** are enabled and SQLite is threadsafe. When that macro is false, +** the mutexes are omitted. Without the mutexes, it is not safe +** to use SQLite from more than one thread. +** +** There is a measurable performance penalty for enabling mutexes. +** So if speed is of utmost importance, it makes sense to disable +** the mutexes. But for maximum safety, mutexes should be enabled. +** The default behavior is for mutexes to be enabled. +** +** This interface can be used by a program to make sure that the +** version of SQLite that it is linking against was compiled with +** the desired setting of the SQLITE_THREADSAFE macro. +** +** INVARIANTS: +** +** {F10101} The [sqlite3_threadsafe()] function returns nonzero if +** SQLite was compiled with its mutexes enabled or zero +** if SQLite was compiled with mutexes disabled. */ int sqlite3_threadsafe(void); /* ** CAPI3REF: Database Connection Handle {F12000} +** KEYWORDS: {database connection} ** ** Each open SQLite database is represented by pointer to an instance of the ** opaque structure named "sqlite3". It is useful to think of an sqlite3 @@ -153,16 +171,22 @@ typedef struct sqlite3 sqlite3; /* ** CAPI3REF: 64-Bit Integer Types {F10200} +** KEYWORDS: sqlite_int64 sqlite_uint64 ** -** Because there is no cross-platform way to specify such types +** Because there is no cross-platform way to specify 64-bit integer types ** SQLite includes typedefs for 64-bit signed and unsigned integers. -** {F10201} The sqlite_int64 and sqlite3_int64 types specify a -** 64-bit signed integer. {F10202} The sqlite_uint64 and -** sqlite3_uint64 types specify a 64-bit unsigned integer. {END} ** ** The sqlite3_int64 and sqlite3_uint64 are the preferred type ** definitions. The sqlite_int64 and sqlite_uint64 types are ** supported for backwards compatibility only. +** +** INVARIANTS: +** +** {F10201} The [sqlite_int64] and [sqlite3_int64] types specify a +** 64-bit signed integer. +** +** {F10202} The [sqlite_uint64] and [sqlite3_uint64] types specify +** a 64-bit unsigned integer. */ #ifdef SQLITE_INT64_TYPE typedef SQLITE_INT64_TYPE sqlite_int64; @@ -188,22 +212,41 @@ typedef sqlite_uint64 sqlite3_uint64; /* ** CAPI3REF: Closing A Database Connection {F12010} ** -** {F12011} The sqlite3_close() interfaces destroys an [sqlite3] object -** allocated by a prior call to [sqlite3_open()], [sqlite3_open16()], or -** [sqlite3_open_v2()]. {F12012} Sqlite3_close() releases all -** memory used by the connection and closes all open files. {END}. +** This routine is the destructor for the [sqlite3] object. +** +** Applications should [sqlite3_finalize | finalize] all +** [prepared statements] and +** [sqlite3_blob_close | close] all [sqlite3_blob | BLOBs] +** associated with the [sqlite3] object prior +** to attempting to close the [sqlite3] object. +** +** What happens to pending transactions? Are they +** rolled back, or abandoned? +** +** INVARIANTS: +** +** {F12011} The [sqlite3_close()] interface destroys an [sqlite3] object +** allocated by a prior call to [sqlite3_open()], +** [sqlite3_open16()], or [sqlite3_open_v2()]. +** +** {F12012} The [sqlite3_close()] function releases all memory used by the +** connection and closes all open files. ** ** {F12013} If the database connection contains -** [sqlite3_stmt | prepared statements] that have not been finalized -** by [sqlite3_finalize()], then sqlite3_close() returns SQLITE_BUSY -** and leaves the connection open. {F12014} Giving sqlite3_close() -** a NULL pointer is a harmless no-op. {END} +** [prepared statements] that have not been +** finalized by [sqlite3_finalize()], then [sqlite3_close()] +** returns [SQLITE_BUSY] and leaves the connection open. +** +** {F12014} Giving sqlite3_close() a NULL pointer is a harmless no-op. ** -** {U12015} Passing this routine a database connection that has already been -** closed results in undefined behavior. {U12016} If other interfaces that -** reference the same database connection are pending (either in the -** same thread or in different threads) when this routine is called, -** then the behavior is undefined and is almost certainly undesirable. +** LIMITATIONS: +** +** {U12015} The parameter to [sqlite3_close()] must be an [sqlite3] object +** pointer previously obtained from [sqlite3_open()] or the +** equivalent, or NULL. +** +** {U12016} The parameter to [sqlite3_close()] must not have been previously +** closed. */ int sqlite3_close(sqlite3 *); @@ -217,60 +260,97 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**); /* ** CAPI3REF: One-Step Query Execution Interface {F12100} ** -** {F12101} The sqlite3_exec() interface evaluates zero or more -** UTF-8 encoded, semicolon-separated SQL statements in the zero-terminated -** string of its second argument. {F12102} The SQL -** statements are evaluated in the context of the database connection -** specified by in the first argument. -** {F12103} SQL statements are prepared one by one using -** [sqlite3_prepare()] or the equivalent, evaluated -** using one or more calls to [sqlite3_step()], then destroyed -** using [sqlite3_finalize()]. {F12104} The return value of -** sqlite3_exec() is SQLITE_OK if all SQL statement run -** successfully. -** -** {F12105} If one or more of the SQL statements handed to -** sqlite3_exec() are queries, then -** the callback function specified by the 3rd parameter is -** invoked once for each row of the query result. {F12106} -** If the callback returns a non-zero value then the query -** is aborted, all subsequent SQL statements -** are skipped and the sqlite3_exec() function returns the [SQLITE_ABORT]. -** -** {F12107} The 4th parameter to sqlite3_exec() is an arbitrary pointer -** that is passed through to the callback function as its first parameter. -** -** {F12108} The 2nd parameter to the callback function is the number of -** columns in the query result. {F12109} The 3rd parameter to the callback -** is an array of pointers to strings holding the values for each column -** as extracted using [sqlite3_column_text()]. NULL values in the result -** set result in a NULL pointer. All other value are in their UTF-8 -** string representation. {F12117} -** The 4th parameter to the callback is an array of strings -** obtained using [sqlite3_column_name()] and holding -** the names of each column, also in UTF-8. -** -** {F12110} The callback function may be NULL, even for queries. A NULL -** callback is not an error. It just means that no callback -** will be invoked. -** -** {F12112} If an error occurs while parsing or evaluating the SQL -** then an appropriate error message is written into memory obtained -** from [sqlite3_malloc()] and *errmsg is made to point to that message -** assuming errmsg is not NULL. -** {U12113} The calling function is responsible for freeing the memory -** using [sqlite3_free()]. -** {F12116} If [sqlite3_malloc()] fails while attempting to generate -** the error message, *errmsg is set to NULL. -** {F12114} If errmsg is NULL then no attempt is made to generate an -** error message. Is the return code SQLITE_NOMEM or the original -** error code? What happens if there are multiple errors? -** Do we get code for the first error, or is the choice of reported -** error arbitrary? -** -** {F12115} The return value is is SQLITE_OK if there are no errors and -** some other [SQLITE_OK | return code] if there is an error. -** The particular return value depends on the type of error. {END} +** The sqlite3_exec() interface is a convenient way of running +** one or more SQL statements without a lot of C code. The +** SQL statements are passed in as the second parameter to +** sqlite3_exec(). The statements are evaluated one by one +** until either an error or an interrupt is encountered or +** until they are all done. The 3rd parameter is an optional +** callback that is invoked once for each row of any query results +** produced by the SQL statements. The 5th parameter tells where +** to write any error messages. +** +** The sqlite3_exec() interface is implemented in terms of +** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()]. +** The sqlite3_exec() routine does nothing that cannot be done +** by [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()]. +** The sqlite3_exec() is just a convenient wrapper. +** +** INVARIANTS: +** +** {F12101} The [sqlite3_exec()] interface evaluates zero or more UTF-8 +** encoded, semicolon-separated, SQL statements in the +** zero-terminated string of its 2nd parameter within the +** context of the [sqlite3] object given in the 1st parameter. +** +** {F12104} The return value of [sqlite3_exec()] is SQLITE_OK if all +** SQL statements run successfully. +** +** {F12105} The return value of [sqlite3_exec()] is an appropriate +** non-zero error code if any SQL statement fails. +** +** {F12107} If one or more of the SQL statements handed to [sqlite3_exec()] +** return results and the 3rd parameter is not NULL, then +** the callback function specified by the 3rd parameter is +** invoked once for each row of result. +** +** {F12110} If the callback returns a non-zero value then [sqlite3_exec()] +** will aborted the SQL statement it is currently evaluating, +** skip all subsequent SQL statements, and return [SQLITE_ABORT]. +** What happens to *errmsg here? Does the result code for +** sqlite3_errcode() get set? +** +** {F12113} The [sqlite3_exec()] routine will pass its 4th parameter through +** as the 1st parameter of the callback. +** +** {F12116} The [sqlite3_exec()] routine sets the 2nd parameter of its +** callback to be the number of columns in the current row of +** result. +** +** {F12119} The [sqlite3_exec()] routine sets the 3rd parameter of its +** callback to be an array of pointers to strings holding the +** values for each column in the current result set row as +** obtained from [sqlite3_column_text()]. +** +** {F12122} The [sqlite3_exec()] routine sets the 4th parameter of its +** callback to be an array of pointers to strings holding the +** names of result columns as obtained from [sqlite3_column_name()]. +** +** {F12125} If the 3rd parameter to [sqlite3_exec()] is NULL then +** [sqlite3_exec()] never invokes a callback. All query +** results are silently discarded. +** +** {F12128} If an error occurs while parsing or evaluating any of the SQL +** statements handed to [sqlite3_exec()] then [sqlite3_exec()] will +** return an [error code] other than [SQLITE_OK]. +** +** {F12131} If an error occurs while parsing or evaluating any of the SQL +** handed to [sqlite3_exec()] and if the 5th parameter (errmsg) +** to [sqlite3_exec()] is not NULL, then an error message is +** allocated using the equivalent of [sqlite3_mprintf()] and +** *errmsg is made to point to that message. +** +** {F12134} The [sqlite3_exec()] routine does not change the value of +** *errmsg if errmsg is NULL or if there are no errors. +** +** {F12137} The [sqlite3_exec()] function sets the error code and message +** accessible via [sqlite3_errcode()], [sqlite3_errmsg()], and +** [sqlite3_errmsg16()]. +** +** LIMITATIONS: +** +** {U12141} The first parameter to [sqlite3_exec()] must be an valid and open +** [database connection]. +** +** {U12142} The database connection must not be closed while +** [sqlite3_exec()] is running. +** +** {U12143} The calling function is should use [sqlite3_free()] to free +** the memory that *errmsg is left pointing at once the error +** message is no longer needed. +** +** {U12145} The SQL statement text in the 2nd parameter to [sqlite3_exec()] +** must remain unchanged while [sqlite3_exec()] is running. */ int sqlite3_exec( sqlite3*, /* An open database */ @@ -282,18 +362,12 @@ int sqlite3_exec( /* ** CAPI3REF: Result Codes {F10210} -** KEYWORDS: SQLITE_OK +** KEYWORDS: SQLITE_OK {error code} {error codes} ** ** Many SQLite functions return an integer result code from the set shown -** above in order to indicates success or failure. -** -** {F10211} The result codes shown here are the only ones returned -** by SQLite in its default configuration. {F10212} However, the -** [sqlite3_extended_result_codes()] API can be used to set a database -** connectoin to return more detailed result codes. {END} +** here in order to indicates success or failure. ** ** See also: [SQLITE_IOERR_READ | extended result codes] -** */ #define SQLITE_OK 0 /* Successful result */ /* beginning-of-error-codes */ @@ -329,6 +403,8 @@ int sqlite3_exec( /* ** CAPI3REF: Extended Result Codes {F10220} +** KEYWORDS: {extended error code} {extended error codes} +** KEYWORDS: {extended result codes} ** ** In its default configuration, SQLite API routines return one of 26 integer ** [SQLITE_OK | result codes]. However, experience has shown that @@ -336,24 +412,30 @@ int sqlite3_exec( ** much information about problems as programmers might like. In an effort to ** address this, newer versions of SQLite (version 3.3.8 and later) include ** support for additional result codes that provide more detailed information -** about errors. {F10221} The extended result codes are enabled or disabled +** about errors. The extended result codes are enabled or disabled ** for each database connection using the [sqlite3_extended_result_codes()] -** API. {END} +** API. ** -** Some of the available extended result codes are listed above. -** We expect the number of extended result codes will be expand -** over time. {U10422} Software that uses extended result codes should expect -** to see new result codes in future releases of SQLite. {END} -** -** {F10223} The symbolic name for an extended result code always contains -** a related primary result code as a prefix. {F10224} Primary result -** codes contain a single "_" character. {F10225} Extended result codes -** contain two or more "_" characters. {F10226} The numeric value of an -** extended result code can be converted to its -** corresponding primary result code by masking off the lower 8 bytes. {END} +** Some of the available extended result codes are listed here. +** One may expect the number of extended result codes will be expand +** over time. Software that uses extended result codes should expect +** to see new result codes in future releases of SQLite. ** ** The SQLITE_OK result code will never be extended. It will always ** be exactly zero. +** +** INVARIANTS: +** +** {F10223} The symbolic name for an extended result code always contains +** a related primary result code as a prefix. +** +** {F10224} Primary result code names contain a single "_" character. +** +** {F10225} Extended result code names contain two or more "_" characters. +** +** {F10226} The numeric value of an extended result code contains the +** numeric value of its corresponding primary result code in +** its least significant 8 bits. */ #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) @@ -371,9 +453,9 @@ int sqlite3_exec( /* ** CAPI3REF: Flags For File Open Operations {F10230} ** -** {F10231} Some combination of the these bit values are used as the -** third argument to the [sqlite3_open_v2()] interface and -** as fourth argument to the xOpen method of the +** These bit values are intended for use in the +** 3rd parameter to the [sqlite3_open_v2()] interface and +** in the 4th parameter to the xOpen method of the ** [sqlite3_vfs] object. */ #define SQLITE_OPEN_READONLY 0x00000001 @@ -392,20 +474,20 @@ int sqlite3_exec( /* ** CAPI3REF: Device Characteristics {F10240} ** -** {F10241} The xDeviceCapabilities method of the [sqlite3_io_methods] +** The xDeviceCapabilities method of the [sqlite3_io_methods] ** object returns an integer which is a vector of the these ** bit values expressing I/O characteristics of the mass storage ** device that holds the file that the [sqlite3_io_methods] -** refers to. {END} +** refers to. ** -** {F10242} The SQLITE_IOCAP_ATOMIC property means that all writes of -** any size are atomic. {F10243} The SQLITE_IOCAP_ATOMICnnn values +** The SQLITE_IOCAP_ATOMIC property means that all writes of +** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values ** mean that writes of blocks that are nnn bytes in size and ** are aligned to an address which is an integer multiple of -** nnn are atomic. {F10244} The SQLITE_IOCAP_SAFE_APPEND value means +** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means ** that when data is appended to a file, the data is appended ** first then the size of the file is extended, never the other -** way around. {F10245} The SQLITE_IOCAP_SEQUENTIAL property means that +** way around. The SQLITE_IOCAP_SEQUENTIAL property means that ** information is written to disk in the same order as calls ** to xWrite(). */ @@ -424,9 +506,9 @@ int sqlite3_exec( /* ** CAPI3REF: File Locking Levels {F10250} ** -** {F10251} SQLite uses one of the following integer values as the second +** SQLite uses one of these integer values as the second ** argument to calls it makes to the xLock() and xUnlock() methods -** of an [sqlite3_io_methods] object. {END} +** of an [sqlite3_io_methods] object. */ #define SQLITE_LOCK_NONE 0 #define SQLITE_LOCK_SHARED 1 @@ -437,14 +519,14 @@ int sqlite3_exec( /* ** CAPI3REF: Synchronization Type Flags {F10260} ** -** {F10261} When SQLite invokes the xSync() method of an -** [sqlite3_io_methods] object it uses a combination of the +** When SQLite invokes the xSync() method of an +** [sqlite3_io_methods] object it uses a combination of ** these integer values as the second argument. ** -** {F10262} When the SQLITE_SYNC_DATAONLY flag is used, it means that the +** When the SQLITE_SYNC_DATAONLY flag is used, it means that the ** sync operation only needs to flush data to mass storage. Inode -** information need not be flushed. {F10263} The SQLITE_SYNC_NORMAL means -** to use normal fsync() semantics. {F10264} The SQLITE_SYNC_FULL flag means +** information need not be flushed. The SQLITE_SYNC_NORMAL flag means +** to use normal fsync() semantics. The SQLITE_SYNC_FULL flag means ** to use Mac OS-X style fullsync instead of fsync(). */ #define SQLITE_SYNC_NORMAL 0x00002 @@ -471,7 +553,7 @@ struct sqlite3_file { ** CAPI3REF: OS Interface File Virtual Methods Object {F11120} ** ** Every file opened by the [sqlite3_vfs] xOpen method contains a pointer to -** an instance of the this object. This object defines the +** an instance of this object. This object defines the ** methods used to perform various operations against the open file. ** ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or @@ -568,11 +650,11 @@ struct sqlite3_io_methods { ** of the [sqlite3_io_methods] object and to the [sqlite3_file_control()] ** interface. ** -** {F11311} The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This -** opcode cases the xFileControl method to write the current state of +** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This +** opcode causes the xFileControl method to write the current state of ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) -** into an integer that the pArg argument points to. {F11312} This capability +** into an integer that the pArg argument points to. This capability ** is used during testing and only needs to be supported when SQLITE_TEST ** is defined. */ @@ -582,8 +664,8 @@ struct sqlite3_io_methods { ** CAPI3REF: Mutex Handle {F17110} ** ** The mutex module within SQLite defines [sqlite3_mutex] to be an -** abstract type for a mutex object. {F17111} The SQLite core never looks -** at the internal representation of an [sqlite3_mutex]. {END} It only +** abstract type for a mutex object. The SQLite core never looks +** at the internal representation of an [sqlite3_mutex]. It only ** deals with pointers to the [sqlite3_mutex] object. ** ** Mutexes are created using [sqlite3_mutex_alloc()]. @@ -605,13 +687,13 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** structure used by this VFS. mxPathname is the maximum length of ** a pathname in this VFS. ** -** Registered vfs modules are kept on a linked list formed by +** Registered sqlite3_vfs objects are kept on a linked list formed by ** the pNext pointer. The [sqlite3_vfs_register()] ** and [sqlite3_vfs_unregister()] interfaces manage this list ** in a thread-safe way. The [sqlite3_vfs_find()] interface ** searches the list. ** -** The pNext field is the only fields in the sqlite3_vfs +** The pNext field is the only field in the sqlite3_vfs ** structure that SQLite will ever modify. SQLite will only access ** or modify this field while holding a particular static mutex. ** The application should never modify anything within the sqlite3_vfs @@ -649,14 +731,14 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** ** The file I/O implementation can use the object type flags to ** changes the way it deals with files. For example, an application -** that does not care about crash recovery or rollback, might make -** the open of a journal file a no-op. Writes to this journal are -** also a no-op. Any attempt to read the journal return SQLITE_IOERR. -** Or the implementation might recognize the a database file will -** be doing page-aligned sector reads and writes in a random order -** and set up its I/O subsystem accordingly. +** that does not care about crash recovery or rollback might make +** the open of a journal file a no-op. Writes to this journal would +** also be no-ops, and any attempt to read the journal would return +** SQLITE_IOERR. Or the implementation might recognize that a database +** file will be doing page-aligned sector reads and writes in a random +** order and set up its I/O subsystem accordingly. ** -** {F11144} SQLite might also add one of the following flags to the xOpen +** SQLite might also add one of the following flags to the xOpen ** method: ** **
      @@ -671,7 +753,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** for exclusive access. This flag is set for all files except ** for the main database file. {END} ** -** {F11148} At least szOsFile bytes of memory is allocated by SQLite +** {F11148} At least szOsFile bytes of memory are allocated by SQLite ** to hold the [sqlite3_file] structure passed as the third ** argument to xOpen. {END} The xOpen method does not have to ** allocate the structure; it should just fill it in. @@ -683,7 +765,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** to test to see if a file is at least readable. {END} The file can be a ** directory. ** -** {F11150} SQLite will always allocate at least mxPathname+1 byte for +** {F11150} SQLite will always allocate at least mxPathname+1 bytes for ** the output buffers for xGetTempname and xFullPathname. {F11151} The exact ** size of the output buffer is also passed as a parameter to both ** methods. {END} If the output buffer is not large enough, SQLITE_CANTOPEN @@ -697,7 +779,7 @@ typedef struct sqlite3_mutex sqlite3_mutex; ** The xRandomness() function attempts to return nBytes bytes ** of good-quality randomness into zOut. The return value is ** the actual number of bytes of randomness obtained. The -** xSleep() method cause the calling thread to sleep for at +** xSleep() method causes the calling thread to sleep for at ** least the number of microseconds given. The xCurrentTime() ** method returns a Julian Day Number for the current date and ** time. @@ -732,7 +814,7 @@ struct sqlite3_vfs { ** ** {F11191} These integer constants can be used as the third parameter to ** the xAccess method of an [sqlite3_vfs] object. {END} They determine -** the kind of what kind of permissions the xAccess method is +** what kind of permissions the xAccess method is ** looking for. {F11192} With SQLITE_ACCESS_EXISTS, the xAccess method ** simply checks to see if the file exists. {F11193} With ** SQLITE_ACCESS_READWRITE, the xAccess method checks to see @@ -747,149 +829,226 @@ struct sqlite3_vfs { /* ** CAPI3REF: Enable Or Disable Extended Result Codes {F12200} ** -** {F12201} The sqlite3_extended_result_codes() routine enables or disables the -** [SQLITE_IOERR_READ | extended result codes] feature on a database -** connection if its 2nd parameter is -** non-zero or zero, respectively. {F12202} -** By default, SQLite API routines return one of only 26 integer -** [SQLITE_OK | result codes]. {F12203} When extended result codes -** are enabled by this routine, the repetoire of result codes can be -** much larger and can (hopefully) provide more detailed information -** about the cause of an error. +** The sqlite3_extended_result_codes() routine enables or disables the +** [SQLITE_IOERR_READ | extended result codes] feature of SQLite. +** The extended result codes are disabled by default for historical +** compatibility. ** -** {F12204} The second argument is a boolean value that turns extended result -** codes on and off. {F12205} Extended result codes are off by default for -** backwards compatibility with older versions of SQLite. +** INVARIANTS: +** +** {F12201} Each new [database connection] has the +** [extended result codes] feature +** disabled by default. +** +** {F12202} The [sqlite3_extended_result_codes(D,F)] interface will enable +** [extended result codes] for the +** [database connection] D if the F parameter +** is true, or disable them if F is false. */ int sqlite3_extended_result_codes(sqlite3*, int onoff); /* ** CAPI3REF: Last Insert Rowid {F12220} ** -** {F12221} Each entry in an SQLite table has a unique 64-bit signed -** integer key called the "rowid". {F12222} The rowid is always available +** Each entry in an SQLite table has a unique 64-bit signed +** integer key called the "rowid". The rowid is always available ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those -** names are not also used by explicitly declared columns. {F12223} If +** names are not also used by explicitly declared columns. If ** the table has a column of type INTEGER PRIMARY KEY then that column -** is another an alias for the rowid. +** is another alias for the rowid. ** -** {F12224} This routine returns the rowid of the most recent +** This routine returns the rowid of the most recent ** successful INSERT into the database from the database connection -** shown in the first argument. {F12225} If no successful inserts +** shown in the first argument. If no successful inserts ** have ever occurred on this database connection, zero is returned. ** -** {F12226} If an INSERT occurs within a trigger, then the rowid of the +** If an INSERT occurs within a trigger, then the rowid of the ** inserted row is returned by this routine as long as the trigger -** is running. {F12227} But once the trigger terminates, the value returned +** is running. But once the trigger terminates, the value returned ** by this routine reverts to the last value inserted before the ** trigger fired. ** -** {F12228} An INSERT that fails due to a constraint violation is not a +** An INSERT that fails due to a constraint violation is not a ** successful insert and does not change the value returned by this -** routine. {F12229} Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, +** routine. Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, ** and INSERT OR ABORT make no changes to the return value of this -** routine when their insertion fails. {F12231} When INSERT OR REPLACE +** routine when their insertion fails. When INSERT OR REPLACE ** encounters a constraint violation, it does not fail. The ** INSERT continues to completion after deleting rows that caused ** the constraint problem so INSERT OR REPLACE will always change ** the return value of this interface. ** -** {UF12232} If another thread does a new insert on the same database connection -** while this routine is running and thus changes the last insert rowid, -** then the return value of this routine is undefined. +** For the purposes of this routine, an insert is considered to +** be successful even if it is subsequently rolled back. +** +** INVARIANTS: +** +** {F12221} The [sqlite3_last_insert_rowid()] function returns the +** rowid of the most recent successful insert done +** on the same database connection and within the same +** trigger context, or zero if there have +** been no qualifying inserts on that connection. +** +** {F12223} The [sqlite3_last_insert_rowid()] function returns +** same value when called from the same trigger context +** immediately before and after a ROLLBACK. +** +** LIMITATIONS: +** +** {U12232} If a separate thread does a new insert on the same +** database connection while the [sqlite3_last_insert_rowid()] +** function is running and thus changes the last insert rowid, +** then the value returned by [sqlite3_last_insert_rowid()] is +** unpredictable and might not equal either the old or the new +** last insert rowid. */ sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); /* ** CAPI3REF: Count The Number Of Rows Modified {F12240} ** -** {F12241} This function returns the number of database rows that were changed +** This function returns the number of database rows that were changed ** or inserted or deleted by the most recently completed SQL statement -** on the connection specified by the first parameter. {F12242} Only +** on the connection specified by the first parameter. Only ** changes that are directly specified by the INSERT, UPDATE, or ** DELETE statement are counted. Auxiliary changes caused by -** triggers are not counted. {F12243} Use the [sqlite3_total_changes()] function +** triggers are not counted. Use the [sqlite3_total_changes()] function ** to find the total number of changes including changes caused by triggers. ** -** {F12244} Within the body of a trigger, the sqlite3_changes() interface +** A "row change" is a change to a single row of a single table +** caused by an INSERT, DELETE, or UPDATE statement. Rows that +** are changed as side effects of REPLACE constraint resolution, +** rollback, ABORT processing, DROP TABLE, or by any other +** mechanisms do not count as direct row changes. +** +** A "trigger context" is a scope of execution that begins and +** ends with the script of a trigger. Most SQL statements are +** evaluated outside of any trigger. This is the "top level" +** trigger context. If a trigger fires from the top level, a +** new trigger context is entered for the duration of that one +** trigger. Subtriggers create subcontexts for their duration. +** +** Calling [sqlite3_exec()] or [sqlite3_step()] recursively does +** not create a new trigger context. +** +** This function returns the number of direct row changes in the +** most recent INSERT, UPDATE, or DELETE statement within the same +** trigger context. +** +** So when called from the top level, this function returns the +** number of changes in the most recent INSERT, UPDATE, or DELETE +** that also occurred at the top level. +** Within the body of a trigger, the sqlite3_changes() interface ** can be called to find the number of ** changes in the most recently completed INSERT, UPDATE, or DELETE ** statement within the body of the same trigger. +** However, the number returned does not include in changes +** caused by subtriggers since they have their own context. ** -** {F12245} All changes are counted, even if they are later undone by a -** ROLLBACK or ABORT. {F12246} Except, changes associated with creating and -** dropping tables are not counted. -** -** {F12247} If a callback invokes [sqlite3_exec()] or [sqlite3_step()] -** recursively, then the changes in the inner, recursive call are -** counted together with the changes in the outer call. -** -** {F12248} SQLite implements the command "DELETE FROM table" without +** SQLite implements the command "DELETE FROM table" without ** a WHERE clause by dropping and recreating the table. (This is much ** faster than going through and deleting individual elements from the -** table.) Because of this optimization, the change count for -** "DELETE FROM table" will be zero regardless of the number of elements -** that were originally in the table. {F12251} To get an accurate count -** of the number of rows deleted, use +** table.) Because of this optimization, the deletions in +** "DELETE FROM table" are not row changes and will not be counted +** by the sqlite3_changes() or [sqlite3_total_changes()] functions. +** To get an accurate count of the number of rows deleted, use ** "DELETE FROM table WHERE 1" instead. ** -** {UF12252} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. +** INVARIANTS: +** +** {F12241} The [sqlite3_changes()] function returns the number of +** row changes caused by the most recent INSERT, UPDATE, +** or DELETE statement on the same database connection and +** within the same trigger context, or zero if there have +** not been any qualifying row changes. +** +** LIMITATIONS: +** +** {U12252} If a separate thread makes changes on the same database connection +** while [sqlite3_changes()] is running then the value returned +** is unpredictable and unmeaningful. */ int sqlite3_changes(sqlite3*); /* ** CAPI3REF: Total Number Of Rows Modified {F12260} *** -** {F12261} This function returns the number of database rows that have been -** modified by INSERT, UPDATE or DELETE statements since the database handle -** was opened. {F12262} The count includes UPDATE, INSERT and DELETE -** statements executed as part of trigger programs. {F12263} All changes +** This function returns the number of row changes caused +** by INSERT, UPDATE or DELETE statements since the database handle +** was opened. The count includes all changes from all trigger +** contexts. But the count does not include changes used to +** implement REPLACE constraints, do rollbacks or ABORT processing, +** or DROP table processing. +** The changes ** are counted as soon as the statement that makes them is completed ** (when the statement handle is passed to [sqlite3_reset()] or -** [sqlite3_finalize()]). {END} -** -** See also the [sqlite3_change()] interface. +** [sqlite3_finalize()]). ** -** {F12265} SQLite implements the command "DELETE FROM table" without +** SQLite implements the command "DELETE FROM table" without ** a WHERE clause by dropping and recreating the table. (This is much ** faster than going -** through and deleting individual elements form the table.) Because of +** through and deleting individual elements from the table.) Because of ** this optimization, the change count for "DELETE FROM table" will be ** zero regardless of the number of elements that were originally in the ** table. To get an accurate count of the number of rows deleted, use ** "DELETE FROM table WHERE 1" instead. ** -** {U12264} If another thread makes changes on the same database connection -** while this routine is running then the return value of this routine -** is undefined. {END} +** See also the [sqlite3_changes()] interface. +** +** INVARIANTS: +** +** {F12261} The [sqlite3_total_changes()] returns the total number +** of row changes caused by INSERT, UPDATE, and/or DELETE +** statements on the same [database connection], in any +** trigger context, since the database connection was +** created. +** +** LIMITATIONS: +** +** {U12264} If a separate thread makes changes on the same database connection +** while [sqlite3_total_changes()] is running then the value +** returned is unpredictable and unmeaningful. */ int sqlite3_total_changes(sqlite3*); /* ** CAPI3REF: Interrupt A Long-Running Query {F12270} ** -** {F12271} This function causes any pending database operation to abort and -** return at its earliest opportunity. {END} This routine is typically +** This function causes any pending database operation to abort and +** return at its earliest opportunity. This routine is typically ** called in response to a user action such as pressing "Cancel" ** or Ctrl-C where the user wants a long query operation to halt ** immediately. ** -** {F12272} It is safe to call this routine from a thread different from the -** thread that is currently running the database operation. {U12273} But it +** It is safe to call this routine from a thread different from the +** thread that is currently running the database operation. But it ** is not safe to call this routine with a database connection that ** is closed or might close before sqlite3_interrupt() returns. ** ** If an SQL is very nearly finished at the time when sqlite3_interrupt() ** is called, then it might not have an opportunity to be interrupted. ** It might continue to completion. -** {F12274} The SQL operation that is interrupted will return -** [SQLITE_INTERRUPT]. {F12275} If the interrupted SQL operation is an +** An SQL operation that is interrupted will return +** [SQLITE_INTERRUPT]. If the interrupted SQL operation is an ** INSERT, UPDATE, or DELETE that is inside an explicit transaction, ** then the entire transaction will be rolled back automatically. -** {F12276} A call to sqlite3_interrupt() has no effect on SQL statements +** A call to sqlite3_interrupt() has no effect on SQL statements ** that are started after sqlite3_interrupt() returns. +** +** INVARIANTS: +** +** {F12271} The [sqlite3_interrupt()] interface will force all running +** SQL statements associated with the same database connection +** to halt after processing at most one additional row of +** data. +** +** {F12272} Any SQL statement that is interrupted by [sqlite3_interrupt()] +** will return [SQLITE_INTERRUPT]. +** +** LIMITATIONS: +** +** {U12279} If the database connection closes while [sqlite3_interrupt()] +** is running then bad things will likely happen. */ void sqlite3_interrupt(sqlite3*); @@ -901,21 +1060,30 @@ void sqlite3_interrupt(sqlite3*); ** if additional input is needed before sending the text into ** SQLite for parsing. These routines return true if the input string ** appears to be a complete SQL statement. A statement is judged to be -** complete if it ends with a semicolon and is not a fragment of a -** CREATE TRIGGER statement. These routines do not parse the SQL and +** complete if it ends with a semicolon token and is not a fragment of a +** CREATE TRIGGER statement. Semicolons that are embedded within +** string literals or quoted identifier names or comments are not +** independent tokens (they are part of the token in which they are +** embedded) and thus do not count as a statement terminator. +** +** These routines do not parse the SQL and ** so will not detect syntactically incorrect SQL. ** -** {F10511} These functions return true if the given input string -** ends with a semicolon optionally followed by whitespace or -** comments. {F10512} For sqlite3_complete(), -** the parameter must be a zero-terminated UTF-8 string. {F10513} For -** sqlite3_complete16(), a zero-terminated machine byte order UTF-16 string -** is required. {F10514} These routines return false if the terminal -** semicolon is within a comment, a string literal or a quoted identifier -** (in other words if the final semicolon is not really a separate token -** but part of a larger token) or if the final semicolon is -** in between the BEGIN and END keywords of a CREATE TRIGGER statement. -** {END} +** INVARIANTS: +** +** {F10511} The sqlite3_complete() and sqlite3_complete16() functions +** return true (non-zero) if and only if the last +** non-whitespace token in their input is a semicolon that +** is not in between the BEGIN and END of a CREATE TRIGGER +** statement. +** +** LIMITATIONS: +** +** {U10512} The input to sqlite3_complete() must be a zero-terminated +** UTF-8 string. +** +** {U10513} The input to sqlite3_complete16() must be a zero-terminated +** UTF-16 string in native byte order. */ int sqlite3_complete(const char *sql); int sqlite3_complete16(const void *sql); @@ -923,29 +1091,29 @@ int sqlite3_complete16(const void *sql); /* ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors {F12310} ** -** {F12311} This routine identifies a callback function that might be +** This routine identifies a callback function that might be ** invoked whenever an attempt is made to open a database table ** that another thread or process has locked. -** {F12312} If the busy callback is NULL, then [SQLITE_BUSY] +** If the busy callback is NULL, then [SQLITE_BUSY] ** or [SQLITE_IOERR_BLOCKED] ** is returned immediately upon encountering the lock. -** {F12313} If the busy callback is not NULL, then the -** callback will be invoked with two arguments. {F12314} The +** If the busy callback is not NULL, then the +** callback will be invoked with two arguments. The ** first argument to the handler is a copy of the void* pointer which -** is the third argument to this routine. {F12315} The second argument to +** is the third argument to this routine. The second argument to ** the handler is the number of times that the busy handler has -** been invoked for this locking event. {F12316} If the +** been invoked for this locking event. If the ** busy callback returns 0, then no additional attempts are made to ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. -** {F12317} If the callback returns non-zero, then another attempt +** If the callback returns non-zero, then another attempt ** is made to open the database for reading and the cycle repeats. ** ** The presence of a busy handler does not guarantee that -** it will be invoked when there is lock contention. {F12319} +** it will be invoked when there is lock contention. ** If SQLite determines that invoking the busy handler could result in ** a deadlock, it will go ahead and return [SQLITE_BUSY] or ** [SQLITE_IOERR_BLOCKED] instead of invoking the -** busy handler. {END} +** busy handler. ** Consider a scenario where one process is holding a read lock that ** it is trying to promote to a reserved lock and ** a second process is holding a reserved lock that it is trying @@ -957,74 +1125,120 @@ int sqlite3_complete16(const void *sql); ** will induce the first process to release its read lock and allow ** the second process to proceed. ** -** {F12321} The default busy callback is NULL. {END} +** The default busy callback is NULL. ** -** {F12322} The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] +** The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] ** when SQLite is in the middle of a large transaction where all the -** changes will not fit into the in-memory cache. {F12323} SQLite will +** changes will not fit into the in-memory cache. SQLite will ** already hold a RESERVED lock on the database file, but it needs ** to promote this lock to EXCLUSIVE so that it can spill cache ** pages into the database file without harm to concurrent -** readers. {F12324} If it is unable to promote the lock, then the in-memory +** readers. If it is unable to promote the lock, then the in-memory ** cache will be left in an inconsistent state and so the error ** code is promoted from the relatively benign [SQLITE_BUSY] to -** the more severe [SQLITE_IOERR_BLOCKED]. {F12325} This error code promotion -** forces an automatic rollback of the changes. {END} See the +** the more severe [SQLITE_IOERR_BLOCKED]. This error code promotion +** forces an automatic rollback of the changes. See the ** ** CorruptionFollowingBusyError wiki page for a discussion of why ** this is important. ** -** {F12326} Sqlite is re-entrant, so the busy handler may start a new -** query. {END} (It is not clear why anyone would every want to do this, -** but it is allowed, in theory.) {U12327} But the busy handler may not -** close the database. Closing the database from a busy handler will delete -** data structures out from under the executing query and will -** probably result in a segmentation fault or other runtime error. {END} -** -** {F12328} There can only be a single busy handler defined for each database +** There can only be a single busy handler defined for each database ** connection. Setting a new busy handler clears any previous one. -** {F12329} Note that calling [sqlite3_busy_timeout()] will also set or clear +** Note that calling [sqlite3_busy_timeout()] will also set or clear ** the busy handler. ** -** {F12331} When operating in [sqlite3_enable_shared_cache | shared cache mode], -** only a single busy handler can be defined for each database file. -** So if two database connections share a single cache, then changing -** the busy handler on one connection will also change the busy -** handler in the other connection. {F12332} The busy handler is invoked -** in the thread that was running when the lock contention occurs. +** INVARIANTS: +** +** {F12311} The [sqlite3_busy_handler()] function replaces the busy handler +** callback in the database connection identified by the 1st +** parameter with a new busy handler identified by the 2nd and 3rd +** parameters. +** +** {F12312} The default busy handler for new database connections is NULL. +** +** {F12314} When two or more database connection share a common cache, +** the busy handler for the database connection currently using +** the cache is invoked when the cache encounters a lock. +** +** {F12316} If a busy handler callback returns zero, then the SQLite +** interface that provoked the locking event will return +** [SQLITE_BUSY]. +** +** {F12318} SQLite will invokes the busy handler with two argument which +** are a copy of the pointer supplied by the 3rd parameter to +** [sqlite3_busy_handler()] and a count of the number of prior +** invocations of the busy handler for the same locking event. +** +** LIMITATIONS: +** +** {U12319} A busy handler should not call close the database connection +** or prepared statement that invoked the busy handler. */ int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); /* ** CAPI3REF: Set A Busy Timeout {F12340} ** -** {F12341} This routine sets a [sqlite3_busy_handler | busy handler] +** This routine sets a [sqlite3_busy_handler | busy handler] ** that sleeps for a while when a -** table is locked. {F12342} The handler will sleep multiple times until +** table is locked. The handler will sleep multiple times until ** at least "ms" milliseconds of sleeping have been done. {F12343} After ** "ms" milliseconds of sleeping, the handler returns 0 which ** causes [sqlite3_step()] to return [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. ** -** {F12344} Calling this routine with an argument less than or equal to zero +** Calling this routine with an argument less than or equal to zero ** turns off all busy handlers. ** -** {F12345} There can only be a single busy handler for a particular database +** There can only be a single busy handler for a particular database ** connection. If another busy handler was defined ** (using [sqlite3_busy_handler()]) prior to calling ** this routine, that other busy handler is cleared. +** +** INVARIANTS: +** +** {F12341} The [sqlite3_busy_timeout()] function overrides any prior +** [sqlite3_busy_timeout()] or [sqlite3_busy_handler()] setting +** on the same database connection. +** +** {F12343} If the 2nd parameter to [sqlite3_busy_timeout()] is less than +** or equal to zero, then the busy handler is cleared so that +** all subsequent locking events immediately return [SQLITE_BUSY]. +** +** {F12344} If the 2nd parameter to [sqlite3_busy_timeout()] is a positive +** number N, then a busy handler is set that repeatedly calls +** the xSleep() method in the VFS interface until either the +** lock clears or until the cumulative sleep time reported back +** by xSleep() exceeds N milliseconds. */ int sqlite3_busy_timeout(sqlite3*, int ms); /* ** CAPI3REF: Convenience Routines For Running Queries {F12370} ** -** This next routine is a convenience wrapper around [sqlite3_exec()]. -** {F12371} Instead of invoking a user-supplied callback for each row of the -** result, this routine remembers each row of the result in memory -** obtained from [sqlite3_malloc()], then returns all of the result after the -** query has finished. {F12372} +** Definition: A result table is memory data structure created by the +** [sqlite3_get_table()] interface. A result table records the +** complete query results from one or more queries. +** +** The table conceptually has a number of rows and columns. But +** these numbers are not part of the result table itself. These +** numbers are obtained separately. Let N be the number of rows +** and M be the number of columns. +** +** A result table is an array of pointers to zero-terminated +** UTF-8 strings. There are (N+1)*M elements in the array. +** The first M pointers point to zero-terminated strings that +** contain the names of the columns. +** The remaining entries all point to query results. NULL +** values are give a NULL pointer. All other values are in +** their UTF-8 zero-terminated string representation as returned by +** [sqlite3_column_text()]. ** -** As an example, suppose the query result where this table: +** A result table might consists of one or more memory allocations. +** It is not safe to pass a result table directly to [sqlite3_free()]. +** A result table should be deallocated using [sqlite3_free_table()]. +** +** As an example of the result table format, suppose a query result +** is as follows: ** **
       **        Name        | Age
      @@ -1034,8 +1248,9 @@ int sqlite3_busy_timeout(sqlite3*, int ms);
       **        Cindy       | 21
       ** 
      ** -** If the 3rd argument were &azResult then after the function returns -** azResult will contain the following data: +** There are two column (M==2) and three rows (N==3). Thus the +** result table has 8 entries. Suppose the result table is stored +** in an array names azResult. Then azResult holds this content: ** **
       **        azResult[0] = "Name";
      @@ -1048,28 +1263,55 @@ int sqlite3_busy_timeout(sqlite3*, int ms);
       **        azResult[7] = "21";
       ** 
      ** -** Notice that there is an extra row of data containing the column -** headers. But the *nrow return value is still 3. *ncolumn is -** set to 2. In general, the number of values inserted into azResult -** will be ((*nrow) + 1)*(*ncolumn). +** The sqlite3_get_table() function evaluates one or more +** semicolon-separated SQL statements in the zero-terminated UTF-8 +** string of its 2nd parameter. It returns a result table to the +** pointer given in its 3rd parameter. ** -** {U12374} After the calling function has finished using the result, it should -** pass the result data pointer to sqlite3_free_table() in order to +** After the calling function has finished using the result, it should +** pass the pointer to the result table to sqlite3_free_table() in order to ** release the memory that was malloc-ed. Because of the way the -** [sqlite3_malloc()] happens, the calling function must not try to call -** [sqlite3_free()] directly. Only [sqlite3_free_table()] is able to release -** the memory properly and safely. {END} -** -** {F12373} The return value of this routine is the same as -** from [sqlite3_exec()]. +** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling +** function must not try to call [sqlite3_free()] directly. Only +** [sqlite3_free_table()] is able to release the memory properly and safely. +** +** The sqlite3_get_table() interface is implemented as a wrapper around +** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access +** to any internal data structures of SQLite. It uses only the public +** interface defined here. As a consequence, errors that occur in the +** wrapper layer outside of the internal [sqlite3_exec()] call are not +** reflected in subsequent calls to [sqlite3_errcode()] or +** [sqlite3_errmsg()]. +** +** INVARIANTS: +** +** {F12371} If a [sqlite3_get_table()] fails a memory allocation, then +** it frees the result table under construction, aborts the +** query in process, skips any subsequent queries, sets the +** *resultp output pointer to NULL and returns [SQLITE_NOMEM]. +** +** {F12373} If the ncolumn parameter to [sqlite3_get_table()] is not NULL +** then [sqlite3_get_table()] write the number of columns in the +** result set of the query into *ncolumn if the query is +** successful (if the function returns SQLITE_OK). +** +** {F12374} If the nrow parameter to [sqlite3_get_table()] is not NULL +** then [sqlite3_get_table()] write the number of rows in the +** result set of the query into *nrow if the query is +** successful (if the function returns SQLITE_OK). +** +** {F12376} The [sqlite3_get_table()] function sets its *ncolumn value +** to the number of columns in the result set of the query in the +** sql parameter, or to zero if the query in sql has an empty +** result set. */ int sqlite3_get_table( - sqlite3*, /* An open database */ - const char *sql, /* SQL to be executed */ - char ***resultp, /* Result written to a char *[] that this points to */ - int *nrow, /* Number of result rows written here */ - int *ncolumn, /* Number of result columns written here */ - char **errmsg /* Error msg written here */ + sqlite3*, /* An open database */ + const char *sql, /* SQL to be evaluated */ + char ***pResult, /* Results of the query */ + int *nrow, /* Number of result rows written here */ + int *ncolumn, /* Number of result columns written here */ + char **errmsg /* Error msg written here */ ); void sqlite3_free_table(char **result); @@ -1079,30 +1321,30 @@ void sqlite3_free_table(char **result); ** These routines are workalikes of the "printf()" family of functions ** from the standard C library. ** -** {F17401} The sqlite3_mprintf() and sqlite3_vmprintf() routines write their +** The sqlite3_mprintf() and sqlite3_vmprintf() routines write their ** results into memory obtained from [sqlite3_malloc()]. -** {U17402} The strings returned by these two routines should be -** released by [sqlite3_free()]. {F17403} Both routines return a +** The strings returned by these two routines should be +** released by [sqlite3_free()]. Both routines return a ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough ** memory to hold the resulting string. ** -** {F17404} In sqlite3_snprintf() routine is similar to "snprintf()" from +** In sqlite3_snprintf() routine is similar to "snprintf()" from ** the standard C library. The result is written into the ** buffer supplied as the second parameter whose size is given by -** the first parameter. {END} Note that the order of the +** the first parameter. Note that the order of the ** first two parameters is reversed from snprintf(). This is an ** historical accident that cannot be fixed without breaking -** backwards compatibility. {F17405} Note also that sqlite3_snprintf() +** backwards compatibility. Note also that sqlite3_snprintf() ** returns a pointer to its buffer instead of the number of -** characters actually written into the buffer. {END} We admit that +** characters actually written into the buffer. We admit that ** the number of characters written would be a more useful return ** value but we cannot change the implementation of sqlite3_snprintf() ** now without breaking compatibility. ** -** {F17406} As long as the buffer size is greater than zero, sqlite3_snprintf() -** guarantees that the buffer is always zero-terminated. {F17407} The first +** As long as the buffer size is greater than zero, sqlite3_snprintf() +** guarantees that the buffer is always zero-terminated. The first ** parameter "n" is the total size of the buffer, including space for -** the zero terminator. {END} So the longest string that can be completely +** the zero terminator. So the longest string that can be completely ** written will be n-1 characters. ** ** These routines all implement some additional formatting @@ -1110,9 +1352,9 @@ void sqlite3_free_table(char **result); ** All of the usual printf formatting options apply. In addition, there ** is are "%q", "%Q", and "%z" options. ** -** {F17410} The %q option works like %s in that it substitutes a null-terminated +** The %q option works like %s in that it substitutes a null-terminated ** string from the argument list. But %q also doubles every '\'' character. -** %q is designed for use inside a string literal. {END} By doubling each '\'' +** %q is designed for use inside a string literal. By doubling each '\'' ** character it escapes that character and allows it to be inserted into ** the string. ** @@ -1148,7 +1390,7 @@ void sqlite3_free_table(char **result); ** should always use %q instead of %s when inserting text into a string ** literal. ** -** {F17411} The %Q option works like %q except it also adds single quotes around +** The %Q option works like %q except it also adds single quotes around ** the outside of the total string. Or if the parameter in the argument ** list is a NULL pointer, %Q substitutes the text "NULL" (without single ** quotes) in place of the %Q option. {END} So, for example, one could say: @@ -1162,9 +1404,27 @@ void sqlite3_free_table(char **result); ** The code above will render a correct SQL statement in the zSQL ** variable even if the zText variable is a NULL pointer. ** -** {F17412} The "%z" formatting option works exactly like "%s" with the +** The "%z" formatting option works exactly like "%s" with the ** addition that after the string has been read and copied into ** the result, [sqlite3_free()] is called on the input string. {END} +** +** INVARIANTS: +** +** {F17403} The [sqlite3_mprintf()] and [sqlite3_vmprintf()] interfaces +** return either pointers to zero-terminated UTF-8 strings held in +** memory obtained from [sqlite3_malloc()] or NULL pointers if +** a call to [sqlite3_malloc()] fails. +** +** {F17406} The [sqlite3_snprintf()] interface writes a zero-terminated +** UTF-8 string into the buffer pointed to by the second parameter +** provided that the first parameter is greater than zero. +** +** {F17407} The [sqlite3_snprintf()] interface does not writes slots of +** its output buffer (the second parameter) outside the range +** of 0 through N-1 (where N is the first parameter) +** regardless of the length of the string +** requested by the format specification. +** */ char *sqlite3_mprintf(const char*,...); char *sqlite3_vmprintf(const char*, va_list); @@ -1173,50 +1433,50 @@ char *sqlite3_snprintf(int,char*,const char*, ...); /* ** CAPI3REF: Memory Allocation Subsystem {F17300} ** -** {F17301} The SQLite core uses these three routines for all of its own -** internal memory allocation needs. {END} "Core" in the previous sentence +** The SQLite core uses these three routines for all of its own +** internal memory allocation needs. "Core" in the previous sentence ** does not include operating-system specific VFS implementation. The ** windows VFS uses native malloc and free for some operations. ** -** {F17302} The sqlite3_malloc() routine returns a pointer to a block +** The sqlite3_malloc() routine returns a pointer to a block ** of memory at least N bytes in length, where N is the parameter. -** {F17303} If sqlite3_malloc() is unable to obtain sufficient free -** memory, it returns a NULL pointer. {F17304} If the parameter N to +** If sqlite3_malloc() is unable to obtain sufficient free +** memory, it returns a NULL pointer. If the parameter N to ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns ** a NULL pointer. ** -** {F17305} Calling sqlite3_free() with a pointer previously returned +** Calling sqlite3_free() with a pointer previously returned ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so -** that it might be reused. {F17306} The sqlite3_free() routine is +** that it might be reused. The sqlite3_free() routine is ** a no-op if is called with a NULL pointer. Passing a NULL pointer -** to sqlite3_free() is harmless. {U17307} After being freed, memory +** to sqlite3_free() is harmless. After being freed, memory ** should neither be read nor written. Even reading previously freed ** memory might result in a segmentation fault or other severe error. -** {U17309} Memory corruption, a segmentation fault, or other severe error +** Memory corruption, a segmentation fault, or other severe error ** might result if sqlite3_free() is called with a non-NULL pointer that ** was not obtained from sqlite3_malloc() or sqlite3_free(). ** -** {F17310} The sqlite3_realloc() interface attempts to resize a +** The sqlite3_realloc() interface attempts to resize a ** prior memory allocation to be at least N bytes, where N is the ** second parameter. The memory allocation to be resized is the first -** parameter. {F17311} If the first parameter to sqlite3_realloc() +** parameter. If the first parameter to sqlite3_realloc() ** is a NULL pointer then its behavior is identical to calling ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). -** {F17312} If the second parameter to sqlite3_realloc() is zero or +** If the second parameter to sqlite3_realloc() is zero or ** negative then the behavior is exactly the same as calling ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). -** {F17313} Sqlite3_realloc() returns a pointer to a memory allocation +** Sqlite3_realloc() returns a pointer to a memory allocation ** of at least N bytes in size or NULL if sufficient memory is unavailable. -** {F17314} If M is the size of the prior allocation, then min(N,M) bytes +** If M is the size of the prior allocation, then min(N,M) bytes ** of the prior allocation are copied into the beginning of buffer returned ** by sqlite3_realloc() and the prior allocation is freed. -** {F17315} If sqlite3_realloc() returns NULL, then the prior allocation +** If sqlite3_realloc() returns NULL, then the prior allocation ** is not freed. ** -** {F17316} The memory returned by sqlite3_malloc() and sqlite3_realloc() +** The memory returned by sqlite3_malloc() and sqlite3_realloc() ** is always aligned to at least an 8 byte boundary. {END} ** -** {F17381} The default implementation +** The default implementation ** of the memory allocation subsystem uses the malloc(), realloc() ** and free() provided by the standard C library. {F17382} However, if ** SQLite is compiled with the following C preprocessor macro @@ -1241,6 +1501,58 @@ char *sqlite3_snprintf(int,char*,const char*, ...); ** installation. Memory allocation errors are detected, but ** they are reported back as [SQLITE_CANTOPEN] or ** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. +** +** INVARIANTS: +** +** {F17303} The [sqlite3_malloc(N)] interface returns either a pointer to +** newly checked-out block of at least N bytes of memory +** that is 8-byte aligned, +** or it returns NULL if it is unable to fulfill the request. +** +** {F17304} The [sqlite3_malloc(N)] interface returns a NULL pointer if +** N is less than or equal to zero. +** +** {F17305} The [sqlite3_free(P)] interface releases memory previously +** returned from [sqlite3_malloc()] or [sqlite3_realloc()], +** making it available for reuse. +** +** {F17306} A call to [sqlite3_free(NULL)] is a harmless no-op. +** +** {F17310} A call to [sqlite3_realloc(0,N)] is equivalent to a call +** to [sqlite3_malloc(N)]. +** +** {F17312} A call to [sqlite3_realloc(P,0)] is equivalent to a call +** to [sqlite3_free(P)]. +** +** {F17315} The SQLite core uses [sqlite3_malloc()], [sqlite3_realloc()], +** and [sqlite3_free()] for all of its memory allocation and +** deallocation needs. +** +** {F17318} The [sqlite3_realloc(P,N)] interface returns either a pointer +** to a block of checked-out memory of at least N bytes in size +** that is 8-byte aligned, or a NULL pointer. +** +** {F17321} When [sqlite3_realloc(P,N)] returns a non-NULL pointer, it first +** copies the first K bytes of content from P into the newly allocated +** where K is the lessor of N and the size of the buffer P. +** +** {F17322} When [sqlite3_realloc(P,N)] returns a non-NULL pointer, it first +** releases the buffer P. +** +** {F17323} When [sqlite3_realloc(P,N)] returns NULL, the buffer P is +** not modified or released. +** +** LIMITATIONS: +** +** {U17350} The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] +** must be either NULL or else a pointer obtained from a prior +** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that has +** not been released. +** +** {U17351} The application must not read or write any part of +** a block of memory after it has been released using +** [sqlite3_free()] or [sqlite3_realloc()]. +** */ void *sqlite3_malloc(int); void *sqlite3_realloc(void*, int); @@ -1249,29 +1561,31 @@ void sqlite3_free(void*); /* ** CAPI3REF: Memory Allocator Statistics {F17370} ** -** In addition to the basic three allocation routines -** [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()], -** the memory allocation subsystem included with the SQLite -** sources provides the interfaces shown here. -** -** {F17371} The sqlite3_memory_used() routine returns the -** number of bytes of memory currently outstanding (malloced but not freed). -** {F17372} The value returned by sqlite3_memory_used() includes -** any overhead added by SQLite, but not overhead added by the -** library malloc() that backs the sqlite3_malloc() implementation. -** {F17373} The sqlite3_memory_highwater() routines returns the -** maximum number of bytes that have been outstanding at any time -** since the highwater mark was last reset. -** {F17374} The byte count returned by sqlite3_memory_highwater() -** uses the same byte counting rules as sqlite3_memory_used(). {END} -** In other words, overhead added internally by SQLite is counted, -** but overhead from the underlying system malloc is not. -** {F17375} If the parameter to sqlite3_memory_highwater() is true, -** then the highwater mark is reset to the current value of -** sqlite3_memory_used() and the prior highwater mark (before the -** reset) is returned. {F17376} If the parameter to -** sqlite3_memory_highwater() is zero, then the highwater mark is -** unchanged. +** SQLite provides these two interfaces for reporting on the status +** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()] +** the memory allocation subsystem included within the SQLite. +** +** INVARIANTS: +** +** {F17371} The [sqlite3_memory_used()] routine returns the +** number of bytes of memory currently outstanding +** (malloced but not freed). +** +** {F17373} The [sqlite3_memory_highwater()] routine returns the maximum +** value of [sqlite3_memory_used()] +** since the highwater mark was last reset. +** +** {F17374} The values returned by [sqlite3_memory_used()] and +** [sqlite3_memory_highwater()] include any overhead +** added by SQLite in its implementation of [sqlite3_malloc()], +** but not overhead added by the any underlying system library +** routines that [sqlite3_malloc()] may call. +** +** {F17375} The memory highwater mark is reset to the current value of +** [sqlite3_memory_used()] if and only if the parameter to +** [sqlite3_memory_highwater()] is true. The value returned +** by [sqlite3_memory_highwater(1)] is the highwater mark +** prior to the reset. */ sqlite3_int64 sqlite3_memory_used(void); sqlite3_int64 sqlite3_memory_highwater(int resetFlag); @@ -1279,44 +1593,41 @@ sqlite3_int64 sqlite3_memory_highwater(int resetFlag); /* ** CAPI3REF: Compile-Time Authorization Callbacks {F12500} ** -** {F12501} This routine registers a authorizer callback with a particular -** database connection, supplied in the first argument. {F12502} +** This routine registers a authorizer callback with a particular +** database connection, supplied in the first argument. ** The authorizer callback is invoked as SQL statements are being compiled ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], -** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. {F12503} At various +** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. At various ** points during the compilation process, as logic is being created ** to perform various actions, the authorizer callback is invoked to ** see if those actions are allowed. The authorizer callback should ** return SQLITE_OK to allow the action, [SQLITE_IGNORE] to disallow the ** specific action but allow the SQL statement to continue to be ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be -** rejected with an error. {F12504} If the authorizer callback returns +** rejected with an error. If the authorizer callback returns ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] ** then [sqlite3_prepare_v2()] or equivalent call that triggered -** the authorizer shall -** fail with an SQLITE_ERROR error code and an appropriate error message. {END} +** the authorizer will fail with an error message. ** ** When the callback returns [SQLITE_OK], that means the operation -** requested is ok. {F12505} When the callback returns [SQLITE_DENY], the +** requested is ok. When the callback returns [SQLITE_DENY], the ** [sqlite3_prepare_v2()] or equivalent call that triggered the -** authorizer shall fail -** with an SQLITE_ERROR error code and an error message explaining that -** access is denied. {F12506} If the authorizer code (the 2nd parameter -** to the authorizer callback is anything other than [SQLITE_READ], then -** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. -** If the authorizer code is [SQLITE_READ] and the callback returns -** [SQLITE_IGNORE] then the prepared statement is constructed to -** insert a NULL value in place of the table column that would have -** been read if [SQLITE_OK] had been returned. {END} -** -** {F12510} The first parameter to the authorizer callback is a copy of +** authorizer will fail with an error message explaining that +** access is denied. If the authorizer code is [SQLITE_READ] +** and the callback returns [SQLITE_IGNORE] then the prepared +** statement is constructed to insert a NULL value in place of +** the table column that would have +** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] +** return can be used to deny an untrusted user access to individual +** columns of a table. +** +** The first parameter to the authorizer callback is a copy of ** the third parameter to the sqlite3_set_authorizer() interface. -** {F12511} The second parameter to the callback is an integer +** The second parameter to the callback is an integer ** [SQLITE_COPY | action code] that specifies the particular action -** to be authorized. {END} The available action codes are -** [SQLITE_COPY | documented separately]. {F12512} The third through sixth +** to be authorized. The third through sixth ** parameters to the callback are zero-terminated strings that contain -** additional details about the action to be authorized. {END} +** additional details about the action to be authorized. ** ** An authorizer is used when preparing SQL statements from an untrusted ** source, to ensure that the SQL statements do not try to access data @@ -1329,14 +1640,66 @@ sqlite3_int64 sqlite3_memory_highwater(int resetFlag); ** user-entered SQL is being prepared that disallows everything ** except SELECT statements. ** -** {F12520} Only a single authorizer can be in place on a database connection +** Only a single authorizer can be in place on a database connection ** at a time. Each call to sqlite3_set_authorizer overrides the -** previous call. {F12521} A NULL authorizer means that no authorization -** callback is invoked. {F12522} The default authorizer is NULL. {END} +** previous call. Disable the authorizer by installing a NULL callback. +** The authorizer is disabled by default. ** ** Note that the authorizer callback is invoked only during -** [sqlite3_prepare()] or its variants. {F12523} Authorization is not -** performed during statement evaluation in [sqlite3_step()]. {END} +** [sqlite3_prepare()] or its variants. Authorization is not +** performed during statement evaluation in [sqlite3_step()]. +** +** INVARIANTS: +** +** {F12501} The [sqlite3_set_authorizer(D,...)] interface registers a +** authorizer callback with database connection D. +** +** {F12502} The authorizer callback is invoked as SQL statements are +** being compiled +** +** {F12503} If the authorizer callback returns any value other than +** [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] then +** the [sqlite3_prepare_v2()] or equivalent call that caused +** the authorizer callback to run shall fail with an +** [SQLITE_ERROR] error code and an appropriate error message. +** +** {F12504} When the authorizer callback returns [SQLITE_OK], the operation +** described is coded normally. +** +** {F12505} When the authorizer callback returns [SQLITE_DENY], the +** [sqlite3_prepare_v2()] or equivalent call that caused the +** authorizer callback to run shall fail +** with an [SQLITE_ERROR] error code and an error message +** explaining that access is denied. +** +** {F12506} If the authorizer code (the 2nd parameter to the authorizer +** callback) is [SQLITE_READ] and the authorizer callback returns +** [SQLITE_IGNORE] then the prepared statement is constructed to +** insert a NULL value in place of the table column that would have +** been read if [SQLITE_OK] had been returned. +** +** {F12507} If the authorizer code (the 2nd parameter to the authorizer +** callback) is anything other than [SQLITE_READ], then +** a return of [SQLITE_IGNORE] has the same effect as [SQLITE_DENY]. +** +** {F12510} The first parameter to the authorizer callback is a copy of +** the third parameter to the [sqlite3_set_authorizer()] interface. +** +** {F12511} The second parameter to the callback is an integer +** [SQLITE_COPY | action code] that specifies the particular action +** to be authorized. +** +** {F12512} The third through sixth parameters to the callback are +** zero-terminated strings that contain +** additional details about the action to be authorized. +** +** {F12520} Each call to [sqlite3_set_authorizer()] overrides the +** any previously installed authorizer. +** +** {F12521} A NULL authorizer means that no authorization +** callback is invoked. +** +** {F12522} The default authorizer is NULL. */ int sqlite3_set_authorizer( sqlite3*, @@ -1360,20 +1723,42 @@ int sqlite3_set_authorizer( ** CAPI3REF: Authorizer Action Codes {F12550} ** ** The [sqlite3_set_authorizer()] interface registers a callback function -** that is invoked to authorizer certain SQL statement actions. {F12551} The +** that is invoked to authorizer certain SQL statement actions. The ** second parameter to the callback is an integer code that specifies ** what action is being authorized. These are the integer action codes that -** the authorizer callback may be passed. {END} +** the authorizer callback may be passed. ** ** These action code values signify what kind of operation is to be -** authorized. {F12552} The 3rd and 4th parameters to the authorization +** authorized. The 3rd and 4th parameters to the authorization ** callback function will be parameters or NULL depending on which of these -** codes is used as the second parameter. {F12553} The 5th parameter to the +** codes is used as the second parameter. The 5th parameter to the ** authorizer callback is the name of the database ("main", "temp", -** etc.) if applicable. {F12554} The 6th parameter to the authorizer callback +** etc.) if applicable. The 6th parameter to the authorizer callback ** is the name of the inner-most trigger or view that is responsible for ** the access attempt or NULL if this access attempt is directly from ** top-level SQL code. +** +** INVARIANTS: +** +** {F12551} The second parameter to an +** [sqlite3_set_authorizer | authorizer callback is always an integer +** [SQLITE_COPY | authorizer code] that specifies what action +** is being authorized. +** +** {F12552} The 3rd and 4th parameters to the +** [sqlite3_set_authorizer | authorization callback function] +** will be parameters or NULL depending on which +** [SQLITE_COPY | authorizer code] is used as the second parameter. +** +** {F12553} The 5th parameter to the +** [sqlite3_set_authorizer | authorizer callback] is the name +** of the database (example: "main", "temp", etc.) if applicable. +** +** {F12554} The 6th parameter to the +** [sqlite3_set_authorizer | authorizer callback] is the name +** of the inner-most trigger or view that is responsible for +** the access attempt or NULL if this access attempt is directly from +** top-level SQL code. */ /******************************************* 3rd ************ 4th ***********/ #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ @@ -1415,31 +1800,60 @@ int sqlite3_set_authorizer( ** These routines register callback functions that can be used for ** tracing and profiling the execution of SQL statements. ** -** {F12281} The callback function registered by sqlite3_trace() is invoked -** at the first [sqlite3_step()] for the evaluation of an SQL statement. -** {F12282} Only a single trace callback can be registered at a time. -** Each call to sqlite3_trace() overrides the previous. {F12283} A -** NULL callback for sqlite3_trace() disables tracing. {F12284} The -** first argument to the trace callback is a copy of the pointer which -** was the 3rd argument to sqlite3_trace. {F12285} The second argument -** to the trace callback is a zero-terminated UTF8 string containing -** the original text of the SQL statement as it was passed into -** [sqlite3_prepare_v2()] or the equivalent. {END} Note that the -** host parameter are not expanded in the SQL statement text. -** -** {F12287} The callback function registered by sqlite3_profile() is invoked -** as each SQL statement finishes. {F12288} The first parameter to the -** profile callback is a copy of the 3rd parameter to sqlite3_profile(). -** {F12289} The second parameter to the profile callback is a -** zero-terminated UTF-8 string that contains the complete text of -** the SQL statement as it was processed by [sqlite3_prepare_v2()] or -** the equivalent. {F12290} The third parameter to the profile -** callback is an estimate of the number of nanoseconds of -** wall-clock time required to run the SQL statement from start -** to finish. {END} +** The callback function registered by sqlite3_trace() is invoked at +** various times when an SQL statement is being run by [sqlite3_step()]. +** The callback returns a UTF-8 rendering of the SQL statement text +** as the statement first begins executing. Additional callbacks occur +** as each triggersubprogram is entered. The callbacks for triggers +** contain a UTF-8 SQL comment that identifies the trigger. +** +** The callback function registered by sqlite3_profile() is invoked +** as each SQL statement finishes. The profile callback contains +** the original statement text and an estimate of wall-clock time +** of how long that statement took to run. ** ** The sqlite3_profile() API is currently considered experimental and -** is subject to change. +** is subject to change or removal in a future release. +** +** The trigger reporting feature of the trace callback is considered +** experimental and is subject to change or removal in future releases. +** Future versions of SQLite might also add new trace callback +** invocations. +** +** INVARIANTS: +** +** {F12281} The callback function registered by [sqlite3_trace()] is +** whenever an SQL statement first begins to execute and +** whenever a trigger subprogram first begins to run. +** +** {F12282} Each call to [sqlite3_trace()] overrides the previously +** registered trace callback. +** +** {F12283} A NULL trace callback disables tracing. +** +** {F12284} The first argument to the trace callback is a copy of +** the pointer which was the 3rd argument to [sqlite3_trace()]. +** +** {F12285} The second argument to the trace callback is a +** zero-terminated UTF8 string containing the original text +** of the SQL statement as it was passed into [sqlite3_prepare_v2()] +** or the equivalent, or an SQL comment indicating the beginning +** of a trigger subprogram. +** +** {F12287} The callback function registered by [sqlite3_profile()] is invoked +** as each SQL statement finishes. +** +** {F12288} The first parameter to the profile callback is a copy of +** the 3rd parameter to [sqlite3_profile()]. +** +** {F12289} The second parameter to the profile callback is a +** zero-terminated UTF-8 string that contains the complete text of +** the SQL statement as it was processed by [sqlite3_prepare_v2()] +** or the equivalent. +** +** {F12290} The third parameter to the profile callback is an estimate +** of the number of nanoseconds of wall-clock time required to +** run the SQL statement from start to finish. */ void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); void *sqlite3_profile(sqlite3*, @@ -1448,68 +1862,78 @@ void *sqlite3_profile(sqlite3*, /* ** CAPI3REF: Query Progress Callbacks {F12910} ** -** {F12911} This routine configures a callback function - the +** This routine configures a callback function - the ** progress callback - that is invoked periodically during long ** running calls to [sqlite3_exec()], [sqlite3_step()] and -** [sqlite3_get_table()]. {END} An example use for this +** [sqlite3_get_table()]. An example use for this ** interface is to keep a GUI updated during a large query. ** +** If the progress callback returns non-zero, the opertion is +** interrupted. This feature can be used to implement a +** "Cancel" button on a GUI dialog box. +** +** INVARIANTS: +** +** {F12911} The callback function registered by [sqlite3_progress_handler()] +** is invoked periodically during long running calls to +** [sqlite3_step()]. +** ** {F12912} The progress callback is invoked once for every N virtual -** machine opcodes, where N is the second argument to this function. +** machine opcodes, where N is the second argument to +** the [sqlite3_progress_handler()] call that registered +** the callback. What if N is less than 1? +** ** {F12913} The progress callback itself is identified by the third -** argument to this function. {F12914} The fourth argument to this -** function is a void pointer passed to the progress callback -** function each time it is invoked. {END} +** argument to [sqlite3_progress_handler()]. +** +** {F12914} The fourth argument [sqlite3_progress_handler()] is a +*** void pointer passed to the progress callback +** function each time it is invoked. ** -** {F12915} If a call to [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] results in fewer than N opcodes being executed, -** then the progress callback is never invoked. {END} +** {F12915} If a call to [sqlite3_step()] results in fewer than +** N opcodes being executed, +** then the progress callback is never invoked. {END} ** -** {F12916} Only a single progress callback function may be registered for each -** open database connection. Every call to sqlite3_progress_handler() -** overwrites the results of the previous call. {F12917} -** To remove the progress callback altogether, pass NULL as the third -** argument to this function. {END} +** {F12916} Every call to [sqlite3_progress_handler()] +** overwrites any previously registere progress handler. +** +** {F12917} If the progress handler callback is NULL then no progress +** handler is invoked. ** ** {F12918} If the progress callback returns a result other than 0, then -** the current query is immediately terminated and any database changes -** rolled back. {F12919} -** The containing [sqlite3_exec()], [sqlite3_step()], or -** [sqlite3_get_table()] call returns SQLITE_INTERRUPT. {END} This feature -** can be used, for example, to implement the "Cancel" button on a -** progress dialog box in a GUI. +** the behavior is a if [sqlite3_interrupt()] had been called. */ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); /* ** CAPI3REF: Opening A New Database Connection {F12700} ** -** {F12701} These routines open an SQLite database file whose name +** These routines open an SQLite database file whose name ** is given by the filename argument. -** {F12702} The filename argument is interpreted as UTF-8 +** The filename argument is interpreted as UTF-8 ** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 ** in the native byte order for [sqlite3_open16()]. -** {F12703} An [sqlite3*] handle is returned in *ppDb, even -** if an error occurs. {F12723} (Exception: if SQLite is unable +** An [sqlite3*] handle is usually returned in *ppDb, even +** if an error occurs. The only exception is if SQLite is unable ** to allocate memory to hold the [sqlite3] object, a NULL will -** be written into *ppDb instead of a pointer to the [sqlite3] object.) -** {F12704} If the database is opened (and/or created) -** successfully, then [SQLITE_OK] is returned. {F12705} Otherwise an -** error code is returned. {F12706} The +** be written into *ppDb instead of a pointer to the [sqlite3] object. +** If the database is opened (and/or created) +** successfully, then [SQLITE_OK] is returned. Otherwise an +** error code is returned. The ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain ** an English language description of the error. ** -** {F12707} The default encoding for the database will be UTF-8 if +** The default encoding for the database will be UTF-8 if ** [sqlite3_open()] or [sqlite3_open_v2()] is called and ** UTF-16 in the native byte order if [sqlite3_open16()] is used. ** -** {F12708} Whether or not an error occurs when it is opened, resources +** Whether or not an error occurs when it is opened, resources ** associated with the [sqlite3*] handle should be released by passing it ** to [sqlite3_close()] when it is no longer required. ** -** {F12709} The [sqlite3_open_v2()] interface works like [sqlite3_open()] +** The [sqlite3_open_v2()] interface works like [sqlite3_open()] ** except that it acccepts two additional parameters for additional control -** over the new database connection. {F12710} The flags parameter can be +** over the new database connection. The flags parameter can be ** one of: ** **
        @@ -1518,41 +1942,104 @@ void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); **
      1. [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE] **
      ** -** {F12711} The first value opens the database read-only. -** {F12712} If the database does not previously exist, an error is returned. -** {F12713} The second option opens +** The first value opens the database read-only. +** If the database does not previously exist, an error is returned. +** The second option opens ** the database for reading and writing if possible, or reading only if -** if the file is write protected. {F12714} In either case the database -** must already exist or an error is returned. {F12715} The third option +** if the file is write protected. In either case the database +** must already exist or an error is returned. The third option ** opens the database for reading and writing and creates it if it does -** not already exist. {F12716} +** not already exist. ** The third options is behavior that is always used for [sqlite3_open()] ** and [sqlite3_open16()]. ** -** {F12717} If the filename is ":memory:", then an private -** in-memory database is created for the connection. {F12718} This in-memory -** database will vanish when the database connection is closed. {END} Future +** If the filename is ":memory:", then an private +** in-memory database is created for the connection. This in-memory +** database will vanish when the database connection is closed. Future ** version of SQLite might make use of additional special filenames ** that begin with the ":" character. It is recommended that ** when a database filename really does begin with ** ":" that you prefix the filename with a pathname like "./" to ** avoid ambiguity. ** -** {F12719} If the filename is an empty string, then a private temporary -** on-disk database will be created. {F12720} This private database will be +** If the filename is an empty string, then a private temporary +** on-disk database will be created. This private database will be ** automatically deleted as soon as the database connection is closed. ** -** {F12721} The fourth parameter to sqlite3_open_v2() is the name of the +** The fourth parameter to sqlite3_open_v2() is the name of the ** [sqlite3_vfs] object that defines the operating system -** interface that the new database connection should use. {F12722} If the +** interface that the new database connection should use. If the ** fourth parameter is a NULL pointer then the default [sqlite3_vfs] -** object is used. {END} +** object is used. ** ** Note to windows users: The encoding used for the filename argument ** of [sqlite3_open()] and [sqlite3_open_v2()] must be UTF-8, not whatever ** codepage is currently defined. Filenames containing international ** characters must be converted to UTF-8 prior to passing them into ** [sqlite3_open()] or [sqlite3_open_v2()]. +** +** INVARIANTS: +** +** {F12701} The [sqlite3_open()], [sqlite3_open16()], and +** [sqlite3_open_v2()] interfaces create a new +** [database connection] associated with +** the database file given in their first parameter. +** +** {F12702} The filename argument is interpreted as UTF-8 +** for [sqlite3_open()] and [sqlite3_open_v2()] and as UTF-16 +** in the native byte order for [sqlite3_open16()]. +** +** {F12703} A successful invocation of [sqlite3_open()], [sqlite3_open16()], +** or [sqlite3_open_v2()] writes a pointer to a new +** [database connection] into *ppDb. +** +** {F12704} The [sqlite3_open()], [sqlite3_open16()], and +** [sqlite3_open_v2()] interfaces return [SQLITE_OK] upon success, +** or an appropriate [error code] on failure. +** +** {F12706} The default text encoding for a new database created using +** [sqlite3_open()] or [sqlite3_open_v2()] will be UTF-8. +** +** {F12707} The default text encoding for a new database created using +** [sqlite3_open16()] will be UTF-16. +** +** {F12709} The [sqlite3_open(F,D)] interface is equivalent to +** [sqlite3_open_v2(F,D,G,0)] where the G parameter is +** [SQLITE_OPEN_READWRITE]|[SQLITE_OPEN_CREATE]. +** +** {F12711} If the G parameter to [sqlite3_open_v2(F,D,G,V)] contains the +** bit value [SQLITE_OPEN_READONLY] then the database is opened +** for reading only. +** +** {F12712} If the G parameter to [sqlite3_open_v2(F,D,G,V)] contains the +** bit value [SQLITE_OPEN_READWRITE] then the database is opened +** reading and writing if possible, or for reading only if the +** file is write protected by the operating system. +** +** {F12713} If the G parameter to [sqlite3_open(v2(F,D,G,V)] omits the +** bit value [SQLITE_OPEN_CREATE] and the database does not +** previously exist, an error is returned. +** +** {F12714} If the G parameter to [sqlite3_open(v2(F,D,G,V)] contains the +** bit value [SQLITE_OPEN_CREATE] and the database does not +** previously exist, then an attempt is made to create and +** initialize the database. +** +** {F12717} If the filename argument to [sqlite3_open()], [sqlite3_open16()], +** or [sqlite3_open_v2()] is ":memory:", then an private, +** ephemeral, in-memory database is created for the connection. +** Is SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE required +** in sqlite3_open_v2()? +** +** {F12719} If the filename is NULL or an empty string, then a private, +** ephermeral on-disk database will be created. +** Is SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE required +** in sqlite3_open_v2()? +** +** {F12721} The [database connection] created by +** [sqlite3_open_v2(F,D,G,V)] will use the +** [sqlite3_vfs] object identified by the V parameter, or +** the default [sqlite3_vfs] object is V is a NULL pointer. */ int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ @@ -1572,35 +2059,46 @@ int sqlite3_open_v2( /* ** CAPI3REF: Error Codes And Messages {F12800} ** -** {F12801} The sqlite3_errcode() interface returns the numeric +** The sqlite3_errcode() interface returns the numeric ** [SQLITE_OK | result code] or [SQLITE_IOERR_READ | extended result code] ** for the most recent failed sqlite3_* API call associated -** with [sqlite3] handle 'db'. {U12802} If a prior API call failed but the +** with [sqlite3] handle 'db'. If a prior API call failed but the ** most recent API call succeeded, the return value from sqlite3_errcode() -** is undefined. {END} +** is undefined. ** -** {F12803} The sqlite3_errmsg() and sqlite3_errmsg16() return English-language +** The sqlite3_errmsg() and sqlite3_errmsg16() return English-language ** text that describes the error, as either UTF8 or UTF16 respectively. -** {F12804} Memory to hold the error message string is managed internally. -** {U12805} The -** string may be overwritten or deallocated by subsequent calls to SQLite -** interface functions. {END} -** -** {F12806} Calls to many sqlite3_* functions set the error code and -** string returned by [sqlite3_errcode()], [sqlite3_errmsg()], and -** [sqlite3_errmsg16()] overwriting the previous values. {F12807} -** Except, calls to [sqlite3_errcode()], -** [sqlite3_errmsg()], and [sqlite3_errmsg16()] themselves do not affect the -** results of future invocations. {F12808} Calls to API routines that -** do not return an error code (example: [sqlite3_data_count()]) do not -** change the error code returned by this routine. {F12809} Interfaces that -** are not associated with a specific database connection (examples: -** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] do not change -** the return code. {END} -** -** {F12810} Assuming no other intervening sqlite3_* API calls are made, -** the error code returned by this function is associated with the same -** error as the strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()]. +** Memory to hold the error message string is managed internally. +** The application does not need to worry with freeing the result. +** However, the error string might be overwritten or deallocated by +** subsequent calls to other SQLite interface functions. +** +** INVARIANTS: +** +** {F12801} The [sqlite3_errcode(D)] interface returns the numeric +** [SQLITE_OK | result code] or +** [SQLITE_IOERR_READ | extended result code] +** for the most recently failed interface call associated +** with [database connection] D. +** +** {F12803} The [sqlite3_errmsg(D)] and [sqlite3_errmsg16(D)] +** interfaces return English-language text that describes +** the error in the mostly recently failed interface call, +** encoded as either UTF8 or UTF16 respectively. +** +** {F12807} The strings returned by [sqlite3_errmsg()] and [sqlite3_errmsg16()] +** are valid until the next SQLite interface call. +** +** {F12808} Calls to API routines that do not return an error code +** (example: [sqlite3_data_count()]) do not +** change the error code or message returned by +** [sqlite3_errcode()], [sqlite3_errmsg()], or [sqlite3_errmsg16()]. +** +** {F12809} Interfaces that are not associated with a specific +** [database connection] (examples: +** [sqlite3_mprintf()] or [sqlite3_enable_shared_cache()] +** do not change the values returned by +** [sqlite3_errcode()], [sqlite3_errmsg()], or [sqlite3_errmsg16()]. */ int sqlite3_errcode(sqlite3 *db); const char *sqlite3_errmsg(sqlite3*); @@ -1608,6 +2106,7 @@ const void *sqlite3_errmsg16(sqlite3*); /* ** CAPI3REF: SQL Statement Object {F13000} +** KEYWORDS: {prepared statement} {prepared statements} ** ** An instance of this object represent single SQL statements. This ** object is variously known as a "prepared statement" or a @@ -1637,72 +2136,105 @@ typedef struct sqlite3_stmt sqlite3_stmt; ** To execute an SQL query, it must first be compiled into a byte-code ** program using one of these routines. ** -** {F13011} The first argument "db" is an [sqlite3 | SQLite database handle] +** The first argument "db" is an [database connection] ** obtained from a prior call to [sqlite3_open()], [sqlite3_open_v2()] -** or [sqlite3_open16()]. {F13012} +** or [sqlite3_open16()]. ** The second argument "zSql" is the statement to be compiled, encoded ** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() ** interfaces uses UTF-8 and sqlite3_prepare16() and sqlite3_prepare16_v2() ** use UTF-16. {END} ** -** {F13013} If the nByte argument is less +** If the nByte argument is less ** than zero, then zSql is read up to the first zero terminator. -** {F13014} If nByte is non-negative, then it is the maximum number of +** If nByte is non-negative, then it is the maximum number of ** bytes read from zSql. When nByte is non-negative, the ** zSql string ends at either the first '\000' or '\u0000' character or ** until the nByte-th byte, whichever comes first. {END} ** -** {F13015} *pzTail is made to point to the first byte past the end of the +** *pzTail is made to point to the first byte past the end of the ** first SQL statement in zSql. These routines only compiles the first ** statement in zSql, so *pzTail is left pointing to what remains -** uncompiled. {END} +** uncompiled. ** -** {F13016} *ppStmt is left pointing to a compiled -** [sqlite3_stmt | SQL statement structure] that can be -** executed using [sqlite3_step()]. Or if there is an error, *ppStmt may be -** set to NULL. {F13017} If the input text contains no SQL (if the input +** *ppStmt is left pointing to a compiled [prepared statement] that can be +** executed using [sqlite3_step()]. Or if there is an error, *ppStmt is +** set to NULL. If the input text contains no SQL (if the input ** is and empty string or a comment) then *ppStmt is set to NULL. ** {U13018} The calling procedure is responsible for deleting the ** compiled SQL statement ** using [sqlite3_finalize()] after it has finished with it. ** -** {F13019} On success, [SQLITE_OK] is returned. Otherwise an -** [SQLITE_ERROR | error code] is returned. {END} +** On success, [SQLITE_OK] is returned. Otherwise an +** [error code] is returned. ** ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are ** recommended for all new programs. The two older interfaces are retained ** for backwards compatibility, but their use is discouraged. -** {F13020} In the "v2" interfaces, the prepared statement +** In the "v2" interfaces, the prepared statement ** that is returned (the [sqlite3_stmt] object) contains a copy of the ** original SQL text. {END} This causes the [sqlite3_step()] interface to ** behave a differently in two ways: ** **
        -**
      1. {F13022} +**
      2. ** If the database schema changes, instead of returning [SQLITE_SCHEMA] as it ** always used to do, [sqlite3_step()] will automatically recompile the SQL -** statement and try to run it again. {F12023} If the schema has changed in +** statement and try to run it again. If the schema has changed in ** a way that makes the statement no longer valid, [sqlite3_step()] will still -** return [SQLITE_SCHEMA]. {END} But unlike the legacy behavior, -** [SQLITE_SCHEMA] is now a fatal error. {F12024} Calling +** return [SQLITE_SCHEMA]. But unlike the legacy behavior, +** [SQLITE_SCHEMA] is now a fatal error. Calling ** [sqlite3_prepare_v2()] again will not make the -** error go away. {F12025} Note: use [sqlite3_errmsg()] to find the text +** error go away. Note: use [sqlite3_errmsg()] to find the text ** of the parsing error that results in an [SQLITE_SCHEMA] return. {END} **
      3. ** **
      4. -** {F13030} When an error occurs, +** When an error occurs, ** [sqlite3_step()] will return one of the detailed -** [SQLITE_ERROR | result codes] or -** [SQLITE_IOERR_READ | extended result codes]. {F13031} +** [error codes] or [extended error codes]. ** The legacy behavior was that [sqlite3_step()] would only return a generic ** [SQLITE_ERROR] result code and you would have to make a second call to ** [sqlite3_reset()] in order to find the underlying cause of the problem. -** {F13032} ** With the "v2" prepare interfaces, the underlying reason for the error is -** returned immediately. {END} +** returned immediately. **
      5. **
      +** +** INVARIANTS: +** +** {F13011} The [sqlite3_prepare(db,zSql,...)] and +** [sqlite3_prepare_v2(db,zSql,...)] interfaces interpret the +** text in their zSql parameter as UTF-8. +** +** {F13012} The [sqlite3_prepare16(db,zSql,...)] and +** [sqlite3_prepare16_v2(db,zSql,...)] interfaces interpret the +** text in their zSql parameter as UTF-16 in the native byte order. +** +** {F13013} If the nByte argument to [sqlite3_prepare_v2(db,zSql,nByte,...)] +** and its variants is less than zero, then SQL text is +** read from zSql is read up to the first zero terminator. +** +** {F13014} If the nByte argument to [sqlite3_prepare_v2(db,zSql,nByte,...)] +** and its variants is non-negative, then nBytes bytes +** SQL text is read from zSql. +** +** {F13015} In [sqlite3_prepare_v2(db,zSql,N,P,pzTail)] and its variants +** if the zSql input text contains more than one SQL statement +** and pzTail is not NULL, then *pzTail is made to point to the +** first byte past the end of the first SQL statement in zSql. +** What does *pzTail point to if there is one statement? +** +** {F13016} A successful call to [sqlite3_prepare_v2(db,zSql,N,ppStmt,...)] +** or one of its variants writes into *ppStmt a pointer to a new +** [prepared statement] or a pointer to NULL +** if zSql contains nothing other than whitespace or comments. +** +** {F13019} The [sqlite3_prepare_v2()] interface and its variants return +** [SQLITE_OK] or an appropriate [error code] upon failure. +** +** {F13021} Before [sqlite3_prepare(db,zSql,nByte,ppStmt,pzTail)] or its +** variants returns an error (any value other than [SQLITE_OK]) +** it first sets *ppStmt to NULL. */ int sqlite3_prepare( sqlite3 *db, /* Database handle */ @@ -1736,29 +2268,37 @@ int sqlite3_prepare16_v2( /* ** CAPIREF: Retrieving Statement SQL {F13100} ** -** {F13101} If the compiled SQL statement passed as an argument was -** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()], -** then this function returns a pointer to a zero-terminated string -** containing a copy of the original SQL statement. {F13102} The -** pointer is valid until the statement -** is deleted using sqlite3_finalize(). -** {F13103} The string returned by sqlite3_sql() is always UTF8 even -** if a UTF16 string was originally entered using [sqlite3_prepare16_v2()] -** or the equivalent. +** This intereface can be used to retrieve a saved copy of the original +** SQL text used to create a [prepared statement]. +** +** INVARIANTS: ** -** {F13104} If the statement was compiled using either of the legacy -** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this -** function returns NULL. +** {F13101} If the [prepared statement] passed as +** the an argument to [sqlite3_sql()] was compiled +** compiled using either [sqlite3_prepare_v2()] or +** [sqlite3_prepare16_v2()], +** then [sqlite3_sql()] function returns a pointer to a +** zero-terminated string containing a UTF-8 rendering +** of the original SQL statement. +** +** {F13102} If the [prepared statement] passed as +** the an argument to [sqlite3_sql()] was compiled +** compiled using either [sqlite3_prepare()] or +** [sqlite3_prepare16()], +** then [sqlite3_sql()] function returns a NULL pointer. +** +** {F13103} The string returned by [sqlite3_sql(S)] is valid until the +** [prepared statement] S is deleted using [sqlite3_finalize(S)]. */ const char *sqlite3_sql(sqlite3_stmt *pStmt); /* ** CAPI3REF: Dynamically Typed Value Object {F15000} ** -** {F15001} SQLite uses the sqlite3_value object to represent all values -** that are or can be stored in a database table. {END} +** SQLite uses the sqlite3_value object to represent all values +** that are or can be stored in a database table. ** SQLite uses dynamic typing for the values it stores. -** {F15002} Values stored in sqlite3_value objects can be +** Values stored in sqlite3_value objects can be ** be integers, floating point values, strings, BLOBs, or NULL. */ typedef struct Mem sqlite3_value; @@ -1767,7 +2307,7 @@ typedef struct Mem sqlite3_value; ** CAPI3REF: SQL Function Context Object {F16001} ** ** The context in which an SQL function executes is stored in an -** sqlite3_context object. {F16002} A pointer to an sqlite3_context +** sqlite3_context object. A pointer to an sqlite3_context ** object is always first parameter to application-defined SQL functions. */ typedef struct sqlite3_context sqlite3_context; @@ -1775,78 +2315,152 @@ typedef struct sqlite3_context sqlite3_context; /* ** CAPI3REF: Binding Values To Prepared Statements {F13500} ** -** {F13501} In the SQL strings input to [sqlite3_prepare_v2()] and its +** In the SQL strings input to [sqlite3_prepare_v2()] and its ** variants, literals may be replace by a parameter in one ** of these forms: ** **
        **
      • ? **
      • ?NNN -**
      • :AAA -**
      • @AAA +**
      • :VVV +**
      • @VVV **
      • $VVV **
      ** ** In the parameter forms shown above NNN is an integer literal, -** AAA is an alphanumeric identifier and VVV is a variable name according -** to the syntax rules of the TCL programming language. {END} -** The values of these parameters (also called "host parameter names") +** VVV alpha-numeric parameter name. +** The values of these parameters (also called "host parameter names" +** or "SQL parameters") ** can be set using the sqlite3_bind_*() routines defined here. ** -** {F13502} The first argument to the sqlite3_bind_*() routines always +** The first argument to the sqlite3_bind_*() routines always ** is a pointer to the [sqlite3_stmt] object returned from -** [sqlite3_prepare_v2()] or its variants. {F13503} The second -** argument is the index of the parameter to be set. {F13504} The -** first parameter has an index of 1. {F13505} When the same named +** [sqlite3_prepare_v2()] or its variants. The second +** argument is the index of the parameter to be set. The +** first parameter has an index of 1. When the same named ** parameter is used more than once, second and subsequent ** occurrences have the same index as the first occurrence. -** {F13506} The index for named parameters can be looked up using the -** [sqlite3_bind_parameter_name()] API if desired. {F13507} The index +** The index for named parameters can be looked up using the +** [sqlite3_bind_parameter_name()] API if desired. The index ** for "?NNN" parameters is the value of NNN. -** {F13508} The NNN value must be between 1 and the compile-time -** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). {END} -** See limits.html for additional information. +** The NNN value must be between 1 and the compile-time +** parameter SQLITE_MAX_VARIABLE_NUMBER (default value: 999). ** -** {F13509} The third argument is the value to bind to the parameter. {END} +** The third argument is the value to bind to the parameter. ** -** {F13510} In those +** In those ** routines that have a fourth argument, its value is the number of bytes -** in the parameter. To be clear: the value is the number of bytes in the -** string, not the number of characters. {F13511} The number +** in the parameter. To be clear: the value is the number of bytes +** in the value, not the number of characters. The number ** of bytes does not include the zero-terminator at the end of strings. -** {F13512} ** If the fourth parameter is negative, the length of the string is -** number of bytes up to the first zero terminator. {END} +** number of bytes up to the first zero terminator. ** -** {F13513} ** The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or -** text after SQLite has finished with it. {F13514} If the fifth argument is -** the special value [SQLITE_STATIC], then the library assumes that the +** string after SQLite has finished with it. If the fifth argument is +** the special value [SQLITE_STATIC], then SQLite assumes that the ** information is in static, unmanaged space and does not need to be freed. -** {F13515} If the fifth argument has the value [SQLITE_TRANSIENT], then +** If the fifth argument has the value [SQLITE_TRANSIENT], then ** SQLite makes its own private copy of the data immediately, before -** the sqlite3_bind_*() routine returns. {END} +** the sqlite3_bind_*() routine returns. ** -** {F13520} The sqlite3_bind_zeroblob() routine binds a BLOB of length N that -** is filled with zeros. {F13521} A zeroblob uses a fixed amount of memory -** (just an integer to hold it size) while it is being processed. {END} +** The sqlite3_bind_zeroblob() routine binds a BLOB of length N that +** is filled with zeros. A zeroblob uses a fixed amount of memory +** (just an integer to hold it size) while it is being processed. ** Zeroblobs are intended to serve as place-holders for BLOBs whose ** content is later written using -** [sqlite3_blob_open | increment BLOB I/O] routines. {F13522} A negative -** value for the zeroblob results in a zero-length BLOB. {END} +** [sqlite3_blob_open | increment BLOB I/O] routines. A negative +** value for the zeroblob results in a zero-length BLOB. ** -** {F13530} The sqlite3_bind_*() routines must be called after +** The sqlite3_bind_*() routines must be called after ** [sqlite3_prepare_v2()] (and its variants) or [sqlite3_reset()] and -** before [sqlite3_step()]. {F13531} +** before [sqlite3_step()]. ** Bindings are not cleared by the [sqlite3_reset()] routine. -** {F13532} Unbound parameters are interpreted as NULL. {END} +** Unbound parameters are interpreted as NULL. ** -** {F13540} These routines return [SQLITE_OK] on success or an error code if -** anything goes wrong. {F13541} [SQLITE_RANGE] is returned if the parameter -** index is out of range. {F13542} [SQLITE_NOMEM] is returned if malloc fails. -** {F13543} [SQLITE_MISUSE] is returned if these routines are called on a +** These routines return [SQLITE_OK] on success or an error code if +** anything goes wrong. [SQLITE_RANGE] is returned if the parameter +** index is out of range. [SQLITE_NOMEM] is returned if malloc fails. +** [SQLITE_MISUSE] might be returned if these routines are called on a ** virtual machine that is the wrong state or which has already been finalized. +** Detection of misuse is unreliable. Applications should not depend +** on SQLITE_MISUSE returns. SQLITE_MISUSE is intended to indicate a +** a logic error in the application. Future versions of SQLite might +** panic rather than return SQLITE_MISUSE. +** +** See also: [sqlite3_bind_parameter_count()], +** [sqlite3_bind_parameter_name()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13506} The [sqlite3_prepare | SQL statement compiler] recognizes +** tokens of the forms "?", "?NNN", "$VVV", ":VVV", and "@VVV" +** as SQL parameters, where NNN is any sequence of one or more +** digits and where VVV is any sequence of one or more +** alphanumeric characters or "::" optionally followed by +** a string containing no spaces and contained within parentheses. +** +** {F13509} The initial value of an SQL parameter is NULL. +** +** {F13512} The index of an "?" SQL parameter is one larger than the +** largest index of SQL parameter to the left, or 1 if +** the "?" is the leftmost SQL parameter. +** +** {F13515} The index of an "?NNN" SQL parameter is the integer NNN. +** +** {F13518} The index of an ":VVV", "$VVV", or "@VVV" SQL parameter is +** the same as the index of leftmost occurances of the same +** parameter, or one more than the largest index over all +** parameters to the left if this is the first occurrance +** of this parameter, or 1 if this is the leftmost parameter. +** +** {F13521} The [sqlite3_prepare | SQL statement compiler] fail with +** an [SQLITE_RANGE] error if the index of an SQL parameter +** is less than 1 or greater than SQLITE_MAX_VARIABLE_NUMBER. +** +** {F13524} Calls to [sqlite3_bind_text | sqlite3_bind(S,N,V,...)] +** associate the value V with all SQL parameters having an +** index of N in the [prepared statement] S. +** +** {F13527} Calls to [sqlite3_bind_text | sqlite3_bind(S,N,...)] +** override prior calls with the same values of S and N. +** +** {F13530} Bindings established by [sqlite3_bind_text | sqlite3_bind(S,...)] +** persist across calls to [sqlite3_reset(S)]. +** +** {F13533} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] SQLite binds the first L +** bytes of the blob or string pointed to by V, when L +** is non-negative. +** +** {F13536} In calls to [sqlite3_bind_text(S,N,V,L,D)] or +** [sqlite3_bind_text16(S,N,V,L,D)] SQLite binds characters +** from V through the first zero character when L is negative. +** +** {F13539} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] when D is the special +** constant [SQLITE_STATIC], SQLite assumes that the value V +** is held in static unmanaged space that will not change +** during the lifetime of the binding. +** +** {F13542} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] when D is the special +** constant [SQLITE_TRANSIENT], the routine makes a +** private copy of V value before it returns. +** +** {F13545} In calls to [sqlite3_bind_blob(S,N,V,L,D)], +** [sqlite3_bind_text(S,N,V,L,D)], or +** [sqlite3_bind_text16(S,N,V,L,D)] when D is a pointer to +** a function, SQLite invokes that function to destroy the +** V value after it has finished using the V value. +** +** {F13548} In calls to [sqlite3_bind_zeroblob(S,N,V,L)] the value bound +** is a blob of L bytes, or a zero-length blob if L is negative. */ int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); int sqlite3_bind_double(sqlite3_stmt*, int, double); @@ -1859,98 +2473,178 @@ int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); /* -** CAPI3REF: Number Of Host Parameters {F13600} +** CAPI3REF: Number Of SQL Parameters {F13600} +** +** This routine can be used to find the number of SQL parameters +** in a prepared statement. SQL parameters are tokens of the +** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as +** place-holders for values that are [sqlite3_bind_blob | bound] +** to the parameters at a later time. ** -** {F13601} Return the largest host parameter index in the precompiled -** statement given as the argument. {F13602} When the host parameters -** are of the forms like ":AAA", "$VVV", "@AAA", or "?", -** then they are assigned sequential increasing numbers beginning -** with one, so the value returned is the number of parameters. -** {F13603} However -** if the same host parameter name is used multiple times, each occurrance -** is given the same number, so the value returned in that case is the number -** of unique host parameter names. {F13604} If host parameters of the -** form "?NNN" are used (where NNN is an integer) then there might be -** gaps in the numbering and the value returned by this interface is -** the index of the host parameter with the largest index value. {END} +** This routine actually returns the index of the largest parameter. +** For all forms except ?NNN, this will correspond to the number of +** unique parameters. If parameters of the ?NNN are used, there may +** be gaps in the list. ** -** {U13605} The prepared statement must not be [sqlite3_finalize | finalized] -** prior to this routine returning. Otherwise the results are undefined -** and probably undesirable. +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_name()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13601} The [sqlite3_bind_parameter_count(S)] interface returns +** the largest index of all SQL parameters in the +** [prepared statement] S, or 0 if S +** contains no SQL parameters. */ int sqlite3_bind_parameter_count(sqlite3_stmt*); /* ** CAPI3REF: Name Of A Host Parameter {F13620} ** -** {F13621} This routine returns a pointer to the name of the n-th -** parameter in a [sqlite3_stmt | prepared statement]. {F13622} -** Host parameters of the form ":AAA" or "@AAA" or "$VVV" have a name +** This routine returns a pointer to the name of the n-th +** SQL parameter in a [prepared statement]. +** SQL parameters of the form ":AAA" or "@AAA" or "$AAA" have a name ** which is the string ":AAA" or "@AAA" or "$VVV". ** In other words, the initial ":" or "$" or "@" -** is included as part of the name. {F13626} +** is included as part of the name. ** Parameters of the form "?" or "?NNN" have no name. ** -** {F13623} The first host parameter has an index of 1, not 0. +** The first host parameter has an index of 1, not 0. ** -** {F13624} If the value n is out of range or if the n-th parameter is -** nameless, then NULL is returned. {F13625} The returned string is +** If the value n is out of range or if the n-th parameter is +** nameless, then NULL is returned. The returned string is ** always in the UTF-8 encoding even if the named parameter was ** originally specified as UTF-16 in [sqlite3_prepare16()] or ** [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13621} The [sqlite3_bind_parameter_name(S,N)] interface returns +** a UTF-8 rendering of the name of the SQL parameter in +** [prepared statement] S having index N, or +** NULL if there is no SQL parameter with index N or if the +** parameter with index N is an anonymous parameter "?" or +** a numbered parameter "?NNN". */ const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); /* ** CAPI3REF: Index Of A Parameter With A Given Name {F13640} ** -** {F13641} This routine returns the index of a host parameter with the -** given name. {F13642} The name must match exactly. {F13643} -** If no parameter with the given name is found, return 0. -** {F13644} Parameter names must be UTF8. +** Return the index of an SQL parameter given its name. The +** index value returned is suitable for use as the second +** parameter to [sqlite3_bind_blob|sqlite3_bind()]. A zero +** is returned if no matching parameter is found. The parameter +** name must be given in UTF-8 even if the original statement +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +** +** INVARIANTS: +** +** {F13641} The [sqlite3_bind_parameter_index(S,N)] interface returns +** the index of SQL parameter in [prepared statement] +** S whose name matches the UTF-8 string N, or 0 if there is +** no match. */ int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); /* ** CAPI3REF: Reset All Bindings On A Prepared Statement {F13660} ** -** {F13661} Contrary to the intuition of many, [sqlite3_reset()] does not +** Contrary to the intuition of many, [sqlite3_reset()] does not ** reset the [sqlite3_bind_blob | bindings] on a -** [sqlite3_stmt | prepared statement]. {F13662} Use this routine to +** [prepared statement]. Use this routine to ** reset all host parameters to NULL. +** +** INVARIANTS: +** +** {F13661} The [sqlite3_clear_bindings(S)] interface resets all +** SQL parameter bindings in [prepared statement] S +** back to NULL. */ int sqlite3_clear_bindings(sqlite3_stmt*); /* ** CAPI3REF: Number Of Columns In A Result Set {F13710} ** -** {F13711} Return the number of columns in the result set returned by the -** [sqlite3_stmt | compiled SQL statement]. {F13712} This routine returns 0 +** Return the number of columns in the result set returned by the +** [prepared statement]. This routine returns 0 ** if pStmt is an SQL statement that does not return data (for ** example an UPDATE). +** +** INVARIANTS: +** +** {F13711} The [sqlite3_column_count(S)] interface returns the number of +** columns in the result set generated by the +** [prepared statement] S, or 0 if S does not generate +** a result set. */ int sqlite3_column_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Column Names In A Result Set {F13720} ** -** {F13721} These routines return the name assigned to a particular column -** in the result set of a SELECT statement. {F13722} The sqlite3_column_name() +** These routines return the name assigned to a particular column +** in the result set of a SELECT statement. The sqlite3_column_name() ** interface returns a pointer to a zero-terminated UTF8 string ** and sqlite3_column_name16() returns a pointer to a zero-terminated -** UTF16 string. {F13723} The first parameter is the -** [sqlite3_stmt | prepared statement] that implements the SELECT statement. +** UTF16 string. The first parameter is the +** [prepared statement] that implements the SELECT statement. ** The second parameter is the column number. The left-most column is ** number 0. ** -** {F13724} The returned string pointer is valid until either the -** [sqlite3_stmt | prepared statement] is destroyed by [sqlite3_finalize()] +** The returned string pointer is valid until either the +** [prepared statement] is destroyed by [sqlite3_finalize()] ** or until the next call sqlite3_column_name() or sqlite3_column_name16() ** on the same column. ** -** {F13725} If sqlite3_malloc() fails during the processing of either routine +** If sqlite3_malloc() fails during the processing of either routine ** (for example during a conversion from UTF-8 to UTF-16) then a ** NULL pointer is returned. +** +** The name of a result column is the value of the "AS" clause for +** that column, if there is an AS clause. If there is no AS clause +** then the name of the column is unspecified and may change from +** one release of SQLite to the next. +** +** INVARIANTS: +** +** {F13721} A successful invocation of the [sqlite3_column_name(S,N)] +** interface returns the name +** of the Nth column (where 0 is the left-most column) for the +** result set of [prepared statement] S as a +** zero-terminated UTF-8 string. +** +** {F13723} A successful invocation of the [sqlite3_column_name16(S,N)] +** interface returns the name +** of the Nth column (where 0 is the left-most column) for the +** result set of [prepared statement] S as a +** zero-terminated UTF-16 string in the native byte order. +** +** {F13724} The [sqlite3_column_name()] and [sqlite3_column_name16()] +** interfaces return a NULL pointer if they are unable to +** allocate memory memory to hold there normal return strings. +** +** {F13725} If the N parameter to [sqlite3_column_name(S,N)] or +** [sqlite3_column_name16(S,N)] is out of range, then the +** interfaces returns a NULL pointer. +** +** {F13726} The strings returned by [sqlite3_column_name(S,N)] and +** [sqlite3_column_name16(S,N)] are valid until the next +** call to either routine with the same S and N parameters +** or until [sqlite3_finalize(S)] is called. +** +** {F13727} When a result column of a [SELECT] statement contains +** an AS clause, the name of that column is the indentifier +** to the right of the AS keyword. */ const char *sqlite3_column_name(sqlite3_stmt*, int N); const void *sqlite3_column_name16(sqlite3_stmt*, int N); @@ -1958,32 +2652,32 @@ const void *sqlite3_column_name16(sqlite3_stmt*, int N); /* ** CAPI3REF: Source Of Data In A Query Result {F13740} ** -** {F13741} These routines provide a means to determine what column of what +** These routines provide a means to determine what column of what ** table in which database a result of a SELECT statement comes from. -** {F13742} The name of the database or table or column can be returned as -** either a UTF8 or UTF16 string. {F13743} The _database_ routines return +** The name of the database or table or column can be returned as +** either a UTF8 or UTF16 string. The _database_ routines return ** the database name, the _table_ routines return the table name, and -** the origin_ routines return the column name. {F13744} +** the origin_ routines return the column name. ** The returned string is valid until -** the [sqlite3_stmt | prepared statement] is destroyed using +** the [prepared statement] is destroyed using ** [sqlite3_finalize()] or until the same information is requested ** again in a different encoding. ** -** {F13745} The names returned are the original un-aliased names of the +** The names returned are the original un-aliased names of the ** database, table, and column. ** -** {F13746} The first argument to the following calls is a -** [sqlite3_stmt | compiled SQL statement]. -** {F13747} These functions return information about the Nth column returned by +** The first argument to the following calls is a [prepared statement]. +** These functions return information about the Nth column returned by ** the statement, where N is the second function argument. ** -** {F13748} If the Nth column returned by the statement is an expression +** If the Nth column returned by the statement is an expression ** or subquery and is not a column value, then all of these functions -** return NULL. {F13749} Otherwise, they return the +** return NULL. These routine might also return NULL if a memory +** allocation error occurs. Otherwise, they return the ** name of the attached database, table and column that query result ** column was extracted from. ** -** {F13750} As with all other SQLite APIs, those postfixed with "16" return +** As with all other SQLite APIs, those postfixed with "16" return ** UTF-16 encoded strings, the other functions return UTF-8. {END} ** ** These APIs are only available if the library was compiled with the @@ -1993,6 +2687,67 @@ const void *sqlite3_column_name16(sqlite3_stmt*, int N); ** If two or more threads call one or more of these routines against the same ** prepared statement and column at the same time then the results are ** undefined. +** +** INVARIANTS: +** +** {F13741} The [sqlite3_column_database_name(S,N)] interface returns either +** the UTF-8 zero-terminated name of the database from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13742} The [sqlite3_column_database_name16(S,N)] interface returns either +** the UTF-16 native byte order +** zero-terminated name of the database from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13743} The [sqlite3_column_table_name(S,N)] interface returns either +** the UTF-8 zero-terminated name of the table from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13744} The [sqlite3_column_table_name16(S,N)] interface returns either +** the UTF-16 native byte order +** zero-terminated name of the table from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13745} The [sqlite3_column_origin_name(S,N)] interface returns either +** the UTF-8 zero-terminated name of the table column from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13746} The [sqlite3_column_origin_name16(S,N)] interface returns either +** the UTF-16 native byte order +** zero-terminated name of the table column from which the +** Nth result column of [prepared statement] S +** is extracted, or NULL if the the Nth column of S is a +** general expression or if unable to allocate memory +** to store the name. +** +** {F13748} The return values from +** [sqlite3_column_database_name|column metadata interfaces] +** are valid +** for the lifetime of the [prepared statement] +** or until the encoding is changed by another metadata +** interface call for the same prepared statement and column. +** +** LIMITATIONS: +** +** {U13751} If two or more threads call one or more +** [sqlite3_column_database_name|column metadata interfaces] +** the same [prepared statement] and result column +** at the same time then the results are undefined. */ const char *sqlite3_column_database_name(sqlite3_stmt*,int); const void *sqlite3_column_database_name16(sqlite3_stmt*,int); @@ -2004,13 +2759,13 @@ const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); /* ** CAPI3REF: Declared Datatype Of A Query Result {F13760} ** -** The first parameter is a [sqlite3_stmt | compiled SQL statement]. -** {F13761} If this statement is a SELECT statement and the Nth column of the +** The first parameter is a [prepared statement]. +** If this statement is a SELECT statement and the Nth column of the ** returned result set of that SELECT is a table column (not an ** expression or subquery) then the declared type of the table -** column is returned. {F13762} If the Nth column of the result set is an +** column is returned. If the Nth column of the result set is an ** expression or subquery, then a NULL pointer is returned. -** {F13763} The returned string is always UTF-8 encoded. {END} +** The returned string is always UTF-8 encoded. {END} ** For example, in the database schema: ** ** CREATE TABLE t1(c1 VARIANT); @@ -2029,14 +2784,36 @@ const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); ** strongly typed, but the typing is dynamic not static. Type ** is associated with individual values, not with the containers ** used to hold those values. +** +** INVARIANTS: +** +** {F13761} A successful call to [sqlite3_column_decltype(S,N)] +** returns a zero-terminated UTF-8 string containing the +** the declared datatype of the table column that appears +** as the Nth column (numbered from 0) of the result set to the +** [prepared statement] S. +** +** {F13762} A successful call to [sqlite3_column_decltype16(S,N)] +** returns a zero-terminated UTF-16 native byte order string +** containing the declared datatype of the table column that appears +** as the Nth column (numbered from 0) of the result set to the +** [prepared statement] S. +** +** {F13763} If N is less than 0 or N is greater than or equal to +** the number of columns in [prepared statement] S +** or if the Nth column of S is an expression or subquery rather +** than a table column or if a memory allocation failure +** occurs during encoding conversions, then +** calls to [sqlite3_column_decltype(S,N)] or +** [sqlite3_column_decltype16(S,N)] return NULL. */ -const char *sqlite3_column_decltype(sqlite3_stmt *, int i); +const char *sqlite3_column_decltype(sqlite3_stmt*,int); const void *sqlite3_column_decltype16(sqlite3_stmt*,int); /* ** CAPI3REF: Evaluate An SQL Statement {F13200} ** -** After an [sqlite3_stmt | SQL statement] has been prepared with a call +** After an [prepared statement] has been prepared with a call ** to either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or to one of ** the legacy interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], ** then this function must be called one or more times to evaluate the @@ -2079,11 +2856,11 @@ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** With the legacy interface, a more specific error code (example: ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) ** can be obtained by calling [sqlite3_reset()] on the -** [sqlite3_stmt | prepared statement]. In the "v2" interface, +** [prepared statement]. In the "v2" interface, ** the more specific error code is returned directly by sqlite3_step(). ** ** [SQLITE_MISUSE] means that the this routine was called inappropriately. -** Perhaps it was called on a [sqlite3_stmt | prepared statement] that has +** Perhaps it was called on a [prepared statement] that has ** already been [sqlite3_finalize | finalized] or on one that had ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could ** be the case that the same database connection is being used by two or @@ -2095,13 +2872,40 @@ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** [SQLITE_ERROR], following any error other than [SQLITE_BUSY] ** and [SQLITE_MISUSE]. You must call [sqlite3_reset()] or ** [sqlite3_finalize()] in order to find one of the specific -** [SQLITE_ERROR | result codes] that better describes the error. +** [error codes] that better describes the error. ** We admit that this is a goofy design. The problem has been fixed ** with the "v2" interface. If you prepare all of your SQL statements ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()], then the -** more specific [SQLITE_ERROR | result codes] are returned directly +** more specific [error codes] are returned directly ** by sqlite3_step(). The use of the "v2" interface is recommended. +** +** INVARIANTS: +** +** {F13202} If [prepared statement] S is ready to be +** run, then [sqlite3_step(S)] advances that prepared statement +** until to completion or until it is ready to return another +** row of the result set or an interrupt or run-time error occurs. +** +** {F15304} When a call to [sqlite3_step(S)] causes the +** [prepared statement] S to run to completion, +** the function returns [SQLITE_DONE]. +** +** {F15306} When a call to [sqlite3_step(S)] stops because it is ready +** to return another row of the result set, it returns +** [SQLITE_ROW]. +** +** {F15308} If a call to [sqlite3_step(S)] encounters an +** [sqlite3_interrupt|interrupt] or a run-time error, +** it returns an appropraite error code that is not one of +** [SQLITE_OK], [SQLITE_ROW], or [SQLITE_DONE]. +** +** {F15310} If an [sqlite3_interrupt|interrupt] or run-time error +** occurs during a call to [sqlite3_step(S)] +** for a [prepared statement] S created using +** legacy interfaces [sqlite3_prepare()] or +** [sqlite3_prepare16()] then the function returns either +** [SQLITE_ERROR], [SQLITE_BUSY], or [SQLITE_MISUSE]. */ int sqlite3_step(sqlite3_stmt*); @@ -2110,19 +2914,25 @@ int sqlite3_step(sqlite3_stmt*); ** ** Return the number of values in the current row of the result set. ** -** {F13771} After a call to [sqlite3_step()] that returns [SQLITE_ROW], -** this routine -** will return the same value as the [sqlite3_column_count()] function. -** {F13772} -** After [sqlite3_step()] has returned an [SQLITE_DONE], [SQLITE_BUSY], or -** a [SQLITE_ERROR | error code], or before [sqlite3_step()] has been -** called on the [sqlite3_stmt | prepared statement] for the first time, -** this routine returns zero. +** INVARIANTS: +** +** {F13771} After a call to [sqlite3_step(S)] that returns +** [SQLITE_ROW], the [sqlite3_data_count(S)] routine +** will return the same value as the +** [sqlite3_column_count(S)] function. +** +** {F13772} After [sqlite3_step(S)] has returned any value other than +** [SQLITE_ROW] or before [sqlite3_step(S)] has been +** called on the [prepared statement] for +** the first time since it was [sqlite3_prepare|prepared] +** or [sqlite3_reset|reset], the [sqlite3_data_count(S)] +** routine returns zero. */ int sqlite3_data_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Fundamental Datatypes {F10265} +** KEYWORDS: SQLITE_TEXT ** ** {F10266}Every value in SQLite has one of five fundamental datatypes: ** @@ -2155,10 +2965,12 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); /* ** CAPI3REF: Results Values From A Query {F13800} ** +** These routines form the "result set query" interface. +** ** These routines return information about ** a single column of the current result row of a query. In every ** case the first argument is a pointer to the -** [sqlite3_stmt | SQL statement] that is being +** [prepared statement] that is being ** evaluated (the [sqlite3_stmt*] that was returned from ** [sqlite3_prepare_v2()] or one of its variants) and ** the second argument is the index of the column for which information @@ -2199,12 +3011,12 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** bytes in the string, not the number of characters. ** ** Strings returned by sqlite3_column_text() and sqlite3_column_text16(), -** even zero-length strings, are always zero terminated. The return +** even empty strings, are always zero terminated. The return ** value from sqlite3_column_blob() for a zero-length blob is an arbitrary ** pointer, possibly even a NULL pointer. ** ** The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes() -** but leaves the result in UTF-16 instead of UTF-8. +** but leaves the result in UTF-16 in native byte order instead of UTF-8. ** The zero terminator is not included in this count. ** ** These routines attempt to convert the value where appropriate. For @@ -2296,6 +3108,61 @@ int sqlite3_data_count(sqlite3_stmt *pStmt); ** is either the integer 0, the floating point number 0.0, or a NULL ** pointer. Subsequent calls to [sqlite3_errcode()] will return ** [SQLITE_NOMEM]. +** +** INVARIANTS: +** +** {F13803} The [sqlite3_column_blob(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a blob and then returns a +** pointer to the converted value. +** +** {F13806} The [sqlite3_column_bytes(S,N)] interface returns the +** number of bytes in the blob or string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_column_blob(S,N)] or +** [sqlite3_column_text(S,N)]. +** +** {F13809} The [sqlite3_column_bytes16(S,N)] interface returns the +** number of bytes in the string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_column_text16(S,N)]. +** +** {F13812} The [sqlite3_column_double(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a floating point value and +** returns a copy of that value. +** +** {F13815} The [sqlite3_column_int(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a 64-bit signed integer and +** returns the lower 32 bits of that integer. +** +** {F13818} The [sqlite3_column_int64(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a 64-bit signed integer and +** returns a copy of that integer. +** +** {F13821} The [sqlite3_column_text(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a zero-terminated UTF-8 +** string and returns a pointer to that string. +** +** {F13824} The [sqlite3_column_text16(S,N)] interface converts the +** Nth column in the current row of the result set for +** [prepared statement] S into a zero-terminated 2-byte +** aligned UTF-16 native byte order +** string and returns a pointer to that string. +** +** {F13827} The [sqlite3_column_type(S,N)] interface returns +** one of [SQLITE_NULL], [SQLITE_INTEGER], [SQLITE_FLOAT], +** [SQLITE_TEXT], or [SQLITE_BLOB] as appropriate for +** the Nth column in the current row of the result set for +** [prepared statement] S. +** +** {F13830} The [sqlite3_column_value(S,N)] interface returns a +** pointer to the [sqlite3_value] object that for the +** Nth column in the current row of the result set for +** [prepared statement] S. */ const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); int sqlite3_column_bytes(sqlite3_stmt*, int iCol); @@ -2312,19 +3179,29 @@ sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); ** CAPI3REF: Destroy A Prepared Statement Object {F13300} ** ** The sqlite3_finalize() function is called to delete a -** [sqlite3_stmt | compiled SQL statement]. If the statement was +** [prepared statement]. If the statement was ** executed successfully, or not executed at all, then SQLITE_OK is returned. ** If execution of the statement failed then an -** [SQLITE_ERROR | error code] or [SQLITE_IOERR_READ | extended error code] +** [error code] or [extended error code] ** is returned. ** ** This routine can be called at any point during the execution of the -** [sqlite3_stmt | virtual machine]. If the virtual machine has not +** [prepared statement]. If the virtual machine has not ** completed execution when this routine is called, that is like ** encountering an error or an interrupt. (See [sqlite3_interrupt()].) ** Incomplete updates may be rolled back and transactions cancelled, ** depending on the circumstances, and the -** [SQLITE_ERROR | result code] returned will be [SQLITE_ABORT]. +** [error code] returned will be [SQLITE_ABORT]. +** +** INVARIANTS: +** +** {F11302} The [sqlite3_finalize(S)] interface destroys the +** [prepared statement] S and releases all +** memory and file resources held by that object. +** +** {F11304} If the most recent call to [sqlite3_step(S)] for the +** [prepared statement] S returned an error, +** then [sqlite3_finalize(S)] returns that same error. */ int sqlite3_finalize(sqlite3_stmt *pStmt); @@ -2332,28 +3209,44 @@ int sqlite3_finalize(sqlite3_stmt *pStmt); ** CAPI3REF: Reset A Prepared Statement Object {F13330} ** ** The sqlite3_reset() function is called to reset a -** [sqlite3_stmt | compiled SQL statement] object. +** [prepared statement] object. ** back to its initial state, ready to be re-executed. ** Any SQL statement variables that had values bound to them using ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. ** Use [sqlite3_clear_bindings()] to reset the bindings. +** +** {F11332} The [sqlite3_reset(S)] interface resets the [prepared statement] S +** back to the beginning of its program. +** +** {F11334} If the most recent call to [sqlite3_step(S)] for +** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE], +** or if [sqlite3_step(S)] has never before been called on S, +** then [sqlite3_reset(S)] returns [SQLITE_OK]. +** +** {F11336} If the most recent call to [sqlite3_step(S)] for +** [prepared statement] S indicated an error, then +** [sqlite3_reset(S)] returns an appropriate [error code]. +** +** {F11338} The [sqlite3_reset(S)] interface does not change the values +** of any [sqlite3_bind_blob|bindings] on [prepared statement] S. */ int sqlite3_reset(sqlite3_stmt *pStmt); /* ** CAPI3REF: Create Or Redefine SQL Functions {F16100} +** KEYWORDS: {function creation routines} ** -** The following two functions are used to add SQL functions or aggregates +** These two functions (collectively known as +** "function creation routines") are used to add SQL functions or aggregates ** or to redefine the behavior of existing SQL functions or aggregates. The ** difference only between the two is that the second parameter, the ** name of the (scalar) function or aggregate, is encoded in UTF-8 for ** sqlite3_create_function() and UTF-16 for sqlite3_create_function16(). ** -** The first argument is the [sqlite3 | database handle] that holds the -** SQL function or aggregate is to be added or redefined. If a single -** program uses more than one database handle internally, then SQL -** functions or aggregates must be added individually to each database -** handle with which they will be used. +** The first parameter is the [database connection] to which the SQL +** function is to be added. If a single +** program uses more than one [database connection] internally, then SQL +** functions must be added individually to each [database connection]. ** ** The second parameter is the name of the SQL function to be created ** or redefined. @@ -2397,23 +3290,83 @@ int sqlite3_reset(sqlite3_stmt *pStmt); ** arguments or differing perferred text encodings. SQLite will use ** the implementation most closely matches the way in which the ** SQL function is used. +** +** INVARIANTS: +** +** {F16103} The [sqlite3_create_function16()] interface behaves exactly +** like [sqlite3_create_function()] in every way except that it +** interprets the zFunctionName argument as +** zero-terminated UTF-16 native byte order instead of as a +** zero-terminated UTF-8. +** +** {F16106} A successful invocation of +** the [sqlite3_create_function(D,X,N,E,...)] interface registers +** or replaces callback functions in [database connection] D +** used to implement the SQL function named X with N parameters +** and having a perferred text encoding of E. +** +** {F16109} A successful call to [sqlite3_create_function(D,X,N,E,P,F,S,L)] +** replaces the P, F, S, and L values from any prior calls with +** the same D, X, N, and E values. +** +** {F16112} The [sqlite3_create_function(D,X,...)] interface fails with +** a return code of [SQLITE_ERROR] if the SQL function name X is +** longer than 255 bytes exclusive of the zero terminator. +** +** {F16118} Either F must be NULL and S and L are non-NULL or else F +** is non-NULL and S and L are NULL, otherwise +** [sqlite3_create_function(D,X,N,E,P,F,S,L)] returns [SQLITE_ERROR]. +** +** {F16121} The [sqlite3_create_function(D,...)] interface fails with an +** error code of [SQLITE_BUSY] if there exist [prepared statements] +** associated with the [database connection] D. +** +** {F16124} The [sqlite3_create_function(D,X,N,...)] interface fails with an +** error code of [SQLITE_ERROR] if parameter N (specifying the number +** of arguments to the SQL function being registered) is less +** than -1 or greater than 127. +** +** {F16127} When N is non-negative, the [sqlite3_create_function(D,X,N,...)] +** interface causes callbacks to be invoked for the SQL function +** named X when the number of arguments to the SQL function is +** exactly N. +** +** {F16130} When N is -1, the [sqlite3_create_function(D,X,N,...)] +** interface causes callbacks to be invoked for the SQL function +** named X with any number of arguments. +** +** {F16133} When calls to [sqlite3_create_function(D,X,N,...)] +** specify multiple implementations of the same function X +** and when one implementation has N>=0 and the other has N=(-1) +** the implementation with a non-zero N is preferred. +** +** {F16136} When calls to [sqlite3_create_function(D,X,N,E,...)] +** specify multiple implementations of the same function X with +** the same number of arguments N but with different +** encodings E, then the implementation where E matches the +** database encoding is preferred. +** +** {F16139} For an aggregate SQL function created using +** [sqlite3_create_function(D,X,N,E,P,0,S,L)] the finializer +** function L will always be invoked exactly once if the +** step function S is called one or more times. */ int sqlite3_create_function( - sqlite3 *, + sqlite3 *db, const char *zFunctionName, int nArg, int eTextRep, - void*, + void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*) ); int sqlite3_create_function16( - sqlite3*, + sqlite3 *db, const void *zFunctionName, int nArg, int eTextRep, - void*, + void *pApp, void (*xFunc)(sqlite3_context*,int,sqlite3_value**), void (*xStep)(sqlite3_context*,int,sqlite3_value**), void (*xFinal)(sqlite3_context*) @@ -2493,6 +3446,68 @@ int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); ** interface, then these routines should be called from the same thread ** that ran [sqlite3_column_value()]. ** +** +** INVARIANTS: +** +** {F15103} The [sqlite3_value_blob(V)] interface converts the +** [sqlite3_value] object V into a blob and then returns a +** pointer to the converted value. +** +** {F15106} The [sqlite3_value_bytes(V)] interface returns the +** number of bytes in the blob or string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_value_blob(V)] or +** [sqlite3_value_text(V)]. +** +** {F15109} The [sqlite3_value_bytes16(V)] interface returns the +** number of bytes in the string (exclusive of the +** zero terminator on the string) that was returned by the +** most recent call to [sqlite3_value_text16(V)], +** [sqlite3_value_text16be(V)], or [sqlite3_value_text16le(V)]. +** +** {F15112} The [sqlite3_value_double(V)] interface converts the +** [sqlite3_value] object V into a floating point value and +** returns a copy of that value. +** +** {F15115} The [sqlite3_value_int(V)] interface converts the +** [sqlite3_value] object V into a 64-bit signed integer and +** returns the lower 32 bits of that integer. +** +** {F15118} The [sqlite3_value_int64(V)] interface converts the +** [sqlite3_value] object V into a 64-bit signed integer and +** returns a copy of that integer. +** +** {F15121} The [sqlite3_value_text(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated UTF-8 +** string and returns a pointer to that string. +** +** {F15124} The [sqlite3_value_text16(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated 2-byte +** aligned UTF-16 native byte order +** string and returns a pointer to that string. +** +** {F15127} The [sqlite3_value_text16be(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated 2-byte +** aligned UTF-16 big-endian +** string and returns a pointer to that string. +** +** {F15130} The [sqlite3_value_text16le(V)] interface converts the +** [sqlite3_value] object V into a zero-terminated 2-byte +** aligned UTF-16 little-endian +** string and returns a pointer to that string. +** +** {F15133} The [sqlite3_value_type(V)] interface returns +** one of [SQLITE_NULL], [SQLITE_INTEGER], [SQLITE_FLOAT], +** [SQLITE_TEXT], or [SQLITE_BLOB] as appropriate for +** the [sqlite3_value] object V. +** +** {F15136} The [sqlite3_value_numeric_type(V)] interface converts +** the [sqlite3_value] object V into either an integer or +** a floating point value if it can do so without loss of +** information, and returns one of [SQLITE_NULL], +** [SQLITE_INTEGER], [SQLITE_FLOAT], [SQLITE_TEXT], or +** [SQLITE_BLOB] as appropriate for +** the [sqlite3_value] object V after the conversion attempt. */ const void *sqlite3_value_blob(sqlite3_value*); int sqlite3_value_bytes(sqlite3_value*); @@ -2512,16 +3527,16 @@ int sqlite3_value_numeric_type(sqlite3_value*); ** ** The implementation of aggregate SQL functions use this routine to allocate ** a structure for storing their state. -** {F16211} The first time the sqlite3_aggregate_context() routine is +** The first time the sqlite3_aggregate_context() routine is ** is called for a particular aggregate, SQLite allocates nBytes of memory ** zeros that memory, and returns a pointer to it. -** {F16212} On second and subsequent calls to sqlite3_aggregate_context() -** for the same aggregate function index, the same buffer is returned. {END} +** On second and subsequent calls to sqlite3_aggregate_context() +** for the same aggregate function index, the same buffer is returned. ** The implementation ** of the aggregate can use the returned buffer to accumulate data. ** -** {F16213} SQLite automatically frees the allocated buffer when the aggregate -** query concludes. {END} +** SQLite automatically frees the allocated buffer when the aggregate +** query concludes. ** ** The first parameter should be a copy of the ** [sqlite3_context | SQL function context] that is the first @@ -2530,20 +3545,49 @@ int sqlite3_value_numeric_type(sqlite3_value*); ** ** This routine must be called from the same thread in which ** the aggregate SQL function is running. +** +** INVARIANTS: +** +** {F16211} The first invocation of [sqlite3_aggregate_context(C,N)] for +** a particular instance of an aggregate function (for a particular +** context C) causes SQLite to allocation N bytes of memory, +** zero that memory, and return a pointer to the allocationed +** memory. +** +** {F16213} If a memory allocation error occurs during +** [sqlite3_aggregate_context(C,N)] then the function returns 0. +** +** {F16215} Second and subsequent invocations of +** [sqlite3_aggregate_context(C,N)] for the same context pointer C +** ignore the N parameter and return a pointer to the same +** block of memory returned by the first invocation. +** +** {F16217} The memory allocated by [sqlite3_aggregate_context(C,N)] is +** automatically freed on the next call to [sqlite3_reset()] +** or [sqlite3_finalize()] for the [prepared statement] containing +** the aggregate function associated with context C. */ void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); /* ** CAPI3REF: User Data For Functions {F16240} ** -** {F16241} The sqlite3_user_data() interface returns a copy of +** The sqlite3_user_data() interface returns a copy of ** the pointer that was the pUserData parameter (the 5th parameter) ** of the the [sqlite3_create_function()] ** and [sqlite3_create_function16()] routines that originally ** registered the application defined function. {END} ** -** {U16243} This routine must be called from the same thread in which +** This routine must be called from the same thread in which ** the application-defined function is running. +** +** INVARIANTS: +** +** {F16243} The [sqlite3_user_data(C)] interface returns a copy of the +** P pointer from the [sqlite3_create_function(D,X,N,E,P,F,S,L)] +** or [sqlite3_create_function16(D,X,N,E,P,F,S,L)] call that +** registered the SQL function associated with +** [sqlite3_context] C. */ void *sqlite3_user_data(sqlite3_context*); @@ -2561,24 +3605,28 @@ void *sqlite3_user_data(sqlite3_context*); ** invocations of the same function so that the original pattern string ** does not need to be recompiled on each invocation. ** -** {F16271} ** The sqlite3_get_auxdata() interface returns a pointer to the meta-data ** associated by the sqlite3_set_auxdata() function with the Nth argument ** value to the application-defined function. -** {F16272} If no meta-data has been ever been set for the Nth +** If no meta-data has been ever been set for the Nth ** argument of the function, or if the cooresponding function parameter ** has changed since the meta-data was set, then sqlite3_get_auxdata() ** returns a NULL pointer. ** -** {F16275} The sqlite3_set_auxdata() interface saves the meta-data +** The sqlite3_set_auxdata() interface saves the meta-data ** pointed to by its 3rd parameter as the meta-data for the N-th -** argument of the application-defined function. {END} Subsequent +** argument of the application-defined function. Subsequent ** calls to sqlite3_get_auxdata() might return this data, if it has ** not been destroyed. -** {F16277} If it is not NULL, SQLite will invoke the destructor +** If it is not NULL, SQLite will invoke the destructor ** function given by the 4th parameter to sqlite3_set_auxdata() on ** the meta-data when the corresponding function parameter changes -** or when the SQL statement completes, whichever comes first. {END} +** or when the SQL statement completes, whichever comes first. +** +** SQLite is free to call the destructor and drop meta-data on +** any parameter of any function at any time. The only guarantee +** is that the destructor will be called before the metadata is +** dropped. ** ** In practice, meta-data is preserved between function calls for ** expressions that are constant at compile time. This includes literal @@ -2586,6 +3634,33 @@ void *sqlite3_user_data(sqlite3_context*); ** ** These routines must be called from the same thread in which ** the SQL function is running. +** +** INVARIANTS: +** +** {F16272} The [sqlite3_get_auxdata(C,N)] interface returns a pointer +** to metadata associated with the Nth parameter of the SQL function +** whose context is C, or NULL if there is no metadata associated +** with that parameter. +** +** {F16274} The [sqlite3_set_auxdata(C,N,P,D)] interface assigns a metadata +** pointer P to the Nth parameter of the SQL function with context +** C. +** +** {F16276} SQLite will invoke the destructor D with a single argument +** which is the metadata pointer P following a call to +** [sqlite3_set_auxdata(C,N,P,D)] when SQLite ceases to hold +** the metadata. +** +** {F16277} SQLite ceases to hold metadata for an SQL function parameter +** when the value of that parameter changes. +** +** {F16278} When [sqlite3_set_auxdata(C,N,P,D)] is invoked, the destructor +** is called for any prior metadata associated with the same function +** context C and parameter N. +** +** {F16279} SQLite will call destructors for any metadata it is holding +** in a particular [prepared statement] S when either +** [sqlite3_reset(S)] or [sqlite3_finalize(S)] is called. */ void *sqlite3_get_auxdata(sqlite3_context*, int N); void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); @@ -2624,90 +3699,188 @@ typedef void (*sqlite3_destructor_type)(void*); ** [sqlite3_bind_blob | sqlite3_bind_* documentation] for ** additional information. ** -** {F16402} The sqlite3_result_blob() interface sets the result from +** The sqlite3_result_blob() interface sets the result from ** an application defined function to be the BLOB whose content is pointed ** to by the second parameter and which is N bytes long where N is the ** third parameter. -** {F16403} The sqlite3_result_zeroblob() inerfaces set the result of +** The sqlite3_result_zeroblob() inerfaces set the result of ** the application defined function to be a BLOB containing all zero ** bytes and N bytes in size, where N is the value of the 2nd parameter. ** -** {F16407} The sqlite3_result_double() interface sets the result from +** The sqlite3_result_double() interface sets the result from ** an application defined function to be a floating point value specified ** by its 2nd argument. ** -** {F16409} The sqlite3_result_error() and sqlite3_result_error16() functions +** The sqlite3_result_error() and sqlite3_result_error16() functions ** cause the implemented SQL function to throw an exception. -** {F16411} SQLite uses the string pointed to by the +** SQLite uses the string pointed to by the ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() -** as the text of an error message. {F16412} SQLite interprets the error -** message string from sqlite3_result_error() as UTF8. {F16413} SQLite +** as the text of an error message. SQLite interprets the error +** message string from sqlite3_result_error() as UTF8. SQLite ** interprets the string from sqlite3_result_error16() as UTF16 in native -** byte order. {F16414} If the third parameter to sqlite3_result_error() +** byte order. If the third parameter to sqlite3_result_error() ** or sqlite3_result_error16() is negative then SQLite takes as the error ** message all text up through the first zero character. -** {F16415} If the third parameter to sqlite3_result_error() or +** If the third parameter to sqlite3_result_error() or ** sqlite3_result_error16() is non-negative then SQLite takes that many ** bytes (not characters) from the 2nd parameter as the error message. -** {F16417} The sqlite3_result_error() and sqlite3_result_error16() +** The sqlite3_result_error() and sqlite3_result_error16() ** routines make a copy private copy of the error message text before -** they return. {END} Hence, the calling function can deallocate or +** they return. Hence, the calling function can deallocate or ** modify the text after they return without harm. +** The sqlite3_result_error_code() function changes the error code +** returned by SQLite as a result of an error in a function. By default, +** the error code is SQLITE_ERROR. ** -** {F16421} The sqlite3_result_toobig() interface causes SQLite +** The sqlite3_result_toobig() interface causes SQLite ** to throw an error indicating that a string or BLOB is to long -** to represent. {F16422} The sqlite3_result_nomem() interface +** to represent. The sqlite3_result_nomem() interface ** causes SQLite to throw an exception indicating that the a ** memory allocation failed. ** -** {F16431} The sqlite3_result_int() interface sets the return value +** The sqlite3_result_int() interface sets the return value ** of the application-defined function to be the 32-bit signed integer ** value given in the 2nd argument. -** {F16432} The sqlite3_result_int64() interface sets the return value +** The sqlite3_result_int64() interface sets the return value ** of the application-defined function to be the 64-bit signed integer ** value given in the 2nd argument. ** -** {F16437} The sqlite3_result_null() interface sets the return value +** The sqlite3_result_null() interface sets the return value ** of the application-defined function to be NULL. ** -** {F16441} The sqlite3_result_text(), sqlite3_result_text16(), +** The sqlite3_result_text(), sqlite3_result_text16(), ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces ** set the return value of the application-defined function to be ** a text string which is represented as UTF-8, UTF-16 native byte order, ** UTF-16 little endian, or UTF-16 big endian, respectively. -** {F16442} SQLite takes the text result from the application from +** SQLite takes the text result from the application from ** the 2nd parameter of the sqlite3_result_text* interfaces. -** {F16444} If the 3rd parameter to the sqlite3_result_text* interfaces +** If the 3rd parameter to the sqlite3_result_text* interfaces ** is negative, then SQLite takes result text from the 2nd parameter ** through the first zero character. -** {F16447} If the 3rd parameter to the sqlite3_result_text* interfaces +** If the 3rd parameter to the sqlite3_result_text* interfaces ** is non-negative, then as many bytes (not characters) of the text ** pointed to by the 2nd parameter are taken as the application-defined ** function result. -** {F16451} If the 4th parameter to the sqlite3_result_text* interfaces +** If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that ** function as the destructor on the text or blob result when it has ** finished using that result. -** {F16453} If the 4th parameter to the sqlite3_result_text* interfaces +** If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is the special constant SQLITE_STATIC, then ** SQLite assumes that the text or blob result is constant space and ** does not copy the space or call a destructor when it has ** finished using that result. -** {F16454} If the 4th parameter to the sqlite3_result_text* interfaces +** If the 4th parameter to the sqlite3_result_text* interfaces ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT ** then SQLite makes a copy of the result into space obtained from ** from [sqlite3_malloc()] before it returns. ** -** {F16461} The sqlite3_result_value() interface sets the result of +** The sqlite3_result_value() interface sets the result of ** the application-defined function to be a copy the [sqlite3_value] -** object specified by the 2nd parameter. {F16463} The +** object specified by the 2nd parameter. The ** sqlite3_result_value() interface makes a copy of the [sqlite3_value] ** so that [sqlite3_value] specified in the parameter may change or ** be deallocated after sqlite3_result_value() returns without harm. ** -** {U16491} These routines are called from within the different thread +** If these routines are called from within the different thread ** than the one containing the application-defined function that recieved ** the [sqlite3_context] pointer, the results are undefined. +** +** INVARIANTS: +** +** {F16403} The default return value from any SQL function is NULL. +** +** {F16406} The [sqlite3_result_blob(C,V,N,D)] interface changes the +** return value of function C to be a blob that is N bytes +** in length and with content pointed to by V. +** +** {F16409} The [sqlite3_result_double(C,V)] interface changes the +** return value of function C to be the floating point value V. +** +** {F16412} The [sqlite3_result_error(C,V,N)] interface changes the return +** value of function C to be an exception with error code +** [SQLITE_ERROR] and a UTF8 error message copied from V up to the +** first zero byte or until N bytes are read if N is positive. +** +** {F16415} The [sqlite3_result_error16(C,V,N)] interface changes the return +** value of function C to be an exception with error code +** [SQLITE_ERROR] and a UTF16 native byte order error message +** copied from V up to the first zero terminator or until N bytes +** are read if N is positive. +** +** {F16418} The [sqlite3_result_error_toobig(C)] interface changes the return +** value of the function C to be an exception with error code +** [SQLITE_TOOBIG] and an appropriate error message. +** +** {F16421} The [sqlite3_result_error_nomem(C)] interface changes the return +** value of the function C to be an exception with error code +** [SQLITE_NOMEM] and an appropriate error message. +** +** {F16424} The [sqlite3_result_error_code(C,E)] interface changes the return +** value of the function C to be an exception with error code E. +** The error message text is unchanged. +** +** {F16427} The [sqlite3_result_int(C,V)] interface changes the +** return value of function C to be the 32-bit integer value V. +** +** {F16430} The [sqlite3_result_int64(C,V)] interface changes the +** return value of function C to be the 64-bit integer value V. +** +** {F16433} The [sqlite3_result_null(C)] interface changes the +** return value of function C to be NULL. +** +** {F16436} The [sqlite3_result_text(C,V,N,D)] interface changes the +** return value of function C to be the UTF8 string +** V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16439} The [sqlite3_result_text16(C,V,N,D)] interface changes the +** return value of function C to be the UTF16 native byte order +** string V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16442} The [sqlite3_result_text16be(C,V,N,D)] interface changes the +** return value of function C to be the UTF16 big-endian +** string V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16445} The [sqlite3_result_text16le(C,V,N,D)] interface changes the +** return value of function C to be the UTF16 little-endian +** string V up through the first zero or until N bytes are read if N +** is positive. +** +** {F16448} The [sqlite3_result_value(C,V)] interface changes the +** return value of function C to be [sqlite3_value] object V. +** +** {F16451} The [sqlite3_result_zeroblob(C,N)] interface changes the +** return value of function C to be an N-byte blob of all zeros. +** +** {F16454} The [sqlite3_result_error()] and [sqlite3_result_error16()] +** interfaces make a copy of their error message strings before +** returning. +** +** {F16457} If the D destructor parameter to [sqlite3_result_blob(C,V,N,D)], +** [sqlite3_result_text(C,V,N,D)], [sqlite3_result_text16(C,V,N,D)], +** [sqlite3_result_text16be(C,V,N,D)], or +** [sqlite3_result_text16le(C,V,N,D)] is the constant [SQLITE_STATIC] +** then no destructor is ever called on the pointer V and SQLite +** assumes that V is immutable. +** +** {F16460} If the D destructor parameter to [sqlite3_result_blob(C,V,N,D)], +** [sqlite3_result_text(C,V,N,D)], [sqlite3_result_text16(C,V,N,D)], +** [sqlite3_result_text16be(C,V,N,D)], or +** [sqlite3_result_text16le(C,V,N,D)] is the constant +** [SQLITE_TRANSIENT] then the interfaces makes a copy of the +** content of V and retains the copy. +** +** {F16463} If the D destructor parameter to [sqlite3_result_blob(C,V,N,D)], +** [sqlite3_result_text(C,V,N,D)], [sqlite3_result_text16(C,V,N,D)], +** [sqlite3_result_text16be(C,V,N,D)], or +** [sqlite3_result_text16le(C,V,N,D)] is some value other than +** the constants [SQLITE_STATIC] and [SQLITE_TRANSIENT] then +** SQLite will invoke the destructor D with V as its only argument +** when it has finished with the V value. */ void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); void sqlite3_result_double(sqlite3_context*, double); @@ -2715,6 +3888,7 @@ void sqlite3_result_error(sqlite3_context*, const char*, int); void sqlite3_result_error16(sqlite3_context*, const void*, int); void sqlite3_result_error_toobig(sqlite3_context*); void sqlite3_result_error_nomem(sqlite3_context*); +void sqlite3_result_error_code(sqlite3_context*, int); void sqlite3_result_int(sqlite3_context*, int); void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); void sqlite3_result_null(sqlite3_context*); @@ -2728,52 +3902,95 @@ void sqlite3_result_zeroblob(sqlite3_context*, int n); /* ** CAPI3REF: Define New Collating Sequences {F16600} ** -** {F16601} ** These functions are used to add new collation sequences to the ** [sqlite3*] handle specified as the first argument. ** -** {F16602} ** The name of the new collation sequence is specified as a UTF-8 string ** for sqlite3_create_collation() and sqlite3_create_collation_v2() -** and a UTF-16 string for sqlite3_create_collation16(). {F16603} In all cases +** and a UTF-16 string for sqlite3_create_collation16(). In all cases ** the name is passed as the second function argument. ** -** {F16604} ** The third argument may be one of the constants [SQLITE_UTF8], ** [SQLITE_UTF16LE] or [SQLITE_UTF16BE], indicating that the user-supplied ** routine expects to be passed pointers to strings encoded using UTF-8, -** UTF-16 little-endian or UTF-16 big-endian respectively. {F16605} The +** UTF-16 little-endian or UTF-16 big-endian respectively. The ** third argument might also be [SQLITE_UTF16_ALIGNED] to indicate that ** the routine expects pointers to 16-bit word aligned strings ** of UTF16 in the native byte order of the host computer. ** -** {F16607} ** A pointer to the user supplied routine must be passed as the fifth -** argument. {F16609} If it is NULL, this is the same as deleting the collation +** argument. If it is NULL, this is the same as deleting the collation ** sequence (so that SQLite cannot call it anymore). -** {F16611} Each time the application +** Each time the application ** supplied function is invoked, it is passed a copy of the void* passed as ** the fourth argument to sqlite3_create_collation() or ** sqlite3_create_collation16() as its first parameter. ** -** {F16612} ** The remaining arguments to the application-supplied routine are two strings, -** each represented by a [length, data] pair and encoded in the encoding +** each represented by a (length, data) pair and encoded in the encoding ** that was passed as the third argument when the collation sequence was ** registered. {END} The application defined collation routine should ** return negative, zero or positive if ** the first string is less than, equal to, or greater than the second ** string. i.e. (STRING1 - STRING2). ** -** {F16615} ** The sqlite3_create_collation_v2() works like sqlite3_create_collation() ** excapt that it takes an extra argument which is a destructor for -** the collation. {F16617} The destructor is called when the collation is +** the collation. The destructor is called when the collation is ** destroyed and is passed a copy of the fourth parameter void* pointer ** of the sqlite3_create_collation_v2(). -** {F16618} Collations are destroyed when +** Collations are destroyed when ** they are overridden by later calls to the collation creation functions ** or when the [sqlite3*] database handle is closed using [sqlite3_close()]. +** +** INVARIANTS: +** +** {F16603} A successful call to the +** [sqlite3_create_collation_v2(B,X,E,P,F,D)] interface +** registers function F as the comparison function used to +** implement collation X on [database connection] B for +** databases having encoding E. +** +** {F16604} SQLite understands the X parameter to +** [sqlite3_create_collation_v2(B,X,E,P,F,D)] as a zero-terminated +** UTF-8 string in which case is ignored for ASCII characters and +** is significant for non-ASCII characters. +** +** {F16606} Successive calls to [sqlite3_create_collation_v2(B,X,E,P,F,D)] +** with the same values for B, X, and E, override prior values +** of P, F, and D. +** +** {F16609} The destructor D in [sqlite3_create_collation_v2(B,X,E,P,F,D)] +** is not NULL then it is called with argument P when the +** collating function is dropped by SQLite. +** +** {F16612} A collating function is dropped when it is overloaded. +** +** {F16615} A collating function is dropped when the database connection +** is closed using [sqlite3_close()]. +** +** {F16618} The pointer P in [sqlite3_create_collation_v2(B,X,E,P,F,D)] +** is passed through as the first parameter to the comparison +** function F for all subsequent invocations of F. +** +** {F16621} A call to [sqlite3_create_collation(B,X,E,P,F)] is exactly +** the same as a call to [sqlite3_create_collation_v2()] with +** the same parameters and a NULL destructor. +** +** {F16624} Following a [sqlite3_create_collation_v2(B,X,E,P,F,D)], +** SQLite uses the comparison function F for all text comparison +** operations on [database connection] B on text values that +** use the collating sequence name X. +** +** {F16627} The [sqlite3_create_collation16(B,X,E,P,F)] works the same +** as [sqlite3_create_collation(B,X,E,P,F)] except that the +** collation name X is understood as UTF-16 in native byte order +** instead of UTF-8. +** +** {F16630} When multiple comparison functions are available for the same +** collating sequence, SQLite chooses the one whose text encoding +** requires the least amount of conversion from the default +** text encoding of the database. */ int sqlite3_create_collation( sqlite3*, @@ -2801,31 +4018,50 @@ int sqlite3_create_collation16( /* ** CAPI3REF: Collation Needed Callbacks {F16700} ** -** {F16701} ** To avoid having to register all collation sequences before a database ** can be used, a single callback function may be registered with the ** database handle to be called whenever an undefined collation sequence is ** required. ** -** {F16702} ** If the function is registered using the sqlite3_collation_needed() API, ** then it is passed the names of undefined collation sequences as strings ** encoded in UTF-8. {F16703} If sqlite3_collation_needed16() is used, the names -** are passed as UTF-16 in machine native byte order. {F16704} A call to either +** are passed as UTF-16 in machine native byte order. A call to either ** function replaces any existing callback. ** -** {F16705} When the callback is invoked, the first argument passed is a copy +** When the callback is invoked, the first argument passed is a copy ** of the second argument to sqlite3_collation_needed() or -** sqlite3_collation_needed16(). {F16706} The second argument is the database -** handle. {F16707} The third argument is one of [SQLITE_UTF8], +** sqlite3_collation_needed16(). The second argument is the database +** handle. The third argument is one of [SQLITE_UTF8], ** [SQLITE_UTF16BE], or [SQLITE_UTF16LE], indicating the most ** desirable form of the collation sequence function required. -** {F16708} The fourth parameter is the name of the -** required collation sequence. {END} +** The fourth parameter is the name of the +** required collation sequence. ** ** The callback function should register the desired collation using ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or ** [sqlite3_create_collation_v2()]. +** +** INVARIANTS: +** +** {F16702} A successful call to [sqlite3_collation_needed(D,P,F)] +** or [sqlite3_collation_needed16(D,P,F)] causes +** the [database connection] D to invoke callback F with first +** parameter P whenever it needs a comparison function for a +** collating sequence that it does not know about. +** +** {F16704} Each successful call to [sqlite3_collation_needed()] or +** [sqlite3_collation_needed16()] overrides the callback registered +** on the same [database connection] by prior calls to either +** interface. +** +** {F16706} The name of the requested collating function passed in the +** 4th parameter to the callback is in UTF-8 if the callback +** was registered using [sqlite3_collation_needed()] and +** is in UTF-16 native byte order if the callback was +** registered using [sqlite3_collation_needed16()]. +** +** */ int sqlite3_collation_needed( sqlite3*, @@ -2866,17 +4102,28 @@ int sqlite3_rekey( /* ** CAPI3REF: Suspend Execution For A Short Time {F10530} ** -** {F10531} The sqlite3_sleep() function +** The sqlite3_sleep() function ** causes the current thread to suspend execution ** for at least a number of milliseconds specified in its parameter. ** -** {F10532} If the operating system does not support sleep requests with +** If the operating system does not support sleep requests with ** millisecond time resolution, then the time will be rounded up to -** the nearest second. {F10533} The number of milliseconds of sleep actually +** the nearest second. The number of milliseconds of sleep actually ** requested from the operating system is returned. ** -** {F10534} SQLite implements this interface by calling the xSleep() -** method of the default [sqlite3_vfs] object. {END} +** SQLite implements this interface by calling the xSleep() +** method of the default [sqlite3_vfs] object. +** +** INVARIANTS: +** +** {F10533} The [sqlite3_sleep(M)] interface invokes the xSleep +** method of the default [sqlite3_vfs|VFS] in order to +** suspend execution of the current thread for at least +** M milliseconds. +** +** {F10536} The [sqlite3_sleep(M)] interface returns the number of +** milliseconds of sleep actually requested of the operating +** system, which might be larger than the parameter M. */ int sqlite3_sleep(int); @@ -2899,35 +4146,57 @@ SQLITE_EXTERN char *sqlite3_temp_directory; /* ** CAPI3REF: Test To See If The Database Is In Auto-Commit Mode {F12930} ** -** {F12931} The sqlite3_get_autocommit() interfaces returns non-zero or +** The sqlite3_get_autocommit() interfaces returns non-zero or ** zero if the given database connection is or is not in autocommit mode, -** respectively. {F12932} Autocommit mode is on -** by default. {F12933} Autocommit mode is disabled by a BEGIN statement. -** {F12934} Autocommit mode is reenabled by a COMMIT or ROLLBACK. {END} +** respectively. Autocommit mode is on +** by default. Autocommit mode is disabled by a [BEGIN] statement. +** Autocommit mode is reenabled by a [COMMIT] or [ROLLBACK]. ** ** If certain kinds of errors occur on a statement within a multi-statement ** transactions (errors including [SQLITE_FULL], [SQLITE_IOERR], ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the -** transaction might be rolled back automatically. {F12935} The only way to +** transaction might be rolled back automatically. The only way to ** find out if SQLite automatically rolled back the transaction after -** an error is to use this function. {END} +** an error is to use this function. +** +** INVARIANTS: +** +** {F12931} The [sqlite3_get_autocommit(D)] interface returns non-zero or +** zero if the [database connection] D is or is not in autocommit +** mode, respectively. ** +** {F12932} Autocommit mode is on by default. +** +** {F12933} Autocommit mode is disabled by a successful [BEGIN] statement. +** +** {F12934} Autocommit mode is enabled by a successful [COMMIT] or [ROLLBACK] +** statement. +** +** +** LIMITATIONS: +*** ** {U12936} If another thread changes the autocommit status of the database -** connection while this routine is running, then the return value -** is undefined. {END} +** connection while this routine is running, then the return value +** is undefined. */ int sqlite3_get_autocommit(sqlite3*); /* ** CAPI3REF: Find The Database Handle Of A Prepared Statement {F13120} ** -** {F13121} The sqlite3_db_handle interface +** The sqlite3_db_handle interface ** returns the [sqlite3*] database handle to which a -** [sqlite3_stmt | prepared statement] belongs. -** {F13122} the database handle returned by sqlite3_db_handle +** [prepared statement] belongs. +** The database handle returned by sqlite3_db_handle ** is the same database handle that was ** the first argument to the [sqlite3_prepare_v2()] or its variants ** that was used to create the statement in the first place. +** +** INVARIANTS: +** +** {F13123} The [sqlite3_db_handle(S)] interface returns a pointer +** to the [database connection] associated with +** [prepared statement] S. */ sqlite3 *sqlite3_db_handle(sqlite3_stmt*); @@ -2935,33 +4204,70 @@ sqlite3 *sqlite3_db_handle(sqlite3_stmt*); /* ** CAPI3REF: Commit And Rollback Notification Callbacks {F12950} ** -** {F12951} The sqlite3_commit_hook() interface registers a callback +** The sqlite3_commit_hook() interface registers a callback ** function to be invoked whenever a transaction is committed. -** {F12952} Any callback set by a previous call to sqlite3_commit_hook() +** Any callback set by a previous call to sqlite3_commit_hook() ** for the same database connection is overridden. -** {F12953} The sqlite3_rollback_hook() interface registers a callback +** The sqlite3_rollback_hook() interface registers a callback ** function to be invoked whenever a transaction is committed. -** {F12954} Any callback set by a previous call to sqlite3_commit_hook() +** Any callback set by a previous call to sqlite3_commit_hook() ** for the same database connection is overridden. -** {F12956} The pArg argument is passed through -** to the callback. {F12957} If the callback on a commit hook function +** The pArg argument is passed through +** to the callback. If the callback on a commit hook function ** returns non-zero, then the commit is converted into a rollback. ** -** {F12958} If another function was previously registered, its +** If another function was previously registered, its ** pArg value is returned. Otherwise NULL is returned. ** -** {F12959} Registering a NULL function disables the callback. +** Registering a NULL function disables the callback. ** -** {F12961} For the purposes of this API, a transaction is said to have been +** For the purposes of this API, a transaction is said to have been ** rolled back if an explicit "ROLLBACK" statement is executed, or ** an error or constraint causes an implicit rollback to occur. -** {F12962} The rollback callback is not invoked if a transaction is +** The rollback callback is not invoked if a transaction is ** automatically rolled back because the database connection is closed. -** {F12964} The rollback callback is not invoked if a transaction is +** The rollback callback is not invoked if a transaction is ** rolled back because a commit callback returned non-zero. -** Check on this {END} +** Check on this ** ** These are experimental interfaces and are subject to change. +** +** INVARIANTS: +** +** {F12951} The [sqlite3_commit_hook(D,F,P)] interface registers the +** callback function F to be invoked with argument P whenever +** a transaction commits on [database connection] D. +** +** {F12952} The [sqlite3_commit_hook(D,F,P)] interface returns the P +** argument from the previous call with the same +** [database connection ] D , or NULL on the first call +** for a particular [database connection] D. +** +** {F12953} Each call to [sqlite3_commit_hook()] overwrites the callback +** registered by prior calls. +** +** {F12954} If the F argument to [sqlite3_commit_hook(D,F,P)] is NULL +** then the commit hook callback is cancelled and no callback +** is invoked when a transaction commits. +** +** {F12955} If the commit callback returns non-zero then the commit is +** converted into a rollback. +** +** {F12961} The [sqlite3_rollback_hook(D,F,P)] interface registers the +** callback function F to be invoked with argument P whenever +** a transaction rolls back on [database connection] D. +** +** {F12962} The [sqlite3_rollback_hook(D,F,P)] interface returns the P +** argument from the previous call with the same +** [database connection ] D , or NULL on the first call +** for a particular [database connection] D. +** +** {F12963} Each call to [sqlite3_rollback_hook()] overwrites the callback +** registered by prior calls. +** +** {F12964} If the F argument to [sqlite3_rollback_hook(D,F,P)] is NULL +** then the rollback hook callback is cancelled and no callback +** is invoked when a transaction rolls back. */ void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); @@ -2969,32 +4275,63 @@ void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); /* ** CAPI3REF: Data Change Notification Callbacks {F12970} ** -** {F12971} The sqlite3_update_hook() interface +** The sqlite3_update_hook() interface ** registers a callback function with the database connection identified by the ** first argument to be invoked whenever a row is updated, inserted or deleted. -** {F12972} Any callback set by a previous call to this function for the same +** Any callback set by a previous call to this function for the same ** database connection is overridden. ** -** {F12974} The second argument is a pointer to the function to invoke when a +** The second argument is a pointer to the function to invoke when a ** row is updated, inserted or deleted. -** {F12976} The first argument to the callback is +** The first argument to the callback is ** a copy of the third argument to sqlite3_update_hook(). -** {F12977} The second callback +** The second callback ** argument is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], ** depending on the operation that caused the callback to be invoked. -** {F12978} The third and +** The third and ** fourth arguments to the callback contain pointers to the database and ** table name containing the affected row. -** {F12979} The final callback parameter is +** The final callback parameter is ** the rowid of the row. -** {F12981} In the case of an update, this is the rowid after +** In the case of an update, this is the rowid after ** the update takes place. ** -** {F12983} The update hook is not invoked when internal system tables are +** The update hook is not invoked when internal system tables are ** modified (i.e. sqlite_master and sqlite_sequence). ** -** {F12984} If another function was previously registered, its pArg value -** is returned. {F12985} Otherwise NULL is returned. +** If another function was previously registered, its pArg value +** is returned. Otherwise NULL is returned. +** +** INVARIANTS: +** +** {F12971} The [sqlite3_update_hook(D,F,P)] interface causes callback +** function F to be invoked with first parameter P whenever +** a table row is modified, inserted, or deleted on +** [database connection] D. +** +** {F12973} The [sqlite3_update_hook(D,F,P)] interface returns the value +** of P for the previous call on the same [database connection] D, +** or NULL for the first call. +** +** {F12975} If the update hook callback F in [sqlite3_update_hook(D,F,P)] +** is NULL then the no update callbacks are made. +** +** {F12977} Each call to [sqlite3_update_hook(D,F,P)] overrides prior calls +** to the same interface on the same [database connection] D. +** +** {F12979} The update hook callback is not invoked when internal system +** tables such as sqlite_master and sqlite_sequence are modified. +** +** {F12981} The second parameter to the update callback +** is one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], +** depending on the operation that caused the callback to be invoked. +** +** {F12983} The third and fourth arguments to the callback contain pointers +** to zero-terminated UTF-8 strings which are the names of the +** database and table that is being updated. + +** {F12985} The final callback parameter is the rowid of the row after +** the change occurs. */ void *sqlite3_update_hook( sqlite3*, @@ -3005,83 +4342,131 @@ void *sqlite3_update_hook( /* ** CAPI3REF: Enable Or Disable Shared Pager Cache {F10330} ** -** {F10331} ** This routine enables or disables the sharing of the database cache ** and schema data structures between connections to the same database. -** {F10332} ** Sharing is enabled if the argument is true and disabled if the argument ** is false. ** -** {F10333} Cache sharing is enabled and disabled +** Cache sharing is enabled and disabled ** for an entire process. {END} This is a change as of SQLite version 3.5.0. ** In prior versions of SQLite, sharing was ** enabled or disabled for each thread separately. ** -** {F10334} ** The cache sharing mode set by this interface effects all subsequent ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. -** {F10335} Existing database connections continue use the sharing mode -** that was in effect at the time they were opened. {END} +** Existing database connections continue use the sharing mode +** that was in effect at the time they were opened. ** -** Virtual tables cannot be used with a shared cache. {F10336} When shared +** Virtual tables cannot be used with a shared cache. When shared ** cache is enabled, the [sqlite3_create_module()] API used to register -** virtual tables will always return an error. {END} +** virtual tables will always return an error. ** -** {F10337} This routine returns [SQLITE_OK] if shared cache was -** enabled or disabled successfully. {F10338} An [SQLITE_ERROR | error code] -** is returned otherwise. {END} +** This routine returns [SQLITE_OK] if shared cache was +** enabled or disabled successfully. An [error code] +** is returned otherwise. ** -** {F10339} Shared cache is disabled by default. {END} But this might change in +** Shared cache is disabled by default. But this might change in ** future releases of SQLite. Applications that care about shared ** cache setting should set it explicitly. +** +** INVARIANTS: +** +** {F10331} A successful invocation of [sqlite3_enable_shared_cache(B)] +** will enable or disable shared cache mode for any subsequently +** created [database connection] in the same process. +** +** {F10336} When shared cache is enabled, the [sqlite3_create_module()] +** interface will always return an error. +** +** {F10337} The [sqlite3_enable_shared_cache(B)] interface returns +** [SQLITE_OK] if shared cache was enabled or disabled successfully. +** +** {F10339} Shared cache is disabled by default. */ int sqlite3_enable_shared_cache(int); /* ** CAPI3REF: Attempt To Free Heap Memory {F17340} ** -** {F17341} The sqlite3_release_memory() interface attempts to +** The sqlite3_release_memory() interface attempts to ** free N bytes of heap memory by deallocating non-essential memory ** allocations held by the database labrary. {END} Memory used ** to cache database pages to improve performance is an example of -** non-essential memory. {F16342} sqlite3_release_memory() returns +** non-essential memory. Sqlite3_release_memory() returns ** the number of bytes actually freed, which might be more or less ** than the amount requested. +** +** INVARIANTS: +** +** {F17341} The [sqlite3_release_memory(N)] interface attempts to +** free N bytes of heap memory by deallocating non-essential +** memory allocations held by the database labrary. +** +** {F16342} The [sqlite3_release_memory(N)] returns the number +** of bytes actually freed, which might be more or less +** than the amount requested. */ int sqlite3_release_memory(int); /* ** CAPI3REF: Impose A Limit On Heap Size {F17350} ** -** {F16351} The sqlite3_soft_heap_limit() interface +** The sqlite3_soft_heap_limit() interface ** places a "soft" limit on the amount of heap memory that may be allocated -** by SQLite. {F16352} If an internal allocation is requested +** by SQLite. If an internal allocation is requested ** that would exceed the soft heap limit, [sqlite3_release_memory()] is ** invoked one or more times to free up some space before the allocation -** is made. {END} +** is made. ** -** {F16353} The limit is called "soft", because if +** The limit is called "soft", because if ** [sqlite3_release_memory()] cannot ** free sufficient memory to prevent the limit from being exceeded, ** the memory is allocated anyway and the current operation proceeds. ** -** {F16354} ** A negative or zero value for N means that there is no soft heap limit and ** [sqlite3_release_memory()] will only be called when memory is exhausted. -** {F16355} The default value for the soft heap limit is zero. +** The default value for the soft heap limit is zero. ** ** SQLite makes a best effort to honor the soft heap limit. -** {F16356} But if the soft heap limit cannot honored, execution will -** continue without error or notification. {END} This is why the limit is +** But if the soft heap limit cannot honored, execution will +** continue without error or notification. This is why the limit is ** called a "soft" limit. It is advisory only. ** ** Prior to SQLite version 3.5.0, this routine only constrained the memory ** allocated by a single thread - the same thread in which this routine ** runs. Beginning with SQLite version 3.5.0, the soft heap limit is -** applied to all threads. {F16357} The value specified for the soft heap limit -** is an upper bound on the total memory allocation for all threads. {END} In +** applied to all threads. The value specified for the soft heap limit +** is an upper bound on the total memory allocation for all threads. In ** version 3.5.0 there is no mechanism for limiting the heap usage for ** individual threads. +** +** INVARIANTS: +** +** {F16351} The [sqlite3_soft_heap_limit(N)] interface places a soft limit +** of N bytes on the amount of heap memory that may be allocated +** using [sqlite3_malloc()] or [sqlite3_realloc()] at any point +** in time. +** +** {F16352} If a call to [sqlite3_malloc()] or [sqlite3_realloc()] would +** cause the total amount of allocated memory to exceed the +** soft heap limit, then [sqlite3_release_memory()] is invoked +** in an attempt to reduce the memory usage prior to proceeding +** with the memory allocation attempt. +** +** {F16353} Calls to [sqlite3_malloc()] or [sqlite3_realloc()] that trigger +** attempts to reduce memory usage through the soft heap limit +** mechanism continue even if the attempt to reduce memory +** usage is unsuccessful. +** +** {F16354} A negative or zero value for N in a call to +** [sqlite3_soft_heap_limit(N)] means that there is no soft +** heap limit and [sqlite3_release_memory()] will only be +** called when memory is completely exhausted. +** +** {F16355} The default value for the soft heap limit is zero. +** +** {F16358} Each call to [sqlite3_soft_heap_limit(N)] overrides the +** values set by all prior calls. */ void sqlite3_soft_heap_limit(int); @@ -3240,7 +4625,7 @@ int sqlite3_auto_extension(void *xEntryPoint); ** ** {F12661} This function disables all previously registered ** automatic extensions. {END} This -** routine undoes the effect of all prior [sqlite3_automatic_extension()] +** routine undoes the effect of all prior [sqlite3_auto_extension()] ** calls. ** ** {F12662} This call disabled automatic extensions in all threads. {END} @@ -3271,6 +4656,9 @@ typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; typedef struct sqlite3_module sqlite3_module; /* +** CAPI3REF: Virtual Table Object {F18000} +** KEYWORDS: sqlite3_module +** ** A module is a class of virtual tables. Each module is defined ** by an instance of the following structure. This structure consists ** mostly of methods for the module. @@ -3307,6 +4695,9 @@ struct sqlite3_module { }; /* +** CAPI3REF: Virtual Table Indexing Information {F18100} +** KEYWORDS: sqlite3_index_info +** ** The sqlite3_index_info structure and its substructures is used to ** pass information into and receive the reply from the xBestIndex ** method of an sqlite3_module. The fields under **Inputs** are the @@ -3387,6 +4778,8 @@ struct sqlite3_index_info { #define SQLITE_INDEX_CONSTRAINT_MATCH 64 /* +** CAPI3REF: Register A Virtual Table Implementation {F18200} +** ** This routine is used to register a new module name with an SQLite ** connection. Module names must be registered before creating new ** virtual tables on the module, or before using preexisting virtual @@ -3400,6 +4793,8 @@ int sqlite3_create_module( ); /* +** CAPI3REF: Register A Virtual Table Implementation {F18210} +** ** This routine is identical to the sqlite3_create_module() method above, ** except that it allows a destructor function to be specified. It is ** even more experimental than the rest of the virtual tables API. @@ -3413,6 +4808,9 @@ int sqlite3_create_module_v2( ); /* +** CAPI3REF: Virtual Table Instance Object {F18010} +** KEYWORDS: sqlite3_vtab +** ** Every module implementation uses a subclass of the following structure ** to describe a particular instance of the module. Each subclass will ** be tailored to the specific needs of the module implementation. The @@ -3436,7 +4834,11 @@ struct sqlite3_vtab { /* Virtual table implementations will typically add additional fields */ }; -/* Every module implementation uses a subclass of the following structure +/* +** CAPI3REF: Virtual Table Cursor Object {F18020} +** KEYWORDS: sqlite3_vtab_cursor +** +** Every module implementation uses a subclass of the following structure ** to describe cursors that point into the virtual table and are used ** to loop through the virtual table. Cursors are created using the ** xOpen method of the module. Each module implementation will define @@ -3451,6 +4853,8 @@ struct sqlite3_vtab_cursor { }; /* +** CAPI3REF: Declare The Schema Of A Virtual Table {F18280} +** ** The xCreate and xConnect methods of a module use the following API ** to declare the format (the names and datatypes of the columns) of ** the virtual tables they implement. @@ -3458,6 +4862,8 @@ struct sqlite3_vtab_cursor { int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); /* +** CAPI3REF: Overload A Function For A Virtual Table {F18300} +** ** Virtual tables can provide alternative implementations of functions ** using the xFindFunction method. But global versions of those functions ** must exist in order to be overloaded. @@ -3490,8 +4896,9 @@ int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); /* ** CAPI3REF: A Handle To An Open BLOB {F17800} ** -** An instance of the following opaque structure is used to -** represent an blob-handle. A blob-handle is created by +** An instance of this object represents an open BLOB on which +** incremental I/O can be preformed. +** Objects of this type are created by ** [sqlite3_blob_open()] and destroyed by [sqlite3_blob_close()]. ** The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces ** can be used to read or write small subsections of the blob. @@ -3503,7 +4910,7 @@ typedef struct sqlite3_blob sqlite3_blob; /* ** CAPI3REF: Open A BLOB For Incremental I/O {F17810} ** -** {F17811} This interfaces opens a handle to the blob located +** This interfaces opens a handle to the blob located ** in row iRow,, column zColumn, table zTable in database zDb; ** in other words, the same blob that would be selected by: ** @@ -3511,18 +4918,38 @@ typedef struct sqlite3_blob sqlite3_blob; ** SELECT zColumn FROM zDb.zTable WHERE rowid = iRow; ** {END} ** -** {F17812} If the flags parameter is non-zero, the blob is opened for +** If the flags parameter is non-zero, the blob is opened for ** read and write access. If it is zero, the blob is opened for read -** access. {END} +** access. ** -** {F17813} On success, [SQLITE_OK] is returned and the new +** On success, [SQLITE_OK] is returned and the new ** [sqlite3_blob | blob handle] is written to *ppBlob. -** {F17814} Otherwise an error code is returned and +** Otherwise an error code is returned and ** any value written to *ppBlob should not be used by the caller. -** {F17815} This function sets the database-handle error code and message +** This function sets the database-handle error code and message ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()]. -** We should go through and mark all interfaces that behave this -** way with a similar statement +** +** INVARIANTS: +** +** {F17813} A successful invocation of the [sqlite3_blob_open(D,B,T,C,R,F,P)] +** interface opens an [sqlite3_blob] object P on the blob +** in column C of table T in database B on [database connection] D. +** +** {F17814} A successful invocation of [sqlite3_blob_open(D,...)] starts +** a new transaction on [database connection] D if that connection +** is not already in a transaction. +** +** {F17816} The [sqlite3_blob_open(D,B,T,C,R,F,P)] interface opens the blob +** for read and write access if and only if the F parameter +** is non-zero. +** +** {F17819} The [sqlite3_blob_open()] interface returns [SQLITE_OK] on +** success and an appropriate [error code] on failure. +** +** {F17821} If an error occurs during evaluation of [sqlite3_blob_open(D,...)] +** then subsequent calls to [sqlite3_errcode(D)], +** [sqlite3_errmsg(D)], and [sqlite3_errmsg16(D)] will return +** information approprate for that error. */ int sqlite3_blob_open( sqlite3*, @@ -3539,26 +4966,50 @@ int sqlite3_blob_open( ** ** Close an open [sqlite3_blob | blob handle]. ** -** {F17831} Closing a BLOB shall cause the current transaction to commit +** Closing a BLOB shall cause the current transaction to commit ** if there are no other BLOBs, no pending prepared statements, and the ** database connection is in autocommit mode. -** {F17832} If any writes were made to the BLOB, they might be held in cache +** If any writes were made to the BLOB, they might be held in cache ** until the close operation if they will fit. {END} ** Closing the BLOB often forces the changes ** out to disk and so if any I/O errors occur, they will likely occur ** at the time when the BLOB is closed. {F17833} Any errors that occur during ** closing are reported as a non-zero return value. ** -** {F17839} The BLOB is closed unconditionally. Even if this routine returns +** The BLOB is closed unconditionally. Even if this routine returns ** an error code, the BLOB is still closed. +** +** INVARIANTS: +** +** {F17833} The [sqlite3_blob_close(P)] interface closes an +** [sqlite3_blob] object P previously opened using +** [sqlite3_blob_open()]. +** +** {F17836} Closing an [sqlite3_blob] object using +** [sqlite3_blob_close()] shall cause the current transaction to +** commit if there are no other open [sqlite3_blob] objects +** or [prepared statements] on the same [database connection] and +** the [database connection] is in +** [sqlite3_get_autocommit | autocommit mode]. +** +** {F17839} The [sqlite3_blob_close(P)] interfaces closes the +** [sqlite3_blob] object P unconditionally, even if +** [sqlite3_blob_close(P)] returns something other than [SQLITE_OK]. +** */ int sqlite3_blob_close(sqlite3_blob *); /* -** CAPI3REF: Return The Size Of An Open BLOB {F17805} +** CAPI3REF: Return The Size Of An Open BLOB {F17840} +** +** Return the size in bytes of the blob accessible via the open +** [sqlite3_blob] object in its only argument. ** -** {F16806} Return the size in bytes of the blob accessible via the open -** [sqlite3_blob | blob-handle] passed as an argument. +** INVARIANTS: +** +** {F17843} The [sqlite3_blob_bytes(P)] interface returns the size +** in bytes of the BLOB that the [sqlite3_blob] object P +** refers to. */ int sqlite3_blob_bytes(sqlite3_blob *); @@ -3567,40 +5018,96 @@ int sqlite3_blob_bytes(sqlite3_blob *); ** ** This function is used to read data from an open ** [sqlite3_blob | blob-handle] into a caller supplied buffer. -** {F17851} n bytes of data are copied into buffer -** z from the open blob, starting at offset iOffset. +** N bytes of data are copied into buffer +** Z from the open blob, starting at offset iOffset. ** -** {F17852} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is read. {F17853} If n is +** If offset iOffset is less than N bytes from the end of the blob, +** [SQLITE_ERROR] is returned and no data is read. If N or iOffset is ** less than zero [SQLITE_ERROR] is returned and no data is read. ** -** {F17854} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** On success, SQLITE_OK is returned. Otherwise, an +** [error code] or an [extended error code] is returned. +** +** INVARIANTS: +** +** {F17853} The [sqlite3_blob_read(P,Z,N,X)] interface reads N bytes +** beginning at offset X from +** the blob that [sqlite3_blob] object P refers to +** and writes those N bytes into buffer Z. +** +** {F17856} In [sqlite3_blob_read(P,Z,N,X)] if the size of the blob +** is less than N+X bytes, then the function returns [SQLITE_ERROR] +** and nothing is read from the blob. +** +** {F17859} In [sqlite3_blob_read(P,Z,N,X)] if X or N is less than zero +** then the function returns [SQLITE_ERROR] +** and nothing is read from the blob. +** +** {F17862} The [sqlite3_blob_read(P,Z,N,X)] interface returns [SQLITE_OK] +** if N bytes where successfully read into buffer Z. +** +** {F17865} If the requested read could not be completed, +** the [sqlite3_blob_read(P,Z,N,X)] interface returns an +** appropriate [error code] or [extended error code]. +** +** {F17868} If an error occurs during evaluation of [sqlite3_blob_read(D,...)] +** then subsequent calls to [sqlite3_errcode(D)], +** [sqlite3_errmsg(D)], and [sqlite3_errmsg16(D)] will return +** information approprate for that error. */ -int sqlite3_blob_read(sqlite3_blob *, void *z, int n, int iOffset); +int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); /* ** CAPI3REF: Write Data Into A BLOB Incrementally {F17870} ** ** This function is used to write data into an open ** [sqlite3_blob | blob-handle] from a user supplied buffer. -** {F17871} n bytes of data are copied from the buffer +** n bytes of data are copied from the buffer ** pointed to by z into the open blob, starting at offset iOffset. ** -** {F17872} If the [sqlite3_blob | blob-handle] passed as the first argument +** If the [sqlite3_blob | blob-handle] passed as the first argument ** was not opened for writing (the flags parameter to [sqlite3_blob_open()] *** was zero), this function returns [SQLITE_READONLY]. ** -** {F17873} This function may only modify the contents of the blob; it is +** This function may only modify the contents of the blob; it is ** not possible to increase the size of a blob using this API. -** {F17874} If offset iOffset is less than n bytes from the end of the blob, -** [SQLITE_ERROR] is returned and no data is written. {F17875} If n is +** If offset iOffset is less than n bytes from the end of the blob, +** [SQLITE_ERROR] is returned and no data is written. If n is ** less than zero [SQLITE_ERROR] is returned and no data is written. ** -** {F17876} On success, SQLITE_OK is returned. Otherwise, an -** [SQLITE_ERROR | SQLite error code] or an -** [SQLITE_IOERR_READ | extended error code] is returned. +** On success, SQLITE_OK is returned. Otherwise, an +** [error code] or an [extended error code] is returned. +** +** INVARIANTS: +** +** {F17873} The [sqlite3_blob_write(P,Z,N,X)] interface writes N bytes +** from buffer Z into +** the blob that [sqlite3_blob] object P refers to +** beginning at an offset of X into the blob. +** +** {F17875} The [sqlite3_blob_write(P,Z,N,X)] interface returns +** [SQLITE_READONLY] if the [sqlite3_blob] object P was +** [sqlite3_blob_open | opened] for reading only. +** +** {F17876} In [sqlite3_blob_write(P,Z,N,X)] if the size of the blob +** is less than N+X bytes, then the function returns [SQLITE_ERROR] +** and nothing is written into the blob. +** +** {F17879} In [sqlite3_blob_write(P,Z,N,X)] if X or N is less than zero +** then the function returns [SQLITE_ERROR] +** and nothing is written into the blob. +** +** {F17882} The [sqlite3_blob_write(P,Z,N,X)] interface returns [SQLITE_OK] +** if N bytes where successfully written into blob. +** +** {F17885} If the requested write could not be completed, +** the [sqlite3_blob_write(P,Z,N,X)] interface returns an +** appropriate [error code] or [extended error code]. +** +** {F17888} If an error occurs during evaluation of [sqlite3_blob_write(D,...)] +** then subsequent calls to [sqlite3_errcode(D)], +** [sqlite3_errmsg(D)], and [sqlite3_errmsg16(D)] will return +** information approprate for that error. */ int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); @@ -3609,30 +5116,57 @@ int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); ** ** A virtual filesystem (VFS) is an [sqlite3_vfs] object ** that SQLite uses to interact -** with the underlying operating system. Most builds come with a +** with the underlying operating system. Most SQLite builds come with a ** single default VFS that is appropriate for the host computer. ** New VFSes can be registered and existing VFSes can be unregistered. ** The following interfaces are provided. ** -** {F11201} The sqlite3_vfs_find() interface returns a pointer to -** a VFS given its name. {F11202} Names are case sensitive. -** {F11203} Names are zero-terminated UTF-8 strings. -** {F11204} If there is no match, a NULL -** pointer is returned. {F11205} If zVfsName is NULL then the default -** VFS is returned. {END} -** -** {F11210} New VFSes are registered with sqlite3_vfs_register(). -** {F11211} Each new VFS becomes the default VFS if the makeDflt flag is set. -** {F11212} The same VFS can be registered multiple times without injury. -** {F11213} To make an existing VFS into the default VFS, register it again -** with the makeDflt flag set. {U11214} If two different VFSes with the -** same name are registered, the behavior is undefined. {U11215} If a +** The sqlite3_vfs_find() interface returns a pointer to +** a VFS given its name. Names are case sensitive. +** Names are zero-terminated UTF-8 strings. +** If there is no match, a NULL +** pointer is returned. If zVfsName is NULL then the default +** VFS is returned. +** +** New VFSes are registered with sqlite3_vfs_register(). +** Each new VFS becomes the default VFS if the makeDflt flag is set. +** The same VFS can be registered multiple times without injury. +** To make an existing VFS into the default VFS, register it again +** with the makeDflt flag set. If two different VFSes with the +** same name are registered, the behavior is undefined. If a ** VFS is registered with a name that is NULL or an empty string, ** then the behavior is undefined. ** -** {F11220} Unregister a VFS with the sqlite3_vfs_unregister() interface. -** {F11221} If the default VFS is unregistered, another VFS is chosen as +** Unregister a VFS with the sqlite3_vfs_unregister() interface. +** If the default VFS is unregistered, another VFS is chosen as ** the default. The choice for the new VFS is arbitrary. +** +** INVARIANTS: +** +** {F11203} The [sqlite3_vfs_find(N)] interface returns a pointer to the +** registered [sqlite3_vfs] object whose name exactly matches +** the zero-terminated UTF-8 string N, or it returns NULL if +** there is no match. +** +** {F11206} If the N parameter to [sqlite3_vfs_find(N)] is NULL then +** the function returns a pointer to the default [sqlite3_vfs] +** object if there is one, or NULL if there is no default +** [sqlite3_vfs] object. +** +** {F11209} The [sqlite3_vfs_register(P,F)] interface registers the +** well-formed [sqlite3_vfs] object P using the name given +** by the zName field of the object. +** +** {F11212} Using the [sqlite3_vfs_register(P,F)] interface to register +** the same [sqlite3_vfs] object multiple times is a harmless no-op. +** +** {F11215} The [sqlite3_vfs_register(P,F)] interface makes the +** the [sqlite3_vfs] object P the default [sqlite3_vfs] object +** if F is non-zero. +** +** {F11218} The [sqlite3_vfs_unregister(P)] interface unregisters the +** [sqlite3_vfs] object P so that it is no longer returned by +** subsequent calls to [sqlite3_vfs_find()]. */ sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); @@ -3827,6 +5361,45 @@ int sqlite3_mutex_notheld(sqlite3_mutex*); int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); /* +** CAPI3REF: Testing Interface {F11400} +** +** The sqlite3_test_control() interface is used to read out internal +** state of SQLite and to inject faults into SQLite for testing +** purposes. The first parameter a operation code that determines +** the number, meaning, and operation of all subsequent parameters. +** +** This interface is not for use by applications. It exists solely +** for verifying the correct operation of the SQLite library. Depending +** on how the SQLite library is compiled, this interface might not exist. +** +** The details of the operation codes, their meanings, the parameters +** they take, and what they do are all subject to change without notice. +** Unlike most of the SQLite API, this function is not guaranteed to +** operate consistently from one release to the next. +*/ +int sqlite3_test_control(int op, ...); + +/* +** CAPI3REF: Testing Interface Operation Codes {F11410} +** +** These constants are the valid operation code parameters used +** as the first argument to [sqlite3_test_control()]. +** +** These parameters and their meansing are subject to change +** without notice. These values are for testing purposes only. +** Applications should not use any of these parameters or the +** [sqlite3_test_control()] interface. +*/ +#define SQLITE_TESTCTRL_FAULT_CONFIG 1 +#define SQLITE_TESTCTRL_FAULT_FAILURES 2 +#define SQLITE_TESTCTRL_FAULT_BENIGN_FAILURES 3 +#define SQLITE_TESTCTRL_FAULT_PENDING 4 + + + + + +/* ** Undo the hack that converts floating point types to integer for ** builds on processors without floating point support. */ diff --git a/libs/sqlite3/sqlite3ext.h b/libs/sqlite3/sqlite3ext.h index 5d4c2de..20c32b6 100644 --- a/libs/sqlite3/sqlite3ext.h +++ b/libs/sqlite3/sqlite3ext.h @@ -15,7 +15,7 @@ ** as extensions by SQLite should #include this file instead of ** sqlite3.h. ** -** @(#) $Id: sqlite3ext.h,v 1.17 2007/08/31 16:11:36 drh Exp $ +** @(#) $Id: sqlite3ext.h,v 1.18 2008/03/02 03:32:05 mlcreech Exp $ */ #ifndef _SQLITE3EXT_H_ #define _SQLITE3EXT_H_ @@ -24,13 +24,13 @@ typedef struct sqlite3_api_routines sqlite3_api_routines; /* -** The following structure hold pointers to all of the SQLite API +** The following structure holds pointers to all of the SQLite API ** routines. ** ** WARNING: In order to maintain backwards compatibility, add new ** interfaces to the end of this structure only. If you insert new ** interfaces in the middle of this structure, then older different -** versions of SQLite will not be able to load each others shared +** versions of SQLite will not be able to load each others' shared ** libraries! */ struct sqlite3_api_routines { diff --git a/readme.txt b/readme.txt index 26b2dab..1c0fe8a 100644 --- a/readme.txt +++ b/readme.txt @@ -31,14 +31,14 @@ Windows, -i will install FMS as a service, and -u will uninstall the service. EXITING ------- -To exist FMS running in console mode, press CTRL+C while at the console. You +To exit FMS running in console mode, press CTRL+C while at the console. You can also use the shutdown button on the web interface to close FMS. As a last resort, you may kill the process. WEB INTERFACE ------------- By default, a web interface for administration will be running at http:// -localhost:8888. You can use the interface to configure and administer FMS. +localhost:8080. You can use the interface to configure and administer FMS. NNTP CONFIGURATION ------------------ diff --git a/src/freenet/boardlistrequester.cpp b/src/freenet/boardlistrequester.cpp index 28ba901..5029460 100644 --- a/src/freenet/boardlistrequester.cpp +++ b/src/freenet/boardlistrequester.cpp @@ -71,6 +71,7 @@ const bool BoardListRequester::HandleAllData(FCPMessage &message) if(description=="" && xml.GetDescription(i)!="") { upd.Bind(0,xml.GetDescription(i)); + upd.Bind(1,boardid); upd.Step(); upd.Reset(); } diff --git a/src/global.cpp b/src/global.cpp index 2dbfcdf..0698703 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -519,6 +519,12 @@ void SetupDefaultOptions() st.Step(); st.Reset(); + st.Bind(0,"HTTPAccessControl"); + st.Bind(1,"-0.0.0.0/0,+127.0.0.1"); + st.Bind(2,"Comma separated list of addresses and/or subnet masks that are allowed access to the administration pages. Default is localhost only. + allows a host, - denies as host."); + st.Step(); + st.Reset(); + // StartFreenetUpdater st.Bind(0,"StartFreenetUpdater"); st.Bind(1,"true"); diff --git a/src/http/httpthread.cpp b/src/http/httpthread.cpp index a58044c..22fc930 100644 --- a/src/http/httpthread.cpp +++ b/src/http/httpthread.cpp @@ -29,6 +29,9 @@ HTTPThread::HTTPThread() Option::Instance()->Get("HTTPListenPort",portstr); StringFunctions::Convert(portstr,port); + std::string aclstr; + Option::Instance()->Get("HTTPAccessControl",aclstr); + // set template templatestr="Home

      Could not find template.htm! Place in program directory and restart!


      [CONTENT]"; FILE *infile=fopen("template.htm","r+b"); @@ -64,8 +67,9 @@ HTTPThread::HTTPThread() m_pagehandlers.push_back(new HomePage(templatestr)); m_ctx=0; - m_ctx=shttpd_init(NULL,"listen_ports",portstr.c_str(),NULL); - shttpd_listen(m_ctx,port,false); + m_ctx=shttpd_init(); + shttpd_set_option(m_ctx,"ports",portstr.c_str()); + shttpd_set_option(m_ctx,"acl",aclstr.c_str()); shttpd_register_uri(m_ctx,"*",HTTPThread::PageCallback,this); -- 2.7.4