1e371033dSdrh /* 2b19a2bc6Sdrh ** 2001 September 15 38c82b350Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 68c82b350Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 108c82b350Sdrh ** 118c82b350Sdrh ************************************************************************* 126f8a503dSdanielk1977 ** This file contains the sqlite3_get_table() and sqlite3_free_table() 13e371033dSdrh ** interface routines. These are just wrappers around the main 146f8a503dSdanielk1977 ** interface routine of sqlite3_exec(). 15e371033dSdrh ** 168c82b350Sdrh ** These routines are in a separate files so that they will not be linked 17e371033dSdrh ** if they are not used. 18e371033dSdrh */ 1922465cedSdrh #include "sqliteInt.h" 20e371033dSdrh #include <stdlib.h> 218e0a2f90Sdrh #include <string.h> 22e371033dSdrh 23ff55c358Sdrh #ifndef SQLITE_OMIT_GET_TABLE 24ff55c358Sdrh 25e371033dSdrh /* 266f8a503dSdanielk1977 ** This structure is used to pass data from sqlite3_get_table() through 27e371033dSdrh ** to the callback function is uses to build the result. 28e371033dSdrh */ 29e371033dSdrh typedef struct TabResult { 3073d34e92Sdrh char **azResult; /* Accumulated output */ 3173d34e92Sdrh char *zErrMsg; /* Error message text, if an error occurs */ 32da4ca9d1Sdrh u32 nAlloc; /* Slots allocated for azResult[] */ 33da4ca9d1Sdrh u32 nRow; /* Number of rows in the result */ 34da4ca9d1Sdrh u32 nColumn; /* Number of columns in the result */ 35da4ca9d1Sdrh u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */ 3673d34e92Sdrh int rc; /* Return code from sqlite3_exec() */ 37e371033dSdrh } TabResult; 38e371033dSdrh 39e371033dSdrh /* 40e371033dSdrh ** This routine is called once for each row in the result table. Its job 41e371033dSdrh ** is to fill in the TabResult structure appropriately, allocating new 42e371033dSdrh ** memory as necessary. 43e371033dSdrh */ 446f8a503dSdanielk1977 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ 4573d34e92Sdrh TabResult *p = (TabResult*)pArg; /* Result accumulator */ 4673d34e92Sdrh int need; /* Slots needed in p->azResult[] */ 4773d34e92Sdrh int i; /* Loop counter */ 4873d34e92Sdrh char *z; /* A single column of result */ 49e371033dSdrh 50e371033dSdrh /* Make sure there is enough space in p->azResult to hold everything 51e371033dSdrh ** we need to remember from this invocation of the callback. 52e371033dSdrh */ 536a535340Sdrh if( p->nRow==0 && argv!=0 ){ 54e371033dSdrh need = nCol*2; 55e371033dSdrh }else{ 56e371033dSdrh need = nCol; 57e371033dSdrh } 5873d34e92Sdrh if( p->nData + need > p->nAlloc ){ 596d4abfbeSdrh char **azNew; 6073d34e92Sdrh p->nAlloc = p->nAlloc*2 + need; 61da4ca9d1Sdrh azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); 62779c6a06Sdrh if( azNew==0 ) goto malloc_failed; 636d4abfbeSdrh p->azResult = azNew; 64e371033dSdrh } 65e371033dSdrh 66e371033dSdrh /* If this is the first row, then generate an extra row containing 67e371033dSdrh ** the names of all columns. 68e371033dSdrh */ 69e371033dSdrh if( p->nRow==0 ){ 7001a34661Sdrh p->nColumn = nCol; 71e371033dSdrh for(i=0; i<nCol; i++){ 722cc55698Sdrh z = sqlite3_mprintf("%s", colv[i]); 7301495b99Sdrh if( z==0 ) goto malloc_failed; 74e371033dSdrh p->azResult[p->nData++] = z; 75e371033dSdrh } 763329a63aSdrh }else if( (int)p->nColumn!=nCol ){ 7701495b99Sdrh sqlite3_free(p->zErrMsg); 7801495b99Sdrh p->zErrMsg = sqlite3_mprintf( 7901495b99Sdrh "sqlite3_get_table() called with two or more incompatible queries" 8001495b99Sdrh ); 81f1a7a139Sdrh p->rc = SQLITE_ERROR; 82f1a7a139Sdrh return 1; 83e371033dSdrh } 84e371033dSdrh 85e371033dSdrh /* Copy over the row data 86e371033dSdrh */ 876a535340Sdrh if( argv!=0 ){ 88e371033dSdrh for(i=0; i<nCol; i++){ 89e371033dSdrh if( argv[i]==0 ){ 90e371033dSdrh z = 0; 91e371033dSdrh }else{ 92ea678832Sdrh int n = sqlite3Strlen30(argv[i])+1; 93*f3cdcdccSdrh z = sqlite3_malloc64( n ); 94779c6a06Sdrh if( z==0 ) goto malloc_failed; 955bb3eb9bSdrh memcpy(z, argv[i], n); 96e371033dSdrh } 97e371033dSdrh p->azResult[p->nData++] = z; 98e371033dSdrh } 99e371033dSdrh p->nRow++; 1006a535340Sdrh } 101e371033dSdrh return 0; 102779c6a06Sdrh 103779c6a06Sdrh malloc_failed: 104779c6a06Sdrh p->rc = SQLITE_NOMEM; 105779c6a06Sdrh return 1; 106e371033dSdrh } 107e371033dSdrh 108e371033dSdrh /* 109e371033dSdrh ** Query the database. But instead of invoking a callback for each row, 110e371033dSdrh ** malloc() for space to hold the result and return the entire results 111e371033dSdrh ** at the conclusion of the call. 112e371033dSdrh ** 113e371033dSdrh ** The result that is written to ***pazResult is held in memory obtained 114e371033dSdrh ** from malloc(). But the caller cannot free this memory directly. 1156f8a503dSdanielk1977 ** Instead, the entire table should be passed to sqlite3_free_table() when 116e371033dSdrh ** the calling procedure is finished using it. 117e371033dSdrh */ 1186f8a503dSdanielk1977 int sqlite3_get_table( 1199bb575fdSdrh sqlite3 *db, /* The database on which the SQL executes */ 1209f71c2e0Sdrh const char *zSql, /* The SQL to be executed */ 121e371033dSdrh char ***pazResult, /* Write the result table here */ 122e371033dSdrh int *pnRow, /* Write the number of rows in the result here */ 123e371033dSdrh int *pnColumn, /* Write the number of columns of result here */ 124e371033dSdrh char **pzErrMsg /* Write error messages here */ 125e371033dSdrh ){ 126e371033dSdrh int rc; 127e371033dSdrh TabResult res; 128d2b3e23bSdrh 1299ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 130cd54bab6Smistachkin if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT; 1319ca95730Sdrh #endif 132e371033dSdrh *pazResult = 0; 133e371033dSdrh if( pnColumn ) *pnColumn = 0; 134e371033dSdrh if( pnRow ) *pnRow = 0; 135770b3cb7Sdrh if( pzErrMsg ) *pzErrMsg = 0; 136f1a7a139Sdrh res.zErrMsg = 0; 137e371033dSdrh res.nRow = 0; 138e371033dSdrh res.nColumn = 0; 139e371033dSdrh res.nData = 1; 140daffd0e5Sdrh res.nAlloc = 20; 141e371033dSdrh res.rc = SQLITE_OK; 142*f3cdcdccSdrh res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc ); 14301495b99Sdrh if( res.azResult==0 ){ 14401495b99Sdrh db->errCode = SQLITE_NOMEM; 14501495b99Sdrh return SQLITE_NOMEM; 14601495b99Sdrh } 147e371033dSdrh res.azResult[0] = 0; 1486f8a503dSdanielk1977 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); 14978aecb72Sdrh assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); 1501fc4129dSshane res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); 1514ac285a1Sdrh if( (rc&0xff)==SQLITE_ABORT ){ 1526f8a503dSdanielk1977 sqlite3_free_table(&res.azResult[1]); 153f1a7a139Sdrh if( res.zErrMsg ){ 154f1a7a139Sdrh if( pzErrMsg ){ 15528dd479cSdrh sqlite3_free(*pzErrMsg); 156ae29ffbeSdrh *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); 157f1a7a139Sdrh } 1581e536953Sdanielk1977 sqlite3_free(res.zErrMsg); 159f1a7a139Sdrh } 16001495b99Sdrh db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ 16126783a58Sdanielk1977 return res.rc; 162e371033dSdrh } 1631e536953Sdanielk1977 sqlite3_free(res.zErrMsg); 164e371033dSdrh if( rc!=SQLITE_OK ){ 1656f8a503dSdanielk1977 sqlite3_free_table(&res.azResult[1]); 16626783a58Sdanielk1977 return rc; 167e371033dSdrh } 168e371033dSdrh if( res.nAlloc>res.nData ){ 1696d4abfbeSdrh char **azNew; 170*f3cdcdccSdrh azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); 17193352470Sdrh if( azNew==0 ){ 1726f8a503dSdanielk1977 sqlite3_free_table(&res.azResult[1]); 17301495b99Sdrh db->errCode = SQLITE_NOMEM; 1746d4abfbeSdrh return SQLITE_NOMEM; 1756d4abfbeSdrh } 1766d4abfbeSdrh res.azResult = azNew; 177e371033dSdrh } 178e371033dSdrh *pazResult = &res.azResult[1]; 179e371033dSdrh if( pnColumn ) *pnColumn = res.nColumn; 180e371033dSdrh if( pnRow ) *pnRow = res.nRow; 18126783a58Sdanielk1977 return rc; 182e371033dSdrh } 183e371033dSdrh 184e371033dSdrh /* 1856f8a503dSdanielk1977 ** This routine frees the space the sqlite3_get_table() malloced. 186e371033dSdrh */ 1876f8a503dSdanielk1977 void sqlite3_free_table( 18860ec914cSpeter.d.reid char **azResult /* Result returned from sqlite3_get_table() */ 189e371033dSdrh ){ 190e371033dSdrh if( azResult ){ 1917209c697Sdrh int i, n; 192e371033dSdrh azResult--; 193d2b3e23bSdrh assert( azResult!=0 ); 1941fc4129dSshane n = SQLITE_PTR_TO_INT(azResult[0]); 19528dd479cSdrh for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } 19628dd479cSdrh sqlite3_free(azResult); 197e371033dSdrh } 198e371033dSdrh } 199ff55c358Sdrh 200ec7429aeSdrh #endif /* SQLITE_OMIT_GET_TABLE */ 201