-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbdimp.c
7443 lines (6559 loc) · 266 KB
/
dbdimp.c
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: dbdimp.c 15563 2013-01-21 14:19:00Z mjevans $
*
* portions Copyright (c) 1994,1995,1996,1997 Tim Bunce
* portions Copyright (c) 1997 Thomas K. Wenrich
* portions Copyright (c) 1997-2001 Jeff Urlwin
* portions Copyright (c) 2007-2012 Martin J. Evans
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the Perl README file.
*
*/
/*
* NOTES:
*
* o Trace levels 1 and 2 are reserved for DBI (see DBI::DBD) so don't
* use them here in DBIc_TRACE_LEVEL tests.
* "Trace Levels" in DBI defines trace levels as:
* 0 - Trace disabled.
* 1 - Trace DBI method calls returning with results or errors.
* 2 - Trace method entry with parameters and returning with results.
* 3 - As above, adding some high-level information from the driver
* and some internal information from the DBI.
* 4 - As above, adding more detailed information from the driver.
* 5 to 15 - As above but with more and more obscure information.
*
* SV Manipulation Functions
* http://perl.active-venture.com/pod/perlapi-svfunctions.html
* Formatted Printing of IVs, UVs, and NVs
* http://perldoc.perl.org/perlguts.html#Formatted-Printing-of-IVs,-UVs,-and-NVs
* http://cpansearch.perl.org/src/RURBAN/illguts-0.36/index.html
* Internal replacements for standard C library functions:
* http://search.cpan.org/~jesse/perl-5.12.1/pod/perlclib.pod
* http://search.cpan.org/dist/Devel-PPPort/PPPort.pm
*
* MS ODBC 64 bit:
* http://msdn.microsoft.com/en-us/library/ms716287%28v=vs.85%29.aspx
*/
#include <limits.h>
#define NEED_newRV_noinc
#define NEED_sv_2pv_flags
#define NEED_my_snprintf
#include "ODBC.h"
#if defined(WITH_UNICODE)
# include "unicode_helper.h"
#endif
/* trap iODBC on Unicode builds */
#if defined(WITH_UNICODE) && (defined(_IODBCUNIX_H) || defined(_IODBCEXT_H))
#error DBD::ODBC will not run properly with iODBC in unicode mode as iODBC defines wide characters as being 4 bytes in size
#endif
/* DBI defines the following but not until 1.617 so we replicate here for now */
/* will remove when DBD::ODBC requires 1.617 or above */
#ifndef DBIf_TRACE_SQL
# define DBIf_TRACE_SQL 0x00000100
#endif
#ifndef DBIf_TRACE_CON
# define DBIf_TRACE_CON 0x00000200
#endif
#ifndef DBIf_TRACE_ENC
# define DBIf_TRACE_ENC 0x00000400
#endif
#ifndef DBIf_TRACE_DBD
# define DBIf_TRACE_DBD 0x00000800
#endif
#ifndef DBIf_TRACE_TXN
# define DBIf_TRACE_TXN 0x000001000
#endif
/* combined DBI trace connection and encoding flags with DBD::ODBC ones */
/* Historically DBD::ODBC had 2 flags before they were made DBI ones */
#define UNICODE_TRACING (0x02000000|DBIf_TRACE_ENC|DBIf_TRACE_DBD)
#define CONNECTION_TRACING (0x04000000|DBIf_TRACE_CON|DBIf_TRACE_DBD)
#define DBD_TRACING DBIf_TRACE_DBD
#define TRANSACTION_TRACING (DBIf_TRACE_TXN|DBIf_TRACE_DBD)
#define SQL_TRACING (DBIf_TRACE_SQL|DBIf_TRACE_DBD)
#define TRACE0(a,b) PerlIO_printf(DBIc_LOGPIO(a), (b))
#define TRACE1(a,b,c) PerlIO_printf(DBIc_LOGPIO(a), (b), (c))
#define TRACE2(a,b,c,d) PerlIO_printf(DBIc_LOGPIO(a), (b), (c), (d))
#define TRACE3(a,b,c,d,e) PerlIO_printf(DBIc_LOGPIO(a), (b), (c), (d), (e))
/* An error return reserved for our internal use and should not clash with
any ODBC error codes like SQL_ERROR, SQL_INVALID_HANDLE etc.
It is used so we can call dbd_error but indicate there is no point in
calling SQLError as the error is internal */
#define DBDODBC_INTERNAL_ERROR -999
static int taf_callback_wrapper (
void *handle,
int type,
int event);
static int get_row_diag(SQLSMALLINT recno,
imp_sth_t *imp_sth,
char *state,
SQLINTEGER *native,
char *msg,
size_t max_msg);
static SQLSMALLINT default_parameter_type(
char *why, imp_sth_t *imp_sth, phs_t *phs);
static int post_connect(SV *dbh, imp_dbh_t *imp_dbh, SV *attr);
static int set_odbc_version(SV *dbh, imp_dbh_t *imp_dbh, SV* attr);
static const char *S_SqlTypeToString (SWORD sqltype);
static const char *S_SqlCTypeToString (SWORD sqltype);
static const char *cSqlTables = "SQLTables(%s,%s,%s,%s)";
static const char *cSqlPrimaryKeys = "SQLPrimaryKeys(%s,%s,%s)";
static const char *cSqlStatistics = "SQLStatistics(%s,%s,%s,%d,%d)";
static const char *cSqlForeignKeys = "SQLForeignKeys(%s,%s,%s,%s,%s,%s)";
static const char *cSqlColumns = "SQLColumns(%s,%s,%s,%s)";
static const char *cSqlGetTypeInfo = "SQLGetTypeInfo(%d)";
static SQLRETURN bind_columns(SV *h, imp_sth_t *imp_sth);
static void AllODBCErrors(HENV henv, HDBC hdbc, HSTMT hstmt, int output,
PerlIO *logfp);
static int check_connection_active(SV *h);
static int build_results(SV *sth, imp_sth_t *imp_sth,
SV *dbh, imp_dbh_t *imp_dbh,
RETCODE orc);
#ifdef WIN32
static HWND GetConsoleHwnd(void);
#endif
int dbd_describe(SV *sth, imp_sth_t *imp_sth, int more);
int dbd_db_login6_sv(SV *dbh, imp_dbh_t *imp_dbh, SV *dbname,
SV *uid, SV *pwd, SV *attr);
int dbd_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname,
char *uid, char *pwd, SV *attr);
int dbd_st_finish(SV *sth, imp_sth_t *imp_sth);
#define av_sz(av) (av_len(av) + 1)
/* for sanity/ease of use with potentially null strings */
#define XXSAFECHAR(p) ((p) ? (p) : "(null)")
/* unique value for db attrib that won't conflict with SQL types, just
* increment by one if you are adding */
#define ODBC_IGNORE_NAMED_PLACEHOLDERS 0x8332
#define ODBC_DEFAULT_BIND_TYPE 0x8333
#define ODBC_ASYNC_EXEC 0x8334
#define ODBC_ERR_HANDLER 0x8335
#define ODBC_ROWCACHESIZE 0x8336
#define ODBC_ROWSINCACHE 0x8337
#define ODBC_FORCE_REBIND 0x8338
#define ODBC_EXEC_DIRECT 0x8339
#define ODBC_VERSION 0x833A
#define ODBC_CURSORTYPE 0x833B
#define ODBC_QUERY_TIMEOUT 0x833C
#define ODBC_HAS_UNICODE 0x833D
#define ODBC_PUTDATA_START 0x833E
#define ODBC_OUTCON_STR 0x833F
#define ODBC_COLUMN_DISPLAY_SIZE 0x8340
#define ODBC_UTF8_ON 0x8341
#define ODBC_FORCE_BIND_TYPE 0x8342
#define ODBC_OLD_UNICODE 0x8343
#define ODBC_DESCRIBE_PARAMETERS 0x8344
#define ODBC_DRIVER_COMPLETE 0x8345
#define ODBC_BATCH_SIZE 0x8346
#define ODBC_ARRAY_OPERATIONS 0x8347
#define ODBC_TAF_CALLBACK 0x8348
/* This is the bind type for parameters we fall back to if the bind_param
method was not given a parameter type and SQLDescribeParam is not supported
or failed.
It also defines the point we switch from VARCHAR to LONGVARCHAR */
#ifdef WITH_UNICODE
# define ODBC_BACKUP_BIND_TYPE_VALUE SQL_WVARCHAR
# define ODBC_BACKUP_LONG_BIND_TYPE_VALUE SQL_WLONGVARCHAR
# define ODBC_SWITCH_TO_LONGVARCHAR 2000
#else
# define ODBC_BACKUP_BIND_TYPE_VALUE SQL_VARCHAR
# define ODBC_BACKUP_LONG_BIND_TYPE_VALUE SQL_LONGVARCHAR
# define ODBC_SWITCH_TO_LONGVARCHAR 4000
#endif
static int rebind_param(SV *sth, imp_sth_t *imp_sth, imp_dbh_t *imp_dbh, phs_t *phs);
static void get_param_type(SV *sth, imp_sth_t *imp_sth, imp_dbh_t *imp_dbh, phs_t *phs);
DBISTATE_DECLARE;
void dbd_init(dbistate_t *dbistate)
{
DBIS = dbistate;
}
static RETCODE odbc_set_query_timeout(imp_dbh_t *imp_dbh, HSTMT hstmt, UV odbc_timeout)
{
RETCODE rc;
if (DBIc_TRACE(imp_dbh, DBD_TRACING, 0, 3)) {
TRACE1(imp_dbh, " Set timeout to: %"UVuf"\n", odbc_timeout);
}
rc = SQLSetStmtAttr(hstmt,(SQLINTEGER)SQL_ATTR_QUERY_TIMEOUT,
(SQLPOINTER)odbc_timeout,(SQLINTEGER)SQL_IS_INTEGER);
if (!SQL_SUCCEEDED(rc)) {
/* Some drivers get upset with this so we ignore errors */
if (DBIc_TRACE(imp_dbh, DBD_TRACING, 0, 3))
TRACE1(
imp_dbh,
" !!Failed to set Statement ATTR Query Timeout to %"UVuf"\n",
odbc_timeout);
}
return rc;
}
static void odbc_clear_result_set(SV *sth, imp_sth_t *imp_sth)
{
SV *value;
char *key;
I32 keylen;
if (DBIc_TRACE(imp_sth, DBD_TRACING, 0, 3)) {
TRACE0(imp_sth, "odbc_clear_result_set\n");
}
Safefree(imp_sth->fbh);
Safefree(imp_sth->ColNames);
Safefree(imp_sth->RowBuffer);
/* dgood - Yikes! I don't want to go down to this level, */
/* but if I don't, it won't figure out that the */
/* number of columns have changed... */
if (DBIc_FIELDS_AV(imp_sth)) {
sv_free((SV*)DBIc_FIELDS_AV(imp_sth));
DBIc_FIELDS_AV(imp_sth) = Nullav;
}
while ( (value = hv_iternextsv((HV*)SvRV(sth), &key, &keylen)) ) {
if (strncmp(key, "NAME_", 5) == 0 ||
strncmp(key, "TYPE", 4) == 0 ||
strncmp(key, "PRECISION", 9) == 0 ||
strncmp(key, "SCALE", 5) == 0 ||
strncmp(key, "NULLABLE", 8) == 0) {
(void)hv_delete((HV*)SvRV(sth), key, keylen, G_DISCARD);
if (DBIc_TRACE(imp_sth, DBD_TRACING, 0, 4)) {
TRACE2(imp_sth, " ODBC_CLEAR_RESULTS '%s' => %s\n",
key, neatsvpv(value,0));
}
}
}
imp_sth->fbh = NULL;
imp_sth->ColNames = NULL;
imp_sth->RowBuffer = NULL;
imp_sth->done_desc = 0;
}
static void odbc_handle_outparams(imp_sth_t *imp_sth, int debug)
{
int i = (imp_sth->out_params_av) ? AvFILL(imp_sth->out_params_av)+1 : 0;
if (debug >= 3)
TRACE1(imp_sth, " processing %d output parameters\n", i);
while (--i >= 0) {
phs_t *phs = (phs_t*)(void*)SvPVX(AvARRAY(imp_sth->out_params_av)[i]);
SV *sv = phs->sv;
if (debug >= 8) {
TRACE2(imp_sth, " outparam %s, length:%ld\n",
phs->name, (long)phs->strlen_or_ind);
}
/* phs->strlen_or_ind has been updated by ODBC to hold the length
* of the result */
if (phs->strlen_or_ind != SQL_NULL_DATA) {
/*
* When ODBC fills an output parameter buffer, the size of the
* data that were available is written into the memory location
* provided by strlen_or_ind pointer argument during the
* SQLBindParameter() call.
*
* If the number of bytes available exceeds the size of the output
* buffer, ODBC will truncate the data such that it fits in the
* available buffer. However, the strlen_or_ind will still reflect
* the size of the data before it was truncated.
*
* This fact provides us a way to detect truncation on this particular
* output parameter. Otherwise, the only way to detect truncation is
* through a follow-up to a SQL_SUCCESS_WITH_INFO result. Such a call
* cannot return enough information to state exactly where the
* truncation occurred.
*/
SvPOK_only(sv); /* string, disable other OK bits */
if (phs->strlen_or_ind > phs->maxlen) { /* out param truncated */
SvCUR_set(sv, phs->maxlen);
*SvEND(sv) = '\0'; /* null terminate */
if (debug >= 2) {
PerlIO_printf(
DBIc_LOGPIO(imp_sth),
" outparam %s = '%s'\t(TRUNCATED from %ld to %ld)\n",
phs->name, SvPV_nolen(sv), (long)phs->strlen_or_ind,
(long)phs->maxlen);
}
} else { /* no truncation occurred */
SvCUR_set(sv, phs->strlen_or_ind); /* new length */
*SvEND(sv) = '\0'; /* null terminate */
if (phs->strlen_or_ind == phs->maxlen &&
(phs->sql_type == SQL_NUMERIC ||
phs->sql_type == SQL_DECIMAL ||
phs->sql_type == SQL_INTEGER ||
phs->sql_type == SQL_SMALLINT ||
phs->sql_type == SQL_FLOAT ||
phs->sql_type == SQL_REAL ||
phs->sql_type == SQL_DOUBLE)) {
/*
* fix up for oracle, which leaves the buffer at the size
* requested, but only returns a few characters. The
* intent is to truncate down to the actual number of
* characters necessary. Need to find the first null
* byte and set the length there.
*/
char *pstart = SvPV_nolen(sv);
char *p = pstart;
while (*p != '\0') {
p++;
}
if (debug >= 2) {
PerlIO_printf(
DBIc_LOGPIO(imp_sth),
" outparam %s = '%s'\t(len %ld), is numeric end"
" of buffer = %d\n",
phs->name, SvPV(sv,PL_na), (long)phs->strlen_or_ind,
p - pstart);
}
SvCUR_set(sv, p - pstart);
}
}
} else { /* is NULL */
if (debug >= 2)
TRACE1(imp_sth, " outparam %s = undef (NULL)\n", phs->name);
(void)SvOK_off(phs->sv);
}
}
}
static int build_results(SV *sth,
imp_sth_t *imp_sth,
SV *dbh,
imp_dbh_t *imp_dbh,
RETCODE orc)
{
RETCODE rc;
if (DBIc_TRACE(imp_sth, DBD_TRACING, 0, 3))
TRACE2(imp_sth, " build_results sql %p\t%s\n",
imp_sth->hstmt, imp_sth->statement);
/* init sth pointers */
imp_sth->fbh = NULL;
imp_sth->ColNames = NULL;
imp_sth->RowBuffer = NULL;
imp_sth->RowCount = -1;
imp_sth->odbc_column_display_size = imp_dbh->odbc_column_display_size;
imp_sth->odbc_utf8_on = imp_dbh->odbc_utf8_on;
if (!dbd_describe(sth, imp_sth, 0)) {
if (DBIc_TRACE(imp_sth, DBD_TRACING, 0, 3)) {
TRACE0(imp_sth, " !!dbd_describe failed, build_results...!\n");
}
SQLFreeHandle(SQL_HANDLE_STMT, imp_sth->hstmt);
imp_sth->hstmt = SQL_NULL_HSTMT;
return 0; /* dbd_describe already called dbd_error() */
}
if (DBIc_TRACE(imp_sth, DBD_TRACING, 0, 3)) {
TRACE0(imp_sth, " dbd_describe build_results #2...!\n");
}
/* TO_DO why is dbd_describe called again? */
if (dbd_describe(sth, imp_sth, 0) <= 0) {
if (DBIc_TRACE(imp_sth, DBD_TRACING, 0, 3)) {
TRACE0(imp_sth, " dbd_describe build_results #3...!\n");
}
return 0;
}
DBIc_IMPSET_on(imp_sth);
if (orc != SQL_NO_DATA) {
imp_sth->RowCount = -1;
rc = SQLRowCount(imp_sth->hstmt, &imp_sth->RowCount);
dbd_error(sth, rc, "build_results/SQLRowCount");
if (rc != SQL_SUCCESS) return -1;
} else {
imp_sth->RowCount = 0;
}
DBIc_ACTIVE_on(imp_sth); /* XXX should only set for select ? */
return 1;
}
int odbc_discon_all(SV *drh,
imp_drh_t *imp_drh)
{
/* The disconnect_all concept is flawed and needs more work */
if (!PL_dirty && !SvTRUE(get_sv("DBI::PERL_ENDING",0))) {
DBIh_SET_ERR_CHAR(drh, (imp_xxh_t*)imp_drh, Nullch, 1,
"disconnect_all not implemented", Nullch, Nullch);
return FALSE;
}
return FALSE;
}
/* error : <=(-2), ok row count : >=0, unknown count : (-1) */
SQLLEN dbd_db_execdirect(SV *dbh,
SV *statement )
{
D_imp_dbh(dbh);
SQLRETURN ret; /* SQLxxx return value */
SQLLEN rows;
SQLHSTMT stmt;
int dbh_active;
if ((dbh_active = check_connection_active(dbh)) == 0) return 0;
ret = SQLAllocHandle(SQL_HANDLE_STMT, imp_dbh->hdbc, &stmt );
if (!SQL_SUCCEEDED(ret)) {
dbd_error( dbh, ret, "Statement allocation error" );
return(-2);
}
/* if odbc_query_timeout has been set, set it in the driver */
if (imp_dbh->odbc_query_timeout != -1) {
ret = odbc_set_query_timeout(imp_dbh, stmt, imp_dbh->odbc_query_timeout);
if (!SQL_SUCCEEDED(ret)) {
dbd_error(dbh, ret, "execdirect set_query_timeout");
}
/* don't fail if the query timeout can't be set. */
}
if (DBIc_TRACE(imp_dbh, SQL_TRACING, 0, 3)) {
TRACE1(imp_dbh, " SQLExecDirect %s\n", SvPV_nolen(statement));
}
#ifdef WITH_UNICODE
if (SvOK(statement) && DO_UTF8(statement)) {
SQLWCHAR *wsql;
STRLEN wsql_len;
SV *sql_copy;
if (DBIc_TRACE(imp_dbh, UNICODE_TRACING, 0, 0)) /* odbcunicode */
TRACE0(imp_dbh, " Processing utf8 sql in unicode mode\n");
sql_copy = sv_mortalcopy(statement);
SV_toWCHAR(sql_copy);
wsql = (SQLWCHAR *)SvPV(sql_copy, wsql_len);
ret = SQLExecDirectW(stmt, wsql, wsql_len / sizeof(SQLWCHAR));
} else {
if (DBIc_TRACE(imp_dbh, UNICODE_TRACING, 0, 0)) /* odbcunicode */
TRACE0(imp_dbh, " Processing non utf8 sql in unicode mode\n");
ret = SQLExecDirect(stmt, (SQLCHAR *)SvPV_nolen(statement), SQL_NTS);
}
#else
if (DBIc_TRACE(imp_dbh, UNICODE_TRACING, 0, 0)) /* odbcunicode */
TRACE0(imp_dbh, " Processing sql in non-unicode mode\n");
ret = SQLExecDirect(stmt, (SQLCHAR *)SvPV_nolen(statement), SQL_NTS);
#endif
if (DBIc_TRACE(imp_dbh, DBD_TRACING, 0, 3))
TRACE1(imp_dbh, " SQLExecDirect = %d\n", ret);
if (!SQL_SUCCEEDED(ret) && ret != SQL_NO_DATA) {
dbd_error2(dbh, ret, "Execute immediate failed",
imp_dbh->henv, imp_dbh->hdbc, stmt );
rows = -2; /* error */
} else {
if (ret == SQL_NO_DATA) {
rows = 0;
}
else if (ret != SQL_SUCCESS) {
dbd_error2(dbh, ret, "Execute immediate success with info",
imp_dbh->henv, imp_dbh->hdbc, stmt );
}
ret = SQLRowCount(stmt, &rows);
if (!SQL_SUCCEEDED(ret)) {
dbd_error( dbh, ret, "SQLRowCount failed" );
rows = -1;
}
}
ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
if (!SQL_SUCCEEDED(ret)) {
dbd_error2(dbh, ret, "Statement destruction error",
imp_dbh->henv, imp_dbh->hdbc, stmt);
}
return rows;
}
void dbd_db_destroy(SV *dbh, imp_dbh_t *imp_dbh)
{
if (DBIc_ACTIVE(imp_dbh))
dbd_db_disconnect(dbh, imp_dbh);
/* Nothing in imp_dbh to be freed */
DBIc_IMPSET_off(imp_dbh);
if (DBIc_TRACE(imp_dbh, DBD_TRACING, 0, 8))
TRACE0(imp_dbh, " DBD::ODBC Disconnected!\n");
}
/*
* quick dumb function to handle case insensitivity for DSN= or DRIVER=
* in DSN...note this is because strncmpi is not available on all
* platforms using that name (VC++, Debian, etc most notably).
* Note, also, strupr doesn't seem to have a standard name, either...
*/
int dsnHasDriverOrDSN(char *dsn) {
char upper_dsn[512];
char *cp = upper_dsn;
strncpy(upper_dsn, dsn, sizeof(upper_dsn)-1);
upper_dsn[sizeof(upper_dsn)-1] = '\0';
while (*cp != '\0') {
*cp = toupper(*cp);
cp++; /* see rt 79190 was a sequence point error*/
}
return (strncmp(upper_dsn, "DSN=", 4) == 0 ||
strncmp(upper_dsn, "DRIVER=", 7) == 0);
}
int dsnHasUIDorPWD(char *dsn) {
char upper_dsn[512];
char *cp = upper_dsn;
strncpy(upper_dsn, dsn, sizeof(upper_dsn)-1);
upper_dsn[sizeof(upper_dsn)-1] = '\0';
while (*cp != '\0') {
*cp = toupper(*cp);
cp++; /* see rt 79190 was a sequence point error*/
}
return (strstr(upper_dsn, "UID=") != 0 || strstr(upper_dsn, "PWD=") != 0);
}
/************************************************************************/
/* */
/* dbd_db_login */
/* ============ */
/* */
/* NOTE: This is the old 5 argument version with no attribs */
/* */
/************************************************************************/
int dbd_db_login(
SV *dbh,
imp_dbh_t *imp_dbh,
char *dbname,
char *uid,
char *pwd)
{
return dbd_db_login6(dbh, imp_dbh, dbname, uid, pwd, Nullsv);
}
/************************************************************************/
/* */
/* dbd_db_login6_sv */
/* ================ */
/* */
/* This API was introduced in DBI after 1.607 (subversion revision */
/* 11723) and is the same as dbd_db_login6 except the connection */
/* strings are SVs so we can detect unicode strings and call */
/* SQLDriveConnectW. */
/* */
/************************************************************************/
int dbd_db_login6_sv(
SV *dbh,
imp_dbh_t *imp_dbh,
SV *dbname,
SV *uid,
SV *pwd,
SV *attr)
{
#ifndef WITH_UNICODE
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE0(imp_dbh, "non-Unicode login6_sv\n");
return dbd_db_login6(dbh, imp_dbh, SvPV_nolen(dbname),
(SvOK(uid) ? SvPV_nolen(uid) : NULL),
(SvOK(pwd) ? SvPV_nolen(pwd) : NULL), attr);
#else
D_imp_drh_from_dbh;
SQLRETURN rc;
SV *wconstr; /* copy of connection string in wide chrs */
/* decoded connection string in wide characters and its length to work
around an issue in older unixODBCs */
SQLWCHAR dc_constr[512];
STRLEN dc_constr_len;
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0)) {
TRACE2(imp_dbh, "Unicode login6 dbname=%s, uid=%s, pwd=xxxxx\n",
SvPV_nolen(dbname), neatsvpv(uid, 0));
}
imp_dbh->out_connect_string = Nullsv;
if (!imp_drh->connects) {
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &imp_drh->henv);
dbd_error(dbh, rc, "db_login6_sv/SQLAllocHandle(env)");
if (!SQL_SUCCEEDED(rc)) return 0;
if (set_odbc_version(dbh, imp_dbh, attr) != 1) return 0;
}
imp_dbh->henv = imp_drh->henv; /* needed for dbd_error */
/* If odbc_trace_file set, set it in ODBC */
{
SV **attr_sv;
char *file;
if ((attr_sv =
DBD_ATTRIB_GET_SVP(attr, "odbc_trace_file",
(I32)strlen("odbc_trace_file"))) != NULL) {
if (SvPOK(*attr_sv)) {
file = SvPV_nolen(*attr_sv);
rc = SQLSetConnectAttr(NULL, SQL_ATTR_TRACEFILE,
file, strlen(file));
if (!SQL_SUCCEEDED(rc)) {
warn("Failed to set trace file");
}
}
}
}
/* If odbc_trace enabled, turn ODBC tracing on */
{
UV dc = 0;
SV **svp;
DBD_ATTRIB_GET_IV(attr, "odbc_trace", 10, svp, dc);
if (svp && dc) {
rc = SQLSetConnectAttr(NULL, SQL_ATTR_TRACE,
(SQLPOINTER)SQL_OPT_TRACE_ON, 0);
if (!SQL_SUCCEEDED(rc)) {
warn("Failed to enable tracing");
}
}
}
rc = SQLAllocHandle(SQL_HANDLE_DBC, imp_drh->henv, &imp_dbh->hdbc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login6_sv/SQLAllocHandle(dbc)");
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
/* If odbc_driver_complete specified we need to grab it */
{
UV dc = 0;
SV **svp;
DBD_ATTRIB_GET_IV(attr, "odbc_driver_complete", 20, svp, dc);
if (svp && dc) {
imp_dbh->odbc_driver_complete = 1;
} else {
imp_dbh->odbc_driver_complete = 0;
}
}
/* If the connection string is too long to pass to SQLConnect or it
contains DSN or DRIVER, we've little choice but to call
SQLDriverConnect and need to tag the uid/pwd on the end of the
connection string (unless they already exist). */
if ((SvCUR(dbname) > SQL_MAX_DSN_LENGTH || /* too big for SQLConnect */
dsnHasDriverOrDSN(SvPV_nolen(dbname))) &&
!dsnHasUIDorPWD(SvPV_nolen(dbname))) {
if (SvOK(uid)) {
sv_catpv(dbname, ";UID=");
sv_catsv(dbname, uid);
}
if (SvOK(pwd)) {
sv_catpv(dbname, ";PWD=");
sv_catsv(dbname, pwd);
}
sv_catpv(dbname, ";");
/*sv_catpvf(dbname, ";UID=%s;PWD=%s;",
SvPV_nolen(uid), SvPV_nolen(pwd));*/
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE1(imp_dbh, "Now using dbname = %s\n", SvPV_nolen(dbname));
}
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE2(imp_dbh, " SQLDriverConnect '%s', '%s', 'xxxx'\n",
SvPV_nolen(dbname), neatsvpv(uid, 0));
wconstr = sv_mortalcopy(dbname);
utf8sv_to_wcharsv(wconstr);
/* The following is to work around a bug in SQLDriverConnectW in unixODBC
which in at least 2.2.11 (and probably up to 2.2.13 official release
[not pre-release]) core dumps if the wide connection string does not end
in a 0 (even though it should not matter as we pass the length. */
{
char *p;
memset(dc_constr, '\0', sizeof(dc_constr));
p = SvPV(wconstr, dc_constr_len);
if (dc_constr_len > (sizeof(dc_constr) - 2)) {
croak("Cannot process connection string - too long");
}
memcpy(dc_constr, p, dc_constr_len);
}
{
SQLWCHAR wout_str[512];
SQLSMALLINT wout_str_len;
#ifdef WIN32
if (imp_dbh->odbc_driver_complete) {
rc = SQLDriverConnectW(imp_dbh->hdbc,
GetConsoleHwnd(), /* no hwnd */
dc_constr,
(SQLSMALLINT)(dc_constr_len / sizeof(SQLWCHAR)),
wout_str, sizeof(wout_str) / sizeof(wout_str[0]),
&wout_str_len,
SQL_DRIVER_COMPLETE);
} else {
#endif
rc = SQLDriverConnectW(imp_dbh->hdbc,
0, /* no hwnd */
dc_constr,
(SQLSMALLINT)(dc_constr_len / sizeof(SQLWCHAR)),
wout_str, sizeof(wout_str) / sizeof(wout_str[0]),
&wout_str_len,
SQL_DRIVER_NOPROMPT);
#ifdef WIN32
}
#endif
if (SQL_SUCCEEDED(rc)) {
imp_dbh->out_connect_string = sv_newwvn(wout_str, wout_str_len);
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE1(imp_dbh, "Out connection string: %s\n",
SvPV_nolen(imp_dbh->out_connect_string));
}
}
if (!SQL_SUCCEEDED(rc)) {
SV *wuid, *wpwd;
SQLWCHAR *wuidp, *wpwdp;
SQLSMALLINT uid_len, pwd_len;
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE0(imp_dbh, " SQLDriverConnectW failed:\n");
/*
* Added code for DBD::ODBC 0.39 to help return a better
* error code in the case where the user is using a
* DSN-less connection and the dbname doesn't look like a
* true DSN.
*/
if (SvCUR(dbname) > SQL_MAX_DSN_LENGTH ||
dsnHasDriverOrDSN(SvPV_nolen(dbname))) {
/* must be DSN= or some "direct" connection attributes,
* probably best to error here and give the user a real
* error code because the SQLConnect call could hide the
* real problem.
*/
dbd_error(dbh, rc, "db_login6sv/SQLDriverConnectW");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV;
}
return 0;
}
/* ok, the DSN is short, so let's try to use it to connect
* and quietly take all error messages */
AllODBCErrors(imp_dbh->henv, imp_dbh->hdbc, 0, 0, DBIc_LOGPIO(imp_dbh));
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE2(imp_dbh, " SQLConnect '%s', '%s'\n",
neatsvpv(dbname, 0), neatsvpv(uid, 0));
wconstr = sv_mortalcopy(dbname);
utf8sv_to_wcharsv(wconstr);
if (SvOK(uid)) {
wuid = sv_mortalcopy(uid);
utf8sv_to_wcharsv(wuid);
wuidp = (SQLWCHAR *)SvPV_nolen(wuid);
uid_len = SvCUR(wuid) / sizeof(SQLWCHAR);
} else {
wuidp = NULL;
uid_len = 0;
}
if (SvOK(pwd)) {
wpwd = sv_mortalcopy(pwd);
utf8sv_to_wcharsv(wpwd);
wpwdp = (SQLWCHAR *)SvPV_nolen(wpwd);
pwd_len = SvCUR(wpwd) / sizeof(SQLWCHAR);
} else {
wpwdp = NULL;
pwd_len = 0;
}
rc = SQLConnectW(imp_dbh->hdbc,
(SQLWCHAR *)SvPV_nolen(wconstr),
(SQLSMALLINT)(SvCUR(wconstr) / sizeof(SQLWCHAR)),
wuidp, uid_len,
wpwdp, pwd_len);
}
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login6sv/SQLConnectW");
SQLFreeHandle(SQL_HANDLE_DBC, imp_dbh->hdbc);
imp_dbh->hdbc = SQL_NULL_HDBC;
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV;
}
return 0;
} else if (rc == SQL_SUCCESS_WITH_INFO) {
dbd_error(dbh, rc, "db_login6sv/SQLConnectW");
}
if (post_connect(dbh, imp_dbh, attr) != 1) return 0;
imp_drh->connects++;
DBIc_IMPSET_on(imp_dbh); /* imp_dbh set up now */
DBIc_ACTIVE_on(imp_dbh); /* call disconnect before freeing */
return 1;
#endif /* WITH_UNICODE */
}
/************************************************************************/
/* */
/* dbd_db_login6 */
/* ============= */
/* */
/* A newer version of the dbd_db_login API with the additional attr as */
/* the sixth argument. Once everyone upgrades to at least */
/* DBI 1.60X (where X > 7) this API won't get called anymore since */
/* dbd_db_login6_sv will be favoured. */
/* */
/* NOTE: I had hoped to make dbd_db_login6_sv support Unicode and */
/* dbd_db_login6 to not support Unicode but as no one (except me) has */
/* a DBI which supports dbd_db_login6_sv and unixODBC REQUIRES us to */
/* call SQLDriverConnectW if we are going to call other SQLXXXW */
/* functions later I've got no choice but to convert the ASCII strings */
/* passed to dbd_db_login6 to wide characters when DBD::ODBC is built */
/* for Unicode. */
/* */
/************************************************************************/
int dbd_db_login6(
SV *dbh,
imp_dbh_t *imp_dbh,
char *dbname,
char *uid,
char *pwd,
SV *attr)
{
D_imp_drh_from_dbh;
RETCODE rc;
char dbname_local[512];
#ifdef WITH_UNICODE
SQLWCHAR wconstr[512];
STRLEN wconstr_len;
unsigned int i;
#endif
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE0(imp_dbh, "dbd_db_login6\n");
if (!imp_drh->connects) {
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &imp_drh->henv);
dbd_error(dbh, rc, "db_login6/SQLAllocHandle(env)");
if (!SQL_SUCCEEDED(rc)) return 0;
if (set_odbc_version(dbh, imp_dbh, attr) != 1) return 0;
}
imp_dbh->henv = imp_drh->henv; /* needed for dbd_error */
/* If odbc_trace_file set, set it in ODBC */
{
SV **attr_sv;
char *file;
if ((attr_sv =
DBD_ATTRIB_GET_SVP(attr, "odbc_trace_file",
(I32)strlen("odbc_trace_file"))) != NULL) {
if (SvPOK(*attr_sv)) {
file = SvPV_nolen(*attr_sv);
rc = SQLSetConnectAttr(NULL, SQL_ATTR_TRACEFILE,
file, strlen(file));
if (!SQL_SUCCEEDED(rc)) {
warn("Failed to set trace file");
}
}
}
}
/* If odbc_trace enabled, turn ODBC tracing on */
{
UV dc = 0;
SV **svp;
DBD_ATTRIB_GET_IV(attr, "odbc_trace", 10, svp, dc);
if (svp && dc) {
rc = SQLSetConnectAttr(NULL, SQL_ATTR_TRACE,
(SQLPOINTER)SQL_OPT_TRACE_ON, 0);
if (!SQL_SUCCEEDED(rc)) {
warn("Failed to enable tracing");
}
}
}
imp_dbh->out_connect_string = NULL;
rc = SQLAllocHandle(SQL_HANDLE_DBC, imp_drh->henv, &imp_dbh->hdbc);
if (!SQL_SUCCEEDED(rc)) {
dbd_error(dbh, rc, "db_login6/SQLAllocHandle(dbc)");
if (imp_drh->connects == 0) {
SQLFreeHandle(SQL_HANDLE_ENV, imp_drh->henv);
imp_drh->henv = SQL_NULL_HENV;
imp_dbh->henv = SQL_NULL_HENV; /* needed for dbd_error */
}
return 0;
}
#ifndef DBD_ODBC_NO_SQLDRIVERCONNECT
/* If the connection string is too long to pass to SQLConnect or it
contains DSN or DRIVER, we've little choice to but to call
SQLDriverConnect and need to tag the uid/pwd on the end of the
connection string (unless they already exist). */
if ((strlen(dbname) > SQL_MAX_DSN_LENGTH ||
dsnHasDriverOrDSN(dbname)) && !dsnHasUIDorPWD(dbname)) {
if ((strlen(dbname) +
(uid ? strlen(uid) : 0) +
(pwd ? strlen(pwd) : 0) +
12) >
sizeof(dbname_local)) {
croak("Connection string too long");
}
strcpy(dbname_local, dbname);
if (uid) {
strcat(dbname_local, ";UID=");
strcat(dbname_local, uid);
}
if (pwd) {
strcat(dbname_local, ";PWD=");
strcat(dbname_local, pwd);
}
dbname = dbname_local;
}
if (DBIc_TRACE(imp_dbh, CONNECTION_TRACING, 0, 0))
TRACE2(imp_dbh, " SQLDriverConnect '%s', '%s', 'xxxx'\n",
dbname, (uid ? uid : ""));
# ifdef WITH_UNICODE
if (strlen(dbname) > (sizeof(wconstr) / sizeof(wconstr[0]))) {
croak("Connection string too big to convert to wide characters");
}
/* The following is a massive simplification assuming only 7-bit ASCII
is ever passed to dbd_db_login6 */
for (i = 0; i < strlen(dbname); i++) {
wconstr[i] = dbname[i];
}
wconstr[i] = 0;
wconstr_len = i;
{
SQLWCHAR wout_str[512];
SQLSMALLINT wout_str_len;