skaes-mysql 2.7.7

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