Line data Source code
1 : /**
2 : * @file db_get_version.c
3 : * @brief Functions for checking metadata table existence and querying database version
4 : */
5 :
6 : #include "precizer.h"
7 :
8 : /**
9 : * @brief Retrieves database version from the metadata table
10 : *
11 : * @details Opens database connection, checks for metadata table existence
12 : * and retrieves version number if available. Handles all necessary
13 : * resource cleanup.
14 : *
15 : * @param[in] db_file_path Path to the SQLite database file
16 : * @param[out] db_version Pointer to store the retrieved version number
17 : *
18 : * @return Return status codes:
19 : * - SUCCESS: Version retrieved successfully (may be 0 if not found)
20 : * - FAILURE: Database error or invalid parameters
21 : */
22 148 : Return db_get_version(
23 : int *db_version,
24 : const char *db_file_path)
25 : {
26 148 : Return status = SUCCESS;
27 148 : sqlite3 *db = NULL;
28 148 : sqlite3_stmt *stmt = NULL;
29 148 : bool table_exists = false;
30 148 : int rc = SQLITE_OK;
31 :
32 : /* Validate input parameters */
33 148 : if(db_file_path == NULL)
34 : {
35 0 : slog(ERROR,"Invalid input parameters: db_file_path\n");
36 0 : provide(FAILURE);
37 : }
38 :
39 : /* Open database connection */
40 148 : rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READONLY,NULL);
41 :
42 148 : if(SQLITE_OK != rc)
43 : {
44 0 : log_sqlite_error(db,rc,NULL,"Failed to open database");
45 0 : status = FAILURE;
46 : }
47 :
48 : /* Check if metadata table exists */
49 148 : if(SUCCESS == status)
50 : {
51 148 : const char *check_query = "SELECT name FROM sqlite_master WHERE type='table' AND name='metadata';";
52 :
53 148 : rc = sqlite3_prepare_v2(db,check_query,-1,&stmt,NULL);
54 :
55 148 : if(SQLITE_OK != rc)
56 : {
57 0 : log_sqlite_error(db,rc,NULL,"Failed to prepare table existence check query");
58 0 : status = FAILURE;
59 : }
60 : }
61 :
62 148 : if(SUCCESS == status)
63 : {
64 148 : if(SQLITE_ROW == sqlite3_step(stmt))
65 : {
66 138 : table_exists = true;
67 : }
68 : }
69 :
70 148 : if(stmt != NULL)
71 : {
72 148 : sqlite3_finalize(stmt);
73 148 : stmt = NULL;
74 : }
75 :
76 : /* Get version if table exists */
77 148 : if(SUCCESS == status && table_exists == true)
78 138 : {
79 138 : const char *version_query = "SELECT db_version FROM metadata;";
80 :
81 138 : rc = sqlite3_prepare_v2(db,version_query,-1,&stmt,NULL);
82 :
83 138 : if(SQLITE_OK != rc)
84 : {
85 0 : log_sqlite_error(db,rc,NULL,"Failed to prepare version query");
86 0 : status = FAILURE;
87 : }
88 :
89 138 : if(SUCCESS == status)
90 : {
91 138 : if(SQLITE_ROW == sqlite3_step(stmt))
92 : {
93 138 : *db_version = sqlite3_column_int(stmt,0);
94 138 : slog(TRACE,"Version number %d found in database\n",*db_version);
95 :
96 : } else {
97 0 : slog(TRACE,"No DB version data found in metadata table\n");
98 : }
99 : }
100 :
101 10 : } else if(SUCCESS == status){
102 10 : slog(TRACE,"Metadata table not found in database\n");
103 : }
104 :
105 : /* Cleanup */
106 148 : if(stmt != NULL)
107 : {
108 138 : sqlite3_finalize(stmt);
109 : }
110 :
111 148 : if(db != NULL)
112 : {
113 148 : rc = sqlite3_close(db);
114 :
115 148 : if(SQLITE_OK != rc)
116 : {
117 0 : log_sqlite_error(db,rc,NULL,"Warning: failed to close database");
118 0 : status = FAILURE;
119 : }
120 : }
121 :
122 148 : provide(status);
123 : }
|