pg 1.5.9 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/{History.md → CHANGELOG.md} +126 -0
  4. data/Gemfile +11 -7
  5. data/README-Windows.rdoc +1 -1
  6. data/README.ja.md +83 -32
  7. data/README.md +79 -29
  8. data/Rakefile +84 -19
  9. data/certs/larskanis-2026.pem +24 -0
  10. data/ext/errorcodes.def +9 -0
  11. data/ext/errorcodes.rb +1 -1
  12. data/ext/errorcodes.txt +8 -1
  13. data/ext/extconf.rb +194 -30
  14. data/ext/gvl_wrappers.c +13 -2
  15. data/ext/gvl_wrappers.h +47 -134
  16. data/ext/pg.c +23 -16
  17. data/ext/pg.h +23 -24
  18. data/ext/pg_binary_decoder.c +157 -3
  19. data/ext/pg_binary_encoder.c +219 -9
  20. data/ext/pg_cancel_connection.c +360 -0
  21. data/ext/pg_coder.c +58 -9
  22. data/ext/pg_connection.c +558 -291
  23. data/ext/pg_copy_coder.c +36 -23
  24. data/ext/pg_record_coder.c +11 -7
  25. data/ext/pg_result.c +144 -76
  26. data/ext/pg_text_decoder.c +7 -7
  27. data/ext/pg_text_encoder.c +29 -11
  28. data/ext/pg_tuple.c +9 -9
  29. data/ext/pg_type_map.c +45 -3
  30. data/ext/pg_type_map_all_strings.c +3 -3
  31. data/ext/pg_type_map_by_class.c +2 -2
  32. data/ext/pg_type_map_by_column.c +4 -3
  33. data/ext/pg_type_map_by_mri_type.c +1 -1
  34. data/ext/pg_type_map_by_oid.c +4 -2
  35. data/ext/pg_type_map_in_ruby.c +2 -2
  36. data/ext/pg_util.c +2 -2
  37. data/ext/pg_util.h +2 -2
  38. data/lib/pg/basic_type_map_based_on_result.rb +5 -0
  39. data/lib/pg/basic_type_map_for_queries.rb +16 -5
  40. data/lib/pg/basic_type_map_for_results.rb +5 -0
  41. data/lib/pg/basic_type_registry.rb +2 -2
  42. data/lib/pg/cancel_connection.rb +53 -0
  43. data/lib/pg/coder.rb +4 -2
  44. data/lib/pg/connection.rb +393 -180
  45. data/lib/pg/text_decoder/date.rb +1 -1
  46. data/lib/pg/text_decoder/json.rb +1 -1
  47. data/lib/pg/text_encoder/date.rb +7 -1
  48. data/lib/pg/text_encoder/json.rb +1 -1
  49. data/lib/pg/version.rb +2 -1
  50. data/lib/pg.rb +156 -130
  51. data/misc/glibc/Dockerfile +20 -0
  52. data/misc/glibc/docker-compose.yml +9 -0
  53. data/misc/glibc/glibc_spec.rb +5 -0
  54. data/misc/yugabyte/Dockerfile +9 -0
  55. data/misc/yugabyte/docker-compose.yml +28 -0
  56. data/misc/yugabyte/pg-test.rb +45 -0
  57. data/pg.gemspec +5 -3
  58. data/ports/patches/krb5/1.22.2/0001-Allow-static-linking-krb5-library.patch +30 -0
  59. data/ports/patches/krb5/1.22.2/0002-unknown-command-line-option-on-clang.patch +12 -0
  60. data/ports/patches/postgresql/18.6/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +42 -0
  61. data/rakelib/pg_gem_helper.rb +64 -0
  62. data/sample/async_mixed.rb +1 -1
  63. data.tar.gz.sig +0 -0
  64. metadata +36 -22
  65. metadata.gz.sig +0 -0
  66. data/Manifest.txt +0 -72
  67. data/Rakefile.cross +0 -303
@@ -11,6 +11,8 @@
11
11
  #endif
12
12
 
13
13
  VALUE rb_mPG_BinaryEncoder;
14
+ static ID s_id_gregorianP;
15
+ static ID s_id_gregorian;
14
16
  static ID s_id_year;
15
17
  static ID s_id_month;
16
18
  static ID s_id_day;
@@ -271,14 +273,16 @@ pg_bin_enc_date(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, i
271
273
  case T_FALSE:
272
274
  write_nbo32(PG_INT32_MIN, out);
273
275
  return 4;
276
+ } {
277
+ /* Only create a new gregorian Date object if necessary */
278
+ if( rb_funcall(value, s_id_gregorianP, 0) != Qtrue )
279
+ value = rb_funcall(value, s_id_gregorian, 0);
280
+ VALUE year = rb_funcall(value, s_id_year, 0);
281
+ VALUE month = rb_funcall(value, s_id_month, 0);
282
+ VALUE day = rb_funcall(value, s_id_day, 0);
283
+ int jday = date2j(NUM2INT(year), NUM2INT(month), NUM2INT(day)) - POSTGRES_EPOCH_JDATE;
284
+ write_nbo32(jday, out);
274
285
  }
275
-
276
- VALUE year = rb_funcall(value, s_id_year, 0);
277
- VALUE month = rb_funcall(value, s_id_month, 0);
278
- VALUE day = rb_funcall(value, s_id_day, 0);
279
- int jday = date2j(NUM2INT(year), NUM2INT(month), NUM2INT(day)) - POSTGRES_EPOCH_JDATE;
280
- write_nbo32(jday, out);
281
-
282
286
  }else{
283
287
  /* first call -> determine the required length */
284
288
  if(TYPE(value) == T_STRING){
@@ -305,6 +309,208 @@ pg_bin_enc_date(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, i
305
309
  return 4;
306
310
  }
307
311
 
312
+ /*
313
+ * Maximum number of array subscripts (arbitrary limit)
314
+ */
315
+ #define MAXDIM 6
316
+
317
+ /*
318
+ * Document-class: PG::BinaryEncoder::Array < PG::CompositeEncoder
319
+ *
320
+ * This is the encoder class for PostgreSQL array types in binary format.
321
+ *
322
+ * All values are encoded according to the #elements_type
323
+ * accessor. Sub-arrays are encoded recursively.
324
+ *
325
+ * This encoder expects an Array of values or sub-arrays as input.
326
+ * Other values are passed through as byte string without interpretation.
327
+ *
328
+ * It is possible to enforce a number of dimensions to be encoded by #dimensions= .
329
+ * Deeper nested arrays are then passed to the elements encoder and less nested arrays raise an ArgumentError.
330
+ *
331
+ * The accessors needs_quotation and delimiter are ignored for binary encoding.
332
+ *
333
+ */
334
+ static int
335
+ pg_bin_enc_array(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
336
+ {
337
+ if (TYPE(value) == T_ARRAY) {
338
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
339
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this->elem);
340
+ int dim_sizes[MAXDIM];
341
+ int ndim = 1;
342
+ int nitems = 1;
343
+ VALUE el1 = value;
344
+
345
+ if (RARRAY_LEN(value) == 0) {
346
+ nitems = 0;
347
+ ndim = 0;
348
+ dim_sizes[0] = 0;
349
+ } else {
350
+ /* Determine number of dimensions, sizes of dimensions and number of items */
351
+ while(1) {
352
+ VALUE el2;
353
+
354
+ dim_sizes[ndim-1] = RARRAY_LENINT(el1);
355
+ nitems *= dim_sizes[ndim-1];
356
+ el2 = rb_ary_entry(el1, 0);
357
+ if ( (this->dimensions < 0 || ndim < this->dimensions) &&
358
+ TYPE(el2) == T_ARRAY) {
359
+ ndim++;
360
+ if (ndim > MAXDIM)
361
+ rb_raise( rb_eArgError, "unsupported number of array dimensions: >%d", ndim );
362
+ } else {
363
+ break;
364
+ }
365
+ el1 = el2;
366
+ }
367
+ }
368
+ if( this->dimensions >= 0 && (ndim==0 ? 1 : ndim) != this->dimensions ){
369
+ rb_raise(rb_eArgError, "less array dimensions to encode (%d) than expected (%d)", ndim, this->dimensions);
370
+ }
371
+
372
+ if(out){
373
+ /* Second encoder pass -> write data to `out` */
374
+ int dimpos[MAXDIM];
375
+ VALUE arrays[MAXDIM];
376
+ int dim = 0;
377
+ int item_idx = 0;
378
+ int i;
379
+ char *orig_out = out;
380
+ Oid elem_oid = this->elem ? this->elem->oid : 0;
381
+
382
+ write_nbo32(ndim, out); out += 4;
383
+ write_nbo32(1 /* flags */, out); out += 4;
384
+ write_nbo32(elem_oid, out); out += 4;
385
+ for (i = 0; i < ndim; i++) {
386
+ dimpos[i] = 0;
387
+ write_nbo32(dim_sizes[i], out); out += 4;
388
+ write_nbo32(1 /* offset */, out); out += 4;
389
+ }
390
+ arrays[0] = value;
391
+
392
+ while(1) {
393
+ /* traverse tree down */
394
+ while (dim < ndim - 1) {
395
+ arrays[dim + 1] = rb_ary_entry(arrays[dim], dimpos[dim]);
396
+ dim++;
397
+ }
398
+
399
+ for (i = 0; i < dim_sizes[dim]; i++) {
400
+ VALUE item = rb_ary_entry(arrays[dim], i);
401
+
402
+ if (NIL_P(item)) {
403
+ write_nbo32(-1, out); out += 4;
404
+ } else {
405
+ /* Encoded string is returned in subint */
406
+ int strlen;
407
+ VALUE is_one_pass = rb_ary_entry(*intermediate, item_idx++);
408
+ VALUE subint = rb_ary_entry(*intermediate, item_idx++);
409
+
410
+ if (is_one_pass == Qtrue) {
411
+ strlen = RSTRING_LENINT(subint);
412
+ memcpy( out + 4, RSTRING_PTR(subint), strlen);
413
+ } else {
414
+ strlen = enc_func(this->elem, item, out + 4, &subint, enc_idx);
415
+ }
416
+ write_nbo32(strlen, out);
417
+ out += 4 /* length */ + strlen;
418
+ }
419
+ }
420
+
421
+ /* traverse tree up and go to next sibling array */
422
+ do {
423
+ if (dim > 0) {
424
+ dimpos[dim] = 0;
425
+ dim--;
426
+ dimpos[dim]++;
427
+ } else {
428
+ goto finished2;
429
+ }
430
+ } while (dimpos[dim] >= dim_sizes[dim]);
431
+ }
432
+ finished2:
433
+ return (int)(out - orig_out);
434
+
435
+ } else {
436
+ /* First encoder pass -> determine required buffer space for `out` */
437
+
438
+ int dimpos[MAXDIM];
439
+ VALUE arrays[MAXDIM];
440
+ int dim = 0;
441
+ int item_idx = 0;
442
+ int i;
443
+ int size_sum = 0;
444
+
445
+ *intermediate = rb_ary_new2(nitems);
446
+
447
+ for (i = 0; i < MAXDIM; i++) {
448
+ dimpos[i] = 0;
449
+ }
450
+ arrays[0] = value;
451
+
452
+ while(1) {
453
+
454
+ /* traverse tree down */
455
+ while (dim < ndim - 1) {
456
+ VALUE array = rb_ary_entry(arrays[dim], dimpos[dim]);
457
+ if (TYPE(array) != T_ARRAY) {
458
+ rb_raise( rb_eArgError, "expected Array instead of %+"PRIsVALUE" in dimension %d", array, dim + 1 );
459
+ }
460
+ if (dim_sizes[dim + 1] != RARRAY_LEN(array)) {
461
+ rb_raise( rb_eArgError, "varying number of array elements (%d and %d) in dimension %d", dim_sizes[dim + 1], RARRAY_LENINT(array), dim + 1 );
462
+ }
463
+ arrays[dim + 1] = array;
464
+ dim++;
465
+ }
466
+
467
+ for (i = 0; i < dim_sizes[dim]; i++) {
468
+ VALUE item = rb_ary_entry(arrays[dim], i);
469
+
470
+ if (NIL_P(item)) {
471
+ size_sum += 4 /* length bytes = -1 */;
472
+ } else {
473
+ VALUE subint;
474
+ int strlen = enc_func(this->elem, item, NULL, &subint, enc_idx);
475
+
476
+ /* Gather all intermediate values of elements into an array, which is returned as intermediate for the array encoder */
477
+ if( strlen == -1 ){
478
+ /* Encoded string is returned in subint */
479
+ rb_ary_store(*intermediate, item_idx++, Qtrue);
480
+ rb_ary_store(*intermediate, item_idx++, subint);
481
+
482
+ strlen = RSTRING_LENINT(subint);
483
+ } else {
484
+ /* Two passes necessary */
485
+ rb_ary_store(*intermediate, item_idx++, Qfalse);
486
+ rb_ary_store(*intermediate, item_idx++, subint);
487
+ }
488
+ size_sum += 4 /* length bytes */ + strlen;
489
+ }
490
+ }
491
+
492
+ /* traverse tree up and go to next sibling array */
493
+ do {
494
+ if (dim > 0) {
495
+ dimpos[dim] = 0;
496
+ dim--;
497
+ dimpos[dim]++;
498
+ } else {
499
+ goto finished1;
500
+ }
501
+ } while (dimpos[dim] >= dim_sizes[dim]);
502
+ }
503
+ finished1:;
504
+
505
+ return 4 /* ndim */ + 4 /* flags */ + 4 /* oid */ +
506
+ ndim * (4 /* dim size */ + 4 /* dim offset */) +
507
+ size_sum;
508
+ }
509
+ } else {
510
+ return pg_coder_enc_to_s( conv, value, out, intermediate, enc_idx );
511
+ }
512
+ }
513
+
308
514
  /*
309
515
  * Document-class: PG::BinaryEncoder::FromBase64 < PG::CompositeEncoder
310
516
  *
@@ -323,7 +529,7 @@ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermed
323
529
  if(out){
324
530
  /* Second encoder pass, if required */
325
531
  strlen = enc_func(this->elem, value, out, intermediate, enc_idx);
326
- strlen = base64_decode( out, out, strlen );
532
+ strlen = rbpg_base64_decode( out, out, strlen );
327
533
 
328
534
  return strlen;
329
535
  } else {
@@ -337,7 +543,7 @@ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermed
337
543
  strlen = RSTRING_LENINT(subint);
338
544
  out_str = rb_str_new(NULL, BASE64_DECODED_SIZE(strlen));
339
545
 
340
- strlen = base64_decode( RSTRING_PTR(out_str), RSTRING_PTR(subint), strlen);
546
+ strlen = rbpg_base64_decode( RSTRING_PTR(out_str), RSTRING_PTR(subint), strlen);
341
547
  rb_str_set_len( out_str, strlen );
342
548
  *intermediate = out_str;
343
549
 
@@ -353,6 +559,8 @@ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermed
353
559
  void
354
560
  init_pg_binary_encoder(void)
355
561
  {
562
+ s_id_gregorianP = rb_intern("gregorian?");
563
+ s_id_gregorian = rb_intern("gregorian");
356
564
  s_id_year = rb_intern("year");
357
565
  s_id_month = rb_intern("month");
358
566
  s_id_day = rb_intern("day");
@@ -382,6 +590,8 @@ init_pg_binary_encoder(void)
382
590
  /* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "Date", rb_cPG_SimpleEncoder ); */
383
591
  pg_define_coder( "Date", pg_bin_enc_date, rb_cPG_SimpleEncoder, rb_mPG_BinaryEncoder );
384
592
 
593
+ /* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "Array", rb_cPG_CompositeEncoder ); */
594
+ pg_define_coder( "Array", pg_bin_enc_array, rb_cPG_CompositeEncoder, rb_mPG_BinaryEncoder );
385
595
  /* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "FromBase64", rb_cPG_CompositeEncoder ); */
386
596
  pg_define_coder( "FromBase64", pg_bin_enc_from_base64, rb_cPG_CompositeEncoder, rb_mPG_BinaryEncoder );
387
597
  }
@@ -0,0 +1,360 @@
1
+ #include "pg.h"
2
+
3
+ /********************************************************************
4
+ *
5
+ * Document-class: PG::CancelConnection
6
+ *
7
+ * The class to represent a connection to cancel a query.
8
+ *
9
+ * On PostgreSQL-17+ client library this class is used to implement PG::Connection#cancel .
10
+ * It works on older PostgreSQL server versions too.
11
+ *
12
+ * Available since PostgreSQL-17
13
+ *
14
+ */
15
+
16
+ #ifdef LIBPQ_HAS_CHUNK_MODE
17
+
18
+ static VALUE rb_cPG_Cancon;
19
+ static ID s_id_autoclose_set;
20
+
21
+ typedef struct {
22
+ PGcancelConn *pg_cancon;
23
+
24
+ /* Cached IO object for the socket descriptor */
25
+ VALUE socket_io;
26
+
27
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
28
+ int ruby_sd;
29
+ } t_pg_cancon;
30
+
31
+
32
+ static void
33
+ pg_cancon_gc_mark( void *_this )
34
+ {
35
+ t_pg_cancon *this = (t_pg_cancon *)_this;
36
+ rb_gc_mark_movable( this->socket_io );
37
+ }
38
+
39
+ static void
40
+ pg_cancon_gc_compact( void *_this )
41
+ {
42
+ t_pg_connection *this = (t_pg_connection *)_this;
43
+ pg_gc_location( this->socket_io );
44
+ }
45
+
46
+ static void
47
+ pg_cancon_gc_free( void *_this )
48
+ {
49
+ t_pg_cancon *this = (t_pg_cancon *)_this;
50
+ #if defined(_WIN32)
51
+ if ( RTEST(this->socket_io) ) {
52
+ if( rb_w32_unwrap_io_handle(this->ruby_sd) ){
53
+ rb_warn("pg: Could not unwrap win32 socket handle by garbage collector");
54
+ }
55
+ }
56
+ #endif
57
+ if (this->pg_cancon)
58
+ PQcancelFinish(this->pg_cancon);
59
+ xfree(this);
60
+ }
61
+
62
+ static size_t
63
+ pg_cancon_memsize( const void *_this )
64
+ {
65
+ const t_pg_cancon *this = (const t_pg_cancon *)_this;
66
+ return sizeof(*this);
67
+ }
68
+
69
+ static const rb_data_type_t pg_cancon_type = {
70
+ "PG::CancelConnection",
71
+ {
72
+ pg_cancon_gc_mark,
73
+ pg_cancon_gc_free,
74
+ pg_cancon_memsize,
75
+ pg_cancon_gc_compact,
76
+ },
77
+ 0, 0,
78
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
79
+ };
80
+
81
+ /*
82
+ * Document-method: allocate
83
+ *
84
+ * call-seq:
85
+ * PG::CancelConnection.allocate -> obj
86
+ */
87
+ static VALUE
88
+ pg_cancon_s_allocate( VALUE klass )
89
+ {
90
+ t_pg_cancon *this;
91
+ return TypedData_Make_Struct( klass, t_pg_cancon, &pg_cancon_type, this );
92
+ }
93
+
94
+ static inline t_pg_cancon *
95
+ pg_cancon_get_this( VALUE self )
96
+ {
97
+ t_pg_cancon *this;
98
+ TypedData_Get_Struct(self, t_pg_cancon, &pg_cancon_type, this);
99
+
100
+ return this;
101
+ }
102
+
103
+ static inline PGcancelConn *
104
+ pg_cancon_get_conn( VALUE self )
105
+ {
106
+ t_pg_cancon *this = pg_cancon_get_this(self);
107
+ if (this->pg_cancon == NULL)
108
+ pg_raise_conn_error( rb_eConnectionBad, self, "PG::CancelConnection is closed");
109
+
110
+ return this->pg_cancon;
111
+ }
112
+
113
+ /*
114
+ * Close the associated socket IO object if there is one.
115
+ */
116
+ static void
117
+ pg_cancon_close_socket_io( VALUE self )
118
+ {
119
+ t_pg_cancon *this = pg_cancon_get_this( self );
120
+ pg_unwrap_socket_io( self, &this->socket_io, this->ruby_sd);
121
+ }
122
+
123
+ /*
124
+ * call-seq:
125
+ * PG::CancelConnection.new(conn) -> obj
126
+ *
127
+ * Prepares a connection over which a cancel request can be sent.
128
+ *
129
+ * Creates a PG::CancelConnection from a PG::Connection object, but it won't instantly start sending a cancel request over this connection.
130
+ * A cancel request can be sent over this connection in a blocking manner using #cancel and in a non-blocking manner using #start.
131
+ * #status can be used to check if the PG::CancelConnection object was connected successfully.
132
+ * This PG::CancelConnection object can be used to cancel the query that's running on the original connection in a thread-safe way.
133
+ *
134
+ * Many connection parameters of the original client will be reused when setting up the connection for the cancel request.
135
+ * Importantly, if the original connection requires encryption of the connection and/or verification of the target host (using sslmode or gssencmode), then the connection for the cancel request is made with these same requirements.
136
+ * Any connection options that are only used during authentication or after authentication of the client are ignored though, because cancellation requests do not require authentication and the connection is closed right after the cancellation request is submitted.
137
+ *
138
+ */
139
+ VALUE
140
+ pg_cancon_initialize(VALUE self, VALUE rb_conn)
141
+ {
142
+ t_pg_cancon *this = pg_cancon_get_this(self);
143
+ PGconn *conn = pg_get_pgconn(rb_conn);
144
+
145
+ this->pg_cancon = PQcancelCreate(conn);
146
+ if (this->pg_cancon == NULL)
147
+ pg_raise_conn_error( rb_eConnectionBad, self, "PQcancelCreate failed");
148
+
149
+ return self;
150
+ }
151
+
152
+ /*
153
+ * call-seq:
154
+ * conn.sync_cancel -> nil
155
+ *
156
+ * Requests that the server abandons processing of the current command in a blocking manner.
157
+ *
158
+ * This method directly calls +PQcancelBlocking+ of libpq, so that it doesn't allow parallel threads, doesn't respond to ruby interrupts and doesn't trigger the +Thread.scheduler+ .
159
+ * It is threrfore recommended to call #cancel instead.
160
+ *
161
+ */
162
+ static VALUE
163
+ pg_cancon_sync_cancel(VALUE self)
164
+ {
165
+ PGcancelConn *conn = pg_cancon_get_conn(self);
166
+
167
+ pg_cancon_close_socket_io( self );
168
+ if(PQcancelBlocking(conn) == 0)
169
+ pg_raise_conn_error( rb_eConnectionBad, self, "PQcancelBlocking %s", PQcancelErrorMessage(conn));
170
+ return Qnil;
171
+ }
172
+
173
+ /*
174
+ * call-seq:
175
+ * conn.start -> nil
176
+ *
177
+ * Requests that the server abandons processing of the current command in a non-blocking manner.
178
+ *
179
+ * The behavior is the same like PG::Connection.connect_start .
180
+ *
181
+ * Use #poll to poll the status of the connection.
182
+ *
183
+ */
184
+ static VALUE
185
+ pg_cancon_start(VALUE self)
186
+ {
187
+ PGcancelConn *conn = pg_cancon_get_conn(self);
188
+
189
+ pg_cancon_close_socket_io( self );
190
+ if(gvl_PQcancelStart(conn) == 0)
191
+ pg_raise_conn_error( rb_eConnectionBad, self, "PQcancelStart %s", PQcancelErrorMessage(conn));
192
+ return Qnil;
193
+ }
194
+
195
+ /*
196
+ * call-seq:
197
+ * conn.error_message -> String
198
+ *
199
+ * Returns the error message most recently generated by an operation on the cancel connection.
200
+ *
201
+ * Nearly all PG::CancelConnection functions will set a message if they fail.
202
+ * Note that by libpq convention, a nonempty error_message result can consist of multiple lines, and will include a trailing newline.
203
+ */
204
+ static VALUE
205
+ pg_cancon_error_message(VALUE self)
206
+ {
207
+ PGcancelConn *conn = pg_cancon_get_conn(self);
208
+ char *p_err;
209
+
210
+ p_err = PQcancelErrorMessage(conn);
211
+
212
+ return p_err ? rb_str_new_cstr(p_err) : Qnil;
213
+ }
214
+
215
+ /*
216
+ * call-seq:
217
+ * conn.poll -> Integer
218
+ *
219
+ * This is to poll libpq so that it can proceed with the cancel connection sequence.
220
+ *
221
+ * The behavior is the same like PG::Connection#connect_poll .
222
+ *
223
+ * See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-cancel.html#LIBPQ-PQCANCELSTART]
224
+ *
225
+ */
226
+ static VALUE
227
+ pg_cancon_poll(VALUE self)
228
+ {
229
+ PostgresPollingStatusType status;
230
+ PGcancelConn *conn = pg_cancon_get_conn(self);
231
+
232
+ pg_cancon_close_socket_io( self );
233
+ status = gvl_PQcancelPoll(conn);
234
+
235
+ return INT2FIX((int)status);
236
+ }
237
+
238
+ /*
239
+ * call-seq:
240
+ * conn.status -> Integer
241
+ *
242
+ * Returns the status of the cancel connection.
243
+ *
244
+ * The status can be one of a number of values.
245
+ * However, only three of these are seen outside of an asynchronous cancel procedure:
246
+ * +CONNECTION_ALLOCATED+, +CONNECTION_OK+ and +CONNECTION_BAD+.
247
+ * The initial state of a PG::CancelConnection that's successfully created is +CONNECTION_ALLOCATED+.
248
+ * A cancel request that was successfully dispatched has the status +CONNECTION_OK+.
249
+ * A failed cancel attempt is signaled by status +CONNECTION_BAD+.
250
+ * An OK status will remain so until #finish or #reset is called.
251
+ *
252
+ * See #poll with regards to other status codes that might be returned.
253
+ *
254
+ * Successful dispatch of the cancellation is no guarantee that the request will have any effect, however.
255
+ * If the cancellation is effective, the command being canceled will terminate early and return an error result.
256
+ * If the cancellation fails (say, because the server was already done processing the command), then there will be no visible result at all.
257
+ *
258
+ */
259
+ static VALUE
260
+ pg_cancon_status(VALUE self)
261
+ {
262
+ ConnStatusType status;
263
+ PGcancelConn *conn = pg_cancon_get_conn(self);
264
+
265
+ status = PQcancelStatus(conn);
266
+
267
+ return INT2NUM(status);
268
+ }
269
+
270
+ /*
271
+ * call-seq:
272
+ * conn.socket_io() -> IO
273
+ *
274
+ * Fetch an IO object created from the CancelConnection's underlying socket.
275
+ * This object can be used per <tt>socket_io.wait_readable</tt>, <tt>socket_io.wait_writable</tt> or for <tt>IO.select</tt> to wait for events while running asynchronous API calls.
276
+ * <tt>IO#wait_*able</tt> is <tt>Fiber.scheduler</tt> compatible in contrast to <tt>IO.select</tt>.
277
+ *
278
+ * The IO object can change while the connection is established.
279
+ * So be sure not to cache the IO object, but repeat calling <tt>conn.socket_io</tt> instead.
280
+ */
281
+ static VALUE
282
+ pg_cancon_socket_io(VALUE self)
283
+ {
284
+ t_pg_cancon *this = pg_cancon_get_this( self );
285
+
286
+ if ( !RTEST(this->socket_io) ) {
287
+ int sd;
288
+ if( (sd = PQcancelSocket(this->pg_cancon)) < 0){
289
+ pg_raise_conn_error( rb_eConnectionBad, self, "PQcancelSocket() can't get socket descriptor");
290
+ }
291
+ return pg_wrap_socket_io( sd, self, &this->socket_io, &this->ruby_sd);
292
+ }
293
+
294
+ return this->socket_io;
295
+ }
296
+
297
+ /*
298
+ * call-seq:
299
+ * conn.reset -> nil
300
+ *
301
+ * Resets the PG::CancelConnection so it can be reused for a new cancel connection.
302
+ *
303
+ * If the PG::CancelConnection is currently used to send a cancel request, then this connection is closed.
304
+ * It will then prepare the PG::CancelConnection object such that it can be used to send a new cancel request.
305
+ *
306
+ * This can be used to create one PG::CancelConnection for a PG::Connection and reuse it multiple times throughout the lifetime of the original PG::Connection.
307
+ */
308
+ static VALUE
309
+ pg_cancon_reset(VALUE self)
310
+ {
311
+ PGcancelConn *conn = pg_cancon_get_conn(self);
312
+
313
+ pg_cancon_close_socket_io( self );
314
+ PQcancelReset(conn);
315
+
316
+ return Qnil;
317
+ }
318
+
319
+ /*
320
+ * call-seq:
321
+ * conn.finish -> nil
322
+ *
323
+ * Closes the cancel connection (if it did not finish sending the cancel request yet). Also frees memory used by the PG::CancelConnection object.
324
+ *
325
+ */
326
+ static VALUE
327
+ pg_cancon_finish(VALUE self)
328
+ {
329
+ t_pg_cancon *this = pg_cancon_get_this( self );
330
+
331
+ pg_cancon_close_socket_io( self );
332
+ if( this->pg_cancon )
333
+ PQcancelFinish(this->pg_cancon);
334
+ this->pg_cancon = NULL;
335
+
336
+ return Qnil;
337
+ }
338
+ #endif
339
+
340
+ void
341
+ init_pg_cancon(void)
342
+ {
343
+ #ifdef LIBPQ_HAS_CHUNK_MODE
344
+ s_id_autoclose_set = rb_intern("autoclose=");
345
+
346
+ rb_cPG_Cancon = rb_define_class_under( rb_mPG, "CancelConnection", rb_cObject );
347
+ rb_define_alloc_func( rb_cPG_Cancon, pg_cancon_s_allocate );
348
+ rb_include_module(rb_cPG_Cancon, rb_mEnumerable);
349
+
350
+ rb_define_method(rb_cPG_Cancon, "initialize", pg_cancon_initialize, 1);
351
+ rb_define_method(rb_cPG_Cancon, "sync_cancel", pg_cancon_sync_cancel, 0);
352
+ rb_define_method(rb_cPG_Cancon, "start", pg_cancon_start, 0);
353
+ rb_define_method(rb_cPG_Cancon, "poll", pg_cancon_poll, 0);
354
+ rb_define_method(rb_cPG_Cancon, "status", pg_cancon_status, 0);
355
+ rb_define_method(rb_cPG_Cancon, "socket_io", pg_cancon_socket_io, 0);
356
+ rb_define_method(rb_cPG_Cancon, "error_message", pg_cancon_error_message, 0);
357
+ rb_define_method(rb_cPG_Cancon, "reset", pg_cancon_reset, 0);
358
+ rb_define_method(rb_cPG_Cancon, "finish", pg_cancon_finish, 0);
359
+ #endif
360
+ }