forked from rowland/fb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb.c
3213 lines (2861 loc) · 102 KB
/
fb.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
/*
* fb.c
* A module to access the Firebird database from Ruby.
* Another fork of fb.c by John W Higgins
* All changes, improvements and associated bugs Copyright (C) 2008 John W Higgins<[email protected]>
* License to all changes, improvements and bugs is granted under the same terms as the original and/or
* the Ruby license, whichever is most applicable.
* fb.c
* Fork of interbase.c to fb.c by Brent Rowland.
* All changes, improvements and associated bugs Copyright (C) 2006 Brent Rowland and Target Training International.
* License to all changes, improvements and bugs is granted under the same terms as the original and/or
* the Ruby license, whichever is most applicable.
* Based on interbase.c
*
* Copyright (C) 1999 by NaCl inc.
* Copyright (C) 1997,1998 by RIOS Corporation
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies.
* RIOS Corporation makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty. By use of this software the user agrees to
* indemnify and hold harmless RIOS Corporation from any claims or
* liability for loss arising out of such use.
*
*
* Some concepts/code taken from KInterbasDB under the following license
*
* KInterbasDB Python Package
*
* Version 3.2
*
* The following contributors hold Copyright (C) over their respective
* portions of code (see KInterbasDB_license.txt for details):
*
* [Original Author (maintained through version 2.0-0.3.1):]
* 1998-2001 [alex] Alexander Kuznetsov <[email protected]>
* [Maintainers (after version 2.0-0.3.1):]
* 2001-2002 [maz] Marek Isalski <[email protected]>
* 2002-2006 [dsr] David Rushby <[email protected]>
* [Contributors:]
* 2001 [eac] Evgeny A. Cherkashin <[email protected]>
* 2001-2002 [janez] Janez Jere <[email protected]>
* KInterbasDB_license.txt has more details
*
* isdst fix for daylight savings times taken from php interbase module
* under the followling license
*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2008 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Jouni Ahto <[email protected]> |
| Andrew Avdeev <[email protected]> |
| Ard Biesheuvel <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "ruby.h"
#ifdef HAVE_RUBY_RE_H
# include <ruby/re.h>
#else
# include <re.h>
#endif
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <ibase.h>
#include <float.h>
#include <time.h>
#include "keywords.h"
#define SQLDA_COLSINIT 50
#define SQLCODE_NOMORE 100
#define TPBBUFF_ALLOC 64
#define CMND_DELIMIT " \t\n\r\f"
#define LIST_DELIMIT ", \t\n\r\f"
#define META_NAME_MAX 31
/* Statement type */
#define STATEMENT_DDL 1
#define STATEMENT_DML 0
/* Execute process flag */
#define EXECF_EXECDML 0
#define EXECF_SETPARM 1
#if (SIZEOF_VOIDP > 4 && defined(FB_API_VER) && FB_API_VER >= 20)
/* 64-bit platform with FB2.0+: */
#define NULL_FB_API_HANDLE 0
#else
/*64-bit platform with FB 1.5, and all 32-bit platforms: */
#define NULL_FB_API_HANDLE NULL
#endif
static VALUE rb_mFb;
static VALUE rb_cFbDatabase;
static VALUE rb_cFbConnection;
static VALUE rb_cFbCursor;
static VALUE rb_cFbSqlType;
static VALUE rb_cFbGlobal;
static VALUE rb_eFbError;
static VALUE rb_sFbField;
static VALUE rb_sFbIndex;
static VALUE rb_cDate;
static ID id_matches;
static ID id_downcase_bang;
static VALUE re_lowercase;
static ID id_rstrip_bang;
/* static char isc_info_stmt[] = { isc_info_sql_stmt_type }; */
/* static char isc_info_buff[16]; */
static char isc_tpb_0[] = {
isc_tpb_version1, isc_tpb_write,
isc_tpb_concurrency, isc_tpb_nowait
};
/* structs */
/* DB handle and TR parameter block list structure */
typedef struct
{
isc_db_handle *dbb_ptr ;
long tpb_len ;
char *tpb_ptr ;
} ISC_TEB ; /* transaction existence block */
/* InterBase varchar structure */
typedef struct
{
short vary_length;
char vary_string[1];
} VARY;
struct FbConnection {
isc_db_handle db; /* DB handle */
isc_tr_handle transact; /* transaction handle */
VALUE cursor;
unsigned short dialect;
unsigned short db_dialect;
short downcase_names;
int dropped;
ISC_STATUS isc_status[20];
/* struct FbConnection *next; */
};
/* static struct FbConnection *fb_connection_list; */
struct FbCursor {
int open;
int eof;
int soft_eof;
isc_tr_handle auto_transact;
isc_stmt_handle stmt;
XSQLDA *i_sqlda;
XSQLDA *o_sqlda;
char *i_buffer;
long i_buffer_size;
char *o_buffer;
long o_buffer_size;
VALUE fields_ary;
VALUE fields_hash;
VALUE connection;
long stmt_type;
};
typedef struct trans_opts
{
char *option1;
char *option2;
char optval;
short position;
struct trans_opts *sub_opts;
} trans_opts;
/* global data */
/* static isc_tr_handle global_transact = 0; */ /* transaction handle */
/* static int connection_count = 0; */
/* global utilities */
#define ALIGN(n, b) ((n + b - 1) & ~(b - 1))
#define FB_ALIGN(n, b) ((n + b - 1) & ~(b - 1))
/* #define FB_ALIGN(n,b) ((n+1) & ~1) */
#define UPPER(c) (((c) >= 'a' && (c)<= 'z') ? (c) - 'a' + 'A' : (c))
#define FREE(p) if (p) { xfree(p); p = 0; }
#define SETNULL(p) if (p && strlen(p) == 0) { p = 0; }
//#define HERE_NUM(i) printf("%i\n", i)
//#define HERE(s) printf("%s\n", s)
#define HERE(s)
static long calculate_buffsize(XSQLDA *sqlda)
{
XSQLVAR *var;
long cols;
short dtp;
long offset = 0;
long alignment;
long length;
long count;
cols = sqlda->sqld;
var = sqlda->sqlvar;
for (count = 0; count < cols; var++,count++) {
length = alignment = var->sqllen;
dtp = var->sqltype & ~1;
if (dtp == SQL_TEXT) {
alignment = 1;
} else if (dtp == SQL_VARYING) {
length += sizeof(short);
alignment = sizeof(short);
}
offset = FB_ALIGN(offset, alignment);
offset += length;
offset = FB_ALIGN(offset, sizeof(short));
offset += sizeof(short);
}
return offset + sizeof(short);
}
#if (FB_API_VER >= 20)
static VALUE fb_error_msg(const ISC_STATUS *isc_status)
{
char msg[1024];
VALUE result = rb_str_new(NULL, 0);
while (fb_interpret(msg, 1024, &isc_status))
{
result = rb_str_cat(result, msg, strlen(msg));
result = rb_str_cat(result, "\n", strlen("\n"));
}
return result;
}
#else
static VALUE fb_error_msg(ISC_STATUS *isc_status)
{
char msg[1024];
VALUE result = rb_str_new(NULL, 0);
while (isc_interprete(msg, &isc_status))
{
result = rb_str_cat(result, msg, strlen(msg));
result = rb_str_cat(result, "\n", strlen("\n"));
}
return result;
}
#endif
struct time_object {
struct timeval tv;
struct tm tm;
int gmt;
int tm_got;
};
#define GetTimeval(obj, tobj) \
Data_Get_Struct(obj, struct time_object, tobj)
static VALUE fb_mktime(struct tm *tm, int milli)
{
return rb_funcall(
rb_cTime, rb_intern("utc"), 7,
INT2FIX(tm->tm_year), INT2FIX(tm->tm_mon), INT2FIX(tm->tm_mday),
INT2FIX(tm->tm_hour), INT2FIX(tm->tm_min), INT2FIX(tm->tm_sec),
INT2FIX(milli));
}
static VALUE fb_mkdate(struct tm *tm)
{
return rb_funcall(
rb_cDate, rb_intern("civil"), 3,
INT2FIX(1900 + tm->tm_year), INT2FIX(tm->tm_mon + 1), INT2FIX(tm->tm_mday));
}
static int responds_like_date(VALUE obj)
{
return rb_respond_to(obj, rb_intern("year")) &&
rb_respond_to(obj, rb_intern("month")) &&
rb_respond_to(obj, rb_intern("day"));
}
static void tm_from_date(struct tm *tm, VALUE date)
{
VALUE year, month, day;
if (!responds_like_date(date)) {
VALUE s = rb_funcall(date, rb_intern("to_s"), 0);
date = rb_funcall(rb_cDate, rb_intern("parse"), 1, s);
}
year = rb_funcall(date, rb_intern("year"), 0);
month = rb_funcall(date, rb_intern("month"), 0);
day = rb_funcall(date, rb_intern("day"), 0);
memset(tm, 0, sizeof(struct tm));
tm->tm_year = FIX2INT(year) - 1900;
tm->tm_mon = FIX2INT(month) - 1;
tm->tm_mday = FIX2INT(day);
}
static void tm_from_timestamp(struct tm *tm, int *milli, VALUE obj)
{
struct time_object *tobj;
if (!rb_obj_is_kind_of(obj, rb_cTime) && rb_respond_to(obj, rb_intern("to_str")))
{
VALUE s = rb_funcall(obj, rb_intern("to_str"), 0);
obj = rb_funcall(rb_cTime, rb_intern("parse"), 1, s);
}
GetTimeval(obj, tobj);
*milli = tobj->tv.tv_usec;
if (tobj->tm_got == 1)
*tm = tobj->tm;
else
localtime_r(&tobj->tv.tv_sec, tm);
}
static VALUE long_from_obj(VALUE obj)
{
ID id_to_str = rb_intern("to_str");
if (TYPE(obj) != T_FIXNUM && rb_respond_to(obj, id_to_str))
{
VALUE s = rb_funcall(obj, id_to_str, 0);
obj = rb_funcall(s, rb_intern("to_i"), 0);
}
return obj;
}
static VALUE ll_from_obj(VALUE obj)
{
if (TYPE(obj) == T_STRING)
{
obj = rb_funcall(obj, rb_intern("to_i"), 0);
}
return obj;
}
static VALUE double_from_obj(VALUE obj)
{
if (TYPE(obj) == T_STRING)
{
obj = rb_funcall(obj, rb_intern("to_f"), 0);
}
return obj;
}
static VALUE fb_sql_type_from_code(int code, int subtype)
{
char *sql_type = NULL;
switch(code) {
case SQL_TEXT:
case blr_text:
sql_type = "CHAR";
break;
case SQL_VARYING:
case blr_varying:
sql_type = "VARCHAR";
break;
case SQL_SHORT:
case blr_short:
switch (subtype) {
case 0: sql_type = "SMALLINT"; break;
case 1: sql_type = "NUMERIC"; break;
case 2: sql_type = "DECIMAL"; break;
}
break;
case SQL_LONG:
case blr_long:
switch (subtype) {
case 0: sql_type = "INTEGER"; break;
case 1: sql_type = "NUMERIC"; break;
case 2: sql_type = "DECIMAL"; break;
}
break;
case SQL_FLOAT:
case blr_float:
sql_type = "FLOAT";
break;
case SQL_DOUBLE:
case blr_double:
switch (subtype) {
case 0: sql_type = "DOUBLE PRECISION"; break;
case 1: sql_type = "NUMERIC"; break;
case 2: sql_type = "DECIMAL"; break;
}
break;
case SQL_D_FLOAT:
case blr_d_float:
sql_type = "DOUBLE PRECISION";
break;
case SQL_TIMESTAMP:
case blr_timestamp:
sql_type = "TIMESTAMP";
break;
case SQL_BLOB:
case blr_blob:
sql_type = "BLOB";
break;
case SQL_ARRAY:
sql_type = "ARRAY";
break;
case SQL_QUAD:
case blr_quad:
sql_type = "DECIMAL";
break;
case SQL_TYPE_TIME:
case blr_sql_time:
sql_type = "TIME";
break;
case SQL_TYPE_DATE:
case blr_sql_date:
sql_type = "DATE";
break;
case SQL_INT64:
case blr_int64:
switch (subtype) {
case 0: sql_type = "BIGINT"; break;
case 1: sql_type = "NUMERIC"; break;
case 2: sql_type = "DECIMAL"; break;
}
break;
default:
printf("Unknown: %d, %d\n", code, subtype);
sql_type = "UNKNOWN";
break;
}
return rb_str_new2(sql_type);
}
static void fb_error_check(long *isc_status)
{
HERE("fb_error_check");
if (isc_status[0] == 1 && isc_status[1]) {
char buf[1024];
VALUE exc, msg, msg1, msg2;
short code = isc_sqlcode(isc_status);
isc_sql_interprete(code, buf, 1024);
msg1 = rb_str_new2(buf);
msg2 = fb_error_msg(isc_status);
msg = rb_str_cat(msg1, "\n", strlen("\n"));
msg = rb_str_concat(msg, msg2);
exc = rb_exc_new3(rb_eFbError, msg);
rb_iv_set(exc, "error_code", INT2FIX(code));
HERE("fb_error_check 1");
rb_exc_raise(exc);
}
HERE("fb_error_check 2");
}
static void fb_error_check_warn(long *isc_status)
{
short code = isc_sqlcode(isc_status);
if (code != 0) {
char buf[1024];
isc_sql_interprete(code, buf, 1024);
rb_warning("%s(%d)", buf, code);
}
}
static XSQLDA* sqlda_alloc(long cols)
{
XSQLDA *sqlda;
sqlda = (XSQLDA*)xmalloc(XSQLDA_LENGTH(cols));
#ifdef SQLDA_CURRENT_VERSION
sqlda->version = SQLDA_CURRENT_VERSION;
#else
sqlda->version = SQLDA_VERSION1;
#endif
sqlda->sqln = cols;
sqlda->sqld = 0;
return sqlda;
}
static VALUE cursor_close _((VALUE));
static VALUE cursor_drop _((VALUE));
static VALUE cursor_execute _((int, VALUE*, VALUE));
static VALUE cursor_fetchall _((int, VALUE*, VALUE));
static void fb_cursor_mark();
static void fb_cursor_free();
/* connection utilities */
static void fb_connection_check(struct FbConnection *fb_connection)
{
HERE("fb_connection_check");
if (fb_connection->db == 0) {
rb_raise(rb_eFbError, "closed db connection");
}
}
/*
static void global_close_cursors()
{
struct FbConnection *list = fb_connection_list;
int i;
while (list) {
for (i = 0; i < RARRAY(list->cursor)->len; i++) {
cursor_close(RARRAY(list->cursor)->ptr[i]);
}
list = list->next;
}
}
*/
static void fb_connection_close_cursors(struct FbConnection *fb_connection)
{
int i;
for (i = 0; i < RARRAY_LEN(fb_connection->cursor); i++) {
cursor_close(RARRAY_PTR(fb_connection->cursor)[i]);
}
}
static void fb_connection_drop_cursors(struct FbConnection *fb_connection)
{
int i;
for (i = 0; i < RARRAY_LEN(fb_connection->cursor); i++) {
cursor_drop(RARRAY_PTR(fb_connection->cursor)[i]);
}
// RARRAY_LEN(fb_connection->cursor) = 0;
}
/*
static void fb_connection_remove(struct FbConnection *fb_connection)
{
if (fb_connection_list != NULL) {
if (fb_connection_list == fb_connection) {
fb_connection_list = fb_connection_list->next;
} else {
struct FbConnection *list = fb_connection_list;
while (list->next) {
if (list->next == fb_connection) {
list->next = fb_connection->next;
break;
}
list = list->next;
}
}
fb_connection->db = 0;
connection_count--;
}
}
*/
static void fb_connection_disconnect(struct FbConnection *fb_connection)
{
if (fb_connection->transact) {
isc_commit_transaction(fb_connection->isc_status, &fb_connection->transact);
fb_error_check(fb_connection->isc_status);
}
if (fb_connection->dropped) {
isc_drop_database(fb_connection->isc_status, &fb_connection->db);
} else {
isc_detach_database(fb_connection->isc_status, &fb_connection->db);
}
fb_error_check(fb_connection->isc_status);
/* fb_connection_remove(fb_connection); */
}
static void fb_connection_disconnect_warn(struct FbConnection *fb_connection)
{
if (fb_connection->transact) {
isc_commit_transaction(fb_connection->isc_status, &fb_connection->transact);
fb_error_check_warn(fb_connection->isc_status);
}
isc_detach_database(fb_connection->isc_status, &fb_connection->db);
fb_error_check_warn(fb_connection->isc_status);
/* fb_connection_remove(fb_connection); */
}
static void fb_connection_mark(struct FbConnection *fb_connection)
{
rb_gc_mark(fb_connection->cursor);
}
static void fb_connection_free(struct FbConnection *fb_connection)
{
if (fb_connection->db) {
fb_connection_disconnect_warn(fb_connection);
}
xfree(fb_connection);
}
/*
static struct FbConnection* fb_connection_check_retrieve(VALUE data)
{
if (TYPE(data) != T_DATA || RDATA(data)->dfree != (void *)fb_connection_free) {
rb_raise(rb_eTypeError,
"Wrong argument type %s (expected Fb::Connection)",
rb_class2name(CLASS_OF(data)));
}
return (struct FbConnection*)RDATA(data)->data;
}
*/
static unsigned short fb_connection_db_SQL_Dialect(struct FbConnection *fb_connection)
{
long dialect;
long length;
char db_info_command = isc_info_db_SQL_dialect;
char isc_info_buff[16];
/* Get the db SQL Dialect */
isc_database_info(fb_connection->isc_status, &fb_connection->db,
1, &db_info_command,
sizeof(isc_info_buff), isc_info_buff);
fb_error_check(fb_connection->isc_status);
if (isc_info_buff[0] == isc_info_db_SQL_dialect) {
length = isc_vax_integer(&isc_info_buff[1], 2);
dialect = isc_vax_integer(&isc_info_buff[3], (short)length);
} else {
dialect = 1;
}
return dialect;
}
static unsigned short fb_connection_dialect(struct FbConnection *fb_connection)
{
return fb_connection->dialect;
}
/*
static unsigned short fb_connection_db_dialect(struct FbConnection *fb_connection)
{
return fb_connection->db_dialect;
}
*/
/* Transaction option list */
static trans_opts rcom_opt_S[] =
{
{"NO" , "RECORD_VERSION", isc_tpb_no_rec_version, -1, 0},
{"RECORD_VERSION", 0 , isc_tpb_rec_version , -1, 0},
{"*" , 0 , isc_tpb_no_rec_version, -1, 0},
{0 , 0 , 0 , 0, 0}
};
static trans_opts read_opt_S[] =
{
{"WRITE" , 0, isc_tpb_write , 1, 0},
{"ONLY" , 0, isc_tpb_read , 1, 0},
{"COMMITTED", 0, isc_tpb_read_committed, 2, rcom_opt_S},
{0 , 0, 0 , 0, 0}
};
static trans_opts snap_opt_S[] =
{
{"TABLE", "STABILITY", isc_tpb_consistency, 2, 0},
{"*" , 0 , isc_tpb_concurrency, 2, 0},
{0 , 0 , 0 , 0, 0}
};
static trans_opts isol_opt_S[] =
{
{"SNAPSHOT", 0 , 0 , 0, snap_opt_S},
{"READ" , "COMMITTED", isc_tpb_read_committed, 2, rcom_opt_S},
{0 , 0 , 0 , 0, 0}
};
static trans_opts trans_opt_S[] =
{
{"READ" , 0 , 0 , 0, read_opt_S},
{"WAIT" , 0 , isc_tpb_wait , 3, 0},
{"NO" , "WAIT" , isc_tpb_nowait, 3, 0},
{"ISOLATION", "LEVEL", 0 , 0, isol_opt_S},
{"SNAPSHOT" , 0 , 0 , 0, snap_opt_S},
{"RESERVING", 0 , -1 , 0, 0},
{0 , 0 , 0 , 0, 0}
};
/* Name1 Name2 Option value Position Sub-option */
#define RESV_TABLEEND "FOR"
#define RESV_SHARED "SHARED"
#define RESV_PROTECTD "PROTECTED"
#define RESV_READ "READ"
#define RESV_WRITE "WRITE"
#define RESV_CONTINUE ','
static char* trans_parseopts(VALUE opt, int *tpb_len)
{
char *s, *trans;
long used;
long size;
char *tpb;
trans_opts *curr_p;
trans_opts *target_p;
char *check1_p;
char *check2_p;
int count;
int next_c;
char check_f[4];
char *resv_p;
char *resend_p;
char *tblend_p = 0;
int tbl_len;
int res_first;
int res_count;
int ofs;
char sp_prm;
char rw_prm;
int cont_f;
char *desc = 0;
/* Initialize */
s = STR2CSTR(opt);
trans = ALLOCA_N(char, strlen(s)+1);
strcpy(trans, s);
s = trans;
while (*s) {
*s = UPPER(*s);
s++;
}
used = 0;
size = 0;
tpb = NULL;
memset((void *)check_f, 0, sizeof(check_f));
/* Set the default transaction option */
tpb = (char*)xmalloc(TPBBUFF_ALLOC);
size = TPBBUFF_ALLOC;
memcpy((void*)tpb, (void*)isc_tpb_0, sizeof(isc_tpb_0));
used = sizeof(isc_tpb_0);
/* Analize the transaction option strings */
curr_p = trans_opt_S;
check1_p = strtok(trans, CMND_DELIMIT);
if (check1_p) {
check2_p = strtok(0, CMND_DELIMIT);
} else {
check2_p = 0;
}
while (curr_p) {
target_p = 0;
next_c = 0;
for (count = 0; curr_p[count].option1; count++) {
if (!strcmp(curr_p[count].option1, "*")) {
target_p = &curr_p[count];
break;
} else if (check1_p && !strcmp(check1_p, curr_p[count].option1)) {
if (!curr_p[count].option2) {
next_c = 1;
target_p = &curr_p[count];
break;
} else if (check2_p && !strcmp(check2_p, curr_p[count].option2)) {
next_c = 2;
target_p = &curr_p[count];
break;
}
}
}
if (!target_p) {
desc = "Illegal transaction option was specified";
goto error;
}
/* Set the transaction option */
if (target_p->optval > '\0') {
if (target_p->position > 0) {
if (check_f[target_p->position]) {
desc = "Duplicate transaction option was specified";
goto error;
}
tpb[target_p->position] = target_p->optval;
check_f[target_p->position] = 1;
} else {
if (used + 1 > size) {
tpb = (char *)realloc(tpb, size + TPBBUFF_ALLOC);
size += TPBBUFF_ALLOC;
}
tpb[used] = target_p->optval;
used++;
}
} else if (target_p->optval) { /* RESERVING ... FOR */
if (check_f[0]) {
desc = "Duplicate transaction option was specified";
goto error;
}
resv_p = check2_p;
if (!resv_p || !strcmp(resv_p, RESV_TABLEEND)) {
desc = "RESERVING needs table name list";
goto error;
}
while (resv_p) {
res_first = used;
res_count = 0;
resend_p = strtok(0, CMND_DELIMIT);
while (resend_p) {
if (!strcmp(resend_p, RESV_TABLEEND)) {
break;
}
resend_p = strtok(0, CMND_DELIMIT);
}
if (!resend_p) {
desc = "Illegal transaction option was specified";
goto error;
}
while (resv_p < resend_p) {
if (*resv_p == '\0' || (ofs = strspn(resv_p, LIST_DELIMIT)) < 0) {
resv_p++;
} else {
resv_p = &resv_p[ofs];
tblend_p = strpbrk(resv_p, LIST_DELIMIT);
if (tblend_p) {
tbl_len = tblend_p - resv_p;
} else {
tbl_len = strlen(resv_p);
}
if (tbl_len > META_NAME_MAX) {
desc = "Illegal table name was specified";
goto error;
}
if (tbl_len > 0) {
if (used + tbl_len + 3 > size) {
tpb = (char*)xrealloc(tpb, size+TPBBUFF_ALLOC);
size += TPBBUFF_ALLOC;
}
tpb[used+1] = (char)tbl_len;
memcpy((void *)&tpb[used+2],resv_p, tbl_len);
used += tbl_len + 3;
res_count++;
}
resv_p += tbl_len;
}
}
resv_p = strtok(0, CMND_DELIMIT);
if (resv_p && !strcmp(resv_p, RESV_SHARED)) {
sp_prm = isc_tpb_shared;
} else if (resv_p && !strcmp(resv_p, RESV_PROTECTD)) {
sp_prm = isc_tpb_protected;
} else {
desc = "RESERVING needs {SHARED|PROTECTED} {READ|WRITE}";
goto error;
}
cont_f = 0;
resv_p = strtok(0, CMND_DELIMIT);
if (resv_p) {
if (resv_p[strlen(resv_p)-1] == RESV_CONTINUE) {
cont_f = 1;
resv_p[strlen(resv_p)-1] = '\0';
} else {
tblend_p = strpbrk(resv_p, LIST_DELIMIT);
if (tblend_p) {
cont_f = 2;
*tblend_p = '\0';
}
}
}
if (resv_p && !strcmp(resv_p, RESV_READ)) {
rw_prm = isc_tpb_lock_read;
} else if (resv_p && !strcmp(resv_p, RESV_WRITE)) {
rw_prm = isc_tpb_lock_write;
} else {
desc = "RESERVING needs {SHARED|PROTECTED} {READ|WRITE}";
goto error;
}
ofs = res_first;
for (count = 0; count < res_count; count++) {
tpb[ofs++] = rw_prm;
ofs += tpb[ofs] + 1;
tpb[ofs++] = sp_prm;
}
if (cont_f == 1) {
resv_p = strtok(0, CMND_DELIMIT);
if (!resv_p) {
desc = "Unexpected end of command";
goto error;
}
}
if (cont_f == 2) {
resv_p = tblend_p + 1;
} else {
resv_p = strtok(0, CMND_DELIMIT);
if (resv_p) {
if ((int)strlen(resv_p) == 1 && resv_p[0] == RESV_CONTINUE) {
resv_p = strtok(0, CMND_DELIMIT);
if (!resv_p) {
desc = "Unexpected end of command";
goto error;
}
} else if (resv_p[0] == RESV_CONTINUE) {
resv_p++;
} else {
next_c = 1;
check2_p = resv_p;
resv_p = 0;
}
} else {
next_c = 0;
check1_p = check2_p = 0;
}
}
}
check_f[0] = 1;
}
/* Set the next check list */
curr_p = target_p->sub_opts;
for (count = 0; count < next_c; count++) {
check1_p = check2_p;
if (check2_p) {
check2_p = strtok(0, CMND_DELIMIT);
}
}
if (check1_p && !curr_p) {
curr_p = trans_opt_S;
}
}
/* Set the results */
*tpb_len = used;
return tpb;
error:
xfree(tpb);
rb_raise(rb_eFbError, desc);
}
/*
static void set_teb_vec(ISC_TEB *vec, struct FbConnection *fb_connection, char *tpb, int len)
{
vec->dbb_ptr = &fb_connection->db;
if (tpb) {
vec->tpb_ptr = tpb;
vec->tpb_len = len;
} else {
vec->tpb_ptr = 0;
vec->tpb_len = 0;
}
}
*/
/*
static void global_transaction_start(VALUE opt, int argc, VALUE *argv)
{
ISC_STATUS isc_status[20];
struct FbConnection *fb_connection;
ISC_TEB *teb_vec = ALLOCA_N(ISC_TEB, connection_count);
ISC_TEB *vec = teb_vec;
char *tpb = 0;
short n;
int tpb_len;
if (global_transact) {
rb_raise(rb_eFbError, "The transaction has been already started");
}
if (!NIL_P(opt)) {
tpb = trans_parseopts(opt, &tpb_len);
}
if (argc > connection_count) {
rb_raise(rb_eFbError, "Too many databases specified for the transaction");
}
if (argc == 0) {
n = connection_count;
for (fb_connection = fb_connection_list; fb_connection; fb_connection = fb_connection->next) {
set_teb_vec(vec, fb_connection, tpb, tpb_len);
vec++;
}
} else {
for (n = 0; n < argc; n++) {
fb_connection = fb_connection_check_retrieve(argv[n]);