blob: cf0a3ee0b0d20481aea2a4eaa58a9d20554f93cf [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.
danielk1977822a5162008-05-16 04:51:54 +000018**
drh73d34e92009-04-10 14:27:59 +000019** $Id: table.c,v 1.40 2009/04/10 14:28:00 drh Exp $
drhe3710332000-09-29 13:30:53 +000020*/
drh22465ce2005-11-24 22:33:05 +000021#include "sqliteInt.h"
drhe3710332000-09-29 13:30:53 +000022#include <stdlib.h>
drh8e0a2f92002-02-23 23:45:45 +000023#include <string.h>
drhe3710332000-09-29 13:30:53 +000024
drhff55c352005-10-05 02:13:40 +000025#ifndef SQLITE_OMIT_GET_TABLE
26
drhe3710332000-09-29 13:30:53 +000027/*
danielk19776f8a5032004-05-10 10:34:51 +000028** This structure is used to pass data from sqlite3_get_table() through
drhe3710332000-09-29 13:30:53 +000029** to the callback function is uses to build the result.
30*/
31typedef struct TabResult {
drh73d34e92009-04-10 14:27:59 +000032 char **azResult; /* Accumulated output */
33 char *zErrMsg; /* Error message text, if an error occurs */
34 int nAlloc; /* Slots allocated for azResult[] */
35 int nRow; /* Number of rows in the result */
36 int nColumn; /* Number of columns in the result */
37 int nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
38 int rc; /* Return code from sqlite3_exec() */
drhe3710332000-09-29 13:30:53 +000039} TabResult;
40
41/*
42** This routine is called once for each row in the result table. Its job
43** is to fill in the TabResult structure appropriately, allocating new
44** memory as necessary.
45*/
danielk19776f8a5032004-05-10 10:34:51 +000046static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
drh73d34e92009-04-10 14:27:59 +000047 TabResult *p = (TabResult*)pArg; /* Result accumulator */
48 int need; /* Slots needed in p->azResult[] */
49 int i; /* Loop counter */
50 char *z; /* A single column of result */
drhe3710332000-09-29 13:30:53 +000051
52 /* Make sure there is enough space in p->azResult to hold everything
53 ** we need to remember from this invocation of the callback.
54 */
drh6a535342001-10-19 16:44:56 +000055 if( p->nRow==0 && argv!=0 ){
drhe3710332000-09-29 13:30:53 +000056 need = nCol*2;
57 }else{
58 need = nCol;
59 }
drh73d34e92009-04-10 14:27:59 +000060 if( p->nData + need > p->nAlloc ){
drh6d4abfb2001-10-22 02:58:08 +000061 char **azNew;
drh73d34e92009-04-10 14:27:59 +000062 p->nAlloc = p->nAlloc*2 + need;
drh28dd4792006-06-26 21:35:44 +000063 azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
drh779c6a02004-06-29 13:04:32 +000064 if( azNew==0 ) goto malloc_failed;
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++){
drhd2b3e232008-01-23 14:51:49 +000074 z = sqlite3_mprintf("%s", colv[i]);
drh01495b92008-01-23 12:52:40 +000075 if( z==0 ) goto malloc_failed;
drhe3710332000-09-29 13:30:53 +000076 p->azResult[p->nData++] = z;
77 }
drhf1a7a132002-03-23 00:52:01 +000078 }else if( p->nColumn!=nCol ){
drh01495b92008-01-23 12:52:40 +000079 sqlite3_free(p->zErrMsg);
80 p->zErrMsg = sqlite3_mprintf(
81 "sqlite3_get_table() called with two or more incompatible queries"
82 );
drhf1a7a132002-03-23 00:52:01 +000083 p->rc = SQLITE_ERROR;
84 return 1;
drhe3710332000-09-29 13:30:53 +000085 }
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{
drhea678832008-12-10 19:26:22 +000094 int n = sqlite3Strlen30(argv[i])+1;
drh5bb3eb92007-05-04 13:15:55 +000095 z = sqlite3_malloc( n );
drh779c6a02004-06-29 13:04:32 +000096 if( z==0 ) goto malloc_failed;
drh5bb3eb92007-05-04 13:15:55 +000097 memcpy(z, argv[i], n);
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;
drhd2b3e232008-01-23 14:51:49 +0000130
drhe3710332000-09-29 13:30:53 +0000131 *pazResult = 0;
132 if( pnColumn ) *pnColumn = 0;
133 if( pnRow ) *pnRow = 0;
drh770b3cb2009-01-19 20:49:09 +0000134 if( pzErrMsg ) *pzErrMsg = 0;
drhf1a7a132002-03-23 00:52:01 +0000135 res.zErrMsg = 0;
drhe3710332000-09-29 13:30:53 +0000136 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;
drh01495b92008-01-23 12:52:40 +0000141 res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
142 if( res.azResult==0 ){
143 db->errCode = SQLITE_NOMEM;
144 return SQLITE_NOMEM;
145 }
drhe3710332000-09-29 13:30:53 +0000146 res.azResult[0] = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000147 rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
drhd2b3e232008-01-23 14:51:49 +0000148 assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
shane1fc41292008-07-08 22:28:48 +0000149 res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
drh4ac285a2006-09-15 07:28:50 +0000150 if( (rc&0xff)==SQLITE_ABORT ){
danielk19776f8a5032004-05-10 10:34:51 +0000151 sqlite3_free_table(&res.azResult[1]);
drhf1a7a132002-03-23 00:52:01 +0000152 if( res.zErrMsg ){
153 if( pzErrMsg ){
drh28dd4792006-06-26 21:35:44 +0000154 sqlite3_free(*pzErrMsg);
drhae29ffb2004-09-25 14:39:18 +0000155 *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000156 }
danielk19771e536952007-08-16 10:09:01 +0000157 sqlite3_free(res.zErrMsg);
drhf1a7a132002-03-23 00:52:01 +0000158 }
drh01495b92008-01-23 12:52:40 +0000159 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
danielk197726783a52007-08-29 14:06:22 +0000160 return res.rc;
drhe3710332000-09-29 13:30:53 +0000161 }
danielk19771e536952007-08-16 10:09:01 +0000162 sqlite3_free(res.zErrMsg);
drhe3710332000-09-29 13:30:53 +0000163 if( rc!=SQLITE_OK ){
danielk19776f8a5032004-05-10 10:34:51 +0000164 sqlite3_free_table(&res.azResult[1]);
danielk197726783a52007-08-29 14:06:22 +0000165 return rc;
drhe3710332000-09-29 13:30:53 +0000166 }
167 if( res.nAlloc>res.nData ){
drh6d4abfb2001-10-22 02:58:08 +0000168 char **azNew;
drh73d34e92009-04-10 14:27:59 +0000169 azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
drh93352472003-05-17 00:05:49 +0000170 if( azNew==0 ){
danielk19776f8a5032004-05-10 10:34:51 +0000171 sqlite3_free_table(&res.azResult[1]);
drh01495b92008-01-23 12:52:40 +0000172 db->errCode = SQLITE_NOMEM;
drh6d4abfb2001-10-22 02:58:08 +0000173 return SQLITE_NOMEM;
174 }
175 res.azResult = azNew;
drhe3710332000-09-29 13:30:53 +0000176 }
177 *pazResult = &res.azResult[1];
178 if( pnColumn ) *pnColumn = res.nColumn;
179 if( pnRow ) *pnRow = res.nRow;
danielk197726783a52007-08-29 14:06:22 +0000180 return rc;
drhe3710332000-09-29 13:30:53 +0000181}
182
183/*
danielk19776f8a5032004-05-10 10:34:51 +0000184** This routine frees the space the sqlite3_get_table() malloced.
drhe3710332000-09-29 13:30:53 +0000185*/
danielk19776f8a5032004-05-10 10:34:51 +0000186void sqlite3_free_table(
drh779c6a02004-06-29 13:04:32 +0000187 char **azResult /* Result returned from from sqlite3_get_table() */
drhe3710332000-09-29 13:30:53 +0000188){
189 if( azResult ){
drh7209c692008-04-27 18:40:11 +0000190 int i, n;
drhe3710332000-09-29 13:30:53 +0000191 azResult--;
drhd2b3e232008-01-23 14:51:49 +0000192 assert( azResult!=0 );
shane1fc41292008-07-08 22:28:48 +0000193 n = SQLITE_PTR_TO_INT(azResult[0]);
drh28dd4792006-06-26 21:35:44 +0000194 for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
195 sqlite3_free(azResult);
drhe3710332000-09-29 13:30:53 +0000196 }
197}
drhff55c352005-10-05 02:13:40 +0000198
drhec7429a2005-10-06 16:53:14 +0000199#endif /* SQLITE_OMIT_GET_TABLE */