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