oldmoe-mysqlplus 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/extconf.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'mkmf'
2
+
3
+ if /mswin32/ =~ RUBY_PLATFORM
4
+ inc, lib = dir_config('mysql')
5
+ exit 1 unless have_library("libmysql")
6
+ elsif mc = with_config('mysql-config') then
7
+ mc = 'mysql_config' if mc == true
8
+ cflags = `#{mc} --cflags`.chomp
9
+ exit 1 if $? != 0
10
+ libs = `#{mc} --libs`.chomp
11
+ exit 1 if $? != 0
12
+ $CPPFLAGS += ' ' + cflags
13
+ $libs = libs + " " + $libs
14
+ else
15
+ inc, lib = dir_config('mysql', '/usr/local')
16
+ libs = ['m', 'z', 'socket', 'nsl', 'mygcc']
17
+ while not find_library('mysqlclient', 'mysql_query', lib, "#{lib}/mysql") do
18
+ exit 1 if libs.empty?
19
+ have_library(libs.shift)
20
+ end
21
+ end
22
+
23
+ have_func('mysql_ssl_set')
24
+ have_func('rb_str_set_len')
25
+
26
+ if have_header('mysql.h') then
27
+ src = "#include <errmsg.h>\n#include <mysqld_error.h>\n"
28
+ elsif have_header('mysql/mysql.h') then
29
+ src = "#include <mysql/errmsg.h>\n#include <mysql/mysqld_error.h>\n"
30
+ else
31
+ exit 1
32
+ end
33
+
34
+ if have_func('rb_thread_blocking_region') and have_macro('RB_UBF_DFL', 'ruby.h')
35
+ flags << "-DHAVE_TBR"
36
+ end
37
+
38
+ # make mysql constant
39
+ File.open("conftest.c", "w") do |f|
40
+ f.puts src
41
+ end
42
+ if defined? cpp_command then
43
+ cpp = Config.expand(cpp_command(''))
44
+ else
45
+ cpp = Config.expand sprintf(CPP, $CPPFLAGS, $CFLAGS, '')
46
+ end
47
+ if /mswin32/ =~ RUBY_PLATFORM && !/-E/.match(cpp)
48
+ cpp << " -E"
49
+ end
50
+ unless system "#{cpp} > confout" then
51
+ exit 1
52
+ end
53
+ File.unlink "conftest.c"
54
+
55
+ error_syms = []
56
+ IO.foreach('confout') do |l|
57
+ next unless l =~ /errmsg\.h|mysqld_error\.h/
58
+ fn = l.split(/\"/)[1]
59
+ IO.foreach(fn) do |m|
60
+ if m =~ /^#define\s+([CE]R_[0-9A-Z_]+)/ then
61
+ error_syms << $1
62
+ end
63
+ end
64
+ end
65
+ File.unlink 'confout'
66
+ error_syms.uniq!
67
+
68
+ File.open('error_const.h', 'w') do |f|
69
+ error_syms.each do |s|
70
+ f.puts " rb_define_mysql_const(#{s});"
71
+ end
72
+ end
73
+
74
+ create_makefile("mysql")
data/ext/mysql.c ADDED
@@ -0,0 +1,2461 @@
1
+ /* ruby mysql module
2
+ * $Id: mysql.c 229 2008-06-20 07:44:04Z tommy $
3
+ * modified 2008-08-26 Muhammad A. Ali
4
+ * added an async interface
5
+ */
6
+
7
+ #include <ruby.h>
8
+ #include <errno.h>
9
+ #ifndef RSTRING_PTR
10
+ #define RSTRING_PTR(str) RSTRING(str)->ptr
11
+ #endif
12
+ #ifndef RSTRING_LEN
13
+ #define RSTRING_LEN(str) RSTRING(str)->len
14
+ #endif
15
+ #ifndef HAVE_RB_STR_SET_LEN
16
+ #define rb_str_set_len(str, length) (RSTRING_LEN(str) = (length))
17
+ #endif
18
+
19
+ #ifdef HAVE_MYSQL_H
20
+ #include <mysql.h>
21
+ #include <mysql_com.h>
22
+ //#include <violite.h>
23
+ #include <errmsg.h>
24
+ #include <mysqld_error.h>
25
+ #else
26
+ #include <mysql/mysql.h>
27
+ #include <mysql/mysql_com.h>
28
+ //#include <mysql/violite.h>
29
+ #include <mysql/errmsg.h>
30
+ #include <mysql/mysqld_error.h>
31
+ #endif
32
+
33
+ #define MYSQL_RUBY_VERSION 20800
34
+
35
+ #define GC_STORE_RESULT_LIMIT 20
36
+
37
+ #if MYSQL_VERSION_ID < 32224
38
+ #define mysql_field_count mysql_num_fields
39
+ #endif
40
+
41
+ #define NILorSTRING(obj) (NIL_P(obj)? NULL: StringValuePtr(obj))
42
+ #define NILorINT(obj) (NIL_P(obj)? 0: NUM2INT(obj))
43
+
44
+ #define GetMysqlStruct(obj) (Check_Type(obj, T_DATA), (struct mysql*)DATA_PTR(obj))
45
+ #define GetHandler(obj) (Check_Type(obj, T_DATA), &(((struct mysql*)DATA_PTR(obj))->handler))
46
+ #define GetMysqlRes(obj) (Check_Type(obj, T_DATA), ((struct mysql_res*)DATA_PTR(obj))->res)
47
+ #define GetMysqlStmt(obj) (Check_Type(obj, T_DATA), ((struct mysql_stmt*)DATA_PTR(obj))->stmt)
48
+
49
+ VALUE cMysql;
50
+ VALUE cMysqlRes;
51
+ VALUE cMysqlField;
52
+ VALUE cMysqlStmt;
53
+ VALUE cMysqlRowOffset;
54
+ VALUE cMysqlTime;
55
+ VALUE eMysql;
56
+
57
+ static int store_result_count = 0;
58
+
59
+ struct mysql {
60
+ MYSQL handler;
61
+ char connection;
62
+ char query_with_result;
63
+ char blocking;
64
+ };
65
+
66
+ struct mysql_res {
67
+ MYSQL_RES* res;
68
+ char freed;
69
+ };
70
+
71
+ #if MYSQL_VERSION_ID >= 40101
72
+ struct mysql_stmt {
73
+ MYSQL_STMT *stmt;
74
+ char closed;
75
+ struct {
76
+ int n;
77
+ MYSQL_BIND *bind;
78
+ unsigned long *length;
79
+ MYSQL_TIME *buffer;
80
+ } param;
81
+ struct {
82
+ int n;
83
+ MYSQL_BIND *bind;
84
+ my_bool *is_null;
85
+ unsigned long *length;
86
+ } result;
87
+ MYSQL_RES *res;
88
+ };
89
+ #endif
90
+
91
+ /* free Mysql class object */
92
+ static void free_mysql(struct mysql* my)
93
+ {
94
+ if (my->connection == Qtrue)
95
+ mysql_close(&my->handler);
96
+ xfree(my);
97
+ }
98
+
99
+ static void free_mysqlres(struct mysql_res* resp)
100
+ {
101
+ if (resp->freed == Qfalse) {
102
+ mysql_free_result(resp->res);
103
+ store_result_count--;
104
+ }
105
+ xfree(resp);
106
+ }
107
+
108
+ #if MYSQL_VERSION_ID >= 40101
109
+ static void free_mysqlstmt_memory(struct mysql_stmt *s)
110
+ {
111
+ if (s->param.bind) {
112
+ xfree(s->param.bind);
113
+ s->param.bind = NULL;
114
+ }
115
+ if (s->param.length) {
116
+ xfree(s->param.length);
117
+ s->param.length = NULL;
118
+ }
119
+ if (s->param.buffer) {
120
+ xfree(s->param.buffer);
121
+ s->param.buffer = NULL;
122
+ }
123
+ s->param.n = 0;
124
+ if (s->res) {
125
+ mysql_free_result(s->res);
126
+ s->res = NULL;
127
+ }
128
+ if (s->result.bind) {
129
+ int i;
130
+ for (i = 0; i < s->result.n; i++) {
131
+ if (s->result.bind[i].buffer)
132
+ xfree(s->result.bind[i].buffer);
133
+ s->result.bind[i].buffer = NULL;
134
+ }
135
+ xfree(s->result.bind);
136
+ s->result.bind = NULL;
137
+ }
138
+ if (s->result.is_null) {
139
+ xfree(s->result.is_null);
140
+ s->result.is_null = NULL;
141
+ }
142
+ if (s->result.length) {
143
+ xfree(s->result.length);
144
+ s->result.length = NULL;
145
+ }
146
+ s->result.n = 0;
147
+ }
148
+
149
+ static void free_execute_memory(struct mysql_stmt *s)
150
+ {
151
+ if (s->res && s->result.bind) {
152
+ int i;
153
+ for (i = 0; i < s->result.n; i++) {
154
+ if (s->result.bind[i].buffer)
155
+ xfree(s->result.bind[i].buffer);
156
+ s->result.bind[i].buffer = NULL;
157
+ }
158
+ }
159
+ mysql_stmt_free_result(s->stmt);
160
+ }
161
+
162
+ static void free_mysqlstmt(struct mysql_stmt* s)
163
+ {
164
+ free_mysqlstmt_memory(s);
165
+ if (s->closed == Qfalse)
166
+ mysql_stmt_close(s->stmt);
167
+ if (s->res)
168
+ mysql_free_result(s->res);
169
+ xfree(s);
170
+ }
171
+ #endif
172
+
173
+ static void mysql_raise(MYSQL* m)
174
+ {
175
+ VALUE e = rb_exc_new2(eMysql, mysql_error(m));
176
+ rb_iv_set(e, "errno", INT2FIX(mysql_errno(m)));
177
+ #if MYSQL_VERSION_ID >= 40101
178
+ rb_iv_set(e, "sqlstate", rb_tainted_str_new2(mysql_sqlstate(m)));
179
+ #endif
180
+ rb_exc_raise(e);
181
+ }
182
+
183
+ static VALUE mysqlres2obj(MYSQL_RES* res)
184
+ {
185
+ VALUE obj;
186
+ struct mysql_res* resp;
187
+ obj = Data_Make_Struct(cMysqlRes, struct mysql_res, 0, free_mysqlres, resp);
188
+ rb_iv_set(obj, "colname", Qnil);
189
+ rb_iv_set(obj, "tblcolname", Qnil);
190
+ resp->res = res;
191
+ resp->freed = Qfalse;
192
+ rb_obj_call_init(obj, 0, NULL);
193
+ if (++store_result_count > GC_STORE_RESULT_LIMIT)
194
+ rb_gc();
195
+ return obj;
196
+ }
197
+
198
+ /* make Mysql::Field object */
199
+ static VALUE make_field_obj(MYSQL_FIELD* f)
200
+ {
201
+ VALUE obj;
202
+ if (f == NULL)
203
+ return Qnil;
204
+ obj = rb_obj_alloc(cMysqlField);
205
+ rb_iv_set(obj, "name", f->name? rb_str_freeze(rb_tainted_str_new2(f->name)): Qnil);
206
+ rb_iv_set(obj, "table", f->table? rb_str_freeze(rb_tainted_str_new2(f->table)): Qnil);
207
+ rb_iv_set(obj, "def", f->def? rb_str_freeze(rb_tainted_str_new2(f->def)): Qnil);
208
+ rb_iv_set(obj, "type", INT2NUM(f->type));
209
+ rb_iv_set(obj, "length", INT2NUM(f->length));
210
+ rb_iv_set(obj, "max_length", INT2NUM(f->max_length));
211
+ rb_iv_set(obj, "flags", INT2NUM(f->flags));
212
+ rb_iv_set(obj, "decimals", INT2NUM(f->decimals));
213
+ return obj;
214
+ }
215
+
216
+ /*-------------------------------
217
+ * Mysql class method
218
+ */
219
+
220
+ /* init() */
221
+ static VALUE init(VALUE klass)
222
+ {
223
+ struct mysql* myp;
224
+ VALUE obj;
225
+
226
+ obj = Data_Make_Struct(klass, struct mysql, 0, free_mysql, myp);
227
+ mysql_init(&myp->handler);
228
+ myp->connection = Qfalse;
229
+ myp->query_with_result = Qtrue;
230
+ rb_obj_call_init(obj, 0, NULL);
231
+ return obj;
232
+ }
233
+
234
+ /* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
235
+ static VALUE real_connect(int argc, VALUE* argv, VALUE klass)
236
+ {
237
+ VALUE host, user, passwd, db, port, sock, flag;
238
+ char *h, *u, *p, *d, *s;
239
+ unsigned int pp, f;
240
+ struct mysql* myp;
241
+ VALUE obj;
242
+
243
+ #if MYSQL_VERSION_ID >= 32200
244
+ rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
245
+ d = NILorSTRING(db);
246
+ f = NILorINT(flag);
247
+ #elif MYSQL_VERSION_ID >= 32115
248
+ rb_scan_args(argc, argv, "06", &host, &user, &passwd, &port, &sock, &flag);
249
+ f = NILorINT(flag);
250
+ #else
251
+ rb_scan_args(argc, argv, "05", &host, &user, &passwd, &port, &sock);
252
+ #endif
253
+ h = NILorSTRING(host);
254
+ u = NILorSTRING(user);
255
+ p = NILorSTRING(passwd);
256
+ pp = NILorINT(port);
257
+ s = NILorSTRING(sock);
258
+
259
+ obj = Data_Make_Struct(klass, struct mysql, 0, free_mysql, myp);
260
+ #if MYSQL_VERSION_ID >= 32200
261
+ mysql_init(&myp->handler);
262
+ if (mysql_real_connect(&myp->handler, h, u, p, d, pp, s, f) == NULL)
263
+ #elif MYSQL_VERSION_ID >= 32115
264
+ if (mysql_real_connect(&myp->handler, h, u, p, pp, s, f) == NULL)
265
+ #else
266
+ if (mysql_real_connect(&myp->handler, h, u, p, pp, s) == NULL)
267
+ #endif
268
+ mysql_raise(&myp->handler);
269
+
270
+ myp->handler.reconnect = 0;
271
+ myp->connection = Qtrue;
272
+
273
+ my_bool was_blocking;
274
+
275
+ vio_blocking(myp->handler.net.vio, 0, &was_blocking);
276
+ myp->blocking = vio_is_blocking( myp->handler.net.vio );
277
+
278
+ vio_fastsend( myp->handler.net.vio );
279
+
280
+ myp->query_with_result = Qtrue;
281
+ rb_obj_call_init(obj, argc, argv);
282
+
283
+ return obj;
284
+ }
285
+
286
+ /* escape_string(string) */
287
+ static VALUE escape_string(VALUE klass, VALUE str)
288
+ {
289
+ VALUE ret;
290
+ Check_Type(str, T_STRING);
291
+ ret = rb_str_new(0, (RSTRING_LEN(str))*2+1);
292
+ rb_str_set_len(ret, mysql_escape_string(RSTRING_PTR(ret), RSTRING_PTR(str), RSTRING_LEN(str)));
293
+ return ret;
294
+ }
295
+
296
+ /* client_info() */
297
+ static VALUE client_info(VALUE klass)
298
+ {
299
+ return rb_tainted_str_new2(mysql_get_client_info());
300
+ }
301
+
302
+ #if MYSQL_VERSION_ID >= 32332
303
+ /* my_debug(string) */
304
+ static VALUE my_debug(VALUE obj, VALUE str)
305
+ {
306
+ mysql_debug(StringValuePtr(str));
307
+ return obj;
308
+ }
309
+ #endif
310
+
311
+ #if MYSQL_VERSION_ID >= 40000
312
+ /* client_version() */
313
+ static VALUE client_version(VALUE obj)
314
+ {
315
+ return INT2NUM(mysql_get_client_version());
316
+ }
317
+ #endif
318
+
319
+ /*-------------------------------
320
+ * Mysql object method
321
+ */
322
+
323
+ #if MYSQL_VERSION_ID >= 32200
324
+ /* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
325
+ static VALUE real_connect2(int argc, VALUE* argv, VALUE obj)
326
+ {
327
+ VALUE host, user, passwd, db, port, sock, flag;
328
+ char *h, *u, *p, *d, *s;
329
+ unsigned int pp, f;
330
+ MYSQL* m = GetHandler(obj);
331
+ rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
332
+ d = NILorSTRING(db);
333
+ f = NILorINT(flag);
334
+ h = NILorSTRING(host);
335
+ u = NILorSTRING(user);
336
+ p = NILorSTRING(passwd);
337
+ pp = NILorINT(port);
338
+ s = NILorSTRING(sock);
339
+
340
+ if (mysql_real_connect(m, h, u, p, d, pp, s, f) == NULL)
341
+ mysql_raise(m);
342
+ m->reconnect = 0;
343
+ GetMysqlStruct(obj)->connection = Qtrue;
344
+
345
+ return obj;
346
+ }
347
+
348
+ /* options(opt, value=nil) */
349
+ static VALUE options(int argc, VALUE* argv, VALUE obj)
350
+ {
351
+ VALUE opt, val;
352
+ int n;
353
+ my_bool b;
354
+ char* v;
355
+ MYSQL* m = GetHandler(obj);
356
+
357
+ rb_scan_args(argc, argv, "11", &opt, &val);
358
+ switch(NUM2INT(opt)) {
359
+ case MYSQL_OPT_CONNECT_TIMEOUT:
360
+ #if MYSQL_VERSION_ID >= 40100
361
+ case MYSQL_OPT_PROTOCOL:
362
+ #endif
363
+ #if MYSQL_VERSION_ID >= 40101
364
+ case MYSQL_OPT_READ_TIMEOUT:
365
+ case MYSQL_OPT_WRITE_TIMEOUT:
366
+ #endif
367
+ if (val == Qnil)
368
+ rb_raise(rb_eArgError, "wrong # of arguments(1 for 2)");
369
+ n = NUM2INT(val);
370
+ v = (char*)&n;
371
+ break;
372
+ case MYSQL_INIT_COMMAND:
373
+ case MYSQL_READ_DEFAULT_FILE:
374
+ case MYSQL_READ_DEFAULT_GROUP:
375
+ #if MYSQL_VERSION_ID >= 32349
376
+ case MYSQL_SET_CHARSET_DIR:
377
+ case MYSQL_SET_CHARSET_NAME:
378
+ #endif
379
+ #if MYSQL_VERSION_ID >= 40100
380
+ case MYSQL_SHARED_MEMORY_BASE_NAME:
381
+ #endif
382
+ #if MYSQL_VERSION_ID >= 40101
383
+ case MYSQL_SET_CLIENT_IP:
384
+ #endif
385
+ if (val == Qnil)
386
+ rb_raise(rb_eArgError, "wrong # of arguments(1 for 2)");
387
+ v = StringValuePtr(val);
388
+ break;
389
+ #if MYSQL_VERSION_ID >= 40101
390
+ case MYSQL_SECURE_AUTH:
391
+ if (val == Qnil || val == Qfalse)
392
+ b = 1;
393
+ else
394
+ b = 0;
395
+ v = (char*)&b;
396
+ break;
397
+ #endif
398
+ #if MYSQL_VERSION_ID >= 32349
399
+ case MYSQL_OPT_LOCAL_INFILE:
400
+ if (val == Qnil || val == Qfalse)
401
+ v = NULL;
402
+ else {
403
+ n = 1;
404
+ v = (char*)&n;
405
+ }
406
+ break;
407
+ #endif
408
+ default:
409
+ v = NULL;
410
+ }
411
+
412
+ if (mysql_options(m, NUM2INT(opt), v) != 0)
413
+ rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
414
+ return obj;
415
+ }
416
+ #endif
417
+
418
+ #if MYSQL_VERSION_ID >= 32332
419
+ /* real_escape_string(string) */
420
+ static VALUE real_escape_string(VALUE obj, VALUE str)
421
+ {
422
+ MYSQL* m = GetHandler(obj);
423
+ VALUE ret;
424
+ Check_Type(str, T_STRING);
425
+ ret = rb_str_new(0, (RSTRING_LEN(str))*2+1);
426
+ rb_str_set_len(ret, mysql_real_escape_string(m, RSTRING_PTR(ret), RSTRING_PTR(str), RSTRING_LEN(str)));
427
+ return ret;
428
+ }
429
+ #endif
430
+
431
+ /* initialize() */
432
+ static VALUE initialize(int argc, VALUE* argv, VALUE obj)
433
+ {
434
+ return obj;
435
+ }
436
+
437
+ /* affected_rows() */
438
+ static VALUE affected_rows(VALUE obj)
439
+ {
440
+ return INT2NUM(mysql_affected_rows(GetHandler(obj)));
441
+ }
442
+
443
+ #if MYSQL_VERSION_ID >= 32303
444
+ /* change_user(user=nil, passwd=nil, db=nil) */
445
+ static VALUE change_user(int argc, VALUE* argv, VALUE obj)
446
+ {
447
+ VALUE user, passwd, db;
448
+ char *u, *p, *d;
449
+ MYSQL* m = GetHandler(obj);
450
+ rb_scan_args(argc, argv, "03", &user, &passwd, &db);
451
+ u = NILorSTRING(user);
452
+ p = NILorSTRING(passwd);
453
+ d = NILorSTRING(db);
454
+ if (mysql_change_user(m, u, p, d) != 0)
455
+ mysql_raise(m);
456
+ return obj;
457
+ }
458
+ #endif
459
+
460
+ #if MYSQL_VERSION_ID >= 32321
461
+ /* character_set_name() */
462
+ static VALUE character_set_name(VALUE obj)
463
+ {
464
+ return rb_tainted_str_new2(mysql_character_set_name(GetHandler(obj)));
465
+ }
466
+ #endif
467
+
468
+ /* close() */
469
+ static VALUE my_close(VALUE obj)
470
+ {
471
+ MYSQL* m = GetHandler(obj);
472
+ mysql_close(m);
473
+ GetMysqlStruct(obj)->connection = Qfalse;
474
+ return obj;
475
+ }
476
+
477
+ #if MYSQL_VERSION_ID < 40000
478
+ /* create_db(db) */
479
+ static VALUE create_db(VALUE obj, VALUE db)
480
+ {
481
+ MYSQL* m = GetHandler(obj);
482
+ if (mysql_create_db(m, StringValuePtr(db)) != 0)
483
+ mysql_raise(m);
484
+ return obj;
485
+ }
486
+
487
+ /* drop_db(db) */
488
+ static VALUE drop_db(VALUE obj, VALUE db)
489
+ {
490
+ MYSQL* m = GetHandler(obj);
491
+ if (mysql_drop_db(m, StringValuePtr(db)) != 0)
492
+ mysql_raise(m);
493
+ return obj;
494
+ }
495
+ #endif
496
+
497
+ #if MYSQL_VERSION_ID >= 32332
498
+ /* dump_debug_info() */
499
+ static VALUE dump_debug_info(VALUE obj)
500
+ {
501
+ MYSQL* m = GetHandler(obj);
502
+ if (mysql_dump_debug_info(m) != 0)
503
+ mysql_raise(m);
504
+ return obj;
505
+ }
506
+ #endif
507
+
508
+ /* errno() */
509
+ static VALUE my_errno(VALUE obj)
510
+ {
511
+ return INT2NUM(mysql_errno(GetHandler(obj)));
512
+ }
513
+
514
+ /* error() */
515
+ static VALUE my_error(VALUE obj)
516
+ {
517
+ return rb_str_new2(mysql_error(GetHandler(obj)));
518
+ }
519
+
520
+ /* field_count() */
521
+ static VALUE field_count(VALUE obj)
522
+ {
523
+ return INT2NUM(mysql_field_count(GetHandler(obj)));
524
+ }
525
+
526
+ /* host_info() */
527
+ static VALUE host_info(VALUE obj)
528
+ {
529
+ return rb_tainted_str_new2(mysql_get_host_info(GetHandler(obj)));
530
+ }
531
+
532
+ /* proto_info() */
533
+ static VALUE proto_info(VALUE obj)
534
+ {
535
+ return INT2NUM(mysql_get_proto_info(GetHandler(obj)));
536
+ }
537
+
538
+ /* server_info() */
539
+ static VALUE server_info(VALUE obj)
540
+ {
541
+ return rb_tainted_str_new2(mysql_get_server_info(GetHandler(obj)));
542
+ }
543
+
544
+ /* info() */
545
+ static VALUE info(VALUE obj)
546
+ {
547
+ const char* p = mysql_info(GetHandler(obj));
548
+ return p? rb_tainted_str_new2(p): Qnil;
549
+ }
550
+
551
+ /* insert_id() */
552
+ static VALUE insert_id(VALUE obj)
553
+ {
554
+ return INT2NUM(mysql_insert_id(GetHandler(obj)));
555
+ }
556
+
557
+ /* kill(pid) */
558
+ static VALUE my_kill(VALUE obj, VALUE pid)
559
+ {
560
+ int p = NUM2INT(pid);
561
+ MYSQL* m = GetHandler(obj);
562
+ if (mysql_kill(m, p) != 0)
563
+ mysql_raise(m);
564
+ return obj;
565
+ }
566
+
567
+ /* list_dbs(db=nil) */
568
+ static VALUE list_dbs(int argc, VALUE* argv, VALUE obj)
569
+ {
570
+ unsigned int i, n;
571
+ VALUE db, ret;
572
+ MYSQL* m = GetHandler(obj);
573
+ MYSQL_RES* res;
574
+
575
+ rb_scan_args(argc, argv, "01", &db);
576
+ res = mysql_list_dbs(m, NILorSTRING(db));
577
+ if (res == NULL)
578
+ mysql_raise(m);
579
+
580
+ n = mysql_num_rows(res);
581
+ ret = rb_ary_new2(n);
582
+ for (i=0; i<n; i++)
583
+ rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
584
+ mysql_free_result(res);
585
+ return ret;
586
+ }
587
+
588
+ /* list_fields(table, field=nil) */
589
+ static VALUE list_fields(int argc, VALUE* argv, VALUE obj)
590
+ {
591
+ VALUE table, field;
592
+ MYSQL* m = GetHandler(obj);
593
+ MYSQL_RES* res;
594
+ rb_scan_args(argc, argv, "11", &table, &field);
595
+ res = mysql_list_fields(m, StringValuePtr(table), NILorSTRING(field));
596
+ if (res == NULL)
597
+ mysql_raise(m);
598
+ return mysqlres2obj(res);
599
+ }
600
+
601
+ /* list_processes() */
602
+ static VALUE list_processes(VALUE obj)
603
+ {
604
+ MYSQL* m = GetHandler(obj);
605
+ MYSQL_RES* res = mysql_list_processes(m);
606
+ if (res == NULL)
607
+ mysql_raise(m);
608
+ return mysqlres2obj(res);
609
+ }
610
+
611
+ /* list_tables(table=nil) */
612
+ static VALUE list_tables(int argc, VALUE* argv, VALUE obj)
613
+ {
614
+ VALUE table;
615
+ MYSQL* m = GetHandler(obj);
616
+ MYSQL_RES* res;
617
+ unsigned int i, n;
618
+ VALUE ret;
619
+
620
+ rb_scan_args(argc, argv, "01", &table);
621
+ res = mysql_list_tables(m, NILorSTRING(table));
622
+ if (res == NULL)
623
+ mysql_raise(m);
624
+
625
+ n = mysql_num_rows(res);
626
+ ret = rb_ary_new2(n);
627
+ for (i=0; i<n; i++)
628
+ rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
629
+ mysql_free_result(res);
630
+ return ret;
631
+ }
632
+
633
+ /* ping() */
634
+ static VALUE ping(VALUE obj)
635
+ {
636
+ MYSQL* m = GetHandler(obj);
637
+ if (mysql_ping(m) != 0)
638
+ mysql_raise(m);
639
+ return obj;
640
+ }
641
+
642
+ /* refresh(r) */
643
+ static VALUE refresh(VALUE obj, VALUE r)
644
+ {
645
+ MYSQL* m = GetHandler(obj);
646
+ if (mysql_refresh(m, NUM2INT(r)) != 0)
647
+ mysql_raise(m);
648
+ return obj;
649
+ }
650
+
651
+ /* reload() */
652
+ static VALUE reload(VALUE obj)
653
+ {
654
+ MYSQL* m = GetHandler(obj);
655
+ if (mysql_reload(m) != 0)
656
+ mysql_raise(m);
657
+ return obj;
658
+ }
659
+
660
+ /* select_db(db) */
661
+ static VALUE select_db(VALUE obj, VALUE db)
662
+ {
663
+ MYSQL* m = GetHandler(obj);
664
+ if (mysql_select_db(m, StringValuePtr(db)) != 0)
665
+ mysql_raise(m);
666
+ return obj;
667
+ }
668
+
669
+ /* shutdown() */
670
+ static VALUE my_shutdown(int argc, VALUE* argv, VALUE obj)
671
+ {
672
+ MYSQL* m = GetHandler(obj);
673
+ VALUE level;
674
+
675
+ rb_scan_args(argc, argv, "01", &level);
676
+ #if MYSQL_VERSION_ID >= 40103
677
+ if (mysql_shutdown(m, NIL_P(level) ? SHUTDOWN_DEFAULT : NUM2INT(level)) != 0)
678
+ #else
679
+ if (mysql_shutdown(m) != 0)
680
+ #endif
681
+ mysql_raise(m);
682
+ return obj;
683
+ }
684
+
685
+ /* stat() */
686
+ static VALUE my_stat(VALUE obj)
687
+ {
688
+ MYSQL* m = GetHandler(obj);
689
+ const char* s = mysql_stat(m);
690
+ if (s == NULL)
691
+ mysql_raise(m);
692
+ return rb_tainted_str_new2(s);
693
+ }
694
+
695
+ /* store_result() */
696
+ static VALUE store_result(VALUE obj)
697
+ {
698
+ MYSQL* m = GetHandler(obj);
699
+ MYSQL_RES* res = mysql_store_result(m);
700
+ if (res == NULL)
701
+ mysql_raise(m);
702
+ return mysqlres2obj(res);
703
+ }
704
+
705
+ /* thread_id() */
706
+ static VALUE thread_id(VALUE obj)
707
+ {
708
+ return INT2NUM(mysql_thread_id(GetHandler(obj)));
709
+ }
710
+
711
+ /* use_result() */
712
+ static VALUE use_result(VALUE obj)
713
+ {
714
+ MYSQL* m = GetHandler(obj);
715
+ MYSQL_RES* res = mysql_use_result(m);
716
+ if (res == NULL)
717
+ mysql_raise(m);
718
+ return mysqlres2obj(res);
719
+ }
720
+
721
+ static VALUE res_free(VALUE);
722
+ /* query(sql) */
723
+ static VALUE query(VALUE obj, VALUE sql)
724
+ {
725
+ int loop = 0;
726
+ MYSQL* m = GetHandler(obj);
727
+ Check_Type(sql, T_STRING);
728
+ if (GetMysqlStruct(obj)->connection == Qfalse) {
729
+ rb_raise(eMysql, "query: not connected");
730
+ }
731
+ if (rb_block_given_p()) {
732
+ if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
733
+ mysql_raise(m);
734
+ do {
735
+ MYSQL_RES* res = mysql_store_result(m);
736
+ if (res == NULL) {
737
+ if (mysql_field_count(m) != 0)
738
+ mysql_raise(m);
739
+ } else {
740
+ VALUE robj = mysqlres2obj(res);
741
+ rb_ensure(rb_yield, robj, res_free, robj);
742
+ }
743
+ #if MYSQL_VERSION_ID >= 40101
744
+ if ((loop = mysql_next_result(m)) > 0)
745
+ mysql_raise(m);
746
+ } while (loop == 0);
747
+ #else
748
+ } while (0);
749
+ #endif
750
+ return obj;
751
+ }
752
+ if (mysql_real_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
753
+ mysql_raise(m);
754
+ if (GetMysqlStruct(obj)->query_with_result == Qfalse)
755
+ return obj;
756
+ if (mysql_field_count(m) == 0)
757
+ return Qnil;
758
+ return store_result(obj);
759
+ }
760
+
761
+ /* socket */
762
+ static VALUE socket(VALUE obj)
763
+ {
764
+ MYSQL* m = GetHandler(obj);
765
+ return INT2NUM(m->net.fd);
766
+ }
767
+
768
+ /* socket_type */
769
+ static VALUE socket_type(VALUE obj)
770
+ {
771
+ MYSQL* m = GetHandler(obj);
772
+ VALUE description = vio_description( m->net.vio );
773
+ return NILorSTRING( description );
774
+ }
775
+
776
+ /* blocking */
777
+ static VALUE blocking(VALUE obj){
778
+ return ( GetMysqlStruct(obj)->blocking ? Qtrue : Qfalse );
779
+ }
780
+
781
+ /* readable(timeout=nil) */
782
+ static VALUE readable( int argc, VALUE* argv, VALUE obj )
783
+ {
784
+ MYSQL* m = GetHandler(obj);
785
+
786
+ VALUE timeout;
787
+
788
+ rb_scan_args(argc, argv, "01", &timeout);
789
+
790
+ if ( NIL_P( timeout ) ){
791
+ timeout = m->net.read_timeout;
792
+ }
793
+
794
+ return ( vio_poll_read( m->net.vio, INT2NUM(timeout) ) == 0 ? Qtrue : Qfalse );
795
+ }
796
+
797
+ /* send_query(sql) */
798
+ static VALUE send_query(VALUE obj, VALUE sql)
799
+ {
800
+ MYSQL* m = GetHandler(obj);
801
+
802
+ Check_Type(sql, T_STRING);
803
+ if (GetMysqlStruct(obj)->connection == Qfalse) {
804
+ rb_raise(eMysql, "query: not connected");
805
+ }
806
+ if (mysql_send_query(m, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
807
+ mysql_raise(m);
808
+ return Qnil;
809
+ }
810
+
811
+ /* get_result */
812
+ static VALUE get_result(VALUE obj)
813
+ {
814
+ MYSQL* m = GetHandler(obj);
815
+ if (GetMysqlStruct(obj)->connection == Qfalse) {
816
+ rb_raise(eMysql, "query: not connected");
817
+ }
818
+ if (mysql_read_query_result(m) != 0)
819
+ mysql_raise(m);
820
+ if (GetMysqlStruct(obj)->query_with_result == Qfalse)
821
+ return obj;
822
+ if (mysql_field_count(m) == 0)
823
+ return Qnil;
824
+ return store_result(obj);
825
+ }
826
+
827
+ /* async_query(sql,timeout=nil) */
828
+ static VALUE async_query(int argc, VALUE* argv, VALUE obj)
829
+ {
830
+ MYSQL* m = GetHandler(obj);
831
+ VALUE sql, timeout;
832
+ fd_set read;
833
+ int ret;
834
+
835
+ rb_scan_args(argc, argv, "11", &sql, &timeout);
836
+
837
+ send_query(obj,sql);
838
+
839
+ timeout = ( NIL_P(timeout) ? m->net.read_timeout : INT2NUM(timeout) );
840
+
841
+ VALUE args[1];
842
+ args[0] = timeout;
843
+
844
+ struct timeval tv = { tv_sec: timeout, tv_usec: 0 };
845
+
846
+ FD_ZERO(&read);
847
+ FD_SET(m->net.fd, &read);
848
+
849
+ for(;;) {
850
+ ret = rb_thread_select(m->net.fd + 1, &read, NULL, NULL, &tv);
851
+ if (ret < 0) {
852
+ rb_raise(eMysql, "query: timeout");
853
+ }
854
+
855
+ if (ret == 0) {
856
+ continue;
857
+ }
858
+
859
+ if ( vio_poll_read( m->net.vio, INT2NUM(timeout) ) == 0 ) {
860
+ break;
861
+ }
862
+ }
863
+
864
+ return get_result(obj);
865
+ }
866
+
867
+ #if MYSQL_VERSION_ID >= 40100
868
+ /* server_version() */
869
+ static VALUE server_version(VALUE obj)
870
+ {
871
+ return INT2NUM(mysql_get_server_version(GetHandler(obj)));
872
+ }
873
+
874
+ /* warning_count() */
875
+ static VALUE warning_count(VALUE obj)
876
+ {
877
+ return INT2NUM(mysql_warning_count(GetHandler(obj)));
878
+ }
879
+
880
+ /* commit() */
881
+ static VALUE commit(VALUE obj)
882
+ {
883
+ MYSQL* m = GetHandler(obj);
884
+ if (mysql_commit(m) != 0)
885
+ mysql_raise(m);
886
+ return obj;
887
+ }
888
+
889
+ /* rollback() */
890
+ static VALUE rollback(VALUE obj)
891
+ {
892
+ MYSQL* m = GetHandler(obj);
893
+ if (mysql_rollback(m) != 0)
894
+ mysql_raise(m);
895
+ return obj;
896
+ }
897
+
898
+ /* autocommit() */
899
+ static VALUE autocommit(VALUE obj, VALUE mode)
900
+ {
901
+ MYSQL* m = GetHandler(obj);
902
+ int f;
903
+ f = (mode == Qnil || mode == Qfalse || (rb_type(mode) == T_FIXNUM && NUM2INT(mode) == 0)) ? 0 : 1;
904
+ if (mysql_autocommit(m, f) != 0)
905
+ mysql_raise(m);
906
+ return obj;
907
+ }
908
+ #endif
909
+
910
+ #ifdef HAVE_MYSQL_SSL_SET
911
+ /* ssl_set(key=nil, cert=nil, ca=nil, capath=nil, cipher=nil) */
912
+ static VALUE ssl_set(int argc, VALUE* argv, VALUE obj)
913
+ {
914
+ VALUE key, cert, ca, capath, cipher;
915
+ char *s_key, *s_cert, *s_ca, *s_capath, *s_cipher;
916
+ MYSQL* m = GetHandler(obj);
917
+ rb_scan_args(argc, argv, "05", &key, &cert, &ca, &capath, &cipher);
918
+ s_key = NILorSTRING(key);
919
+ s_cert = NILorSTRING(cert);
920
+ s_ca = NILorSTRING(ca);
921
+ s_capath = NILorSTRING(capath);
922
+ s_cipher = NILorSTRING(cipher);
923
+ mysql_ssl_set(m, s_key, s_cert, s_ca, s_capath, s_cipher);
924
+ return obj;
925
+ }
926
+ #endif
927
+
928
+ #if MYSQL_VERSION_ID >= 40100
929
+ /* more_results() */
930
+ static VALUE more_results(VALUE obj)
931
+ {
932
+ if (mysql_more_results(GetHandler(obj)) == 0)
933
+ return Qfalse;
934
+ else
935
+ return Qtrue;
936
+ }
937
+
938
+ static VALUE next_result(VALUE obj)
939
+ {
940
+ MYSQL* m = GetHandler(obj);
941
+ int ret;
942
+ ret = mysql_next_result(m);
943
+ if (ret > 0)
944
+ mysql_raise(m);
945
+ if (ret == 0)
946
+ return Qtrue;
947
+ return Qfalse;
948
+ }
949
+ #endif
950
+
951
+ #if MYSQL_VERSION_ID >= 40101
952
+ /* set_server_option(option) */
953
+ static VALUE set_server_option(VALUE obj, VALUE option)
954
+ {
955
+ MYSQL *m = GetHandler(obj);
956
+ if (mysql_set_server_option(m, NUM2INT(option)) != 0)
957
+ mysql_raise(m);
958
+ return obj;
959
+ }
960
+
961
+ /* sqlstate() */
962
+ static VALUE sqlstate(VALUE obj)
963
+ {
964
+ MYSQL *m = GetHandler(obj);
965
+ return rb_tainted_str_new2(mysql_sqlstate(m));
966
+ }
967
+ #endif
968
+
969
+ #if MYSQL_VERSION_ID >= 40102
970
+ /* stmt_init() */
971
+ static VALUE stmt_init(VALUE obj)
972
+ {
973
+ MYSQL *m = GetHandler(obj);
974
+ MYSQL_STMT *s;
975
+ struct mysql_stmt* stmt;
976
+ my_bool true = 1;
977
+ VALUE st_obj;
978
+
979
+ if ((s = mysql_stmt_init(m)) == NULL)
980
+ mysql_raise(m);
981
+ if (mysql_stmt_attr_set(s, STMT_ATTR_UPDATE_MAX_LENGTH, &true))
982
+ rb_raise(rb_eArgError, "mysql_stmt_attr_set() failed");
983
+ st_obj = Data_Make_Struct(cMysqlStmt, struct mysql_stmt, 0, free_mysqlstmt, stmt);
984
+ memset(stmt, 0, sizeof(*stmt));
985
+ stmt->stmt = s;
986
+ stmt->closed = Qfalse;
987
+ return st_obj;
988
+ }
989
+
990
+ static VALUE stmt_prepare(VALUE obj, VALUE query);
991
+ /* prepare(query) */
992
+ static VALUE prepare(VALUE obj, VALUE query)
993
+ {
994
+ VALUE st;
995
+ st = stmt_init(obj);
996
+ return stmt_prepare(st, query);
997
+ }
998
+ #endif
999
+
1000
+ /* query_with_result() */
1001
+ static VALUE query_with_result(VALUE obj)
1002
+ {
1003
+ return GetMysqlStruct(obj)->query_with_result? Qtrue: Qfalse;
1004
+ }
1005
+
1006
+ /* query_with_result=(flag) */
1007
+ static VALUE query_with_result_set(VALUE obj, VALUE flag)
1008
+ {
1009
+ if (TYPE(flag) != T_TRUE && TYPE(flag) != T_FALSE)
1010
+ rb_raise(rb_eTypeError, "invalid type, required true or false.");
1011
+ GetMysqlStruct(obj)->query_with_result = flag;
1012
+ return flag;
1013
+ }
1014
+
1015
+ /* reconnect() */
1016
+ static VALUE reconnect(VALUE obj)
1017
+ {
1018
+ return GetHandler(obj)->reconnect ? Qtrue : Qfalse;
1019
+ }
1020
+
1021
+ /* reconnect=(flag) */
1022
+ static VALUE reconnect_set(VALUE obj, VALUE flag)
1023
+ {
1024
+ GetHandler(obj)->reconnect = (flag == Qnil || flag == Qfalse) ? 0 : 1;
1025
+ return flag;
1026
+ }
1027
+
1028
+ /*-------------------------------
1029
+ * Mysql::Result object method
1030
+ */
1031
+
1032
+ /* check if already freed */
1033
+ static void check_free(VALUE obj)
1034
+ {
1035
+ struct mysql_res* resp = DATA_PTR(obj);
1036
+ if (resp->freed == Qtrue)
1037
+ rb_raise(eMysql, "Mysql::Result object is already freed");
1038
+ }
1039
+
1040
+ /* data_seek(offset) */
1041
+ static VALUE data_seek(VALUE obj, VALUE offset)
1042
+ {
1043
+ check_free(obj);
1044
+ mysql_data_seek(GetMysqlRes(obj), NUM2INT(offset));
1045
+ return obj;
1046
+ }
1047
+
1048
+ /* fetch_field() */
1049
+ static VALUE fetch_field(VALUE obj)
1050
+ {
1051
+ check_free(obj);
1052
+ return make_field_obj(mysql_fetch_field(GetMysqlRes(obj)));
1053
+ }
1054
+
1055
+ /* fetch_fields() */
1056
+ static VALUE fetch_fields(VALUE obj)
1057
+ {
1058
+ MYSQL_RES* res;
1059
+ MYSQL_FIELD* f;
1060
+ unsigned int n;
1061
+ VALUE ret;
1062
+ unsigned int i;
1063
+ check_free(obj);
1064
+ res = GetMysqlRes(obj);
1065
+ f = mysql_fetch_fields(res);
1066
+ n = mysql_num_fields(res);
1067
+ ret = rb_ary_new2(n);
1068
+ for (i=0; i<n; i++)
1069
+ rb_ary_store(ret, i, make_field_obj(&f[i]));
1070
+ return ret;
1071
+ }
1072
+
1073
+ /* fetch_field_direct(nr) */
1074
+ static VALUE fetch_field_direct(VALUE obj, VALUE nr)
1075
+ {
1076
+ MYSQL_RES* res;
1077
+ unsigned int max;
1078
+ unsigned int n;
1079
+ check_free(obj);
1080
+ res = GetMysqlRes(obj);
1081
+ max = mysql_num_fields(res);
1082
+ n = NUM2INT(nr);
1083
+ if (n >= max)
1084
+ rb_raise(eMysql, "%d: out of range (max: %d)", n, max-1);
1085
+ #if MYSQL_VERSION_ID >= 32226
1086
+ return make_field_obj(mysql_fetch_field_direct(res, n));
1087
+ #else
1088
+ return make_field_obj(&mysql_fetch_field_direct(res, n));
1089
+ #endif
1090
+ }
1091
+
1092
+ /* fetch_lengths() */
1093
+ static VALUE fetch_lengths(VALUE obj)
1094
+ {
1095
+ MYSQL_RES* res;
1096
+ unsigned int n;
1097
+ unsigned long* lengths;
1098
+ VALUE ary;
1099
+ unsigned int i;
1100
+ check_free(obj);
1101
+ res = GetMysqlRes(obj);
1102
+ n = mysql_num_fields(res);
1103
+ lengths = mysql_fetch_lengths(res);
1104
+ if (lengths == NULL)
1105
+ return Qnil;
1106
+ ary = rb_ary_new2(n);
1107
+ for (i=0; i<n; i++)
1108
+ rb_ary_store(ary, i, INT2NUM(lengths[i]));
1109
+ return ary;
1110
+ }
1111
+
1112
+ /* fetch_row() */
1113
+ static VALUE fetch_row(VALUE obj)
1114
+ {
1115
+ MYSQL_RES* res;
1116
+ unsigned int n;
1117
+ MYSQL_ROW row;
1118
+ unsigned long* lengths;
1119
+ VALUE ary;
1120
+ unsigned int i;
1121
+ check_free(obj);
1122
+ res = GetMysqlRes(obj);
1123
+ n = mysql_num_fields(res);
1124
+ row = mysql_fetch_row(res);
1125
+ lengths = mysql_fetch_lengths(res);
1126
+ if (row == NULL)
1127
+ return Qnil;
1128
+ ary = rb_ary_new2(n);
1129
+ for (i=0; i<n; i++)
1130
+ rb_ary_store(ary, i, row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
1131
+ return ary;
1132
+ }
1133
+
1134
+ /* process_all_hashes (internal) */
1135
+ static VALUE process_all_hashes(VALUE obj, VALUE with_table, int build_array, int yield)
1136
+ {
1137
+ MYSQL_RES* res = GetMysqlRes(obj);
1138
+ unsigned int n = mysql_num_fields(res);
1139
+ VALUE ary;
1140
+ if(build_array)
1141
+ ary = rb_ary_new();
1142
+ MYSQL_ROW row = mysql_fetch_row(res); // grab one off the top, to determine the rows
1143
+ if (row == NULL){
1144
+ if(build_array){
1145
+ return ary;
1146
+ }else{
1147
+ return Qnil;
1148
+ }
1149
+ }
1150
+ MYSQL_FIELD* fields = mysql_fetch_fields(res);
1151
+ unsigned int i;
1152
+ VALUE hash;
1153
+ VALUE colname;
1154
+
1155
+ if (with_table == Qfalse) {
1156
+ colname = rb_iv_get(obj, "colname");
1157
+ if (colname == Qnil) {
1158
+ colname = rb_ary_new2(n);
1159
+ for (i=0; i<n; i++) {
1160
+ VALUE s = rb_tainted_str_new2(fields[i].name);
1161
+ rb_obj_freeze(s);
1162
+ rb_ary_store(colname, i, s);
1163
+ }
1164
+ rb_obj_freeze(colname);
1165
+ rb_iv_set(obj, "colname", colname);
1166
+ }
1167
+ } else {
1168
+ colname = rb_iv_get(obj, "tblcolname");
1169
+ if (colname == Qnil) {
1170
+ colname = rb_ary_new2(n);
1171
+ for (i=0; i<n; i++) {
1172
+ int len = strlen(fields[i].table)+strlen(fields[i].name)+1;
1173
+ VALUE s = rb_tainted_str_new(NULL, len);
1174
+ snprintf(RSTRING_PTR(s), len+1, "%s.%s", fields[i].table, fields[i].name);
1175
+ rb_obj_freeze(s);
1176
+ rb_ary_store(colname, i, s);
1177
+ }
1178
+ rb_obj_freeze(colname);
1179
+ rb_iv_set(obj, "tblcolname", colname);
1180
+ }
1181
+ }
1182
+
1183
+ unsigned long* lengths = NULL;
1184
+ while(row != NULL)
1185
+ {
1186
+ hash = rb_hash_new();
1187
+ lengths = mysql_fetch_lengths(res);
1188
+ for (i=0; i<n; i++) {
1189
+ rb_hash_aset(hash, rb_ary_entry(colname, i), row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
1190
+ }
1191
+ if(build_array)
1192
+ rb_ary_push(ary, hash);
1193
+
1194
+ if(yield)
1195
+ rb_yield(hash);
1196
+
1197
+ row = mysql_fetch_row(res);
1198
+ }
1199
+
1200
+ /* pass back apropriate return values */
1201
+ if(build_array)
1202
+ return ary;
1203
+
1204
+ if(yield)
1205
+ return obj;
1206
+ }
1207
+
1208
+ /* fetch_hash2 (internal) */
1209
+ static VALUE fetch_hash2(VALUE obj, VALUE with_table)
1210
+ {
1211
+ MYSQL_RES* res = GetMysqlRes(obj);
1212
+ unsigned int n = mysql_num_fields(res);
1213
+ MYSQL_ROW row = mysql_fetch_row(res);
1214
+ unsigned long* lengths = mysql_fetch_lengths(res);
1215
+ MYSQL_FIELD* fields = mysql_fetch_fields(res);
1216
+ unsigned int i;
1217
+ VALUE hash;
1218
+ VALUE colname;
1219
+ if (row == NULL)
1220
+ return Qnil;
1221
+ hash = rb_hash_new();
1222
+
1223
+ if (with_table == Qfalse) {
1224
+ colname = rb_iv_get(obj, "colname");
1225
+ if (colname == Qnil) {
1226
+ colname = rb_ary_new2(n);
1227
+ for (i=0; i<n; i++) {
1228
+ VALUE s = rb_tainted_str_new2(fields[i].name);
1229
+ rb_obj_freeze(s);
1230
+ rb_ary_store(colname, i, s);
1231
+ }
1232
+ rb_obj_freeze(colname);
1233
+ rb_iv_set(obj, "colname", colname);
1234
+ }
1235
+ } else {
1236
+ colname = rb_iv_get(obj, "tblcolname");
1237
+ if (colname == Qnil) {
1238
+ colname = rb_ary_new2(n);
1239
+ for (i=0; i<n; i++) {
1240
+ int len = strlen(fields[i].table)+strlen(fields[i].name)+1;
1241
+ VALUE s = rb_tainted_str_new(NULL, len);
1242
+ snprintf(RSTRING_PTR(s), len+1, "%s.%s", fields[i].table, fields[i].name);
1243
+ rb_obj_freeze(s);
1244
+ rb_ary_store(colname, i, s);
1245
+ }
1246
+ rb_obj_freeze(colname);
1247
+ rb_iv_set(obj, "tblcolname", colname);
1248
+ }
1249
+ }
1250
+ for (i=0; i<n; i++) {
1251
+ rb_hash_aset(hash, rb_ary_entry(colname, i), row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
1252
+ }
1253
+ return hash;
1254
+ }
1255
+
1256
+ /* fetch_hash(with_table=false) */
1257
+ static VALUE fetch_hash(int argc, VALUE* argv, VALUE obj)
1258
+ {
1259
+ VALUE with_table;
1260
+ check_free(obj);
1261
+ rb_scan_args(argc, argv, "01", &with_table);
1262
+ if (with_table == Qnil)
1263
+ with_table = Qfalse;
1264
+ return fetch_hash2(obj, with_table);
1265
+ }
1266
+
1267
+ /* field_seek(offset) */
1268
+ static VALUE field_seek(VALUE obj, VALUE offset)
1269
+ {
1270
+ check_free(obj);
1271
+ return INT2NUM(mysql_field_seek(GetMysqlRes(obj), NUM2INT(offset)));
1272
+ }
1273
+
1274
+ /* field_tell() */
1275
+ static VALUE field_tell(VALUE obj)
1276
+ {
1277
+ check_free(obj);
1278
+ return INT2NUM(mysql_field_tell(GetMysqlRes(obj)));
1279
+ }
1280
+
1281
+ /* free() */
1282
+ static VALUE res_free(VALUE obj)
1283
+ {
1284
+ struct mysql_res* resp = DATA_PTR(obj);
1285
+ check_free(obj);
1286
+ mysql_free_result(resp->res);
1287
+ resp->freed = Qtrue;
1288
+ store_result_count--;
1289
+ return Qnil;
1290
+ }
1291
+
1292
+ /* num_fields() */
1293
+ static VALUE num_fields(VALUE obj)
1294
+ {
1295
+ check_free(obj);
1296
+ return INT2NUM(mysql_num_fields(GetMysqlRes(obj)));
1297
+ }
1298
+
1299
+ /* num_rows() */
1300
+ static VALUE num_rows(VALUE obj)
1301
+ {
1302
+ check_free(obj);
1303
+ return INT2NUM(mysql_num_rows(GetMysqlRes(obj)));
1304
+ }
1305
+
1306
+ /* row_seek(offset) */
1307
+ static VALUE row_seek(VALUE obj, VALUE offset)
1308
+ {
1309
+ MYSQL_ROW_OFFSET prev_offset;
1310
+ if (CLASS_OF(offset) != cMysqlRowOffset)
1311
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Mysql::RowOffset)", rb_obj_classname(offset));
1312
+ check_free(obj);
1313
+ prev_offset = mysql_row_seek(GetMysqlRes(obj), DATA_PTR(offset));
1314
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, prev_offset);
1315
+ }
1316
+
1317
+ /* row_tell() */
1318
+ static VALUE row_tell(VALUE obj)
1319
+ {
1320
+ MYSQL_ROW_OFFSET offset;
1321
+ check_free(obj);
1322
+ offset = mysql_row_tell(GetMysqlRes(obj));
1323
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, offset);
1324
+ }
1325
+
1326
+ /* each {...} */
1327
+ static VALUE each(VALUE obj)
1328
+ {
1329
+ VALUE row;
1330
+ check_free(obj);
1331
+ while ((row = fetch_row(obj)) != Qnil)
1332
+ rb_yield(row);
1333
+ return obj;
1334
+ }
1335
+
1336
+ /* each_hash(with_table=false) {...} */
1337
+ static VALUE each_hash(int argc, VALUE* argv, VALUE obj)
1338
+ {
1339
+ VALUE with_table;
1340
+ check_free(obj);
1341
+ rb_scan_args(argc, argv, "01", &with_table);
1342
+ if (with_table == Qnil)
1343
+ with_table = Qfalse;
1344
+ process_all_hashes(obj, with_table, 0, 1);
1345
+ return obj;
1346
+ }
1347
+
1348
+ /* all_hashes(with_table=false) -- returns an array of hashes, one hash per row */
1349
+ static VALUE all_hashes(int argc, VALUE* argv, VALUE obj)
1350
+ {
1351
+ VALUE with_table;
1352
+ check_free(obj);
1353
+ rb_scan_args(argc, argv, "01", &with_table);
1354
+ if (with_table == Qnil)
1355
+ with_table = Qfalse;
1356
+ return process_all_hashes(obj, with_table, 1, 0);
1357
+ }
1358
+
1359
+
1360
+ /*-------------------------------
1361
+ * Mysql::Field object method
1362
+ */
1363
+
1364
+ /* hash */
1365
+ static VALUE field_hash(VALUE obj)
1366
+ {
1367
+ VALUE h = rb_hash_new();
1368
+ rb_hash_aset(h, rb_str_new2("name"), rb_iv_get(obj, "name"));
1369
+ rb_hash_aset(h, rb_str_new2("table"), rb_iv_get(obj, "table"));
1370
+ rb_hash_aset(h, rb_str_new2("def"), rb_iv_get(obj, "def"));
1371
+ rb_hash_aset(h, rb_str_new2("type"), rb_iv_get(obj, "type"));
1372
+ rb_hash_aset(h, rb_str_new2("length"), rb_iv_get(obj, "length"));
1373
+ rb_hash_aset(h, rb_str_new2("max_length"), rb_iv_get(obj, "max_length"));
1374
+ rb_hash_aset(h, rb_str_new2("flags"), rb_iv_get(obj, "flags"));
1375
+ rb_hash_aset(h, rb_str_new2("decimals"), rb_iv_get(obj, "decimals"));
1376
+ return h;
1377
+ }
1378
+
1379
+ /* inspect */
1380
+ static VALUE field_inspect(VALUE obj)
1381
+ {
1382
+ VALUE n = rb_iv_get(obj, "name");
1383
+ VALUE s = rb_str_new(0, RSTRING_LEN(n) + 16);
1384
+ sprintf(RSTRING_PTR(s), "#<Mysql::Field:%s>", RSTRING_PTR(n));
1385
+ return s;
1386
+ }
1387
+
1388
+ #define DefineMysqlFieldMemberMethod(m)\
1389
+ static VALUE field_##m(VALUE obj)\
1390
+ {return rb_iv_get(obj, #m);}
1391
+
1392
+ DefineMysqlFieldMemberMethod(name)
1393
+ DefineMysqlFieldMemberMethod(table)
1394
+ DefineMysqlFieldMemberMethod(def)
1395
+ DefineMysqlFieldMemberMethod(type)
1396
+ DefineMysqlFieldMemberMethod(length)
1397
+ DefineMysqlFieldMemberMethod(max_length)
1398
+ DefineMysqlFieldMemberMethod(flags)
1399
+ DefineMysqlFieldMemberMethod(decimals)
1400
+
1401
+ #ifdef IS_NUM
1402
+ /* is_num? */
1403
+ static VALUE field_is_num(VALUE obj)
1404
+ {
1405
+ return IS_NUM(NUM2INT(rb_iv_get(obj, "type"))) ? Qtrue : Qfalse;
1406
+ }
1407
+ #endif
1408
+
1409
+ #ifdef IS_NOT_NULL
1410
+ /* is_not_null? */
1411
+ static VALUE field_is_not_null(VALUE obj)
1412
+ {
1413
+ return IS_NOT_NULL(NUM2INT(rb_iv_get(obj, "flags"))) ? Qtrue : Qfalse;
1414
+ }
1415
+ #endif
1416
+
1417
+ #ifdef IS_PRI_KEY
1418
+ /* is_pri_key? */
1419
+ static VALUE field_is_pri_key(VALUE obj)
1420
+ {
1421
+ return IS_PRI_KEY(NUM2INT(rb_iv_get(obj, "flags"))) ? Qtrue : Qfalse;
1422
+ }
1423
+ #endif
1424
+
1425
+ #if MYSQL_VERSION_ID >= 40102
1426
+ /*-------------------------------
1427
+ * Mysql::Stmt object method
1428
+ */
1429
+
1430
+ /* check if stmt is already closed */
1431
+ static void check_stmt_closed(VALUE obj)
1432
+ {
1433
+ struct mysql_stmt* s = DATA_PTR(obj);
1434
+ if (s->closed == Qtrue)
1435
+ rb_raise(eMysql, "Mysql::Stmt object is already closed");
1436
+ }
1437
+
1438
+ static void mysql_stmt_raise(MYSQL_STMT* s)
1439
+ {
1440
+ VALUE e = rb_exc_new2(eMysql, mysql_stmt_error(s));
1441
+ rb_iv_set(e, "errno", INT2FIX(mysql_stmt_errno(s)));
1442
+ rb_iv_set(e, "sqlstate", rb_tainted_str_new2(mysql_stmt_sqlstate(s)));
1443
+ rb_exc_raise(e);
1444
+ }
1445
+
1446
+ /* affected_rows() */
1447
+ static VALUE stmt_affected_rows(VALUE obj)
1448
+ {
1449
+ struct mysql_stmt* s = DATA_PTR(obj);
1450
+ my_ulonglong n;
1451
+ check_stmt_closed(obj);
1452
+ n = mysql_stmt_affected_rows(s->stmt);
1453
+ return INT2NUM(n);
1454
+ }
1455
+
1456
+ #if 0
1457
+ /* attr_get(option) */
1458
+ static VALUE stmt_attr_get(VALUE obj, VALUE opt)
1459
+ {
1460
+ struct mysql_stmt* s = DATA_PTR(obj);
1461
+ check_stmt_closed(obj);
1462
+ if (NUM2INT(opt) == STMT_ATTR_UPDATE_MAX_LENGTH) {
1463
+ my_bool arg;
1464
+ mysql_stmt_attr_get(s->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &arg);
1465
+ return arg == 1 ? Qtrue : Qfalse;
1466
+ }
1467
+ rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
1468
+ }
1469
+
1470
+ /* attr_set(option, arg) */
1471
+ static VALUE stmt_attr_set(VALUE obj, VALUE opt, VALUE val)
1472
+ {
1473
+ struct mysql_stmt* s = DATA_PTR(obj);
1474
+ check_stmt_closed(obj);
1475
+ if (NUM2INT(opt) == STMT_ATTR_UPDATE_MAX_LENGTH) {
1476
+ my_bool arg;
1477
+ arg = (val == Qnil || val == Qfalse) ? 0 : 1;
1478
+ mysql_stmt_attr_set(s->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &arg);
1479
+ return obj;
1480
+ }
1481
+ rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
1482
+ }
1483
+ #endif
1484
+
1485
+ /* bind_result(bind,...) */
1486
+ static VALUE stmt_bind_result(int argc, VALUE *argv, VALUE obj)
1487
+ {
1488
+ struct mysql_stmt* s = DATA_PTR(obj);
1489
+ int i;
1490
+ MYSQL_FIELD *field;
1491
+
1492
+ check_stmt_closed(obj);
1493
+ if (argc != s->result.n)
1494
+ rb_raise(eMysql, "bind_result: result value count(%d) != number of argument(%d)", s->result.n, argc);
1495
+ for (i = 0; i < argc; i++) {
1496
+ if (argv[i] == Qnil || argv[i] == rb_cNilClass) {
1497
+ field = mysql_fetch_fields(s->res);
1498
+ s->result.bind[i].buffer_type = field[i].type;
1499
+ }
1500
+ else if (argv[i] == rb_cString)
1501
+ s->result.bind[i].buffer_type = MYSQL_TYPE_STRING;
1502
+ else if (argv[i] == rb_cNumeric || argv[i] == rb_cInteger || argv[i] == rb_cFixnum)
1503
+ s->result.bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
1504
+ else if (argv[i] == rb_cFloat)
1505
+ s->result.bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
1506
+ else if (argv[i] == cMysqlTime)
1507
+ s->result.bind[i].buffer_type = MYSQL_TYPE_DATETIME;
1508
+ else
1509
+ rb_raise(rb_eTypeError, "unrecognized class: %s", RSTRING_PTR(rb_inspect(argv[i])));
1510
+ if (mysql_stmt_bind_result(s->stmt, s->result.bind))
1511
+ mysql_stmt_raise(s->stmt);
1512
+ }
1513
+ return obj;
1514
+ }
1515
+
1516
+ /* close() */
1517
+ static VALUE stmt_close(VALUE obj)
1518
+ {
1519
+ struct mysql_stmt* s = DATA_PTR(obj);
1520
+ check_stmt_closed(obj);
1521
+ mysql_stmt_close(s->stmt);
1522
+ s->closed = Qtrue;
1523
+ return Qnil;
1524
+ }
1525
+
1526
+ /* data_seek(offset) */
1527
+ static VALUE stmt_data_seek(VALUE obj, VALUE offset)
1528
+ {
1529
+ struct mysql_stmt* s = DATA_PTR(obj);
1530
+ check_stmt_closed(obj);
1531
+ mysql_stmt_data_seek(s->stmt, NUM2INT(offset));
1532
+ return obj;
1533
+ }
1534
+
1535
+ /* execute(arg,...) */
1536
+ static VALUE stmt_execute(int argc, VALUE *argv, VALUE obj)
1537
+ {
1538
+ struct mysql_stmt *s = DATA_PTR(obj);
1539
+ MYSQL_STMT *stmt = s->stmt;
1540
+ int i;
1541
+
1542
+ check_stmt_closed(obj);
1543
+ free_execute_memory(s);
1544
+ if (s->param.n != argc)
1545
+ rb_raise(eMysql, "execute: param_count(%d) != number of argument(%d)", s->param.n, argc);
1546
+ if (argc > 0) {
1547
+ memset(s->param.bind, 0, sizeof(*(s->param.bind))*argc);
1548
+ for (i = 0; i < argc; i++) {
1549
+ switch (TYPE(argv[i])) {
1550
+ case T_NIL:
1551
+ s->param.bind[i].buffer_type = MYSQL_TYPE_NULL;
1552
+ break;
1553
+ case T_FIXNUM:
1554
+ #if SIZEOF_INT < SIZEOF_LONG
1555
+ s->param.bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
1556
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1557
+ *(LONG_LONG*)(s->param.bind[i].buffer) = FIX2LONG(argv[i]);
1558
+ #else
1559
+ s->param.bind[i].buffer_type = MYSQL_TYPE_LONG;
1560
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1561
+ *(int*)(s->param.bind[i].buffer) = FIX2INT(argv[i]);
1562
+ #endif
1563
+ break;
1564
+ case T_BIGNUM:
1565
+ s->param.bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
1566
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1567
+ *(LONG_LONG*)(s->param.bind[i].buffer) = rb_big2ll(argv[i]);
1568
+ break;
1569
+ case T_FLOAT:
1570
+ s->param.bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
1571
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1572
+ *(double*)(s->param.bind[i].buffer) = NUM2DBL(argv[i]);
1573
+ break;
1574
+ case T_STRING:
1575
+ s->param.bind[i].buffer_type = MYSQL_TYPE_STRING;
1576
+ s->param.bind[i].buffer = RSTRING_PTR(argv[i]);
1577
+ s->param.bind[i].buffer_length = RSTRING_LEN(argv[i]);
1578
+ s->param.length[i] = RSTRING_LEN(argv[i]);
1579
+ s->param.bind[i].length = &(s->param.length[i]);
1580
+ break;
1581
+ default:
1582
+ if (CLASS_OF(argv[i]) == rb_cTime) {
1583
+ MYSQL_TIME t;
1584
+ VALUE a = rb_funcall(argv[i], rb_intern("to_a"), 0);
1585
+ s->param.bind[i].buffer_type = MYSQL_TYPE_DATETIME;
1586
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1587
+ t.second_part = 0;
1588
+ t.neg = 0;
1589
+ t.second = FIX2INT(RARRAY(a)->ptr[0]);
1590
+ t.minute = FIX2INT(RARRAY(a)->ptr[1]);
1591
+ t.hour = FIX2INT(RARRAY(a)->ptr[2]);
1592
+ t.day = FIX2INT(RARRAY(a)->ptr[3]);
1593
+ t.month = FIX2INT(RARRAY(a)->ptr[4]);
1594
+ t.year = FIX2INT(RARRAY(a)->ptr[5]);
1595
+ *(MYSQL_TIME*)&(s->param.buffer[i]) = t;
1596
+ } else if (CLASS_OF(argv[i]) == cMysqlTime) {
1597
+ MYSQL_TIME t;
1598
+ s->param.bind[i].buffer_type = MYSQL_TYPE_DATETIME;
1599
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1600
+ t.second_part = 0;
1601
+ t.neg = 0;
1602
+ t.second = NUM2INT(rb_iv_get(argv[i], "second"));
1603
+ t.minute = NUM2INT(rb_iv_get(argv[i], "minute"));
1604
+ t.hour = NUM2INT(rb_iv_get(argv[i], "hour"));
1605
+ t.day = NUM2INT(rb_iv_get(argv[i], "day"));
1606
+ t.month = NUM2INT(rb_iv_get(argv[i], "month"));
1607
+ t.year = NUM2INT(rb_iv_get(argv[i], "year"));
1608
+ *(MYSQL_TIME*)&(s->param.buffer[i]) = t;
1609
+ } else
1610
+ rb_raise(rb_eTypeError, "unsupported type: %d", TYPE(argv[i]));
1611
+ }
1612
+ }
1613
+ if (mysql_stmt_bind_param(stmt, s->param.bind))
1614
+ mysql_stmt_raise(stmt);
1615
+ }
1616
+
1617
+ if (mysql_stmt_execute(stmt))
1618
+ mysql_stmt_raise(stmt);
1619
+ if (s->res) {
1620
+ MYSQL_FIELD *field;
1621
+ if (mysql_stmt_store_result(stmt))
1622
+ mysql_stmt_raise(stmt);
1623
+ field = mysql_fetch_fields(s->res);
1624
+ for (i = 0; i < s->result.n; i++) {
1625
+ switch(s->result.bind[i].buffer_type) {
1626
+ case MYSQL_TYPE_NULL:
1627
+ break;
1628
+ case MYSQL_TYPE_TINY:
1629
+ case MYSQL_TYPE_SHORT:
1630
+ case MYSQL_TYPE_YEAR:
1631
+ case MYSQL_TYPE_INT24:
1632
+ case MYSQL_TYPE_LONG:
1633
+ case MYSQL_TYPE_LONGLONG:
1634
+ case MYSQL_TYPE_FLOAT:
1635
+ case MYSQL_TYPE_DOUBLE:
1636
+ s->result.bind[i].buffer = xmalloc(8);
1637
+ s->result.bind[i].buffer_length = 8;
1638
+ memset(s->result.bind[i].buffer, 0, 8);
1639
+ break;
1640
+ case MYSQL_TYPE_DECIMAL:
1641
+ case MYSQL_TYPE_STRING:
1642
+ case MYSQL_TYPE_VAR_STRING:
1643
+ case MYSQL_TYPE_TINY_BLOB:
1644
+ case MYSQL_TYPE_BLOB:
1645
+ case MYSQL_TYPE_MEDIUM_BLOB:
1646
+ case MYSQL_TYPE_LONG_BLOB:
1647
+ #if MYSQL_VERSION_ID >= 50003
1648
+ case MYSQL_TYPE_NEWDECIMAL:
1649
+ case MYSQL_TYPE_BIT:
1650
+ #endif
1651
+ s->result.bind[i].buffer = xmalloc(field[i].max_length);
1652
+ memset(s->result.bind[i].buffer, 0, field[i].max_length);
1653
+ s->result.bind[i].buffer_length = field[i].max_length;
1654
+ break;
1655
+ case MYSQL_TYPE_TIME:
1656
+ case MYSQL_TYPE_DATE:
1657
+ case MYSQL_TYPE_DATETIME:
1658
+ case MYSQL_TYPE_TIMESTAMP:
1659
+ s->result.bind[i].buffer = xmalloc(sizeof(MYSQL_TIME));
1660
+ s->result.bind[i].buffer_length = sizeof(MYSQL_TIME);
1661
+ memset(s->result.bind[i].buffer, 0, sizeof(MYSQL_TIME));
1662
+ break;
1663
+ default:
1664
+ rb_raise(rb_eTypeError, "unknown buffer_type: %d", s->result.bind[i].buffer_type);
1665
+ }
1666
+ }
1667
+ if (mysql_stmt_bind_result(s->stmt, s->result.bind))
1668
+ mysql_stmt_raise(s->stmt);
1669
+ }
1670
+ return obj;
1671
+ }
1672
+
1673
+ /* fetch() */
1674
+ static VALUE stmt_fetch(VALUE obj)
1675
+ {
1676
+ struct mysql_stmt* s = DATA_PTR(obj);
1677
+ VALUE ret;
1678
+ int i;
1679
+ int r;
1680
+
1681
+ check_stmt_closed(obj);
1682
+ r = mysql_stmt_fetch(s->stmt);
1683
+ if (r == MYSQL_NO_DATA)
1684
+ return Qnil;
1685
+ #ifdef MYSQL_DATA_TRUNCATED
1686
+ if (r == MYSQL_DATA_TRUNCATED)
1687
+ rb_raise(rb_eRuntimeError, "unexpectedly data truncated");
1688
+ #endif
1689
+ if (r == 1)
1690
+ mysql_stmt_raise(s->stmt);
1691
+
1692
+ ret = rb_ary_new2(s->result.n);
1693
+ for (i = 0; i < s->result.n; i++) {
1694
+ if (s->result.is_null[i])
1695
+ rb_ary_push(ret, Qnil);
1696
+ else {
1697
+ VALUE v;
1698
+ MYSQL_TIME *t;
1699
+ switch (s->result.bind[i].buffer_type) {
1700
+ case MYSQL_TYPE_TINY:
1701
+ if (s->result.bind[i].is_unsigned)
1702
+ v = UINT2NUM(*(unsigned char *)s->result.bind[i].buffer);
1703
+ else
1704
+ v = INT2NUM(*(signed char *)s->result.bind[i].buffer);
1705
+ break;
1706
+ case MYSQL_TYPE_SHORT:
1707
+ case MYSQL_TYPE_YEAR:
1708
+ if (s->result.bind[i].is_unsigned)
1709
+ v = UINT2NUM(*(unsigned short *)s->result.bind[i].buffer);
1710
+ else
1711
+ v = INT2NUM(*(short *)s->result.bind[i].buffer);
1712
+ break;
1713
+ case MYSQL_TYPE_INT24:
1714
+ case MYSQL_TYPE_LONG:
1715
+ if (s->result.bind[i].is_unsigned)
1716
+ v = UINT2NUM(*(unsigned int *)s->result.bind[i].buffer);
1717
+ else
1718
+ v = INT2NUM(*(int *)s->result.bind[i].buffer);
1719
+ break;
1720
+ case MYSQL_TYPE_LONGLONG:
1721
+ if (s->result.bind[i].is_unsigned)
1722
+ v = ULL2NUM(*(unsigned long long *)s->result.bind[i].buffer);
1723
+ else
1724
+ v = LL2NUM(*(long long *)s->result.bind[i].buffer);
1725
+ break;
1726
+ case MYSQL_TYPE_FLOAT:
1727
+ v = rb_float_new((double)(*(float *)s->result.bind[i].buffer));
1728
+ break;
1729
+ case MYSQL_TYPE_DOUBLE:
1730
+ v = rb_float_new(*(double *)s->result.bind[i].buffer);
1731
+ break;
1732
+ case MYSQL_TYPE_TIME:
1733
+ case MYSQL_TYPE_DATE:
1734
+ case MYSQL_TYPE_DATETIME:
1735
+ case MYSQL_TYPE_TIMESTAMP:
1736
+ t = (MYSQL_TIME *)s->result.bind[i].buffer;
1737
+ v = rb_obj_alloc(cMysqlTime);
1738
+ rb_funcall(v, rb_intern("initialize"), 8,
1739
+ INT2FIX(t->year), INT2FIX(t->month),
1740
+ INT2FIX(t->day), INT2FIX(t->hour),
1741
+ INT2FIX(t->minute), INT2FIX(t->second),
1742
+ (t->neg ? Qtrue : Qfalse),
1743
+ INT2FIX(t->second_part));
1744
+ break;
1745
+ case MYSQL_TYPE_DECIMAL:
1746
+ case MYSQL_TYPE_STRING:
1747
+ case MYSQL_TYPE_VAR_STRING:
1748
+ case MYSQL_TYPE_TINY_BLOB:
1749
+ case MYSQL_TYPE_BLOB:
1750
+ case MYSQL_TYPE_MEDIUM_BLOB:
1751
+ case MYSQL_TYPE_LONG_BLOB:
1752
+ #if MYSQL_VERSION_ID >= 50003
1753
+ case MYSQL_TYPE_NEWDECIMAL:
1754
+ case MYSQL_TYPE_BIT:
1755
+ #endif
1756
+ v = rb_tainted_str_new(s->result.bind[i].buffer, s->result.length[i]);
1757
+ break;
1758
+ default:
1759
+ rb_raise(rb_eTypeError, "unknown buffer_type: %d", s->result.bind[i].buffer_type);
1760
+ }
1761
+ rb_ary_push(ret, v);
1762
+ }
1763
+ }
1764
+ return ret;
1765
+ }
1766
+
1767
+ /* each {...} */
1768
+ static VALUE stmt_each(VALUE obj)
1769
+ {
1770
+ VALUE row;
1771
+ check_stmt_closed(obj);
1772
+ while ((row = stmt_fetch(obj)) != Qnil)
1773
+ rb_yield(row);
1774
+ return obj;
1775
+ }
1776
+
1777
+ /* field_count() */
1778
+ static VALUE stmt_field_count(VALUE obj)
1779
+ {
1780
+ struct mysql_stmt* s = DATA_PTR(obj);
1781
+ unsigned int n;
1782
+ check_stmt_closed(obj);
1783
+ n = mysql_stmt_field_count(s->stmt);
1784
+ return INT2NUM(n);
1785
+ }
1786
+
1787
+ /* free_result() */
1788
+ static VALUE stmt_free_result(VALUE obj)
1789
+ {
1790
+ struct mysql_stmt* s = DATA_PTR(obj);
1791
+ check_stmt_closed(obj);
1792
+ if (mysql_stmt_free_result(s->stmt))
1793
+ mysql_stmt_raise(s->stmt);
1794
+ return obj;
1795
+ }
1796
+
1797
+ /* insert_id() */
1798
+ static VALUE stmt_insert_id(VALUE obj)
1799
+ {
1800
+ struct mysql_stmt* s = DATA_PTR(obj);
1801
+ my_ulonglong n;
1802
+ check_stmt_closed(obj);
1803
+ n = mysql_stmt_insert_id(s->stmt);
1804
+ return INT2NUM(n);
1805
+ }
1806
+
1807
+ /* num_rows() */
1808
+ static VALUE stmt_num_rows(VALUE obj)
1809
+ {
1810
+ struct mysql_stmt* s = DATA_PTR(obj);
1811
+ my_ulonglong n;
1812
+ check_stmt_closed(obj);
1813
+ n = mysql_stmt_num_rows(s->stmt);
1814
+ return INT2NUM(n);
1815
+ }
1816
+
1817
+ /* param_count() */
1818
+ static VALUE stmt_param_count(VALUE obj)
1819
+ {
1820
+ struct mysql_stmt* s = DATA_PTR(obj);
1821
+ unsigned long n;
1822
+ check_stmt_closed(obj);
1823
+ n = mysql_stmt_param_count(s->stmt);
1824
+ return INT2NUM(n);
1825
+ }
1826
+
1827
+ /* prepare(query) */
1828
+ static VALUE stmt_prepare(VALUE obj, VALUE query)
1829
+ {
1830
+ struct mysql_stmt* s = DATA_PTR(obj);
1831
+ int n;
1832
+ int i;
1833
+ MYSQL_FIELD *field;
1834
+
1835
+ free_mysqlstmt_memory(s);
1836
+ check_stmt_closed(obj);
1837
+ Check_Type(query, T_STRING);
1838
+ if (mysql_stmt_prepare(s->stmt, RSTRING_PTR(query), RSTRING_LEN(query)))
1839
+ mysql_stmt_raise(s->stmt);
1840
+
1841
+ n = mysql_stmt_param_count(s->stmt);
1842
+ s->param.n = n;
1843
+ s->param.bind = xmalloc(sizeof(s->param.bind[0]) * n);
1844
+ s->param.length = xmalloc(sizeof(s->param.length[0]) * n);
1845
+ s->param.buffer = xmalloc(sizeof(s->param.buffer[0]) * n);
1846
+
1847
+ s->res = mysql_stmt_result_metadata(s->stmt);
1848
+ if (s->res) {
1849
+ n = s->result.n = mysql_num_fields(s->res);
1850
+ s->result.bind = xmalloc(sizeof(s->result.bind[0]) * n);
1851
+ s->result.is_null = xmalloc(sizeof(s->result.is_null[0]) * n);
1852
+ s->result.length = xmalloc(sizeof(s->result.length[0]) * n);
1853
+ field = mysql_fetch_fields(s->res);
1854
+ memset(s->result.bind, 0, sizeof(s->result.bind[0]) * n);
1855
+ for (i = 0; i < n; i++) {
1856
+ s->result.bind[i].buffer_type = field[i].type;
1857
+ #if MYSQL_VERSION_ID < 50003
1858
+ if (field[i].type == MYSQL_TYPE_DECIMAL)
1859
+ s->result.bind[i].buffer_type = MYSQL_TYPE_STRING;
1860
+ #endif
1861
+ s->result.bind[i].is_null = &(s->result.is_null[i]);
1862
+ s->result.bind[i].length = &(s->result.length[i]);
1863
+ s->result.bind[i].is_unsigned = ((field[i].flags & UNSIGNED_FLAG) != 0);
1864
+ }
1865
+ } else {
1866
+ if (mysql_stmt_errno(s->stmt))
1867
+ mysql_stmt_raise(s->stmt);
1868
+ }
1869
+
1870
+ return obj;
1871
+ }
1872
+
1873
+ #if 0
1874
+ /* reset() */
1875
+ static VALUE stmt_reset(VALUE obj)
1876
+ {
1877
+ struct mysql_stmt* s = DATA_PTR(obj);
1878
+ check_stmt_closed(obj);
1879
+ if (mysql_stmt_reset(s->stmt))
1880
+ mysql_stmt_raise(s->stmt);
1881
+ return obj;
1882
+ }
1883
+ #endif
1884
+
1885
+ /* result_metadata() */
1886
+ static VALUE stmt_result_metadata(VALUE obj)
1887
+ {
1888
+ struct mysql_stmt* s = DATA_PTR(obj);
1889
+ MYSQL_RES *res;
1890
+ check_stmt_closed(obj);
1891
+ res = mysql_stmt_result_metadata(s->stmt);
1892
+ if (res == NULL) {
1893
+ if (mysql_stmt_errno(s->stmt) != 0)
1894
+ mysql_stmt_raise(s->stmt);
1895
+ return Qnil;
1896
+ }
1897
+ return mysqlres2obj(res);
1898
+ }
1899
+
1900
+ /* row_seek(offset) */
1901
+ static VALUE stmt_row_seek(VALUE obj, VALUE offset)
1902
+ {
1903
+ struct mysql_stmt* s = DATA_PTR(obj);
1904
+ MYSQL_ROW_OFFSET prev_offset;
1905
+ if (CLASS_OF(offset) != cMysqlRowOffset)
1906
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Mysql::RowOffset)", rb_obj_classname(offset));
1907
+ check_stmt_closed(obj);
1908
+ prev_offset = mysql_stmt_row_seek(s->stmt, DATA_PTR(offset));
1909
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, prev_offset);
1910
+ }
1911
+
1912
+ /* row_tell() */
1913
+ static VALUE stmt_row_tell(VALUE obj)
1914
+ {
1915
+ struct mysql_stmt* s = DATA_PTR(obj);
1916
+ MYSQL_ROW_OFFSET offset;
1917
+ check_stmt_closed(obj);
1918
+ offset = mysql_stmt_row_tell(s->stmt);
1919
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, offset);
1920
+ }
1921
+
1922
+ #if 0
1923
+ /* send_long_data(col, data) */
1924
+ static VALUE stmt_send_long_data(VALUE obj, VALUE col, VALUE data)
1925
+ {
1926
+ struct mysql_stmt* s = DATA_PTR(obj);
1927
+ int c;
1928
+ check_stmt_closed(obj);
1929
+ c = NUM2INT(col);
1930
+ if (0 <= c && c < s->param.n) {
1931
+ s->param.bind[c].buffer_type = MYSQL_TYPE_STRING;
1932
+ if (mysql_stmt_bind_param(s->stmt, s->param.bind))
1933
+ mysql_stmt_raise(s->stmt);
1934
+ }
1935
+ if (mysql_stmt_send_long_data(s->stmt, c, RSTRING_PTR(data), RSTRING_LEN(data)))
1936
+ mysql_stmt_raise(s->stmt);
1937
+ return obj;
1938
+ }
1939
+ #endif
1940
+
1941
+ /* sqlstate() */
1942
+ static VALUE stmt_sqlstate(VALUE obj)
1943
+ {
1944
+ struct mysql_stmt* s = DATA_PTR(obj);
1945
+ return rb_tainted_str_new2(mysql_stmt_sqlstate(s->stmt));
1946
+ }
1947
+
1948
+ /*-------------------------------
1949
+ * Mysql::Time object method
1950
+ */
1951
+
1952
+ static VALUE time_initialize(int argc, VALUE* argv, VALUE obj)
1953
+ {
1954
+ VALUE year, month, day, hour, minute, second, neg, second_part;
1955
+ rb_scan_args(argc, argv, "08", &year, &month, &day, &hour, &minute, &second, &neg, &second_part);
1956
+ #define NILorFIXvalue(o) (NIL_P(o) ? INT2FIX(0) : (Check_Type(o, T_FIXNUM), o))
1957
+ rb_iv_set(obj, "year", NILorFIXvalue(year));
1958
+ rb_iv_set(obj, "month", NILorFIXvalue(month));
1959
+ rb_iv_set(obj, "day", NILorFIXvalue(day));
1960
+ rb_iv_set(obj, "hour", NILorFIXvalue(hour));
1961
+ rb_iv_set(obj, "minute", NILorFIXvalue(minute));
1962
+ rb_iv_set(obj, "second", NILorFIXvalue(second));
1963
+ rb_iv_set(obj, "neg", (neg == Qnil || neg == Qfalse) ? Qfalse : Qtrue);
1964
+ rb_iv_set(obj, "second_part", NILorFIXvalue(second_part));
1965
+ return obj;
1966
+ }
1967
+
1968
+ static VALUE time_inspect(VALUE obj)
1969
+ {
1970
+ char buf[36];
1971
+ sprintf(buf, "#<Mysql::Time:%04d-%02d-%02d %02d:%02d:%02d>",
1972
+ NUM2INT(rb_iv_get(obj, "year")),
1973
+ NUM2INT(rb_iv_get(obj, "month")),
1974
+ NUM2INT(rb_iv_get(obj, "day")),
1975
+ NUM2INT(rb_iv_get(obj, "hour")),
1976
+ NUM2INT(rb_iv_get(obj, "minute")),
1977
+ NUM2INT(rb_iv_get(obj, "second")));
1978
+ return rb_str_new2(buf);
1979
+ }
1980
+
1981
+ static VALUE time_to_s(VALUE obj)
1982
+ {
1983
+ char buf[20];
1984
+ sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
1985
+ NUM2INT(rb_iv_get(obj, "year")),
1986
+ NUM2INT(rb_iv_get(obj, "month")),
1987
+ NUM2INT(rb_iv_get(obj, "day")),
1988
+ NUM2INT(rb_iv_get(obj, "hour")),
1989
+ NUM2INT(rb_iv_get(obj, "minute")),
1990
+ NUM2INT(rb_iv_get(obj, "second")));
1991
+ return rb_str_new2(buf);
1992
+ }
1993
+
1994
+ #define DefineMysqlTimeGetMethod(m)\
1995
+ static VALUE time_get_##m(VALUE obj)\
1996
+ {return rb_iv_get(obj, #m);}
1997
+
1998
+ DefineMysqlTimeGetMethod(year)
1999
+ DefineMysqlTimeGetMethod(month)
2000
+ DefineMysqlTimeGetMethod(day)
2001
+ DefineMysqlTimeGetMethod(hour)
2002
+ DefineMysqlTimeGetMethod(minute)
2003
+ DefineMysqlTimeGetMethod(second)
2004
+ DefineMysqlTimeGetMethod(neg)
2005
+ DefineMysqlTimeGetMethod(second_part)
2006
+
2007
+ #define DefineMysqlTimeSetMethod(m)\
2008
+ static VALUE time_set_##m(VALUE obj, VALUE v)\
2009
+ {rb_iv_set(obj, #m, NILorFIXvalue(v)); return v;}
2010
+
2011
+ DefineMysqlTimeSetMethod(year)
2012
+ DefineMysqlTimeSetMethod(month)
2013
+ DefineMysqlTimeSetMethod(day)
2014
+ DefineMysqlTimeSetMethod(hour)
2015
+ DefineMysqlTimeSetMethod(minute)
2016
+ DefineMysqlTimeSetMethod(second)
2017
+ DefineMysqlTimeSetMethod(second_part)
2018
+
2019
+ static VALUE time_set_neg(VALUE obj, VALUE v)
2020
+ {
2021
+ rb_iv_set(obj, "neg", (v == Qnil || v == Qfalse) ? Qfalse : Qtrue);
2022
+ return v;
2023
+ }
2024
+
2025
+ static VALUE time_equal(VALUE obj, VALUE v)
2026
+ {
2027
+ if (CLASS_OF(v) == cMysqlTime &&
2028
+ NUM2INT(rb_iv_get(obj, "year")) == NUM2INT(rb_iv_get(v, "year")) &&
2029
+ NUM2INT(rb_iv_get(obj, "month")) == NUM2INT(rb_iv_get(v, "month")) &&
2030
+ NUM2INT(rb_iv_get(obj, "day")) == NUM2INT(rb_iv_get(v, "day")) &&
2031
+ NUM2INT(rb_iv_get(obj, "hour")) == NUM2INT(rb_iv_get(v, "hour")) &&
2032
+ NUM2INT(rb_iv_get(obj, "minute")) == NUM2INT(rb_iv_get(v, "minute")) &&
2033
+ NUM2INT(rb_iv_get(obj, "second")) == NUM2INT(rb_iv_get(v, "second")) &&
2034
+ rb_iv_get(obj, "neg") == rb_iv_get(v, "neg") &&
2035
+ NUM2INT(rb_iv_get(obj, "second_part")) == NUM2INT(rb_iv_get(v, "second_part")))
2036
+ return Qtrue;
2037
+ return Qfalse;
2038
+ }
2039
+
2040
+ #endif
2041
+
2042
+ /*-------------------------------
2043
+ * Mysql::Error object method
2044
+ */
2045
+
2046
+ static VALUE error_error(VALUE obj)
2047
+ {
2048
+ return rb_iv_get(obj, "mesg");
2049
+ }
2050
+
2051
+ static VALUE error_errno(VALUE obj)
2052
+ {
2053
+ return rb_iv_get(obj, "errno");
2054
+ }
2055
+
2056
+ static VALUE error_sqlstate(VALUE obj)
2057
+ {
2058
+ return rb_iv_get(obj, "sqlstate");
2059
+ }
2060
+
2061
+ /*-------------------------------
2062
+ * Initialize
2063
+ */
2064
+
2065
+ void Init_mysql(void)
2066
+ {
2067
+ cMysql = rb_define_class("Mysql", rb_cObject);
2068
+ cMysqlRes = rb_define_class_under(cMysql, "Result", rb_cObject);
2069
+ cMysqlField = rb_define_class_under(cMysql, "Field", rb_cObject);
2070
+ #if MYSQL_VERSION_ID >= 40102
2071
+ cMysqlStmt = rb_define_class_under(cMysql, "Stmt", rb_cObject);
2072
+ cMysqlRowOffset = rb_define_class_under(cMysql, "RowOffset", rb_cObject);
2073
+ cMysqlTime = rb_define_class_under(cMysql, "Time", rb_cObject);
2074
+ #endif
2075
+ eMysql = rb_define_class_under(cMysql, "Error", rb_eStandardError);
2076
+
2077
+ rb_define_global_const("MysqlRes", cMysqlRes);
2078
+ rb_define_global_const("MysqlField", cMysqlField);
2079
+ rb_define_global_const("MysqlError", eMysql);
2080
+
2081
+ /* Mysql class method */
2082
+ rb_define_singleton_method(cMysql, "init", init, 0);
2083
+ rb_define_singleton_method(cMysql, "real_connect", real_connect, -1);
2084
+ rb_define_singleton_method(cMysql, "connect", real_connect, -1);
2085
+ rb_define_singleton_method(cMysql, "new", real_connect, -1);
2086
+ rb_define_singleton_method(cMysql, "escape_string", escape_string, 1);
2087
+ rb_define_singleton_method(cMysql, "quote", escape_string, 1);
2088
+ rb_define_singleton_method(cMysql, "client_info", client_info, 0);
2089
+ rb_define_singleton_method(cMysql, "get_client_info", client_info, 0);
2090
+ #if MYSQL_VERSION_ID >= 32332
2091
+ rb_define_singleton_method(cMysql, "debug", my_debug, 1);
2092
+ #endif
2093
+ #if MYSQL_VERSION_ID >= 40000
2094
+ rb_define_singleton_method(cMysql, "get_client_version", client_version, 0);
2095
+ rb_define_singleton_method(cMysql, "client_version", client_version, 0);
2096
+ #endif
2097
+
2098
+ /* Mysql object method */
2099
+ #if MYSQL_VERSION_ID >= 32200
2100
+ rb_define_method(cMysql, "real_connect", real_connect2, -1);
2101
+ rb_define_method(cMysql, "connect", real_connect2, -1);
2102
+ rb_define_method(cMysql, "options", options, -1);
2103
+ #endif
2104
+ rb_define_method(cMysql, "initialize", initialize, -1);
2105
+ #if MYSQL_VERSION_ID >= 32332
2106
+ rb_define_method(cMysql, "escape_string", real_escape_string, 1);
2107
+ rb_define_method(cMysql, "quote", real_escape_string, 1);
2108
+ #else
2109
+ rb_define_method(cMysql, "escape_string", escape_string, 1);
2110
+ rb_define_method(cMysql, "quote", escape_string, 1);
2111
+ #endif
2112
+ rb_define_method(cMysql, "client_info", client_info, 0);
2113
+ rb_define_method(cMysql, "get_client_info", client_info, 0);
2114
+ rb_define_method(cMysql, "affected_rows", affected_rows, 0);
2115
+ #if MYSQL_VERSION_ID >= 32303
2116
+ rb_define_method(cMysql, "change_user", change_user, -1);
2117
+ #endif
2118
+ #if MYSQL_VERSION_ID >= 32321
2119
+ rb_define_method(cMysql, "character_set_name", character_set_name, 0);
2120
+ #endif
2121
+ rb_define_method(cMysql, "close", my_close, 0);
2122
+ #if MYSQL_VERSION_ID < 40000
2123
+ rb_define_method(cMysql, "create_db", create_db, 1);
2124
+ rb_define_method(cMysql, "drop_db", drop_db, 1);
2125
+ #endif
2126
+ #if MYSQL_VERSION_ID >= 32332
2127
+ rb_define_method(cMysql, "dump_debug_info", dump_debug_info, 0);
2128
+ #endif
2129
+ rb_define_method(cMysql, "errno", my_errno, 0);
2130
+ rb_define_method(cMysql, "error", my_error, 0);
2131
+ rb_define_method(cMysql, "field_count", field_count, 0);
2132
+ #if MYSQL_VERSION_ID >= 40000
2133
+ rb_define_method(cMysql, "get_client_version", client_version, 0);
2134
+ rb_define_method(cMysql, "client_version", client_version, 0);
2135
+ #endif
2136
+ rb_define_method(cMysql, "get_host_info", host_info, 0);
2137
+ rb_define_method(cMysql, "host_info", host_info, 0);
2138
+ rb_define_method(cMysql, "get_proto_info", proto_info, 0);
2139
+ rb_define_method(cMysql, "proto_info", proto_info, 0);
2140
+ rb_define_method(cMysql, "get_server_info", server_info, 0);
2141
+ rb_define_method(cMysql, "server_info", server_info, 0);
2142
+ rb_define_method(cMysql, "info", info, 0);
2143
+ rb_define_method(cMysql, "insert_id", insert_id, 0);
2144
+ rb_define_method(cMysql, "kill", my_kill, 1);
2145
+ rb_define_method(cMysql, "list_dbs", list_dbs, -1);
2146
+ rb_define_method(cMysql, "list_fields", list_fields, -1);
2147
+ rb_define_method(cMysql, "list_processes", list_processes, 0);
2148
+ rb_define_method(cMysql, "list_tables", list_tables, -1);
2149
+ #if MYSQL_VERSION_ID >= 32200
2150
+ rb_define_method(cMysql, "ping", ping, 0);
2151
+ #endif
2152
+ rb_define_method(cMysql, "query", query, 1);
2153
+ rb_define_method(cMysql, "real_query", query, 1);
2154
+ rb_define_method(cMysql, "c_async_query", async_query, -1);
2155
+ rb_define_method(cMysql, "send_query", send_query, 1);
2156
+ rb_define_method(cMysql, "get_result", get_result, 0);
2157
+ rb_define_method(cMysql, "readable?", readable, -1);
2158
+ rb_define_method(cMysql, "blocking?", blocking, 0);
2159
+ rb_define_method(cMysql, "socket", socket, 0);
2160
+ rb_define_method(cMysql, "socket_type", socket_type, 0);
2161
+ rb_define_method(cMysql, "refresh", refresh, 1);
2162
+ rb_define_method(cMysql, "reload", reload, 0);
2163
+ rb_define_method(cMysql, "select_db", select_db, 1);
2164
+ rb_define_method(cMysql, "shutdown", my_shutdown, -1);
2165
+ rb_define_method(cMysql, "stat", my_stat, 0);
2166
+ rb_define_method(cMysql, "store_result", store_result, 0);
2167
+ rb_define_method(cMysql, "thread_id", thread_id, 0);
2168
+ rb_define_method(cMysql, "use_result", use_result, 0);
2169
+ #if MYSQL_VERSION_ID >= 40100
2170
+ rb_define_method(cMysql, "get_server_version", server_version, 0);
2171
+ rb_define_method(cMysql, "server_version", server_version, 0);
2172
+ rb_define_method(cMysql, "warning_count", warning_count, 0);
2173
+ rb_define_method(cMysql, "commit", commit, 0);
2174
+ rb_define_method(cMysql, "rollback", rollback, 0);
2175
+ rb_define_method(cMysql, "autocommit", autocommit, 1);
2176
+ #endif
2177
+ #ifdef HAVE_MYSQL_SSL_SET
2178
+ rb_define_method(cMysql, "ssl_set", ssl_set, -1);
2179
+ #endif
2180
+ #if MYSQL_VERSION_ID >= 40102
2181
+ rb_define_method(cMysql, "stmt_init", stmt_init, 0);
2182
+ rb_define_method(cMysql, "prepare", prepare, 1);
2183
+ #endif
2184
+ #if MYSQL_VERSION_ID >= 40100
2185
+ rb_define_method(cMysql, "more_results", more_results, 0);
2186
+ rb_define_method(cMysql, "more_results?", more_results, 0);
2187
+ rb_define_method(cMysql, "next_result", next_result, 0);
2188
+ #endif
2189
+ #if MYSQL_VERSION_ID >= 40101
2190
+ rb_define_method(cMysql, "set_server_option", set_server_option, 1);
2191
+ rb_define_method(cMysql, "sqlstate", sqlstate, 0);
2192
+ #endif
2193
+ rb_define_method(cMysql, "query_with_result", query_with_result, 0);
2194
+ rb_define_method(cMysql, "query_with_result=", query_with_result_set, 1);
2195
+
2196
+ rb_define_method(cMysql, "reconnect", reconnect, 0);
2197
+ rb_define_method(cMysql, "reconnect=", reconnect_set, 1);
2198
+
2199
+ /* Mysql constant */
2200
+ rb_define_const(cMysql, "VERSION", INT2FIX(MYSQL_RUBY_VERSION));
2201
+ #if MYSQL_VERSION_ID >= 32200
2202
+ rb_define_const(cMysql, "OPT_CONNECT_TIMEOUT", INT2NUM(MYSQL_OPT_CONNECT_TIMEOUT));
2203
+ rb_define_const(cMysql, "OPT_COMPRESS", INT2NUM(MYSQL_OPT_COMPRESS));
2204
+ rb_define_const(cMysql, "OPT_NAMED_PIPE", INT2NUM(MYSQL_OPT_NAMED_PIPE));
2205
+ rb_define_const(cMysql, "INIT_COMMAND", INT2NUM(MYSQL_INIT_COMMAND));
2206
+ rb_define_const(cMysql, "READ_DEFAULT_FILE", INT2NUM(MYSQL_READ_DEFAULT_FILE));
2207
+ rb_define_const(cMysql, "READ_DEFAULT_GROUP", INT2NUM(MYSQL_READ_DEFAULT_GROUP));
2208
+ #endif
2209
+ #if MYSQL_VERSION_ID >= 32349
2210
+ rb_define_const(cMysql, "SET_CHARSET_DIR", INT2NUM(MYSQL_SET_CHARSET_DIR));
2211
+ rb_define_const(cMysql, "SET_CHARSET_NAME", INT2NUM(MYSQL_SET_CHARSET_NAME));
2212
+ rb_define_const(cMysql, "OPT_LOCAL_INFILE", INT2NUM(MYSQL_OPT_LOCAL_INFILE));
2213
+ #endif
2214
+ #if MYSQL_VERSION_ID >= 40100
2215
+ rb_define_const(cMysql, "OPT_PROTOCOL", INT2NUM(MYSQL_OPT_PROTOCOL));
2216
+ rb_define_const(cMysql, "SHARED_MEMORY_BASE_NAME", INT2NUM(MYSQL_SHARED_MEMORY_BASE_NAME));
2217
+ #endif
2218
+ #if MYSQL_VERSION_ID >= 40101
2219
+ rb_define_const(cMysql, "OPT_READ_TIMEOUT", INT2NUM(MYSQL_OPT_READ_TIMEOUT));
2220
+ rb_define_const(cMysql, "OPT_WRITE_TIMEOUT", INT2NUM(MYSQL_OPT_WRITE_TIMEOUT));
2221
+ rb_define_const(cMysql, "SECURE_AUTH", INT2NUM(MYSQL_SECURE_AUTH));
2222
+ rb_define_const(cMysql, "OPT_GUESS_CONNECTION", INT2NUM(MYSQL_OPT_GUESS_CONNECTION));
2223
+ rb_define_const(cMysql, "OPT_USE_EMBEDDED_CONNECTION", INT2NUM(MYSQL_OPT_USE_EMBEDDED_CONNECTION));
2224
+ rb_define_const(cMysql, "OPT_USE_REMOTE_CONNECTION", INT2NUM(MYSQL_OPT_USE_REMOTE_CONNECTION));
2225
+ rb_define_const(cMysql, "SET_CLIENT_IP", INT2NUM(MYSQL_SET_CLIENT_IP));
2226
+ #endif
2227
+ rb_define_const(cMysql, "REFRESH_GRANT", INT2NUM(REFRESH_GRANT));
2228
+ rb_define_const(cMysql, "REFRESH_LOG", INT2NUM(REFRESH_LOG));
2229
+ rb_define_const(cMysql, "REFRESH_TABLES", INT2NUM(REFRESH_TABLES));
2230
+ #ifdef REFRESH_HOSTS
2231
+ rb_define_const(cMysql, "REFRESH_HOSTS", INT2NUM(REFRESH_HOSTS));
2232
+ #endif
2233
+ #ifdef REFRESH_STATUS
2234
+ rb_define_const(cMysql, "REFRESH_STATUS", INT2NUM(REFRESH_STATUS));
2235
+ #endif
2236
+ #ifdef REFRESH_THREADS
2237
+ rb_define_const(cMysql, "REFRESH_THREADS", INT2NUM(REFRESH_THREADS));
2238
+ #endif
2239
+ #ifdef REFRESH_SLAVE
2240
+ rb_define_const(cMysql, "REFRESH_SLAVE", INT2NUM(REFRESH_SLAVE));
2241
+ #endif
2242
+ #ifdef REFRESH_MASTER
2243
+ rb_define_const(cMysql, "REFRESH_MASTER", INT2NUM(REFRESH_MASTER));
2244
+ #endif
2245
+ #ifdef CLIENT_LONG_PASSWORD
2246
+ #endif
2247
+ #ifdef CLIENT_FOUND_ROWS
2248
+ rb_define_const(cMysql, "CLIENT_FOUND_ROWS", INT2NUM(CLIENT_FOUND_ROWS));
2249
+ #endif
2250
+ #ifdef CLIENT_LONG_FLAG
2251
+ #endif
2252
+ #ifdef CLIENT_CONNECT_WITH_DB
2253
+ #endif
2254
+ #ifdef CLIENT_NO_SCHEMA
2255
+ rb_define_const(cMysql, "CLIENT_NO_SCHEMA", INT2NUM(CLIENT_NO_SCHEMA));
2256
+ #endif
2257
+ #ifdef CLIENT_COMPRESS
2258
+ rb_define_const(cMysql, "CLIENT_COMPRESS", INT2NUM(CLIENT_COMPRESS));
2259
+ #endif
2260
+ #ifdef CLIENT_ODBC
2261
+ rb_define_const(cMysql, "CLIENT_ODBC", INT2NUM(CLIENT_ODBC));
2262
+ #endif
2263
+ #ifdef CLIENT_LOCAL_FILES
2264
+ rb_define_const(cMysql, "CLIENT_LOCAL_FILES", INT2NUM(CLIENT_LOCAL_FILES));
2265
+ #endif
2266
+ #ifdef CLIENT_IGNORE_SPACE
2267
+ rb_define_const(cMysql, "CLIENT_IGNORE_SPACE", INT2NUM(CLIENT_IGNORE_SPACE));
2268
+ #endif
2269
+ #ifdef CLIENT_CHANGE_USER
2270
+ rb_define_const(cMysql, "CLIENT_CHANGE_USER", INT2NUM(CLIENT_CHANGE_USER));
2271
+ #endif
2272
+ #ifdef CLIENT_INTERACTIVE
2273
+ rb_define_const(cMysql, "CLIENT_INTERACTIVE", INT2NUM(CLIENT_INTERACTIVE));
2274
+ #endif
2275
+ #ifdef CLIENT_SSL
2276
+ rb_define_const(cMysql, "CLIENT_SSL", INT2NUM(CLIENT_SSL));
2277
+ #endif
2278
+ #ifdef CLIENT_IGNORE_SIGPIPE
2279
+ rb_define_const(cMysql, "CLIENT_IGNORE_SIGPIPE", INT2NUM(CLIENT_IGNORE_SIGPIPE));
2280
+ #endif
2281
+ #ifdef CLIENT_TRANSACTIONS
2282
+ rb_define_const(cMysql, "CLIENT_TRANSACTIONS", INT2NUM(CLIENT_TRANSACTIONS));
2283
+ #endif
2284
+ #ifdef CLIENT_MULTI_STATEMENTS
2285
+ rb_define_const(cMysql, "CLIENT_MULTI_STATEMENTS", INT2NUM(CLIENT_MULTI_STATEMENTS));
2286
+ #endif
2287
+ #ifdef CLIENT_MULTI_RESULTS
2288
+ rb_define_const(cMysql, "CLIENT_MULTI_RESULTS", INT2NUM(CLIENT_MULTI_RESULTS));
2289
+ #endif
2290
+ #if MYSQL_VERSION_ID >= 40101
2291
+ rb_define_const(cMysql, "OPTION_MULTI_STATEMENTS_ON", INT2NUM(MYSQL_OPTION_MULTI_STATEMENTS_ON));
2292
+ rb_define_const(cMysql, "OPTION_MULTI_STATEMENTS_OFF", INT2NUM(MYSQL_OPTION_MULTI_STATEMENTS_OFF));
2293
+ #endif
2294
+
2295
+ /* Mysql::Result object method */
2296
+ rb_define_method(cMysqlRes, "data_seek", data_seek, 1);
2297
+ rb_define_method(cMysqlRes, "fetch_field", fetch_field, 0);
2298
+ rb_define_method(cMysqlRes, "fetch_fields", fetch_fields, 0);
2299
+ rb_define_method(cMysqlRes, "fetch_field_direct", fetch_field_direct, 1);
2300
+ rb_define_method(cMysqlRes, "fetch_lengths", fetch_lengths, 0);
2301
+ rb_define_method(cMysqlRes, "fetch_row", fetch_row, 0);
2302
+ rb_define_method(cMysqlRes, "fetch_hash", fetch_hash, -1);
2303
+ rb_define_method(cMysqlRes, "field_seek", field_seek, 1);
2304
+ rb_define_method(cMysqlRes, "field_tell", field_tell, 0);
2305
+ rb_define_method(cMysqlRes, "free", res_free, 0);
2306
+ rb_define_method(cMysqlRes, "num_fields", num_fields, 0);
2307
+ rb_define_method(cMysqlRes, "num_rows", num_rows, 0);
2308
+ rb_define_method(cMysqlRes, "row_seek", row_seek, 1);
2309
+ rb_define_method(cMysqlRes, "row_tell", row_tell, 0);
2310
+ rb_define_method(cMysqlRes, "each", each, 0);
2311
+ rb_define_method(cMysqlRes, "each_hash", each_hash, -1);
2312
+ rb_define_method(cMysqlRes, "all_hashes", all_hashes, -1);
2313
+
2314
+ /* MysqlField object method */
2315
+ rb_define_method(cMysqlField, "name", field_name, 0);
2316
+ rb_define_method(cMysqlField, "table", field_table, 0);
2317
+ rb_define_method(cMysqlField, "def", field_def, 0);
2318
+ rb_define_method(cMysqlField, "type", field_type, 0);
2319
+ rb_define_method(cMysqlField, "length", field_length, 0);
2320
+ rb_define_method(cMysqlField, "max_length", field_max_length, 0);
2321
+ rb_define_method(cMysqlField, "flags", field_flags, 0);
2322
+ rb_define_method(cMysqlField, "decimals", field_decimals, 0);
2323
+ rb_define_method(cMysqlField, "hash", field_hash, 0);
2324
+ rb_define_method(cMysqlField, "inspect", field_inspect, 0);
2325
+ #ifdef IS_NUM
2326
+ rb_define_method(cMysqlField, "is_num?", field_is_num, 0);
2327
+ #endif
2328
+ #ifdef IS_NOT_NULL
2329
+ rb_define_method(cMysqlField, "is_not_null?", field_is_not_null, 0);
2330
+ #endif
2331
+ #ifdef IS_PRI_KEY
2332
+ rb_define_method(cMysqlField, "is_pri_key?", field_is_pri_key, 0);
2333
+ #endif
2334
+
2335
+ /* Mysql::Field constant: TYPE */
2336
+ rb_define_const(cMysqlField, "TYPE_TINY", INT2NUM(FIELD_TYPE_TINY));
2337
+ #if MYSQL_VERSION_ID >= 32115
2338
+ rb_define_const(cMysqlField, "TYPE_ENUM", INT2NUM(FIELD_TYPE_ENUM));
2339
+ #endif
2340
+ rb_define_const(cMysqlField, "TYPE_DECIMAL", INT2NUM(FIELD_TYPE_DECIMAL));
2341
+ rb_define_const(cMysqlField, "TYPE_SHORT", INT2NUM(FIELD_TYPE_SHORT));
2342
+ rb_define_const(cMysqlField, "TYPE_LONG", INT2NUM(FIELD_TYPE_LONG));
2343
+ rb_define_const(cMysqlField, "TYPE_FLOAT", INT2NUM(FIELD_TYPE_FLOAT));
2344
+ rb_define_const(cMysqlField, "TYPE_DOUBLE", INT2NUM(FIELD_TYPE_DOUBLE));
2345
+ rb_define_const(cMysqlField, "TYPE_NULL", INT2NUM(FIELD_TYPE_NULL));
2346
+ rb_define_const(cMysqlField, "TYPE_TIMESTAMP", INT2NUM(FIELD_TYPE_TIMESTAMP));
2347
+ rb_define_const(cMysqlField, "TYPE_LONGLONG", INT2NUM(FIELD_TYPE_LONGLONG));
2348
+ rb_define_const(cMysqlField, "TYPE_INT24", INT2NUM(FIELD_TYPE_INT24));
2349
+ rb_define_const(cMysqlField, "TYPE_DATE", INT2NUM(FIELD_TYPE_DATE));
2350
+ rb_define_const(cMysqlField, "TYPE_TIME", INT2NUM(FIELD_TYPE_TIME));
2351
+ rb_define_const(cMysqlField, "TYPE_DATETIME", INT2NUM(FIELD_TYPE_DATETIME));
2352
+ #if MYSQL_VERSION_ID >= 32130
2353
+ rb_define_const(cMysqlField, "TYPE_YEAR", INT2NUM(FIELD_TYPE_YEAR));
2354
+ #endif
2355
+ #if MYSQL_VERSION_ID >= 50003
2356
+ rb_define_const(cMysqlField, "TYPE_BIT", INT2NUM(FIELD_TYPE_BIT));
2357
+ rb_define_const(cMysqlField, "TYPE_NEWDECIMAL", INT2NUM(FIELD_TYPE_NEWDECIMAL));
2358
+ #endif
2359
+ rb_define_const(cMysqlField, "TYPE_SET", INT2NUM(FIELD_TYPE_SET));
2360
+ rb_define_const(cMysqlField, "TYPE_BLOB", INT2NUM(FIELD_TYPE_BLOB));
2361
+ rb_define_const(cMysqlField, "TYPE_STRING", INT2NUM(FIELD_TYPE_STRING));
2362
+ #if MYSQL_VERSION_ID >= 40000
2363
+ rb_define_const(cMysqlField, "TYPE_VAR_STRING", INT2NUM(FIELD_TYPE_VAR_STRING));
2364
+ #endif
2365
+ rb_define_const(cMysqlField, "TYPE_CHAR", INT2NUM(FIELD_TYPE_CHAR));
2366
+
2367
+ /* Mysql::Field constant: FLAG */
2368
+ rb_define_const(cMysqlField, "NOT_NULL_FLAG", INT2NUM(NOT_NULL_FLAG));
2369
+ rb_define_const(cMysqlField, "PRI_KEY_FLAG", INT2NUM(PRI_KEY_FLAG));
2370
+ rb_define_const(cMysqlField, "UNIQUE_KEY_FLAG", INT2NUM(UNIQUE_KEY_FLAG));
2371
+ rb_define_const(cMysqlField, "MULTIPLE_KEY_FLAG", INT2NUM(MULTIPLE_KEY_FLAG));
2372
+ rb_define_const(cMysqlField, "BLOB_FLAG", INT2NUM(BLOB_FLAG));
2373
+ rb_define_const(cMysqlField, "UNSIGNED_FLAG", INT2NUM(UNSIGNED_FLAG));
2374
+ rb_define_const(cMysqlField, "ZEROFILL_FLAG", INT2NUM(ZEROFILL_FLAG));
2375
+ rb_define_const(cMysqlField, "BINARY_FLAG", INT2NUM(BINARY_FLAG));
2376
+ #ifdef ENUM_FLAG
2377
+ rb_define_const(cMysqlField, "ENUM_FLAG", INT2NUM(ENUM_FLAG));
2378
+ #endif
2379
+ #ifdef AUTO_INCREMENT_FLAG
2380
+ rb_define_const(cMysqlField, "AUTO_INCREMENT_FLAG", INT2NUM(AUTO_INCREMENT_FLAG));
2381
+ #endif
2382
+ #ifdef TIMESTAMP_FLAG
2383
+ rb_define_const(cMysqlField, "TIMESTAMP_FLAG", INT2NUM(TIMESTAMP_FLAG));
2384
+ #endif
2385
+ #ifdef SET_FLAG
2386
+ rb_define_const(cMysqlField, "SET_FLAG", INT2NUM(SET_FLAG));
2387
+ #endif
2388
+ #ifdef NUM_FLAG
2389
+ rb_define_const(cMysqlField, "NUM_FLAG", INT2NUM(NUM_FLAG));
2390
+ #endif
2391
+ #ifdef PART_KEY_FLAG
2392
+ rb_define_const(cMysqlField, "PART_KEY_FLAG", INT2NUM(PART_KEY_FLAG));
2393
+ #endif
2394
+
2395
+ #if MYSQL_VERSION_ID >= 40102
2396
+ /* Mysql::Stmt object method */
2397
+ rb_define_method(cMysqlStmt, "affected_rows", stmt_affected_rows, 0);
2398
+ #if 0
2399
+ rb_define_method(cMysqlStmt, "attr_get", stmt_attr_get, 1);
2400
+ rb_define_method(cMysqlStmt, "attr_set", stmt_attr_set, 2);
2401
+ #endif
2402
+ rb_define_method(cMysqlStmt, "bind_result", stmt_bind_result, -1);
2403
+ rb_define_method(cMysqlStmt, "close", stmt_close, 0);
2404
+ rb_define_method(cMysqlStmt, "data_seek", stmt_data_seek, 1);
2405
+ rb_define_method(cMysqlStmt, "each", stmt_each, 0);
2406
+ rb_define_method(cMysqlStmt, "execute", stmt_execute, -1);
2407
+ rb_define_method(cMysqlStmt, "fetch", stmt_fetch, 0);
2408
+ rb_define_method(cMysqlStmt, "field_count", stmt_field_count, 0);
2409
+ rb_define_method(cMysqlStmt, "free_result", stmt_free_result, 0);
2410
+ rb_define_method(cMysqlStmt, "insert_id", stmt_insert_id, 0);
2411
+ rb_define_method(cMysqlStmt, "num_rows", stmt_num_rows, 0);
2412
+ rb_define_method(cMysqlStmt, "param_count", stmt_param_count, 0);
2413
+ rb_define_method(cMysqlStmt, "prepare", stmt_prepare, 1);
2414
+ #if 0
2415
+ rb_define_method(cMysqlStmt, "reset", stmt_reset, 0);
2416
+ #endif
2417
+ rb_define_method(cMysqlStmt, "result_metadata", stmt_result_metadata, 0);
2418
+ rb_define_method(cMysqlStmt, "row_seek", stmt_row_seek, 1);
2419
+ rb_define_method(cMysqlStmt, "row_tell", stmt_row_tell, 0);
2420
+ #if 0
2421
+ rb_define_method(cMysqlStmt, "send_long_data", stmt_send_long_data, 2);
2422
+ #endif
2423
+ rb_define_method(cMysqlStmt, "sqlstate", stmt_sqlstate, 0);
2424
+
2425
+ #if 0
2426
+ rb_define_const(cMysqlStmt, "ATTR_UPDATE_MAX_LENGTH", INT2NUM(STMT_ATTR_UPDATE_MAX_LENGTH));
2427
+ #endif
2428
+
2429
+ /* Mysql::Time object method */
2430
+ rb_define_method(cMysqlTime, "initialize", time_initialize, -1);
2431
+ rb_define_method(cMysqlTime, "inspect", time_inspect, 0);
2432
+ rb_define_method(cMysqlTime, "to_s", time_to_s, 0);
2433
+ rb_define_method(cMysqlTime, "year", time_get_year, 0);
2434
+ rb_define_method(cMysqlTime, "month", time_get_month, 0);
2435
+ rb_define_method(cMysqlTime, "day", time_get_day, 0);
2436
+ rb_define_method(cMysqlTime, "hour", time_get_hour, 0);
2437
+ rb_define_method(cMysqlTime, "minute", time_get_minute, 0);
2438
+ rb_define_method(cMysqlTime, "second", time_get_second, 0);
2439
+ rb_define_method(cMysqlTime, "neg", time_get_neg, 0);
2440
+ rb_define_method(cMysqlTime, "second_part", time_get_second_part, 0);
2441
+ rb_define_method(cMysqlTime, "year=", time_set_year, 1);
2442
+ rb_define_method(cMysqlTime, "month=", time_set_month, 1);
2443
+ rb_define_method(cMysqlTime, "day=", time_set_day, 1);
2444
+ rb_define_method(cMysqlTime, "hour=", time_set_hour, 1);
2445
+ rb_define_method(cMysqlTime, "minute=", time_set_minute, 1);
2446
+ rb_define_method(cMysqlTime, "second=", time_set_second, 1);
2447
+ rb_define_method(cMysqlTime, "neg=", time_set_neg, 1);
2448
+ rb_define_method(cMysqlTime, "second_part=", time_set_second_part, 1);
2449
+ rb_define_method(cMysqlTime, "==", time_equal, 1);
2450
+
2451
+ #endif
2452
+
2453
+ /* Mysql::Error object method */
2454
+ rb_define_method(eMysql, "error", error_error, 0);
2455
+ rb_define_method(eMysql, "errno", error_errno, 0);
2456
+ rb_define_method(eMysql, "sqlstate", error_sqlstate, 0);
2457
+
2458
+ /* Mysql::Error constant */
2459
+ #define rb_define_mysql_const(s) rb_define_const(eMysql, #s, INT2NUM(s))
2460
+ #include "error_const.h"
2461
+ }