blob: e27c7146014845f156aa5ea5e6070472355990ed [file] [log] [blame]
drhe3710332000-09-29 13:30:53 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh8c82b352000-12-10 18:23:50 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh8c82b352000-12-10 18:23:50 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh8c82b352000-12-10 18:23:50 +000010**
11*************************************************************************
drhe3710332000-09-29 13:30:53 +000012** 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().
15**
drh8c82b352000-12-10 18:23:50 +000016** These routines are in a separate files so that they will not be linked
drhe3710332000-09-29 13:30:53 +000017** if they are not used.
18*/
19#include <stdlib.h>
20#include "sqlite.h"
21
22/*
23** This structure is used to pass data from sqlite_get_table() through
24** to the callback function is uses to build the result.
25*/
26typedef struct TabResult {
27 char **azResult;
28 int nResult;
29 int nAlloc;
30 int nRow;
31 int nColumn;
32 int nData;
33 int rc;
34} TabResult;
35
36/*
37** This routine is called once for each row in the result table. Its job
38** is to fill in the TabResult structure appropriately, allocating new
39** memory as necessary.
40*/
41static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
42 TabResult *p = (TabResult*)pArg;
43 int need;
drh7c68d602000-10-11 19:28:51 +000044 int i;
45 char *z;
drhe3710332000-09-29 13:30:53 +000046
47 /* Make sure there is enough space in p->azResult to hold everything
48 ** we need to remember from this invocation of the callback.
49 */
50 if( p->nRow==0 ){
51 p->nColumn = nCol;
52 need = nCol*2;
53 }else{
54 need = nCol;
55 }
56 if( p->nData + need >= p->nAlloc ){
57 p->nAlloc = p->nAlloc*2 + need + 1;
58 p->azResult = realloc( p->azResult, sizeof(char*)*p->nAlloc );
59 if( p->azResult==0 ){
60 p->rc = SQLITE_NOMEM;
61 return 1;
62 }
63 }
64
65 /* If this is the first row, then generate an extra row containing
66 ** the names of all columns.
67 */
68 if( p->nRow==0 ){
69 for(i=0; i<nCol; i++){
70 if( colv[i]==0 ){
71 z = 0;
72 }else{
73 z = malloc( strlen(colv[i])+1 );
74 if( z==0 ){
75 p->rc = SQLITE_NOMEM;
76 return 1;
77 }
78 strcpy(z, colv[i]);
79 }
80 p->azResult[p->nData++] = z;
81 }
82 }
83
84 /* Copy over the row data
85 */
86 for(i=0; i<nCol; i++){
87 if( argv[i]==0 ){
88 z = 0;
89 }else{
90 z = malloc( strlen(argv[i])+1 );
91 if( z==0 ){
92 p->rc = SQLITE_NOMEM;
93 return 1;
94 }
95 strcpy(z, argv[i]);
96 }
97 p->azResult[p->nData++] = z;
98 }
99 p->nRow++;
100 return 0;
101}
102
103/*
104** Query the database. But instead of invoking a callback for each row,
105** malloc() for space to hold the result and return the entire results
106** at the conclusion of the call.
107**
108** The result that is written to ***pazResult is held in memory obtained
109** from malloc(). But the caller cannot free this memory directly.
110** Instead, the entire table should be passed to sqlite_free_table() when
111** the calling procedure is finished using it.
112*/
113int sqlite_get_table(
114 sqlite *db, /* The database on which the SQL executes */
115 char *zSql, /* The SQL to be executed */
116 char ***pazResult, /* Write the result table here */
117 int *pnRow, /* Write the number of rows in the result here */
118 int *pnColumn, /* Write the number of columns of result here */
119 char **pzErrMsg /* Write error messages here */
120){
121 int rc;
122 TabResult res;
123 if( pazResult==0 ){ return SQLITE_ERROR; }
124 *pazResult = 0;
125 if( pnColumn ) *pnColumn = 0;
126 if( pnRow ) *pnRow = 0;
127 res.nResult = 0;
128 res.nRow = 0;
129 res.nColumn = 0;
130 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000131 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000132 res.rc = SQLITE_OK;
133 res.azResult = malloc( sizeof(char*)*res.nAlloc );
134 if( res.azResult==0 ){
135 return SQLITE_NOMEM;
136 }
137 res.azResult[0] = 0;
138 rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
139 if( res.azResult ){
140 res.azResult[0] = (char*)res.nData;
141 }
142 if( rc==SQLITE_ABORT ){
143 sqlite_free_table(&res.azResult[1]);
144 return res.rc;
145 }
146 if( rc!=SQLITE_OK ){
147 sqlite_free_table(&res.azResult[1]);
148 return rc;
149 }
150 if( res.nAlloc>res.nData ){
151 res.azResult = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
152 if( res.azResult==0 ) return SQLITE_NOMEM;
153 }
154 *pazResult = &res.azResult[1];
155 if( pnColumn ) *pnColumn = res.nColumn;
156 if( pnRow ) *pnRow = res.nRow;
157 return rc;
158}
159
160/*
161** This routine frees the space the sqlite_get_table() malloced.
162*/
163void sqlite_free_table(
164 char **azResult /* Result returned from from sqlite_get_table() */
165){
166 if( azResult ){
167 int i, n;
168 azResult--;
169 n = (int)azResult[0];
170 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
171 free(azResult);
172 }
173}