~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to src/libs/zbxdbhigh/odbc.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2008-11-27 16:44:15 UTC
  • mfrom: (1.1.8 upstream) (8.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20081127164415-f0vl8ow4redvpg0c
Tags: 1:1.6.1-3
remove versioned depends on postgreql-8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** ZABBIX
 
3
** Copyright (C) 2000-2005 SIA Zabbix
 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
**/
 
19
 
 
20
#include "common.h"
 
21
#include "log.h"
 
22
#include "zlog.h"
 
23
#include "zbxodbc.h"
 
24
 
 
25
static char zbx_last_odbc_strerror[255];
 
26
 
 
27
char* get_last_odbc_strerror(void)
 
28
{
 
29
        return zbx_last_odbc_strerror;
 
30
}
 
31
 
 
32
#ifdef HAVE___VA_ARGS__
 
33
#       define set_last_odbc_strerror(fmt, ...) __zbx_set_last_odbc_strerror(ZBX_CONST_STRING(fmt), ##__VA_ARGS__)
 
34
#else
 
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, ...)
 
38
{
 
39
        va_list args;
 
40
 
 
41
        va_start(args, fmt);
 
42
 
 
43
        zbx_vsnprintf(zbx_last_odbc_strerror, sizeof(zbx_last_odbc_strerror), fmt, args);
 
44
 
 
45
        va_end(args);
 
46
}
 
47
 
 
48
#define clean_odbc_strerror() zbx_last_odbc_strerror[0]='\0'
 
49
 
 
50
static void odbc_free_row_data(ZBX_ODBC_DBH *pdbh)
 
51
{
 
52
        SQLSMALLINT i;
 
53
 
 
54
        if(pdbh->row_data)
 
55
        {
 
56
                for(i = 0; i < pdbh->col_num; i++)
 
57
                        zbx_free(pdbh->row_data[i]);    
 
58
 
 
59
                zbx_free(pdbh->row_data);       
 
60
                pdbh->row_data = NULL;
 
61
        }
 
62
        if(pdbh->data_len)
 
63
        {
 
64
                zbx_free(pdbh->data_len);
 
65
                pdbh->data_len = NULL;
 
66
        }
 
67
        pdbh->col_num = 0;
 
68
}
 
69
 
 
70
void    odbc_DBclose(ZBX_ODBC_DBH *pdbh)
 
71
{
 
72
        if(pdbh->hstmt)
 
73
        {
 
74
                SQLFreeHandle(SQL_HANDLE_STMT, pdbh->hstmt);
 
75
                pdbh->hstmt = NULL;
 
76
        }
 
77
        
 
78
        if(pdbh->hdbc)
 
79
        {
 
80
                if(pdbh->connected)
 
81
                {
 
82
                        SQLDisconnect(pdbh->hdbc);
 
83
                }
 
84
                                
 
85
                SQLFreeHandle(SQL_HANDLE_DBC, pdbh->hdbc);
 
86
                pdbh->hdbc = NULL;
 
87
        }
 
88
 
 
89
        if(pdbh->henv)
 
90
        {       
 
91
                SQLFreeHandle(SQL_HANDLE_ENV, pdbh->henv);
 
92
                pdbh->henv = NULL;
 
93
        }
 
94
 
 
95
        odbc_free_row_data(pdbh);
 
96
}
 
97
 
 
98
int     odbc_DBconnect(ZBX_ODBC_DBH *pdbh, const char *db_dsn, const char *user, const char *pass)
 
99
{
 
100
        SQLCHAR         
 
101
                err_stat[10],
 
102
                err_msg[100];
 
103
 
 
104
        SQLINTEGER 
 
105
                err_int;
 
106
 
 
107
        SQLSMALLINT     
 
108
                err_msg_len;
 
109
        
 
110
        SQLRETURN       retcode;
 
111
 
 
112
        clean_odbc_strerror();
 
113
 
 
114
        memset(pdbh, 0 , sizeof(ZBX_ODBC_DBH));
 
115
        
 
116
        zabbix_log(LOG_LEVEL_DEBUG, "ODBC connect [%s] [%s]", db_dsn, user);
 
117
 
 
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)
 
121
        {
 
122
                set_last_odbc_strerror("%s","failed environment handle allocation.");
 
123
        }
 
124
        else
 
125
        {
 
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)
 
129
                {
 
130
                        set_last_odbc_strerror("%s","failed ODBC version setting.");
 
131
                }
 
132
                else
 
133
                {
 
134
                        /* Allocate connection handle */
 
135
                        retcode = SQLAllocHandle(SQL_HANDLE_DBC, pdbh->henv, &(pdbh->hdbc)); 
 
136
                        if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
 
137
                        {
 
138
                                set_last_odbc_strerror("%s","failed connection handle allocation.");
 
139
                        }
 
140
                        else
 
141
                        {
 
142
                                /* Set login timeout to 5 seconds. */
 
143
                                SQLSetConnectAttr(pdbh->hdbc, (SQLINTEGER)SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, (SQLINTEGER)0);
 
144
 
 
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
 
150
                                        );
 
151
                                if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
 
152
                                {
 
153
        
 
154
                                        SQLGetDiagRec(SQL_HANDLE_DBC, 
 
155
                                                        pdbh->hdbc,
 
156
                                                        1,
 
157
                                                        err_stat,
 
158
                                                        &err_int,
 
159
                                                        err_msg,
 
160
                                                        sizeof(err_msg),
 
161
                                                        &err_msg_len
 
162
                                                        );
 
163
                                        
 
164
                                        set_last_odbc_strerror("failed connection [%s] (%d)", err_msg, err_int);
 
165
                                }
 
166
                                else
 
167
                                {
 
168
                                        pdbh->connected = 1;
 
169
 
 
170
                                        /* Allocate statement handle */
 
171
                                        retcode = SQLAllocHandle(SQL_HANDLE_STMT, pdbh->hdbc, &(pdbh->hstmt)); 
 
172
 
 
173
                                        if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
 
174
                                        {
 
175
                                                return SUCCEED;
 
176
                                        }
 
177
                                        else
 
178
                                        {
 
179
                                                SQLFreeHandle(SQL_HANDLE_STMT, pdbh->hstmt);
 
180
                                                pdbh->hstmt = NULL;
 
181
                                        }
 
182
                                        SQLDisconnect(pdbh->hdbc);
 
183
                                }
 
184
                                SQLFreeHandle(SQL_HANDLE_DBC, pdbh->hdbc);
 
185
                                pdbh->hdbc = NULL;
 
186
                        }
 
187
                }
 
188
                SQLFreeHandle(SQL_HANDLE_ENV, pdbh->henv);
 
189
                pdbh->henv = NULL;
 
190
        }
 
191
 
 
192
        zabbix_log(LOG_LEVEL_ERR, "Failed to connect to DSN '%s' : Error: %s", db_dsn, get_last_odbc_strerror());
 
193
        return FAIL; /* error */
 
194
}
 
195
 
 
196
 
 
197
int     odbc_DBexecute(ZBX_ODBC_DBH *pdbh, const char *query)
 
198
{
 
199
        SQLCHAR         
 
200
                err_stat[10],
 
201
                err_msg[100];
 
202
 
 
203
        SQLINTEGER 
 
204
                err_int;
 
205
 
 
206
        SQLSMALLINT     
 
207
                err_msg_len;
 
208
 
 
209
        SQLRETURN       retcode;
 
210
 
 
211
        clean_odbc_strerror();
 
212
 
 
213
        odbc_free_row_data(pdbh);
 
214
 
 
215
        retcode = SQLExecDirect(pdbh->hstmt, (SQLCHAR*) query, SQL_NTS);
 
216
        
 
217
        if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) goto lbl_err_exit;
 
218
        
 
219
        return SUCCEED;
 
220
                
 
221
lbl_err_exit:   
 
222
        
 
223
        SQLGetDiagRec(SQL_HANDLE_STMT, 
 
224
                        pdbh->hstmt,
 
225
                        1,
 
226
                        err_stat,
 
227
                        &err_int,
 
228
                        err_msg,
 
229
                        sizeof(err_msg),
 
230
                        &err_msg_len
 
231
                        );
 
232
        
 
233
        set_last_odbc_strerror("Failed select execution [%s] (%d)", err_msg, err_int);
 
234
 
 
235
        zabbix_log(LOG_LEVEL_ERR, "%s", get_last_odbc_strerror());
 
236
 
 
237
        return FAIL;
 
238
}
 
239
 
 
240
ZBX_ODBC_ROW    odbc_DBfetch(ZBX_ODBC_RESULT pdbh)
 
241
{
 
242
        SQLCHAR         
 
243
                err_stat[10],
 
244
                err_msg[100];
 
245
 
 
246
        SQLINTEGER 
 
247
                err_int;
 
248
 
 
249
        SQLSMALLINT     
 
250
                err_msg_len;
 
251
 
 
252
        SQLRETURN       retcode;
 
253
        SQLSMALLINT     i;
 
254
        
 
255
        if(pdbh == NULL)        return NULL;
 
256
 
 
257
        clean_odbc_strerror();
 
258
        
 
259
        zabbix_log(LOG_LEVEL_DEBUG, "ODBC fetch");
 
260
 
 
261
        retcode = SQLFetch(pdbh->hstmt);
 
262
        if (retcode == SQL_ERROR) goto lbl_err_exit;
 
263
        
 
264
        if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO )
 
265
        {
 
266
                zabbix_log(LOG_LEVEL_DEBUG, "odbc_DBfetch [end of rows received]");
 
267
                return NULL;
 
268
        }
 
269
 
 
270
        for(i=0; i < pdbh->col_num; i++)
 
271
        {
 
272
                rtrim_spaces(pdbh->row_data[i]);
 
273
                zabbix_log(LOG_LEVEL_DEBUG, "Featched [%i col]: %s", i, pdbh->row_data[i]);
 
274
        }
 
275
 
 
276
        return pdbh->row_data;
 
277
        
 
278
lbl_err_exit:   
 
279
        
 
280
        SQLGetDiagRec(SQL_HANDLE_STMT, 
 
281
                        pdbh->hstmt,
 
282
                        1,
 
283
                        err_stat,
 
284
                        &err_int,
 
285
                        err_msg,
 
286
                        sizeof(err_msg),
 
287
                        &err_msg_len
 
288
                        );
 
289
        
 
290
        set_last_odbc_strerror("Failed data fetching [%s] (%d)", err_msg, err_int);
 
291
 
 
292
        zabbix_log(LOG_LEVEL_ERR, "%s", get_last_odbc_strerror());
 
293
 
 
294
        return NULL;
 
295
}
 
296
 
 
297
ZBX_ODBC_RESULT odbc_DBselect(ZBX_ODBC_DBH *pdbh, const char *query)
 
298
{
 
299
        SQLCHAR         
 
300
                err_stat[10],
 
301
                err_msg[100];
 
302
 
 
303
        SQLINTEGER 
 
304
                err_int;
 
305
 
 
306
        SQLSMALLINT     
 
307
                err_msg_len;
 
308
 
 
309
        SQLRETURN       retcode;
 
310
        SQLSMALLINT     
 
311
                i = 0,
 
312
                col_num = 0;
 
313
 
 
314
        clean_odbc_strerror();
 
315
 
 
316
        odbc_free_row_data(pdbh);
 
317
 
 
318
        zabbix_log(LOG_LEVEL_DEBUG, "ODBC select [%s]", query);
 
319
        
 
320
        retcode = SQLExecDirect(pdbh->hstmt, (SQLCHAR*) query, SQL_NTS);
 
321
        
 
322
        if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) goto lbl_err_exit;
 
323
        
 
324
        retcode = SQLNumResultCols(pdbh->hstmt, &col_num);
 
325
        if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) goto lbl_err_exit;
 
326
 
 
327
        pdbh->col_num  = col_num;
 
328
 
 
329
        pdbh->row_data = zbx_malloc(pdbh->row_data, sizeof(char*) * col_num);
 
330
        memset(pdbh->row_data, 0, sizeof(char*) * col_num);
 
331
 
 
332
        pdbh->data_len = zbx_malloc(pdbh->data_len, sizeof(SQLINTEGER) * col_num);
 
333
        memset(pdbh->data_len, 0, sizeof(SQLINTEGER) * col_num);
 
334
 
 
335
        for(i=0; i < col_num; i++)
 
336
        {
 
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]);
 
339
        }
 
340
        
 
341
        zabbix_log(LOG_LEVEL_DEBUG, "selected %i cols", col_num);
 
342
 
 
343
        return (ZBX_ODBC_RESULT) pdbh;
 
344
                
 
345
lbl_err_exit:   
 
346
        
 
347
        SQLGetDiagRec(SQL_HANDLE_STMT, 
 
348
                        pdbh->hstmt,
 
349
                        1,
 
350
                        err_stat,
 
351
                        &err_int,
 
352
                        err_msg,
 
353
                        sizeof(err_msg),
 
354
                        &err_msg_len
 
355
                        );
 
356
        
 
357
        set_last_odbc_strerror("Failed selection [%s] (%d)", err_msg, err_int);
 
358
 
 
359
        zabbix_log(LOG_LEVEL_ERR, "%s", get_last_odbc_strerror());
 
360
 
 
361
        return NULL;
 
362
}