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
  
/* -*- 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. 
*/ 
 
/* All this code is pretty useless if we don't have a msql library...*/ 
#include "msql_config.h" 
 
#ifdef HAVE_MSQL 
 
#include <msql.h> 
 
/* Avoid a redefinition */ 
#ifdef INT_TYPE 
#undef INT_TYPE 
#define MSQL_INT_TYPE   1 
#endif /* INT_TYPE */ 
 
/* #define MSQL_DEBUG 1 */ 
 
#endif /*HAVE_MSQL*/ 
 
#include "module.h" 
#include "pike_compiler.h" 
#include "module_support.h" 
 
#ifdef HAVE_MSQL 
 
#include "threads.h" 
#include "interpret.h" 
#include "builtin_functions.h" 
#include "operators.h" 
#include "pike_types.h" 
 
#ifdef _REENTRANT 
PIKE_MUTEX_T pike_msql_mutex STATIC_MUTEX_INIT; 
#define MSQL_LOCK() mt_lock(&pike_msql_mutex) 
#define MSQL_UNLOCK() mt_unlock(&pike_msql_mutex) 
#else 
#define MSQL_LOCK() /**/ 
#define MSQL_UNLOCK() /**/ 
#endif 
 
#define sp Pike_sp 
 
/*! @module Msql 
 *! This is an interface to the mSQL database server. 
 *! This module may or may not be available on your Pike, depending 
 *! whether the appropriate include and library files (msql.h and libmsql.a 
 *! respectively) could be found at compile-time. Note that you @b{do not@} 
 *! need to have a mSQL server running on your host to use this module: 
 *! you can connect to the database over a TCP/IP socket 
 *! 
 *! Please notice that unless you wish to specifically connect to a mSQL 
 *! server, you'd better use the @[Sql.Sql] program instead. Using @[Sql.Sql] 
 *! ensures that your Pike applications will run with any supported SQL 
 *! server without changing a single line of code. 
 *! 
 *! Also notice that some functions may be mSQL/2.0-specific, and thus missing 
 *! on hosts running mSQL/1.0.* 
 *! 
 *! @note 
 *!  The mSQL C API has some extermal dependencies. 
 *!  They take the form of certain environment variables 
 *!  which, if defined in the environment of the pike interpreter, influence 
 *!  the interface's behavior. Those are "MSQL_TCP_PORT" which forces the 
 *!  server to connect to a port other than the default, "MSQL_UNIX_PORT", same 
 *!  as above, only referring to the UNIX domain sockets. If you built your 
 *!  mSQL server with the default setttings, you shouldn't worry about these. 
 *!  The variable MINERVA_DEBUG can be used to debug the mSQL API (you 
 *!  shouldn't worry about this either). Refer to the mSQL documentation 
 *!  for further details. 
 *! 
 *!  Also note that THIS MODULE USES BLOCKING I/O to connect to the server. 
 *!  mSQL should be reasonably fast, but you might want to consider this 
 *!  particular aspect. It is thread-safe, and so it can be used 
 *!  in a multithread environment. 
 *! 
 *! @fixme 
 *! Although it seems that mSQL/2.0 has some support for server statistics, 
 *! it's really VERY VERY primitive, so it won't be added for now. 
 *! 
 *! @seealso 
 *!   @[Sql.Sql] 
 */ 
 
/*! @class msql 
 */ 
 
DECLARATIONS; 
 
PIKECLASS msql 
{ 
 
static char * decode_msql_type (int msql_type) 
{ 
        switch (msql_type) { 
                case MSQL_INT_TYPE: return "int"; 
                case CHAR_TYPE: return "char"; 
                case REAL_TYPE: return "real"; 
                case IDENT_TYPE: return "ident"; 
                case NULL_TYPE: return "null"; 
#ifdef MSQL_VERSION_2 
                case TEXT_TYPE: return "text"; 
                case UINT_TYPE: return "unsigned int"; 
                case IDX_TYPE: return "index"; 
                case SYSVAR_TYPE: return "sysvar"; 
                case ANY_TYPE: return "any"; 
#endif 
                default: return "unknown"; 
        } 
} 
 
        CVAR int socket; /* the communication socket between us and the database engine. */ 
        CVAR unsigned int db_selected:1; /*flag: if we selected a database*/ 
        CVAR unsigned int connected:1; /*flag: we connected to a server*/ 
        CVAR struct pike_string *error_msg; 
#ifdef MSQL_VERSION_2 
        CVAR int affected; 
#endif 
 
        DECLARE_STORAGE; 
 
        /*! @decl inherit __builtin.Sql.Connection 
         */ 
        INHERIT "__builtin.Sql.Connection"; 
 
INIT 
{ 
        THIS->connected=0; 
        THIS->db_selected=0; 
        THIS->error_msg=NULL; 
} 
 
EXIT 
{ 
        if (THIS->connected) { 
                int socket=THIS->socket; 
                THREADS_ALLOW(); 
                MSQL_LOCK(); 
                msqlClose(socket); 
                MSQL_UNLOCK(); 
                THREADS_DISALLOW(); 
        } 
        if (THIS->error_msg) 
                free_string(THIS->error_msg); 
} 
 
static void report_error (void) 
{ 
        if (THIS->error_msg) 
                free_string(THIS->error_msg); 
        THIS->error_msg=make_shared_string(msqlErrMsg); 
        /* msqlErrMsg is really a char[160] in mSQL/2.0, but I don't want 
                 to take any chances, even if I'm wasting some time here. */ 
} 
 
static void do_select_db(char * dbname) 
{ 
        /* NOTICE: We're assuming we're connected. CHECK before calling! */ 
        int status,socket=THIS->socket; 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        status=msqlSelectDB(socket,dbname); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
 
        if (status==-1) 
        { 
                THIS->db_selected=0; 
                report_error(); 
                Pike_error("Could not select database.\n"); 
        } 
        THIS->db_selected=1; 
} 
 
/*! @decl void shutdown() 
 *! 
 *! This function shuts a SQL-server down. 
 */ 
 
PIKEFUN void shutdown() 
/* Notice: the msqlShutdown() function is undocumented. I'll have to go 
         through the source to find how to report errors.*/ 
{ 
        int status=0,socket=THIS->socket; 
 
        if (!THIS->connected) 
                Pike_error ("Not connected to any server.\n"); 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        status=msqlShutdown(socket); 
        if (status>=0) 
                msqlClose(socket); /*DBserver is shut down, might as well close */ 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        if (status<0) { 
                report_error(); 
                Pike_error ("Error while shutting down the DBserver, connection not closed.\n"); 
        } 
        THIS->connected=0; 
        THIS->db_selected=0; 
} 
 
/*! @decl void reload_acl() 
 *! 
 *! This function forces a server to reload its ACLs. 
 *! 
 *! @note 
 *!  This function is @b{not@} part of the standard interface, so it is 
 *!  @b{not@} available through the @[Sql.Sql] interface, but only through 
 *!  @[Sql.msql] and @[Msql.msql] programs. 
 *! 
 *! @seealso 
 *!   @[create] 
 */ 
 
PIKEFUN void reload_acl() 
/* Undocumented mSQL function. */ 
{ 
        int socket,status=0; 
        if (!THIS->connected) 
                Pike_error ("Not connected to any server.\n"); 
 
        socket=THIS->socket; 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        status=msqlReloadAcls(socket); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        if (status<0) { 
                report_error(); 
                Pike_error ("Could not reload ACLs.\n"); 
        } 
} 
 
/*! @decl void create (void|string dbserver, void|string dbname,@ 
 *!                    void|string username, void|string passwd) 
 *! With one argument, this function 
 *! tries to connect to the specified (use hostname or IP address) database 
 *! server. To connect to a server running on the local host via UNIX domain 
 *! sockets use @expr{"localhost"@}. To connect to the local host via TCP/IP 
 *! sockets 
 *! you have to use the IP address @expr{"127.0.0.1"@}. 
 *! With two arguments it also selects a database to use on the server. 
 *! With no arguments it tries to connect to the server on localhost, using 
 *! UNIX sockets. 
 *! 
 *! @throws 
 *! You need to have a database selected before using the sql-object, 
 *! otherwise you'll get exceptions when you try to query it. 
 *! Also notice that this function @b{can@} raise exceptions if the db 
 *! server doesn't respond, if the database doesn't exist or is not accessible 
 *! by you. 
 *! 
 *! @note 
 *! You don't need bothering about syncronizing the connection to the database: 
 *! it is automatically closed (and the database is sync-ed) when the msql 
 *! object is destroyed. 
 *! 
 *! @seealso 
 *!   @[select_db] 
 */ 
 
  /* 1st arg: hostname or "localhost", 2nd arg: dbname or nothing 
   * CAN raise exception if there is no server listening, or no database 
   * To connect using the UNIX socket instead of a localfunction use the 
   * hostname "localhost", or use no argument. It will use UNIX sockets. 
   * Third and fourth argument are currently ignored, since mSQL doesn't 
   * support user/passwd authorization. The user will be the owner of 
   * the current process. 
   * The first argument can have the format "hostname:port". Since mSQL 
   * doesn't support nonstandard ports, that portion is silently ignored, 
   * and is provided only for generic-interface compliancy 
   */ 
 
PIKEFUN void create(void|string(1..255) dbserver, void|string(1..255) dbname, 
                    void|string(1..255) username, void|string(1..255) passwd, 
                    mapping|int|void options) 
{ 
        struct pike_string * arg1=NULL, *arg2=NULL; 
        int sock, status; 
        char *colon; 
 
        if (dbserver) 
          if (dbserver->len) 
            arg1 = dbserver; 
        if (dbname) 
          if (dbname->len) 
            arg2 = dbname; 
 
        /*Okay. We had one or two arguments, and we must connect to a server 
        and if needed select a database. 
        First off let's check whether we are already connected. In this case, 
        disconnect.*/ 
        if (THIS->connected) 
        { 
                msqlClose (THIS->socket); 
                THIS->connected=0; 
                THIS->db_selected=0; 
        } 
 
        /* msql won' support specifying a port number to connect to. 
         * As far as I know, ':' is not a legal character in an hostname, 
         * so we'll silently ignore it. 
         */ 
        if (arg1) { 
                colon=strchr(arg1->str,':'); 
                if (colon) { 
                  arg1 = make_shared_binary_string(arg1->str, 
                                                   colon - arg1->str); 
                  free_string(sp[-args].u.string); 
                  sp[-args].u.string = arg1; 
                } 
        } 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        /* Warning! If there were no args, we're deferencing a NULL pointer!*/ 
        if (!arg1 || !strcmp (arg1->str,"localhost")) 
                sock=msqlConnect(NULL); 
        else 
                sock=msqlConnect(arg1->str); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
 
        if (sock==-1) { 
                THIS->db_selected=0; 
                THIS->connected=0; 
                report_error(); 
                Pike_error("Error while connecting to mSQL server.\n"); 
        } 
        THIS->socket=sock; 
        THIS->connected=1; 
        if (arg2) 
          do_select_db(arg2->str); 
} 
 
/*! @decl array(string) list_dbs(void|string wild) 
 *! 
 *! Returns an array containing the names of all databases available on 
 *! the system. Will throw an exception if there is no server connected. 
 *! If an argument is specified, it will return only those databases 
 *! whose name matches the given glob. 
 */ 
 
PIKEFUN array(string) list_dbs(string(1..255)|void wild) 
{ 
        m_result * result; 
        m_row row; 
        int fields,numrows=0,socket=THIS->socket; 
 
        if (!THIS->connected) 
                Pike_error ("Not connected.\n"); 
        if (wild && !wild->len) 
          wild = NULL; 
        else { 
                /* We have a glob. We should pop the arg and push it again for a later 
                 * call to glob() */ 
                pop_n_elems(args-1);    /* Paranoia. */ 
                args = 1; 
        } 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        result=msqlListDBs(socket); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
 
        if (!result) { 
                f_aggregate(0); /*empty array if no databases*/ 
                return; 
        } 
        while ((row=msqlFetchRow(result))) /*it's fast, we're in RAM*/ 
        { 
                numrows++; 
                push_text((char *)row[0]); 
        } 
        f_aggregate(numrows); 
        msqlFreeResult(result); 
        if (wild) 
                f_glob(2); 
} 
 
/*! @decl array(string) list_tables(void|string wild) 
 *! 
 *! Returns an array containing the names of all the tables in the currently 
 *! selected database. Will throw an exception if we aren't connected to 
 *! a database. 
 *! If an argument is specified, it will return only those tables 
 *! whose name matches the given glob. 
 */ 
 
PIKEFUN array(string) list_tables(void|string(1..255) wild) 
        /* ARGH! There's much code duplication here... but the subtle differences 
         * between the various functions make it impervious to try to generalize..*/ 
{ 
        m_result * result; 
        m_row row; 
        int fields,numrows=0,socket=THIS->socket; 
 
        if (!THIS->db_selected) 
                Pike_error ("No database selected.\n"); 
 
        if (wild && !wild->len) 
          wild = NULL; 
        else { 
                /* We have a glob. We should pop the arg and push it again for a later 
                 * call to glob() */ 
                pop_n_elems(args-1);    /* Paranoia. */ 
                args = 1; 
        } 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        result=msqlListTables(socket); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
 
        if (!result) { 
                f_aggregate(0); /*empty array if no databases*/ 
                return; 
        } 
        while ((row=msqlFetchRow(result))) 
        { 
                numrows++; 
                push_text((char *)row[0]); 
        } 
        f_aggregate(numrows); 
        msqlFreeResult(result); 
        if (wild) 
                f_glob(2); 
} 
 
/*! @decl void select_db(string dbname) 
 *! 
 *! Before querying a database you have to select it. This can be accomplished 
 *! in two ways: the first is calling the @[create] function with two arguments, 
 *! another is calling it with one or no argument and then calling @[select_db]. 
 *! You can also use this function to change the database you're querying, 
 *! as long as it is on the same server you are connected to. 
 *! 
 *! @throws 
 *! This function CAN raise exceptions in case something goes wrong 
 *! (for example: unexistant database, insufficient permissions, whatever). 
 *! 
 *! @seealso 
 *!  @[create], @[error] 
 */ 
 
/* if no db was selected by connect, does it now. 
 * CAN raise an exception if there's no such database or we haven't selected 
 * an host. 
 */ 
 
PIKEFUN void select_db(string(1..255) dbname) 
{ 
        if (!THIS->connected) 
                Pike_error ("Not connected.\n"); 
        do_select_db(dbname->str); 
} 
 
  /*! @class Result 
   */ 
  PIKECLASS Result 
  { 
    CVAR m_result *result; 
 
    /*! @decl inherit __builtin.Sql.Result 
     */ 
    INHERIT "__builtin.Sql.Result"; 
 
    EXIT 
    { 
      if (THIS->result) { 
        msqlFreeResult(THIS->result); 
      } 
    } 
 
    PIKEFUN int num_rows() 
    { 
      RETURN msqlNumRows(THIS->result); 
    } 
 
    PIKEFUN int num_fields() 
    { 
      RETURN msqlNumFields(THIS->result); 
    } 
 
    PIKEFUN array(mapping(string:mixed)) fetch_fields() 
    { 
      int i; 
      int num_fields = msqlNumFields(THIS->result); 
 
      msqlFieldSeek(THIS->result, 0); 
 
      BEGIN_AGGREGATE_ARRAY(num_fields); 
      for (i = 0; i < num_fields; i++) { 
        m_field *field = msqlFetchField(THIS->result); 
        push_text("name"); 
        push_text(field->name); 
        push_text("table"); 
        push_text(field->table); 
        f_aggregate_mapping(4); 
        DO_AGGREGATE_ARRAY(100); 
      } 
      END_AGGREGATE_ARRAY; 
    } 
 
    PIKEFUN array(string|int(0..0)) fetch_row() 
    { 
      int num_fields = msqlNumFields(THIS->result); 
      m_row row = msqlFetchRow(THIS->result); 
      int i; 
      BEGIN_AGGREGATE_ARRAY(num_fields); 
      for (i = 0; i < num_fields; i++) { 
        if (row[i]) { 
          push_text(row[i]); 
        } else { 
          push_undefined(); 
        } 
        DO_AGGREGATE_ARRAY(100); 
      } 
      END_AGGREGATE_ARRAY; 
    } 
  } 
  /*! @endclass Result 
   */ 
 
/*! @decl variant Result big_query(string(8bit) sqlquery) 
 *! 
 *! This is all you need to query the database. It takes as argument an SQL 
 *! query string (e.g.: "SELECT foo,bar FROM baz WHERE name like '%kinkie%'" 
 *! or "INSERT INTO baz VALUES ('kinkie','made','this')") 
 *! and returns an object containing the returned values. 
 *! 
 *! @throws 
 *! Errors (both from the interface and the SQL server) are reported via 
 *! exceptions, and you definitely want to catch them. Error messages are 
 *! not particularly verbose, since they account only for errors inside the 
 *! driver. To get server-related error messages, you have to use the 
 *! @[error] function. 
 *! 
 *! @note 
 *! Note that if the query is NOT a of SELECT type, but UPDATE or 
 *! MODIFY, the returned value is a @expr{0@} (zero). 
 *! @b{This is not an error@}. Errors are reported @b{only@} via exceptions. 
 *! 
 *! @seealso 
 *!   @[error] 
 */ 
  PIKEFUN object big_query(string(1..255) sqlquery) 
    flags ID_VARIANT; 
  { 
        int status, socket; 
        m_result *result = NULL; 
        struct object *o; 
        struct Msql_msql_Result_struct *res; 
 
        if (!THIS->connected) 
                Pike_error("Must connect to database server before querying it.\n"); 
        if (!THIS->db_selected) 
                Pike_error("Must select database before querying it.\n"); 
 
        socket = THIS->socket; 
 
#ifdef MSQL_DEBUG 
        fprintf(stderr, "MSQLMod: Query is\"%s\"\n", sqlquery->str); 
#endif 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        status = msqlQuery(socket, sqlquery->str); 
 
        if (status != -1) { 
          /*We have what we want. We need to construct the returned structure*/ 
          result=msqlStoreResult(); 
        } 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
 
        if (status==-1) { 
                report_error(); 
                Pike_error("Error in SQL query.\n"); 
        } 
 
#ifdef MSQL_VERSION_2 
        THIS->affected=status; 
#endif 
        if (!result) { 
          push_int(0); 
          return; 
        } 
 
        apply_current(Msql_msql_Result_program_fun_num, 0); 
 
        if ((TYPEOF(Pike_sp[-1]) != PIKE_T_OBJECT) || 
            !(res = get_storage(Pike_sp[-1].u.object, 
                                Msql_msql_Result_program)) || 
            res->result) { 
          msqlFreeResult(result); 
          Pike_error("Bad msql result object!\n"); 
        } 
 
        res->result = result; 
  } 
 
/*! @decl string server_info() 
 *! 
 *! This function returns a string describing the server we are talking 
 *! to. It has the form "servername/serverversion" (like the HTTP protocol 
 *! description) and is most useful in conjunction with the generic SQL-server 
 *! module. 
 *! 
 */ 
 
PIKEFUN string server_info() 
{ 
        char * info; 
 
        pop_n_elems(args); 
        if (!THIS->connected) 
                Pike_error ("Not connected.\n"); 
        push_static_text("msql/"); 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        info=msqlGetServerInfo(); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        push_text(info); 
        f_add(2); 
} 
 
/*! @decl string host_info() 
 *! 
 *! This function returns a string describing what host are we talking to, 
 *! and how (TCP/IP or UNIX sockets). 
 */ 
 
PIKEFUN string host_info() 
{ 
        if (!THIS->connected) 
                Pike_error ("Not connected.\n"); 
        /*it's local to the client library. Not even worth allowing 
         context switches*/ 
        push_text(msqlGetHostInfo()); 
} 
 
/*! @decl string error(void|int clear) 
 *! 
 *! This function returns the textual description of the last server-related 
 *! error. Returns 0 if no error has occurred yet. It is not cleared upon 
 *! reading (can be invoked multiple times, will return the same result 
 *! until a new error occurs). 
 *! 
 *! @seealso 
 *!   @[query] 
 */ 
 
PIKEFUN string error(void|int clear) 
{ 
        int clearit = clear && clear->u.integer; 
 
        pop_n_elems(args); 
 
        if (THIS->error_msg) { 
                ref_push_string(THIS->error_msg); 
                if (clearit) { 
                        free_string(THIS->error_msg); 
                        THIS->error_msg = 0; 
                } 
        } else 
                push_int(0); 
} 
 
/*! @decl void create_db(string dbname) 
 *! 
 *! This function creates a new database with the given name (assuming we 
 *! have enough permissions to do this). 
 *! 
 *! @seealso 
 *!   @[drop_db] 
 */ 
 
PIKEFUN void create_db(string(1..255) dbname) 
{ 
        int dbresult; 
        int socket; 
 
        if (!THIS->connected) 
                Pike_error("Not connected.\n"); 
        socket=THIS->socket; 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        dbresult=msqlCreateDB(socket,dbname->str); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        if (dbresult==-1) { 
                report_error(); 
                Pike_error ("Could not create database.\n"); 
        } 
} 
 
/*! @decl void drop_db(string dbname) 
 *! 
 *! This function destroys a database and all the data it contains (assuming 
 *! we have enough permissions to do so). USE WITH CAUTION! 
 *! 
 *! @seealso 
 *!   @[create_db] 
 */ 
 
PIKEFUN void drop_db(string(1..255) dbname) 
{ 
        int dbresult; 
        int socket; 
 
        if (!THIS->connected) 
                Pike_error("Not connected.\n"); 
        socket=THIS->socket; 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        dbresult=msqlDropDB(socket,dbname->str); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        if (dbresult==-1) { 
                report_error(); 
                Pike_error ("Could not drop database.\n"); 
        } 
} 
 
/*! @decl mapping(string:mapping(string:mixed)) list_fields(string table) 
 *! 
 *! Returns a mapping describing the fields of a table in the database. 
 *! The returned value is a mapping, indexed on the column name, 
 *! of mappings. These contain currently the fields: 
 *! 
 *! @mapping 
 *!   @member string "type" 
 *!      Describes the field's mSQL data type ("char","integer",...) 
 *!   @member int "length" 
 *!      It describes the field's length. It is only interesting for 
 *!      char() fields, in fact.  Also 
 *!      notice that the strings returned by msql->query() have the correct length. 
 *!      This field only specifies the _maximum_ length a "char()" field can have. 
 *!   @member string "table" 
 *!      The table this field is in. Added only for interface compliancy. 
 *!   @member multiset(string) "flags" 
 *!      It's a multiset containing textual 
 *!      descriptions of the server's flags associated with the current field. 
 *!      Currently it can be empty, or contain "unique" or "not null". 
 *! @endmapping 
 *! 
 *! @note 
 *!   The version of this function in the Msql.msql() program is @b{not@} 
 *!   sql-interface compliant (this is the main reason why using that program 
 *!   directly is deprecated). Use @[Sql.Sql] instead. 
 *! 
 *! @seealso 
 *!   @[query] 
 */ 
 
PIKEFUN mapping(string:mapping(string:mixed)) list_fields(string(1..255) table, 
                                                          void|string glob) 
{ 
        m_result * result; 
        m_field * field; 
        int fields, j, socket=THIS->socket; 
 
        if (!THIS->connected) 
                Pike_error ("Not connected.\n"); 
        if (!THIS->db_selected) 
                Pike_error ("Must select a db first.\n"); 
#ifdef MSQL_DEBUG 
        printf("list_fields: table=%s(%d)\n", table->str, table->len); 
#endif 
 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        result=msqlListFields(socket,table->str); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        pop_n_elems(args); 
 
        if (!result) { 
                report_error(); 
                Pike_error ("No fields information.\n"); 
        } 
 
        fields = msqlNumFields(result); 
        if (!fields) 
                Pike_error ("No such table.\n"); 
 
        for (j=0;j<fields;j++) 
        { 
                int flagsnum=0; 
                field=msqlFetchField(result); 
 
                push_static_text("name"); 
                push_text(field->name); 
                ref_push_string(literal_type_string); 
                push_static_text(decode_msql_type(field->type)); 
                push_static_text("length"); 
                push_int(field->length); 
                push_static_text("table"); 
                push_text(field->table); 
                push_static_text("flags"); 
#ifdef IS_UNIQUE 
                if (IS_UNIQUE(field->flags)) { 
                        push_static_text("unique"); 
                        flagsnum++; 
                } 
#endif 
#ifdef IS_NOT_NULL 
                if (IS_NOT_NULL(field->flags)) { 
                        push_static_text("not_null"); 
                        flagsnum++; 
                } 
#endif 
#ifdef IS_PRI_KEY 
                if (IS_PRI_KEY(field->flags)) { 
                        push_static_text("primary_key"); 
                        flagsnum++; 
                } 
#endif 
                f_aggregate_multiset(flagsnum); 
                f_aggregate_mapping(10); 
        } 
        f_aggregate(fields); 
        msqlFreeResult(result); 
} 
 
#ifdef MSQL_VERSION_2 
 
/*! @decl int affected_rows() 
 *! 
 *! This function returns how many rows in the database were affected by 
 *! our last SQL query. 
 *! 
 *! @note 
 *!  This function is available only if you're using mSQL version 2 
 *!  or later. (That means: if the includes and library of version 2 of mSQL 
 *!  were available when the module was compiled). 
 *! 
 *!  This function is @b{not@} part of the standard interface, so it is @b{not@} 
 *!  available through the @[Sql.Sql] interface, but only through @[Sql.msql] and 
 *!  @[Msql.msql] programs 
 */ 
 
PIKEFUN int affected_rows() 
{ 
        pop_n_elems(args); 
        push_int(THIS->affected); 
} 
 
/*! @decl array list_index(string tablename, string indexname) 
 *! 
 *! This function returns an array describing the index structure for the 
 *! given table and index name, as defined by the non-standard SQL query 
 *! 'create index' (see the mSQL documentation for further informations). 
 *! More than one index can be created for a table. There's currently NO way 
 *! to have a listing of the indexes defined for a table (blame it on 
 *! the mSQL API). 
 *! 
 *! @note 
 *!  This function is available if you're using mSQL version 2 
 *!  or later. 
 *! 
 *!  This function is @b{not@} part of the standard interface, so it is @b{not@} 
 *!  available through the @[Sql.Sql] interface, but only through @[Sql.msql] and 
 *!  @[Msql.msql] programs. 
 */ 
 
PIKEFUN array list_index(string(1..255) tablename, string(1..255) indexname) 
{ 
        m_result * result; 
        m_row row; 
        int sock, rows, j; 
 
        if (!THIS->db_selected) 
                Pike_error ("No database selected.\n"); 
        sock=THIS->socket; 
        THREADS_ALLOW(); 
        MSQL_LOCK(); 
        result=msqlListIndex(sock,tablename->str,indexname->str); 
        MSQL_UNLOCK(); 
        THREADS_DISALLOW(); 
        pop_n_elems(args); 
        if (!result || !(rows=msqlNumRows(result)) ) { 
                f_aggregate(0); 
                return; 
        } 
        msqlFetchRow(result); /*The first one is the internal type, useless*/ 
        rows--; 
        for (j=0; j<rows; j++) 
        { 
                row=msqlFetchRow(result); 
                push_text(row[0]); 
        } 
        f_aggregate(rows); 
} 
#endif 
 
} 
/*! @endclass 
 */ 
 
/*! @decl constant version 
 *! 
 *! Should you need to report a bug to the author, please submit along with 
 *! the report the driver version number, as returned by this call. 
 */ 
 
/*! @endmodule 
 */ 
 
PIKE_MODULE_INIT 
{ 
  INIT; 
 
  /* Deprecated */ 
  add_string_constant("version","mSQL/1.1.1",0); 
} 
 
PIKE_MODULE_EXIT 
{ 
  EXIT; 
} 
 
#else /*HAVE_MSQL*/ 
PIKE_MODULE_INIT { 
  HIDE_MODULE(); 
} 
 
PIKE_MODULE_EXIT { } 
#endif /*HAVE_MSQL*/