LCOV - code coverage report
Current view: top level - src - db_determine_mode.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.9 % 44 40
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 3 3

            Line data    Source code
       1              : #include "precizer.h"
       2              : 
       3              : /**
       4              :  * @brief Gets corresponding string based on provided code
       5              :  * @return Pointer to selected string constant
       6              :  */
       7          200 : static const char *get_flag_string_by_code(void)
       8              : {
       9          200 :         switch(config->sqlite_open_flag)
      10              :         {
      11           38 :                 case SQLITE_OPEN_READONLY:
      12           38 :                         return "SQLITE_OPEN_READONLY";
      13              :                         break;
      14           82 :                 case SQLITE_OPEN_READWRITE:
      15           82 :                         return "SQLITE_OPEN_READWRITE";
      16              :                         break;
      17           78 :                 case SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE:
      18           78 :                         return "SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE";
      19              :                         break;
      20            2 :                 case SQL_DRY_RUN_MODE:
      21            2 :                         return "SQL_DRY_RUN_MODE";
      22              :                         break;
      23            0 :                 default:
      24            0 :                         return "ERROR";
      25              :                         break;
      26              :         }
      27              : }
      28              : 
      29              : /**
      30              :  * @brief Gets corresponding string based on provided code
      31              :  * @return Pointer to selected string constant
      32              :  */
      33          200 : static const char *get_initialize_string_by_code(void)
      34              : {
      35          200 :         if(config->db_initialize_tables == true)
      36              :         {
      37           80 :                 return "true";
      38              : 
      39          120 :         } else if(config->db_initialize_tables == false){
      40          120 :                 return "false";
      41              : 
      42              :         } else {
      43              : 
      44            0 :                 return "ERROR!";
      45              :         }
      46              : }
      47              : 
      48              : /**
      49              :  *
      50              :  * @brief Define the database operation mode
      51              :  *
      52              :  */
      53          200 : Return db_determine_mode(void)
      54              : {
      55              :         /// The status that will be passed to return() before exiting.
      56              :         /// By default, the function worked without errors.
      57          200 :         Return status = SUCCESS;
      58              : 
      59              :         /* Interrupt the function smoothly */
      60              :         /* Interrupt when Ctrl+C */
      61          200 :         if(global_interrupt_flag == true)
      62              :         {
      63            0 :                 provide(status);
      64              :         }
      65              : 
      66              :         // Initialize tables of the database or not
      67              :         // Default value
      68          200 :         config->db_initialize_tables = false;
      69              : 
      70              :         /// The flags parameter to sqlite3_open_v2()
      71              :         /// must include, at a minimum, one of the
      72              :         /// following flag combinations:
      73              :         ///   - SQLITE_OPEN_READONLY
      74              :         ///   - SQLITE_OPEN_READWRITE
      75              :         ///   - SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE
      76              :         ///   - SQL_DRY_RUN_MODE
      77              :         /// Default value: RO
      78          200 :         config->sqlite_open_flag = SQLITE_OPEN_READONLY;
      79              : 
      80              :         // Update mode is enabled for the existing database
      81              :         // The new DB file does NOT need to be created
      82          200 :         if(config->compare == true)
      83              :         {
      84              :                 // Compare mode should not modify any database
      85           36 :                 config->sqlite_open_flag = SQLITE_OPEN_READONLY;
      86              : 
      87          164 :         } else if(config->update == true){
      88           80 :                 if(config->db_primary_file_exists == false)
      89              :                 {
      90              :                         // The primary database file does NOT exists
      91            2 :                         slog(ERROR,"Update mode is only available for existing databases. A brand new database file will not be created if it wasn't created previously\n");
      92            2 :                         status = FAILURE;
      93              :                 } else {
      94           78 :                         config->sqlite_open_flag = SQLITE_OPEN_READWRITE;
      95              :                 }
      96              : 
      97              :         } else {
      98              : 
      99           84 :                 if(config->db_primary_file_exists == true)
     100              :                 {
     101              :                         // RW mode
     102            4 :                         config->sqlite_open_flag = SQLITE_OPEN_READWRITE;
     103              : 
     104              :                 } else {
     105              :                         // Initialize brand new database
     106           80 :                         config->db_initialize_tables = true;
     107              : 
     108              :                         // Regular mode
     109           80 :                         config->sqlite_open_flag = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
     110              : 
     111              :                         // The database file does NOT exists
     112           80 :                         slog(EVERY,"The primary DB file not yet exists. Brand new database will be created\n");
     113              :                 }
     114              :         }
     115              : 
     116          200 :         if(SUCCESS == status)
     117              :         {
     118          198 :                 if(config->dry_run == true)
     119              :                 {
     120           10 :                         if(config->db_primary_file_exists == false)
     121              :                         {
     122              :                                 // Dry Run mode is activated and the primary database file hasn't been created
     123              :                                 // before launching without this mode. In this case, the database will be
     124              :                                 // created in memory and this won't affect any changes on disk.
     125              :                                 //
     126              :                                 // This describes a common database initialization scenario where:
     127              :                                 // * The application is running in Dry Run mode (test/simulation mode)
     128              :                                 // * No physical database file exists on disk from previous non-Dry Run executions
     129              :                                 // * As a result, an in-memory database instance is created temporarily
     130              :                                 // * No disk I/O or persistent storage operations will occur
     131              : 
     132              :                                 // In-memory special flag
     133            2 :                                 config->sqlite_open_flag = SQL_DRY_RUN_MODE;
     134              :                         }
     135              :                 }
     136              :         }
     137              : 
     138          200 :         slog(TRACE,"Final value for config->sqlite_open_flag: %s\n",get_flag_string_by_code());
     139          200 :         slog(TRACE,"Final value for config->db_initialize_tables: %s\n",get_initialize_string_by_code());
     140              : 
     141          200 :         slog(TRACE,"DB mode determined\n");
     142              : 
     143          200 :         provide(status);
     144              : }
        

Generated by: LCOV version 2.0-1