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 
25 #define ZBX_DB_OK	0
26 #define ZBX_DB_FAIL	-1
27 #define ZBX_DB_DOWN	-2
28 
29 #define ZBX_DB_WAIT_DOWN	10
30 
31 #define ZBX_MAX_SQL_SIZE	262144	/* 256KB */
32 #ifndef ZBX_MAX_OVERFLOW_SQL_SIZE
33 #	ifdef HAVE_ORACLE
34 		/* Do not use "overflowing" (multi-statement) queries for Oracle. */
35 		/* Zabbix benefits from cursor_sharing=force Oracle parameter */
36 		/* which doesn't apply to PL/SQL blocks. */
37 #		define ZBX_MAX_OVERFLOW_SQL_SIZE	0
38 #	else
39 #		define ZBX_MAX_OVERFLOW_SQL_SIZE	ZBX_MAX_SQL_SIZE
40 #	endif
41 #elif 0 != ZBX_MAX_OVERFLOW_SQL_SIZE && \
42 		(1024 > ZBX_MAX_OVERFLOW_SQL_SIZE || ZBX_MAX_OVERFLOW_SQL_SIZE > ZBX_MAX_SQL_SIZE)
43 #error ZBX_MAX_OVERFLOW_SQL_SIZE is out of range
44 #endif
45 
46 #define ZBX_DB_TLS_CONNECT_REQUIRED_TXT		"required"
47 #define ZBX_DB_TLS_CONNECT_VERIFY_CA_TXT	"verify_ca"
48 #define ZBX_DB_TLS_CONNECT_VERIFY_FULL_TXT	"verify_full"
49 
50 typedef char	**DB_ROW;
51 typedef struct zbx_db_result	*DB_RESULT;
52 
53 /* database field value */
54 typedef union
55 {
56 	int		i32;
57 	zbx_uint64_t	ui64;
58 	double		dbl;
59 	char		*str;
60 }
61 zbx_db_value_t;
62 
63 #ifdef HAVE_SQLITE3
64 	/* we have to put double % here for sprintf */
65 #	define ZBX_SQL_MOD(x, y) #x "%%" #y
66 #else
67 #	define ZBX_SQL_MOD(x, y) "mod(" #x "," #y ")"
68 #endif
69 
70 #ifdef HAVE_SQLITE3
71 #	define ZBX_FOR_UPDATE	""	/* SQLite3 does not support "select ... for update" */
72 #else
73 #	define ZBX_FOR_UPDATE	" for update"
74 #endif
75 
76 #ifdef HAVE_MULTIROW_INSERT
77 #	define ZBX_ROW_DL	","
78 #else
79 #	define ZBX_ROW_DL	";\n"
80 #endif
81 
82 int	zbx_db_init(const char *dbname, const char *const dbschema, char **error);
83 void	zbx_db_deinit(void);
84 
85 void	zbx_db_init_autoincrement_options(void);
86 
87 int	zbx_db_connect(char *host, char *user, char *password, char *dbname, char *dbschema, char *dbsocket, int port,
88 			char *tls_connect, char *cert, char *key, char *ca, char *cipher, char *cipher_13);
89 void	zbx_db_close(void);
90 
91 int	zbx_db_begin(void);
92 int	zbx_db_commit(void);
93 int	zbx_db_rollback(void);
94 int	zbx_db_txn_level(void);
95 int	zbx_db_txn_error(void);
96 int	zbx_db_txn_end_error(void);
97 const char	*zbx_db_last_strerr(void);
98 
99 #ifdef HAVE_POSTGRESQL
100 int	zbx_dbms_get_version(void);
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, size_t maxlen);
149 
150 #endif
151