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