xref: /sqlite-3.40.0/src/table.c (revision d924e7bc)
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 
21ff55c358Sdrh #ifndef SQLITE_OMIT_GET_TABLE
22ff55c358Sdrh 
23e371033dSdrh /*
246f8a503dSdanielk1977 ** This structure is used to pass data from sqlite3_get_table() through
25e371033dSdrh ** to the callback function is uses to build the result.
26e371033dSdrh */
27e371033dSdrh typedef struct TabResult {
2873d34e92Sdrh   char **azResult;   /* Accumulated output */
2973d34e92Sdrh   char *zErrMsg;     /* Error message text, if an error occurs */
30da4ca9d1Sdrh   u32 nAlloc;        /* Slots allocated for azResult[] */
31da4ca9d1Sdrh   u32 nRow;          /* Number of rows in the result */
32da4ca9d1Sdrh   u32 nColumn;       /* Number of columns in the result */
33da4ca9d1Sdrh   u32 nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
3473d34e92Sdrh   int rc;            /* Return code from sqlite3_exec() */
35e371033dSdrh } TabResult;
36e371033dSdrh 
37e371033dSdrh /*
38e371033dSdrh ** This routine is called once for each row in the result table.  Its job
39e371033dSdrh ** is to fill in the TabResult structure appropriately, allocating new
40e371033dSdrh ** memory as necessary.
41e371033dSdrh */
sqlite3_get_table_cb(void * pArg,int nCol,char ** argv,char ** colv)426f8a503dSdanielk1977 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
4373d34e92Sdrh   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
4473d34e92Sdrh   int need;                         /* Slots needed in p->azResult[] */
4573d34e92Sdrh   int i;                            /* Loop counter */
4673d34e92Sdrh   char *z;                          /* A single column of result */
47e371033dSdrh 
48e371033dSdrh   /* Make sure there is enough space in p->azResult to hold everything
49e371033dSdrh   ** we need to remember from this invocation of the callback.
50e371033dSdrh   */
516a535340Sdrh   if( p->nRow==0 && argv!=0 ){
52e371033dSdrh     need = nCol*2;
53e371033dSdrh   }else{
54e371033dSdrh     need = nCol;
55e371033dSdrh   }
5673d34e92Sdrh   if( p->nData + need > p->nAlloc ){
576d4abfbeSdrh     char **azNew;
5873d34e92Sdrh     p->nAlloc = p->nAlloc*2 + need;
59*d924e7bcSdrh     azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
60779c6a06Sdrh     if( azNew==0 ) goto malloc_failed;
616d4abfbeSdrh     p->azResult = azNew;
62e371033dSdrh   }
63e371033dSdrh 
64e371033dSdrh   /* If this is the first row, then generate an extra row containing
65e371033dSdrh   ** the names of all columns.
66e371033dSdrh   */
67e371033dSdrh   if( p->nRow==0 ){
6801a34661Sdrh     p->nColumn = nCol;
69e371033dSdrh     for(i=0; i<nCol; i++){
702cc55698Sdrh       z = sqlite3_mprintf("%s", colv[i]);
7101495b99Sdrh       if( z==0 ) goto malloc_failed;
72e371033dSdrh       p->azResult[p->nData++] = z;
73e371033dSdrh     }
743329a63aSdrh   }else if( (int)p->nColumn!=nCol ){
7501495b99Sdrh     sqlite3_free(p->zErrMsg);
7601495b99Sdrh     p->zErrMsg = sqlite3_mprintf(
7701495b99Sdrh        "sqlite3_get_table() called with two or more incompatible queries"
7801495b99Sdrh     );
79f1a7a139Sdrh     p->rc = SQLITE_ERROR;
80f1a7a139Sdrh     return 1;
81e371033dSdrh   }
82e371033dSdrh 
83e371033dSdrh   /* Copy over the row data
84e371033dSdrh   */
856a535340Sdrh   if( argv!=0 ){
86e371033dSdrh     for(i=0; i<nCol; i++){
87e371033dSdrh       if( argv[i]==0 ){
88e371033dSdrh         z = 0;
89e371033dSdrh       }else{
90ea678832Sdrh         int n = sqlite3Strlen30(argv[i])+1;
91f3cdcdccSdrh         z = sqlite3_malloc64( n );
92779c6a06Sdrh         if( z==0 ) goto malloc_failed;
935bb3eb9bSdrh         memcpy(z, argv[i], n);
94e371033dSdrh       }
95e371033dSdrh       p->azResult[p->nData++] = z;
96e371033dSdrh     }
97e371033dSdrh     p->nRow++;
986a535340Sdrh   }
99e371033dSdrh   return 0;
100779c6a06Sdrh 
101779c6a06Sdrh malloc_failed:
102fad3039cSmistachkin   p->rc = SQLITE_NOMEM_BKPT;
103779c6a06Sdrh   return 1;
104e371033dSdrh }
105e371033dSdrh 
106e371033dSdrh /*
107e371033dSdrh ** Query the database.  But instead of invoking a callback for each row,
108e371033dSdrh ** malloc() for space to hold the result and return the entire results
109e371033dSdrh ** at the conclusion of the call.
110e371033dSdrh **
111e371033dSdrh ** The result that is written to ***pazResult is held in memory obtained
112e371033dSdrh ** from malloc().  But the caller cannot free this memory directly.
1136f8a503dSdanielk1977 ** Instead, the entire table should be passed to sqlite3_free_table() when
114e371033dSdrh ** the calling procedure is finished using it.
115e371033dSdrh */
sqlite3_get_table(sqlite3 * db,const char * zSql,char *** pazResult,int * pnRow,int * pnColumn,char ** pzErrMsg)1166f8a503dSdanielk1977 int sqlite3_get_table(
1179bb575fdSdrh   sqlite3 *db,                /* The database on which the SQL executes */
1189f71c2e0Sdrh   const char *zSql,           /* The SQL to be executed */
119e371033dSdrh   char ***pazResult,          /* Write the result table here */
120e371033dSdrh   int *pnRow,                 /* Write the number of rows in the result here */
121e371033dSdrh   int *pnColumn,              /* Write the number of columns of result here */
122e371033dSdrh   char **pzErrMsg             /* Write error messages here */
123e371033dSdrh ){
124e371033dSdrh   int rc;
125e371033dSdrh   TabResult res;
126d2b3e23bSdrh 
1279ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR
128cd54bab6Smistachkin   if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
1299ca95730Sdrh #endif
130e371033dSdrh   *pazResult = 0;
131e371033dSdrh   if( pnColumn ) *pnColumn = 0;
132e371033dSdrh   if( pnRow ) *pnRow = 0;
133770b3cb7Sdrh   if( pzErrMsg ) *pzErrMsg = 0;
134f1a7a139Sdrh   res.zErrMsg = 0;
135e371033dSdrh   res.nRow = 0;
136e371033dSdrh   res.nColumn = 0;
137e371033dSdrh   res.nData = 1;
138daffd0e5Sdrh   res.nAlloc = 20;
139e371033dSdrh   res.rc = SQLITE_OK;
140f3cdcdccSdrh   res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
14101495b99Sdrh   if( res.azResult==0 ){
14201495b99Sdrh      db->errCode = SQLITE_NOMEM;
143fad3039cSmistachkin      return SQLITE_NOMEM_BKPT;
14401495b99Sdrh   }
145e371033dSdrh   res.azResult[0] = 0;
1466f8a503dSdanielk1977   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
14778aecb72Sdrh   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
1481fc4129dSshane   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
1494ac285a1Sdrh   if( (rc&0xff)==SQLITE_ABORT ){
1506f8a503dSdanielk1977     sqlite3_free_table(&res.azResult[1]);
151f1a7a139Sdrh     if( res.zErrMsg ){
152f1a7a139Sdrh       if( pzErrMsg ){
15328dd479cSdrh         sqlite3_free(*pzErrMsg);
154ae29ffbeSdrh         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
155f1a7a139Sdrh       }
1561e536953Sdanielk1977       sqlite3_free(res.zErrMsg);
157f1a7a139Sdrh     }
15801495b99Sdrh     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
15926783a58Sdanielk1977     return res.rc;
160e371033dSdrh   }
1611e536953Sdanielk1977   sqlite3_free(res.zErrMsg);
162e371033dSdrh   if( rc!=SQLITE_OK ){
1636f8a503dSdanielk1977     sqlite3_free_table(&res.azResult[1]);
16426783a58Sdanielk1977     return rc;
165e371033dSdrh   }
166e371033dSdrh   if( res.nAlloc>res.nData ){
1676d4abfbeSdrh     char **azNew;
168*d924e7bcSdrh     azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
16993352470Sdrh     if( azNew==0 ){
1706f8a503dSdanielk1977       sqlite3_free_table(&res.azResult[1]);
17101495b99Sdrh       db->errCode = SQLITE_NOMEM;
172fad3039cSmistachkin       return SQLITE_NOMEM_BKPT;
1736d4abfbeSdrh     }
1746d4abfbeSdrh     res.azResult = azNew;
175e371033dSdrh   }
176e371033dSdrh   *pazResult = &res.azResult[1];
177e371033dSdrh   if( pnColumn ) *pnColumn = res.nColumn;
178e371033dSdrh   if( pnRow ) *pnRow = res.nRow;
17926783a58Sdanielk1977   return rc;
180e371033dSdrh }
181e371033dSdrh 
182e371033dSdrh /*
1836f8a503dSdanielk1977 ** This routine frees the space the sqlite3_get_table() malloced.
184e371033dSdrh */
sqlite3_free_table(char ** azResult)1856f8a503dSdanielk1977 void sqlite3_free_table(
18660ec914cSpeter.d.reid   char **azResult            /* Result returned from sqlite3_get_table() */
187e371033dSdrh ){
188e371033dSdrh   if( azResult ){
1897209c697Sdrh     int i, n;
190e371033dSdrh     azResult--;
191d2b3e23bSdrh     assert( azResult!=0 );
1921fc4129dSshane     n = SQLITE_PTR_TO_INT(azResult[0]);
19328dd479cSdrh     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
19428dd479cSdrh     sqlite3_free(azResult);
195e371033dSdrh   }
196e371033dSdrh }
197ff55c358Sdrh 
198ec7429aeSdrh #endif /* SQLITE_OMIT_GET_TABLE */
199