3
** Copyright (C) 2000-2005 SIA Zabbix
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
25
static char zbx_last_odbc_strerror[255];
27
char* get_last_odbc_strerror(void)
29
return zbx_last_odbc_strerror;
32
#ifdef HAVE___VA_ARGS__
33
# define set_last_odbc_strerror(fmt, ...) __zbx_set_last_odbc_strerror(ZBX_CONST_STRING(fmt), ##__VA_ARGS__)
35
# define set_last_odbc_strerror __zbx_set_last_odbc_strerror
36
#endif /* HAVE___VA_ARGS__ */
37
static void __zbx_set_last_odbc_strerror(const char *fmt, ...)
43
zbx_vsnprintf(zbx_last_odbc_strerror, sizeof(zbx_last_odbc_strerror), fmt, args);
48
#define clean_odbc_strerror() zbx_last_odbc_strerror[0]='\0'
50
static void odbc_free_row_data(ZBX_ODBC_DBH *pdbh)
56
for(i = 0; i < pdbh->col_num; i++)
57
zbx_free(pdbh->row_data[i]);
59
zbx_free(pdbh->row_data);
60
pdbh->row_data = NULL;
64
zbx_free(pdbh->data_len);
65
pdbh->data_len = NULL;
70
void odbc_DBclose(ZBX_ODBC_DBH *pdbh)
74
SQLFreeHandle(SQL_HANDLE_STMT, pdbh->hstmt);
82
SQLDisconnect(pdbh->hdbc);
85
SQLFreeHandle(SQL_HANDLE_DBC, pdbh->hdbc);
91
SQLFreeHandle(SQL_HANDLE_ENV, pdbh->henv);
95
odbc_free_row_data(pdbh);
98
int odbc_DBconnect(ZBX_ODBC_DBH *pdbh, const char *db_dsn, const char *user, const char *pass)
112
clean_odbc_strerror();
114
memset(pdbh, 0 , sizeof(ZBX_ODBC_DBH));
116
zabbix_log(LOG_LEVEL_DEBUG, "ODBC connect [%s] [%s]", db_dsn, user);
118
/*Allocate environment handle */
119
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &(pdbh->henv));
120
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
122
set_last_odbc_strerror("%s","failed environment handle allocation.");
126
/* Set the ODBC version environment attribute */
127
retcode = SQLSetEnvAttr(pdbh->henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
128
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
130
set_last_odbc_strerror("%s","failed ODBC version setting.");
134
/* Allocate connection handle */
135
retcode = SQLAllocHandle(SQL_HANDLE_DBC, pdbh->henv, &(pdbh->hdbc));
136
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
138
set_last_odbc_strerror("%s","failed connection handle allocation.");
142
/* Set login timeout to 5 seconds. */
143
SQLSetConnectAttr(pdbh->hdbc, (SQLINTEGER)SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, (SQLINTEGER)0);
145
/* Connect to data source */
146
retcode = SQLConnect(pdbh->hdbc,
147
(SQLCHAR*) db_dsn, SQL_NTS,
148
(SQLCHAR*) user, SQL_NTS,
149
(SQLCHAR*) pass, SQL_NTS
151
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
154
SQLGetDiagRec(SQL_HANDLE_DBC,
164
set_last_odbc_strerror("failed connection [%s] (%d)", err_msg, err_int);
170
/* Allocate statement handle */
171
retcode = SQLAllocHandle(SQL_HANDLE_STMT, pdbh->hdbc, &(pdbh->hstmt));
173
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
179
SQLFreeHandle(SQL_HANDLE_STMT, pdbh->hstmt);
182
SQLDisconnect(pdbh->hdbc);
184
SQLFreeHandle(SQL_HANDLE_DBC, pdbh->hdbc);
188
SQLFreeHandle(SQL_HANDLE_ENV, pdbh->henv);
192
zabbix_log(LOG_LEVEL_ERR, "Failed to connect to DSN '%s' : Error: %s", db_dsn, get_last_odbc_strerror());
193
return FAIL; /* error */
197
int odbc_DBexecute(ZBX_ODBC_DBH *pdbh, const char *query)
211
clean_odbc_strerror();
213
odbc_free_row_data(pdbh);
215
retcode = SQLExecDirect(pdbh->hstmt, (SQLCHAR*) query, SQL_NTS);
217
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) goto lbl_err_exit;
223
SQLGetDiagRec(SQL_HANDLE_STMT,
233
set_last_odbc_strerror("Failed select execution [%s] (%d)", err_msg, err_int);
235
zabbix_log(LOG_LEVEL_ERR, "%s", get_last_odbc_strerror());
240
ZBX_ODBC_ROW odbc_DBfetch(ZBX_ODBC_RESULT pdbh)
255
if(pdbh == NULL) return NULL;
257
clean_odbc_strerror();
259
zabbix_log(LOG_LEVEL_DEBUG, "ODBC fetch");
261
retcode = SQLFetch(pdbh->hstmt);
262
if (retcode == SQL_ERROR) goto lbl_err_exit;
264
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO )
266
zabbix_log(LOG_LEVEL_DEBUG, "odbc_DBfetch [end of rows received]");
270
for(i=0; i < pdbh->col_num; i++)
272
rtrim_spaces(pdbh->row_data[i]);
273
zabbix_log(LOG_LEVEL_DEBUG, "Featched [%i col]: %s", i, pdbh->row_data[i]);
276
return pdbh->row_data;
280
SQLGetDiagRec(SQL_HANDLE_STMT,
290
set_last_odbc_strerror("Failed data fetching [%s] (%d)", err_msg, err_int);
292
zabbix_log(LOG_LEVEL_ERR, "%s", get_last_odbc_strerror());
297
ZBX_ODBC_RESULT odbc_DBselect(ZBX_ODBC_DBH *pdbh, const char *query)
314
clean_odbc_strerror();
316
odbc_free_row_data(pdbh);
318
zabbix_log(LOG_LEVEL_DEBUG, "ODBC select [%s]", query);
320
retcode = SQLExecDirect(pdbh->hstmt, (SQLCHAR*) query, SQL_NTS);
322
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) goto lbl_err_exit;
324
retcode = SQLNumResultCols(pdbh->hstmt, &col_num);
325
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) goto lbl_err_exit;
327
pdbh->col_num = col_num;
329
pdbh->row_data = zbx_malloc(pdbh->row_data, sizeof(char*) * col_num);
330
memset(pdbh->row_data, 0, sizeof(char*) * col_num);
332
pdbh->data_len = zbx_malloc(pdbh->data_len, sizeof(SQLINTEGER) * col_num);
333
memset(pdbh->data_len, 0, sizeof(SQLINTEGER) * col_num);
335
for(i=0; i < col_num; i++)
337
pdbh->row_data[i] = zbx_malloc(pdbh->row_data[i], MAX_STRING_LEN);
338
SQLBindCol(pdbh->hstmt, i+1, SQL_C_CHAR, pdbh->row_data[i], MAX_STRING_LEN, &pdbh->data_len[i]);
341
zabbix_log(LOG_LEVEL_DEBUG, "selected %i cols", col_num);
343
return (ZBX_ODBC_RESULT) pdbh;
347
SQLGetDiagRec(SQL_HANDLE_STMT,
357
set_last_odbc_strerror("Failed selection [%s] (%d)", err_msg, err_int);
359
zabbix_log(LOG_LEVEL_ERR, "%s", get_last_odbc_strerror());