yugabytedb-ysql 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.appveyor.yml +42 -0
  3. data/.gems +6 -0
  4. data/.gemtest +0 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +143 -0
  7. data/.gitignore +24 -0
  8. data/.hgsigs +34 -0
  9. data/.hgtags +41 -0
  10. data/.irbrc +23 -0
  11. data/.pryrc +23 -0
  12. data/.tm_properties +21 -0
  13. data/.travis.yml +49 -0
  14. data/BSDL +22 -0
  15. data/Contributors.rdoc +46 -0
  16. data/Gemfile +18 -0
  17. data/History.md +901 -0
  18. data/LICENSE +56 -0
  19. data/Manifest.txt +73 -0
  20. data/POSTGRES +23 -0
  21. data/README-OS_X.rdoc +68 -0
  22. data/README-Windows.rdoc +56 -0
  23. data/README.ja.md +302 -0
  24. data/README.md +381 -0
  25. data/Rakefile +118 -0
  26. data/Rakefile.cross +299 -0
  27. data/certs/ged.pem +24 -0
  28. data/certs/kanis@comcard.de.pem +20 -0
  29. data/certs/larskanis-2022.pem +26 -0
  30. data/certs/larskanis-2023.pem +24 -0
  31. data/certs/larskanis-2024.pem +24 -0
  32. data/ext/errorcodes.def +1044 -0
  33. data/ext/errorcodes.rb +45 -0
  34. data/ext/errorcodes.txt +497 -0
  35. data/ext/extconf.rb +174 -0
  36. data/ext/gvl_wrappers.c +21 -0
  37. data/ext/gvl_wrappers.h +264 -0
  38. data/ext/pg.c +692 -0
  39. data/ext/pg.h +392 -0
  40. data/ext/pg_binary_decoder.c +308 -0
  41. data/ext/pg_binary_encoder.c +387 -0
  42. data/ext/pg_coder.c +624 -0
  43. data/ext/pg_connection.c +4681 -0
  44. data/ext/pg_copy_coder.c +917 -0
  45. data/ext/pg_errors.c +95 -0
  46. data/ext/pg_record_coder.c +522 -0
  47. data/ext/pg_result.c +1766 -0
  48. data/ext/pg_text_decoder.c +1005 -0
  49. data/ext/pg_text_encoder.c +827 -0
  50. data/ext/pg_tuple.c +572 -0
  51. data/ext/pg_type_map.c +200 -0
  52. data/ext/pg_type_map_all_strings.c +130 -0
  53. data/ext/pg_type_map_by_class.c +271 -0
  54. data/ext/pg_type_map_by_column.c +355 -0
  55. data/ext/pg_type_map_by_mri_type.c +313 -0
  56. data/ext/pg_type_map_by_oid.c +388 -0
  57. data/ext/pg_type_map_in_ruby.c +333 -0
  58. data/ext/pg_util.c +149 -0
  59. data/ext/pg_util.h +65 -0
  60. data/ext/vc/pg.sln +26 -0
  61. data/ext/vc/pg_18/pg.vcproj +216 -0
  62. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  63. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  64. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  65. data/lib/pg/basic_type_map_for_results.rb +104 -0
  66. data/lib/pg/basic_type_registry.rb +303 -0
  67. data/lib/pg/binary_decoder/date.rb +9 -0
  68. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  69. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  70. data/lib/pg/coder.rb +106 -0
  71. data/lib/pg/connection.rb +997 -0
  72. data/lib/pg/exceptions.rb +25 -0
  73. data/lib/pg/load_balance_service.rb +406 -0
  74. data/lib/pg/result.rb +43 -0
  75. data/lib/pg/text_decoder/date.rb +18 -0
  76. data/lib/pg/text_decoder/inet.rb +9 -0
  77. data/lib/pg/text_decoder/json.rb +14 -0
  78. data/lib/pg/text_decoder/numeric.rb +9 -0
  79. data/lib/pg/text_decoder/timestamp.rb +30 -0
  80. data/lib/pg/text_encoder/date.rb +12 -0
  81. data/lib/pg/text_encoder/inet.rb +28 -0
  82. data/lib/pg/text_encoder/json.rb +14 -0
  83. data/lib/pg/text_encoder/numeric.rb +9 -0
  84. data/lib/pg/text_encoder/timestamp.rb +24 -0
  85. data/lib/pg/tuple.rb +30 -0
  86. data/lib/pg/type_map_by_column.rb +16 -0
  87. data/lib/pg/version.rb +5 -0
  88. data/lib/ysql.rb +130 -0
  89. data/misc/openssl-pg-segfault.rb +31 -0
  90. data/misc/postgres/History.txt +9 -0
  91. data/misc/postgres/Manifest.txt +5 -0
  92. data/misc/postgres/README.txt +21 -0
  93. data/misc/postgres/Rakefile +21 -0
  94. data/misc/postgres/lib/postgres.rb +16 -0
  95. data/misc/ruby-pg/History.txt +9 -0
  96. data/misc/ruby-pg/Manifest.txt +5 -0
  97. data/misc/ruby-pg/README.txt +21 -0
  98. data/misc/ruby-pg/Rakefile +21 -0
  99. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  100. data/rakelib/task_extension.rb +46 -0
  101. data/sample/array_insert.rb +20 -0
  102. data/sample/async_api.rb +102 -0
  103. data/sample/async_copyto.rb +39 -0
  104. data/sample/async_mixed.rb +56 -0
  105. data/sample/check_conn.rb +21 -0
  106. data/sample/copydata.rb +71 -0
  107. data/sample/copyfrom.rb +81 -0
  108. data/sample/copyto.rb +19 -0
  109. data/sample/cursor.rb +21 -0
  110. data/sample/disk_usage_report.rb +177 -0
  111. data/sample/issue-119.rb +94 -0
  112. data/sample/losample.rb +69 -0
  113. data/sample/minimal-testcase.rb +17 -0
  114. data/sample/notify_wait.rb +72 -0
  115. data/sample/pg_statistics.rb +285 -0
  116. data/sample/replication_monitor.rb +222 -0
  117. data/sample/test_binary_values.rb +33 -0
  118. data/sample/wal_shipper.rb +434 -0
  119. data/sample/warehouse_partitions.rb +311 -0
  120. data/yugabytedb-ysql.gemspec +33 -0
  121. metadata +232 -0
data/lib/pg/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ module YSQL
2
+ # Library version
3
+ PG_VERSION = '1.5.6'
4
+ VERSION = '0.3'
5
+ end
data/lib/ysql.rb ADDED
@@ -0,0 +1,130 @@
1
+
2
+ # -*- ruby -*-
3
+ # frozen_string_literal: true
4
+
5
+ # The top-level PG namespace.
6
+ module YSQL
7
+
8
+ # Is this file part of a fat binary gem with bundled libpq?
9
+ bundled_libpq_path = File.join(__dir__, RUBY_PLATFORM.gsub(/^i386-/, "x86-"))
10
+ if File.exist?(bundled_libpq_path)
11
+ POSTGRESQL_LIB_PATH = bundled_libpq_path
12
+ else
13
+ bundled_libpq_path = nil
14
+ # Try to load libpq path as found by extconf.rb
15
+ begin
16
+ require "pg/postgresql_lib_path"
17
+ rescue LoadError
18
+ # rake-compiler doesn't use regular "make install", but uses it's own install tasks.
19
+ # It therefore doesn't copy pg/postgresql_lib_path.rb in case of "rake compile".
20
+ POSTGRESQL_LIB_PATH = false
21
+ end
22
+ end
23
+
24
+ add_dll_path = proc do |path, &block|
25
+ if RUBY_PLATFORM =~/(mswin|mingw)/i && path && File.exist?(path)
26
+ begin
27
+ require 'ruby_installer/runtime'
28
+ RubyInstaller::Runtime.add_dll_directory(path, &block)
29
+ rescue LoadError
30
+ old_path = ENV['PATH']
31
+ ENV['PATH'] = "#{path};#{old_path}"
32
+ block.call
33
+ ENV['PATH'] = old_path
34
+ end
35
+ else
36
+ # No need to set a load path manually - it's set as library rpath.
37
+ block.call
38
+ end
39
+ end
40
+
41
+ # Add a load path to the one retrieved from pg_config
42
+ add_dll_path.call(POSTGRESQL_LIB_PATH) do
43
+ if bundled_libpq_path
44
+ # It's a Windows binary gem, try the <major>.<minor> subdirectory
45
+ major_minor = RUBY_VERSION[ /^(\d+\.\d+)/ ] or
46
+ raise "Oops, can't extract the major/minor version from #{RUBY_VERSION.dump}"
47
+ require "#{major_minor}/pg_ext"
48
+ else
49
+ require 'pg_ext'
50
+ end
51
+ end
52
+
53
+ # Get the PG library version.
54
+ #
55
+ # +include_buildnum+ is no longer used and any value passed will be ignored.
56
+ def self.version_string( include_buildnum=nil )
57
+ "%s %s" % [ self.name, VERSION ]
58
+ end
59
+
60
+
61
+ ### Convenience alias for PG::Connection.new.
62
+ def self.connect( *args, &block )
63
+ Connection.new( *args, &block )
64
+ end
65
+
66
+ if defined?(Ractor.make_shareable)
67
+ def self.make_shareable(obj)
68
+ Ractor.make_shareable(obj)
69
+ end
70
+ else
71
+ def self.make_shareable(obj)
72
+ obj.freeze
73
+ end
74
+ end
75
+
76
+ module BinaryDecoder
77
+ %i[ TimestampUtc TimestampUtcToLocal TimestampLocal ].each do |klass|
78
+ autoload klass, 'pg/binary_decoder/timestamp'
79
+ end
80
+ autoload :Date, 'pg/binary_decoder/date'
81
+ end
82
+ module BinaryEncoder
83
+ %i[ TimestampUtc TimestampLocal ].each do |klass|
84
+ autoload klass, 'pg/binary_encoder/timestamp'
85
+ end
86
+ end
87
+ module TextDecoder
88
+ %i[ TimestampUtc TimestampUtcToLocal TimestampLocal TimestampWithoutTimeZone TimestampWithTimeZone ].each do |klass|
89
+ autoload klass, 'pg/text_decoder/timestamp'
90
+ end
91
+ autoload :Date, 'pg/text_decoder/date'
92
+ autoload :Inet, 'pg/text_decoder/inet'
93
+ autoload :JSON, 'pg/text_decoder/json'
94
+ autoload :Numeric, 'pg/text_decoder/numeric'
95
+ end
96
+ module TextEncoder
97
+ %i[ TimestampUtc TimestampWithoutTimeZone TimestampWithTimeZone ].each do |klass|
98
+ autoload klass, 'pg/text_encoder/timestamp'
99
+ end
100
+ autoload :Date, 'pg/text_encoder/date'
101
+ autoload :Inet, 'pg/text_encoder/inet'
102
+ autoload :JSON, 'pg/text_encoder/json'
103
+ autoload :Numeric, 'pg/text_encoder/numeric'
104
+ end
105
+
106
+ autoload :BasicTypeMapBasedOnResult, 'pg/basic_type_map_based_on_result'
107
+ autoload :BasicTypeMapForQueries, 'pg/basic_type_map_for_queries'
108
+ autoload :BasicTypeMapForResults, 'pg/basic_type_map_for_results'
109
+ autoload :BasicTypeRegistry, 'pg/basic_type_registry'
110
+ require 'pg/exceptions'
111
+ require 'pg/coder'
112
+ require 'pg/type_map_by_column'
113
+ require 'pg/connection'
114
+ require 'pg/result'
115
+ require 'pg/tuple'
116
+ require 'pg/load_balance_service'
117
+ autoload :VERSION, 'pg/version'
118
+
119
+
120
+ # Avoid "uninitialized constant Truffle::WarningOperations" on Truffleruby up to 22.3.1
121
+ if RUBY_ENGINE=="truffleruby" && !defined?(Truffle::WarningOperations)
122
+ module TruffleFixWarn
123
+ def warn(str, category=nil)
124
+ super(str)
125
+ end
126
+ end
127
+ Warning.extend(TruffleFixWarn)
128
+ end
129
+
130
+ end # module PG
@@ -0,0 +1,31 @@
1
+ # -*- ruby -*-
2
+
3
+ PGHOST = 'localhost'
4
+ PGDB = 'test'
5
+ #SOCKHOST = 'github.com'
6
+ SOCKHOST = 'it-trac.laika.com'
7
+
8
+ # Load pg first, so the libssl.so that libpq is linked against is loaded.
9
+ require 'ysql'
10
+ $stderr.puts "connecting to postgres://#{PGHOST}/#{PGDB}"
11
+ conn = YSQL.connect(PGHOST, :dbname => PGDB )
12
+
13
+ # Now load OpenSSL, which might be linked against a different libssl.
14
+ require 'socket'
15
+ require 'openssl'
16
+ $stderr.puts "Connecting to #{SOCKHOST}"
17
+ sock = TCPSocket.open( SOCKHOST, 443 )
18
+ ctx = OpenSSL::SSL::SSLContext.new
19
+ sock = OpenSSL::SSL::SSLSocket.new( sock, ctx )
20
+ sock.sync_close = true
21
+
22
+ # The moment of truth...
23
+ $stderr.puts "Attempting to connect..."
24
+ begin
25
+ sock.connect
26
+ rescue Errno
27
+ $stderr.puts "Got an error connecting, but no segfault."
28
+ else
29
+ $stderr.puts "Nope, no segfault!"
30
+ end
31
+
@@ -0,0 +1,9 @@
1
+ == v0.8.0 [2012-02-09] Michael Granger <ged@FaerieMUD.org>
2
+
3
+ This placeholder version.
4
+
5
+
6
+ == v0.7.9.2008.01.28 [2008-01-28] Jeff Davis <<ruby-pg@j-davis.com>>
7
+
8
+ The last actual version.
9
+
@@ -0,0 +1,5 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/postgres.rb
@@ -0,0 +1,21 @@
1
+ = postgres
2
+
3
+ * https://github.com/ged/ruby-pg
4
+
5
+ == Description
6
+
7
+ This is an old, deprecated version of the Ruby PostgreSQL driver that hasn't
8
+ been maintained or supported since early 2008.
9
+
10
+ You should install/require 'pg' instead.
11
+
12
+ If you need the 'postgres' gem for legacy code that can't be converted, you can
13
+ still install it using an explicit version, like so:
14
+
15
+ gem install postgres -v '0.7.9.2008.01.28'
16
+ gem uninstall postgres -v '>0.7.9.2008.01.28'
17
+
18
+ If you have any questions, the nice folks in the Google group can help:
19
+
20
+ http://goo.gl/OjOPP / ruby-pg@googlegroups.com
21
+
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'date'
4
+ require 'rubygems'
5
+ require 'hoe'
6
+ require 'pp'
7
+
8
+ Hoe.spec 'postgres' do
9
+ self.developer 'Michael Granger', 'ged@FaerieMUD.org'
10
+ self.dependency 'pg', '~> 0'
11
+ self.spec_extras[:date] = Date.parse( '2008/01/30' )
12
+
13
+ line = '-' * 75
14
+ msg = paragraphs_of( 'README.txt', 3..-1 )
15
+ msg.unshift( line )
16
+ msg.push( line )
17
+
18
+ self.spec_extras[:post_install_message] = msg.join( "\n\n" ) + "\n"
19
+ end
20
+
21
+ # vim: syntax=ruby
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pathname'
4
+
5
+ module Postgres
6
+
7
+ VERSION = '0.8.1'
8
+
9
+ gemdir = Pathname( __FILE__ ).dirname.parent
10
+ readme = gemdir + 'README.txt'
11
+
12
+ header, message = readme.read.split( /^== Description/m )
13
+ abort( message.strip )
14
+
15
+ end
16
+
@@ -0,0 +1,9 @@
1
+ == v0.8.0 [2012-02-09] Michael Granger <ged@FaerieMUD.org>
2
+
3
+ This placeholder version.
4
+
5
+
6
+ == v0.7.9.2008.01.28 [2008-01-28] Jeff Davis <<ruby-pg@j-davis.com>>
7
+
8
+ The last actual version.
9
+
@@ -0,0 +1,5 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/ruby/pg.rb
@@ -0,0 +1,21 @@
1
+ = ruby-pg
2
+
3
+ * https://github.com/ged/ruby-pg
4
+
5
+ == Description
6
+
7
+ This is an old, deprecated version of the 'pg' gem that hasn't been
8
+ maintained or supported since early 2008.
9
+
10
+ You should install/require 'pg' instead.
11
+
12
+ If you need ruby-pg for legacy code that can't be converted, you can still
13
+ install it using an explicit version, like so:
14
+
15
+ gem install ruby-pg -v '0.7.9.2008.01.28'
16
+ gem uninstall ruby-pg -v '>0.7.9.2008.01.28'
17
+
18
+ If you have any questions, the nice folks in the Google group can help:
19
+
20
+ http://goo.gl/OjOPP / ruby-pg@googlegroups.com
21
+
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'date'
4
+ require 'rubygems'
5
+ require 'hoe'
6
+ require 'pp'
7
+
8
+ Hoe.spec 'ruby-pg' do
9
+ self.developer 'Michael Granger', 'ged@FaerieMUD.org'
10
+ self.dependency 'pg', '~> 0'
11
+ self.spec_extras[:date] = Date.parse( '2008/01/30' )
12
+
13
+ line = '-' * 75
14
+ msg = paragraphs_of( 'README.txt', 3..-1 )
15
+ msg.unshift( line )
16
+ msg.push( line )
17
+
18
+ self.spec_extras[:post_install_message] = msg.join( "\n\n" ) + "\n"
19
+ end
20
+
21
+ # vim: syntax=ruby
@@ -0,0 +1,16 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pathname'
4
+
5
+ module Pg
6
+
7
+ VERSION = '0.8.0'
8
+
9
+ gemdir = Pathname( __FILE__ ).dirname.parent.parent
10
+ readme = gemdir + 'README.txt'
11
+
12
+ header, message = readme.read.split( /^== Description/m )
13
+ abort( message.strip )
14
+
15
+ end
16
+
@@ -0,0 +1,46 @@
1
+ # This source code is borrowed from:
2
+ # https://github.com/oneclick/rubyinstaller2/blob/b3dcbf69f131e44c78ea3a1c5e0041c223f266ce/lib/ruby_installer/build/utils.rb#L104-L144
3
+
4
+ module TaskExtension
5
+ # Extend rake's file task to be defined only once and to check the expected file is indeed generated
6
+ #
7
+ # The same as #task, but for #file.
8
+ # In addition this file task raises an error, if the file that is expected to be generated is not present after the block was executed.
9
+ def file(name, *args, &block)
10
+ task_once(name, block) do
11
+ super(name, *args) do |ta|
12
+ block&.call(ta).tap do
13
+ raise "file #{ta.name} is missing after task executed" unless File.exist?(ta.name)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ # Extend rake's task definition to be defined only once, even if called several times
20
+ #
21
+ # This allows to define common tasks next to specific tasks.
22
+ # It is expected that any variation of the task's block is reflected in the task name or namespace.
23
+ # If the task name is identical, the task block is executed only once, even if the file task definition is executed twice.
24
+ def task(name, *args, &block)
25
+ task_once(name, block) do
26
+ super
27
+ end
28
+ end
29
+
30
+ private def task_once(name, block)
31
+ name = name.keys.first if name.is_a?(Hash)
32
+ if block &&
33
+ Rake::Task.task_defined?(name) &&
34
+ Rake::Task[name].instance_variable_get('@task_block_location') == block.source_location
35
+ # task is already defined for this target and the same block
36
+ # So skip double definition of the same action
37
+ Rake::Task[name]
38
+ elsif block
39
+ yield.tap do
40
+ Rake::Task[name].instance_variable_set('@task_block_location', block.source_location)
41
+ end
42
+ else
43
+ yield
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,20 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'ysql'
4
+
5
+ c = YSQL.connect(dbname: 'test' )
6
+
7
+ # this one works:
8
+ c.exec( "DROP TABLE IF EXISTS foo" )
9
+ c.exec( "CREATE TABLE foo (strings character varying[]);" )
10
+
11
+ # But using a prepared statement works:
12
+ c.set_error_verbosity(YSQL::PQERRORS_VERBOSE )
13
+ c.prepare( 'stmt', "INSERT INTO foo VALUES ($1);" )
14
+
15
+ # This won't work
16
+ #c.exec_prepared( 'stmt', ["ARRAY['this','that']"] )
17
+
18
+ # but this will:
19
+ c.exec_prepared( 'stmt', ["{'this','that'}"] )
20
+
@@ -0,0 +1,102 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'ysql'
4
+
5
+ # This is a example of how to use the asynchronous API to query the
6
+ # server without blocking other threads. It's intentionally low-level;
7
+ # if you hooked up the PG::Connection#socket to some kind of reactor, you
8
+ # could make this much nicer.
9
+
10
+ TIMEOUT = 5.0 # seconds to wait for an async operation to complete
11
+
12
+ # Print 'x' continuously to demonstrate that other threads aren't
13
+ # blocked while waiting for the connection, for the query to be sent,
14
+ # for results, etc. You might want to sleep inside the loop or
15
+ # comment this out entirely for cleaner output.
16
+ progress_thread = Thread.new { loop { print 'x' } }
17
+
18
+ # Output progress messages
19
+ def output_progress( msg )
20
+ puts "\n>>> #{msg}\n"
21
+ end
22
+
23
+ # Start the connection
24
+ output_progress "Starting connection..."
25
+ conn = YSQL::Connection.connect_start(:dbname => 'test' ) or
26
+ abort "Unable to create a new connection!"
27
+ abort "Connection failed: %s" % [ conn.error_message ] if
28
+ conn.status == YSQL::CONNECTION_BAD
29
+
30
+ # Track the progress of the connection, waiting for the socket to become readable/writable
31
+ # before polling it
32
+ poll_status = YSQL::PGRES_POLLING_WRITING
33
+ until poll_status == YSQL::PGRES_POLLING_OK ||
34
+ poll_status == YSQL::PGRES_POLLING_FAILED
35
+
36
+ # If the socket needs to read, wait 'til it becomes readable to poll again
37
+ case poll_status
38
+ when YSQL::PGRES_POLLING_READING
39
+ output_progress " waiting for socket to become readable"
40
+ select( [conn.socket_io], nil, nil, TIMEOUT ) or
41
+ raise "Asynchronous connection timed out!"
42
+
43
+ # ...and the same for when the socket needs to write
44
+ when YSQL::PGRES_POLLING_WRITING
45
+ output_progress " waiting for socket to become writable"
46
+ select( nil, [conn.socket_io], nil, TIMEOUT ) or
47
+ raise "Asynchronous connection timed out!"
48
+ end
49
+
50
+ # Output a status message about the progress
51
+ case conn.status
52
+ when YSQL::CONNECTION_STARTED
53
+ output_progress " waiting for connection to be made."
54
+ when YSQL::CONNECTION_MADE
55
+ output_progress " connection OK; waiting to send."
56
+ when YSQL::CONNECTION_AWAITING_RESPONSE
57
+ output_progress " waiting for a response from the server."
58
+ when YSQL::CONNECTION_AUTH_OK
59
+ output_progress " received authentication; waiting for backend start-up to finish."
60
+ when YSQL::CONNECTION_SSL_STARTUP
61
+ output_progress " negotiating SSL encryption."
62
+ when YSQL::CONNECTION_SETENV
63
+ output_progress " negotiating environment-driven parameter settings."
64
+ when YSQL::CONNECTION_NEEDED
65
+ output_progress " internal state: connect() needed."
66
+ end
67
+
68
+ # Check to see if it's finished or failed yet
69
+ poll_status = conn.connect_poll
70
+ end
71
+
72
+ abort "Connect failed: %s" % [ conn.error_message ] unless conn.status == YSQL::CONNECTION_OK
73
+
74
+ output_progress "Sending query"
75
+ conn.send_query( "SELECT * FROM pg_stat_activity" )
76
+
77
+ # Fetch results until there aren't any more
78
+ loop do
79
+ output_progress " waiting for a response"
80
+
81
+ # Buffer any incoming data on the socket until a full result is ready.
82
+ conn.consume_input
83
+ while conn.is_busy
84
+ select( [conn.socket_io], nil, nil, TIMEOUT ) or
85
+ raise "Timeout waiting for query response."
86
+ conn.consume_input
87
+ end
88
+
89
+ # Fetch the next result. If there isn't one, the query is finished
90
+ result = conn.get_result or break
91
+
92
+ puts "\n\nQuery result:\n%p\n" % [ result.values ]
93
+ end
94
+
95
+ output_progress "Done."
96
+ conn.finish
97
+
98
+ if defined?( progress_thread )
99
+ progress_thread.kill
100
+ progress_thread.join
101
+ end
102
+
@@ -0,0 +1,39 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'ysql'
4
+ require 'stringio'
5
+
6
+ # Using COPY asynchronously
7
+
8
+ $stderr.puts "Opening database connection ..."
9
+ conn = YSQL.connect(:dbname => 'test' )
10
+ conn.setnonblocking( true )
11
+
12
+ socket = conn.socket_io
13
+
14
+ $stderr.puts "Running COPY command ..."
15
+ buf = ''
16
+ conn.transaction do
17
+ conn.send_query( "COPY logs TO STDOUT WITH csv" )
18
+ buf = nil
19
+
20
+ # #get_copy_data returns a row if there's a whole one to return, false
21
+ # if there isn't one but the COPY is still running, or nil when it's
22
+ # finished.
23
+ begin
24
+ $stderr.puts "COPY loop"
25
+ conn.consume_input
26
+ while conn.is_busy
27
+ $stderr.puts " ready loop"
28
+ select( [socket], nil, nil, 5.0 ) or
29
+ raise "Timeout (5s) waiting for query response."
30
+ conn.consume_input
31
+ end
32
+
33
+ buf = conn.get_copy_data
34
+ $stdout.puts( buf ) if buf
35
+ end until buf.nil?
36
+ end
37
+
38
+ conn.finish
39
+
@@ -0,0 +1,56 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'ysql'
4
+
5
+ $stdout.sync = true
6
+
7
+ # This is a example of how to mix and match synchronous and async APIs. In this case,
8
+ # the connection to the server is made syncrhonously, and then queries are
9
+ # asynchronous.
10
+
11
+ TIMEOUT = 5.0 # seconds to wait for an async operation to complete
12
+ CONN_OPTS = {
13
+ :host => 'localhost',
14
+ :dbname => 'test',
15
+ }
16
+
17
+ # Output progress messages
18
+ def output_progress( msg )
19
+ puts ">>> #{msg}\n"
20
+ end
21
+
22
+ # Start the (synchronous) connection
23
+ output_progress "Starting connection..."
24
+ conn = YSQL.connect(CONN_OPTS ) or abort "Unable to create a new connection!"
25
+
26
+ abort "Connect failed: %s" % [ conn.error_message ] unless conn.status == YSQL::CONNECTION_OK
27
+
28
+ # Now grab a reference to the underlying socket to select() on while the query is running
29
+ socket = conn.socket_io
30
+
31
+ # Send the (asynchronous) query
32
+ output_progress "Sending query"
33
+ conn.send_query( "SELECT * FROM pg_stat_activity" )
34
+
35
+ # Fetch results until there aren't any more
36
+ loop do
37
+ output_progress " waiting for a response"
38
+
39
+ # Buffer any incoming data on the socket until a full result is ready.
40
+ conn.consume_input
41
+ while conn.is_busy
42
+ output_progress " waiting for data to be available on %p..." % [ socket ]
43
+ select( [socket], nil, nil, TIMEOUT ) or
44
+ raise "Timeout waiting for query response."
45
+ conn.consume_input
46
+ end
47
+
48
+ # Fetch the next result. If there isn't one, the query is finished
49
+ result = conn.get_result or break
50
+
51
+ output_progress "Query result:\n%p\n" % [ result.values ]
52
+ end
53
+
54
+ output_progress "Done."
55
+ conn.finish
56
+
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+ # vim: set nosta noet ts=4 sw=4:
3
+ # encoding: utf-8
4
+
5
+ require 'ysql'
6
+
7
+ # This is a minimal example of a function that can test an existing PG::Connection and
8
+ # reset it if necessary.
9
+
10
+ def check_connection( conn )
11
+ begin
12
+ conn.exec( "SELECT 1" )
13
+ rescue YSQL::Error => err
14
+ $stderr.puts "%p while testing connection: %s" % [ err.class, err.message ]
15
+ conn.reset
16
+ end
17
+ end
18
+
19
+ conn = YSQL.connect(dbname: 'test' )
20
+ check_connection( conn )
21
+