Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: db60a827a0a90383b5a33cb1a185640ebe8ba739 [file] [log] [blame]
drhe3710332000-09-29 13:30:531/*
drhb19a2bc2001-09-16 00:13:262** 2001 September 15
drh8c82b352000-12-10 18:23:503**
drhb19a2bc2001-09-16 00:13:264** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh8c82b352000-12-10 18:23:506**
drhb19a2bc2001-09-16 00:13:267** 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:5010**
11*************************************************************************
danielk19776f8a5032004-05-10 10:34:5112** This file contains the sqlite3_get_table() and sqlite3_free_table()
drhe3710332000-09-29 13:30:5313** interface routines. These are just wrappers around the main
danielk19776f8a5032004-05-10 10:34:5114** interface routine of sqlite3_exec().
drhe3710332000-09-29 13:30:5315**
drh8c82b352000-12-10 18:23:5016** These routines are in a separate files so that they will not be linked
drhe3710332000-09-29 13:30:5317** if they are not used.
18*/
drh22465ce2005-11-24 22:33:0519#include "sqliteInt.h"
drhe3710332000-09-29 13:30:5320
drhff55c352005-10-05 02:13:4021#ifndef SQLITE_OMIT_GET_TABLE
22
drhe3710332000-09-29 13:30:5323/*
danielk19776f8a5032004-05-10 10:34:5124** This structure is used to pass data from sqlite3_get_table() through
drhe3710332000-09-29 13:30:5325** to the callback function is uses to build the result.
26*/
27typedef struct TabResult {
drh73d34e92009-04-10 14:27:5928 char **azResult; /* Accumulated output */
29 char *zErrMsg; /* Error message text, if an error occurs */
drhda4ca9d2014-09-09 17:27:3530 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:5934 int rc; /* Return code from sqlite3_exec() */
drhe3710332000-09-29 13:30:5335} 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:5142static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
drh73d34e92009-04-10 14:27:5943 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:5347
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:5651 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:5352 need = nCol*2;
53 }else{
54 need = nCol;
55 }
drh73d34e92009-04-10 14:27:5956 if( p->nData + need > p->nAlloc ){
drh6d4abfb2001-10-22 02:58:0857 char **azNew;
drh73d34e92009-04-10 14:27:5958 p->nAlloc = p->nAlloc*2 + need;
drhd924e7b2020-05-17 00:26:4459 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
drh779c6a02004-06-29 13:04:3260 if( azNew==0 ) goto malloc_failed;
drh6d4abfb2001-10-22 02:58:0861 p->azResult = azNew;
drhe3710332000-09-29 13:30:5362 }
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:1068 p->nColumn = nCol;
drhe3710332000-09-29 13:30:5369 for(i=0; i<nCol; i++){
drhd2b3e232008-01-23 14:51:4970 z = sqlite3_mprintf("%s", colv[i]);
drh01495b92008-01-23 12:52:4071 if( z==0 ) goto malloc_failed;
drhe3710332000-09-29 13:30:5372 p->azResult[p->nData++] = z;
73 }
drh3329a632014-09-18 01:21:4374 }else if( (int)p->nColumn!=nCol ){
drh01495b92008-01-23 12:52:4075 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:0179 p->rc = SQLITE_ERROR;
80 return 1;
drhe3710332000-09-29 13:30:5381 }
82
83 /* Copy over the row data
84 */
drh6a535342001-10-19 16:44:5685 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:2290 int n = sqlite3Strlen30(argv[i])+1;
drhf3cdcdc2015-04-29 16:50:2891 z = sqlite3_malloc64( n );
drh779c6a02004-06-29 13:04:3292 if( z==0 ) goto malloc_failed;
drh5bb3eb92007-05-04 13:15:5593 memcpy(z, argv[i], n);
drhe3710332000-09-29 13:30:5394 }
drh6a535342001-10-19 16:44:5695 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:5396 }
drh6a535342001-10-19 16:44:5697 p->nRow++;
drhe3710332000-09-29 13:30:5398 }
drhe3710332000-09-29 13:30:5399 return 0;
drh779c6a02004-06-29 13:04:32100
101malloc_failed:
mistachkinfad30392016-02-13 23:43:46102 p->rc = SQLITE_NOMEM_BKPT;
drh779c6a02004-06-29 13:04:32103 return 1;
drhe3710332000-09-29 13:30:53104}
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:51113** Instead, the entire table should be passed to sqlite3_free_table() when
drhe3710332000-09-29 13:30:53114** the calling procedure is finished using it.
115*/
danielk19776f8a5032004-05-10 10:34:51116int sqlite3_get_table(
drh9bb575f2004-09-06 17:24:11117 sqlite3 *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09118 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53119 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:49126
drh9ca95732014-10-24 00:35:58127#ifdef SQLITE_ENABLE_API_ARMOR
mistachkincd54bab2014-12-20 21:14:14128 if( !sqlite3SafetyCheckOk(db) || pazResult==0 ) return SQLITE_MISUSE_BKPT;
drh9ca95732014-10-24 00:35:58129#endif
drhe3710332000-09-29 13:30:53130 *pazResult = 0;
131 if( pnColumn ) *pnColumn = 0;
132 if( pnRow ) *pnRow = 0;
drh770b3cb2009-01-19 20:49:09133 if( pzErrMsg ) *pzErrMsg = 0;
drhf1a7a132002-03-23 00:52:01134 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53135 res.nRow = 0;
136 res.nColumn = 0;
137 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42138 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53139 res.rc = SQLITE_OK;
drhf3cdcdc2015-04-29 16:50:28140 res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc );
drh01495b92008-01-23 12:52:40141 if( res.azResult==0 ){
142 db->errCode = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46143 return SQLITE_NOMEM_BKPT;
drh01495b92008-01-23 12:52:40144 }
drhe3710332000-09-29 13:30:53145 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51146 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhd2b3e232008-01-23 14:51:49147 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
shane1fc41292008-07-08 22:28:48148 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
drh4ac285a2006-09-15 07:28:50149 if( (rc&0xff)==SQLITE_ABORT ){
danielk19776f8a5032004-05-10 10:34:51150 sqlite3_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01151 if( res.zErrMsg ){
152 if( pzErrMsg ){
drh28dd4792006-06-26 21:35:44153 sqlite3_free(*pzErrMsg);
drhae29ffb2004-09-25 14:39:18154 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
drhf1a7a132002-03-23 00:52:01155 }
danielk19771e536952007-08-16 10:09:01156 sqlite3_free(res.zErrMsg);
drhf1a7a132002-03-23 00:52:01157 }
drh01495b92008-01-23 12:52:40158 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
danielk197726783a52007-08-29 14:06:22159 return res.rc;
drhe3710332000-09-29 13:30:53160 }
danielk19771e536952007-08-16 10:09:01161 sqlite3_free(res.zErrMsg);
drhe3710332000-09-29 13:30:53162 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51163 sqlite3_free_table(&res.azResult[1]);
danielk197726783a52007-08-29 14:06:22164 return rc;
drhe3710332000-09-29 13:30:53165 }
166 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08167 char **azNew;
drhd924e7b2020-05-17 00:26:44168 azNew = sqlite3Realloc( res.azResult, sizeof(char*)*res.nData );
drh93352472003-05-17 00:05:49169 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51170 sqlite3_free_table(&res.azResult[1]);
drh01495b92008-01-23 12:52:40171 db->errCode = SQLITE_NOMEM;
mistachkinfad30392016-02-13 23:43:46172 return SQLITE_NOMEM_BKPT;
drh6d4abfb2001-10-22 02:58:08173 }
174 res.azResult = azNew;
drhe3710332000-09-29 13:30:53175 }
176 *pazResult = &res.azResult[1];
177 if( pnColumn ) *pnColumn = res.nColumn;
178 if( pnRow ) *pnRow = res.nRow;
danielk197726783a52007-08-29 14:06:22179 return rc;
drhe3710332000-09-29 13:30:53180}
181
182/*
danielk19776f8a5032004-05-10 10:34:51183** This routine frees the space the sqlite3_get_table() malloced.
drhe3710332000-09-29 13:30:53184*/
danielk19776f8a5032004-05-10 10:34:51185void sqlite3_free_table(
peter.d.reid60ec9142014-09-06 16:39:46186 char **azResult /* Result returned from sqlite3_get_table() */
drhe3710332000-09-29 13:30:53187){
188 if( azResult ){
drh7209c692008-04-27 18:40:11189 int i, n;
drhe3710332000-09-29 13:30:53190 azResult--;
drhd2b3e232008-01-23 14:51:49191 assert( azResult!=0 );
shane1fc41292008-07-08 22:28:48192 n = SQLITE_PTR_TO_INT(azResult[0]);
drh28dd4792006-06-26 21:35:44193 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
194 sqlite3_free(azResult);
drhe3710332000-09-29 13:30:53195 }
196}
drhff55c352005-10-05 02:13:40197
drhec7429a2005-10-06 16:53:14198#endif /* SQLITE_OMIT_GET_TABLE */