Ruby Postgres

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites & 1 Group

    Ruby Postgres - Presentation Transcript

    1. ActiveRecordの PostgreSQLアダプタ 周辺について調べてみた (株)サイクル・オブ・フィフス 石田朗雄
    2. はじめに ActiveRecordのPostgreSQLアダプタについて調べてみて、 はまったところや気になったとこをまとめました。 でもメインはその下のレイヤの話です。 普段Rubyを使っているわけではないので、僕の話にRubyに対 する愛が足りないように聞こえたらごめんなさい。
    3. 構成 ActiveRecord::Base AR::ConnectionAdapter::Base AR::CA::PostgreSQLAdapter AR::CA::他のRDBMS向け ruby-postgres postgres-pr (pure ruby) libpq(C言語) 04:54:22 PM Ruby-Sapporo 2007-11-17 3
    4. postgres-prとの比較 ruby-postgres-0.7.1.2006.04.06-mswin32 postgres-pr-0.4.0 postgres-prは、postgres.soがあればそっちを呼ぶので、両 方入っている環境では工夫が必要 ruby-postgres irb(main):002:0> require \"postgres\" => true postgres-pr irb(main):003:0> require \"postgres-pr/postgres-compat\" => true 04:54:22 PM Ruby-Sapporo 2007-11-17 4
    5. postgres-prとの比較 > PGconn.instance_methods(false).sort => [\"async_exec\", \"async_query\", \"client_encoding\", \"close\", \"db\", \"endcopy\", \"error\", \"exec\", \"finish\", \"get_notify\", \"getline\", \"host\", \"insert_table\", \"lo_create\", \"lo_export\", \"lo_import\", \"lo_open\", \"lo_unlink\", \"locreate\", \"loexport\", \"loimport\", \"loopen\", \"lounlink\", \"on_notice\", \"options\", \"port\", \"protocol_version\", \"putline\", \"query\", \"reset\", \"select_one\", \"select_value\", \"select_values\", \"server_version\", \"set_client_encoding\", \"status\", \"trace\", \"transaction_status\", \"tty\", \"untrace\", \"user\"] > PGconn.instance_methods(false).sort => [\"close\", \"db\", \"exec\", \"host\", \"query\", \"user\"] 04:54:22 PM Ruby-Sapporo 2007-11-17 5
    6. 接続 irb(main):013:0> c=PGconn.new(nil, 9999, nil, nil, 'ishida', 'ishida', nil) PGError: could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host \"???\" and accepting TCP/IP connections on port 9999? from (irb):13:in `initialize' postgres-prではhostがnilだと from (irb):13:in `new' portが無視される from (irb):13 irb(main):004:0> c=PGconn.new(nil, 9999, nil, nil, 'ishida', 'ishida', nil) => #<PGconn:0x2dc3d98 @db=\"ishida\", @host=nil, @conn=#<PostgresPR::Connection:0x2dc3d70 @params={\"integer_datetimes\"=>\"off\", \"TimeZone\"=>\"JST-9\", \"server_encoding\"=>\"EUC_JP\", \"standard_conforming_strings\"=>\"off\", \"DateStyle\"=>\"ISO, MDY\", \"client_encoding\"=>\"EUC_JP\", \"session_authorization\"=>\"ishida\", \"server_version\"=>\"8.1.3\", \"is_superuser\"=>\"off\"}, @conn=#<TCPSocket:0x2dc3bf4>>, @user=\"ishida\"> 04:54:22 PM Ruby-Sapporo 2007-11-17 6
    7. ParameterStatus PostgreSQLのコンフィグのうち、クライアントの動作に影響 するものをサーバからクライアントに対して送る 接続時、SETコマンドによる変更、コンフィグファイルのリ ロード等 server_version client_encoding DateStyle 等々 バージョンが上がると増えることがあるの で、PGconn#getparamみたいのが欲しい 04:54:22 PM Ruby-Sapporo 2007-11-17 7
    8. postgres-prとの比較 irb(main):004:0> c.client_encoding => \"SQL_ASCII\" irb(main):005:0> c.exec(\"set client_encoding to sjis\") => #<PGresult:0x2dc2358> irb(main):006:0> c.client_encoding => \"SJIS\" irb(main):010:0> c.instance_variable_get(:@conn).instance_variable_get(:@params) [\"client_encoding\"] => \"SQL_ASCII\" irb(main):011:0> c.exec(\"set client_encoding to sjis\") => #<PGresult:0x2e98cb4 @result=[], @fields=[], @res=#<PostgresPR::Connection::Result:0x2e98c50 @cmd_tag=\"SET\", @fields=[], @rows=[]>> irb(main):012:0> c.instance_variable_get(:@conn).instance_variable_get(:@params) [\"client_encoding\"] => \"SQL_ASCII\" postgres-prは ParameterStatusの変更を 04:54:22 PM Ruby-Sapporo 2007-11-17 拾っていない 8
    9. コイツ、、、動くぞ、、、 PGconn.translate_results = false if PGconn.respond_to? :translate_results= .... if @connection.respond_to?(:status) ... # TODO: postgres-pr doesn't have PGconn#reset. if @connection.respond_to?(:reset) 但し、allow_concurrencyだけは使ってはいけない def query(sql, name = nil) #:nodoc: log(sql, name) do if @async @connection.async_query(sql) else @connection.query(sql) end end 04:54:22 PM Ruby-Sapporo 2007-11-17 9
    10. PGconn#async_exec pgconn_async_exec():postgres.c if (!PQsendQuery(conn, RSTRING(str)->ptr)) { rb_raise(rb_ePGError, PQerrorMessage(conn)); } cs = PQsocket(conn); PQsendQueryで非同期にクエリ for(;;) { 送信した後、結果が返ってくるの FD_ZERO(&rset); を待っている? FD_SET(cs, &rset); ret = rb_thread_select(cs + 1, &rset, NULL, NULL, NULL); 04:54:22 PM Ruby-Sapporo 2007-11-17 10
    11. まとめ? ruby-postgresとpostgres-prは結構違うけ ど、ActiveRecordが違いを吸収している 今回挙げたものの他にもpostgres-prは、引数のoptionsを無 視してたり環境変数も無視してたり、ParameterStatusを無 視するケースがあったり、、、 素のpostgres-prを使うには覚悟が必要 04:54:22 PM Ruby-Sapporo 2007-11-17 11
    12. C:\\ruby\\lib\\ruby\\gems\\1.8\\gems\\postgres-pr-0.4.0\\lib>grep -r TODO * postgres-pr/connection.rb: # TODO postgres-pr/connection.rb: # TODO: use transaction status postgres-pr/connection.rb: # TODO postgres-pr/connection.rb: # TODO postgres-pr/message.rb:# TODO postgres-pr/message.rb:# TODO postgres-pr/message.rb: # TODO: zero means unspecified. map to nil? postgres-pr/message.rb:# TODO: duplicate message-type, split into client/servermessages postgres-pr/postgres-compat.rb: # TODO: correct? postgres-pr/postgres-compat.rb: # TODO: status, getlength, cmdstatus postgres-pr/postgres-compat.rb: # TODO: correct? postgres-pr/postgres-compat.rb: # TODO: correct? postgres-pr/typeconv/TC_conv.rb: assert_equal [\"\"], decode_array(\"{ }\") # TODO: Correct? 04:54:22 PM Ruby-Sapporo 2007-11-17 12

    + iakioiakio, 3 years ago

    custom

    4328 views, 2 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 4328
      • 4328 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 32
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events