MySQL/Ruby
This is the MySQL API module for Ruby. It provides the same functions for Ruby programs that the MySQL C API provides for C programs.
Requirement
- MySQL 3.23.58 +
- Ruby 1.6.8 +
The module may work for other versions, but that has not been verified.
License
This program is under Ruby's license.
Install
1st:
% ruby extconf.rb
or
% ruby extconf.rb --with-mysql-dir=/usr/local/mysql
or
% ruby extconf.rb --with-mysql-config
then
% make
extconf.rb has following options:
- --with-mysql-include=dir
- MySQL header file directory. Default is /usr/local/include.
- --with-mysql-lib=dir
- MySQL library directory. Default is /usr/local/lib.
- --with-mysql-dir=dir
- Same as --with-mysql-include=dir/include, --with-mysql-lib=dir/lib.
- --with-mysql-config[=/path/to/mysql_config]
- Get compile-parameter from mysql_config command.
2nd:
% ruby -I. ./test.rb <i>hostname</i> <i>user</i> <i>passwd</i>
test.rb should be invoked with three arguments that indicate the MySQL server hostname, and the username and password for a MySQL account that can create a database named "rubytest". An optional fourth argument is allowed, to specify a database name to use rather than "rubytest". The database should not already exist.
3rd:
# make install
Note
If you get error like 'libmysqlclient not found' when testing, you need to specify the directory in which the library is located so that make can find it.
% env LD_RUN_PATH=<i>libmysqlclient.so directory</i> make
Usage
The names of methods provided by this module basically are the same as the names of the functions in the C API, except that the Ruby method names do not begin with a 'mysql_' prefix. For example, the Ruby query() method corresponds to the C API mysql_query() function. For details on the use of each Ruby method, see the descriptions of the corresponding C functions in the MySQL Reference Manual.
Some Ruby methods may be invoked under other names that serve as equivalent aliases, as noted below.
If an error occurs when a method executes, it raises a Mysql::Error exception.
Mysql class
CLASS METHODS
- init()
-
It return Mysql object. It not connect to mysqld.
- real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
- connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
- new(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
- connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
-
connect to mysqld and return Mysql object.
- escape_string(str)
- quote(str)
-
quote string for insert/update.
- get_client_info()
- client_info()
-
return client version information.
- get_client_version()
- client_version()
-
return client version as number.
- debug(str)
-
same as C API mysql_debug().
OBJECT METHODS
- options(opt, val=nil)
-
same as C API mysql_options().
- real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
- connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
-
same as Mysql.real_connect().
- affected_rows()
-
return affected rows.
- autocommit(mode)
-
set autocommit mode.
- change_user(user=nil, passwd=nil, db=nil)
-
change user.
- character_set_name()
-
return character set.
- close()
-
close connection.
- commit()
-
commit transaction.
- create_db(db)
-
create database.
- drop_db(db)
-
drop database.
- dump_debug_info()
-
same as C API mysql_dump_debug_info().
- errno()
-
return error number.
- error()
-
return error message.
- escape_string(str)
- quote(str)
-
quote strings for insert/update. same as C API mysql_real_escape_string().
- field_count()
-
return number of columns of last query.
- get_client_info()
- client_info()
-
return client version information.
- get_client_version()
- client_version()
-
return client version number.
- get_host_info()
- host_info()
-
return connection information.
- get_proto_info()
- proto_info()
-
return connection protocol version.
- get_server_info()
- server_info()
-
return server version information.
- get_server_version()
- server_version()
-
return server version number.
- info()
-
return information of last query.
- insert_id()
-
return last AUTO_INCREMENT value.
- kill(id)
-
kill thread.
- list_dbs(db=nil)
-
return database list.
- list_fields(table, field=nil)
-
return Mysql::Result object.
- list_processes()
-
return Mysql::Result object.
- list_tables(table=nil)
-
return table list Array.
- ping()
-
check server.
- query(q)
-
do query and store_result(). return Mysql::Result object. If query_with_result is false, it does not store_result().
- refresh(r)
-
flush server log or cache.
- reload()
-
reload access privilege table.
- rollback()
-
rollback transaction.
- select_db(db)
-
select database.
- shutdown()
-
shutdown server.
- ssl_set(key=nil, cert=nil, ca=nil, capath=nil, cipher=nil)
-
use SSL.
- stat()
-
return server status.
- store_result()
-
return Mysql::Result object.
- thread_id()
-
retrun thread id.
- use_result()
-
return Mysql::Result object.
- warning_count()
-
return warning count last query.
OBJECT VARIABLES
- query_with_result
- If it is true then do store_result() on query() If true, query() also invokes store_result() and returns a Mysql::Result object. Default is true.
Mysql::Result class
OBJECT METHODS
- free()
-
free memory of result table.
- data_seek(offset)
-
seek row.
- fetch_field()
-
return next Mysql::Field object.
- fetch_fields()
-
return Array of Mysql::Field object.
- fetch_field_direct(fieldnr)
-
return Mysql::Field object.
- fetch_lengths()
-
return Array of field length.
- fetch_row()
-
return row as Array.
- fetch_hash(with_table=false)
-
return row as Hash. If with_table is true, hash key format is "tablename.fieldname".
- field_seek(offset)
-
seek field.
- field_tell()
-
return field position.
- num_fields()
-
return number of fields.
- num_rows()
-
return number of rows.
- row_seek(offset)
-
seek row.
- row_tell()
-
return row position.
ITERATOR
- each() {|x| ...}
-
'x' is array of column values.
- each_hash(with_table=false) {|x| ...}
-
'x' is hash of column values, and the keys are the column names.
Mysql::Field class
OBJECT VARIABLES(read only)
- name
- field name
- table
- table name
- def
- default value
- type
- field type
- length
- field length
- max_length
- max field length
- flags
- field flag
- decimals
- number of decimals
OBJECT METHODS
- hash()
-
return field as Hash.
ex.) obj.name == obj.hash['name']
- is_not_null?()
-
True if this field is defined as NOT NULL.
- is_num?()
-
True if this field type is numeric.
- is_pri_key?()
-
True if this field is a primary key.
- inspect()
-
return "#<Mysql::Field:fieldname>"
Mysql::Error class
OBJECT VARIABLES(read only)
- error
- eror message
- errno
- error number
Histroy
- 2004-09-20
-
version 2.5.1
- add Mysql#set_ssl().
- 2004-08-31
-
version 2.5
- correspond to MySQL 4.1.x.
- change MysqlRes, MysqlField, MysqlError to Mysql::Result, Mysql::Field, Mysql::Error.
- add Mysql.client_version(), Mysql.get_client_version(), Mysql#client_version(), Mysql#get_client_version(), Mysql#server_version(), Mysql#get_server_version(), Mysql#warning_count(), Mysql#commit(), Mysql#rollback(), Mysql#autocommit().
- add Mysql::Field#is_not_null?(), Mysql::Field#is_pri_key?(), Mysql::Field#is_num?().
- add MysqlField::TYPE_VAR_STRING.
- 2003-08-10
-
version 2.4.5
- extconf.rb: correspond to MySQL 4.1.
- mysql.c.in: correspond to Ruby 1.8.
- 2003-02-23
-
version 2.4.4a
- make extconf.rb to correspond to Ruby 1.8.0
- 2003-01-29
-
version 2.4.4
- add Mysql::OPT_LOCAL_INFILE.
- add --with-mysql-config option to extconf.rb.
- extconf.rb automatically detect typical library.
- 2003-01-05
-
version 2.4.3c
- modified English README. Thanks to Paul DuBois.
- 2002-12-24
-
version 2.4.3b
- make extconf.rb to correspond to Ruby 1.6.8.
- 2002-11-07
-
version 2.4.3a
- fix bug duplicating constant.
- 2002-09-10
-
version 2.4.3
- for error number with prefix ER_ .
- get error constant from errmsg.h and mysqld_error.h automatically.
- 2002-01-07
-
version 2.4.2
- for MySQL 4.0.
- change `uint' to `unsigned int' (for mswin).
- 2001-12-02
-
version 2.4.1
- remove `extern' (for Cygiwn).
- change option of extconf.rb.
- 2001-10-12
-
version 2.4.0
- for Ruby 1.7.
- add Mysql::debug(), Mysql#change_user(), Mysql#character_set_name(), Mysql#dump_debug_info().
Author
e-mail: TOMITA Masahiro [email protected] http://tmtm.org
TOMITA Masahiro Last modified: Mon Sep 20 21:20:17 JST 2004