mysqlplus 0.1.1

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