blob: 235d8dd3dff9d8dc5f239b09c86b6671ddc49ead [file] [log] [blame]
drhe3710332000-09-29 13:30:53 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh8c82b352000-12-10 18:23:50 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh8c82b352000-12-10 18:23:50 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh8c82b352000-12-10 18:23:50 +000010**
11*************************************************************************
danielk19776f8a5032004-05-10 10:34:51 +000012** This file contains the sqlite3_get_table() and sqlite3_free_table()
drhe3710332000-09-29 13:30:53 +000013** interface routines. These are just wrappers around the main
danielk19776f8a5032004-05-10 10:34:51 +000014** interface routine of sqlite3_exec().
drhe3710332000-09-29 13:30:53 +000015**
drh8c82b352000-12-10 18:23:50 +000016** These routines are in a separate files so that they will not be linked
drhe3710332000-09-29 13:30:53 +000017** if they are not used.
18*/
drh22465ce2005-11-24 22:33:05 +000019#include "sqliteInt.h"
drhe3710332000-09-29 13:30:53 +000020#include <stdlib.h>
drh8e0a2f92002-02-23 23:45:45 +000021#include <string.h>
drhe3710332000-09-29 13:30:53 +000022
drhff55c352005-10-05 02:13:40 +000023#ifndef SQLITE_OMIT_GET_TABLE
24
drhe3710332000-09-29 13:30:53 +000025/*
danielk19776f8a5032004-05-10 10:34:51 +000026** This structure is used to pass data from sqlite3_get_table() through
drhe3710332000-09-29 13:30:53 +000027** to the callback function is uses to build the result.
28*/
29typedef struct TabResult {
drh73d34e92009-04-10 14:27:59 +000030 char **azResult; /* Accumulated output */
31 char *zErrMsg; /* Error message text, if an error occurs */
drhda4ca9d2014-09-09 17:27:35 +000032 u32 nAlloc; /* Slots allocated for azResult[] */
33 u32 nRow; /* Number of rows in the result */
34 u32 nColumn; /* Number of columns in the result */
35 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
drh73d34e92009-04-10 14:27:59 +000036 int rc; /* Return code from sqlite3_exec() */
drhe3710332000-09-29 13:30:53 +000037} TabResult;
38
39/*
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
42** memory as necessary.
43*/
danielk19776f8a5032004-05-10 10:34:51 +000044static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
drh73d34e92009-04-10 14:27:59 +000045 TabResult *p = (TabResult*)pArg; /* Result accumulator */
46 int need; /* Slots needed in p->azResult[] */
47 int i; /* Loop counter */
48 char *z; /* A single column of result */
drhe3710332000-09-29 13:30:53 +000049
50 /* Make sure there is enough space in p->azResult to hold everything
51 ** we need to remember from this invocation of the callback.
52 */
drh6a535342001-10-19 16:44:56 +000053 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000054 need = nCol*2;
55 }else{
56 need = nCol;
57 }
drh73d34e92009-04-10 14:27:59 +000058 if( p->nData + need > p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000059 char **azNew;
drh73d34e92009-04-10 14:27:59 +000060 p->nAlloc = p->nAlloc*2 + need;
drhda4ca9d2014-09-09 17:27:35 +000061 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
drh779c6a02004-06-29 13:04:32 +000062 if( azNew==0 ) goto malloc_failed;
drh6d4abfb2001-10-22 02:58:08 +000063 p->azResult = azNew;
drhe3710332000-09-29 13:30:53 +000064 }
65
66 /* If this is the first row, then generate an extra row containing
67 ** the names of all columns.
68 */
69 if( p->nRow==0 ){
drh01a34662001-10-20 12:30:10 +000070 p->nColumn = nCol;
drhe3710332000-09-29 13:30:53 +000071 for(i=0; i<nCol; i++){
drhd2b3e232008-01-23 14:51:49 +000072 z = sqlite3_mprintf("%s", colv[i]);
drh01495b92008-01-23 12:52:40 +000073 if( z==0 ) goto malloc_failed;
drhe3710332000-09-29 13:30:53 +000074 p->azResult[p->nData++] = z;
75 }
drh3329a632014-09-18 01:21:43 +000076 }else if( (int)p->nColumn!=nCol ){
drh01495b92008-01-23 12:52:40 +000077 sqlite3_free(p->zErrMsg);
78 p->zErrMsg = sqlite3_mprintf(
79 "sqlite3_get_table() called with two or more incompatible queries"
80 );
drhf1a7a132002-03-23 00:52:01 +000081 p->rc = SQLITE_ERROR;
82 return 1;
drhe3710332000-09-29 13:30:53 +000083 }
84
85 /* Copy over the row data
86 */
drh6a535342001-10-19 16:44:56 +000087 if( argv!=0 ){
88 for(i=0; i<nCol; i++){
89 if( argv[i]==0 ){
90 z = 0;
91 }else{
drhea678832008-12-10 19:26:22 +000092 int n = sqlite3Strlen30(argv[i])+1;
drh5bb3eb92007-05-04 13:15:55 +000093 z = sqlite3_malloc( n );
drh779c6a02004-06-29 13:04:32 +000094 if( z==0 ) goto malloc_failed;
drh5bb3eb92007-05-04 13:15:55 +000095 memcpy(z, argv[i], n);
drhe3710332000-09-29 13:30:53 +000096 }
drh6a535342001-10-19 16:44:56 +000097 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +000098 }
drh6a535342001-10-19 16:44:56 +000099 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000100 }
drhe3710332000-09-29 13:30:53 +0000101 return 0;
drh779c6a02004-06-29 13:04:32 +0000102
103malloc_failed:
104 p->rc = SQLITE_NOMEM;
105 return 1;
drhe3710332000-09-29 13:30:53 +0000106}
107
108/*
109** Query the database. But instead of invoking a callback for each row,
110** malloc() for space to hold the result and return the entire results
111** at the conclusion of the call.
112**
113** The result that is written to ***pazResult is held in memory obtained
114** from malloc(). But the caller cannot free this memory directly.
danielk19776f8a5032004-05-10 10:34:51 +0000115** Instead, the entire table should be passed to sqlite3_free_table() when
drhe3710332000-09-29 13:30:53 +0000116** the calling procedure is finished using it.
117*/
danielk19776f8a5032004-05-10 10:34:51 +0000118int sqlite3_get_table(
drh9bb575f2004-09-06 17:24:11 +0000119 sqlite3 *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000120 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000121 char ***pazResult, /* Write the result table here */
122 int *pnRow, /* Write the number of rows in the result here */
123 int *pnColumn, /* Write the number of columns of result here */
124 char **pzErrMsg /* Write error messages here */
125){
126 int rc;
127 TabResult res;
drhd2b3e232008-01-23 14:51:49 +0000128
drh9ca95732014-10-24 00:35:58 +0000129#ifdef SQLITE_ENABLE_API_ARMOR
mistachkincd54bab2014-12-20 21:14:14 +0000130 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
drh9ca95732014-10-24 00:35:58 +0000131#endif
drhe3710332000-09-29 13:30:53 +0000132 *pazResult = 0;
133 if( pnColumn ) *pnColumn = 0;
134 if( pnRow ) *pnRow = 0;
drh770b3cb2009-01-19 20:49:09 +0000135 if( pzErrMsg ) *pzErrMsg = 0;
drhf1a7a132002-03-23 00:52:01 +0000136 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000137 res.nRow = 0;
138 res.nColumn = 0;
139 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000140 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000141 res.rc = SQLITE_OK;
drh01495b92008-01-23 12:52:40 +0000142 res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
143 if( res.azResult==0 ){
144 db->errCode = SQLITE_NOMEM;
145 return SQLITE_NOMEM;
146 }
drhe3710332000-09-29 13:30:53 +0000147 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000148 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhd2b3e232008-01-23 14:51:49 +0000149 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
shane1fc41292008-07-08 22:28:48 +0000150 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
drh4ac285a2006-09-15 07:28:50 +0000151 if( (rc&0xff)==SQLITE_ABORT ){
danielk19776f8a5032004-05-10 10:34:51 +0000152 sqlite3_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01 +0000153 if( res.zErrMsg ){
154 if( pzErrMsg ){
drh28dd4792006-06-26 21:35:44 +0000155 sqlite3_free(*pzErrMsg);
drhae29ffb2004-09-25 14:39:18 +0000156 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000157 }
danielk19771e536952007-08-16 10:09:01 +0000158 sqlite3_free(res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000159 }
drh01495b92008-01-23 12:52:40 +0000160 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
danielk197726783a52007-08-29 14:06:22 +0000161 return res.rc;
drhe3710332000-09-29 13:30:53 +0000162 }
danielk19771e536952007-08-16 10:09:01 +0000163 sqlite3_free(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000164 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000165 sqlite3_free_table(&res.azResult[1]);
danielk197726783a52007-08-29 14:06:22 +0000166 return rc;
drhe3710332000-09-29 13:30:53 +0000167 }
168 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000169 char **azNew;
drh73d34e92009-04-10 14:27:59 +0000170 azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
drh93352472003-05-17 00:05:49 +0000171 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000172 sqlite3_free_table(&res.azResult[1]);
drh01495b92008-01-23 12:52:40 +0000173 db->errCode = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000174 return SQLITE_NOMEM;
175 }
176 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000177 }
178 *pazResult = &res.azResult[1];
179 if( pnColumn ) *pnColumn = res.nColumn;
180 if( pnRow ) *pnRow = res.nRow;
danielk197726783a52007-08-29 14:06:22 +0000181 return rc;
drhe3710332000-09-29 13:30:53 +0000182}
183
184/*
danielk19776f8a5032004-05-10 10:34:51 +0000185** This routine frees the space the sqlite3_get_table() malloced.
drhe3710332000-09-29 13:30:53 +0000186*/
danielk19776f8a5032004-05-10 10:34:51 +0000187void sqlite3_free_table(
peter.d.reid60ec9142014-09-06 16:39:46 +0000188 char **azResult /* Result returned from sqlite3_get_table() */
drhe3710332000-09-29 13:30:53 +0000189){
190 if( azResult ){
drh7209c692008-04-27 18:40:11 +0000191 int i, n;
drhe3710332000-09-29 13:30:53 +0000192 azResult--;
drhd2b3e232008-01-23 14:51:49 +0000193 assert( azResult!=0 );
shane1fc41292008-07-08 22:28:48 +0000194 n = SQLITE_PTR_TO_INT(azResult[0]);
drh28dd4792006-06-26 21:35:44 +0000195 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
196 sqlite3_free(azResult);
drhe3710332000-09-29 13:30:53 +0000197 }
198}
drhff55c352005-10-05 02:13:40 +0000199
drhec7429a2005-10-06 16:53:14 +0000200#endif /* SQLITE_OMIT_GET_TABLE */