LCOV - code coverage report
Current view: top level - src - db_close.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 66.7 % 24 16
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 1 1

            Line data    Source code
       1              : #include "precizer.h"
       2              : 
       3              : /**
       4              :  * Disable journaling, flush the journal to the main database,
       5              :  * clear the cache, and close the database
       6              :  *
       7              :  */
       8          385 : Return db_close(
       9              :         sqlite3    *db,
      10              :         const bool *db_file_modified)
      11              : {
      12          385 :         Return status = SUCCESS;
      13          385 :         int rc = SQLITE_OK;
      14              : 
      15              :         /* Cleanup and close previously used DB */
      16          385 :         if(db != NULL)
      17              :         {
      18          376 :                 if(*db_file_modified == true)
      19              :                 {
      20              :                         sqlite3_stmt *stmt;
      21              : 
      22          288 :                         while((stmt = sqlite3_next_stmt(db,NULL)) != NULL)
      23              :                         {
      24            0 :                                 slog(ERROR,"Attention! The program is in the process of shutting down, but there are still uncompleted SQLite statements!\n");
      25            0 :                                 sqlite3_finalize(stmt);
      26              :                         }
      27              : 
      28              :                         /**
      29              :                          * @brief Configure SQLite for maximum reliability using PRAGMA
      30              :                          * @note This is the second approach to ensure data integrity
      31              :                          * @details Sets synchronous mode to FULL for maximum durability
      32              :                          */
      33          288 :                         const char *sql =
      34              :                                 "PRAGMA journal_mode=DELETE;"
      35              :                                 "PRAGMA fsync=ON;"
      36              :                                 "PRAGMA synchronous=EXTRA;"
      37              :                                 "PRAGMA locking_mode=EXCLUSIVE;";
      38              : 
      39          288 :                         rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
      40              : 
      41          288 :                         if(SQLITE_OK != rc)
      42              :                         {
      43            0 :                                 log_sqlite_error(db,rc,NULL,"Warning: failed to tune database integrity");
      44            0 :                                 status = FAILURE;
      45              :                         }
      46              :                 }
      47              : 
      48              :                 /**
      49              :                  * @brief Force cache flush to disk for data persistence
      50              :                  * @note This is the first approach to ensure data integrity
      51              :                  */
      52          376 :                 rc = sqlite3_db_cacheflush(db);
      53              : 
      54          376 :                 if(SQLITE_OK != rc)
      55              :                 {
      56            0 :                         log_sqlite_error(db,rc,NULL,"Warning: failed to flush database");
      57            0 :                         status = FAILURE;
      58              :                 }
      59              : 
      60              :                 /**
      61              :                  * @brief Close database connection and cleanup resources
      62              :                  * @note Must be called to prevent resource leaks
      63              :                  */
      64          376 :                 rc = sqlite3_close(db);
      65              : 
      66          376 :                 if(SQLITE_OK != rc)
      67              :                 {
      68            0 :                         log_sqlite_error(db,rc,NULL,"Warning: failed to close database");
      69            0 :                         status = FAILURE;
      70              :                 }
      71              : 
      72          376 :                 if(SUCCESS == status)
      73              :                 {
      74          376 :                         slog(TRACE,"The database connection has been closed\n");
      75              :                 }
      76              :         }
      77              : 
      78          385 :         provide(status);
      79              : }
        

Generated by: LCOV version 2.0-1