PerfectlyNormal-mysql-ruby 2.8.1.2

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