xref: /sqlite-3.40.0/src/test_intarray.h (revision 522efc62)
1*522efc62Sdrh /*
2*522efc62Sdrh ** 2009 November 10
3*522efc62Sdrh **
4*522efc62Sdrh ** The author disclaims copyright to this source code.  In place of
5*522efc62Sdrh ** a legal notice, here is a blessing:
6*522efc62Sdrh **
7*522efc62Sdrh **    May you do good and not evil.
8*522efc62Sdrh **    May you find forgiveness for yourself and forgive others.
9*522efc62Sdrh **    May you share freely, never taking more than you give.
10*522efc62Sdrh **
11*522efc62Sdrh *************************************************************************
12*522efc62Sdrh **
13*522efc62Sdrh ** This is the C-language interface definition for the "intarray" or
14*522efc62Sdrh ** integer array virtual table for SQLite.
15*522efc62Sdrh **
16*522efc62Sdrh ** The intarray virtual table is designed to facilitate using an
17*522efc62Sdrh ** array of integers as the right-hand side of an IN operator.  So
18*522efc62Sdrh ** instead of doing a prepared statement like this:
19*522efc62Sdrh **
20*522efc62Sdrh **     SELECT * FROM table WHERE x IN (?,?,?,...,?);
21*522efc62Sdrh **
22*522efc62Sdrh ** And then binding indivdual integers to each of ? slots, a C-language
23*522efc62Sdrh ** application can create an intarray object (named "ex1" in the following
24*522efc62Sdrh ** example), prepare a statement like this:
25*522efc62Sdrh **
26*522efc62Sdrh **     SELECT * FROM table WHERE x IN ex1;
27*522efc62Sdrh **
28*522efc62Sdrh ** Then bind an ordinary C/C++ array of integer values to the ex1 object
29*522efc62Sdrh ** to run the statement.
30*522efc62Sdrh **
31*522efc62Sdrh ** USAGE:
32*522efc62Sdrh **
33*522efc62Sdrh ** One or more intarray objects can be created as follows:
34*522efc62Sdrh **
35*522efc62Sdrh **      sqlite3_intarray *p1, *p2, *p3;
36*522efc62Sdrh **      sqlite3_intarray_create(db, "ex1", &p1);
37*522efc62Sdrh **      sqlite3_intarray_create(db, "ex2", &p2);
38*522efc62Sdrh **      sqlite3_intarray_create(db, "ex3", &p3);
39*522efc62Sdrh **
40*522efc62Sdrh ** Each call to sqlite3_intarray_create() generates a new virtual table
41*522efc62Sdrh ** module and a singleton of that virtual table module in the TEMP
42*522efc62Sdrh ** database.  Both the module and the virtual table instance use the
43*522efc62Sdrh ** name given by the second parameter.  The virtual tables can then be
44*522efc62Sdrh ** used in prepared statements:
45*522efc62Sdrh **
46*522efc62Sdrh **      SELECT * FROM t1, t2, t3
47*522efc62Sdrh **       WHERE t1.x IN ex1
48*522efc62Sdrh **         AND t2.y IN ex2
49*522efc62Sdrh **         AND t3.z IN ex3;
50*522efc62Sdrh **
51*522efc62Sdrh ** Each integer array is initially empty.  New arrays can be bound to
52*522efc62Sdrh ** an integer array as follows:
53*522efc62Sdrh **
54*522efc62Sdrh **     sqlite3_int64 a1[] = { 1, 2, 3, 4 };
55*522efc62Sdrh **     sqlite3_int64 a2[] = { 5, 6, 7, 8, 9, 10, 11 };
56*522efc62Sdrh **     sqlite3_int64 *a3 = sqlite3_malloc( 100*sizeof(sqlite3_int64) );
57*522efc62Sdrh **     // Fill in content of a3[]
58*522efc62Sdrh **     sqlite3_intarray_bind(p1, 4, a1, 0);
59*522efc62Sdrh **     sqlite3_intarray_bind(p2, 7, a2, 0);
60*522efc62Sdrh **     sqlite3_intarray_bind(p3, 100, a3, sqlite3_free);
61*522efc62Sdrh **
62*522efc62Sdrh ** A single intarray object can be rebound multiple times.  But do not
63*522efc62Sdrh ** attempt to change the bindings of an intarray while it is in the middle
64*522efc62Sdrh ** of a query.
65*522efc62Sdrh **
66*522efc62Sdrh ** The array that holds the integers is automatically freed by the function
67*522efc62Sdrh ** in the fourth parameter to sqlite3_intarray_bind() when the array is no
68*522efc62Sdrh ** longer needed.  The application must not change the intarray values
69*522efc62Sdrh ** while an intarray is in the middle of a query.
70*522efc62Sdrh **
71*522efc62Sdrh ** The intarray object is automatically destroyed when its corresponding
72*522efc62Sdrh ** virtual table is dropped.  Since the virtual tables are created in the
73*522efc62Sdrh ** TEMP database, they are automatically dropped when the database connection
74*522efc62Sdrh ** closes so the application does not normally need to take any special
75*522efc62Sdrh ** action to free the intarray objects.
76*522efc62Sdrh */
77*522efc62Sdrh #include "sqlite3.h"
78*522efc62Sdrh 
79*522efc62Sdrh /*
80*522efc62Sdrh ** An sqlite3_intarray is an abstract type to stores an instance of
81*522efc62Sdrh ** an integer array.
82*522efc62Sdrh */
83*522efc62Sdrh typedef struct sqlite3_intarray sqlite3_intarray;
84*522efc62Sdrh 
85*522efc62Sdrh /*
86*522efc62Sdrh ** Invoke this routine to create a specific instance of an intarray object.
87*522efc62Sdrh ** The new intarray object is returned by the 3rd parameter.
88*522efc62Sdrh **
89*522efc62Sdrh ** Each intarray object corresponds to a virtual table in the TEMP table
90*522efc62Sdrh ** with a name of zName.
91*522efc62Sdrh **
92*522efc62Sdrh ** Destroy the intarray object by dropping the virtual table.  If not done
93*522efc62Sdrh ** explicitly by the application, the virtual table will be dropped implicitly
94*522efc62Sdrh ** by the system when the database connection is closed.
95*522efc62Sdrh */
96*522efc62Sdrh int sqlite3_intarray_create(
97*522efc62Sdrh   sqlite3 *db,
98*522efc62Sdrh   const char *zName,
99*522efc62Sdrh   sqlite3_intarray **ppReturn
100*522efc62Sdrh );
101*522efc62Sdrh 
102*522efc62Sdrh /*
103*522efc62Sdrh ** Bind a new array array of integers to a specific intarray object.
104*522efc62Sdrh **
105*522efc62Sdrh ** The array of integers bound must be unchanged for the duration of
106*522efc62Sdrh ** any query against the corresponding virtual table.  If the integer
107*522efc62Sdrh ** array does change or is deallocated undefined behavior will result.
108*522efc62Sdrh */
109*522efc62Sdrh int sqlite3_intarray_bind(
110*522efc62Sdrh   sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
111*522efc62Sdrh   int nElements,                 /* Number of elements in the intarray */
112*522efc62Sdrh   sqlite3_int64 *aElements,      /* Content of the intarray */
113*522efc62Sdrh   void (*xFree)(void*)           /* How to dispose of the intarray when done */
114*522efc62Sdrh );
115