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