xref: /sqlite-3.40.0/src/test_intarray.h (revision f8181eaa)
1522efc62Sdrh /*
2522efc62Sdrh ** 2009 November 10
3522efc62Sdrh **
4522efc62Sdrh ** The author disclaims copyright to this source code.  In place of
5522efc62Sdrh ** a legal notice, here is a blessing:
6522efc62Sdrh **
7522efc62Sdrh **    May you do good and not evil.
8522efc62Sdrh **    May you find forgiveness for yourself and forgive others.
9522efc62Sdrh **    May you share freely, never taking more than you give.
10522efc62Sdrh **
11522efc62Sdrh *************************************************************************
12522efc62Sdrh **
13522efc62Sdrh ** This is the C-language interface definition for the "intarray" or
14522efc62Sdrh ** integer array virtual table for SQLite.
15522efc62Sdrh **
16*f8181eaaSdrh ** This virtual table is used for internal testing of SQLite only.  It is
17*f8181eaaSdrh ** not recommended for use in production.  For a similar virtual table that
18*f8181eaaSdrh ** is production-ready, see the "carray" virtual table over in ext/misc.
19*f8181eaaSdrh **
20522efc62Sdrh ** The intarray virtual table is designed to facilitate using an
21522efc62Sdrh ** array of integers as the right-hand side of an IN operator.  So
22522efc62Sdrh ** instead of doing a prepared statement like this:
23522efc62Sdrh **
24522efc62Sdrh **     SELECT * FROM table WHERE x IN (?,?,?,...,?);
25522efc62Sdrh **
26522efc62Sdrh ** And then binding indivdual integers to each of ? slots, a C-language
27522efc62Sdrh ** application can create an intarray object (named "ex1" in the following
28522efc62Sdrh ** example), prepare a statement like this:
29522efc62Sdrh **
30522efc62Sdrh **     SELECT * FROM table WHERE x IN ex1;
31522efc62Sdrh **
32522efc62Sdrh ** Then bind an ordinary C/C++ array of integer values to the ex1 object
33522efc62Sdrh ** to run the statement.
34522efc62Sdrh **
35522efc62Sdrh ** USAGE:
36522efc62Sdrh **
37522efc62Sdrh ** One or more intarray objects can be created as follows:
38522efc62Sdrh **
39522efc62Sdrh **      sqlite3_intarray *p1, *p2, *p3;
40522efc62Sdrh **      sqlite3_intarray_create(db, "ex1", &p1);
41522efc62Sdrh **      sqlite3_intarray_create(db, "ex2", &p2);
42522efc62Sdrh **      sqlite3_intarray_create(db, "ex3", &p3);
43522efc62Sdrh **
44522efc62Sdrh ** Each call to sqlite3_intarray_create() generates a new virtual table
45522efc62Sdrh ** module and a singleton of that virtual table module in the TEMP
46522efc62Sdrh ** database.  Both the module and the virtual table instance use the
47522efc62Sdrh ** name given by the second parameter.  The virtual tables can then be
48522efc62Sdrh ** used in prepared statements:
49522efc62Sdrh **
50522efc62Sdrh **      SELECT * FROM t1, t2, t3
51522efc62Sdrh **       WHERE t1.x IN ex1
52522efc62Sdrh **         AND t2.y IN ex2
53522efc62Sdrh **         AND t3.z IN ex3;
54522efc62Sdrh **
55522efc62Sdrh ** Each integer array is initially empty.  New arrays can be bound to
56522efc62Sdrh ** an integer array as follows:
57522efc62Sdrh **
58522efc62Sdrh **     sqlite3_int64 a1[] = { 1, 2, 3, 4 };
59522efc62Sdrh **     sqlite3_int64 a2[] = { 5, 6, 7, 8, 9, 10, 11 };
60522efc62Sdrh **     sqlite3_int64 *a3 = sqlite3_malloc( 100*sizeof(sqlite3_int64) );
61522efc62Sdrh **     // Fill in content of a3[]
62522efc62Sdrh **     sqlite3_intarray_bind(p1, 4, a1, 0);
63522efc62Sdrh **     sqlite3_intarray_bind(p2, 7, a2, 0);
64522efc62Sdrh **     sqlite3_intarray_bind(p3, 100, a3, sqlite3_free);
65522efc62Sdrh **
66522efc62Sdrh ** A single intarray object can be rebound multiple times.  But do not
67522efc62Sdrh ** attempt to change the bindings of an intarray while it is in the middle
68522efc62Sdrh ** of a query.
69522efc62Sdrh **
70522efc62Sdrh ** The array that holds the integers is automatically freed by the function
71522efc62Sdrh ** in the fourth parameter to sqlite3_intarray_bind() when the array is no
72522efc62Sdrh ** longer needed.  The application must not change the intarray values
73522efc62Sdrh ** while an intarray is in the middle of a query.
74522efc62Sdrh **
75522efc62Sdrh ** The intarray object is automatically destroyed when its corresponding
76522efc62Sdrh ** virtual table is dropped.  Since the virtual tables are created in the
77522efc62Sdrh ** TEMP database, they are automatically dropped when the database connection
78522efc62Sdrh ** closes so the application does not normally need to take any special
79*f8181eaaSdrh ** action to free the intarray objects.  Because of the way virtual tables
80*f8181eaaSdrh ** work and the (somewhat goofy) way that the intarray virtual table is
81*f8181eaaSdrh ** implemented, it is not allowed to invoke sqlite3_intarray_create(D,N,P)
82*f8181eaaSdrh ** more than once with the same D and N values.
83522efc62Sdrh */
84522efc62Sdrh #include "sqlite3.h"
8543f58d6aSdrh #ifndef SQLITE_INTARRAY_H
8643f58d6aSdrh #define SQLITE_INTARRAY_H
87522efc62Sdrh 
88522efc62Sdrh /*
896e227bf3Sdrh ** Make sure we can call this stuff from C++.
906e227bf3Sdrh */
916e227bf3Sdrh #ifdef __cplusplus
926e227bf3Sdrh extern "C" {
936e227bf3Sdrh #endif
946e227bf3Sdrh 
956e227bf3Sdrh /*
96522efc62Sdrh ** An sqlite3_intarray is an abstract type to stores an instance of
97522efc62Sdrh ** an integer array.
98522efc62Sdrh */
99522efc62Sdrh typedef struct sqlite3_intarray sqlite3_intarray;
100522efc62Sdrh 
101522efc62Sdrh /*
102522efc62Sdrh ** Invoke this routine to create a specific instance of an intarray object.
103522efc62Sdrh ** The new intarray object is returned by the 3rd parameter.
104522efc62Sdrh **
105522efc62Sdrh ** Each intarray object corresponds to a virtual table in the TEMP table
106522efc62Sdrh ** with a name of zName.
107522efc62Sdrh **
108522efc62Sdrh ** Destroy the intarray object by dropping the virtual table.  If not done
109522efc62Sdrh ** explicitly by the application, the virtual table will be dropped implicitly
110522efc62Sdrh ** by the system when the database connection is closed.
111522efc62Sdrh */
112fb90841aSdrh SQLITE_API int sqlite3_intarray_create(
113522efc62Sdrh   sqlite3 *db,
114522efc62Sdrh   const char *zName,
115522efc62Sdrh   sqlite3_intarray **ppReturn
116522efc62Sdrh );
117522efc62Sdrh 
118522efc62Sdrh /*
119522efc62Sdrh ** Bind a new array array of integers to a specific intarray object.
120522efc62Sdrh **
121522efc62Sdrh ** The array of integers bound must be unchanged for the duration of
122522efc62Sdrh ** any query against the corresponding virtual table.  If the integer
123522efc62Sdrh ** array does change or is deallocated undefined behavior will result.
124522efc62Sdrh */
125fb90841aSdrh SQLITE_API int sqlite3_intarray_bind(
126522efc62Sdrh   sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
127522efc62Sdrh   int nElements,                 /* Number of elements in the intarray */
128522efc62Sdrh   sqlite3_int64 *aElements,      /* Content of the intarray */
129522efc62Sdrh   void (*xFree)(void*)           /* How to dispose of the intarray when done */
130522efc62Sdrh );
1316e227bf3Sdrh 
1326e227bf3Sdrh #ifdef __cplusplus
1336e227bf3Sdrh }  /* End of the 'extern "C"' block */
1346e227bf3Sdrh #endif
13543f58d6aSdrh #endif /* SQLITE_INTARRAY_H */
136