blob: e957cf3d56ee864dcd80e5ab09d897ad1cdc7241 [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>
20#include "sqlite.h"
21
22/*
23** This structure is used to pass data from sqlite_get_table() through
24** to the callback function is uses to build the result.
25*/
26typedef struct TabResult {
27 char **azResult;
28 int nResult;
29 int nAlloc;
30 int nRow;
31 int nColumn;
32 int nData;
33 int rc;
34} TabResult;
35
36/*
37** This routine is called once for each row in the result table. Its job
38** is to fill in the TabResult structure appropriately, allocating new
39** memory as necessary.
40*/
41static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
42 TabResult *p = (TabResult*)pArg;
43 int need;
drh7c68d602000-10-11 19:28:51 +000044 int i;
45 char *z;
drhe3710332000-09-29 13:30:53 +000046
47 /* Make sure there is enough space in p->azResult to hold everything
48 ** we need to remember from this invocation of the callback.
49 */
drh6a535342001-10-19 16:44:56 +000050 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000051 need = nCol*2;
52 }else{
53 need = nCol;
54 }
55 if( p->nData + need >= p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000056 char **azNew;
drhe3710332000-09-29 13:30:53 +000057 p->nAlloc = p->nAlloc*2 + need + 1;
drh6d4abfb2001-10-22 02:58:08 +000058 azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
59 if( azNew==0 ){
drhe3710332000-09-29 13:30:53 +000060 p->rc = SQLITE_NOMEM;
61 return 1;
62 }
drh6d4abfb2001-10-22 02:58:08 +000063 p->azResult = azNew;
drhe3710332000-09-29 13:30:53 +000064 }
65
66 /* If this is the first row, then generate an extra row containing
67 ** the names of all columns.
68 */
69 if( p->nRow==0 ){
drh01a34662001-10-20 12:30:10 +000070 p->nColumn = nCol;
drhe3710332000-09-29 13:30:53 +000071 for(i=0; i<nCol; i++){
72 if( colv[i]==0 ){
73 z = 0;
74 }else{
75 z = malloc( strlen(colv[i])+1 );
76 if( z==0 ){
77 p->rc = SQLITE_NOMEM;
78 return 1;
79 }
80 strcpy(z, colv[i]);
81 }
82 p->azResult[p->nData++] = z;
83 }
84 }
85
86 /* Copy over the row data
87 */
drh6a535342001-10-19 16:44:56 +000088 if( argv!=0 ){
89 for(i=0; i<nCol; i++){
90 if( argv[i]==0 ){
91 z = 0;
92 }else{
93 z = malloc( strlen(argv[i])+1 );
94 if( z==0 ){
95 p->rc = SQLITE_NOMEM;
96 return 1;
97 }
98 strcpy(z, argv[i]);
drhe3710332000-09-29 13:30:53 +000099 }
drh6a535342001-10-19 16:44:56 +0000100 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +0000101 }
drh6a535342001-10-19 16:44:56 +0000102 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000103 }
drhe3710332000-09-29 13:30:53 +0000104 return 0;
105}
106
107/*
108** Query the database. But instead of invoking a callback for each row,
109** malloc() for space to hold the result and return the entire results
110** at the conclusion of the call.
111**
112** The result that is written to ***pazResult is held in memory obtained
113** from malloc(). But the caller cannot free this memory directly.
114** Instead, the entire table should be passed to sqlite_free_table() when
115** the calling procedure is finished using it.
116*/
117int sqlite_get_table(
118 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000119 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000120 char ***pazResult, /* Write the result table here */
121 int *pnRow, /* Write the number of rows in the result here */
122 int *pnColumn, /* Write the number of columns of result here */
123 char **pzErrMsg /* Write error messages here */
124){
125 int rc;
126 TabResult res;
127 if( pazResult==0 ){ return SQLITE_ERROR; }
128 *pazResult = 0;
129 if( pnColumn ) *pnColumn = 0;
130 if( pnRow ) *pnRow = 0;
131 res.nResult = 0;
132 res.nRow = 0;
133 res.nColumn = 0;
134 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000135 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000136 res.rc = SQLITE_OK;
137 res.azResult = malloc( sizeof(char*)*res.nAlloc );
138 if( res.azResult==0 ){
139 return SQLITE_NOMEM;
140 }
141 res.azResult[0] = 0;
142 rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
143 if( res.azResult ){
144 res.azResult[0] = (char*)res.nData;
145 }
146 if( rc==SQLITE_ABORT ){
147 sqlite_free_table(&res.azResult[1]);
148 return res.rc;
149 }
150 if( rc!=SQLITE_OK ){
151 sqlite_free_table(&res.azResult[1]);
152 return rc;
153 }
154 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000155 char **azNew;
156 azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
157 if( res.azResult==0 ){
158 sqlite_free_table(&res.azResult[1]);
159 return SQLITE_NOMEM;
160 }
161 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000162 }
163 *pazResult = &res.azResult[1];
164 if( pnColumn ) *pnColumn = res.nColumn;
165 if( pnRow ) *pnRow = res.nRow;
166 return rc;
167}
168
169/*
170** This routine frees the space the sqlite_get_table() malloced.
171*/
172void sqlite_free_table(
173 char **azResult /* Result returned from from sqlite_get_table() */
174){
175 if( azResult ){
176 int i, n;
177 azResult--;
178 n = (int)azResult[0];
179 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
180 free(azResult);
181 }
182}