pg 1.5.9-x86-mingw32 → 1.6.0.rc1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -95,7 +95,7 @@ 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,
@@ -119,7 +119,7 @@ static const rb_data_type_t pg_composite_coder_type = {
119
119
  (RUBY_DATA_FUNC) NULL,
120
120
  RUBY_TYPED_DEFAULT_FREE,
121
121
  pg_composite_coder_memsize,
122
- pg_compact_callback(pg_composite_coder_compact),
122
+ pg_composite_coder_compact,
123
123
  },
124
124
  &pg_coder_type,
125
125
  0,
@@ -175,7 +175,7 @@ static VALUE
175
175
  pg_coder_encode(int argc, VALUE *argv, VALUE self)
176
176
  {
177
177
  VALUE res;
178
- VALUE intermediate;
178
+ VALUE intermediate = Qnil;
179
179
  VALUE value;
180
180
  int len, len2;
181
181
  int enc_idx;
@@ -213,8 +213,6 @@ pg_coder_encode(int argc, VALUE *argv, VALUE self)
213
213
  }
214
214
  rb_str_set_len( res, len2 );
215
215
 
216
- RB_GC_GUARD(intermediate);
217
-
218
216
  return res;
219
217
  }
220
218