Debian Codegraph @

external/glpk/glpsql.c

raw symbols SCIP
1 /* glpsql.c */
2
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
5 *
6 * Author: Heinrich Schuchardt <[email protected]>.
7 *
8 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
9 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
10 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
11 * E-mail: <[email protected]>.
12 *
13 * GLPK is free software: you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * GLPK is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
21 * License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
25 ***********************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include "glpmpl.h"
32 #include "glpsql.h"
33
34 #ifdef ODBC_DLNAME
35 #define HAVE_ODBC
36 #define libodbc ODBC_DLNAME
37 #define h_odbc (get_env_ptr()->h_odbc)
38 #endif
39
40 #ifdef MYSQL_DLNAME
41 #define HAVE_MYSQL
42 #define libmysql MYSQL_DLNAME
43 #define h_mysql (get_env_ptr()->h_mysql)
44 #endif
45
46 static void *db_iodbc_open_int(TABDCA *dca, int mode, const char
47 **sqllines);
48 static void *db_mysql_open_int(TABDCA *dca, int mode, const char
49 **sqllines);
50
51 /**********************************************************************/
52
53 #if defined(HAVE_ODBC) || defined(HAVE_MYSQL)
54
55 #define SQL_FIELD_MAX 100
56 /* maximal field count */
57
58 #define SQL_FDLEN_MAX 255
59 /* maximal field length */
60
61 /***********************************************************************
62 * NAME
63 *
64 * args_concat - concatenate arguments
65 *
66 * SYNOPSIS
67 *
68 * static char **args_concat(TABDCA *dca);
69 *
70 * DESCRIPTION
71 *
72 * The arguments passed in dca are SQL statements. A SQL statement may
73 * be split over multiple arguments. The last argument of a SQL
74 * statement will be terminated with a semilocon. Each SQL statement is
75 * merged into a single zero terminated string. Boundaries between
76 * arguments are replaced by space.
77 *
78 * RETURNS
79 *
80 * Buffer with SQL statements */
81
82 static char **args_concat(TABDCA *dca)
83 {
84 const char *arg;
85 int i;
86 int j;
87 int j0;
88 int j1;
89 int len;
90 int lentot;
91 int narg;
92 int nline = 0;
93 void *ret;
94 char **sqllines = NULL;
95
96 narg = mpl_tab_num_args(dca);
97 /* The SQL statements start with argument 3. */
98 if (narg < 3)
99 return NULL;
100 /* Count the SQL statements */
101 for (j = 3; j <= narg; j++)
102 {
103 arg = mpl_tab_get_arg(dca, j);
104 len = strlen(arg);
105 if (arg[len-1] == ';' || j == narg)
106 nline ++;
107 }
108 /* Allocate string buffer. */
109 sqllines = (char **) xmalloc((nline+1) * sizeof(char **));
110 /* Join arguments */
111 sqllines[0] = NULL;
112 j0 = 3;
113 i = 0;
114 lentot = 0;
115 for (j = 3; j <= narg; j++)
116 {
117 arg = mpl_tab_get_arg(dca, j);
118 len = strlen(arg);
119 lentot += len;
120 if (arg[len-1] == ';' || j == narg)
121 { /* Join arguments for a single SQL statement */
122 sqllines[i] = xmalloc(lentot+1);
123 sqllines[i+1] = NULL;
124 sqllines[i][0] = 0x00;
125 for (j1 = j0; j1 <= j; j1++)
126 { if(j1>j0)
127 strcat(sqllines[i], " ");
128 strcat(sqllines[i], mpl_tab_get_arg(dca, j1));
129 }
130 len = strlen(sqllines[i]);
131 if (sqllines[i][len-1] == ';')
132 sqllines[i][len-1] = 0x00;
133 j0 = j+1;
134 i++;
135 lentot = 0;
136 }
137 }
138 return sqllines;
139 }
140
141 /***********************************************************************
142 * NAME
143 *
144 * free_buffer - free multiline string buffer
145 *
146 * SYNOPSIS
147 *
148 * static void free_buffer(char **buf);
149 *
150 * DESCRIPTION
151 *
152 * buf is a list of strings terminated by NULL.
153 * The memory for the strings and for the list is released. */
154
155 static void free_buffer(char **buf)
156 { int i;
157
158 for(i = 0; buf[i] != NULL; i++)
159 xfree(buf[i]);
160 xfree(buf);
161 }
162
163 static int db_escaped_string_length(const char* from)
164 /* length of escaped string */
165 {
166 int count;
167 const char *pointer;
168
169 for (pointer = from, count = 0; *pointer != (char) '\0'; pointer++,
170 count++)
171 {
172 switch (*pointer)
173 {
174 case '\'':
175 count++;
176 break;
177 }
178 }
179
180 return count;
181 }
182
183 static int db_escape_string (char *to, const char *from)
184 /* escape string*/
185 {
186 const char *source = from;
187 char *target = to;
188 unsigned int remaining;
189
190 remaining = strlen(from);
191
192 if (to == NULL)
193 to = (char *) (from + remaining);
194
195 while (remaining > 0)
196 {
197 switch (*source)
198 {
199 case '\'':
200 *target = '\'';
201 target++;
202 *target = '\'';
203 break;
204
205 default:
206 *target = *source;
207 }
208 source++;
209 target++;
210 remaining--;
211 }
212
213 /* Write the terminating NUL character. */
214 *target = '\0';
215
216 return target - to;
217 }
218
219 static char *db_generate_select_stmt(TABDCA *dca)
220 /* generate select statement */
221 {
222 char *arg;
223 char const *field;
224 char *query;
225 int j;
226 int narg;
227 int nf;
228 int total;
229
230 total = 50;
231 nf = mpl_tab_num_flds(dca);
232 narg = mpl_tab_num_args(dca);
233 for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
234 {
235 field = mpl_tab_get_name(dca, j);
236 total += strlen(field);
237 total += 2;
238 }
239 arg = (char *) mpl_tab_get_arg(dca, narg);
240 total += strlen(arg);
241 query = xmalloc( total * sizeof(char));
242 strcpy (query, "SELECT ");
243 for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
244 {
245 field = mpl_tab_get_name(dca, j);
246 strcat(query, field);
247 if ( j < nf )
248 strcat(query, ", ");
249 }
250 strcat(query, " FROM ");
251 strcat(query, arg);
252 return query;
253 }
254
255 static char *db_generate_insert_stmt(TABDCA *dca)
256 /* generate insert statement */
257 {
258 char *arg;
259 char const *field;
260 char *query;
261 int j;
262 int narg;
263 int nf;
264 int total;
265
266 total = 50;
267 nf = mpl_tab_num_flds(dca);
268 narg = mpl_tab_num_args(dca);
269 for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
270 {
271 field = mpl_tab_get_name(dca, j);
272 total += strlen(field);
273 total += 5;
274 }
275 arg = (char *) mpl_tab_get_arg(dca, narg);
276 total += strlen(arg);
277 query = xmalloc( (total+1) * sizeof(char));
278 strcpy (query, "INSERT INTO ");
279 strcat(query, arg);
280 strcat(query, " ( ");
281 for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
282 {
283 field = mpl_tab_get_name(dca, j);
284 strcat(query, field);
285 if ( j < nf )
286 strcat(query, ", ");
287 }
288 strcat(query, " ) VALUES ( ");
289 for (j=1; j <= nf && j <= SQL_FIELD_MAX; j++)
290 {
291 strcat(query, "?");
292 if ( j < nf )
293 strcat(query, ", ");
294 }
295 strcat(query, " )");
296 return query;
297 }
298
299 #endif
300
301 /**********************************************************************/
302
303 #ifndef HAVE_ODBC
304
305 void *db_iodbc_open(TABDCA *dca, int mode)
306 { xassert(dca == dca);
307 xassert(mode == mode);
308 xprintf("iODBC table driver not supported\n");
309 return NULL;
310 }
311
312 int db_iodbc_read(TABDCA *dca, void *link)
313 { xassert(dca != dca);
314 xassert(link != link);
315 return 0;
316 }
317
318 int db_iodbc_write(TABDCA *dca, void *link)
319 { xassert(dca != dca);
320 xassert(link != link);
321 return 0;
322 }
323
324 int db_iodbc_close(TABDCA *dca, void *link)
325 { xassert(dca != dca);
326 xassert(link != link);
327 return 0;
328 }
329
330 #else
331
332 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__WOE__)
333 #include <windows.h>
334 #endif
335
336 #include <sql.h>
337 #include <sqlext.h>
338
339 struct db_odbc
340 {
341 int mode; /*'R' = Read, 'W' = Write*/
342 SQLHDBC hdbc; /*connection handle*/
343 SQLHENV henv; /*environment handle*/
344 SQLHSTMT hstmt; /*statement handle*/
345 SQLSMALLINT nresultcols; /* columns in result*/
346 SQLULEN collen[SQL_FIELD_MAX+1];
347 SQLLEN outlen[SQL_FIELD_MAX+1];
348 SQLSMALLINT coltype[SQL_FIELD_MAX+1];
349 SQLCHAR data[SQL_FIELD_MAX+1][SQL_FDLEN_MAX+1];
350 SQLCHAR colname[SQL_FIELD_MAX+1][SQL_FDLEN_MAX+1];
351 int isnumeric[SQL_FIELD_MAX+1];
352 int nf;
353 /* number of fields in the csv file */
354 int ref[1+SQL_FIELD_MAX];
355 /* ref[k] = k', if k-th field of the csv file corresponds to
356 k'-th field in the table statement; if ref[k] = 0, k-th field
357 of the csv file is ignored */
358 SQLCHAR *query;
359 /* query generated by db_iodbc_open */
360 };
361
362 SQLRETURN SQL_API dl_SQLAllocHandle (
363 SQLSMALLINT HandleType,
364 SQLHANDLE InputHandle,
365 SQLHANDLE *OutputHandle)
366 {
367 typedef SQLRETURN SQL_API ep_SQLAllocHandle(
368 SQLSMALLINT HandleType,
369 SQLHANDLE InputHandle,
370 SQLHANDLE *OutputHandle);
371
372 ep_SQLAllocHandle *fn;
373 fn = (ep_SQLAllocHandle *) xdlsym(h_odbc, "SQLAllocHandle");
374 xassert(fn != NULL);
375 return (*fn)(HandleType, InputHandle, OutputHandle);
376 }
377
378 SQLRETURN SQL_API dl_SQLBindCol (
379 SQLHSTMT StatementHandle,
380 SQLUSMALLINT ColumnNumber,
381 SQLSMALLINT TargetType,
382 SQLPOINTER TargetValue,
383 SQLLEN BufferLength,
384 SQLLEN *StrLen_or_Ind)
385 {
386 typedef SQLRETURN SQL_API ep_SQLBindCol(
387 SQLHSTMT StatementHandle,
388 SQLUSMALLINT ColumnNumber,
389 SQLSMALLINT TargetType,
390 SQLPOINTER TargetValue,
391 SQLLEN BufferLength,
392 SQLLEN *StrLen_or_Ind);
393 ep_SQLBindCol *fn;
394 fn = (ep_SQLBindCol *) xdlsym(h_odbc, "SQLBindCol");
395 xassert(fn != NULL);
396 return (*fn)(StatementHandle, ColumnNumber, TargetType,
397 TargetValue, BufferLength, StrLen_or_Ind);
398 }
399
400 SQLRETURN SQL_API dl_SQLCloseCursor (
401 SQLHSTMT StatementHandle)
402 {
403 typedef SQLRETURN SQL_API ep_SQLCloseCursor (
404 SQLHSTMT StatementHandle);
405
406 ep_SQLCloseCursor *fn;
407 fn = (ep_SQLCloseCursor *) xdlsym(h_odbc, "SQLCloseCursor");
408 xassert(fn != NULL);
409 return (*fn)(StatementHandle);
410 }
411
412
413 SQLRETURN SQL_API dl_SQLDisconnect (
414 SQLHDBC ConnectionHandle)
415 {
416 typedef SQLRETURN SQL_API ep_SQLDisconnect(
417 SQLHDBC ConnectionHandle);
418
419 ep_SQLDisconnect *fn;
420 fn = (ep_SQLDisconnect *) xdlsym(h_odbc, "SQLDisconnect");
421 xassert(fn != NULL);
422 return (*fn)(ConnectionHandle);
423 }
424
425 SQLRETURN SQL_API dl_SQLDriverConnect (
426 SQLHDBC hdbc,
427 SQLHWND hwnd,
428 SQLCHAR *szConnStrIn,
429 SQLSMALLINT cbConnStrIn,
430 SQLCHAR *szConnStrOut,
431 SQLSMALLINT cbConnStrOutMax,
432 SQLSMALLINT *pcbConnStrOut,
433 SQLUSMALLINT fDriverCompletion)
434 {
435 typedef SQLRETURN SQL_API ep_SQLDriverConnect(
436 SQLHDBC hdbc,
437 SQLHWND hwnd,
438 SQLCHAR * szConnStrIn,
439 SQLSMALLINT cbConnStrIn,
440 SQLCHAR * szConnStrOut,
441 SQLSMALLINT cbConnStrOutMax,
442 SQLSMALLINT * pcbConnStrOut,
443 SQLUSMALLINT fDriverCompletion);
444
445 ep_SQLDriverConnect *fn;
446 fn = (ep_SQLDriverConnect *) xdlsym(h_odbc, "SQLDriverConnect");
447 xassert(fn != NULL);
448 return (*fn)(hdbc, hwnd, szConnStrIn, cbConnStrIn, szConnStrOut,
449 cbConnStrOutMax, pcbConnStrOut, fDriverCompletion);
450 }
451
452 SQLRETURN SQL_API dl_SQLEndTran (
453 SQLSMALLINT HandleType,
454 SQLHANDLE Handle,
455 SQLSMALLINT CompletionType)
456 {
457 typedef SQLRETURN SQL_API ep_SQLEndTran (
458 SQLSMALLINT HandleType,
459 SQLHANDLE Handle,
460 SQLSMALLINT CompletionType);
461
462 ep_SQLEndTran *fn;
463 fn = (ep_SQLEndTran *) xdlsym(h_odbc, "SQLEndTran");
464 xassert(fn != NULL);
465 return (*fn)(HandleType, Handle, CompletionType);
466 }
467
468 SQLRETURN SQL_API dl_SQLExecDirect (
469 SQLHSTMT StatementHandle,
470 SQLCHAR * StatementText,
471 SQLINTEGER TextLength)
472 {
473 typedef SQLRETURN SQL_API ep_SQLExecDirect (
474 SQLHSTMT StatementHandle,
475 SQLCHAR * StatementText,
476 SQLINTEGER TextLength);
477
478 ep_SQLExecDirect *fn;
479 fn = (ep_SQLExecDirect *) xdlsym(h_odbc, "SQLExecDirect");
480 xassert(fn != NULL);
481 return (*fn)(StatementHandle, StatementText, TextLength);
482 }
483
484 SQLRETURN SQL_API dl_SQLFetch (
485 SQLHSTMT StatementHandle)
486 {
487 typedef SQLRETURN SQL_API ep_SQLFetch (
488 SQLHSTMT StatementHandle);
489
490 ep_SQLFetch *fn;
491 fn = (ep_SQLFetch*) xdlsym(h_odbc, "SQLFetch");
492 xassert(fn != NULL);
493 return (*fn)(StatementHandle);
494 }
495
496 SQLRETURN SQL_API dl_SQLFreeHandle (
497 SQLSMALLINT HandleType,
498 SQLHANDLE Handle)
499 {
500 typedef SQLRETURN SQL_API ep_SQLFreeHandle (
501 SQLSMALLINT HandleType,
502 SQLHANDLE Handle);
503
504 ep_SQLFreeHandle *fn;
505 fn = (ep_SQLFreeHandle *) xdlsym(h_odbc, "SQLFreeHandle");
506 xassert(fn != NULL);
507 return (*fn)(HandleType, Handle);
508 }
509
510 SQLRETURN SQL_API dl_SQLDescribeCol (
511 SQLHSTMT StatementHandle,
512 SQLUSMALLINT ColumnNumber,
513 SQLCHAR * ColumnName,
514 SQLSMALLINT BufferLength,
515 SQLSMALLINT * NameLength,
516 SQLSMALLINT * DataType,
517 SQLULEN * ColumnSize,
518 SQLSMALLINT * DecimalDigits,
519 SQLSMALLINT * Nullable)
520 {
521 typedef SQLRETURN SQL_API ep_SQLDescribeCol (
522 SQLHSTMT StatementHandle,
523 SQLUSMALLINT ColumnNumber,
524 SQLCHAR *ColumnName,
525 SQLSMALLINT BufferLength,
526 SQLSMALLINT *NameLength,
527 SQLSMALLINT *DataType,
528 SQLULEN *ColumnSize,
529 SQLSMALLINT *DecimalDigits,
530 SQLSMALLINT *Nullable);
531
532 ep_SQLDescribeCol *fn;
533 fn = (ep_SQLDescribeCol *) xdlsym(h_odbc, "SQLDescribeCol");
534 xassert(fn != NULL);
535 return (*fn)(StatementHandle, ColumnNumber, ColumnName,
536 BufferLength, NameLength,
537 DataType, ColumnSize, DecimalDigits, Nullable);
538 }
539
540 SQLRETURN SQL_API dl_SQLGetDiagRec (
541 SQLSMALLINT HandleType,
542 SQLHANDLE Handle,
543 SQLSMALLINT RecNumber,
544 SQLCHAR *Sqlstate,
545 SQLINTEGER *NativeError,
546 SQLCHAR *MessageText,
547 SQLSMALLINT BufferLength,
548 SQLSMALLINT *TextLength)
549 {
550 typedef SQLRETURN SQL_API ep_SQLGetDiagRec (
551 SQLSMALLINT HandleType,
552 SQLHANDLE Handle,
553 SQLSMALLINT RecNumber,
554 SQLCHAR *Sqlstate,
555 SQLINTEGER *NativeError,
556 SQLCHAR *MessageText,
557 SQLSMALLINT BufferLength,
558 SQLSMALLINT *TextLength);
559
560 ep_SQLGetDiagRec *fn;
561 fn = (ep_SQLGetDiagRec *) xdlsym(h_odbc, "SQLGetDiagRec");
562 xassert(fn != NULL);
563 return (*fn)(HandleType, Handle, RecNumber, Sqlstate,
564 NativeError, MessageText, BufferLength, TextLength);
565 }
566
567 SQLRETURN SQL_API dl_SQLGetInfo (
568 SQLHDBC ConnectionHandle,
569 SQLUSMALLINT InfoType,
570 SQLPOINTER InfoValue,
571 SQLSMALLINT BufferLength,
572 SQLSMALLINT *StringLength)
573 {
574 typedef SQLRETURN SQL_API ep_SQLGetInfo (
575 SQLHDBC ConnectionHandle,
576 SQLUSMALLINT InfoType,
577 SQLPOINTER InfoValue,
578 SQLSMALLINT BufferLength,
579 SQLSMALLINT *StringLength);
580
581 ep_SQLGetInfo *fn;
582 fn = (ep_SQLGetInfo *) xdlsym(h_odbc, "SQLGetInfo");
583 xassert(fn != NULL);
584 return (*fn)(ConnectionHandle, InfoType, InfoValue, BufferLength,
585 StringLength);
586 }
587
588 SQLRETURN SQL_API dl_SQLNumResultCols (
589 SQLHSTMT StatementHandle,
590 SQLSMALLINT *ColumnCount)
591 {
592 typedef SQLRETURN SQL_API ep_SQLNumResultCols (
593 SQLHSTMT StatementHandle,
594 SQLSMALLINT *ColumnCount);
595
596 ep_SQLNumResultCols *fn;
597 fn = (ep_SQLNumResultCols *) xdlsym(h_odbc, "SQLNumResultCols");
598 xassert(fn != NULL);
599 return (*fn)(StatementHandle, ColumnCount);
600 }
601
602 SQLRETURN SQL_API dl_SQLSetConnectAttr (
603 SQLHDBC ConnectionHandle,
604 SQLINTEGER Attribute,
605 SQLPOINTER Value,
606 SQLINTEGER StringLength)
607 {
608 typedef SQLRETURN SQL_API ep_SQLSetConnectAttr (
609 SQLHDBC ConnectionHandle,
610 SQLINTEGER Attribute,
611 SQLPOINTER Value,
612 SQLINTEGER StringLength);
613
614 ep_SQLSetConnectAttr *fn;
615 fn = (ep_SQLSetConnectAttr *) xdlsym(h_odbc, "SQLSetConnectAttr");
616 xassert(fn != NULL);
617 return (*fn)(ConnectionHandle, Attribute, Value, StringLength);
618 }
619
620 SQLRETURN SQL_API dl_SQLSetEnvAttr (
621 SQLHENV EnvironmentHandle,
622 SQLINTEGER Attribute,
623 SQLPOINTER Value,
624 SQLINTEGER StringLength)
625 {
626 typedef SQLRETURN SQL_API ep_SQLSetEnvAttr (
627 SQLHENV EnvironmentHandle,
628 SQLINTEGER Attribute,
629 SQLPOINTER Value,
630 SQLINTEGER StringLength);
631
632 ep_SQLSetEnvAttr *fn;
633 fn = (ep_SQLSetEnvAttr *) xdlsym(h_odbc, "SQLSetEnvAttr");
634 xassert(fn != NULL);
635 return (*fn)(EnvironmentHandle, Attribute, Value, StringLength);
636 }
637
638 static void extract_error(
639 char *fn,
640 SQLHANDLE handle,
641 SQLSMALLINT type);
642
643 static int is_numeric(
644 SQLSMALLINT coltype);
645
646 /***********************************************************************
647 * NAME
648 *
649 * db_iodbc_open - open connection to ODBC data base
650 *
651 * SYNOPSIS
652 *
653 * #include "glpsql.h"
654 * void *db_iodbc_open(TABDCA *dca, int mode);
655 *
656 * DESCRIPTION
657 *
658 * The routine db_iodbc_open opens a connection to an ODBC data base.
659 * It then executes the sql statements passed.
660 *
661 * In the case of table read the SELECT statement is executed.
662 *
663 * In the case of table write the INSERT statement is prepared.
664 * RETURNS
665 *
666 * The routine returns a pointer to data storage area created. */
667 void *db_iodbc_open(TABDCA *dca, int mode)
668 { void *ret;
669 char **sqllines;
670
671 sqllines = args_concat(dca);
672 if (sqllines == NULL)
673 { xprintf("Missing arguments in table statement.\n"
674 "Please, supply table driver, dsn, and query.\n");
675 return NULL;
676 }
677 ret = db_iodbc_open_int(dca, mode, (const char **) sqllines);
678 free_buffer(sqllines);
679 return ret;
680 }
681
682 static void *db_iodbc_open_int(TABDCA *dca, int mode, const char
683 **sqllines)
684 {
685 struct db_odbc *sql;
686 SQLRETURN ret;
687 SQLCHAR FAR *dsn;
688 SQLCHAR info[256];
689 SQLSMALLINT colnamelen;
690 SQLSMALLINT nullable;
691 SQLSMALLINT scale;
692 const char *arg;
693 int narg;
694 int i, j;
695 int total;
696
697 if (libodbc == NULL)
698 {
699 xprintf("No loader for shared ODBC library available\n");
700 return NULL;
701 }
702
703 if (h_odbc == NULL)
704 {
705 h_odbc = xdlopen(libodbc);
706 if (h_odbc == NULL)
707 { xprintf("unable to open library %s\n", libodbc);
708 xprintf("%s\n", xerrmsg());
709 return NULL;
710 }
711 }
712
713 sql = (struct db_odbc *) xmalloc(sizeof(struct db_odbc));
714 if (sql == NULL)
715 return NULL;
716
717 sql->mode = mode;
718 sql->hdbc = NULL;
719 sql->henv = NULL;
720 sql->hstmt = NULL;
721 sql->query = NULL;
722 narg = mpl_tab_num_args(dca);
723
724 dsn = (SQLCHAR FAR *) mpl_tab_get_arg(dca, 2);
725 /* allocate an environment handle */
726 ret = dl_SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,
727 &(sql->henv));
728 /* set attribute to enable application to run as ODBC 3.0
729 application */
730 ret = dl_SQLSetEnvAttr(sql->henv, SQL_ATTR_ODBC_VERSION,
731 (void *) SQL_OV_ODBC3, 0);
732 /* allocate a connection handle */
733 ret = dl_SQLAllocHandle(SQL_HANDLE_DBC, sql->henv, &(sql->hdbc));
734 /* connect */
735 ret = dl_SQLDriverConnect(sql->hdbc, NULL, dsn, SQL_NTS, NULL, 0,
736 NULL, SQL_DRIVER_COMPLETE);
737 if (SQL_SUCCEEDED(ret))
738 { /* output information about data base connection */
739 xprintf("Connected to ");
740 dl_SQLGetInfo(sql->hdbc, SQL_DBMS_NAME, (SQLPOINTER)info,
741 sizeof(info), NULL);
742 xprintf("%s ", info);
743 dl_SQLGetInfo(sql->hdbc, SQL_DBMS_VER, (SQLPOINTER)info,
744 sizeof(info), NULL);
745 xprintf("%s - ", info);
746 dl_SQLGetInfo(sql->hdbc, SQL_DATABASE_NAME, (SQLPOINTER)info,
747 sizeof(info), NULL);
748 xprintf("%s\n", info);
749 }
750 else
751 { /* describe error */
752 xprintf("Failed to connect\n");
753 extract_error("SQLDriverConnect", sql->hdbc, SQL_HANDLE_DBC);
754 dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
755 dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
756 xfree(sql);
757 return NULL;
758 }
759 /* set AUTOCOMMIT on*/
760 ret = dl_SQLSetConnectAttr(sql->hdbc, SQL_ATTR_AUTOCOMMIT,
761 (SQLPOINTER)SQL_AUTOCOMMIT_ON, 0);
762 /* allocate a statement handle */
763 ret = dl_SQLAllocHandle(SQL_HANDLE_STMT, sql->hdbc, &(sql->hstmt));
764
765 /* initialization queries */
766 for(j = 0; sqllines[j+1] != NULL; j++)
767 {
768 sql->query = (SQLCHAR *) sqllines[j];
769 xprintf("%s\n", sql->query);
770 ret = dl_SQLExecDirect(sql->hstmt, sql->query, SQL_NTS);
771 switch (ret)
772 {
773 case SQL_SUCCESS:
774 case SQL_SUCCESS_WITH_INFO:
775 case SQL_NO_DATA_FOUND:
776 break;
777 default:
778 xprintf("db_iodbc_open: Query\n\"%s\"\nfailed.\n",
779 sql->query);
780 extract_error("SQLExecDirect", sql->hstmt, SQL_HANDLE_STMT);
781 dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
782 dl_SQLDisconnect(sql->hdbc);
783 dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
784 dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
785 xfree(sql);
786 return NULL;
787 }
788 /* commit statement */
789 dl_SQLEndTran(SQL_HANDLE_ENV, sql->henv, SQL_COMMIT);
790 }
791
792 if ( sql->mode == 'R' )
793 { sql->nf = mpl_tab_num_flds(dca);
794 for(j = 0; sqllines[j] != NULL; j++)
795 arg = sqllines[j];
796 total = strlen(arg);
797 if (total > 7 && 0 == strncmp(arg, "SELECT ", 7))
798 {
799 total = strlen(arg);
800 sql->query = xmalloc( (total+1) * sizeof(char));
801 strcpy (sql->query, arg);
802 }
803 else
804 {
805 sql->query = db_generate_select_stmt(dca);
806 }
807 xprintf("%s\n", sql->query);
808 if (dl_SQLExecDirect(sql->hstmt, sql->query, SQL_NTS) !=
809 SQL_SUCCESS)
810 {
811 xprintf("db_iodbc_open: Query\n\"%s\"\nfailed.\n", sql->query);
812 extract_error("SQLExecDirect", sql->hstmt, SQL_HANDLE_STMT);
813 dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
814 dl_SQLDisconnect(sql->hdbc);
815 dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
816 dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
817 xfree(sql->query);
818 xfree(sql);
819 return NULL;
820 }
821 xfree(sql->query);
822 /* determine number of result columns */
823 ret = dl_SQLNumResultCols(sql->hstmt, &sql->nresultcols);
824 total = sql->nresultcols;
825 if (total > SQL_FIELD_MAX)
826 { xprintf("db_iodbc_open: Too many fields (> %d) in query.\n"
827 "\"%s\"\n", SQL_FIELD_MAX, sql->query);
828 dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
829 dl_SQLDisconnect(sql->hdbc);
830 dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
831 dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
832 xfree(sql->query);
833 return NULL;
834 }
835 for (i = 1; i <= total; i++)
836 { /* return a set of attributes for a column */
837 ret = dl_SQLDescribeCol(sql->hstmt, (SQLSMALLINT) i,
838 sql->colname[i], SQL_FDLEN_MAX,
839 &colnamelen, &(sql->coltype[i]), &(sql->collen[i]), &scale,
840 &nullable);
841 sql->isnumeric[i] = is_numeric(sql->coltype[i]);
842 /* bind columns to program vars, converting all types to CHAR*/
843 dl_SQLBindCol(sql->hstmt, i, SQL_CHAR, sql->data[i],
844 SQL_FDLEN_MAX, &(sql->outlen[i]));
845 for (j = sql->nf; j >= 1; j--)
846 { if (strcmp(mpl_tab_get_name(dca, j), sql->colname[i]) == 0)
847 break;
848 }
849 sql->ref[i] = j;
850 }
851 }
852 else if ( sql->mode == 'W' )
853 { for(j = 0; sqllines[j] != NULL; j++)
854 arg = sqllines[j];
855 if ( NULL != strchr(arg, '?') )
856 {
857 total = strlen(arg);
858 sql->query = xmalloc( (total+1) * sizeof(char));
859 strcpy (sql->query, arg);
860 }
861 else
862 {
863 sql->query = db_generate_insert_stmt(dca);
864 }
865 xprintf("%s\n", sql->query);
866 }
867 return sql;
868 }
869
870 int db_iodbc_read(TABDCA *dca, void *link)
871 {
872 struct db_odbc *sql;
873 SQLRETURN ret;
874 char buf[SQL_FDLEN_MAX+1];
875 int i;
876 int len;
877 double num;
878
879 sql = (struct db_odbc *) link;
880
881 xassert(sql != NULL);
882 xassert(sql->mode == 'R');
883
884 ret=dl_SQLFetch(sql->hstmt);
885 if (ret== SQL_ERROR)
886 return -1;
887 if (ret== SQL_NO_DATA_FOUND)
888 return -1; /*EOF*/
889 for (i=1; i <= sql->nresultcols; i++)
890 {
891 if (sql->ref[i] > 0)
892 {
893 len = sql->outlen[i];
894 if (len != SQL_NULL_DATA)
895 {
896 if (len > SQL_FDLEN_MAX)
897 len = SQL_FDLEN_MAX;
898 else if (len < 0)
899 len = 0;
900 strncpy(buf, (const char *) sql->data[i], len);
901 buf[len] = 0x00;
902 if (0 != (sql->isnumeric[i]))
903 { strspx(buf); /* remove spaces*/
904 if (str2num(buf, &num) != 0)
905 { xprintf("'%s' cannot be converted to a number.\n",
906 buf);
907 return 1;
908 }
909 mpl_tab_set_num(dca, sql->ref[i], num);
910 }
911 else
912 { mpl_tab_set_str(dca, sql->ref[i], strtrim(buf));
913 }
914 }
915 }
916 }
917 return 0;
918 }
919
920 int db_iodbc_write(TABDCA *dca, void *link)
921 {
922 struct db_odbc *sql;
923 char *part;
924 char *query;
925 char *template;
926 char num[50];
927 int k;
928 int len;
929 int nf;
930
931 sql = (struct db_odbc *) link;
932 xassert(sql != NULL);
933 xassert(sql->mode == 'W');
934
935 len = strlen(sql->query);
936 template = (char *) xmalloc( (len + 1) * sizeof(char) );
937 strcpy(template, sql->query);
938
939 nf = mpl_tab_num_flds(dca);
940 for (k = 1; k <= nf; k++)
941 { switch (mpl_tab_get_type(dca, k))
942 { case 'N':
943 len += 20;
944 break;
945 case 'S':
946 len += db_escaped_string_length(mpl_tab_get_str(dca, k));
947 len += 2;
948 break;
949 default:
950 xassert(dca != dca);
951 }
952 }
953 query = xmalloc( (len + 1 ) * sizeof(char) );
954 query[0] = 0x00;
955 for (k = 1, part = strtok (template, "?"); (part != NULL);
956 part = strtok (NULL, "?"), k++)
957 {
958 if (k > nf) break;
959 strcat( query, part );
960 switch (mpl_tab_get_type(dca, k))
961 { case 'N':
962 sprintf(num, "%-18g",mpl_tab_get_num(dca, k));
963 strcat( query, num );
964 break;
965 case 'S':
966 strcat( query, "'");
967 db_escape_string( query + strlen(query),
968 mpl_tab_get_str(dca, k) );
969 strcat( query, "'");
970 break;
971 default:
972 xassert(dca != dca);
973 }
974 }
975 if (part != NULL)
976 strcat(query, part);
977 if (dl_SQLExecDirect(sql->hstmt, (SQLCHAR *) query, SQL_NTS)
978 != SQL_SUCCESS)
979 {
980 xprintf("db_iodbc_write: Query\n\"%s\"\nfailed.\n", query);
981 extract_error("SQLExecDirect", sql->hdbc, SQL_HANDLE_DBC);
982 xfree(query);
983 xfree(template);
984 return 1;
985 }
986
987 xfree(query);
988 xfree(template);
989 return 0;
990 }
991
992 int db_iodbc_close(TABDCA *dca, void *link)
993 {
994 struct db_odbc *sql;
995
996 sql = (struct db_odbc *) link;
997 xassert(sql != NULL);
998 /* Commit */
999 if ( sql->mode == 'W' )
1000 dl_SQLEndTran(SQL_HANDLE_ENV, sql->henv, SQL_COMMIT);
1001 if ( sql->mode == 'R' )
1002 dl_SQLCloseCursor(sql->hstmt);
1003
1004 dl_SQLFreeHandle(SQL_HANDLE_STMT, sql->hstmt);
1005 dl_SQLDisconnect(sql->hdbc);
1006 dl_SQLFreeHandle(SQL_HANDLE_DBC, sql->hdbc);
1007 dl_SQLFreeHandle(SQL_HANDLE_ENV, sql->henv);
1008 if ( sql->mode == 'W' )
1009 xfree(sql->query);
1010 xfree(sql);
1011 dca->link = NULL;
1012 return 0;
1013 }
1014
1015 static void extract_error(
1016 char *fn,
1017 SQLHANDLE handle,
1018 SQLSMALLINT type)
1019 {
1020 SQLINTEGER i = 0;
1021 SQLINTEGER native;
1022 SQLCHAR state[ 7 ];
1023 SQLCHAR text[256];
1024 SQLSMALLINT len;
1025 SQLRETURN ret;
1026
1027 xprintf("\nThe driver reported the following diagnostics whilst "
1028 "running %s\n", fn);
1029
1030 do
1031 {
1032 ret = dl_SQLGetDiagRec(type, handle, ++i, state, &native, text,
1033 sizeof(text), &len );
1034 if (SQL_SUCCEEDED(ret))
1035 xprintf("%s:%ld:%ld:%s\n", state, i, native, text);
1036 }
1037 while( ret == SQL_SUCCESS );
1038 }
1039
1040 static int is_numeric(SQLSMALLINT coltype)
1041 {
1042 int ret = 0;
1043 switch (coltype)
1044 {
1045 case SQL_DECIMAL:
1046 case SQL_NUMERIC:
1047 case SQL_SMALLINT:
1048 case SQL_INTEGER:
1049 case SQL_REAL:
1050 case SQL_FLOAT:
1051 case SQL_DOUBLE:
1052 case SQL_TINYINT:
1053 case SQL_BIGINT:
1054 ret = 1;
1055 break;
1056 }
1057 return ret;
1058 }
1059
1060 #endif
1061
1062 /**********************************************************************/
1063
1064 #ifndef HAVE_MYSQL
1065
1066 void *db_mysql_open(TABDCA *dca, int mode)
1067 { xassert(dca == dca);
1068 xassert(mode == mode);
1069 xprintf("MySQL table driver not supported\n");
1070 return NULL;
1071 }
1072
1073 int db_mysql_read(TABDCA *dca, void *link)
1074 { xassert(dca != dca);
1075 xassert(link != link);
1076 return 0;
1077 }
1078
1079 int db_mysql_write(TABDCA *dca, void *link)
1080 { xassert(dca != dca);
1081 xassert(link != link);
1082 return 0;
1083 }
1084
1085 int db_mysql_close(TABDCA *dca, void *link)
1086 { xassert(dca != dca);
1087 xassert(link != link);
1088 return 0;
1089 }
1090
1091 #else
1092
1093 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__WOE__)
1094 #include <windows.h>
1095 #endif
1096
1097 #ifdef __CYGWIN__
1098 #define byte_defined 1
1099 #endif
1100
1101 #include <my_global.h>
1102 #include <my_sys.h>
1103 #include <mysql.h>
1104
1105 struct db_mysql
1106 {
1107 int mode; /*'R' = Read, 'W' = Write*/
1108 MYSQL *con; /*connection*/
1109 MYSQL_RES *res; /*result*/
1110 int nf;
1111 /* number of fields in the csv file */
1112 int ref[1+SQL_FIELD_MAX];
1113 /* ref[k] = k', if k-th field of the csv file corresponds to
1114 k'-th field in the table statement; if ref[k] = 0, k-th field
1115 of the csv file is ignored */
1116 char *query;
1117 /* query generated by db_mysql_open */
1118 };
1119
1120 void STDCALL dl_mysql_close(MYSQL *sock)
1121 {
1122 typedef void STDCALL ep_mysql_close(MYSQL *sock);
1123
1124 ep_mysql_close *fn;
1125 fn = (ep_mysql_close *) xdlsym(h_mysql, "mysql_close");
1126 xassert(fn != NULL);
1127 return (*fn)(sock);
1128 }
1129
1130 const char * STDCALL dl_mysql_error(MYSQL *mysql)
1131 {
1132 typedef const char * STDCALL ep_mysql_error(MYSQL *mysql);
1133
1134 ep_mysql_error *fn;
1135 fn = (ep_mysql_error *) xdlsym(h_mysql, "mysql_error");
1136 xassert(fn != NULL);
1137 return (*fn)(mysql);
1138 }
1139
1140 MYSQL_FIELD * STDCALL dl_mysql_fetch_fields(MYSQL_RES *res)
1141 {
1142 typedef MYSQL_FIELD * STDCALL
1143 ep_mysql_fetch_fields(MYSQL_RES *res);
1144
1145 ep_mysql_fetch_fields *fn;
1146 fn = (ep_mysql_fetch_fields *) xdlsym(h_mysql, "mysql_fetch_fields");
1147 xassert(fn != NULL);
1148 return (*fn)(res);
1149 }
1150
1151 unsigned long * STDCALL dl_mysql_fetch_lengths(MYSQL_RES *result)
1152 {
1153 typedef unsigned long * STDCALL
1154 ep_mysql_fetch_lengths(MYSQL_RES *result);
1155
1156 ep_mysql_fetch_lengths *fn;
1157 fn = (ep_mysql_fetch_lengths *) xdlsym(h_mysql,
1158 "mysql_fetch_lengths");
1159 xassert(fn != NULL);
1160 return (*fn)(result);
1161 }
1162
1163 MYSQL_ROW STDCALL dl_mysql_fetch_row(MYSQL_RES *result)
1164 {
1165 typedef MYSQL_ROW STDCALL ep_mysql_fetch_row(MYSQL_RES *result);
1166
1167 ep_mysql_fetch_row *fn;
1168 fn = (ep_mysql_fetch_row *) xdlsym(h_mysql, "mysql_fetch_row");
1169 xassert(fn != NULL);
1170 return (*fn)(result);
1171 }
1172
1173 unsigned int STDCALL dl_mysql_field_count(MYSQL *mysql)
1174 {
1175 typedef unsigned int STDCALL ep_mysql_field_count(MYSQL *mysql);
1176
1177 ep_mysql_field_count *fn;
1178 fn = (ep_mysql_field_count *) xdlsym(h_mysql, "mysql_field_count");
1179 xassert(fn != NULL);
1180 return (*fn)(mysql);
1181 }
1182
1183 MYSQL * STDCALL dl_mysql_init(MYSQL *mysql)
1184 {
1185 typedef MYSQL * STDCALL ep_mysql_init(MYSQL *mysql);
1186
1187 ep_mysql_init *fn;
1188 fn = (ep_mysql_init *) xdlsym(h_mysql, "mysql_init");
1189 xassert(fn != NULL);
1190 return (*fn)(mysql);
1191 }
1192
1193 unsigned int STDCALL dl_mysql_num_fields(MYSQL_RES *res)
1194 {
1195 typedef unsigned int STDCALL ep_mysql_num_fields(MYSQL_RES *res);
1196
1197 ep_mysql_num_fields *fn;
1198 fn = (ep_mysql_num_fields *) xdlsym(h_mysql, "mysql_num_fields");
1199 xassert(fn != NULL);
1200 return (*fn)(res);
1201 }
1202
1203 int STDCALL dl_mysql_query(MYSQL *mysql, const char *q)
1204 {
1205 typedef int STDCALL ep_mysql_query(MYSQL *mysql, const char *q);
1206
1207 ep_mysql_query *fn;
1208 fn = (ep_mysql_query *) xdlsym(h_mysql, "mysql_query");
1209 xassert(fn != NULL);
1210 return (*fn)(mysql, q);
1211 }
1212
1213 MYSQL * STDCALL dl_mysql_real_connect(MYSQL *mysql, const char *host,
1214 const char *user,
1215 const char *passwd,
1216 const char *db,
1217 unsigned int port,
1218 const char *unix_socket,
1219 unsigned long clientflag)
1220 {
1221 typedef MYSQL * STDCALL ep_mysql_real_connect(MYSQL *mysql,
1222 const char *host,
1223 const char *user,
1224 const char *passwd,
1225 const char *db,
1226 unsigned int port,
1227 const char *unix_socket,
1228 unsigned long clientflag);
1229
1230 ep_mysql_real_connect *fn;
1231 fn = (ep_mysql_real_connect *) xdlsym(h_mysql,
1232 "mysql_real_connect");
1233 xassert(fn != NULL);
1234 return (*fn)(mysql, host, user, passwd, db, port, unix_socket,
1235 clientflag);
1236 }
1237
1238 MYSQL_RES * STDCALL dl_mysql_use_result(MYSQL *mysql)
1239 {
1240 typedef MYSQL_RES * STDCALL ep_mysql_use_result(MYSQL *mysql);
1241 ep_mysql_use_result *fn;
1242 fn = (ep_mysql_use_result *) xdlsym(h_mysql, "mysql_use_result");
1243 xassert(fn != NULL);
1244 return (*fn)(mysql);
1245 }
1246
1247 /***********************************************************************
1248 * NAME
1249 *
1250 * db_mysql_open - open connection to ODBC data base
1251 *
1252 * SYNOPSIS
1253 *
1254 * #include "glpsql.h"
1255 * void *db_mysql_open(TABDCA *dca, int mode);
1256 *
1257 * DESCRIPTION
1258 *
1259 * The routine db_mysql_open opens a connection to a MySQL data base.
1260 * It then executes the sql statements passed.
1261 *
1262 * In the case of table read the SELECT statement is executed.
1263 *
1264 * In the case of table write the INSERT statement is prepared.
1265 * RETURNS
1266 *
1267 * The routine returns a pointer to data storage area created. */
1268
1269 void *db_mysql_open(TABDCA *dca, int mode)
1270 { void *ret;
1271 char **sqllines;
1272
1273 sqllines = args_concat(dca);
1274 if (sqllines == NULL)
1275 { xprintf("Missing arguments in table statement.\n"
1276 "Please, supply table driver, dsn, and query.\n");
1277 return NULL;
1278 }
1279 ret = db_mysql_open_int(dca, mode, (const char **) sqllines);
1280 free_buffer(sqllines);
1281 return ret;
1282 }
1283
1284 static void *db_mysql_open_int(TABDCA *dca, int mode, const char
1285 **sqllines)
1286 {
1287 struct db_mysql *sql = NULL;
1288 char *arg = NULL;
1289 const char *field;
1290 MYSQL_FIELD *fields;
1291 char *keyword;
1292 char *value;
1293 char *query;
1294 char *dsn;
1295 /* "Server=[server_name];Database=[database_name];UID=[username];*/
1296 /* PWD=[password];Port=[port]"*/
1297 char *server = NULL; /* Server */
1298 char *user = NULL; /* UID */
1299 char *password = NULL; /* PWD */
1300 char *database = NULL; /* Database */
1301 unsigned int port = 0; /* Port */
1302 int narg;
1303 int i, j, total;
1304
1305 if (libmysql == NULL)
1306 {
1307 xprintf("No loader for shared MySQL library available\n");
1308 return NULL;
1309 }
1310
1311 if (h_mysql == NULL)
1312 {
1313 h_mysql = xdlopen(libmysql);
1314 if (h_mysql == NULL)
1315 { xprintf("unable to open library %s\n", libmysql);
1316 xprintf("%s\n", xerrmsg());
1317 return NULL;
1318 }
1319 }
1320
1321 sql = (struct db_mysql *) xmalloc(sizeof(struct db_mysql));
1322 if (sql == NULL)
1323 return NULL;
1324 sql->mode = mode;
1325 sql->res = NULL;
1326 sql->query = NULL;
1327 sql->nf = mpl_tab_num_flds(dca);
1328
1329 narg = mpl_tab_num_args(dca);
1330 if (narg < 3 )
1331 xprintf("MySQL driver: string list too short \n");
1332
1333 /* get connection string*/
1334 dsn = (char *) mpl_tab_get_arg(dca, 2);
1335 /* copy connection string*/
1336 i = strlen(dsn);
1337 i++;
1338 arg = xmalloc(i * sizeof(char));
1339 strcpy(arg, dsn);
1340 /*tokenize connection string*/
1341 for (i = 1, keyword = strtok (arg, "="); (keyword != NULL);
1342 keyword = strtok (NULL, "="), i++)
1343 {
1344 value = strtok (NULL, ";");
1345 if (value==NULL)
1346 {
1347 xprintf("db_mysql_open: Missing value for keyword %s\n",
1348 keyword);
1349 xfree(arg);
1350 xfree(sql);
1351 return NULL;
1352 }
1353 if (0 == strcmp(keyword, "Server"))
1354 server = value;
1355 else if (0 == strcmp(keyword, "Database"))
1356 database = value;
1357 else if (0 == strcmp(keyword, "UID"))
1358 user = value;
1359 else if (0 == strcmp(keyword, "PWD"))
1360 password = value;
1361 else if (0 == strcmp(keyword, "Port"))
1362 port = (unsigned int) atol(value);
1363 }
1364 /* Connect to database */
1365 sql->con = dl_mysql_init(NULL);
1366 if (!dl_mysql_real_connect(sql->con, server, user, password, database,
1367 port, NULL, 0))
1368 {
1369 xprintf("db_mysql_open: Connect failed\n");
1370 xprintf("%s\n", dl_mysql_error(sql->con));
1371 xfree(arg);
1372 xfree(sql);
1373 return NULL;
1374 }
1375 xfree(arg);
1376
1377 for(j = 0; sqllines[j+1] != NULL; j++)
1378 { query = (char *) sqllines[j];
1379 xprintf("%s\n", query);
1380 if (dl_mysql_query(sql->con, query))
1381 {
1382 xprintf("db_mysql_open: Query\n\"%s\"\nfailed.\n", query);
1383 xprintf("%s\n",dl_mysql_error(sql->con));
1384 dl_mysql_close(sql->con);
1385 xfree(sql);
1386 return NULL;
1387 }
1388 }
1389
1390 if ( sql->mode == 'R' )
1391 { sql->nf = mpl_tab_num_flds(dca);
1392 for(j = 0; sqllines[j] != NULL; j++)
1393 arg = (char *) sqllines[j];
1394 total = strlen(arg);
1395 if (total > 7 && 0 == strncmp(arg, "SELECT ", 7))
1396 {
1397 total = strlen(arg);
1398 query = xmalloc( (total+1) * sizeof(char));
1399 strcpy (query, arg);
1400 }
1401 else
1402 {
1403 query = db_generate_select_stmt(dca);
1404 }
1405 xprintf("%s\n", query);
1406 if (dl_mysql_query(sql->con, query))
1407 {
1408 xprintf("db_mysql_open: Query\n\"%s\"\nfailed.\n", query);
1409 xprintf("%s\n",dl_mysql_error(sql->con));
1410 dl_mysql_close(sql->con);
1411 xfree(query);
1412 xfree(sql);
1413 return NULL;
1414 }
1415 xfree(query);
1416 sql->res = dl_mysql_use_result(sql->con);
1417 if (sql->res)
1418 {
1419 /* create references between query results and table fields*/
1420 total = dl_mysql_num_fields(sql->res);
1421 if (total > SQL_FIELD_MAX)
1422 { xprintf("db_mysql_open: Too many fields (> %d) in query.\n"
1423 "\"%s\"\n", SQL_FIELD_MAX, query);
1424 xprintf("%s\n",dl_mysql_error(sql->con));
1425 dl_mysql_close(sql->con);
1426 xfree(query);
1427 xfree(sql);
1428 return NULL;
1429 }
1430 fields = dl_mysql_fetch_fields(sql->res);
1431 for (i = 1; i <= total; i++)
1432 {
1433 for (j = sql->nf; j >= 1; j--)
1434 {
1435 if (strcmp(mpl_tab_get_name(dca, j), fields[i-1].name)
1436 == 0)
1437 break;
1438 }
1439 sql->ref[i] = j;
1440 }
1441 }
1442 else
1443 {
1444 if(dl_mysql_field_count(sql->con) == 0)
1445 {
1446 xprintf("db_mysql_open: Query was not a SELECT\n\"%s\"\n",
1447 query);
1448 xprintf("%s\n",dl_mysql_error(sql->con));
1449 xfree(query);
1450 xfree(sql);
1451 return NULL;
1452 }
1453 else
1454 {
1455 xprintf("db_mysql_open: Query\n\"%s\"\nfailed.\n", query);
1456 xprintf("%s\n",dl_mysql_error(sql->con));
1457 xfree(query);
1458 xfree(sql);
1459 return NULL;
1460 }
1461 }
1462 }
1463 else if ( sql->mode == 'W' )
1464 { for(j = 0; sqllines[j] != NULL; j++)
1465 arg = (char *) sqllines[j];
1466 if ( NULL != strchr(arg, '?') )
1467 {
1468 total = strlen(arg);
1469 query = xmalloc( (total+1) * sizeof(char));
1470 strcpy (query, arg);
1471 }
1472 else
1473 query = db_generate_insert_stmt(dca);
1474 sql->query = query;
1475 xprintf("%s\n", query);
1476 }
1477 return sql;
1478 }
1479
1480 int db_mysql_read(TABDCA *dca, void *link)
1481 { struct db_mysql *sql;
1482 char buf[255+1];
1483 char **row;
1484 unsigned long *lengths;
1485 MYSQL_FIELD *fields;
1486 double num;
1487 int len;
1488 unsigned long num_fields;
1489 int i;
1490
1491 sql = (struct db_mysql *) link;
1492
1493 xassert(sql != NULL);
1494 xassert(sql->mode == 'R');
1495 if (NULL == sql->res)
1496 {
1497 xprintf("db_mysql_read: no result set available");
1498 return 1;
1499 }
1500 if (NULL==(row = (char **)dl_mysql_fetch_row(sql->res))) {
1501 return -1; /*EOF*/
1502 }
1503 lengths = dl_mysql_fetch_lengths(sql->res);
1504 fields = dl_mysql_fetch_fields(sql->res);
1505 num_fields = dl_mysql_num_fields(sql->res);
1506 for (i=1; i <= num_fields; i++)
1507 {
1508 if (row[i-1] != NULL)
1509 { len = (size_t) lengths[i-1];
1510 if (len > 255)
1511 len = 255;
1512 strncpy(buf, (const char *) row[i-1], len);
1513 buf[len] = 0x00;
1514 if (0 != (fields[i-1].flags & NUM_FLAG))
1515 { strspx(buf); /* remove spaces*/
1516 if (str2num(buf, &num) != 0)
1517 { xprintf("'%s' cannot be converted to a number.\n", buf);
1518 return 1;
1519 }
1520 if (sql->ref[i] > 0)
1521 mpl_tab_set_num(dca, sql->ref[i], num);
1522 }
1523 else
1524 { if (sql->ref[i] > 0)
1525 mpl_tab_set_str(dca, sql->ref[i], strtrim(buf));
1526 }
1527 }
1528 }
1529 return 0;
1530 }
1531
1532 int db_mysql_write(TABDCA *dca, void *link)
1533 {
1534 struct db_mysql *sql;
1535 char *part;
1536 char *query;
1537 char *template;
1538 char num[50];
1539 int k;
1540 int len;
1541 int nf;
1542
1543 sql = (struct db_mysql *) link;
1544 xassert(sql != NULL);
1545 xassert(sql->mode == 'W');
1546
1547 len = strlen(sql->query);
1548 template = (char *) xmalloc( (len + 1) * sizeof(char) );
1549 strcpy(template, sql->query);
1550
1551 nf = mpl_tab_num_flds(dca);
1552 for (k = 1; k <= nf; k++)
1553 { switch (mpl_tab_get_type(dca, k))
1554 { case 'N':
1555 len += 20;
1556 break;
1557 case 'S':
1558 len += db_escaped_string_length(mpl_tab_get_str(dca, k));
1559 len += 2;
1560 break;
1561 default:
1562 xassert(dca != dca);
1563 }
1564 }
1565 query = xmalloc( (len + 1 ) * sizeof(char) );
1566 query[0] = 0x00;
1567 for (k = 1, part = strtok (template, "?"); (part != NULL);
1568 part = strtok (NULL, "?"), k++)
1569 {
1570 if (k > nf) break;
1571 strcat( query, part );
1572 switch (mpl_tab_get_type(dca, k))
1573 { case 'N':
1574 sprintf(num, "%-18g",mpl_tab_get_num(dca, k));
1575 strcat( query, num );
1576 break;
1577 case 'S':
1578 strcat( query, "'");
1579 db_escape_string( query + strlen(query),
1580 mpl_tab_get_str(dca, k) );
1581 strcat( query, "'");
1582 break;
1583 default:
1584 xassert(dca != dca);
1585 }
1586 }
1587 if (part != NULL)
1588 strcat(query, part);
1589 if (dl_mysql_query(sql->con, query))
1590 {
1591 xprintf("db_mysql_write: Query\n\"%s\"\nfailed.\n", query);
1592 xprintf("%s\n",dl_mysql_error(sql->con));
1593 xfree(query);
1594 xfree(template);
1595 return 1;
1596 }
1597
1598 xfree(query);
1599 xfree(template);
1600 return 0;
1601 }
1602
1603 int db_mysql_close(TABDCA *dca, void *link)
1604 {
1605 struct db_mysql *sql;
1606
1607 sql = (struct db_mysql *) link;
1608 xassert(sql != NULL);
1609 dl_mysql_close(sql->con);
1610 if ( sql->mode == 'W' )
1611 xfree(sql->query);
1612 xfree(sql);
1613 dca->link = NULL;
1614 return 0;
1615 }
1616
1617 #endif
1618
1619 /* eof */