Hoodow-mysql-ruby 2.8.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 +1098 -0
  4. data/README_ja.html +1323 -0
  5. data/ext/extconf.rb +112 -0
  6. data/ext/mysql.c +2271 -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,2271 @@
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
+ rb_hash_aset(hash, rb_ary_entry(colname, i), row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
1078
+ }
1079
+ return hash;
1080
+ }
1081
+
1082
+ /* fetch_hash(with_table=false) */
1083
+ static VALUE fetch_hash(int argc, VALUE* argv, VALUE obj)
1084
+ {
1085
+ VALUE with_table;
1086
+ check_free(obj);
1087
+ rb_scan_args(argc, argv, "01", &with_table);
1088
+ if (with_table == Qnil)
1089
+ with_table = Qfalse;
1090
+ return fetch_hash2(obj, with_table);
1091
+ }
1092
+
1093
+ /* field_seek(offset) */
1094
+ static VALUE field_seek(VALUE obj, VALUE offset)
1095
+ {
1096
+ check_free(obj);
1097
+ return INT2NUM(mysql_field_seek(GetMysqlRes(obj), NUM2INT(offset)));
1098
+ }
1099
+
1100
+ /* field_tell() */
1101
+ static VALUE field_tell(VALUE obj)
1102
+ {
1103
+ check_free(obj);
1104
+ return INT2NUM(mysql_field_tell(GetMysqlRes(obj)));
1105
+ }
1106
+
1107
+ /* free() */
1108
+ static VALUE res_free(VALUE obj)
1109
+ {
1110
+ struct mysql_res* resp = DATA_PTR(obj);
1111
+ check_free(obj);
1112
+ mysql_free_result(resp->res);
1113
+ resp->freed = Qtrue;
1114
+ store_result_count--;
1115
+ return Qnil;
1116
+ }
1117
+
1118
+ /* num_fields() */
1119
+ static VALUE num_fields(VALUE obj)
1120
+ {
1121
+ check_free(obj);
1122
+ return INT2NUM(mysql_num_fields(GetMysqlRes(obj)));
1123
+ }
1124
+
1125
+ /* num_rows() */
1126
+ static VALUE num_rows(VALUE obj)
1127
+ {
1128
+ check_free(obj);
1129
+ return INT2NUM(mysql_num_rows(GetMysqlRes(obj)));
1130
+ }
1131
+
1132
+ /* row_seek(offset) */
1133
+ static VALUE row_seek(VALUE obj, VALUE offset)
1134
+ {
1135
+ MYSQL_ROW_OFFSET prev_offset;
1136
+ if (CLASS_OF(offset) != cMysqlRowOffset)
1137
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Mysql::RowOffset)", rb_obj_classname(offset));
1138
+ check_free(obj);
1139
+ prev_offset = mysql_row_seek(GetMysqlRes(obj), DATA_PTR(offset));
1140
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, prev_offset);
1141
+ }
1142
+
1143
+ /* row_tell() */
1144
+ static VALUE row_tell(VALUE obj)
1145
+ {
1146
+ MYSQL_ROW_OFFSET offset;
1147
+ check_free(obj);
1148
+ offset = mysql_row_tell(GetMysqlRes(obj));
1149
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, offset);
1150
+ }
1151
+
1152
+ /* each {...} */
1153
+ static VALUE each(VALUE obj)
1154
+ {
1155
+ VALUE row;
1156
+ check_free(obj);
1157
+ while ((row = fetch_row(obj)) != Qnil)
1158
+ rb_yield(row);
1159
+ return obj;
1160
+ }
1161
+
1162
+ /* each_hash(with_table=false) {...} */
1163
+ static VALUE each_hash(int argc, VALUE* argv, VALUE obj)
1164
+ {
1165
+ VALUE with_table;
1166
+ VALUE hash;
1167
+ check_free(obj);
1168
+ rb_scan_args(argc, argv, "01", &with_table);
1169
+ if (with_table == Qnil)
1170
+ with_table = Qfalse;
1171
+ while ((hash = fetch_hash2(obj, with_table)) != Qnil)
1172
+ rb_yield(hash);
1173
+ return obj;
1174
+ }
1175
+
1176
+ /*-------------------------------
1177
+ * Mysql::Field object method
1178
+ */
1179
+
1180
+ /* hash */
1181
+ static VALUE field_hash(VALUE obj)
1182
+ {
1183
+ VALUE h = rb_hash_new();
1184
+ rb_hash_aset(h, rb_str_new2("name"), rb_iv_get(obj, "name"));
1185
+ rb_hash_aset(h, rb_str_new2("table"), rb_iv_get(obj, "table"));
1186
+ rb_hash_aset(h, rb_str_new2("def"), rb_iv_get(obj, "def"));
1187
+ rb_hash_aset(h, rb_str_new2("type"), rb_iv_get(obj, "type"));
1188
+ rb_hash_aset(h, rb_str_new2("length"), rb_iv_get(obj, "length"));
1189
+ rb_hash_aset(h, rb_str_new2("max_length"), rb_iv_get(obj, "max_length"));
1190
+ rb_hash_aset(h, rb_str_new2("flags"), rb_iv_get(obj, "flags"));
1191
+ rb_hash_aset(h, rb_str_new2("decimals"), rb_iv_get(obj, "decimals"));
1192
+ return h;
1193
+ }
1194
+
1195
+ /* inspect */
1196
+ static VALUE field_inspect(VALUE obj)
1197
+ {
1198
+ VALUE n = rb_iv_get(obj, "name");
1199
+ VALUE s = rb_str_new(0, RSTRING_LEN(n) + 16);
1200
+ sprintf(RSTRING_PTR(s), "#<Mysql::Field:%s>", RSTRING_PTR(n));
1201
+ return s;
1202
+ }
1203
+
1204
+ #define DefineMysqlFieldMemberMethod(m)\
1205
+ static VALUE field_##m(VALUE obj)\
1206
+ {return rb_iv_get(obj, #m);}
1207
+
1208
+ DefineMysqlFieldMemberMethod(name)
1209
+ DefineMysqlFieldMemberMethod(table)
1210
+ DefineMysqlFieldMemberMethod(def)
1211
+ DefineMysqlFieldMemberMethod(type)
1212
+ DefineMysqlFieldMemberMethod(length)
1213
+ DefineMysqlFieldMemberMethod(max_length)
1214
+ DefineMysqlFieldMemberMethod(flags)
1215
+ DefineMysqlFieldMemberMethod(decimals)
1216
+
1217
+ #ifdef IS_NUM
1218
+ /* is_num? */
1219
+ static VALUE field_is_num(VALUE obj)
1220
+ {
1221
+ return IS_NUM(NUM2INT(rb_iv_get(obj, "type"))) ? Qtrue : Qfalse;
1222
+ }
1223
+ #endif
1224
+
1225
+ #ifdef IS_NOT_NULL
1226
+ /* is_not_null? */
1227
+ static VALUE field_is_not_null(VALUE obj)
1228
+ {
1229
+ return IS_NOT_NULL(NUM2INT(rb_iv_get(obj, "flags"))) ? Qtrue : Qfalse;
1230
+ }
1231
+ #endif
1232
+
1233
+ #ifdef IS_PRI_KEY
1234
+ /* is_pri_key? */
1235
+ static VALUE field_is_pri_key(VALUE obj)
1236
+ {
1237
+ return IS_PRI_KEY(NUM2INT(rb_iv_get(obj, "flags"))) ? Qtrue : Qfalse;
1238
+ }
1239
+ #endif
1240
+
1241
+ #if MYSQL_VERSION_ID >= 40102
1242
+ /*-------------------------------
1243
+ * Mysql::Stmt object method
1244
+ */
1245
+
1246
+ /* check if stmt is already closed */
1247
+ static void check_stmt_closed(VALUE obj)
1248
+ {
1249
+ struct mysql_stmt* s = DATA_PTR(obj);
1250
+ if (s->closed == Qtrue)
1251
+ rb_raise(eMysql, "Mysql::Stmt object is already closed");
1252
+ }
1253
+
1254
+ static void mysql_stmt_raise(MYSQL_STMT* s)
1255
+ {
1256
+ VALUE e = rb_exc_new2(eMysql, mysql_stmt_error(s));
1257
+ rb_iv_set(e, "errno", INT2FIX(mysql_stmt_errno(s)));
1258
+ rb_iv_set(e, "sqlstate", rb_tainted_str_new2(mysql_stmt_sqlstate(s)));
1259
+ rb_exc_raise(e);
1260
+ }
1261
+
1262
+ /* affected_rows() */
1263
+ static VALUE stmt_affected_rows(VALUE obj)
1264
+ {
1265
+ struct mysql_stmt* s = DATA_PTR(obj);
1266
+ my_ulonglong n;
1267
+ check_stmt_closed(obj);
1268
+ n = mysql_stmt_affected_rows(s->stmt);
1269
+ return INT2NUM(n);
1270
+ }
1271
+
1272
+ #if 0
1273
+ /* attr_get(option) */
1274
+ static VALUE stmt_attr_get(VALUE obj, VALUE opt)
1275
+ {
1276
+ struct mysql_stmt* s = DATA_PTR(obj);
1277
+ check_stmt_closed(obj);
1278
+ if (NUM2INT(opt) == STMT_ATTR_UPDATE_MAX_LENGTH) {
1279
+ my_bool arg;
1280
+ mysql_stmt_attr_get(s->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &arg);
1281
+ return arg == 1 ? Qtrue : Qfalse;
1282
+ }
1283
+ rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
1284
+ }
1285
+
1286
+ /* attr_set(option, arg) */
1287
+ static VALUE stmt_attr_set(VALUE obj, VALUE opt, VALUE val)
1288
+ {
1289
+ struct mysql_stmt* s = DATA_PTR(obj);
1290
+ check_stmt_closed(obj);
1291
+ if (NUM2INT(opt) == STMT_ATTR_UPDATE_MAX_LENGTH) {
1292
+ my_bool arg;
1293
+ arg = (val == Qnil || val == Qfalse) ? 0 : 1;
1294
+ mysql_stmt_attr_set(s->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &arg);
1295
+ return obj;
1296
+ }
1297
+ rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
1298
+ }
1299
+ #endif
1300
+
1301
+ /* bind_result(bind,...) */
1302
+ static VALUE stmt_bind_result(int argc, VALUE *argv, VALUE obj)
1303
+ {
1304
+ struct mysql_stmt* s = DATA_PTR(obj);
1305
+ int i;
1306
+ MYSQL_FIELD *field;
1307
+
1308
+ check_stmt_closed(obj);
1309
+ if (argc != s->result.n)
1310
+ rb_raise(eMysql, "bind_result: result value count(%d) != number of argument(%d)", s->result.n, argc);
1311
+ for (i = 0; i < argc; i++) {
1312
+ if (argv[i] == Qnil || argv[i] == rb_cNilClass) {
1313
+ field = mysql_fetch_fields(s->res);
1314
+ s->result.bind[i].buffer_type = field[i].type;
1315
+ }
1316
+ else if (argv[i] == rb_cString)
1317
+ s->result.bind[i].buffer_type = MYSQL_TYPE_STRING;
1318
+ else if (argv[i] == rb_cNumeric || argv[i] == rb_cInteger || argv[i] == rb_cFixnum)
1319
+ s->result.bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
1320
+ else if (argv[i] == rb_cFloat)
1321
+ s->result.bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
1322
+ else if (argv[i] == cMysqlTime)
1323
+ s->result.bind[i].buffer_type = MYSQL_TYPE_DATETIME;
1324
+ else
1325
+ rb_raise(rb_eTypeError, "unrecognized class: %s", RSTRING_PTR(rb_inspect(argv[i])));
1326
+ if (mysql_stmt_bind_result(s->stmt, s->result.bind))
1327
+ mysql_stmt_raise(s->stmt);
1328
+ }
1329
+ return obj;
1330
+ }
1331
+
1332
+ /* close() */
1333
+ static VALUE stmt_close(VALUE obj)
1334
+ {
1335
+ struct mysql_stmt* s = DATA_PTR(obj);
1336
+ check_stmt_closed(obj);
1337
+ mysql_stmt_close(s->stmt);
1338
+ s->closed = Qtrue;
1339
+ return Qnil;
1340
+ }
1341
+
1342
+ /* data_seek(offset) */
1343
+ static VALUE stmt_data_seek(VALUE obj, VALUE offset)
1344
+ {
1345
+ struct mysql_stmt* s = DATA_PTR(obj);
1346
+ check_stmt_closed(obj);
1347
+ mysql_stmt_data_seek(s->stmt, NUM2INT(offset));
1348
+ return obj;
1349
+ }
1350
+
1351
+ /* execute(arg,...) */
1352
+ static VALUE stmt_execute(int argc, VALUE *argv, VALUE obj)
1353
+ {
1354
+ struct mysql_stmt *s = DATA_PTR(obj);
1355
+ MYSQL_STMT *stmt = s->stmt;
1356
+ int i;
1357
+
1358
+ check_stmt_closed(obj);
1359
+ free_execute_memory(s);
1360
+ if (s->param.n != argc)
1361
+ rb_raise(eMysql, "execute: param_count(%d) != number of argument(%d)", s->param.n, argc);
1362
+ if (argc > 0) {
1363
+ memset(s->param.bind, 0, sizeof(*(s->param.bind))*argc);
1364
+ for (i = 0; i < argc; i++) {
1365
+ switch (TYPE(argv[i])) {
1366
+ case T_NIL:
1367
+ s->param.bind[i].buffer_type = MYSQL_TYPE_NULL;
1368
+ break;
1369
+ case T_FIXNUM:
1370
+ #if SIZEOF_INT < SIZEOF_LONG
1371
+ s->param.bind[i].buffer_type = MYSQL_TYPE_LONGLONG;
1372
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1373
+ *(LONG_LONG*)(s->param.bind[i].buffer) = FIX2LONG(argv[i]);
1374
+ #else
1375
+ s->param.bind[i].buffer_type = MYSQL_TYPE_LONG;
1376
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1377
+ *(int*)(s->param.bind[i].buffer) = FIX2INT(argv[i]);
1378
+ #endif
1379
+ break;
1380
+ case T_BIGNUM:
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) = rb_big2ll(argv[i]);
1384
+ break;
1385
+ case T_FLOAT:
1386
+ s->param.bind[i].buffer_type = MYSQL_TYPE_DOUBLE;
1387
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1388
+ *(double*)(s->param.bind[i].buffer) = NUM2DBL(argv[i]);
1389
+ break;
1390
+ case T_STRING:
1391
+ s->param.bind[i].buffer_type = MYSQL_TYPE_STRING;
1392
+ s->param.bind[i].buffer = RSTRING_PTR(argv[i]);
1393
+ s->param.bind[i].buffer_length = RSTRING_LEN(argv[i]);
1394
+ s->param.length[i] = RSTRING_LEN(argv[i]);
1395
+ s->param.bind[i].length = &(s->param.length[i]);
1396
+ break;
1397
+ default:
1398
+ if (CLASS_OF(argv[i]) == rb_cTime) {
1399
+ MYSQL_TIME t;
1400
+ VALUE a = rb_funcall(argv[i], rb_intern("to_a"), 0);
1401
+ s->param.bind[i].buffer_type = MYSQL_TYPE_DATETIME;
1402
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1403
+ memset(&t, 0, sizeof(t)); /* avoid warning */
1404
+ t.second_part = 0;
1405
+ t.neg = 0;
1406
+ t.second = FIX2INT(RARRAY_PTR(a)[0]);
1407
+ t.minute = FIX2INT(RARRAY_PTR(a)[1]);
1408
+ t.hour = FIX2INT(RARRAY_PTR(a)[2]);
1409
+ t.day = FIX2INT(RARRAY_PTR(a)[3]);
1410
+ t.month = FIX2INT(RARRAY_PTR(a)[4]);
1411
+ t.year = FIX2INT(RARRAY_PTR(a)[5]);
1412
+ *(MYSQL_TIME*)&(s->param.buffer[i]) = t;
1413
+ } else if (CLASS_OF(argv[i]) == cMysqlTime) {
1414
+ MYSQL_TIME t;
1415
+ s->param.bind[i].buffer_type = MYSQL_TYPE_DATETIME;
1416
+ s->param.bind[i].buffer = &(s->param.buffer[i]);
1417
+ memset(&t, 0, sizeof(t)); /* avoid warning */
1418
+ t.second_part = 0;
1419
+ t.neg = 0;
1420
+ t.second = NUM2INT(rb_iv_get(argv[i], "second"));
1421
+ t.minute = NUM2INT(rb_iv_get(argv[i], "minute"));
1422
+ t.hour = NUM2INT(rb_iv_get(argv[i], "hour"));
1423
+ t.day = NUM2INT(rb_iv_get(argv[i], "day"));
1424
+ t.month = NUM2INT(rb_iv_get(argv[i], "month"));
1425
+ t.year = NUM2INT(rb_iv_get(argv[i], "year"));
1426
+ *(MYSQL_TIME*)&(s->param.buffer[i]) = t;
1427
+ } else
1428
+ rb_raise(rb_eTypeError, "unsupported type: %d", TYPE(argv[i]));
1429
+ }
1430
+ }
1431
+ if (mysql_stmt_bind_param(stmt, s->param.bind))
1432
+ mysql_stmt_raise(stmt);
1433
+ }
1434
+
1435
+ if (mysql_stmt_execute(stmt))
1436
+ mysql_stmt_raise(stmt);
1437
+ if (s->res) {
1438
+ MYSQL_FIELD *field;
1439
+ if (mysql_stmt_store_result(stmt))
1440
+ mysql_stmt_raise(stmt);
1441
+ field = mysql_fetch_fields(s->res);
1442
+ for (i = 0; i < s->result.n; i++) {
1443
+ switch(s->result.bind[i].buffer_type) {
1444
+ case MYSQL_TYPE_NULL:
1445
+ break;
1446
+ case MYSQL_TYPE_TINY:
1447
+ case MYSQL_TYPE_SHORT:
1448
+ case MYSQL_TYPE_YEAR:
1449
+ case MYSQL_TYPE_INT24:
1450
+ case MYSQL_TYPE_LONG:
1451
+ case MYSQL_TYPE_LONGLONG:
1452
+ case MYSQL_TYPE_FLOAT:
1453
+ case MYSQL_TYPE_DOUBLE:
1454
+ s->result.bind[i].buffer = xmalloc(8);
1455
+ s->result.bind[i].buffer_length = 8;
1456
+ memset(s->result.bind[i].buffer, 0, 8);
1457
+ break;
1458
+ case MYSQL_TYPE_DECIMAL:
1459
+ case MYSQL_TYPE_STRING:
1460
+ case MYSQL_TYPE_VAR_STRING:
1461
+ case MYSQL_TYPE_TINY_BLOB:
1462
+ case MYSQL_TYPE_BLOB:
1463
+ case MYSQL_TYPE_MEDIUM_BLOB:
1464
+ case MYSQL_TYPE_LONG_BLOB:
1465
+ #if MYSQL_VERSION_ID >= 50003
1466
+ case MYSQL_TYPE_NEWDECIMAL:
1467
+ case MYSQL_TYPE_BIT:
1468
+ #endif
1469
+ s->result.bind[i].buffer = xmalloc(field[i].max_length);
1470
+ memset(s->result.bind[i].buffer, 0, field[i].max_length);
1471
+ s->result.bind[i].buffer_length = field[i].max_length;
1472
+ break;
1473
+ case MYSQL_TYPE_TIME:
1474
+ case MYSQL_TYPE_DATE:
1475
+ case MYSQL_TYPE_DATETIME:
1476
+ case MYSQL_TYPE_TIMESTAMP:
1477
+ s->result.bind[i].buffer = xmalloc(sizeof(MYSQL_TIME));
1478
+ s->result.bind[i].buffer_length = sizeof(MYSQL_TIME);
1479
+ memset(s->result.bind[i].buffer, 0, sizeof(MYSQL_TIME));
1480
+ break;
1481
+ default:
1482
+ rb_raise(rb_eTypeError, "unknown buffer_type: %d", s->result.bind[i].buffer_type);
1483
+ }
1484
+ }
1485
+ if (mysql_stmt_bind_result(s->stmt, s->result.bind))
1486
+ mysql_stmt_raise(s->stmt);
1487
+ }
1488
+ return obj;
1489
+ }
1490
+
1491
+ /* fetch() */
1492
+ static VALUE stmt_fetch(VALUE obj)
1493
+ {
1494
+ struct mysql_stmt* s = DATA_PTR(obj);
1495
+ VALUE ret;
1496
+ int i;
1497
+ int r;
1498
+
1499
+ check_stmt_closed(obj);
1500
+ r = mysql_stmt_fetch(s->stmt);
1501
+ if (r == MYSQL_NO_DATA)
1502
+ return Qnil;
1503
+ #ifdef MYSQL_DATA_TRUNCATED
1504
+ if (r == MYSQL_DATA_TRUNCATED)
1505
+ rb_raise(rb_eRuntimeError, "unexpectedly data truncated");
1506
+ #endif
1507
+ if (r == 1)
1508
+ mysql_stmt_raise(s->stmt);
1509
+
1510
+ ret = rb_ary_new2(s->result.n);
1511
+ for (i = 0; i < s->result.n; i++) {
1512
+ if (s->result.is_null[i])
1513
+ rb_ary_push(ret, Qnil);
1514
+ else {
1515
+ VALUE v;
1516
+ MYSQL_TIME *t;
1517
+ switch (s->result.bind[i].buffer_type) {
1518
+ case MYSQL_TYPE_TINY:
1519
+ if (s->result.bind[i].is_unsigned)
1520
+ v = UINT2NUM(*(unsigned char *)s->result.bind[i].buffer);
1521
+ else
1522
+ v = INT2NUM(*(signed char *)s->result.bind[i].buffer);
1523
+ break;
1524
+ case MYSQL_TYPE_SHORT:
1525
+ case MYSQL_TYPE_YEAR:
1526
+ if (s->result.bind[i].is_unsigned)
1527
+ v = UINT2NUM(*(unsigned short *)s->result.bind[i].buffer);
1528
+ else
1529
+ v = INT2NUM(*(short *)s->result.bind[i].buffer);
1530
+ break;
1531
+ case MYSQL_TYPE_INT24:
1532
+ case MYSQL_TYPE_LONG:
1533
+ if (s->result.bind[i].is_unsigned)
1534
+ v = UINT2NUM(*(unsigned int *)s->result.bind[i].buffer);
1535
+ else
1536
+ v = INT2NUM(*(int *)s->result.bind[i].buffer);
1537
+ break;
1538
+ case MYSQL_TYPE_LONGLONG:
1539
+ if (s->result.bind[i].is_unsigned)
1540
+ v = ULL2NUM(*(unsigned long long *)s->result.bind[i].buffer);
1541
+ else
1542
+ v = LL2NUM(*(long long *)s->result.bind[i].buffer);
1543
+ break;
1544
+ case MYSQL_TYPE_FLOAT:
1545
+ v = rb_float_new((double)(*(float *)s->result.bind[i].buffer));
1546
+ break;
1547
+ case MYSQL_TYPE_DOUBLE:
1548
+ v = rb_float_new(*(double *)s->result.bind[i].buffer);
1549
+ break;
1550
+ case MYSQL_TYPE_TIME:
1551
+ case MYSQL_TYPE_DATE:
1552
+ case MYSQL_TYPE_DATETIME:
1553
+ case MYSQL_TYPE_TIMESTAMP:
1554
+ t = (MYSQL_TIME *)s->result.bind[i].buffer;
1555
+ v = rb_obj_alloc(cMysqlTime);
1556
+ rb_funcall(v, rb_intern("initialize"), 8,
1557
+ INT2FIX(t->year), INT2FIX(t->month),
1558
+ INT2FIX(t->day), INT2FIX(t->hour),
1559
+ INT2FIX(t->minute), INT2FIX(t->second),
1560
+ (t->neg ? Qtrue : Qfalse),
1561
+ INT2FIX(t->second_part));
1562
+ break;
1563
+ case MYSQL_TYPE_DECIMAL:
1564
+ case MYSQL_TYPE_STRING:
1565
+ case MYSQL_TYPE_VAR_STRING:
1566
+ case MYSQL_TYPE_TINY_BLOB:
1567
+ case MYSQL_TYPE_BLOB:
1568
+ case MYSQL_TYPE_MEDIUM_BLOB:
1569
+ case MYSQL_TYPE_LONG_BLOB:
1570
+ #if MYSQL_VERSION_ID >= 50003
1571
+ case MYSQL_TYPE_NEWDECIMAL:
1572
+ case MYSQL_TYPE_BIT:
1573
+ #endif
1574
+ v = rb_tainted_str_new(s->result.bind[i].buffer, s->result.length[i]);
1575
+ break;
1576
+ default:
1577
+ rb_raise(rb_eTypeError, "unknown buffer_type: %d", s->result.bind[i].buffer_type);
1578
+ }
1579
+ rb_ary_push(ret, v);
1580
+ }
1581
+ }
1582
+ return ret;
1583
+ }
1584
+
1585
+ /* each {...} */
1586
+ static VALUE stmt_each(VALUE obj)
1587
+ {
1588
+ VALUE row;
1589
+ check_stmt_closed(obj);
1590
+ while ((row = stmt_fetch(obj)) != Qnil)
1591
+ rb_yield(row);
1592
+ return obj;
1593
+ }
1594
+
1595
+ /* field_count() */
1596
+ static VALUE stmt_field_count(VALUE obj)
1597
+ {
1598
+ struct mysql_stmt* s = DATA_PTR(obj);
1599
+ unsigned int n;
1600
+ check_stmt_closed(obj);
1601
+ n = mysql_stmt_field_count(s->stmt);
1602
+ return INT2NUM(n);
1603
+ }
1604
+
1605
+ /* free_result() */
1606
+ static VALUE stmt_free_result(VALUE obj)
1607
+ {
1608
+ struct mysql_stmt* s = DATA_PTR(obj);
1609
+ check_stmt_closed(obj);
1610
+ if (mysql_stmt_free_result(s->stmt))
1611
+ mysql_stmt_raise(s->stmt);
1612
+ return obj;
1613
+ }
1614
+
1615
+ /* insert_id() */
1616
+ static VALUE stmt_insert_id(VALUE obj)
1617
+ {
1618
+ struct mysql_stmt* s = DATA_PTR(obj);
1619
+ my_ulonglong n;
1620
+ check_stmt_closed(obj);
1621
+ n = mysql_stmt_insert_id(s->stmt);
1622
+ return INT2NUM(n);
1623
+ }
1624
+
1625
+ /* num_rows() */
1626
+ static VALUE stmt_num_rows(VALUE obj)
1627
+ {
1628
+ struct mysql_stmt* s = DATA_PTR(obj);
1629
+ my_ulonglong n;
1630
+ check_stmt_closed(obj);
1631
+ n = mysql_stmt_num_rows(s->stmt);
1632
+ return INT2NUM(n);
1633
+ }
1634
+
1635
+ /* param_count() */
1636
+ static VALUE stmt_param_count(VALUE obj)
1637
+ {
1638
+ struct mysql_stmt* s = DATA_PTR(obj);
1639
+ unsigned long n;
1640
+ check_stmt_closed(obj);
1641
+ n = mysql_stmt_param_count(s->stmt);
1642
+ return INT2NUM(n);
1643
+ }
1644
+
1645
+ /* prepare(query) */
1646
+ static VALUE stmt_prepare(VALUE obj, VALUE query)
1647
+ {
1648
+ struct mysql_stmt* s = DATA_PTR(obj);
1649
+ int n;
1650
+ int i;
1651
+ MYSQL_FIELD *field;
1652
+
1653
+ free_mysqlstmt_memory(s);
1654
+ check_stmt_closed(obj);
1655
+ Check_Type(query, T_STRING);
1656
+ if (mysql_stmt_prepare(s->stmt, RSTRING_PTR(query), RSTRING_LEN(query)))
1657
+ mysql_stmt_raise(s->stmt);
1658
+
1659
+ n = mysql_stmt_param_count(s->stmt);
1660
+ s->param.n = n;
1661
+ s->param.bind = xmalloc(sizeof(s->param.bind[0]) * n);
1662
+ s->param.length = xmalloc(sizeof(s->param.length[0]) * n);
1663
+ s->param.buffer = xmalloc(sizeof(s->param.buffer[0]) * n);
1664
+
1665
+ s->res = mysql_stmt_result_metadata(s->stmt);
1666
+ if (s->res) {
1667
+ n = s->result.n = mysql_num_fields(s->res);
1668
+ s->result.bind = xmalloc(sizeof(s->result.bind[0]) * n);
1669
+ s->result.is_null = xmalloc(sizeof(s->result.is_null[0]) * n);
1670
+ s->result.length = xmalloc(sizeof(s->result.length[0]) * n);
1671
+ field = mysql_fetch_fields(s->res);
1672
+ memset(s->result.bind, 0, sizeof(s->result.bind[0]) * n);
1673
+ for (i = 0; i < n; i++) {
1674
+ s->result.bind[i].buffer_type = field[i].type;
1675
+ #if MYSQL_VERSION_ID < 50003
1676
+ if (field[i].type == MYSQL_TYPE_DECIMAL)
1677
+ s->result.bind[i].buffer_type = MYSQL_TYPE_STRING;
1678
+ #endif
1679
+ s->result.bind[i].is_null = &(s->result.is_null[i]);
1680
+ s->result.bind[i].length = &(s->result.length[i]);
1681
+ s->result.bind[i].is_unsigned = ((field[i].flags & UNSIGNED_FLAG) != 0);
1682
+ }
1683
+ } else {
1684
+ if (mysql_stmt_errno(s->stmt))
1685
+ mysql_stmt_raise(s->stmt);
1686
+ }
1687
+
1688
+ return obj;
1689
+ }
1690
+
1691
+ #if 0
1692
+ /* reset() */
1693
+ static VALUE stmt_reset(VALUE obj)
1694
+ {
1695
+ struct mysql_stmt* s = DATA_PTR(obj);
1696
+ check_stmt_closed(obj);
1697
+ if (mysql_stmt_reset(s->stmt))
1698
+ mysql_stmt_raise(s->stmt);
1699
+ return obj;
1700
+ }
1701
+ #endif
1702
+
1703
+ /* result_metadata() */
1704
+ static VALUE stmt_result_metadata(VALUE obj)
1705
+ {
1706
+ struct mysql_stmt* s = DATA_PTR(obj);
1707
+ MYSQL_RES *res;
1708
+ check_stmt_closed(obj);
1709
+ res = mysql_stmt_result_metadata(s->stmt);
1710
+ if (res == NULL) {
1711
+ if (mysql_stmt_errno(s->stmt) != 0)
1712
+ mysql_stmt_raise(s->stmt);
1713
+ return Qnil;
1714
+ }
1715
+ return mysqlres2obj(res);
1716
+ }
1717
+
1718
+ /* row_seek(offset) */
1719
+ static VALUE stmt_row_seek(VALUE obj, VALUE offset)
1720
+ {
1721
+ struct mysql_stmt* s = DATA_PTR(obj);
1722
+ MYSQL_ROW_OFFSET prev_offset;
1723
+ if (CLASS_OF(offset) != cMysqlRowOffset)
1724
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Mysql::RowOffset)", rb_obj_classname(offset));
1725
+ check_stmt_closed(obj);
1726
+ prev_offset = mysql_stmt_row_seek(s->stmt, DATA_PTR(offset));
1727
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, prev_offset);
1728
+ }
1729
+
1730
+ /* row_tell() */
1731
+ static VALUE stmt_row_tell(VALUE obj)
1732
+ {
1733
+ struct mysql_stmt* s = DATA_PTR(obj);
1734
+ MYSQL_ROW_OFFSET offset;
1735
+ check_stmt_closed(obj);
1736
+ offset = mysql_stmt_row_tell(s->stmt);
1737
+ return Data_Wrap_Struct(cMysqlRowOffset, 0, NULL, offset);
1738
+ }
1739
+
1740
+ #if 0
1741
+ /* send_long_data(col, data) */
1742
+ static VALUE stmt_send_long_data(VALUE obj, VALUE col, VALUE data)
1743
+ {
1744
+ struct mysql_stmt* s = DATA_PTR(obj);
1745
+ int c;
1746
+ check_stmt_closed(obj);
1747
+ c = NUM2INT(col);
1748
+ if (0 <= c && c < s->param.n) {
1749
+ s->param.bind[c].buffer_type = MYSQL_TYPE_STRING;
1750
+ if (mysql_stmt_bind_param(s->stmt, s->param.bind))
1751
+ mysql_stmt_raise(s->stmt);
1752
+ }
1753
+ if (mysql_stmt_send_long_data(s->stmt, c, RSTRING_PTR(data), RSTRING_LEN(data)))
1754
+ mysql_stmt_raise(s->stmt);
1755
+ return obj;
1756
+ }
1757
+ #endif
1758
+
1759
+ /* sqlstate() */
1760
+ static VALUE stmt_sqlstate(VALUE obj)
1761
+ {
1762
+ struct mysql_stmt* s = DATA_PTR(obj);
1763
+ return rb_tainted_str_new2(mysql_stmt_sqlstate(s->stmt));
1764
+ }
1765
+
1766
+ /*-------------------------------
1767
+ * Mysql::Time object method
1768
+ */
1769
+
1770
+ static VALUE time_initialize(int argc, VALUE* argv, VALUE obj)
1771
+ {
1772
+ VALUE year, month, day, hour, minute, second, neg, second_part;
1773
+ rb_scan_args(argc, argv, "08", &year, &month, &day, &hour, &minute, &second, &neg, &second_part);
1774
+ #define NILorFIXvalue(o) (NIL_P(o) ? INT2FIX(0) : (Check_Type(o, T_FIXNUM), o))
1775
+ rb_iv_set(obj, "year", NILorFIXvalue(year));
1776
+ rb_iv_set(obj, "month", NILorFIXvalue(month));
1777
+ rb_iv_set(obj, "day", NILorFIXvalue(day));
1778
+ rb_iv_set(obj, "hour", NILorFIXvalue(hour));
1779
+ rb_iv_set(obj, "minute", NILorFIXvalue(minute));
1780
+ rb_iv_set(obj, "second", NILorFIXvalue(second));
1781
+ rb_iv_set(obj, "neg", (neg == Qnil || neg == Qfalse) ? Qfalse : Qtrue);
1782
+ rb_iv_set(obj, "second_part", NILorFIXvalue(second_part));
1783
+ return obj;
1784
+ }
1785
+
1786
+ static VALUE time_inspect(VALUE obj)
1787
+ {
1788
+ char buf[36];
1789
+ sprintf(buf, "#<Mysql::Time:%04d-%02d-%02d %02d:%02d:%02d>",
1790
+ NUM2INT(rb_iv_get(obj, "year")),
1791
+ NUM2INT(rb_iv_get(obj, "month")),
1792
+ NUM2INT(rb_iv_get(obj, "day")),
1793
+ NUM2INT(rb_iv_get(obj, "hour")),
1794
+ NUM2INT(rb_iv_get(obj, "minute")),
1795
+ NUM2INT(rb_iv_get(obj, "second")));
1796
+ return rb_str_new2(buf);
1797
+ }
1798
+
1799
+ static VALUE time_to_s(VALUE obj)
1800
+ {
1801
+ char buf[20];
1802
+ sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d",
1803
+ NUM2INT(rb_iv_get(obj, "year")),
1804
+ NUM2INT(rb_iv_get(obj, "month")),
1805
+ NUM2INT(rb_iv_get(obj, "day")),
1806
+ NUM2INT(rb_iv_get(obj, "hour")),
1807
+ NUM2INT(rb_iv_get(obj, "minute")),
1808
+ NUM2INT(rb_iv_get(obj, "second")));
1809
+ return rb_str_new2(buf);
1810
+ }
1811
+
1812
+ #define DefineMysqlTimeGetMethod(m)\
1813
+ static VALUE time_get_##m(VALUE obj)\
1814
+ {return rb_iv_get(obj, #m);}
1815
+
1816
+ DefineMysqlTimeGetMethod(year)
1817
+ DefineMysqlTimeGetMethod(month)
1818
+ DefineMysqlTimeGetMethod(day)
1819
+ DefineMysqlTimeGetMethod(hour)
1820
+ DefineMysqlTimeGetMethod(minute)
1821
+ DefineMysqlTimeGetMethod(second)
1822
+ DefineMysqlTimeGetMethod(neg)
1823
+ DefineMysqlTimeGetMethod(second_part)
1824
+
1825
+ #define DefineMysqlTimeSetMethod(m)\
1826
+ static VALUE time_set_##m(VALUE obj, VALUE v)\
1827
+ {rb_iv_set(obj, #m, NILorFIXvalue(v)); return v;}
1828
+
1829
+ DefineMysqlTimeSetMethod(year)
1830
+ DefineMysqlTimeSetMethod(month)
1831
+ DefineMysqlTimeSetMethod(day)
1832
+ DefineMysqlTimeSetMethod(hour)
1833
+ DefineMysqlTimeSetMethod(minute)
1834
+ DefineMysqlTimeSetMethod(second)
1835
+ DefineMysqlTimeSetMethod(second_part)
1836
+
1837
+ static VALUE time_set_neg(VALUE obj, VALUE v)
1838
+ {
1839
+ rb_iv_set(obj, "neg", (v == Qnil || v == Qfalse) ? Qfalse : Qtrue);
1840
+ return v;
1841
+ }
1842
+
1843
+ static VALUE time_equal(VALUE obj, VALUE v)
1844
+ {
1845
+ if (CLASS_OF(v) == cMysqlTime &&
1846
+ NUM2INT(rb_iv_get(obj, "year")) == NUM2INT(rb_iv_get(v, "year")) &&
1847
+ NUM2INT(rb_iv_get(obj, "month")) == NUM2INT(rb_iv_get(v, "month")) &&
1848
+ NUM2INT(rb_iv_get(obj, "day")) == NUM2INT(rb_iv_get(v, "day")) &&
1849
+ NUM2INT(rb_iv_get(obj, "hour")) == NUM2INT(rb_iv_get(v, "hour")) &&
1850
+ NUM2INT(rb_iv_get(obj, "minute")) == NUM2INT(rb_iv_get(v, "minute")) &&
1851
+ NUM2INT(rb_iv_get(obj, "second")) == NUM2INT(rb_iv_get(v, "second")) &&
1852
+ rb_iv_get(obj, "neg") == rb_iv_get(v, "neg") &&
1853
+ NUM2INT(rb_iv_get(obj, "second_part")) == NUM2INT(rb_iv_get(v, "second_part")))
1854
+ return Qtrue;
1855
+ return Qfalse;
1856
+ }
1857
+
1858
+ #endif
1859
+
1860
+ /*-------------------------------
1861
+ * Mysql::Error object method
1862
+ */
1863
+
1864
+ static VALUE error_error(VALUE obj)
1865
+ {
1866
+ return rb_iv_get(obj, "mesg");
1867
+ }
1868
+
1869
+ static VALUE error_errno(VALUE obj)
1870
+ {
1871
+ return rb_iv_get(obj, "errno");
1872
+ }
1873
+
1874
+ static VALUE error_sqlstate(VALUE obj)
1875
+ {
1876
+ return rb_iv_get(obj, "sqlstate");
1877
+ }
1878
+
1879
+ /*-------------------------------
1880
+ * Initialize
1881
+ */
1882
+
1883
+ void Init_mysql(void)
1884
+ {
1885
+ cMysql = rb_define_class("Mysql", rb_cObject);
1886
+ cMysqlRes = rb_define_class_under(cMysql, "Result", rb_cObject);
1887
+ cMysqlField = rb_define_class_under(cMysql, "Field", rb_cObject);
1888
+ #if MYSQL_VERSION_ID >= 40102
1889
+ cMysqlStmt = rb_define_class_under(cMysql, "Stmt", rb_cObject);
1890
+ cMysqlRowOffset = rb_define_class_under(cMysql, "RowOffset", rb_cObject);
1891
+ cMysqlTime = rb_define_class_under(cMysql, "Time", rb_cObject);
1892
+ #endif
1893
+ eMysql = rb_define_class_under(cMysql, "Error", rb_eStandardError);
1894
+
1895
+ rb_define_global_const("MysqlRes", cMysqlRes);
1896
+ rb_define_global_const("MysqlField", cMysqlField);
1897
+ rb_define_global_const("MysqlError", eMysql);
1898
+
1899
+ /* Mysql class method */
1900
+ rb_define_singleton_method(cMysql, "init", init, 0);
1901
+ rb_define_singleton_method(cMysql, "real_connect", real_connect, -1);
1902
+ rb_define_singleton_method(cMysql, "connect", real_connect, -1);
1903
+ rb_define_singleton_method(cMysql, "new", real_connect, -1);
1904
+ rb_define_singleton_method(cMysql, "escape_string", escape_string, 1);
1905
+ rb_define_singleton_method(cMysql, "quote", escape_string, 1);
1906
+ rb_define_singleton_method(cMysql, "client_info", client_info, 0);
1907
+ rb_define_singleton_method(cMysql, "get_client_info", client_info, 0);
1908
+ #if MYSQL_VERSION_ID >= 32332
1909
+ rb_define_singleton_method(cMysql, "debug", my_debug, 1);
1910
+ #endif
1911
+ #if MYSQL_VERSION_ID >= 40000
1912
+ rb_define_singleton_method(cMysql, "get_client_version", client_version, 0);
1913
+ rb_define_singleton_method(cMysql, "client_version", client_version, 0);
1914
+ #endif
1915
+
1916
+ /* Mysql object method */
1917
+ #if MYSQL_VERSION_ID >= 32200
1918
+ rb_define_method(cMysql, "real_connect", real_connect2, -1);
1919
+ rb_define_method(cMysql, "connect", real_connect2, -1);
1920
+ rb_define_method(cMysql, "options", options, -1);
1921
+ #endif
1922
+ rb_define_method(cMysql, "initialize", initialize, -1);
1923
+ #if MYSQL_VERSION_ID >= 32332
1924
+ rb_define_method(cMysql, "escape_string", real_escape_string, 1);
1925
+ rb_define_method(cMysql, "quote", real_escape_string, 1);
1926
+ #else
1927
+ rb_define_method(cMysql, "escape_string", escape_string, 1);
1928
+ rb_define_method(cMysql, "quote", escape_string, 1);
1929
+ #endif
1930
+ rb_define_method(cMysql, "client_info", client_info, 0);
1931
+ rb_define_method(cMysql, "get_client_info", client_info, 0);
1932
+ rb_define_method(cMysql, "affected_rows", affected_rows, 0);
1933
+ #if MYSQL_VERSION_ID >= 32303
1934
+ rb_define_method(cMysql, "change_user", change_user, -1);
1935
+ #endif
1936
+ #if MYSQL_VERSION_ID >= 32321
1937
+ rb_define_method(cMysql, "character_set_name", character_set_name, 0);
1938
+ #endif
1939
+ rb_define_method(cMysql, "close", my_close, 0);
1940
+ #if MYSQL_VERSION_ID < 40000
1941
+ rb_define_method(cMysql, "create_db", create_db, 1);
1942
+ rb_define_method(cMysql, "drop_db", drop_db, 1);
1943
+ #endif
1944
+ #if MYSQL_VERSION_ID >= 32332
1945
+ rb_define_method(cMysql, "dump_debug_info", dump_debug_info, 0);
1946
+ #endif
1947
+ rb_define_method(cMysql, "errno", my_errno, 0);
1948
+ rb_define_method(cMysql, "error", my_error, 0);
1949
+ rb_define_method(cMysql, "field_count", field_count, 0);
1950
+ #if MYSQL_VERSION_ID >= 40000
1951
+ rb_define_method(cMysql, "get_client_version", client_version, 0);
1952
+ rb_define_method(cMysql, "client_version", client_version, 0);
1953
+ #endif
1954
+ rb_define_method(cMysql, "get_host_info", host_info, 0);
1955
+ rb_define_method(cMysql, "host_info", host_info, 0);
1956
+ rb_define_method(cMysql, "get_proto_info", proto_info, 0);
1957
+ rb_define_method(cMysql, "proto_info", proto_info, 0);
1958
+ rb_define_method(cMysql, "get_server_info", server_info, 0);
1959
+ rb_define_method(cMysql, "server_info", server_info, 0);
1960
+ rb_define_method(cMysql, "info", info, 0);
1961
+ rb_define_method(cMysql, "insert_id", insert_id, 0);
1962
+ rb_define_method(cMysql, "kill", my_kill, 1);
1963
+ rb_define_method(cMysql, "list_dbs", list_dbs, -1);
1964
+ rb_define_method(cMysql, "list_fields", list_fields, -1);
1965
+ rb_define_method(cMysql, "list_processes", list_processes, 0);
1966
+ rb_define_method(cMysql, "list_tables", list_tables, -1);
1967
+ #if MYSQL_VERSION_ID >= 32200
1968
+ rb_define_method(cMysql, "ping", ping, 0);
1969
+ #endif
1970
+ rb_define_method(cMysql, "query", query, 1);
1971
+ rb_define_method(cMysql, "real_query", query, 1);
1972
+ rb_define_method(cMysql, "refresh", refresh, 1);
1973
+ rb_define_method(cMysql, "reload", reload, 0);
1974
+ rb_define_method(cMysql, "select_db", select_db, 1);
1975
+ rb_define_method(cMysql, "shutdown", my_shutdown, -1);
1976
+ rb_define_method(cMysql, "stat", my_stat, 0);
1977
+ rb_define_method(cMysql, "store_result", store_result, 0);
1978
+ rb_define_method(cMysql, "thread_id", thread_id, 0);
1979
+ rb_define_method(cMysql, "use_result", use_result, 0);
1980
+ #if MYSQL_VERSION_ID >= 40100
1981
+ rb_define_method(cMysql, "get_server_version", server_version, 0);
1982
+ rb_define_method(cMysql, "server_version", server_version, 0);
1983
+ rb_define_method(cMysql, "warning_count", warning_count, 0);
1984
+ rb_define_method(cMysql, "commit", commit, 0);
1985
+ rb_define_method(cMysql, "rollback", rollback, 0);
1986
+ rb_define_method(cMysql, "autocommit", autocommit, 1);
1987
+ #endif
1988
+ #ifdef HAVE_MYSQL_SSL_SET
1989
+ rb_define_method(cMysql, "ssl_set", ssl_set, -1);
1990
+ #endif
1991
+ #if MYSQL_VERSION_ID >= 40102
1992
+ rb_define_method(cMysql, "stmt_init", stmt_init, 0);
1993
+ rb_define_method(cMysql, "prepare", prepare, 1);
1994
+ #endif
1995
+ #if MYSQL_VERSION_ID >= 40100
1996
+ rb_define_method(cMysql, "more_results", more_results, 0);
1997
+ rb_define_method(cMysql, "more_results?", more_results, 0);
1998
+ rb_define_method(cMysql, "next_result", next_result, 0);
1999
+ #endif
2000
+ #if MYSQL_VERSION_ID >= 40101
2001
+ rb_define_method(cMysql, "set_server_option", set_server_option, 1);
2002
+ rb_define_method(cMysql, "sqlstate", sqlstate, 0);
2003
+ #endif
2004
+ rb_define_method(cMysql, "query_with_result", query_with_result, 0);
2005
+ rb_define_method(cMysql, "query_with_result=", query_with_result_set, 1);
2006
+
2007
+ rb_define_method(cMysql, "reconnect", reconnect, 0);
2008
+ rb_define_method(cMysql, "reconnect=", reconnect_set, 1);
2009
+
2010
+ /* Mysql constant */
2011
+ rb_define_const(cMysql, "VERSION", INT2FIX(MYSQL_RUBY_VERSION));
2012
+ #if MYSQL_VERSION_ID >= 32200
2013
+ rb_define_const(cMysql, "OPT_CONNECT_TIMEOUT", INT2NUM(MYSQL_OPT_CONNECT_TIMEOUT));
2014
+ rb_define_const(cMysql, "OPT_COMPRESS", INT2NUM(MYSQL_OPT_COMPRESS));
2015
+ rb_define_const(cMysql, "OPT_NAMED_PIPE", INT2NUM(MYSQL_OPT_NAMED_PIPE));
2016
+ rb_define_const(cMysql, "INIT_COMMAND", INT2NUM(MYSQL_INIT_COMMAND));
2017
+ rb_define_const(cMysql, "READ_DEFAULT_FILE", INT2NUM(MYSQL_READ_DEFAULT_FILE));
2018
+ rb_define_const(cMysql, "READ_DEFAULT_GROUP", INT2NUM(MYSQL_READ_DEFAULT_GROUP));
2019
+ #endif
2020
+ #if MYSQL_VERSION_ID >= 32349
2021
+ rb_define_const(cMysql, "SET_CHARSET_DIR", INT2NUM(MYSQL_SET_CHARSET_DIR));
2022
+ rb_define_const(cMysql, "SET_CHARSET_NAME", INT2NUM(MYSQL_SET_CHARSET_NAME));
2023
+ rb_define_const(cMysql, "OPT_LOCAL_INFILE", INT2NUM(MYSQL_OPT_LOCAL_INFILE));
2024
+ #endif
2025
+ #if MYSQL_VERSION_ID >= 40100
2026
+ rb_define_const(cMysql, "OPT_PROTOCOL", INT2NUM(MYSQL_OPT_PROTOCOL));
2027
+ rb_define_const(cMysql, "SHARED_MEMORY_BASE_NAME", INT2NUM(MYSQL_SHARED_MEMORY_BASE_NAME));
2028
+ #endif
2029
+ #if MYSQL_VERSION_ID >= 40101
2030
+ rb_define_const(cMysql, "OPT_READ_TIMEOUT", INT2NUM(MYSQL_OPT_READ_TIMEOUT));
2031
+ rb_define_const(cMysql, "OPT_WRITE_TIMEOUT", INT2NUM(MYSQL_OPT_WRITE_TIMEOUT));
2032
+ rb_define_const(cMysql, "SECURE_AUTH", INT2NUM(MYSQL_SECURE_AUTH));
2033
+ rb_define_const(cMysql, "OPT_GUESS_CONNECTION", INT2NUM(MYSQL_OPT_GUESS_CONNECTION));
2034
+ rb_define_const(cMysql, "OPT_USE_EMBEDDED_CONNECTION", INT2NUM(MYSQL_OPT_USE_EMBEDDED_CONNECTION));
2035
+ rb_define_const(cMysql, "OPT_USE_REMOTE_CONNECTION", INT2NUM(MYSQL_OPT_USE_REMOTE_CONNECTION));
2036
+ rb_define_const(cMysql, "SET_CLIENT_IP", INT2NUM(MYSQL_SET_CLIENT_IP));
2037
+ #endif
2038
+ rb_define_const(cMysql, "REFRESH_GRANT", INT2NUM(REFRESH_GRANT));
2039
+ rb_define_const(cMysql, "REFRESH_LOG", INT2NUM(REFRESH_LOG));
2040
+ rb_define_const(cMysql, "REFRESH_TABLES", INT2NUM(REFRESH_TABLES));
2041
+ #ifdef REFRESH_HOSTS
2042
+ rb_define_const(cMysql, "REFRESH_HOSTS", INT2NUM(REFRESH_HOSTS));
2043
+ #endif
2044
+ #ifdef REFRESH_STATUS
2045
+ rb_define_const(cMysql, "REFRESH_STATUS", INT2NUM(REFRESH_STATUS));
2046
+ #endif
2047
+ #ifdef REFRESH_THREADS
2048
+ rb_define_const(cMysql, "REFRESH_THREADS", INT2NUM(REFRESH_THREADS));
2049
+ #endif
2050
+ #ifdef REFRESH_SLAVE
2051
+ rb_define_const(cMysql, "REFRESH_SLAVE", INT2NUM(REFRESH_SLAVE));
2052
+ #endif
2053
+ #ifdef REFRESH_MASTER
2054
+ rb_define_const(cMysql, "REFRESH_MASTER", INT2NUM(REFRESH_MASTER));
2055
+ #endif
2056
+ #ifdef CLIENT_LONG_PASSWORD
2057
+ #endif
2058
+ #ifdef CLIENT_FOUND_ROWS
2059
+ rb_define_const(cMysql, "CLIENT_FOUND_ROWS", INT2NUM(CLIENT_FOUND_ROWS));
2060
+ #endif
2061
+ #ifdef CLIENT_LONG_FLAG
2062
+ #endif
2063
+ #ifdef CLIENT_CONNECT_WITH_DB
2064
+ #endif
2065
+ #ifdef CLIENT_NO_SCHEMA
2066
+ rb_define_const(cMysql, "CLIENT_NO_SCHEMA", INT2NUM(CLIENT_NO_SCHEMA));
2067
+ #endif
2068
+ #ifdef CLIENT_COMPRESS
2069
+ rb_define_const(cMysql, "CLIENT_COMPRESS", INT2NUM(CLIENT_COMPRESS));
2070
+ #endif
2071
+ #ifdef CLIENT_ODBC
2072
+ rb_define_const(cMysql, "CLIENT_ODBC", INT2NUM(CLIENT_ODBC));
2073
+ #endif
2074
+ #ifdef CLIENT_LOCAL_FILES
2075
+ rb_define_const(cMysql, "CLIENT_LOCAL_FILES", INT2NUM(CLIENT_LOCAL_FILES));
2076
+ #endif
2077
+ #ifdef CLIENT_IGNORE_SPACE
2078
+ rb_define_const(cMysql, "CLIENT_IGNORE_SPACE", INT2NUM(CLIENT_IGNORE_SPACE));
2079
+ #endif
2080
+ #ifdef CLIENT_CHANGE_USER
2081
+ rb_define_const(cMysql, "CLIENT_CHANGE_USER", INT2NUM(CLIENT_CHANGE_USER));
2082
+ #endif
2083
+ #ifdef CLIENT_INTERACTIVE
2084
+ rb_define_const(cMysql, "CLIENT_INTERACTIVE", INT2NUM(CLIENT_INTERACTIVE));
2085
+ #endif
2086
+ #ifdef CLIENT_SSL
2087
+ rb_define_const(cMysql, "CLIENT_SSL", INT2NUM(CLIENT_SSL));
2088
+ #endif
2089
+ #ifdef CLIENT_IGNORE_SIGPIPE
2090
+ rb_define_const(cMysql, "CLIENT_IGNORE_SIGPIPE", INT2NUM(CLIENT_IGNORE_SIGPIPE));
2091
+ #endif
2092
+ #ifdef CLIENT_TRANSACTIONS
2093
+ rb_define_const(cMysql, "CLIENT_TRANSACTIONS", INT2NUM(CLIENT_TRANSACTIONS));
2094
+ #endif
2095
+ #ifdef CLIENT_MULTI_STATEMENTS
2096
+ rb_define_const(cMysql, "CLIENT_MULTI_STATEMENTS", INT2NUM(CLIENT_MULTI_STATEMENTS));
2097
+ #endif
2098
+ #ifdef CLIENT_MULTI_RESULTS
2099
+ rb_define_const(cMysql, "CLIENT_MULTI_RESULTS", INT2NUM(CLIENT_MULTI_RESULTS));
2100
+ #endif
2101
+ #if MYSQL_VERSION_ID >= 40101
2102
+ rb_define_const(cMysql, "OPTION_MULTI_STATEMENTS_ON", INT2NUM(MYSQL_OPTION_MULTI_STATEMENTS_ON));
2103
+ rb_define_const(cMysql, "OPTION_MULTI_STATEMENTS_OFF", INT2NUM(MYSQL_OPTION_MULTI_STATEMENTS_OFF));
2104
+ #endif
2105
+
2106
+ /* Mysql::Result object method */
2107
+ rb_define_method(cMysqlRes, "data_seek", data_seek, 1);
2108
+ rb_define_method(cMysqlRes, "fetch_field", fetch_field, 0);
2109
+ rb_define_method(cMysqlRes, "fetch_fields", fetch_fields, 0);
2110
+ rb_define_method(cMysqlRes, "fetch_field_direct", fetch_field_direct, 1);
2111
+ rb_define_method(cMysqlRes, "fetch_lengths", fetch_lengths, 0);
2112
+ rb_define_method(cMysqlRes, "fetch_row", fetch_row, 0);
2113
+ rb_define_method(cMysqlRes, "fetch_hash", fetch_hash, -1);
2114
+ rb_define_method(cMysqlRes, "field_seek", field_seek, 1);
2115
+ rb_define_method(cMysqlRes, "field_tell", field_tell, 0);
2116
+ rb_define_method(cMysqlRes, "free", res_free, 0);
2117
+ rb_define_method(cMysqlRes, "num_fields", num_fields, 0);
2118
+ rb_define_method(cMysqlRes, "num_rows", num_rows, 0);
2119
+ rb_define_method(cMysqlRes, "row_seek", row_seek, 1);
2120
+ rb_define_method(cMysqlRes, "row_tell", row_tell, 0);
2121
+ rb_define_method(cMysqlRes, "each", each, 0);
2122
+ rb_define_method(cMysqlRes, "each_hash", each_hash, -1);
2123
+
2124
+ /* MysqlField object method */
2125
+ rb_define_method(cMysqlField, "name", field_name, 0);
2126
+ rb_define_method(cMysqlField, "table", field_table, 0);
2127
+ rb_define_method(cMysqlField, "def", field_def, 0);
2128
+ rb_define_method(cMysqlField, "type", field_type, 0);
2129
+ rb_define_method(cMysqlField, "length", field_length, 0);
2130
+ rb_define_method(cMysqlField, "max_length", field_max_length, 0);
2131
+ rb_define_method(cMysqlField, "flags", field_flags, 0);
2132
+ rb_define_method(cMysqlField, "decimals", field_decimals, 0);
2133
+ rb_define_method(cMysqlField, "hash", field_hash, 0);
2134
+ rb_define_method(cMysqlField, "inspect", field_inspect, 0);
2135
+ #ifdef IS_NUM
2136
+ rb_define_method(cMysqlField, "is_num?", field_is_num, 0);
2137
+ #endif
2138
+ #ifdef IS_NOT_NULL
2139
+ rb_define_method(cMysqlField, "is_not_null?", field_is_not_null, 0);
2140
+ #endif
2141
+ #ifdef IS_PRI_KEY
2142
+ rb_define_method(cMysqlField, "is_pri_key?", field_is_pri_key, 0);
2143
+ #endif
2144
+
2145
+ /* Mysql::Field constant: TYPE */
2146
+ rb_define_const(cMysqlField, "TYPE_TINY", INT2NUM(FIELD_TYPE_TINY));
2147
+ #if MYSQL_VERSION_ID >= 32115
2148
+ rb_define_const(cMysqlField, "TYPE_ENUM", INT2NUM(FIELD_TYPE_ENUM));
2149
+ #endif
2150
+ rb_define_const(cMysqlField, "TYPE_DECIMAL", INT2NUM(FIELD_TYPE_DECIMAL));
2151
+ rb_define_const(cMysqlField, "TYPE_SHORT", INT2NUM(FIELD_TYPE_SHORT));
2152
+ rb_define_const(cMysqlField, "TYPE_LONG", INT2NUM(FIELD_TYPE_LONG));
2153
+ rb_define_const(cMysqlField, "TYPE_FLOAT", INT2NUM(FIELD_TYPE_FLOAT));
2154
+ rb_define_const(cMysqlField, "TYPE_DOUBLE", INT2NUM(FIELD_TYPE_DOUBLE));
2155
+ rb_define_const(cMysqlField, "TYPE_NULL", INT2NUM(FIELD_TYPE_NULL));
2156
+ rb_define_const(cMysqlField, "TYPE_TIMESTAMP", INT2NUM(FIELD_TYPE_TIMESTAMP));
2157
+ rb_define_const(cMysqlField, "TYPE_LONGLONG", INT2NUM(FIELD_TYPE_LONGLONG));
2158
+ rb_define_const(cMysqlField, "TYPE_INT24", INT2NUM(FIELD_TYPE_INT24));
2159
+ rb_define_const(cMysqlField, "TYPE_DATE", INT2NUM(FIELD_TYPE_DATE));
2160
+ rb_define_const(cMysqlField, "TYPE_TIME", INT2NUM(FIELD_TYPE_TIME));
2161
+ rb_define_const(cMysqlField, "TYPE_DATETIME", INT2NUM(FIELD_TYPE_DATETIME));
2162
+ #if MYSQL_VERSION_ID >= 32130
2163
+ rb_define_const(cMysqlField, "TYPE_YEAR", INT2NUM(FIELD_TYPE_YEAR));
2164
+ #endif
2165
+ #if MYSQL_VERSION_ID >= 50003
2166
+ rb_define_const(cMysqlField, "TYPE_BIT", INT2NUM(FIELD_TYPE_BIT));
2167
+ rb_define_const(cMysqlField, "TYPE_NEWDECIMAL", INT2NUM(FIELD_TYPE_NEWDECIMAL));
2168
+ #endif
2169
+ rb_define_const(cMysqlField, "TYPE_SET", INT2NUM(FIELD_TYPE_SET));
2170
+ rb_define_const(cMysqlField, "TYPE_BLOB", INT2NUM(FIELD_TYPE_BLOB));
2171
+ rb_define_const(cMysqlField, "TYPE_STRING", INT2NUM(FIELD_TYPE_STRING));
2172
+ #if MYSQL_VERSION_ID >= 40000
2173
+ rb_define_const(cMysqlField, "TYPE_VAR_STRING", INT2NUM(FIELD_TYPE_VAR_STRING));
2174
+ #endif
2175
+ rb_define_const(cMysqlField, "TYPE_CHAR", INT2NUM(FIELD_TYPE_CHAR));
2176
+
2177
+ /* Mysql::Field constant: FLAG */
2178
+ rb_define_const(cMysqlField, "NOT_NULL_FLAG", INT2NUM(NOT_NULL_FLAG));
2179
+ rb_define_const(cMysqlField, "PRI_KEY_FLAG", INT2NUM(PRI_KEY_FLAG));
2180
+ rb_define_const(cMysqlField, "UNIQUE_KEY_FLAG", INT2NUM(UNIQUE_KEY_FLAG));
2181
+ rb_define_const(cMysqlField, "MULTIPLE_KEY_FLAG", INT2NUM(MULTIPLE_KEY_FLAG));
2182
+ rb_define_const(cMysqlField, "BLOB_FLAG", INT2NUM(BLOB_FLAG));
2183
+ rb_define_const(cMysqlField, "UNSIGNED_FLAG", INT2NUM(UNSIGNED_FLAG));
2184
+ rb_define_const(cMysqlField, "ZEROFILL_FLAG", INT2NUM(ZEROFILL_FLAG));
2185
+ rb_define_const(cMysqlField, "BINARY_FLAG", INT2NUM(BINARY_FLAG));
2186
+ #ifdef ENUM_FLAG
2187
+ rb_define_const(cMysqlField, "ENUM_FLAG", INT2NUM(ENUM_FLAG));
2188
+ #endif
2189
+ #ifdef AUTO_INCREMENT_FLAG
2190
+ rb_define_const(cMysqlField, "AUTO_INCREMENT_FLAG", INT2NUM(AUTO_INCREMENT_FLAG));
2191
+ #endif
2192
+ #ifdef TIMESTAMP_FLAG
2193
+ rb_define_const(cMysqlField, "TIMESTAMP_FLAG", INT2NUM(TIMESTAMP_FLAG));
2194
+ #endif
2195
+ #ifdef SET_FLAG
2196
+ rb_define_const(cMysqlField, "SET_FLAG", INT2NUM(SET_FLAG));
2197
+ #endif
2198
+ #ifdef NUM_FLAG
2199
+ rb_define_const(cMysqlField, "NUM_FLAG", INT2NUM(NUM_FLAG));
2200
+ #endif
2201
+ #ifdef PART_KEY_FLAG
2202
+ rb_define_const(cMysqlField, "PART_KEY_FLAG", INT2NUM(PART_KEY_FLAG));
2203
+ #endif
2204
+
2205
+ #if MYSQL_VERSION_ID >= 40102
2206
+ /* Mysql::Stmt object method */
2207
+ rb_define_method(cMysqlStmt, "affected_rows", stmt_affected_rows, 0);
2208
+ #if 0
2209
+ rb_define_method(cMysqlStmt, "attr_get", stmt_attr_get, 1);
2210
+ rb_define_method(cMysqlStmt, "attr_set", stmt_attr_set, 2);
2211
+ #endif
2212
+ rb_define_method(cMysqlStmt, "bind_result", stmt_bind_result, -1);
2213
+ rb_define_method(cMysqlStmt, "close", stmt_close, 0);
2214
+ rb_define_method(cMysqlStmt, "data_seek", stmt_data_seek, 1);
2215
+ rb_define_method(cMysqlStmt, "each", stmt_each, 0);
2216
+ rb_define_method(cMysqlStmt, "execute", stmt_execute, -1);
2217
+ rb_define_method(cMysqlStmt, "fetch", stmt_fetch, 0);
2218
+ rb_define_method(cMysqlStmt, "field_count", stmt_field_count, 0);
2219
+ rb_define_method(cMysqlStmt, "free_result", stmt_free_result, 0);
2220
+ rb_define_method(cMysqlStmt, "insert_id", stmt_insert_id, 0);
2221
+ rb_define_method(cMysqlStmt, "num_rows", stmt_num_rows, 0);
2222
+ rb_define_method(cMysqlStmt, "param_count", stmt_param_count, 0);
2223
+ rb_define_method(cMysqlStmt, "prepare", stmt_prepare, 1);
2224
+ #if 0
2225
+ rb_define_method(cMysqlStmt, "reset", stmt_reset, 0);
2226
+ #endif
2227
+ rb_define_method(cMysqlStmt, "result_metadata", stmt_result_metadata, 0);
2228
+ rb_define_method(cMysqlStmt, "row_seek", stmt_row_seek, 1);
2229
+ rb_define_method(cMysqlStmt, "row_tell", stmt_row_tell, 0);
2230
+ #if 0
2231
+ rb_define_method(cMysqlStmt, "send_long_data", stmt_send_long_data, 2);
2232
+ #endif
2233
+ rb_define_method(cMysqlStmt, "sqlstate", stmt_sqlstate, 0);
2234
+
2235
+ #if 0
2236
+ rb_define_const(cMysqlStmt, "ATTR_UPDATE_MAX_LENGTH", INT2NUM(STMT_ATTR_UPDATE_MAX_LENGTH));
2237
+ #endif
2238
+
2239
+ /* Mysql::Time object method */
2240
+ rb_define_method(cMysqlTime, "initialize", time_initialize, -1);
2241
+ rb_define_method(cMysqlTime, "inspect", time_inspect, 0);
2242
+ rb_define_method(cMysqlTime, "to_s", time_to_s, 0);
2243
+ rb_define_method(cMysqlTime, "year", time_get_year, 0);
2244
+ rb_define_method(cMysqlTime, "month", time_get_month, 0);
2245
+ rb_define_method(cMysqlTime, "day", time_get_day, 0);
2246
+ rb_define_method(cMysqlTime, "hour", time_get_hour, 0);
2247
+ rb_define_method(cMysqlTime, "minute", time_get_minute, 0);
2248
+ rb_define_method(cMysqlTime, "second", time_get_second, 0);
2249
+ rb_define_method(cMysqlTime, "neg", time_get_neg, 0);
2250
+ rb_define_method(cMysqlTime, "second_part", time_get_second_part, 0);
2251
+ rb_define_method(cMysqlTime, "year=", time_set_year, 1);
2252
+ rb_define_method(cMysqlTime, "month=", time_set_month, 1);
2253
+ rb_define_method(cMysqlTime, "day=", time_set_day, 1);
2254
+ rb_define_method(cMysqlTime, "hour=", time_set_hour, 1);
2255
+ rb_define_method(cMysqlTime, "minute=", time_set_minute, 1);
2256
+ rb_define_method(cMysqlTime, "second=", time_set_second, 1);
2257
+ rb_define_method(cMysqlTime, "neg=", time_set_neg, 1);
2258
+ rb_define_method(cMysqlTime, "second_part=", time_set_second_part, 1);
2259
+ rb_define_method(cMysqlTime, "==", time_equal, 1);
2260
+
2261
+ #endif
2262
+
2263
+ /* Mysql::Error object method */
2264
+ rb_define_method(eMysql, "error", error_error, 0);
2265
+ rb_define_method(eMysql, "errno", error_errno, 0);
2266
+ rb_define_method(eMysql, "sqlstate", error_sqlstate, 0);
2267
+
2268
+ /* Mysql::Error constant */
2269
+ #define rb_define_mysql_const(s) rb_define_const(eMysql, #s, INT2NUM(s))
2270
+ #include "error_const.h"
2271
+ }