fauna-mysql 2.8.1.1 → 2.8.1.2

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