forked from rowland/fb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb.c
3227 lines (2881 loc) · 87.8 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.
* 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.
*/
#include "ruby.h"
/* Ensure compatibility with early releases of Ruby 1.8.5 */
#ifndef RSTRING_PTR
# define RSTRING_PTR(v) RSTRING(v)->ptr
#endif
#ifndef RSTRING_LEN
# define RSTRING_LEN(v) RSTRING(v)->len
#endif
#ifndef RARRAY_PTR
# define RARRAY_PTR(v) RARRAY(v)->ptr
#endif
#ifndef RARRAY_LEN
# define RARRAY_LEN(v) RARRAY(v)->len
#endif
#ifdef HAVE_RUBY_REGEX_H
# include "ruby/re.h"
#else
# include "re.h"
#endif
// this sucks. but for some reason these moved around between 1.8 and 1.9
#ifdef ONIGURUMA_H
#define IGNORECASE ONIG_OPTION_IGNORECASE
#define MULTILINE ONIG_OPTION_MULTILINE
#define EXTENDED ONIG_OPTION_EXTEND
#else
#define IGNORECASE RE_OPTION_IGNORECASE
#define MULTILINE RE_OPTION_MULTILINE
#define EXTENDED RE_OPTION_EXTENDED
#endif
// this sucks too.
#ifndef RREGEXP_SRC_PTR
#define RREGEXP_SRC_PTR(r) RREGEXP(r)->str
#define RREGEXP_SRC_LEN(r) RREGEXP(r)->len
#endif
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <ibase.h>
#include <float.h>
#include <time.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
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_sFbColumn;
static VALUE rb_cDate;
static ID id_matches;
static ID id_downcase_bang;
static VALUE re_lowercase;
static ID id_rstrip_bang;
static ID id_sub_bang;
static ID id_force_encoding;
/* 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;
VALUE encoding;
int dropped;
ISC_STATUS isc_status[20];
/* struct FbConnection *next; */
};
/* static struct FbConnection *fb_connection_list; */
struct FbCursor {
int open;
int 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;
};
typedef struct trans_opts
{
const char *option1;
const 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(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, const char *which)
{
#if defined(_WIN32)
if (tm->tm_year + 1900 < 1970)
{
tm->tm_year = 70;
tm->tm_mon = 0;
tm->tm_mday = 1;
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
}
#endif
#if defined(_LP64) || defined(__LP64__) || defined(__arch64__)
// No need to floor time on 64-bit Unix.
#else
if (tm->tm_year + 1900 < 1902)
{
tm->tm_year = 2;
tm->tm_mon = 0;
tm->tm_mday = 1;
tm->tm_hour = 0;
tm->tm_min = 0;
tm->tm_sec = 0;
}
#endif
return rb_funcall(
rb_cTime, rb_intern(which), 6,
INT2FIX(tm->tm_year + 1900), INT2FIX(tm->tm_mon + 1), INT2FIX(tm->tm_mday),
INT2FIX(tm->tm_hour), INT2FIX(tm->tm_min), INT2FIX(tm->tm_sec));
}
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, VALUE obj)
{
#ifdef TypedData_Get_Struct
VALUE year, month, day, hour, min, sec;
#endif
struct time_object *tobj;
if (!rb_obj_is_kind_of(obj, rb_cTime))
{
if (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);
}
else
{
rb_raise(rb_eTypeError, "Expecting Time object or string.");
}
}
#ifdef TypedData_Get_Struct
year = rb_funcall(obj, rb_intern("year"), 0);
month = rb_funcall(obj, rb_intern("month"), 0);
day = rb_funcall(obj, rb_intern("day"), 0);
hour = rb_funcall(obj, rb_intern("hour"), 0);
min = rb_funcall(obj, rb_intern("min"), 0);
sec = rb_funcall(obj, rb_intern("sec"), 0);
memset(tm, 0, sizeof(struct tm));
tm->tm_year = FIX2INT(year) - 1900;
tm->tm_mon = FIX2INT(month) - 1;
tm->tm_mday = FIX2INT(day);
tm->tm_hour = FIX2INT(hour);
tm->tm_min = FIX2INT(min);
tm->tm_sec = FIX2INT(sec);
#else
GetTimeval(obj, tobj);
*tm = tobj->tm;
#endif
}
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)
{
const 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);
}
/* call-seq:
* from_code(code, subtype) -> String
*
* Returns the SQL type, such as VARCHAR or INTEGER for a given type code and subtype.
*/
static VALUE sql_type_from_code(VALUE self, VALUE code, VALUE subtype)
{
return fb_sql_type_from_code(NUM2INT(code), NUM2INT(subtype));
}
static void fb_error_check(ISC_STATUS *isc_status)
{
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));
rb_exc_raise(exc);
}
}
static void fb_error_check_warn(ISC_STATUS *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)
{
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;
long len = RARRAY_LEN(fb_connection->cursor);
for (i = 0; i < len; i++) {
cursor_close(RARRAY_PTR(fb_connection->cursor)[i]);
}
}
static void fb_connection_drop_cursors(struct FbConnection *fb_connection)
{
int i;
long len = RARRAY_LEN(fb_connection->cursor);
for (i = 0; i < len; i++) {
cursor_drop(RARRAY_PTR(fb_connection->cursor)[i]);
}
rb_ary_clear(fb_connection->cursor);
}
/*
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, long *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;
long tbl_len;
long res_first;
int res_count;
long ofs;
char sp_prm;
char rw_prm;
int cont_f;
const char *desc = 0;
/* Initialize */
s = StringValuePtr(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, "%s", 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;
}
}
*/