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 ** |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 #ifndef SQLITE_OMIT_GET_TABLE | 23 #ifndef SQLITE_OMIT_GET_TABLE |
24 | 24 |
25 /* | 25 /* |
26 ** This structure is used to pass data from sqlite3_get_table() through | 26 ** This structure is used to pass data from sqlite3_get_table() through |
27 ** to the callback function is uses to build the result. | 27 ** to the callback function is uses to build the result. |
28 */ | 28 */ |
29 typedef struct TabResult { | 29 typedef struct TabResult { |
30 char **azResult; /* Accumulated output */ | 30 char **azResult; /* Accumulated output */ |
31 char *zErrMsg; /* Error message text, if an error occurs */ | 31 char *zErrMsg; /* Error message text, if an error occurs */ |
32 int nAlloc; /* Slots allocated for azResult[] */ | 32 u32 nAlloc; /* Slots allocated for azResult[] */ |
33 int nRow; /* Number of rows in the result */ | 33 u32 nRow; /* Number of rows in the result */ |
34 int nColumn; /* Number of columns in the result */ | 34 u32 nColumn; /* Number of columns in the result */ |
35 int nData; /* Slots used in azResult[]. (nRow+1)*nColumn */ | 35 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */ |
36 int rc; /* Return code from sqlite3_exec() */ | 36 int rc; /* Return code from sqlite3_exec() */ |
37 } TabResult; | 37 } TabResult; |
38 | 38 |
39 /* | 39 /* |
40 ** This routine is called once for each row in the result table. Its job | 40 ** This routine is called once for each row in the result table. Its job |
41 ** is to fill in the TabResult structure appropriately, allocating new | 41 ** is to fill in the TabResult structure appropriately, allocating new |
42 ** memory as necessary. | 42 ** memory as necessary. |
43 */ | 43 */ |
44 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ | 44 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ |
45 TabResult *p = (TabResult*)pArg; /* Result accumulator */ | 45 TabResult *p = (TabResult*)pArg; /* Result accumulator */ |
46 int need; /* Slots needed in p->azResult[] */ | 46 int need; /* Slots needed in p->azResult[] */ |
47 int i; /* Loop counter */ | 47 int i; /* Loop counter */ |
48 char *z; /* A single column of result */ | 48 char *z; /* A single column of result */ |
49 | 49 |
50 /* Make sure there is enough space in p->azResult to hold everything | 50 /* Make sure there is enough space in p->azResult to hold everything |
51 ** we need to remember from this invocation of the callback. | 51 ** we need to remember from this invocation of the callback. |
52 */ | 52 */ |
53 if( p->nRow==0 && argv!=0 ){ | 53 if( p->nRow==0 && argv!=0 ){ |
54 need = nCol*2; | 54 need = nCol*2; |
55 }else{ | 55 }else{ |
56 need = nCol; | 56 need = nCol; |
57 } | 57 } |
58 if( p->nData + need > p->nAlloc ){ | 58 if( p->nData + need > p->nAlloc ){ |
59 char **azNew; | 59 char **azNew; |
60 p->nAlloc = p->nAlloc*2 + need; | 60 p->nAlloc = p->nAlloc*2 + need; |
61 azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc ); | 61 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); |
62 if( azNew==0 ) goto malloc_failed; | 62 if( azNew==0 ) goto malloc_failed; |
63 p->azResult = azNew; | 63 p->azResult = azNew; |
64 } | 64 } |
65 | 65 |
66 /* If this is the first row, then generate an extra row containing | 66 /* If this is the first row, then generate an extra row containing |
67 ** the names of all columns. | 67 ** the names of all columns. |
68 */ | 68 */ |
69 if( p->nRow==0 ){ | 69 if( p->nRow==0 ){ |
70 p->nColumn = nCol; | 70 p->nColumn = nCol; |
71 for(i=0; i<nCol; i++){ | 71 for(i=0; i<nCol; i++){ |
72 z = sqlite3_mprintf("%s", colv[i]); | 72 z = sqlite3_mprintf("%s", colv[i]); |
73 if( z==0 ) goto malloc_failed; | 73 if( z==0 ) goto malloc_failed; |
74 p->azResult[p->nData++] = z; | 74 p->azResult[p->nData++] = z; |
75 } | 75 } |
76 }else if( p->nColumn!=nCol ){ | 76 }else if( (int)p->nColumn!=nCol ){ |
77 sqlite3_free(p->zErrMsg); | 77 sqlite3_free(p->zErrMsg); |
78 p->zErrMsg = sqlite3_mprintf( | 78 p->zErrMsg = sqlite3_mprintf( |
79 "sqlite3_get_table() called with two or more incompatible queries" | 79 "sqlite3_get_table() called with two or more incompatible queries" |
80 ); | 80 ); |
81 p->rc = SQLITE_ERROR; | 81 p->rc = SQLITE_ERROR; |
82 return 1; | 82 return 1; |
83 } | 83 } |
84 | 84 |
85 /* Copy over the row data | 85 /* Copy over the row data |
86 */ | 86 */ |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 *pazResult = &res.azResult[1]; | 175 *pazResult = &res.azResult[1]; |
176 if( pnColumn ) *pnColumn = res.nColumn; | 176 if( pnColumn ) *pnColumn = res.nColumn; |
177 if( pnRow ) *pnRow = res.nRow; | 177 if( pnRow ) *pnRow = res.nRow; |
178 return rc; | 178 return rc; |
179 } | 179 } |
180 | 180 |
181 /* | 181 /* |
182 ** This routine frees the space the sqlite3_get_table() malloced. | 182 ** This routine frees the space the sqlite3_get_table() malloced. |
183 */ | 183 */ |
184 void sqlite3_free_table( | 184 void sqlite3_free_table( |
185 char **azResult /* Result returned from from sqlite3_get_table() */ | 185 char **azResult /* Result returned from sqlite3_get_table() */ |
186 ){ | 186 ){ |
187 if( azResult ){ | 187 if( azResult ){ |
188 int i, n; | 188 int i, n; |
189 azResult--; | 189 azResult--; |
190 assert( azResult!=0 ); | 190 assert( azResult!=0 ); |
191 n = SQLITE_PTR_TO_INT(azResult[0]); | 191 n = SQLITE_PTR_TO_INT(azResult[0]); |
192 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } | 192 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } |
193 sqlite3_free(azResult); | 193 sqlite3_free(azResult); |
194 } | 194 } |
195 } | 195 } |
196 | 196 |
197 #endif /* SQLITE_OMIT_GET_TABLE */ | 197 #endif /* SQLITE_OMIT_GET_TABLE */ |
OLD | NEW |