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 0 : static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
46 0 : 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 0 : if( p->nRow==0 && argv!=0 ){
55 0 : need = nCol*2;
56 : }else{
57 0 : need = nCol;
58 : }
59 0 : if( p->nData + need >= p->nAlloc ){
60 : char **azNew;
61 0 : p->nAlloc = p->nAlloc*2 + need + 1;
62 0 : azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
63 0 : if( azNew==0 ) goto malloc_failed;
64 0 : 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 0 : if( p->nRow==0 ){
71 0 : p->nColumn = nCol;
72 0 : for(i=0; i<nCol; i++){
73 0 : if( colv[i]==0 ){
74 0 : z = sqlite3_mprintf("");
75 : }else{
76 0 : z = sqlite3_mprintf("%s", colv[i]);
77 : }
78 0 : p->azResult[p->nData++] = z;
79 : }
80 0 : }else if( p->nColumn!=nCol ){
81 0 : sqlite3SetString(&p->zErrMsg,
82 : "sqlite3_get_table() called with two or more incompatible queries",
83 : (char*)0);
84 0 : p->rc = SQLITE_ERROR;
85 0 : return 1;
86 : }
87 :
88 : /* Copy over the row data
89 : */
90 0 : if( argv!=0 ){
91 0 : for(i=0; i<nCol; i++){
92 0 : if( argv[i]==0 ){
93 0 : z = 0;
94 : }else{
95 0 : z = sqlite3_malloc( strlen(argv[i])+1 );
96 0 : if( z==0 ) goto malloc_failed;
97 0 : strcpy(z, argv[i]);
98 : }
99 0 : p->azResult[p->nData++] = z;
100 : }
101 0 : p->nRow++;
102 : }
103 0 : return 0;
104 :
105 0 : malloc_failed:
106 0 : p->rc = SQLITE_NOMEM;
107 0 : 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 0 : ){
128 : int rc;
129 : TabResult res;
130 0 : if( pazResult==0 ){ return SQLITE_ERROR; }
131 0 : *pazResult = 0;
132 0 : if( pnColumn ) *pnColumn = 0;
133 0 : if( pnRow ) *pnRow = 0;
134 0 : res.zErrMsg = 0;
135 0 : res.nResult = 0;
136 0 : res.nRow = 0;
137 0 : res.nColumn = 0;
138 0 : res.nData = 1;
139 0 : res.nAlloc = 20;
140 0 : res.rc = SQLITE_OK;
141 0 : res.azResult = sqlite3_malloc( sizeof(char*)*res.nAlloc );
142 0 : if( res.azResult==0 ) return SQLITE_NOMEM;
143 0 : res.azResult[0] = 0;
144 0 : rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
145 0 : if( res.azResult ){
146 : assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
147 0 : res.azResult[0] = (char*)res.nData;
148 : }
149 0 : if( (rc&0xff)==SQLITE_ABORT ){
150 0 : sqlite3_free_table(&res.azResult[1]);
151 0 : if( res.zErrMsg ){
152 0 : if( pzErrMsg ){
153 0 : sqlite3_free(*pzErrMsg);
154 0 : *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
155 : }
156 0 : sqliteFree(res.zErrMsg);
157 : }
158 0 : db->errCode = res.rc;
159 0 : return res.rc & db->errMask;
160 : }
161 0 : sqliteFree(res.zErrMsg);
162 0 : if( rc!=SQLITE_OK ){
163 0 : sqlite3_free_table(&res.azResult[1]);
164 0 : return rc & db->errMask;
165 : }
166 0 : if( res.nAlloc>res.nData ){
167 : char **azNew;
168 0 : azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) );
169 0 : if( azNew==0 ){
170 0 : sqlite3_free_table(&res.azResult[1]);
171 0 : return SQLITE_NOMEM;
172 : }
173 0 : res.nAlloc = res.nData+1;
174 0 : res.azResult = azNew;
175 : }
176 0 : *pazResult = &res.azResult[1];
177 0 : if( pnColumn ) *pnColumn = res.nColumn;
178 0 : if( pnRow ) *pnRow = res.nRow;
179 0 : return rc & db->errMask;
180 : }
181 :
182 : /*
183 : ** This routine frees the space the sqlite3_get_table() malloced.
184 : */
185 : void sqlite3_free_table(
186 : char **azResult /* Result returned from from sqlite3_get_table() */
187 0 : ){
188 0 : if( azResult ){
189 : int i, n;
190 0 : azResult--;
191 0 : if( azResult==0 ) return;
192 0 : n = (int)azResult[0];
193 0 : for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
194 0 : sqlite3_free(azResult);
195 : }
196 : }
197 :
198 : #endif /* SQLITE_OMIT_GET_TABLE */
|