Debian Codegraph @

www/carray.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>The Carray() Table-Valued Function</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 The Carray() Table-Valued Function
122 </div>
123 </div>
124
125
126
127
128 <h1 id="overview"><span>1. </span>Overview</h1>
129
130 <p>Carray() is a <a href="vtab.html#tabfunc2">table-valued function</a> with a single column (named
131 "value") and zero or more rows.
132 The "value" of each row in the carray() is taken from a C-language array
133 supplied by the application via <a href="c3ref/bind_blob.html">parameter binding</a>.
134 In this way, the carray() function provides a convenient mechanism to
135 bind C-language arrays to SQL queries.
136
137 </p><h1 id="availability"><span>2. </span>Availability</h1>
138
139 <p>The carray() function is not compiled into SQLite by default.
140 It is available as a <a href="loadext.html">loadable extension</a> in the
141 <a href="https://www.sqlite.org/src/file/ext/misc/carray.c">ext/misc/carray.c</a>
142 source file.
143
144 </p><p>The carray() function was first added to SQLite in version 3.14
145 (2016-08-08). The sqlite3_carray_bind() interface and the
146 single-argument variant of carray() was added in SQLite version 3.34.0
147 (2020-12-01). The ability to bind an array of <tt>struct iovec</tt>
148 objects that are interpreted as BLOBs was added in SQLite version 3.41.0
149 (2023-02-21).
150
151 </p><h1 id="details"><span>3. </span>Details</h1>
152
153 <p>The carray() function takes one, two, or three arguments.
154
155 </p><p>For the two- and three-argument versions of carray(),
156 the first argument is a pointer to an array. Since pointer values cannot
157 be specified directly in SQL, the first argument must be a <a href="lang_expr.html#varparam">parameter</a> that
158 is bound to a pointer value using the <a href="c3ref/bind_blob.html">sqlite3_bind_pointer()</a> interface
159 using a pointer-type of "carray".
160 The second argument is the number of elements in the array. The optional
161 third argument is a string that determines the datatype of the elements
162 in the C-language array. Allowed values for the third argument are:
163
164 </p><ol>
165 <li> 'int32'
166 </li><li> 'int64'
167 </li><li> 'double'
168 </li><li> 'char*'
169 </li><li> 'struct iovec'
170 </li></ol>
171
172 <p>The default datatype is 'int32'.
173
174 </p><p>The 'struct iovec' type used for BLOB data is a standard Posix data
175 structure, normally declared using "<tt>#include &lt;sys/uio.h&gt;</tt>".
176 The format is:
177
178 </p><blockquote><pre>
179 struct iovec {
180 void *iov_base; /* Starting address */
181 size_t iov_len; /* Number of bytes to transfer */
182 };
183 </pre></blockquote>
184
185 <a name="onearg"></a>
186
187 <h2 id="single_argument_carray"><span>3.1. </span>Single-Argument CARRAY</h2>
188
189 <p>The single-argument form of carray() requires a special C-language
190 interface named "sqlite3_carray_bind()" in order to attach values:
191
192 </p><blockquote><pre>
193 int sqlite3_carray_bind(
194 sqlite3_stmt *pStmt, /* Statement containing the CARRAY */
195 int idx, /* Parameter number for CARRAY argument */
196 void *aData, /* Data array */
197 int nData, /* Number of entries in the array */
198 int mFlags, /* Datatype flag */
199 void (*xDestroy)(void*) /* Destructor for aData */
200 );
201 </pre></blockquote>
202
203 <p>The mFlags parameter to sqlite3_carray_bind() must be one of:
204
205 </p><blockquote><pre>
206 #define CARRAY_INT32 0
207 #define CARRAY_INT64 1
208 #define CARRAY_DOUBLE 2
209 #define CARRAY_TEXT 3
210 #define CARRAY_BLOB 4
211 </pre></blockquote>
212
213 <p>Higher order bits of the mFlags parameter must all be zero for now,
214 though they may be used in future enhancements. The definitions for the
215 constants that specify the datatype and a prototype for the
216 sqlite3_carray_bind() function are both available in the auxiliary
217 header file
218 <a href="https://www.sqlite.org/src/file/ext/misc/carray.h">ext/misc/carray.h</a>.
219
220 </p><p>The xDestroy argument to sqlite3_carray_bind() routine is a pointer
221 to a function that frees the input array. SQLite will invoke this
222 function after it has finished with the data. The xDestroy argument
223 may optionally be one of the following constants defined in
224 "sqlite3.h":
225
226 </p><ul>
227 <li><p>
228 <a href="c3ref/c_static.html">SQLITE_STATIC</a> &rarr; This means that the application that invokes
229 sqlite3_carray_bind() maintains ownership of the data array and that
230 the application promises SQLite that it will not change or deallocate
231 the data until after the prepared statement is finialized.
232
233 </p></li><li><p>
234 <a href="c3ref/c_static.html">SQLITE_TRANSIENT</a> &rarr; This special value instructs SQLite to make
235 its own private copy of the data before the
236 sqlite3_carray_bind() interface returns.
237 </p></li></ul>
238
239
240 <h1 id="usage"><span>4. </span>Usage</h1>
241
242 <p>The carray() function can be used in the FROM clause of a query.
243 For example, to query two entries from the OBJ table using rowids
244 taken from a C-language array at address $PTR.
245
246 </p><div class="codeblock"><pre>SELECT obj.* FROM obj, carray($PTR, 10) AS x
247 WHERE obj.rowid=x.value;
248 </pre></div>
249
250 <p>This query gives the same result:
251
252 </p><div class="codeblock"><pre>SELECT * FROM obj WHERE rowid IN carray($PTR, 10);
253 </pre></div>
254
255