Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Ruby                01 LightningTalk



MySQL
   O/R

             makoto kuwata
      http://www.kuwata-lab.com/



    c...
1

MySQL



     copyright(c) 2008 kuwata-lab.com all rights reserved.
                                                   ...
MySQL/Ruby

‣ MySQL/Ruby
  • Ruby          MySQL
  •         :
  • Rails
  • http://www.tmtm.org/mysql/ruby/


        cop...
‣ Mysql::Result#fetch()
   •
‣ Mysql::Stmt#fetch()
   • 3.14      3.14000010490417
   • timestamp            Time         ...
motto-mysql

‣ motto-mysql
  • MySQL/Ruby

  • http://motto-mysql.rubyforge.org/
      http://github.com/kwatch/motto-mysq...
‣ Mysql::Result
                                                                    fetch
   • #fetch_as_hash()/array()
  ...
‣ MySQL::Result#fetch_as(klass)
  MySQL::Result#fetch_all_as(klass)
   • klass

   • Hash        fetch




         copyri...
require 'mysql'
require 'motto_mysql'
conn = Mysql.connect('host', 'user', 'pass', 'db')
sql = 'select * from users'
resul...
DB                    systime                        usertime

     Result#each (           )                4.79 1.08    ...
DB                    systime                    usertime


      Result#each (     +new)                   4.86 1.09     ...
‣ integer(1)            boolean
  true/false
‣ '0000-00-00 00:00:00'                 Time    Date
                        ...
2

    O/R



     copyright(c) 2008 kuwata-lab.com all rights reserved.
                                                 ...
Kwery

‣ Kwery …                                     O/R
  •                          CGI
  • Model class
  • Join-less
  ...
class User
   include Kwery::Model             create table
                                                 DSL
   create...
# Query
conn = Mysql.connect('host', 'user', 'pass', 'db')
q = Kwery::Query.new(conn)
#
#              Hash
user = q.get(U...
: Join
    ‣ Join-less programming                            (   :          )

        • select * from users, groups wher...
Web                            !
http://www.github.com/kwatch/kwery/




     copyright(c) 2008 kuwata-lab.com all rights ...
thank you



copyright(c) 2008 kuwata-lab.com all rights reserved.
                                                       ...
Upcoming SlideShare
Loading in …5
×

MySQLドライバの改良と軽量O/Rマッパーの紹介

12,047 views

Published on

MySQL/Ruby は広く使われていますが、個人的にいくつか問題点を感じています。その問題点を改良し、ついでに機能を強化した補助的な拡張ライブラリを作成しましたので、それを紹介します。またCGIで使える軽量なO/Rマッパーを自作したので、そちらの紹介も行ないます。

Published in: Technology
  • Be the first to comment

MySQLドライバの改良と軽量O/Rマッパーの紹介

  1. 1. Ruby 01 LightningTalk MySQL O/R makoto kuwata http://www.kuwata-lab.com/ copyright(c) 2008 kuwata-lab.com all rights reserved. 1
  2. 2. 1 MySQL copyright(c) 2008 kuwata-lab.com all rights reserved. 2
  3. 3. MySQL/Ruby ‣ MySQL/Ruby • Ruby MySQL • : • Rails • http://www.tmtm.org/mysql/ruby/ copyright(c) 2008 kuwata-lab.com all rights reserved. 3
  4. 4. ‣ Mysql::Result#fetch() • ‣ Mysql::Stmt#fetch() • 3.14 3.14000010490417 • timestamp Time Mysql::Time copyright(c) 2008 kuwata-lab.com all rights reserved. 4
  5. 5. motto-mysql ‣ motto-mysql • MySQL/Ruby • http://motto-mysql.rubyforge.org/ http://github.com/kwatch/motto-mysql • : $ which mysql_config /usr/local/mysql/bin/mysql_config $ sudo gem install motto-mysql copyright(c) 2008 kuwata-lab.com all rights reserved. 5
  6. 6. ‣ Mysql::Result fetch • #fetch_as_hash()/array() • #fetch_all_as_hashes()/arrays() ‣ Mysql::Stmt 3.14 3.14 timestamp Time • #fetch_as_hash()/array() • #fetch_all_as_hashes()/arrays() copyright(c) 2008 kuwata-lab.com all rights reserved. 6
  7. 7. ‣ MySQL::Result#fetch_as(klass) MySQL::Result#fetch_all_as(klass) • klass • Hash fetch copyright(c) 2008 kuwata-lab.com all rights reserved. 7
  8. 8. require 'mysql' require 'motto_mysql' conn = Mysql.connect('host', 'user', 'pass', 'db') sql = 'select * from users' result = conn.query(sql) p result.fetch_as(User) #=> User object result.fetch_all_as(User) do |user| p user #=> User object end copyright(c) 2008 kuwata-lab.com all rights reserved. 8
  9. 9. DB systime usertime Result#each ( ) 4.79 1.08 3.83 Result#each ( ) 4.84 1.08 5.12 Hash fetch + Result#each ( +new) 4.86 1.09 6.99 + Result#each_hash ( ) 4.80 1.08 5.30 Object.new Result#each_hash ( ) 4.87 1.09 7.66 Resutl#each_hash ( +new) 4.90 1.11 10.45 Result#fetch_all_as_arrays() 4.76 1.07 3.57 Result#fetch_all_as_hashes() 4.82 1.08 5.00 , usertime Result#fetch_all_as(klass) 4.80 1.08 4.74 1/2, 2/3 0 5 10 15 20 (sec) copyright(c) 2008 kuwata-lab.com all rights reserved. 9
  10. 10. DB systime usertime Result#each ( +new) 4.86 1.09 6.99 Resutl#each_hash ( +new) 4.90 1.11 10.45 Result#fetch_all_as(klass) 4.80 1.08 4.74 DB fetch Java Java1.5+MySQL Connector5.1.5 5.34 0.80 6.28 Python2.5.2+MySQLdb(dict) 4.87 1.28 9.05 Python2.5.2+MySQLdb(tuple) 4.81 1.27 5.97 0 5 10 15 20 (sec) copyright(c) 2008 kuwata-lab.com all rights reserved. 10
  11. 11. ‣ integer(1) boolean true/false ‣ '0000-00-00 00:00:00' Time Date (ArgumentError) ‣ Ruby on Rails copyright(c) 2008 kuwata-lab.com all rights reserved. 11
  12. 12. 2 O/R copyright(c) 2008 kuwata-lab.com all rights reserved. 12
  13. 13. Kwery ‣ Kwery … O/R • CGI • Model class • Join-less • MySQL motto-mysql • http://github.com/kwatch/kwery/ copyright(c) 2008 kuwata-lab.com all rights reserved. 13
  14. 14. class User include Kwery::Model create table DSL create_table('users') do |t| t.integer(:id) {|c| c.primary_key.serial } t.string(:name) {|c| c.not_null.unique } t.timestamp(:created_at) {|c| c.default(:current_timestamp) } end end # User.to_sql SQL copyright(c) 2008 kuwata-lab.com all rights reserved. 14
  15. 15. # Query conn = Mysql.connect('host', 'user', 'pass', 'db') q = Kwery::Query.new(conn) # # Hash user = q.get(User, :id, 7) # where order_by method chain users = q.get_all(User) {|c| c.where('age >', 20).order_by(:name) } copyright(c) 2008 kuwata-lab.com all rights reserved. 15
  16. 16. : Join ‣ Join-less programming ( : ) • select * from users, groups where user.group_id = groups.id; select * from users; select * from groups where id in (...); • 1+N 2 users = q.get_all(User) #=> select * from users q.bind_references_to(users, Group, 'team_id', 'team') #=> select * from groups where id in (...) copyright(c) 2008 kuwata-lab.com all rights reserved. 16
  17. 17. Web ! http://www.github.com/kwatch/kwery/ copyright(c) 2008 kuwata-lab.com all rights reserved. 17
  18. 18. thank you copyright(c) 2008 kuwata-lab.com all rights reserved. 18

×