pg 1.5.4 → 1.6.1
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/{History.md → CHANGELOG.md} +106 -4
- data/Gemfile +12 -3
- data/README-Windows.rdoc +1 -1
- data/README.ja.md +4 -4
- data/README.md +58 -17
- data/Rakefile +95 -14
- data/certs/kanis@comcard.de.pem +20 -0
- data/certs/larskanis-2024.pem +24 -0
- data/ext/errorcodes.def +4 -5
- data/ext/errorcodes.txt +2 -5
- data/ext/extconf.rb +161 -14
- data/ext/gvl_wrappers.c +13 -2
- data/ext/gvl_wrappers.h +33 -0
- data/ext/pg.c +17 -6
- data/ext/pg.h +9 -9
- data/ext/pg_binary_decoder.c +152 -0
- data/ext/pg_binary_encoder.c +211 -8
- data/ext/pg_cancel_connection.c +360 -0
- data/ext/pg_coder.c +54 -5
- data/ext/pg_connection.c +409 -167
- data/ext/pg_copy_coder.c +19 -15
- data/ext/pg_record_coder.c +7 -7
- data/ext/pg_result.c +11 -13
- data/ext/pg_text_decoder.c +4 -1
- data/ext/pg_text_encoder.c +37 -18
- data/ext/pg_tuple.c +2 -2
- data/ext/pg_type_map.c +1 -1
- data/ext/pg_type_map_all_strings.c +1 -1
- data/ext/pg_type_map_by_class.c +1 -1
- data/ext/pg_type_map_by_column.c +2 -1
- data/ext/pg_type_map_by_mri_type.c +1 -1
- data/ext/pg_type_map_by_oid.c +3 -1
- data/ext/pg_type_map_in_ruby.c +1 -1
- data/lib/pg/basic_type_map_for_queries.rb +15 -7
- data/lib/pg/basic_type_registry.rb +16 -4
- data/lib/pg/cancel_connection.rb +53 -0
- data/lib/pg/coder.rb +4 -2
- data/lib/pg/connection.rb +310 -167
- data/lib/pg/exceptions.rb +6 -0
- data/lib/pg/text_decoder/date.rb +3 -0
- data/lib/pg/text_decoder/json.rb +3 -0
- data/lib/pg/text_encoder/date.rb +1 -0
- data/lib/pg/text_encoder/inet.rb +3 -0
- data/lib/pg/text_encoder/json.rb +3 -0
- data/lib/pg/version.rb +1 -1
- data/lib/pg.rb +23 -8
- data/misc/yugabyte/Dockerfile +9 -0
- data/misc/yugabyte/docker-compose.yml +28 -0
- data/misc/yugabyte/pg-test.rb +45 -0
- data/pg.gemspec +8 -4
- data/ports/patches/krb5/1.21.3/0001-Allow-static-linking-krb5-library.patch +30 -0
- data/ports/patches/openssl/3.5.1/0001-aarch64-mingw.patch +21 -0
- data/ports/patches/postgresql/17.5/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +42 -0
- data/ports/patches/postgresql/17.5/0001-libpq-Process-buffered-SSL-read-bytes-to-support-rec.patch +52 -0
- data/rakelib/pg_gem_helper.rb +64 -0
- data.tar.gz.sig +0 -0
- metadata +45 -47
- metadata.gz.sig +0 -0
- data/.appveyor.yml +0 -42
- data/.gems +0 -6
- data/.gemtest +0 -0
- data/.github/workflows/binary-gems.yml +0 -117
- data/.github/workflows/source-gem.yml +0 -141
- data/.gitignore +0 -22
- data/.hgsigs +0 -34
- data/.hgtags +0 -41
- data/.irbrc +0 -23
- data/.pryrc +0 -23
- data/.tm_properties +0 -21
- data/.travis.yml +0 -49
- data/Manifest.txt +0 -72
- data/Rakefile.cross +0 -298
- data/translation/.po4a-version +0 -7
- data/translation/po/all.pot +0 -936
- data/translation/po/ja.po +0 -1036
- data/translation/po4a.cfg +0 -12
data/ext/pg_binary_encoder.c
CHANGED
@@ -185,7 +185,7 @@ pg_bin_enc_timestamp(t_pg_coder *this, VALUE value, char *out, VALUE *intermedia
|
|
185
185
|
ts = rb_time_timespec(*intermediate);
|
186
186
|
/* PostgreSQL's timestamp is based on year 2000 and Ruby's time is based on 1970.
|
187
187
|
* Adjust the 30 years difference. */
|
188
|
-
timestamp = (ts.tv_sec - 10957L * 24L * 3600L) * 1000000 + (ts.tv_nsec / 1000);
|
188
|
+
timestamp = ((int64_t)ts.tv_sec - 10957L * 24L * 3600L) * 1000000 + ((int64_t)ts.tv_nsec / 1000);
|
189
189
|
|
190
190
|
if( this->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
|
191
191
|
/* send as local time */
|
@@ -271,14 +271,13 @@ pg_bin_enc_date(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, i
|
|
271
271
|
case T_FALSE:
|
272
272
|
write_nbo32(PG_INT32_MIN, out);
|
273
273
|
return 4;
|
274
|
+
} {
|
275
|
+
VALUE year = rb_funcall(value, s_id_year, 0);
|
276
|
+
VALUE month = rb_funcall(value, s_id_month, 0);
|
277
|
+
VALUE day = rb_funcall(value, s_id_day, 0);
|
278
|
+
int jday = date2j(NUM2INT(year), NUM2INT(month), NUM2INT(day)) - POSTGRES_EPOCH_JDATE;
|
279
|
+
write_nbo32(jday, out);
|
274
280
|
}
|
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
281
|
}else{
|
283
282
|
/* first call -> determine the required length */
|
284
283
|
if(TYPE(value) == T_STRING){
|
@@ -305,6 +304,208 @@ pg_bin_enc_date(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, i
|
|
305
304
|
return 4;
|
306
305
|
}
|
307
306
|
|
307
|
+
/*
|
308
|
+
* Maximum number of array subscripts (arbitrary limit)
|
309
|
+
*/
|
310
|
+
#define MAXDIM 6
|
311
|
+
|
312
|
+
/*
|
313
|
+
* Document-class: PG::BinaryEncoder::Array < PG::CompositeEncoder
|
314
|
+
*
|
315
|
+
* This is the encoder class for PostgreSQL array types in binary format.
|
316
|
+
*
|
317
|
+
* All values are encoded according to the #elements_type
|
318
|
+
* accessor. Sub-arrays are encoded recursively.
|
319
|
+
*
|
320
|
+
* This encoder expects an Array of values or sub-arrays as input.
|
321
|
+
* Other values are passed through as byte string without interpretation.
|
322
|
+
*
|
323
|
+
* It is possible to enforce a number of dimensions to be encoded by #dimensions= .
|
324
|
+
* Deeper nested arrays are then passed to the elements encoder and less nested arrays raise an ArgumentError.
|
325
|
+
*
|
326
|
+
* The accessors needs_quotation and delimiter are ignored for binary encoding.
|
327
|
+
*
|
328
|
+
*/
|
329
|
+
static int
|
330
|
+
pg_bin_enc_array(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
|
331
|
+
{
|
332
|
+
if (TYPE(value) == T_ARRAY) {
|
333
|
+
t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
|
334
|
+
t_pg_coder_enc_func enc_func = pg_coder_enc_func(this->elem);
|
335
|
+
int dim_sizes[MAXDIM];
|
336
|
+
int ndim = 1;
|
337
|
+
int nitems = 1;
|
338
|
+
VALUE el1 = value;
|
339
|
+
|
340
|
+
if (RARRAY_LEN(value) == 0) {
|
341
|
+
nitems = 0;
|
342
|
+
ndim = 0;
|
343
|
+
dim_sizes[0] = 0;
|
344
|
+
} else {
|
345
|
+
/* Determine number of dimensions, sizes of dimensions and number of items */
|
346
|
+
while(1) {
|
347
|
+
VALUE el2;
|
348
|
+
|
349
|
+
dim_sizes[ndim-1] = RARRAY_LENINT(el1);
|
350
|
+
nitems *= dim_sizes[ndim-1];
|
351
|
+
el2 = rb_ary_entry(el1, 0);
|
352
|
+
if ( (this->dimensions < 0 || ndim < this->dimensions) &&
|
353
|
+
TYPE(el2) == T_ARRAY) {
|
354
|
+
ndim++;
|
355
|
+
if (ndim > MAXDIM)
|
356
|
+
rb_raise( rb_eArgError, "unsupported number of array dimensions: >%d", ndim );
|
357
|
+
} else {
|
358
|
+
break;
|
359
|
+
}
|
360
|
+
el1 = el2;
|
361
|
+
}
|
362
|
+
}
|
363
|
+
if( this->dimensions >= 0 && (ndim==0 ? 1 : ndim) != this->dimensions ){
|
364
|
+
rb_raise(rb_eArgError, "less array dimensions to encode (%d) than expected (%d)", ndim, this->dimensions);
|
365
|
+
}
|
366
|
+
|
367
|
+
if(out){
|
368
|
+
/* Second encoder pass -> write data to `out` */
|
369
|
+
int dimpos[MAXDIM];
|
370
|
+
VALUE arrays[MAXDIM];
|
371
|
+
int dim = 0;
|
372
|
+
int item_idx = 0;
|
373
|
+
int i;
|
374
|
+
char *orig_out = out;
|
375
|
+
Oid elem_oid = this->elem ? this->elem->oid : 0;
|
376
|
+
|
377
|
+
write_nbo32(ndim, out); out += 4;
|
378
|
+
write_nbo32(1 /* flags */, out); out += 4;
|
379
|
+
write_nbo32(elem_oid, out); out += 4;
|
380
|
+
for (i = 0; i < ndim; i++) {
|
381
|
+
dimpos[i] = 0;
|
382
|
+
write_nbo32(dim_sizes[i], out); out += 4;
|
383
|
+
write_nbo32(1 /* offset */, out); out += 4;
|
384
|
+
}
|
385
|
+
arrays[0] = value;
|
386
|
+
|
387
|
+
while(1) {
|
388
|
+
/* traverse tree down */
|
389
|
+
while (dim < ndim - 1) {
|
390
|
+
arrays[dim + 1] = rb_ary_entry(arrays[dim], dimpos[dim]);
|
391
|
+
dim++;
|
392
|
+
}
|
393
|
+
|
394
|
+
for (i = 0; i < dim_sizes[dim]; i++) {
|
395
|
+
VALUE item = rb_ary_entry(arrays[dim], i);
|
396
|
+
|
397
|
+
if (NIL_P(item)) {
|
398
|
+
write_nbo32(-1, out); out += 4;
|
399
|
+
} else {
|
400
|
+
/* Encoded string is returned in subint */
|
401
|
+
int strlen;
|
402
|
+
VALUE is_one_pass = rb_ary_entry(*intermediate, item_idx++);
|
403
|
+
VALUE subint = rb_ary_entry(*intermediate, item_idx++);
|
404
|
+
|
405
|
+
if (is_one_pass == Qtrue) {
|
406
|
+
strlen = RSTRING_LENINT(subint);
|
407
|
+
memcpy( out + 4, RSTRING_PTR(subint), strlen);
|
408
|
+
} else {
|
409
|
+
strlen = enc_func(this->elem, item, out + 4, &subint, enc_idx);
|
410
|
+
}
|
411
|
+
write_nbo32(strlen, out);
|
412
|
+
out += 4 /* length */ + strlen;
|
413
|
+
}
|
414
|
+
}
|
415
|
+
|
416
|
+
/* traverse tree up and go to next sibling array */
|
417
|
+
do {
|
418
|
+
if (dim > 0) {
|
419
|
+
dimpos[dim] = 0;
|
420
|
+
dim--;
|
421
|
+
dimpos[dim]++;
|
422
|
+
} else {
|
423
|
+
goto finished2;
|
424
|
+
}
|
425
|
+
} while (dimpos[dim] >= dim_sizes[dim]);
|
426
|
+
}
|
427
|
+
finished2:
|
428
|
+
return (int)(out - orig_out);
|
429
|
+
|
430
|
+
} else {
|
431
|
+
/* First encoder pass -> determine required buffer space for `out` */
|
432
|
+
|
433
|
+
int dimpos[MAXDIM];
|
434
|
+
VALUE arrays[MAXDIM];
|
435
|
+
int dim = 0;
|
436
|
+
int item_idx = 0;
|
437
|
+
int i;
|
438
|
+
int size_sum = 0;
|
439
|
+
|
440
|
+
*intermediate = rb_ary_new2(nitems);
|
441
|
+
|
442
|
+
for (i = 0; i < MAXDIM; i++) {
|
443
|
+
dimpos[i] = 0;
|
444
|
+
}
|
445
|
+
arrays[0] = value;
|
446
|
+
|
447
|
+
while(1) {
|
448
|
+
|
449
|
+
/* traverse tree down */
|
450
|
+
while (dim < ndim - 1) {
|
451
|
+
VALUE array = rb_ary_entry(arrays[dim], dimpos[dim]);
|
452
|
+
if (TYPE(array) != T_ARRAY) {
|
453
|
+
rb_raise( rb_eArgError, "expected Array instead of %+"PRIsVALUE" in dimension %d", array, dim + 1 );
|
454
|
+
}
|
455
|
+
if (dim_sizes[dim + 1] != RARRAY_LEN(array)) {
|
456
|
+
rb_raise( rb_eArgError, "varying number of array elements (%d and %d) in dimension %d", dim_sizes[dim + 1], RARRAY_LENINT(array), dim + 1 );
|
457
|
+
}
|
458
|
+
arrays[dim + 1] = array;
|
459
|
+
dim++;
|
460
|
+
}
|
461
|
+
|
462
|
+
for (i = 0; i < dim_sizes[dim]; i++) {
|
463
|
+
VALUE item = rb_ary_entry(arrays[dim], i);
|
464
|
+
|
465
|
+
if (NIL_P(item)) {
|
466
|
+
size_sum += 4 /* length bytes = -1 */;
|
467
|
+
} else {
|
468
|
+
VALUE subint;
|
469
|
+
int strlen = enc_func(this->elem, item, NULL, &subint, enc_idx);
|
470
|
+
|
471
|
+
/* Gather all intermediate values of elements into an array, which is returned as intermediate for the array encoder */
|
472
|
+
if( strlen == -1 ){
|
473
|
+
/* Encoded string is returned in subint */
|
474
|
+
rb_ary_store(*intermediate, item_idx++, Qtrue);
|
475
|
+
rb_ary_store(*intermediate, item_idx++, subint);
|
476
|
+
|
477
|
+
strlen = RSTRING_LENINT(subint);
|
478
|
+
} else {
|
479
|
+
/* Two passes necessary */
|
480
|
+
rb_ary_store(*intermediate, item_idx++, Qfalse);
|
481
|
+
rb_ary_store(*intermediate, item_idx++, subint);
|
482
|
+
}
|
483
|
+
size_sum += 4 /* length bytes */ + strlen;
|
484
|
+
}
|
485
|
+
}
|
486
|
+
|
487
|
+
/* traverse tree up and go to next sibling array */
|
488
|
+
do {
|
489
|
+
if (dim > 0) {
|
490
|
+
dimpos[dim] = 0;
|
491
|
+
dim--;
|
492
|
+
dimpos[dim]++;
|
493
|
+
} else {
|
494
|
+
goto finished1;
|
495
|
+
}
|
496
|
+
} while (dimpos[dim] >= dim_sizes[dim]);
|
497
|
+
}
|
498
|
+
finished1:;
|
499
|
+
|
500
|
+
return 4 /* ndim */ + 4 /* flags */ + 4 /* oid */ +
|
501
|
+
ndim * (4 /* dim size */ + 4 /* dim offset */) +
|
502
|
+
size_sum;
|
503
|
+
}
|
504
|
+
} else {
|
505
|
+
return pg_coder_enc_to_s( conv, value, out, intermediate, enc_idx );
|
506
|
+
}
|
507
|
+
}
|
508
|
+
|
308
509
|
/*
|
309
510
|
* Document-class: PG::BinaryEncoder::FromBase64 < PG::CompositeEncoder
|
310
511
|
*
|
@@ -382,6 +583,8 @@ init_pg_binary_encoder(void)
|
|
382
583
|
/* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "Date", rb_cPG_SimpleEncoder ); */
|
383
584
|
pg_define_coder( "Date", pg_bin_enc_date, rb_cPG_SimpleEncoder, rb_mPG_BinaryEncoder );
|
384
585
|
|
586
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "Array", rb_cPG_CompositeEncoder ); */
|
587
|
+
pg_define_coder( "Array", pg_bin_enc_array, rb_cPG_CompositeEncoder, rb_mPG_BinaryEncoder );
|
385
588
|
/* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "FromBase64", rb_cPG_CompositeEncoder ); */
|
386
589
|
pg_define_coder( "FromBase64", pg_bin_enc_from_base64, rb_cPG_CompositeEncoder, rb_mPG_BinaryEncoder );
|
387
590
|
}
|
@@ -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 libaray 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 HAVE_PQSETCHUNKEDROWSMODE
|
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 | PG_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 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(gvl_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 HAVE_PQSETCHUNKEDROWSMODE
|
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
|
+
}
|