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 | */ |
drh | 22465ce | 2005-11-24 22:33:05 +0000 | [diff] [blame] | 19 | #include "sqliteInt.h" |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 21 | #include <string.h> |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 22 | |
drh | ff55c35 | 2005-10-05 02:13:40 +0000 | [diff] [blame] | 23 | #ifndef SQLITE_OMIT_GET_TABLE |
| 24 | |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 25 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 26 | ** This structure is used to pass data from sqlite3_get_table() through |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 27 | ** to the callback function is uses to build the result. |
| 28 | */ |
| 29 | typedef struct TabResult { |
| 30 | char **azResult; |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 31 | char *zErrMsg; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 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 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 45 | 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] | 46 | TabResult *p = (TabResult*)pArg; |
| 47 | int need; |
drh | 7c68d60 | 2000-10-11 19:28:51 +0000 | [diff] [blame] | 48 | int i; |
| 49 | char *z; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 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 | */ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 54 | if( p->nRow==0 && argv!=0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 55 | need = nCol*2; |
| 56 | }else{ |
| 57 | need = nCol; |
| 58 | } |
| 59 | if( p->nData + need >= p->nAlloc ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 60 | char **azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 61 | p->nAlloc = p->nAlloc*2 + need + 1; |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 62 | azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc ); |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 63 | if( azNew==0 ) goto malloc_failed; |
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++){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 73 | z = sqlite3_mprintf("%s", colv[i]); |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 74 | if( z==0 ) goto malloc_failed; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 75 | p->azResult[p->nData++] = z; |
| 76 | } |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 77 | }else if( p->nColumn!=nCol ){ |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 78 | sqlite3_free(p->zErrMsg); |
| 79 | p->zErrMsg = sqlite3_mprintf( |
| 80 | "sqlite3_get_table() called with two or more incompatible queries" |
| 81 | ); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 82 | p->rc = SQLITE_ERROR; |
| 83 | return 1; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Copy over the row data |
| 87 | */ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 88 | if( argv!=0 ){ |
| 89 | for(i=0; i<nCol; i++){ |
| 90 | if( argv[i]==0 ){ |
| 91 | z = 0; |
| 92 | }else{ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 93 | int n = strlen(argv[i])+1; |
| 94 | z = sqlite3_malloc( n ); |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 95 | if( z==0 ) goto malloc_failed; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 96 | memcpy(z, argv[i], n); |
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; |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 103 | |
| 104 | malloc_failed: |
| 105 | p->rc = SQLITE_NOMEM; |
| 106 | return 1; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | ** Query the database. But instead of invoking a callback for each row, |
| 111 | ** malloc() for space to hold the result and return the entire results |
| 112 | ** at the conclusion of the call. |
| 113 | ** |
| 114 | ** The result that is written to ***pazResult is held in memory obtained |
| 115 | ** from malloc(). But the caller cannot free this memory directly. |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 116 | ** Instead, the entire table should be passed to sqlite3_free_table() when |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 117 | ** the calling procedure is finished using it. |
| 118 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 119 | int sqlite3_get_table( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 120 | sqlite3 *db, /* The database on which the SQL executes */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 121 | const char *zSql, /* The SQL to be executed */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 122 | char ***pazResult, /* Write the result table here */ |
| 123 | int *pnRow, /* Write the number of rows in the result here */ |
| 124 | int *pnColumn, /* Write the number of columns of result here */ |
| 125 | char **pzErrMsg /* Write error messages here */ |
| 126 | ){ |
| 127 | int rc; |
| 128 | TabResult res; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 129 | |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 130 | *pazResult = 0; |
| 131 | if( pnColumn ) *pnColumn = 0; |
| 132 | if( pnRow ) *pnRow = 0; |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 133 | res.zErrMsg = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 134 | res.nResult = 0; |
| 135 | res.nRow = 0; |
| 136 | res.nColumn = 0; |
| 137 | res.nData = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 138 | res.nAlloc = 20; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 139 | res.rc = SQLITE_OK; |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 140 | res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc ); |
| 141 | if( res.azResult==0 ){ |
| 142 | db->errCode = SQLITE_NOMEM; |
| 143 | return SQLITE_NOMEM; |
| 144 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 145 | res.azResult[0] = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 146 | rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 147 | assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); |
| 148 | res.azResult[0] = (char*)res.nData; |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 149 | if( (rc&0xff)==SQLITE_ABORT ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 150 | sqlite3_free_table(&res.azResult[1]); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 151 | if( res.zErrMsg ){ |
| 152 | if( pzErrMsg ){ |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 153 | sqlite3_free(*pzErrMsg); |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 154 | *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 155 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 156 | sqlite3_free(res.zErrMsg); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 157 | } |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 158 | db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 159 | return res.rc; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 160 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 161 | sqlite3_free(res.zErrMsg); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 162 | if( rc!=SQLITE_OK ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 163 | sqlite3_free_table(&res.azResult[1]); |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 164 | return rc; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 165 | } |
| 166 | if( res.nAlloc>res.nData ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 167 | char **azNew; |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 168 | azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) ); |
drh | 9335247 | 2003-05-17 00:05:49 +0000 | [diff] [blame] | 169 | if( azNew==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 170 | sqlite3_free_table(&res.azResult[1]); |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 171 | db->errCode = SQLITE_NOMEM; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 172 | return SQLITE_NOMEM; |
| 173 | } |
drh | 9335247 | 2003-05-17 00:05:49 +0000 | [diff] [blame] | 174 | res.nAlloc = res.nData+1; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 175 | res.azResult = azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 176 | } |
| 177 | *pazResult = &res.azResult[1]; |
| 178 | if( pnColumn ) *pnColumn = res.nColumn; |
| 179 | if( pnRow ) *pnRow = res.nRow; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 180 | return rc; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 184 | ** This routine frees the space the sqlite3_get_table() malloced. |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 185 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 186 | void sqlite3_free_table( |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 187 | char **azResult /* Result returned from from sqlite3_get_table() */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 188 | ){ |
| 189 | if( azResult ){ |
| 190 | int i, n; |
| 191 | azResult--; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame^] | 192 | assert( azResult!=0 ); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 193 | n = (int)azResult[0]; |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 194 | for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } |
| 195 | sqlite3_free(azResult); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
drh | ff55c35 | 2005-10-05 02:13:40 +0000 | [diff] [blame] | 198 | |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 199 | #endif /* SQLITE_OMIT_GET_TABLE */ |