odbc.c
8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2005, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include <mailutils/sql.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
struct mu_odbc_data
{
SQLHENV env; /* Environment */
SQLHDBC dbc; /* DBC */
/* Result data: */
SQLHSTMT stmt; /* Statement being executed */
mu_list_t result; /* List of returned field values */
char **fnames; /* A list of field names */
size_t fcount;
/* Error reporting: */
struct odbc_err_buffer
{
SQLSMALLINT handle_type; /* Type of the handle */
SQLHANDLE handle; /* Handle that caused the error */
char *what; /* Name of the function that failed */
SQLCHAR *msg; /* Error message buffer */
char *text; /* Error text buffer */
} err;
};
static void
mu_odbc_diag(struct mu_odbc_data *dp,
SQLSMALLINT handle_type, SQLHANDLE handle, char *what)
{
dp->err.what = what;
dp->err.handle_type = handle_type;
dp->err.handle = handle;
}
/* ************************************************************************* */
/* Interface routines */
static int
init (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = calloc (1, sizeof (*dp));
if (!dp)
return ENOMEM;
conn->data = dp;
return 0;
}
static int
destroy (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = conn->data;
free (dp->err.msg);
free (dp->err.text);
if (dp->stmt)
SQLFreeHandle (SQL_HANDLE_STMT, dp->stmt);
free (dp);
conn->data = NULL;
return 0;
}
static int
connect (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = conn->data;
long rc;
rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &dp->env);
if (rc != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_ENV, dp->env, "SQLAllocHandle");
return MU_ERR_SQL;
}
rc = SQLSetEnvAttr (dp->env, SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3, 0);
if (rc != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_ENV, dp->dbc, "SQLSetEnvAttr");
return MU_ERR_SQL;
}
rc = SQLAllocHandle (SQL_HANDLE_DBC, dp->env, &dp->dbc);
if (rc != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_DBC, dp->dbc, "SQLAllocHandle");
return MU_ERR_SQL;
}
rc = SQLConnect(dp->dbc,
(SQLCHAR*)conn->dbname, SQL_NTS,
(SQLCHAR*)conn->login, SQL_NTS,
(SQLCHAR*)conn->password, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
{
mu_odbc_diag (dp, SQL_HANDLE_DBC, dp->dbc, "SQLConnect");
return MU_ERR_SQL;
}
return 0;
}
static int
disconnect (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = conn->data;
SQLDisconnect (dp->dbc);
SQLFreeHandle (SQL_HANDLE_ENV, dp->env);
return 0;
}
static int
query (mu_sql_connection_t conn, char *query)
{
struct mu_odbc_data *dp = conn->data;
long rc;
if (dp->stmt)
SQLFreeHandle (SQL_HANDLE_STMT, dp->stmt);
rc = SQLAllocHandle (SQL_HANDLE_STMT, dp->dbc, &dp->stmt);
if (rc != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_DBC, dp->dbc, "SQLAllocHandle");
return MU_ERR_SQL;
}
/* FIXME: In some implementations only default (forward only) cursors
may be available. Do we need a sequential access method after all?
FIXME2: On SQL_SUCCESS_WITH_INFO no info is output */
rc = SQLSetStmtAttr (dp->stmt,
SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_DYNAMIC,
0);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLSetStmtAttr");
return MU_ERR_SQL;
}
rc = SQLExecDirect (dp->stmt, query, SQL_NTS);
if (rc != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLExecDirect");
return MU_ERR_SQL;
}
return 0;
}
static int
store_result (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = conn->data;
mu_list_create (&dp->result);
return 0;
}
static int
free_char_data (void *item, void *data ARG_UNUSED)
{
free (item);
return 0;
}
static int
release_result (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = conn->data;
mu_list_do (dp->result, free_char_data, NULL);
mu_list_destroy (&dp->result);
mu_argcv_free (dp->fcount, dp->fnames);
dp->fcount = 0;
dp->fnames = NULL;
return 0;
}
static int
num_columns (mu_sql_connection_t conn, size_t *np)
{
struct mu_odbc_data *dp = conn->data;
SQLSMALLINT count;
if (dp->fcount == 0)
{
if (SQLNumResultCols (dp->stmt, &count) != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLNumResultCount");
return MU_ERR_SQL;
}
}
dp->fcount = count;
*np = count;
return 0;
}
static int
num_tuples (mu_sql_connection_t conn, size_t *np)
{
struct mu_odbc_data *dp = conn->data;
SQLINTEGER count;
if (SQLRowCount (dp->stmt, &count) != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLRowCount");
return MU_ERR_SQL;
}
*np = count;
return 0;
}
static int
get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol, char **pdata)
{
struct mu_odbc_data *dp = conn->data;
char buffer[1024];
SQLINTEGER size;
if (SQLFetchScroll (dp->stmt, SQL_FETCH_ABSOLUTE, nrow + 1) != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLFetchScroll");
return MU_ERR_SQL;
}
if (SQLGetData (dp->stmt, ncol + 1, SQL_C_CHAR,
buffer, sizeof buffer, &size) != SQL_SUCCESS)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLGetData");
return MU_ERR_SQL;
}
*pdata = strdup (buffer);
mu_list_append (dp->result, *pdata);
return 0;
}
/* FIXME: untested */
static int
get_field_number (mu_sql_connection_t conn, const char *fname, size_t *fno)
{
size_t count;
if (!dp->fnames)
{
int rc;
rc = num_columns (conn, &count);
if (rc)
return rc;
dp->fnames = calloc(count + 1, sizeof dp->fnames[0]);
if (!dp->fnames)
return ENOMEM;
for (i = 0; i < count; i++)
{
char *name
SQLRETURN ret;
SQLSMALLINT namelen;
ret = SQLDescribeCol (dp->stmt,
i + 1,
NULL,
0,
&namelen,
NULL,
NULL,
NULL,
NULL);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLDescribeColl");
return MU_ERR_SQL;
}
name = malloc (namelen + 1);
if (!name)
return ENOMEM;
dp->fnames[i] = name;
ret = SQLDescribeCol (dp->stmt,
i + 1,
name,
namelen + 1,
&namelen,
NULL,
NULL,
NULL,
NULL);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
{
mu_odbc_diag (dp, SQL_HANDLE_STMT, dp->stmt, "SQLDescribeColl");
return MU_ERR_SQL;
}
}
dp->fnames[i] = NULL;
}
else
count = df->fcount;
for (i = 0; i < count; i++)
{
if (strcmp (fname, df->fnames) == 0)
{
*fno = i;
return 0;
}
}
return MU_ERR_NOENT;
}
#define DEFAULT_ERROR_BUFFER_SIZE 1024
static const char *
errstr (mu_sql_connection_t conn)
{
struct mu_odbc_data *dp = conn->data;
char state[16];
char nbuf[64];
SQLINTEGER nerror;
SQLSMALLINT msglen;
size_t length;
if (!dp->err.what)
return mu_strerror (0);
if (!dp->err.msg)
{
dp->err.msg = malloc (DEFAULT_ERROR_BUFFER_SIZE);
if (!dp->err.msg)
return mu_strerror (ENOMEM);
}
SQLGetDiagRec (dp->err.handle_type,
dp->err.handle,
1,
state,
&nerror,
dp->err.msg, DEFAULT_ERROR_BUFFER_SIZE, &msglen);
snprintf (nbuf, sizeof nbuf, "%d", (int) nerror);
length = strlen (dp->err.what) + 1
+ strlen (state) + 1
+ strlen (nbuf) + 1
+ strlen (dp->err.msg) + 1;
if (dp->err.text)
free (dp->err.text);
dp->err.text = malloc (length);
if (!dp->err.text)
return dp->err.msg;
snprintf (dp->err.text, length, "%s %s %s %s", dp->err.what, state, nbuf,
dp->err.msg);
return dp->err.text;
}
MU_DECL_SQL_DISPATCH_T(odbc) = {
"odbc",
0,
init,
destroy,
connect,
disconnect,
query,
store_result,
release_result,
num_tuples,
num_columns,
get_column,
get_field_number,
errstr,
};