blob: c79255f990cf88f154af08f02ca6207c4c8a7e0f [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
drhff55c352005-10-05 02:13:40 +000021#ifndef SQLITE_OMIT_GET_TABLE
22
drhe3710332000-09-29 13:30:53 +000023/*
danielk19776f8a5032004-05-10 10:34:51 +000024** This structure is used to pass data from sqlite3_get_table() through
drhe3710332000-09-29 13:30:53 +000025** to the callback function is uses to build the result.
26*/
27typedef struct TabResult {
drh73d34e92009-04-10 14:27:59 +000028 char **azResult; /* Accumulated output */
29 char *zErrMsg; /* Error message text, if an error occurs */
drhda4ca9d2014-09-09 17:27:35 +000030 u32 nAlloc; /* Slots allocated for azResult[] */
31 u32 nRow; /* Number of rows in the result */
32 u32 nColumn; /* Number of columns in the result */
33 u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
drh73d34e92009-04-10 14:27:59 +000034 int rc; /* Return code from sqlite3_exec() */
drhe3710332000-09-29 13:30:53 +000035} TabResult;
36
37/*
38** This routine is called once for each row in the result table. Its job
39** is to fill in the TabResult structure appropriately, allocating new
40** memory as necessary.
41*/
danielk19776f8a5032004-05-10 10:34:51 +000042static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
drh73d34e92009-04-10 14:27:59 +000043 TabResult *p = (TabResult*)pArg; /* Result accumulator */
44 int need; /* Slots needed in p->azResult[] */
45 int i; /* Loop counter */
46 char *z; /* A single column of result */
drhe3710332000-09-29 13:30:53 +000047
48 /* Make sure there is enough space in p->azResult to hold everything
49 ** we need to remember from this invocation of the callback.
50 */
drh6a535342001-10-19 16:44:56 +000051 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000052 need = nCol*2;
53 }else{
54 need = nCol;
55 }
drh73d34e92009-04-10 14:27:59 +000056 if( p->nData + need > p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000057 char **azNew;
drh73d34e92009-04-10 14:27:59 +000058 p->nAlloc = p->nAlloc*2 + need;
drhda4ca9d2014-09-09 17:27:35 +000059 azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc );
drh779c6a02004-06-29 13:04:32 +000060 if( azNew==0 ) goto malloc_failed;
drh6d4abfb2001-10-22 02:58:08 +000061 p->azResult = azNew;
drhe3710332000-09-29 13:30:53 +000062 }
63
64 /* If this is the first row, then generate an extra row containing
65 ** the names of all columns.
66 */
67 if( p->nRow==0 ){
drh01a34662001-10-20 12:30:10 +000068 p->nColumn = nCol;
drhe3710332000-09-29 13:30:53 +000069 for(i=0; i<nCol; i++){
drhd2b3e232008-01-23 14:51:49 +000070 z = sqlite3_mprintf("%s", colv[i]);
drh01495b92008-01-23 12:52:40 +000071 if( z==0 ) goto malloc_failed;
drhe3710332000-09-29 13:30:53 +000072 p->azResult[p->nData++] = z;
73 }
drh3329a632014-09-18 01:21:43 +000074 }else if( (int)p->nColumn!=nCol ){
drh01495b92008-01-23 12:52:40 +000075 sqlite3_free(p->zErrMsg);
76 p->zErrMsg = sqlite3_mprintf(
77 "sqlite3_get_table() called with two or more incompatible queries"
78 );
drhf1a7a132002-03-23 00:52:01 +000079 p->rc = SQLITE_ERROR;
80 return 1;
drhe3710332000-09-29 13:30:53 +000081 }
82
83 /* Copy over the row data
84 */
drh6a535342001-10-19 16:44:56 +000085 if( argv!=0 ){
86 for(i=0; i<nCol; i++){
87 if( argv[i]==0 ){
88 z = 0;
89 }else{
drhea678832008-12-10 19:26:22 +000090 int n = sqlite3Strlen30(argv[i])+1;
drhf3cdcdc2015-04-29 16:50:28 +000091 z = sqlite3_malloc64( n );
drh779c6a02004-06-29 13:04:32 +000092 if( z==0 ) goto malloc_failed;
drh5bb3eb92007-05-04 13:15:55 +000093 memcpy(z, argv[i], n);
drhe3710332000-09-29 13:30:53 +000094 }
drh6a535342001-10-19 16:44:56 +000095 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +000096 }
drh6a535342001-10-19 16:44:56 +000097 p->nRow++;
drhe3710332000-09-29 13:30:53 +000098 }
drhe3710332000-09-29 13:30:53 +000099 return 0;
drh779c6a02004-06-29 13:04:32 +0000100
101malloc_failed:
mistachkinfad30392016-02-13 23:43:46 +0000102 p->rc = SQLITE_NOMEM_BKPT;
drh779c6a02004-06-29 13:04:32 +0000103 return 1;
drhe3710332000-09-29 13:30:53 +0000104}
105
106/*
107** Query the database. But instead of invoking a callback for each row,
108** malloc() for space to hold the result and return the entire results
109** at the conclusion of the call.
110**
111** The result that is written to ***pazResult is held in memory obtained
112** from malloc(). But the caller cannot free this memory directly.
danielk19776f8a5032004-05-10 10:34:51 +0000113** Instead, the entire table should be passed to sqlite3_free_table() when
drhe3710332000-09-29 13:30:53 +0000114** the calling procedure is finished using it.
115*/
danielk19776f8a5032004-05-10 10:34:51 +0000116int sqlite3_get_table(
drh9bb575f2004-09-06 17:24:11 +0000117 sqlite3 *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000118 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000119 char ***pazResult, /* Write the result table here */
120 int *pnRow, /* Write the number of rows in the result here */
121 int *pnColumn, /* Write the number of columns of result here */
122 char **pzErrMsg /* Write error messages here */
123){
124 int rc;
125 TabResult res;
drhd2b3e232008-01-23 14:51:49 +0000126
drh9ca95732014-10-24 00:35:58 +0000127#ifdef SQLITE_ENABLE_API_ARMOR
mistachkincd54bab2014-12-20 21:14:14 +0000128 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
drh9ca95732014-10-24 00:35:58 +0000129#endif
drhe3710332000-09-29 13:30:53 +0000130 *pazResult = 0;
131 if( pnColumn ) *pnColumn = 0;
132 if( pnRow ) *pnRow = 0;
drh770b3cb2009-01-19 20:49:09 +0000133 if( pzErrMsg ) *pzErrMsg = 0;
drhf1a7a132002-03-23 00:52:01 +0000134 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000135 res.nRow = 0;
136 res.nColumn = 0;
137 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000138 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000139 res.rc = SQLITE_OK;
drhf3cdcdc2015-04-29 16:50:28 +0000140 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
drh01495b92008-01-23 12:52:40 +0000141 if( res.azResult==0 ){
142 db->errCode = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46 +0000143 return SQLITE_NOMEM_BKPT;
drh01495b92008-01-23 12:52:40 +0000144 }
drhe3710332000-09-29 13:30:53 +0000145 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000146 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhd2b3e232008-01-23 14:51:49 +0000147 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
shane1fc41292008-07-08 22:28:48 +0000148 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
drh4ac285a2006-09-15 07:28:50 +0000149 if( (rc&0xff)==SQLITE_ABORT ){
danielk19776f8a5032004-05-10 10:34:51 +0000150 sqlite3_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01 +0000151 if( res.zErrMsg ){
152 if( pzErrMsg ){
drh28dd4792006-06-26 21:35:44 +0000153 sqlite3_free(*pzErrMsg);
drhae29ffb2004-09-25 14:39:18 +0000154 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000155 }
danielk19771e536952007-08-16 10:09:01 +0000156 sqlite3_free(res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000157 }
drh01495b92008-01-23 12:52:40 +0000158 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
danielk197726783a52007-08-29 14:06:22 +0000159 return res.rc;
drhe3710332000-09-29 13:30:53 +0000160 }
danielk19771e536952007-08-16 10:09:01 +0000161 sqlite3_free(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000162 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000163 sqlite3_free_table(&res.azResult[1]);
danielk197726783a52007-08-29 14:06:22 +0000164 return rc;
drhe3710332000-09-29 13:30:53 +0000165 }
166 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000167 char **azNew;
drhf3cdcdc2015-04-29 16:50:28 +0000168 azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData );
drh93352472003-05-17 00:05:49 +0000169 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000170 sqlite3_free_table(&res.azResult[1]);
drh01495b92008-01-23 12:52:40 +0000171 db->errCode = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46 +0000172 return SQLITE_NOMEM_BKPT;
drh6d4abfb2001-10-22 02:58:08 +0000173 }
174 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000175 }
176 *pazResult = &res.azResult[1];
177 if( pnColumn ) *pnColumn = res.nColumn;
178 if( pnRow ) *pnRow = res.nRow;
danielk197726783a52007-08-29 14:06:22 +0000179 return rc;
drhe3710332000-09-29 13:30:53 +0000180}
181
182/*
danielk19776f8a5032004-05-10 10:34:51 +0000183** This routine frees the space the sqlite3_get_table() malloced.
drhe3710332000-09-29 13:30:53 +0000184*/
danielk19776f8a5032004-05-10 10:34:51 +0000185void sqlite3_free_table(
peter.d.reid60ec9142014-09-06 16:39:46 +0000186 char **azResult /* Result returned from sqlite3_get_table() */
drhe3710332000-09-29 13:30:53 +0000187){
188 if( azResult ){
drh7209c692008-04-27 18:40:11 +0000189 int i, n;
drhe3710332000-09-29 13:30:53 +0000190 azResult--;
drhd2b3e232008-01-23 14:51:49 +0000191 assert( azResult!=0 );
shane1fc41292008-07-08 22:28:48 +0000192 n = SQLITE_PTR_TO_INT(azResult[0]);
drh28dd4792006-06-26 21:35:44 +0000193 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
194 sqlite3_free(azResult);
drhe3710332000-09-29 13:30:53 +0000195 }
196}
drhff55c352005-10-05 02:13:40 +0000197
drhec7429a2005-10-06 16:53:14 +0000198#endif /* SQLITE_OMIT_GET_TABLE */