00001 /* 00002 ** 2001 September 15 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** This file contains the sqlite3_get_table() and sqlite3_free_table() 00013 ** interface routines. These are just wrappers around the main 00014 ** interface routine of sqlite3_exec(). 00015 ** 00016 ** These routines are in a separate files so that they will not be linked 00017 ** if they are not used. 00018 ** 00019 ** $Id: table.c,v 1.36 2008/07/08 22:28:49 shane Exp $ 00020 */ 00021 #include "sqliteInt.h" 00022 #include <stdlib.h> 00023 #include <string.h> 00024 00025 #ifndef SQLITE_OMIT_GET_TABLE 00026 00027 /* 00028 ** This structure is used to pass data from sqlite3_get_table() through 00029 ** to the callback function is uses to build the result. 00030 */ 00031 typedef struct TabResult { 00032 char **azResult; 00033 char *zErrMsg; 00034 int nResult; 00035 int nAlloc; 00036 int nRow; 00037 int nColumn; 00038 int nData; 00039 int rc; 00040 } TabResult; 00041 00042 /* 00043 ** This routine is called once for each row in the result table. Its job 00044 ** is to fill in the TabResult structure appropriately, allocating new 00045 ** memory as necessary. 00046 */ 00047 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ 00048 TabResult *p = (TabResult*)pArg; 00049 int need; 00050 int i; 00051 char *z; 00052 00053 /* Make sure there is enough space in p->azResult to hold everything 00054 ** we need to remember from this invocation of the callback. 00055 */ 00056 if( p->nRow==0 && argv!=0 ){ 00057 need = nCol*2; 00058 }else{ 00059 need = nCol; 00060 } 00061 if( p->nData + need >= p->nAlloc ){ 00062 char **azNew; 00063 p->nAlloc = p->nAlloc*2 + need + 1; 00064 azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc ); 00065 if( azNew==0 ) goto malloc_failed; 00066 p->azResult = azNew; 00067 } 00068 00069 /* If this is the first row, then generate an extra row containing 00070 ** the names of all columns. 00071 */ 00072 if( p->nRow==0 ){ 00073 p->nColumn = nCol; 00074 for(i=0; i<nCol; i++){ 00075 z = sqlite3_mprintf("%s", colv[i]); 00076 if( z==0 ) goto malloc_failed; 00077 p->azResult[p->nData++] = z; 00078 } 00079 }else if( p->nColumn!=nCol ){ 00080 sqlite3_free(p->zErrMsg); 00081 p->zErrMsg = sqlite3_mprintf( 00082 "sqlite3_get_table() called with two or more incompatible queries" 00083 ); 00084 p->rc = SQLITE_ERROR; 00085 return 1; 00086 } 00087 00088 /* Copy over the row data 00089 */ 00090 if( argv!=0 ){ 00091 for(i=0; i<nCol; i++){ 00092 if( argv[i]==0 ){ 00093 z = 0; 00094 }else{ 00095 int n = strlen(argv[i])+1; 00096 z = sqlite3_malloc( n ); 00097 if( z==0 ) goto malloc_failed; 00098 memcpy(z, argv[i], n); 00099 } 00100 p->azResult[p->nData++] = z; 00101 } 00102 p->nRow++; 00103 } 00104 return 0; 00105 00106 malloc_failed: 00107 p->rc = SQLITE_NOMEM; 00108 return 1; 00109 } 00110 00111 /* 00112 ** Query the database. But instead of invoking a callback for each row, 00113 ** malloc() for space to hold the result and return the entire results 00114 ** at the conclusion of the call. 00115 ** 00116 ** The result that is written to ***pazResult is held in memory obtained 00117 ** from malloc(). But the caller cannot free this memory directly. 00118 ** Instead, the entire table should be passed to sqlite3_free_table() when 00119 ** the calling procedure is finished using it. 00120 */ 00121 int sqlite3_get_table( 00122 sqlite3 *db, /* The database on which the SQL executes */ 00123 const char *zSql, /* The SQL to be executed */ 00124 char ***pazResult, /* Write the result table here */ 00125 int *pnRow, /* Write the number of rows in the result here */ 00126 int *pnColumn, /* Write the number of columns of result here */ 00127 char **pzErrMsg /* Write error messages here */ 00128 ){ 00129 int rc; 00130 TabResult res; 00131 00132 *pazResult = 0; 00133 if( pnColumn ) *pnColumn = 0; 00134 if( pnRow ) *pnRow = 0; 00135 res.zErrMsg = 0; 00136 res.nResult = 0; 00137 res.nRow = 0; 00138 res.nColumn = 0; 00139 res.nData = 1; 00140 res.nAlloc = 20; 00141 res.rc = SQLITE_OK; 00142 res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc ); 00143 if( res.azResult==0 ){ 00144 db->errCode = SQLITE_NOMEM; 00145 return SQLITE_NOMEM; 00146 } 00147 res.azResult[0] = 0; 00148 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); 00149 assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); 00150 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); 00151 if( (rc&0xff)==SQLITE_ABORT ){ 00152 sqlite3_free_table(&res.azResult[1]); 00153 if( res.zErrMsg ){ 00154 if( pzErrMsg ){ 00155 sqlite3_free(*pzErrMsg); 00156 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); 00157 } 00158 sqlite3_free(res.zErrMsg); 00159 } 00160 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ 00161 return res.rc; 00162 } 00163 sqlite3_free(res.zErrMsg); 00164 if( rc!=SQLITE_OK ){ 00165 sqlite3_free_table(&res.azResult[1]); 00166 return rc; 00167 } 00168 if( res.nAlloc>res.nData ){ 00169 char **azNew; 00170 azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) ); 00171 if( azNew==0 ){ 00172 sqlite3_free_table(&res.azResult[1]); 00173 db->errCode = SQLITE_NOMEM; 00174 return SQLITE_NOMEM; 00175 } 00176 res.nAlloc = res.nData+1; 00177 res.azResult = azNew; 00178 } 00179 *pazResult = &res.azResult[1]; 00180 if( pnColumn ) *pnColumn = res.nColumn; 00181 if( pnRow ) *pnRow = res.nRow; 00182 return rc; 00183 } 00184 00185 /* 00186 ** This routine frees the space the sqlite3_get_table() malloced. 00187 */ 00188 void sqlite3_free_table( 00189 char **azResult /* Result returned from from sqlite3_get_table() */ 00190 ){ 00191 if( azResult ){ 00192 int i, n; 00193 azResult--; 00194 assert( azResult!=0 ); 00195 n = SQLITE_PTR_TO_INT(azResult[0]); 00196 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } 00197 sqlite3_free(azResult); 00198 } 00199 } 00200 00201 #endif /* SQLITE_OMIT_GET_TABLE */
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:56 2011 by Doxygen 1.6.1