mysql 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,67 @@
1
+ require 'mkmf'
2
+
3
+ if mc = with_config('mysql-config') then
4
+ mc = 'mysql_config' if mc == true
5
+ cflags = `#{mc} --cflags`.chomp
6
+ exit 1 if $? != 0
7
+ libs = `#{mc} --libs`.chomp
8
+ exit 1 if $? != 0
9
+ $CPPFLAGS += ' ' + cflags
10
+ $libs = libs + " " + $libs
11
+ else
12
+ inc, lib = dir_config('mysql', '/usr/local')
13
+ libs = ['m', 'z', 'socket', 'nsl']
14
+ while not find_library('mysqlclient', 'mysql_query', lib, "#{lib}/mysql") do
15
+ exit 1 if libs.empty?
16
+ have_library(libs.shift)
17
+ end
18
+ end
19
+
20
+ have_func('mysql_ssl_set')
21
+
22
+ if have_header('mysql.h') then
23
+ src = "#include <errmsg.h>\n#include <mysqld_error.h>\n"
24
+ elsif have_header('mysql/mysql.h') then
25
+ src = "#include <mysql/errmsg.h>\n#include <mysql/mysqld_error.h>\n"
26
+ else
27
+ exit 1
28
+ end
29
+
30
+ # make mysql constant
31
+ File::open("conftest.c", "w") do |f|
32
+ f.puts src
33
+ end
34
+ if defined? cpp_command then
35
+ cpp = Config::expand(cpp_command(''))
36
+ else
37
+ cpp = Config::expand sprintf(CPP, $CPPFLAGS, $CFLAGS, '')
38
+ end
39
+ unless system "#{cpp} > confout" then
40
+ exit 1
41
+ end
42
+ File::unlink "conftest.c"
43
+
44
+ error_syms = []
45
+ IO::foreach('confout') do |l|
46
+ next unless l =~ /errmsg\.h|mysqld_error\.h/
47
+ fn = l.split(/\"/)[1]
48
+ IO::foreach(fn) do |m|
49
+ if m =~ /^#define\s+([CE]R_[0-9A-Z_]+)/ then
50
+ error_syms << $1
51
+ end
52
+ end
53
+ end
54
+ File::unlink 'confout'
55
+ error_syms.uniq!
56
+
57
+ newf = File::open('mysql.c', 'w')
58
+ IO::foreach('mysql.c.in') do |l|
59
+ newf.puts l
60
+ if l =~ /\/\* Mysql::Error constant \*\// then
61
+ error_syms.each do |s|
62
+ newf.puts " rb_define_const(eMysql, \"#{s}\", INT2NUM(#{s}));"
63
+ end
64
+ end
65
+ end
66
+
67
+ create_makefile("mysql")
@@ -0,0 +1,62 @@
1
+ # for compatibility with 1.x
2
+ # $Id: mysql-compat.rb,v 1.2 1998/11/29 13:02:15 tommy Exp $
3
+
4
+ require 'mysql.o'
5
+
6
+ class Mysql
7
+ def Mysql.connect(host=nil, db=nil, user=nil, pass=nil)
8
+ Mysql.real_connect(host, user, pass, db)
9
+ end
10
+ def Mysql.new(host=nil, db=nil, user=nil, pass=nil)
11
+ Mysql.real_connect(host, user, pass, db)
12
+ end
13
+
14
+ alias selectdb select_db
15
+ alias affectedrows affected_rows
16
+ alias insertid insert_id
17
+ alias createdb create_db
18
+ alias dropdb drop_db
19
+
20
+ def listdbs()
21
+ ret = []
22
+ query("show databases").each {|x| ret << x[0]}
23
+ ret
24
+ end
25
+
26
+ def listtables()
27
+ ret = []
28
+ query("show tables").each {|x| ret << x[0]}
29
+ ret
30
+ end
31
+
32
+ def listfields(table)
33
+ query("show fields from #{table}")
34
+ end
35
+ end
36
+
37
+ class MysqlRes
38
+ alias numrows num_rows
39
+ alias numfields num_fields
40
+ alias fetchrow fetch_row
41
+ alias dataseek data_seek
42
+
43
+ def fetchfields()
44
+ ret = []
45
+ fetch_fields.each{|f| ret << f.name}
46
+ ret
47
+ end
48
+
49
+ def fetchhash()
50
+ row = fetchrow or return nil
51
+ fields = fetchfields
52
+ ret = {}
53
+ fields.each_index {|i| ret[fields[i]] = row[i]}
54
+ ret
55
+ end
56
+
57
+ def each_hash()
58
+ while hash = fetchhash
59
+ yield hash
60
+ end
61
+ end
62
+ end
Binary file
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+
3
+ SPEC = Gem::Specification.new do |s|
4
+ s.name = "mysql"
5
+ s.version = "2.5.1"
6
+ s.author = "TOMITA Masahiro"
7
+ s.email = "tommy@tmtm.org"
8
+ s.homepage = "http://www.tmtm.org/en/mysql/ruby/"
9
+ s.platform = Gem::Platform::RUBY
10
+ s.summary = "MySQL/Ruby provides the same functions for Ruby programs that the MySQL C API provides for C programs."
11
+ s.extensions = %w{extconf.rb}
12
+ s.has_rdoc = false
13
+ s.autorequire = "mysql"
14
+ s.files = Dir.glob('**/*')
15
+ end
16
+
17
+ if $0 == __FILE__
18
+ Gem::manage_gems
19
+ Gem::Builder.new(spec).build
20
+ end
@@ -0,0 +1,1267 @@
1
+ /* ruby mysql module
2
+ * $Id: mysql.c.in,v 1.8 2004/09/20 12:03:47 tommy Exp $
3
+ */
4
+
5
+ #include "ruby.h"
6
+ #include "version.h"
7
+ #ifdef HAVE_MYSQL_H
8
+ #include <mysql.h>
9
+ #include <errmsg.h>
10
+ #include <mysqld_error.h>
11
+ #else
12
+ #include <mysql/mysql.h>
13
+ #include <mysql/errmsg.h>
14
+ #include <mysql/mysqld_error.h>
15
+ #endif
16
+
17
+ #define GC_STORE_RESULT_LIMIT 20
18
+
19
+ #ifndef Qtrue /* ruby 1.2.x ? */
20
+ #define Qtrue TRUE
21
+ #define Qfalse FALSE
22
+ #define rb_exc_raise rb_raise
23
+ #define rb_exc_new2 exc_new2
24
+ #define rb_str_new str_new
25
+ #define rb_str_new2 str_new2
26
+ #define rb_ary_new2 ary_new2
27
+ #define rb_ary_store ary_store
28
+ #define rb_obj_alloc obj_alloc
29
+ #define rb_hash_new hash_new
30
+ #define rb_hash_aset hash_aset
31
+ #define rb_eStandardError eStandardError
32
+ #define rb_cObject cObject
33
+ #endif
34
+
35
+ #if MYSQL_VERSION_ID < 32224
36
+ #define mysql_field_count mysql_num_fields
37
+ #endif
38
+
39
+ #define NILorSTRING(obj) (NIL_P(obj)? NULL: STR2CSTR(obj))
40
+ #define NILorINT(obj) (NIL_P(obj)? 0: NUM2INT(obj))
41
+
42
+ #define GetMysqlStruct(obj) (Check_Type(obj, T_DATA), (struct mysql*)DATA_PTR(obj))
43
+ #define GetHandler(obj) (Check_Type(obj, T_DATA), &(((struct mysql*)DATA_PTR(obj))->handler))
44
+ #define GetMysqlRes(obj) (Check_Type(obj, T_DATA), ((struct mysql_res*)DATA_PTR(obj))->res)
45
+
46
+ VALUE cMysql;
47
+ VALUE cMysqlRes;
48
+ VALUE cMysqlField;
49
+ VALUE eMysql;
50
+
51
+ static int store_result_count = 0;
52
+
53
+ struct mysql {
54
+ MYSQL handler;
55
+ char connection;
56
+ char query_with_result;
57
+ };
58
+
59
+ struct mysql_res {
60
+ MYSQL_RES* res;
61
+ char freed;
62
+ };
63
+
64
+
65
+ /* free Mysql class object */
66
+ static void free_mysql(struct mysql* my)
67
+ {
68
+ if (my->connection == Qtrue)
69
+ mysql_close(&my->handler);
70
+ free(my);
71
+ }
72
+
73
+ static void free_mysqlres(struct mysql_res* resp)
74
+ {
75
+ if (resp->freed == Qfalse) {
76
+ mysql_free_result(resp->res);
77
+ store_result_count--;
78
+ }
79
+ free(resp);
80
+ }
81
+
82
+ static void mysql_raise(MYSQL* m)
83
+ {
84
+ VALUE e = rb_exc_new2(eMysql, mysql_error(m));
85
+ rb_iv_set(e, "errno", INT2FIX(mysql_errno(m)));
86
+ rb_exc_raise(e);
87
+ }
88
+
89
+ static VALUE mysqlres2obj(MYSQL_RES* res)
90
+ {
91
+ VALUE obj;
92
+ struct mysql_res* resp;
93
+ obj = Data_Make_Struct(cMysqlRes, struct mysql_res, 0, free_mysqlres, resp);
94
+ resp->res = res;
95
+ resp->freed = Qfalse;
96
+ rb_obj_call_init(obj, 0, NULL);
97
+ if (++store_result_count > GC_STORE_RESULT_LIMIT)
98
+ rb_gc();
99
+ return obj;
100
+ }
101
+
102
+ /* make Mysql::Field object */
103
+ static VALUE make_field_obj(MYSQL_FIELD* f)
104
+ {
105
+ VALUE obj;
106
+ VALUE hash;
107
+ if (f == NULL)
108
+ return Qnil;
109
+ obj = rb_obj_alloc(cMysqlField);
110
+ rb_iv_set(obj, "name", f->name? rb_str_freeze(rb_tainted_str_new2(f->name)): Qnil);
111
+ rb_iv_set(obj, "table", f->table? rb_str_freeze(rb_tainted_str_new2(f->table)): Qnil);
112
+ rb_iv_set(obj, "def", f->def? rb_str_freeze(rb_tainted_str_new2(f->def)): Qnil);
113
+ rb_iv_set(obj, "type", INT2NUM(f->type));
114
+ rb_iv_set(obj, "length", INT2NUM(f->length));
115
+ rb_iv_set(obj, "max_length", INT2NUM(f->max_length));
116
+ rb_iv_set(obj, "flags", INT2NUM(f->flags));
117
+ rb_iv_set(obj, "decimals", INT2NUM(f->decimals));
118
+ return obj;
119
+ }
120
+
121
+ /*-------------------------------
122
+ * Mysql class method
123
+ */
124
+
125
+ /* init() */
126
+ static VALUE init(VALUE klass)
127
+ {
128
+ struct mysql* myp;
129
+ VALUE obj;
130
+
131
+ obj = Data_Make_Struct(klass, struct mysql, 0, free_mysql, myp);
132
+ mysql_init(&myp->handler);
133
+ myp->connection = Qfalse;
134
+ myp->query_with_result = Qtrue;
135
+ rb_obj_call_init(obj, 0, NULL);
136
+ return obj;
137
+ }
138
+
139
+ /* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
140
+ static VALUE real_connect(int argc, VALUE* argv, VALUE klass)
141
+ {
142
+ VALUE host, user, passwd, db, port, sock, flag;
143
+ char *h, *u, *p, *d, *s;
144
+ unsigned int pp, f;
145
+ struct mysql* myp;
146
+ VALUE obj;
147
+
148
+ #if MYSQL_VERSION_ID >= 32200
149
+ rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
150
+ d = NILorSTRING(db);
151
+ f = NILorINT(flag);
152
+ #elif MYSQL_VERSION_ID >= 32115
153
+ rb_scan_args(argc, argv, "06", &host, &user, &passwd, &port, &sock, &flag);
154
+ f = NILorINT(flag);
155
+ #else
156
+ rb_scan_args(argc, argv, "05", &host, &user, &passwd, &port, &sock);
157
+ #endif
158
+ h = NILorSTRING(host);
159
+ u = NILorSTRING(user);
160
+ p = NILorSTRING(passwd);
161
+ pp = NILorINT(port);
162
+ s = NILorSTRING(sock);
163
+
164
+ obj = Data_Make_Struct(klass, struct mysql, 0, free_mysql, myp);
165
+ #if MYSQL_VERSION_ID >= 32200
166
+ mysql_init(&myp->handler);
167
+ if (mysql_real_connect(&myp->handler, h, u, p, d, pp, s, f) == NULL)
168
+ #elif MYSQL_VERSION_ID >= 32115
169
+ if (mysql_real_connect(&myp->handler, h, u, p, pp, s, f) == NULL)
170
+ #else
171
+ if (mysql_real_connect(&myp->handler, h, u, p, pp, s) == NULL)
172
+ #endif
173
+ mysql_raise(&myp->handler);
174
+
175
+ myp->connection = Qtrue;
176
+ myp->query_with_result = Qtrue;
177
+ rb_obj_call_init(obj, argc, argv);
178
+
179
+ return obj;
180
+ }
181
+
182
+ /* escape_string(string) */
183
+ static VALUE escape_string(VALUE klass, VALUE str)
184
+ {
185
+ VALUE ret;
186
+ Check_Type(str, T_STRING);
187
+ ret = rb_str_new(0, (RSTRING(str)->len)*2+1);
188
+ RSTRING(ret)->len = mysql_escape_string(RSTRING(ret)->ptr, RSTRING(str)->ptr, RSTRING(str)->len);
189
+ return ret;
190
+ }
191
+
192
+ /* client_info() */
193
+ static VALUE client_info(VALUE klass)
194
+ {
195
+ return rb_tainted_str_new2(mysql_get_client_info());
196
+ }
197
+
198
+ #if MYSQL_VERSION_ID >= 32332
199
+ /* my_debug(string) */
200
+ static VALUE my_debug(VALUE obj, VALUE str)
201
+ {
202
+ mysql_debug(STR2CSTR(str));
203
+ return obj;
204
+ }
205
+ #endif
206
+
207
+ #if MYSQL_VERSION_ID >= 40000
208
+ /* client_version() */
209
+ static VALUE client_version(VALUE obj)
210
+ {
211
+ return INT2NUM(mysql_get_client_version());
212
+ }
213
+ #endif
214
+
215
+ /*-------------------------------
216
+ * Mysql object method
217
+ */
218
+
219
+ #if MYSQL_VERSION_ID >= 32200
220
+ /* real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil) */
221
+ static VALUE real_connect2(int argc, VALUE* argv, VALUE obj)
222
+ {
223
+ VALUE host, user, passwd, db, port, sock, flag;
224
+ char *h, *u, *p, *d, *s;
225
+ unsigned int pp, f;
226
+ MYSQL* m = GetHandler(obj);
227
+ rb_scan_args(argc, argv, "07", &host, &user, &passwd, &db, &port, &sock, &flag);
228
+ d = NILorSTRING(db);
229
+ f = NILorINT(flag);
230
+ h = NILorSTRING(host);
231
+ u = NILorSTRING(user);
232
+ p = NILorSTRING(passwd);
233
+ pp = NILorINT(port);
234
+ s = NILorSTRING(sock);
235
+
236
+ if (mysql_real_connect(m, h, u, p, d, pp, s, f) == NULL)
237
+ mysql_raise(m);
238
+
239
+ return obj;
240
+ }
241
+
242
+ /* options(opt, value=nil) */
243
+ static VALUE options(int argc, VALUE* argv, VALUE obj)
244
+ {
245
+ VALUE opt, val;
246
+ int n;
247
+ char* v;
248
+ MYSQL* m = GetHandler(obj);
249
+
250
+ rb_scan_args(argc, argv, "11", &opt, &val);
251
+ switch(NUM2INT(opt)) {
252
+ case MYSQL_OPT_CONNECT_TIMEOUT:
253
+ if (val == Qnil)
254
+ rb_raise(rb_eArgError, "wrong # of arguments(1 for 2)");
255
+ n = NUM2INT(val);
256
+ v = (char*)&n;
257
+ break;
258
+ case MYSQL_INIT_COMMAND:
259
+ case MYSQL_READ_DEFAULT_FILE:
260
+ case MYSQL_READ_DEFAULT_GROUP:
261
+ if (val == Qnil)
262
+ rb_raise(rb_eArgError, "wrong # of arguments(1 for 2)");
263
+ v = STR2CSTR(val);
264
+ break;
265
+ #if MYSQL_VERSION_ID >= 32349
266
+ case MYSQL_OPT_LOCAL_INFILE:
267
+ if (val == Qnil || val == Qfalse)
268
+ v = NULL;
269
+ else {
270
+ n = 1;
271
+ v = (char*)&n;
272
+ }
273
+ break;
274
+ #endif
275
+ default:
276
+ v = NULL;
277
+ }
278
+
279
+ if (mysql_options(m, NUM2INT(opt), v) != 0)
280
+ rb_raise(eMysql, "unknown option: %d", NUM2INT(opt));
281
+ return obj;
282
+ }
283
+ #endif
284
+
285
+ #if MYSQL_VERSION_ID >= 32332
286
+ /* real_escape_string(string) */
287
+ static VALUE real_escape_string(VALUE obj, VALUE str)
288
+ {
289
+ MYSQL* m = GetHandler(obj);
290
+ VALUE ret;
291
+ Check_Type(str, T_STRING);
292
+ ret = rb_str_new(0, (RSTRING(str)->len)*2+1);
293
+ RSTRING(ret)->len = mysql_real_escape_string(m, RSTRING(ret)->ptr, RSTRING(str)->ptr, RSTRING(str)->len);
294
+ return ret;
295
+ }
296
+ #endif
297
+
298
+ /* initialize() */
299
+ static VALUE initialize(int argc, VALUE* argv, VALUE obj)
300
+ {
301
+ return obj;
302
+ }
303
+
304
+ /* affected_rows() */
305
+ static VALUE affected_rows(VALUE obj)
306
+ {
307
+ return INT2NUM(mysql_affected_rows(GetHandler(obj)));
308
+ }
309
+
310
+ #if MYSQL_VERSION_ID >= 32303
311
+ /* change_user(user=nil, passwd=nil, db=nil) */
312
+ static VALUE change_user(int argc, VALUE* argv, VALUE obj)
313
+ {
314
+ VALUE user, passwd, db;
315
+ char *u, *p, *d;
316
+ MYSQL* m = GetHandler(obj);
317
+ rb_scan_args(argc, argv, "03", &user, &passwd, &db);
318
+ u = NILorSTRING(user);
319
+ p = NILorSTRING(passwd);
320
+ d = NILorSTRING(db);
321
+ if (mysql_change_user(m, u, p, d) != 0)
322
+ mysql_raise(m);
323
+ return obj;
324
+ }
325
+ #endif
326
+
327
+ #if MYSQL_VERSION_ID >= 32321
328
+ /* character_set_name() */
329
+ static VALUE character_set_name(VALUE obj)
330
+ {
331
+ return rb_tainted_str_new2(mysql_character_set_name(GetHandler(obj)));
332
+ }
333
+ #endif
334
+
335
+ /* close() */
336
+ static VALUE my_close(VALUE obj)
337
+ {
338
+ MYSQL* m = GetHandler(obj);
339
+ mysql_close(m);
340
+ if (mysql_errno(m))
341
+ mysql_raise(m);
342
+ GetMysqlStruct(obj)->connection = Qfalse;
343
+ return obj;
344
+ }
345
+
346
+ #if MYSQL_VERSION_ID < 40000
347
+ /* create_db(db) */
348
+ static VALUE create_db(VALUE obj, VALUE db)
349
+ {
350
+ MYSQL* m = GetHandler(obj);
351
+ if (mysql_create_db(m, STR2CSTR(db)) != 0)
352
+ mysql_raise(m);
353
+ return obj;
354
+ }
355
+
356
+ /* drop_db(db) */
357
+ static VALUE drop_db(VALUE obj, VALUE db)
358
+ {
359
+ MYSQL* m = GetHandler(obj);
360
+ if (mysql_drop_db(m, STR2CSTR(db)) != 0)
361
+ mysql_raise(m);
362
+ return obj;
363
+ }
364
+ #endif
365
+
366
+ #if MYSQL_VERSION_ID >= 32332
367
+ /* dump_debug_info() */
368
+ static VALUE dump_debug_info(VALUE obj)
369
+ {
370
+ MYSQL* m = GetHandler(obj);
371
+ if (mysql_dump_debug_info(m) != 0)
372
+ mysql_raise(m);
373
+ return obj;
374
+ }
375
+ #endif
376
+
377
+ /* errno() */
378
+ static VALUE my_errno(VALUE obj)
379
+ {
380
+ return INT2NUM(mysql_errno(GetHandler(obj)));
381
+ }
382
+
383
+ /* error() */
384
+ static VALUE my_error(VALUE obj)
385
+ {
386
+ return rb_str_new2(mysql_error(GetHandler(obj)));
387
+ }
388
+
389
+ /* field_count() */
390
+ static VALUE field_count(VALUE obj)
391
+ {
392
+ return INT2NUM(mysql_field_count(GetHandler(obj)));
393
+ }
394
+
395
+ /* host_info() */
396
+ static VALUE host_info(VALUE obj)
397
+ {
398
+ return rb_tainted_str_new2(mysql_get_host_info(GetHandler(obj)));
399
+ }
400
+
401
+ /* proto_info() */
402
+ static VALUE proto_info(VALUE obj)
403
+ {
404
+ return INT2NUM(mysql_get_proto_info(GetHandler(obj)));
405
+ }
406
+
407
+ /* server_info() */
408
+ static VALUE server_info(VALUE obj)
409
+ {
410
+ return rb_tainted_str_new2(mysql_get_server_info(GetHandler(obj)));
411
+ }
412
+
413
+ /* info() */
414
+ static VALUE info(VALUE obj)
415
+ {
416
+ const char* p = mysql_info(GetHandler(obj));
417
+ return p? rb_tainted_str_new2(p): Qnil;
418
+ }
419
+
420
+ /* insert_id() */
421
+ static VALUE insert_id(VALUE obj)
422
+ {
423
+ return INT2NUM(mysql_insert_id(GetHandler(obj)));
424
+ }
425
+
426
+ /* kill(pid) */
427
+ static VALUE my_kill(VALUE obj, VALUE pid)
428
+ {
429
+ int p = NUM2INT(pid);
430
+ MYSQL* m = GetHandler(obj);
431
+ if (mysql_kill(m, p) != 0)
432
+ mysql_raise(m);
433
+ return obj;
434
+ }
435
+
436
+ /* list_dbs(db=nil) */
437
+ static VALUE list_dbs(int argc, VALUE* argv, VALUE obj)
438
+ {
439
+ unsigned int i, n;
440
+ VALUE db, ret;
441
+ MYSQL* m = GetHandler(obj);
442
+ MYSQL_RES* res;
443
+
444
+ rb_scan_args(argc, argv, "01", &db);
445
+ res = mysql_list_dbs(m, NILorSTRING(db));
446
+ if (res == NULL)
447
+ mysql_raise(m);
448
+
449
+ n = mysql_num_rows(res);
450
+ ret = rb_ary_new2(n);
451
+ for (i=0; i<n; i++)
452
+ rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
453
+ mysql_free_result(res);
454
+ return ret;
455
+ }
456
+
457
+ /* list_fields(table, field=nil) */
458
+ static VALUE list_fields(int argc, VALUE* argv, VALUE obj)
459
+ {
460
+ VALUE table, field;
461
+ MYSQL* m = GetHandler(obj);
462
+ MYSQL_RES* res;
463
+ rb_scan_args(argc, argv, "11", &table, &field);
464
+ res = mysql_list_fields(m, STR2CSTR(table), NILorSTRING(field));
465
+ if (res == NULL)
466
+ mysql_raise(m);
467
+ return mysqlres2obj(res);
468
+ }
469
+
470
+ /* list_processes() */
471
+ static VALUE list_processes(VALUE obj)
472
+ {
473
+ MYSQL* m = GetHandler(obj);
474
+ MYSQL_RES* res = mysql_list_processes(m);
475
+ if (res == NULL)
476
+ mysql_raise(m);
477
+ return mysqlres2obj(res);
478
+ }
479
+
480
+ /* list_tables(table=nil) */
481
+ static VALUE list_tables(int argc, VALUE* argv, VALUE obj)
482
+ {
483
+ VALUE table;
484
+ MYSQL* m = GetHandler(obj);
485
+ MYSQL_RES* res;
486
+ unsigned int i, n;
487
+ VALUE ret;
488
+
489
+ rb_scan_args(argc, argv, "01", &table);
490
+ res = mysql_list_tables(m, NILorSTRING(table));
491
+ if (res == NULL)
492
+ mysql_raise(m);
493
+
494
+ n = mysql_num_rows(res);
495
+ ret = rb_ary_new2(n);
496
+ for (i=0; i<n; i++)
497
+ rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
498
+ mysql_free_result(res);
499
+ return ret;
500
+ }
501
+
502
+ /* ping() */
503
+ static VALUE ping(VALUE obj)
504
+ {
505
+ MYSQL* m = GetHandler(obj);
506
+ if (mysql_ping(m) != 0)
507
+ mysql_raise(m);
508
+ return obj;
509
+ }
510
+
511
+ /* refresh(r) */
512
+ static VALUE refresh(VALUE obj, VALUE r)
513
+ {
514
+ MYSQL* m = GetHandler(obj);
515
+ if (mysql_refresh(m, NUM2INT(r)) != 0)
516
+ mysql_raise(m);
517
+ return obj;
518
+ }
519
+
520
+ /* reload() */
521
+ static VALUE reload(VALUE obj)
522
+ {
523
+ MYSQL* m = GetHandler(obj);
524
+ if (mysql_reload(m) != 0)
525
+ mysql_raise(m);
526
+ return obj;
527
+ }
528
+
529
+ /* select_db(db) */
530
+ static VALUE select_db(VALUE obj, VALUE db)
531
+ {
532
+ MYSQL* m = GetHandler(obj);
533
+ if (mysql_select_db(m, STR2CSTR(db)) != 0)
534
+ mysql_raise(m);
535
+ return obj;
536
+ }
537
+
538
+ /* shutdown() */
539
+ static VALUE my_shutdown(int argc, VALUE* argv, VALUE obj)
540
+ {
541
+ MYSQL* m = GetHandler(obj);
542
+ VALUE level;
543
+
544
+ rb_scan_args(argc, argv, "01", &level);
545
+ #if MYSQL_VERSION_ID >= 40103
546
+ if (mysql_shutdown(m, NIL_P(level) ? SHUTDOWN_DEFAULT : NUM2INT(level)) != 0)
547
+ #else
548
+ if (mysql_shutdown(m) != 0)
549
+ #endif
550
+ mysql_raise(m);
551
+ return obj;
552
+ }
553
+
554
+ /* stat() */
555
+ static VALUE my_stat(VALUE obj)
556
+ {
557
+ MYSQL* m = GetHandler(obj);
558
+ const char* s = mysql_stat(m);
559
+ if (s == NULL)
560
+ mysql_raise(m);
561
+ return rb_tainted_str_new2(s);
562
+ }
563
+
564
+ /* store_result() */
565
+ static VALUE store_result(VALUE obj)
566
+ {
567
+ MYSQL* m = GetHandler(obj);
568
+ MYSQL_RES* res = mysql_store_result(m);
569
+ if (res == NULL)
570
+ mysql_raise(m);
571
+ return mysqlres2obj(res);
572
+ }
573
+
574
+ /* thread_id() */
575
+ static VALUE thread_id(VALUE obj)
576
+ {
577
+ return INT2NUM(mysql_thread_id(GetHandler(obj)));
578
+ }
579
+
580
+ /* use_result() */
581
+ static VALUE use_result(VALUE obj)
582
+ {
583
+ MYSQL* m = GetHandler(obj);
584
+ MYSQL_RES* res = mysql_use_result(m);
585
+ if (res == NULL)
586
+ mysql_raise(m);
587
+ return mysqlres2obj(res);
588
+ }
589
+
590
+ /* query(sql) */
591
+ static VALUE query(VALUE obj, VALUE sql)
592
+ {
593
+ MYSQL* m = GetHandler(obj);
594
+ Check_Type(sql, T_STRING);
595
+ if (mysql_real_query(m, RSTRING(sql)->ptr, RSTRING(sql)->len) != 0)
596
+ mysql_raise(m);
597
+ if (GetMysqlStruct(obj)->query_with_result == Qfalse)
598
+ return obj;
599
+ if (mysql_field_count(m) == 0)
600
+ return Qnil;
601
+ return store_result(obj);
602
+ }
603
+
604
+ #if MYSQL_VERSION_ID >= 40100
605
+ /* server_version() */
606
+ static VALUE server_version(VALUE obj)
607
+ {
608
+ return INT2NUM(mysql_get_server_version(GetHandler(obj)));
609
+ }
610
+
611
+ /* warning_count() */
612
+ static VALUE warning_count(VALUE obj)
613
+ {
614
+ return INT2NUM(mysql_warning_count(GetHandler(obj)));
615
+ }
616
+
617
+ /* commit() */
618
+ static VALUE commit(VALUE obj)
619
+ {
620
+ MYSQL* m = GetHandler(obj);
621
+ if (mysql_commit(m) != 0)
622
+ mysql_raise(m);
623
+ return obj;
624
+ }
625
+
626
+ /* rollback() */
627
+ static VALUE rollback(VALUE obj)
628
+ {
629
+ MYSQL* m = GetHandler(obj);
630
+ if (mysql_rollback(m) != 0)
631
+ mysql_raise(m);
632
+ return obj;
633
+ }
634
+
635
+ /* autocommit() */
636
+ static VALUE autocommit(VALUE obj, VALUE mode)
637
+ {
638
+ MYSQL* m = GetHandler(obj);
639
+ int f;
640
+ f = (mode == Qnil || mode == Qfalse || NUM2INT(mode) == 0) ? 0 : 1;
641
+ if (mysql_autocommit(m, f) != 0)
642
+ mysql_raise(m);
643
+ return obj;
644
+ }
645
+ #endif
646
+
647
+ #ifdef HAVE_MYSQL_SSL_SET
648
+ /* ssl_set(key=nil, cert=nil, ca=nil, capath=nil, cipher=nil) */
649
+ static VALUE ssl_set(int argc, VALUE* argv, VALUE obj)
650
+ {
651
+ VALUE key, cert, ca, capath, cipher;
652
+ char *s_key, *s_cert, *s_ca, *s_capath, *s_cipher;
653
+ MYSQL* m = GetHandler(obj);
654
+ rb_scan_args(argc, argv, "05", &key, &cert, &ca, &capath, &cipher);
655
+ s_key = NILorSTRING(key);
656
+ s_cert = NILorSTRING(cert);
657
+ s_ca = NILorSTRING(ca);
658
+ s_capath = NILorSTRING(capath);
659
+ s_cipher = NILorSTRING(cipher);
660
+ mysql_ssl_set(m, s_key, s_cert, s_ca, s_capath, s_cipher);
661
+ return obj;
662
+ }
663
+ #endif
664
+
665
+ /* query_with_result() */
666
+ static VALUE query_with_result(VALUE obj)
667
+ {
668
+ return GetMysqlStruct(obj)->query_with_result? Qtrue: Qfalse;
669
+ }
670
+
671
+ /* query_with_result=(flag) */
672
+ static VALUE query_with_result_set(VALUE obj, VALUE flag)
673
+ {
674
+ if (TYPE(flag) != T_TRUE && TYPE(flag) != T_FALSE)
675
+ #if RUBY_VERSION_CODE < 160
676
+ TypeError("invalid type, required true or false.");
677
+ #else
678
+ rb_raise(rb_eTypeError, "invalid type, required true or false.");
679
+ #endif
680
+ GetMysqlStruct(obj)->query_with_result = flag;
681
+ return flag;
682
+ }
683
+
684
+ /*-------------------------------
685
+ * Mysql::Result object method
686
+ */
687
+
688
+ /* check if alread freed */
689
+ static void check_free(VALUE obj)
690
+ {
691
+ struct mysql_res* resp = DATA_PTR(obj);
692
+ if (resp->freed == Qtrue)
693
+ rb_raise(eMysql, "Mysql::Result object is already freed");
694
+ }
695
+
696
+ /* data_seek(offset) */
697
+ static VALUE data_seek(VALUE obj, VALUE offset)
698
+ {
699
+ check_free(obj);
700
+ mysql_data_seek(GetMysqlRes(obj), NUM2INT(offset));
701
+ return obj;
702
+ }
703
+
704
+ /* fetch_field() */
705
+ static VALUE fetch_field(VALUE obj)
706
+ {
707
+ check_free(obj);
708
+ return make_field_obj(mysql_fetch_field(GetMysqlRes(obj)));
709
+ }
710
+
711
+ /* fetch_fields() */
712
+ static VALUE fetch_fields(VALUE obj)
713
+ {
714
+ MYSQL_RES* res;
715
+ MYSQL_FIELD* f;
716
+ unsigned int n;
717
+ VALUE ret;
718
+ unsigned int i;
719
+ check_free(obj);
720
+ res = GetMysqlRes(obj);
721
+ f = mysql_fetch_fields(res);
722
+ n = mysql_num_fields(res);
723
+ ret = rb_ary_new2(n);
724
+ for (i=0; i<n; i++)
725
+ rb_ary_store(ret, i, make_field_obj(&f[i]));
726
+ return ret;
727
+ }
728
+
729
+ /* fetch_field_direct(nr) */
730
+ static VALUE fetch_field_direct(VALUE obj, VALUE nr)
731
+ {
732
+ MYSQL_RES* res;
733
+ unsigned int max;
734
+ unsigned int n;
735
+ check_free(obj);
736
+ res = GetMysqlRes(obj);
737
+ max = mysql_num_fields(res);
738
+ n = NUM2INT(nr);
739
+ if (n >= max)
740
+ #if RUBY_VERSION_CODE < 160
741
+ Raise(eMysql, "%d: out of range (max: %d)", n, max-1);
742
+ #else
743
+ rb_raise(eMysql, "%d: out of range (max: %d)", n, max-1);
744
+ #endif
745
+ #if MYSQL_VERSION_ID >= 32226
746
+ return make_field_obj(mysql_fetch_field_direct(res, n));
747
+ #else
748
+ return make_field_obj(&mysql_fetch_field_direct(res, n));
749
+ #endif
750
+ }
751
+
752
+ /* fetch_lengths() */
753
+ static VALUE fetch_lengths(VALUE obj)
754
+ {
755
+ MYSQL_RES* res;
756
+ unsigned int n;
757
+ unsigned long* lengths;
758
+ VALUE ary;
759
+ unsigned int i;
760
+ check_free(obj);
761
+ res = GetMysqlRes(obj);
762
+ n = mysql_num_fields(res);
763
+ lengths = mysql_fetch_lengths(res);
764
+ if (lengths == NULL)
765
+ return Qnil;
766
+ ary = rb_ary_new2(n);
767
+ for (i=0; i<n; i++)
768
+ rb_ary_store(ary, i, INT2NUM(lengths[i]));
769
+ return ary;
770
+ }
771
+
772
+ /* fetch_row() */
773
+ static VALUE fetch_row(VALUE obj)
774
+ {
775
+ MYSQL_RES* res;
776
+ unsigned int n;
777
+ MYSQL_ROW row;
778
+ unsigned long* lengths;
779
+ VALUE ary;
780
+ unsigned int i;
781
+ check_free(obj);
782
+ res = GetMysqlRes(obj);
783
+ n = mysql_num_fields(res);
784
+ row = mysql_fetch_row(res);
785
+ lengths = mysql_fetch_lengths(res);
786
+ if (row == NULL)
787
+ return Qnil;
788
+ ary = rb_ary_new2(n);
789
+ for (i=0; i<n; i++)
790
+ rb_ary_store(ary, i, row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
791
+ return ary;
792
+ }
793
+
794
+ /* fetch_hash2 (internal) */
795
+ static VALUE fetch_hash2(VALUE obj, VALUE with_table)
796
+ {
797
+ MYSQL_RES* res = GetMysqlRes(obj);
798
+ unsigned int n = mysql_num_fields(res);
799
+ MYSQL_ROW row = mysql_fetch_row(res);
800
+ unsigned long* lengths = mysql_fetch_lengths(res);
801
+ MYSQL_FIELD* fields = mysql_fetch_fields(res);
802
+ unsigned int i;
803
+ VALUE hash;
804
+ if (row == NULL)
805
+ return Qnil;
806
+ hash = rb_hash_new();
807
+ for (i=0; i<n; i++) {
808
+ VALUE col;
809
+ if (row[i] == NULL)
810
+ continue;
811
+ if (with_table == Qnil || with_table == Qfalse)
812
+ col = rb_tainted_str_new2(fields[i].name);
813
+ else {
814
+ col = rb_tainted_str_new(fields[i].table, strlen(fields[i].table)+strlen(fields[i].name)+1);
815
+ RSTRING(col)->ptr[strlen(fields[i].table)] = '.';
816
+ strcpy(RSTRING(col)->ptr+strlen(fields[i].table)+1, fields[i].name);
817
+ }
818
+ rb_hash_aset(hash, col, row[i]? rb_tainted_str_new(row[i], lengths[i]): Qnil);
819
+ }
820
+ return hash;
821
+ }
822
+
823
+ /* fetch_hash(with_table=false) */
824
+ static VALUE fetch_hash(int argc, VALUE* argv, VALUE obj)
825
+ {
826
+ VALUE with_table;
827
+ check_free(obj);
828
+ rb_scan_args(argc, argv, "01", &with_table);
829
+ if (with_table == Qnil)
830
+ with_table = Qfalse;
831
+ return fetch_hash2(obj, with_table);
832
+ }
833
+
834
+ /* field_seek(offset) */
835
+ static VALUE field_seek(VALUE obj, VALUE offset)
836
+ {
837
+ check_free(obj);
838
+ return INT2NUM(mysql_field_seek(GetMysqlRes(obj), NUM2INT(offset)));
839
+ }
840
+
841
+ /* field_tell() */
842
+ static VALUE field_tell(VALUE obj)
843
+ {
844
+ check_free(obj);
845
+ return INT2NUM(mysql_field_tell(GetMysqlRes(obj)));
846
+ }
847
+
848
+ /* free() */
849
+ static VALUE res_free(VALUE obj)
850
+ {
851
+ struct mysql_res* resp = DATA_PTR(obj);
852
+ check_free(obj);
853
+ mysql_free_result(resp->res);
854
+ resp->freed = Qtrue;
855
+ store_result_count--;
856
+ return Qnil;
857
+ }
858
+
859
+ /* num_fields() */
860
+ static VALUE num_fields(VALUE obj)
861
+ {
862
+ check_free(obj);
863
+ return INT2NUM(mysql_num_fields(GetMysqlRes(obj)));
864
+ }
865
+
866
+ /* num_rows() */
867
+ static VALUE num_rows(VALUE obj)
868
+ {
869
+ check_free(obj);
870
+ return INT2NUM(mysql_num_rows(GetMysqlRes(obj)));
871
+ }
872
+
873
+ /* row_seek(offset) */
874
+ static VALUE row_seek(VALUE obj, VALUE offset)
875
+ {
876
+ check_free(obj);
877
+ return INT2NUM((int)mysql_row_seek(GetMysqlRes(obj), (MYSQL_ROWS*)NUM2INT(offset)));
878
+ }
879
+
880
+ /* row_tell() */
881
+ static VALUE row_tell(VALUE obj)
882
+ {
883
+ check_free(obj);
884
+ return INT2NUM((int)mysql_row_tell(GetMysqlRes(obj)));
885
+ }
886
+
887
+ /* each {...} */
888
+ static VALUE each(VALUE obj)
889
+ {
890
+ VALUE row;
891
+ check_free(obj);
892
+ while ((row = fetch_row(obj)) != Qnil)
893
+ rb_yield(row);
894
+ return obj;
895
+ }
896
+
897
+ /* each_hash(with_table=false) {...} */
898
+ static VALUE each_hash(int argc, VALUE* argv, VALUE obj)
899
+ {
900
+ VALUE with_table;
901
+ VALUE hash;
902
+ check_free(obj);
903
+ rb_scan_args(argc, argv, "01", &with_table);
904
+ if (with_table == Qnil)
905
+ with_table = Qfalse;
906
+ while ((hash = fetch_hash2(obj, with_table)) != Qnil)
907
+ rb_yield(hash);
908
+ return obj;
909
+ }
910
+
911
+ /*-------------------------------
912
+ * Mysql::Field object method
913
+ */
914
+
915
+ /* hash */
916
+ static VALUE field_hash(VALUE obj)
917
+ {
918
+ VALUE h = rb_hash_new();
919
+ rb_hash_aset(h, rb_str_new2("name"), rb_iv_get(obj, "name"));
920
+ rb_hash_aset(h, rb_str_new2("table"), rb_iv_get(obj, "table"));
921
+ rb_hash_aset(h, rb_str_new2("def"), rb_iv_get(obj, "def"));
922
+ rb_hash_aset(h, rb_str_new2("type"), rb_iv_get(obj, "type"));
923
+ rb_hash_aset(h, rb_str_new2("length"), rb_iv_get(obj, "length"));
924
+ rb_hash_aset(h, rb_str_new2("max_length"), rb_iv_get(obj, "max_length"));
925
+ rb_hash_aset(h, rb_str_new2("flags"), rb_iv_get(obj, "flags"));
926
+ rb_hash_aset(h, rb_str_new2("decimals"), rb_iv_get(obj, "decimals"));
927
+ return h;
928
+ }
929
+
930
+ /* inspect */
931
+ static VALUE field_inspect(VALUE obj)
932
+ {
933
+ VALUE n = rb_iv_get(obj, "name");
934
+ VALUE s = rb_str_new(0, RSTRING(n)->len + 14);
935
+ sprintf(RSTRING(s)->ptr, "#<Mysql::Field:%s>", RSTRING(n)->ptr);
936
+ return s;
937
+ }
938
+
939
+ #define DefineMysqlFieldMemberMethod(m)\
940
+ static VALUE field_##m(VALUE obj)\
941
+ {return rb_iv_get(obj, #m);}
942
+
943
+ DefineMysqlFieldMemberMethod(name)
944
+ DefineMysqlFieldMemberMethod(table)
945
+ DefineMysqlFieldMemberMethod(def)
946
+ DefineMysqlFieldMemberMethod(type)
947
+ DefineMysqlFieldMemberMethod(length)
948
+ DefineMysqlFieldMemberMethod(max_length)
949
+ DefineMysqlFieldMemberMethod(flags)
950
+ DefineMysqlFieldMemberMethod(decimals)
951
+
952
+ #ifdef IS_NUM
953
+ /* is_num? */
954
+ static VALUE field_is_num(VALUE obj)
955
+ {
956
+ return IS_NUM(NUM2INT(rb_iv_get(obj, "type"))) ? Qtrue : Qfalse;
957
+ }
958
+ #endif
959
+
960
+ #ifdef IS_NOT_NULL
961
+ /* is_not_null? */
962
+ static VALUE field_is_not_null(VALUE obj)
963
+ {
964
+ return IS_NOT_NULL(NUM2INT(rb_iv_get(obj, "flags"))) ? Qtrue : Qfalse;
965
+ }
966
+ #endif
967
+
968
+ #ifdef IS_PRI_KEY
969
+ /* is_pri_key? */
970
+ static VALUE field_is_pri_key(VALUE obj)
971
+ {
972
+ return IS_PRI_KEY(NUM2INT(rb_iv_get(obj, "flags"))) ? Qtrue : Qfalse;
973
+ }
974
+ #endif
975
+
976
+
977
+ /*-------------------------------
978
+ * Mysql::Error object method
979
+ */
980
+
981
+ static VALUE error_error(VALUE obj)
982
+ {
983
+ return rb_iv_get(obj, "mesg");
984
+ }
985
+
986
+ static VALUE error_errno(VALUE obj)
987
+ {
988
+ return rb_iv_get(obj, "errno");
989
+ }
990
+
991
+ /*-------------------------------
992
+ * Initialize
993
+ */
994
+
995
+ void Init_mysql(void)
996
+ {
997
+ cMysql = rb_define_class("Mysql", rb_cObject);
998
+ cMysqlRes = rb_define_class_under(cMysql, "Result", rb_cObject);
999
+ cMysqlField = rb_define_class_under(cMysql, "Field", rb_cObject);
1000
+ eMysql = rb_define_class_under(cMysql, "Error", rb_eStandardError);
1001
+
1002
+ rb_define_global_const("MysqlRes", cMysqlRes);
1003
+ rb_define_global_const("MysqlField", cMysqlField);
1004
+ rb_define_global_const("MysqlError", eMysql);
1005
+
1006
+ /* Mysql class method */
1007
+ rb_define_singleton_method(cMysql, "init", init, 0);
1008
+ rb_define_singleton_method(cMysql, "real_connect", real_connect, -1);
1009
+ rb_define_singleton_method(cMysql, "connect", real_connect, -1);
1010
+ rb_define_singleton_method(cMysql, "new", real_connect, -1);
1011
+ rb_define_singleton_method(cMysql, "escape_string", escape_string, 1);
1012
+ rb_define_singleton_method(cMysql, "quote", escape_string, 1);
1013
+ rb_define_singleton_method(cMysql, "client_info", client_info, 0);
1014
+ rb_define_singleton_method(cMysql, "get_client_info", client_info, 0);
1015
+ #if MYSQL_VERSION_ID >= 32332
1016
+ rb_define_singleton_method(cMysql, "debug", my_debug, 1);
1017
+ #endif
1018
+ #if MYSQL_VERSION_ID >= 40000
1019
+ rb_define_singleton_method(cMysql, "get_client_version", client_version, 0);
1020
+ rb_define_singleton_method(cMysql, "client_version", client_version, 0);
1021
+ #endif
1022
+
1023
+ /* Mysql object method */
1024
+ #if MYSQL_VERSION_ID >= 32200
1025
+ rb_define_method(cMysql, "real_connect", real_connect2, -1);
1026
+ rb_define_method(cMysql, "connect", real_connect2, -1);
1027
+ rb_define_method(cMysql, "options", options, -1);
1028
+ #endif
1029
+ rb_define_method(cMysql, "initialize", initialize, -1);
1030
+ #if MYSQL_VERSION_ID >= 32332
1031
+ rb_define_method(cMysql, "escape_string", real_escape_string, 1);
1032
+ rb_define_method(cMysql, "quote", real_escape_string, 1);
1033
+ #else
1034
+ rb_define_method(cMysql, "escape_string", escape_string, 1);
1035
+ rb_define_method(cMysql, "quote", escape_string, 1);
1036
+ #endif
1037
+ rb_define_method(cMysql, "client_info", client_info, 0);
1038
+ rb_define_method(cMysql, "get_client_info", client_info, 0);
1039
+ rb_define_method(cMysql, "affected_rows", affected_rows, 0);
1040
+ #if MYSQL_VERSION_ID >= 32303
1041
+ rb_define_method(cMysql, "change_user", change_user, -1);
1042
+ #endif
1043
+ #if MYSQL_VERSION_ID >= 32321
1044
+ rb_define_method(cMysql, "character_set_name", character_set_name, 0);
1045
+ #endif
1046
+ rb_define_method(cMysql, "close", my_close, 0);
1047
+ #if MYSQL_VERSION_ID < 40000
1048
+ rb_define_method(cMysql, "create_db", create_db, 1);
1049
+ rb_define_method(cMysql, "drop_db", drop_db, 1);
1050
+ #endif
1051
+ #if MYSQL_VERSION_ID >= 32332
1052
+ rb_define_method(cMysql, "dump_debug_info", dump_debug_info, 0);
1053
+ #endif
1054
+ rb_define_method(cMysql, "errno", my_errno, 0);
1055
+ rb_define_method(cMysql, "error", my_error, 0);
1056
+ rb_define_method(cMysql, "field_count", field_count, 0);
1057
+ #if MYSQL_VERSION_ID >= 40000
1058
+ rb_define_method(cMysql, "get_client_version", client_version, 0);
1059
+ rb_define_method(cMysql, "client_version", client_version, 0);
1060
+ #endif
1061
+ rb_define_method(cMysql, "get_host_info", host_info, 0);
1062
+ rb_define_method(cMysql, "host_info", host_info, 0);
1063
+ rb_define_method(cMysql, "get_proto_info", proto_info, 0);
1064
+ rb_define_method(cMysql, "proto_info", proto_info, 0);
1065
+ rb_define_method(cMysql, "get_server_info", server_info, 0);
1066
+ rb_define_method(cMysql, "server_info", server_info, 0);
1067
+ rb_define_method(cMysql, "info", info, 0);
1068
+ rb_define_method(cMysql, "insert_id", insert_id, 0);
1069
+ rb_define_method(cMysql, "kill", my_kill, 1);
1070
+ rb_define_method(cMysql, "list_dbs", list_dbs, -1);
1071
+ rb_define_method(cMysql, "list_fields", list_fields, -1);
1072
+ rb_define_method(cMysql, "list_processes", list_processes, 0);
1073
+ rb_define_method(cMysql, "list_tables", list_tables, -1);
1074
+ #if MYSQL_VERSION_ID >= 32200
1075
+ rb_define_method(cMysql, "ping", ping, 0);
1076
+ #endif
1077
+ rb_define_method(cMysql, "query", query, 1);
1078
+ rb_define_method(cMysql, "refresh", refresh, 1);
1079
+ rb_define_method(cMysql, "reload", reload, 0);
1080
+ rb_define_method(cMysql, "select_db", select_db, 1);
1081
+ rb_define_method(cMysql, "shutdown", my_shutdown, -1);
1082
+ rb_define_method(cMysql, "stat", my_stat, 0);
1083
+ rb_define_method(cMysql, "store_result", store_result, 0);
1084
+ rb_define_method(cMysql, "thread_id", thread_id, 0);
1085
+ rb_define_method(cMysql, "use_result", use_result, 0);
1086
+ #if MYSQL_VERSION_ID >= 40100
1087
+ rb_define_method(cMysql, "get_server_version", server_version, 0);
1088
+ rb_define_method(cMysql, "server_version", server_version, 0);
1089
+ rb_define_method(cMysql, "warning_count", warning_count, 0);
1090
+ rb_define_method(cMysql, "commit", commit, 0);
1091
+ rb_define_method(cMysql, "rollback", rollback, 0);
1092
+ rb_define_method(cMysql, "autocommit", autocommit, 1);
1093
+ #endif
1094
+ #ifdef HAVE_MYSQL_SSL_SET
1095
+ rb_define_method(cMysql, "ssl_set", ssl_set, -1);
1096
+ #endif
1097
+ rb_define_method(cMysql, "query_with_result", query_with_result, 0);
1098
+ rb_define_method(cMysql, "query_with_result=", query_with_result_set, 1);
1099
+
1100
+ /* Mysql constant */
1101
+ #if MYSQL_VERSION_ID >= 32200
1102
+ rb_define_const(cMysql, "OPT_CONNECT_TIMEOUT", INT2NUM(MYSQL_OPT_CONNECT_TIMEOUT));
1103
+ rb_define_const(cMysql, "OPT_COMPRESS", INT2NUM(MYSQL_OPT_COMPRESS));
1104
+ rb_define_const(cMysql, "OPT_NAMED_PIPE", INT2NUM(MYSQL_OPT_NAMED_PIPE));
1105
+ rb_define_const(cMysql, "INIT_COMMAND", INT2NUM(MYSQL_INIT_COMMAND));
1106
+ rb_define_const(cMysql, "READ_DEFAULT_FILE", INT2NUM(MYSQL_READ_DEFAULT_FILE));
1107
+ rb_define_const(cMysql, "READ_DEFAULT_GROUP", INT2NUM(MYSQL_READ_DEFAULT_GROUP));
1108
+ #endif
1109
+ #if MYSQL_VERSION_ID >= 32349
1110
+ rb_define_const(cMysql, "OPT_LOCAL_INFILE", INT2NUM(MYSQL_OPT_LOCAL_INFILE));
1111
+ #endif
1112
+ rb_define_const(cMysql, "REFRESH_GRANT", INT2NUM(REFRESH_GRANT));
1113
+ rb_define_const(cMysql, "REFRESH_LOG", INT2NUM(REFRESH_LOG));
1114
+ rb_define_const(cMysql, "REFRESH_TABLES", INT2NUM(REFRESH_TABLES));
1115
+ #ifdef REFRESH_HOSTS
1116
+ rb_define_const(cMysql, "REFRESH_HOSTS", INT2NUM(REFRESH_HOSTS));
1117
+ #endif
1118
+ #ifdef REFRESH_STATUS
1119
+ rb_define_const(cMysql, "REFRESH_STATUS", INT2NUM(REFRESH_STATUS));
1120
+ #endif
1121
+ #ifdef REFRESH_THREADS
1122
+ rb_define_const(cMysql, "REFRESH_THREADS", INT2NUM(REFRESH_THREADS));
1123
+ #endif
1124
+ #ifdef REFRESH_SLAVE
1125
+ rb_define_const(cMysql, "REFRESH_SLAVE", INT2NUM(REFRESH_SLAVE));
1126
+ #endif
1127
+ #ifdef REFRESH_MASTER
1128
+ rb_define_const(cMysql, "REFRESH_MASTER", INT2NUM(REFRESH_MASTER));
1129
+ #endif
1130
+ #ifdef CLIENT_LONG_PASSWORD
1131
+ #endif
1132
+ #ifdef CLIENT_FOUND_ROWS
1133
+ rb_define_const(cMysql, "CLIENT_FOUND_ROWS", INT2NUM(CLIENT_FOUND_ROWS));
1134
+ #endif
1135
+ #ifdef CLIENT_LONG_FLAG
1136
+ #endif
1137
+ #ifdef CLIENT_CONNECT_WITH_DB
1138
+ #endif
1139
+ #ifdef CLIENT_NO_SCHEMA
1140
+ rb_define_const(cMysql, "CLIENT_NO_SCHEMA", INT2NUM(CLIENT_NO_SCHEMA));
1141
+ #endif
1142
+ #ifdef CLIENT_COMPRESS
1143
+ rb_define_const(cMysql, "CLIENT_COMPRESS", INT2NUM(CLIENT_COMPRESS));
1144
+ #endif
1145
+ #ifdef CLIENT_ODBC
1146
+ rb_define_const(cMysql, "CLIENT_ODBC", INT2NUM(CLIENT_ODBC));
1147
+ #endif
1148
+ #ifdef CLIENT_LOCAL_FILES
1149
+ rb_define_const(cMysql, "CLIENT_LOCAL_FILES", INT2NUM(CLIENT_LOCAL_FILES));
1150
+ #endif
1151
+ #ifdef CLIENT_IGNORE_SPACE
1152
+ rb_define_const(cMysql, "CLIENT_IGNORE_SPACE", INT2NUM(CLIENT_IGNORE_SPACE));
1153
+ #endif
1154
+ #ifdef CLIENT_CHANGE_USER
1155
+ rb_define_const(cMysql, "CLIENT_CHANGE_USER", INT2NUM(CLIENT_CHANGE_USER));
1156
+ #endif
1157
+ #ifdef CLIENT_INTERACTIVE
1158
+ rb_define_const(cMysql, "CLIENT_INTERACTIVE", INT2NUM(CLIENT_INTERACTIVE));
1159
+ #endif
1160
+ #ifdef CLIENT_SSL
1161
+ rb_define_const(cMysql, "CLIENT_SSL", INT2NUM(CLIENT_SSL));
1162
+ #endif
1163
+ #ifdef CLIENT_IGNORE_SIGPIPE
1164
+ rb_define_const(cMysql, "CLIENT_IGNORE_SIGPIPE", INT2NUM(CLIENT_IGNORE_SIGPIPE));
1165
+ #endif
1166
+ #ifdef CLIENT_TRANSACTIONS
1167
+ rb_define_const(cMysql, "CLIENT_TRANSACTIONS", INT2NUM(CLIENT_TRANSACTIONS));
1168
+ #endif
1169
+
1170
+ /* Mysql::Result object method */
1171
+ rb_define_method(cMysqlRes, "data_seek", data_seek, 1);
1172
+ rb_define_method(cMysqlRes, "fetch_field", fetch_field, 0);
1173
+ rb_define_method(cMysqlRes, "fetch_fields", fetch_fields, 0);
1174
+ rb_define_method(cMysqlRes, "fetch_field_direct", fetch_field_direct, 1);
1175
+ rb_define_method(cMysqlRes, "fetch_lengths", fetch_lengths, 0);
1176
+ rb_define_method(cMysqlRes, "fetch_row", fetch_row, 0);
1177
+ rb_define_method(cMysqlRes, "fetch_hash", fetch_hash, -1);
1178
+ rb_define_method(cMysqlRes, "field_seek", field_seek, 1);
1179
+ rb_define_method(cMysqlRes, "field_tell", field_tell, 0);
1180
+ rb_define_method(cMysqlRes, "free", res_free, 0);
1181
+ rb_define_method(cMysqlRes, "num_fields", num_fields, 0);
1182
+ rb_define_method(cMysqlRes, "num_rows", num_rows, 0);
1183
+ rb_define_method(cMysqlRes, "row_seek", row_seek, 1);
1184
+ rb_define_method(cMysqlRes, "row_tell", row_tell, 0);
1185
+ rb_define_method(cMysqlRes, "each", each, 0);
1186
+ rb_define_method(cMysqlRes, "each_hash", each_hash, -1);
1187
+
1188
+ /* MysqlField object method */
1189
+ rb_define_method(cMysqlField, "name", field_name, 0);
1190
+ rb_define_method(cMysqlField, "table", field_table, 0);
1191
+ rb_define_method(cMysqlField, "def", field_def, 0);
1192
+ rb_define_method(cMysqlField, "type", field_type, 0);
1193
+ rb_define_method(cMysqlField, "length", field_length, 0);
1194
+ rb_define_method(cMysqlField, "max_length", field_max_length, 0);
1195
+ rb_define_method(cMysqlField, "flags", field_flags, 0);
1196
+ rb_define_method(cMysqlField, "decimals", field_decimals, 0);
1197
+ rb_define_method(cMysqlField, "hash", field_hash, 0);
1198
+ rb_define_method(cMysqlField, "inspect", field_inspect, 0);
1199
+ #ifdef IS_NUM
1200
+ rb_define_method(cMysqlField, "is_num?", field_is_num, 0);
1201
+ #endif
1202
+ #ifdef IS_NOT_NULL
1203
+ rb_define_method(cMysqlField, "is_not_null?", field_is_not_null, 0);
1204
+ #endif
1205
+ #ifdef IS_PRI_KEY
1206
+ rb_define_method(cMysqlField, "is_pri_key?", field_is_pri_key, 0);
1207
+ #endif
1208
+
1209
+ /* Mysql::Field constant: TYPE */
1210
+ rb_define_const(cMysqlField, "TYPE_TINY", INT2NUM(FIELD_TYPE_TINY));
1211
+ #if MYSQL_VERSION_ID >= 32115
1212
+ rb_define_const(cMysqlField, "TYPE_ENUM", INT2NUM(FIELD_TYPE_ENUM));
1213
+ #endif
1214
+ rb_define_const(cMysqlField, "TYPE_DECIMAL", INT2NUM(FIELD_TYPE_DECIMAL));
1215
+ rb_define_const(cMysqlField, "TYPE_SHORT", INT2NUM(FIELD_TYPE_SHORT));
1216
+ rb_define_const(cMysqlField, "TYPE_LONG", INT2NUM(FIELD_TYPE_LONG));
1217
+ rb_define_const(cMysqlField, "TYPE_FLOAT", INT2NUM(FIELD_TYPE_FLOAT));
1218
+ rb_define_const(cMysqlField, "TYPE_DOUBLE", INT2NUM(FIELD_TYPE_DOUBLE));
1219
+ rb_define_const(cMysqlField, "TYPE_NULL", INT2NUM(FIELD_TYPE_NULL));
1220
+ rb_define_const(cMysqlField, "TYPE_TIMESTAMP", INT2NUM(FIELD_TYPE_TIMESTAMP));
1221
+ rb_define_const(cMysqlField, "TYPE_LONGLONG", INT2NUM(FIELD_TYPE_LONGLONG));
1222
+ rb_define_const(cMysqlField, "TYPE_INT24", INT2NUM(FIELD_TYPE_INT24));
1223
+ rb_define_const(cMysqlField, "TYPE_DATE", INT2NUM(FIELD_TYPE_DATE));
1224
+ rb_define_const(cMysqlField, "TYPE_TIME", INT2NUM(FIELD_TYPE_TIME));
1225
+ rb_define_const(cMysqlField, "TYPE_DATETIME", INT2NUM(FIELD_TYPE_DATETIME));
1226
+ #if MYSQL_VERSION_ID >= 32130
1227
+ rb_define_const(cMysqlField, "TYPE_YEAR", INT2NUM(FIELD_TYPE_YEAR));
1228
+ #endif
1229
+ rb_define_const(cMysqlField, "TYPE_SET", INT2NUM(FIELD_TYPE_SET));
1230
+ rb_define_const(cMysqlField, "TYPE_BLOB", INT2NUM(FIELD_TYPE_BLOB));
1231
+ rb_define_const(cMysqlField, "TYPE_STRING", INT2NUM(FIELD_TYPE_STRING));
1232
+ #if MYSQL_VERSION_ID >= 40000
1233
+ rb_define_const(cMysqlField, "TYPE_VAR_STRING", INT2NUM(FIELD_TYPE_VAR_STRING));
1234
+ #endif
1235
+ rb_define_const(cMysqlField, "TYPE_CHAR", INT2NUM(FIELD_TYPE_CHAR));
1236
+
1237
+ /* Mysql::Field constant: FLAG */
1238
+ rb_define_const(cMysqlField, "NOT_NULL_FLAG", INT2NUM(NOT_NULL_FLAG));
1239
+ rb_define_const(cMysqlField, "PRI_KEY_FLAG", INT2NUM(PRI_KEY_FLAG));
1240
+ rb_define_const(cMysqlField, "UNIQUE_KEY_FLAG", INT2NUM(UNIQUE_KEY_FLAG));
1241
+ rb_define_const(cMysqlField, "MULTIPLE_KEY_FLAG", INT2NUM(MULTIPLE_KEY_FLAG));
1242
+ rb_define_const(cMysqlField, "BLOB_FLAG", INT2NUM(BLOB_FLAG));
1243
+ rb_define_const(cMysqlField, "UNSIGNED_FLAG", INT2NUM(UNSIGNED_FLAG));
1244
+ rb_define_const(cMysqlField, "ZEROFILL_FLAG", INT2NUM(ZEROFILL_FLAG));
1245
+ rb_define_const(cMysqlField, "BINARY_FLAG", INT2NUM(BINARY_FLAG));
1246
+ #ifdef ENUM_FLAG
1247
+ rb_define_const(cMysqlField, "ENUM_FLAG", INT2NUM(ENUM_FLAG));
1248
+ #endif
1249
+ #ifdef AUTO_INCREMENT_FLAG
1250
+ rb_define_const(cMysqlField, "AUTO_INCREMENT_FLAG", INT2NUM(AUTO_INCREMENT_FLAG));
1251
+ #endif
1252
+ #ifdef TIMESTAMP_FLAG
1253
+ rb_define_const(cMysqlField, "TIMESTAMP_FLAG", INT2NUM(TIMESTAMP_FLAG));
1254
+ #endif
1255
+ #ifdef SET_FLAG
1256
+ rb_define_const(cMysqlField, "SET_FLAG", INT2NUM(SET_FLAG));
1257
+ #endif
1258
+ #ifdef NUM_FLAG
1259
+ rb_define_const(cMysqlField, "NUM_FLAG", INT2NUM(NUM_FLAG));
1260
+ #endif
1261
+
1262
+ /* Mysql::Error object method */
1263
+ rb_define_method(eMysql, "error", error_error, 0);
1264
+ rb_define_method(eMysql, "errno", error_errno, 0);
1265
+
1266
+ /* Mysql::Error constant */
1267
+ }