xref: /titanic_41/usr/src/lib/libsqlite/src/table.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292 
2*c5c4113dSnw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292 
4*c5c4113dSnw141292 /*
5*c5c4113dSnw141292 ** 2001 September 15
6*c5c4113dSnw141292 **
7*c5c4113dSnw141292 ** The author disclaims copyright to this source code.  In place of
8*c5c4113dSnw141292 ** a legal notice, here is a blessing:
9*c5c4113dSnw141292 **
10*c5c4113dSnw141292 **    May you do good and not evil.
11*c5c4113dSnw141292 **    May you find forgiveness for yourself and forgive others.
12*c5c4113dSnw141292 **    May you share freely, never taking more than you give.
13*c5c4113dSnw141292 **
14*c5c4113dSnw141292 *************************************************************************
15*c5c4113dSnw141292 ** This file contains the sqlite_get_table() and sqlite_free_table()
16*c5c4113dSnw141292 ** interface routines.  These are just wrappers around the main
17*c5c4113dSnw141292 ** interface routine of sqlite_exec().
18*c5c4113dSnw141292 **
19*c5c4113dSnw141292 ** These routines are in a separate files so that they will not be linked
20*c5c4113dSnw141292 ** if they are not used.
21*c5c4113dSnw141292 */
22*c5c4113dSnw141292 #include <stdlib.h>
23*c5c4113dSnw141292 #include <string.h>
24*c5c4113dSnw141292 #include "sqliteInt.h"
25*c5c4113dSnw141292 
26*c5c4113dSnw141292 /*
27*c5c4113dSnw141292 ** This structure is used to pass data from sqlite_get_table() through
28*c5c4113dSnw141292 ** to the callback function is uses to build the result.
29*c5c4113dSnw141292 */
30*c5c4113dSnw141292 typedef struct TabResult {
31*c5c4113dSnw141292   char **azResult;
32*c5c4113dSnw141292   char *zErrMsg;
33*c5c4113dSnw141292   int nResult;
34*c5c4113dSnw141292   int nAlloc;
35*c5c4113dSnw141292   int nRow;
36*c5c4113dSnw141292   int nColumn;
37*c5c4113dSnw141292   long nData;
38*c5c4113dSnw141292   int rc;
39*c5c4113dSnw141292 } TabResult;
40*c5c4113dSnw141292 
41*c5c4113dSnw141292 /*
42*c5c4113dSnw141292 ** This routine is called once for each row in the result table.  Its job
43*c5c4113dSnw141292 ** is to fill in the TabResult structure appropriately, allocating new
44*c5c4113dSnw141292 ** memory as necessary.
45*c5c4113dSnw141292 */
sqlite_get_table_cb(void * pArg,int nCol,char ** argv,char ** colv)46*c5c4113dSnw141292 static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
47*c5c4113dSnw141292   TabResult *p = (TabResult*)pArg;
48*c5c4113dSnw141292   int need;
49*c5c4113dSnw141292   int i;
50*c5c4113dSnw141292   char *z;
51*c5c4113dSnw141292 
52*c5c4113dSnw141292   /* Make sure there is enough space in p->azResult to hold everything
53*c5c4113dSnw141292   ** we need to remember from this invocation of the callback.
54*c5c4113dSnw141292   */
55*c5c4113dSnw141292   if( p->nRow==0 && argv!=0 ){
56*c5c4113dSnw141292     need = nCol*2;
57*c5c4113dSnw141292   }else{
58*c5c4113dSnw141292     need = nCol;
59*c5c4113dSnw141292   }
60*c5c4113dSnw141292   if( p->nData + need >= p->nAlloc ){
61*c5c4113dSnw141292     char **azNew;
62*c5c4113dSnw141292     p->nAlloc = p->nAlloc*2 + need + 1;
63*c5c4113dSnw141292     azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc );
64*c5c4113dSnw141292     if( azNew==0 ){
65*c5c4113dSnw141292       p->rc = SQLITE_NOMEM;
66*c5c4113dSnw141292       return 1;
67*c5c4113dSnw141292     }
68*c5c4113dSnw141292     p->azResult = azNew;
69*c5c4113dSnw141292   }
70*c5c4113dSnw141292 
71*c5c4113dSnw141292   /* If this is the first row, then generate an extra row containing
72*c5c4113dSnw141292   ** the names of all columns.
73*c5c4113dSnw141292   */
74*c5c4113dSnw141292   if( p->nRow==0 ){
75*c5c4113dSnw141292     p->nColumn = nCol;
76*c5c4113dSnw141292     for(i=0; i<nCol; i++){
77*c5c4113dSnw141292       if( colv[i]==0 ){
78*c5c4113dSnw141292         z = 0;
79*c5c4113dSnw141292       }else{
80*c5c4113dSnw141292         z = malloc( strlen(colv[i])+1 );
81*c5c4113dSnw141292         if( z==0 ){
82*c5c4113dSnw141292           p->rc = SQLITE_NOMEM;
83*c5c4113dSnw141292           return 1;
84*c5c4113dSnw141292         }
85*c5c4113dSnw141292         strcpy(z, colv[i]);
86*c5c4113dSnw141292       }
87*c5c4113dSnw141292       p->azResult[p->nData++] = z;
88*c5c4113dSnw141292     }
89*c5c4113dSnw141292   }else if( p->nColumn!=nCol ){
90*c5c4113dSnw141292     sqliteSetString(&p->zErrMsg,
91*c5c4113dSnw141292        "sqlite_get_table() called with two or more incompatible queries",
92*c5c4113dSnw141292        (char*)0);
93*c5c4113dSnw141292     p->rc = SQLITE_ERROR;
94*c5c4113dSnw141292     return 1;
95*c5c4113dSnw141292   }
96*c5c4113dSnw141292 
97*c5c4113dSnw141292   /* Copy over the row data
98*c5c4113dSnw141292   */
99*c5c4113dSnw141292   if( argv!=0 ){
100*c5c4113dSnw141292     for(i=0; i<nCol; i++){
101*c5c4113dSnw141292       if( argv[i]==0 ){
102*c5c4113dSnw141292         z = 0;
103*c5c4113dSnw141292       }else{
104*c5c4113dSnw141292         z = malloc( strlen(argv[i])+1 );
105*c5c4113dSnw141292         if( z==0 ){
106*c5c4113dSnw141292           p->rc = SQLITE_NOMEM;
107*c5c4113dSnw141292           return 1;
108*c5c4113dSnw141292         }
109*c5c4113dSnw141292         strcpy(z, argv[i]);
110*c5c4113dSnw141292       }
111*c5c4113dSnw141292       p->azResult[p->nData++] = z;
112*c5c4113dSnw141292     }
113*c5c4113dSnw141292     p->nRow++;
114*c5c4113dSnw141292   }
115*c5c4113dSnw141292   return 0;
116*c5c4113dSnw141292 }
117*c5c4113dSnw141292 
118*c5c4113dSnw141292 /*
119*c5c4113dSnw141292 ** Query the database.  But instead of invoking a callback for each row,
120*c5c4113dSnw141292 ** malloc() for space to hold the result and return the entire results
121*c5c4113dSnw141292 ** at the conclusion of the call.
122*c5c4113dSnw141292 **
123*c5c4113dSnw141292 ** The result that is written to ***pazResult is held in memory obtained
124*c5c4113dSnw141292 ** from malloc().  But the caller cannot free this memory directly.
125*c5c4113dSnw141292 ** Instead, the entire table should be passed to sqlite_free_table() when
126*c5c4113dSnw141292 ** the calling procedure is finished using it.
127*c5c4113dSnw141292 */
sqlite_get_table(sqlite * db,const char * zSql,char *** pazResult,int * pnRow,int * pnColumn,char ** pzErrMsg)128*c5c4113dSnw141292 int sqlite_get_table(
129*c5c4113dSnw141292   sqlite *db,                 /* The database on which the SQL executes */
130*c5c4113dSnw141292   const char *zSql,           /* The SQL to be executed */
131*c5c4113dSnw141292   char ***pazResult,          /* Write the result table here */
132*c5c4113dSnw141292   int *pnRow,                 /* Write the number of rows in the result here */
133*c5c4113dSnw141292   int *pnColumn,              /* Write the number of columns of result here */
134*c5c4113dSnw141292   char **pzErrMsg             /* Write error messages here */
135*c5c4113dSnw141292 ){
136*c5c4113dSnw141292   int rc;
137*c5c4113dSnw141292   TabResult res;
138*c5c4113dSnw141292   if( pazResult==0 ){ return SQLITE_ERROR; }
139*c5c4113dSnw141292   *pazResult = 0;
140*c5c4113dSnw141292   if( pnColumn ) *pnColumn = 0;
141*c5c4113dSnw141292   if( pnRow ) *pnRow = 0;
142*c5c4113dSnw141292   res.zErrMsg = 0;
143*c5c4113dSnw141292   res.nResult = 0;
144*c5c4113dSnw141292   res.nRow = 0;
145*c5c4113dSnw141292   res.nColumn = 0;
146*c5c4113dSnw141292   res.nData = 1;
147*c5c4113dSnw141292   res.nAlloc = 20;
148*c5c4113dSnw141292   res.rc = SQLITE_OK;
149*c5c4113dSnw141292   res.azResult = malloc( sizeof(char*)*res.nAlloc );
150*c5c4113dSnw141292   if( res.azResult==0 ){
151*c5c4113dSnw141292     return SQLITE_NOMEM;
152*c5c4113dSnw141292   }
153*c5c4113dSnw141292   res.azResult[0] = 0;
154*c5c4113dSnw141292   rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg);
155*c5c4113dSnw141292   if( res.azResult ){
156*c5c4113dSnw141292     res.azResult[0] = (char*)res.nData;
157*c5c4113dSnw141292   }
158*c5c4113dSnw141292   if( rc==SQLITE_ABORT ){
159*c5c4113dSnw141292     sqlite_free_table(&res.azResult[1]);
160*c5c4113dSnw141292     if( res.zErrMsg ){
161*c5c4113dSnw141292       if( pzErrMsg ){
162*c5c4113dSnw141292         free(*pzErrMsg);
163*c5c4113dSnw141292         *pzErrMsg = res.zErrMsg;
164*c5c4113dSnw141292         sqliteStrRealloc(pzErrMsg);
165*c5c4113dSnw141292       }else{
166*c5c4113dSnw141292         sqliteFree(res.zErrMsg);
167*c5c4113dSnw141292       }
168*c5c4113dSnw141292     }
169*c5c4113dSnw141292     return res.rc;
170*c5c4113dSnw141292   }
171*c5c4113dSnw141292   sqliteFree(res.zErrMsg);
172*c5c4113dSnw141292   if( rc!=SQLITE_OK ){
173*c5c4113dSnw141292     sqlite_free_table(&res.azResult[1]);
174*c5c4113dSnw141292     return rc;
175*c5c4113dSnw141292   }
176*c5c4113dSnw141292   if( res.nAlloc>res.nData ){
177*c5c4113dSnw141292     char **azNew;
178*c5c4113dSnw141292     azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) );
179*c5c4113dSnw141292     if( azNew==0 ){
180*c5c4113dSnw141292       sqlite_free_table(&res.azResult[1]);
181*c5c4113dSnw141292       return SQLITE_NOMEM;
182*c5c4113dSnw141292     }
183*c5c4113dSnw141292     res.nAlloc = res.nData+1;
184*c5c4113dSnw141292     res.azResult = azNew;
185*c5c4113dSnw141292   }
186*c5c4113dSnw141292   *pazResult = &res.azResult[1];
187*c5c4113dSnw141292   if( pnColumn ) *pnColumn = res.nColumn;
188*c5c4113dSnw141292   if( pnRow ) *pnRow = res.nRow;
189*c5c4113dSnw141292   return rc;
190*c5c4113dSnw141292 }
191*c5c4113dSnw141292 
192*c5c4113dSnw141292 /*
193*c5c4113dSnw141292 ** This routine frees the space the sqlite_get_table() malloced.
194*c5c4113dSnw141292 */
sqlite_free_table(char ** azResult)195*c5c4113dSnw141292 void sqlite_free_table(
196*c5c4113dSnw141292   char **azResult             /* Result returned from from sqlite_get_table() */
197*c5c4113dSnw141292 ){
198*c5c4113dSnw141292   if( azResult ){
199*c5c4113dSnw141292     int i, n;
200*c5c4113dSnw141292     azResult--;
201*c5c4113dSnw141292     if( azResult==0 ) return;
202*c5c4113dSnw141292     n = (int)(long)azResult[0];
203*c5c4113dSnw141292     for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); }
204*c5c4113dSnw141292     free(azResult);
205*c5c4113dSnw141292   }
206*c5c4113dSnw141292 }
207