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