blob: 2725be927279cedbd15da885c0fb1321ddb8dada [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 );
drh779c6a02004-06-29 13:04:32 +000061 if( azNew==0 ) goto malloc_failed;
drh6d4abfb2001-10-22 02:58:08 +000062 p->azResult = azNew;
drhe3710332000-09-29 13:30:53 +000063 }
64
65 /* If this is the first row, then generate an extra row containing
66 ** the names of all columns.
67 */
68 if( p->nRow==0 ){
drh01a34662001-10-20 12:30:10 +000069 p->nColumn = nCol;
drhe3710332000-09-29 13:30:53 +000070 for(i=0; i<nCol; i++){
71 if( colv[i]==0 ){
72 z = 0;
73 }else{
74 z = malloc( strlen(colv[i])+1 );
drh779c6a02004-06-29 13:04:32 +000075 if( z==0 ) goto malloc_failed;
drhe3710332000-09-29 13:30:53 +000076 strcpy(z, colv[i]);
77 }
78 p->azResult[p->nData++] = z;
79 }
drhf1a7a132002-03-23 00:52:01 +000080 }else if( p->nColumn!=nCol ){
danielk19774adee202004-05-08 08:23:19 +000081 sqlite3SetString(&p->zErrMsg,
danielk19776f8a5032004-05-10 10:34:51 +000082 "sqlite3_get_table() called with two or more incompatible queries",
drh41743982003-12-06 21:43:55 +000083 (char*)0);
drhf1a7a132002-03-23 00:52:01 +000084 p->rc = SQLITE_ERROR;
85 return 1;
drhe3710332000-09-29 13:30:53 +000086 }
87
88 /* Copy over the row data
89 */
drh6a535342001-10-19 16:44:56 +000090 if( argv!=0 ){
91 for(i=0; i<nCol; i++){
92 if( argv[i]==0 ){
93 z = 0;
94 }else{
95 z = malloc( strlen(argv[i])+1 );
drh779c6a02004-06-29 13:04:32 +000096 if( z==0 ) goto malloc_failed;
drh6a535342001-10-19 16:44:56 +000097 strcpy(z, argv[i]);
drhe3710332000-09-29 13:30:53 +000098 }
drh6a535342001-10-19 16:44:56 +000099 p->azResult[p->nData++] = z;
drhe3710332000-09-29 13:30:53 +0000100 }
drh6a535342001-10-19 16:44:56 +0000101 p->nRow++;
drhe3710332000-09-29 13:30:53 +0000102 }
drhe3710332000-09-29 13:30:53 +0000103 return 0;
drh779c6a02004-06-29 13:04:32 +0000104
105malloc_failed:
106 p->rc = SQLITE_NOMEM;
107 return 1;
drhe3710332000-09-29 13:30:53 +0000108}
109
110/*
111** Query the database. But instead of invoking a callback for each row,
112** malloc() for space to hold the result and return the entire results
113** at the conclusion of the call.
114**
115** The result that is written to ***pazResult is held in memory obtained
116** from malloc(). But the caller cannot free this memory directly.
danielk19776f8a5032004-05-10 10:34:51 +0000117** Instead, the entire table should be passed to sqlite3_free_table() when
drhe3710332000-09-29 13:30:53 +0000118** the calling procedure is finished using it.
119*/
danielk19776f8a5032004-05-10 10:34:51 +0000120int sqlite3_get_table(
drh9bb575f2004-09-06 17:24:11 +0000121 sqlite3 *db, /* The database on which the SQL executes */
drh9f71c2e2001-11-03 23:57:09 +0000122 const char *zSql, /* The SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000123 char ***pazResult, /* Write the result table here */
124 int *pnRow, /* Write the number of rows in the result here */
125 int *pnColumn, /* Write the number of columns of result here */
126 char **pzErrMsg /* Write error messages here */
127){
128 int rc;
129 TabResult res;
130 if( pazResult==0 ){ return SQLITE_ERROR; }
131 *pazResult = 0;
132 if( pnColumn ) *pnColumn = 0;
133 if( pnRow ) *pnRow = 0;
drhf1a7a132002-03-23 00:52:01 +0000134 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000135 res.nResult = 0;
136 res.nRow = 0;
137 res.nColumn = 0;
138 res.nData = 1;
drhdaffd0e2001-04-11 14:28:42 +0000139 res.nAlloc = 20;
drhe3710332000-09-29 13:30:53 +0000140 res.rc = SQLITE_OK;
141 res.azResult = malloc( sizeof(char*)*res.nAlloc );
drh779c6a02004-06-29 13:04:32 +0000142 if( res.azResult==0 ) return SQLITE_NOMEM;
drhe3710332000-09-29 13:30:53 +0000143 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000144 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhe3710332000-09-29 13:30:53 +0000145 if( res.azResult ){
146 res.azResult[0] = (char*)res.nData;
147 }
148 if( rc==SQLITE_ABORT ){
danielk19776f8a5032004-05-10 10:34:51 +0000149 sqlite3_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01 +0000150 if( res.zErrMsg ){
151 if( pzErrMsg ){
152 free(*pzErrMsg);
153 *pzErrMsg = res.zErrMsg;
danielk19774adee202004-05-08 08:23:19 +0000154 sqlite3StrRealloc(pzErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000155 }else{
156 sqliteFree(res.zErrMsg);
157 }
158 }
drhe3710332000-09-29 13:30:53 +0000159 return res.rc;
160 }
drhf1a7a132002-03-23 00:52:01 +0000161 sqliteFree(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000162 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000163 sqlite3_free_table(&res.azResult[1]);
drhe3710332000-09-29 13:30:53 +0000164 return rc;
165 }
166 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000167 char **azNew;
168 azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
drh93352472003-05-17 00:05:49 +0000169 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000170 sqlite3_free_table(&res.azResult[1]);
drh6d4abfb2001-10-22 02:58:08 +0000171 return SQLITE_NOMEM;
172 }
drh93352472003-05-17 00:05:49 +0000173 res.nAlloc = res.nData+1;
drh6d4abfb2001-10-22 02:58:08 +0000174 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000175 }
176 *pazResult = &res.azResult[1];
177 if( pnColumn ) *pnColumn = res.nColumn;
178 if( pnRow ) *pnRow = res.nRow;
179 return rc;
180}
181
182/*
danielk19776f8a5032004-05-10 10:34:51 +0000183** This routine frees the space the sqlite3_get_table() malloced.
drhe3710332000-09-29 13:30:53 +0000184*/
danielk19776f8a5032004-05-10 10:34:51 +0000185void sqlite3_free_table(
drh779c6a02004-06-29 13:04:32 +0000186 char **azResult /* Result returned from from sqlite3_get_table() */
drhe3710332000-09-29 13:30:53 +0000187){
188 if( azResult ){
189 int i, n;
190 azResult--;
drh93352472003-05-17 00:05:49 +0000191 if( azResult==0 ) return;
drhe3710332000-09-29 13:30:53 +0000192 n = (int)azResult[0];
193 for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
194 free(azResult);
195 }
196}