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 188 : Return db_contains_data(void)
20 : {
21 : /** @var Return status
22 : * @brief The status that will be passed to return() before exiting
23 : * @details By default, the function worked without errors
24 : */
25 188 : Return status = SUCCESS;
26 :
27 : /* Interrupt the function smoothly */
28 : /* Interrupt when Ctrl+C */
29 188 : if(global_interrupt_flag == true)
30 : {
31 0 : provide(status);
32 : }
33 :
34 : /* Skip check if in comparison mode */
35 188 : if(config->compare == true)
36 : {
37 32 : slog(TRACE,"Comparison mode is enabled. The primary database verification is not required\n");
38 32 : provide(status);
39 : }
40 :
41 : /** @var sqlite3_stmt *select_stmt
42 : * @brief SQLite prepared statement for counting paths
43 : */
44 156 : sqlite3_stmt *select_stmt = NULL;
45 156 : int rc = 0;
46 :
47 : /* Initialize existence flag */
48 156 : config->db_contains_data = false;
49 :
50 : /** @var const char *sql_db_contains_data
51 : * @brief SQL query to count rows in paths table
52 : */
53 156 : const char *sql_db_contains_data = "SELECT COUNT(*) FROM paths";
54 :
55 : /* Prepare the SQL statement */
56 156 : rc = sqlite3_prepare_v2(config->db,sql_db_contains_data,-1,&select_stmt,NULL);
57 :
58 156 : if(SQLITE_OK != rc)
59 : {
60 0 : log_sqlite_error(config->db,rc,NULL,"Can't prepare select statement");
61 0 : status = FAILURE;
62 : }
63 :
64 156 : if(SUCCESS == status)
65 : {
66 : /* Execute the query and process results */
67 468 : while(SQLITE_ROW == (rc = sqlite3_step(select_stmt)))
68 : {
69 156 : sqlite3_int64 rows = -1;
70 156 : rows = sqlite3_column_int64(select_stmt,0);
71 :
72 156 : if(rows > 0)
73 : {
74 78 : config->db_contains_data = true;
75 : }
76 : }
77 :
78 : /* Check for query execution errors */
79 156 : if(SQLITE_DONE != rc)
80 : {
81 0 : log_sqlite_error(config->db,rc,NULL,"Select statement didn't finish with DONE");
82 0 : status = FAILURE;
83 : }
84 : }
85 156 : sqlite3_finalize(select_stmt);
86 :
87 156 : if(SUCCESS == status)
88 : {
89 : /* Handle existing database case */
90 156 : if(config->db_contains_data == true)
91 : {
92 78 : if(config->update == true)
93 : {
94 76 : slog(TRACE,"The database %s has already been created previously\n",config->db_file_name);
95 : } else {
96 2 : slog(EVERY,"The database %s was previously created and already contains data with files and their checksums."
97 : " Use the " BOLD "--update" RESET " option only when you are certain"
98 : " that the database needs to be updated and when file information"
99 : " (including changes, deletions, and additions) should be synchronized"
100 : " with the database.\n",config->db_file_name);
101 2 : status = WARNING;
102 : }
103 : }
104 : }
105 :
106 156 : provide(status);
107 : }
|