blob: 8f61d6f2f5669f5e3b13a837e42ff3296a435c45 [file] [log] [blame]
drhe3710332000-09-29 13:30:53 +00001/*
drh8c82b352000-12-10 18:23:50 +00002** Copyright (c) 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
drhe3710332000-09-29 13:30:53 +000024** This file contains the sqlite_get_table() and sqlite_free_table()
25** interface routines. These are just wrappers around the main
26** interface routine of sqlite_exec().
27**
drh8c82b352000-12-10 18:23:50 +000028** These routines are in a separate files so that they will not be linked
drhe3710332000-09-29 13:30:53 +000029** if they are not used.
30*/
31#include <stdlib.h>
32#include "sqlite.h"
33
34/*
35** This structure is used to pass data from sqlite_get_table() through
36** to the callback function is uses to build the result.
37*/
38typedef struct TabResult {
39 char **azResult;
40 int nResult;
41 int nAlloc;
42 int nRow;
43 int nColumn;
44 int nData;
45 int rc;
46} TabResult;
47
48/*
49** This routine is called once for each row in the result table. Its job
50** is to fill in the TabResult structure appropriately, allocating new
51** memory as necessary.
52*/
53static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
54 TabResult *p = (TabResult*)pArg;
55 int need;
drh7c68d602000-10-11 19:28:51 +000056 int i;
57 char *z;
drhe3710332000-09-29 13:30:53 +000058
59 /* Make sure there is enough space in p->azResult to hold everything
60 ** we need to remember from this invocation of the callback.
61 */
62 if( p->nRow==0 ){
63 p->nColumn = nCol;
64 need = nCol*2;
65 }else{
66 need = nCol;
67 }
68 if( p->nData + need >= p->nAlloc ){
69 p->nAlloc = p->nAlloc*2 + need + 1;
70 p->azResult = realloc( p->azResult, sizeof(char*)*p->nAlloc );
71 if( p->azResult==0 ){
72 p->rc = SQLITE_NOMEM;
73 return 1;
74 }
75 }
76
77 /* If this is the first row, then generate an extra row containing
78 ** the names of all columns.
79 */
80 if( p->nRow==0 ){
81 for(i=0; i<nCol; i++){
82 if( colv[i]==0 ){
83 z = 0;
84 }else{
85 z = malloc( strlen(colv[i])+1 );
86 if( z==0 ){
87 p->rc = SQLITE_NOMEM;
88 return 1;
89 }
90 strcpy(z, colv[i]);
91 }
92 p->azResult[p->nData++] = z;
93 }
94 }
95
96 /* Copy over the row data
97 */
98 for(i=0; i<nCol; i++){
99 if( argv[i]==0 ){
100 z = 0;
101 }else{
102 z = malloc( strlen(argv[i])+1 );
103 if( z==0 ){
104 p->rc = SQLITE_NOMEM;
105 return 1;
106 }
107 strcpy(z, argv[i]);
108 }
109 p->azResult[p->nData++] = z;
110 }
111 p->nRow++;
112 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.
122** Instead, the entire table should be passed to sqlite_free_table() when
123** the calling procedure is finished using it.
124*/
125int sqlite_get_table(
126 sqlite *db, /* The database on which the SQL executes */
127 char *zSql, /* The SQL to be executed */
128 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;
139 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]);
156 return res.rc;
157 }
158 if( rc!=SQLITE_OK ){
159 sqlite_free_table(&res.azResult[1]);
160 return rc;
161 }
162 if( res.nAlloc>res.nData ){
163 res.azResult = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
164 if( res.azResult==0 ) return SQLITE_NOMEM;
165 }
166 *pazResult = &res.azResult[1];
167 if( pnColumn ) *pnColumn = res.nColumn;
168 if( pnRow ) *pnRow = res.nRow;
169 return rc;
170}
171
172/*
173** This routine frees the space the sqlite_get_table() malloced.
174*/
175void sqlite_free_table(
176 char **azResult /* Result returned from from sqlite_get_table() */
177){
178 if( azResult ){
179 int i, n;
180 azResult--;
181 n = (int)azResult[0];
182 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
183 free(azResult);
184 }
185}