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
@@ -0,0 +1,997 @@
1
+ # -*- ruby -*-
2
+ # frozen_string_literal: true
3
+
4
+ require 'ysql' unless defined?( YSQL )
5
+ require 'io/wait' unless ::IO.public_instance_methods(false).include?(:wait_readable)
6
+ require 'socket'
7
+ require_relative 'load_balance_service'
8
+
9
+ # The PostgreSQL connection class. The interface for this class is based on
10
+ # {libpq}[http://www.postgresql.org/docs/current/libpq.html], the C
11
+ # application programmer's interface to PostgreSQL. Some familiarity with libpq
12
+ # is recommended, but not necessary.
13
+ #
14
+ # For example, to send query to the database on the localhost:
15
+ #
16
+ # require 'pg'
17
+ # conn = PG::Connection.open(:dbname => 'test')
18
+ # res = conn.exec_params('SELECT $1 AS a, $2 AS b, $3 AS c', [1, 2, nil])
19
+ # # Equivalent to:
20
+ # # res = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
21
+ #
22
+ # See the PG::Result class for information on working with the results of a query.
23
+ #
24
+ # Many methods of this class have three variants kind of:
25
+ # 1. #exec - the base method which is an alias to #async_exec .
26
+ # This is the method that should be used in general.
27
+ # 2. #async_exec - the async aware version of the method, implemented by libpq's async API.
28
+ # 3. #sync_exec - the method version that is implemented by blocking function(s) of libpq.
29
+ #
30
+ # Sync and async version of the method can be switched by Connection.async_api= , however it is not recommended to change the default.
31
+ class YSQL::Connection
32
+
33
+ # The order the options are passed to the ::connect method.
34
+ CONNECT_ARGUMENT_ORDER = %w[host port options tty dbname user password load_balance topology_keys yb_servers_refresh_interval fallback_to_topology_keys_only failed_host_reconnect_delay_secs].freeze
35
+ private_constant :CONNECT_ARGUMENT_ORDER
36
+
37
+ ### Quote a single +value+ for use in a connection-parameter string.
38
+ def self.quote_connstr( value )
39
+ return "'" + value.to_s.gsub( /[\\']/ ) {|m| '\\' + m } + "'"
40
+ end
41
+
42
+ # Convert Hash options to connection String
43
+ #
44
+ # Values are properly quoted and escaped.
45
+ def self.connect_hash_to_string( hash )
46
+ hash.map { |k,v| "#{k}=#{quote_connstr(v)}" }.join( ' ' )
47
+ end
48
+
49
+ # Shareable program name for Ractor
50
+ PROGRAM_NAME = $PROGRAM_NAME.dup.freeze
51
+ private_constant :PROGRAM_NAME
52
+
53
+ # Parse the connection +args+ into a connection-parameter string.
54
+ # See PG::Connection.new for valid arguments.
55
+ #
56
+ # It accepts:
57
+ # * an option String kind of "host=name port=5432"
58
+ # * an option Hash kind of {host: "name", port: 5432}
59
+ # * URI string
60
+ # * URI object
61
+ # * positional arguments
62
+ #
63
+ # The method adds the option "fallback_application_name" if it isn't already set.
64
+ # It returns a connection string with "key=value" pairs.
65
+ def self.parse_connect_args( *args )
66
+ conn_info, _ = parse_connect_args_and_return_lb_props(*args)
67
+ conn_info
68
+ end
69
+
70
+ def self.parse_connect_args_and_return_lb_props( *args )
71
+ hash_arg = args.last.is_a?( Hash ) ? args.pop.transform_keys(&:to_sym) : {}
72
+ iopts = {}
73
+ if not hash_arg.empty? and not hash_arg.key?(:port)
74
+ hash_arg[:port] = 5433
75
+ end
76
+
77
+ lb_props = {}
78
+ if args.length == 1
79
+ case args.first.to_s
80
+ when /=/, /:\/\//
81
+ # Option or URL string style
82
+ conn_string = args.first.to_s
83
+ # extract and parse lb properties from conn_string
84
+ conn_string, lb_props = YSQL::LoadBalanceService.parse_lb_args_from_url conn_string
85
+ iopts = YSQL::Connection.conninfo_parse(conn_string).each_with_object({}){|h, o| o[h[:keyword].to_sym] = h[:val] if h[:val] }
86
+ else
87
+ # Positional parameters (only host given)
88
+ iopts[CONNECT_ARGUMENT_ORDER.first.to_sym] = args.first
89
+ end
90
+ else
91
+ # Positional parameters with host and more
92
+ max = CONNECT_ARGUMENT_ORDER.length
93
+ raise ArgumentError,
94
+ "Extra positional parameter %d: %p" % [ max + 1, args[max] ] if args.length > max
95
+
96
+ CONNECT_ARGUMENT_ORDER.zip( args ) do |(k,v)|
97
+ iopts[ k.to_sym ] = v if v
98
+ end
99
+ iopts.delete(:tty) # ignore obsolete tty parameter
100
+ end
101
+
102
+ lb_props = YSQL::LoadBalanceService.parse_connect_lb_args hash_arg unless hash_arg.empty?
103
+
104
+ iopts.merge!( hash_arg )
105
+
106
+ if !iopts[:fallback_application_name]
107
+ iopts[:fallback_application_name] = PROGRAM_NAME.sub( /^(.{30}).{4,}(.{30})$/ ){ $1+"..."+$2 }
108
+ end
109
+
110
+ return connect_hash_to_string(iopts), lb_props
111
+ end
112
+
113
+ # Return a String representation of the object suitable for debugging.
114
+ def inspect
115
+ str = self.to_s
116
+ str[-1,0] = if finished?
117
+ " finished"
118
+ else
119
+ stats = []
120
+ stats << " status=#{ YSQL.constants.grep(/CONNECTION_/).find{|c| YSQL.const_get(c) == status} }" if status != CONNECTION_OK
121
+ stats << " transaction_status=#{ YSQL.constants.grep(/PQTRANS_/).find{|c| YSQL.const_get(c) == transaction_status} }" if transaction_status != YSQL::PQTRANS_IDLE
122
+ stats << " nonblocking=#{ isnonblocking }" if isnonblocking
123
+ stats << " pipeline_status=#{ YSQL.constants.grep(/PQ_PIPELINE_/).find{|c| YSQL.const_get(c) == pipeline_status} }" if respond_to?(:pipeline_status) && pipeline_status != YSQL::PQ_PIPELINE_OFF
124
+ stats << " client_encoding=#{ get_client_encoding }" if get_client_encoding != "UTF8"
125
+ stats << " type_map_for_results=#{ type_map_for_results.to_s }" unless type_map_for_results.is_a?(YSQL::TypeMapAllStrings)
126
+ stats << " type_map_for_queries=#{ type_map_for_queries.to_s }" unless type_map_for_queries.is_a?(YSQL::TypeMapAllStrings)
127
+ stats << " encoder_for_put_copy_data=#{ encoder_for_put_copy_data.to_s }" if encoder_for_put_copy_data
128
+ stats << " decoder_for_get_copy_data=#{ decoder_for_get_copy_data.to_s }" if decoder_for_get_copy_data
129
+ " host=#{host} port=#{port} user=#{user}#{stats.join}"
130
+ end
131
+ return str
132
+ end
133
+
134
+ BinarySignature = "PGCOPY\n\377\r\n\0".b
135
+ private_constant :BinarySignature
136
+
137
+ # call-seq:
138
+ # conn.copy_data( sql [, coder] ) {|sql_result| ... } -> PG::Result
139
+ #
140
+ # Execute a copy process for transferring data to or from the server.
141
+ #
142
+ # This issues the SQL COPY command via #exec. The response to this
143
+ # (if there is no error in the command) is a PG::Result object that
144
+ # is passed to the block, bearing a status code of PGRES_COPY_OUT or
145
+ # PGRES_COPY_IN (depending on the specified copy direction).
146
+ # The application should then use #put_copy_data or #get_copy_data
147
+ # to receive or transmit data rows and should return from the block
148
+ # when finished.
149
+ #
150
+ # #copy_data returns another PG::Result object when the data transfer
151
+ # is complete. An exception is raised if some problem was encountered,
152
+ # so it isn't required to make use of any of them.
153
+ # At this point further SQL commands can be issued via #exec.
154
+ # (It is not possible to execute other SQL commands using the same
155
+ # connection while the COPY operation is in progress.)
156
+ #
157
+ # This method ensures, that the copy process is properly terminated
158
+ # in case of client side or server side failures. Therefore, in case
159
+ # of blocking mode of operation, #copy_data is preferred to raw calls
160
+ # of #put_copy_data, #get_copy_data and #put_copy_end.
161
+ #
162
+ # _coder_ can be a PG::Coder derivation
163
+ # (typically PG::TextEncoder::CopyRow or PG::TextDecoder::CopyRow).
164
+ # This enables encoding of data fields given to #put_copy_data
165
+ # or decoding of fields received by #get_copy_data.
166
+ #
167
+ # Example with CSV input format:
168
+ # conn.exec "create table my_table (a text,b text,c text,d text)"
169
+ # conn.copy_data "COPY my_table FROM STDIN CSV" do
170
+ # conn.put_copy_data "some,data,to,copy\n"
171
+ # conn.put_copy_data "more,data,to,copy\n"
172
+ # end
173
+ # This creates +my_table+ and inserts two CSV rows.
174
+ #
175
+ # The same with text format encoder PG::TextEncoder::CopyRow
176
+ # and Array input:
177
+ # enco = PG::TextEncoder::CopyRow.new
178
+ # conn.copy_data "COPY my_table FROM STDIN", enco do
179
+ # conn.put_copy_data ['some', 'data', 'to', 'copy']
180
+ # conn.put_copy_data ['more', 'data', 'to', 'copy']
181
+ # end
182
+ #
183
+ # Also PG::BinaryEncoder::CopyRow can be used to send data in binary format to the server.
184
+ # In this case copy_data generates the header and trailer data automatically:
185
+ # enco = PG::BinaryEncoder::CopyRow.new
186
+ # conn.copy_data "COPY my_table FROM STDIN (FORMAT binary)", enco do
187
+ # conn.put_copy_data ['some', 'data', 'to', 'copy']
188
+ # conn.put_copy_data ['more', 'data', 'to', 'copy']
189
+ # end
190
+ #
191
+ # Example with CSV output format:
192
+ # conn.copy_data "COPY my_table TO STDOUT CSV" do
193
+ # while row=conn.get_copy_data
194
+ # p row
195
+ # end
196
+ # end
197
+ # This prints all rows of +my_table+ to stdout:
198
+ # "some,data,to,copy\n"
199
+ # "more,data,to,copy\n"
200
+ #
201
+ # The same with text format decoder PG::TextDecoder::CopyRow
202
+ # and Array output:
203
+ # deco = PG::TextDecoder::CopyRow.new
204
+ # conn.copy_data "COPY my_table TO STDOUT", deco do
205
+ # while row=conn.get_copy_data
206
+ # p row
207
+ # end
208
+ # end
209
+ # This receives all rows of +my_table+ as ruby array:
210
+ # ["some", "data", "to", "copy"]
211
+ # ["more", "data", "to", "copy"]
212
+ #
213
+ # Also PG::BinaryDecoder::CopyRow can be used to retrieve data in binary format from the server.
214
+ # In this case the header and trailer data is processed by the decoder and the remaining +nil+ from get_copy_data is processed by copy_data, so that binary data can be processed equally to text data:
215
+ # deco = PG::BinaryDecoder::CopyRow.new
216
+ # conn.copy_data "COPY my_table TO STDOUT (FORMAT binary)", deco do
217
+ # while row=conn.get_copy_data
218
+ # p row
219
+ # end
220
+ # end
221
+ # This receives all rows of +my_table+ as ruby array:
222
+ # ["some", "data", "to", "copy"]
223
+ # ["more", "data", "to", "copy"]
224
+
225
+ def copy_data( sql, coder=nil )
226
+ raise YSQL::NotInBlockingMode.new("copy_data can not be used in nonblocking mode", connection: self) if nonblocking?
227
+ res = exec( sql )
228
+
229
+ case res.result_status
230
+ when PGRES_COPY_IN
231
+ begin
232
+ if coder && res.binary_tuples == 1
233
+ # Binary file header (11 byte signature, 32 bit flags and 32 bit extension length)
234
+ put_copy_data(BinarySignature + ("\x00" * 8))
235
+ end
236
+
237
+ if coder
238
+ old_coder = self.encoder_for_put_copy_data
239
+ self.encoder_for_put_copy_data = coder
240
+ end
241
+
242
+ yield res
243
+ rescue Exception => err
244
+ errmsg = "%s while copy data: %s" % [ err.class.name, err.message ]
245
+ begin
246
+ put_copy_end( errmsg )
247
+ rescue YSQL::Error
248
+ # Ignore error in cleanup to avoid losing original exception
249
+ end
250
+ discard_results
251
+ raise err
252
+ else
253
+ begin
254
+ self.encoder_for_put_copy_data = old_coder if coder
255
+
256
+ if coder && res.binary_tuples == 1
257
+ put_copy_data("\xFF\xFF") # Binary file trailer 16 bit "-1"
258
+ end
259
+
260
+ put_copy_end
261
+ rescue YSQL::Error => err
262
+ raise YSQL::LostCopyState.new("#{err} (probably by executing another SQL query while running a COPY command)", connection: self)
263
+ end
264
+ get_last_result
265
+ ensure
266
+ self.encoder_for_put_copy_data = old_coder if coder
267
+ end
268
+
269
+ when PGRES_COPY_OUT
270
+ begin
271
+ if coder
272
+ old_coder = self.decoder_for_get_copy_data
273
+ self.decoder_for_get_copy_data = coder
274
+ end
275
+ yield res
276
+ rescue Exception
277
+ cancel
278
+ discard_results
279
+ raise
280
+ else
281
+ if coder && res.binary_tuples == 1
282
+ # There are two end markers in binary mode: file trailer and the final nil.
283
+ # The file trailer is expected to be processed by BinaryDecoder::CopyRow and already returns nil, so that the remaining NULL from PQgetCopyData is retrieved here:
284
+ if get_copy_data
285
+ discard_results
286
+ raise YSQL::NotAllCopyDataRetrieved.new("Not all binary COPY data retrieved", connection: self)
287
+ end
288
+ end
289
+ res = get_last_result
290
+ if !res
291
+ discard_results
292
+ raise YSQL::LostCopyState.new("Lost COPY state (probably by executing another SQL query while running a COPY command)", connection: self)
293
+ elsif res.result_status != PGRES_COMMAND_OK
294
+ discard_results
295
+ raise YSQL::NotAllCopyDataRetrieved.new("Not all COPY data retrieved", connection: self)
296
+ end
297
+ res
298
+ ensure
299
+ self.decoder_for_get_copy_data = old_coder if coder
300
+ end
301
+
302
+ else
303
+ raise ArgumentError, "SQL command is no COPY statement: #{sql}"
304
+ end
305
+ end
306
+
307
+ # Backward-compatibility aliases for stuff that's moved into PG.
308
+ class << self
309
+ define_method( :isthreadsafe, &YSQL.method(:isthreadsafe) )
310
+ end
311
+
312
+ #
313
+ # call-seq:
314
+ # conn.transaction { |conn| ... } -> result of the block
315
+ #
316
+ # Executes a +BEGIN+ at the start of the block,
317
+ # and a +COMMIT+ at the end of the block, or
318
+ # +ROLLBACK+ if any exception occurs.
319
+ def transaction
320
+ rollback = false
321
+ exec "BEGIN"
322
+ yield(self)
323
+ rescue Exception
324
+ rollback = true
325
+ cancel if transaction_status == YSQL::PQTRANS_ACTIVE
326
+ block
327
+ exec "ROLLBACK"
328
+ raise
329
+ ensure
330
+ exec "COMMIT" unless rollback
331
+ end
332
+
333
+ ### Returns an array of Hashes with connection defaults. See ::conndefaults
334
+ ### for details.
335
+ def conndefaults
336
+ original = self.class.conndefaults
337
+ original << {:keyword=>"load_balance", :label=>"YB-Load-Balance", :dispchar=>"", :dispsize=>5}
338
+ original << {:keyword=>"topology_keys", :label=>"YB-Topology-Keys", :dispchar=>"", :dispsize=>64}
339
+ original << {:keyword=>"yb_servers_refresh_interval", :label=>"YB-Refresh-Interval", :dispchar=>"", :dispsize=>3}
340
+ original << {:keyword=>"fallback_to_topology_keys_only", :label=>"YB-Fallback-To-Topology-Keys-Only", :dispchar=>"", :dispsize=>5}
341
+ original << {:keyword=>"failed_host_reconnect_delay_secs", :label=>"YB-Failed-Host-Reconnect-Delay", :dispchar=>"", :dispsize=>3}
342
+
343
+ original
344
+ end
345
+
346
+ ### Return the Postgres connection defaults structure as a Hash keyed by option
347
+ ### keyword (as a Symbol).
348
+ ###
349
+ ### See also #conndefaults
350
+ def self.conndefaults_hash
351
+ return self.conndefaults.each_with_object({}) do |info, hash|
352
+ hash[ info[:keyword].to_sym ] = info[:val]
353
+ end
354
+ end
355
+
356
+ ### Returns a Hash with connection defaults. See ::conndefaults_hash
357
+ ### for details.
358
+ def conndefaults_hash
359
+ return self.class.conndefaults_hash
360
+ end
361
+
362
+ ### Return the Postgres connection info structure as a Hash keyed by option
363
+ ### keyword (as a Symbol).
364
+ ###
365
+ ### See also #conninfo
366
+ def conninfo_hash
367
+ return self.conninfo.each_with_object({}) do |info, hash|
368
+ hash[ info[:keyword].to_sym ] = info[:val]
369
+ end
370
+ end
371
+
372
+ # Method 'ssl_attribute' was introduced in PostgreSQL 9.5.
373
+ if self.instance_methods.find{|m| m.to_sym == :ssl_attribute }
374
+ # call-seq:
375
+ # conn.ssl_attributes -> Hash<String,String>
376
+ #
377
+ # Returns SSL-related information about the connection as key/value pairs
378
+ #
379
+ # The available attributes varies depending on the SSL library being used,
380
+ # and the type of connection.
381
+ #
382
+ # See also #ssl_attribute
383
+ def ssl_attributes
384
+ ssl_attribute_names.each.with_object({}) do |n,h|
385
+ h[n] = ssl_attribute(n)
386
+ end
387
+ end
388
+ end
389
+
390
+ # Read all pending socket input to internal memory and raise an exception in case of errors.
391
+ #
392
+ # This verifies that the connection socket is in a usable state and not aborted in any way.
393
+ # No communication is done with the server.
394
+ # Only pending data is read from the socket - the method doesn't wait for any outstanding server answers.
395
+ #
396
+ # Raises a kind of PG::Error if there was an error reading the data or if the socket is in a failure state.
397
+ #
398
+ # The method doesn't verify that the server is still responding.
399
+ # To verify that the communication to the server works, it is recommended to use something like <tt>conn.exec('')</tt> instead.
400
+ def check_socket
401
+ while socket_io.wait_readable(0)
402
+ consume_input
403
+ end
404
+ nil
405
+ end
406
+
407
+ # call-seq:
408
+ # conn.get_result() -> PG::Result
409
+ # conn.get_result() {|pg_result| block }
410
+ #
411
+ # Blocks waiting for the next result from a call to
412
+ # #send_query (or another asynchronous command), and returns
413
+ # it. Returns +nil+ if no more results are available.
414
+ #
415
+ # Note: call this function repeatedly until it returns +nil+, or else
416
+ # you will not be able to issue further commands.
417
+ #
418
+ # If the optional code block is given, it will be passed <i>result</i> as an argument,
419
+ # and the PG::Result object will automatically be cleared when the block terminates.
420
+ # In this instance, <code>conn.exec</code> returns the value of the block.
421
+ def get_result
422
+ block
423
+ sync_get_result
424
+ end
425
+ alias async_get_result get_result
426
+
427
+ # call-seq:
428
+ # conn.get_copy_data( [ nonblock = false [, decoder = nil ]] ) -> Object
429
+ #
430
+ # Return one row of data, +nil+
431
+ # if the copy is done, or +false+ if the call would
432
+ # block (only possible if _nonblock_ is true).
433
+ #
434
+ # If _decoder_ is not set or +nil+, data is returned as binary string.
435
+ #
436
+ # If _decoder_ is set to a PG::Coder derivation, the return type depends on this decoder.
437
+ # PG::TextDecoder::CopyRow decodes the received data fields from one row of PostgreSQL's
438
+ # COPY text format to an Array of Strings.
439
+ # Optionally the decoder can type cast the single fields to various Ruby types in one step,
440
+ # if PG::TextDecoder::CopyRow#type_map is set accordingly.
441
+ #
442
+ # See also #copy_data.
443
+ #
444
+ def get_copy_data(async=false, decoder=nil)
445
+ if async
446
+ return sync_get_copy_data(async, decoder)
447
+ else
448
+ while (res=sync_get_copy_data(true, decoder)) == false
449
+ socket_io.wait_readable
450
+ consume_input
451
+ end
452
+ return res
453
+ end
454
+ end
455
+ alias async_get_copy_data get_copy_data
456
+
457
+
458
+ # In async_api=true mode (default) all send calls run nonblocking.
459
+ # The difference is that setnonblocking(true) disables automatic handling of would-block cases.
460
+ # In async_api=false mode all send calls run directly on libpq.
461
+ # Blocking vs. nonblocking state can be changed in libpq.
462
+
463
+ # call-seq:
464
+ # conn.setnonblocking(Boolean) -> nil
465
+ #
466
+ # Sets the nonblocking status of the connection.
467
+ # In the blocking state, calls to #send_query
468
+ # will block until the message is sent to the server,
469
+ # but will not wait for the query results.
470
+ # In the nonblocking state, calls to #send_query
471
+ # will return an error if the socket is not ready for
472
+ # writing.
473
+ # Note: This function does not affect #exec, because
474
+ # that function doesn't return until the server has
475
+ # processed the query and returned the results.
476
+ #
477
+ # Returns +nil+.
478
+ def setnonblocking(enabled)
479
+ singleton_class.async_send_api = !enabled
480
+ self.flush_data = !enabled
481
+ sync_setnonblocking(true)
482
+ end
483
+ alias async_setnonblocking setnonblocking
484
+
485
+ # sync/async isnonblocking methods are switched by async_setnonblocking()
486
+
487
+ # call-seq:
488
+ # conn.isnonblocking() -> Boolean
489
+ #
490
+ # Returns the blocking status of the database connection.
491
+ # Returns +true+ if the connection is set to nonblocking mode and +false+ if blocking.
492
+ def isnonblocking
493
+ false
494
+ end
495
+ alias async_isnonblocking isnonblocking
496
+ alias nonblocking? isnonblocking
497
+
498
+ # call-seq:
499
+ # conn.put_copy_data( buffer [, encoder] ) -> Boolean
500
+ #
501
+ # Transmits _buffer_ as copy data to the server.
502
+ # Returns true if the data was sent, false if it was
503
+ # not sent (false is only possible if the connection
504
+ # is in nonblocking mode, and this command would block).
505
+ #
506
+ # _encoder_ can be a PG::Coder derivation (typically PG::TextEncoder::CopyRow).
507
+ # This encodes the data fields given as _buffer_ from an Array of Strings to
508
+ # PostgreSQL's COPY text format inclusive proper escaping. Optionally
509
+ # the encoder can type cast the fields from various Ruby types in one step,
510
+ # if PG::TextEncoder::CopyRow#type_map is set accordingly.
511
+ #
512
+ # Raises an exception if an error occurs.
513
+ #
514
+ # See also #copy_data.
515
+ #
516
+ def put_copy_data(buffer, encoder=nil)
517
+ # sync_put_copy_data does a non-blocking attept to flush data.
518
+ until res=sync_put_copy_data(buffer, encoder)
519
+ # It didn't flush immediately and allocation of more buffering memory failed.
520
+ # Wait for all data sent by doing a blocking flush.
521
+ res = flush
522
+ end
523
+
524
+ # And do a blocking flush every 100 calls.
525
+ # This is to avoid memory bloat, when sending the data is slower than calls to put_copy_data happen.
526
+ if (@calls_to_put_copy_data += 1) > 100
527
+ @calls_to_put_copy_data = 0
528
+ res = flush
529
+ end
530
+ res
531
+ end
532
+ alias async_put_copy_data put_copy_data
533
+
534
+ # call-seq:
535
+ # conn.put_copy_end( [ error_message ] ) -> Boolean
536
+ #
537
+ # Sends end-of-data indication to the server.
538
+ #
539
+ # _error_message_ is an optional parameter, and if set,
540
+ # forces the COPY command to fail with the string
541
+ # _error_message_.
542
+ #
543
+ # Returns true if the end-of-data was sent, #false* if it was
544
+ # not sent (*false* is only possible if the connection
545
+ # is in nonblocking mode, and this command would block).
546
+ def put_copy_end(*args)
547
+ until sync_put_copy_end(*args)
548
+ flush
549
+ end
550
+ @calls_to_put_copy_data = 0
551
+ flush
552
+ end
553
+ alias async_put_copy_end put_copy_end
554
+
555
+ if method_defined? :sync_encrypt_password
556
+ # call-seq:
557
+ # conn.encrypt_password( password, username, algorithm=nil ) -> String
558
+ #
559
+ # This function is intended to be used by client applications that wish to send commands like <tt>ALTER USER joe PASSWORD 'pwd'</tt>.
560
+ # It is good practice not to send the original cleartext password in such a command, because it might be exposed in command logs, activity displays, and so on.
561
+ # Instead, use this function to convert the password to encrypted form before it is sent.
562
+ #
563
+ # The +password+ and +username+ arguments are the cleartext password, and the SQL name of the user it is for.
564
+ # +algorithm+ specifies the encryption algorithm to use to encrypt the password.
565
+ # Currently supported algorithms are +md5+ and +scram-sha-256+ (+on+ and +off+ are also accepted as aliases for +md5+, for compatibility with older server versions).
566
+ # Note that support for +scram-sha-256+ was introduced in PostgreSQL version 10, and will not work correctly with older server versions.
567
+ # If algorithm is omitted or +nil+, this function will query the server for the current value of the +password_encryption+ setting.
568
+ # That can block, and will fail if the current transaction is aborted, or if the connection is busy executing another query.
569
+ # If you wish to use the default algorithm for the server but want to avoid blocking, query +password_encryption+ yourself before calling #encrypt_password, and pass that value as the algorithm.
570
+ #
571
+ # Return value is the encrypted password.
572
+ # The caller can assume the string doesn't contain any special characters that would require escaping.
573
+ #
574
+ # Available since PostgreSQL-10.
575
+ # See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-misc.html#LIBPQ-PQENCRYPTPASSWORDCONN].
576
+ def encrypt_password( password, username, algorithm=nil )
577
+ algorithm ||= exec("SHOW password_encryption").getvalue(0,0)
578
+ sync_encrypt_password(password, username, algorithm)
579
+ end
580
+ alias async_encrypt_password encrypt_password
581
+ end
582
+
583
+ # call-seq:
584
+ # conn.reset()
585
+ #
586
+ # Resets the backend connection. This method closes the
587
+ # backend connection and tries to re-connect.
588
+ def reset
589
+ iopts = conninfo_hash.compact
590
+ if iopts[:host] && !iopts[:host].empty? && YSQL.library_version >= 100000
591
+ iopts = self.class.send(:resolve_hosts, iopts)
592
+ end
593
+ conninfo = self.class.parse_connect_args( iopts );
594
+ reset_start2(conninfo)
595
+ async_connect_or_reset(:reset_poll)
596
+ self
597
+ end
598
+ alias async_reset reset
599
+
600
+ # call-seq:
601
+ # conn.cancel() -> String
602
+ #
603
+ # Requests cancellation of the command currently being
604
+ # processed.
605
+ #
606
+ # Returns +nil+ on success, or a string containing the
607
+ # error message if a failure occurs.
608
+ def cancel
609
+ be_pid = backend_pid
610
+ be_key = backend_key
611
+ cancel_request = [0x10, 1234, 5678, be_pid, be_key].pack("NnnNN")
612
+
613
+ if Fiber.respond_to?(:scheduler) && Fiber.scheduler && RUBY_PLATFORM =~ /mingw|mswin/
614
+ # Ruby's nonblocking IO is not really supported on Windows.
615
+ # We work around by using threads and explicit calls to wait_readable/wait_writable.
616
+ cl = Thread.new(socket_io.remote_address) { |ra| ra.connect }.value
617
+ begin
618
+ cl.write_nonblock(cancel_request)
619
+ rescue IO::WaitReadable, Errno::EINTR
620
+ cl.wait_writable
621
+ retry
622
+ end
623
+ begin
624
+ cl.read_nonblock(1)
625
+ rescue IO::WaitReadable, Errno::EINTR
626
+ cl.wait_readable
627
+ retry
628
+ rescue EOFError
629
+ end
630
+ elsif RUBY_ENGINE == 'truffleruby'
631
+ begin
632
+ cl = socket_io.remote_address.connect
633
+ rescue NotImplementedError
634
+ # Workaround for truffleruby < 21.3.0
635
+ cl2 = Socket.for_fd(socket_io.fileno)
636
+ cl2.autoclose = false
637
+ adr = cl2.remote_address
638
+ if adr.ip?
639
+ cl = TCPSocket.new(adr.ip_address, adr.ip_port)
640
+ cl.autoclose = false
641
+ else
642
+ cl = UNIXSocket.new(adr.unix_path)
643
+ cl.autoclose = false
644
+ end
645
+ end
646
+ cl.write(cancel_request)
647
+ cl.read(1)
648
+ else
649
+ cl = socket_io.remote_address.connect
650
+ # Send CANCEL_REQUEST_CODE and parameters
651
+ cl.write(cancel_request)
652
+ # Wait for the postmaster to close the connection, which indicates that it's processed the request.
653
+ cl.read(1)
654
+ end
655
+
656
+ cl.close
657
+ nil
658
+ rescue SystemCallError => err
659
+ err.to_s
660
+ end
661
+ alias async_cancel cancel
662
+
663
+ private def async_connect_or_reset(poll_meth)
664
+ # Track the progress of the connection, waiting for the socket to become readable/writable before polling it
665
+
666
+ if (timeo = conninfo_hash[:connect_timeout].to_i) && timeo > 0
667
+ # Lowest timeout is 2 seconds - like in libpq
668
+ timeo = [timeo, 2].max
669
+ host_count = conninfo_hash[:host].to_s.count(",") + 1
670
+ stop_time = timeo * host_count + Process.clock_gettime(Process::CLOCK_MONOTONIC)
671
+ end
672
+
673
+ poll_status = YSQL::PGRES_POLLING_WRITING
674
+ until poll_status == YSQL::PGRES_POLLING_OK ||
675
+ poll_status == YSQL::PGRES_POLLING_FAILED
676
+
677
+ # Set single timeout to parameter "connect_timeout" but
678
+ # don't exceed total connection time of number-of-hosts * connect_timeout.
679
+ timeout = [timeo, stop_time - Process.clock_gettime(Process::CLOCK_MONOTONIC)].min if stop_time
680
+ event = if !timeout || timeout >= 0
681
+ # If the socket needs to read, wait 'til it becomes readable to poll again
682
+ case poll_status
683
+ when YSQL::PGRES_POLLING_READING
684
+ if defined?(IO::READABLE) # ruby-3.0+
685
+ socket_io.wait(IO::READABLE | IO::PRIORITY, timeout)
686
+ else
687
+ IO.select([socket_io], nil, [socket_io], timeout)
688
+ end
689
+
690
+ # ...and the same for when the socket needs to write
691
+ when YSQL::PGRES_POLLING_WRITING
692
+ if defined?(IO::WRITABLE) # ruby-3.0+
693
+ # Use wait instead of wait_readable, since connection errors are delivered as
694
+ # exceptional/priority events on Windows.
695
+ socket_io.wait(IO::WRITABLE | IO::PRIORITY, timeout)
696
+ else
697
+ # io#wait on ruby-2.x doesn't wait for priority, so fallback to IO.select
698
+ IO.select(nil, [socket_io], [socket_io], timeout)
699
+ end
700
+ end
701
+ end
702
+ # connection to server at "localhost" (127.0.0.1), port 5433 failed: timeout expired (PG::ConnectionBad)
703
+ # connection to server on socket "/var/run/postgresql/.s.PGSQL.5433" failed: No such file or directory
704
+ unless event
705
+ if self.class.send(:host_is_named_pipe?, host)
706
+ connhost = "on socket \"#{host}\""
707
+ elsif respond_to?(:hostaddr)
708
+ connhost = "at \"#{host}\" (#{hostaddr}), port #{port}"
709
+ else
710
+ connhost = "at \"#{host}\", port #{port}"
711
+ end
712
+ raise YSQL::ConnectionBad.new("connection to server #{connhost} failed: timeout expired", connection: self)
713
+ end
714
+
715
+ # Check to see if it's finished or failed yet
716
+ poll_status = send( poll_meth )
717
+ end
718
+
719
+ unless status == YSQL::CONNECTION_OK
720
+ msg = error_message
721
+ finish
722
+ raise YSQL::ConnectionBad.new(msg, connection: self)
723
+ end
724
+
725
+ # Set connection to nonblocking to handle all blocking states in ruby.
726
+ # That way a fiber scheduler is able to handle IO requests.
727
+ sync_setnonblocking(true)
728
+ self.flush_data = true
729
+ set_default_encoding
730
+ end
731
+
732
+ class << self
733
+ # call-seq:
734
+ # PG::Connection.new -> conn
735
+ # PG::Connection.new(connection_hash) -> conn
736
+ # PG::Connection.new(connection_string) -> conn
737
+ # PG::Connection.new(host, port, options, tty, dbname, user, password) -> conn
738
+ #
739
+ # Create a connection to the specified server.
740
+ #
741
+ # +connection_hash+ must be a ruby Hash with connection parameters.
742
+ # See the {list of valid parameters}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS] in the PostgreSQL documentation.
743
+ #
744
+ # There are two accepted formats for +connection_string+: plain <code>keyword = value</code> strings and URIs.
745
+ # See the documentation of {connection strings}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING].
746
+ #
747
+ # The positional parameter form has the same functionality except that the missing parameters will always take on default values. The parameters are:
748
+ # [+host+]
749
+ # server hostname
750
+ # [+port+]
751
+ # server port number
752
+ # [+options+]
753
+ # backend options
754
+ # [+tty+]
755
+ # (ignored in all versions of PostgreSQL)
756
+ # [+dbname+]
757
+ # connecting database name
758
+ # [+user+]
759
+ # login user name
760
+ # [+password+]
761
+ # login password
762
+ #
763
+ # Examples:
764
+ #
765
+ # # Connect using all defaults
766
+ # PG::Connection.new
767
+ #
768
+ # # As a Hash
769
+ # PG::Connection.new( dbname: 'test', port: 5432 )
770
+ #
771
+ # # As a String
772
+ # PG::Connection.new( "dbname=test port=5432" )
773
+ #
774
+ # # As an Array
775
+ # PG::Connection.new( nil, 5432, nil, nil, 'test', nil, nil )
776
+ #
777
+ # # As an URI
778
+ # PG::Connection.new( "postgresql://user:pass@pgsql.example.com:5432/testdb?sslmode=require" )
779
+ #
780
+ # If the Ruby default internal encoding is set (i.e., <code>Encoding.default_internal != nil</code>), the
781
+ # connection will have its +client_encoding+ set accordingly.
782
+ #
783
+ # Raises a PG::Error if the connection fails.
784
+ def new(*args)
785
+ conn = connect_to_hosts(*args)
786
+
787
+ if block_given?
788
+ begin
789
+ return yield conn
790
+ ensure
791
+ conn.finish
792
+ end
793
+ end
794
+ conn
795
+ end
796
+ alias async_connect new
797
+ alias connect new
798
+ alias open new
799
+ alias setdb new
800
+ alias setdblogin new
801
+
802
+ # Resolve DNS in Ruby to avoid blocking state while connecting.
803
+ # Multiple comma-separated values are generated, if the hostname resolves to both IPv4 and IPv6 addresses.
804
+ # This requires PostgreSQL-10+, so no DNS resolving is done on earlier versions.
805
+ private def resolve_hosts(iopts)
806
+ ihosts = iopts[:host].split(",", -1)
807
+ iports = iopts[:port].split(",", -1)
808
+ iports = [nil] if iports.size == 0
809
+ iports = iports * ihosts.size if iports.size == 1
810
+ raise YSQL::ConnectionBad, "could not match #{iports.size} port numbers to #{ihosts.size} hosts" if iports.size != ihosts.size
811
+
812
+ dests = ihosts.each_with_index.flat_map do |mhost, idx|
813
+ unless host_is_named_pipe?(mhost)
814
+ if Fiber.respond_to?(:scheduler) &&
815
+ Fiber.scheduler &&
816
+ RUBY_VERSION < '3.1.'
817
+
818
+ # Use a second thread to avoid blocking of the scheduler.
819
+ # `TCPSocket.gethostbyname` isn't fiber aware before ruby-3.1.
820
+ hostaddrs = Thread.new{ Addrinfo.getaddrinfo(mhost, nil, nil, :STREAM).map(&:ip_address) rescue [''] }.value
821
+ else
822
+ hostaddrs = Addrinfo.getaddrinfo(mhost, nil, nil, :STREAM).map(&:ip_address) rescue ['']
823
+ end
824
+ else
825
+ # No hostname to resolve (UnixSocket)
826
+ hostaddrs = [nil]
827
+ end
828
+ hostaddrs.map { |hostaddr| [hostaddr, mhost, iports[idx]] }
829
+ end
830
+ iopts.merge(
831
+ hostaddr: dests.map{|d| d[0] }.join(","),
832
+ host: dests.map{|d| d[1] }.join(","),
833
+ port: dests.map{|d| d[2] }.join(","))
834
+ end
835
+
836
+ private def connect_to_hosts(*args)
837
+ option_string, lb_properties = parse_connect_args_and_return_lb_props(*args)
838
+ iopts = YSQL::Connection.conninfo_parse(option_string).each_with_object({}){|h, o| o[h[:keyword].to_sym] = h[:val] if h[:val] }
839
+ iopts = YSQL::Connection.conndefaults.each_with_object({}){|h, o| o[h[:keyword].to_sym] = h[:val] if h[:val] }.merge(iopts)
840
+ original_host = iopts[:host]
841
+ original_port = iopts[:port]
842
+
843
+ if lb_properties
844
+ connection = YSQL::LoadBalanceService.connect_to_lb_hosts(lb_properties, iopts)
845
+ end
846
+ if connection.nil?
847
+ if lb_properties
848
+ iopts[:host] = original_host
849
+ iopts[:port] = original_port
850
+ end
851
+ connection = do_connect_to_hosts(iopts)
852
+ end
853
+ connection
854
+ end
855
+
856
+ def do_connect_to_hosts(iopts)
857
+ if iopts[:hostaddr]
858
+ # hostaddr is provided -> no need to resolve hostnames
859
+
860
+ elsif iopts[:host] && !iopts[:host].empty? && YSQL.library_version >= 100000
861
+ iopts = resolve_hosts(iopts)
862
+ else
863
+ # No host given
864
+ end
865
+ conn = self.connect_start(iopts) or
866
+ raise(YSQL::Error, "Unable to create a new connection")
867
+
868
+ raise YSQL::ConnectionBad, conn.error_message if conn.status == YSQL::CONNECTION_BAD
869
+
870
+ conn.send(:async_connect_or_reset, :connect_poll)
871
+ conn
872
+ end
873
+
874
+ def host_is_named_pipe?(host_string)
875
+ host_string.empty? || host_string.start_with?("/") || # it's UnixSocket?
876
+ host_string.start_with?("@") || # it's UnixSocket in the abstract namespace?
877
+ # it's a path on Windows?
878
+ (RUBY_PLATFORM =~ /mingw|mswin/ && host_string =~ /\A([\/\\]|\w:[\/\\])/)
879
+ end
880
+
881
+ # call-seq:
882
+ # PG::Connection.ping(connection_hash) -> Integer
883
+ # PG::Connection.ping(connection_string) -> Integer
884
+ # PG::Connection.ping(host, port, options, tty, dbname, login, password) -> Integer
885
+ #
886
+ # PQpingParams reports the status of the server.
887
+ #
888
+ # It accepts connection parameters identical to those of PQ::Connection.new .
889
+ # It is not necessary to supply correct user name, password, or database name values to obtain the server status; however, if incorrect values are provided, the server will log a failed connection attempt.
890
+ #
891
+ # See PG::Connection.new for a description of the parameters.
892
+ #
893
+ # Returns one of:
894
+ # [+PQPING_OK+]
895
+ # server is accepting connections
896
+ # [+PQPING_REJECT+]
897
+ # server is alive but rejecting connections
898
+ # [+PQPING_NO_RESPONSE+]
899
+ # could not establish connection
900
+ # [+PQPING_NO_ATTEMPT+]
901
+ # connection not attempted (bad params)
902
+ #
903
+ # See also check_socket for a way to check the connection without doing any server communication.
904
+ def ping(*args)
905
+ if Fiber.respond_to?(:scheduler) && Fiber.scheduler
906
+ # Run PQping in a second thread to avoid blocking of the scheduler.
907
+ # Unfortunately there's no nonblocking way to run ping.
908
+ Thread.new { sync_ping(*args) }.value
909
+ else
910
+ sync_ping(*args)
911
+ end
912
+ end
913
+ alias async_ping ping
914
+
915
+ REDIRECT_CLASS_METHODS = YSQL.make_shareable({
916
+ :new => [:async_connect, :sync_connect],
917
+ :connect => [:async_connect, :sync_connect],
918
+ :open => [:async_connect, :sync_connect],
919
+ :setdb => [:async_connect, :sync_connect],
920
+ :setdblogin => [:async_connect, :sync_connect],
921
+ :ping => [:async_ping, :sync_ping],
922
+ })
923
+ private_constant :REDIRECT_CLASS_METHODS
924
+
925
+ # These methods are affected by PQsetnonblocking
926
+ REDIRECT_SEND_METHODS = YSQL.make_shareable({
927
+ :isnonblocking => [:async_isnonblocking, :sync_isnonblocking],
928
+ :nonblocking? => [:async_isnonblocking, :sync_isnonblocking],
929
+ :put_copy_data => [:async_put_copy_data, :sync_put_copy_data],
930
+ :put_copy_end => [:async_put_copy_end, :sync_put_copy_end],
931
+ :flush => [:async_flush, :sync_flush],
932
+ })
933
+ private_constant :REDIRECT_SEND_METHODS
934
+ REDIRECT_METHODS = {
935
+ :exec => [:async_exec, :sync_exec],
936
+ :query => [:async_exec, :sync_exec],
937
+ :exec_params => [:async_exec_params, :sync_exec_params],
938
+ :prepare => [:async_prepare, :sync_prepare],
939
+ :exec_prepared => [:async_exec_prepared, :sync_exec_prepared],
940
+ :describe_portal => [:async_describe_portal, :sync_describe_portal],
941
+ :describe_prepared => [:async_describe_prepared, :sync_describe_prepared],
942
+ :setnonblocking => [:async_setnonblocking, :sync_setnonblocking],
943
+ :get_result => [:async_get_result, :sync_get_result],
944
+ :get_last_result => [:async_get_last_result, :sync_get_last_result],
945
+ :get_copy_data => [:async_get_copy_data, :sync_get_copy_data],
946
+ :reset => [:async_reset, :sync_reset],
947
+ :set_client_encoding => [:async_set_client_encoding, :sync_set_client_encoding],
948
+ :client_encoding= => [:async_set_client_encoding, :sync_set_client_encoding],
949
+ :cancel => [:async_cancel, :sync_cancel],
950
+ }
951
+ private_constant :REDIRECT_METHODS
952
+
953
+ if YSQL::Connection.instance_methods.include? :async_encrypt_password
954
+ REDIRECT_METHODS.merge!({
955
+ :encrypt_password => [:async_encrypt_password, :sync_encrypt_password],
956
+ })
957
+ end
958
+ YSQL.make_shareable(REDIRECT_METHODS)
959
+
960
+ def async_send_api=(enable)
961
+ REDIRECT_SEND_METHODS.each do |ali, (async, sync)|
962
+ undef_method(ali) if method_defined?(ali)
963
+ alias_method( ali, enable ? async : sync )
964
+ end
965
+ end
966
+
967
+ # Switch between sync and async libpq API.
968
+ #
969
+ # PG::Connection.async_api = true
970
+ # this is the default.
971
+ # It sets an alias from #exec to #async_exec, #reset to #async_reset and so on.
972
+ #
973
+ # PG::Connection.async_api = false
974
+ # sets an alias from #exec to #sync_exec, #reset to #sync_reset and so on.
975
+ #
976
+ # pg-1.1.0+ defaults to libpq's async API for query related blocking methods.
977
+ # pg-1.3.0+ defaults to libpq's async API for all possibly blocking methods.
978
+ #
979
+ # _PLEASE_ _NOTE_: This method is not part of the public API and is for debug and development use only.
980
+ # Do not use this method in production code.
981
+ # Any issues with the default setting of <tt>async_api=true</tt> should be reported to the maintainers instead.
982
+ #
983
+ def async_api=(enable)
984
+ self.async_send_api = enable
985
+ REDIRECT_METHODS.each do |ali, (async, sync)|
986
+ remove_method(ali) if method_defined?(ali)
987
+ alias_method( ali, enable ? async : sync )
988
+ end
989
+ REDIRECT_CLASS_METHODS.each do |ali, (async, sync)|
990
+ singleton_class.remove_method(ali) if method_defined?(ali)
991
+ singleton_class.alias_method(ali, enable ? async : sync )
992
+ end
993
+ end
994
+ end
995
+
996
+ self.async_api = true
997
+ end # class PG::Connection