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 | ************************************************************************* |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 12 | ** This file contains the sqlite_get_table() and sqlite_free_table() |
| 13 | ** interface routines. These are just wrappers around the main |
| 14 | ** interface routine of sqlite_exec(). |
| 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 | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 21 | #include "sqlite.h" |
| 22 | |
| 23 | /* |
| 24 | ** This structure is used to pass data from sqlite_get_table() through |
| 25 | ** to the callback function is uses to build the result. |
| 26 | */ |
| 27 | typedef struct TabResult { |
| 28 | char **azResult; |
| 29 | int nResult; |
| 30 | int nAlloc; |
| 31 | int nRow; |
| 32 | int nColumn; |
| 33 | int nData; |
| 34 | int rc; |
| 35 | } TabResult; |
| 36 | |
| 37 | /* |
| 38 | ** This routine is called once for each row in the result table. Its job |
| 39 | ** is to fill in the TabResult structure appropriately, allocating new |
| 40 | ** memory as necessary. |
| 41 | */ |
| 42 | static int sqlite_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ |
| 43 | TabResult *p = (TabResult*)pArg; |
| 44 | int need; |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 45 | int i; |
| 46 | char *z; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 47 | |
| 48 | /* Make sure there is enough space in p->azResult to hold everything |
| 49 | ** we need to remember from this invocation of the callback. |
| 50 | */ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 51 | if( p->nRow==0 && argv!=0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 52 | need = nCol*2; |
| 53 | }else{ |
| 54 | need = nCol; |
| 55 | } |
| 56 | if( p->nData + need >= p->nAlloc ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 57 | char **azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 58 | p->nAlloc = p->nAlloc*2 + need + 1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 59 | azNew = realloc( p->azResult, sizeof(char*)*p->nAlloc ); |
| 60 | if( azNew==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 61 | p->rc = SQLITE_NOMEM; |
| 62 | return 1; |
| 63 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 64 | p->azResult = azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 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 ){ |
drh | 01a3466 | 2001-10-20 12:30:10 +0000 | [diff] [blame] | 71 | p->nColumn = nCol; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 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 ){ |
| 78 | p->rc = SQLITE_NOMEM; |
| 79 | return 1; |
| 80 | } |
| 81 | strcpy(z, colv[i]); |
| 82 | } |
| 83 | p->azResult[p->nData++] = z; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* Copy over the row data |
| 88 | */ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 89 | if( argv!=0 ){ |
| 90 | for(i=0; i<nCol; i++){ |
| 91 | if( argv[i]==0 ){ |
| 92 | z = 0; |
| 93 | }else{ |
| 94 | z = malloc( strlen(argv[i])+1 ); |
| 95 | if( z==0 ){ |
| 96 | p->rc = SQLITE_NOMEM; |
| 97 | return 1; |
| 98 | } |
| 99 | strcpy(z, argv[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 100 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 101 | p->azResult[p->nData++] = z; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 102 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 103 | p->nRow++; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 104 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | ** Query the database. But instead of invoking a callback for each row, |
| 110 | ** malloc() for space to hold the result and return the entire results |
| 111 | ** at the conclusion of the call. |
| 112 | ** |
| 113 | ** The result that is written to ***pazResult is held in memory obtained |
| 114 | ** from malloc(). But the caller cannot free this memory directly. |
| 115 | ** Instead, the entire table should be passed to sqlite_free_table() when |
| 116 | ** the calling procedure is finished using it. |
| 117 | */ |
| 118 | int sqlite_get_table( |
| 119 | sqlite *db, /* The database on which the SQL executes */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 120 | const char *zSql, /* The SQL to be executed */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 121 | char ***pazResult, /* Write the result table here */ |
| 122 | int *pnRow, /* Write the number of rows in the result here */ |
| 123 | int *pnColumn, /* Write the number of columns of result here */ |
| 124 | char **pzErrMsg /* Write error messages here */ |
| 125 | ){ |
| 126 | int rc; |
| 127 | TabResult res; |
| 128 | if( pazResult==0 ){ return SQLITE_ERROR; } |
| 129 | *pazResult = 0; |
| 130 | if( pnColumn ) *pnColumn = 0; |
| 131 | if( pnRow ) *pnRow = 0; |
| 132 | res.nResult = 0; |
| 133 | res.nRow = 0; |
| 134 | res.nColumn = 0; |
| 135 | res.nData = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 136 | res.nAlloc = 20; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 137 | res.rc = SQLITE_OK; |
| 138 | res.azResult = malloc( sizeof(char*)*res.nAlloc ); |
| 139 | if( res.azResult==0 ){ |
| 140 | return SQLITE_NOMEM; |
| 141 | } |
| 142 | res.azResult[0] = 0; |
| 143 | rc = sqlite_exec(db, zSql, sqlite_get_table_cb, &res, pzErrMsg); |
| 144 | if( res.azResult ){ |
| 145 | res.azResult[0] = (char*)res.nData; |
| 146 | } |
| 147 | if( rc==SQLITE_ABORT ){ |
| 148 | sqlite_free_table(&res.azResult[1]); |
| 149 | return res.rc; |
| 150 | } |
| 151 | if( rc!=SQLITE_OK ){ |
| 152 | sqlite_free_table(&res.azResult[1]); |
| 153 | return rc; |
| 154 | } |
| 155 | if( res.nAlloc>res.nData ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 156 | char **azNew; |
| 157 | azNew = realloc( res.azResult, sizeof(char*)*(res.nData+1) ); |
| 158 | if( res.azResult==0 ){ |
| 159 | sqlite_free_table(&res.azResult[1]); |
| 160 | return SQLITE_NOMEM; |
| 161 | } |
| 162 | res.azResult = azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 163 | } |
| 164 | *pazResult = &res.azResult[1]; |
| 165 | if( pnColumn ) *pnColumn = res.nColumn; |
| 166 | if( pnRow ) *pnRow = res.nRow; |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | ** This routine frees the space the sqlite_get_table() malloced. |
| 172 | */ |
| 173 | void sqlite_free_table( |
| 174 | char **azResult /* Result returned from from sqlite_get_table() */ |
| 175 | ){ |
| 176 | if( azResult ){ |
| 177 | int i, n; |
| 178 | azResult--; |
| 179 | n = (int)azResult[0]; |
| 180 | for(i=1; i<n; i++){ if( azResult[i] ) free(azResult[i]); } |
| 181 | free(azResult); |
| 182 | } |
| 183 | } |