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