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