blob: f47ba60a2d9b8b31628cf4713502934325035972 [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*/
19#include <stdlib.h>
drh8e0a2f92002-02-23 23:45:45 +000020#include <string.h>
drhf1a7a132002-03-23 00:52:01 +000021#include "sqliteInt.h"
drhe3710332000-09-29 13:30:53 +000022
23/*
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 {
28 char **azResult;
drhf1a7a132002-03-23 00:52:01 +000029 char *zErrMsg;
drhe3710332000-09-29 13:30:53 +000030 int nResult;
31 int nAlloc;
32 int nRow;
33 int nColumn;
34 int nData;
35 int rc;
36} TabResult;
37
38/*
39** This routine is called once for each row in the result table. Its job
40** is to fill in the TabResult structure appropriately, allocating new
41** memory as necessary.
42*/
danielk19776f8a5032004-05-10 10:34:51 +000043static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
drhe3710332000-09-29 13:30:53 +000044 TabResult *p = (TabResult*)pArg;
45 int need;
drh7c68d602000-10-11 19:28:51 +000046 int i;
47 char *z;
drhe3710332000-09-29 13:30:53 +000048
49 /* Make sure there is enough space in p->azResult to hold everything
50 ** we need to remember from this invocation of the callback.
51 */
drh6a535342001-10-19 16:44:56 +000052 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000053 need = nCol*2;
54 }else{
55 need = nCol;
56 }
57 if( p->nData + need >= p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000058 char **azNew;
drhe3710332000-09-29 13:30:53 +000059 p->nAlloc = p->nAlloc*2 + need + 1;
drh6d4abfb2001-10-22 02:58:08 +000060 azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
61 if( azNew==0 ){
drhe3710332000-09-29 13:30:53 +000062 p->rc = SQLITE_NOMEM;
63 return 1;
64 }
drh6d4abfb2001-10-22 02:58:08 +000065 p->azResult = azNew;
drhe3710332000-09-29 13:30:53 +000066 }
67
68 /* If this is the first row, then generate an extra row containing
69 ** the names of all columns.
70 */
71 if( p->nRow==0 ){
drh01a34662001-10-20 12:30:10 +000072 p->nColumn = nCol;
drhe3710332000-09-29 13:30:53 +000073 for(i=0; i<nCol; i++){
74 if( colv[i]==0 ){
75 z = 0;
76 }else{
77 z = malloc( strlen(colv[i])+1 );
78 if( z==0 ){
79 p->rc = SQLITE_NOMEM;
80 return 1;
81 }
82 strcpy(z, colv[i]);
83 }
84 p->azResult[p->nData++] = z;
85 }
drhf1a7a132002-03-23 00:52:01 +000086 }else if( p->nColumn!=nCol ){
danielk19774adee202004-05-08 08:23:19 +000087 sqlite3SetString(&p->zErrMsg,
danielk19776f8a5032004-05-10 10:34:51 +000088 "sqlite3_get_table() called with two or more incompatible queries",
drh41743982003-12-06 21:43:55 +000089 (char*)0);
drhf1a7a132002-03-23 00:52:01 +000090 p->rc = SQLITE_ERROR;
91 return 1;
drhe3710332000-09-29 13:30:53 +000092 }
93
94 /* Copy over the row data
95 */
drh6a535342001-10-19 16:44:56 +000096 if( argv!=0 ){
97 for(i=0; i<nCol; i++){
98 if( argv[i]==0 ){
99 z = 0;
100 }else{
101 z = malloc( strlen(argv[i])+1 );
102 if( z==0 ){
103 p->rc = SQLITE_NOMEM;
104 return 1;
105 }
106 strcpy(z, argv[i]);
drhe3710332000-09-29 13:30:53 +0000107 }
drh6a535342001-10-19 16:44:56 +0000108 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +0000109 }
drh6a535342001-10-19 16:44:56 +0000110 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000111 }
drhe3710332000-09-29 13:30:53 +0000112 return 0;
113}
114
115/*
116** Query the database. But instead of invoking a callback for each row,
117** malloc() for space to hold the result and return the entire results
118** at the conclusion of the call.
119**
120** The result that is written to ***pazResult is held in memory obtained
121** from malloc(). But the caller cannot free this memory directly.
danielk19776f8a5032004-05-10 10:34:51 +0000122** Instead, the entire table should be passed to sqlite3_free_table() when
drhe3710332000-09-29 13:30:53 +0000123** the calling procedure is finished using it.
124*/
danielk19776f8a5032004-05-10 10:34:51 +0000125int sqlite3_get_table(
drhe3710332000-09-29 13:30:53 +0000126 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000127 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000128 char ***pazResult, /* Write the result table here */
129 int *pnRow, /* Write the number of rows in the result here */
130 int *pnColumn, /* Write the number of columns of result here */
131 char **pzErrMsg /* Write error messages here */
132){
133 int rc;
134 TabResult res;
135 if( pazResult==0 ){ return SQLITE_ERROR; }
136 *pazResult = 0;
137 if( pnColumn ) *pnColumn = 0;
138 if( pnRow ) *pnRow = 0;
drhf1a7a132002-03-23 00:52:01 +0000139 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000140 res.nResult = 0;
141 res.nRow = 0;
142 res.nColumn = 0;
143 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000144 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000145 res.rc = SQLITE_OK;
146 res.azResult = malloc( sizeof(char*)*res.nAlloc );
147 if( res.azResult==0 ){
148 return SQLITE_NOMEM;
149 }
150 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000151 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhe3710332000-09-29 13:30:53 +0000152 if( res.azResult ){
153 res.azResult[0] = (char*)res.nData;
154 }
155 if( rc==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 ){
159 free(*pzErrMsg);
160 *pzErrMsg = res.zErrMsg;
danielk19774adee202004-05-08 08:23:19 +0000161 sqlite3StrRealloc(pzErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000162 }else{
163 sqliteFree(res.zErrMsg);
164 }
165 }
drhe3710332000-09-29 13:30:53 +0000166 return res.rc;
167 }
drhf1a7a132002-03-23 00:52:01 +0000168 sqliteFree(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000169 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000170 sqlite3_free_table(&res.azResult[1]);
drhe3710332000-09-29 13:30:53 +0000171 return rc;
172 }
173 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000174 char **azNew;
175 azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
drh93352472003-05-17 00:05:49 +0000176 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000177 sqlite3_free_table(&res.azResult[1]);
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;
186 return rc;
187}
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(
193 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];
200 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
201 free(azResult);
202 }
203}
danielk19774adee202004-05-08 08:23:19 +0000204
205
206