Line data Source code
1 : #include "precizer.h"
2 :
3 : /**
4 : * @brief Finalize a prepared SQLite statement and reset its pointer
5 : *
6 : * Also performs WAL checkpoint and cache flush in the same way as db_close()
7 : * to ensure WAL/SHM files are cleaned up when the database was modified.
8 : *
9 : * @param[in] db Pointer to SQLite database handle
10 : * @param[in] db_alias Attached database alias name
11 : * @param[in,out] stmt Pointer to prepared statement pointer to finalize
12 : * @return Return status code
13 : */
14 64 : Return db_finalize(
15 : sqlite3 *db,
16 : const char *db_alias,
17 : sqlite3_stmt **stmt)
18 : {
19 : /** Return status
20 : * The status that will be passed to return() before exiting
21 : * By default, the function worked without errors
22 : */
23 64 : Return status = SUCCESS;
24 64 : int rc = SQLITE_OK;
25 :
26 64 : if(db == NULL || db_alias == NULL || stmt == NULL)
27 : {
28 0 : slog(ERROR,"Invalid input parameters: db=%p, db_alias=%p, stmt=%p\n",db,db_alias,stmt);
29 0 : status = FAILURE;
30 : }
31 :
32 64 : if(SUCCESS == status)
33 : {
34 64 : if(*stmt != NULL)
35 : {
36 32 : rc = sqlite3_finalize(*stmt);
37 :
38 32 : if(SQLITE_OK != rc)
39 : {
40 0 : log_sqlite_error(db,rc,NULL,"Failed to finalize SQLite statement");
41 0 : status = FAILURE;
42 : } else {
43 32 : *stmt = NULL;
44 : }
45 : }
46 :
47 64 : sqlite3_stmt *active_stmt = NULL;
48 :
49 64 : while((active_stmt = sqlite3_next_stmt(db,NULL)) != NULL)
50 : {
51 0 : slog(ERROR,"Attention! The program is in the process of shutting down, but there are still uncompleted SQLite statements!\n");
52 0 : sqlite3_finalize(active_stmt);
53 : }
54 :
55 64 : char *sql = NULL;
56 :
57 64 : if(asprintf(&sql,
58 : "PRAGMA %s.journal_mode=DELETE;"
59 : "PRAGMA %s.fsync=ON;"
60 : "PRAGMA %s.synchronous=EXTRA;"
61 : "PRAGMA %s.locking_mode=EXCLUSIVE;",
62 : db_alias,
63 : db_alias,
64 : db_alias,
65 : db_alias) == -1)
66 : {
67 0 : status = FAILURE;
68 0 : report("Memory allocation failed for WAL checkpoint SQL");
69 : }
70 :
71 64 : if(SUCCESS == status)
72 : {
73 64 : rc = sqlite3_exec(db,sql,NULL,NULL,NULL);
74 :
75 64 : if(SQLITE_OK != rc)
76 : {
77 0 : log_sqlite_error(db,rc,NULL,"Warning: failed to tune database integrity");
78 0 : status = FAILURE;
79 : }
80 : }
81 :
82 64 : free(sql);
83 :
84 64 : if(SUCCESS == status)
85 : {
86 64 : rc = sqlite3_db_cacheflush(db);
87 :
88 64 : if(SQLITE_OK != rc)
89 : {
90 0 : log_sqlite_error(db,rc,NULL,"Warning: failed to flush database");
91 0 : status = FAILURE;
92 : }
93 : }
94 : }
95 :
96 64 : provide(status);
97 : }
|