pg 1.1.3 → 1.4.3

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 (118) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.appveyor.yml +36 -0
  4. data/.gems +6 -0
  5. data/.github/workflows/binary-gems.yml +86 -0
  6. data/.github/workflows/source-gem.yml +129 -0
  7. data/.gitignore +13 -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/Gemfile +14 -0
  15. data/History.rdoc +291 -6
  16. data/Manifest.txt +3 -3
  17. data/README-Windows.rdoc +4 -4
  18. data/README.ja.rdoc +1 -2
  19. data/README.rdoc +51 -15
  20. data/Rakefile +31 -140
  21. data/Rakefile.cross +60 -56
  22. data/certs/ged.pem +24 -0
  23. data/certs/larskanis-2022.pem +26 -0
  24. data/ext/errorcodes.def +76 -0
  25. data/ext/errorcodes.rb +0 -0
  26. data/ext/errorcodes.txt +21 -2
  27. data/ext/extconf.rb +101 -26
  28. data/ext/gvl_wrappers.c +4 -0
  29. data/ext/gvl_wrappers.h +23 -0
  30. data/ext/pg.c +190 -122
  31. data/ext/pg.h +43 -17
  32. data/ext/pg_binary_decoder.c +20 -16
  33. data/ext/pg_binary_encoder.c +13 -12
  34. data/ext/pg_coder.c +95 -29
  35. data/ext/pg_connection.c +1214 -919
  36. data/ext/pg_copy_coder.c +50 -18
  37. data/ext/pg_record_coder.c +521 -0
  38. data/ext/pg_result.c +344 -153
  39. data/ext/pg_text_decoder.c +15 -9
  40. data/ext/pg_text_encoder.c +185 -53
  41. data/ext/pg_tuple.c +63 -35
  42. data/ext/pg_type_map.c +42 -9
  43. data/ext/pg_type_map_all_strings.c +19 -5
  44. data/ext/pg_type_map_by_class.c +54 -24
  45. data/ext/pg_type_map_by_column.c +73 -34
  46. data/ext/pg_type_map_by_mri_type.c +48 -19
  47. data/ext/pg_type_map_by_oid.c +55 -25
  48. data/ext/pg_type_map_in_ruby.c +51 -20
  49. data/ext/{util.c → pg_util.c} +7 -7
  50. data/ext/{util.h → pg_util.h} +0 -0
  51. data/lib/pg/basic_type_map_based_on_result.rb +47 -0
  52. data/lib/pg/basic_type_map_for_queries.rb +193 -0
  53. data/lib/pg/basic_type_map_for_results.rb +81 -0
  54. data/lib/pg/basic_type_registry.rb +301 -0
  55. data/lib/pg/binary_decoder.rb +1 -0
  56. data/lib/pg/coder.rb +23 -2
  57. data/lib/pg/connection.rb +669 -71
  58. data/lib/pg/constants.rb +1 -0
  59. data/lib/pg/exceptions.rb +8 -1
  60. data/lib/pg/result.rb +13 -1
  61. data/lib/pg/text_decoder.rb +2 -3
  62. data/lib/pg/text_encoder.rb +8 -18
  63. data/lib/pg/type_map_by_column.rb +2 -1
  64. data/lib/pg/version.rb +4 -0
  65. data/lib/pg.rb +48 -33
  66. data/misc/openssl-pg-segfault.rb +31 -0
  67. data/misc/postgres/History.txt +9 -0
  68. data/misc/postgres/Manifest.txt +5 -0
  69. data/misc/postgres/README.txt +21 -0
  70. data/misc/postgres/Rakefile +21 -0
  71. data/misc/postgres/lib/postgres.rb +16 -0
  72. data/misc/ruby-pg/History.txt +9 -0
  73. data/misc/ruby-pg/Manifest.txt +5 -0
  74. data/misc/ruby-pg/README.txt +21 -0
  75. data/misc/ruby-pg/Rakefile +21 -0
  76. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  77. data/pg.gemspec +32 -0
  78. data/rakelib/task_extension.rb +46 -0
  79. data/sample/array_insert.rb +20 -0
  80. data/sample/async_api.rb +102 -0
  81. data/sample/async_copyto.rb +39 -0
  82. data/sample/async_mixed.rb +56 -0
  83. data/sample/check_conn.rb +21 -0
  84. data/sample/copydata.rb +71 -0
  85. data/sample/copyfrom.rb +81 -0
  86. data/sample/copyto.rb +19 -0
  87. data/sample/cursor.rb +21 -0
  88. data/sample/disk_usage_report.rb +177 -0
  89. data/sample/issue-119.rb +94 -0
  90. data/sample/losample.rb +69 -0
  91. data/sample/minimal-testcase.rb +17 -0
  92. data/sample/notify_wait.rb +72 -0
  93. data/sample/pg_statistics.rb +285 -0
  94. data/sample/replication_monitor.rb +222 -0
  95. data/sample/test_binary_values.rb +33 -0
  96. data/sample/wal_shipper.rb +434 -0
  97. data/sample/warehouse_partitions.rb +311 -0
  98. data.tar.gz.sig +0 -0
  99. metadata +94 -237
  100. metadata.gz.sig +0 -0
  101. data/ChangeLog +0 -6595
  102. data/lib/pg/basic_type_mapping.rb +0 -459
  103. data/spec/data/expected_trace.out +0 -26
  104. data/spec/data/random_binary_data +0 -0
  105. data/spec/helpers.rb +0 -381
  106. data/spec/pg/basic_type_mapping_spec.rb +0 -508
  107. data/spec/pg/connection_spec.rb +0 -1849
  108. data/spec/pg/connection_sync_spec.rb +0 -41
  109. data/spec/pg/result_spec.rb +0 -491
  110. data/spec/pg/tuple_spec.rb +0 -280
  111. data/spec/pg/type_map_by_class_spec.rb +0 -138
  112. data/spec/pg/type_map_by_column_spec.rb +0 -222
  113. data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
  114. data/spec/pg/type_map_by_oid_spec.rb +0 -149
  115. data/spec/pg/type_map_in_ruby_spec.rb +0 -164
  116. data/spec/pg/type_map_spec.rb +0 -22
  117. data/spec/pg/type_spec.rb +0 -949
  118. data/spec/pg_spec.rb +0 -50
data/ext/pg.c CHANGED
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * pg.c - Toplevel extension
3
- * $Id: pg.c,v 2d334508484a 2018/08/27 11:33:43 lars $
3
+ * $Id$
4
4
  *
5
5
  * Author/s:
6
6
  *
@@ -16,7 +16,7 @@
16
16
  * See Contributors.rdoc for the many additional fine people that have contributed
17
17
  * to this library over the years.
18
18
  *
19
- * Copyright (c) 1997-2016 by the authors.
19
+ * Copyright (c) 1997-2019 by the authors.
20
20
  *
21
21
  * You may redistribute this software under the same terms as Ruby itself; see
22
22
  * https://www.ruby-lang.org/en/about/license.txt or the BSDL file in the source
@@ -47,6 +47,7 @@
47
47
  */
48
48
 
49
49
  #include "pg.h"
50
+ #include "pg_config.h"
50
51
 
51
52
  int pg_skip_deprecation_warning;
52
53
  VALUE rb_mPG;
@@ -126,26 +127,6 @@ const char * const (pg_enc_pg2ruby_mapping[][2]) = {
126
127
  static struct st_table *enc_pg2ruby;
127
128
 
128
129
 
129
- /*
130
- * Look up the JOHAB encoding, creating it as a dummy encoding if it's not
131
- * already defined.
132
- */
133
- static rb_encoding *
134
- pg_find_or_create_johab(void)
135
- {
136
- static const char * const aliases[] = { "JOHAB", "Windows-1361", "CP1361" };
137
- int enc_index;
138
- size_t i;
139
-
140
- for (i = 0; i < sizeof(aliases)/sizeof(aliases[0]); ++i) {
141
- enc_index = rb_enc_find_index(aliases[i]);
142
- if (enc_index > 0) return rb_enc_from_index(enc_index);
143
- }
144
-
145
- enc_index = rb_define_dummy_encoding(aliases[0]);
146
- return rb_enc_from_index(enc_index);
147
- }
148
-
149
130
  /*
150
131
  * Return the given PostgreSQL encoding ID as an rb_encoding.
151
132
  *
@@ -186,10 +167,6 @@ pg_get_pg_encname_as_rb_encoding( const char *pg_encname )
186
167
  return rb_enc_find( pg_enc_pg2ruby_mapping[i][1] );
187
168
  }
188
169
 
189
- /* JOHAB isn't a builtin encoding, so make up a dummy encoding if it's seen */
190
- if ( strncmp(pg_encname, "JOHAB", 5) == 0 )
191
- return pg_find_or_create_johab();
192
-
193
170
  /* Fallthrough to ASCII-8BIT */
194
171
  return rb_ascii8bit_encoding();
195
172
  }
@@ -331,8 +308,7 @@ pg_to_bool_int(VALUE value)
331
308
  * If your application initializes libssl and/or libcrypto libraries and libpq is
332
309
  * built with SSL support, you should call PG.init_openssl() to tell libpq that the
333
310
  * libssl and/or libcrypto libraries have been initialized by your application,
334
- * so that libpq will not also initialize those libraries. See
335
- * http://h71000.www7.hp.com/doc/83final/BA554_90007/ch04.html for details on the SSL API.
311
+ * so that libpq will not also initialize those libraries.
336
312
  *
337
313
  * When do_ssl is +true+, libpq will initialize the OpenSSL library before first
338
314
  * opening a database connection. When do_crypto is +true+, the libcrypto library
@@ -416,14 +392,34 @@ Init_pg_ext()
416
392
  rb_define_const(rb_mPGconstants, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
417
393
  /* Waiting for a response from the server. */
418
394
  rb_define_const(rb_mPGconstants, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
419
- /* Received authentication; waiting for backend start-up to finish. */
395
+ /* Received authentication; waiting for backend startup. */
420
396
  rb_define_const(rb_mPGconstants, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
397
+ /* This state is no longer used. */
398
+ rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
421
399
  /* Negotiating SSL encryption. */
422
400
  rb_define_const(rb_mPGconstants, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
423
- /* Negotiating environment-driven parameter settings. */
424
- rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
425
- /* Internal state: connect() needed. */
401
+ /* Internal state - PG.connect() needed. */
426
402
  rb_define_const(rb_mPGconstants, "CONNECTION_NEEDED", INT2FIX(CONNECTION_NEEDED));
403
+ #if PG_MAJORVERSION_NUM >= 10
404
+ /* Checking if session is read-write. Available since PostgreSQL-10. */
405
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_WRITABLE", INT2FIX(CONNECTION_CHECK_WRITABLE));
406
+ #endif
407
+ #if PG_MAJORVERSION_NUM >= 10
408
+ /* Consuming any extra messages. Available since PostgreSQL-10. */
409
+ rb_define_const(rb_mPGconstants, "CONNECTION_CONSUME", INT2FIX(CONNECTION_CONSUME));
410
+ #endif
411
+ #if PG_MAJORVERSION_NUM >= 12
412
+ /* Negotiating GSSAPI. Available since PostgreSQL-12. */
413
+ rb_define_const(rb_mPGconstants, "CONNECTION_GSS_STARTUP", INT2FIX(CONNECTION_GSS_STARTUP));
414
+ #endif
415
+ #if PG_MAJORVERSION_NUM >= 13
416
+ /* Checking target server properties. Available since PostgreSQL-13. */
417
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_TARGET", INT2FIX(CONNECTION_CHECK_TARGET));
418
+ #endif
419
+ #if PG_MAJORVERSION_NUM >= 14
420
+ /* Checking if server is in standby mode. Available since PostgreSQL-14. */
421
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_STANDBY", INT2FIX(CONNECTION_CHECK_STANDBY));
422
+ #endif
427
423
 
428
424
  /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
429
425
 
@@ -438,26 +434,48 @@ Init_pg_ext()
438
434
 
439
435
  /****** PG::Connection CLASS CONSTANTS: Transaction Status ******/
440
436
 
441
- /* Transaction is currently idle (#transaction_status) */
437
+ /* Transaction is currently idle ( Connection#transaction_status ) */
442
438
  rb_define_const(rb_mPGconstants, "PQTRANS_IDLE", INT2FIX(PQTRANS_IDLE));
443
- /* Transaction is currently active; query has been sent to the server, but not yet completed. (#transaction_status) */
439
+ /* Transaction is currently active; query has been sent to the server, but not yet completed. ( Connection#transaction_status ) */
444
440
  rb_define_const(rb_mPGconstants, "PQTRANS_ACTIVE", INT2FIX(PQTRANS_ACTIVE));
445
- /* Transaction is currently idle, in a valid transaction block (#transaction_status) */
441
+ /* Transaction is currently idle, in a valid transaction block ( Connection#transaction_status ) */
446
442
  rb_define_const(rb_mPGconstants, "PQTRANS_INTRANS", INT2FIX(PQTRANS_INTRANS));
447
- /* Transaction is currently idle, in a failed transaction block (#transaction_status) */
443
+ /* Transaction is currently idle, in a failed transaction block ( Connection#transaction_status ) */
448
444
  rb_define_const(rb_mPGconstants, "PQTRANS_INERROR", INT2FIX(PQTRANS_INERROR));
449
- /* Transaction's connection is bad (#transaction_status) */
445
+ /* Transaction's connection is bad ( Connection#transaction_status ) */
450
446
  rb_define_const(rb_mPGconstants, "PQTRANS_UNKNOWN", INT2FIX(PQTRANS_UNKNOWN));
451
447
 
452
448
  /****** PG::Connection CLASS CONSTANTS: Error Verbosity ******/
453
449
 
454
- /* Terse error verbosity level (#set_error_verbosity) */
450
+ /* Error verbosity level ( Connection#set_error_verbosity ).
451
+ * In TERSE mode, returned messages include severity, primary text, and position only; this will normally fit on a single line. */
455
452
  rb_define_const(rb_mPGconstants, "PQERRORS_TERSE", INT2FIX(PQERRORS_TERSE));
456
- /* Default error verbosity level (#set_error_verbosity) */
453
+ /* Error verbosity level ( Connection#set_error_verbosity ).
454
+ * The DEFAULT mode produces messages that include the above plus any detail, hint, or context fields (these might span multiple lines). */
457
455
  rb_define_const(rb_mPGconstants, "PQERRORS_DEFAULT", INT2FIX(PQERRORS_DEFAULT));
458
- /* Verbose error verbosity level (#set_error_verbosity) */
456
+ /* Error verbosity level ( Connection#set_error_verbosity ).
457
+ * The VERBOSE mode includes all available fields. */
459
458
  rb_define_const(rb_mPGconstants, "PQERRORS_VERBOSE", INT2FIX(PQERRORS_VERBOSE));
460
459
 
460
+ /* PQERRORS_SQLSTATE was introduced in PG-12 together with PQresultMemorySize() */
461
+ #ifdef HAVE_PQRESULTMEMORYSIZE
462
+ /* Error verbosity level ( Connection#set_error_verbosity ).
463
+ * The SQLSTATE mode includes only the error severity and the SQLSTATE error code, if one is available (if not, the output is like TERSE mode).
464
+ *
465
+ * Available since PostgreSQL-12.
466
+ */
467
+ rb_define_const(rb_mPGconstants, "PQERRORS_SQLSTATE", INT2FIX(PQERRORS_SQLSTATE));
468
+ #endif
469
+
470
+ #ifdef HAVE_PQRESULTVERBOSEERRORMESSAGE
471
+ /* See Connection#set_error_context_visibility */
472
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_NEVER", INT2FIX(PQSHOW_CONTEXT_NEVER));
473
+ /* See Connection#set_error_context_visibility */
474
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ERRORS", INT2FIX(PQSHOW_CONTEXT_ERRORS));
475
+ /* See Connection#set_error_context_visibility */
476
+ rb_define_const(rb_mPGconstants, "PQSHOW_CONTEXT_ALWAYS", INT2FIX(PQSHOW_CONTEXT_ALWAYS));
477
+ #endif
478
+
461
479
  /****** PG::Connection CLASS CONSTANTS: Check Server Status ******/
462
480
 
463
481
  /* Server is accepting connections. */
@@ -471,144 +489,193 @@ Init_pg_ext()
471
489
 
472
490
  /****** PG::Connection CLASS CONSTANTS: Large Objects ******/
473
491
 
474
- /* Flag for #lo_creat, #lo_open -- open for writing */
492
+ /* Flag for Connection#lo_creat, Connection#lo_open -- open for writing */
475
493
  rb_define_const(rb_mPGconstants, "INV_WRITE", INT2FIX(INV_WRITE));
476
- /* Flag for #lo_creat, #lo_open -- open for reading */
494
+ /* Flag for Connection#lo_creat, Connection#lo_open -- open for reading */
477
495
  rb_define_const(rb_mPGconstants, "INV_READ", INT2FIX(INV_READ));
478
- /* Flag for #lo_lseek -- seek from object start */
496
+ /* Flag for Connection#lo_lseek -- seek from object start */
479
497
  rb_define_const(rb_mPGconstants, "SEEK_SET", INT2FIX(SEEK_SET));
480
- /* Flag for #lo_lseek -- seek from current position */
498
+ /* Flag for Connection#lo_lseek -- seek from current position */
481
499
  rb_define_const(rb_mPGconstants, "SEEK_CUR", INT2FIX(SEEK_CUR));
482
- /* Flag for #lo_lseek -- seek from object end */
500
+ /* Flag for Connection#lo_lseek -- seek from object end */
483
501
  rb_define_const(rb_mPGconstants, "SEEK_END", INT2FIX(SEEK_END));
484
502
 
485
503
  /****** PG::Result CONSTANTS: result status ******/
486
504
 
487
- /* #result_status constant: The string sent to the server was empty. */
505
+ /* Result#result_status constant - The string sent to the server was empty. */
488
506
  rb_define_const(rb_mPGconstants, "PGRES_EMPTY_QUERY", INT2FIX(PGRES_EMPTY_QUERY));
489
- /* #result_status constant: Successful completion of a command returning no data. */
507
+ /* Result#result_status constant - Successful completion of a command returning no data. */
490
508
  rb_define_const(rb_mPGconstants, "PGRES_COMMAND_OK", INT2FIX(PGRES_COMMAND_OK));
491
- /* #result_status constant: Successful completion of a command returning data
492
- (such as a SELECT or SHOW). */
509
+ /* Result#result_status constant - Successful completion of a command returning data (such as a SELECT or SHOW). */
493
510
  rb_define_const(rb_mPGconstants, "PGRES_TUPLES_OK", INT2FIX(PGRES_TUPLES_OK));
494
- /* #result_status constant: Copy Out (from server) data transfer started. */
511
+ /* Result#result_status constant - Copy Out (from server) data transfer started. */
495
512
  rb_define_const(rb_mPGconstants, "PGRES_COPY_OUT", INT2FIX(PGRES_COPY_OUT));
496
- /* #result_status constant: Copy In (to server) data transfer started. */
513
+ /* Result#result_status constant - Copy In (to server) data transfer started. */
497
514
  rb_define_const(rb_mPGconstants, "PGRES_COPY_IN", INT2FIX(PGRES_COPY_IN));
498
- /* #result_status constant: The server’s response was not understood. */
515
+ /* Result#result_status constant - The server’s response was not understood. */
499
516
  rb_define_const(rb_mPGconstants, "PGRES_BAD_RESPONSE", INT2FIX(PGRES_BAD_RESPONSE));
500
- /* #result_status constant: A nonfatal error (a notice or warning) occurred. */
517
+ /* Result#result_status constant - A nonfatal error (a notice or warning) occurred. */
501
518
  rb_define_const(rb_mPGconstants, "PGRES_NONFATAL_ERROR",INT2FIX(PGRES_NONFATAL_ERROR));
502
- /* #result_status constant: A fatal error occurred. */
519
+ /* Result#result_status constant - A fatal error occurred. */
503
520
  rb_define_const(rb_mPGconstants, "PGRES_FATAL_ERROR", INT2FIX(PGRES_FATAL_ERROR));
504
- /* #result_status constant: Copy In/Out data transfer in progress. */
521
+ /* Result#result_status constant - Copy In/Out data transfer in progress. */
505
522
  rb_define_const(rb_mPGconstants, "PGRES_COPY_BOTH", INT2FIX(PGRES_COPY_BOTH));
506
- /* #result_status constant: Single tuple from larger resultset. */
523
+ /* Result#result_status constant - Single tuple from larger resultset. */
507
524
  rb_define_const(rb_mPGconstants, "PGRES_SINGLE_TUPLE", INT2FIX(PGRES_SINGLE_TUPLE));
508
525
 
526
+ #ifdef HAVE_PQENTERPIPELINEMODE
527
+ /* Result#result_status constant - The PG::Result represents a synchronization point in pipeline mode, requested by Connection#pipeline_sync.
528
+ *
529
+ * This status occurs only when pipeline mode has been selected. */
530
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_SYNC", INT2FIX(PGRES_PIPELINE_SYNC));
531
+
532
+ /* Result#result_status constant - The PG::Result represents a pipeline that has received an error from the server.
533
+ *
534
+ * Connection#get_result must be called repeatedly, and each time it will return this status code until the end of the current pipeline, at which point it will return PG::PGRES_PIPELINE_SYNC and normal processing can resume.
535
+ */
536
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_ABORTED", INT2FIX(PGRES_PIPELINE_ABORTED));
537
+ #endif
538
+
509
539
  /****** Result CONSTANTS: result error field codes ******/
510
540
 
511
- /* #result_error_field argument constant: The severity; the field contents
512
- * are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE,
513
- * DEBUG, INFO, or LOG (in a notice message), or a localized translation
514
- * of one of these. Always present.
541
+ /* Result#result_error_field argument constant
542
+ *
543
+ * The severity; the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a localized translation
544
+ * of one of these.
545
+ * Always present.
515
546
  */
516
547
  rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY", INT2FIX(PG_DIAG_SEVERITY));
517
548
 
518
- /* #result_error_field argument constant: The SQLSTATE code for the
519
- * error. The SQLSTATE code identies the type of error that has occurred;
520
- * it can be used by front-end applications to perform specic operations
521
- * (such as er- ror handling) in response to a particular database
522
- * error. For a list of the possible SQLSTATE codes, see Appendix A.
523
- * This eld is not localizable, and is always present.
549
+ #ifdef PG_DIAG_SEVERITY_NONLOCALIZED
550
+ /* Result#result_error_field argument constant
551
+ *
552
+ * The severity; the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message).
553
+ * This is identical to the PG_DIAG_SEVERITY field except that the contents are never localized.
554
+ *
555
+ * Available since PostgreSQL-9.6
556
+ */
557
+ rb_define_const(rb_mPGconstants, "PG_DIAG_SEVERITY_NONLOCALIZED", INT2FIX(PG_DIAG_SEVERITY_NONLOCALIZED));
558
+ #endif
559
+ /* Result#result_error_field argument constant
560
+ *
561
+ * The SQLSTATE code for the error.
562
+ * The SQLSTATE code identies the type of error that has occurred; it can be used by front-end applications to perform specific operations (such as error handling) in response to a particular database error.
563
+ * For a list of the possible SQLSTATE codes, see Appendix A.
564
+ * This field is not localizable, and is always present.
524
565
  */
525
566
  rb_define_const(rb_mPGconstants, "PG_DIAG_SQLSTATE", INT2FIX(PG_DIAG_SQLSTATE));
526
-
527
- /* #result_error_field argument constant: The primary human-readable
528
- * error message (typically one line). Always present. */
567
+ /* Result#result_error_field argument constant
568
+ *
569
+ * The primary human-readable error message (typically one line).
570
+ * Always present. */
529
571
  rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_PRIMARY", INT2FIX(PG_DIAG_MESSAGE_PRIMARY));
530
-
531
- /* #result_error_field argument constant: Detail: an optional secondary
532
- * error message carrying more detail about the problem. Might run to
533
- * multiple lines.
572
+ /* Result#result_error_field argument constant
573
+ *
574
+ * Detail: an optional secondary error message carrying more detail about the problem.
575
+ * Might run to multiple lines.
534
576
  */
535
577
  rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_DETAIL", INT2FIX(PG_DIAG_MESSAGE_DETAIL));
536
-
537
- /* #result_error_field argument constant: Hint: an optional suggestion
538
- * what to do about the problem. This is intended to differ from detail
539
- * in that it offers advice (potentially inappropriate) rather than
540
- * hard facts. Might run to multiple lines.
578
+ /* Result#result_error_field argument constant
579
+ *
580
+ * Hint: an optional suggestion what to do about the problem.
581
+ * This is intended to differ from detail in that it offers advice (potentially inappropriate) rather than hard facts.
582
+ * Might run to multiple lines.
541
583
  */
542
-
543
584
  rb_define_const(rb_mPGconstants, "PG_DIAG_MESSAGE_HINT", INT2FIX(PG_DIAG_MESSAGE_HINT));
544
- /* #result_error_field argument constant: A string containing a decimal
545
- * integer indicating an error cursor position as an index into the
546
- * original statement string. The rst character has index 1, and
547
- * positions are measured in characters not bytes.
585
+ /* Result#result_error_field argument constant
586
+ *
587
+ * A string containing a decimal integer indicating an error cursor position as an index into the original statement string.
588
+ *
589
+ * The first character has index 1, and positions are measured in characters not bytes.
548
590
  */
549
-
550
591
  rb_define_const(rb_mPGconstants, "PG_DIAG_STATEMENT_POSITION", INT2FIX(PG_DIAG_STATEMENT_POSITION));
551
- /* #result_error_field argument constant: This is dened the same as
552
- * the PG_DIAG_STATEMENT_POSITION eld, but it is used when the cursor
553
- * position refers to an internally generated command rather than the
554
- * one submitted by the client. The PG_DIAG_INTERNAL_QUERY eld will
555
- * always appear when this eld appears.
592
+ /* Result#result_error_field argument constant
593
+ *
594
+ * This is defined the same as the PG_DIAG_STATEMENT_POSITION field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client.
595
+ * The PG_DIAG_INTERNAL_QUERY field will always appear when this field appears.
556
596
  */
557
-
558
597
  rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_POSITION", INT2FIX(PG_DIAG_INTERNAL_POSITION));
559
- /* #result_error_field argument constant: The text of a failed
560
- * internally-generated command. This could be, for example, a SQL
561
- * query issued by a PL/pgSQL function.
598
+ /* Result#result_error_field argument constant
599
+ *
600
+ * The text of a failed internally-generated command.
601
+ * This could be, for example, a SQL query issued by a PL/pgSQL function.
562
602
  */
563
-
564
603
  rb_define_const(rb_mPGconstants, "PG_DIAG_INTERNAL_QUERY", INT2FIX(PG_DIAG_INTERNAL_QUERY));
565
- /* #result_error_field argument constant: An indication of the context
566
- * in which the error occurred. Presently this includes a call stack
567
- * traceback of active procedural language functions and internally-generated
568
- * queries. The trace is one entry per line, most recent rst.
604
+ /* Result#result_error_field argument constant
605
+ *
606
+ * An indication of the context in which the error occurred.
607
+ * Presently this includes a call stack traceback of active procedural language functions and internally-generated queries.
608
+ * The trace is one entry per line, most recent first.
569
609
  */
570
-
571
610
  rb_define_const(rb_mPGconstants, "PG_DIAG_CONTEXT", INT2FIX(PG_DIAG_CONTEXT));
572
- /* #result_error_field argument constant: The le name of the source-code
573
- * location where the error was reported. */
611
+ /* Result#result_error_field argument constant
612
+ *
613
+ * The file name of the source-code location where the error was reported. */
574
614
  rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FILE", INT2FIX(PG_DIAG_SOURCE_FILE));
575
615
 
576
- /* #result_error_field argument constant: The line number of the
577
- * source-code location where the error was reported. */
616
+ /* Result#result_error_field argument constant
617
+ *
618
+ * The line number of the source-code location where the error was reported. */
578
619
  rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_LINE", INT2FIX(PG_DIAG_SOURCE_LINE));
579
620
 
580
- /* #result_error_field argument constant: The name of the source-code
581
- * function reporting the error. */
621
+ /* Result#result_error_field argument constant
622
+ *
623
+ * The name of the source-code function reporting the error. */
582
624
  rb_define_const(rb_mPGconstants, "PG_DIAG_SOURCE_FUNCTION", INT2FIX(PG_DIAG_SOURCE_FUNCTION));
583
625
 
584
- #ifdef HAVE_CONST_PG_DIAG_TABLE_NAME
585
- /* #result_error_field argument constant: If the error was associated with a
586
- * specific database object, the name of the schema containing that object, if any. */
626
+ #ifdef PG_DIAG_TABLE_NAME
627
+ /* Result#result_error_field argument constant
628
+ *
629
+ * If the error was associated with a specific database object, the name of the schema containing that object, if any. */
587
630
  rb_define_const(rb_mPGconstants, "PG_DIAG_SCHEMA_NAME", INT2FIX(PG_DIAG_SCHEMA_NAME));
588
631
 
589
- /* #result_error_field argument constant: If the error was associated with a
590
- *specific table, the name of the table. (When this field is present, the schema name
591
- * field provides the name of the table's schema.) */
632
+ /* Result#result_error_field argument constant
633
+ *
634
+ * If the error was associated with a specific table, the name of the table.
635
+ * (When this field is present, the schema name field provides the name of the table's schema.) */
592
636
  rb_define_const(rb_mPGconstants, "PG_DIAG_TABLE_NAME", INT2FIX(PG_DIAG_TABLE_NAME));
593
637
 
594
- /* #result_error_field argument constant: If the error was associated with a
595
- * specific table column, the name of the column. (When this field is present, the
596
- * schema and table name fields identify the table.) */
638
+ /* Result#result_error_field argument constant
639
+ *
640
+ * If the error was associated with a specific table column, the name of the column.
641
+ * (When this field is present, the schema and table name fields identify the table.) */
597
642
  rb_define_const(rb_mPGconstants, "PG_DIAG_COLUMN_NAME", INT2FIX(PG_DIAG_COLUMN_NAME));
598
643
 
599
- /* #result_error_field argument constant: If the error was associated with a
600
- * specific datatype, the name of the datatype. (When this field is present, the
601
- * schema name field provides the name of the datatype's schema.) */
644
+ /* Result#result_error_field argument constant
645
+ *
646
+ * If the error was associated with a specific datatype, the name of the datatype.
647
+ * (When this field is present, the schema name field provides the name of the datatype's schema.) */
602
648
  rb_define_const(rb_mPGconstants, "PG_DIAG_DATATYPE_NAME", INT2FIX(PG_DIAG_DATATYPE_NAME));
603
649
 
604
- /* #result_error_field argument constant: If the error was associated with a
605
- * specific constraint, the name of the constraint. The table or domain that the
606
- * constraint belongs to is reported using the fields listed above. (For this
607
- * purpose, indexes are treated as constraints, even if they weren't created with
608
- * constraint syntax.) */
650
+ /* Result#result_error_field argument constant
651
+ *
652
+ * If the error was associated with a specific constraint, the name of the constraint.
653
+ * The table or domain that the constraint belongs to is reported using the fields listed above.
654
+ * (For this purpose, indexes are treated as constraints, even if they weren't created with constraint syntax.) */
609
655
  rb_define_const(rb_mPGconstants, "PG_DIAG_CONSTRAINT_NAME", INT2FIX(PG_DIAG_CONSTRAINT_NAME));
610
656
  #endif
611
657
 
658
+ #ifdef HAVE_PQENTERPIPELINEMODE
659
+ /* Connection#pipeline_status constant
660
+ *
661
+ * The libpq connection is in pipeline mode.
662
+ */
663
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ON", INT2FIX(PQ_PIPELINE_ON));
664
+
665
+ /* Connection#pipeline_status constant
666
+ *
667
+ * The libpq connection is not in pipeline mode.
668
+ */
669
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_OFF", INT2FIX(PQ_PIPELINE_OFF));
670
+
671
+ /* Connection#pipeline_status constant
672
+ *
673
+ * The libpq connection is in pipeline mode and an error occurred while processing the current pipeline.
674
+ * The aborted flag is cleared when PQgetResult returns a result of type PGRES_PIPELINE_SYNC.
675
+ */
676
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ABORTED", INT2FIX(PQ_PIPELINE_ABORTED));
677
+ #endif
678
+
612
679
  /* Invalid OID constant */
613
680
  rb_define_const(rb_mPGconstants, "INVALID_OID", INT2FIX(InvalidOid));
614
681
  rb_define_const(rb_mPGconstants, "InvalidOid", INT2FIX(InvalidOid));
@@ -635,6 +702,7 @@ Init_pg_ext()
635
702
  init_pg_binary_encoder();
636
703
  init_pg_binary_decoder();
637
704
  init_pg_copycoder();
705
+ init_pg_recordcoder();
638
706
  init_pg_tuple();
639
707
  }
640
708
 
data/ext/pg.h CHANGED
@@ -11,6 +11,7 @@
11
11
  #include <sys/types.h>
12
12
  #if !defined(_WIN32)
13
13
  # include <sys/time.h>
14
+ # include <sys/socket.h>
14
15
  #endif
15
16
  #if defined(HAVE_UNISTD_H) && !defined(_WIN32)
16
17
  # include <unistd.h>
@@ -78,6 +79,17 @@ typedef long suseconds_t;
78
79
  #define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
79
80
  #endif
80
81
 
82
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
83
+ #define pg_compact_callback(x) (x)
84
+ #define pg_gc_location(x) x = rb_gc_location(x)
85
+ #else
86
+ #define rb_gc_mark_movable(x) rb_gc_mark(x)
87
+ #define pg_compact_callback(x) {(x)}
88
+ #define pg_gc_location(x) UNUSED(x)
89
+ #endif
90
+
91
+ #define PG_ENC_IDX_BITS 28
92
+
81
93
  /* The data behind each PG::Connection object */
82
94
  typedef struct {
83
95
  PGconn *pgconn;
@@ -94,15 +106,21 @@ typedef struct {
94
106
  VALUE type_map_for_results;
95
107
  /* IO object internally used for the trace stream */
96
108
  VALUE trace_stream;
97
- /* Cached Encoding object */
98
- VALUE external_encoding;
99
109
  /* Kind of PG::Coder object for casting ruby values to COPY rows */
100
110
  VALUE encoder_for_put_copy_data;
101
111
  /* Kind of PG::Coder object for casting COPY rows to ruby values */
102
112
  VALUE decoder_for_get_copy_data;
113
+ /* Ruby encoding index of the client/internal encoding */
114
+ int enc_idx : PG_ENC_IDX_BITS;
115
+ /* flags controlling Symbol/String field names */
116
+ unsigned int flags : 2;
117
+ /* enable automatic flushing of send data at the end of send_query calls */
118
+ unsigned int flush_data : 1;
103
119
 
104
- /* enable/disable guessing size of PGresult's allocated memory */
105
- int guess_result_memsize;
120
+ #if defined(_WIN32)
121
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
122
+ int ruby_sd;
123
+ #endif
106
124
  } t_pg_connection;
107
125
 
108
126
  typedef struct pg_coder t_pg_coder;
@@ -123,10 +141,16 @@ typedef struct {
123
141
  */
124
142
  t_typemap *p_typemap;
125
143
 
144
+ /* Ruby encoding index of the client/internal encoding */
145
+ int enc_idx : PG_ENC_IDX_BITS;
146
+
126
147
  /* 0 = PGresult is cleared by PG::Result#clear or by the GC
127
148
  * 1 = PGresult is cleared internally by libpq
128
149
  */
129
- int autoclear;
150
+ unsigned int autoclear : 1;
151
+
152
+ /* flags controlling Symbol/String field names */
153
+ unsigned int flags : 2;
130
154
 
131
155
  /* Number of fields in fnames[] .
132
156
  * Set to -1 if fnames[] is not yet initialized.
@@ -142,7 +166,7 @@ typedef struct {
142
166
  /* Hash with fnames[] to field number mapping. */
143
167
  VALUE field_map;
144
168
 
145
- /* List of field names as frozen String objects.
169
+ /* List of field names as frozen String or Symbol objects.
146
170
  * Only valid if nfields != -1
147
171
  */
148
172
  VALUE fnames[0];
@@ -158,6 +182,10 @@ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
158
182
  typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
159
183
  typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
160
184
 
185
+ #define PG_RESULT_FIELD_NAMES_MASK 0x03
186
+ #define PG_RESULT_FIELD_NAMES_SYMBOL 0x01
187
+ #define PG_RESULT_FIELD_NAMES_STATIC_SYMBOL 0x02
188
+
161
189
  #define PG_CODER_TIMESTAMP_DB_UTC 0x0
162
190
  #define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
163
191
  #define PG_CODER_TIMESTAMP_APP_UTC 0x0
@@ -204,6 +232,8 @@ typedef struct {
204
232
  } convs[0];
205
233
  } t_tmbc;
206
234
 
235
+ extern const rb_data_type_t pg_typemap_type;
236
+ extern const rb_data_type_t pg_coder_type;
207
237
 
208
238
  #include "gvl_wrappers.h"
209
239
 
@@ -270,6 +300,7 @@ void init_pg_type_map_by_oid _(( void ));
270
300
  void init_pg_type_map_in_ruby _(( void ));
271
301
  void init_pg_coder _(( void ));
272
302
  void init_pg_copycoder _(( void ));
303
+ void init_pg_recordcoder _(( void ));
273
304
  void init_pg_text_encoder _(( void ));
274
305
  void init_pg_text_decoder _(( void ));
275
306
  void init_pg_binary_encoder _(( void ));
@@ -287,6 +318,7 @@ VALUE pg_obj_to_i _(( VALUE ));
287
318
  VALUE pg_tmbc_allocate _(( void ));
288
319
  void pg_coder_init_encoder _(( VALUE ));
289
320
  void pg_coder_init_decoder _(( VALUE ));
321
+ void pg_coder_compact _(( void * ));
290
322
  char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
291
323
 
292
324
  #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
@@ -300,20 +332,19 @@ char *pg_rb_str_ensure_capa _(( VALUE, long, char *,
300
332
  (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
301
333
  )
302
334
 
303
- #define PG_RB_TAINTED_STR_NEW( str, curr_ptr, end_ptr ) ( \
304
- (str) = rb_tainted_str_new( NULL, 0 ), \
305
- (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
306
- )
307
-
308
335
  VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
309
336
  VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
310
337
  int pg_typemap_fit_to_copy_get _(( VALUE ));
311
338
  VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
312
339
  t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
313
340
  VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
341
+ void pg_typemap_mark _(( void * ));
342
+ size_t pg_typemap_memsize _(( const void * ));
343
+ void pg_typemap_compact _(( void * ));
314
344
 
315
345
  PGconn *pg_get_pgconn _(( VALUE ));
316
346
  t_pg_connection *pg_get_connection _(( VALUE ));
347
+ VALUE pgconn_block _(( int, VALUE *, VALUE ));
317
348
 
318
349
  VALUE pg_new_result _(( PGresult *, VALUE ));
319
350
  VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
@@ -328,12 +359,7 @@ VALUE pg_tuple_new _(( VALUE, int ));
328
359
  static inline t_pg_result *
329
360
  pgresult_get_this( VALUE self )
330
361
  {
331
- t_pg_result *this = RTYPEDDATA_DATA(self);
332
-
333
- if( this == NULL )
334
- rb_raise(rb_ePGerror, "result has been cleared");
335
-
336
- return this;
362
+ return RTYPEDDATA_DATA(self);
337
363
  }
338
364
 
339
365