table.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  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.
  10. **
  11. *************************************************************************
  12. ** This file contains the sqlite3_get_table() and sqlite3_free_table()
  13. ** interface routines. These are just wrappers around the main
  14. ** interface routine of sqlite3_exec().
  15. **
  16. ** These routines are in a separate files so that they will not be linked
  17. ** if they are not used.
  18. */
  19. #include "sqliteInt.h"
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #ifndef SQLITE_OMIT_GET_TABLE
  23. /*
  24. ** This structure is used to pass data from sqlite3_get_table() through
  25. ** to the callback function is uses to build the result.
  26. */
  27. typedef struct TabResult {
  28. char **azResult;
  29. char *zErrMsg;
  30. int nResult;
  31. int nAlloc;
  32. int nRow;
  33. int nColumn;
  34. int nData;
  35. int rc;
  36. } TabResult;
  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 sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
  43. TabResult *p = (TabResult*)pArg;
  44. int need;
  45. int i;
  46. char *z;
  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. */
  50. if( p->nRow==0 && argv!=0 ){
  51. need = nCol*2;
  52. }else{
  53. need = nCol;
  54. }
  55. if( p->nData + need >= p->nAlloc ){
  56. char **azNew;
  57. p->nAlloc = p->nAlloc*2 + need + 1;
  58. azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
  59. if( azNew==0 ) goto malloc_failed;
  60. p->azResult = azNew;
  61. }
  62. /* If this is the first row, then generate an extra row containing
  63. ** the names of all columns.
  64. */
  65. if( p->nRow==0 ){
  66. p->nColumn = nCol;
  67. for(i=0; i<nCol; i++){
  68. if( colv[i]==0 ){
  69. z = sqlite3_mprintf("");
  70. }else{
  71. z = sqlite3_mprintf("%s", colv[i]);
  72. }
  73. p->azResult[p->nData++] = z;
  74. }
  75. }else if( p->nColumn!=nCol ){
  76. sqlite3SetString(&p->zErrMsg,
  77. "sqlite3_get_table() called with two or more incompatible queries",
  78. (char*)0);
  79. p->rc = SQLITE_ERROR;
  80. return 1;
  81. }
  82. /* Copy over the row data
  83. */
  84. if( argv!=0 ){
  85. for(i=0; i<nCol; i++){
  86. if( argv[i]==0 ){
  87. z = 0;
  88. }else{
  89. int n = strlen(argv[i])+1;
  90. z = sqlite3_malloc( n );
  91. if( z==0 ) goto malloc_failed;
  92. memcpy(z, argv[i], n);
  93. }
  94. p->azResult[p->nData++] = z;
  95. }
  96. p->nRow++;
  97. }
  98. return 0;
  99. malloc_failed:
  100. p->rc = SQLITE_NOMEM;
  101. return 1;
  102. }
  103. /*
  104. ** Query the database. But instead of invoking a callback for each row,
  105. ** malloc() for space to hold the result and return the entire results
  106. ** at the conclusion of the call.
  107. **
  108. ** The result that is written to ***pazResult is held in memory obtained
  109. ** from malloc(). But the caller cannot free this memory directly.
  110. ** Instead, the entire table should be passed to sqlite3_free_table() when
  111. ** the calling procedure is finished using it.
  112. */
  113. int sqlite3_get_table(
  114. sqlite3 *db, /* The database on which the SQL executes */
  115. const char *zSql, /* The SQL to be executed */
  116. char ***pazResult, /* Write the result table here */
  117. int *pnRow, /* Write the number of rows in the result here */
  118. int *pnColumn, /* Write the number of columns of result here */
  119. char **pzErrMsg /* Write error messages here */
  120. ){
  121. int rc;
  122. TabResult res;
  123. if( pazResult==0 ){ return SQLITE_ERROR; }
  124. *pazResult = 0;
  125. if( pnColumn ) *pnColumn = 0;
  126. if( pnRow ) *pnRow = 0;
  127. res.zErrMsg = 0;
  128. res.nResult = 0;
  129. res.nRow = 0;
  130. res.nColumn = 0;
  131. res.nData = 1;
  132. res.nAlloc = 20;
  133. res.rc = SQLITE_OK;
  134. res.azResult = sqlite3_malloc( sizeof(char*)*res.nAlloc );
  135. if( res.azResult==0 ) return SQLITE_NOMEM;
  136. res.azResult[0] = 0;
  137. rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
  138. #ifndef NDEBUG
  139. sqlite3_mutex_enter(db->mutex);
  140. assert((rc&db->errMask)==rc && (res.rc&db->errMask)==res.rc);
  141. sqlite3_mutex_leave(db->mutex);
  142. #endif
  143. if( res.azResult ){
  144. assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
  145. res.azResult[0] = (char*)res.nData;
  146. }
  147. if( (rc&0xff)==SQLITE_ABORT ){
  148. sqlite3_free_table(&res.azResult[1]);
  149. if( res.zErrMsg ){
  150. if( pzErrMsg ){
  151. sqlite3_free(*pzErrMsg);
  152. *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
  153. }
  154. sqlite3_free(res.zErrMsg);
  155. }
  156. sqlite3_mutex_enter(db->mutex);
  157. db->errCode = res.rc;
  158. sqlite3_mutex_leave(db->mutex);
  159. return res.rc;
  160. }
  161. sqlite3_free(res.zErrMsg);
  162. if( rc!=SQLITE_OK ){
  163. sqlite3_free_table(&res.azResult[1]);
  164. return rc;
  165. }
  166. if( res.nAlloc>res.nData ){
  167. char **azNew;
  168. azNew = sqlite3_realloc( res.azResult, sizeof(char*)*(res.nData+1) );
  169. if( azNew==0 ){
  170. sqlite3_free_table(&res.azResult[1]);
  171. return SQLITE_NOMEM;
  172. }
  173. res.nAlloc = res.nData+1;
  174. res.azResult = azNew;
  175. }
  176. *pazResult = &res.azResult[1];
  177. if( pnColumn ) *pnColumn = res.nColumn;
  178. if( pnRow ) *pnRow = res.nRow;
  179. return rc;
  180. }
  181. /*
  182. ** This routine frees the space the sqlite3_get_table() malloced.
  183. */
  184. void sqlite3_free_table(
  185. char **azResult /* Result returned from from sqlite3_get_table() */
  186. ){
  187. if( azResult ){
  188. int i, n;
  189. azResult--;
  190. if( azResult==0 ) return;
  191. n = (int)azResult[0];
  192. for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
  193. sqlite3_free(azResult);
  194. }
  195. }
  196. #endif /* SQLITE_OMIT_GET_TABLE */