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