pg 1.4.6 → 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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/{History.md → CHANGELOG.md} +185 -3
  4. data/Gemfile +12 -3
  5. data/README-Windows.rdoc +1 -1
  6. data/README.ja.md +75 -41
  7. data/README.md +86 -31
  8. data/Rakefile +95 -14
  9. data/certs/kanis@comcard.de.pem +20 -0
  10. data/certs/larskanis-2024.pem +24 -0
  11. data/ext/errorcodes.def +4 -5
  12. data/ext/errorcodes.txt +2 -5
  13. data/ext/extconf.rb +165 -14
  14. data/ext/gvl_wrappers.c +13 -2
  15. data/ext/gvl_wrappers.h +33 -0
  16. data/ext/pg.c +28 -35
  17. data/ext/pg.h +18 -14
  18. data/ext/pg_binary_decoder.c +231 -0
  19. data/ext/pg_binary_encoder.c +427 -0
  20. data/ext/pg_cancel_connection.c +360 -0
  21. data/ext/pg_coder.c +70 -12
  22. data/ext/pg_connection.c +473 -208
  23. data/ext/pg_copy_coder.c +316 -23
  24. data/ext/pg_record_coder.c +12 -11
  25. data/ext/pg_result.c +102 -30
  26. data/ext/pg_text_decoder.c +31 -10
  27. data/ext/pg_text_encoder.c +58 -26
  28. data/ext/pg_tuple.c +36 -33
  29. data/ext/pg_type_map.c +4 -3
  30. data/ext/pg_type_map_all_strings.c +3 -3
  31. data/ext/pg_type_map_by_class.c +6 -4
  32. data/ext/pg_type_map_by_column.c +9 -4
  33. data/ext/pg_type_map_by_mri_type.c +1 -1
  34. data/ext/pg_type_map_by_oid.c +10 -5
  35. data/ext/pg_type_map_in_ruby.c +6 -3
  36. data/lib/pg/basic_type_map_based_on_result.rb +21 -1
  37. data/lib/pg/basic_type_map_for_queries.rb +23 -10
  38. data/lib/pg/basic_type_map_for_results.rb +26 -3
  39. data/lib/pg/basic_type_registry.rb +46 -36
  40. data/lib/pg/binary_decoder/date.rb +9 -0
  41. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  42. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  43. data/lib/pg/cancel_connection.rb +53 -0
  44. data/lib/pg/coder.rb +18 -14
  45. data/lib/pg/connection.rb +387 -172
  46. data/lib/pg/exceptions.rb +6 -0
  47. data/lib/pg/text_decoder/date.rb +21 -0
  48. data/lib/pg/text_decoder/inet.rb +9 -0
  49. data/lib/pg/text_decoder/json.rb +17 -0
  50. data/lib/pg/text_decoder/numeric.rb +9 -0
  51. data/lib/pg/text_decoder/timestamp.rb +30 -0
  52. data/lib/pg/text_encoder/date.rb +13 -0
  53. data/lib/pg/text_encoder/inet.rb +31 -0
  54. data/lib/pg/text_encoder/json.rb +17 -0
  55. data/lib/pg/text_encoder/numeric.rb +9 -0
  56. data/lib/pg/text_encoder/timestamp.rb +24 -0
  57. data/lib/pg/version.rb +1 -1
  58. data/lib/pg.rb +78 -17
  59. data/misc/yugabyte/Dockerfile +9 -0
  60. data/misc/yugabyte/docker-compose.yml +28 -0
  61. data/misc/yugabyte/pg-test.rb +45 -0
  62. data/pg.gemspec +9 -5
  63. data/ports/patches/krb5/1.21.3/0001-Allow-static-linking-krb5-library.patch +30 -0
  64. data/ports/patches/openssl/3.5.1/0001-aarch64-mingw.patch +21 -0
  65. data/ports/patches/postgresql/17.5/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +42 -0
  66. data/ports/patches/postgresql/17.5/0001-libpq-Process-buffered-SSL-read-bytes-to-support-rec.patch +52 -0
  67. data/rakelib/pg_gem_helper.rb +64 -0
  68. data.tar.gz.sig +0 -0
  69. metadata +61 -49
  70. metadata.gz.sig +0 -0
  71. data/.appveyor.yml +0 -42
  72. data/.gems +0 -6
  73. data/.gemtest +0 -0
  74. data/.github/workflows/binary-gems.yml +0 -117
  75. data/.github/workflows/source-gem.yml +0 -137
  76. data/.gitignore +0 -19
  77. data/.hgsigs +0 -34
  78. data/.hgtags +0 -41
  79. data/.irbrc +0 -23
  80. data/.pryrc +0 -23
  81. data/.tm_properties +0 -21
  82. data/.travis.yml +0 -49
  83. data/Manifest.txt +0 -72
  84. data/Rakefile.cross +0 -298
  85. data/lib/pg/binary_decoder.rb +0 -23
  86. data/lib/pg/constants.rb +0 -12
  87. data/lib/pg/text_decoder.rb +0 -46
  88. data/lib/pg/text_encoder.rb +0 -59
  89. data/translation/.po4a-version +0 -7
  90. data/translation/po/all.pot +0 -875
  91. data/translation/po/ja.po +0 -868
  92. data/translation/po4a.cfg +0 -9
@@ -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
+ }
data/ext/pg_coder.c CHANGED
@@ -35,7 +35,7 @@ pg_coder_init_encoder( VALUE self )
35
35
  this->enc_func = NULL;
36
36
  }
37
37
  this->dec_func = NULL;
38
- this->coder_obj = self;
38
+ RB_OBJ_WRITE(self, &this->coder_obj, self);
39
39
  this->oid = 0;
40
40
  this->format = 0;
41
41
  this->flags = 0;
@@ -54,7 +54,7 @@ pg_coder_init_decoder( VALUE self )
54
54
  } else {
55
55
  this->dec_func = NULL;
56
56
  }
57
- this->coder_obj = self;
57
+ RB_OBJ_WRITE(self, &this->coder_obj, self);
58
58
  this->oid = 0;
59
59
  this->format = 0;
60
60
  this->flags = 0;
@@ -95,11 +95,13 @@ const rb_data_type_t pg_coder_type = {
95
95
  (RUBY_DATA_FUNC) NULL,
96
96
  RUBY_TYPED_DEFAULT_FREE,
97
97
  pg_coder_memsize,
98
- pg_compact_callback(pg_coder_compact),
98
+ pg_coder_compact,
99
99
  },
100
100
  0,
101
101
  0,
102
- RUBY_TYPED_FREE_IMMEDIATELY,
102
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
103
+ // macro to update VALUE references, as to trigger write barriers.
104
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
103
105
  };
104
106
 
105
107
  static VALUE
@@ -117,11 +119,11 @@ static const rb_data_type_t pg_composite_coder_type = {
117
119
  (RUBY_DATA_FUNC) NULL,
118
120
  RUBY_TYPED_DEFAULT_FREE,
119
121
  pg_composite_coder_memsize,
120
- pg_compact_callback(pg_composite_coder_compact),
122
+ pg_composite_coder_compact,
121
123
  },
122
124
  &pg_coder_type,
123
125
  0,
124
- RUBY_TYPED_FREE_IMMEDIATELY,
126
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
125
127
  };
126
128
 
127
129
  static VALUE
@@ -133,6 +135,7 @@ pg_composite_encoder_allocate( VALUE klass )
133
135
  this->elem = NULL;
134
136
  this->needs_quotation = 1;
135
137
  this->delimiter = ',';
138
+ this->dimensions = -1;
136
139
  rb_iv_set( self, "@elements_type", Qnil );
137
140
  return self;
138
141
  }
@@ -155,6 +158,7 @@ pg_composite_decoder_allocate( VALUE klass )
155
158
  this->elem = NULL;
156
159
  this->needs_quotation = 1;
157
160
  this->delimiter = ',';
161
+ this->dimensions = -1;
158
162
  rb_iv_set( self, "@elements_type", Qnil );
159
163
  return self;
160
164
  }
@@ -173,7 +177,7 @@ static VALUE
173
177
  pg_coder_encode(int argc, VALUE *argv, VALUE self)
174
178
  {
175
179
  VALUE res;
176
- VALUE intermediate;
180
+ VALUE intermediate = Qnil;
177
181
  VALUE value;
178
182
  int len, len2;
179
183
  int enc_idx;
@@ -211,8 +215,6 @@ pg_coder_encode(int argc, VALUE *argv, VALUE self)
211
215
  }
212
216
  rb_str_set_len( res, len2 );
213
217
 
214
- RB_GC_GUARD(intermediate);
215
-
216
218
  return res;
217
219
  }
218
220
 
@@ -273,6 +275,7 @@ static VALUE
273
275
  pg_coder_oid_set(VALUE self, VALUE oid)
274
276
  {
275
277
  t_pg_coder *this = RTYPEDDATA_DATA(self);
278
+ rb_check_frozen(self);
276
279
  this->oid = NUM2UINT(oid);
277
280
  return oid;
278
281
  }
@@ -304,6 +307,7 @@ static VALUE
304
307
  pg_coder_format_set(VALUE self, VALUE format)
305
308
  {
306
309
  t_pg_coder *this = RTYPEDDATA_DATA(self);
310
+ rb_check_frozen(self);
307
311
  this->format = NUM2INT(format);
308
312
  return format;
309
313
  }
@@ -335,6 +339,7 @@ static VALUE
335
339
  pg_coder_flags_set(VALUE self, VALUE flags)
336
340
  {
337
341
  t_pg_coder *this = RTYPEDDATA_DATA(self);
342
+ rb_check_frozen(self);
338
343
  this->flags = NUM2INT(flags);
339
344
  return flags;
340
345
  }
@@ -359,6 +364,7 @@ pg_coder_flags_get(VALUE self)
359
364
  * Specifies whether the assigned #elements_type requires quotation marks to
360
365
  * be transferred safely. Encoding with #needs_quotation=false is somewhat
361
366
  * faster.
367
+ * It is only used by text coders and ignored by binary coders.
362
368
  *
363
369
  * The default is +true+. This option is ignored for decoding of values.
364
370
  */
@@ -366,6 +372,7 @@ static VALUE
366
372
  pg_coder_needs_quotation_set(VALUE self, VALUE needs_quotation)
367
373
  {
368
374
  t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
375
+ rb_check_frozen(self);
369
376
  this->needs_quotation = RTEST(needs_quotation);
370
377
  return needs_quotation;
371
378
  }
@@ -391,11 +398,13 @@ pg_coder_needs_quotation_get(VALUE self)
391
398
  * Specifies the character that separates values within the composite type.
392
399
  * The default is a comma.
393
400
  * This must be a single one-byte character.
401
+ * It is only used by text coders and ignored by binary coders.
394
402
  */
395
403
  static VALUE
396
404
  pg_coder_delimiter_set(VALUE self, VALUE delimiter)
397
405
  {
398
406
  t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
407
+ rb_check_frozen(self);
399
408
  StringValue(delimiter);
400
409
  if(RSTRING_LEN(delimiter) != 1)
401
410
  rb_raise( rb_eArgError, "delimiter size must be one byte");
@@ -416,6 +425,49 @@ pg_coder_delimiter_get(VALUE self)
416
425
  return rb_str_new(&this->delimiter, 1);
417
426
  }
418
427
 
428
+ /*
429
+ * call-seq:
430
+ * coder.dimensions = Integer
431
+ * coder.dimensions = nil
432
+ *
433
+ * Set number of array dimensions to be encoded.
434
+ *
435
+ * This property ensures, that this number of dimensions is always encoded.
436
+ * If less dimensions than this number are in the given value, an ArgumentError is raised.
437
+ * If more dimensions than this number are in the value, the Array value is passed to the next encoder.
438
+ *
439
+ * Setting dimensions is especially useful, when a Record shall be encoded into an Array, since the Array encoder can not distinguish if the array shall be encoded as a higher dimension or as a record otherwise.
440
+ *
441
+ * The default is +nil+.
442
+ *
443
+ * See #dimensions
444
+ */
445
+ static VALUE
446
+ pg_coder_dimensions_set(VALUE self, VALUE dimensions)
447
+ {
448
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
449
+ rb_check_frozen(self);
450
+ if(!NIL_P(dimensions) && NUM2INT(dimensions) < 0)
451
+ rb_raise( rb_eArgError, "dimensions must be nil or >= 0");
452
+ this->dimensions = NIL_P(dimensions) ? -1 : NUM2INT(dimensions);
453
+ return dimensions;
454
+ }
455
+
456
+ /*
457
+ * call-seq:
458
+ * coder.dimensions -> Integer | nil
459
+ *
460
+ * Get number of enforced array dimensions or +nil+ if not set.
461
+ *
462
+ * See #dimensions=
463
+ */
464
+ static VALUE
465
+ pg_coder_dimensions_get(VALUE self)
466
+ {
467
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
468
+ return this->dimensions < 0 ? Qnil : INT2NUM(this->dimensions);
469
+ }
470
+
419
471
  /*
420
472
  * call-seq:
421
473
  * coder.elements_type = coder
@@ -430,6 +482,7 @@ pg_coder_elements_type_set(VALUE self, VALUE elem_type)
430
482
  {
431
483
  t_pg_composite_coder *this = RTYPEDDATA_DATA( self );
432
484
 
485
+ rb_check_frozen(self);
433
486
  if ( NIL_P(elem_type) ){
434
487
  this->elem = NULL;
435
488
  } else if ( rb_obj_is_kind_of(elem_type, rb_cPG_Coder) ){
@@ -452,10 +505,10 @@ static const rb_data_type_t pg_coder_cfunc_type = {
452
505
  },
453
506
  0,
454
507
  0,
455
- RUBY_TYPED_FREE_IMMEDIATELY,
508
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
456
509
  };
457
510
 
458
- void
511
+ VALUE
459
512
  pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
460
513
  {
461
514
  VALUE cfunc_obj = TypedData_Wrap_Struct( rb_cObject, &pg_coder_cfunc_type, func );
@@ -468,9 +521,10 @@ pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
468
521
  if( nsp==rb_mPG_BinaryDecoder || nsp==rb_mPG_TextDecoder )
469
522
  rb_define_method( coder_klass, "decode", pg_coder_decode, -1 );
470
523
 
471
- rb_define_const( coder_klass, "CFUNC", cfunc_obj );
524
+ rb_define_const( coder_klass, "CFUNC", rb_obj_freeze(cfunc_obj) );
472
525
 
473
526
  RB_GC_GUARD(cfunc_obj);
527
+ return coder_klass;
474
528
  }
475
529
 
476
530
 
@@ -595,6 +649,8 @@ init_pg_coder(void)
595
649
  *
596
650
  * This is the base class for all type cast classes of PostgreSQL types,
597
651
  * that are made up of some sub type.
652
+ *
653
+ * See PG::TextEncoder::Array, PG::TextDecoder::Array, PG::BinaryEncoder::Array, PG::BinaryDecoder::Array, etc.
598
654
  */
599
655
  rb_cPG_CompositeCoder = rb_define_class_under( rb_mPG, "CompositeCoder", rb_cPG_Coder );
600
656
  rb_define_method( rb_cPG_CompositeCoder, "elements_type=", pg_coder_elements_type_set, 1 );
@@ -603,6 +659,8 @@ init_pg_coder(void)
603
659
  rb_define_method( rb_cPG_CompositeCoder, "needs_quotation?", pg_coder_needs_quotation_get, 0 );
604
660
  rb_define_method( rb_cPG_CompositeCoder, "delimiter=", pg_coder_delimiter_set, 1 );
605
661
  rb_define_method( rb_cPG_CompositeCoder, "delimiter", pg_coder_delimiter_get, 0 );
662
+ rb_define_method( rb_cPG_CompositeCoder, "dimensions=", pg_coder_dimensions_set, 1 );
663
+ rb_define_method( rb_cPG_CompositeCoder, "dimensions", pg_coder_dimensions_get, 0 );
606
664
 
607
665
  /* Document-class: PG::CompositeEncoder < PG::CompositeCoder */
608
666
  rb_cPG_CompositeEncoder = rb_define_class_under( rb_mPG, "CompositeEncoder", rb_cPG_CompositeCoder );