blob: 71f0b59a3ae2aab84800cb9b1f8ce88d88072891 [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*************************************************************************
drhe3710332000-09-29 13:30:53 +000012** This file contains the sqlite_get_table() and sqlite_free_table()
13** interface routines. These are just wrappers around the main
14** interface routine of sqlite_exec().
15**
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/*
24** This structure is used to pass data from sqlite_get_table() through
25** 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*/
43static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
44 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 ){
87 sqliteSetString(&p->zErrMsg,
88 "sqlite_get_table() called with two or more incompatible queries", 0);
89 p->rc = SQLITE_ERROR;
90 return 1;
drhe3710332000-09-29 13:30:53 +000091 }
92
93 /* Copy over the row data
94 */
drh6a535342001-10-19 16:44:56 +000095 if( argv!=0 ){
96 for(i=0; i<nCol; i++){
97 if( argv[i]==0 ){
98 z = 0;
99 }else{
100 z = malloc( strlen(argv[i])+1 );
101 if( z==0 ){
102 p->rc = SQLITE_NOMEM;
103 return 1;
104 }
105 strcpy(z, argv[i]);
drhe3710332000-09-29 13:30:53 +0000106 }
drh6a535342001-10-19 16:44:56 +0000107 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +0000108 }
drh6a535342001-10-19 16:44:56 +0000109 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000110 }
drhe3710332000-09-29 13:30:53 +0000111 return 0;
112}
113
114/*
115** Query the database. But instead of invoking a callback for each row,
116** malloc() for space to hold the result and return the entire results
117** at the conclusion of the call.
118**
119** The result that is written to ***pazResult is held in memory obtained
120** from malloc(). But the caller cannot free this memory directly.
121** Instead, the entire table should be passed to sqlite_free_table() when
122** the calling procedure is finished using it.
123*/
124int sqlite_get_table(
125 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000126 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000127 char ***pazResult, /* Write the result table here */
128 int *pnRow, /* Write the number of rows in the result here */
129 int *pnColumn, /* Write the number of columns of result here */
130 char **pzErrMsg /* Write error messages here */
131){
132 int rc;
133 TabResult res;
134 if( pazResult==0 ){ return SQLITE_ERROR; }
135 *pazResult = 0;
136 if( pnColumn ) *pnColumn = 0;
137 if( pnRow ) *pnRow = 0;
drhf1a7a132002-03-23 00:52:01 +0000138 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000139 res.nResult = 0;
140 res.nRow = 0;
141 res.nColumn = 0;
142 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000143 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000144 res.rc = SQLITE_OK;
145 res.azResult = malloc( sizeof(char*)*res.nAlloc );
146 if( res.azResult==0 ){
147 return SQLITE_NOMEM;
148 }
149 res.azResult[0] = 0;
150 rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
151 if( res.azResult ){
152 res.azResult[0] = (char*)res.nData;
153 }
154 if( rc==SQLITE_ABORT ){
155 sqlite_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01 +0000156 if( res.zErrMsg ){
157 if( pzErrMsg ){
158 free(*pzErrMsg);
159 *pzErrMsg = res.zErrMsg;
160 sqliteStrRealloc(pzErrMsg);
161 }else{
162 sqliteFree(res.zErrMsg);
163 }
164 }
drhe3710332000-09-29 13:30:53 +0000165 return res.rc;
166 }
drhf1a7a132002-03-23 00:52:01 +0000167 sqliteFree(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000168 if( rc!=SQLITE_OK ){
169 sqlite_free_table(&res.azResult[1]);
170 return rc;
171 }
172 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000173 char **azNew;
174 azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
drh93352472003-05-17 00:05:49 +0000175 if( azNew==0 ){
drh6d4abfb2001-10-22 02:58:08 +0000176 sqlite_free_table(&res.azResult[1]);
177 return SQLITE_NOMEM;
178 }
drh93352472003-05-17 00:05:49 +0000179 res.nAlloc = res.nData+1;
drh6d4abfb2001-10-22 02:58:08 +0000180 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000181 }
182 *pazResult = &res.azResult[1];
183 if( pnColumn ) *pnColumn = res.nColumn;
184 if( pnRow ) *pnRow = res.nRow;
185 return rc;
186}
187
188/*
189** This routine frees the space the sqlite_get_table() malloced.
190*/
191void sqlite_free_table(
192 char **azResult /* Result returned from from sqlite_get_table() */
193){
194 if( azResult ){
195 int i, n;
196 azResult--;
drh93352472003-05-17 00:05:49 +0000197 if( azResult==0 ) return;
drhe3710332000-09-29 13:30:53 +0000198 n = (int)azResult[0];
199 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
200 free(azResult);
201 }
202}