LCOV - code coverage report
Current view: top level - src - db_get_version.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 76.6 % 47 36
Test Date: 2026-03-01 04:31:48 Functions: 100.0 % 1 1
Branches: 59.4 % 32 19

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             :  * @file db_get_version.c
       3                 :             :  * @brief Functions for checking metadata table existence and querying database version
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "precizer.h"
       7                 :             : 
       8                 :             : /**
       9                 :             :  * @brief Retrieves database version from the metadata table
      10                 :             :  *
      11                 :             :  * @details Opens database connection, checks for metadata table existence
      12                 :             :  *          and retrieves 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                 :         264 : Return db_get_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                 :         264 :         Return status = SUCCESS;
      29                 :         264 :         sqlite3 *db = NULL;
      30                 :         264 :         sqlite3_stmt *stmt = NULL;
      31                 :         264 :         bool table_exists = false;
      32                 :         264 :         int rc = SQLITE_OK;
      33                 :             : 
      34                 :             :         /* Validate input parameters */
      35         [ -  + ]:         264 :         if(db_file_path == NULL)
      36                 :             :         {
      37                 :           0 :                 slog(ERROR,"Invalid input parameters: db_file_path\n");
      38                 :           0 :                 provide(FAILURE);
      39                 :             :         }
      40                 :             : 
      41                 :             :         /* Open database connection */
      42                 :         264 :         rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READONLY,NULL);
      43                 :             : 
      44         [ -  + ]:         264 :         if(SQLITE_OK != rc)
      45                 :             :         {
      46                 :           0 :                 log_sqlite_error(db,rc,NULL,"Failed to open database");
      47                 :           0 :                 status = FAILURE;
      48                 :             :         }
      49                 :             : 
      50                 :             :         /* Check if metadata table exists */
      51         [ +  - ]:         264 :         if(SUCCESS == status)
      52                 :             :         {
      53                 :         264 :                 const char *check_query = "SELECT name FROM sqlite_master WHERE type='table' AND name='metadata';";
      54                 :             : 
      55                 :         264 :                 rc = sqlite3_prepare_v2(db,check_query,-1,&stmt,NULL);
      56                 :             : 
      57         [ -  + ]:         264 :                 if(SQLITE_OK != rc)
      58                 :             :                 {
      59                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to prepare table existence check query");
      60                 :           0 :                         status = FAILURE;
      61                 :             :                 }
      62                 :             :         }
      63                 :             : 
      64         [ +  - ]:         264 :         if(SUCCESS == status)
      65                 :             :         {
      66         [ +  + ]:         264 :                 if(SQLITE_ROW == sqlite3_step(stmt))
      67                 :             :                 {
      68                 :         252 :                         table_exists = true;
      69                 :             :                 }
      70                 :             :         }
      71                 :             : 
      72         [ +  - ]:         264 :         if(stmt != NULL)
      73                 :             :         {
      74                 :         264 :                 sqlite3_finalize(stmt);
      75                 :         264 :                 stmt = NULL;
      76                 :             :         }
      77                 :             : 
      78                 :             :         /* Get version if table exists */
      79   [ +  -  +  + ]:         264 :         if(SUCCESS == status && table_exists == true)
      80                 :         252 :         {
      81                 :         252 :                 const char *version_query = "SELECT db_version FROM metadata;";
      82                 :             : 
      83                 :         252 :                 rc = sqlite3_prepare_v2(db,version_query,-1,&stmt,NULL);
      84                 :             : 
      85         [ -  + ]:         252 :                 if(SQLITE_OK != rc)
      86                 :             :                 {
      87                 :           0 :                         log_sqlite_error(db,rc,NULL,"Failed to prepare version query");
      88                 :           0 :                         status = FAILURE;
      89                 :             :                 }
      90                 :             : 
      91         [ +  - ]:         252 :                 if(SUCCESS == status)
      92                 :             :                 {
      93         [ +  - ]:         252 :                         if(SQLITE_ROW == sqlite3_step(stmt))
      94                 :             :                         {
      95                 :         252 :                                 *db_version = sqlite3_column_int(stmt,0);
      96                 :         252 :                                 slog(TRACE,"Version number %d found in database\n",*db_version);
      97                 :             : 
      98                 :             :                         } else {
      99                 :           0 :                                 slog(TRACE,"No DB version data found in metadata table\n");
     100                 :             :                         }
     101                 :             :                 }
     102                 :             : 
     103         [ +  - ]:          12 :         } else if(SUCCESS == status){
     104                 :          12 :                 slog(TRACE,"Metadata table not found in database\n");
     105                 :             :         }
     106                 :             : 
     107                 :             :         /* Cleanup */
     108         [ +  + ]:         264 :         if(stmt != NULL)
     109                 :             :         {
     110                 :         252 :                 sqlite3_finalize(stmt);
     111                 :             :         }
     112                 :             : 
     113         [ +  - ]:         264 :         if(db != NULL)
     114                 :             :         {
     115                 :         264 :                 rc = sqlite3_close(db);
     116                 :             : 
     117         [ -  + ]:         264 :                 if(SQLITE_OK != rc)
     118                 :             :                 {
     119                 :           0 :                         log_sqlite_error(db,rc,NULL,"Warning: failed to close database");
     120                 :           0 :                         status = FAILURE;
     121                 :             :                 }
     122                 :             :         }
     123                 :             : 
     124                 :         264 :         provide(status);
     125                 :             : }
        

Generated by: LCOV version 2.0-1