MySQL/Ruby

MySQL/Ruby

[English]


?????? MySQL ?? Ruby API ??????MySQL ?? C API ????????????????????????????

????????????

tmtm.org

??????????

  • MySQL 5.0.67
  • Ruby 1.8.7, 1.9.1

?????????????? make ????????????????????????????????????????

??????????

???????????????? Ruby ?????????? ????????????

????????????

??????????????????????


% ruby extconf.rb

??????


% ruby extconf.rb --with-mysql-dir=/usr/local/mysql

??????


% ruby extconf.rb --with-mysql-config

????????


% make

extconf.rb ??????????????????????????????????

--with-mysql-include=dir
MySQL ???????????????????????????? /usr/local/include ????????????????????????????????????
--with-mysql-lib=dir
MySQL ???????????????????????? /usr/local/lib ????????????????????????????????????
--with-mysql-dir=dir
--with-mysql-include=dir/include, --with-mysql-lib=dir/lib ????????????
--with-mysql-config[=/path/to/mysql_config]
mysql_config ????????????????????????????????????????????????

????????????????????????????


% ruby ./test.rb -- [<i>hostname</i> [<i>user</i> [<i>passwd</i> [<i>dbname</i> [<i>port</i> [<i>socket</i> [<i>flag</i>]]]]]]]

test.rb ?????????????? Mysql.real_connect() ??????????????????

????????????????????????????????????????????????????????


# make install

????

???????????????????? libmysqlclient ????????????????????????????????????????make ???????????????????????????????????????????????????????????? make ??????????????????


% env LD_RUN_PATH=<i>libmysqlclient.so??????</i> make

test.rb ?? Linux ???????????????????????????????? ????????????????????????????????????????????????????

??????

???????????? C API ?????????? mysql_ ?????????????????????????????????????????????????????????????????? C API ?????????????????????????? MySQL ????????????????????????????

?????????????????????????????????? Mysql::Error ??????????????????

???????????????????????????????? self ????????????

Mysql ??????

MySQL ????????????????????????????

??????????????

init()

Mysql ??????????????????????????????mysqld ?????????????????? 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)
new(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)

mysqld ??????????Mysql ?????????????????????????????? flag ???????? C API ??????????????????

??) Mysql::CLIENT_FOUND_ROWS

escape_string(str)
quote(str)

insert, update ????????????????????????????

get_client_info()
client_info()

??????????????????????????????????????????????

get_client_version()
client_version()

????????????????????????????????????????????

debug(str)

C API mysql_debug() ????????

????????????????????

options(opt, val=nil)

C API ?? mysql_options() ???????????? opt ???????????????? C API ???? MYSQL_ ????????????????????????????

??) Mysql::OPT_CONNECT_TIMEOUT

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)

Mysql.real_connect() ????????????Mysql.init() ??????????????????????????????????????????????????????????

affected_rows()

??????????????????????????

autocommit(mode)

autocommit ???????? mode ??????????????mode ?? nil, false, 0 ??????????????????????????????????????

change_user(user=nil, passwd=nil, db=nil)

????????????????????????

character_set_name()

????????????????????????????

close()

??????????????????

commit()

??????????????????????????????????

create_db(db)

??????????????????????????

drop_db(db)

??????????????????????????

dump_debug_info()

C API mysql_dump_debug_info() ????????

errno()

??????????????????????

error()

????????????????????????????

escape_string(str)
quote(str)

insert, update ???????????????????????????? C API ?? mysql_real_escape_string() ????????

field_count()

??????????????????????????????????????????

get_client_info()
client_info()

??????????????????????????????????????????????

get_client_version()
client_version()

????????????????????????????????????????????

get_host_info()
host_info()

????????????????????????????

get_proto_info()
proto_info()

??????????????????????????????????????????

get_server_info()
server_info()

??????????????????????????????????????????

get_server_version()
server_version()

????????????????????????????????????????

info()

???????????????????????????????????????????????????????? nil ????????????

insert_id()

???????????????? AUTO_INCREMENT ????????????????????

kill(id)

id ??????????????????????????????

list_dbs(db=nil)

????????????????????????????????????

list_fields(table, field=nil)

???????????????????????????????? Mysql::Result ??????????????????????????????

list_processes()

???????????????????????????????????? Mysql::Result ??????????????????????????????

list_tables(table=nil)

????????????????????????????????

more_results?()

??????????????????????????????????????????????????

next_result()

?????????????????????????????????????????????????? ???????? store_result() ??????????????????????????????????????

ping()

????????????????????????????????????????????

prepare(q)

?????????????????????????????????????????????????????????? Mysql::Stmt ??????????????????????????????

Mysql#stmt_init.prepare(q) ????????????

query(q)
real_query(q)

???????????????????? ???????????????????????????????? store_result() ????????????Mysql::Result ?????????????????????????????? query_with_result ?? false ????????????????????store_result() ????????????????

query(q) {|res| ...}
real_query(q) {|res| ...}

???????????????????? ????????????????????????Mysql::Result ?????????????????????????????????????????????? ???????????????? Mysql::Result ???????????????????????????? ??????????????????????????????????????;??????????????????????????????????????????????????????????????????????????????????

MySQL/Ruby 2.8 ?????????????????????????????????????????????????????????????????? Mysql.connect ?? flag ?? Mysql::CLIENT_MULTI_STATEMENTS ??????????????Mysql#set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON) ????????????????????

refresh(r)

??????????????????????????????????????????????

reload()

??????????????????????????????????????

rollback()

??????????????????????????????????????

select_db(db)

??????????????????????????

set_server_option(opt)

?????????????????????????????????????????????? ??????????Mysql::OPTION_MULTI_STATEMENTS_ON, Mysql::OPTION_MULTI_STATEMENTS_OFF ????????????????

shutdown()

????????????????????

ssl_set(key=nil, cert=nil, ca=nil, capath=nil, cipher=nil)

SSL??????????????????Mysql.init() ????Mysql#connect() ??????????????????????????

stat()

????????????????????????????????

stmt_init()

Mysql::Stmt ??????????????????????????????

store_result()

?????????????? Mysql::Result ??????????????????????????????

thread_id()

????????????????????ID????????????

use_result()

?????????????? Mysql::Result ??????????????????????????????

warning_count()

????????????????????????????????

????????????????

query_with_result

true ???????????? query() ???? store_result() ????????????Mysql::Result ?????????????????????????????? false ???????????????????????????????????????????????? true ??????

reconnect

true ???????????? MySQL ?????????????????????????????????????????????????? ???????????? false ??????

Mysql::Result ??????

????????????????????????

????????????????????

free()

??????????????????????????????????

data_seek(offset)

???????????????? offset ??????????????????

fetch_field()

???????????? Mysql::Field ?????????????????????????????? ????????????????????????????????????

fetch_fields()

?????????????? Mysql::Field ????????????????????????????????????

fetch_field_direct(fieldnr)

fieldnr ???????????? Mysql::Field ??????????????????????????????

fetch_lengths()

??????????????????????????????????????????

fetch_row()

???????????????????????????????????????????????????????????? ??????????????????????????

fetch_hash(with_table=false)

???????????????????????????????????????????????????????????? ???????????????????????????????????????????????? with_table ?? true ????????????????????????????????????"??????????.??????" ????????????????????????????

field_seek(offset)

???????????????? offset ????????????????????

field_tell()

????????????????????????????

num_fields()

??????????????????

num_rows()

????????????????????

row_seek(offset)

???????????????????????????? offset ???????????? row_tell() ???????? Mysql::RowOffset ????????????????????????

row_tell()

???????????????? Mysql::RowOffset ??????????????????????????????

??????????

each() {|x| ??}

???????????????????? ?? ????????????????x ????????????????????

each_hash(with_table=false) {|x| ??}

???????????????????? ?? ???????????????? x ?????????????????????????????????????????? with_table ?? true ????????????????????????????????????"??????????.??????" ????????????????????????????

Mysql::Field ??????

????????????????????????????C API ???????????????????????? Mysql::Result ??????????????????????????Mysql::Result ?????????????????????????????????????????????????????????????? C API ??????????????????????????

????????????????(????????????)

name
??????
table
??????????
def
????????????
type
????????
length
??????????
max_length
??????????????????????????
flags
??????
decimals
??????????

type ???????????????? C API ?????????? FIELD_ ????????????????????????

??) Mysql::Field::TYPE_STRING

flag ???????????????? C API ??????????????????

??) Mysql::Field::BLOB_FLAG

????????????????????

hash()

????????????????????????????????????????????

??) obj.name == obj.hash['name']

is_not_null?()

???????????? "NOT NULL" ????????????????????????????????

is_num?()

??????????????????????????????????????

is_pri_key?()

????????????????????????????????????????????????

inspect()

?????? "#<Mysql::Field:??????>" ????????????

Mysql::Stmt ??????

MySQL ??????????????????????????????????????????????????

??????:


my = Mysql.new(hostname, username, password, databasename)
st = my.prepare("insert into tblname (col1,col2,col3) values (?,?,?)")
st.execute("abc",123,Time.now)
st.prepare("select col1,col2,col3 from tblname")
st.execute
st.fetch  # => ["abc", 123, #<Mysql::Time:2005-07-24 23:52:55>]
st.close

????????????????????

affected_rows()

??????????????????????????

bind_result(class, ...)

?????????????????????????????????????????????????????????????? ????????????????????Numeric, Integer, Fixnum, Float, Mysql::Time ?????? nil ?????????????????????????????? bind_result ??????????????????????????????????

close()

Mysql::Stmt ??????????????????????????

data_seek(offset)

???? fetch() ?????????????????? offset ?????????????????? offset ?? 0 ????????????????

execute(arg, ...)

prepare() ??????????????????????????????????????????

fetch()

execute() ???????????????????????????????????????????????? ???????????????????????????????????????????????????? nil ????????????

?????????????? MySQL ????????????????????????????????

MySQL????Ruby????????
TINYINT, SMALLINT, MEDIUMINT, YEARFixnum
INT, BIGINTFixnum??????Bignum
FLOAT, DOUBLEFloat
DECIMALString
DATE, DATETIME, TIMESTAMP, TIMEMysql::Time
CHAR, VARCHAR, BINARY, VARBINARY, TINYBLOB, TINYTEXT, TINYBLOB, TINYTEXT, MEDIUMBLOB, MEDIUMTEXT, LONGBLOB, LONGTEXT, ENUM, SET, BITString
NULLNilClass
field_count()

prepare ??????????????????????????????????????????????

free_result()

??????????????????????

insert_id()

?????????? AUTO_INCREMENT ????????????????????

num_rows()

??????????????????????????

param_count()

prepare() ???????????????????????????????????????????

prepare(q)

?????????????????????????????????????????????????????????? execute() ????????????????

result_metadata()

prepare() ?????????????????????????????????????? Mysql::Result ??????????????????????????????

row_seek(offset)

???? fetch() ?????????????????? offset ???????????????? data_seek ???????? offset ?? row_tell() ?????????? Mysql::RowOffset ????????????????????????

row_tell()

???????????????? Mysql::RowOffset ??????????????????????????????

sqlstate()

?????????????? SQLSTATE ??????????????????

??????????

each() {|x| ??}

???????????????????? ?? ???????????????? x ?? fetch() ??????????????????????????????

Mysql::Time ??????

Mysql::Stmt ??????????????????????????????????????

??????????????

new(year=0,month=0,day=0,hour=0,minute=0,second=0,neg=false,second_part=0)

Mysql::Time????????????????????

????????????????

year
??
month
??
day
??
hour
??
minute
??
second
??
neg
???????????????? false, ?????????? true
second_part
??????????????????????

Mysql::Error ??????

MySQL ???????????????????????????? MySQL ????????????????????????????????????????????????

????????????????(????????????)

error
????????????????
errno
??????????

errno ???????????????? C API ??????????????????

??) Mysql::Error::CR_UNKNOWN_HOST

????

2009-02-01
version 2.8.1
  • Ruby 1.9.1 ????????
2008-09-29
version 2.8
version 2.7.7
  • MySQL???????????? SIGVTALRM ?? EINTR ?????????????????????????? ([ruby-dev:31842])
  • MySQL/Ruby 2.7.* ?? Ruby 1.8.5 ??????????????????????????
2008-06-20
version 2.8pre4
  • ????????????????????????????????????[ruby-dev:35152]
2008-06-17
version 2.8pre3
version 2.7.6
  • 64bit ?????? Mysql::Stmt#execute ????????????(2**30 ????)??????????????????????????
2008-03-08
version 2.8pre2
version 2.7.5
  • 64bit ?????? Mysql::Stmt#fetch ??????????????????????????
2007-12-26
version 2.8pre1
  • Ruby 1.9.0 ????
  • Incompat: Mysql::Result#each_hash ????????????????????????????????????????????????????????????????
  • Incompat: ???????????? Mysql#query ??????????????????????????????????????????????????????????
2007-08-22
version 2.7.4
  • BUG: Mysql::Stmt#execute ????????????????????????
2006-12-20
version 2.7.3
  • BUG: ???????????? Mysql#query ????????????????????????????????????????????????
2006-10-28
version 2.7.2
  • BUG: ?????????????????????? Mysql::Stmt#result_metadata ?? nil ?????????????????? (Thanks to Hidetoshi)
  • BUG: Mysql#close ?? mysql_errno ??????????????????
  • BUG: ???????????? Mysql#query ??????????????????????????????????????????????
  • extconf.rb ?? Visual C++ ???? (Thanks to Shugo Maeda)
  • BIT????????
  • Mysql::Field ?? TYPE_BIT, TYPE_NEWDECIMAL ????
2006-06-04
version 2.7.1
  • free() ?? xfree() ????????Windows ????????????????????????????(Tobias Grimm ??????)
2005-08-22
version 2.7
  • Mysql#options????????????Mysql::OPT_GUESS_CONNECTION, Mysql::OPT_USE_EMBEDDED_CONNECTION, Mysql::OPT_USE_REMOTE_CONNECTION, Mysql::SET_CLIENT_IP
  • test.rb: 4.0.x, 5.0.x ????
2005-08-16
version 2.7-beta3
  • Mysql::Stmt#bind_result ????
2005-08-02
version 2.7-beta2
  • BUG: mysql.c.in: fetch_hash: ???? nil ??????????????????????????????(Stefan Kaes ??????)
  • ???? Mysql::VERSION ????
  • Mysql#prepare ????
2005-07-24
version 2.7-beta
  • Mysql#stmt_init ????
  • Mysql::Stmt, Mysql::Time, Mysql::RowOffset ??????????
  • Mysql::Error#sqlstate ????
  • Mysql::Result#row_seek,row_tell ?????? offset ???? Mysql::RowOffset ??????????????????
2005-07-31
version 2.6.3
  • ???? Mysql::VERSION ????
2005-07-26
version 2.6.2
  • BUG: mysql.c.in: fetch_hash: ???? nil ??????????????????????????????(Stefan Kaes ??????)
2005-06-28
version 2.6.1
  • mysql.c.in: MacOSX ????????????????????????????
2005-04-25
version 2.6
  • Mysql#option() ???????????? Mysql::OPT_PROTOCOL, Mysql::OPT_READ_TIMEOUT, Mysql::OPT_WRITE_TIMEOUT, Mysql::SET_CHARSET_DIR, Mysql::SET_CHARSET_NAME, Mysql::SHARED_MEMORY_BASE_NAME, Mysql::SECURE_AUTH
  • Mysql#more_results?(), Mysql#next_result(), Mysql#set_server_option(), Mysql#sqlstate() ????
  • Mysql#connect() ?????????????????? Mysql::CLIENT_MULTI_STATEMENTS, Mysql::CLIENT_MULTI_RESULTS
  • Mysql#set_server_option() ???????????? Mysql::OPTION_MULTI_STATEMENTS_ON, Mysql::OPTION_MULTI_STATEMENTS_OFF
  • ???????????? Mysql#query()
  • Mysql#reconnect(), Mysql#reconnect=() ????
  • MySQL ??????????????????????????????????????????????????????????????
2005-02-12
version 2.5.2
  • BUG: Mysql#connect ????????????????????????????????(Andres Salomon ??????)
2004-09-20
version 2.5.1
  • Mysql#set_ssl() ??????
2004-08-31
version 2.5
  • MySQL 4.1.x ????
  • MysqlRes, MysqlField, MysqlError ?? Mysql::Result, Mysql::Field, Mysql::Error ????????
  • 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() ??????
  • Mysql::Field#is_not_null?(), Mysql::Field#is_pri_key?(), Mysql::Field#is_num?() ??????
  • MysqlField::TYPE_VAR_STRING ??????
2003-08-10
version 2.4.5
  • extconf.rb: MySQL 4.1 ??????
  • mysql.c.in: Ruby 1.8 ??????
2003-02-23
version 2.4.4a
  • extconf.rb ?? Ruby 1.8.0 ??????
2003-01-29
version 2.4.4
  • Mysql::OPT_LOCAL_INFILE ????????
  • extconf.rb ?? --with-mysql-config ????????????????
  • extconf.rb ????????????????????????????????????????????
2003-01-05
version 2.4.3c
  • ?????? README ????????Paul DuBois ????????
2002-12-24
version 2.4.3b
  • extconf.rb ?? Ruby 1.6.8 ??????
2002-11-07
version 2.4.3a
  • ????????????????????????????????????????????????
2002-09-10
version 2.4.3
  • ER_ ????????????????????????????????
  • errmsg.h ?? mysqld_error.h ????????????????????????????????????????????
2002-01-07
version 2.4.2
  • MySQL 4.0 ??????
  • uint ?? unsigned int ????????mswin????????
2001-12-02
version 2.4.1
  • ?????? extern ????????Cygiwn????????
  • extconf.rb ????????????????????
2001-10-12
version 2.4.0
  • Ruby 1.7 ????????
  • Mysql::debug(), Mysql#change_user(), Mysql#character_set_name(), Mysql#dump_debug_info() ????????
2001-03-25
version 2.3.2a
  • t/50update.rb ????????????????
2001-03-19
version 2.3.2
  • ??????(20??) MysqlRes ?????????????????????????????????? GC ????????????????
  • Mysql#escape_string(), Mysql#quote() ?? mysql_real_escape_string() ??????????????????????
2000-09-02
version 2.3.1
  • Mysql#initialize() ??????(Ruby 1.6 ????)??
2000-07-22
version 2.3.0
  • MysqlRes#free() ????????
  • Mysql#initialize(), MysqlRes#initialize() ??????????????
  • true ????????????????????????self ??????????????????
2000-05-27
version 2.2.1a
  • test.rb ??????????????????????????????????????????
2000-05-10
version 2.2.1
  • ??????????????????????????????????????????????????????
  • ??????????????????????
1999-09-28
version 2.2.0
  • Mysql::init(), Mysql#options(), Mysql#real_connect() ????????
  • Mysql#field_count ????????
1999-09-24
version 2.1.7
  • MySQL 3.22.26 ????????
  • MysqlField#inspect() ????????
1999-06-17
version 2.1.6
  • fetch_field ??????????????????????
1999-06-12
version 2.1.5
  • Ruby 1.3.x ??????????????????????
1999-05-30
version 2.1.4
  • Ruby 1.3.x ????????
1999-04-13
version 2.1.3
  • fetch_hash/each_hash ????????????????????????????????
  • fetch_hash/each_hash ?? true ??????????????????????????????????
  • NULL???????????????? fetch_hash/each_hash ??????????????????????
1999-02-01
version 2.1.2
  • Mysql#refresh() ?? Mysql::REFRESH_* ????????
  • MySQL 3.21.xx ????????(??????????????)??
1999-01-24
version 2.1.1
  • MysqlError#error(), MysqlError#errno() ????????
  • MysqlError::CR_* ????????
1999-01-17
version 2.1
  • fetch_hash, each_hash ?? C ??????????????
  • MysqlField#hash() ????????
  • escape_string, get_client_info ????????????????????????????????????????????????
1998-11-29
version 2.0.1
  • fetch_hash, each_hash ?? with_table ????????????
  • ???????? get_* ???????????? C API ??????????????
  • mysql-compat.rb ?? alias ??????????????
  • Mysql ?????? CLIENT_* ?? MysqlField ????????????????????
1998-11-15
version 2.0
  • ???????????? C API ??????????????
  • C++ ??????????????????????????
1998-08-13
version 1.0
  • ????????????????

????

e-mail: ?????????????? [email protected] http://tmtm.org


TOMITA Masahiro
Last modified: Sun Feb 1 17:41:18 JST 2009