Sha256: 58e890002a2898a2ce7d5a89a60a121d4f827a3e6273cacce86856e9e50059d1

Contents?: true

Size: 1.5 KB

Versions: 118

Compression:

Stored size: 1.5 KB

Contents

# -*- ruby -*-

require 'pg'

$stdout.sync = true

# This is a example of how to mix and match synchronous and async APIs. In this case,
# the connection to the server is made syncrhonously, and then queries are
# asynchronous.

TIMEOUT = 5.0 # seconds to wait for an async operation to complete
CONN_OPTS = {
	:host     => 'localhost',
	:dbname   => 'test',
}

# Output progress messages
def output_progress( msg )
	puts ">>> #{msg}\n"
end

# Start the (synchronous) connection
output_progress "Starting connection..."
conn = PG.connect( CONN_OPTS ) or abort "Unable to create a new connection!"

abort "Connect failed: %s" % [ conn.error_message ] unless conn.status == PG::CONNECTION_OK

# Now grab a reference to the underlying socket to select() on while the query is running
socket = conn.socket_io

# Send the (asynchronous) query
output_progress "Sending query"
conn.send_query( "SELECT * FROM pg_stat_activity" )

# Fetch results until there aren't any more
loop do
	output_progress "  waiting for a response"

	# Buffer any incoming data on the socket until a full result is ready.
	conn.consume_input
	while conn.is_busy
		output_progress "  waiting for data to be available on %p..." % [ socket ]
		select( [socket], nil, nil, TIMEOUT ) or
			raise "Timeout waiting for query response."
		conn.consume_input
	end

	# Fetch the next result. If there isn't one, the query is finished
	result = conn.get_result or break

	output_progress "Query result:\n%p\n" % [ result.values ]
end

output_progress "Done."
conn.finish

Version data entries

118 entries across 118 versions & 4 rubygems

Version Path
pg-1.3.5 sample/async_mixed.rb
postgresql-lambda-1.3.4.3 ./sample/async_mixed.rb
postgresql-lambda-1.3.4.1 ./sample/async_mixed.rb
pg-1.3.4-x64-mingw32 sample/async_mixed.rb
pg-1.3.4-x64-mingw-ucrt sample/async_mixed.rb
pg-1.3.4-x86-mingw32 sample/async_mixed.rb
pg-1.3.4 sample/async_mixed.rb
pg-1.3.3-x64-mingw-ucrt sample/async_mixed.rb
pg-1.3.3-x64-mingw32 sample/async_mixed.rb
pg-1.3.3-x86-mingw32 sample/async_mixed.rb
pg-1.3.3 sample/async_mixed.rb
pg-1.3.2-x86-mingw32 sample/async_mixed.rb
pg-1.3.2-x64-mingw-ucrt sample/async_mixed.rb
pg-1.3.2-x64-mingw32 sample/async_mixed.rb
pg-1.3.2 sample/async_mixed.rb
pg-1.3.1 sample/async_mixed.rb
pg-1.3.0-x86-mingw32 sample/async_mixed.rb
pg-1.3.0-x64-mingw32 sample/async_mixed.rb
pg-1.3.0-x64-mingw-ucrt sample/async_mixed.rb
pg-1.3.0 sample/async_mixed.rb