LCOV - code coverage report
Current view: top level - src - db_retrieve_version.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.6 % 45 34
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 1 1
Branches: 57.1 % 28 16

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file db_retrieve_version.c
       3                 :             :  * @brief Functions for checking metadata table existence and retrieving database version
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "precizer.h"
       7                 :             : 
       8                 :             : /**
       9                 :             :  * @brief Retrieve database version from the metadata table
      10                 :             :  *
      11                 :             :  * @details Opens database connection, checks for metadata table existence
      12                 :             :  *          and retrieves the version number if available. Handles all necessary
      13                 :             :  *          resource cleanup.
      14                 :             :  *
      15                 :             :  * @param[in] db_file_path Path to the SQLite database file
      16                 :             :  * @param[out] db_version Pointer to store the retrieved version number
      17                 :             :  *
      18                 :             :  * @return Return status codes:
      19                 :             :  *         - SUCCESS: Version retrieved successfully (may be 0 if not found)
      20                 :             :  *         - FAILURE: Database error or invalid parameters
      21                 :             :  */
      22                 :         397 : Return db_retrieve_version(
      23                 :             :         int        *db_version,
      24                 :             :         const char *db_file_path)
      25                 :             : {
      26                 :             :         /* Status returned by this function through provide()
      27                 :             :            Default value assumes successful completion */
      28                 :         397 :         Return status = SUCCESS;
      29                 :         397 :         sqlite3 *db = NULL;
      30                 :         397 :         sqlite3_stmt *table_exists_stmt = NULL;
      31                 :         397 :         sqlite3_stmt *version_stmt = NULL;
      32                 :         397 :         bool table_exists = false;
      33                 :         397 :         int rc = SQLITE_OK;
      34                 :             : 
      35                 :             :         /* Validate input parameters */
      36         [ -  + ]:         397 :         if(db_file_path == NULL)
      37                 :             :         {
      38                 :           0 :                 slog(ERROR,"Invalid input parameters: db_file_path\n");
      39                 :           0 :                 provide(FAILURE);
      40                 :             :         }
      41                 :             : 
      42                 :             :         /* Open database connection */
      43                 :         397 :         rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READONLY,NULL);
      44                 :             : 
      45         [ -  + ]:         397 :         if(SQLITE_OK != rc)
      46                 :             :         {
      47                 :           0 :                 log_sqlite_error(db,rc,NULL,"Failed to open database");
      48                 :           0 :                 status = FAILURE;
      49                 :             :         }
      50                 :             : 
      51                 :             :         /* Check if metadata table exists */
      52         [ +  - ]:         397 :         if(SUCCESS == status)
      53                 :             :         {
      54                 :         397 :                 const char *check_query = "SELECT name FROM sqlite_master WHERE type='table' AND name='metadata';";
      55                 :             : 
      56                 :         397 :                 rc = sqlite3_prepare_v2(db,check_query,-1,&table_exists_stmt,NULL);
      57                 :             : 
      58         [ -  + ]:         397 :                 if(SQLITE_OK != rc)
      59                 :             :                 {
      60                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to prepare table existence check query");
      61                 :           0 :                         status = FAILURE;
      62                 :             :                 }
      63                 :             :         }
      64                 :             : 
      65         [ +  - ]:         397 :         if(SUCCESS == status)
      66                 :             :         {
      67         [ +  + ]:         397 :                 if(SQLITE_ROW == sqlite3_step(table_exists_stmt))
      68                 :             :                 {
      69                 :         385 :                         table_exists = true;
      70                 :             :                 }
      71                 :             :         }
      72                 :             : 
      73                 :         397 :         sqlite3_finalize(table_exists_stmt);
      74                 :             : 
      75                 :             :         /* Retrieve version if table exists */
      76   [ +  -  +  + ]:         397 :         if(SUCCESS == status && table_exists == true)
      77                 :         385 :         {
      78                 :         385 :                 const char *version_query = "SELECT db_version FROM metadata;";
      79                 :             : 
      80                 :         385 :                 rc = sqlite3_prepare_v2(db,version_query,-1,&version_stmt,NULL);
      81                 :             : 
      82         [ -  + ]:         385 :                 if(SQLITE_OK != rc)
      83                 :             :                 {
      84                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to prepare version query");
      85                 :           0 :                         status = FAILURE;
      86                 :             :                 }
      87                 :             : 
      88         [ +  - ]:         385 :                 if(SUCCESS == status)
      89                 :             :                 {
      90         [ +  - ]:         385 :                         if(SQLITE_ROW == sqlite3_step(version_stmt))
      91                 :             :                         {
      92                 :         385 :                                 *db_version = sqlite3_column_int(version_stmt,0);
      93                 :         385 :                                 slog(TRACE,"Version number %d found in database\n",*db_version);
      94                 :             : 
      95                 :             :                         } else {
      96                 :           0 :                                 slog(TRACE,"No DB version data found in metadata table\n");
      97                 :             :                         }
      98                 :             :                 }
      99                 :             : 
     100         [ +  - ]:          12 :         } else if(SUCCESS == status){
     101                 :          12 :                 slog(TRACE,"Metadata table not found in database\n");
     102                 :             :         }
     103                 :             : 
     104                 :             :         /* Cleanup */
     105                 :         397 :         sqlite3_finalize(version_stmt);
     106                 :             : 
     107         [ +  - ]:         397 :         if(db != NULL)
     108                 :             :         {
     109                 :         397 :                 rc = sqlite3_close(db);
     110                 :             : 
     111         [ -  + ]:         397 :                 if(SQLITE_OK != rc)
     112                 :             :                 {
     113                 :           0 :                         log_sqlite_error(db,rc,NULL,"Warning: failed to close database");
     114                 :           0 :                         status = FAILURE;
     115                 :             :                 }
     116                 :             :         }
     117                 :             : 
     118                 :         397 :         provide(status);
     119                 :             : }
        

Generated by: LCOV version 2.0-1