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-01-12 05:34:38 Functions: 100.0 % 1 1

            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           30 :         Return status = SUCCESS;
      25           30 :         sqlite3 *db = NULL;
      26           30 :         sqlite3_stmt *stmt = NULL;
      27           30 :         bool db_file_modified = false;
      28           30 :         int rc = SQLITE_OK;
      29              : 
      30              :         /* Validate input parameters */
      31           30 :         if(db_file_path == NULL)
      32              :         {
      33            0 :                 slog(ERROR,"Invalid input parameter: db_file_path\n");
      34            0 :                 provide(FAILURE);
      35              :         }
      36              : 
      37           30 :         if(global_interrupt_flag == true)
      38              :         {
      39            0 :                 slog(TRACE,"The program has been gracefully terminated. Store the current database version is not required\n");
      40            0 :                 provide(status);
      41              :         }
      42              : 
      43           30 :         if(config->dry_run == true)
      44              :         {
      45            0 :                 slog(TRACE,"Dry Run mode is enabled. Store the current database version is not required\n");
      46            0 :                 provide(status);
      47              :         }
      48              : 
      49              :         /* Open database connection */
      50           30 :         rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READWRITE,NULL);
      51              : 
      52           30 :         if(SQLITE_OK != rc)
      53              :         {
      54            0 :                 log_sqlite_error(db,rc,NULL,"Failed to open database");
      55            0 :                 status = FAILURE;
      56              :         }
      57              : 
      58           30 :         if(SUCCESS == status)
      59              :         {
      60              :                 /* Begin transaction */
      61           30 :                 rc = sqlite3_exec(db,"BEGIN TRANSACTION",NULL,NULL,NULL);
      62              : 
      63           30 :                 if(SQLITE_OK != rc)
      64              :                 {
      65            0 :                         log_sqlite_error(db,rc,NULL,"Failed to begin transaction");
      66            0 :                         status = FAILURE;
      67              :                 }
      68              :         }
      69              : 
      70           30 :         if(SUCCESS == status)
      71              :         {
      72              :                 /* Remove all from the table */
      73           30 :                 rc = sqlite3_exec(db,"DELETE FROM metadata;",NULL,NULL,NULL);
      74              : 
      75           30 :                 if(SQLITE_OK != rc)
      76              :                 {
      77            0 :                         log_sqlite_error(db,rc,NULL,"Failed to remove all from the table");
      78            0 :                         status = FAILURE;
      79              :                 }
      80              :         }
      81              : 
      82              :         /* Insert version number */
      83           30 :         if(SUCCESS == status)
      84              :         {
      85           30 :                 rc = sqlite3_prepare_v2(db,"INSERT INTO metadata (db_version) VALUES(?);",-1,&stmt,NULL);
      86              : 
      87           30 :                 if(SQLITE_OK != rc)
      88              :                 {
      89            0 :                         log_sqlite_error(db,rc,NULL,"Failed to prepare insert query");
      90            0 :                         status = FAILURE;
      91              :                 } else {
      92           30 :                         slog(TRACE,"The database version %d has been successfully stored in the DB\n",version);
      93              :                 }
      94              :         }
      95              : 
      96           30 :         if(SUCCESS == status)
      97              :         {
      98           30 :                 rc = sqlite3_bind_int(stmt,1,version);
      99              : 
     100           30 :                 if(SQLITE_OK != rc)
     101              :                 {
     102            0 :                         log_sqlite_error(db,rc,NULL,"Failed to bind version number");
     103            0 :                         status = FAILURE;
     104              :                 }
     105              :         }
     106              : 
     107           30 :         if(SUCCESS == status)
     108              :         {
     109           30 :                 rc = sqlite3_step(stmt);
     110              : 
     111           30 :                 if(SQLITE_DONE != rc)
     112              :                 {
     113            0 :                         log_sqlite_error(db,rc,NULL,"Failed to execute insert query");
     114            0 :                         status = FAILURE;
     115              :                 }
     116              :         }
     117              : 
     118           30 :         if(SUCCESS == status)
     119              :         {
     120              :                 /* Commit transaction */
     121           30 :                 rc = sqlite3_exec(db,"COMMIT",NULL,NULL,NULL);
     122              : 
     123           30 :                 if(SQLITE_OK != rc)
     124              :                 {
     125            0 :                         log_sqlite_error(db,rc,NULL,"Failed to commit transaction");
     126            0 :                         status = FAILURE;
     127              :                 } else {
     128           30 :                         db_file_modified = true;
     129              : 
     130           30 :                         if(strcmp(db_file_path,config->db_primary_file_path) == 0)
     131              :                         {
     132              :                                 /* Changes have been made to the database. Update
     133              :                                    this in the global variable value. */
     134           18 :                                 config->db_primary_file_modified = true;
     135              :                         }
     136              :                 }
     137              :         }
     138              : 
     139           30 :         if(SUCCESS != status)
     140              :         {
     141              :                 /* Attempt rollback */
     142            0 :                 rc = sqlite3_exec(db,"ROLLBACK",NULL,NULL,NULL);
     143              : 
     144            0 :                 if(SQLITE_OK == rc)
     145              :                 {
     146            0 :                         slog(TRACE,"The transaction has been rolled back\n");
     147              :                 } else {
     148            0 :                         log_sqlite_error(db,rc,NULL,"Failed to rollback transaction");
     149              :                 }
     150              :         }
     151              : 
     152              :         /* Cleanup */
     153           30 :         if(stmt != NULL)
     154              :         {
     155           30 :                 sqlite3_finalize(stmt);
     156              :         }
     157              : 
     158              :         /* Cleanup */
     159           30 :         call(db_close(db,&db_file_modified));
     160              : 
     161           30 :         provide(status);
     162              : }
        

Generated by: LCOV version 2.0-1