master
h 173 lines 11.2 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef NETDATA_SQLITE_FUNCTIONS_H
4 #define NETDATA_SQLITE_FUNCTIONS_H
5
6 #include "database/rrd.h"
7 #include "database/sqlite/vendored/sqlite3.h"
8
9 void analytics_set_data_str(char **name, const char *value);
10
11 #define SQLITE_BIND_FAIL(label, rc) \
12 do { \
13 if ((rc) != SQLITE_OK) \
14 goto label; \
15 } while (0)
16
17 #define REPORT_BIND_FAIL(res, param) \
18 do { \
19 if (unlikely((param))) { \
20 const char *failed_param = sqlite3_bind_parameter_name((res), (param)); \
21 nd_log( \
22 NDLS_DAEMON, \
23 NDLP_ERR, \
24 "Failed to bind parameter %d (%s) in %s", \
25 (param), \
26 failed_param ? failed_param : "?", \
27 __FUNCTION__); \
28 } \
29 } while (0)
30
31 #define SQLITE_FINALIZE(res) \
32 do { \
33 if ((res)) { \
34 int _rc = sqlite3_finalize((res)); \
35 if (_rc != SQLITE_OK) { \
36 nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to finalize statement rc=%d in %s", _rc, __FUNCTION__); \
37 } \
38 } \
39 } while (0)
40
41 #define SQLITE_RESET(res) \
42 do { \
43 if ((res)) { \
44 int _rc = sqlite3_reset((res)); \
45 if (_rc != SQLITE_OK) { \
46 nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to reset statement rc=%d in %s", _rc, __FUNCTION__); \
47 } \
48 } \
49 } while (0)
50
51 #define REQUIRE_DB(db) \
52 ({ \
53 if (unlikely(!(db))) { \
54 if (default_rrd_memory_mode == RRD_DB_MODE_DBENGINE) \
55 error_report("Database has not been initialized in %s", __FUNCTION__); \
56 } \
57 (db) != NULL; \
58 })
59
60 extern bool sqlite_databases_closed;
61 #define REQUIRE_HEALTH_DB_OPEN() \
62 (!__atomic_load_n(&sqlite_databases_closed, __ATOMIC_ACQUIRE))
63
64 #define PREPARE_COMPILED_STATEMENT(db, sql, stmt_ptr) \
65 ({ \
66 bool _ret = true; \
67 if ((!*(stmt_ptr))) { \
68 int _rc = prepare_statement((db), (sql), stmt_ptr); \
69 if (_rc != SQLITE_OK) { \
70 internal_error(true, "Failed to prepare statement \"%s\", rc=%d in %s", (sql), _rc, __FUNCTION__); \
71 nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to prepare statement, rc=%d in %s", _rc, __FUNCTION__); \
72 } \
73 _ret = (_rc == SQLITE_OK); \
74 } \
75 _ret; \
76 })
77
78 #define PREPARE_STATEMENT(db, sql, stmt_ptr) \
79 ({ \
80 int _rc = simple_prepare_statement((db), (sql), stmt_ptr); \
81 if (_rc != SQLITE_OK) { \
82 internal_error(true, "Failed to prepare statement \"%s\", rc=%d in %s", (sql), _rc, __FUNCTION__); \
83 nd_log(NDLS_DAEMON, NDLP_ERR, "Failed to prepare statement, rc=%d in %s", _rc, __FUNCTION__); \
84 } \
85 _rc == SQLITE_OK; \
86 })
87
88 #define SQL_MAX_RETRY (100)
89 #define SQLITE_INSERT_DELAY (10) // Insert delay in case of lock
90 #define SQLITE_BUSY_DELAY_MS (30) // Busy timeout delay
91
92 SQLITE_API int sqlite3_step_monitored(sqlite3_stmt *stmt);
93 SQLITE_API int sqlite3_exec_monitored(
94 sqlite3 *db, /* An open database */
95 const char *sql, /* SQL to be evaluated */
96 int (*callback)(void*,int,char**,char**), /* Callback function */
97 void *data, /* 1st argument to callback */
98 char **errmsg /* Error msg written here */
99 );
100
101 // Initialization and shutdown
102 int init_database_batch(sqlite3 *database, const char *batch[], const char *description);
103 int configure_sqlite_database(sqlite3 *database, int target_version, const char *description);
104
105 // Helpers
106 int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_be_null);
107 int prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **statement);
108 int simple_prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **statement);
109 void finalize_self_prepared_sql_statements();
110 void finalize_all_prepared_sql_statements();
111
112 int execute_insert(sqlite3_stmt *res);
113 int db_execute(sqlite3 *database, const char *cmd, int *sqlite_rc);
114 char *get_database_extented_error(sqlite3 *database, int i, const char *description);
115
116 void sql_drop_table(const char *table);
117 void sqlite_now_usec(sqlite3_context *context, int argc, sqlite3_value **argv);
118
119 uint64_t sqlite_get_db_space(sqlite3 *db);
120
121 int get_free_page_count(sqlite3 *database);
122 int get_database_page_count(sqlite3 *database);
123
124 // Return a pointer to the UUID blob stored in column iCol when the current row
125 // contains a BLOB with exactly sizeof(nd_uuid_t) bytes. The caller must pass a
126 // valid statement positioned on a row. The returned pointer refers to
127 // SQLite-owned memory and is only valid until the statement is stepped, reset,
128 // or finalized. Returns NULL for NULL, non-BLOB, or malformed values.
129 static inline const nd_uuid_t *sqlite3_column_uuid_ptr(sqlite3_stmt *res, int iCol) {
130 if (sqlite3_column_type(res, iCol) != SQLITE_BLOB)
131 return NULL;
132
133 const void *uuid = sqlite3_column_blob(res, iCol);
134 if (!uuid || sqlite3_column_bytes(res, iCol) != sizeof(nd_uuid_t))
135 return NULL;
136
137 return (const nd_uuid_t *)uuid;
138 }
139
140 // Copy the UUID stored in column iCol into dst. The caller must pass a valid
141 // statement positioned on a row and a writable destination UUID buffer.
142 // Returns true on success, false when the column does not contain a valid UUID blob.
143 static inline bool sqlite3_column_uuid_copy(sqlite3_stmt *res, int iCol, nd_uuid_t dst) {
144 const nd_uuid_t *uuid = sqlite3_column_uuid_ptr(res, iCol);
145 if (!uuid)
146 return false;
147
148 uuid_copy(dst, *uuid);
149 return true;
150 }
151
152 // Convert the UUID stored in column iCol to lowercase text in out. The caller
153 // must pass a valid statement positioned on a row and a writable buffer large
154 // enough for UUID_STR_LEN bytes. Returns true on success, false when the column
155 // does not contain a valid UUID blob.
156 static inline bool sqlite3_column_uuid_unparse_lower(sqlite3_stmt *res, int iCol, char *out) {
157 const nd_uuid_t *uuid = sqlite3_column_uuid_ptr(res, iCol);
158 if (!uuid)
159 return false;
160
161 uuid_unparse_lower(*uuid, out);
162 return true;
163 }
164
165 int sqlite_library_init(void);
166 void sqlite_library_shutdown(void);
167
168 void sql_close_database(sqlite3 *database, const char *database_name);
169 void sql_close_thread_db_safe(sqlite3 **database);
170 void sqlite_close_databases(void);
171 uint64_t get_total_database_space(void);
172 int sqlite_release_memory(int bytes);
173 #endif //NETDATA_SQLITE_FUNCTIONS_H