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
  
#pike __REAL_VERSION__ 
 
//! This class is the base class for connections to SQL servers. It is a 
//! generic interface on top of which the DB server specific implement 
//! their specifics. 
//! 
//! This class thus serves as an interface guideline for the DB server 
//! specific connection classes. 
//! 
//! @section Untyped and typed mode 
//! 
//!   The query results are returned in different ways depending on the 
//!   query functions used: The @tt{..typed_query@} functions select 
//!   typed mode, while the other query functions uses the older untyped 
//!   mode. 
//! 
//!   @ul 
//!     @item 
//!       In untyped mode, all values except SQL NULL are returned as 
//!       strings in their display representation, and SQL NULL is returned 
//!       as zero. 
//!     @item 
//!       In typed mode, values are returned in pike native form where it 
//!       works well. That means at least that SQL integer fields are 
//!       returned as pike integers, floats as floats, SQL NULL as 
//!       @[Val.null], and of course strings still as strings. The 
//!       representation of other SQL types depend on the capabilities of 
//!       the server specific backends. It's also possible that floats in 
//!       some cases are represented in other ways if too much precision is 
//!       lost in the conversion to pike floats. 
//!   @endul 
//! 
//! @endsection 
//! 
//! @note 
//!   For historical reasons, there may be server specific backends that 
//!   operate differently from what is described here, e.g. some that 
//!   return a bit of typed data in untyped mode. 
//! 
//! @note 
//!   Typed operation was not supported at all prior to Pike 7.8.363, 
//!   and may not be supported for all databases. 
 
#define ERROR(X ...)  predef::error(X) 
 
//! Compatibility interface. 
//! 
//! This used to be a variable containing the server specific 
//! connection object used for the actual SQL queries. 
//! 
//! As the wrapper no longer exists, this symbol now 
//! just evaluates to the object. 
__deprecated__ this_program `master_sql() 
{ 
  return this; 
} 
 
//! Convert all field names in mappings to @[lower_case]. 
//! Only relevant to databases which only implement @[big_query()], 
//! and use upper/mixed-case fieldnames (eg Oracle). 
//! @int 
//! @value 0 
//!   No (default) 
//! @value 1 
//!   Yes 
//! @endint 
int(0..1) case_convert; 
 
//! @decl string quote(string s) 
//! Quote a string @[s] so that it can safely be put in a query. 
//! 
//! All input that is used in SQL-querys should be quoted to prevent 
//! SQL injections. 
//! 
//! Consider this harmfull code: 
//! @code 
//!   string my_input = "rob' OR name!='rob"; 
//!   string my_query = "DELETE FROM tblUsers WHERE name='"+my_input+"'"; 
//!   my_db->query(my_query); 
//! @endcode 
//! 
//! This type of problems can be avoided by quoting @tt{my_input@}. 
//! @tt{my_input@} would then probably read something like 
//! @i{rob\' OR name!=\'rob@} 
//! 
//! Usually this is done - not by calling @[quote] explicitly - but through 
//! using a @[sprintf] like syntax: 
//! @code 
//!   string my_input = "rob' OR name!='rob"; 
//!   my_db->query("DELETE FROM tblUsers WHERE name=%s", my_input); 
//! @endcode 
//! 
//! The default implementation quotes single quotes by doubling them. 
string quote(string s) 
{ 
  return replace(s, "\'", "\'\'"); 
} 
 
private constant timezone = localtime (0)->timezone; 
 
//! Converts a system time value to an appropriately formatted time 
//! spec for the database. 
//! 
//! @param time 
//!   Time to encode. 
//! 
//! @param date 
//!   If nonzero then @[time] is taken as a "full" unix time spec 
//!   (where the date part is ignored), otherwise it's converted as a 
//!   seconds-since-midnight value. 
//! 
//! The default implementation returns a colon-separated ISO 9601 time. 
string encode_time(int time, int|void date) 
{ 
  if (date) { 
    if (!time) return "00:00:00"; 
    mapping(string:int) ct = localtime (time); 
    return sprintf("%02d:%02d:%02d", ct->hour, ct->min, ct->sec); 
  } 
  else 
    return sprintf("%02d:%02d:%02d", 
                   (time / 3600) % 24, 
                   (time / 60) % 60, 
                   time % 60); 
} 
 
//! Converts a database time spec to a system time value. 
//! 
//! @param timestr 
//!   Time spec to decode. 
//! 
//! @param date 
//!   Take the date part from this system time value. If zero, a 
//!   seconds-since-midnight value is returned. 
//! 
//! @returns 
//!   Returns the number of seconds since midnight. 
int decode_time(string timestr, int|void date) 
{ 
  int hour = 0, min = 0, sec = 0; 
  if (sscanf(timestr, "%d:%d:%d", hour, min, sec) <= 1) 
    sscanf(timestr, "%2d%2d%2d", hour, min, sec); 
  if (date && (hour || min || sec)) { 
    mapping(string:int) ct = localtime(date); 
    return mktime(sec, min, hour, 
                  ct->mday, ct->mon, ct->year, 
                  ct->isdst, ct->timezone); 
  } 
  else return (hour * 60 + min) * 60 + sec; 
} 
 
//! Converts a system time value to an appropriately formatted 
//! date-only spec for the database. 
//! 
//! @param t 
//!   Time to encode. 
//! 
//! The default implementation returns an ISO 9601 date. 
string encode_date(int time) 
{ 
  if (!time) return "0000-00-00"; 
  mapping(string:int) ct = localtime (time); 
  return sprintf("%04d-%02d-%02d", ct->year + 1900, ct->mon + 1, ct->mday); 
} 
 
//! Converts a database date-only spec to a system time value. 
//! 
//! @param datestr 
//!   Date spec to decode. 
//! 
//! @returns 
//!   Returns the number of seconds since 1970-01-01T00:00:00 UTC 
//!   to 00:00:00 at the specified date in the current timezone. 
int decode_date(string datestr) 
{ 
  int year = 0, mon = 0, mday = 0, n; 
  n = sscanf (datestr, "%d-%d-%d", year, mon, mday); 
  if (n <= 1) n = sscanf (datestr, "%4d%2d%2d", year, mon, mday); 
  if (year || mon || mday) 
    return mktime (0, 0, 0, 
                   n == 3 ? mday : 1, n >= 2 && mon - 1, year - 1900, 
                   -1, timezone); 
  else return 0; 
} 
 
//! Converts a system time value to an appropriately formatted 
//! date and time spec for the database. 
//! 
//! @param time 
//!   Time to encode. 
//! 
//! The default implementation returns an ISO 9601 timestamp. 
string encode_datetime(int time) 
{ 
  if (!time) return "0000-00-00T00:00:00"; 
  mapping(string:int) ct = localtime (time); 
  return sprintf ("%04d-%02d-%02dT%02d:%02d:%02d", 
                  ct->year + 1900, ct->mon + 1, ct->mday, 
                  ct->hour, ct->min, ct->sec); 
} 
 
//! Converts a database date and time spec to a system time value. 
//! 
//! @param datetime 
//!   Date and time spec to decode. 
//! 
//! @returns 
//!   Returns the number of seconds since 1970-01-01T00:00:00 UTC 
//!   to the specified date and time in the current timezone. 
//! 
//! The default implementation decodes an ISO 9601 timestamp. 
int decode_datetime(string datetime) 
{ 
  array(string) a = datetime / "T"; 
  if (sizeof (a) == 2) 
    return decode_date(a[0]) + decode_time(a[1]); 
 
  a = datetime / " "; 
  if (sizeof (a) == 2) 
    return decode_date(a[0]) + decode_time(a[1]); 
 
  int n = sizeof(datetime); 
  if (n >= 12) 
    return decode_date(datetime[..n-7]) + decode_time(datetime[n-6..n-1]); 
  else 
    return decode_date(datetime); 
} 
 
//! @decl void create(string host) 
//! @decl void create(string host, string db) 
//! @decl void create(string host, mapping(string:int|string) options) 
//! @decl void create(string host, string db, string user) 
//! @decl void create(string host, string db, string user, @ 
//!                   string password) 
//! @decl void create(string host, string db, string user, @ 
//!                   string password, mapping(string:int|string) options) 
//! 
//! Create a new SQL connection. 
//! 
//! @param host 
//!   @mixed 
//!     @type string 
//!       Connect to the server specified. 
//!     @type int(0..0) 
//!       Access through a UNIX-domain socket or similar. 
//!   @endmixed 
//! 
//! @param db 
//!   Select this database. 
//! 
//! @param user 
//!   User name to access the database as. 
//! 
//! @param password 
//!   Password to access the database. 
//! 
//! @param options 
//!   Optional mapping of options. 
//!   See the SQL-database documentation for the supported options. 
//!   (eg @[Mysql.mysql()->create()]). 
//! 
//! @note 
//!   Support for @[options] was added in Pike 7.3. 
//! 
//! @note 
//!   The base class (@[__builtin.Sql.Connection]) only has a prototype. 
void create(string host, void|string|mapping(string:int|string) db, 
            void|string user, void|string _password, 
            void|mapping(string:int|string) options); 
 
//! Returns true if the connection seems to be open. 
//! 
//! @note 
//!   This function only checks that there's an open connection, 
//!   and that the other end hasn't closed it yet. No data is 
//!   sent over the connection. 
//! 
//!   For a more reliable check of whether the connection 
//!   is alive, please use @[ping()]. 
//! 
//! @note 
//!   The default implementation just returns the value @expr{1@}. 
//! 
//! @seealso 
//!   @[ping()] 
int is_open() 
{ 
  return 1; 
} 
 
//! @decl int ping() 
//! 
//! Check whether the connection is alive. 
//! 
//! @returns 
//!   Returns one of the following: 
//!   @int 
//!     @value 0 
//!       Everything ok. 
//!     @value 1 
//!       The connection reconnected automatically. 
//!     @value -1 
//!       The server has gone away, and the connection is dead. 
//!   @endint 
//! 
//! The default implementation performs a trivial select to 
//! check the connection. 
//! 
//! @seealso 
//!   @[is_open()] 
int ping() 
{ 
  if (!is_open()) return -1; 
  catch { 
    return sizeof(query("SELECT 0 AS zero") || ({})) - 1; 
  }; 
  return -1; 
} 
 
void set_charset (string charset) 
//! Changes the charset that the connection uses for queries and 
//! returned text strings. 
//! 
//! @param charset 
//!   The charset to use. The valid values and their meanings depends 
//!   on the database brand. However, the special value 
//!   @expr{"unicode"@} (if supported) selects a mode where the query 
//!   and result strings are unencoded (and possibly wide) unicode 
//!   strings. 
//! 
//! @throws 
//!   An error is thrown if the connection doesn't support the 
//!   specified charset, or doesn't support charsets being set this 
//!   way at all. 
//! 
//! @note 
//!   See the @expr{set_charset@} functions for each database 
//!   connection type for further details about the effects on the 
//!   connection. 
//! 
//! @seealso 
//!   @[get_charset], @[Sql.mysql.set_charset] 
{ 
  predef::error("This database connection does not " 
                "support charset switching.\n"); 
} 
 
string get_charset() 
//! Returns the (database dependent) name of the charset used for (at 
//! least) query strings. Returns zero if the connection doesn't 
//! support charsets this way (typically means that a call to 
//! @[set_charset] will throw an error). 
//! 
//! @seealso 
//!   @[set_charset], @[Sql.mysql.get_charset] 
{ 
  return UNDEFINED; 
} 
 
protected array(mapping(string:mixed)) res_obj_to_array(.Result res_obj) 
{ 
  if (!res_obj) 
    return 0; 
 
  array(mapping(string:mixed)) res = ({}); 
  while (res_obj) 
  { 
    // Not very efficient, but sufficient 
    array(string) fieldnames; 
    array(mixed) row; 
 
    array(mapping) fields = res_obj->fetch_fields(); 
    if(!sizeof(fields)) return ({}); 
 
    int has_table = fields[0]->table && fields[0]->table!=""; 
 
    if(has_table) 
      fieldnames = (map(fields, 
                        lambda (mapping(string:mixed) m) { 
                          return (m->table||"") + "." + m->name; 
                        }) + 
                    fields->name); 
    else 
      fieldnames = fields->name; 
 
    if (case_convert) 
      fieldnames = map(fieldnames, lower_case); 
 
    if(has_table) 
      while (row = res_obj->fetch_row()) 
        res += ({ mkmapping(fieldnames, row + row) }); 
    else 
      while (row = res_obj->fetch_row()) 
        res += ({ mkmapping(fieldnames, row) }); 
 
    // Try the next result. 
    res_obj = res_obj->next_result && res_obj->next_result(); 
  } 
  return res; 
} 
 
//! Return last error message. 
int|string error() 
{ 
  return "Unknown error"; 
} 
 
//! Return last SQLSTATE. 
//! 
//! The SQLSTATE error codes are specified in ANSI SQL. 
string sqlstate() 
{ 
  return "IM001"; // "driver does not support this function" 
} 
 
//! Select database to access. 
void select_db(string db); 
 
//! Compiles the query (if possible). Otherwise returns it as is. 
//! The resulting object can be used multiple times to the query 
//! functions. 
//! 
//! @param q 
//!   SQL-query to compile. 
//! 
//! @note 
//!   The default implementation just returns @[q] unmodified. 
//! 
//! @seealso 
//! @[query], @[typed_query], @[big_query], @[big_typed_query], 
//! @[streaming_query], @[streaming_typed_query] 
string|object compile_query(string q) 
{ 
  return q; 
} 
 
//! Build a raw SQL query, given the cooked query and the variable bindings 
//! It's meant to be used as an emulation engine for those drivers not 
//! providing such a behaviour directly (i.e. Oracle). 
//! The raw query can contain some variables (identified by prefixing 
//! a colon to a name or a number (i.e. ":var" or  ":2"). They will be 
//! replaced by the corresponding value in the mapping. 
//! 
//! @param query 
//!   The query. 
//! 
//! @param bindings 
//!   Mapping containing the variable bindings. Make sure that 
//!   no confusion is possible in the query. If necessary, change the 
//!   variables' names. 
protected string emulate_bindings(string query, 
                                  mapping(string|int:mixed) bindings) 
{ 
  array(string)k, v; 
  v = map(values(bindings), 
          lambda(mixed m) { 
            if(undefinedp(m)) 
              return "NULL"; 
            if (objectp (m) && m->is_val_null) 
              // Note: Could need bug compatibility here - in some cases 
              // we might be passed a null object that can be cast to 
              // "", and before this it would be. This is an observed 
              // compat issue in comment #7 in [bug 5900]. 
              return "NULL"; 
            if(multisetp(m)) 
              return sizeof(m) ? indices(m)[0] : ""; 
            return "'"+(intp(m)?(string)m:quote((string)m))+"'"; 
          }); 
  // Throws if mapping key is empty string. 
  k = map(indices(bindings),lambda(string s){ 
                              return ( (stringp(s)&&s[0]==':') ? 
                                       s : ":"+s); 
                            }); 
  return replace(query,k,v); 
} 
 
//! Handle @[sprintf]-based quoted arguments 
//! 
//! @param query 
//!   The query as sent to one of the query functions. 
//! 
//! @param extraargs 
//!   The arguments following the query. 
//! 
//! @returns 
//!   Returns an array with two elements: 
//!   @array 
//!     @elem string 0 
//!       The query altered to use bindings-syntax. 
//!     @elem mapping(string|int:mixed) 1 
//!       A bindings mapping. Not present if no bindings were added. 
//!   @endarray 
protected array(string|mapping(string|int:mixed)) 
  handle_extraargs(string query, array(mixed) extraargs) 
{ 
  array(mixed) args = allocate(sizeof(extraargs)); 
  mapping(string:mixed) bindings = ([]); 
 
  int a; 
  foreach(extraargs; int j; mixed s) { 
    if (stringp(s) || multisetp(s)) { 
      string bind_name = ":arg"+(a++); 
      args[j] = bind_name; 
      bindings[bind_name] = s; 
      continue; 
    } 
    if (intp(s) || floatp(s)) { 
      args[j] = s || .zero; 
      continue; 
    } 
    if (objectp(s) && s->is_val_null) { 
      args[j] = .null_arg; 
      continue; 
    } 
    ERROR("Wrong type to query argument %d: %O\n", j + 1, s); 
  } 
 
  query = sprintf(query, @args); 
 
  if (sizeof(bindings)) return ({ query, bindings }); 
  return ({ query }); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in untyped mode. 
//! 
//! @param q 
//!   Query to send to the SQL-server. This can either be a string with the 
//!   query, or a previously compiled query (see @[compile_query()]). 
//! 
//! @returns 
//!   The result is returned as an @[Sql.sql_result] object in untyped 
//!   mode. This allows for having some more info about the result as 
//!   well as processing the result in a streaming fashion, although the 
//!   result itself wasn't obtained streamingly from the server. 
//! 
//!   Returns @expr{0@} if the query didn't return any result 
//!   (e.g. @tt{INSERT@} or similar). 
//! 
//! @throws 
//!   Throws an exception if the query fails. 
//! 
//! Called by other variants of @[big_query()] after they have processed 
//! their extra arguments. 
//! 
//! This prototype function is the base variant and is intended to be 
//! overloaded by actual drivers. 
//! 
//! @note 
//! Despite the name, this function is not only useful for "big" 
//! queries. It typically has less overhead than @[query] also for 
//! ones that return only a few rows. 
//! 
//! @seealso 
//!   @[query], @[streaming_query] 
int|.Result big_query(object|string q); 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in untyped mode. 
//! 
//! @param q 
//!   Query to send to the SQL-server. This can either be a string with the 
//!   query, or a previously compiled query (see @[compile_query()]). 
//! 
//! @param bindings 
//!   A mapping containing bindings of variables used in the query. 
//!   A variable is identified by a colon (:) followed by a name or number. 
//!   Each index in the mapping corresponds to one such variable, and the 
//!   value for that index is substituted (quoted) into the query wherever 
//!   the variable is used. 
//! 
//! @code 
//! res = query("SELECT foo FROM bar WHERE gazonk=:baz", 
//!             ([":baz":"value"])); 
//! @endcode 
//! 
//!   Binary values (BLOBs) may need to be placed in multisets. 
//! 
//! @returns 
//!   The result is returned as an @[Sql.sql_result] object in untyped 
//!   mode. This allows for having some more info about the result as 
//!   well as processing the result in a streaming fashion, although the 
//!   result itself wasn't obtained streamingly from the server. 
//! 
//!   Returns @expr{0@} if the query didn't return any result 
//!   (e.g. @tt{INSERT@} or similar). 
//! 
//! @throws 
//!   Throws an exception if the query fails. 
//! 
//! Calls the base variant of @[big_query()] after having inserted 
//! the bindings into the query (using @[emulate_bindings()]). 
//! 
//! Drivers that actually support bindings should overload this 
//! variant in addition to the base variant. 
//! 
//! @note 
//! Despite the name, this function is not only useful for "big" 
//! queries. It typically has less overhead than @[query] also for 
//! ones that return only a few rows. 
//! 
//! @seealso 
//!   @[query], @[emulate_bindings], @[streaming_query] 
variant int|.Result big_query(object|string q, 
                              mapping(string|int:mixed) bindings) 
{ 
  return big_query(emulate_bindings(q, bindings)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in untyped mode. 
//! 
//! @param q 
//!   Query to send to the SQL-server. This can either be a string with the 
//!   query, or a previously compiled query (see @[compile_query()]). 
//! 
//! @param extraarg 
//! @param extraargs 
//!   Arguments as you would use in sprintf. They are automatically 
//!   quoted. 
//! 
//! @code 
//! res = query("select foo from bar where gazonk=%s","value"); 
//! @endcode 
//! 
//! The result is returned as an @[Sql.sql_result] object in untyped 
//! mode. This allows for having some more info about the result as 
//! well as processing the result in a streaming fashion, although the 
//! result itself wasn't obtained streamingly from the server. Returns 
//! @expr{0@} if the query didn't return any result (e.g. @tt{INSERT@} 
//! or similar). 
//! 
//! The default implementation normalizes @[q] and @[extraargs] to 
//! use the bindings mapping (via @[handle_extraargs()]), and calls 
//! one of the other variants of @[big_query()] with the result. 
//! 
//! @note 
//! Despite the name, this function is not only useful for "big" 
//! queries. It typically has less overhead than @[query] also for 
//! ones that return only a few rows. 
//! 
//! @seealso 
//!   @[query], @[handle_extraargs], @[streaming_query] 
variant int|.Result big_query(object|string q, 
                              string|multiset|int|float|object extraarg, 
                              string|multiset|int|float|object ... extraargs) 
{ 
  return big_query(@handle_extraargs(q, ({ extraarg }) + extraargs)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in untyped mode. 
//! 
//! For the arguments, please see the @[big_query()] function. 
//! 
//! @returns 
//!   Returns one of the following on success: 
//!   @mixed 
//!     @type array(mapping(string:string)) 
//!       The result as an array of mappings indexed on the name of 
//!       the columns. The values are either strings with the display 
//!       representations or zero for the SQL NULL value. 
//!     @type zero 
//!       The value @expr{0@} (zero) if the query didn't return any 
//!       result (eg @tt{INSERT@} or similar). 
//!   @endmixed 
//! 
//! @throws 
//!   Throws an exception if the query fails. 
//! 
//! @note 
//!   The default implementation calls @[big_query()] 
//!   and converts its result. 
//! 
//! @seealso 
//!   @[typed_query], @[big_query], @[streaming_query] 
array(mapping(string:string)) query(object|string q, 
                                    mixed ... extraargs) 
{ 
  return res_obj_to_array(big_query(q, @extraargs));   
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in typed mode. 
//! 
//! For the argument, please see the @[big_query()] function. 
//! 
//! The result is returned as an @[Sql.sql_result] object in typed 
//! mode. This allows for having some more info about the result as 
//! well as processing the result in a streaming fashion, although the 
//! result itself wasn't obtained streamingly from the server. Returns 
//! @expr{0@} if the query didn't return any result (e.g. @tt{INSERT@} 
//! or similar). 
//! 
//! @note 
//!   Typed mode is not supported by all sql databases. If not 
//!   supported, an error is thrown. 
//! 
//! @note 
//! Despite the name, this function is not only useful for "big" 
//! queries. It typically has less overhead than @[typed_query] also 
//! for ones that return only a few rows. 
//! 
//! Called by @[big_typed_query()] after it has processed any bindings. 
//! 
//! This prototype function is intended for overriding by drivers. 
//! 
//! @seealso 
//!   @[query], @[typed_query], @[big_query], @[streaming_query] 
int|.Result big_typed_query(object|string q); 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in typed mode. 
//! 
//! For the arguments, please see the @[big_query()] function. 
//! 
//! The result is returned as an @[Sql.sql_result] object in typed 
//! mode. This allows for having some more info about the result as 
//! well as processing the result in a streaming fashion, although the 
//! result itself wasn't obtained streamingly from the server. Returns 
//! @expr{0@} if the query didn't return any result (e.g. @tt{INSERT@} 
//! or similar). 
//! 
//! @note 
//!   Typed mode is not supported by all sql databases. If not 
//!   supported, an error is thrown. 
//! 
//! @note 
//! Despite the name, this function is not only useful for "big" 
//! queries. It typically has less overhead than @[typed_query] also 
//! for ones that return only a few rows. 
//! 
//! Called by @[big_typed_query()] after it has processed any bindings. 
//! 
//! @seealso 
//!   @[query], @[typed_query], @[big_query], @[streaming_query] 
variant int|.Result big_typed_query(object|string q, 
                                    mapping(string|int:mixed) bindings) 
{ 
  return big_typed_query(emulate_bindings(q, bindings)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in typed mode. 
//! 
//! For the arguments, please see the @[big_query()] function. 
//! 
//! The result is returned as an @[Sql.sql_result] object in typed 
//! mode. This allows for having some more info about the result as 
//! well as processing the result in a streaming fashion, although the 
//! result itself wasn't obtained streamingly from the server. Returns 
//! @expr{0@} if the query didn't return any result (e.g. @tt{INSERT@} 
//! or similar). 
//! 
//! @note 
//!   Typed mode is not supported by all sql databases. If not 
//!   supported, an error is thrown. 
//! 
//! @note 
//! Despite the name, this function is not only useful for "big" 
//! queries. It typically has less overhead than @[typed_query] also 
//! for ones that return only a few rows. 
//! 
//! @seealso 
//!   @[query], @[typed_query], @[big_query], @[streaming_query] 
variant int|.Result big_typed_query(object|string q, 
                                    string|multiset|int|float|object extraarg, 
                                    string|multiset|int|float|object ... extraargs) 
{ 
  return big_typed_query(@handle_extraargs(q, ({ extraarg }) + extraargs)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results in typed mode. 
//! 
//! For the arguments, please see the @[query()] function. 
//! 
//! @returns 
//!   Returns one of the following on success: 
//!   @mixed 
//!     @type array(mapping(string:mixed)) 
//!       The result as an array of mappings indexed on the name of 
//!       the columns. The values have the appropriate native pike 
//!       types where they fit the SQL data types - see the class doc 
//!       for details on typed mode. 
//!     @type zero 
//!       The value @expr{0@} (zero) if the query didn't return any 
//!       result (eg @tt{INSERT@} or similar). 
//!   @endmixed 
//! 
//! @note 
//!   Typed mode is not supported by all sql databases. If not 
//!   supported, an error is thrown. 
//! 
//! @note 
//!   The default implementation calls @[big_typed_query()] 
//!   and converts its result. 
//! 
//! @seealso 
//!   @[query], @[big_typed_query] 
array(mapping(string:mixed)) typed_query(object|string q, mixed ... extraargs) 
{ 
  return res_obj_to_array(big_typed_query(q, @extraargs));   
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results streaming in untyped mode. 
//! 
//! For the arguments, please see the @[low_big_query()] function. 
//! 
//! The result is returned as a streaming @[Sql.sql_result] object in 
//! untyped mode. This allows for having results larger than the 
//! available memory, and returning some more info about the result. 
//! Returns @expr{0@} if the query didn't return any result (e.g. 
//! INSERT or similar). For the other arguments, they are the same as 
//! for the @[query()] function. 
//! 
//! Called by @[streaming_query()] after it has processed any bindings. 
//! 
//! This variant function is intended for overriding by drivers. 
//! 
//! @note 
//!   Streaming operation is not supported by all sql databases. 
//!   The default implementation falls back to calling @[big_query()]. 
//! 
//! @seealso 
//!   @[big_query], @[streaming_typed_query] 
int|.Result streaming_query(object|string q) 
{ 
  return big_query(q); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results streaming in untyped mode. 
//! 
//! For the arguments, please see the @[low_big_query()] function. 
//! 
//! The result is returned as a streaming @[Sql.sql_result] object in 
//! untyped mode. This allows for having results larger than the 
//! available memory, and returning some more info about the result. 
//! Returns @expr{0@} if the query didn't return any result (e.g. 
//! INSERT or similar). For the other arguments, they are the same as 
//! for the @[query()] function. 
//! 
//! Called by @[streaming_query()] after it has processed any bindings. 
//! 
//! This variant function is intended for overriding by drivers that 
//! actually implement bindings. 
//! 
//! @note 
//!   Streaming operation is not supported by all sql databases. 
//!   The default implementation falls back to calling @[big_query()]. 
//! 
//! @seealso 
//!   @[big_query], @[streaming_typed_query] 
variant int|.Result streaming_query(object|string q, 
                                    mapping(string:mixed) bindings) 
{ 
  return streaming_query(emulate_bindings(q, bindings)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results streaming in untyped mode. 
//! 
//! For the arguments, please see the @[query()] function. 
//! 
//! The result is returned as a streaming @[Sql.sql_result] object in 
//! untyped mode. This allows for having results larger than the 
//! available memory, and returning some more info about the result. 
//! Returns @expr{0@} if the query didn't return any result (e.g. 
//! INSERT or similar). For the other arguments, they are the same as 
//! for the @[query()] function. 
//! 
//! @note 
//!   Streaming operation is not supported by all sql databases. 
//!   If not supported, this function will fall back to calling 
//!   @[big_query()]. 
//! 
//! @seealso 
//!   @[big_query], @[streaming_typed_query] 
variant int|.Result streaming_query(object|string q, 
                                    string|multiset|int|float|object extraarg, 
                                    string|multiset|int|float|object ... extraargs) 
{ 
  return streaming_query(@handle_extraargs(q, ({ extraarg }) + extraargs)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results streaming in typed mode. 
//! 
//! For the arguments, please see the @[query()] function. 
//! 
//! The result is returned as a streaming @[Sql.sql_result] object in 
//! typed mode. This allows for having results larger than the 
//! available memory, and returning some more info about the result. 
//! Returns @expr{0@} if the query didn't return any result (e.g. 
//! INSERT or similar). 
//! 
//! 
//! Called by @[streaming_query()] after it has processed any bindings. 
//! 
//! This function is intended for overriding by drivers. 
//! 
//! @note 
//!   Neither streaming operation nor typed results are supported 
//!   by all sql databases. The default implentation falls back to 
//!   calling @[big_typed_query()]. 
//! 
//! @seealso 
//!   @[streaming_query], @[big_typed_query] 
int|.Result streaming_typed_query(object|string q) 
{ 
  return big_typed_query(q); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results streaming in typed mode. 
//! 
//! For the arguments, please see the @[query()] function. 
//! 
//! The result is returned as a streaming @[Sql.sql_result] object in 
//! typed mode. This allows for having results larger than the 
//! available memory, and returning some more info about the result. 
//! Returns @expr{0@} if the query didn't return any result (e.g. 
//! INSERT or similar). 
//! 
//! 
//! Called by @[streaming_query()] after it has processed any bindings. 
//! 
//! This function is intended for overriding by drivers. 
//! 
//! @note 
//!   Neither streaming operation nor typed results are supported 
//!   by all sql databases. The default implentation falls back to 
//!   calling @[low_big_typed_query()]. 
//! 
//! @seealso 
//!   @[streaming_query], @[big_typed_query] 
variant int|.Result streaming_typed_query(object|string q, 
                                          mapping(string|int:mixed) bindings) 
{ 
  return streaming_typed_query(emulate_bindings(q, bindings)); 
} 
 
//! Send an SQL query synchronously to the SQL-server and return 
//! the results streaming in typed mode. 
//! 
//! For the arguments, please see the @[query()] function. 
//! 
//! The result is returned as a streaming @[Sql.sql_result] object in 
//! typed mode. This allows for having results larger than the 
//! available memory, and returning some more info about the result. 
//! Returns @expr{0@} if the query didn't return any result (e.g. 
//! INSERT or similar). 
//! 
//! @note 
//!   Neither streaming operation nor typed results are supported 
//!   by all sql databases. If not supported, this function will 
//!   fall back to calling @[big_typed_query()]. 
//! 
//! @seealso 
//!   @[streaming_query], @[big_typed_query] 
variant int|.Result streaming_typed_query(object|string q, 
                                          string|multiset|int|float|object extraarg, 
                                          string|multiset|int|float|object ... extraargs) 
{ 
  return streaming_typed_query(@handle_extraargs(q, ({ extraarg }) + extraargs)); 
} 
 
//! Create a new database. 
//! 
//! @param db 
//!   Name of database to create. 
void create_db(string db); 
 
//! Drop database 
//! 
//! @param db 
//!   Name of database to drop. 
void drop_db(string db); 
 
//! Shutdown a database server. 
void shutdown() 
{ 
  ERROR("Not supported by this database.\n"); 
} 
 
//! Reload the tables. 
void reload() 
{ 
  // Probably safe to make this a NOOP 
} 
 
//! Return info about the current SQL-server. 
string server_info() 
{ 
  return "Unknown SQL-server"; 
} 
 
//! Return info about the connection to the SQL-server. 
string host_info() 
{ 
  return "Unknown connection to host"; 
} 
 
//! List available databases on this SQL-server. 
//! 
//! @returns 
//!   Returns an array with database names on success and @expr{0@} 
//!   (zero) on failure. 
//! 
//! Called by @[list_dbs()]. 
//! 
//! This function is intended for overriding by drivers 
//! not supporting wildcard filtering of database names. 
//! 
//! @note 
//!   The default implementation attempts the query 
//!   @expr{"SHOW DATABASES"@}. 
//! 
//! @seealso 
//!   @[list_dbs()] 
protected array(string) low_list_dbs() 
{ 
  catch { 
    array(mapping) res = query("SHOW DATABASES"); 
    if (res && sizeof(res) && mappingp(res[0])) { 
      return map(res, lambda (mapping m) { 
                        return values(m)[0]; // Hope that there's only one field 
                      } ); 
    } 
    return res && ({}); 
  }; 
 
  return 0; 
} 
 
//! List available databases on this SQL-server. 
//! 
//! @param wild 
//!   Optional wildcard to match against. 
//! 
//! This function calls @[low_list_dbs()] and optionally 
//! performs wildcard filtering. 
//! 
//! @seealso 
//!   @[low_list_dbs()] 
array(string) list_dbs(string|void wild) 
{ 
  array(string) res = list_dbs(); 
 
  if (res && wild) { 
    res = filter(res, 
                 Regexp(replace(wild, ({"%", "_"}), ({".*", "."})))->match); 
  } 
  return res; 
} 
 
//! List tables available in the current database. 
//! 
//! This function is intended for overriding by drivers 
//! not supporting wildcard filtering of table names. 
//! 
//! @note 
//!   The default implementation attempts the query 
//!   @expr{"SHOW TABLES"@}. 
//! 
//! @seealso 
//!   @[list_tables()] 
protected array(string) low_list_tables() 
{ 
  array(string)|array(mapping(string:mixed))|object res; 
 
  catch { 
    array(mapping(string:mixed)) res = query("SHOW TABLES"); 
 
    if (res && sizeof(res) && mappingp(res[0])) { 
      string col_name = indices(res[0])[0]; 
      if (sizeof(res[0]) > 1) { 
        if (!zero_type(res[0]["TABLE_NAME"])) { 
          col_name = "TABLE_NAME"; 
        } 
      } 
      return map(res, lambda (mapping m, string col_name) { 
                        return m[col_name]; 
                      }, col_name); 
    } 
    return res && ({}); 
  }; 
 
  return 0; 
} 
 
//! List tables available in the current database. 
//! 
//! @param wild 
//!   Optional wildcard to match against. 
//! 
//! The default implementation calls @[low_list_tables()]. 
array(string) list_tables(string|void wild) 
{ 
  array(string) res = low_list_tables(); 
 
  if (res && wild) { 
    res = filter(res, 
                 Regexp(replace(wild, ({"%", "_"}), ({".*", "."})))->match); 
  } 
  return res; 
} 
 
//! List fields available in the specified table 
//! 
//! @param table 
//!   Table to list the fields of. 
//! 
//! @returns 
//!   Returns an array of mappings with at least the fields: 
//!   @mapping 
//!     @member string "name" 
//!       The name of the field. 
//!     @member string "table" 
//!       The name of the table. 
//!   @endmapping 
//! 
//!   Typically there are also entries for the field types, 
//!   field widths and nullability. 
//! 
//! This function is intended for overriding by drivers 
//! not supporting wildcard filtering of field names. 
//! 
//! @note 
//!   The default implementation attempts the query 
//!   @expr{"SHOW FIELDS FROM 'table'"@}, and then 
//!   performs some normalization of the result. 
//! 
//! @seealso 
//!   @[list_fields()] 
protected array(mapping(string:mixed)) low_list_fields(string table) 
{ 
 
  catch { 
    array(mapping(string:mixed)) res = 
      query("SHOW FIELDS FROM \'" + table + "\'"); 
 
    res = res && map(res, 
                     lambda (mapping m, string table) { 
                       foreach(indices(m), string str) { 
                         // Add the lower case variants 
                         string low_str = lower_case(str); 
                         if (low_str != str && !m[low_str]) 
                           m[low_str] = m_delete(m, str); 
                       } 
 
                       if ((!m->name) && m->field) 
                         m["name"] = m_delete(m, "field"); 
 
                       if (!m->table) 
                         m["table"] = table; 
 
                       return m; 
                     }, table); 
    return res; 
  }; 
  return 0; 
} 
 
//! List fields available in the specified table 
//! 
//! @param table 
//!   Table to list the fields of. 
//! 
//! @param wild 
//!   Optional wildcard to match against. 
//! 
//! The default implementation calls @[low_list_fields()] 
//! and applies the wild-card filter on the result. 
array(mapping(string:mixed)) list_fields(string table, string|void wild) 
{ 
  array(mapping(string:mixed)) res = low_list_fields(table); 
 
  if (res && wild) { 
    res = 
      filter(res, 
             map(res->name, 
                 Regexp(replace(wild, ({"%", "_"}), ({".*", "."})))->match)); 
  } 
 
  return res; 
}