xref: /sqlite-3.40.0/src/table.c (revision c023e03e)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
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.
10 **
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().
15 **
16 ** These routines are in a separate files so that they will not be linked
17 ** if they are not used.
18 */
19 #include <stdlib.h>
20 #include <string.h>
21 #include "sqliteInt.h"
22 
23 /*
24 ** This structure is used to pass data from sqlite_get_table() through
25 ** to the callback function is uses to build the result.
26 */
27 typedef struct TabResult {
28   char **azResult;
29   char *zErrMsg;
30   int nResult;
31   int nAlloc;
32   int nRow;
33   int nColumn;
34   int nData;
35   int rc;
36 } TabResult;
37 
38 /*
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.
42 */
43 static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
44   TabResult *p = (TabResult*)pArg;
45   int need;
46   int i;
47   char *z;
48 
49   /* Make sure there is enough space in p->azResult to hold everything
50   ** we need to remember from this invocation of the callback.
51   */
52   if( p->nRow==0 && argv!=0 ){
53     need = nCol*2;
54   }else{
55     need = nCol;
56   }
57   if( p->nData + need >= p->nAlloc ){
58     char **azNew;
59     p->nAlloc = p->nAlloc*2 + need + 1;
60     azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
61     if( azNew==0 ){
62       p->rc = SQLITE_NOMEM;
63       return 1;
64     }
65     p->azResult = azNew;
66   }
67 
68   /* If this is the first row, then generate an extra row containing
69   ** the names of all columns.
70   */
71   if( p->nRow==0 ){
72     p->nColumn = nCol;
73     for(i=0; i<nCol; i++){
74       if( colv[i]==0 ){
75         z = 0;
76       }else{
77         z = malloc( strlen(colv[i])+1 );
78         if( z==0 ){
79           p->rc = SQLITE_NOMEM;
80           return 1;
81         }
82         strcpy(z, colv[i]);
83       }
84       p->azResult[p->nData++] = z;
85     }
86   }else if( p->nColumn!=nCol ){
87     sqliteSetString(&p->zErrMsg,
88        "sqlite_get_table() called with two or more incompatible queries", 0);
89     p->rc = SQLITE_ERROR;
90     return 1;
91   }
92 
93   /* Copy over the row data
94   */
95   if( argv!=0 ){
96     for(i=0; i<nCol; i++){
97       if( argv[i]==0 ){
98         z = 0;
99       }else{
100         z = malloc( strlen(argv[i])+1 );
101         if( z==0 ){
102           p->rc = SQLITE_NOMEM;
103           return 1;
104         }
105         strcpy(z, argv[i]);
106       }
107       p->azResult[p->nData++] = z;
108     }
109     p->nRow++;
110   }
111   return 0;
112 }
113 
114 /*
115 ** Query the database.  But instead of invoking a callback for each row,
116 ** malloc() for space to hold the result and return the entire results
117 ** at the conclusion of the call.
118 **
119 ** The result that is written to ***pazResult is held in memory obtained
120 ** from malloc().  But the caller cannot free this memory directly.
121 ** Instead, the entire table should be passed to sqlite_free_table() when
122 ** the calling procedure is finished using it.
123 */
124 int sqlite_get_table(
125   sqlite *db,                 /* The database on which the SQL executes */
126   const char *zSql,           /* The SQL to be executed */
127   char ***pazResult,          /* Write the result table here */
128   int *pnRow,                 /* Write the number of rows in the result here */
129   int *pnColumn,              /* Write the number of columns of result here */
130   char **pzErrMsg             /* Write error messages here */
131 ){
132   int rc;
133   TabResult res;
134   if( pazResult==0 ){ return SQLITE_ERROR; }
135   *pazResult = 0;
136   if( pnColumn ) *pnColumn = 0;
137   if( pnRow ) *pnRow = 0;
138   res.zErrMsg = 0;
139   res.nResult = 0;
140   res.nRow = 0;
141   res.nColumn = 0;
142   res.nData = 1;
143   res.nAlloc = 20;
144   res.rc = SQLITE_OK;
145   res.azResult = malloc( sizeof(char*)*res.nAlloc );
146   if( res.azResult==0 ){
147     return SQLITE_NOMEM;
148   }
149   res.azResult[0] = 0;
150   rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
151   if( res.azResult ){
152     res.azResult[0] = (char*)res.nData;
153   }
154   if( rc==SQLITE_ABORT ){
155     sqlite_free_table(&res.azResult[1]);
156     if( res.zErrMsg ){
157       if( pzErrMsg ){
158         free(*pzErrMsg);
159         *pzErrMsg = res.zErrMsg;
160         sqliteStrRealloc(pzErrMsg);
161       }else{
162         sqliteFree(res.zErrMsg);
163       }
164     }
165     return res.rc;
166   }
167   sqliteFree(res.zErrMsg);
168   if( rc!=SQLITE_OK ){
169     sqlite_free_table(&res.azResult[1]);
170     return rc;
171   }
172   if( res.nAlloc>res.nData ){
173     char **azNew;
174     azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
175     if( azNew==0 ){
176       sqlite_free_table(&res.azResult[1]);
177       return SQLITE_NOMEM;
178     }
179     res.nAlloc = res.nData+1;
180     res.azResult = azNew;
181   }
182   *pazResult = &res.azResult[1];
183   if( pnColumn ) *pnColumn = res.nColumn;
184   if( pnRow ) *pnRow = res.nRow;
185   return rc;
186 }
187 
188 /*
189 ** This routine frees the space the sqlite_get_table() malloced.
190 */
191 void sqlite_free_table(
192   char **azResult             /* Result returned from from sqlite_get_table() */
193 ){
194   if( azResult ){
195     int i, n;
196     azResult--;
197     if( azResult==0 ) return;
198     n = (int)azResult[0];
199     for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
200     free(azResult);
201   }
202 }
203