Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Checks if the database exists and contains data
5 : : *
6 : : * @details This function verifies if the database has been previously created
7 : : * and contains any paths data. If the database exists and the update flag
8 : : * is not set, it warns the user about using the --update option.
9 : : *
10 : : * The function will skip the check if comparison mode is enabled.
11 : : *
12 : : * @note The function uses the global config structure to access database
13 : : * settings and flags.
14 : : *
15 : : * @return Return enum value:
16 : : * - SUCCESS: Check completed successfully
17 : : * - FAILURE: Error occurred during check
18 : : */
19 : 305 : Return db_contains_data(void)
20 : : {
21 : : /* Status returned by this function through provide()
22 : : Default value assumes successful completion */
23 : 305 : Return status = SUCCESS;
24 : :
25 : : /* Interrupt the function smoothly */
26 : : /* Interrupt when Ctrl+C */
27 [ - + ]: 305 : if(global_interrupt_flag == true)
28 : : {
29 : 0 : provide(status);
30 : : }
31 : :
32 : : /* Skip check if in comparison mode */
33 [ + + ]: 305 : if(config->compare == true)
34 : : {
35 : 70 : slog(TRACE,"Comparison mode is enabled. The primary database verification is not required\n");
36 : 70 : provide(status);
37 : : }
38 : :
39 : : /** @var sqlite3_stmt *select_stmt
40 : : * @brief SQLite prepared statement for counting paths
41 : : */
42 : 235 : sqlite3_stmt *select_stmt = NULL;
43 : 235 : int rc = 0;
44 : :
45 : : /* Initialize existence flag */
46 : 235 : config->db_contains_data = false;
47 : :
48 : : /** @var const char *sql_db_contains_data
49 : : * @brief SQL query to count rows in paths table
50 : : */
51 : 235 : const char *sql_db_contains_data = "SELECT COUNT(*) FROM paths";
52 : :
53 : : /* Prepare the SQL statement */
54 : 235 : rc = sqlite3_prepare_v2(config->db,sql_db_contains_data,-1,&select_stmt,NULL);
55 : :
56 [ - + ]: 235 : if(SQLITE_OK != rc)
57 : : {
58 : 0 : log_sqlite_error(config->db,rc,NULL,"Can't prepare select statement");
59 : 0 : status = FAILURE;
60 : : }
61 : :
62 [ + - ]: 235 : if(SUCCESS == status)
63 : : {
64 : : /* Execute the query and process results */
65 [ + + ]: 705 : while(SQLITE_ROW == (rc = sqlite3_step(select_stmt)))
66 : : {
67 : 235 : sqlite3_int64 rows = -1;
68 : 235 : rows = sqlite3_column_int64(select_stmt,0);
69 : :
70 [ + + ]: 235 : if(rows > 0)
71 : : {
72 : 110 : config->db_contains_data = true;
73 : : }
74 : : }
75 : :
76 : : /* Check for query execution errors */
77 [ - + ]: 235 : if(SQLITE_DONE != rc)
78 : : {
79 : 0 : log_sqlite_error(config->db,rc,NULL,"Select statement didn't finish with DONE");
80 : 0 : status = FAILURE;
81 : : }
82 : : }
83 : 235 : sqlite3_finalize(select_stmt);
84 : :
85 [ + - ]: 235 : if(SUCCESS == status)
86 : : {
87 : : /* Handle existing database case */
88 [ + + ]: 235 : if(config->db_contains_data == true)
89 : : {
90 [ + + ]: 110 : if(config->update == true)
91 : : {
92 : 108 : slog(TRACE,"The database %s has already been created previously\n",confstr(db_file_name));
93 : : } else {
94 : 2 : slog(EVERY,"The database %s was previously created and already contains data with files and their checksums."
95 : : " Use the " BOLD "--update" RESET " option only when you are certain"
96 : : " that the database needs to be updated and when file information"
97 : : " (including changes, deletions, and additions) should be synchronized"
98 : : " with the database.\n",confstr(db_file_name));
99 : 2 : status = WARNING;
100 : : }
101 : : }
102 : : }
103 : :
104 : 235 : provide(status);
105 : : }
|