Branch data 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 : 323 : static const char *get_flag_string_by_code(void)
8 : : {
9 [ + + + + : 323 : switch(config->sqlite_open_flag)
- ]
10 : : {
11 : 88 : case SQLITE_OPEN_READONLY:
12 : 88 : return "SQLITE_OPEN_READONLY";
13 : : break;
14 : 108 : case SQLITE_OPEN_READWRITE:
15 : 108 : return "SQLITE_OPEN_READWRITE";
16 : : break;
17 : 121 : case SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE:
18 : 121 : return "SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE";
19 : : break;
20 : 6 : case SQL_DRY_RUN_MODE:
21 : 6 : 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 : 323 : static const char *get_initialize_string_by_code(void)
34 : : {
35 [ + + ]: 323 : if(config->db_initialize_tables == true)
36 : : {
37 : 127 : return "true";
38 : :
39 [ + - ]: 196 : } else if(config->db_initialize_tables == false){
40 : 196 : 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 : 323 : Return db_determine_mode(void)
54 : : {
55 : : /* Status returned by this function through provide()
56 : : Default value assumes successful completion */
57 : 323 : Return status = SUCCESS;
58 : :
59 : : /* Interrupt the function smoothly */
60 : : /* Interrupt when Ctrl+C */
61 [ - + ]: 323 : if(global_interrupt_flag == true)
62 : : {
63 : 0 : provide(status);
64 : : }
65 : :
66 : : // Initialize tables of the database or not
67 : : // Default value
68 : 323 : 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 : 323 : 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 [ + + ]: 323 : if(config->compare == true)
83 : : {
84 : : // Compare mode should not modify any database
85 : 76 : config->sqlite_open_flag = SQLITE_OPEN_READONLY;
86 : :
87 [ + + ]: 247 : } else if(config->update == true){
88 [ + + ]: 112 : 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 : 110 : config->sqlite_open_flag = SQLITE_OPEN_READWRITE;
95 : : }
96 : :
97 : : } else {
98 : :
99 [ + + ]: 135 : if(config->db_primary_file_exists == true)
100 : : {
101 : : // RW mode
102 : 8 : config->sqlite_open_flag = SQLITE_OPEN_READWRITE;
103 : :
104 : : } else {
105 : : // Initialize brand new database
106 : 127 : config->db_initialize_tables = true;
107 : :
108 : : // Regular mode
109 : 127 : config->sqlite_open_flag = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
110 : :
111 : : // The database file does NOT exists
112 : 127 : slog(EVERY,"The primary DB file not yet exists. Brand new database will be created\n");
113 : : }
114 : : }
115 : :
116 [ + + ]: 323 : if(SUCCESS == status)
117 : : {
118 [ + + ]: 321 : if(config->dry_run == true)
119 : : {
120 [ + + ]: 16 : 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 : 6 : config->sqlite_open_flag = SQL_DRY_RUN_MODE;
134 : : } else {
135 : : // Keep the primary database read-only in Dry Run mode
136 : 10 : config->sqlite_open_flag = SQLITE_OPEN_READONLY;
137 : : }
138 : : }
139 : : }
140 : :
141 : 323 : slog(TRACE,"Final value for config->sqlite_open_flag: %s\n",get_flag_string_by_code());
142 : 323 : slog(TRACE,"Final value for config->db_initialize_tables: %s\n",get_initialize_string_by_code());
143 : :
144 : 323 : slog(TRACE,"DB mode determined\n");
145 : :
146 : 323 : provide(status);
147 : : }
|