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
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
/* -*- c -*- 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
/* 
 * Sybase driver for the Pike programming language. 
 * 
 * By Francesco Chemolli <kinkie@roxen.com> 10/12/1999 
 * 
 */ 
 
/* The sybase libraries are thread-safe, and very well-documented 
 * about that (page 2-131) 
 * For simplicity's sake, for now I'll use a per-connection (== per-object) 
 * lock to serialize connection-related calls, and a program-level lock 
 * for context-related operations. I could use more locks, to have a 
 * more fine-grained locking, but since I now aim to generic SQL-interface 
 * compliancy, this should do nicely. 
 */ 
 
/* 
 * wishlist/todolist: 
 * - have an asynchronous messages reporter 
 * - solve the signals-related problems 
 */ 
 
#include "sybase_config.h" 
#include "module.h" 
 
#ifdef HAVE_SYBASE 
 
#include "pike_error.h" 
#include "las.h" 
#include "threads.h" 
#include "module_support.h" 
#include "builtin_functions.h" 
 
#include "sybase.h" 
 
 
#define sp Pike_sp 
 
/* define this to enable debugging */ 
/* #define SYBDEBUG */ 
 
/* define this to cancel pending results instead of retrieving them */ 
#define FLUSH_BY_CANCELING 
 
/* the sybase libraries are not particularly clever in letting the user 
 * know how much memory space he'll need to allocate in order to completely 
 * fit a column. They _will_ notify him if an overflow occurs, but then 
 * it's too late, isn't it? Some complex strategies, involving re-fetching 
 * an offending row (if an overflow occurs, ct_fetch will return CS_ROW_FAIL), 
 * or trying to query sybase about the needed sizes for each fetched row. 
 * For now it's just easier to reserve more space than actually necessary 
 * This define marks how much extra memory is to be reserved for each 
 * column. 
 */ 
#define EXTRA_COLUMN_SPACE 10240 
 
/* big. VERY big. (2^31) */ 
#define MAX_RESULTS_SIZE 0x7FFFFFFF 
 
#ifdef SYBDEBUG 
#define sybdebug(...) fprintf(stderr,__VA_ARGS__) 
#define errdebug(err) if(err) sybdebug(err) 
#else 
#define sybdebug(...) 
#define errdebug(err) 
#endif 
 
 
/* Actual code */ 
#ifdef _REENTRANT 
/* note to self. Whenever the mainlock is needed and there is an 
 * object involved, always acquire the object's lock first. 
 * post scriptum. Override this: if the main lock is needed, acquire 
 * only that. However, if anybody experiences deadlocks, restore this 
 * behaviour. 
 */ 
static PIKE_MUTEX_T mainlock; 
#define SYB_LOCK(X) sybdebug("locking %s\n",#X);mt_lock(&(X)) 
#define SYB_UNLOCK(X) sybdebug("unlocking %s\n",#X);mt_unlock(&(X)) 
#define SYB_MT_INIT(X) mt_init(&(X)) 
#define SYB_MT_EXIT(X) mt_destroy(&(X)) 
#else 
#define SYB_LOCK(lock) 
#define SYB_UNLOCK(lock) 
#define SYB_MT_INIT(lock) 
#define SYB_MT_EXIT(lock) 
#endif 
 
#define FAILED(status) (status!=CS_SUCCEED) 
#define OK(status) (status==CS_SUCCEED) 
 
DECLARATIONS; 
 
/*************/ 
/* Utilities */ 
/*************/ 
 
#ifdef SYBDEBUG 
 
/* just a small thing. see f_fetch_row to understand how it's used */ 
#define SHOW_STATUS(Y,X) case X:sybdebug("\t%s is %s\n",Y,#X);break 
 
static void show_status(CS_RETCODE ret) { 
    switch(ret) { 
      SHOW_STATUS("status",CS_SUCCEED); 
      SHOW_STATUS("status",CS_PENDING); 
      SHOW_STATUS("status",CS_BUSY); 
      SHOW_STATUS("status",CS_END_RESULTS); 
      SHOW_STATUS("status",CS_FAIL); 
      SHOW_STATUS("status",CS_END_DATA); 
    default: 
      sybdebug("Unknown status: %d\n",(int)ret); 
      break; 
    } 
} 
static void show_results_type (CS_INT rtype) { 
    switch(rtype) { 
      SHOW_STATUS("type",CS_ROW_RESULT); 
      SHOW_STATUS("type",CS_CURSOR_RESULT); 
      SHOW_STATUS("type",CS_PARAM_RESULT); 
      SHOW_STATUS("type",CS_STATUS_RESULT); 
      SHOW_STATUS("type",CS_COMPUTE_RESULT); 
      SHOW_STATUS("type",CS_MSG_RESULT); 
      SHOW_STATUS("type",CS_DESCRIBE_RESULT); 
      SHOW_STATUS("type",CS_ROWFMT_RESULT); 
      SHOW_STATUS("type",CS_COMPUTEFMT_RESULT); 
      SHOW_STATUS("type",CS_CMD_DONE); 
      SHOW_STATUS("type",CS_CMD_SUCCEED); 
      SHOW_STATUS("type",CS_CMD_FAIL); 
    default: 
      sybdebug("Unknown type: %d\n",(int)rtype); 
      break; 
    } 
} 
static void show_severity(CS_INT severity) { 
    switch (severity) { 
      SHOW_STATUS("severity",CS_SV_INFORM); 
      SHOW_STATUS("severity",CS_SV_CONFIG_FAIL); 
      SHOW_STATUS("severity",CS_SV_RETRY_FAIL); 
      SHOW_STATUS("severity",CS_SV_API_FAIL); 
      SHOW_STATUS("severity",CS_SV_RESOURCE_FAIL); 
      SHOW_STATUS("severity",CS_SV_COMM_FAIL); 
      SHOW_STATUS("severity",CS_SV_INTERNAL_FAIL); 
      SHOW_STATUS("severity",CS_SV_FATAL); 
    } 
} 
#else /* SYBDEBUG */ 
#define show_status(X) 
#define show_results_type(X) 
#define show_severity(X) 
#endif 
 
PIKECLASS sybase 
{ 
  CVAR CS_CONTEXT *context; 
  CVAR CS_CONNECTION *connection; 
  CVAR CS_COMMAND *cmd; 
  CVAR char busy; /* only one pending command per connection */ 
 
  CVAR char had_error; /* non-zero if had error */ 
  CVAR char error[256]; /* The last error string. The size is determined by the */ 
                    /* sybase API */ 
 
  CVAR char **results; 
  CVAR CS_INT *results_lengths; 
  CVAR CS_SMALLINT *nulls; 
 
  CVAR int numcols; /* the number of columns */ 
 
#ifdef _REENTRANT 
  CVAR PIKE_MUTEX_T lock; 
#endif 
 
  DECLARE_STORAGE; 
 
  /*! @decl inherit __builtin.Sql.Connection 
   */ 
  INHERIT "__builtin.Sql.Connection"; 
 
  typedef struct sybase_sybase_struct pike_sybase_connection; 
 
/* 
 * Must be called in a MT-safe fashion 
 */ 
static INT32 guess_column_length (CS_COMMAND *cmd, int column_number) { 
  CS_DATAFMT description; 
 
  ct_describe(cmd,column_number,&description); 
  return (INT32)description.maxlength; 
} 
 
/* this function does _NOT_ involve any pike processing 
 * If run in MT-enabled sections, locking is to be insured by the caller 
 * on a per-command-structure basis. 
 */ 
static void flush_results_queue(pike_sybase_connection *this) { 
  CS_INT rtype; 
  CS_RETCODE ret; 
  int j; 
  CS_COMMAND *cmd; 
 
 
  sybdebug("Flushing the results queue\n"); 
#ifdef FLUSH_BY_CANCELING 
  ct_cancel(this->connection,NULL,CS_CANCEL_ALL); 
#else 
  cmd=this->cmd; 
  for (j=0;j<100;j++) { /* safety valve, I don't want to loop forever */ 
 
    sybdebug("Getting results #%d\n",j); 
    ret=ct_results(cmd,&rtype); 
    show_status(ret); 
    show_results_type(rtype); 
 
    switch(ret) { 
    case CS_END_RESULTS: 
      sybdebug("Finished flushing results\n"); 
      return; 
    case CS_FAIL: 
      ret=ct_cancel(this->connection,NULL,CS_CANCEL_ALL); 
      continue; 
    } 
 
    /* I'd probably be getting a result back here. I don't need it, so 
     * I'll just cancel */ 
    switch(rtype) { 
    case CS_ROW_RESULT: 
    case CS_STATUS_RESULT: 
    case CS_PARAM_RESULT: 
      sybdebug("Unexpected result. Cancel-ing\n"); 
      ret=ct_cancel(NULL,this->cmd,CS_CANCEL_CURRENT); 
      show_status(ret); 
      break; 
    } 
  } 
#endif 
} 
 
/* 
 * returns 1 if there's any Pike_error 
 * the same thread-safety rules as flush_results_queue apply 
 * QUESTION: 
 * Should I explore all messages, and leave in this->error the 
 * last one with an error-level severity? 
 * or should we maybe only consider server messages? 
 */ 
static int handle_errors (pike_sybase_connection *this) { 
  SQLCA message; 
  CS_INT num,j, severity; 
  CS_RETCODE ret; 
 
  sybdebug("Handling errors\n"); 
  ret=ct_diag(this->connection,CS_STATUS,CS_ALLMSG_TYPE,CS_UNUSED,&num); 
  show_status(ret); 
  if (FAILED(ret)) { 
    sybdebug("Error while retrieving errors number"); 
    return 1; 
  } 
  sybdebug ("I have %d messages in queue\n",(int)num); 
 
  if (!num) /* no need to go through the moves */ 
    return 0; 
 
  for (j=1;j<=num;j++) { 
    ct_diag(this->connection, CS_GET, SQLCA_TYPE, j, &message); 
    if (FAILED(ret)) { 
      sybdebug("Error while retrieving last message\n"); 
      return 1; 
    } 
    sybdebug("\nMessage %d length: %ld; text:\n%s\n",j, 
             ((int)message.sqlerrm.sqlerrml),message.sqlerrm.sqlerrmc); 
    /* If it's severe enough? */ 
    severity=CS_SEVERITY(message.sqlcode); 
    show_severity(severity); 
  } 
 
  memcpy(this->error,message.sqlerrm.sqlerrmc, 
         message.sqlerrm.sqlerrml+1); 
 
  this->had_error=1; 
  ct_diag(this->connection,CS_CLEAR,SQLCA_TYPE,CS_UNUSED,NULL); 
 
  return 0; 
} 
 
 
/*******************/ 
/* low_level stuff */ 
/*******************/ 
 
INIT 
{ 
  pike_sybase_connection *this=THIS; 
 
  sybdebug("sybase_create()\n"); 
  this->context=NULL; 
  this->connection=NULL; 
  this->cmd=NULL; 
  this->busy=0; 
  this->results=NULL; 
  this->results_lengths=NULL; 
  this->nulls=NULL; 
  this->numcols=0; 
  this->had_error=0; 
  SYB_MT_INIT(THIS->lock); 
} 
 
 
EXIT 
{ 
  pike_sybase_connection *this=THIS; 
  CS_RETCODE ret; 
  int j; 
 
  sybdebug("sybase_destroy()\n"); 
  THREADS_ALLOW(); 
  SYB_LOCK(this->lock); 
 
 
  if (this->busy > 0) { 
    sybdebug("We're busy while destroying. Trying to cancel\n"); 
    ret=ct_cancel(this->connection,NULL,CS_CANCEL_ALL); 
    show_status(ret); 
    /* we only have one active command, but what the hell.. */ 
    if (FAILED(ret)) { 
      sybdebug("\tUh oh... failed\n"); 
    } 
    this->busy--; 
    sybdebug("Busy status: %d\n",this->busy); 
    /* if we fail, it's useless anyways. Maybe we should Pike_fatal() */ 
  } 
 
  if (this->cmd) { 
    sybdebug("this->cmd still active. Dropping\n"); 
    ret=ct_cmd_drop(this->cmd); 
    show_status(ret); 
    if (FAILED(ret)) { 
      sybdebug("\tHm... failed\n"); 
    } 
    this->cmd=NULL; 
    /* if we fail, it's useless anyways. Maybe we should Pike_fatal() */ 
  } 
 
  if (this->results) { 
    sybdebug("Freeing results buffers\n"); 
    for (j=0;j<this->numcols;j++) { 
      free(this->results[j]); 
    } 
    free (this->results); 
    free (this->results_lengths); 
    free (this->nulls); 
    this->results=NULL; 
    this->results_lengths=NULL; 
    this->nulls=NULL; 
    this->numcols=0; 
  } 
 
  if (this->connection) { 
    sybdebug("this->connection still active. Closing\n"); 
    ret=ct_close(this->connection,CS_UNUSED); 
    show_status(ret); 
    if (FAILED(ret)) { 
      sybdebug("\tArgh! Failed!\n"); 
    } 
    sybdebug("Dropping connection\n"); 
    ret=ct_con_drop(this->connection); 
    show_status(ret); 
    if (FAILED(ret)) { 
      sybdebug("Gasp! Failed!\n"); 
    } 
    this->connection=NULL; 
    /* if we fail, it's useless anyways. Maybe we should Pike_fatal() */ 
  } 
 
  SYB_LOCK(mainlock); /* this is really needed only here */ 
 
  if (this->context) { 
    sybdebug("this->context still active. Exiting\n"); 
    ret = ct_exit(this->context, CS_UNUSED); 
    show_status(ret); 
    if (FAILED(ret)) { 
      sybdebug("\tGosh Batman! We failed!\n"); 
    } 
    ret = cs_ctx_drop(this->context); 
    show_status(ret); 
    if (FAILED(ret)) { 
      sybdebug("\tGosh Robin! You're right!\n"); 
    } 
    this->context=NULL; 
    /* if we fail, it's useless anyways. Maybe we should Pike_fatal() */ 
  } 
 
  SYB_UNLOCK(mainlock); 
  SYB_UNLOCK(this->lock); 
 
  THREADS_DISALLOW(); 
  SYB_MT_EXIT(THIS->lock); 
} 
 
/**********************/ 
/* exported functions */ 
/**********************/ 
 
PIKEFUN string error(void|int clear) 
{ 
  int clearit = clear && clear->u.integer; 
  pike_sybase_connection *this=THIS; 
 
  pop_n_elems(args); 
 
  if (this->had_error) { 
    push_text(this->error); 
    if (clearit) 
      this->had_error = 0; 
  } else 
    push_int(0); 
} 
 
 
/* void connect(host, database, username, password, port) */ 
/*  The port and database arguments are silently ignored  */ 
PIKEFUN void connect(string|void hostname_, string|void database_, 
                     string|void username_, string|void pass_, 
                     int|void port_) 
{ 
  CS_RETCODE ret; 
  CS_CONTEXT *context; 
  CS_CONNECTION *connection; 
  char *username=NULL, *pass=NULL, *hostname=NULL, *err=NULL; 
  int usernamelen=0, passlen=0, hostnamelen=CS_UNUSED; 
  pike_sybase_connection *this=THIS; 
 
  sybdebug("sybase::connect(args=%d)\n",args); 
 
  /* fetch arguments, so that we can enable threads */ 
  if (TYPEOF(sp[2-args]) == T_STRING && sp[2-args].u.string->len) { /*username*/ 
    username=sp[2-args].u.string->str; 
    usernamelen=sp[2-args].u.string->len; 
    sybdebug("\tgot username: %s\n",username); 
  } 
  if (TYPEOF(sp[3-args]) == T_STRING && sp[2-args].u.string->len) { /*password*/ 
      pass=sp[3-args].u.string->str; 
    passlen=sp[3-args].u.string->len; 
    sybdebug("\tgot password: %s\n",pass); 
  } 
  if (TYPEOF(sp[-args]) == T_STRING && sp[-args].u.string->len) { /*hostname*/ 
    hostname=sp[-args].u.string->str; 
    hostnamelen=sp[-args].u.string->len; 
    sybdebug("\tgot hostname: %s\n",hostname); 
  } 
 
  THREADS_ALLOW(); 
  SYB_LOCK(mainlock); 
 
  /* It's OK not to lock here. It's just a check that should never happen. 
   * if it happens, we're in deep sh*t already.*/ 
  if (!(context=this->context)) { 
    err="Internal error: connection attempted, but no context available\n"; 
  } 
 
  if (!err) { 
    sybdebug("\tallocating context\n"); 
    ret=ct_con_alloc(context,&connection); /*sybase says it's thread-safe..*/ 
    show_status(ret); 
    if (FAILED(ret)) { 
      err="Cannot initialize connection\n"; 
    } 
  } 
  errdebug(err); 
 
  if (!err) { /* initialize error-handling code */ 
    ret=ct_diag(connection,CS_INIT,CS_UNUSED,CS_UNUSED,NULL); 
    show_status(ret); 
    if (FAILED(ret)) { 
      err="Can't initialize error-handling code\n"; 
    } 
  } 
  errdebug(err); 
 
  /* if there already was an error, we just locked uselessly. 
   * No big deal, it should never happen anyways... */ 
 
  /* username */ 
  if (!err && usernamelen) { 
    sybdebug("\tsetting username to \"%s\"\n",username); 
    ret=ct_con_props(connection,CS_SET,CS_USERNAME, 
                     username,usernamelen,NULL); 
    show_status(ret); 
    if (FAILED(ret)) { 
      err="Can't set username\n"; 
    } 
  } 
  errdebug(err); 
 
  /* password */ 
  if (!err && passlen) { 
    sybdebug("\tsetting password to \"%s\"\n",pass); 
    ret=ct_con_props(connection,CS_SET,CS_PASSWORD, 
                     pass,passlen,NULL); 
    show_status(ret); 
    if (FAILED(ret)) { 
      err="Can't set password\n"; 
    } 
  } 
  errdebug(err); 
 
  /* connect, finally */ 
  if (!err) { 
    if (hostname) { 
      sybdebug("\tconnecting to hostname is \"%s\"\n",hostname); 
    } 
    ret=ct_connect(connection,(CS_CHAR *)hostname, hostnamelen); 
    show_status(ret); 
    if (FAILED(ret)) { 
      err="Can't connect\n"; 
    } else { 
      this->connection=connection; 
    } 
  } 
  errdebug(err); 
 
  if (err) ct_con_drop(connection); 
 
  SYB_UNLOCK(mainlock); 
  THREADS_DISALLOW(); 
 
  if (err) { 
    handle_errors(this); /* let's centralize */ 
    Pike_error(err); 
  } 
 
  sybdebug("sybase::connect exiting\n"); 
} 
 
/* create (host,database,username,password,port|options) */ 
PIKEFUN void create(string|void host_, string|void db_, 
                    string|void user_, string|void pass_, 
                    int|mapping(string:mixed)|void options_) 
{ 
  char *host,*db,*user,*pass; 
  CS_RETCODE ret; 
  CS_CONTEXT *context; 
  char* err=NULL; 
  pike_sybase_connection *this=THIS; 
 
  sybdebug("sybase::create(args=%d)\n",args); 
 
  /* if connected, disconnect */ 
  if (this->context) 
    sybase_destroy(Pike_fp->current_object); 
 
  THREADS_ALLOW(); 
  SYB_LOCK(mainlock); 
 
  /* allocate context */ 
  ret=cs_ctx_alloc(CS_VERSION_110,&(this->context)); 
  show_status(ret); 
  if (FAILED(ret)) 
    err = "Cannot allocate context!\n"; 
 
  context=this->context; 
 
  /* initialize open client-library emulation */ 
  if (!err) { 
    ret=ct_init(context,CS_VERSION_110); 
    if (FAILED(ret)) { 
      ct_exit(context,CS_UNUSED); 
      err="Cannot initialize library version 1.11.X!\n"; 
    } 
  } 
 
  /* if there was no error, we can try to connect. Since f_connect 
   * will do its own threads meddling, we must disable threads first 
   */ 
 
  if (err) { /* there was an error. bail out */ 
    cs_ctx_drop(context); 
    this->context=NULL; 
  } 
 
  SYB_UNLOCK(mainlock); 
  THREADS_DISALLOW(); 
 
  if (err) Pike_error(err); /* throw the exception if appropriate */ 
 
  /* now connect */ 
  if (args) 
    f_connect(args); 
 
  sybdebug("sybase::create exiting\n"); 
} 
 
/* TODO: should I cancel a pending query instead of throwing errors? 
 * 
 * object(sybase.sybase_result)|string big_query(string q) 
 * 
 * NOTICE: the string return value is an extension to the standard API. 
 *  It will returned ONLY if it is the return status from a stored procedure 
 */ 
 
#define PS_NORESULT 1 
#define PS_RESULT_THIS 2 
#define PS_RESULT_STATUS 3 
/* if function_result == PS_RESULT_STATUS, the value to be returned 
   is *function_result */ 
 
PIKEFUN object|string big_query(string query_) 
  flags ID_VARIANT; 
{ 
  pike_sybase_connection *this=THIS; 
  char *query, *err=NULL, *function_result=NULL; 
  int querylen, numcols,j, done=0; 
  CS_COMMAND *cmd=this->cmd; 
  CS_RETCODE ret; 
  CS_DATAFMT description, got_desc; 
  CS_INT rtype; 
  char ** results; 
  CS_INT *results_lengths; 
  CS_SMALLINT *nulls; 
  int toreturn=PS_NORESULT; /* one of the #defines here above */ 
 
  /* check, get and pop args */ 
  check_all_args(NULL,args,BIT_STRING,0); 
  query=sp[-args].u.string->str; 
  querylen=sp[-args].u.string->len; 
  sybdebug("query: '%s'\n",query); 
  /* pop_n_elems(args); Let's try moving it later */ 
 
  sybdebug("Busy status: %d\n",this->busy); 
 
  /* verify that we're not busy handing out results */ 
  if (this->busy > 0) 
    Pike_error("Busy\n"); 
 
  THREADS_ALLOW(); 
  SYB_LOCK(this->lock); 
 
  if (cmd==NULL) { 
    /* no sense in alloc-ing everytime */ 
    sybdebug("Allocating command structure\n"); 
    ret=ct_cmd_alloc(this->connection, &cmd); 
    show_status(ret); 
 
    if (FAILED(ret)) { 
      sybdebug("\tUh oh... problems\n"); 
      err="Error allocating command\n"; 
    } 
 
    this->cmd=cmd; 
  } 
 
  if (this->results) { 
    sybdebug("Freeing previous results DMA memory\n"); 
    for (j=0;j<(this->numcols);j++) { 
      sybdebug("Column %d (%p)\n",j,this->results[j]); 
      free(this->results[j]); 
    } 
    sybdebug("Freeing main results array\n"); 
    free(this->results); 
    free(this->results_lengths); 
    free(this->nulls); 
    this->results=NULL; 
    this->results_lengths=NULL; 
    this->nulls=NULL; 
    this->numcols=0; 
  } 
 
  if (!err) { /* we can't be done yet */ 
    sybdebug("issung command\n"); 
    ret=ct_command(cmd,CS_LANG_CMD,query,querylen,CS_UNUSED); 
    show_status(ret); 
    if (FAILED(ret)) { 
      sybdebug("\tUh oh... something wrong here\n"); 
      err="Error while setting command\n"; 
    } 
  } 
 
  if (!err) { /* we can't be done yet */ 
    sybdebug("Sending command\n"); 
    ret=ct_send(cmd); 
    show_status(ret); 
    this->busy++; 
    sybdebug("Busy status: %d\n",this->busy); 
    if (FAILED(ret)) { 
      sybdebug("\thm... I don't like what I'm seeing\n"); 
      err="Error while sending command\n"; 
    } 
  } 
 
  if (err) { /* we can't be done yet */ 
    sybdebug("Problems. Dropping command\n"); 
    ct_cmd_drop(cmd); 
    this->cmd=NULL; 
/*     this->busy--; */ 
/*     sybdebug("Busy status: %d\n",this->busy); */ 
  } 
 
 
  /* let's move the first part of a process here. 
   */ 
 
  while (!err && !done) { 
    /* okay, let's see what we got */ 
    sybdebug("Issuing results\n"); 
    ret=ct_results(cmd,&rtype); 
    show_status(ret); 
    show_results_type(rtype); 
 
    switch(ret) { 
    case CS_PENDING: 
    case CS_BUSY: 
      sybdebug("Got strange stuff\n"); 
      err="Async operations are not supported\n"; 
      break; 
    case CS_END_RESULTS: 
      sybdebug("No more results\n"); 
/*       this->busy--; */ 
/*       sybdebug("Busy status: %d\n",this->busy); */ 
      done=1;/* we return 0 from a query. Sounds good.. */ 
      break; 
    case CS_FAIL: /* uh oh, something's terribly wrong */ 
      sybdebug("Command failed. Canceling\n"); 
      ct_cancel(this->connection,NULL,CS_CANCEL_ALL);/* try to cancel result */ 
/*       this->busy--; */ 
/*       sybdebug("Busy status: %d\n",this->busy); */ 
      /* we should really check the return code here.. */ 
      err="Canceled\n"; 
      flush_results_queue(this); 
      break; 
    case CS_CANCELED: 
      sybdebug("Results canceled\n"); 
/*       this->busy--; */ 
      flush_results_queue(this); 
      break; 
    default:; /* Ok */ 
    } 
 
    if (err||done) break; /* get out of the while cycle */ 
 
  /* We should probably put this in a do..while cycle, so that we can 
   * ignore STATUS results and such: only the first three carry useful 
   * information.. 
   */ 
  /* we need to set the fetch-cycle up. It really looks like DMA to me.. */ 
    switch (rtype) { 
    case CS_ROW_RESULT: 
    case CS_CURSOR_RESULT: 
    case CS_PARAM_RESULT: 
      sybdebug("Got useful results\n"); 
      /* we're getting rows of data here. Also, I'm skipping a few command 
         results, for command which should be pretty safe */ 
      ct_res_info(cmd,CS_NUMDATA,&numcols,CS_UNUSED,NULL); 
      this->numcols=numcols; 
      sybdebug("I have %d columns' worth of data\n",numcols); 
      sybdebug("Allocating results**\n"); 
      /* it looks like xalloc can't be used here. Hubbe? */ 
      results=calloc(numcols, sizeof(char*)); 
      results_lengths=calloc(numcols, sizeof(CS_INT)); 
      nulls=calloc(numcols, sizeof(CS_SMALLINT)); 
      this->results=results; 
      this->results_lengths=results_lengths; 
      this->nulls=nulls; 
 
      /* these values are set for each column, since we're fetching 
       * one row per cycle in fetch_row */ 
/*       description.datatype=CS_TEXT_TYPE; */ 
      description.datatype=CS_CHAR_TYPE; /* let's see if this works better */ 
      description.format=CS_FMT_NULLTERM; 
      description.maxlength=MAX_RESULTS_SIZE-1; 
      description.scale=CS_SRC_VALUE; 
      description.precision=CS_SRC_VALUE; 
      description.count=1; 
      description.locale=NULL; 
 
      for(j=0;j<numcols;j++) { 
        INT32 length=guess_column_length(cmd,j+1); 
        sybdebug("beginning work on column %d (index %d)\n",j+1,j); 
        sybdebug("Allocating %ld+%d+1 bytes\n",length,EXTRA_COLUMN_SPACE); 
        /* it looks like xalloc can't be used here. Hubbe? */ 
        results[j]=calloc(length+EXTRA_COLUMN_SPACE+1, 
                          sizeof(char)); 
        if (results[j]==NULL) { 
          sybdebug("PANIC! COULDN'T ALLOCATE MEMORY!"); 
        } 
 
        sybdebug("Binding column %d\n",j+1); 
        description.maxlength=length+EXTRA_COLUMN_SPACE; 
        /* maxlength used to be MAX_RESULTS_SIZE-1. Let's make sure we don't 
         * goof*/ 
        ret=ct_bind(cmd,j+1,&description,results[j], 
                    &(results_lengths[j]),&(nulls[j])); 
        /* TODO: replace the first NULL with the length indicator */ 
        /* TODO: replace the second NULL with (SQL)NULL indicators */ 
        /* see page 3-10 */ 
        show_status(ret); 
        if (FAILED(ret)) { 
          err="Failed to bind column\n"; 
          break; 
        } 
      } 
      toreturn=PS_RESULT_THIS; 
      done=1; 
      break; 
    case CS_STATUS_RESULT: 
      sybdebug("Got status result. Retrieving\n"); 
      do { 
        INT32 length; 
        CS_DATAFMT description; 
        CS_INT rows_read; 
 
        length=guess_column_length(cmd,1); 
 
        sybdebug("Guessed length of %d\n",length); 
        function_result=calloc(length+EXTRA_COLUMN_SPACE+1, 
                               sizeof(char)); 
 
        description.datatype=CS_CHAR_TYPE; 
        description.format=CS_FMT_NULLTERM; 
        description.maxlength=length+EXTRA_COLUMN_SPACE; 
        description.scale=CS_SRC_VALUE; 
        description.precision=CS_SRC_VALUE; 
        description.count=1; 
        description.locale=NULL; 
 
 
        sybdebug("Binding...\n"); 
        ret=ct_bind(cmd,1,&description,function_result,NULL,NULL); 
        /* TODO: use binary strings */ 
 
        do { 
          sybdebug("Fetching\n"); 
          ret=ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read); 
          show_status(ret); 
          sybdebug("Got: %s\n",function_result); 
        } while (OK(ret)); 
 
 
 
        toreturn=PS_RESULT_STATUS; 
 
      } while (0); 
      /* flush_results_queue(this); No need to. We'll loop in the while 
       * cycle, and if there's some result we'll catch it elsewhere. 
       */ 
      break; 
    case CS_COMPUTE_RESULT: 
/*       this->busy--; */ 
      err="Compute result is not supported\n"; 
      flush_results_queue(this); 
      break; 
    case CS_MSG_RESULT: 
/*       this->busy--; */ 
      err="Message result is not supported\n"; 
      flush_results_queue(this); 
      break; 
    case CS_DESCRIBE_RESULT: 
/*       this->busy--; */ 
      err="Describe result is not supported\n"; 
      flush_results_queue(this); 
      break; 
    case CS_ROWFMT_RESULT: 
/*       this->busy--; */ 
      err="Row Format result is not supported\n"; 
      flush_results_queue(this); 
      break; 
    case CS_COMPUTEFMT_RESULT: 
/*       this->busy--; */ 
      err="Compute Format result is not supported\n"; 
      flush_results_queue(this); 
      break; 
    case CS_CMD_DONE: 
/*       this->busy--; */ 
      flush_results_queue(this); 
      done=1; 
      break; 
    case CS_CMD_SUCCEED: 
/*       this->busy--; */ 
      flush_results_queue(this); 
      done=1; 
      break; 
    case CS_CMD_FAIL: 
/*       this->busy--; */ 
      err="Command failed\n"; 
      flush_results_queue(this); 
      break; 
    } 
  } 
 
  sybdebug("Busy status: %d\n",this->busy); 
 
  SYB_UNLOCK(this->lock); 
  THREADS_DISALLOW(); 
 
  if (err) { 
    handle_errors(this); 
    this->busy--; 
    Pike_error(err); 
  } 
 
  pop_n_elems(args); /* moved from earlier. */ 
 
  /* we're surely done */ 
  switch (toreturn) { 
  case PS_NORESULT: 
    this->busy--; 
    push_int(0); 
    break; 
  case PS_RESULT_THIS: 
    ref_push_object(Pike_fp->current_object); 
    break; 
  case PS_RESULT_STATUS: 
    this->busy--; 
    push_text(function_result); 
    free(function_result); 
    function_result=NULL; 
    break; 
  default: 
    Pike_fatal("Internal error! Wrong result in big_query\n"); 
    break; 
  } 
  /* extra safety check. Paranoia. */ 
  if (function_result) { 
    sybdebug("Internal error! Function_result!=NULL"); 
    free(function_result); 
  } 
} 
 
 
/* 
 * The while cycle is supposed to be outside this function call, in the pike 
 * code. 
 */ 
/* void|array(mix) fetch_row() */ 
PIKEFUN array(mixed) fetch_row() 
{ 
  pike_sybase_connection *this=THIS; 
  CS_RETCODE ret; 
  CS_COMMAND *cmd=this->cmd; 
  CS_INT numread,j; 
  char **results=this->results; 
  CS_INT *results_lengths=this->results_lengths; 
  CS_SMALLINT *nulls=this->nulls; 
  int numcols=this->numcols; 
 
  pop_n_elems(args); 
 
  if (this->busy<=0) 
    Pike_error("No pending results\n"); 
 
  THREADS_ALLOW(); 
  SYB_LOCK(this->lock); 
 
  sybdebug("Fetching row\n"); 
  ret=ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&numread); 
  show_status(ret); 
 
  SYB_UNLOCK(this->lock); 
  THREADS_DISALLOW(); 
 
  switch(ret) { 
  case CS_SUCCEED: 
    sybdebug("Got row\n"); 
    for(j=0;j<numcols;j++) { 
      if (nulls[j] != -1) { 
        /* !null */ 
        push_string(make_shared_binary_string(results[j], 
                                              results_lengths[j]-1)); 
      } else { 
        /* null */ 
        push_int(0); 
      } 
    } 
    f_aggregate(numcols); 
    return; 
  case CS_END_DATA: 
    sybdebug("Got end of data\n"); 
    flush_results_queue(this); 
    push_int(0); 
    this->busy--; 
    sybdebug("Busy status: %d\n",this->busy); 
    return; 
  case CS_ROW_FAIL: 
    handle_errors(this); 
    Pike_error("Recoverable error while fetching row\n"); 
    break; 
  case CS_FAIL: 
    handle_errors(this); 
    ct_cancel(this->connection,cmd,CS_CANCEL_ALL); 
    this->busy--; 
    sybdebug("Busy status: %d\n",this->busy); 
    Pike_error("Unrecoverable error while fetching row\n"); 
    break; 
  case CS_CANCELED: 
    sybdebug("Canceled\n"); 
    push_int(0); 
    this->busy--; 
    sybdebug("Busy status: %d\n",this->busy); 
    return; 
  case CS_PENDING: 
  case CS_BUSY: 
    Pike_error("Asynchronous operations are not supported\n"); 
    break; 
  } 
  Pike_error("Internal error. We shouldn't get here\n"); 
} 
 
/* int num_fields() */ 
PIKEFUN int num_fields() 
{ 
  CS_INT cols; 
  CS_RETCODE ret; 
  char* err=NULL; 
  pike_sybase_connection *this=THIS; 
 
  pop_n_elems(args); 
  if (this->busy<=0) { 
    Pike_error("Issue a command first!\n"); 
  } 
 
  THREADS_ALLOW(); 
  SYB_LOCK(this->lock); 
 
  ret=ct_res_info(this->cmd,CS_NUMDATA,&cols,CS_UNUSED,NULL); 
  if (FAILED(ret)) { 
    err="Can't retrieve columns number information\n"; 
    handle_errors(this); 
  } 
 
  SYB_UNLOCK(this->lock); 
  THREADS_DISALLOW(); 
 
  if (err) Pike_error(err); 
 
  push_int(cols); 
} 
 
/* int affected_rows() */ 
PIKEFUN int affected_rows() 
{ 
  CS_INT rows; 
  CS_RETCODE ret; 
  char *err=NULL; 
  pike_sybase_connection *this=THIS; 
 
  pop_n_elems(args); 
 
  THREADS_ALLOW(); 
  SYB_LOCK(this->lock); 
 
  ret=ct_res_info(this->cmd,CS_ROW_COUNT,&rows,CS_UNUSED,NULL); 
  if (FAILED(ret)) { 
    err="Can't retrieve affected rows information\n"; 
    handle_errors(this); 
  } 
 
  SYB_UNLOCK(this->lock); 
  THREADS_DISALLOW(); 
 
  if (err) Pike_error(err); 
 
  push_int(rows); 
} 
 
 
#define desc_type(X,Y) case X: push_static_text(#Y);sybdebug("type is %s\n",#Y); break 
PIKEFUN array(mapping(string:mixed)) fetch_fields() 
{ 
  pike_sybase_connection *this=THIS; 
  int j, nflags, numcols=this->numcols; 
  CS_DATAFMT descs[numcols], *desc; 
  CS_RETCODE ret; 
  char* err=NULL; 
 
  if (this->busy<=0) 
    Pike_error("You must issue a command first\n"); 
 
  pop_n_elems(args); 
 
  THREADS_ALLOW(); 
  SYB_LOCK(this->lock); 
 
  for (j=0;j<numcols;j++) { 
    sybdebug("Describing column %d\n",j+1); 
    ret=ct_describe(this->cmd,j+1,&descs[j]); 
    show_status(ret); 
    if (FAILED(ret)) { 
      err="Error while fetching descriptions\n"; 
      break; 
    } 
  } 
 
  SYB_UNLOCK(this->lock); 
  THREADS_DISALLOW(); 
 
  for(j=0;j<numcols;j++) { 
    nflags=0; 
    desc=&descs[j]; 
    push_static_text("name"); 
    push_text(desc->name); 
    sybdebug("name is %s\n",desc->name); 
    ref_push_string(literal_type_string); 
    switch(desc->datatype) { 
      desc_type(CS_ILLEGAL_TYPE,illegal); 
      desc_type(CS_CHAR_TYPE,char); 
      desc_type(CS_BINARY_TYPE,bynary); 
      desc_type(CS_LONGCHAR_TYPE,longchar); 
      desc_type(CS_LONGBINARY_TYPE,longbinary); 
      desc_type(CS_TEXT_TYPE,text); 
      desc_type(CS_IMAGE_TYPE,image); 
      desc_type(CS_TINYINT_TYPE,tinyint); 
      desc_type(CS_SMALLINT_TYPE,smallint); 
      desc_type(CS_INT_TYPE,integer); 
      desc_type(CS_REAL_TYPE,real); 
      desc_type(CS_FLOAT_TYPE,float); 
      desc_type(CS_BIT_TYPE,bit); 
      desc_type(CS_DATETIME_TYPE,datetime); 
      desc_type(CS_DATETIME4_TYPE,datetime4); 
      desc_type(CS_MONEY_TYPE,money); 
      desc_type(CS_MONEY4_TYPE,money4); 
      desc_type(CS_NUMERIC_TYPE,numeric); 
      desc_type(CS_DECIMAL_TYPE,decimal); 
      desc_type(CS_VARCHAR_TYPE,varchar); 
      desc_type(CS_VARBINARY_TYPE,varbinary); 
      desc_type(CS_LONG_TYPE,long); 
      desc_type(CS_SENSITIVITY_TYPE,sensitivity); 
      desc_type(CS_BOUNDARY_TYPE,boundary); 
      desc_type(CS_VOID_TYPE,void); 
      desc_type(CS_USHORT_TYPE,ushort); 
    default: 
      push_static_text("unknown"); 
    } 
    push_static_text("max_length"); 
    push_int(desc->maxlength); 
    sybdebug("max_length is %d\n",desc->maxlength); 
 
    push_static_text("flags"); 
    if (!(desc->status & CS_CANBENULL)) { 
      sybdebug("Flag: not null\n"); 
      push_static_text("not_null"); 
      nflags++; 
    } 
    if (desc->status & CS_HIDDEN) { 
      sybdebug("Flag: hidden\n"); 
      push_static_text("hidden"); 
      nflags++; 
    } 
    if (desc->status & CS_IDENTITY) { 
      sybdebug("Flag: identity\n"); 
      push_static_text("identity"); 
      nflags++; 
    } 
    if (desc->status & CS_KEY) { 
      sybdebug("Flag: key\n"); 
      push_static_text("key"); 
      nflags++; 
    } 
    if (desc->status & CS_VERSION_KEY) { 
      sybdebug("Flag: version_key\n"); 
      push_static_text("version_key"); 
      nflags++; 
    } 
    if (desc->status & CS_TIMESTAMP) { 
      sybdebug("Flag: timestamp\n"); 
      push_static_text("timestamp"); 
      nflags++; 
    } 
    if (desc->status & CS_UPDATABLE) { 
      sybdebug("Flag: updatable\n"); 
      push_static_text("updatable"); 
      nflags++; 
    } 
    if (nflags) { 
      sybdebug("Aggregating flags: %d members\n",nflags); 
      f_aggregate_multiset(nflags); 
    } else { 
      sybdebug("No flags"); 
      push_int(0); 
    } 
 
    f_aggregate_mapping(2*4); 
  } 
 
  sybdebug("Aggregating columns: %d members\n",numcols); 
  f_aggregate(numcols); 
 
} 
 
  /* TODO */ 
  /* int num_rows() */ /* looks like sybase doesn't support this one.. */ 
  /* void seek(int skip) */ /*implemented in pike. Inefficient but simple */ 
  /* mapping* fetch_fields() */ 
} 
 
/********/ 
/* Glue */ 
/********/ 
 
PIKE_MODULE_EXIT { 
  EXIT; 
 
  SYB_MT_EXIT(mainlock); 
} 
 
PIKE_MODULE_INIT { 
 
  sybdebug("sybase driver release " SYBASE_DRIVER_VERSION "\n"); 
 
  INIT; 
 
  SYB_MT_INIT(mainlock); 
} 
 
 
#else /* HAVE_SYBASE */ 
PIKE_MODULE_INIT {} 
PIKE_MODULE_EXIT {} 
#endif /* HAVE_SYBASE */