Debian Codegraph @

www/loadext.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Run-Time Loadable Extensions</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Run-Time Loadable Extensions
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#loading_an_extension">2. Loading An Extension</a></div>
130 <div class="fancy-toc1"><a href="#compiling_a_loadable_extension">3. Compiling A Loadable Extension</a></div>
131 <div class="fancy-toc1"><a href="#programming_loadable_extensions">4. Programming Loadable Extensions</a></div>
132 <div class="fancy-toc2"><a href="#example_extensions">4.1. Example Extensions</a></div>
133 <div class="fancy-toc1"><a href="#persistent_loadable_extensions">5. Persistent Loadable Extensions</a></div>
134 <div class="fancy-toc1"><a href="#statically_linking_a_run_time_loadable_extension">6. Statically Linking A Run-Time Loadable Extension</a></div>
135 <div class="fancy-toc1"><a href="#implementation_details">7. Implementation Details</a></div>
136 </div>
137 </div>
138 <script>
139 function toggle_toc(){
140 var sub = document.getElementById("toc_sub")
141 var mk = document.getElementById("toc_mk")
142 if( sub.style.display!="block" ){
143 sub.style.display = "block";
144 mk.innerHTML = "&#x25bc;";
145 } else {
146 sub.style.display = "none";
147 mk.innerHTML = "&#x25ba;";
148 }
149 }
150 </script>
151 </div>
152
153
154
155
156
157 <h1 id="overview"><span>1. </span>Overview</h1>
158
159 <p>SQLite has the ability to load extensions (including new
160 <a href="appfunc.html">application-defined SQL functions</a>,
161 <a href="datatype3.html#collation">collating sequences</a>, <a href="vtab.html">virtual tables</a>, and <a href="vfs.html">VFSes</a>) at run-time.
162 This feature allows the code for extensions to be developed and
163 tested separately from the application and then loaded
164 on an as-needed basis.</p>
165
166 <p>Extensions can also be statically linked with the application.
167 The code template shown below will work just as well as a statically
168 linked extension as it does as a run-time loadable extension except that
169 you should give the entry point function ("sqlite3_extension_init")
170 a different name to avoid name collisions if your application contains
171 two or more extensions.</p>
172
173 <h1 id="loading_an_extension"><span>2. </span>Loading An Extension</h1>
174
175 <p>An SQLite extension is a shared library or DLL. To load it, you
176 need to supply SQLite with the name of the file containing the
177 shared library or DLL and an entry point to initialize the extension.
178 In C code, this information is supplied using the
179 <a href="c3ref/load_extension.html">sqlite3_load_extension()</a> API. See the documentation on that
180 routine for additional information.</p>
181
182 <p>Note that different operating systems use different filename
183 suffixes for their shared libraries. Windows uses ".dll", Mac uses
184 ".dylib", and most unixes other than mac use ".so". If you want to
185 make your code portable, you can omit the suffix from the shared
186 library filename and the appropriate suffix will be added automatically
187 by the <a href="c3ref/load_extension.html">sqlite3_load_extension()</a> interface.</p>
188
189 <p>There is also an SQL function that can be used to load extensions:
190 <a href="lang_corefunc.html#load_extension">load_extension(X,Y)</a>. It works just like the <a href="c3ref/load_extension.html">sqlite3_load_extension()</a>
191 C interface.</p>
192
193 <p>Both methods for loading an extension allow you to specify
194 the name of an entry point for the extension.
195 You can leave this argument blank - passing in
196 a NULL pointer for the <a href="c3ref/load_extension.html">sqlite3_load_extension()</a> C-language interface
197 or omitting the second argument for the <a href="lang_corefunc.html#load_extension">load_extension()</a> SQL interface -
198 and the extension loader logic will attempt to figure out the entry point
199 on its own. It will first try the generic extension name
200 "sqlite3_extension_init". If that does not work, it constructs a
201 entry point using the template "sqlite3_X_init" where the X is replaced
202 by the lowercase equivalent of every ASCII character in the filename
203 after the last "/" and before the first following "." omitting the
204 first three characters if they happen to be "lib". So, for example,
205 if the filename is "/usr/lib/libmathfunc-4.8.so" the entry point name
206 would be "sqlite3_mathfunc_init". Or if the filename is
207 "./SpellFixExt.dll" then the entry point would be called
208 "sqlite3_spellfixext_init".
209
210 </p><p>For security reasons, extension loading is turned off by default.
211 In order to use either the C-language or SQL extension loading functions,
212 one must first enable extension loading using the
213 <a href="c3ref/db_config.html">sqlite3_db_config</a>(db,<a href="c3ref/c_dbconfig_defensive.html#sqlitedbconfigenableloadextension">SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</a>,1,NULL)
214 C-language API in your application.</p>
215
216 <p>From the <a href="cli.html">command-line shell</a>, extensions can be loaded using the
217 ".load" dot-command. For example:
218
219 </p><blockquote><pre>
220 .load ./YourCode
221 </pre></blockquote>
222
223 <p>Note that the command-line shell program has already enabled
224 extension loading for you (by calling the <a href="c3ref/enable_load_extension.html">sqlite3_enable_load_extension()</a>
225 interface as part of its setup) so the command above works without
226 any special switches, setup, or other complications.</p>
227
228 <p>The ".load" command with one argument invokes sqlite3_load_extension()
229 with the zProc parameter set to NULL, causing SQLite to first look for
230 an entry point named "sqlite3_extension_init" and then "sqlite3_X_init"
231 where "X" is derived from the filename. If your extension has an entry
232 point with a different name, simply supply that name as the second
233 argument. For example:</p>
234
235 <blockquote><pre>
236 .load ./YourCode nonstandard_entry_point
237 </pre></blockquote>
238
239 <a name="build"></a>
240
241 <h1 id="compiling_a_loadable_extension"><span>3. </span>Compiling A Loadable Extension</h1>
242
243 <p>Loadable extensions are C-code. To compile them on
244 most unix-like operating
245 systems, the usual command is something like this:</p>
246
247 <blockquote><pre>
248 gcc -g -fPIC -shared YourCode.c -o YourCode.so
249 </pre></blockquote>
250
251 <p>Macs are unix-like, but they do not follow the usual shared library
252 conventions. To compile a shared library on a Mac, use a command like
253 this:</p>
254
255 <blockquote><pre>
256 gcc -g -fPIC -dynamiclib YourCode.c -o YourCode.dylib
257 </pre></blockquote>
258
259 <p>If when you try to load your library you get back an error message
260 that says "mach-o, but wrong architecture" then you might need to add
261 command-line options "-arch i386" or "arch x86_64" to gcc, depending
262 on how your application is built.</p>
263
264 <p>To compile on Windows using MSVC, a command similar to the following
265 will usually work:</p>
266
267 <blockquote><pre>
268 cl YourCode.c -link -dll -out:YourCode.dll
269 </pre></blockquote>
270
271 <p>To compile for Windows using MinGW, the command line is just like it
272 is for unix except that the output file suffix is changed to ".dll" and
273 the -fPIC argument is omitted:</p>
274
275 <blockquote><pre>
276 gcc -g -shared YourCode.c -o YourCode.dll
277 </pre></blockquote>
278
279 <a name="write"></a>
280
281 <h1 id="programming_loadable_extensions"><span>4. </span>Programming Loadable Extensions</h1>
282
283 <p>A template loadable extension contains the following three elements:</p>
284
285 <ol>
286 <li><p>
287 Use "<tt>#include &lt;sqlite3ext.h&gt;</tt>" at the top of your source
288 code files instead of "<tt>#include &lt;sqlite3.h&gt;</tt>".
289 </p>
290
291 </li><li><p>
292 Put the macro "<tt>SQLITE_EXTENSION_INIT1</tt>" on a line by itself
293 right after the "<tt>#include &lt;sqlite3ext.h&gt;</tt>" line.
294 </p>
295
296 </li><li><p>
297 Add an extension loading entry point routine that looks like
298 something the following:
299
300 </p><div class="codeblock"><pre>#ifdef _WIN32
301 __declspec(dllexport)
302 #endif
303 int sqlite3_extension_init( /* &lt;== Change this name, maybe */
304 sqlite3 *db,
305 char **pzErrMsg,
306 const sqlite3_api_routines *pApi
307 ){
308 int rc = SQLITE_OK;
309 SQLITE_EXTENSION_INIT2(pApi);
310 /* insert code to initialize your extension here */
311 return rc;
312 }
313 </pre></div>
314
315 <p>You will do well to customize the name of your entry point to
316 correspond to the name of the shared library you will be generating,
317 rather than using the generic "sqlite3_extension_init" name. Giving
318 your extension a custom entry point name will enable you to statically
319 link two or more extensions into the same program without a linker
320 conflict, if you later decide to use static linking rather than run-time
321 linking.
322 If your shared library ends up being named "YourCode.so" or
323 "YourCode.dll" or "YourCode.dylib" as shown in the compiler examples
324 above, then the correct entry point name would be
325 "sqlite3_yourcode_init".
326 </p></li></ol>
327
328 <p>Here is a complete template extension that you can copy/paste
329 to get started:</p>
330
331 <div class="codeblock"><pre>/* Add your header comment here */
332 #include &lt;sqlite3ext.h&gt; /* Do not use &lt;sqlite3.h&gt;! */
333 SQLITE_EXTENSION_INIT1
334
335 /* Insert your extension code here */
336
337 #ifdef _WIN32
338 __declspec(dllexport)
339 #endif
340 /* TODO: Change the entry point name so that "extension" is replaced by
341 ** text derived from the shared library filename as follows: Copy every
342 ** ASCII alphabetic character from the filename after the last "/" through
343 ** the next following ".", converting each character to lowercase, and
344 ** discarding the first three characters if they are "lib".
345 */
346 int sqlite3_extension_init(
347 sqlite3 *db,
348 char **pzErrMsg,
349 const sqlite3_api_routines *pApi
350 ){
351 int rc = SQLITE_OK;
352 SQLITE_EXTENSION_INIT2(pApi);
353 /* Insert here calls to
354 ** sqlite3_create_function_v2(),
355 ** sqlite3_create_collation_v2(),
356 ** sqlite3_create_module_v2(), and/or
357 ** sqlite3_vfs_register()
358 ** to register the new features that your extension adds.
359 */
360 return rc;
361 }
362 </pre></div>
363
364 <h2 id="example_extensions"><span>4.1. </span>Example Extensions</h2>
365
366 <p>Many examples of complete and working loadable extensions can be
367 seen in the SQLite source tree in the
368 <a href="https://www.sqlite.org/src/file/ext/misc">ext/misc</a> subdirectory.
369 Each file in that directory is a separate extension. Documentation
370 is provided by a header comment on the file.
371 Here are brief notes on a few of the extensions in
372 the <a href="https://www.sqlite.org/src/file/ext/misc">ext/misc</a> subdirectory:
373
374 </p><ul>
375 <li><p>
376 <a href="https://www.sqlite.org/src/file/ext/misc/carray.c">carray.c</a> &mdash;
377 Implementation of the <a href="carray.html">carray table-valued function</a>.
378 </p></li><li><p>
379 <a href="https://www.sqlite.org/src/file/ext/misc/compress.c">compress.c</a> &mdash;
380 Implementation of <a href="appfunc.html">application-defined SQL functions</a> compress() and
381 uncompress() that do zLib compression of text or blob content.
382 </p></li><li><p>
383 <a href="https://www.sqlite.org/src/file/ext/misc/json1.c">json1.c</a> &mdash;
384 Implementation of <a href="json1.html">JSON SQL functions</a> and <a href="vtab.html#tabfunc2">table-valued functions</a>.
385 This is a larger and more complex extension.
386 </p></li><li><p>
387 <a href="https://www.sqlite.org/src/file/ext/misc/memvfs.c">memvfs.c</a> &mdash;
388 Implementation of a new <a href="vfs.html">VFS</a> that stores all content in-memory.
389 </p></li><li><p>
390 <a href="https://www.sqlite.org/src/file/ext/misc/rot13.c">rot13.c</a> &mdash;
391 Implementation of a <a href="https://en.wikipedia.org/wiki/ROT13">rot13()</a>
392 SQL function. This is a very simple example of an extension function
393 and is useful as a template for creating new extensions.
394 </p></li><li><p>
395 <a href="https://www.sqlite.org/src/file/ext/misc/series.c">series.c</a> &mdash;
396 Implementation of the generate_series <a href="vtab.html">virtual table</a> and
397 <a href="vtab.html#tabfunc2">table-valued function</a>. This is a relatively simple example of a
398 virtual table implementation which can serve as a template for writing
399 new virtual tables.
400 </p></li></ul>
401
402 <p>Other and more complex extensions can be found in subfolders
403 under <a href="https://www.sqlite.org/src/file/ext">ext/</a> other than ext/misc/.
404
405 <a name="persist"></a>
406
407 </p><h1 id="persistent_loadable_extensions"><span>5. </span>Persistent Loadable Extensions</h1>
408
409 <p>The default behavior for a loadable extension is that it is unloaded
410 from process memory when the database connection that originally invoked
411 <a href="c3ref/load_extension.html">sqlite3_load_extension()</a> closes. (In other words, the xDlClose method
412 of the <a href="c3ref/vfs.html">sqlite3_vfs</a> object is called for all extensions when a database
413 connection closes.) However, if the initialization procedure returns
414 <a href="rescode.html#ok_load_permanently">SQLITE_OK_LOAD_PERMANENTLY</a> instead of SQLITE_OK, then the extension will
415 not be unloaded (xDlClose will not be invoked) and the extension will remain
416 in process memory indefinitely. The SQLITE_OK_LOAD_PERMANENTLY return
417 value is useful for extensions that want to register new <a href="vfs.html">VFSes</a>.
418
419 </p><p>To clarify: an extension for which the initialization function returns
420 SQLITE_OK_LOAD_PERMANENTLY continues to exist in memory after the database
421 connection closes. However, the extension is <em>not</em> automatically
422 registered with subsequent database connections. This makes it possible
423 to load extensions that implement new <a href="vfs.html">VFSes</a>.
424 To persistently load and register an extension that implements new SQL
425 functions, collating sequences, and/or virtual tables, such that those
426 added capabilities are available to all subsequent database connections,
427 then the initialization routine should also invoke <a href="c3ref/auto_extension.html">sqlite3_auto_extension()</a>
428 on a subfunction that will register those services.
429
430 </p><p>The <a href="https://sqlite.org/src/file/ext/misc/vfsstat.c">vfsstat.c</a> extension
431 show an example of a loadable extension that persistently registers both
432 a new VFS and a new virtual table. The
433 <a href="https://sqlite.org/src/info/77b5b4235c9f7f11?ln=801-819">sqlite3_vfsstat_init()</a>
434 initialization routine in that extension is called only once, when the
435 extension is first loaded. It registers the new "vfslog" VFS just that
436 one time, and it returns SQLITE_OK_LOAD_PERMANENTLY so that the code used
437 to implement the "vfslog" VFS will remain in memory. The initialization routine
438 also invokes <a href="c3ref/auto_extension.html">sqlite3_auto_extension()</a> on a pointer to the "vstatRegister()"
439 function so that all subsequent database connections will invoke the
440 "vstatRegister()" function as they start up, and hence register the
441 "vfsstat" virtual table.
442
443 <a name="statext"></a>
444
445 </p><h1 id="statically_linking_a_run_time_loadable_extension"><span>6. </span>Statically Linking A Run-Time Loadable Extension</h1>
446
447 <p>The exact same source code can be used for both a run-time loadable
448 shared library or DLL and as a module that is statically linked with your
449 application. This provides flexibility and allows you to reuse the same
450 code in different ways.</p>
451
452 <p>To statically link your extension, simply add the -DSQLITE_CORE
453 compile-time option. The SQLITE_CORE macro causes the SQLITE_EXTENSION_INIT1
454 and SQLITE_EXTENSION_INIT2 macros to become no-ops. Then modify your
455 application to invoke the entry point directly, passing in a NULL pointer
456 as the third "pApi" parameter.</p>
457
458 <p>It is particularly important to use an entry point name that is
459 based on the extension filename, rather than the generic
460 "sqlite3_extension_init" entry point name, if you will be statically
461 linking two or more extensions. If you use the generic name, there
462 will be multiple definitions of the same symbol and the link will fail.</p>
463
464 <p>If you will be opening multiple database connections in your application,
465 rather than invoking the extension entry points for each database
466 connection separately, you might want to consider using the
467 <a href="c3ref/auto_extension.html">sqlite3_auto_extension()</a> interface to register your extensions and
468 to cause them to be automatically started as each database connection
469 is opened. You only have to register each extension once, and you can
470 do so near the beginning of your main() routine. Using the
471 <a href="c3ref/auto_extension.html">sqlite3_auto_extension()</a> interface to register your extensions makes
472 your extensions work as if they were built into the core SQLite - they
473 automatically exist whenever you open a new database connection
474 without needing to be initialized. Just be sure to complete any
475 configuration you need to accomplish using <a href="c3ref/config.html">sqlite3_config()</a> before
476 registering your extensions, since the <a href="c3ref/auto_extension.html">sqlite3_auto_extension()</a>
477 interface implicitly calls <a href="c3ref/initialize.html">sqlite3_initialize()</a>.</p>
478
479 <h1 id="implementation_details"><span>7. </span>Implementation Details</h1>
480
481 <p>SQLite implements run-time extension loading using the
482 xDlOpen(), xDlError(), xDlSym(), and xDlClose() methods of the
483 <a href="c3ref/vfs.html">sqlite3_vfs</a> object. These methods are implemented using
484 the dlopen() library on unix (which explains why SQLite commonly
485 needs to be linked against the "-ldl" library on unix systems)
486 and using LoadLibrary() API on Windows. In a custom <a href="vfs.html">VFS</a> for
487 unusual systems, these methods can all be omitted, in which case
488 the run-time extension loading mechanism will not work (though
489 you will still be able to statically link the extension code, assuming
490 the entry pointers are uniquely named).
491 SQLite can be compiled with
492 <a href="compile.html#omit_load_extension">SQLITE_OMIT_LOAD_EXTENSION</a> to omit the extension loading code
493 from the build.
494 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/loadext.in?m=685a124ac3">2024-01-31 23:45:50</a> UTC </small></i></p>
495