blob: cc01f7c2be88b66fbbf9b188fadff345c7b9a822 [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>
drhe3710332000-09-29 13:30:53 +000021#include "sqlite.h"
22
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;
29 int nResult;
30 int nAlloc;
31 int nRow;
32 int nColumn;
33 int nData;
34 int rc;
35} TabResult;
36
37/*
38** This routine is called once for each row in the result table. Its job
39** is to fill in the TabResult structure appropriately, allocating new
40** memory as necessary.
41*/
42static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
43 TabResult *p = (TabResult*)pArg;
44 int need;
drh7c68d602000-10-11 19:28:51 +000045 int i;
46 char *z;
drhe3710332000-09-29 13:30:53 +000047
48 /* Make sure there is enough space in p->azResult to hold everything
49 ** we need to remember from this invocation of the callback.
50 */
drh6a535342001-10-19 16:44:56 +000051 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000052 need = nCol*2;
53 }else{
54 need = nCol;
55 }
56 if( p->nData + need >= p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000057 char **azNew;
drhe3710332000-09-29 13:30:53 +000058 p->nAlloc = p->nAlloc*2 + need + 1;
drh6d4abfb2001-10-22 02:58:08 +000059 azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
60 if( azNew==0 ){
drhe3710332000-09-29 13:30:53 +000061 p->rc = SQLITE_NOMEM;
62 return 1;
63 }
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 ){
74 z = 0;
75 }else{
76 z = malloc( strlen(colv[i])+1 );
77 if( z==0 ){
78 p->rc = SQLITE_NOMEM;
79 return 1;
80 }
81 strcpy(z, colv[i]);
82 }
83 p->azResult[p->nData++] = z;
84 }
85 }
86
87 /* Copy over the row data
88 */
drh6a535342001-10-19 16:44:56 +000089 if( argv!=0 ){
90 for(i=0; i<nCol; i++){
91 if( argv[i]==0 ){
92 z = 0;
93 }else{
94 z = malloc( strlen(argv[i])+1 );
95 if( z==0 ){
96 p->rc = SQLITE_NOMEM;
97 return 1;
98 }
99 strcpy(z, argv[i]);
drhe3710332000-09-29 13:30:53 +0000100 }
drh6a535342001-10-19 16:44:56 +0000101 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +0000102 }
drh6a535342001-10-19 16:44:56 +0000103 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000104 }
drhe3710332000-09-29 13:30:53 +0000105 return 0;
106}
107
108/*
109** Query the database. But instead of invoking a callback for each row,
110** malloc() for space to hold the result and return the entire results
111** at the conclusion of the call.
112**
113** The result that is written to ***pazResult is held in memory obtained
114** from malloc(). But the caller cannot free this memory directly.
115** Instead, the entire table should be passed to sqlite_free_table() when
116** the calling procedure is finished using it.
117*/
118int sqlite_get_table(
119 sqlite *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000120 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000121 char ***pazResult, /* Write the result table here */
122 int *pnRow, /* Write the number of rows in the result here */
123 int *pnColumn, /* Write the number of columns of result here */
124 char **pzErrMsg /* Write error messages here */
125){
126 int rc;
127 TabResult res;
128 if( pazResult==0 ){ return SQLITE_ERROR; }
129 *pazResult = 0;
130 if( pnColumn ) *pnColumn = 0;
131 if( pnRow ) *pnRow = 0;
132 res.nResult = 0;
133 res.nRow = 0;
134 res.nColumn = 0;
135 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000136 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000137 res.rc = SQLITE_OK;
138 res.azResult = malloc( sizeof(char*)*res.nAlloc );
139 if( res.azResult==0 ){
140 return SQLITE_NOMEM;
141 }
142 res.azResult[0] = 0;
143 rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
144 if( res.azResult ){
145 res.azResult[0] = (char*)res.nData;
146 }
147 if( rc==SQLITE_ABORT ){
148 sqlite_free_table(&res.azResult[1]);
149 return res.rc;
150 }
151 if( rc!=SQLITE_OK ){
152 sqlite_free_table(&res.azResult[1]);
153 return rc;
154 }
155 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000156 char **azNew;
157 azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
158 if( res.azResult==0 ){
159 sqlite_free_table(&res.azResult[1]);
160 return SQLITE_NOMEM;
161 }
162 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000163 }
164 *pazResult = &res.azResult[1];
165 if( pnColumn ) *pnColumn = res.nColumn;
166 if( pnRow ) *pnRow = res.nRow;
167 return rc;
168}
169
170/*
171** This routine frees the space the sqlite_get_table() malloced.
172*/
173void sqlite_free_table(
174 char **azResult /* Result returned from from sqlite_get_table() */
175){
176 if( azResult ){
177 int i, n;
178 azResult--;
179 n = (int)azResult[0];
180 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
181 free(azResult);
182 }
183}