xref: /sqlite-3.40.0/src/test_intarray.h (revision 6e227bf3)
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 **
16522efc62Sdrh ** The intarray virtual table is designed to facilitate using an
17522efc62Sdrh ** array of integers as the right-hand side of an IN operator.  So
18522efc62Sdrh ** instead of doing a prepared statement like this:
19522efc62Sdrh **
20522efc62Sdrh **     SELECT * FROM table WHERE x IN (?,?,?,...,?);
21522efc62Sdrh **
22522efc62Sdrh ** And then binding indivdual integers to each of ? slots, a C-language
23522efc62Sdrh ** application can create an intarray object (named "ex1" in the following
24522efc62Sdrh ** example), prepare a statement like this:
25522efc62Sdrh **
26522efc62Sdrh **     SELECT * FROM table WHERE x IN ex1;
27522efc62Sdrh **
28522efc62Sdrh ** Then bind an ordinary C/C++ array of integer values to the ex1 object
29522efc62Sdrh ** to run the statement.
30522efc62Sdrh **
31522efc62Sdrh ** USAGE:
32522efc62Sdrh **
33522efc62Sdrh ** One or more intarray objects can be created as follows:
34522efc62Sdrh **
35522efc62Sdrh **      sqlite3_intarray *p1, *p2, *p3;
36522efc62Sdrh **      sqlite3_intarray_create(db, "ex1", &p1);
37522efc62Sdrh **      sqlite3_intarray_create(db, "ex2", &p2);
38522efc62Sdrh **      sqlite3_intarray_create(db, "ex3", &p3);
39522efc62Sdrh **
40522efc62Sdrh ** Each call to sqlite3_intarray_create() generates a new virtual table
41522efc62Sdrh ** module and a singleton of that virtual table module in the TEMP
42522efc62Sdrh ** database.  Both the module and the virtual table instance use the
43522efc62Sdrh ** name given by the second parameter.  The virtual tables can then be
44522efc62Sdrh ** used in prepared statements:
45522efc62Sdrh **
46522efc62Sdrh **      SELECT * FROM t1, t2, t3
47522efc62Sdrh **       WHERE t1.x IN ex1
48522efc62Sdrh **         AND t2.y IN ex2
49522efc62Sdrh **         AND t3.z IN ex3;
50522efc62Sdrh **
51522efc62Sdrh ** Each integer array is initially empty.  New arrays can be bound to
52522efc62Sdrh ** an integer array as follows:
53522efc62Sdrh **
54522efc62Sdrh **     sqlite3_int64 a1[] = { 1, 2, 3, 4 };
55522efc62Sdrh **     sqlite3_int64 a2[] = { 5, 6, 7, 8, 9, 10, 11 };
56522efc62Sdrh **     sqlite3_int64 *a3 = sqlite3_malloc( 100*sizeof(sqlite3_int64) );
57522efc62Sdrh **     // Fill in content of a3[]
58522efc62Sdrh **     sqlite3_intarray_bind(p1, 4, a1, 0);
59522efc62Sdrh **     sqlite3_intarray_bind(p2, 7, a2, 0);
60522efc62Sdrh **     sqlite3_intarray_bind(p3, 100, a3, sqlite3_free);
61522efc62Sdrh **
62522efc62Sdrh ** A single intarray object can be rebound multiple times.  But do not
63522efc62Sdrh ** attempt to change the bindings of an intarray while it is in the middle
64522efc62Sdrh ** of a query.
65522efc62Sdrh **
66522efc62Sdrh ** The array that holds the integers is automatically freed by the function
67522efc62Sdrh ** in the fourth parameter to sqlite3_intarray_bind() when the array is no
68522efc62Sdrh ** longer needed.  The application must not change the intarray values
69522efc62Sdrh ** while an intarray is in the middle of a query.
70522efc62Sdrh **
71522efc62Sdrh ** The intarray object is automatically destroyed when its corresponding
72522efc62Sdrh ** virtual table is dropped.  Since the virtual tables are created in the
73522efc62Sdrh ** TEMP database, they are automatically dropped when the database connection
74522efc62Sdrh ** closes so the application does not normally need to take any special
75522efc62Sdrh ** action to free the intarray objects.
76522efc62Sdrh */
77522efc62Sdrh #include "sqlite3.h"
78522efc62Sdrh 
79522efc62Sdrh /*
80*6e227bf3Sdrh ** Make sure we can call this stuff from C++.
81*6e227bf3Sdrh */
82*6e227bf3Sdrh #ifdef __cplusplus
83*6e227bf3Sdrh extern "C" {
84*6e227bf3Sdrh #endif
85*6e227bf3Sdrh 
86*6e227bf3Sdrh /*
87522efc62Sdrh ** An sqlite3_intarray is an abstract type to stores an instance of
88522efc62Sdrh ** an integer array.
89522efc62Sdrh */
90522efc62Sdrh typedef struct sqlite3_intarray sqlite3_intarray;
91522efc62Sdrh 
92522efc62Sdrh /*
93522efc62Sdrh ** Invoke this routine to create a specific instance of an intarray object.
94522efc62Sdrh ** The new intarray object is returned by the 3rd parameter.
95522efc62Sdrh **
96522efc62Sdrh ** Each intarray object corresponds to a virtual table in the TEMP table
97522efc62Sdrh ** with a name of zName.
98522efc62Sdrh **
99522efc62Sdrh ** Destroy the intarray object by dropping the virtual table.  If not done
100522efc62Sdrh ** explicitly by the application, the virtual table will be dropped implicitly
101522efc62Sdrh ** by the system when the database connection is closed.
102522efc62Sdrh */
103522efc62Sdrh int sqlite3_intarray_create(
104522efc62Sdrh   sqlite3 *db,
105522efc62Sdrh   const char *zName,
106522efc62Sdrh   sqlite3_intarray **ppReturn
107522efc62Sdrh );
108522efc62Sdrh 
109522efc62Sdrh /*
110522efc62Sdrh ** Bind a new array array of integers to a specific intarray object.
111522efc62Sdrh **
112522efc62Sdrh ** The array of integers bound must be unchanged for the duration of
113522efc62Sdrh ** any query against the corresponding virtual table.  If the integer
114522efc62Sdrh ** array does change or is deallocated undefined behavior will result.
115522efc62Sdrh */
116522efc62Sdrh int sqlite3_intarray_bind(
117522efc62Sdrh   sqlite3_intarray *pIntArray,   /* The intarray object to bind to */
118522efc62Sdrh   int nElements,                 /* Number of elements in the intarray */
119522efc62Sdrh   sqlite3_int64 *aElements,      /* Content of the intarray */
120522efc62Sdrh   void (*xFree)(void*)           /* How to dispose of the intarray when done */
121522efc62Sdrh );
122*6e227bf3Sdrh 
123*6e227bf3Sdrh #ifdef __cplusplus
124*6e227bf3Sdrh }  /* End of the 'extern "C"' block */
125*6e227bf3Sdrh #endif
126