require("cipherstash-pg") TIMEOUT = 5.0 progress_thread = Thread.new { loop { print("x") } } def output_progress(msg) puts("\n>>> #{msg}\n") end output_progress("Starting connection...") (conn = CipherStashPG::Connection.connect_start(:dbname => "test") or abort("Unable to create a new connection!")) if (conn.status == CipherStashPG::CONNECTION_BAD) then abort(("Connection failed: %s" % [conn.error_message])) end poll_status = CipherStashPG::PGRES_POLLING_WRITING until ((poll_status == CipherStashPG::PGRES_POLLING_OK) or (poll_status == CipherStashPG::PGRES_POLLING_FAILED)) do (case poll_status when CipherStashPG::PGRES_POLLING_READING then output_progress(" waiting for socket to become readable") (select([conn.socket_io], nil, nil, TIMEOUT) or raise("Asynchronous connection timed out!")) when CipherStashPG::PGRES_POLLING_WRITING then output_progress(" waiting for socket to become writable") (select(nil, [conn.socket_io], nil, TIMEOUT) or raise("Asynchronous connection timed out!")) end case conn.status when CipherStashPG::CONNECTION_STARTED then output_progress(" waiting for connection to be made.") when CipherStashPG::CONNECTION_MADE then output_progress(" connection OK; waiting to send.") when CipherStashPG::CONNECTION_AWAITING_RESPONSE then output_progress(" waiting for a response from the server.") when CipherStashPG::CONNECTION_AUTH_OK then output_progress(" received authentication; waiting for backend start-up to finish.") when CipherStashPG::CONNECTION_SSL_STARTUP then output_progress(" negotiating SSL encryption.") when CipherStashPG::CONNECTION_SETENV then output_progress(" negotiating environment-driven parameter settings.") when CipherStashPG::CONNECTION_NEEDED then output_progress(" internal state: connect() needed.") end poll_status = conn.connect_poll) end unless (conn.status == CipherStashPG::CONNECTION_OK) then abort(("Connect failed: %s" % [conn.error_message])) end output_progress("Sending query") conn.send_query("SELECT * FROM pg_stat_activity") loop do output_progress(" waiting for a response") conn.consume_input while conn.is_busy do (select([conn.socket_io], nil, nil, TIMEOUT) or raise("Timeout waiting for query response.")) conn.consume_input end (result = conn.get_result or break) puts(("\n\nQuery result:\n%p\n" % [result.values])) end output_progress("Done.") conn.finish if defined? progress_thread then progress_thread.kill progress_thread.join end