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 { |
drh | 73d34e9 | 2009-04-10 14:27:59 +0000 | [diff] [blame] | 30 | char **azResult; /* Accumulated output */ |
| 31 | char *zErrMsg; /* Error message text, if an error occurs */ |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 32 | u32 nAlloc; /* Slots allocated for azResult[] */ |
| 33 | u32 nRow; /* Number of rows in the result */ |
| 34 | u32 nColumn; /* Number of columns in the result */ |
| 35 | u32 nData; /* Slots used in azResult[]. (nRow+1)*nColumn */ |
drh | 73d34e9 | 2009-04-10 14:27:59 +0000 | [diff] [blame] | 36 | int rc; /* Return code from sqlite3_exec() */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 37 | } TabResult; |
| 38 | |
| 39 | /* |
| 40 | ** This routine is called once for each row in the result table. Its job |
| 41 | ** is to fill in the TabResult structure appropriately, allocating new |
| 42 | ** memory as necessary. |
| 43 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 44 | static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ |
drh | 73d34e9 | 2009-04-10 14:27:59 +0000 | [diff] [blame] | 45 | TabResult *p = (TabResult*)pArg; /* Result accumulator */ |
| 46 | int need; /* Slots needed in p->azResult[] */ |
| 47 | int i; /* Loop counter */ |
| 48 | char *z; /* A single column of result */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 49 | |
| 50 | /* Make sure there is enough space in p->azResult to hold everything |
| 51 | ** we need to remember from this invocation of the callback. |
| 52 | */ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 53 | if( p->nRow==0 && argv!=0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 54 | need = nCol*2; |
| 55 | }else{ |
| 56 | need = nCol; |
| 57 | } |
drh | 73d34e9 | 2009-04-10 14:27:59 +0000 | [diff] [blame] | 58 | if( p->nData + need > p->nAlloc ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 59 | char **azNew; |
drh | 73d34e9 | 2009-04-10 14:27:59 +0000 | [diff] [blame] | 60 | p->nAlloc = p->nAlloc*2 + need; |
drh | da4ca9d | 2014-09-09 17:27:35 +0000 | [diff] [blame] | 61 | azNew = sqlite3_realloc64( p->azResult, sizeof(char*)*p->nAlloc ); |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 62 | if( azNew==0 ) goto malloc_failed; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 63 | p->azResult = azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* If this is the first row, then generate an extra row containing |
| 67 | ** the names of all columns. |
| 68 | */ |
| 69 | if( p->nRow==0 ){ |
drh | 01a3466 | 2001-10-20 12:30:10 +0000 | [diff] [blame] | 70 | p->nColumn = nCol; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 71 | for(i=0; i<nCol; i++){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 72 | z = sqlite3_mprintf("%s", colv[i]); |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 73 | if( z==0 ) goto malloc_failed; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 74 | p->azResult[p->nData++] = z; |
| 75 | } |
drh | 3329a63 | 2014-09-18 01:21:43 +0000 | [diff] [blame^] | 76 | }else if( (int)p->nColumn!=nCol ){ |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 77 | sqlite3_free(p->zErrMsg); |
| 78 | p->zErrMsg = sqlite3_mprintf( |
| 79 | "sqlite3_get_table() called with two or more incompatible queries" |
| 80 | ); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 81 | p->rc = SQLITE_ERROR; |
| 82 | return 1; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Copy over the row data |
| 86 | */ |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 87 | if( argv!=0 ){ |
| 88 | for(i=0; i<nCol; i++){ |
| 89 | if( argv[i]==0 ){ |
| 90 | z = 0; |
| 91 | }else{ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 92 | int n = sqlite3Strlen30(argv[i])+1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 93 | z = sqlite3_malloc( n ); |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 94 | if( z==0 ) goto malloc_failed; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 95 | memcpy(z, argv[i], n); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 96 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 97 | p->azResult[p->nData++] = z; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 98 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 99 | p->nRow++; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 100 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 101 | return 0; |
drh | 779c6a0 | 2004-06-29 13:04:32 +0000 | [diff] [blame] | 102 | |
| 103 | malloc_failed: |
| 104 | p->rc = SQLITE_NOMEM; |
| 105 | return 1; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 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. |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 115 | ** Instead, the entire table should be passed to sqlite3_free_table() when |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 116 | ** the calling procedure is finished using it. |
| 117 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 118 | int sqlite3_get_table( |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 119 | sqlite3 *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; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 128 | |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 129 | *pazResult = 0; |
| 130 | if( pnColumn ) *pnColumn = 0; |
| 131 | if( pnRow ) *pnRow = 0; |
drh | 770b3cb | 2009-01-19 20:49:09 +0000 | [diff] [blame] | 132 | if( pzErrMsg ) *pzErrMsg = 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.nRow = 0; |
| 135 | res.nColumn = 0; |
| 136 | res.nData = 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 137 | res.nAlloc = 20; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 138 | res.rc = SQLITE_OK; |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 139 | res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc ); |
| 140 | if( res.azResult==0 ){ |
| 141 | db->errCode = SQLITE_NOMEM; |
| 142 | return SQLITE_NOMEM; |
| 143 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 144 | res.azResult[0] = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 145 | rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 146 | assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); |
shane | 1fc4129 | 2008-07-08 22:28:48 +0000 | [diff] [blame] | 147 | res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 148 | if( (rc&0xff)==SQLITE_ABORT ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 149 | sqlite3_free_table(&res.azResult[1]); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 150 | if( res.zErrMsg ){ |
| 151 | if( pzErrMsg ){ |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 152 | sqlite3_free(*pzErrMsg); |
drh | ae29ffb | 2004-09-25 14:39:18 +0000 | [diff] [blame] | 153 | *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 154 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 155 | sqlite3_free(res.zErrMsg); |
drh | f1a7a13 | 2002-03-23 00:52:01 +0000 | [diff] [blame] | 156 | } |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 157 | db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 158 | return res.rc; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 159 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 160 | sqlite3_free(res.zErrMsg); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 161 | if( rc!=SQLITE_OK ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 162 | sqlite3_free_table(&res.azResult[1]); |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 163 | return rc; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 164 | } |
| 165 | if( res.nAlloc>res.nData ){ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 166 | char **azNew; |
drh | 73d34e9 | 2009-04-10 14:27:59 +0000 | [diff] [blame] | 167 | azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData ); |
drh | 9335247 | 2003-05-17 00:05:49 +0000 | [diff] [blame] | 168 | if( azNew==0 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 169 | sqlite3_free_table(&res.azResult[1]); |
drh | 01495b9 | 2008-01-23 12:52:40 +0000 | [diff] [blame] | 170 | db->errCode = SQLITE_NOMEM; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 171 | return SQLITE_NOMEM; |
| 172 | } |
| 173 | res.azResult = azNew; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 174 | } |
| 175 | *pazResult = &res.azResult[1]; |
| 176 | if( pnColumn ) *pnColumn = res.nColumn; |
| 177 | if( pnRow ) *pnRow = res.nRow; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 178 | return rc; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 182 | ** This routine frees the space the sqlite3_get_table() malloced. |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 183 | */ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 184 | void sqlite3_free_table( |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 185 | char **azResult /* Result returned from sqlite3_get_table() */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 186 | ){ |
| 187 | if( azResult ){ |
drh | 7209c69 | 2008-04-27 18:40:11 +0000 | [diff] [blame] | 188 | int i, n; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 189 | azResult--; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 190 | assert( azResult!=0 ); |
shane | 1fc4129 | 2008-07-08 22:28:48 +0000 | [diff] [blame] | 191 | n = SQLITE_PTR_TO_INT(azResult[0]); |
drh | 28dd479 | 2006-06-26 21:35:44 +0000 | [diff] [blame] | 192 | for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); } |
| 193 | sqlite3_free(azResult); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
drh | ff55c35 | 2005-10-05 02:13:40 +0000 | [diff] [blame] | 196 | |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 197 | #endif /* SQLITE_OMIT_GET_TABLE */ |