sqlite3intck.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ** 2024-02-08
  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. */
  13. /*
  14. ** Incremental Integrity-Check Extension
  15. ** -------------------------------------
  16. **
  17. ** This module contains code to check whether or not an SQLite database
  18. ** is well-formed or corrupt. This is the same task as performed by SQLite's
  19. ** built-in "PRAGMA integrity_check" command. This module differs from
  20. ** "PRAGMA integrity_check" in that:
  21. **
  22. ** + It is less thorough - this module does not detect certain types
  23. ** of corruption that are detected by the PRAGMA command. However,
  24. ** it does detect all kinds of corruption that are likely to cause
  25. ** errors in SQLite applications.
  26. **
  27. ** + It is slower. Sometimes up to three times slower.
  28. **
  29. ** + It allows integrity-check operations to be split into multiple
  30. ** transactions, so that the database does not need to be read-locked
  31. ** for the duration of the integrity-check.
  32. **
  33. ** One way to use the API to run integrity-check on the "main" database
  34. ** of handle db is:
  35. **
  36. ** int rc = SQLITE_OK;
  37. ** sqlite3_intck *p = 0;
  38. **
  39. ** sqlite3_intck_open(db, "main", &p);
  40. ** while( SQLITE_OK==sqlite3_intck_step(p) ){
  41. ** const char *zMsg = sqlite3_intck_message(p);
  42. ** if( zMsg ) printf("corruption: %s\n", zMsg);
  43. ** }
  44. ** rc = sqlite3_intck_error(p, &zErr);
  45. ** if( rc!=SQLITE_OK ){
  46. ** printf("error occured (rc=%d), (errmsg=%s)\n", rc, zErr);
  47. ** }
  48. ** sqlite3_intck_close(p);
  49. **
  50. ** Usually, the sqlite3_intck object opens a read transaction within the
  51. ** first call to sqlite3_intck_step() and holds it open until the
  52. ** integrity-check is complete. However, if sqlite3_intck_unlock() is
  53. ** called, the read transaction is ended and a new read transaction opened
  54. ** by the subsequent call to sqlite3_intck_step().
  55. */
  56. #ifndef _SQLITE_INTCK_H
  57. #define _SQLITE_INTCK_H
  58. #include "sqlite3.h"
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. /*
  63. ** An ongoing incremental integrity-check operation is represented by an
  64. ** opaque pointer of the following type.
  65. */
  66. typedef struct sqlite3_intck sqlite3_intck;
  67. /*
  68. ** Open a new incremental integrity-check object. If successful, populate
  69. ** output variable (*ppOut) with the new object handle and return SQLITE_OK.
  70. ** Or, if an error occurs, set (*ppOut) to NULL and return an SQLite error
  71. ** code (e.g. SQLITE_NOMEM).
  72. **
  73. ** The integrity-check will be conducted on database zDb (which must be "main",
  74. ** "temp", or the name of an attached database) of database handle db. Once
  75. ** this function has been called successfully, the caller should not use
  76. ** database handle db until the integrity-check object has been destroyed
  77. ** using sqlite3_intck_close().
  78. */
  79. int sqlite3_intck_open(
  80. sqlite3 *db, /* Database handle */
  81. const char *zDb, /* Database name ("main", "temp" etc.) */
  82. sqlite3_intck **ppOut /* OUT: New sqlite3_intck handle */
  83. );
  84. /*
  85. ** Close and release all resources associated with a handle opened by an
  86. ** earlier call to sqlite3_intck_open(). The results of using an
  87. ** integrity-check handle after it has been passed to this function are
  88. ** undefined.
  89. */
  90. void sqlite3_intck_close(sqlite3_intck *pCk);
  91. /*
  92. ** Do the next step of the integrity-check operation specified by the handle
  93. ** passed as the only argument. This function returns SQLITE_DONE if the
  94. ** integrity-check operation is finished, or an SQLite error code if
  95. ** an error occurs, or SQLITE_OK if no error occurs but the integrity-check
  96. ** is not finished. It is not considered an error if database corruption
  97. ** is encountered.
  98. **
  99. ** Following a successful call to sqlite3_intck_step() (one that returns
  100. ** SQLITE_OK), sqlite3_intck_message() returns a non-NULL value if
  101. ** corruption was detected in the db.
  102. **
  103. ** If an error occurs and a value other than SQLITE_OK or SQLITE_DONE is
  104. ** returned, then the integrity-check handle is placed in an error state.
  105. ** In this state all subsequent calls to sqlite3_intck_step() or
  106. ** sqlite3_intck_unlock() will immediately return the same error. The
  107. ** sqlite3_intck_error() method may be used to obtain an English language
  108. ** error message in this case.
  109. */
  110. int sqlite3_intck_step(sqlite3_intck *pCk);
  111. /*
  112. ** If the previous call to sqlite3_intck_step() encountered corruption
  113. ** within the database, then this function returns a pointer to a buffer
  114. ** containing a nul-terminated string describing the corruption in
  115. ** English. If the previous call to sqlite3_intck_step() did not encounter
  116. ** corruption, or if there was no previous call, this function returns
  117. ** NULL.
  118. */
  119. const char *sqlite3_intck_message(sqlite3_intck *pCk);
  120. /*
  121. ** Close any read-transaction opened by an earlier call to
  122. ** sqlite3_intck_step(). Any subsequent call to sqlite3_intck_step() will
  123. ** open a new transaction. Return SQLITE_OK if successful, or an SQLite error
  124. ** code otherwise.
  125. **
  126. ** If an error occurs, then the integrity-check handle is placed in an error
  127. ** state. In this state all subsequent calls to sqlite3_intck_step() or
  128. ** sqlite3_intck_unlock() will immediately return the same error. The
  129. ** sqlite3_intck_error() method may be used to obtain an English language
  130. ** error message in this case.
  131. */
  132. int sqlite3_intck_unlock(sqlite3_intck *pCk);
  133. /*
  134. ** If an error has occurred in an earlier call to sqlite3_intck_step()
  135. ** or sqlite3_intck_unlock(), then this method returns the associated
  136. ** SQLite error code. Additionally, if pzErr is not NULL, then (*pzErr)
  137. ** may be set to point to a nul-terminated string containing an English
  138. ** language error message. Or, if no error message is available, to
  139. ** NULL.
  140. **
  141. ** If no error has occurred within sqlite3_intck_step() or
  142. ** sqlite_intck_unlock() calls on the handle passed as the first argument,
  143. ** then SQLITE_OK is returned and (*pzErr) set to NULL.
  144. */
  145. int sqlite3_intck_error(sqlite3_intck *pCk, const char **pzErr);
  146. /*
  147. ** This API is used for testing only. It returns the full-text of an SQL
  148. ** statement used to test object zObj, which may be a table or index.
  149. ** The returned buffer is valid until the next call to either this function
  150. ** or sqlite3_intck_close() on the same sqlite3_intck handle.
  151. */
  152. const char *sqlite3_intck_test_sql(sqlite3_intck *pCk, const char *zObj);
  153. #ifdef __cplusplus
  154. } /* end of the 'extern "C"' block */
  155. #endif
  156. #endif /* ifndef _SQLITE_INTCK_H */