kwatch-mysql-ruby 2.8.0.1

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