LCOV - code coverage report
Current view: top level - src - db_finalize.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 62.5 % 32 20
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 1 1
Branches: 54.2 % 24 13

             Branch data     Line data    Source code
       1                 :             : #include "precizer.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Finalize a prepared SQLite statement and reset its pointer
       5                 :             :  *
       6                 :             :  * Also performs WAL checkpoint and cache flush in the same way as db_close()
       7                 :             :  * to ensure WAL/SHM files are cleaned up when the database was modified.
       8                 :             :  *
       9                 :             :  * @param[in] db Pointer to SQLite database handle
      10                 :             :  * @param[in] db_alias Attached database alias name
      11                 :             :  * @param[in,out] stmt Pointer to prepared statement pointer to finalize
      12                 :             :  * @return Return status code
      13                 :             :  */
      14                 :         140 : Return db_finalize(
      15                 :             :         sqlite3      *db,
      16                 :             :         const char   *db_alias,
      17                 :             :         sqlite3_stmt **stmt)
      18                 :             : {
      19                 :             :         /* Status returned by this function through provide()
      20                 :             :            Default value assumes successful completion */
      21                 :         140 :         Return status = SUCCESS;
      22                 :             : 
      23   [ +  -  +  -  :         140 :         if(db == NULL || db_alias == NULL || stmt == NULL)
                   -  + ]
      24                 :             :         {
      25                 :           0 :                 slog(ERROR,"Invalid input parameters: db=%p, db_alias=%p, stmt=%p\n",db,db_alias,stmt);
      26                 :           0 :                 status = FAILURE;
      27                 :             :         }
      28                 :             : 
      29         [ +  - ]:         140 :         if(SUCCESS == status)
      30                 :             :         {
      31         [ +  + ]:         140 :                 if(*stmt != NULL)
      32                 :             :                 {
      33                 :          58 :                         int rc = sqlite3_finalize(*stmt);
      34                 :             : 
      35         [ -  + ]:          58 :                         if(SQLITE_OK != rc)
      36                 :             :                         {
      37                 :           0 :                                 log_sqlite_error(db,rc,NULL,"Failed to finalize SQLite statement");
      38                 :           0 :                                 status = FAILURE;
      39                 :             :                         } else {
      40                 :          58 :                                 *stmt = NULL;
      41                 :             :                         }
      42                 :             :                 }
      43                 :             : 
      44                 :         140 :                 sqlite3_stmt *active_stmt = NULL;
      45                 :             : 
      46         [ -  + ]:         140 :                 while((active_stmt = sqlite3_next_stmt(db,NULL)) != NULL)
      47                 :             :                 {
      48                 :           0 :                         slog(ERROR,"Attention! The program is in the process of shutting down, but there are still uncompleted SQLite statements!\n");
      49                 :           0 :                         sqlite3_finalize(active_stmt);
      50                 :             :                 }
      51                 :             : 
      52                 :         140 :                 char *sql = NULL;
      53                 :             : 
      54         [ -  + ]:         140 :                 if(asprintf(&sql,
      55                 :             :                         "PRAGMA %s.journal_mode=DELETE;"
      56                 :             :                         "PRAGMA %s.fsync=ON;"
      57                 :             :                         "PRAGMA %s.synchronous=EXTRA;"
      58                 :             :                         "PRAGMA %s.locking_mode=EXCLUSIVE;",
      59                 :             :                         db_alias,
      60                 :             :                         db_alias,
      61                 :             :                         db_alias,
      62                 :             :                         db_alias) == -1)
      63                 :             :                 {
      64                 :           0 :                         status = FAILURE;
      65                 :           0 :                         report("Memory allocation failed for WAL checkpoint SQL");
      66                 :             :                 }
      67                 :             : 
      68         [ +  - ]:         140 :                 if(SUCCESS == status)
      69                 :             :                 {
      70                 :         140 :                         int rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
      71                 :             : 
      72         [ -  + ]:         140 :                         if(SQLITE_OK != rc)
      73                 :             :                         {
      74                 :           0 :                                 log_sqlite_error(db,rc,NULL,"Warning: failed to tune database integrity");
      75                 :           0 :                                 status = FAILURE;
      76                 :             :                         }
      77                 :             :                 }
      78                 :             : 
      79                 :         140 :                 free(sql);
      80                 :             : 
      81         [ +  - ]:         140 :                 if(SUCCESS == status)
      82                 :             :                 {
      83                 :         140 :                         int rc = sqlite3_db_cacheflush(db);
      84                 :             : 
      85         [ -  + ]:         140 :                         if(SQLITE_OK != rc)
      86                 :             :                         {
      87                 :           0 :                                 log_sqlite_error(db,rc,NULL,"Warning: failed to flush database");
      88                 :           0 :                                 status = FAILURE;
      89                 :             :                         }
      90                 :             :                 }
      91                 :             :         }
      92                 :             : 
      93                 :         140 :         provide(status);
      94                 :             : }
        

Generated by: LCOV version 2.0-1