xref: /sqlite-3.40.0/src/table.c (revision 5d00d0a8)
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 ** $Id: table.c,v 1.40 2009/04/10 14:28:00 drh Exp $
20 */
21 #include "sqliteInt.h"
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #ifndef SQLITE_OMIT_GET_TABLE
26 
27 /*
28 ** This structure is used to pass data from sqlite3_get_table() through
29 ** to the callback function is uses to build the result.
30 */
31 typedef struct TabResult {
32   char **azResult;   /* Accumulated output */
33   char *zErrMsg;     /* Error message text, if an error occurs */
34   int nAlloc;        /* Slots allocated for azResult[] */
35   int nRow;          /* Number of rows in the result */
36   int nColumn;       /* Number of columns in the result */
37   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
38   int rc;            /* Return code from sqlite3_exec() */
39 } TabResult;
40 
41 /*
42 ** This routine is called once for each row in the result table.  Its job
43 ** is to fill in the TabResult structure appropriately, allocating new
44 ** memory as necessary.
45 */
46 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
47   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
48   int need;                         /* Slots needed in p->azResult[] */
49   int i;                            /* Loop counter */
50   char *z;                          /* A single column of result */
51 
52   /* Make sure there is enough space in p->azResult to hold everything
53   ** we need to remember from this invocation of the callback.
54   */
55   if( p->nRow==0 && argv!=0 ){
56     need = nCol*2;
57   }else{
58     need = nCol;
59   }
60   if( p->nData + need > p->nAlloc ){
61     char **azNew;
62     p->nAlloc = p->nAlloc*2 + need;
63     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
64     if( azNew==0 ) goto malloc_failed;
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       z = sqlite3_mprintf("%s", colv[i]);
75       if( z==0 ) goto malloc_failed;
76       p->azResult[p->nData++] = z;
77     }
78   }else if( p->nColumn!=nCol ){
79     sqlite3_free(p->zErrMsg);
80     p->zErrMsg = sqlite3_mprintf(
81        "sqlite3_get_table() called with two or more incompatible queries"
82     );
83     p->rc = SQLITE_ERROR;
84     return 1;
85   }
86 
87   /* Copy over the row data
88   */
89   if( argv!=0 ){
90     for(i=0; i<nCol; i++){
91       if( argv[i]==0 ){
92         z = 0;
93       }else{
94         int n = sqlite3Strlen30(argv[i])+1;
95         z = sqlite3_malloc( n );
96         if( z==0 ) goto malloc_failed;
97         memcpy(z, argv[i], n);
98       }
99       p->azResult[p->nData++] = z;
100     }
101     p->nRow++;
102   }
103   return 0;
104 
105 malloc_failed:
106   p->rc = SQLITE_NOMEM;
107   return 1;
108 }
109 
110 /*
111 ** Query the database.  But instead of invoking a callback for each row,
112 ** malloc() for space to hold the result and return the entire results
113 ** at the conclusion of the call.
114 **
115 ** The result that is written to ***pazResult is held in memory obtained
116 ** from malloc().  But the caller cannot free this memory directly.
117 ** Instead, the entire table should be passed to sqlite3_free_table() when
118 ** the calling procedure is finished using it.
119 */
120 int sqlite3_get_table(
121   sqlite3 *db,                /* The database on which the SQL executes */
122   const char *zSql,           /* The SQL to be executed */
123   char ***pazResult,          /* Write the result table here */
124   int *pnRow,                 /* Write the number of rows in the result here */
125   int *pnColumn,              /* Write the number of columns of result here */
126   char **pzErrMsg             /* Write error messages here */
127 ){
128   int rc;
129   TabResult res;
130 
131   *pazResult = 0;
132   if( pnColumn ) *pnColumn = 0;
133   if( pnRow ) *pnRow = 0;
134   if( pzErrMsg ) *pzErrMsg = 0;
135   res.zErrMsg = 0;
136   res.nRow = 0;
137   res.nColumn = 0;
138   res.nData = 1;
139   res.nAlloc = 20;
140   res.rc = SQLITE_OK;
141   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
142   if( res.azResult==0 ){
143      db->errCode = SQLITE_NOMEM;
144      return SQLITE_NOMEM;
145   }
146   res.azResult[0] = 0;
147   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
148   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
149   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
150   if( (rc&0xff)==SQLITE_ABORT ){
151     sqlite3_free_table(&res.azResult[1]);
152     if( res.zErrMsg ){
153       if( pzErrMsg ){
154         sqlite3_free(*pzErrMsg);
155         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
156       }
157       sqlite3_free(res.zErrMsg);
158     }
159     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
160     return res.rc;
161   }
162   sqlite3_free(res.zErrMsg);
163   if( rc!=SQLITE_OK ){
164     sqlite3_free_table(&res.azResult[1]);
165     return rc;
166   }
167   if( res.nAlloc>res.nData ){
168     char **azNew;
169     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
170     if( azNew==0 ){
171       sqlite3_free_table(&res.azResult[1]);
172       db->errCode = SQLITE_NOMEM;
173       return SQLITE_NOMEM;
174     }
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     int i, n;
191     azResult--;
192     assert( azResult!=0 );
193     n = SQLITE_PTR_TO_INT(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