table.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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; /* Accumulated output */
  29. char *zErrMsg; /* Error message text, if an error occurs */
  30. int nAlloc; /* Slots allocated for azResult[] */
  31. int nRow; /* Number of rows in the result */
  32. int nColumn; /* Number of columns in the result */
  33. int nData; /* Slots used in azResult[]. (nRow+1)*nColumn */
  34. int rc; /* Return code from sqlite3_exec() */
  35. } TabResult;
  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 sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
  42. TabResult *p = (TabResult*)pArg; /* Result accumulator */
  43. int need; /* Slots needed in p->azResult[] */
  44. int i; /* Loop counter */
  45. char *z; /* A single column of result */
  46. /* Make sure there is enough space in p->azResult to hold everything
  47. ** we need to remember from this invocation of the callback.
  48. */
  49. if( p->nRow==0 && argv!=0 ){
  50. need = nCol*2;
  51. }else{
  52. need = nCol;
  53. }
  54. if( p->nData + need > p->nAlloc ){
  55. char **azNew;
  56. p->nAlloc = p->nAlloc*2 + need;
  57. azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
  58. if( azNew==0 ) goto malloc_failed;
  59. p->azResult = azNew;
  60. }
  61. /* If this is the first row, then generate an extra row containing
  62. ** the names of all columns.
  63. */
  64. if( p->nRow==0 ){
  65. p->nColumn = nCol;
  66. for(i=0; i<nCol; i++){
  67. z = sqlite3_mprintf("%s", colv[i]);
  68. if( z==0 ) goto malloc_failed;
  69. p->azResult[p->nData++] = z;
  70. }
  71. }else if( p->nColumn!=nCol ){
  72. sqlite3_free(p->zErrMsg);
  73. p->zErrMsg = sqlite3_mprintf(
  74. "sqlite3_get_table() called with two or more incompatible queries"
  75. );
  76. p->rc = SQLITE_ERROR;
  77. return 1;
  78. }
  79. /* Copy over the row data
  80. */
  81. if( argv!=0 ){
  82. for(i=0; i<nCol; i++){
  83. if( argv[i]==0 ){
  84. z = 0;
  85. }else{
  86. int n = sqlite3Strlen30(argv[i])+1;
  87. z = sqlite3_malloc( n );
  88. if( z==0 ) goto malloc_failed;
  89. memcpy(z, argv[i], n);
  90. }
  91. p->azResult[p->nData++] = z;
  92. }
  93. p->nRow++;
  94. }
  95. return 0;
  96. malloc_failed:
  97. p->rc = SQLITE_NOMEM;
  98. return 1;
  99. }
  100. /*
  101. ** Query the database. But instead of invoking a callback for each row,
  102. ** malloc() for space to hold the result and return the entire results
  103. ** at the conclusion of the call.
  104. **
  105. ** The result that is written to ***pazResult is held in memory obtained
  106. ** from malloc(). But the caller cannot free this memory directly.
  107. ** Instead, the entire table should be passed to sqlite3_free_table() when
  108. ** the calling procedure is finished using it.
  109. */
  110. int sqlite3_get_table(
  111. sqlite3 *db, /* The database on which the SQL executes */
  112. const char *zSql, /* The SQL to be executed */
  113. char ***pazResult, /* Write the result table here */
  114. int *pnRow, /* Write the number of rows in the result here */
  115. int *pnColumn, /* Write the number of columns of result here */
  116. char **pzErrMsg /* Write error messages here */
  117. ){
  118. int rc;
  119. TabResult res;
  120. *pazResult = 0;
  121. if( pnColumn ) *pnColumn = 0;
  122. if( pnRow ) *pnRow = 0;
  123. if( pzErrMsg ) *pzErrMsg = 0;
  124. res.zErrMsg = 0;
  125. res.nRow = 0;
  126. res.nColumn = 0;
  127. res.nData = 1;
  128. res.nAlloc = 20;
  129. res.rc = SQLITE_OK;
  130. res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
  131. if( res.azResult==0 ){
  132. db->errCode = SQLITE_NOMEM;
  133. return SQLITE_NOMEM;
  134. }
  135. res.azResult[0] = 0;
  136. rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
  137. assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
  138. res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
  139. if( (rc&0xff)==SQLITE_ABORT ){
  140. sqlite3_free_table(&res.azResult[1]);
  141. if( res.zErrMsg ){
  142. if( pzErrMsg ){
  143. sqlite3_free(*pzErrMsg);
  144. *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
  145. }
  146. sqlite3_free(res.zErrMsg);
  147. }
  148. db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
  149. return res.rc;
  150. }
  151. sqlite3_free(res.zErrMsg);
  152. if( rc!=SQLITE_OK ){
  153. sqlite3_free_table(&res.azResult[1]);
  154. return rc;
  155. }
  156. if( res.nAlloc>res.nData ){
  157. char **azNew;
  158. azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
  159. if( azNew==0 ){
  160. sqlite3_free_table(&res.azResult[1]);
  161. db->errCode = SQLITE_NOMEM;
  162. return SQLITE_NOMEM;
  163. }
  164. res.azResult = azNew;
  165. }
  166. *pazResult = &res.azResult[1];
  167. if( pnColumn ) *pnColumn = res.nColumn;
  168. if( pnRow ) *pnRow = res.nRow;
  169. return rc;
  170. }
  171. /*
  172. ** This routine frees the space the sqlite3_get_table() malloced.
  173. */
  174. void sqlite3_free_table(
  175. char **azResult /* Result returned from from sqlite3_get_table() */
  176. ){
  177. if( azResult ){
  178. int i, n;
  179. azResult--;
  180. assert( azResult!=0 );
  181. n = SQLITE_PTR_TO_INT(azResult[0]);
  182. for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
  183. sqlite3_free(azResult);
  184. }
  185. }
  186. #endif /* SQLITE_OMIT_GET_TABLE */