/
mysql.c
2577 lines (2333 loc) · 75.5 KB
/
mysql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ruby mysql module
* $Id: mysql.c 229 2008-06-20 07:44:04Z tommy $
* modified 2008-08-26 Muhammad A. Ali
* added an async interface
*/
#include <ruby.h>
#include <errno.h>
#ifndef RSTRING_PTR
#define RSTRING_PTR(str) RSTRING(str)->ptr
#endif
#ifndef RSTRING_LEN
#define RSTRING_LEN(str) RSTRING(str)->len
#endif
#ifndef HAVE_RB_STR_SET_LEN
#define rb_str_set_len(str, length) (RSTRING_LEN(str) = (length))
#endif
#ifdef HAVE_MYSQL_H
#include <mysql.h>
#include <mysql_com.h>
//#include <violite.h>
#include <errmsg.h>
#include <mysqld_error.h>
#else
#include <mysql/mysql.h>
#include <mysql/mysql_com.h>
//#include <mysql/violite.h>
#include <mysql/errmsg.h>
#include <mysql/mysqld_error.h>
#endif
#define MYSQL_RUBY_VERSION 20800
#define GC_STORE_RESULT_LIMIT 20
#if MYSQL_VERSION_ID < 32224
#define mysql_field_count mysql_num_fields
#endif
#define NILorSTRING(obj) (NIL_P(obj)? NULL: StringValuePtr(obj))
#define NILorINT(obj) (NIL_P(obj)? 0: NUM2INT(obj))
#define GetMysqlStruct(obj) (Check_Type(obj, T_DATA), (struct mysql*)DATA_PTR(obj))
#define GetHandler(obj) (Check_Type(obj, T_DATA), &(((struct mysql*)DATA_PTR(obj))->handler))
#define GetMysqlRes(obj) (Check_Type(obj, T_DATA), ((struct mysql_res*)DATA_PTR(obj))->res)
#define GetMysqlStmt(obj) (Check_Type(obj, T_DATA), ((struct mysql_stmt*)DATA_PTR(obj))->stmt)
VALUE cMysql;
VALUE cMysqlRes;
VALUE cMysqlField;
VALUE cMysqlStmt;
VALUE cMysqlRowOffset;
VALUE cMysqlTime;
VALUE eMysql;
static int store_result_count = 0;
struct mysql {
MYSQL handler;
char connection;
char query_with_result;
char gc_disabled;
char blocking;
int async_in_progress;
};
struct mysql_res {
MYSQL_RES* res;
char freed;
};
#if MYSQL_VERSION_ID >= 40101
struct mysql_stmt {
MYSQL_STMT *stmt;
char closed;
struct {
int n;
MYSQL_BIND *bind;
unsigned long *length;
MYSQL_TIME *buffer;
} param;
struct {
int n;
MYSQL_BIND *bind;
my_bool *is_null;
unsigned long *length;
} result;
MYSQL_RES *res;
};
#endif
/* free Mysql class object */
static void free_mysql(struct mysql* my)
{
if (my->connection == Qtrue)
mysql_close(&my->handler);
xfree(my);
}
static void free_mysqlres(struct mysql_res* resp)
{
if (resp->freed == Qfalse) {
mysql_free_result(resp->res);
store_result_count--;
}
xfree(resp);
}
#if MYSQL_VERSION_ID >= 40101
static void free_mysqlstmt_memory(struct mysql_stmt *s)
{
if (s->param.bind) {
xfree(s->param.bind);
s->param.bind = NULL;
}
if (s->param.length) {
xfree(s->param.length);
s->param.length = NULL;
}
if (s->param.buffer) {
xfree(s->param.buffer);
s->param.buffer = NULL;
}
s->param.n = 0;
if (s->res) {
mysql_free_result(s->res);
s->res = NULL;
}
if (s->result.bind) {
int i;
for (i = 0; i < s->result.n; i++) {
if (s->result.bind[i].buffer)
xfree(s->result.bind[i].buffer);
s->result.bind[i].buffer = NULL;
}
xfree(s->result.bind);
s->result.bind = NULL;
}
if (s->result.is_null) {
xfree(s->result.is_null);
s->result.is_null = NULL;
}
if (s->result.length) {
xfree(s->result.length);
s->result.length = NULL;
}
s->result.n = 0;
}
static void free_execute_memory(struct mysql_stmt *s)
{
if (s->res && s->result.bind) {
int i;
for (i = 0; i < s->result.n; i++) {
if (s->result.bind[i].buffer)
xfree(s->result.bind[i].buffer);
s->result.bind[i].buffer = NULL;
}
}
mysql_stmt_free_result(s->stmt);
}
static void free_mysqlstmt(struct mysql_stmt* s)
{
free_mysqlstmt_memory(s);
if (s->closed == Qfalse)
mysql_stmt_close(s->stmt);
if (s->res)
mysql_free_result(s->res);
xfree(s);
}
#endif
static void mysql_raise(MYSQL* m)
{
VALUE e = rb_exc_new2(eMysql, mysql_error(m));
rb_iv_set(e, "errno", INT2FIX(mysql_errno(m)));
#if MYSQL_VERSION_ID >= 40101
rb_iv_set(e, "sqlstate", rb_tainted_str_new2(mysql_sqlstate(m)));
#endif
rb_exc_raise(e);
}
static VALUE mysqlres2obj(MYSQL_RES* res, VALUE gc_disabled)
{
VALUE obj;
struct mysql_res* resp;
obj = Data_Make_Struct(cMysqlRes, struct mysql_res, 0, free_mysqlres, resp);
rb_iv_set(obj, "colname", Qnil);
rb_iv_set(obj, "tblcolname", Qnil);
resp->res = res;
resp->freed = Qfalse;
rb_obj_call_init(obj, 0, NULL);
if (++store_result_count > GC_STORE_RESULT_LIMIT && gc_disabled == Qfalse){
rb_gc();
}
return obj;
}
/* make Mysql::Field object */
static VALUE make_field_obj(MYSQL_FIELD* f)
{
VALUE obj;
if (f == NULL)
return Qnil;
obj = rb_obj_alloc(cMysqlField);
rb_iv_set(obj, "name", f->name? rb_str_freeze(rb_tainted_str_new2(f->name)): Qnil);
rb_iv_set(obj, "table", f->table? rb_str_freeze(rb_tainted_str_new2(f->table)): Qnil);
rb_iv_set(obj, "def", f->def? rb_str_freeze(rb_tainted_str_new2(f->def)): Qnil);
rb_iv_set(obj, "type", INT2NUM(f->type));
rb_iv_set(obj, "length", INT2NUM(f->length));
rb_iv_set(obj, "max_length", INT2NUM(f->max_length));
rb_iv_set(obj, "flags", INT2NUM(f->flags));
rb_iv_set(obj, "decimals", INT2NUM(f->decimals));
return obj;
}
/*-------------------------------
* Mysql class method
*/
/* init() */
static VALUE init(VALUE klass)
{
struct mysql* myp;
VALUE obj;
obj = Data_Make_Struct(klass, struct mysql, 0, free_mysql, myp);
mysql_init(&myp->handler);
myp->connection = Qfalse;
myp->query_with_result = Qtrue;
myp->gc_disabled = Qfalse;
rb_obj_call_init(obj, 0, NULL);
return obj;
}
static VALUE connection_identifier( VALUE obj )
{
MYSQL* m = GetHandler(obj);
return mysql_thread_id( m );
}
static VALUE async_in_progress( VALUE obj )
{
struct mysql* m = GetMysqlStruct(obj);
return ( m->async_in_progress == connection_identifier(obj) ) ? Qtrue : Qfalse;
}
static VALUE async_in_progress_set( VALUE obj, VALUE flag )
{
struct mysql* m = GetMysqlStruct(obj);
m->async_in_progress = (flag == Qnil || flag == Qfalse) ? 0 : connection_identifier(obj);
return flag;
}
static void optimize_for_async( VALUE obj )
{
struct mysql* m = GetMysqlStruct(obj);
my_bool was_blocking;
vio_blocking(m->handler.net.vio, 0, &was_blocking);
m->blocking = vio_is_blocking( m->handler.net.vio );
vio_fastsend( m->handler.net.vio );
async_in_progress_set( obj, Qfalse );
}
static void schedule_connect(VALUE obj )
{
MYSQL* m = GetHandler(obj);
fd_set read;
struct timeval tv = { tv_sec: m->options.connect_timeout, tv_usec: 0 };
if (rb_thread_select(0, NULL, NULL, NULL, &tv) < 0) {
rb_raise(eMysql, "connect: timeout");
}
/*
FD_ZERO(&read);
FD_SET(m->net.fd, &read);
if (rb_thread_select(m->net.fd + 1, &read, NULL, NULL, &tv) < 0) {
rb_raise(eMysql, "connect: timeout");
}
*/
}
/* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
static VALUE real_connect(int argc, VALUE* argv, VALUE klass)
{
VALUE host, user, passwd, db, port, sock, flag;
char *h, *u, *p, *d, *s;
unsigned int pp, f;
struct mysql* myp;
VALUE obj;
#if MYSQL_VERSION_ID >= 32200
rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
d = NILorSTRING(db);
f = NILorINT(flag);
#elif MYSQL_VERSION_ID >= 32115
rb_scan_args(argc, argv, "06", &host, &user, &passwd, &port, &sock, &flag);
f = NILorINT(flag);
#else
rb_scan_args(argc, argv, "05", &host, &user, &passwd, &port, &sock);
#endif
h = NILorSTRING(host);
u = NILorSTRING(user);
p = NILorSTRING(passwd);
pp = NILorINT(port);
s = NILorSTRING(sock);
obj = Data_Make_Struct(klass, struct mysql, 0, free_mysql, myp);
#if MYSQL_VERSION_ID >= 32200
mysql_init(&myp->handler);
if (mysql_real_connect(&myp->handler, h, u, p, d, pp, s, f) == NULL)
#elif MYSQL_VERSION_ID >= 32115
if (mysql_real_connect(&myp->handler, h, u, p, pp, s, f) == NULL)
#else
if (mysql_real_connect(&myp->handler, h, u, p, pp, s) == NULL)
#endif
mysql_raise(&myp->handler);
myp->handler.reconnect = 0;
myp->connection = Qtrue;
optimize_for_async(obj);
myp->query_with_result = Qtrue;
rb_obj_call_init(obj, argc, argv);
schedule_connect(obj);
return obj;
}
/* escape_string(string) */
static VALUE escape_string(VALUE klass, VALUE str)
{
VALUE ret;
Check_Type(str, T_STRING);
ret = rb_str_new(0, (RSTRING_LEN(str))*2+1);
rb_str_set_len(ret, mysql_escape_string(RSTRING_PTR(ret), RSTRING_PTR(str), RSTRING_LEN(str)));
return ret;
}
/* client_info() */
static VALUE client_info(VALUE klass)
{
return rb_tainted_str_new2(mysql_get_client_info());
}
#if MYSQL_VERSION_ID >= 32332
/* my_debug(string) */
static VALUE my_debug(VALUE obj, VALUE str)
{
mysql_debug(StringValuePtr(str));
return obj;
}
#endif
#if MYSQL_VERSION_ID >= 40000
/* client_version() */
static VALUE client_version(VALUE obj)
{
return INT2NUM(mysql_get_client_version());
}
#endif
/*-------------------------------
* Mysql object method
*/
#if MYSQL_VERSION_ID >= 32200
/* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
static VALUE real_connect2(int argc, VALUE* argv, VALUE obj)
{
VALUE host, user, passwd, db, port, sock, flag;
char *h, *u, *p, *d, *s;
unsigned int pp, f;
MYSQL* m = GetHandler(obj);
rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
d = NILorSTRING(db);
f = NILorINT(flag);
h = NILorSTRING(host);
u = NILorSTRING(user);
p = NILorSTRING(passwd);
pp = NILorINT(port);
s = NILorSTRING(sock);
if (mysql_real_connect(m, h, u, p, d, pp, s, f) == NULL)
mysql_raise(m);
m->reconnect = 0;
GetMysqlStruct(obj)->connection = Qtrue;
optimize_for_async(obj);
schedule_connect(obj);
return obj;
}
/* options(opt, value=nil) */
static VALUE options(int argc, VALUE* argv, VALUE obj)
{
VALUE opt, val;
int n;
my_bool b;
char* v;
MYSQL* m = GetHandler(obj);
rb_scan_args(argc, argv, "11", &opt, &val);
switch(NUM2INT(opt)) {
case MYSQL_OPT_CONNECT_TIMEOUT:
#if MYSQL_VERSION_ID >= 40100
case MYSQL_OPT_PROTOCOL:
#endif
#if MYSQL_VERSION_ID >= 40101
case MYSQL_OPT_READ_TIMEOUT:
case MYSQL_OPT_WRITE_TIMEOUT:
#endif
if (val == Qnil)
rb_raise(rb_eArgError, "wrong # of arguments(1 for 2)");
n = NUM2INT(val);
v = (char*)&n;
break;
case MYSQL_INIT_COMMAND:
case MYSQL_READ_DEFAULT_FILE:
case MYSQL_READ_DEFAULT_GROUP:
#if MYSQL_VERSION_ID >= 32349
case MYSQL_SET_CHARSET_DIR:
case MYSQL_SET_CHARSET_NAME:
#endif
#if MYSQL_VERSION_ID >= 40100
case MYSQL_SHARED_MEMORY_BASE_NAME:
#endif
#if MYSQL_VERSION_ID >= 40101
case MYSQL_SET_CLIENT_IP:
#endif
if (val == Qnil)
rb_raise(rb_eArgError, "wrong # of arguments(1 for 2)");
v = StringValuePtr(val);
break;
#if MYSQL_VERSION_ID >= 40101
case MYSQL_SECURE_AUTH:
if (val == Qnil || val == Qfalse)
b = 1;
else
b = 0;
v = (char*)&b;
break;
#endif
#if MYSQL_VERSION_ID >= 32349
case MYSQL_OPT_LOCAL_INFILE:
if (val == Qnil || val == Qfalse)
v = NULL;
else {
n = 1;
v = (char*)&n;
}
break;
#endif
default:
v = NULL;
}
if (mysql_options(m, NUM2INT(opt), v) != 0)
rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
return obj;
}
#endif
#if MYSQL_VERSION_ID >= 32332
/* real_escape_string(string) */
static VALUE real_escape_string(VALUE obj, VALUE str)
{
MYSQL* m = GetHandler(obj);
VALUE ret;
Check_Type(str, T_STRING);
ret = rb_str_new(0, (RSTRING_LEN(str))*2+1);
rb_str_set_len(ret, mysql_real_escape_string(m, RSTRING_PTR(ret), RSTRING_PTR(str), RSTRING_LEN(str)));
return ret;
}
#endif
/* initialize() */
static VALUE initialize(int argc, VALUE* argv, VALUE obj)
{
return obj;
}
/* affected_rows() */
static VALUE affected_rows(VALUE obj)
{
return INT2NUM(mysql_affected_rows(GetHandler(obj)));
}
#if MYSQL_VERSION_ID >= 32303
/* change_user(user=nil, passwd=nil, db=nil) */
static VALUE change_user(int argc, VALUE* argv, VALUE obj)
{
VALUE user, passwd, db;
char *u, *p, *d;
MYSQL* m = GetHandler(obj);
rb_scan_args(argc, argv, "03", &user, &passwd, &db);
u = NILorSTRING(user);
p = NILorSTRING(passwd);
d = NILorSTRING(db);
if (mysql_change_user(m, u, p, d) != 0)
mysql_raise(m);
return obj;
}
#endif
#if MYSQL_VERSION_ID >= 32321
/* character_set_name() */
static VALUE character_set_name(VALUE obj)
{
return rb_tainted_str_new2(mysql_character_set_name(GetHandler(obj)));
}
#endif
/* close() */
static VALUE my_close(VALUE obj)
{
MYSQL* m = GetHandler(obj);
mysql_close(m);
GetMysqlStruct(obj)->connection = Qfalse;
return obj;
}
#if MYSQL_VERSION_ID < 40000
/* create_db(db) */
static VALUE create_db(VALUE obj, VALUE db)
{
MYSQL* m = GetHandler(obj);
if (mysql_create_db(m, StringValuePtr(db)) != 0)
mysql_raise(m);
return obj;
}
/* drop_db(db) */
static VALUE drop_db(VALUE obj, VALUE db)
{
MYSQL* m = GetHandler(obj);
if (mysql_drop_db(m, StringValuePtr(db)) != 0)
mysql_raise(m);
return obj;
}
#endif
#if MYSQL_VERSION_ID >= 32332
/* dump_debug_info() */
static VALUE dump_debug_info(VALUE obj)
{
MYSQL* m = GetHandler(obj);
if (mysql_dump_debug_info(m) != 0)
mysql_raise(m);
return obj;
}
#endif
/* errno() */
static VALUE my_errno(VALUE obj)
{
return INT2NUM(mysql_errno(GetHandler(obj)));
}
/* error() */
static VALUE my_error(VALUE obj)
{
return rb_str_new2(mysql_error(GetHandler(obj)));
}
/* field_count() */
static VALUE field_count(VALUE obj)
{
return INT2NUM(mysql_field_count(GetHandler(obj)));
}
/* host_info() */
static VALUE host_info(VALUE obj)
{
return rb_tainted_str_new2(mysql_get_host_info(GetHandler(obj)));
}
/* proto_info() */
static VALUE proto_info(VALUE obj)
{
return INT2NUM(mysql_get_proto_info(GetHandler(obj)));
}
/* server_info() */
static VALUE server_info(VALUE obj)
{
return rb_tainted_str_new2(mysql_get_server_info(GetHandler(obj)));
}
/* info() */
static VALUE info(VALUE obj)
{
const char* p = mysql_info(GetHandler(obj));
return p? rb_tainted_str_new2(p): Qnil;
}
/* insert_id() */
static VALUE insert_id(VALUE obj)
{
return INT2NUM(mysql_insert_id(GetHandler(obj)));
}
/* kill(pid) */
static VALUE my_kill(VALUE obj, VALUE pid)
{
int p = NUM2INT(pid);
MYSQL* m = GetHandler(obj);
if (mysql_kill(m, p) != 0)
mysql_raise(m);
return obj;
}
/* list_dbs(db=nil) */
static VALUE list_dbs(int argc, VALUE* argv, VALUE obj)
{
unsigned int i, n;
VALUE db, ret;
MYSQL* m = GetHandler(obj);
MYSQL_RES* res;
rb_scan_args(argc, argv, "01", &db);
res = mysql_list_dbs(m, NILorSTRING(db));
if (res == NULL)
mysql_raise(m);
n = mysql_num_rows(res);
ret = rb_ary_new2(n);
for (i=0; i<n; i++)
rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
mysql_free_result(res);
return ret;
}
/* list_fields(table, field=nil) */
static VALUE list_fields(int argc, VALUE* argv, VALUE obj)
{
VALUE table, field;
MYSQL* m = GetHandler(obj);
MYSQL_RES* res;
rb_scan_args(argc, argv, "11", &table, &field);
res = mysql_list_fields(m, StringValuePtr(table), NILorSTRING(field));
if (res == NULL)
mysql_raise(m);
return mysqlres2obj(res, GetMysqlStruct(obj)->gc_disabled);
}
/* list_processes() */
static VALUE list_processes(VALUE obj)
{
MYSQL* m = GetHandler(obj);
MYSQL_RES* res = mysql_list_processes(m);
if (res == NULL)
mysql_raise(m);
return mysqlres2obj(res, GetMysqlStruct(obj)->gc_disabled);
}
/* list_tables(table=nil) */
static VALUE list_tables(int argc, VALUE* argv, VALUE obj)
{
VALUE table;
MYSQL* m = GetHandler(obj);
MYSQL_RES* res;
unsigned int i, n;
VALUE ret;
rb_scan_args(argc, argv, "01", &table);
res = mysql_list_tables(m, NILorSTRING(table));
if (res == NULL)
mysql_raise(m);
n = mysql_num_rows(res);
ret = rb_ary_new2(n);
for (i=0; i<n; i++)
rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
mysql_free_result(res);
return ret;
}
/* ping() */
static VALUE ping(VALUE obj)
{
MYSQL* m = GetHandler(obj);
if (mysql_ping(m) != 0)
mysql_raise(m);
return obj;
}
/* refresh(r) */
static VALUE refresh(VALUE obj, VALUE r)
{
MYSQL* m = GetHandler(obj);
if (mysql_refresh(m, NUM2INT(r)) != 0)
mysql_raise(m);
return obj;
}
/* reload() */
static VALUE reload(VALUE obj)
{
MYSQL* m = GetHandler(obj);
if (mysql_reload(m) != 0)
mysql_raise(m);
return obj;
}
/* select_db(db) */
static VALUE select_db(VALUE obj, VALUE db)
{
MYSQL* m = GetHandler(obj);
if (mysql_select_db(m, StringValuePtr(db)) != 0)
mysql_raise(m);
return obj;
}
/* shutdown() */
static VALUE my_shutdown(int argc, VALUE* argv, VALUE obj)
{
MYSQL* m = GetHandler(obj);
VALUE level;
rb_scan_args(argc, argv, "01", &level);
#if MYSQL_VERSION_ID >= 40103
if (mysql_shutdown(m, NIL_P(level) ? SHUTDOWN_DEFAULT : NUM2INT(level)) != 0)
#else
if (mysql_shutdown(m) != 0)
#endif
mysql_raise(m);
return obj;
}
/* stat() */
static VALUE my_stat(VALUE obj)
{
MYSQL* m = GetHandler(obj);
const char* s = mysql_stat(m);
if (s == NULL)
mysql_raise(m);
return rb_tainted_str_new2(s);
}
/* store_result() */
static VALUE store_result(VALUE obj)
{
MYSQL* m = GetHandler(obj);
MYSQL_RES* res = mysql_store_result(m);
if (res == NULL)
mysql_raise(m);
return mysqlres2obj(res, GetMysqlStruct(obj)->gc_disabled);
}
/* thread_id() */
static VALUE thread_id(VALUE obj)
{
return INT2NUM(mysql_thread_id(GetHandler(obj)));
}
/* use_result() */
static VALUE use_result(VALUE obj)
{
MYSQL* m = GetHandler(obj);
MYSQL_RES* res = mysql_use_result(m);
if (res == NULL)
mysql_raise(m);
return mysqlres2obj(res, GetMysqlStruct(obj)->gc_disabled);
}
static VALUE res_free(VALUE);
/* query(sql) */
static VALUE query(VALUE obj, VALUE sql)
{
int loop = 0;
MYSQL* m = GetHandler(obj);
Check_Type(sql, T_STRING);
if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected");
}
if (rb_block_given_p()) {
if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
mysql_raise(m);
do {
MYSQL_RES* res = mysql_store_result(m);
if (res == NULL) {
if (mysql_field_count(m) != 0)
mysql_raise(m);
} else {
VALUE robj = mysqlres2obj(res, GetMysqlStruct(obj)->gc_disabled);
rb_ensure(rb_yield, robj, res_free, robj);
}
#if MYSQL_VERSION_ID >= 40101
if ((loop = mysql_next_result(m)) > 0)
mysql_raise(m);
} while (loop == 0);
#else
} while (0);
#endif
return obj;
}
if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
mysql_raise(m);
if (GetMysqlStruct(obj)->query_with_result == Qfalse)
return obj;
if (mysql_field_count(m) == 0)
return Qnil;
return store_result(obj);
}
/* socket */
static VALUE socket(VALUE obj)
{
MYSQL* m = GetHandler(obj);
return INT2NUM(m->net.fd);
}
/* blocking */
static VALUE blocking(VALUE obj){
return ( GetMysqlStruct(obj)->blocking ? Qtrue : Qfalse );
}
/* readable(timeout=nil) */
static VALUE readable( int argc, VALUE* argv, VALUE obj )
{
MYSQL* m = GetHandler(obj);
VALUE timeout;
rb_scan_args(argc, argv, "01", &timeout);
if ( NIL_P( timeout ) ){
timeout = m->net.read_timeout;
}
return ( vio_poll_read( m->net.vio, INT2NUM(timeout) ) == 0 ? Qtrue : Qfalse );
}
/* retry */
static VALUE retry( VALUE obj )
{
MYSQL* m = GetHandler(obj);
return ( vio_should_retry( m->net.vio ) == 1 ? Qtrue : Qfalse );
}
/* interrupted */
static VALUE interrupted( VALUE obj )
{
MYSQL* m = GetHandler(obj);
return ( vio_was_interrupted( m->net.vio ) == 1 ? Qtrue : Qfalse );
}
/* reconnected */
static VALUE reconnected( VALUE obj ){
MYSQL* m = GetHandler(obj);
int current_connection_id = mysql_thread_id( m );
mysql_ping(m);
return ( current_connection_id == mysql_thread_id( m ) ) ? Qfalse : Qtrue;
}
/* disable_gc(true|false) */
static VALUE disable_gc_set(VALUE obj, VALUE flag)
{
if (TYPE(flag) != T_TRUE && TYPE(flag) != T_FALSE)
rb_raise(rb_eTypeError, "invalid type, required true or false.");
GetMysqlStruct(obj)->gc_disabled = flag;
return flag;
}
/* gc_disabled */
static VALUE gc_disabled( VALUE obj ){
return GetMysqlStruct(obj)->gc_disabled ? Qtrue: Qfalse;
}
static void validate_async_query( VALUE obj )
{
MYSQL* m = GetHandler(obj);
if( async_in_progress(obj) == Qtrue ){
async_in_progress_set(obj, Qfalse);
rb_raise(eMysql, "Query out of sequence: Each call to Mysql#send_query requires a successive Mysql#get_result.");
}
}
static VALUE simulate_disconnect( VALUE obj )
{
MYSQL* m = GetHandler(obj);
mysql_library_end();
return Qnil;
}
/* send_query(sql) */
static VALUE send_query(VALUE obj, VALUE sql)
{
MYSQL* m = GetHandler(obj);
Check_Type(sql, T_STRING);
if (GetMysqlStruct(obj)->connection == Qfalse && async_in_progress(obj) == Qtrue ) {
rb_raise(eMysql, "query: not connected");
}
validate_async_query(obj);
if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0){
mysql_raise(m);
}
async_in_progress_set( obj, Qtrue );
return Qnil;
}
/* get_result */
static VALUE get_result(VALUE obj)
{
MYSQL* m = GetHandler(obj);
async_in_progress_set( obj, Qfalse );
if (GetMysqlStruct(obj)->connection == Qfalse) {
rb_raise(eMysql, "query: not connected");
}
if (mysql_read_query_result(m) != 0)
mysql_raise(m);
if (GetMysqlStruct(obj)->query_with_result == Qfalse)
return obj;
if (mysql_field_count(m) == 0)
return Qnil;
return store_result(obj);
}
static void schedule_query(VALUE obj, VALUE timeout)
{
MYSQL* m = GetHandler(obj);
fd_set read;
timeout = ( NIL_P(timeout) ? m->net.read_timeout : INT2NUM(timeout) );
struct timeval tv = { tv_sec: timeout, tv_usec: 0 };
FD_ZERO(&read);
FD_SET(m->net.fd, &read);
if (rb_thread_select(m->net.fd + 1, &read, NULL, NULL, &tv) < 0) {
rb_raise(eMysql, "query: timeout");
}
}
/* async_query(sql,timeout=nil) */
static VALUE async_query(int argc, VALUE* argv, VALUE obj)
{
MYSQL* m = GetHandler(obj);
VALUE sql, timeout;
rb_scan_args(argc, argv, "11", &sql, &timeout);
async_in_progress_set( obj, Qfalse );
send_query(obj,sql);
schedule_query(obj, timeout);
if (rb_block_given_p()) {
rb_yield( get_result(obj) );
return obj;
}else{
return get_result(obj);
}
}
#if MYSQL_VERSION_ID >= 40100
/* server_version() */
static VALUE server_version(VALUE obj)
{
return INT2NUM(mysql_get_server_version(GetHandler(obj)));
}
/* warning_count() */
static VALUE warning_count(VALUE obj)
{
return INT2NUM(mysql_warning_count(GetHandler(obj)));
}
/* commit() */
static VALUE commit(VALUE obj)
{
MYSQL* m = GetHandler(obj);
if (mysql_commit(m) != 0)
mysql_raise(m);
return obj;
}
/* rollback() */
static VALUE rollback(VALUE obj)
{