pg 1.2.3 → 1.4.6

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 (119) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.appveyor.yml +42 -0
  4. data/.gems +6 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +137 -0
  7. data/.gitignore +19 -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.md +804 -0
  16. data/Manifest.txt +0 -1
  17. data/README.ja.md +266 -0
  18. data/README.md +272 -0
  19. data/Rakefile +33 -135
  20. data/Rakefile.cross +12 -13
  21. data/certs/ged.pem +24 -0
  22. data/certs/larskanis-2022.pem +26 -0
  23. data/certs/larskanis-2023.pem +24 -0
  24. data/ext/errorcodes.def +12 -0
  25. data/ext/errorcodes.rb +0 -0
  26. data/ext/errorcodes.txt +4 -1
  27. data/ext/extconf.rb +100 -25
  28. data/ext/gvl_wrappers.c +4 -0
  29. data/ext/gvl_wrappers.h +23 -0
  30. data/ext/pg.c +62 -29
  31. data/ext/pg.h +20 -1
  32. data/ext/pg_binary_decoder.c +1 -1
  33. data/ext/pg_binary_encoder.c +1 -1
  34. data/ext/pg_coder.c +83 -29
  35. data/ext/pg_connection.c +953 -670
  36. data/ext/pg_copy_coder.c +46 -17
  37. data/ext/pg_errors.c +1 -1
  38. data/ext/pg_record_coder.c +46 -16
  39. data/ext/pg_result.c +88 -49
  40. data/ext/pg_text_decoder.c +2 -2
  41. data/ext/pg_text_encoder.c +7 -7
  42. data/ext/pg_tuple.c +50 -30
  43. data/ext/pg_type_map.c +42 -9
  44. data/ext/pg_type_map_all_strings.c +16 -2
  45. data/ext/pg_type_map_by_class.c +50 -25
  46. data/ext/pg_type_map_by_column.c +68 -30
  47. data/ext/pg_type_map_by_mri_type.c +48 -19
  48. data/ext/pg_type_map_by_oid.c +53 -24
  49. data/ext/pg_type_map_in_ruby.c +51 -20
  50. data/ext/pg_util.c +2 -2
  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/coder.rb +1 -1
  56. data/lib/pg/connection.rb +669 -81
  57. data/lib/pg/exceptions.rb +14 -1
  58. data/lib/pg/version.rb +4 -0
  59. data/lib/pg.rb +45 -36
  60. data/misc/openssl-pg-segfault.rb +31 -0
  61. data/misc/postgres/History.txt +9 -0
  62. data/misc/postgres/Manifest.txt +5 -0
  63. data/misc/postgres/README.txt +21 -0
  64. data/misc/postgres/Rakefile +21 -0
  65. data/misc/postgres/lib/postgres.rb +16 -0
  66. data/misc/ruby-pg/History.txt +9 -0
  67. data/misc/ruby-pg/Manifest.txt +5 -0
  68. data/misc/ruby-pg/README.txt +21 -0
  69. data/misc/ruby-pg/Rakefile +21 -0
  70. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  71. data/pg.gemspec +34 -0
  72. data/rakelib/task_extension.rb +46 -0
  73. data/sample/array_insert.rb +20 -0
  74. data/sample/async_api.rb +102 -0
  75. data/sample/async_copyto.rb +39 -0
  76. data/sample/async_mixed.rb +56 -0
  77. data/sample/check_conn.rb +21 -0
  78. data/sample/copydata.rb +71 -0
  79. data/sample/copyfrom.rb +81 -0
  80. data/sample/copyto.rb +19 -0
  81. data/sample/cursor.rb +21 -0
  82. data/sample/disk_usage_report.rb +177 -0
  83. data/sample/issue-119.rb +94 -0
  84. data/sample/losample.rb +69 -0
  85. data/sample/minimal-testcase.rb +17 -0
  86. data/sample/notify_wait.rb +72 -0
  87. data/sample/pg_statistics.rb +285 -0
  88. data/sample/replication_monitor.rb +222 -0
  89. data/sample/test_binary_values.rb +33 -0
  90. data/sample/wal_shipper.rb +434 -0
  91. data/sample/warehouse_partitions.rb +311 -0
  92. data/translation/.po4a-version +7 -0
  93. data/translation/po/all.pot +875 -0
  94. data/translation/po/ja.po +868 -0
  95. data/translation/po4a.cfg +9 -0
  96. data.tar.gz.sig +0 -0
  97. metadata +120 -206
  98. metadata.gz.sig +0 -0
  99. data/ChangeLog +0 -0
  100. data/History.rdoc +0 -578
  101. data/README.ja.rdoc +0 -13
  102. data/README.rdoc +0 -213
  103. data/lib/pg/basic_type_mapping.rb +0 -522
  104. data/spec/data/expected_trace.out +0 -26
  105. data/spec/data/random_binary_data +0 -0
  106. data/spec/helpers.rb +0 -380
  107. data/spec/pg/basic_type_mapping_spec.rb +0 -630
  108. data/spec/pg/connection_spec.rb +0 -1949
  109. data/spec/pg/connection_sync_spec.rb +0 -41
  110. data/spec/pg/result_spec.rb +0 -681
  111. data/spec/pg/tuple_spec.rb +0 -333
  112. data/spec/pg/type_map_by_class_spec.rb +0 -138
  113. data/spec/pg/type_map_by_column_spec.rb +0 -226
  114. data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
  115. data/spec/pg/type_map_by_oid_spec.rb +0 -149
  116. data/spec/pg/type_map_in_ruby_spec.rb +0 -164
  117. data/spec/pg/type_map_spec.rb +0 -22
  118. data/spec/pg/type_spec.rb +0 -1123
  119. data/spec/pg_spec.rb +0 -50
data/ext/gvl_wrappers.h CHANGED
@@ -17,6 +17,10 @@
17
17
 
18
18
  #include <ruby/thread.h>
19
19
 
20
+ #ifdef RUBY_EXTCONF_H
21
+ # include RUBY_EXTCONF_H
22
+ #endif
23
+
20
24
  #define DEFINE_PARAM_LIST1(type, name) \
21
25
  name,
22
26
 
@@ -46,6 +50,7 @@
46
50
  return NULL; \
47
51
  }
48
52
 
53
+ #ifdef ENABLE_GVL_UNLOCK
49
54
  #define DEFINE_GVL_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
50
55
  rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
51
56
  struct gvl_wrapper_##name##_params params = { \
@@ -54,6 +59,13 @@
54
59
  rb_thread_call_without_gvl(gvl_##name##_skeleton, &params, RUBY_UBF_IO, 0); \
55
60
  when_non_void( return params.retval; ) \
56
61
  }
62
+ #else
63
+ #define DEFINE_GVL_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
64
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
65
+ when_non_void( return ) \
66
+ name( FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST1) lastparamname ); \
67
+ }
68
+ #endif
57
69
 
58
70
  #define DEFINE_GVL_STUB_DECL(name, when_non_void, rettype, lastparamtype, lastparamname) \
59
71
  rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname);
@@ -66,6 +78,7 @@
66
78
  return NULL; \
67
79
  }
68
80
 
81
+ #ifdef ENABLE_GVL_UNLOCK
69
82
  #define DEFINE_GVLCB_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
70
83
  rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
71
84
  struct gvl_wrapper_##name##_params params = { \
@@ -74,6 +87,13 @@
74
87
  rb_thread_call_with_gvl(gvl_##name##_skeleton, &params); \
75
88
  when_non_void( return params.retval; ) \
76
89
  }
90
+ #else
91
+ #define DEFINE_GVLCB_STUB(name, when_non_void, rettype, lastparamtype, lastparamname) \
92
+ rettype gvl_##name(FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST3) lastparamtype lastparamname){ \
93
+ when_non_void( return ) \
94
+ name( FOR_EACH_PARAM_OF_##name(DEFINE_PARAM_LIST1) lastparamname ); \
95
+ }
96
+ #endif
77
97
 
78
98
  #define GVL_TYPE_VOID(string)
79
99
  #define GVL_TYPE_NONVOID(string) string
@@ -95,6 +115,8 @@
95
115
 
96
116
  #define FOR_EACH_PARAM_OF_PQresetPoll(param)
97
117
 
118
+ #define FOR_EACH_PARAM_OF_PQping(param)
119
+
98
120
  #define FOR_EACH_PARAM_OF_PQexec(param) \
99
121
  param(PGconn *, conn)
100
122
 
@@ -196,6 +218,7 @@
196
218
  function(PQreset, GVL_TYPE_VOID, void, PGconn *, conn) \
197
219
  function(PQresetStart, GVL_TYPE_NONVOID, int, PGconn *, conn) \
198
220
  function(PQresetPoll, GVL_TYPE_NONVOID, PostgresPollingStatusType, PGconn *, conn) \
221
+ function(PQping, GVL_TYPE_NONVOID, PGPing, const char *, conninfo) \
199
222
  function(PQexec, GVL_TYPE_NONVOID, PGresult *, const char *, command) \
200
223
  function(PQexecParams, GVL_TYPE_NONVOID, PGresult *, int, resultFormat) \
201
224
  function(PQexecPrepared, GVL_TYPE_NONVOID, PGresult *, int, resultFormat) \
data/ext/pg.c CHANGED
@@ -126,26 +126,6 @@ const char * const (pg_enc_pg2ruby_mapping[][2]) = {
126
126
  static struct st_table *enc_pg2ruby;
127
127
 
128
128
 
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
129
  /*
150
130
  * Return the given PostgreSQL encoding ID as an rb_encoding.
151
131
  *
@@ -186,10 +166,6 @@ pg_get_pg_encname_as_rb_encoding( const char *pg_encname )
186
166
  return rb_enc_find( pg_enc_pg2ruby_mapping[i][1] );
187
167
  }
188
168
 
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
169
  /* Fallthrough to ASCII-8BIT */
194
170
  return rb_ascii8bit_encoding();
195
171
  }
@@ -376,7 +352,7 @@ pg_s_init_ssl(VALUE self, VALUE do_ssl)
376
352
  **************************************************************************/
377
353
 
378
354
  void
379
- Init_pg_ext()
355
+ Init_pg_ext(void)
380
356
  {
381
357
  if( RTEST(rb_eval_string("ENV['PG_SKIP_DEPRECATION_WARNING']")) ){
382
358
  /* Set all bits to disable all deprecation warnings. */
@@ -415,14 +391,34 @@ Init_pg_ext()
415
391
  rb_define_const(rb_mPGconstants, "CONNECTION_MADE", INT2FIX(CONNECTION_MADE));
416
392
  /* Waiting for a response from the server. */
417
393
  rb_define_const(rb_mPGconstants, "CONNECTION_AWAITING_RESPONSE", INT2FIX(CONNECTION_AWAITING_RESPONSE));
418
- /* Received authentication; waiting for backend start-up to finish. */
394
+ /* Received authentication; waiting for backend startup. */
419
395
  rb_define_const(rb_mPGconstants, "CONNECTION_AUTH_OK", INT2FIX(CONNECTION_AUTH_OK));
396
+ /* This state is no longer used. */
397
+ rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
420
398
  /* Negotiating SSL encryption. */
421
399
  rb_define_const(rb_mPGconstants, "CONNECTION_SSL_STARTUP", INT2FIX(CONNECTION_SSL_STARTUP));
422
- /* Negotiating environment-driven parameter settings. */
423
- rb_define_const(rb_mPGconstants, "CONNECTION_SETENV", INT2FIX(CONNECTION_SETENV));
424
400
  /* Internal state - PG.connect() needed. */
425
401
  rb_define_const(rb_mPGconstants, "CONNECTION_NEEDED", INT2FIX(CONNECTION_NEEDED));
402
+ #if PG_MAJORVERSION_NUM >= 10
403
+ /* Checking if session is read-write. Available since PostgreSQL-10. */
404
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_WRITABLE", INT2FIX(CONNECTION_CHECK_WRITABLE));
405
+ #endif
406
+ #if PG_MAJORVERSION_NUM >= 10
407
+ /* Consuming any extra messages. Available since PostgreSQL-10. */
408
+ rb_define_const(rb_mPGconstants, "CONNECTION_CONSUME", INT2FIX(CONNECTION_CONSUME));
409
+ #endif
410
+ #if PG_MAJORVERSION_NUM >= 12
411
+ /* Negotiating GSSAPI. Available since PostgreSQL-12. */
412
+ rb_define_const(rb_mPGconstants, "CONNECTION_GSS_STARTUP", INT2FIX(CONNECTION_GSS_STARTUP));
413
+ #endif
414
+ #if PG_MAJORVERSION_NUM >= 13
415
+ /* Checking target server properties. Available since PostgreSQL-13. */
416
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_TARGET", INT2FIX(CONNECTION_CHECK_TARGET));
417
+ #endif
418
+ #if PG_MAJORVERSION_NUM >= 14
419
+ /* Checking if server is in standby mode. Available since PostgreSQL-14. */
420
+ rb_define_const(rb_mPGconstants, "CONNECTION_CHECK_STANDBY", INT2FIX(CONNECTION_CHECK_STANDBY));
421
+ #endif
426
422
 
427
423
  /****** PG::Connection CLASS CONSTANTS: Nonblocking connection polling status ******/
428
424
 
@@ -526,6 +522,19 @@ Init_pg_ext()
526
522
  /* Result#result_status constant - Single tuple from larger resultset. */
527
523
  rb_define_const(rb_mPGconstants, "PGRES_SINGLE_TUPLE", INT2FIX(PGRES_SINGLE_TUPLE));
528
524
 
525
+ #ifdef HAVE_PQENTERPIPELINEMODE
526
+ /* Result#result_status constant - The PG::Result represents a synchronization point in pipeline mode, requested by Connection#pipeline_sync.
527
+ *
528
+ * This status occurs only when pipeline mode has been selected. */
529
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_SYNC", INT2FIX(PGRES_PIPELINE_SYNC));
530
+
531
+ /* Result#result_status constant - The PG::Result represents a pipeline that has received an error from the server.
532
+ *
533
+ * 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.
534
+ */
535
+ rb_define_const(rb_mPGconstants, "PGRES_PIPELINE_ABORTED", INT2FIX(PGRES_PIPELINE_ABORTED));
536
+ #endif
537
+
529
538
  /****** Result CONSTANTS: result error field codes ******/
530
539
 
531
540
  /* Result#result_error_field argument constant
@@ -549,7 +558,7 @@ Init_pg_ext()
549
558
  /* Result#result_error_field argument constant
550
559
  *
551
560
  * The SQLSTATE code for the error.
552
- * The SQLSTATE code identies the type of error that has occurred; it can be used by front-end applications to perform specic operations (such as error handling) in response to a particular database error.
561
+ * 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.
553
562
  * For a list of the possible SQLSTATE codes, see Appendix A.
554
563
  * This field is not localizable, and is always present.
555
564
  */
@@ -645,10 +654,34 @@ Init_pg_ext()
645
654
  rb_define_const(rb_mPGconstants, "PG_DIAG_CONSTRAINT_NAME", INT2FIX(PG_DIAG_CONSTRAINT_NAME));
646
655
  #endif
647
656
 
657
+ #ifdef HAVE_PQENTERPIPELINEMODE
658
+ /* Connection#pipeline_status constant
659
+ *
660
+ * The libpq connection is in pipeline mode.
661
+ */
662
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ON", INT2FIX(PQ_PIPELINE_ON));
663
+
664
+ /* Connection#pipeline_status constant
665
+ *
666
+ * The libpq connection is not in pipeline mode.
667
+ */
668
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_OFF", INT2FIX(PQ_PIPELINE_OFF));
669
+
670
+ /* Connection#pipeline_status constant
671
+ *
672
+ * The libpq connection is in pipeline mode and an error occurred while processing the current pipeline.
673
+ * The aborted flag is cleared when PQgetResult returns a result of type PGRES_PIPELINE_SYNC.
674
+ */
675
+ rb_define_const(rb_mPGconstants, "PQ_PIPELINE_ABORTED", INT2FIX(PQ_PIPELINE_ABORTED));
676
+ #endif
677
+
648
678
  /* Invalid OID constant */
649
679
  rb_define_const(rb_mPGconstants, "INVALID_OID", INT2FIX(InvalidOid));
650
680
  rb_define_const(rb_mPGconstants, "InvalidOid", INT2FIX(InvalidOid));
651
681
 
682
+ /* PostgreSQL compiled in default port */
683
+ rb_define_const(rb_mPGconstants, "DEF_PGPORT", INT2FIX(DEF_PGPORT));
684
+
652
685
  /* Add the constants to the toplevel namespace */
653
686
  rb_include_module( rb_mPG, rb_mPGconstants );
654
687
 
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>
@@ -56,6 +57,7 @@
56
57
  #endif
57
58
 
58
59
  /* PostgreSQL headers */
60
+ #include "pg_config.h"
59
61
  #include "libpq-fe.h"
60
62
  #include "libpq/libpq-fs.h" /* large-object interface */
61
63
  #include "pg_config_manual.h"
@@ -78,6 +80,15 @@ typedef long suseconds_t;
78
80
  #define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
79
81
  #endif
80
82
 
83
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
84
+ #define pg_compact_callback(x) (x)
85
+ #define pg_gc_location(x) x = rb_gc_location(x)
86
+ #else
87
+ #define rb_gc_mark_movable(x) rb_gc_mark(x)
88
+ #define pg_compact_callback(x) {(x)}
89
+ #define pg_gc_location(x) UNUSED(x)
90
+ #endif
91
+
81
92
  #define PG_ENC_IDX_BITS 28
82
93
 
83
94
  /* The data behind each PG::Connection object */
@@ -104,6 +115,8 @@ typedef struct {
104
115
  int enc_idx : PG_ENC_IDX_BITS;
105
116
  /* flags controlling Symbol/String field names */
106
117
  unsigned int flags : 2;
118
+ /* enable automatic flushing of send data at the end of send_query calls */
119
+ unsigned int flush_data : 1;
107
120
 
108
121
  #if defined(_WIN32)
109
122
  /* File descriptor to be used for rb_w32_unwrap_io_handle() */
@@ -220,6 +233,8 @@ typedef struct {
220
233
  } convs[0];
221
234
  } t_tmbc;
222
235
 
236
+ extern const rb_data_type_t pg_typemap_type;
237
+ extern const rb_data_type_t pg_coder_type;
223
238
 
224
239
  #include "gvl_wrappers.h"
225
240
 
@@ -304,7 +319,7 @@ VALUE pg_obj_to_i _(( VALUE ));
304
319
  VALUE pg_tmbc_allocate _(( void ));
305
320
  void pg_coder_init_encoder _(( VALUE ));
306
321
  void pg_coder_init_decoder _(( VALUE ));
307
- void pg_coder_mark _(( t_pg_coder * ));
322
+ void pg_coder_compact _(( void * ));
308
323
  char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
309
324
 
310
325
  #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
@@ -324,9 +339,13 @@ int pg_typemap_fit_to_copy_get _(( VALUE ));
324
339
  VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
325
340
  t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
326
341
  VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
342
+ void pg_typemap_mark _(( void * ));
343
+ size_t pg_typemap_memsize _(( const void * ));
344
+ void pg_typemap_compact _(( void * ));
327
345
 
328
346
  PGconn *pg_get_pgconn _(( VALUE ));
329
347
  t_pg_connection *pg_get_connection _(( VALUE ));
348
+ VALUE pgconn_block _(( int, VALUE *, VALUE ));
330
349
 
331
350
  VALUE pg_new_result _(( PGresult *, VALUE ));
332
351
  VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
@@ -205,7 +205,7 @@ pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int
205
205
  */
206
206
 
207
207
  void
208
- init_pg_binary_decoder()
208
+ init_pg_binary_decoder(void)
209
209
  {
210
210
  /* This module encapsulates all decoder classes with binary input format */
211
211
  rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" );
@@ -139,7 +139,7 @@ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermed
139
139
  }
140
140
 
141
141
  void
142
- init_pg_binary_encoder()
142
+ init_pg_binary_encoder(void)
143
143
  {
144
144
  /* This module encapsulates all encoder classes with binary output format */
145
145
  rb_mPG_BinaryEncoder = rb_define_module_under( rb_mPG, "BinaryEncoder" );
data/ext/pg_coder.c CHANGED
@@ -26,11 +26,11 @@ pg_coder_allocate( VALUE klass )
26
26
  void
27
27
  pg_coder_init_encoder( VALUE self )
28
28
  {
29
- t_pg_coder *this = DATA_PTR( self );
29
+ t_pg_coder *this = RTYPEDDATA_DATA( self );
30
30
  VALUE klass = rb_class_of(self);
31
31
  if( rb_const_defined( klass, s_id_CFUNC ) ){
32
32
  VALUE cfunc = rb_const_get( klass, s_id_CFUNC );
33
- this->enc_func = DATA_PTR(cfunc);
33
+ this->enc_func = RTYPEDDATA_DATA(cfunc);
34
34
  } else {
35
35
  this->enc_func = NULL;
36
36
  }
@@ -45,12 +45,12 @@ pg_coder_init_encoder( VALUE self )
45
45
  void
46
46
  pg_coder_init_decoder( VALUE self )
47
47
  {
48
- t_pg_coder *this = DATA_PTR( self );
48
+ t_pg_coder *this = RTYPEDDATA_DATA( self );
49
49
  VALUE klass = rb_class_of(self);
50
50
  this->enc_func = NULL;
51
51
  if( rb_const_defined( klass, s_id_CFUNC ) ){
52
52
  VALUE cfunc = rb_const_get( klass, s_id_CFUNC );
53
- this->dec_func = DATA_PTR(cfunc);
53
+ this->dec_func = RTYPEDDATA_DATA(cfunc);
54
54
  } else {
55
55
  this->dec_func = NULL;
56
56
  }
@@ -61,32 +61,74 @@ pg_coder_init_decoder( VALUE self )
61
61
  rb_iv_set( self, "@name", Qnil );
62
62
  }
63
63
 
64
+ static size_t
65
+ pg_coder_memsize(const void *_this)
66
+ {
67
+ const t_pg_coder *this = (const t_pg_coder *)_this;
68
+ return sizeof(*this);
69
+ }
70
+
71
+ static size_t
72
+ pg_composite_coder_memsize(const void *_this)
73
+ {
74
+ const t_pg_composite_coder *this = (const t_pg_composite_coder *)_this;
75
+ return sizeof(*this);
76
+ }
77
+
64
78
  void
65
- pg_coder_mark(t_pg_coder *this)
79
+ pg_coder_compact(void *_this)
66
80
  {
67
- rb_gc_mark(this->coder_obj);
81
+ t_pg_coder *this = (t_pg_coder *)_this;
82
+ pg_gc_location(this->coder_obj);
68
83
  }
69
84
 
70
85
  static void
71
- pg_composite_coder_mark(t_pg_composite_coder *this)
86
+ pg_composite_coder_compact(void *_this)
72
87
  {
73
- pg_coder_mark(&this->comp);
88
+ t_pg_composite_coder *this = (t_pg_composite_coder *)_this;
89
+ pg_coder_compact(&this->comp);
74
90
  }
75
91
 
92
+ const rb_data_type_t pg_coder_type = {
93
+ "PG::Coder",
94
+ {
95
+ (RUBY_DATA_FUNC) NULL,
96
+ RUBY_TYPED_DEFAULT_FREE,
97
+ pg_coder_memsize,
98
+ pg_compact_callback(pg_coder_compact),
99
+ },
100
+ 0,
101
+ 0,
102
+ RUBY_TYPED_FREE_IMMEDIATELY,
103
+ };
104
+
76
105
  static VALUE
77
106
  pg_simple_encoder_allocate( VALUE klass )
78
107
  {
79
108
  t_pg_coder *this;
80
- VALUE self = Data_Make_Struct( klass, t_pg_coder, pg_coder_mark, -1, this );
109
+ VALUE self = TypedData_Make_Struct( klass, t_pg_coder, &pg_coder_type, this );
81
110
  pg_coder_init_encoder( self );
82
111
  return self;
83
112
  }
84
113
 
114
+ static const rb_data_type_t pg_composite_coder_type = {
115
+ "PG::CompositeCoder",
116
+ {
117
+ (RUBY_DATA_FUNC) NULL,
118
+ RUBY_TYPED_DEFAULT_FREE,
119
+ pg_composite_coder_memsize,
120
+ pg_compact_callback(pg_composite_coder_compact),
121
+ },
122
+ &pg_coder_type,
123
+ 0,
124
+ RUBY_TYPED_FREE_IMMEDIATELY,
125
+ };
126
+
85
127
  static VALUE
86
128
  pg_composite_encoder_allocate( VALUE klass )
87
129
  {
88
130
  t_pg_composite_coder *this;
89
- VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, pg_composite_coder_mark, -1, this );
131
+ VALUE self = TypedData_Make_Struct( klass, t_pg_composite_coder, &pg_composite_coder_type, this );
90
132
  pg_coder_init_encoder( self );
91
133
  this->elem = NULL;
92
134
  this->needs_quotation = 1;
@@ -99,7 +141,7 @@ static VALUE
99
141
  pg_simple_decoder_allocate( VALUE klass )
100
142
  {
101
143
  t_pg_coder *this;
102
- VALUE self = Data_Make_Struct( klass, t_pg_coder, pg_coder_mark, -1, this );
144
+ VALUE self = TypedData_Make_Struct( klass, t_pg_coder, &pg_coder_type, this );
103
145
  pg_coder_init_decoder( self );
104
146
  return self;
105
147
  }
@@ -108,7 +150,7 @@ static VALUE
108
150
  pg_composite_decoder_allocate( VALUE klass )
109
151
  {
110
152
  t_pg_composite_coder *this;
111
- VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, pg_composite_coder_mark, -1, this );
153
+ VALUE self = TypedData_Make_Struct( klass, t_pg_composite_coder, &pg_composite_coder_type, this );
112
154
  pg_coder_init_decoder( self );
113
155
  this->elem = NULL;
114
156
  this->needs_quotation = 1;
@@ -135,7 +177,7 @@ pg_coder_encode(int argc, VALUE *argv, VALUE self)
135
177
  VALUE value;
136
178
  int len, len2;
137
179
  int enc_idx;
138
- t_pg_coder *this = DATA_PTR(self);
180
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
139
181
 
140
182
  if(argc < 1 || argc > 2){
141
183
  rb_raise(rb_eArgError, "wrong number of arguments (%i for 1..2)", argc);
@@ -192,7 +234,7 @@ pg_coder_decode(int argc, VALUE *argv, VALUE self)
192
234
  int tuple = -1;
193
235
  int field = -1;
194
236
  VALUE res;
195
- t_pg_coder *this = DATA_PTR(self);
237
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
196
238
 
197
239
  if(argc < 1 || argc > 3){
198
240
  rb_raise(rb_eArgError, "wrong number of arguments (%i for 1..3)", argc);
@@ -213,7 +255,7 @@ pg_coder_decode(int argc, VALUE *argv, VALUE self)
213
255
  rb_raise(rb_eRuntimeError, "no decoder function defined");
214
256
  }
215
257
 
216
- res = this->dec_func(this, val, RSTRING_LEN(argv[0]), tuple, field, ENCODING_GET(argv[0]));
258
+ res = this->dec_func(this, val, RSTRING_LENINT(argv[0]), tuple, field, ENCODING_GET(argv[0]));
217
259
 
218
260
  return res;
219
261
  }
@@ -230,7 +272,7 @@ pg_coder_decode(int argc, VALUE *argv, VALUE self)
230
272
  static VALUE
231
273
  pg_coder_oid_set(VALUE self, VALUE oid)
232
274
  {
233
- t_pg_coder *this = DATA_PTR(self);
275
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
234
276
  this->oid = NUM2UINT(oid);
235
277
  return oid;
236
278
  }
@@ -245,7 +287,7 @@ pg_coder_oid_set(VALUE self, VALUE oid)
245
287
  static VALUE
246
288
  pg_coder_oid_get(VALUE self)
247
289
  {
248
- t_pg_coder *this = DATA_PTR(self);
290
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
249
291
  return UINT2NUM(this->oid);
250
292
  }
251
293
 
@@ -261,7 +303,7 @@ pg_coder_oid_get(VALUE self)
261
303
  static VALUE
262
304
  pg_coder_format_set(VALUE self, VALUE format)
263
305
  {
264
- t_pg_coder *this = DATA_PTR(self);
306
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
265
307
  this->format = NUM2INT(format);
266
308
  return format;
267
309
  }
@@ -276,7 +318,7 @@ pg_coder_format_set(VALUE self, VALUE format)
276
318
  static VALUE
277
319
  pg_coder_format_get(VALUE self)
278
320
  {
279
- t_pg_coder *this = DATA_PTR(self);
321
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
280
322
  return INT2NUM(this->format);
281
323
  }
282
324
 
@@ -292,7 +334,7 @@ pg_coder_format_get(VALUE self)
292
334
  static VALUE
293
335
  pg_coder_flags_set(VALUE self, VALUE flags)
294
336
  {
295
- t_pg_coder *this = DATA_PTR(self);
337
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
296
338
  this->flags = NUM2INT(flags);
297
339
  return flags;
298
340
  }
@@ -306,7 +348,7 @@ pg_coder_flags_set(VALUE self, VALUE flags)
306
348
  static VALUE
307
349
  pg_coder_flags_get(VALUE self)
308
350
  {
309
- t_pg_coder *this = DATA_PTR(self);
351
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
310
352
  return INT2NUM(this->flags);
311
353
  }
312
354
 
@@ -323,7 +365,7 @@ pg_coder_flags_get(VALUE self)
323
365
  static VALUE
324
366
  pg_coder_needs_quotation_set(VALUE self, VALUE needs_quotation)
325
367
  {
326
- t_pg_composite_coder *this = DATA_PTR(self);
368
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
327
369
  this->needs_quotation = RTEST(needs_quotation);
328
370
  return needs_quotation;
329
371
  }
@@ -338,7 +380,7 @@ pg_coder_needs_quotation_set(VALUE self, VALUE needs_quotation)
338
380
  static VALUE
339
381
  pg_coder_needs_quotation_get(VALUE self)
340
382
  {
341
- t_pg_composite_coder *this = DATA_PTR(self);
383
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
342
384
  return this->needs_quotation ? Qtrue : Qfalse;
343
385
  }
344
386
 
@@ -353,7 +395,7 @@ pg_coder_needs_quotation_get(VALUE self)
353
395
  static VALUE
354
396
  pg_coder_delimiter_set(VALUE self, VALUE delimiter)
355
397
  {
356
- t_pg_composite_coder *this = DATA_PTR(self);
398
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
357
399
  StringValue(delimiter);
358
400
  if(RSTRING_LEN(delimiter) != 1)
359
401
  rb_raise( rb_eArgError, "delimiter size must be one byte");
@@ -370,7 +412,7 @@ pg_coder_delimiter_set(VALUE self, VALUE delimiter)
370
412
  static VALUE
371
413
  pg_coder_delimiter_get(VALUE self)
372
414
  {
373
- t_pg_composite_coder *this = DATA_PTR(self);
415
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
374
416
  return rb_str_new(&this->delimiter, 1);
375
417
  }
376
418
 
@@ -386,12 +428,12 @@ pg_coder_delimiter_get(VALUE self)
386
428
  static VALUE
387
429
  pg_coder_elements_type_set(VALUE self, VALUE elem_type)
388
430
  {
389
- t_pg_composite_coder *this = DATA_PTR( self );
431
+ t_pg_composite_coder *this = RTYPEDDATA_DATA( self );
390
432
 
391
433
  if ( NIL_P(elem_type) ){
392
434
  this->elem = NULL;
393
435
  } else if ( rb_obj_is_kind_of(elem_type, rb_cPG_Coder) ){
394
- this->elem = DATA_PTR( elem_type );
436
+ this->elem = RTYPEDDATA_DATA( elem_type );
395
437
  } else {
396
438
  rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::Coder)",
397
439
  rb_obj_classname( elem_type ) );
@@ -401,10 +443,22 @@ pg_coder_elements_type_set(VALUE self, VALUE elem_type)
401
443
  return elem_type;
402
444
  }
403
445
 
446
+ static const rb_data_type_t pg_coder_cfunc_type = {
447
+ "PG::Coder::CFUNC",
448
+ {
449
+ (RUBY_DATA_FUNC)NULL,
450
+ (RUBY_DATA_FUNC)NULL,
451
+ (size_t (*)(const void *))NULL,
452
+ },
453
+ 0,
454
+ 0,
455
+ RUBY_TYPED_FREE_IMMEDIATELY,
456
+ };
457
+
404
458
  void
405
459
  pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
406
460
  {
407
- VALUE cfunc_obj = Data_Wrap_Struct( rb_cObject, NULL, NULL, func );
461
+ VALUE cfunc_obj = TypedData_Wrap_Struct( rb_cObject, &pg_coder_cfunc_type, func );
408
462
  VALUE coder_klass = rb_define_class_under( nsp, name, base_klass );
409
463
  if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_BinaryDecoder )
410
464
  rb_include_module( coder_klass, rb_mPG_BinaryFormatting );
@@ -483,7 +537,7 @@ pg_coder_dec_func(t_pg_coder *this, int binary)
483
537
 
484
538
 
485
539
  void
486
- init_pg_coder()
540
+ init_pg_coder(void)
487
541
  {
488
542
  s_id_encode = rb_intern("encode");
489
543
  s_id_decode = rb_intern("decode");