Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Return a diagnostic name for the selected SQLite open mode
5 : : *
6 : : * @return Pointer to a static string that describes `config->sqlite_open_flag`
7 : : */
8 : 496 : static const char *get_flag_string_by_code(void)
9 : : {
10 [ + + + + : 496 : switch(config->sqlite_open_flag)
- ]
11 : : {
12 : 131 : case SQLITE_OPEN_READONLY:
13 : 131 : return "SQLITE_OPEN_READONLY";
14 : : break;
15 : 156 : case SQLITE_OPEN_READWRITE:
16 : 156 : return "SQLITE_OPEN_READWRITE";
17 : : break;
18 : 203 : case SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE:
19 : 203 : return "SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE";
20 : : break;
21 : 6 : case SQL_DRY_RUN_MODE:
22 : 6 : return "SQL_DRY_RUN_MODE";
23 : : break;
24 : 0 : default:
25 : 0 : return "ERROR";
26 : : break;
27 : : }
28 : : }
29 : :
30 : : /**
31 : : * @brief Return a diagnostic name for table-initialization state
32 : : *
33 : : * @return `"true"` when database tables should be initialized, otherwise `"false"`
34 : : */
35 : 496 : static const char *get_initialize_string_by_code(void)
36 : : {
37 [ + + ]: 496 : if(config->db_initialize_tables == true)
38 : : {
39 : 209 : return "true";
40 : :
41 : : } else {
42 : 287 : return "false";
43 : : }
44 : : }
45 : :
46 : : /**
47 : : * @brief Select SQLite open flags and table-initialization behavior
48 : : *
49 : : * Uses the parsed command-line mode and primary database existence state to set
50 : : * `config->sqlite_open_flag` and `config->db_initialize_tables`. Compare mode
51 : : * opens read-only storage, update mode requires an existing database, normal
52 : : * mode creates a missing database, and dry-run mode either opens an existing
53 : : * database read-only or uses the in-memory dry-run flag when no physical
54 : : * database exists
55 : : *
56 : : * @return SUCCESS when a valid mode was selected, otherwise FAILURE
57 : : */
58 : 496 : Return db_determine_mode(void)
59 : : {
60 : : /* Status returned by this function through provide()
61 : : Default value assumes successful completion */
62 : 496 : Return status = SUCCESS;
63 : :
64 : : /* Interrupt the function smoothly */
65 : : /* Interrupt when Ctrl+C */
66 [ - + ]: 496 : if(global_interrupt_flag == true)
67 : : {
68 : 0 : provide(status);
69 : : }
70 : :
71 : : // Initialize tables of the database or not
72 : : // Default value
73 : 496 : config->db_initialize_tables = false;
74 : :
75 : : /// The flags parameter to sqlite3_open_v2()
76 : : /// must include, at a minimum, one of the
77 : : /// following flag combinations:
78 : : /// - SQLITE_OPEN_READONLY
79 : : /// - SQLITE_OPEN_READWRITE
80 : : /// - SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE
81 : : /// - SQL_DRY_RUN_MODE
82 : : /// Default value: RO
83 : 496 : config->sqlite_open_flag = SQLITE_OPEN_READONLY;
84 : :
85 : : // Update mode is enabled for the existing database
86 : : // The new DB file does NOT need to be created
87 [ + + ]: 496 : if(config->compare == true)
88 : : {
89 : : // Compare mode should not modify any database
90 : 118 : config->sqlite_open_flag = SQLITE_OPEN_READONLY;
91 : :
92 [ + + ]: 378 : } else if(config->update == true){
93 [ + + ]: 161 : if(config->db_primary_file_exists == false)
94 : : {
95 : : // The primary database file does NOT exists
96 : 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");
97 : 2 : status = FAILURE;
98 : : } else {
99 : 159 : config->sqlite_open_flag = SQLITE_OPEN_READWRITE;
100 : : }
101 : :
102 : : } else {
103 : :
104 [ + + ]: 217 : if(config->db_primary_file_exists == true)
105 : : {
106 : : // RW mode
107 : 8 : config->sqlite_open_flag = SQLITE_OPEN_READWRITE;
108 : :
109 : : } else {
110 : : // Initialize brand new database
111 : 209 : config->db_initialize_tables = true;
112 : :
113 : : // Regular mode
114 : 209 : config->sqlite_open_flag = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
115 : :
116 : : // The database file does NOT exists
117 : 209 : slog(EVERY,"The primary DB file not yet exists. Brand new database will be created\n");
118 : : }
119 : : }
120 : :
121 [ + + ]: 496 : if(SUCCESS == status)
122 : : {
123 [ + + ]: 494 : if(config->dry_run == true)
124 : : {
125 [ + + ]: 17 : if(config->db_primary_file_exists == false)
126 : : {
127 : : // Dry Run mode is activated and the primary database file hasn't been created
128 : : // before launching without this mode. In this case, the database will be
129 : : // created in memory and this won't affect any changes on disk.
130 : : //
131 : : // This describes a common database initialization scenario where:
132 : : // * The application is running in Dry Run mode (test/simulation mode)
133 : : // * No physical database file exists on disk from previous non-Dry Run executions
134 : : // * As a result, an in-memory database instance is created temporarily
135 : : // * No disk I/O or persistent storage operations will occur
136 : :
137 : : // In-memory special flag
138 : 6 : config->sqlite_open_flag = SQL_DRY_RUN_MODE;
139 : : } else {
140 : : // Keep the primary database read-only in Dry Run mode
141 : 11 : config->sqlite_open_flag = SQLITE_OPEN_READONLY;
142 : : }
143 : : }
144 : : }
145 : :
146 : 496 : slog(TRACE,"Final value for config->sqlite_open_flag: %s\n",get_flag_string_by_code());
147 : 496 : slog(TRACE,"Final value for config->db_initialize_tables: %s\n",get_initialize_string_by_code());
148 : :
149 : 496 : slog(TRACE,"DB mode determined\n");
150 : :
151 : 496 : provide(status);
152 : : }
|