OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains the sqlite3_get_table() and sqlite3_free_table() | 12 ** This file contains the sqlite3_get_table() and sqlite3_free_table() |
13 ** interface routines. These are just wrappers around the main | 13 ** interface routines. These are just wrappers around the main |
14 ** interface routine of sqlite3_exec(). | 14 ** interface routine of sqlite3_exec(). |
15 ** | 15 ** |
16 ** These routines are in a separate files so that they will not be linked | 16 ** These routines are in a separate files so that they will not be linked |
17 ** if they are not used. | 17 ** if they are not used. |
18 */ | 18 */ |
19 #include "sqliteInt.h" | 19 #include "sqliteInt.h" |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 | 20 |
23 #ifndef SQLITE_OMIT_GET_TABLE | 21 #ifndef SQLITE_OMIT_GET_TABLE |
24 | 22 |
25 /* | 23 /* |
26 ** This structure is used to pass data from sqlite3_get_table() through | 24 ** This structure is used to pass data from sqlite3_get_table() through |
27 ** to the callback function is uses to build the result. | 25 ** to the callback function is uses to build the result. |
28 */ | 26 */ |
29 typedef struct TabResult { | 27 typedef struct TabResult { |
30 char **azResult; /* Accumulated output */ | 28 char **azResult; /* Accumulated output */ |
31 char *zErrMsg; /* Error message text, if an error occurs */ | 29 char *zErrMsg; /* Error message text, if an error occurs */ |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 if( z==0 ) goto malloc_failed; | 92 if( z==0 ) goto malloc_failed; |
95 memcpy(z, argv[i], n); | 93 memcpy(z, argv[i], n); |
96 } | 94 } |
97 p->azResult[p->nData++] = z; | 95 p->azResult[p->nData++] = z; |
98 } | 96 } |
99 p->nRow++; | 97 p->nRow++; |
100 } | 98 } |
101 return 0; | 99 return 0; |
102 | 100 |
103 malloc_failed: | 101 malloc_failed: |
104 p->rc = SQLITE_NOMEM; | 102 p->rc = SQLITE_NOMEM_BKPT; |
105 return 1; | 103 return 1; |
106 } | 104 } |
107 | 105 |
108 /* | 106 /* |
109 ** Query the database. But instead of invoking a callback for each row, | 107 ** Query the database. But instead of invoking a callback for each row, |
110 ** malloc() for space to hold the result and return the entire results | 108 ** malloc() for space to hold the result and return the entire results |
111 ** at the conclusion of the call. | 109 ** at the conclusion of the call. |
112 ** | 110 ** |
113 ** The result that is written to ***pazResult is held in memory obtained | 111 ** The result that is written to ***pazResult is held in memory obtained |
114 ** from malloc(). But the caller cannot free this memory directly. | 112 ** from malloc(). But the caller cannot free this memory directly. |
(...skipping 20 matching lines...) Expand all Loading... |
135 if( pzErrMsg ) *pzErrMsg = 0; | 133 if( pzErrMsg ) *pzErrMsg = 0; |
136 res.zErrMsg = 0; | 134 res.zErrMsg = 0; |
137 res.nRow = 0; | 135 res.nRow = 0; |
138 res.nColumn = 0; | 136 res.nColumn = 0; |
139 res.nData = 1; | 137 res.nData = 1; |
140 res.nAlloc = 20; | 138 res.nAlloc = 20; |
141 res.rc = SQLITE_OK; | 139 res.rc = SQLITE_OK; |
142 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc ); | 140 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc ); |
143 if( res.azResult==0 ){ | 141 if( res.azResult==0 ){ |
144 db->errCode = SQLITE_NOMEM; | 142 db->errCode = SQLITE_NOMEM; |
145 return SQLITE_NOMEM; | 143 return SQLITE_NOMEM_BKPT; |
146 } | 144 } |
147 res.azResult[0] = 0; | 145 res.azResult[0] = 0; |
148 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); | 146 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); |
149 assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); | 147 assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); |
150 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); | 148 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); |
151 if( (rc&0xff)==SQLITE_ABORT ){ | 149 if( (rc&0xff)==SQLITE_ABORT ){ |
152 sqlite3_free_table(&res.azResult[1]); | 150 sqlite3_free_table(&res.azResult[1]); |
153 if( res.zErrMsg ){ | 151 if( res.zErrMsg ){ |
154 if( pzErrMsg ){ | 152 if( pzErrMsg ){ |
155 sqlite3_free(*pzErrMsg); | 153 sqlite3_free(*pzErrMsg); |
156 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); | 154 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); |
157 } | 155 } |
158 sqlite3_free(res.zErrMsg); | 156 sqlite3_free(res.zErrMsg); |
159 } | 157 } |
160 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ | 158 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ |
161 return res.rc; | 159 return res.rc; |
162 } | 160 } |
163 sqlite3_free(res.zErrMsg); | 161 sqlite3_free(res.zErrMsg); |
164 if( rc!=SQLITE_OK ){ | 162 if( rc!=SQLITE_OK ){ |
165 sqlite3_free_table(&res.azResult[1]); | 163 sqlite3_free_table(&res.azResult[1]); |
166 return rc; | 164 return rc; |
167 } | 165 } |
168 if( res.nAlloc>res.nData ){ | 166 if( res.nAlloc>res.nData ){ |
169 char **azNew; | 167 char **azNew; |
170 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); | 168 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); |
171 if( azNew==0 ){ | 169 if( azNew==0 ){ |
172 sqlite3_free_table(&res.azResult[1]); | 170 sqlite3_free_table(&res.azResult[1]); |
173 db->errCode = SQLITE_NOMEM; | 171 db->errCode = SQLITE_NOMEM; |
174 return SQLITE_NOMEM; | 172 return SQLITE_NOMEM_BKPT; |
175 } | 173 } |
176 res.azResult = azNew; | 174 res.azResult = azNew; |
177 } | 175 } |
178 *pazResult = &res.azResult[1]; | 176 *pazResult = &res.azResult[1]; |
179 if( pnColumn ) *pnColumn = res.nColumn; | 177 if( pnColumn ) *pnColumn = res.nColumn; |
180 if( pnRow ) *pnRow = res.nRow; | 178 if( pnRow ) *pnRow = res.nRow; |
181 return rc; | 179 return rc; |
182 } | 180 } |
183 | 181 |
184 /* | 182 /* |
185 ** This routine frees the space the sqlite3_get_table() malloced. | 183 ** This routine frees the space the sqlite3_get_table() malloced. |
186 */ | 184 */ |
187 void sqlite3_free_table( | 185 void sqlite3_free_table( |
188 char **azResult /* Result returned from sqlite3_get_table() */ | 186 char **azResult /* Result returned from sqlite3_get_table() */ |
189 ){ | 187 ){ |
190 if( azResult ){ | 188 if( azResult ){ |
191 int i, n; | 189 int i, n; |
192 azResult--; | 190 azResult--; |
193 assert( azResult!=0 ); | 191 assert( azResult!=0 ); |
194 n = SQLITE_PTR_TO_INT(azResult[0]); | 192 n = SQLITE_PTR_TO_INT(azResult[0]); |
195 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } | 193 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } |
196 sqlite3_free(azResult); | 194 sqlite3_free(azResult); |
197 } | 195 } |
198 } | 196 } |
199 | 197 |
200 #endif /* SQLITE_OMIT_GET_TABLE */ | 198 #endif /* SQLITE_OMIT_GET_TABLE */ |
OLD | NEW |