blob: 48782e8953f7e70c5c624db377858c875f281cab [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 {
30 char **azResult;
drhf1a7a132002-03-23 00:52:01 +000031 char *zErrMsg;
drhe3710332000-09-29 13:30:53 +000032 int nResult;
33 int nAlloc;
34 int nRow;
35 int nColumn;
36 int nData;
37 int rc;
38} TabResult;
39
40/*
41** This routine is called once for each row in the result table. Its job
42** is to fill in the TabResult structure appropriately, allocating new
43** memory as necessary.
44*/
danielk19776f8a5032004-05-10 10:34:51 +000045static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
drhe3710332000-09-29 13:30:53 +000046 TabResult *p = (TabResult*)pArg;
47 int need;
drh7c68d602000-10-11 19:28:51 +000048 int i;
49 char *z;
drhe3710332000-09-29 13:30:53 +000050
51 /* Make sure there is enough space in p->azResult to hold everything
52 ** we need to remember from this invocation of the callback.
53 */
drh6a535342001-10-19 16:44:56 +000054 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000055 need = nCol*2;
56 }else{
57 need = nCol;
58 }
59 if( p->nData + need >= p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000060 char **azNew;
drhe3710332000-09-29 13:30:53 +000061 p->nAlloc = p->nAlloc*2 + need + 1;
drh28dd4792006-06-26 21:35:44 +000062 azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
drh779c6a02004-06-29 13:04:32 +000063 if( azNew==0 ) goto malloc_failed;
drh6d4abfb2001-10-22 02:58:08 +000064 p->azResult = azNew;
drhe3710332000-09-29 13:30:53 +000065 }
66
67 /* If this is the first row, then generate an extra row containing
68 ** the names of all columns.
69 */
70 if( p->nRow==0 ){
drh01a34662001-10-20 12:30:10 +000071 p->nColumn = nCol;
drhe3710332000-09-29 13:30:53 +000072 for(i=0; i<nCol; i++){
73 if( colv[i]==0 ){
drh2cc55692006-06-27 20:39:04 +000074 z = sqlite3_mprintf("");
drhe3710332000-09-29 13:30:53 +000075 }else{
drh2cc55692006-06-27 20:39:04 +000076 z = sqlite3_mprintf("%s", colv[i]);
drhe3710332000-09-29 13:30:53 +000077 }
drh01495b92008-01-23 12:52:40 +000078 if( z==0 ) goto malloc_failed;
drhe3710332000-09-29 13:30:53 +000079 p->azResult[p->nData++] = z;
80 }
drhf1a7a132002-03-23 00:52:01 +000081 }else if( p->nColumn!=nCol ){
drh01495b92008-01-23 12:52:40 +000082 sqlite3_free(p->zErrMsg);
83 p->zErrMsg = sqlite3_mprintf(
84 "sqlite3_get_table() called with two or more incompatible queries"
85 );
drhf1a7a132002-03-23 00:52:01 +000086 p->rc = SQLITE_ERROR;
87 return 1;
drhe3710332000-09-29 13:30:53 +000088 }
89
90 /* Copy over the row data
91 */
drh6a535342001-10-19 16:44:56 +000092 if( argv!=0 ){
93 for(i=0; i<nCol; i++){
94 if( argv[i]==0 ){
95 z = 0;
96 }else{
drh5bb3eb92007-05-04 13:15:55 +000097 int n = strlen(argv[i])+1;
98 z = sqlite3_malloc( n );
drh779c6a02004-06-29 13:04:32 +000099 if( z==0 ) goto malloc_failed;
drh5bb3eb92007-05-04 13:15:55 +0000100 memcpy(z, argv[i], n);
drhe3710332000-09-29 13:30:53 +0000101 }
drh6a535342001-10-19 16:44:56 +0000102 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +0000103 }
drh6a535342001-10-19 16:44:56 +0000104 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000105 }
drhe3710332000-09-29 13:30:53 +0000106 return 0;
drh779c6a02004-06-29 13:04:32 +0000107
108malloc_failed:
109 p->rc = SQLITE_NOMEM;
110 return 1;
drhe3710332000-09-29 13:30:53 +0000111}
112
113/*
114** Query the database. But instead of invoking a callback for each row,
115** malloc() for space to hold the result and return the entire results
116** at the conclusion of the call.
117**
118** The result that is written to ***pazResult is held in memory obtained
119** from malloc(). But the caller cannot free this memory directly.
danielk19776f8a5032004-05-10 10:34:51 +0000120** Instead, the entire table should be passed to sqlite3_free_table() when
drhe3710332000-09-29 13:30:53 +0000121** the calling procedure is finished using it.
122*/
danielk19776f8a5032004-05-10 10:34:51 +0000123int sqlite3_get_table(
drh9bb575f2004-09-06 17:24:11 +0000124 sqlite3 *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000125 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000126 char ***pazResult, /* Write the result table here */
127 int *pnRow, /* Write the number of rows in the result here */
128 int *pnColumn, /* Write the number of columns of result here */
129 char **pzErrMsg /* Write error messages here */
130){
131 int rc;
132 TabResult res;
133 if( pazResult==0 ){ return SQLITE_ERROR; }
134 *pazResult = 0;
135 if( pnColumn ) *pnColumn = 0;
136 if( pnRow ) *pnRow = 0;
drhf1a7a132002-03-23 00:52:01 +0000137 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000138 res.nResult = 0;
139 res.nRow = 0;
140 res.nColumn = 0;
141 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000142 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000143 res.rc = SQLITE_OK;
drh01495b92008-01-23 12:52:40 +0000144 res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
145 if( res.azResult==0 ){
146 db->errCode = SQLITE_NOMEM;
147 return SQLITE_NOMEM;
148 }
drhe3710332000-09-29 13:30:53 +0000149 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000150 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhe3710332000-09-29 13:30:53 +0000151 if( res.azResult ){
drh78aecb72006-02-10 18:08:09 +0000152 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
drhe3710332000-09-29 13:30:53 +0000153 res.azResult[0] = (char*)res.nData;
154 }
drh4ac285a2006-09-15 07:28:50 +0000155 if( (rc&0xff)==SQLITE_ABORT ){
danielk19776f8a5032004-05-10 10:34:51 +0000156 sqlite3_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01 +0000157 if( res.zErrMsg ){
158 if( pzErrMsg ){
drh28dd4792006-06-26 21:35:44 +0000159 sqlite3_free(*pzErrMsg);
drhae29ffb2004-09-25 14:39:18 +0000160 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000161 }
danielk19771e536952007-08-16 10:09:01 +0000162 sqlite3_free(res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000163 }
drh01495b92008-01-23 12:52:40 +0000164 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
danielk197726783a52007-08-29 14:06:22 +0000165 return res.rc;
drhe3710332000-09-29 13:30:53 +0000166 }
danielk19771e536952007-08-16 10:09:01 +0000167 sqlite3_free(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000168 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000169 sqlite3_free_table(&res.azResult[1]);
danielk197726783a52007-08-29 14:06:22 +0000170 return rc;
drhe3710332000-09-29 13:30:53 +0000171 }
172 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000173 char **azNew;
drh28dd4792006-06-26 21:35:44 +0000174 azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) );
drh93352472003-05-17 00:05:49 +0000175 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000176 sqlite3_free_table(&res.azResult[1]);
drh01495b92008-01-23 12:52:40 +0000177 db->errCode = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000178 return SQLITE_NOMEM;
179 }
drh93352472003-05-17 00:05:49 +0000180 res.nAlloc = res.nData+1;
drh6d4abfb2001-10-22 02:58:08 +0000181 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000182 }
183 *pazResult = &res.azResult[1];
184 if( pnColumn ) *pnColumn = res.nColumn;
185 if( pnRow ) *pnRow = res.nRow;
danielk197726783a52007-08-29 14:06:22 +0000186 return rc;
drhe3710332000-09-29 13:30:53 +0000187}
188
189/*
danielk19776f8a5032004-05-10 10:34:51 +0000190** This routine frees the space the sqlite3_get_table() malloced.
drhe3710332000-09-29 13:30:53 +0000191*/
danielk19776f8a5032004-05-10 10:34:51 +0000192void sqlite3_free_table(
drh779c6a02004-06-29 13:04:32 +0000193 char **azResult /* Result returned from from sqlite3_get_table() */
drhe3710332000-09-29 13:30:53 +0000194){
195 if( azResult ){
196 int i, n;
197 azResult--;
drh93352472003-05-17 00:05:49 +0000198 if( azResult==0 ) return;
drhe3710332000-09-29 13:30:53 +0000199 n = (int)azResult[0];
drh28dd4792006-06-26 21:35:44 +0000200 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
201 sqlite3_free(azResult);
drhe3710332000-09-29 13:30:53 +0000202 }
203}
drhff55c352005-10-05 02:13:40 +0000204
drhec7429a2005-10-06 16:53:14 +0000205#endif /* SQLITE_OMIT_GET_TABLE */