-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdbdimp.c
3087 lines (2514 loc) · 98.4 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
/* vim: set noai ts=4 et sw=4: */
/*
Copyright (c) 2010, 2011 Popa Marius Adrian <mapopa@gmail.com>
Copyright (c) 2011, 2012, 2013, 2024 Damyan Ivanov <dmn@debian.org>
Copyright (c) 2010 Mike Pomraning <mjp@pilcrow.madison.wi.us>
Copyright (c) 1999-2008 Edwin Pratomo
Portions Copyright (c) 2001-2005 Daniel Ritz
You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.
*/
#include "Firebird.h"
#include <stdint.h>
#ifndef _MSC_VER
#include <inttypes.h>
#endif
DBISTATE_DECLARE;
#define ERRBUFSIZE 255
#define IB_SQLtimeformat(xxh, format, sv) \
do { \
STRLEN len; \
char *frmt = NULL; \
char *buf = SvPV(sv, len); \
if (len < 2 || len > 30) break; \
Newx(frmt, len + 1, char); \
strcpy(frmt, buf); \
if (format) Safefree(format); \
format = frmt; \
} while (0)
#define IB_alloc_sqlda(sqlda, n) \
do { \
short len = n; \
char *tmp; \
if (sqlda) \
{ \
Safefree(sqlda); \
sqlda = NULL; \
} \
Newxz(tmp, XSQLDA_LENGTH(len), char); \
sqlda = (XSQLDA*)tmp; \
sqlda->sqln = len; \
sqlda->version = SQLDA_OK_VERSION; \
} while (0)
#ifndef is_ascii_string
#warning "Using built-in implementation of is_ascii_string."
#warning "Upgrading perl to 5.12 is suggested."
// for perl before 5.12.0 RC1
// taken straight from the perl source
bool is_ascii_string(const U8 *s, STRLEN len) {
const U8* const send = s + (len ? len : strlen((const char *)s));
const U8* x = s;
for (; x < send; ++x) {
if (!UTF8_IS_INVARIANT(*x))
break;
}
return x == send;
}
#endif
int create_cursor_name(SV *sth, imp_sth_t *imp_sth)
{
ISC_STATUS status[ISC_STATUS_LENGTH];
#define CURSOR_NAME_LEN 22
Newxz(imp_sth->cursor_name, CURSOR_NAME_LEN, char);
snprintf(imp_sth->cursor_name, CURSOR_NAME_LEN, "perl%16.16X", (uint32_t)imp_sth->stmt);
isc_dsql_set_cursor_name(status, &(imp_sth->stmt), imp_sth->cursor_name, 0);
if (ib_error_check(sth, status))
return FALSE;
return TRUE;
}
void maybe_upgrade_to_utf8(imp_dbh_t *imp_dbh, SV *sv) {
if (imp_dbh->ib_enable_utf8) {
U8 *p;
STRLEN len;
p = (U8*)SvPV(sv, len);
if (!is_ascii_string(p, len)
&& is_utf8_string(p, len)) {
SvUTF8_on(sv);
}
}
}
void dbd_init(dbistate_t *dbistate)
{
DBISTATE_INIT;
}
void ib_cleanup_st_prepare (imp_sth_t *imp_sth)
{
FREE_SETNULL(imp_sth->in_sqlda);
FREE_SETNULL(imp_sth->out_sqlda);
FREE_SETNULL(imp_sth->dateformat);
FREE_SETNULL(imp_sth->timeformat);
FREE_SETNULL(imp_sth->timestampformat);
}
void ib_cleanup_st_execute (imp_sth_t *imp_sth)
{
if (imp_sth->in_sqlda)
{
int i;
XSQLVAR *var = imp_sth->in_sqlda->sqlvar;
for (i = 0; i < imp_sth->in_sqlda->sqln; i++, var++)
{
Safefree(var->sqldata);
var->sqldata = NULL;
if (var->sqlind)
*(var->sqlind) = -1; /* isNULL */
}
}
}
/* lower level error handling */
void do_error(SV *h, int rc, char *what)
{
D_imp_xxh(h);
SV *errstr = DBIc_ERRSTR(imp_xxh);
sv_setiv(DBIc_ERR(imp_xxh), (IV)rc);
sv_setpv(errstr, what);
if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh), "%s error %d recorded: %s\n",
what, rc, SvPV(errstr,PL_na));
}
#define CALC_AVAILABLE(buff) sizeof(buff) - strlen(buff) - 1
/*
decode status vector into a char pointer (implemented by a mortal scalar)
Returns NULL if there is no error
*/
char* ib_error_decode(const ISC_STATUS *status) {
SV *sv = NULL;
long sqlcode;
#if !defined(FB_API_VER) || FB_API_VER < 20
ISC_STATUS *pvector = status;
#else
const ISC_STATUS *pvector = status;
#endif
#if defined (INCLUDE_TYPES_PUB_H)
ISC_SCHAR msg[1024];
#else
char msg[1024];
#endif
if (status[0] != 1 || status[1] <= 0)
return NULL;
if ((sqlcode = isc_sqlcode(status)) != 0)
{
isc_sql_interprete((short) sqlcode, msg, sizeof(msg));
sv = sv_2mortal(newSVpv(msg, 0));
}
#if !defined(FB_API_VER) || FB_API_VER < 20
while (isc_interprete(msg, &pvector))
#else
while (fb_interpret(msg, sizeof(msg), &pvector))
#endif
{
if ( sv != NULL ) {
sv_catpvn(sv, "\n-", 2);
sv_catpv(sv, msg);
}
else sv = sv_2mortal(newSVpv(msg,0));
}
sv_catpvn(sv, "\0", 1); // NUL-terminate
return SvPV_nolen(sv);
}
/* higher level error handling, check and decode status */
int ib_error_check(SV *h, ISC_STATUS *status)
{
char *msg = ib_error_decode(status);
if (msg == NULL)
return SUCCESS;
do_error(h, isc_sqlcode(status), msg);
return FAILURE;
}
static int ib2sql_type(int ibtype)
{
/* Firebird Internal (not external) types */
switch(ibtype & ~1)
{
case SQL_TEXT:
return DBI_SQL_CHAR;
case SQL_LONG:
return DBI_SQL_INTEGER; /* integer */
case SQL_SHORT:
return DBI_SQL_SMALLINT; /* smallint */
case SQL_FLOAT:
return DBI_SQL_FLOAT;
case SQL_DOUBLE:
return DBI_SQL_DOUBLE;
case SQL_TIMESTAMP:
return DBI_SQL_TIMESTAMP;
case SQL_TYPE_DATE:
return DBI_SQL_DATE;
case SQL_TYPE_TIME:
return DBI_SQL_TIME;
case SQL_VARYING:
return DBI_SQL_VARCHAR;
#ifdef SQL_INT64
case SQL_INT64:
return DBI_SQL_BIGINT;
#endif
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
return DBI_SQL_BOOLEAN;
#endif
}
/* else map type into DBI reserved standard range */
return -9000 - ibtype;
}
#if 0
/* from DBI (ANSI/ISO/ODBC) types to Firebird types */
static int ib_sql_type(imp_sth_t *imp_sth, char *name, int sql_type)
{
/* XXX should detect DBI reserved standard type range here */
switch (sql_type) {
case DBI_SQL_NUMERIC:
case DBI_SQL_DECIMAL:
case DBI_SQL_INTEGER:
case DBI_SQL_BIGINT:
case DBI_SQL_TINYINT:
case DBI_SQL_SMALLINT:
case DBI_SQL_FLOAT:
case DBI_SQL_REAL:
case DBI_SQL_DOUBLE:
return 481;
case DBI_SQL_VARCHAR:
return 449;
case DBI_SQL_CHAR:
return 453;
case SQL_DATE:
case SQL_TIME:
case SQL_TIMESTAMP:
default:
if (DBIc_WARN(imp_sth) && imp_sth && name)
warn("SQL type %d for '%s' is not fully supported, bound as SQL_VARCHAR instead");
return ib_sql_type(imp_sth, name, DBI_SQL_VARCHAR);
}
}
#endif
int dbd_db_login6(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *uid,
char *pwd, SV *attr)
{
dTHR;
ISC_STATUS status[ISC_STATUS_LENGTH];
HV *hv;
SV *sv;
SV **svp; /* versatile scalar storage */
unsigned short ib_dialect, ib_cache;
bool ib_db_triggers = true;
char *ib_role;
char ISC_FAR *dpb_buffer, *dpb;
int connect_timeout = 0;
char ISC_FAR *database;
STRLEN len, db_len; /* for SvPV */
char dbkey_scope = 0;
short dpb_length = 0;
unsigned int buflen = 0; /* buffer size is dynamic */
imp_dbh->db = 0L;
imp_dbh->tr = 0L;
imp_dbh->tpb_buffer = NULL;
imp_dbh->tpb_length = 0;
imp_dbh->sth_ddl = 0;
imp_dbh->soft_commit = 0; /* use soft commit (isc_commit_retaining)? */
imp_dbh->ib_enable_utf8 = FALSE;
/* default date/time formats
+ *
* Old API: dateformat ........ %c
* timeformat ........ (none)
* timestampformat ... (none)
*
* v6 API: dateformat ........ %x
* timeformat ........ %X
* timestampformat ... %c
*/
Newxz(imp_dbh->dateformat, 3, char);
strcpy(imp_dbh->dateformat, "%x");
Newxz(imp_dbh->timeformat, 3, char);
strcpy(imp_dbh->timeformat, "%X");
Newxz(imp_dbh->timestampformat, 3, char);
strcpy(imp_dbh->timestampformat, "%c");
/* linked list */
imp_dbh->first_sth = NULL;
imp_dbh->last_sth = NULL;
/* save the current context for thread/multiplicy safety */
#if defined(USE_THREADS) || defined(USE_ITHREADS) || defined(MULTIPLICITY)
imp_dbh->context = PERL_GET_CONTEXT;
#endif
/* Parse DSN and init values */
sv = DBIc_IMP_DATA(imp_dbh);
if (!sv || !SvROK(sv))
return FALSE;
hv = (HV*) SvRV(sv);
if (SvTYPE(hv) != SVt_PVHV)
return FALSE;
if (uid != NULL) {
DPB_PREP_STRING(buflen, uid);
}
if (pwd != NULL) {
DPB_PREP_STRING(buflen, pwd);
}
/* does't go to DPB -> no buflen inc */
if ((svp = hv_fetch(hv, "database", 8, FALSE)))
database = SvPV(*svp, db_len);
else database = NULL;
/* role, cache, charset, sqldialect */
if ((svp = hv_fetch(hv, "ib_dialect", 10, FALSE)))
ib_dialect = (unsigned short) SvIV(*svp);
else
ib_dialect = DEFAULT_SQL_DIALECT;
DPB_PREP_INTEGER(buflen);
if ((svp = hv_fetch(hv, "ib_cache", 8, FALSE)))
{
ib_cache = (unsigned short) SvIV(*svp);
DPB_PREP_INTEGER(buflen);
}
else
ib_cache = 0;
if ((svp = hv_fetch(hv, "ib_charset", 10, FALSE)))
{
char *p = SvPV(*svp, len);
DPB_PREP_STRING_LEN(buflen, len);
Newx(imp_dbh->ib_charset, len+1, char);
strncpy(imp_dbh->ib_charset, p, len);
*(imp_dbh->ib_charset + len) = '\0';
}
else {
imp_dbh->ib_charset = NULL;
}
if ((svp = hv_fetch(hv, "ib_role", 7, FALSE)))
{
ib_role = SvPV(*svp, len);
DPB_PREP_STRING_LEN(buflen, len);
}
else
ib_role = NULL;
if ((svp = hv_fetch(hv, "ib_dbkey_scope", 14, FALSE)))
{
dbkey_scope = (char)SvIV(*svp);
if (dbkey_scope)
DPB_PREP_INTEGER(buflen);
}
if ((svp = hv_fetch(hv, "timeout", 7, FALSE)))
{
int val = SvIV(*svp);
if (val <= 0) croak("Positive timeout required");
connect_timeout = val;
DPB_PREP_INTEGER(buflen);
}
if ((svp = hv_fetch(hv, "ib_db_triggers", 14, FALSE)))
{
ib_db_triggers = SvTRUE(*svp);
if (!ib_db_triggers)
DPB_PREP_BYTE(buflen);
}
else {
if (SvOK(attr)) {
HV *attr_h = (HV*) SvRV(attr);
if (SvTYPE(hv) == SVt_PVHV) {
if (svp = hv_fetch(attr_h, "ib_db_triggers", 14, FALSE)) {
ib_db_triggers = SvTRUE(*svp);
if (!ib_db_triggers)
DPB_PREP_BYTE(buflen);
}
}
}
}
/* add length of other parameters to needed buflen */
buflen += 1; /* dbpversion */
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_login6\n"));
/* Allocate DPB */
Newx(dpb_buffer, buflen, char);
/* Default SQL dialect for every statement */
imp_dbh->sqldialect = ib_dialect;
/* Fill DPB */
dpb = dpb_buffer;
*dpb++ = isc_dpb_version1;
DPB_FILL_STRING(dpb, isc_dpb_user_name, uid);
DPB_FILL_STRING(dpb, isc_dpb_password, pwd);
if (ib_cache)
{
/*
* Safety check: Do not allocate a cache buffer greater than
* 10000 pages, so we don't exhaust memory inadvertently.
*/
if (ib_cache > 10000) ib_cache = 10000;
DPB_FILL_INTEGER(dpb, isc_dpb_num_buffers, ib_cache);
}
DPB_FILL_INTEGER(dpb, isc_dpb_sql_dialect, ib_dialect);
if (dbkey_scope)
{
DPB_FILL_INTEGER(dpb, isc_dpb_dbkey_scope, dbkey_scope);
}
if (imp_dbh->ib_charset)
{
DPB_FILL_STRING(dpb, isc_dpb_lc_ctype, imp_dbh->ib_charset);
}
if (ib_role)
{
DPB_FILL_STRING(dpb, isc_dpb_sql_role_name, ib_role);
}
if (connect_timeout)
{
DPB_FILL_INTEGER(dpb, isc_dpb_connect_timeout, connect_timeout);
}
if (!ib_db_triggers)
{
DPB_FILL_BYTE(dpb, isc_dpb_no_db_triggers, 1);
}
dpb_length = dpb - dpb_buffer;
if ( dpb_length != buflen ) {
fprintf(stderr, "# db_login6: %d != %d\n", dpb_length, buflen);
fflush(stderr);
abort();
}
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_login6: attaching to database %s..\n", database));
isc_attach_database(status, /* status vector */
db_len,
database, /* connect string */
&(imp_dbh->db), /* ref to db handle */
dpb_length, /* length of dpb */
dpb_buffer); /* connect options */
/* freeing database parameter buffer */
Safefree(dpb_buffer);
/* return false on failed attach */
if (ib_error_check(dbh, status))
return FALSE;
imp_dbh->charset_bytes_per_char = NULL;
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_login6: success attaching.\n"));
/* Tell DBI, that dbh->destroy should be called for this handle */
DBIc_IMPSET_on(imp_dbh);
/* Tell DBI, that dbh->disconnect should be called for this handle */
DBIc_ACTIVE_on(imp_dbh);
return TRUE;
}
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);
}
int dbd_db_ping(SV *dbh)
{
D_imp_dbh(dbh);
ISC_STATUS status[ISC_STATUS_LENGTH];
char buffer[100];
char req[] = {
isc_info_ods_version,
isc_info_end
};
DBI_TRACE_imp_xxh(imp_dbh, 1, (DBIc_LOGPIO(imp_dbh), "dbd_db_ping\n"));
if (isc_database_info(status, &(imp_dbh->db), sizeof(req), req, sizeof(buffer), buffer))
if (ib_error_check(dbh, status))
return FALSE;
return TRUE;
}
int dbd_db_disconnect(SV *dbh, imp_dbh_t *imp_dbh)
{
dTHR;
ISC_STATUS status[ISC_STATUS_LENGTH];
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_disconnect\n"));
/* set the database handle to inactive */
DBIc_ACTIVE_off(imp_dbh);
/* always do a rollback if there's an open transaction.
* Firebird requires to close open transactions before
* detaching a database.
*/
if (imp_dbh->tr)
{
/* rollback and close trans context */
isc_rollback_transaction(status, &(imp_dbh->tr));
if (ib_error_check(dbh, status))
return FALSE;
imp_dbh->tr = 0L;
}
FREE_SETNULL(imp_dbh->ib_charset);
FREE_SETNULL(imp_dbh->tpb_buffer);
FREE_SETNULL(imp_dbh->dateformat);
FREE_SETNULL(imp_dbh->timeformat);
FREE_SETNULL(imp_dbh->timestampformat);
FREE_SETNULL(imp_dbh->charset_bytes_per_char);
/* detach database */
isc_detach_database(status, &(imp_dbh->db));
if (ib_error_check(dbh, status))
return FALSE;
return TRUE;
}
void dbd_db_destroy (SV *dbh, imp_dbh_t *imp_dbh)
{
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_destroy\n"));
if (DBIc_ACTIVE(imp_dbh))
dbd_db_disconnect(dbh, imp_dbh);
/* Nothing in imp_dbh to be freed */
DBIc_IMPSET_off(imp_dbh);
}
int dbd_db_commit (SV *dbh, imp_dbh_t *imp_dbh)
{
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_commit\n"));
/* no commit if AutoCommit on */
if (DBIc_has(imp_dbh, DBIcf_AutoCommit))
return FALSE;
/* commit the transaction */
if (!ib_commit_transaction(dbh, imp_dbh))
return FALSE;
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_commit succeed.\n"));
return TRUE;
}
int dbd_db_rollback(SV *dbh, imp_dbh_t *imp_dbh)
{
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_rollback\n"));
/* no rollback if AutoCommit = on */
if (DBIc_has(imp_dbh, DBIcf_AutoCommit) != FALSE)
return FALSE;
/* rollback the transaction */
if (!ib_rollback_transaction(dbh, imp_dbh))
return FALSE;
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_rollback succeed.\n"));
return TRUE;
}
int dbd_db_STORE_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv, SV *valuesv)
{
STRLEN kl;
char *key = SvPV(keysv, kl);
int on = SvTRUE(valuesv)? 1: 0;
int oldval;
int set_frmts = 0;
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_STORE - %s\n", key));
/**************************************************************************/
if ((kl==10) && strEQ(key, "AutoCommit"))
{
oldval = DBIc_has(imp_dbh, DBIcf_AutoCommit)? 1: 0;
DBIc_set(imp_dbh, DBIcf_AutoCommit, on);
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_STORE: switch AutoCommit from %d to %d\n", oldval, on));
if (oldval == FALSE && on)
{
/* AutoCommit set from 0 to 1, commit any outstanding changes */
if (imp_dbh->tr)
{
if (!ib_commit_transaction(dbh, imp_dbh))
return FALSE;
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_STORE: commit open transaction\n"));
}
}
return TRUE; /* handled */
}
/**************************************************************************/
else if ((kl==13) && strEQ(key, "ib_softcommit"))
{
oldval = imp_dbh->soft_commit;
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_STORE: switch ib_softcommit from %d to %d\n", oldval, on));
/* set new value */
imp_dbh->soft_commit = on;
/* switching softcommit from 1 to 0 -> make a hard commit */
if (!on && oldval)
{
if (imp_dbh->tr)
{
if (!ib_commit_transaction(dbh, imp_dbh))
return FALSE;
DBI_TRACE_imp_xxh(imp_dbh, 3, (DBIc_LOGPIO(imp_dbh), "dbd_db_STORE: commit open transaction\n"));
}
}
return TRUE; /* handled */
}
else if ((kl==14) && strEQ(key, "ib_enable_utf8")) {
if (on) {
if (imp_dbh->ib_charset && strEQ(imp_dbh->ib_charset, "UTF8")) {
imp_dbh->ib_enable_utf8 = TRUE;
return TRUE;
}
else croak( "ib_enable_utf8 requires ib_charset=UTF8 in DSN (you gave %s)", imp_dbh->ib_charset ? imp_dbh->ib_charset : "<nothing>" );
}
else {
imp_dbh->ib_enable_utf8 = FALSE;
return TRUE;
}
}
else if ((kl==11) && strEQ(key, "ib_time_all"))
set_frmts = 1;
/**************************************************************************/
if (set_frmts || ((kl==13) && strEQ(key, "ib_dateformat")))
{
IB_SQLtimeformat(dbh, imp_dbh->dateformat, valuesv);
if (!set_frmts) return TRUE;
}
if (set_frmts || ((kl==13) && strEQ(key, "ib_timeformat")))
{
IB_SQLtimeformat(dbh, imp_dbh->timeformat, valuesv);
if (!set_frmts) return TRUE;
}
if (set_frmts || ((kl==18) && strEQ(key, "ib_timestampformat")))
{
IB_SQLtimeformat(dbh, imp_dbh->timestampformat, valuesv);
if (!set_frmts) return TRUE;
}
/**************************************************************************/
if (set_frmts) return TRUE;
else return FALSE; /* not handled */
}
SV *dbd_db_FETCH_attrib(SV *dbh, imp_dbh_t *imp_dbh, SV *keysv)
{
STRLEN kl;
char * key = SvPV(keysv, kl);
SV * result = NULL;
DBI_TRACE_imp_xxh(imp_dbh, 2, (DBIc_LOGPIO(imp_dbh), "dbd_db_FETCH - %s\n", key));
if ((kl==10) && strEQ(key, "AutoCommit"))
result = boolSV(DBIc_has(imp_dbh, DBIcf_AutoCommit));
else if ((kl==13) && strEQ(key, "ib_softcommit"))
result = boolSV(imp_dbh->soft_commit);
else if ((kl==14) && strEQ(key, "ib_enable_utf8"))
result = boolSV(imp_dbh->ib_enable_utf8);
else if ((kl==13) && strEQ(key, "ib_dateformat"))
result = newSVpvn(imp_dbh->dateformat, strlen(imp_dbh->dateformat));
else if ((kl==13) && strEQ(key, "ib_timeformat"))
result = newSVpvn(imp_dbh->timeformat, strlen(imp_dbh->timeformat));
else if ((kl==18) && strEQ(key, "ib_timestampformat"))
result = newSVpvn(imp_dbh->timestampformat,
strlen(imp_dbh->timestampformat));
else if ((kl==11) && strEQ(key, "ib_embedded"))
#ifdef EMBEDDED
result = &PL_sv_yes;
#else
result = &PL_sv_no;
#endif
if (result == NULL)
return Nullsv;
else
{
if ((result == &PL_sv_yes) || (result == &PL_sv_no))
return result;
else
return sv_2mortal(result);
}
}
void dbd_preparse(SV *sth, imp_sth_t *imp_sth, char *statement)
{
ISC_STATUS status[ISC_STATUS_LENGTH];
DBI_TRACE_imp_xxh(imp_sth, 2, (DBIc_LOGPIO(imp_sth), "Enter dbd_preparse\n"));
isc_dsql_describe_bind(status, &(imp_sth->stmt), 1, imp_sth->in_sqlda);
if (ib_error_check(sth, status))
{
ib_cleanup_st_prepare(imp_sth);
return;
}
/* realloc in_sqlda and rebind if not enough XSQLVAR for bind params */
if (imp_sth->in_sqlda->sqld > imp_sth->in_sqlda->sqln)
{
IB_alloc_sqlda(imp_sth->in_sqlda, imp_sth->in_sqlda->sqld);
if (imp_sth->in_sqlda == NULL)
{
do_error(sth, 1, "Fail to reallocate in_slqda");
ib_cleanup_st_prepare(imp_sth);
return;
}
else
{
isc_dsql_describe_bind(status, &(imp_sth->stmt), 1, imp_sth->in_sqlda);
if (ib_error_check(sth, status))
{
ib_cleanup_st_prepare(imp_sth);
return;
}
}
}
DBI_TRACE_imp_xxh(imp_sth, 3, (DBIc_LOGPIO(imp_sth), "dbd_preparse: describe_bind passed.\n"
"dbd_preparse: exit; in_sqlda: sqld: %d, sqln: %d.\n",
imp_sth->in_sqlda->sqld, imp_sth->in_sqlda->sqln));
DBIc_NUM_PARAMS(imp_sth) = imp_sth->in_sqlda->sqld;
}
int dbd_st_prepare(SV *sth, imp_sth_t *imp_sth, char *statement, SV *attribs)
{
D_imp_dbh_from_sth;
ISC_STATUS status[ISC_STATUS_LENGTH];
int i;
short dtype;
static char stmt_info[1];
char info_buffer[20], count_item;
XSQLVAR *var;
DBI_TRACE_imp_xxh(imp_sth, 2, (DBIc_LOGPIO(imp_sth), "Enter dbd_st_prepare\n"));
if (!DBIc_ACTIVE(imp_dbh))
{
do_error(sth, -1, "Database disconnected");
return FALSE;
}
/* init values */
count_item = 0;
imp_sth->count_item = 0;
imp_sth->affected = -1;
imp_sth->in_sqlda = NULL;
imp_sth->out_sqlda = NULL;
imp_sth->cursor_name = NULL;
imp_sth->dateformat = NULL;
imp_sth->timestampformat = NULL;
imp_sth->timeformat = NULL;
/* double linked list */
imp_sth->prev_sth = NULL;
imp_sth->next_sth = NULL;
if (attribs)
{
SV **svp;
if ((svp = DBD_ATTRIB_GET_SVP(attribs, "ib_time_all", 11)) != NULL)
{
IB_SQLtimeformat(sth, imp_sth->dateformat, *svp);
IB_SQLtimeformat(sth, imp_sth->timestampformat, *svp);
IB_SQLtimeformat(sth, imp_sth->timeformat, *svp);
}
if ((svp = DBD_ATTRIB_GET_SVP(attribs, "ib_dateformat", 13)) != NULL)
IB_SQLtimeformat(sth, imp_sth->dateformat, *svp);
if ((svp = DBD_ATTRIB_GET_SVP(attribs, "ib_timestampformat", 18)) != NULL)
IB_SQLtimeformat(sth, imp_sth->timestampformat, *svp);
if ((svp = DBD_ATTRIB_GET_SVP(attribs, "ib_timeformat", 13)) != NULL)
IB_SQLtimeformat(sth, imp_sth->timeformat, *svp);
}
/* allocate 1 XSQLVAR to in_sqlda */
IB_alloc_sqlda(imp_sth->in_sqlda, 1);
if (imp_sth->in_sqlda == NULL)
{
do_error(sth, 2, "Fail to allocate in_sqlda");
return FALSE;
}
/* allocate 1 XSQLVAR to out_sqlda */
IB_alloc_sqlda(imp_sth->out_sqlda, 1);
if (imp_sth->out_sqlda == NULL)
{
do_error(sth, 2, "Fail to allocate out_sqlda");
ib_cleanup_st_prepare(imp_sth);
return FALSE;
}
/* init statement handle */
isc_dsql_alloc_statement2(status, &(imp_dbh->db), &(imp_sth->stmt));
if (ib_error_check(sth, status))
{
ib_cleanup_st_prepare(imp_sth);
return FALSE;
}
DBI_TRACE_imp_xxh(imp_sth, 3, (DBIc_LOGPIO(imp_sth), "dbd_st_prepare: sqldialect: %d.\n", imp_dbh->sqldialect));
if (!imp_dbh->tr)
{
/* start a new transaction using current TPB */
if (!ib_start_transaction(sth, imp_dbh))
{
ib_cleanup_st_prepare(imp_sth);
return FALSE;
}
}
DBI_TRACE_imp_xxh(imp_sth, 3, (DBIc_LOGPIO(imp_sth), "dbd_st_prepare: statement: %s.\n", statement));
isc_dsql_prepare(status, &(imp_dbh->tr), &(imp_sth->stmt), 0, statement,
imp_dbh->sqldialect, imp_sth->out_sqlda);
if (ib_error_check(sth, status))
{
ib_cleanup_st_prepare(imp_sth);
return FALSE;
}
DBI_TRACE_imp_xxh(imp_sth, 3, (DBIc_LOGPIO(imp_sth), "dbd_st_prepare: isc_dsql_prepare succeed..\n"));
stmt_info[0] = isc_info_sql_stmt_type;
isc_dsql_sql_info(status, &(imp_sth->stmt), sizeof (stmt_info), stmt_info,
sizeof (info_buffer), info_buffer);
if (ib_error_check(sth, status))
{
ib_cleanup_st_prepare(imp_sth);
return FALSE;
}
{
short l = (short) isc_vax_integer((char *) info_buffer + 1, 2);
imp_sth->type = isc_vax_integer((char *) info_buffer + 3, l);
}
/* sanity check of statement type */
DBI_TRACE_imp_xxh(imp_sth, 3, (DBIc_LOGPIO(imp_sth), "dbd_st_prepare: statement type: %ld.\n", imp_sth->type));
switch (imp_sth->type)
{
/* Implemented statement types. */
case isc_info_sql_stmt_select:
case isc_info_sql_stmt_select_for_upd:
/*
* Unfortunately, select count item doesn't work
* in current versions of Firebird.
* isql does it literally by fetching everything
* and counting the number of rows it fetched.
* Firebird doesn't seem to be able to estimate
* the number of rows before the client app
* fetches them all.
*/
//count_item = isc_info_req_select_count;
break;
case isc_info_sql_stmt_insert:
count_item = isc_info_req_insert_count;
break;
case isc_info_sql_stmt_update:
count_item = isc_info_req_update_count;
break;
case isc_info_sql_stmt_delete:
count_item = isc_info_req_delete_count;
break;
case isc_info_sql_stmt_ddl:
case isc_info_sql_stmt_set_generator:
case isc_info_sql_stmt_exec_procedure:
case isc_info_sql_stmt_savepoint:
case isc_info_sql_stmt_start_trans:
case isc_info_sql_stmt_commit:
case isc_info_sql_stmt_rollback:
/* no count_item to gather */
break;
/*
* Unimplemented statement types. Some may be implemented in the future.
*/
case isc_info_sql_stmt_get_segment:
case isc_info_sql_stmt_put_segment:
default:
do_error(sth, 10, "Statement type is not implemented in this version of DBD::Firebird");
return FALSE;