4
** The author disclaims copyright to this source code. In place of
5
** a legal notice, here is a blessing:
7
** May you do good and not evil.
8
** May you find forgiveness for yourself and forgive others.
9
** May you share freely, never taking more than you give.
11
*************************************************************************
12
** This file contains the sqlite_get_table() and sqlite_free_table()
13
** interface routines. These are just wrappers around the main
14
** interface routine of sqlite_exec().
16
** These routines are in a separate files so that they will not be linked
17
** if they are not used.
21
#include "sqliteInt.h"
24
** This structure is used to pass data from sqlite_get_table() through
25
** to the callback function is uses to build the result.
27
typedef struct TabResult {
39
** This routine is called once for each row in the result table. Its job
40
** is to fill in the TabResult structure appropriately, allocating new
41
** memory as necessary.
43
static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
44
TabResult *p = (TabResult*)pArg;
49
/* Make sure there is enough space in p->azResult to hold everything
50
** we need to remember from this invocation of the callback.
52
if( p->nRow==0 && argv!=0 ){
57
if( p->nData + need >= p->nAlloc ){
59
p->nAlloc = p->nAlloc*2 + need + 1;
60
azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
68
/* If this is the first row, then generate an extra row containing
69
** the names of all columns.
73
for(i=0; i<nCol; i++){
77
z = malloc( strlen(colv[i])+1 );
84
p->azResult[p->nData++] = z;
86
}else if( p->nColumn!=nCol ){
87
sqliteSetString(&p->zErrMsg,
88
"sqlite_get_table() called with two or more incompatible queries",
94
/* Copy over the row data
97
for(i=0; i<nCol; i++){
101
z = malloc( strlen(argv[i])+1 );
103
p->rc = SQLITE_NOMEM;
108
p->azResult[p->nData++] = z;
116
** Query the database. But instead of invoking a callback for each row,
117
** malloc() for space to hold the result and return the entire results
118
** at the conclusion of the call.
120
** The result that is written to ***pazResult is held in memory obtained
121
** from malloc(). But the caller cannot free this memory directly.
122
** Instead, the entire table should be passed to sqlite_free_table() when
123
** the calling procedure is finished using it.
125
int sqlite_get_table(
126
sqlite *db, /* The database on which the SQL executes */
127
const char *zSql, /* The SQL to be executed */
128
char ***pazResult, /* Write the result table here */
129
int *pnRow, /* Write the number of rows in the result here */
130
int *pnColumn, /* Write the number of columns of result here */
131
char **pzErrMsg /* Write error messages here */
135
if( pazResult==0 ){ return SQLITE_ERROR; }
137
if( pnColumn ) *pnColumn = 0;
138
if( pnRow ) *pnRow = 0;
146
res.azResult = malloc( sizeof(char*)*res.nAlloc );
147
if( res.azResult==0 ){
151
rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
153
res.azResult[0] = (char*)res.nData;
155
if( rc==SQLITE_ABORT ){
156
sqlite_free_table(&res.azResult[1]);
160
*pzErrMsg = res.zErrMsg;
161
sqliteStrRealloc(pzErrMsg);
163
sqliteFree(res.zErrMsg);
168
sqliteFree(res.zErrMsg);
170
sqlite_free_table(&res.azResult[1]);
173
if( res.nAlloc>res.nData ){
175
azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
177
sqlite_free_table(&res.azResult[1]);
180
res.nAlloc = res.nData+1;
181
res.azResult = azNew;
183
*pazResult = &res.azResult[1];
184
if( pnColumn ) *pnColumn = res.nColumn;
185
if( pnRow ) *pnRow = res.nRow;
190
** This routine frees the space the sqlite_get_table() malloced.
192
void sqlite_free_table(
193
char **azResult /* Result returned from from sqlite_get_table() */
198
if( azResult==0 ) return;
199
n = (int)azResult[0];
200
for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }