1 /* 2 ** Zabbix 3 ** Copyright (C) 2001-2021 Zabbix SIA 4 ** 5 ** This program is free software; you can redistribute it and/or modify 6 ** it under the terms of the GNU General Public License as published by 7 ** the Free Software Foundation; either version 2 of the License, or 8 ** (at your option) any later version. 9 ** 10 ** This program is distributed in the hope that it will be useful, 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 ** GNU General Public License for more details. 14 ** 15 ** You should have received a copy of the GNU General Public License 16 ** along with this program; if not, write to the Free Software 17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 **/ 19 20 #ifndef ZABBIX_ZBXDB_H 21 #define ZABBIX_ZBXDB_H 22 23 #include "common.h" 24 #include "zbxjson.h" 25 26 #define ZBX_DB_OK 0 27 #define ZBX_DB_FAIL -1 28 #define ZBX_DB_DOWN -2 29 30 #define ZBX_DB_WAIT_DOWN 10 31 32 #define ZBX_MAX_SQL_SIZE 262144 /* 256KB */ 33 #ifndef ZBX_MAX_OVERFLOW_SQL_SIZE 34 # ifdef HAVE_ORACLE 35 /* Do not use "overflowing" (multi-statement) queries for Oracle. */ 36 /* Zabbix benefits from cursor_sharing=force Oracle parameter */ 37 /* which doesn't apply to PL/SQL blocks. */ 38 # define ZBX_MAX_OVERFLOW_SQL_SIZE 0 39 # else 40 # define ZBX_MAX_OVERFLOW_SQL_SIZE ZBX_MAX_SQL_SIZE 41 # endif 42 #elif 0 != ZBX_MAX_OVERFLOW_SQL_SIZE && \ 43 (1024 > ZBX_MAX_OVERFLOW_SQL_SIZE || ZBX_MAX_OVERFLOW_SQL_SIZE > ZBX_MAX_SQL_SIZE) 44 #error ZBX_MAX_OVERFLOW_SQL_SIZE is out of range 45 #endif 46 47 #define ZBX_DB_TLS_CONNECT_REQUIRED_TXT "required" 48 #define ZBX_DB_TLS_CONNECT_VERIFY_CA_TXT "verify_ca" 49 #define ZBX_DB_TLS_CONNECT_VERIFY_FULL_TXT "verify_full" 50 51 typedef char **DB_ROW; 52 typedef struct zbx_db_result *DB_RESULT; 53 54 /* database field value */ 55 typedef union 56 { 57 int i32; 58 zbx_uint64_t ui64; 59 double dbl; 60 char *str; 61 } 62 zbx_db_value_t; 63 64 #ifdef HAVE_SQLITE3 65 /* we have to put double % here for sprintf */ 66 # define ZBX_SQL_MOD(x, y) #x "%%" #y 67 #else 68 # define ZBX_SQL_MOD(x, y) "mod(" #x "," #y ")" 69 #endif 70 71 #ifdef HAVE_SQLITE3 72 # define ZBX_FOR_UPDATE "" /* SQLite3 does not support "select ... for update" */ 73 #else 74 # define ZBX_FOR_UPDATE " for update" 75 #endif 76 77 #ifdef HAVE_MULTIROW_INSERT 78 # define ZBX_ROW_DL "," 79 #else 80 # define ZBX_ROW_DL ";\n" 81 #endif 82 83 int zbx_db_init(const char *dbname, const char *const dbschema, char **error); 84 void zbx_db_deinit(void); 85 86 void zbx_db_init_autoincrement_options(void); 87 88 int zbx_db_connect(char *host, char *user, char *password, char *dbname, char *dbschema, char *dbsocket, int port, 89 char *tls_connect, char *cert, char *key, char *ca, char *cipher, char *cipher_13); 90 void zbx_db_close(void); 91 92 int zbx_db_begin(void); 93 int zbx_db_commit(void); 94 int zbx_db_rollback(void); 95 int zbx_db_txn_level(void); 96 int zbx_db_txn_error(void); 97 int zbx_db_txn_end_error(void); 98 const char *zbx_db_last_strerr(void); 99 100 #ifdef HAVE_POSTGRESQL 101 int zbx_tsdb_get_version(void); 102 #define ZBX_DB_TSDB_V1 (20000 > zbx_tsdb_get_version()) 103 #endif 104 105 #ifdef HAVE_ORACLE 106 107 /* context for dynamic parameter binding */ 108 typedef struct 109 { 110 /* the parameter position, starting with 0 */ 111 int position; 112 /* the parameter type (ZBX_TYPE_* ) */ 113 unsigned char type; 114 /* the maximum parameter size */ 115 size_t size_max; 116 /* the data to bind - array of rows, each row being an array of columns */ 117 zbx_db_value_t **rows; 118 /* custom data, depending on column type */ 119 void *data; 120 } 121 zbx_db_bind_context_t; 122 123 int zbx_db_statement_prepare(const char *sql); 124 int zbx_db_bind_parameter_dyn(zbx_db_bind_context_t *context, int position, unsigned char type, 125 zbx_db_value_t **rows, int rows_num); 126 void zbx_db_clean_bind_context(zbx_db_bind_context_t *context); 127 int zbx_db_statement_execute(int iters); 128 #endif 129 int zbx_db_vexecute(const char *fmt, va_list args); 130 DB_RESULT zbx_db_vselect(const char *fmt, va_list args); 131 DB_RESULT zbx_db_select_n(const char *query, int n); 132 133 DB_ROW zbx_db_fetch(DB_RESULT result); 134 void DBfree_result(DB_RESULT result); 135 int zbx_db_is_null(const char *field); 136 137 typedef enum 138 { 139 ESCAPE_SEQUENCE_OFF, 140 ESCAPE_SEQUENCE_ON 141 } 142 zbx_escape_sequence_t; 143 char *zbx_db_dyn_escape_string(const char *src, size_t max_bytes, size_t max_chars, 144 zbx_escape_sequence_t flag); 145 #define ZBX_SQL_LIKE_ESCAPE_CHAR '!' 146 char *zbx_db_dyn_escape_like_pattern(const char *src); 147 148 int zbx_db_strlen_n(const char *text_loc, size_t maxlen); 149 150 #define ZBX_MYSQL_MIN_VERSION 50728 151 #define ZBX_MYSQL_MIN_VERSION_FRIENDLY "5.07.28" 152 #define ZBX_MYSQL_MAX_VERSION 80099 153 #define ZBX_MYSQL_MAX_VERSION_FRIENDLY "8.00.x" 154 155 #define ZBX_MARIA_MIN_VERSION 100037 156 #define ZBX_MARIA_MIN_VERSION_FRIENDLY "10.00.37" 157 #define ZBX_MARIA_MAX_VERSION 100599 158 #define ZBX_MARIA_MAX_VERSION_FRIENDLY "10.05.x" 159 160 #define ZBX_POSTGRESQL_MIN_VERSION 100009 161 #define ZBX_POSTGRESQL_MIN_VERSION_FRIENDLY "10.9" 162 #define ZBX_POSTGRESQL_MAX_VERSION 139999 163 #define ZBX_POSTGRESQL_MAX_VERSION_FRIENDLY "13.x" 164 165 #define ZBX_ORACLE_MIN_VERSION 1201000200 166 #define ZBX_ORACLE_MIN_VERSION_FRIENDLY "Database 12c Release 12.01.00.02.x" 167 #define ZBX_ORACLE_MAX_VERSION 1999000000 168 #define ZBX_ORACLE_MAX_VERSION_FRIENDLY "Database 19c Release 19.x.x" 169 170 #define ZBX_ELASTIC_MIN_VERSION 70000 171 #define ZBX_ELASTIC_MIN_VERSION_FRIENDLY "7.x" 172 173 #define ZBX_DBVERSION_UNDEFINED 0 174 175 typedef enum 176 { /* db version status flags shared with FRONTEND */ 177 DB_VERSION_SUPPORTED, 178 DB_VERSION_LOWER_THAN_MINIMUM, 179 DB_VERSION_HIGHER_THAN_MAXIMUM, 180 DB_VERSION_FAILED_TO_RETRIEVE 181 } zbx_db_version_status_t; 182 183 zbx_uint32_t zbx_dbms_version_get(void); 184 zbx_uint32_t zbx_dbms_version_extract(struct zbx_json *json); 185 186 #ifdef HAVE_MYSQL 187 int zbx_dbms_mariadb_used(void); 188 #endif 189 190 int zbx_db_version_check(const char *database, zbx_uint32_t current_version, zbx_uint32_t min_version, 191 zbx_uint32_t max_version); 192 void zbx_db_version_json_create(struct zbx_json *json, const char *database, const char *friendly_current_version, 193 const char *friendly_min_version, const char *friendly_max_version, int flag); 194 195 #endif 196