jberkel-mysql-ruby 2.8.1

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