LCOV - code coverage report
Current view: top level - src - db_close.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 65.2 % 23 15
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 1 1
Branches: 64.3 % 14 9

             Branch data     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                 :         624 : Return db_close(
       9                 :             :         sqlite3    *db,
      10                 :             :         const bool *db_file_modified)
      11                 :             : {
      12                 :             :         /* Status returned by this function through provide()
      13                 :             :            Default value assumes successful completion */
      14                 :         624 :         Return status = SUCCESS;
      15                 :             : 
      16                 :             :         /* Cleanup and close previously used DB */
      17         [ +  + ]:         624 :         if(db != NULL)
      18                 :             :         {
      19                 :             :                 int rc;
      20                 :             : 
      21         [ +  + ]:         560 :                 if(*db_file_modified == true)
      22                 :             :                 {
      23                 :             :                         sqlite3_stmt *stmt;
      24                 :             : 
      25         [ -  + ]:         412 :                         while((stmt = sqlite3_next_stmt(db,NULL)) != NULL)
      26                 :             :                         {
      27                 :           0 :                                 slog(ERROR,"Attention! The program is in the process of shutting down, but there are still uncompleted SQLite statements!\n");
      28                 :           0 :                                 sqlite3_finalize(stmt);
      29                 :             :                         }
      30                 :             : 
      31                 :             :                         /**
      32                 :             :                          * @brief Configure SQLite for maximum reliability using PRAGMA
      33                 :             :                          * @note This is the second approach to ensure data integrity
      34                 :             :                          * @details Sets synchronous mode to FULL for maximum durability
      35                 :             :                          */
      36                 :         412 :                         const char *sql =
      37                 :             :                                 "PRAGMA journal_mode=DELETE;"
      38                 :             :                                 "PRAGMA fsync=ON;"
      39                 :             :                                 "PRAGMA synchronous=EXTRA;"
      40                 :             :                                 "PRAGMA locking_mode=EXCLUSIVE;";
      41                 :             : 
      42                 :         412 :                         rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
      43                 :             : 
      44         [ -  + ]:         412 :                         if(SQLITE_OK != rc)
      45                 :             :                         {
      46                 :           0 :                                 log_sqlite_error(db,rc,NULL,"Warning: failed to tune database integrity");
      47                 :           0 :                                 status = FAILURE;
      48                 :             :                         }
      49                 :             :                 }
      50                 :             : 
      51                 :             :                 /**
      52                 :             :                  * @brief Force cache flush to disk for data persistence
      53                 :             :                  * @note This is the first approach to ensure data integrity
      54                 :             :                  */
      55                 :         560 :                 rc = sqlite3_db_cacheflush(db);
      56                 :             : 
      57         [ -  + ]:         560 :                 if(SQLITE_OK != rc)
      58                 :             :                 {
      59                 :           0 :                         log_sqlite_error(db,rc,NULL,"Warning: failed to flush database");
      60                 :           0 :                         status = FAILURE;
      61                 :             :                 }
      62                 :             : 
      63                 :             :                 /**
      64                 :             :                  * @brief Close database connection and cleanup resources
      65                 :             :                  * @note Must be called to prevent resource leaks
      66                 :             :                  */
      67                 :         560 :                 rc = sqlite3_close(db);
      68                 :             : 
      69         [ -  + ]:         560 :                 if(SQLITE_OK != rc)
      70                 :             :                 {
      71                 :           0 :                         log_sqlite_error(db,rc,NULL,"Warning: failed to close database");
      72                 :           0 :                         status = FAILURE;
      73                 :             :                 }
      74                 :             : 
      75         [ +  - ]:         560 :                 if(SUCCESS == status)
      76                 :             :                 {
      77                 :         560 :                         slog(TRACE,"The database connection has been closed\n");
      78                 :             :                 }
      79                 :             :         }
      80                 :             : 
      81                 :         624 :         provide(status);
      82                 :             : }
        

Generated by: LCOV version 2.0-1