LCOV - code coverage report
Current view: top level - src - db_specify_version.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.3 % 62 38
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 1 1
Branches: 50.0 % 44 22

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file db_specify_version.c
       3                 :             :  * @brief
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "precizer.h"
       7                 :             : 
       8                 :             : /**
       9                 :             :  * @brief Store the current database version in the metadata table
      10                 :             :  *
      11                 :             :  * @details Opens database connection and sets version number to CURRENT_DB_VERSION
      12                 :             :  *          constant in the metadata table. Handles all necessary resource cleanup.
      13                 :             :  *
      14                 :             :  * @param[in] db_file_path Path to the SQLite database file
      15                 :             :  *
      16                 :             :  * @return Return status codes:
      17                 :             :  *         - SUCCESS: Version set successfully
      18                 :             :  *         - FAILURE: Database error or invalid parameters
      19                 :             :  */
      20                 :          30 : Return db_specify_version(
      21                 :             :         const char *db_file_path,
      22                 :             :         int        version)
      23                 :             : {
      24                 :             :         /* Status returned by this function through provide()
      25                 :             :            Default value assumes successful completion */
      26                 :          30 :         Return status = SUCCESS;
      27                 :          30 :         sqlite3 *db = NULL;
      28                 :          30 :         sqlite3_stmt *stmt = NULL;
      29                 :          30 :         bool db_file_modified = false;
      30                 :          30 :         int rc = SQLITE_OK;
      31                 :             : 
      32                 :             :         /* Validate input parameters */
      33         [ -  + ]:          30 :         if(db_file_path == NULL)
      34                 :             :         {
      35                 :           0 :                 slog(ERROR,"Invalid input parameter: db_file_path\n");
      36                 :           0 :                 provide(FAILURE);
      37                 :             :         }
      38                 :             : 
      39         [ -  + ]:          30 :         if(global_interrupt_flag == true)
      40                 :             :         {
      41                 :           0 :                 slog(TRACE,"The program has been gracefully terminated. Store the current database version is not required\n");
      42                 :           0 :                 provide(status);
      43                 :             :         }
      44                 :             : 
      45         [ -  + ]:          30 :         if(config->dry_run == true)
      46                 :             :         {
      47                 :           0 :                 slog(TRACE,"Dry Run mode is enabled. Store the current database version is not required\n");
      48                 :           0 :                 provide(status);
      49                 :             :         }
      50                 :             : 
      51                 :             :         /* Open database connection */
      52                 :          30 :         rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READWRITE,NULL);
      53                 :             : 
      54         [ -  + ]:          30 :         if(SQLITE_OK != rc)
      55                 :             :         {
      56                 :           0 :                 log_sqlite_error(db,rc,NULL,"Failed to open database");
      57                 :           0 :                 status = FAILURE;
      58                 :             :         }
      59                 :             : 
      60         [ +  - ]:          30 :         if(SUCCESS == status)
      61                 :             :         {
      62                 :             :                 /* Begin transaction */
      63                 :          30 :                 rc = sqlite3_exec(db,"BEGIN TRANSACTION",NULL,NULL,NULL);
      64                 :             : 
      65         [ -  + ]:          30 :                 if(SQLITE_OK != rc)
      66                 :             :                 {
      67                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to begin transaction");
      68                 :           0 :                         status = FAILURE;
      69                 :             :                 }
      70                 :             :         }
      71                 :             : 
      72         [ +  - ]:          30 :         if(SUCCESS == status)
      73                 :             :         {
      74                 :             :                 /* Remove all from the table */
      75                 :          30 :                 rc = sqlite3_exec(db,"DELETE FROM metadata;",NULL,NULL,NULL);
      76                 :             : 
      77         [ -  + ]:          30 :                 if(SQLITE_OK != rc)
      78                 :             :                 {
      79                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to remove all from the table");
      80                 :           0 :                         status = FAILURE;
      81                 :             :                 }
      82                 :             :         }
      83                 :             : 
      84                 :             :         /* Insert version number */
      85         [ +  - ]:          30 :         if(SUCCESS == status)
      86                 :             :         {
      87                 :          30 :                 rc = sqlite3_prepare_v2(db,"INSERT INTO metadata (db_version) VALUES(?);",-1,&stmt,NULL);
      88                 :             : 
      89         [ -  + ]:          30 :                 if(SQLITE_OK != rc)
      90                 :             :                 {
      91                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to prepare insert query");
      92                 :           0 :                         status = FAILURE;
      93                 :             :                 } else {
      94                 :          30 :                         slog(TRACE,"The database version %d has been successfully stored in the DB\n",version);
      95                 :             :                 }
      96                 :             :         }
      97                 :             : 
      98         [ +  - ]:          30 :         if(SUCCESS == status)
      99                 :             :         {
     100                 :          30 :                 rc = sqlite3_bind_int(stmt,1,version);
     101                 :             : 
     102         [ -  + ]:          30 :                 if(SQLITE_OK != rc)
     103                 :             :                 {
     104                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to bind version number");
     105                 :           0 :                         status = FAILURE;
     106                 :             :                 }
     107                 :             :         }
     108                 :             : 
     109         [ +  - ]:          30 :         if(SUCCESS == status)
     110                 :             :         {
     111                 :          30 :                 rc = sqlite3_step(stmt);
     112                 :             : 
     113         [ -  + ]:          30 :                 if(SQLITE_DONE != rc)
     114                 :             :                 {
     115                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to execute insert query");
     116                 :           0 :                         status = FAILURE;
     117                 :             :                 }
     118                 :             :         }
     119                 :             : 
     120         [ +  - ]:          30 :         if(SUCCESS == status)
     121                 :             :         {
     122                 :             :                 /* Commit transaction */
     123                 :          30 :                 rc = sqlite3_exec(db,"COMMIT",NULL,NULL,NULL);
     124                 :             : 
     125         [ -  + ]:          30 :                 if(SQLITE_OK != rc)
     126                 :             :                 {
     127                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to commit transaction");
     128                 :           0 :                         status = FAILURE;
     129                 :             :                 } else {
     130                 :          30 :                         db_file_modified = true;
     131                 :             : 
     132         [ +  + ]:          30 :                         if(strcmp(db_file_path,confstr(db_primary_file_path)) == 0)
     133                 :             :                         {
     134                 :             :                                 /* Changes have been made to the database. Update
     135                 :             :                                    this in the global variable value. */
     136                 :          14 :                                 config->db_primary_file_modified = true;
     137                 :             :                         }
     138                 :             :                 }
     139                 :             :         }
     140                 :             : 
     141         [ -  + ]:          30 :         if(SUCCESS != status)
     142                 :             :         {
     143                 :             :                 /* Attempt rollback */
     144                 :           0 :                 rc = sqlite3_exec(db,"ROLLBACK",NULL,NULL,NULL);
     145                 :             : 
     146         [ #  # ]:           0 :                 if(SQLITE_OK == rc)
     147                 :             :                 {
     148                 :           0 :                         slog(TRACE,"The transaction has been rolled back\n");
     149                 :             :                 } else {
     150                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to rollback transaction");
     151                 :             :                 }
     152                 :             :         }
     153                 :             : 
     154                 :             :         /* Cleanup */
     155         [ +  - ]:          30 :         if(stmt != NULL)
     156                 :             :         {
     157                 :          30 :                 sqlite3_finalize(stmt);
     158                 :             :         }
     159                 :             : 
     160                 :             :         /* Cleanup */
     161   [ -  +  -  + ]:          30 :         call(db_close(db,&db_file_modified));
     162                 :             : 
     163                 :          30 :         provide(status);
     164                 :             : }
        

Generated by: LCOV version 2.0-1