pg 1.6.0.rc1-x86_64-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +4 -0
- data/BSDL +22 -0
- data/Contributors.rdoc +46 -0
- data/Gemfile +23 -0
- data/History.md +958 -0
- data/LICENSE +56 -0
- data/Manifest.txt +72 -0
- data/POSTGRES +23 -0
- data/README-OS_X.rdoc +68 -0
- data/README-Windows.rdoc +56 -0
- data/README.ja.md +300 -0
- data/README.md +286 -0
- data/Rakefile +161 -0
- data/certs/ged.pem +24 -0
- data/certs/kanis@comcard.de.pem +20 -0
- data/certs/larskanis-2022.pem +26 -0
- data/certs/larskanis-2023.pem +24 -0
- data/certs/larskanis-2024.pem +24 -0
- data/ext/errorcodes.def +1043 -0
- data/ext/errorcodes.rb +45 -0
- data/ext/errorcodes.txt +494 -0
- data/ext/extconf.rb +282 -0
- data/ext/gvl_wrappers.c +32 -0
- data/ext/gvl_wrappers.h +297 -0
- data/ext/pg.c +703 -0
- data/ext/pg.h +390 -0
- data/ext/pg_binary_decoder.c +460 -0
- data/ext/pg_binary_encoder.c +583 -0
- data/ext/pg_cancel_connection.c +360 -0
- data/ext/pg_coder.c +622 -0
- data/ext/pg_connection.c +4869 -0
- data/ext/pg_copy_coder.c +921 -0
- data/ext/pg_errors.c +95 -0
- data/ext/pg_record_coder.c +522 -0
- data/ext/pg_result.c +1764 -0
- data/ext/pg_text_decoder.c +1008 -0
- data/ext/pg_text_encoder.c +833 -0
- data/ext/pg_tuple.c +572 -0
- data/ext/pg_type_map.c +200 -0
- data/ext/pg_type_map_all_strings.c +130 -0
- data/ext/pg_type_map_by_class.c +271 -0
- data/ext/pg_type_map_by_column.c +355 -0
- data/ext/pg_type_map_by_mri_type.c +313 -0
- data/ext/pg_type_map_by_oid.c +388 -0
- data/ext/pg_type_map_in_ruby.c +333 -0
- data/ext/pg_util.c +149 -0
- data/ext/pg_util.h +65 -0
- data/ext/vc/pg.sln +26 -0
- data/ext/vc/pg_18/pg.vcproj +216 -0
- data/ext/vc/pg_19/pg_19.vcproj +209 -0
- data/lib/2.7/pg_ext.so +0 -0
- data/lib/3.0/pg_ext.so +0 -0
- data/lib/3.1/pg_ext.so +0 -0
- data/lib/3.2/pg_ext.so +0 -0
- data/lib/3.3/pg_ext.so +0 -0
- data/lib/pg/basic_type_map_based_on_result.rb +67 -0
- data/lib/pg/basic_type_map_for_queries.rb +202 -0
- data/lib/pg/basic_type_map_for_results.rb +104 -0
- data/lib/pg/basic_type_registry.rb +311 -0
- data/lib/pg/binary_decoder/date.rb +9 -0
- data/lib/pg/binary_decoder/timestamp.rb +26 -0
- data/lib/pg/binary_encoder/timestamp.rb +20 -0
- data/lib/pg/cancel_connection.rb +30 -0
- data/lib/pg/coder.rb +106 -0
- data/lib/pg/connection.rb +1027 -0
- data/lib/pg/exceptions.rb +31 -0
- data/lib/pg/result.rb +43 -0
- data/lib/pg/text_decoder/date.rb +21 -0
- data/lib/pg/text_decoder/inet.rb +9 -0
- data/lib/pg/text_decoder/json.rb +17 -0
- data/lib/pg/text_decoder/numeric.rb +9 -0
- data/lib/pg/text_decoder/timestamp.rb +30 -0
- data/lib/pg/text_encoder/date.rb +13 -0
- data/lib/pg/text_encoder/inet.rb +31 -0
- data/lib/pg/text_encoder/json.rb +17 -0
- data/lib/pg/text_encoder/numeric.rb +9 -0
- data/lib/pg/text_encoder/timestamp.rb +24 -0
- data/lib/pg/tuple.rb +30 -0
- data/lib/pg/type_map_by_column.rb +16 -0
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +144 -0
- data/misc/openssl-pg-segfault.rb +31 -0
- data/misc/postgres/History.txt +9 -0
- data/misc/postgres/Manifest.txt +5 -0
- data/misc/postgres/README.txt +21 -0
- data/misc/postgres/Rakefile +21 -0
- data/misc/postgres/lib/postgres.rb +16 -0
- data/misc/ruby-pg/History.txt +9 -0
- data/misc/ruby-pg/Manifest.txt +5 -0
- data/misc/ruby-pg/README.txt +21 -0
- data/misc/ruby-pg/Rakefile +21 -0
- data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
- data/pg.gemspec +36 -0
- data/ports/x86_64-linux/lib/libpq-ruby-pg.so.1 +0 -0
- data/rakelib/task_extension.rb +46 -0
- data/sample/array_insert.rb +20 -0
- data/sample/async_api.rb +102 -0
- data/sample/async_copyto.rb +39 -0
- data/sample/async_mixed.rb +56 -0
- data/sample/check_conn.rb +21 -0
- data/sample/copydata.rb +71 -0
- data/sample/copyfrom.rb +81 -0
- data/sample/copyto.rb +19 -0
- data/sample/cursor.rb +21 -0
- data/sample/disk_usage_report.rb +177 -0
- data/sample/issue-119.rb +94 -0
- data/sample/losample.rb +69 -0
- data/sample/minimal-testcase.rb +17 -0
- data/sample/notify_wait.rb +72 -0
- data/sample/pg_statistics.rb +285 -0
- data/sample/replication_monitor.rb +222 -0
- data/sample/test_binary_values.rb +33 -0
- data/sample/wal_shipper.rb +434 -0
- data/sample/warehouse_partitions.rb +311 -0
- data.tar.gz.sig +0 -0
- metadata +252 -0
- metadata.gz.sig +0 -0
data/ext/pg_connection.c
ADDED
@@ -0,0 +1,4869 @@
|
|
1
|
+
/*
|
2
|
+
* pg_connection.c - PG::Connection class extension
|
3
|
+
* $Id$
|
4
|
+
*
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "pg.h"
|
8
|
+
|
9
|
+
/* Number of bytes that are reserved on the stack for query params. */
|
10
|
+
#define QUERYDATA_BUFFER_SIZE 4000
|
11
|
+
|
12
|
+
|
13
|
+
VALUE rb_cPGconn;
|
14
|
+
static ID s_id_encode;
|
15
|
+
static ID s_id_autoclose_set;
|
16
|
+
static VALUE sym_type, sym_format, sym_value;
|
17
|
+
static VALUE sym_symbol, sym_string, sym_static_symbol;
|
18
|
+
|
19
|
+
static VALUE pgconn_finish( VALUE );
|
20
|
+
static VALUE pgconn_set_default_encoding( VALUE self );
|
21
|
+
static VALUE pgconn_wait_for_flush( VALUE self );
|
22
|
+
static void pgconn_set_internal_encoding_index( VALUE );
|
23
|
+
static const rb_data_type_t pg_connection_type;
|
24
|
+
static VALUE pgconn_async_flush(VALUE self);
|
25
|
+
|
26
|
+
/*
|
27
|
+
* Global functions
|
28
|
+
*/
|
29
|
+
|
30
|
+
/*
|
31
|
+
* Convenience function to raise connection errors
|
32
|
+
*/
|
33
|
+
void
|
34
|
+
pg_raise_conn_error( VALUE klass, VALUE self, const char *format, ...)
|
35
|
+
{
|
36
|
+
VALUE msg, error;
|
37
|
+
va_list ap;
|
38
|
+
|
39
|
+
va_start(ap, format);
|
40
|
+
msg = rb_vsprintf(format, ap);
|
41
|
+
va_end(ap);
|
42
|
+
error = rb_exc_new_str(klass, msg);
|
43
|
+
rb_iv_set(error, "@connection", self);
|
44
|
+
rb_exc_raise(error);
|
45
|
+
}
|
46
|
+
|
47
|
+
/*
|
48
|
+
* Fetch the PG::Connection object data pointer.
|
49
|
+
*/
|
50
|
+
t_pg_connection *
|
51
|
+
pg_get_connection( VALUE self )
|
52
|
+
{
|
53
|
+
t_pg_connection *this;
|
54
|
+
TypedData_Get_Struct( self, t_pg_connection, &pg_connection_type, this);
|
55
|
+
|
56
|
+
return this;
|
57
|
+
}
|
58
|
+
|
59
|
+
/*
|
60
|
+
* Fetch the PG::Connection object data pointer and check it's
|
61
|
+
* PGconn data pointer for sanity.
|
62
|
+
*/
|
63
|
+
static t_pg_connection *
|
64
|
+
pg_get_connection_safe( VALUE self )
|
65
|
+
{
|
66
|
+
t_pg_connection *this;
|
67
|
+
TypedData_Get_Struct( self, t_pg_connection, &pg_connection_type, this);
|
68
|
+
|
69
|
+
if ( !this->pgconn )
|
70
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "connection is closed");
|
71
|
+
|
72
|
+
return this;
|
73
|
+
}
|
74
|
+
|
75
|
+
/*
|
76
|
+
* Fetch the PGconn data pointer and check it for sanity.
|
77
|
+
*
|
78
|
+
* Note: This function is used externally by the sequel_pg gem,
|
79
|
+
* so do changes carefully.
|
80
|
+
*
|
81
|
+
*/
|
82
|
+
PGconn *
|
83
|
+
pg_get_pgconn( VALUE self )
|
84
|
+
{
|
85
|
+
t_pg_connection *this;
|
86
|
+
TypedData_Get_Struct( self, t_pg_connection, &pg_connection_type, this);
|
87
|
+
|
88
|
+
if ( !this->pgconn ){
|
89
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "connection is closed");
|
90
|
+
}
|
91
|
+
|
92
|
+
return this->pgconn;
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
void
|
97
|
+
pg_unwrap_socket_io( VALUE self, VALUE *p_socket_io, int ruby_sd )
|
98
|
+
{
|
99
|
+
if ( RTEST(*p_socket_io) ) {
|
100
|
+
#if defined(_WIN32)
|
101
|
+
if( rb_w32_unwrap_io_handle(ruby_sd) )
|
102
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "Could not unwrap win32 socket handle");
|
103
|
+
#endif
|
104
|
+
rb_funcall( *p_socket_io, rb_intern("close"), 0 );
|
105
|
+
}
|
106
|
+
|
107
|
+
RB_OBJ_WRITE(self, p_socket_io, Qnil);
|
108
|
+
}
|
109
|
+
|
110
|
+
|
111
|
+
/*
|
112
|
+
* Close the associated socket IO object if there is one.
|
113
|
+
*/
|
114
|
+
static void
|
115
|
+
pgconn_close_socket_io( VALUE self )
|
116
|
+
{
|
117
|
+
t_pg_connection *this = pg_get_connection( self );
|
118
|
+
pg_unwrap_socket_io( self, &this->socket_io, this->ruby_sd);
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
/*
|
123
|
+
* Create a Ruby Array of Hashes out of a PGconninfoOptions array.
|
124
|
+
*/
|
125
|
+
static VALUE
|
126
|
+
pgconn_make_conninfo_array( const PQconninfoOption *options )
|
127
|
+
{
|
128
|
+
VALUE ary = rb_ary_new();
|
129
|
+
VALUE hash;
|
130
|
+
int i = 0;
|
131
|
+
|
132
|
+
if (!options) return Qnil;
|
133
|
+
|
134
|
+
for(i = 0; options[i].keyword != NULL; i++) {
|
135
|
+
hash = rb_hash_new();
|
136
|
+
if(options[i].keyword)
|
137
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("keyword")), rb_str_new2(options[i].keyword));
|
138
|
+
if(options[i].envvar)
|
139
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("envvar")), rb_str_new2(options[i].envvar));
|
140
|
+
if(options[i].compiled)
|
141
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("compiled")), rb_str_new2(options[i].compiled));
|
142
|
+
if(options[i].val)
|
143
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("val")), rb_str_new2(options[i].val));
|
144
|
+
if(options[i].label)
|
145
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("label")), rb_str_new2(options[i].label));
|
146
|
+
if(options[i].dispchar)
|
147
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("dispchar")), rb_str_new2(options[i].dispchar));
|
148
|
+
rb_hash_aset(hash, ID2SYM(rb_intern("dispsize")), INT2NUM(options[i].dispsize));
|
149
|
+
rb_ary_push(ary, hash);
|
150
|
+
}
|
151
|
+
|
152
|
+
return ary;
|
153
|
+
}
|
154
|
+
|
155
|
+
static const char *pg_cstr_enc(VALUE str, int enc_idx){
|
156
|
+
const char *ptr = StringValueCStr(str);
|
157
|
+
if( ENCODING_GET(str) == enc_idx ){
|
158
|
+
return ptr;
|
159
|
+
} else {
|
160
|
+
str = rb_str_export_to_enc(str, rb_enc_from_index(enc_idx));
|
161
|
+
return StringValueCStr(str);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
/*
|
167
|
+
* GC Mark function
|
168
|
+
*/
|
169
|
+
static void
|
170
|
+
pgconn_gc_mark( void *_this )
|
171
|
+
{
|
172
|
+
t_pg_connection *this = (t_pg_connection *)_this;
|
173
|
+
rb_gc_mark_movable( this->socket_io );
|
174
|
+
rb_gc_mark_movable( this->notice_receiver );
|
175
|
+
rb_gc_mark_movable( this->notice_processor );
|
176
|
+
rb_gc_mark_movable( this->type_map_for_queries );
|
177
|
+
rb_gc_mark_movable( this->type_map_for_results );
|
178
|
+
rb_gc_mark_movable( this->trace_stream );
|
179
|
+
rb_gc_mark_movable( this->encoder_for_put_copy_data );
|
180
|
+
rb_gc_mark_movable( this->decoder_for_get_copy_data );
|
181
|
+
}
|
182
|
+
|
183
|
+
static void
|
184
|
+
pgconn_gc_compact( void *_this )
|
185
|
+
{
|
186
|
+
t_pg_connection *this = (t_pg_connection *)_this;
|
187
|
+
pg_gc_location( this->socket_io );
|
188
|
+
pg_gc_location( this->notice_receiver );
|
189
|
+
pg_gc_location( this->notice_processor );
|
190
|
+
pg_gc_location( this->type_map_for_queries );
|
191
|
+
pg_gc_location( this->type_map_for_results );
|
192
|
+
pg_gc_location( this->trace_stream );
|
193
|
+
pg_gc_location( this->encoder_for_put_copy_data );
|
194
|
+
pg_gc_location( this->decoder_for_get_copy_data );
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
/*
|
199
|
+
* GC Free function
|
200
|
+
*/
|
201
|
+
static void
|
202
|
+
pgconn_gc_free( void *_this )
|
203
|
+
{
|
204
|
+
t_pg_connection *this = (t_pg_connection *)_this;
|
205
|
+
#if defined(_WIN32)
|
206
|
+
if ( RTEST(this->socket_io) ) {
|
207
|
+
if( rb_w32_unwrap_io_handle(this->ruby_sd) ){
|
208
|
+
rb_warn("pg: Could not unwrap win32 socket handle by garbage collector");
|
209
|
+
}
|
210
|
+
}
|
211
|
+
#endif
|
212
|
+
if (this->pgconn != NULL)
|
213
|
+
PQfinish( this->pgconn );
|
214
|
+
|
215
|
+
xfree(this);
|
216
|
+
}
|
217
|
+
|
218
|
+
/*
|
219
|
+
* Object Size function
|
220
|
+
*/
|
221
|
+
static size_t
|
222
|
+
pgconn_memsize( const void *_this )
|
223
|
+
{
|
224
|
+
const t_pg_connection *this = (const t_pg_connection *)_this;
|
225
|
+
return sizeof(*this);
|
226
|
+
}
|
227
|
+
|
228
|
+
static const rb_data_type_t pg_connection_type = {
|
229
|
+
"PG::Connection",
|
230
|
+
{
|
231
|
+
pgconn_gc_mark,
|
232
|
+
pgconn_gc_free,
|
233
|
+
pgconn_memsize,
|
234
|
+
pgconn_gc_compact,
|
235
|
+
},
|
236
|
+
0,
|
237
|
+
0,
|
238
|
+
RUBY_TYPED_WB_PROTECTED,
|
239
|
+
};
|
240
|
+
|
241
|
+
|
242
|
+
/**************************************************************************
|
243
|
+
* Class Methods
|
244
|
+
**************************************************************************/
|
245
|
+
|
246
|
+
/*
|
247
|
+
* Document-method: allocate
|
248
|
+
*
|
249
|
+
* call-seq:
|
250
|
+
* PG::Connection.allocate -> conn
|
251
|
+
*/
|
252
|
+
static VALUE
|
253
|
+
pgconn_s_allocate( VALUE klass )
|
254
|
+
{
|
255
|
+
t_pg_connection *this;
|
256
|
+
VALUE self = TypedData_Make_Struct( klass, t_pg_connection, &pg_connection_type, this );
|
257
|
+
|
258
|
+
this->pgconn = NULL;
|
259
|
+
RB_OBJ_WRITE(self, &this->socket_io, Qnil);
|
260
|
+
RB_OBJ_WRITE(self, &this->notice_receiver, Qnil);
|
261
|
+
RB_OBJ_WRITE(self, &this->notice_processor, Qnil);
|
262
|
+
RB_OBJ_WRITE(self, &this->type_map_for_queries, pg_typemap_all_strings);
|
263
|
+
RB_OBJ_WRITE(self, &this->type_map_for_results, pg_typemap_all_strings);
|
264
|
+
RB_OBJ_WRITE(self, &this->encoder_for_put_copy_data, Qnil);
|
265
|
+
RB_OBJ_WRITE(self, &this->decoder_for_get_copy_data, Qnil);
|
266
|
+
RB_OBJ_WRITE(self, &this->trace_stream, Qnil);
|
267
|
+
rb_ivar_set(self, rb_intern("@calls_to_put_copy_data"), INT2FIX(0));
|
268
|
+
rb_ivar_set(self, rb_intern("@iopts_for_reset"), Qnil);
|
269
|
+
|
270
|
+
return self;
|
271
|
+
}
|
272
|
+
|
273
|
+
static VALUE
|
274
|
+
pgconn_s_sync_connect(int argc, VALUE *argv, VALUE klass)
|
275
|
+
{
|
276
|
+
t_pg_connection *this;
|
277
|
+
VALUE conninfo;
|
278
|
+
VALUE self = pgconn_s_allocate( klass );
|
279
|
+
|
280
|
+
this = pg_get_connection( self );
|
281
|
+
conninfo = rb_funcall2( rb_cPGconn, rb_intern("parse_connect_args"), argc, argv );
|
282
|
+
this->pgconn = gvl_PQconnectdb(StringValueCStr(conninfo));
|
283
|
+
|
284
|
+
if(this->pgconn == NULL)
|
285
|
+
rb_raise(rb_ePGerror, "PQconnectdb() unable to allocate PGconn structure");
|
286
|
+
|
287
|
+
if (PQstatus(this->pgconn) == CONNECTION_BAD)
|
288
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "%s", PQerrorMessage(this->pgconn));
|
289
|
+
|
290
|
+
pgconn_set_default_encoding( self );
|
291
|
+
|
292
|
+
if (rb_block_given_p()) {
|
293
|
+
return rb_ensure(rb_yield, self, pgconn_finish, self);
|
294
|
+
}
|
295
|
+
return self;
|
296
|
+
}
|
297
|
+
|
298
|
+
/*
|
299
|
+
* call-seq:
|
300
|
+
* PG::Connection.connect_start(connection_hash) -> conn
|
301
|
+
* PG::Connection.connect_start(connection_string) -> conn
|
302
|
+
* PG::Connection.connect_start(host, port, options, tty, dbname, login, password) -> conn
|
303
|
+
*
|
304
|
+
* This is an asynchronous version of PG::Connection.new.
|
305
|
+
*
|
306
|
+
* Use #connect_poll to poll the status of the connection.
|
307
|
+
*
|
308
|
+
* NOTE: this does *not* set the connection's +client_encoding+ for you if
|
309
|
+
* +Encoding.default_internal+ is set. To set it after the connection is established,
|
310
|
+
* call #internal_encoding=. You can also set it automatically by setting
|
311
|
+
* <code>ENV['PGCLIENTENCODING']</code>, or include the 'options' connection parameter.
|
312
|
+
*
|
313
|
+
* See also the 'sample' directory of this gem and the corresponding {libpq functions}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PQCONNECTSTARTPARAMS].
|
314
|
+
*
|
315
|
+
*/
|
316
|
+
static VALUE
|
317
|
+
pgconn_s_connect_start( int argc, VALUE *argv, VALUE klass )
|
318
|
+
{
|
319
|
+
VALUE rb_conn;
|
320
|
+
VALUE conninfo;
|
321
|
+
t_pg_connection *this;
|
322
|
+
|
323
|
+
/*
|
324
|
+
* PG::Connection.connect_start must act as both alloc() and initialize()
|
325
|
+
* because it is not invoked by calling new().
|
326
|
+
*/
|
327
|
+
rb_conn = pgconn_s_allocate( klass );
|
328
|
+
this = pg_get_connection( rb_conn );
|
329
|
+
conninfo = rb_funcall2( klass, rb_intern("parse_connect_args"), argc, argv );
|
330
|
+
this->pgconn = gvl_PQconnectStart( StringValueCStr(conninfo) );
|
331
|
+
|
332
|
+
if( this->pgconn == NULL )
|
333
|
+
rb_raise(rb_ePGerror, "PQconnectStart() unable to allocate PGconn structure");
|
334
|
+
|
335
|
+
if ( PQstatus(this->pgconn) == CONNECTION_BAD )
|
336
|
+
pg_raise_conn_error( rb_eConnectionBad, rb_conn, "%s", PQerrorMessage(this->pgconn));
|
337
|
+
|
338
|
+
if ( rb_block_given_p() ) {
|
339
|
+
return rb_ensure( rb_yield, rb_conn, pgconn_finish, rb_conn );
|
340
|
+
}
|
341
|
+
return rb_conn;
|
342
|
+
}
|
343
|
+
|
344
|
+
static VALUE
|
345
|
+
pgconn_s_sync_ping( int argc, VALUE *argv, VALUE klass )
|
346
|
+
{
|
347
|
+
PGPing ping;
|
348
|
+
VALUE conninfo;
|
349
|
+
|
350
|
+
conninfo = rb_funcall2( klass, rb_intern("parse_connect_args"), argc, argv );
|
351
|
+
ping = gvl_PQping( StringValueCStr(conninfo) );
|
352
|
+
|
353
|
+
return INT2FIX((int)ping);
|
354
|
+
}
|
355
|
+
|
356
|
+
|
357
|
+
/*
|
358
|
+
* Document-method: PG::Connection.conndefaults
|
359
|
+
*
|
360
|
+
* call-seq:
|
361
|
+
* PG::Connection.conndefaults() -> Array
|
362
|
+
*
|
363
|
+
* Returns an array of hashes. Each hash has the keys:
|
364
|
+
* [+:keyword+]
|
365
|
+
* the name of the option
|
366
|
+
* [+:envvar+]
|
367
|
+
* the environment variable to fall back to
|
368
|
+
* [+:compiled+]
|
369
|
+
* the compiled in option as a secondary fallback
|
370
|
+
* [+:val+]
|
371
|
+
* the option's current value, or +nil+ if not known
|
372
|
+
* [+:label+]
|
373
|
+
* the label for the field
|
374
|
+
* [+:dispchar+]
|
375
|
+
* "" for normal, "D" for debug, and "*" for password
|
376
|
+
* [+:dispsize+]
|
377
|
+
* field size
|
378
|
+
*/
|
379
|
+
static VALUE
|
380
|
+
pgconn_s_conndefaults(VALUE self)
|
381
|
+
{
|
382
|
+
PQconninfoOption *options = PQconndefaults();
|
383
|
+
VALUE array = pgconn_make_conninfo_array( options );
|
384
|
+
|
385
|
+
PQconninfoFree(options);
|
386
|
+
|
387
|
+
UNUSED( self );
|
388
|
+
|
389
|
+
return array;
|
390
|
+
}
|
391
|
+
|
392
|
+
/*
|
393
|
+
* Document-method: PG::Connection.conninfo_parse
|
394
|
+
*
|
395
|
+
* call-seq:
|
396
|
+
* PG::Connection.conninfo_parse(conninfo_string) -> Array
|
397
|
+
*
|
398
|
+
* Returns parsed connection options from the provided connection string as an array of hashes.
|
399
|
+
* Each hash has the same keys as PG::Connection.conndefaults() .
|
400
|
+
* The values from the +conninfo_string+ are stored in the +:val+ key.
|
401
|
+
*/
|
402
|
+
static VALUE
|
403
|
+
pgconn_s_conninfo_parse(VALUE self, VALUE conninfo)
|
404
|
+
{
|
405
|
+
VALUE array;
|
406
|
+
char *errmsg = NULL;
|
407
|
+
PQconninfoOption *options = PQconninfoParse(StringValueCStr(conninfo), &errmsg);
|
408
|
+
if(errmsg){
|
409
|
+
VALUE error = rb_str_new_cstr(errmsg);
|
410
|
+
PQfreemem(errmsg);
|
411
|
+
rb_raise(rb_ePGerror, "%"PRIsVALUE, error);
|
412
|
+
}
|
413
|
+
array = pgconn_make_conninfo_array( options );
|
414
|
+
|
415
|
+
PQconninfoFree(options);
|
416
|
+
|
417
|
+
UNUSED( self );
|
418
|
+
|
419
|
+
return array;
|
420
|
+
}
|
421
|
+
|
422
|
+
|
423
|
+
static VALUE
|
424
|
+
pgconn_sync_encrypt_password(int argc, VALUE *argv, VALUE self)
|
425
|
+
{
|
426
|
+
char *encrypted = NULL;
|
427
|
+
VALUE rval = Qnil;
|
428
|
+
VALUE password, username, algorithm;
|
429
|
+
PGconn *conn = pg_get_pgconn(self);
|
430
|
+
|
431
|
+
rb_scan_args( argc, argv, "21", &password, &username, &algorithm );
|
432
|
+
|
433
|
+
Check_Type(password, T_STRING);
|
434
|
+
Check_Type(username, T_STRING);
|
435
|
+
|
436
|
+
encrypted = gvl_PQencryptPasswordConn(conn, StringValueCStr(password), StringValueCStr(username), RTEST(algorithm) ? StringValueCStr(algorithm) : NULL);
|
437
|
+
if ( encrypted ) {
|
438
|
+
rval = rb_str_new2( encrypted );
|
439
|
+
PQfreemem( encrypted );
|
440
|
+
} else {
|
441
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
442
|
+
}
|
443
|
+
|
444
|
+
return rval;
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
/*
|
449
|
+
* call-seq:
|
450
|
+
* PG::Connection.encrypt_password( password, username ) -> String
|
451
|
+
*
|
452
|
+
* This is an older, deprecated version of #encrypt_password.
|
453
|
+
* The difference is that this function always uses +md5+ as the encryption algorithm.
|
454
|
+
*
|
455
|
+
*/
|
456
|
+
static VALUE
|
457
|
+
pgconn_s_encrypt_password(VALUE self, VALUE password, VALUE username)
|
458
|
+
{
|
459
|
+
char *encrypted = NULL;
|
460
|
+
VALUE rval = Qnil;
|
461
|
+
|
462
|
+
UNUSED( self );
|
463
|
+
|
464
|
+
Check_Type(password, T_STRING);
|
465
|
+
Check_Type(username, T_STRING);
|
466
|
+
|
467
|
+
encrypted = PQencryptPassword(StringValueCStr(password), StringValueCStr(username));
|
468
|
+
rval = rb_str_new2( encrypted );
|
469
|
+
PQfreemem( encrypted );
|
470
|
+
|
471
|
+
return rval;
|
472
|
+
}
|
473
|
+
|
474
|
+
|
475
|
+
/**************************************************************************
|
476
|
+
* PG::Connection INSTANCE METHODS
|
477
|
+
**************************************************************************/
|
478
|
+
|
479
|
+
/*
|
480
|
+
* call-seq:
|
481
|
+
* conn.connect_poll() -> Integer
|
482
|
+
*
|
483
|
+
* Returns one of:
|
484
|
+
* [+PGRES_POLLING_READING+]
|
485
|
+
* wait until the socket is ready to read
|
486
|
+
* [+PGRES_POLLING_WRITING+]
|
487
|
+
* wait until the socket is ready to write
|
488
|
+
* [+PGRES_POLLING_FAILED+]
|
489
|
+
* the asynchronous connection has failed
|
490
|
+
* [+PGRES_POLLING_OK+]
|
491
|
+
* the asynchronous connection is ready
|
492
|
+
*
|
493
|
+
* Example:
|
494
|
+
* require "io/wait"
|
495
|
+
*
|
496
|
+
* conn = PG::Connection.connect_start(dbname: 'mydatabase')
|
497
|
+
* status = conn.connect_poll
|
498
|
+
* while(status != PG::PGRES_POLLING_OK) do
|
499
|
+
* # do some work while waiting for the connection to complete
|
500
|
+
* if(status == PG::PGRES_POLLING_READING)
|
501
|
+
* unless conn.socket_io.wait_readable(10.0)
|
502
|
+
* raise "Asynchronous connection timed out!"
|
503
|
+
* end
|
504
|
+
* elsif(status == PG::PGRES_POLLING_WRITING)
|
505
|
+
* unless conn.socket_io.wait_writable(10.0)
|
506
|
+
* raise "Asynchronous connection timed out!"
|
507
|
+
* end
|
508
|
+
* end
|
509
|
+
* status = conn.connect_poll
|
510
|
+
* end
|
511
|
+
* # now conn.status == CONNECTION_OK, and connection
|
512
|
+
* # is ready.
|
513
|
+
*/
|
514
|
+
static VALUE
|
515
|
+
pgconn_connect_poll(VALUE self)
|
516
|
+
{
|
517
|
+
PostgresPollingStatusType status;
|
518
|
+
|
519
|
+
pgconn_close_socket_io(self);
|
520
|
+
status = gvl_PQconnectPoll(pg_get_pgconn(self));
|
521
|
+
|
522
|
+
return INT2FIX((int)status);
|
523
|
+
}
|
524
|
+
|
525
|
+
/*
|
526
|
+
* call-seq:
|
527
|
+
* conn.finish
|
528
|
+
*
|
529
|
+
* Closes the backend connection.
|
530
|
+
*/
|
531
|
+
static VALUE
|
532
|
+
pgconn_finish( VALUE self )
|
533
|
+
{
|
534
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
535
|
+
|
536
|
+
pgconn_close_socket_io( self );
|
537
|
+
PQfinish( this->pgconn );
|
538
|
+
this->pgconn = NULL;
|
539
|
+
return Qnil;
|
540
|
+
}
|
541
|
+
|
542
|
+
|
543
|
+
/*
|
544
|
+
* call-seq:
|
545
|
+
* conn.finished? -> boolean
|
546
|
+
*
|
547
|
+
* Returns +true+ if the backend connection has been closed.
|
548
|
+
*/
|
549
|
+
static VALUE
|
550
|
+
pgconn_finished_p( VALUE self )
|
551
|
+
{
|
552
|
+
t_pg_connection *this = pg_get_connection( self );
|
553
|
+
if ( this->pgconn ) return Qfalse;
|
554
|
+
return Qtrue;
|
555
|
+
}
|
556
|
+
|
557
|
+
|
558
|
+
static VALUE
|
559
|
+
pgconn_sync_reset( VALUE self )
|
560
|
+
{
|
561
|
+
pgconn_close_socket_io( self );
|
562
|
+
gvl_PQreset( pg_get_pgconn(self) );
|
563
|
+
return self;
|
564
|
+
}
|
565
|
+
|
566
|
+
static VALUE
|
567
|
+
pgconn_reset_start2( VALUE self, VALUE conninfo )
|
568
|
+
{
|
569
|
+
t_pg_connection *this = pg_get_connection( self );
|
570
|
+
|
571
|
+
/* Close old connection */
|
572
|
+
pgconn_close_socket_io( self );
|
573
|
+
PQfinish( this->pgconn );
|
574
|
+
|
575
|
+
/* Start new connection */
|
576
|
+
this->pgconn = gvl_PQconnectStart( StringValueCStr(conninfo) );
|
577
|
+
|
578
|
+
if( this->pgconn == NULL )
|
579
|
+
rb_raise(rb_ePGerror, "PQconnectStart() unable to allocate PGconn structure");
|
580
|
+
|
581
|
+
if ( PQstatus(this->pgconn) == CONNECTION_BAD )
|
582
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "%s", PQerrorMessage(this->pgconn));
|
583
|
+
|
584
|
+
return Qnil;
|
585
|
+
}
|
586
|
+
|
587
|
+
/*
|
588
|
+
* call-seq:
|
589
|
+
* conn.reset_start() -> nil
|
590
|
+
*
|
591
|
+
* Initiate a connection reset in a nonblocking manner.
|
592
|
+
* This will close the current connection and attempt to
|
593
|
+
* reconnect using the same connection parameters.
|
594
|
+
* Use #reset_poll to check the status of the
|
595
|
+
* connection reset.
|
596
|
+
*/
|
597
|
+
static VALUE
|
598
|
+
pgconn_reset_start(VALUE self)
|
599
|
+
{
|
600
|
+
pgconn_close_socket_io( self );
|
601
|
+
if(gvl_PQresetStart(pg_get_pgconn(self)) == 0)
|
602
|
+
pg_raise_conn_error( rb_eUnableToSend, self, "reset has failed");
|
603
|
+
return Qnil;
|
604
|
+
}
|
605
|
+
|
606
|
+
/*
|
607
|
+
* call-seq:
|
608
|
+
* conn.reset_poll -> Integer
|
609
|
+
*
|
610
|
+
* Checks the status of a connection reset operation.
|
611
|
+
* See Connection.connect_start and #connect_poll for
|
612
|
+
* usage information and return values.
|
613
|
+
*/
|
614
|
+
static VALUE
|
615
|
+
pgconn_reset_poll(VALUE self)
|
616
|
+
{
|
617
|
+
PostgresPollingStatusType status;
|
618
|
+
|
619
|
+
pgconn_close_socket_io(self);
|
620
|
+
status = gvl_PQresetPoll(pg_get_pgconn(self));
|
621
|
+
|
622
|
+
return INT2FIX((int)status);
|
623
|
+
}
|
624
|
+
|
625
|
+
|
626
|
+
/*
|
627
|
+
* call-seq:
|
628
|
+
* conn.db()
|
629
|
+
*
|
630
|
+
* Returns the connected database name.
|
631
|
+
*/
|
632
|
+
static VALUE
|
633
|
+
pgconn_db(VALUE self)
|
634
|
+
{
|
635
|
+
char *db = PQdb(pg_get_pgconn(self));
|
636
|
+
if (!db) return Qnil;
|
637
|
+
return rb_str_new2(db);
|
638
|
+
}
|
639
|
+
|
640
|
+
/*
|
641
|
+
* call-seq:
|
642
|
+
* conn.user()
|
643
|
+
*
|
644
|
+
* Returns the authenticated user name.
|
645
|
+
*/
|
646
|
+
static VALUE
|
647
|
+
pgconn_user(VALUE self)
|
648
|
+
{
|
649
|
+
char *user = PQuser(pg_get_pgconn(self));
|
650
|
+
if (!user) return Qnil;
|
651
|
+
return rb_str_new2(user);
|
652
|
+
}
|
653
|
+
|
654
|
+
/*
|
655
|
+
* call-seq:
|
656
|
+
* conn.pass()
|
657
|
+
*
|
658
|
+
* Returns the authenticated password.
|
659
|
+
*/
|
660
|
+
static VALUE
|
661
|
+
pgconn_pass(VALUE self)
|
662
|
+
{
|
663
|
+
char *user = PQpass(pg_get_pgconn(self));
|
664
|
+
if (!user) return Qnil;
|
665
|
+
return rb_str_new2(user);
|
666
|
+
}
|
667
|
+
|
668
|
+
/*
|
669
|
+
* call-seq:
|
670
|
+
* conn.host()
|
671
|
+
*
|
672
|
+
* Returns the server host name of the active connection.
|
673
|
+
* This can be a host name, an IP address, or a directory path if the connection is via Unix socket.
|
674
|
+
* (The path case can be distinguished because it will always be an absolute path, beginning with +/+ .)
|
675
|
+
*
|
676
|
+
* If the connection parameters specified both host and hostaddr, then +host+ will return the host information.
|
677
|
+
* If only hostaddr was specified, then that is returned.
|
678
|
+
* If multiple hosts were specified in the connection parameters, +host+ returns the host actually connected to.
|
679
|
+
*
|
680
|
+
* If there is an error producing the host information (perhaps if the connection has not been fully established or there was an error), it returns an empty string.
|
681
|
+
*
|
682
|
+
* If multiple hosts were specified in the connection parameters, it is not possible to rely on the result of +host+ until the connection is established.
|
683
|
+
* The status of the connection can be checked using the function Connection#status .
|
684
|
+
*/
|
685
|
+
static VALUE
|
686
|
+
pgconn_host(VALUE self)
|
687
|
+
{
|
688
|
+
char *host = PQhost(pg_get_pgconn(self));
|
689
|
+
if (!host) return Qnil;
|
690
|
+
return rb_str_new2(host);
|
691
|
+
}
|
692
|
+
|
693
|
+
/* PQhostaddr() appeared in PostgreSQL-12 together with PQresultMemorySize() */
|
694
|
+
#if defined(HAVE_PQRESULTMEMORYSIZE)
|
695
|
+
/*
|
696
|
+
* call-seq:
|
697
|
+
* conn.hostaddr()
|
698
|
+
*
|
699
|
+
* Returns the server IP address of the active connection.
|
700
|
+
* This can be the address that a host name resolved to, or an IP address provided through the hostaddr parameter.
|
701
|
+
* If there is an error producing the host information (perhaps if the connection has not been fully established or there was an error), it returns an empty string.
|
702
|
+
*
|
703
|
+
*/
|
704
|
+
static VALUE
|
705
|
+
pgconn_hostaddr(VALUE self)
|
706
|
+
{
|
707
|
+
char *host = PQhostaddr(pg_get_pgconn(self));
|
708
|
+
if (!host) return Qnil;
|
709
|
+
return rb_str_new2(host);
|
710
|
+
}
|
711
|
+
#endif
|
712
|
+
|
713
|
+
/*
|
714
|
+
* call-seq:
|
715
|
+
* conn.port()
|
716
|
+
*
|
717
|
+
* Returns the connected server port number.
|
718
|
+
*/
|
719
|
+
static VALUE
|
720
|
+
pgconn_port(VALUE self)
|
721
|
+
{
|
722
|
+
char* port = PQport(pg_get_pgconn(self));
|
723
|
+
if (!port || port[0] == '\0')
|
724
|
+
return INT2NUM(DEF_PGPORT);
|
725
|
+
else
|
726
|
+
return INT2NUM(atoi(port));
|
727
|
+
}
|
728
|
+
|
729
|
+
/*
|
730
|
+
* call-seq:
|
731
|
+
* conn.tty()
|
732
|
+
*
|
733
|
+
* Obsolete function.
|
734
|
+
*/
|
735
|
+
static VALUE
|
736
|
+
pgconn_tty(VALUE self)
|
737
|
+
{
|
738
|
+
return rb_str_new2("");
|
739
|
+
}
|
740
|
+
|
741
|
+
/*
|
742
|
+
* call-seq:
|
743
|
+
* conn.options()
|
744
|
+
*
|
745
|
+
* Returns backend option string.
|
746
|
+
*/
|
747
|
+
static VALUE
|
748
|
+
pgconn_options(VALUE self)
|
749
|
+
{
|
750
|
+
char *options = PQoptions(pg_get_pgconn(self));
|
751
|
+
if (!options) return Qnil;
|
752
|
+
return rb_str_new2(options);
|
753
|
+
}
|
754
|
+
|
755
|
+
|
756
|
+
/*
|
757
|
+
* call-seq:
|
758
|
+
* conn.conninfo -> hash
|
759
|
+
*
|
760
|
+
* Returns the connection options used by a live connection.
|
761
|
+
*
|
762
|
+
*/
|
763
|
+
static VALUE
|
764
|
+
pgconn_conninfo( VALUE self )
|
765
|
+
{
|
766
|
+
PGconn *conn = pg_get_pgconn(self);
|
767
|
+
PQconninfoOption *options = PQconninfo( conn );
|
768
|
+
VALUE array = pgconn_make_conninfo_array( options );
|
769
|
+
|
770
|
+
PQconninfoFree(options);
|
771
|
+
|
772
|
+
return array;
|
773
|
+
}
|
774
|
+
|
775
|
+
|
776
|
+
/*
|
777
|
+
* call-seq:
|
778
|
+
* conn.status()
|
779
|
+
*
|
780
|
+
* Returns the status of the connection, which is one:
|
781
|
+
* PG::Constants::CONNECTION_OK
|
782
|
+
* PG::Constants::CONNECTION_BAD
|
783
|
+
*
|
784
|
+
* ... and other constants of kind PG::Constants::CONNECTION_*
|
785
|
+
*
|
786
|
+
* This method returns the status of the last command from memory.
|
787
|
+
* It doesn't do any socket access hence is not suitable to test the connectivity.
|
788
|
+
* See check_socket for a way to verify the socket state.
|
789
|
+
*
|
790
|
+
* Example:
|
791
|
+
* PG.constants.grep(/CONNECTION_/).find{|c| PG.const_get(c) == conn.status} # => :CONNECTION_OK
|
792
|
+
*/
|
793
|
+
static VALUE
|
794
|
+
pgconn_status(VALUE self)
|
795
|
+
{
|
796
|
+
return INT2NUM(PQstatus(pg_get_pgconn(self)));
|
797
|
+
}
|
798
|
+
|
799
|
+
/*
|
800
|
+
* call-seq:
|
801
|
+
* conn.transaction_status()
|
802
|
+
*
|
803
|
+
* returns one of the following statuses:
|
804
|
+
* PQTRANS_IDLE = 0 (connection idle)
|
805
|
+
* PQTRANS_ACTIVE = 1 (command in progress)
|
806
|
+
* PQTRANS_INTRANS = 2 (idle, within transaction block)
|
807
|
+
* PQTRANS_INERROR = 3 (idle, within failed transaction)
|
808
|
+
* PQTRANS_UNKNOWN = 4 (cannot determine status)
|
809
|
+
*/
|
810
|
+
static VALUE
|
811
|
+
pgconn_transaction_status(VALUE self)
|
812
|
+
{
|
813
|
+
return INT2NUM(PQtransactionStatus(pg_get_pgconn(self)));
|
814
|
+
}
|
815
|
+
|
816
|
+
/*
|
817
|
+
* call-seq:
|
818
|
+
* conn.parameter_status( param_name ) -> String
|
819
|
+
*
|
820
|
+
* Returns the setting of parameter _param_name_, where
|
821
|
+
* _param_name_ is one of
|
822
|
+
* * +server_version+
|
823
|
+
* * +server_encoding+
|
824
|
+
* * +client_encoding+
|
825
|
+
* * +is_superuser+
|
826
|
+
* * +session_authorization+
|
827
|
+
* * +DateStyle+
|
828
|
+
* * +TimeZone+
|
829
|
+
* * +integer_datetimes+
|
830
|
+
* * +standard_conforming_strings+
|
831
|
+
*
|
832
|
+
* Returns nil if the value of the parameter is not known.
|
833
|
+
*/
|
834
|
+
static VALUE
|
835
|
+
pgconn_parameter_status(VALUE self, VALUE param_name)
|
836
|
+
{
|
837
|
+
const char *ret = PQparameterStatus(pg_get_pgconn(self), StringValueCStr(param_name));
|
838
|
+
if(ret == NULL)
|
839
|
+
return Qnil;
|
840
|
+
else
|
841
|
+
return rb_str_new2(ret);
|
842
|
+
}
|
843
|
+
|
844
|
+
/*
|
845
|
+
* call-seq:
|
846
|
+
* conn.protocol_version -> Integer
|
847
|
+
*
|
848
|
+
* The 3.0 protocol will normally be used when communicating with PostgreSQL 7.4
|
849
|
+
* or later servers; pre-7.4 servers support only protocol 2.0. (Protocol 1.0 is
|
850
|
+
* obsolete and not supported by libpq.)
|
851
|
+
*/
|
852
|
+
static VALUE
|
853
|
+
pgconn_protocol_version(VALUE self)
|
854
|
+
{
|
855
|
+
return INT2NUM(PQprotocolVersion(pg_get_pgconn(self)));
|
856
|
+
}
|
857
|
+
|
858
|
+
/*
|
859
|
+
* call-seq:
|
860
|
+
* conn.server_version -> Integer
|
861
|
+
*
|
862
|
+
* The number is formed by converting the major, minor, and revision
|
863
|
+
* numbers into two-decimal-digit numbers and appending them together.
|
864
|
+
* For example, version 7.4.2 will be returned as 70402, and version
|
865
|
+
* 8.1 will be returned as 80100 (leading zeroes are not shown). Zero
|
866
|
+
* is returned if the connection is bad.
|
867
|
+
*
|
868
|
+
*/
|
869
|
+
static VALUE
|
870
|
+
pgconn_server_version(VALUE self)
|
871
|
+
{
|
872
|
+
return INT2NUM(PQserverVersion(pg_get_pgconn(self)));
|
873
|
+
}
|
874
|
+
|
875
|
+
/*
|
876
|
+
* call-seq:
|
877
|
+
* conn.error_message -> String
|
878
|
+
*
|
879
|
+
* Returns the error message most recently generated by an operation on the connection.
|
880
|
+
*
|
881
|
+
* Nearly all libpq functions will set a message for conn.error_message if they fail.
|
882
|
+
* Note that by libpq convention, a nonempty error_message result can consist of multiple lines, and will include a trailing newline.
|
883
|
+
*/
|
884
|
+
static VALUE
|
885
|
+
pgconn_error_message(VALUE self)
|
886
|
+
{
|
887
|
+
char *error = PQerrorMessage(pg_get_pgconn(self));
|
888
|
+
if (!error) return Qnil;
|
889
|
+
return rb_str_new2(error);
|
890
|
+
}
|
891
|
+
|
892
|
+
/*
|
893
|
+
* call-seq:
|
894
|
+
* conn.socket() -> Integer
|
895
|
+
*
|
896
|
+
* This method is deprecated. Please use the more portable method #socket_io .
|
897
|
+
*
|
898
|
+
* Returns the socket's file descriptor for this connection.
|
899
|
+
* <tt>IO.for_fd()</tt> can be used to build a proper IO object to the socket.
|
900
|
+
* If you do so, you will likely also want to set <tt>autoclose=false</tt>
|
901
|
+
* on it to prevent Ruby from closing the socket to PostgreSQL if it
|
902
|
+
* goes out of scope. Alternatively, you can use #socket_io, which
|
903
|
+
* creates an IO that's associated with the connection object itself,
|
904
|
+
* and so won't go out of scope until the connection does.
|
905
|
+
*
|
906
|
+
* *Note:* On Windows the file descriptor is not usable,
|
907
|
+
* since it can not be used to build a Ruby IO object.
|
908
|
+
*/
|
909
|
+
static VALUE
|
910
|
+
pgconn_socket(VALUE self)
|
911
|
+
{
|
912
|
+
int sd;
|
913
|
+
pg_deprecated(4, ("conn.socket is deprecated and should be replaced by conn.socket_io"));
|
914
|
+
|
915
|
+
if( (sd = PQsocket(pg_get_pgconn(self))) < 0)
|
916
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "PQsocket() can't get socket descriptor");
|
917
|
+
|
918
|
+
return INT2NUM(sd);
|
919
|
+
}
|
920
|
+
|
921
|
+
|
922
|
+
VALUE
|
923
|
+
pg_wrap_socket_io(int sd, VALUE self, VALUE *p_socket_io, int *p_ruby_sd)
|
924
|
+
{
|
925
|
+
int ruby_sd;
|
926
|
+
VALUE cSocket;
|
927
|
+
VALUE socket_io = *p_socket_io;
|
928
|
+
|
929
|
+
#ifdef _WIN32
|
930
|
+
ruby_sd = rb_w32_wrap_io_handle((HANDLE)(intptr_t)sd, O_RDWR|O_BINARY|O_NOINHERIT);
|
931
|
+
if( ruby_sd == -1 )
|
932
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "Could not wrap win32 socket handle");
|
933
|
+
|
934
|
+
*p_ruby_sd = ruby_sd;
|
935
|
+
#else
|
936
|
+
*p_ruby_sd = ruby_sd = sd;
|
937
|
+
#endif
|
938
|
+
|
939
|
+
cSocket = rb_const_get(rb_cObject, rb_intern("BasicSocket"));
|
940
|
+
socket_io = rb_funcall( cSocket, rb_intern("for_fd"), 1, INT2NUM(ruby_sd));
|
941
|
+
|
942
|
+
/* Disable autoclose feature */
|
943
|
+
rb_funcall( socket_io, s_id_autoclose_set, 1, Qfalse );
|
944
|
+
|
945
|
+
RB_OBJ_WRITE(self, p_socket_io, socket_io);
|
946
|
+
|
947
|
+
return socket_io;
|
948
|
+
}
|
949
|
+
|
950
|
+
/*
|
951
|
+
* call-seq:
|
952
|
+
* conn.socket_io() -> IO
|
953
|
+
*
|
954
|
+
* Fetch an IO object created from the Connection's underlying socket.
|
955
|
+
* 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.
|
956
|
+
* <tt>IO#wait_*able</tt> is <tt>Fiber.scheduler</tt> compatible in contrast to <tt>IO.select</tt>.
|
957
|
+
*
|
958
|
+
* The IO object can change while the connection is established, but is memorized afterwards.
|
959
|
+
* So be sure not to cache the IO object, but repeat calling <tt>conn.socket_io</tt> instead.
|
960
|
+
*
|
961
|
+
* Using this method also works on Windows in contrast to using #socket .
|
962
|
+
* It also avoids the problem of the underlying connection being closed by Ruby when an IO created using <tt>IO.for_fd(conn.socket)</tt> goes out of scope.
|
963
|
+
*/
|
964
|
+
static VALUE
|
965
|
+
pgconn_socket_io(VALUE self)
|
966
|
+
{
|
967
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
968
|
+
|
969
|
+
if ( !RTEST(this->socket_io) ) {
|
970
|
+
int sd;
|
971
|
+
if( (sd = PQsocket(this->pgconn)) < 0){
|
972
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "PQsocket() can't get socket descriptor");
|
973
|
+
}
|
974
|
+
return pg_wrap_socket_io( sd, self, &this->socket_io, &this->ruby_sd);
|
975
|
+
}
|
976
|
+
|
977
|
+
return this->socket_io;
|
978
|
+
}
|
979
|
+
|
980
|
+
/*
|
981
|
+
* call-seq:
|
982
|
+
* conn.backend_pid() -> Integer
|
983
|
+
*
|
984
|
+
* Returns the process ID of the backend server
|
985
|
+
* process for this connection.
|
986
|
+
* Note that this is a PID on database server host.
|
987
|
+
*/
|
988
|
+
static VALUE
|
989
|
+
pgconn_backend_pid(VALUE self)
|
990
|
+
{
|
991
|
+
return INT2NUM(PQbackendPID(pg_get_pgconn(self)));
|
992
|
+
}
|
993
|
+
|
994
|
+
#ifndef HAVE_PQSETCHUNKEDROWSMODE
|
995
|
+
typedef struct
|
996
|
+
{
|
997
|
+
struct sockaddr_storage addr;
|
998
|
+
socklen_t salen;
|
999
|
+
} SockAddr;
|
1000
|
+
|
1001
|
+
/* Copy of struct pg_cancel from libpq-int.h
|
1002
|
+
*
|
1003
|
+
* See https://github.com/postgres/postgres/blame/master/src/interfaces/libpq/libpq-int.h#L577-L586
|
1004
|
+
*/
|
1005
|
+
struct pg_cancel
|
1006
|
+
{
|
1007
|
+
SockAddr raddr; /* Remote address */
|
1008
|
+
int be_pid; /* PID of backend --- needed for cancels */
|
1009
|
+
int be_key; /* key of backend --- needed for cancels */
|
1010
|
+
};
|
1011
|
+
|
1012
|
+
/*
|
1013
|
+
* call-seq:
|
1014
|
+
* conn.backend_key() -> Integer
|
1015
|
+
*
|
1016
|
+
* Returns the key of the backend server process for this connection.
|
1017
|
+
* This key can be used to cancel queries on the server.
|
1018
|
+
*/
|
1019
|
+
static VALUE
|
1020
|
+
pgconn_backend_key(VALUE self)
|
1021
|
+
{
|
1022
|
+
int be_key;
|
1023
|
+
struct pg_cancel *cancel;
|
1024
|
+
PGconn *conn = pg_get_pgconn(self);
|
1025
|
+
|
1026
|
+
cancel = (struct pg_cancel*)PQgetCancel(conn);
|
1027
|
+
if(cancel == NULL)
|
1028
|
+
pg_raise_conn_error( rb_ePGerror, self, "Invalid connection!");
|
1029
|
+
|
1030
|
+
if( cancel->be_pid != PQbackendPID(conn) )
|
1031
|
+
rb_raise(rb_ePGerror,"Unexpected binary struct layout - please file a bug report at ruby-pg!");
|
1032
|
+
|
1033
|
+
be_key = cancel->be_key;
|
1034
|
+
|
1035
|
+
PQfreeCancel(cancel);
|
1036
|
+
|
1037
|
+
return INT2NUM(be_key);
|
1038
|
+
}
|
1039
|
+
#endif
|
1040
|
+
|
1041
|
+
/*
|
1042
|
+
* call-seq:
|
1043
|
+
* conn.connection_needs_password() -> Boolean
|
1044
|
+
*
|
1045
|
+
* Returns +true+ if the authentication method required a
|
1046
|
+
* password, but none was available. +false+ otherwise.
|
1047
|
+
*/
|
1048
|
+
static VALUE
|
1049
|
+
pgconn_connection_needs_password(VALUE self)
|
1050
|
+
{
|
1051
|
+
return PQconnectionNeedsPassword(pg_get_pgconn(self)) ? Qtrue : Qfalse;
|
1052
|
+
}
|
1053
|
+
|
1054
|
+
/*
|
1055
|
+
* call-seq:
|
1056
|
+
* conn.connection_used_password() -> Boolean
|
1057
|
+
*
|
1058
|
+
* Returns +true+ if the authentication method used
|
1059
|
+
* a caller-supplied password, +false+ otherwise.
|
1060
|
+
*/
|
1061
|
+
static VALUE
|
1062
|
+
pgconn_connection_used_password(VALUE self)
|
1063
|
+
{
|
1064
|
+
return PQconnectionUsedPassword(pg_get_pgconn(self)) ? Qtrue : Qfalse;
|
1065
|
+
}
|
1066
|
+
|
1067
|
+
|
1068
|
+
/* :TODO: get_ssl */
|
1069
|
+
|
1070
|
+
|
1071
|
+
static VALUE pgconn_sync_exec_params( int, VALUE *, VALUE );
|
1072
|
+
|
1073
|
+
/*
|
1074
|
+
* call-seq:
|
1075
|
+
* conn.sync_exec(sql) -> PG::Result
|
1076
|
+
* conn.sync_exec(sql) {|pg_result| block }
|
1077
|
+
*
|
1078
|
+
* This function has the same behavior as #async_exec, but is implemented using the synchronous command processing API of libpq.
|
1079
|
+
* It's not recommended to use explicit sync or async variants but #exec instead, unless you have a good reason to do so.
|
1080
|
+
*
|
1081
|
+
* Both #sync_exec and #async_exec release the GVL while waiting for server response, so that concurrent threads will get executed.
|
1082
|
+
* However #async_exec has two advantages:
|
1083
|
+
*
|
1084
|
+
* 1. #async_exec can be aborted by signals (like Ctrl-C), while #exec blocks signal processing until the query is answered.
|
1085
|
+
* 2. Ruby VM gets notified about IO blocked operations and can pass them through <tt>Fiber.scheduler</tt>.
|
1086
|
+
* So only <tt>async_*</tt> methods are compatible to event based schedulers like the async gem.
|
1087
|
+
*/
|
1088
|
+
static VALUE
|
1089
|
+
pgconn_sync_exec(int argc, VALUE *argv, VALUE self)
|
1090
|
+
{
|
1091
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1092
|
+
PGresult *result = NULL;
|
1093
|
+
VALUE rb_pgresult;
|
1094
|
+
|
1095
|
+
/* If called with no or nil parameters, use PQexec for compatibility */
|
1096
|
+
if ( argc == 1 || (argc >= 2 && argc <= 4 && NIL_P(argv[1]) )) {
|
1097
|
+
VALUE query_str = argv[0];
|
1098
|
+
|
1099
|
+
result = gvl_PQexec(this->pgconn, pg_cstr_enc(query_str, this->enc_idx));
|
1100
|
+
rb_pgresult = pg_new_result(result, self);
|
1101
|
+
pg_result_check(rb_pgresult);
|
1102
|
+
if (rb_block_given_p()) {
|
1103
|
+
return rb_ensure(rb_yield, rb_pgresult, pg_result_clear, rb_pgresult);
|
1104
|
+
}
|
1105
|
+
return rb_pgresult;
|
1106
|
+
}
|
1107
|
+
pg_deprecated(0, ("forwarding exec to exec_params is deprecated"));
|
1108
|
+
|
1109
|
+
/* Otherwise, just call #exec_params instead for backward-compatibility */
|
1110
|
+
return pgconn_sync_exec_params( argc, argv, self );
|
1111
|
+
|
1112
|
+
}
|
1113
|
+
|
1114
|
+
|
1115
|
+
struct linked_typecast_data {
|
1116
|
+
struct linked_typecast_data *next;
|
1117
|
+
char data[0];
|
1118
|
+
};
|
1119
|
+
|
1120
|
+
/* This struct is allocated on the stack for all query execution functions. */
|
1121
|
+
struct query_params_data {
|
1122
|
+
|
1123
|
+
/*
|
1124
|
+
* Filled by caller
|
1125
|
+
*/
|
1126
|
+
|
1127
|
+
/* The character encoding index of the connection. Any strings
|
1128
|
+
* given as query parameters are converted to this encoding.
|
1129
|
+
*/
|
1130
|
+
int enc_idx;
|
1131
|
+
/* Is the query function to execute one with types array? */
|
1132
|
+
int with_types;
|
1133
|
+
/* Array of query params from user space */
|
1134
|
+
VALUE params;
|
1135
|
+
/* The typemap given from user space */
|
1136
|
+
VALUE typemap;
|
1137
|
+
|
1138
|
+
/*
|
1139
|
+
* Filled by alloc_query_params()
|
1140
|
+
*/
|
1141
|
+
|
1142
|
+
/* Wraps the pointer of allocated memory, if function parameters don't
|
1143
|
+
* fit in the memory_pool below.
|
1144
|
+
*/
|
1145
|
+
VALUE heap_pool;
|
1146
|
+
|
1147
|
+
/* Pointer to the value string pointers (either within memory_pool or heap_pool).
|
1148
|
+
* The value strings itself are either directly within RString memory or,
|
1149
|
+
* in case of type casted values, within memory_pool or typecast_heap_chain.
|
1150
|
+
*/
|
1151
|
+
char **values;
|
1152
|
+
/* Pointer to the param lengths (either within memory_pool or heap_pool) */
|
1153
|
+
int *lengths;
|
1154
|
+
/* Pointer to the format codes (either within memory_pool or heap_pool) */
|
1155
|
+
int *formats;
|
1156
|
+
/* Pointer to the OID types (either within memory_pool or heap_pool) */
|
1157
|
+
Oid *types;
|
1158
|
+
|
1159
|
+
/* This array takes the string values for the timeframe of the query,
|
1160
|
+
* if param value conversion is required
|
1161
|
+
*/
|
1162
|
+
VALUE gc_array;
|
1163
|
+
|
1164
|
+
/* Wraps a single linked list of allocated memory chunks for type casted params.
|
1165
|
+
* Used when the memory_pool is to small.
|
1166
|
+
*/
|
1167
|
+
VALUE typecast_heap_chain;
|
1168
|
+
|
1169
|
+
/* This memory pool is used to place above query function parameters on it. */
|
1170
|
+
char memory_pool[QUERYDATA_BUFFER_SIZE];
|
1171
|
+
};
|
1172
|
+
|
1173
|
+
static void
|
1174
|
+
free_typecast_heap_chain(void *_chain_entry)
|
1175
|
+
{
|
1176
|
+
struct linked_typecast_data *chain_entry = (struct linked_typecast_data *)_chain_entry;
|
1177
|
+
while(chain_entry){
|
1178
|
+
struct linked_typecast_data *next = chain_entry->next;
|
1179
|
+
xfree(chain_entry);
|
1180
|
+
chain_entry = next;
|
1181
|
+
}
|
1182
|
+
}
|
1183
|
+
|
1184
|
+
static const rb_data_type_t pg_typecast_buffer_type = {
|
1185
|
+
"PG::Connection typecast buffer chain",
|
1186
|
+
{
|
1187
|
+
(RUBY_DATA_FUNC) NULL,
|
1188
|
+
free_typecast_heap_chain,
|
1189
|
+
(size_t (*)(const void *))NULL,
|
1190
|
+
},
|
1191
|
+
0,
|
1192
|
+
0,
|
1193
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
1194
|
+
};
|
1195
|
+
|
1196
|
+
static char *
|
1197
|
+
alloc_typecast_buf( VALUE *typecast_heap_chain, int len )
|
1198
|
+
{
|
1199
|
+
/* Allocate a new memory chunk from heap */
|
1200
|
+
struct linked_typecast_data *allocated =
|
1201
|
+
(struct linked_typecast_data *)xmalloc(sizeof(struct linked_typecast_data) + len);
|
1202
|
+
|
1203
|
+
/* Did we already wrap a memory chain per T_DATA object? */
|
1204
|
+
if( NIL_P( *typecast_heap_chain ) ){
|
1205
|
+
/* Leave free'ing of the buffer chain to the GC, when paramsData has left the stack */
|
1206
|
+
*typecast_heap_chain = TypedData_Wrap_Struct( rb_cObject, &pg_typecast_buffer_type, allocated );
|
1207
|
+
allocated->next = NULL;
|
1208
|
+
} else {
|
1209
|
+
/* Append to the chain */
|
1210
|
+
allocated->next = RTYPEDDATA_DATA( *typecast_heap_chain );
|
1211
|
+
RTYPEDDATA_DATA( *typecast_heap_chain ) = allocated;
|
1212
|
+
}
|
1213
|
+
|
1214
|
+
return &allocated->data[0];
|
1215
|
+
}
|
1216
|
+
|
1217
|
+
static const rb_data_type_t pg_query_heap_pool_type = {
|
1218
|
+
"PG::Connection query heap pool",
|
1219
|
+
{
|
1220
|
+
(RUBY_DATA_FUNC) NULL,
|
1221
|
+
RUBY_TYPED_DEFAULT_FREE,
|
1222
|
+
(size_t (*)(const void *))NULL,
|
1223
|
+
},
|
1224
|
+
0,
|
1225
|
+
0,
|
1226
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
|
1227
|
+
};
|
1228
|
+
|
1229
|
+
static int
|
1230
|
+
alloc_query_params(struct query_params_data *paramsData)
|
1231
|
+
{
|
1232
|
+
VALUE param_value;
|
1233
|
+
t_typemap *p_typemap;
|
1234
|
+
int nParams;
|
1235
|
+
int i=0;
|
1236
|
+
t_pg_coder *conv;
|
1237
|
+
unsigned int required_pool_size;
|
1238
|
+
char *memory_pool;
|
1239
|
+
|
1240
|
+
Check_Type(paramsData->params, T_ARRAY);
|
1241
|
+
|
1242
|
+
p_typemap = RTYPEDDATA_DATA( paramsData->typemap );
|
1243
|
+
p_typemap->funcs.fit_to_query( paramsData->typemap, paramsData->params );
|
1244
|
+
|
1245
|
+
paramsData->heap_pool = Qnil;
|
1246
|
+
paramsData->typecast_heap_chain = Qnil;
|
1247
|
+
paramsData->gc_array = Qnil;
|
1248
|
+
|
1249
|
+
nParams = (int)RARRAY_LEN(paramsData->params);
|
1250
|
+
|
1251
|
+
required_pool_size = nParams * (
|
1252
|
+
sizeof(char *) +
|
1253
|
+
sizeof(int) +
|
1254
|
+
sizeof(int) +
|
1255
|
+
(paramsData->with_types ? sizeof(Oid) : 0));
|
1256
|
+
|
1257
|
+
if( sizeof(paramsData->memory_pool) < required_pool_size ){
|
1258
|
+
/* Allocate one combined memory pool for all possible function parameters */
|
1259
|
+
memory_pool = (char*)xmalloc( required_pool_size );
|
1260
|
+
/* Leave free'ing of the buffer to the GC, when paramsData has left the stack */
|
1261
|
+
paramsData->heap_pool = TypedData_Wrap_Struct( rb_cObject, &pg_query_heap_pool_type, memory_pool );
|
1262
|
+
required_pool_size = 0;
|
1263
|
+
}else{
|
1264
|
+
/* Use stack memory for function parameters */
|
1265
|
+
memory_pool = paramsData->memory_pool;
|
1266
|
+
}
|
1267
|
+
|
1268
|
+
paramsData->values = (char **)memory_pool;
|
1269
|
+
paramsData->lengths = (int *)((char*)paramsData->values + sizeof(char *) * nParams);
|
1270
|
+
paramsData->formats = (int *)((char*)paramsData->lengths + sizeof(int) * nParams);
|
1271
|
+
paramsData->types = (Oid *)((char*)paramsData->formats + sizeof(int) * nParams);
|
1272
|
+
|
1273
|
+
{
|
1274
|
+
char *typecast_buf = paramsData->memory_pool + required_pool_size;
|
1275
|
+
|
1276
|
+
for ( i = 0; i < nParams; i++ ) {
|
1277
|
+
param_value = rb_ary_entry(paramsData->params, i);
|
1278
|
+
|
1279
|
+
paramsData->formats[i] = 0;
|
1280
|
+
if( paramsData->with_types )
|
1281
|
+
paramsData->types[i] = 0;
|
1282
|
+
|
1283
|
+
/* Let the given typemap select a coder for this param */
|
1284
|
+
conv = p_typemap->funcs.typecast_query_param(p_typemap, param_value, i);
|
1285
|
+
|
1286
|
+
/* Using a coder object for the param_value? Then set it's format code and oid. */
|
1287
|
+
if( conv ){
|
1288
|
+
paramsData->formats[i] = conv->format;
|
1289
|
+
if( paramsData->with_types )
|
1290
|
+
paramsData->types[i] = conv->oid;
|
1291
|
+
} else {
|
1292
|
+
/* No coder, but got we a hash form for the query param?
|
1293
|
+
* Then take format code and oid from there. */
|
1294
|
+
if (TYPE(param_value) == T_HASH) {
|
1295
|
+
VALUE format_value = rb_hash_aref(param_value, sym_format);
|
1296
|
+
if( !NIL_P(format_value) )
|
1297
|
+
paramsData->formats[i] = NUM2INT(format_value);
|
1298
|
+
if( paramsData->with_types ){
|
1299
|
+
VALUE type_value = rb_hash_aref(param_value, sym_type);
|
1300
|
+
if( !NIL_P(type_value) )
|
1301
|
+
paramsData->types[i] = NUM2UINT(type_value);
|
1302
|
+
}
|
1303
|
+
param_value = rb_hash_aref(param_value, sym_value);
|
1304
|
+
}
|
1305
|
+
}
|
1306
|
+
|
1307
|
+
if( NIL_P(param_value) ){
|
1308
|
+
paramsData->values[i] = NULL;
|
1309
|
+
paramsData->lengths[i] = 0;
|
1310
|
+
} else {
|
1311
|
+
t_pg_coder_enc_func enc_func = pg_coder_enc_func( conv );
|
1312
|
+
VALUE intermediate = Qnil;
|
1313
|
+
|
1314
|
+
/* 1st pass for retiving the required memory space */
|
1315
|
+
int len = enc_func(conv, param_value, NULL, &intermediate, paramsData->enc_idx);
|
1316
|
+
|
1317
|
+
if( len == -1 ){
|
1318
|
+
/* The intermediate value is a String that can be used directly. */
|
1319
|
+
|
1320
|
+
/* Ensure that the String object is zero terminated as expected by libpq. */
|
1321
|
+
if( paramsData->formats[i] == 0 )
|
1322
|
+
StringValueCStr(intermediate);
|
1323
|
+
/* In case a new string object was generated, make sure it doesn't get freed by the GC */
|
1324
|
+
if( intermediate != param_value ){
|
1325
|
+
if( NIL_P(paramsData->gc_array) )
|
1326
|
+
paramsData->gc_array = rb_ary_new();
|
1327
|
+
rb_ary_push(paramsData->gc_array, intermediate);
|
1328
|
+
}
|
1329
|
+
paramsData->values[i] = RSTRING_PTR(intermediate);
|
1330
|
+
paramsData->lengths[i] = RSTRING_LENINT(intermediate);
|
1331
|
+
|
1332
|
+
} else {
|
1333
|
+
/* Is the stack memory pool too small to take the type casted value? */
|
1334
|
+
if( sizeof(paramsData->memory_pool) < required_pool_size + len + 1){
|
1335
|
+
typecast_buf = alloc_typecast_buf( ¶msData->typecast_heap_chain, len + 1 );
|
1336
|
+
}
|
1337
|
+
|
1338
|
+
/* 2nd pass for writing the data to prepared buffer */
|
1339
|
+
len = enc_func(conv, param_value, typecast_buf, &intermediate, paramsData->enc_idx);
|
1340
|
+
paramsData->values[i] = typecast_buf;
|
1341
|
+
if( paramsData->formats[i] == 0 ){
|
1342
|
+
/* text format strings must be zero terminated and lengths are ignored */
|
1343
|
+
typecast_buf[len] = 0;
|
1344
|
+
typecast_buf += len + 1;
|
1345
|
+
required_pool_size += len + 1;
|
1346
|
+
} else {
|
1347
|
+
paramsData->lengths[i] = len;
|
1348
|
+
typecast_buf += len;
|
1349
|
+
required_pool_size += len;
|
1350
|
+
}
|
1351
|
+
}
|
1352
|
+
}
|
1353
|
+
}
|
1354
|
+
}
|
1355
|
+
|
1356
|
+
return nParams;
|
1357
|
+
}
|
1358
|
+
|
1359
|
+
static void
|
1360
|
+
free_query_params(struct query_params_data *paramsData)
|
1361
|
+
{
|
1362
|
+
/* currently nothing to free */
|
1363
|
+
}
|
1364
|
+
|
1365
|
+
void
|
1366
|
+
pgconn_query_assign_typemap( VALUE self, struct query_params_data *paramsData )
|
1367
|
+
{
|
1368
|
+
if(NIL_P(paramsData->typemap)){
|
1369
|
+
/* Use default typemap for queries. It's type is checked when assigned. */
|
1370
|
+
paramsData->typemap = pg_get_connection(self)->type_map_for_queries;
|
1371
|
+
}else{
|
1372
|
+
t_typemap *tm;
|
1373
|
+
UNUSED(tm);
|
1374
|
+
|
1375
|
+
/* Check type of method param */
|
1376
|
+
TypedData_Get_Struct(paramsData->typemap, t_typemap, &pg_typemap_type, tm);
|
1377
|
+
}
|
1378
|
+
}
|
1379
|
+
|
1380
|
+
/*
|
1381
|
+
* call-seq:
|
1382
|
+
* conn.sync_exec_params(sql, params[, result_format[, type_map]] ) -> PG::Result
|
1383
|
+
* conn.sync_exec_params(sql, params[, result_format[, type_map]] ) {|pg_result| block }
|
1384
|
+
*
|
1385
|
+
* This function has the same behavior as #async_exec_params, but is implemented using the synchronous command processing API of libpq.
|
1386
|
+
* See #async_exec for the differences between the two API variants.
|
1387
|
+
* It's not recommended to use explicit sync or async variants but #exec_params instead, unless you have a good reason to do so.
|
1388
|
+
*/
|
1389
|
+
static VALUE
|
1390
|
+
pgconn_sync_exec_params( int argc, VALUE *argv, VALUE self )
|
1391
|
+
{
|
1392
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1393
|
+
PGresult *result = NULL;
|
1394
|
+
VALUE rb_pgresult;
|
1395
|
+
VALUE command, in_res_fmt;
|
1396
|
+
int nParams;
|
1397
|
+
int resultFormat;
|
1398
|
+
struct query_params_data paramsData = { this->enc_idx };
|
1399
|
+
|
1400
|
+
/* For compatibility we accept 1 to 4 parameters */
|
1401
|
+
rb_scan_args(argc, argv, "13", &command, ¶msData.params, &in_res_fmt, ¶msData.typemap);
|
1402
|
+
paramsData.with_types = 1;
|
1403
|
+
|
1404
|
+
/*
|
1405
|
+
* For backward compatibility no or +nil+ for the second parameter
|
1406
|
+
* is passed to #exec
|
1407
|
+
*/
|
1408
|
+
if ( NIL_P(paramsData.params) ) {
|
1409
|
+
pg_deprecated(1, ("forwarding exec_params to exec is deprecated"));
|
1410
|
+
return pgconn_sync_exec( 1, argv, self );
|
1411
|
+
}
|
1412
|
+
pgconn_query_assign_typemap( self, ¶msData );
|
1413
|
+
|
1414
|
+
resultFormat = NIL_P(in_res_fmt) ? 0 : NUM2INT(in_res_fmt);
|
1415
|
+
nParams = alloc_query_params( ¶msData );
|
1416
|
+
|
1417
|
+
result = gvl_PQexecParams(this->pgconn, pg_cstr_enc(command, paramsData.enc_idx), nParams, paramsData.types,
|
1418
|
+
(const char * const *)paramsData.values, paramsData.lengths, paramsData.formats, resultFormat);
|
1419
|
+
|
1420
|
+
free_query_params( ¶msData );
|
1421
|
+
|
1422
|
+
rb_pgresult = pg_new_result(result, self);
|
1423
|
+
pg_result_check(rb_pgresult);
|
1424
|
+
|
1425
|
+
if (rb_block_given_p()) {
|
1426
|
+
return rb_ensure(rb_yield, rb_pgresult, pg_result_clear, rb_pgresult);
|
1427
|
+
}
|
1428
|
+
|
1429
|
+
return rb_pgresult;
|
1430
|
+
}
|
1431
|
+
|
1432
|
+
/*
|
1433
|
+
* call-seq:
|
1434
|
+
* conn.sync_prepare(stmt_name, sql [, param_types ] ) -> PG::Result
|
1435
|
+
*
|
1436
|
+
* This function has the same behavior as #async_prepare, but is implemented using the synchronous command processing API of libpq.
|
1437
|
+
* See #async_exec for the differences between the two API variants.
|
1438
|
+
* It's not recommended to use explicit sync or async variants but #prepare instead, unless you have a good reason to do so.
|
1439
|
+
*/
|
1440
|
+
static VALUE
|
1441
|
+
pgconn_sync_prepare(int argc, VALUE *argv, VALUE self)
|
1442
|
+
{
|
1443
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1444
|
+
PGresult *result = NULL;
|
1445
|
+
VALUE rb_pgresult;
|
1446
|
+
VALUE name, command, in_paramtypes;
|
1447
|
+
VALUE param;
|
1448
|
+
int i = 0;
|
1449
|
+
int nParams = 0;
|
1450
|
+
Oid *paramTypes = NULL;
|
1451
|
+
const char *name_cstr;
|
1452
|
+
const char *command_cstr;
|
1453
|
+
int enc_idx = this->enc_idx;
|
1454
|
+
|
1455
|
+
rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
|
1456
|
+
name_cstr = pg_cstr_enc(name, enc_idx);
|
1457
|
+
command_cstr = pg_cstr_enc(command, enc_idx);
|
1458
|
+
|
1459
|
+
if(! NIL_P(in_paramtypes)) {
|
1460
|
+
Check_Type(in_paramtypes, T_ARRAY);
|
1461
|
+
nParams = (int)RARRAY_LEN(in_paramtypes);
|
1462
|
+
paramTypes = ALLOC_N(Oid, nParams);
|
1463
|
+
for(i = 0; i < nParams; i++) {
|
1464
|
+
param = rb_ary_entry(in_paramtypes, i);
|
1465
|
+
if(param == Qnil)
|
1466
|
+
paramTypes[i] = 0;
|
1467
|
+
else
|
1468
|
+
paramTypes[i] = NUM2UINT(param);
|
1469
|
+
}
|
1470
|
+
}
|
1471
|
+
result = gvl_PQprepare(this->pgconn, name_cstr, command_cstr, nParams, paramTypes);
|
1472
|
+
|
1473
|
+
xfree(paramTypes);
|
1474
|
+
|
1475
|
+
rb_pgresult = pg_new_result(result, self);
|
1476
|
+
pg_result_check(rb_pgresult);
|
1477
|
+
return rb_pgresult;
|
1478
|
+
}
|
1479
|
+
|
1480
|
+
/*
|
1481
|
+
* call-seq:
|
1482
|
+
* conn.sync_exec_prepared(statement_name [, params, result_format[, type_map]] ) -> PG::Result
|
1483
|
+
* conn.sync_exec_prepared(statement_name [, params, result_format[, type_map]] ) {|pg_result| block }
|
1484
|
+
*
|
1485
|
+
* This function has the same behavior as #async_exec_prepared, but is implemented using the synchronous command processing API of libpq.
|
1486
|
+
* See #async_exec for the differences between the two API variants.
|
1487
|
+
* It's not recommended to use explicit sync or async variants but #exec_prepared instead, unless you have a good reason to do so.
|
1488
|
+
*/
|
1489
|
+
static VALUE
|
1490
|
+
pgconn_sync_exec_prepared(int argc, VALUE *argv, VALUE self)
|
1491
|
+
{
|
1492
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1493
|
+
PGresult *result = NULL;
|
1494
|
+
VALUE rb_pgresult;
|
1495
|
+
VALUE name, in_res_fmt;
|
1496
|
+
int nParams;
|
1497
|
+
int resultFormat;
|
1498
|
+
struct query_params_data paramsData = { this->enc_idx };
|
1499
|
+
|
1500
|
+
rb_scan_args(argc, argv, "13", &name, ¶msData.params, &in_res_fmt, ¶msData.typemap);
|
1501
|
+
paramsData.with_types = 0;
|
1502
|
+
|
1503
|
+
if(NIL_P(paramsData.params)) {
|
1504
|
+
paramsData.params = rb_ary_new2(0);
|
1505
|
+
}
|
1506
|
+
pgconn_query_assign_typemap( self, ¶msData );
|
1507
|
+
|
1508
|
+
resultFormat = NIL_P(in_res_fmt) ? 0 : NUM2INT(in_res_fmt);
|
1509
|
+
nParams = alloc_query_params( ¶msData );
|
1510
|
+
|
1511
|
+
result = gvl_PQexecPrepared(this->pgconn, pg_cstr_enc(name, paramsData.enc_idx), nParams,
|
1512
|
+
(const char * const *)paramsData.values, paramsData.lengths, paramsData.formats,
|
1513
|
+
resultFormat);
|
1514
|
+
|
1515
|
+
free_query_params( ¶msData );
|
1516
|
+
|
1517
|
+
rb_pgresult = pg_new_result(result, self);
|
1518
|
+
pg_result_check(rb_pgresult);
|
1519
|
+
if (rb_block_given_p()) {
|
1520
|
+
return rb_ensure(rb_yield, rb_pgresult,
|
1521
|
+
pg_result_clear, rb_pgresult);
|
1522
|
+
}
|
1523
|
+
return rb_pgresult;
|
1524
|
+
}
|
1525
|
+
|
1526
|
+
static VALUE
|
1527
|
+
pgconn_sync_describe_close_prepared_portal(VALUE self, VALUE name, PGresult *(*func)(PGconn *, const char *))
|
1528
|
+
{
|
1529
|
+
PGresult *result;
|
1530
|
+
VALUE rb_pgresult;
|
1531
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1532
|
+
const char *stmt = NIL_P(name) ? NULL : pg_cstr_enc(name, this->enc_idx);
|
1533
|
+
result = func(this->pgconn, stmt);
|
1534
|
+
rb_pgresult = pg_new_result(result, self);
|
1535
|
+
pg_result_check(rb_pgresult);
|
1536
|
+
return rb_pgresult;
|
1537
|
+
}
|
1538
|
+
|
1539
|
+
/*
|
1540
|
+
* call-seq:
|
1541
|
+
* conn.sync_describe_prepared( statement_name ) -> PG::Result
|
1542
|
+
*
|
1543
|
+
* This function has the same behavior as #async_describe_prepared, but is implemented using the synchronous command processing API of libpq.
|
1544
|
+
* See #async_exec for the differences between the two API variants.
|
1545
|
+
* It's not recommended to use explicit sync or async variants but #describe_prepared instead, unless you have a good reason to do so.
|
1546
|
+
*/
|
1547
|
+
static VALUE
|
1548
|
+
pgconn_sync_describe_prepared(VALUE self, VALUE stmt_name)
|
1549
|
+
{
|
1550
|
+
return pgconn_sync_describe_close_prepared_portal(self, stmt_name, gvl_PQdescribePrepared);
|
1551
|
+
}
|
1552
|
+
|
1553
|
+
|
1554
|
+
/*
|
1555
|
+
* call-seq:
|
1556
|
+
* conn.sync_describe_portal( portal_name ) -> PG::Result
|
1557
|
+
*
|
1558
|
+
* This function has the same behavior as #async_describe_portal, but is implemented using the synchronous command processing API of libpq.
|
1559
|
+
* See #async_exec for the differences between the two API variants.
|
1560
|
+
* It's not recommended to use explicit sync or async variants but #describe_portal instead, unless you have a good reason to do so.
|
1561
|
+
*/
|
1562
|
+
static VALUE
|
1563
|
+
pgconn_sync_describe_portal(VALUE self, VALUE stmt_name)
|
1564
|
+
{
|
1565
|
+
return pgconn_sync_describe_close_prepared_portal(self, stmt_name, gvl_PQdescribePortal);
|
1566
|
+
}
|
1567
|
+
|
1568
|
+
|
1569
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
1570
|
+
/*
|
1571
|
+
* call-seq:
|
1572
|
+
* conn.sync_close_prepared( stmt_name ) -> PG::Result
|
1573
|
+
*
|
1574
|
+
* This function has the same behavior as #async_close_prepared, but is implemented using the synchronous command processing API of libpq.
|
1575
|
+
* See #async_exec for the differences between the two API variants.
|
1576
|
+
* It's not recommended to use explicit sync or async variants but #close_prepared instead, unless you have a good reason to do so.
|
1577
|
+
*
|
1578
|
+
* Available since PostgreSQL-17.
|
1579
|
+
*/
|
1580
|
+
static VALUE
|
1581
|
+
pgconn_sync_close_prepared(VALUE self, VALUE stmt_name)
|
1582
|
+
{
|
1583
|
+
return pgconn_sync_describe_close_prepared_portal(self, stmt_name, gvl_PQclosePrepared);
|
1584
|
+
}
|
1585
|
+
|
1586
|
+
/*
|
1587
|
+
* call-seq:
|
1588
|
+
* conn.sync_close_portal( portal_name ) -> PG::Result
|
1589
|
+
*
|
1590
|
+
* This function has the same behavior as #async_close_portal, but is implemented using the synchronous command processing API of libpq.
|
1591
|
+
* See #async_exec for the differences between the two API variants.
|
1592
|
+
* It's not recommended to use explicit sync or async variants but #close_portal instead, unless you have a good reason to do so.
|
1593
|
+
*
|
1594
|
+
* Available since PostgreSQL-17.
|
1595
|
+
*/
|
1596
|
+
static VALUE
|
1597
|
+
pgconn_sync_close_portal(VALUE self, VALUE stmt_name)
|
1598
|
+
{
|
1599
|
+
return pgconn_sync_describe_close_prepared_portal(self, stmt_name, gvl_PQclosePortal);
|
1600
|
+
}
|
1601
|
+
#endif
|
1602
|
+
|
1603
|
+
/*
|
1604
|
+
* call-seq:
|
1605
|
+
* conn.make_empty_pgresult( status ) -> PG::Result
|
1606
|
+
*
|
1607
|
+
* Constructs and empty PG::Result with status _status_.
|
1608
|
+
* _status_ may be one of:
|
1609
|
+
* * +PGRES_EMPTY_QUERY+
|
1610
|
+
* * +PGRES_COMMAND_OK+
|
1611
|
+
* * +PGRES_TUPLES_OK+
|
1612
|
+
* * +PGRES_COPY_OUT+
|
1613
|
+
* * +PGRES_COPY_IN+
|
1614
|
+
* * +PGRES_BAD_RESPONSE+
|
1615
|
+
* * +PGRES_NONFATAL_ERROR+
|
1616
|
+
* * +PGRES_FATAL_ERROR+
|
1617
|
+
* * +PGRES_COPY_BOTH+
|
1618
|
+
* * +PGRES_SINGLE_TUPLE+
|
1619
|
+
* * +PGRES_TUPLES_CHUNK+
|
1620
|
+
* * +PGRES_PIPELINE_SYNC+
|
1621
|
+
* * +PGRES_PIPELINE_ABORTED+
|
1622
|
+
*/
|
1623
|
+
static VALUE
|
1624
|
+
pgconn_make_empty_pgresult(VALUE self, VALUE status)
|
1625
|
+
{
|
1626
|
+
PGresult *result;
|
1627
|
+
VALUE rb_pgresult;
|
1628
|
+
PGconn *conn = pg_get_pgconn(self);
|
1629
|
+
result = PQmakeEmptyPGresult(conn, NUM2INT(status));
|
1630
|
+
rb_pgresult = pg_new_result(result, self);
|
1631
|
+
pg_result_check(rb_pgresult);
|
1632
|
+
return rb_pgresult;
|
1633
|
+
}
|
1634
|
+
|
1635
|
+
|
1636
|
+
/*
|
1637
|
+
* call-seq:
|
1638
|
+
* conn.escape_string( str ) -> String
|
1639
|
+
*
|
1640
|
+
* Returns a SQL-safe version of the String _str_.
|
1641
|
+
* This is the preferred way to make strings safe for inclusion in
|
1642
|
+
* SQL queries.
|
1643
|
+
*
|
1644
|
+
* Consider using exec_params, which avoids the need for passing values
|
1645
|
+
* inside of SQL commands.
|
1646
|
+
*
|
1647
|
+
* Character encoding of escaped string will be equal to client encoding of connection.
|
1648
|
+
*
|
1649
|
+
* NOTE: This class version of this method can only be used safely in client
|
1650
|
+
* programs that use a single PostgreSQL connection at a time (in this case it can
|
1651
|
+
* find out what it needs to know "behind the scenes"). It might give the wrong
|
1652
|
+
* results if used in programs that use multiple database connections; use the
|
1653
|
+
* same method on the connection object in such cases.
|
1654
|
+
*
|
1655
|
+
* See also convenience functions #escape_literal and #escape_identifier which also add proper quotes around the string.
|
1656
|
+
*/
|
1657
|
+
static VALUE
|
1658
|
+
pgconn_s_escape(VALUE self, VALUE string)
|
1659
|
+
{
|
1660
|
+
size_t size;
|
1661
|
+
int error;
|
1662
|
+
VALUE result;
|
1663
|
+
int enc_idx;
|
1664
|
+
int singleton = !rb_obj_is_kind_of(self, rb_cPGconn);
|
1665
|
+
|
1666
|
+
StringValueCStr(string);
|
1667
|
+
enc_idx = singleton ? ENCODING_GET(string) : pg_get_connection(self)->enc_idx;
|
1668
|
+
if( ENCODING_GET(string) != enc_idx ){
|
1669
|
+
string = rb_str_export_to_enc(string, rb_enc_from_index(enc_idx));
|
1670
|
+
}
|
1671
|
+
|
1672
|
+
result = rb_str_new(NULL, RSTRING_LEN(string) * 2 + 1);
|
1673
|
+
PG_ENCODING_SET_NOCHECK(result, enc_idx);
|
1674
|
+
if( !singleton ) {
|
1675
|
+
size = PQescapeStringConn(pg_get_pgconn(self), RSTRING_PTR(result),
|
1676
|
+
RSTRING_PTR(string), RSTRING_LEN(string), &error);
|
1677
|
+
if(error)
|
1678
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(pg_get_pgconn(self)));
|
1679
|
+
|
1680
|
+
} else {
|
1681
|
+
size = PQescapeString(RSTRING_PTR(result), RSTRING_PTR(string), RSTRING_LEN(string));
|
1682
|
+
}
|
1683
|
+
rb_str_set_len(result, size);
|
1684
|
+
|
1685
|
+
return result;
|
1686
|
+
}
|
1687
|
+
|
1688
|
+
/*
|
1689
|
+
* call-seq:
|
1690
|
+
* conn.escape_bytea( string ) -> String
|
1691
|
+
*
|
1692
|
+
* Escapes binary data for use within an SQL command with the type +bytea+.
|
1693
|
+
*
|
1694
|
+
* Certain byte values must be escaped (but all byte values may be escaped)
|
1695
|
+
* when used as part of a +bytea+ literal in an SQL statement. In general, to
|
1696
|
+
* escape a byte, it is converted into the three digit octal number equal to
|
1697
|
+
* the octet value, and preceded by two backslashes. The single quote (') and
|
1698
|
+
* backslash (\) characters have special alternative escape sequences.
|
1699
|
+
* #escape_bytea performs this operation, escaping only the minimally required
|
1700
|
+
* bytes.
|
1701
|
+
*
|
1702
|
+
* Consider using exec_params, which avoids the need for passing values inside of
|
1703
|
+
* SQL commands.
|
1704
|
+
*
|
1705
|
+
* NOTE: This class version of this method can only be used safely in client
|
1706
|
+
* programs that use a single PostgreSQL connection at a time (in this case it can
|
1707
|
+
* find out what it needs to know "behind the scenes"). It might give the wrong
|
1708
|
+
* results if used in programs that use multiple database connections; use the
|
1709
|
+
* same method on the connection object in such cases.
|
1710
|
+
*/
|
1711
|
+
static VALUE
|
1712
|
+
pgconn_s_escape_bytea(VALUE self, VALUE str)
|
1713
|
+
{
|
1714
|
+
unsigned char *from, *to;
|
1715
|
+
size_t from_len, to_len;
|
1716
|
+
VALUE ret;
|
1717
|
+
|
1718
|
+
Check_Type(str, T_STRING);
|
1719
|
+
from = (unsigned char*)RSTRING_PTR(str);
|
1720
|
+
from_len = RSTRING_LEN(str);
|
1721
|
+
|
1722
|
+
if ( rb_obj_is_kind_of(self, rb_cPGconn) ) {
|
1723
|
+
to = PQescapeByteaConn(pg_get_pgconn(self), from, from_len, &to_len);
|
1724
|
+
} else {
|
1725
|
+
to = PQescapeBytea( from, from_len, &to_len);
|
1726
|
+
}
|
1727
|
+
|
1728
|
+
ret = rb_str_new((char*)to, to_len - 1);
|
1729
|
+
PQfreemem(to);
|
1730
|
+
return ret;
|
1731
|
+
}
|
1732
|
+
|
1733
|
+
|
1734
|
+
/*
|
1735
|
+
* call-seq:
|
1736
|
+
* PG::Connection.unescape_bytea( string )
|
1737
|
+
*
|
1738
|
+
* Converts an escaped string representation of binary data into binary data --- the
|
1739
|
+
* reverse of #escape_bytea. This is needed when retrieving +bytea+ data in text format,
|
1740
|
+
* but not when retrieving it in binary format.
|
1741
|
+
*
|
1742
|
+
*/
|
1743
|
+
static VALUE
|
1744
|
+
pgconn_s_unescape_bytea(VALUE self, VALUE str)
|
1745
|
+
{
|
1746
|
+
unsigned char *from, *to;
|
1747
|
+
size_t to_len;
|
1748
|
+
VALUE ret;
|
1749
|
+
|
1750
|
+
UNUSED( self );
|
1751
|
+
|
1752
|
+
Check_Type(str, T_STRING);
|
1753
|
+
from = (unsigned char*)StringValueCStr(str);
|
1754
|
+
|
1755
|
+
to = PQunescapeBytea(from, &to_len);
|
1756
|
+
|
1757
|
+
ret = rb_str_new((char*)to, to_len);
|
1758
|
+
PQfreemem(to);
|
1759
|
+
return ret;
|
1760
|
+
}
|
1761
|
+
|
1762
|
+
/*
|
1763
|
+
* call-seq:
|
1764
|
+
* conn.escape_literal( str ) -> String
|
1765
|
+
*
|
1766
|
+
* Escape an arbitrary String +str+ as a literal.
|
1767
|
+
*
|
1768
|
+
* See also PG::TextEncoder::QuotedLiteral for a type cast integrated version of this function.
|
1769
|
+
*/
|
1770
|
+
static VALUE
|
1771
|
+
pgconn_escape_literal(VALUE self, VALUE string)
|
1772
|
+
{
|
1773
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1774
|
+
char *escaped = NULL;
|
1775
|
+
VALUE result = Qnil;
|
1776
|
+
int enc_idx = this->enc_idx;
|
1777
|
+
|
1778
|
+
StringValueCStr(string);
|
1779
|
+
if( ENCODING_GET(string) != enc_idx ){
|
1780
|
+
string = rb_str_export_to_enc(string, rb_enc_from_index(enc_idx));
|
1781
|
+
}
|
1782
|
+
|
1783
|
+
escaped = PQescapeLiteral(this->pgconn, RSTRING_PTR(string), RSTRING_LEN(string));
|
1784
|
+
if (escaped == NULL)
|
1785
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(this->pgconn));
|
1786
|
+
|
1787
|
+
result = rb_str_new2(escaped);
|
1788
|
+
PQfreemem(escaped);
|
1789
|
+
PG_ENCODING_SET_NOCHECK(result, enc_idx);
|
1790
|
+
|
1791
|
+
return result;
|
1792
|
+
}
|
1793
|
+
|
1794
|
+
/*
|
1795
|
+
* call-seq:
|
1796
|
+
* conn.escape_identifier( str ) -> String
|
1797
|
+
*
|
1798
|
+
* Escape an arbitrary String +str+ as an identifier.
|
1799
|
+
*
|
1800
|
+
* This method does the same as #quote_ident with a String argument,
|
1801
|
+
* but it doesn't support an Array argument and it makes use of libpq
|
1802
|
+
* to process the string.
|
1803
|
+
*/
|
1804
|
+
static VALUE
|
1805
|
+
pgconn_escape_identifier(VALUE self, VALUE string)
|
1806
|
+
{
|
1807
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1808
|
+
char *escaped = NULL;
|
1809
|
+
VALUE result = Qnil;
|
1810
|
+
int enc_idx = this->enc_idx;
|
1811
|
+
|
1812
|
+
StringValueCStr(string);
|
1813
|
+
if( ENCODING_GET(string) != enc_idx ){
|
1814
|
+
string = rb_str_export_to_enc(string, rb_enc_from_index(enc_idx));
|
1815
|
+
}
|
1816
|
+
|
1817
|
+
escaped = PQescapeIdentifier(this->pgconn, RSTRING_PTR(string), RSTRING_LEN(string));
|
1818
|
+
if (escaped == NULL)
|
1819
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(this->pgconn));
|
1820
|
+
|
1821
|
+
result = rb_str_new2(escaped);
|
1822
|
+
PQfreemem(escaped);
|
1823
|
+
PG_ENCODING_SET_NOCHECK(result, enc_idx);
|
1824
|
+
|
1825
|
+
return result;
|
1826
|
+
}
|
1827
|
+
|
1828
|
+
/*
|
1829
|
+
* call-seq:
|
1830
|
+
* conn.set_single_row_mode -> self
|
1831
|
+
*
|
1832
|
+
* To enter single-row mode, call this method immediately after a successful
|
1833
|
+
* call of send_query (or a sibling function). This mode selection is effective
|
1834
|
+
* only for the currently executing query.
|
1835
|
+
* Then call Connection#get_result repeatedly, until it returns nil.
|
1836
|
+
*
|
1837
|
+
* Each (but the last) received Result has exactly one row and a
|
1838
|
+
* Result#result_status of PGRES_SINGLE_TUPLE. The last Result has
|
1839
|
+
* zero rows and is used to indicate a successful execution of the query.
|
1840
|
+
* All of these Result objects will contain the same row description data
|
1841
|
+
* (column names, types, etc) that an ordinary Result object for the query
|
1842
|
+
* would have.
|
1843
|
+
*
|
1844
|
+
* *Caution:* While processing a query, the server may return some rows and then encounter an error, causing the query to be aborted.
|
1845
|
+
* Ordinarily, pg discards any such rows and reports only the error.
|
1846
|
+
* But in single-row or chunked mode, some rows may have already been returned to the application.
|
1847
|
+
* Hence, the application will see some PGRES_SINGLE_TUPLE or PGRES_TUPLES_CHUNK PG::Result objects followed by a PG::Error raised in get_result.
|
1848
|
+
* For proper transactional behavior, the application must be designed to discard or undo whatever has been done with the previously-processed rows, if the query ultimately fails.
|
1849
|
+
*
|
1850
|
+
* Example:
|
1851
|
+
* conn.send_query( "your SQL command" )
|
1852
|
+
* conn.set_single_row_mode
|
1853
|
+
* loop do
|
1854
|
+
* res = conn.get_result or break
|
1855
|
+
* res.check
|
1856
|
+
* res.each do |row|
|
1857
|
+
* # do something with the received row
|
1858
|
+
* end
|
1859
|
+
* end
|
1860
|
+
*/
|
1861
|
+
static VALUE
|
1862
|
+
pgconn_set_single_row_mode(VALUE self)
|
1863
|
+
{
|
1864
|
+
PGconn *conn = pg_get_pgconn(self);
|
1865
|
+
|
1866
|
+
rb_check_frozen(self);
|
1867
|
+
if( PQsetSingleRowMode(conn) == 0 )
|
1868
|
+
pg_raise_conn_error( rb_ePGerror, self, "PQsetSingleRowMode %s", PQerrorMessage(conn));
|
1869
|
+
|
1870
|
+
return self;
|
1871
|
+
}
|
1872
|
+
|
1873
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
1874
|
+
/*
|
1875
|
+
* call-seq:
|
1876
|
+
* conn.set_chunked_rows_mode -> self
|
1877
|
+
*
|
1878
|
+
* Select chunked mode for the currently-executing query.
|
1879
|
+
*
|
1880
|
+
* This function is similar to set_single_row_mode, except that it specifies retrieval of up to +chunk_size+ rows per PGresult, not necessarily just one row.
|
1881
|
+
* This function can only be called immediately after send_query or one of its sibling functions, before any other operation on the connection such as consume_input or get_result.
|
1882
|
+
* If called at the correct time, the function activates chunked mode for the current query.
|
1883
|
+
* Otherwise the mode stays unchanged and the function raises an error.
|
1884
|
+
* In any case, the mode reverts to normal after completion of the current query.
|
1885
|
+
*
|
1886
|
+
* Example:
|
1887
|
+
* conn.send_query( "your SQL command" )
|
1888
|
+
* conn.set_chunked_rows_mode(10)
|
1889
|
+
* loop do
|
1890
|
+
* res = conn.get_result or break
|
1891
|
+
* res.check
|
1892
|
+
* res.each do |row|
|
1893
|
+
* # do something with the received max. 10 rows
|
1894
|
+
* end
|
1895
|
+
* end
|
1896
|
+
*
|
1897
|
+
* Available since PostgreSQL-17
|
1898
|
+
*/
|
1899
|
+
static VALUE
|
1900
|
+
pgconn_set_chunked_rows_mode(VALUE self, VALUE chunk_size)
|
1901
|
+
{
|
1902
|
+
PGconn *conn = pg_get_pgconn(self);
|
1903
|
+
|
1904
|
+
rb_check_frozen(self);
|
1905
|
+
if( PQsetChunkedRowsMode(conn, NUM2INT(chunk_size)) == 0 )
|
1906
|
+
pg_raise_conn_error( rb_ePGerror, self, "PQsetChunkedRowsMode %s", PQerrorMessage(conn));
|
1907
|
+
|
1908
|
+
return self;
|
1909
|
+
}
|
1910
|
+
#endif
|
1911
|
+
|
1912
|
+
static VALUE pgconn_send_query_params(int argc, VALUE *argv, VALUE self);
|
1913
|
+
|
1914
|
+
/*
|
1915
|
+
* call-seq:
|
1916
|
+
* conn.send_query(sql) -> nil
|
1917
|
+
*
|
1918
|
+
* Sends SQL query request specified by _sql_ to PostgreSQL for
|
1919
|
+
* asynchronous processing, and immediately returns.
|
1920
|
+
* On failure, it raises a PG::Error.
|
1921
|
+
*
|
1922
|
+
* For backward compatibility, if you pass more than one parameter to this method,
|
1923
|
+
* it will call #send_query_params for you. New code should explicitly use #send_query_params if
|
1924
|
+
* argument placeholders are used.
|
1925
|
+
*
|
1926
|
+
*/
|
1927
|
+
static VALUE
|
1928
|
+
pgconn_send_query(int argc, VALUE *argv, VALUE self)
|
1929
|
+
{
|
1930
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1931
|
+
|
1932
|
+
/* If called with no or nil parameters, use PQexec for compatibility */
|
1933
|
+
if ( argc == 1 || (argc >= 2 && argc <= 4 && NIL_P(argv[1]) )) {
|
1934
|
+
if(gvl_PQsendQuery(this->pgconn, pg_cstr_enc(argv[0], this->enc_idx)) == 0)
|
1935
|
+
pg_raise_conn_error( rb_eUnableToSend, self, "PQsendQuery %s", PQerrorMessage(this->pgconn));
|
1936
|
+
|
1937
|
+
pgconn_wait_for_flush( self );
|
1938
|
+
return Qnil;
|
1939
|
+
}
|
1940
|
+
|
1941
|
+
pg_deprecated(2, ("forwarding async_exec to async_exec_params and send_query to send_query_params is deprecated"));
|
1942
|
+
|
1943
|
+
/* If called with parameters, and optionally result_format,
|
1944
|
+
* use PQsendQueryParams
|
1945
|
+
*/
|
1946
|
+
return pgconn_send_query_params( argc, argv, self);
|
1947
|
+
}
|
1948
|
+
|
1949
|
+
/*
|
1950
|
+
* call-seq:
|
1951
|
+
* conn.send_query_params(sql, params [, result_format [, type_map ]] ) -> nil
|
1952
|
+
*
|
1953
|
+
* Sends SQL query request specified by _sql_ to PostgreSQL for
|
1954
|
+
* asynchronous processing, and immediately returns.
|
1955
|
+
* On failure, it raises a PG::Error.
|
1956
|
+
*
|
1957
|
+
* +params+ is an array of the bind parameters for the SQL query.
|
1958
|
+
* Each element of the +params+ array may be either:
|
1959
|
+
* a hash of the form:
|
1960
|
+
* {:value => String (value of bind parameter)
|
1961
|
+
* :type => Integer (oid of type of bind parameter)
|
1962
|
+
* :format => Integer (0 for text, 1 for binary)
|
1963
|
+
* }
|
1964
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
1965
|
+
* { :value => <string value>, :type => 0, :format => 0 }
|
1966
|
+
*
|
1967
|
+
* PostgreSQL bind parameters are represented as $1, $2, $3, etc.,
|
1968
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
1969
|
+
* to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
|
1970
|
+
*
|
1971
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
1972
|
+
* Instead of specifying type oids, it's recommended to simply add
|
1973
|
+
* explicit casts in the query to ensure that the right type is used.
|
1974
|
+
*
|
1975
|
+
* For example: "SELECT $1::int"
|
1976
|
+
*
|
1977
|
+
* The optional +result_format+ should be 0 for text results, 1
|
1978
|
+
* for binary.
|
1979
|
+
*
|
1980
|
+
* +type_map+ can be a PG::TypeMap derivation (such as PG::BasicTypeMapForQueries).
|
1981
|
+
* This will type cast the params from various Ruby types before transmission
|
1982
|
+
* based on the encoders defined by the type map. When a type encoder is used
|
1983
|
+
* the format and oid of a given bind parameter are retrieved from the encoder
|
1984
|
+
* instead out of the hash form described above.
|
1985
|
+
*
|
1986
|
+
*/
|
1987
|
+
static VALUE
|
1988
|
+
pgconn_send_query_params(int argc, VALUE *argv, VALUE self)
|
1989
|
+
{
|
1990
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
1991
|
+
int result;
|
1992
|
+
VALUE command, in_res_fmt;
|
1993
|
+
int nParams;
|
1994
|
+
int resultFormat;
|
1995
|
+
struct query_params_data paramsData = { this->enc_idx };
|
1996
|
+
|
1997
|
+
rb_scan_args(argc, argv, "22", &command, ¶msData.params, &in_res_fmt, ¶msData.typemap);
|
1998
|
+
paramsData.with_types = 1;
|
1999
|
+
|
2000
|
+
pgconn_query_assign_typemap( self, ¶msData );
|
2001
|
+
resultFormat = NIL_P(in_res_fmt) ? 0 : NUM2INT(in_res_fmt);
|
2002
|
+
nParams = alloc_query_params( ¶msData );
|
2003
|
+
|
2004
|
+
result = gvl_PQsendQueryParams(this->pgconn, pg_cstr_enc(command, paramsData.enc_idx), nParams, paramsData.types,
|
2005
|
+
(const char * const *)paramsData.values, paramsData.lengths, paramsData.formats, resultFormat);
|
2006
|
+
|
2007
|
+
free_query_params( ¶msData );
|
2008
|
+
|
2009
|
+
if(result == 0)
|
2010
|
+
pg_raise_conn_error( rb_eUnableToSend, self, "PQsendQueryParams %s", PQerrorMessage(this->pgconn));
|
2011
|
+
|
2012
|
+
pgconn_wait_for_flush( self );
|
2013
|
+
return Qnil;
|
2014
|
+
}
|
2015
|
+
|
2016
|
+
/*
|
2017
|
+
* call-seq:
|
2018
|
+
* conn.send_prepare( stmt_name, sql [, param_types ] ) -> nil
|
2019
|
+
*
|
2020
|
+
* Prepares statement _sql_ with name _name_ to be executed later.
|
2021
|
+
* Sends prepare command asynchronously, and returns immediately.
|
2022
|
+
* On failure, it raises a PG::Error.
|
2023
|
+
*
|
2024
|
+
* +param_types+ is an optional parameter to specify the Oids of the
|
2025
|
+
* types of the parameters.
|
2026
|
+
*
|
2027
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
2028
|
+
* Instead of specifying type oids, it's recommended to simply add
|
2029
|
+
* explicit casts in the query to ensure that the right type is used.
|
2030
|
+
*
|
2031
|
+
* For example: "SELECT $1::int"
|
2032
|
+
*
|
2033
|
+
* PostgreSQL bind parameters are represented as $1, $2, $3, etc.,
|
2034
|
+
* inside the SQL query.
|
2035
|
+
*/
|
2036
|
+
static VALUE
|
2037
|
+
pgconn_send_prepare(int argc, VALUE *argv, VALUE self)
|
2038
|
+
{
|
2039
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2040
|
+
int result;
|
2041
|
+
VALUE name, command, in_paramtypes;
|
2042
|
+
VALUE param;
|
2043
|
+
int i = 0;
|
2044
|
+
int nParams = 0;
|
2045
|
+
Oid *paramTypes = NULL;
|
2046
|
+
const char *name_cstr;
|
2047
|
+
const char *command_cstr;
|
2048
|
+
int enc_idx = this->enc_idx;
|
2049
|
+
|
2050
|
+
rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
|
2051
|
+
name_cstr = pg_cstr_enc(name, enc_idx);
|
2052
|
+
command_cstr = pg_cstr_enc(command, enc_idx);
|
2053
|
+
|
2054
|
+
if(! NIL_P(in_paramtypes)) {
|
2055
|
+
Check_Type(in_paramtypes, T_ARRAY);
|
2056
|
+
nParams = (int)RARRAY_LEN(in_paramtypes);
|
2057
|
+
paramTypes = ALLOC_N(Oid, nParams);
|
2058
|
+
for(i = 0; i < nParams; i++) {
|
2059
|
+
param = rb_ary_entry(in_paramtypes, i);
|
2060
|
+
if(param == Qnil)
|
2061
|
+
paramTypes[i] = 0;
|
2062
|
+
else
|
2063
|
+
paramTypes[i] = NUM2UINT(param);
|
2064
|
+
}
|
2065
|
+
}
|
2066
|
+
result = gvl_PQsendPrepare(this->pgconn, name_cstr, command_cstr, nParams, paramTypes);
|
2067
|
+
|
2068
|
+
xfree(paramTypes);
|
2069
|
+
|
2070
|
+
if(result == 0) {
|
2071
|
+
pg_raise_conn_error( rb_eUnableToSend, self, "PQsendPrepare %s", PQerrorMessage(this->pgconn));
|
2072
|
+
}
|
2073
|
+
pgconn_wait_for_flush( self );
|
2074
|
+
return Qnil;
|
2075
|
+
}
|
2076
|
+
|
2077
|
+
/*
|
2078
|
+
* call-seq:
|
2079
|
+
* conn.send_query_prepared( statement_name [, params, result_format[, type_map ]] )
|
2080
|
+
* -> nil
|
2081
|
+
*
|
2082
|
+
* Execute prepared named statement specified by _statement_name_
|
2083
|
+
* asynchronously, and returns immediately.
|
2084
|
+
* On failure, it raises a PG::Error.
|
2085
|
+
*
|
2086
|
+
* +params+ is an array of the optional bind parameters for the
|
2087
|
+
* SQL query. Each element of the +params+ array may be either:
|
2088
|
+
* a hash of the form:
|
2089
|
+
* {:value => String (value of bind parameter)
|
2090
|
+
* :format => Integer (0 for text, 1 for binary)
|
2091
|
+
* }
|
2092
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
2093
|
+
* { :value => <string value>, :format => 0 }
|
2094
|
+
*
|
2095
|
+
* PostgreSQL bind parameters are represented as $1, $2, $3, etc.,
|
2096
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
2097
|
+
* to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
|
2098
|
+
*
|
2099
|
+
* The optional +result_format+ should be 0 for text results, 1
|
2100
|
+
* for binary.
|
2101
|
+
*
|
2102
|
+
* +type_map+ can be a PG::TypeMap derivation (such as PG::BasicTypeMapForQueries).
|
2103
|
+
* This will type cast the params from various Ruby types before transmission
|
2104
|
+
* based on the encoders defined by the type map. When a type encoder is used
|
2105
|
+
* the format and oid of a given bind parameter are retrieved from the encoder
|
2106
|
+
* instead out of the hash form described above.
|
2107
|
+
*
|
2108
|
+
*/
|
2109
|
+
static VALUE
|
2110
|
+
pgconn_send_query_prepared(int argc, VALUE *argv, VALUE self)
|
2111
|
+
{
|
2112
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2113
|
+
int result;
|
2114
|
+
VALUE name, in_res_fmt;
|
2115
|
+
int nParams;
|
2116
|
+
int resultFormat;
|
2117
|
+
struct query_params_data paramsData = { this->enc_idx };
|
2118
|
+
|
2119
|
+
rb_scan_args(argc, argv, "13", &name, ¶msData.params, &in_res_fmt, ¶msData.typemap);
|
2120
|
+
paramsData.with_types = 0;
|
2121
|
+
|
2122
|
+
if(NIL_P(paramsData.params)) {
|
2123
|
+
paramsData.params = rb_ary_new2(0);
|
2124
|
+
}
|
2125
|
+
pgconn_query_assign_typemap( self, ¶msData );
|
2126
|
+
|
2127
|
+
resultFormat = NIL_P(in_res_fmt) ? 0 : NUM2INT(in_res_fmt);
|
2128
|
+
nParams = alloc_query_params( ¶msData );
|
2129
|
+
|
2130
|
+
result = gvl_PQsendQueryPrepared(this->pgconn, pg_cstr_enc(name, paramsData.enc_idx), nParams,
|
2131
|
+
(const char * const *)paramsData.values, paramsData.lengths, paramsData.formats,
|
2132
|
+
resultFormat);
|
2133
|
+
|
2134
|
+
free_query_params( ¶msData );
|
2135
|
+
|
2136
|
+
if(result == 0)
|
2137
|
+
pg_raise_conn_error( rb_eUnableToSend, self, "PQsendQueryPrepared %s", PQerrorMessage(this->pgconn));
|
2138
|
+
|
2139
|
+
pgconn_wait_for_flush( self );
|
2140
|
+
return Qnil;
|
2141
|
+
}
|
2142
|
+
|
2143
|
+
|
2144
|
+
static VALUE
|
2145
|
+
pgconn_send_describe_close_prepared_portal(VALUE self, VALUE name, int (*func)(PGconn *, const char *), const char *funame)
|
2146
|
+
{
|
2147
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2148
|
+
const char *stmt = NIL_P(name) ? NULL : pg_cstr_enc(name, this->enc_idx);
|
2149
|
+
/* returns 0 on failure */
|
2150
|
+
if(func(this->pgconn, stmt) == 0)
|
2151
|
+
pg_raise_conn_error( rb_eUnableToSend, self, "%s %s", funame, PQerrorMessage(this->pgconn));
|
2152
|
+
|
2153
|
+
pgconn_wait_for_flush( self );
|
2154
|
+
return Qnil;
|
2155
|
+
}
|
2156
|
+
|
2157
|
+
/*
|
2158
|
+
* call-seq:
|
2159
|
+
* conn.send_describe_prepared( statement_name ) -> nil
|
2160
|
+
*
|
2161
|
+
* Asynchronously send _command_ to the server. Does not block.
|
2162
|
+
* Use in combination with +conn.get_result+.
|
2163
|
+
*/
|
2164
|
+
static VALUE
|
2165
|
+
pgconn_send_describe_prepared(VALUE self, VALUE stmt_name)
|
2166
|
+
{
|
2167
|
+
return pgconn_send_describe_close_prepared_portal(
|
2168
|
+
self, stmt_name, gvl_PQsendDescribePrepared,
|
2169
|
+
"PQsendDescribePrepared");
|
2170
|
+
}
|
2171
|
+
|
2172
|
+
|
2173
|
+
/*
|
2174
|
+
* call-seq:
|
2175
|
+
* conn.send_describe_portal( portal_name ) -> nil
|
2176
|
+
*
|
2177
|
+
* Asynchronously send _command_ to the server. Does not block.
|
2178
|
+
* Use in combination with +conn.get_result+.
|
2179
|
+
*/
|
2180
|
+
static VALUE
|
2181
|
+
pgconn_send_describe_portal(VALUE self, VALUE portal)
|
2182
|
+
{
|
2183
|
+
return pgconn_send_describe_close_prepared_portal(
|
2184
|
+
self, portal, gvl_PQsendDescribePortal,
|
2185
|
+
"PQsendDescribePortal");
|
2186
|
+
}
|
2187
|
+
|
2188
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
2189
|
+
/*
|
2190
|
+
* call-seq:
|
2191
|
+
* conn.send_close_prepared( statement_name ) -> nil
|
2192
|
+
*
|
2193
|
+
* Asynchronously send _command_ to the server. Does not block.
|
2194
|
+
* Use in combination with +conn.get_result+.
|
2195
|
+
*
|
2196
|
+
* Available since PostgreSQL-17.
|
2197
|
+
*/
|
2198
|
+
static VALUE
|
2199
|
+
pgconn_send_close_prepared(VALUE self, VALUE stmt_name)
|
2200
|
+
{
|
2201
|
+
return pgconn_send_describe_close_prepared_portal(
|
2202
|
+
self, stmt_name, gvl_PQsendClosePrepared,
|
2203
|
+
"PQsendClosePrepared");
|
2204
|
+
}
|
2205
|
+
|
2206
|
+
|
2207
|
+
/*
|
2208
|
+
* call-seq:
|
2209
|
+
* conn.send_close_portal( portal_name ) -> nil
|
2210
|
+
*
|
2211
|
+
* Asynchronously send _command_ to the server. Does not block.
|
2212
|
+
* Use in combination with +conn.get_result+.
|
2213
|
+
*
|
2214
|
+
* Available since PostgreSQL-17.
|
2215
|
+
*/
|
2216
|
+
static VALUE
|
2217
|
+
pgconn_send_close_portal(VALUE self, VALUE portal)
|
2218
|
+
{
|
2219
|
+
return pgconn_send_describe_close_prepared_portal(
|
2220
|
+
self, portal, gvl_PQsendClosePortal,
|
2221
|
+
"PQsendClosePortal");
|
2222
|
+
}
|
2223
|
+
#endif
|
2224
|
+
|
2225
|
+
static VALUE
|
2226
|
+
pgconn_sync_get_result(VALUE self)
|
2227
|
+
{
|
2228
|
+
PGconn *conn = pg_get_pgconn(self);
|
2229
|
+
PGresult *result;
|
2230
|
+
VALUE rb_pgresult;
|
2231
|
+
|
2232
|
+
result = gvl_PQgetResult(conn);
|
2233
|
+
if(result == NULL)
|
2234
|
+
return Qnil;
|
2235
|
+
rb_pgresult = pg_new_result(result, self);
|
2236
|
+
if (rb_block_given_p()) {
|
2237
|
+
return rb_ensure(rb_yield, rb_pgresult,
|
2238
|
+
pg_result_clear, rb_pgresult);
|
2239
|
+
}
|
2240
|
+
return rb_pgresult;
|
2241
|
+
}
|
2242
|
+
|
2243
|
+
/*
|
2244
|
+
* call-seq:
|
2245
|
+
* conn.consume_input()
|
2246
|
+
*
|
2247
|
+
* If input is available from the server, consume it.
|
2248
|
+
* After calling +consume_input+, you can check +is_busy+
|
2249
|
+
* or *notifies* to see if the state has changed.
|
2250
|
+
*/
|
2251
|
+
static VALUE
|
2252
|
+
pgconn_consume_input(VALUE self)
|
2253
|
+
{
|
2254
|
+
PGconn *conn = pg_get_pgconn(self);
|
2255
|
+
/* returns 0 on error */
|
2256
|
+
if(PQconsumeInput(conn) == 0) {
|
2257
|
+
pgconn_close_socket_io(self);
|
2258
|
+
pg_raise_conn_error( rb_eConnectionBad, self, "%s", PQerrorMessage(conn));
|
2259
|
+
}
|
2260
|
+
|
2261
|
+
return Qnil;
|
2262
|
+
}
|
2263
|
+
|
2264
|
+
/*
|
2265
|
+
* call-seq:
|
2266
|
+
* conn.is_busy() -> Boolean
|
2267
|
+
*
|
2268
|
+
* Returns +true+ if a command is busy, that is, if
|
2269
|
+
* #get_result would block. Otherwise returns +false+.
|
2270
|
+
*/
|
2271
|
+
static VALUE
|
2272
|
+
pgconn_is_busy(VALUE self)
|
2273
|
+
{
|
2274
|
+
return gvl_PQisBusy(pg_get_pgconn(self)) ? Qtrue : Qfalse;
|
2275
|
+
}
|
2276
|
+
|
2277
|
+
static VALUE
|
2278
|
+
pgconn_sync_setnonblocking(VALUE self, VALUE state)
|
2279
|
+
{
|
2280
|
+
int arg;
|
2281
|
+
PGconn *conn = pg_get_pgconn(self);
|
2282
|
+
rb_check_frozen(self);
|
2283
|
+
if(state == Qtrue)
|
2284
|
+
arg = 1;
|
2285
|
+
else if (state == Qfalse)
|
2286
|
+
arg = 0;
|
2287
|
+
else
|
2288
|
+
rb_raise(rb_eArgError, "Boolean value expected");
|
2289
|
+
|
2290
|
+
if(PQsetnonblocking(conn, arg) == -1)
|
2291
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
2292
|
+
|
2293
|
+
return Qnil;
|
2294
|
+
}
|
2295
|
+
|
2296
|
+
|
2297
|
+
static VALUE
|
2298
|
+
pgconn_sync_isnonblocking(VALUE self)
|
2299
|
+
{
|
2300
|
+
return PQisnonblocking(pg_get_pgconn(self)) ? Qtrue : Qfalse;
|
2301
|
+
}
|
2302
|
+
|
2303
|
+
static VALUE
|
2304
|
+
pgconn_sync_flush(VALUE self)
|
2305
|
+
{
|
2306
|
+
PGconn *conn = pg_get_pgconn(self);
|
2307
|
+
int ret = PQflush(conn);
|
2308
|
+
if(ret == -1)
|
2309
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
2310
|
+
|
2311
|
+
return (ret) ? Qfalse : Qtrue;
|
2312
|
+
}
|
2313
|
+
|
2314
|
+
#ifndef HAVE_PQSETCHUNKEDROWSMODE
|
2315
|
+
static VALUE
|
2316
|
+
pgconn_sync_cancel(VALUE self)
|
2317
|
+
{
|
2318
|
+
char errbuf[256];
|
2319
|
+
PGcancel *cancel;
|
2320
|
+
VALUE retval;
|
2321
|
+
int ret;
|
2322
|
+
|
2323
|
+
cancel = PQgetCancel(pg_get_pgconn(self));
|
2324
|
+
if(cancel == NULL)
|
2325
|
+
pg_raise_conn_error( rb_ePGerror, self, "Invalid connection!");
|
2326
|
+
|
2327
|
+
ret = gvl_PQcancel(cancel, errbuf, sizeof(errbuf));
|
2328
|
+
if(ret == 1)
|
2329
|
+
retval = Qnil;
|
2330
|
+
else
|
2331
|
+
retval = rb_str_new2(errbuf);
|
2332
|
+
|
2333
|
+
PQfreeCancel(cancel);
|
2334
|
+
return retval;
|
2335
|
+
}
|
2336
|
+
#endif
|
2337
|
+
|
2338
|
+
|
2339
|
+
/*
|
2340
|
+
* call-seq:
|
2341
|
+
* conn.notifies()
|
2342
|
+
*
|
2343
|
+
* Returns a hash of the unprocessed notifications.
|
2344
|
+
* If there is no unprocessed notifier, it returns +nil+.
|
2345
|
+
*/
|
2346
|
+
static VALUE
|
2347
|
+
pgconn_notifies(VALUE self)
|
2348
|
+
{
|
2349
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2350
|
+
PGnotify *notification;
|
2351
|
+
VALUE hash;
|
2352
|
+
VALUE sym_relname, sym_be_pid, sym_extra;
|
2353
|
+
VALUE relname, be_pid, extra;
|
2354
|
+
|
2355
|
+
sym_relname = ID2SYM(rb_intern("relname"));
|
2356
|
+
sym_be_pid = ID2SYM(rb_intern("be_pid"));
|
2357
|
+
sym_extra = ID2SYM(rb_intern("extra"));
|
2358
|
+
|
2359
|
+
notification = gvl_PQnotifies(this->pgconn);
|
2360
|
+
if (notification == NULL) {
|
2361
|
+
return Qnil;
|
2362
|
+
}
|
2363
|
+
|
2364
|
+
hash = rb_hash_new();
|
2365
|
+
relname = rb_str_new2(notification->relname);
|
2366
|
+
be_pid = INT2NUM(notification->be_pid);
|
2367
|
+
extra = rb_str_new2(notification->extra);
|
2368
|
+
PG_ENCODING_SET_NOCHECK( relname, this->enc_idx );
|
2369
|
+
PG_ENCODING_SET_NOCHECK( extra, this->enc_idx );
|
2370
|
+
|
2371
|
+
rb_hash_aset(hash, sym_relname, relname);
|
2372
|
+
rb_hash_aset(hash, sym_be_pid, be_pid);
|
2373
|
+
rb_hash_aset(hash, sym_extra, extra);
|
2374
|
+
|
2375
|
+
PQfreemem(notification);
|
2376
|
+
return hash;
|
2377
|
+
}
|
2378
|
+
|
2379
|
+
#ifndef HAVE_RB_IO_DESCRIPTOR
|
2380
|
+
static int
|
2381
|
+
rb_io_descriptor(VALUE io)
|
2382
|
+
{
|
2383
|
+
rb_io_t *fptr;
|
2384
|
+
Check_Type(io, T_FILE);
|
2385
|
+
fptr = RFILE(io)->fptr;
|
2386
|
+
rb_io_check_closed(fptr);
|
2387
|
+
return fptr->fd;
|
2388
|
+
}
|
2389
|
+
#endif
|
2390
|
+
|
2391
|
+
#if defined(_WIN32)
|
2392
|
+
|
2393
|
+
/* We use a specialized implementation of rb_io_wait() on Windows.
|
2394
|
+
* This is because rb_io_wait() and rb_wait_for_single_fd() are very slow on Windows.
|
2395
|
+
*/
|
2396
|
+
|
2397
|
+
#if defined(HAVE_RUBY_FIBER_SCHEDULER_H)
|
2398
|
+
#include <ruby/fiber/scheduler.h>
|
2399
|
+
#endif
|
2400
|
+
|
2401
|
+
typedef enum {
|
2402
|
+
PG_RUBY_IO_READABLE = RB_WAITFD_IN,
|
2403
|
+
PG_RUBY_IO_WRITABLE = RB_WAITFD_OUT,
|
2404
|
+
PG_RUBY_IO_PRIORITY = RB_WAITFD_PRI,
|
2405
|
+
} pg_rb_io_event_t;
|
2406
|
+
|
2407
|
+
int rb_w32_wait_events( HANDLE *events, int num, DWORD timeout );
|
2408
|
+
|
2409
|
+
static VALUE
|
2410
|
+
pg_rb_thread_io_wait(VALUE io, VALUE events, VALUE timeout) {
|
2411
|
+
struct timeval ptimeout;
|
2412
|
+
|
2413
|
+
struct timeval aborttime={0,0}, currtime, waittime;
|
2414
|
+
DWORD timeout_milisec = INFINITE;
|
2415
|
+
HANDLE hEvent = WSACreateEvent();
|
2416
|
+
|
2417
|
+
long rb_events = NUM2UINT(events);
|
2418
|
+
long w32_events = 0;
|
2419
|
+
DWORD wait_ret;
|
2420
|
+
|
2421
|
+
if( !NIL_P(timeout) ){
|
2422
|
+
ptimeout.tv_sec = (time_t)(NUM2DBL(timeout));
|
2423
|
+
ptimeout.tv_usec = (time_t)((NUM2DBL(timeout) - (double)ptimeout.tv_sec) * 1e6);
|
2424
|
+
|
2425
|
+
gettimeofday(&currtime, NULL);
|
2426
|
+
timeradd(&currtime, &ptimeout, &aborttime);
|
2427
|
+
}
|
2428
|
+
|
2429
|
+
if(rb_events & PG_RUBY_IO_READABLE) w32_events |= FD_READ | FD_ACCEPT | FD_CLOSE;
|
2430
|
+
if(rb_events & PG_RUBY_IO_WRITABLE) w32_events |= FD_WRITE | FD_CONNECT;
|
2431
|
+
if(rb_events & PG_RUBY_IO_PRIORITY) w32_events |= FD_OOB;
|
2432
|
+
|
2433
|
+
for(;;) {
|
2434
|
+
if ( WSAEventSelect(_get_osfhandle(rb_io_descriptor(io)), hEvent, w32_events) == SOCKET_ERROR ) {
|
2435
|
+
WSACloseEvent( hEvent );
|
2436
|
+
rb_raise( rb_eConnectionBad, "WSAEventSelect socket error: %d", WSAGetLastError() );
|
2437
|
+
}
|
2438
|
+
|
2439
|
+
if ( !NIL_P(timeout) ) {
|
2440
|
+
gettimeofday(&currtime, NULL);
|
2441
|
+
timersub(&aborttime, &currtime, &waittime);
|
2442
|
+
timeout_milisec = (DWORD)( waittime.tv_sec * 1e3 + waittime.tv_usec / 1e3 );
|
2443
|
+
}
|
2444
|
+
|
2445
|
+
if( NIL_P(timeout) || (waittime.tv_sec >= 0 && waittime.tv_usec >= 0) ){
|
2446
|
+
/* Wait for the socket to become readable before checking again */
|
2447
|
+
wait_ret = rb_w32_wait_events( &hEvent, 1, timeout_milisec );
|
2448
|
+
} else {
|
2449
|
+
wait_ret = WAIT_TIMEOUT;
|
2450
|
+
}
|
2451
|
+
|
2452
|
+
if ( wait_ret == WAIT_TIMEOUT ) {
|
2453
|
+
WSACloseEvent( hEvent );
|
2454
|
+
return UINT2NUM(0);
|
2455
|
+
} else if ( wait_ret == WAIT_OBJECT_0 ) {
|
2456
|
+
WSACloseEvent( hEvent );
|
2457
|
+
/* The event we were waiting for. */
|
2458
|
+
return UINT2NUM(rb_events);
|
2459
|
+
} else if ( wait_ret == WAIT_OBJECT_0 + 1) {
|
2460
|
+
/* This indicates interruption from timer thread, GC, exception
|
2461
|
+
* from other threads etc... */
|
2462
|
+
rb_thread_check_ints();
|
2463
|
+
} else if ( wait_ret == WAIT_FAILED ) {
|
2464
|
+
WSACloseEvent( hEvent );
|
2465
|
+
rb_raise( rb_eConnectionBad, "Wait on socket error (WaitForMultipleObjects): %lu", GetLastError() );
|
2466
|
+
} else {
|
2467
|
+
WSACloseEvent( hEvent );
|
2468
|
+
rb_raise( rb_eConnectionBad, "Wait on socket abandoned (WaitForMultipleObjects)" );
|
2469
|
+
}
|
2470
|
+
}
|
2471
|
+
}
|
2472
|
+
|
2473
|
+
static VALUE
|
2474
|
+
pg_rb_io_wait(VALUE io, VALUE events, VALUE timeout) {
|
2475
|
+
#if defined(HAVE_RUBY_FIBER_SCHEDULER_H)
|
2476
|
+
/* We don't support Fiber.scheduler on Windows ruby-3.0 because there is no fast way to check whether a scheduler is active.
|
2477
|
+
* Fortunately ruby-3.1 offers a C-API for it.
|
2478
|
+
*/
|
2479
|
+
VALUE scheduler = rb_fiber_scheduler_current();
|
2480
|
+
|
2481
|
+
if (!NIL_P(scheduler)) {
|
2482
|
+
return rb_io_wait(io, events, timeout);
|
2483
|
+
}
|
2484
|
+
#endif
|
2485
|
+
return pg_rb_thread_io_wait(io, events, timeout);
|
2486
|
+
}
|
2487
|
+
|
2488
|
+
#elif defined(HAVE_RB_IO_WAIT)
|
2489
|
+
|
2490
|
+
/* Use our own function and constants names, to avoid conflicts with truffleruby-head on its road to ruby-3.0 compatibility. */
|
2491
|
+
#define pg_rb_io_wait rb_io_wait
|
2492
|
+
#define PG_RUBY_IO_READABLE RUBY_IO_READABLE
|
2493
|
+
#define PG_RUBY_IO_WRITABLE RUBY_IO_WRITABLE
|
2494
|
+
#define PG_RUBY_IO_PRIORITY RUBY_IO_PRIORITY
|
2495
|
+
|
2496
|
+
#else
|
2497
|
+
/* For compat with ruby < 3.0 */
|
2498
|
+
|
2499
|
+
typedef enum {
|
2500
|
+
PG_RUBY_IO_READABLE = RB_WAITFD_IN,
|
2501
|
+
PG_RUBY_IO_WRITABLE = RB_WAITFD_OUT,
|
2502
|
+
PG_RUBY_IO_PRIORITY = RB_WAITFD_PRI,
|
2503
|
+
} pg_rb_io_event_t;
|
2504
|
+
|
2505
|
+
static VALUE
|
2506
|
+
pg_rb_io_wait(VALUE io, VALUE events, VALUE timeout) {
|
2507
|
+
struct timeval waittime;
|
2508
|
+
int res;
|
2509
|
+
|
2510
|
+
if( !NIL_P(timeout) ){
|
2511
|
+
waittime.tv_sec = (time_t)(NUM2DBL(timeout));
|
2512
|
+
waittime.tv_usec = (time_t)((NUM2DBL(timeout) - (double)waittime.tv_sec) * 1e6);
|
2513
|
+
}
|
2514
|
+
res = rb_wait_for_single_fd(rb_io_descriptor(io), NUM2UINT(events), NIL_P(timeout) ? NULL : &waittime);
|
2515
|
+
|
2516
|
+
return UINT2NUM(res);
|
2517
|
+
}
|
2518
|
+
#endif
|
2519
|
+
|
2520
|
+
static void *
|
2521
|
+
wait_socket_readable( VALUE self, struct timeval *ptimeout, void *(*is_readable)(PGconn *))
|
2522
|
+
{
|
2523
|
+
VALUE ret;
|
2524
|
+
void *retval;
|
2525
|
+
struct timeval aborttime={0,0}, currtime, waittime;
|
2526
|
+
VALUE wait_timeout = Qnil;
|
2527
|
+
PGconn *conn = pg_get_pgconn(self);
|
2528
|
+
|
2529
|
+
if ( ptimeout ) {
|
2530
|
+
gettimeofday(&currtime, NULL);
|
2531
|
+
timeradd(&currtime, ptimeout, &aborttime);
|
2532
|
+
}
|
2533
|
+
|
2534
|
+
while ( !(retval=is_readable(conn)) ) {
|
2535
|
+
if ( ptimeout ) {
|
2536
|
+
gettimeofday(&currtime, NULL);
|
2537
|
+
timersub(&aborttime, &currtime, &waittime);
|
2538
|
+
wait_timeout = DBL2NUM((double)(waittime.tv_sec) + (double)(waittime.tv_usec) / 1000000.0);
|
2539
|
+
}
|
2540
|
+
|
2541
|
+
/* Is the given timeout valid? */
|
2542
|
+
if( !ptimeout || (waittime.tv_sec >= 0 && waittime.tv_usec >= 0) ){
|
2543
|
+
VALUE socket_io;
|
2544
|
+
|
2545
|
+
/* before we wait for data, make sure everything has been sent */
|
2546
|
+
pgconn_async_flush(self);
|
2547
|
+
if ((retval=is_readable(conn)))
|
2548
|
+
return retval;
|
2549
|
+
|
2550
|
+
socket_io = pgconn_socket_io(self);
|
2551
|
+
/* Wait for the socket to become readable before checking again */
|
2552
|
+
ret = pg_rb_io_wait(socket_io, RB_INT2NUM(PG_RUBY_IO_READABLE), wait_timeout);
|
2553
|
+
} else {
|
2554
|
+
ret = Qfalse;
|
2555
|
+
}
|
2556
|
+
|
2557
|
+
/* Return false if the select() timed out */
|
2558
|
+
if ( ret == Qfalse ){
|
2559
|
+
return NULL;
|
2560
|
+
}
|
2561
|
+
|
2562
|
+
/* Check for connection errors (PQisBusy is true on connection errors) */
|
2563
|
+
if ( PQconsumeInput(conn) == 0 ){
|
2564
|
+
pgconn_close_socket_io(self);
|
2565
|
+
pg_raise_conn_error(rb_eConnectionBad, self, "PQconsumeInput() %s", PQerrorMessage(conn));
|
2566
|
+
}
|
2567
|
+
}
|
2568
|
+
|
2569
|
+
return retval;
|
2570
|
+
}
|
2571
|
+
|
2572
|
+
/*
|
2573
|
+
* call-seq:
|
2574
|
+
* conn.flush() -> Boolean
|
2575
|
+
*
|
2576
|
+
* Attempts to flush any queued output data to the server.
|
2577
|
+
* Returns +true+ if data is successfully flushed, +false+
|
2578
|
+
* if not. It can only return +false+ if connection is
|
2579
|
+
* in nonblocking mode.
|
2580
|
+
* Raises PG::Error if some other failure occurred.
|
2581
|
+
*/
|
2582
|
+
static VALUE
|
2583
|
+
pgconn_async_flush(VALUE self)
|
2584
|
+
{
|
2585
|
+
while( pgconn_sync_flush(self) == Qfalse ){
|
2586
|
+
/* wait for the socket to become read- or write-ready */
|
2587
|
+
int events;
|
2588
|
+
VALUE socket_io = pgconn_socket_io(self);
|
2589
|
+
events = RB_NUM2INT(pg_rb_io_wait(socket_io, RB_INT2NUM(PG_RUBY_IO_READABLE | PG_RUBY_IO_WRITABLE), Qnil));
|
2590
|
+
|
2591
|
+
if (events & PG_RUBY_IO_READABLE){
|
2592
|
+
pgconn_consume_input(self);
|
2593
|
+
}
|
2594
|
+
}
|
2595
|
+
return Qtrue;
|
2596
|
+
}
|
2597
|
+
|
2598
|
+
static VALUE
|
2599
|
+
pgconn_wait_for_flush( VALUE self ){
|
2600
|
+
if( !pg_get_connection_safe(self)->flush_data )
|
2601
|
+
return Qnil;
|
2602
|
+
|
2603
|
+
return pgconn_async_flush(self);
|
2604
|
+
}
|
2605
|
+
|
2606
|
+
static VALUE
|
2607
|
+
pgconn_flush_data_set( VALUE self, VALUE enabled ){
|
2608
|
+
t_pg_connection *conn = pg_get_connection(self);
|
2609
|
+
rb_check_frozen(self);
|
2610
|
+
conn->flush_data = RTEST(enabled);
|
2611
|
+
return enabled;
|
2612
|
+
}
|
2613
|
+
|
2614
|
+
static void *
|
2615
|
+
notify_readable(PGconn *conn)
|
2616
|
+
{
|
2617
|
+
return (void*)gvl_PQnotifies(conn);
|
2618
|
+
}
|
2619
|
+
|
2620
|
+
/*
|
2621
|
+
* call-seq:
|
2622
|
+
* conn.wait_for_notify( [ timeout ] ) { |event, pid, payload| block } -> String
|
2623
|
+
*
|
2624
|
+
* Blocks while waiting for notification(s), or until the optional
|
2625
|
+
* _timeout_ is reached, whichever comes first. _timeout_ is
|
2626
|
+
* measured in seconds and can be fractional.
|
2627
|
+
*
|
2628
|
+
* Returns +nil+ if _timeout_ is reached, the name of the NOTIFY event otherwise.
|
2629
|
+
* If used in block form, passes the name of the NOTIFY +event+, the generating
|
2630
|
+
* +pid+ and the optional +payload+ string into the block.
|
2631
|
+
*/
|
2632
|
+
static VALUE
|
2633
|
+
pgconn_wait_for_notify(int argc, VALUE *argv, VALUE self)
|
2634
|
+
{
|
2635
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2636
|
+
PGnotify *pnotification;
|
2637
|
+
struct timeval timeout;
|
2638
|
+
struct timeval *ptimeout = NULL;
|
2639
|
+
VALUE timeout_in = Qnil, relname = Qnil, be_pid = Qnil, extra = Qnil;
|
2640
|
+
double timeout_sec;
|
2641
|
+
|
2642
|
+
rb_scan_args( argc, argv, "01", &timeout_in );
|
2643
|
+
|
2644
|
+
if ( RTEST(timeout_in) ) {
|
2645
|
+
timeout_sec = NUM2DBL( timeout_in );
|
2646
|
+
timeout.tv_sec = (time_t)timeout_sec;
|
2647
|
+
timeout.tv_usec = (suseconds_t)( (timeout_sec - (long)timeout_sec) * 1e6 );
|
2648
|
+
ptimeout = &timeout;
|
2649
|
+
}
|
2650
|
+
|
2651
|
+
pnotification = (PGnotify*) wait_socket_readable( self, ptimeout, notify_readable);
|
2652
|
+
|
2653
|
+
/* Return nil if the select timed out */
|
2654
|
+
if ( !pnotification ) return Qnil;
|
2655
|
+
|
2656
|
+
relname = rb_str_new2( pnotification->relname );
|
2657
|
+
PG_ENCODING_SET_NOCHECK( relname, this->enc_idx );
|
2658
|
+
be_pid = INT2NUM( pnotification->be_pid );
|
2659
|
+
if ( *pnotification->extra ) {
|
2660
|
+
extra = rb_str_new2( pnotification->extra );
|
2661
|
+
PG_ENCODING_SET_NOCHECK( extra, this->enc_idx );
|
2662
|
+
}
|
2663
|
+
PQfreemem( pnotification );
|
2664
|
+
|
2665
|
+
if ( rb_block_given_p() )
|
2666
|
+
rb_yield_values( 3, relname, be_pid, extra );
|
2667
|
+
|
2668
|
+
return relname;
|
2669
|
+
}
|
2670
|
+
|
2671
|
+
|
2672
|
+
static VALUE
|
2673
|
+
pgconn_sync_put_copy_data(int argc, VALUE *argv, VALUE self)
|
2674
|
+
{
|
2675
|
+
int ret;
|
2676
|
+
int len;
|
2677
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2678
|
+
VALUE value;
|
2679
|
+
VALUE buffer = Qnil;
|
2680
|
+
VALUE encoder;
|
2681
|
+
VALUE intermediate = Qnil;
|
2682
|
+
t_pg_coder *p_coder = NULL;
|
2683
|
+
|
2684
|
+
rb_scan_args( argc, argv, "11", &value, &encoder );
|
2685
|
+
|
2686
|
+
if( NIL_P(encoder) ){
|
2687
|
+
if( NIL_P(this->encoder_for_put_copy_data) ){
|
2688
|
+
buffer = value;
|
2689
|
+
} else {
|
2690
|
+
p_coder = RTYPEDDATA_DATA( this->encoder_for_put_copy_data );
|
2691
|
+
}
|
2692
|
+
} else {
|
2693
|
+
/* Check argument type and use argument encoder */
|
2694
|
+
TypedData_Get_Struct(encoder, t_pg_coder, &pg_coder_type, p_coder);
|
2695
|
+
}
|
2696
|
+
|
2697
|
+
if( p_coder ){
|
2698
|
+
t_pg_coder_enc_func enc_func;
|
2699
|
+
int enc_idx = this->enc_idx;
|
2700
|
+
|
2701
|
+
enc_func = pg_coder_enc_func( p_coder );
|
2702
|
+
len = enc_func( p_coder, value, NULL, &intermediate, enc_idx);
|
2703
|
+
|
2704
|
+
if( len == -1 ){
|
2705
|
+
/* The intermediate value is a String that can be used directly. */
|
2706
|
+
buffer = intermediate;
|
2707
|
+
} else {
|
2708
|
+
buffer = rb_str_new(NULL, len);
|
2709
|
+
len = enc_func( p_coder, value, RSTRING_PTR(buffer), &intermediate, enc_idx);
|
2710
|
+
rb_str_set_len( buffer, len );
|
2711
|
+
}
|
2712
|
+
}
|
2713
|
+
|
2714
|
+
Check_Type(buffer, T_STRING);
|
2715
|
+
|
2716
|
+
ret = gvl_PQputCopyData(this->pgconn, RSTRING_PTR(buffer), RSTRING_LENINT(buffer));
|
2717
|
+
if(ret == -1)
|
2718
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(this->pgconn));
|
2719
|
+
|
2720
|
+
RB_GC_GUARD(buffer);
|
2721
|
+
|
2722
|
+
return (ret) ? Qtrue : Qfalse;
|
2723
|
+
}
|
2724
|
+
|
2725
|
+
static VALUE
|
2726
|
+
pgconn_sync_put_copy_end(int argc, VALUE *argv, VALUE self)
|
2727
|
+
{
|
2728
|
+
VALUE str;
|
2729
|
+
int ret;
|
2730
|
+
const char *error_message = NULL;
|
2731
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2732
|
+
|
2733
|
+
if (rb_scan_args(argc, argv, "01", &str) == 0)
|
2734
|
+
error_message = NULL;
|
2735
|
+
else
|
2736
|
+
error_message = pg_cstr_enc(str, this->enc_idx);
|
2737
|
+
|
2738
|
+
ret = gvl_PQputCopyEnd(this->pgconn, error_message);
|
2739
|
+
if(ret == -1)
|
2740
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(this->pgconn));
|
2741
|
+
|
2742
|
+
return (ret) ? Qtrue : Qfalse;
|
2743
|
+
}
|
2744
|
+
|
2745
|
+
static VALUE
|
2746
|
+
pgconn_sync_get_copy_data(int argc, VALUE *argv, VALUE self )
|
2747
|
+
{
|
2748
|
+
VALUE async_in;
|
2749
|
+
VALUE result;
|
2750
|
+
int ret;
|
2751
|
+
char *buffer;
|
2752
|
+
VALUE decoder;
|
2753
|
+
t_pg_coder *p_coder = NULL;
|
2754
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2755
|
+
|
2756
|
+
rb_scan_args(argc, argv, "02", &async_in, &decoder);
|
2757
|
+
|
2758
|
+
if( NIL_P(decoder) ){
|
2759
|
+
if( !NIL_P(this->decoder_for_get_copy_data) ){
|
2760
|
+
p_coder = RTYPEDDATA_DATA( this->decoder_for_get_copy_data );
|
2761
|
+
}
|
2762
|
+
} else {
|
2763
|
+
/* Check argument type and use argument decoder */
|
2764
|
+
TypedData_Get_Struct(decoder, t_pg_coder, &pg_coder_type, p_coder);
|
2765
|
+
}
|
2766
|
+
|
2767
|
+
ret = gvl_PQgetCopyData(this->pgconn, &buffer, RTEST(async_in));
|
2768
|
+
if(ret == -2){ /* error */
|
2769
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(this->pgconn));
|
2770
|
+
}
|
2771
|
+
if(ret == -1) { /* No data left */
|
2772
|
+
return Qnil;
|
2773
|
+
}
|
2774
|
+
if(ret == 0) { /* would block */
|
2775
|
+
return Qfalse;
|
2776
|
+
}
|
2777
|
+
|
2778
|
+
if( p_coder ){
|
2779
|
+
t_pg_coder_dec_func dec_func = pg_coder_dec_func( p_coder, p_coder->format );
|
2780
|
+
result = dec_func( p_coder, buffer, ret, 0, 0, this->enc_idx );
|
2781
|
+
} else {
|
2782
|
+
result = rb_str_new(buffer, ret);
|
2783
|
+
}
|
2784
|
+
|
2785
|
+
PQfreemem(buffer);
|
2786
|
+
return result;
|
2787
|
+
}
|
2788
|
+
|
2789
|
+
/*
|
2790
|
+
* call-seq:
|
2791
|
+
* conn.set_error_verbosity( verbosity ) -> Integer
|
2792
|
+
*
|
2793
|
+
* Sets connection's verbosity to _verbosity_ and returns
|
2794
|
+
* the previous setting. Available settings are:
|
2795
|
+
*
|
2796
|
+
* * PQERRORS_TERSE
|
2797
|
+
* * PQERRORS_DEFAULT
|
2798
|
+
* * PQERRORS_VERBOSE
|
2799
|
+
* * PQERRORS_SQLSTATE
|
2800
|
+
*
|
2801
|
+
* Changing the verbosity does not affect the messages available from already-existing PG::Result objects, only subsequently-created ones.
|
2802
|
+
* (But see PG::Result#verbose_error_message if you want to print a previous error with a different verbosity.)
|
2803
|
+
*
|
2804
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-control.html#LIBPQ-PQSETERRORVERBOSITY].
|
2805
|
+
*/
|
2806
|
+
static VALUE
|
2807
|
+
pgconn_set_error_verbosity(VALUE self, VALUE in_verbosity)
|
2808
|
+
{
|
2809
|
+
PGconn *conn = pg_get_pgconn(self);
|
2810
|
+
PGVerbosity verbosity = NUM2INT(in_verbosity);
|
2811
|
+
return INT2FIX(PQsetErrorVerbosity(conn, verbosity));
|
2812
|
+
}
|
2813
|
+
|
2814
|
+
/*
|
2815
|
+
* call-seq:
|
2816
|
+
* conn.set_error_context_visibility( context_visibility ) -> Integer
|
2817
|
+
*
|
2818
|
+
* Sets connection's context display mode to _context_visibility_ and returns
|
2819
|
+
* the previous setting. Available settings are:
|
2820
|
+
* * PQSHOW_CONTEXT_NEVER
|
2821
|
+
* * PQSHOW_CONTEXT_ERRORS
|
2822
|
+
* * PQSHOW_CONTEXT_ALWAYS
|
2823
|
+
*
|
2824
|
+
* This mode controls whether the CONTEXT field is included in messages (unless the verbosity setting is TERSE, in which case CONTEXT is never shown).
|
2825
|
+
* The NEVER mode never includes CONTEXT, while ALWAYS always includes it if available.
|
2826
|
+
* In ERRORS mode (the default), CONTEXT fields are included only for error messages, not for notices and warnings.
|
2827
|
+
*
|
2828
|
+
* Changing this mode does not affect the messages available from already-existing PG::Result objects, only subsequently-created ones.
|
2829
|
+
* (But see PG::Result#verbose_error_message if you want to print a previous error with a different display mode.)
|
2830
|
+
*
|
2831
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-control.html#LIBPQ-PQSETERRORCONTEXTVISIBILITY].
|
2832
|
+
*
|
2833
|
+
*/
|
2834
|
+
static VALUE
|
2835
|
+
pgconn_set_error_context_visibility(VALUE self, VALUE in_context_visibility)
|
2836
|
+
{
|
2837
|
+
PGconn *conn = pg_get_pgconn(self);
|
2838
|
+
PGContextVisibility context_visibility = NUM2INT(in_context_visibility);
|
2839
|
+
return INT2FIX(PQsetErrorContextVisibility(conn, context_visibility));
|
2840
|
+
}
|
2841
|
+
|
2842
|
+
/*
|
2843
|
+
* call-seq:
|
2844
|
+
* conn.trace( stream ) -> nil
|
2845
|
+
*
|
2846
|
+
* Enables tracing message passing between backend. The
|
2847
|
+
* trace message will be written to the stream _stream_,
|
2848
|
+
* which must implement a method +fileno+ that returns
|
2849
|
+
* a writable file descriptor.
|
2850
|
+
*/
|
2851
|
+
static VALUE
|
2852
|
+
pgconn_trace(VALUE self, VALUE stream)
|
2853
|
+
{
|
2854
|
+
VALUE fileno;
|
2855
|
+
FILE *new_fp;
|
2856
|
+
int old_fd, new_fd;
|
2857
|
+
VALUE new_file;
|
2858
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2859
|
+
|
2860
|
+
rb_check_frozen(self);
|
2861
|
+
if(!rb_respond_to(stream,rb_intern("fileno")))
|
2862
|
+
rb_raise(rb_eArgError, "stream does not respond to method: fileno");
|
2863
|
+
|
2864
|
+
fileno = rb_funcall(stream, rb_intern("fileno"), 0);
|
2865
|
+
if(fileno == Qnil)
|
2866
|
+
rb_raise(rb_eArgError, "can't get file descriptor from stream");
|
2867
|
+
|
2868
|
+
/* Duplicate the file descriptor and re-open
|
2869
|
+
* it. Then, make it into a ruby File object
|
2870
|
+
* and assign it to an instance variable.
|
2871
|
+
* This prevents a problem when the File
|
2872
|
+
* object passed to this function is closed
|
2873
|
+
* before the connection object is. */
|
2874
|
+
old_fd = NUM2INT(fileno);
|
2875
|
+
new_fd = dup(old_fd);
|
2876
|
+
new_fp = fdopen(new_fd, "w");
|
2877
|
+
|
2878
|
+
if(new_fp == NULL)
|
2879
|
+
rb_raise(rb_eArgError, "stream is not writable");
|
2880
|
+
|
2881
|
+
new_file = rb_funcall(rb_cIO, rb_intern("new"), 1, INT2NUM(new_fd));
|
2882
|
+
RB_OBJ_WRITE(self, &this->trace_stream, new_file);
|
2883
|
+
|
2884
|
+
PQtrace(this->pgconn, new_fp);
|
2885
|
+
return Qnil;
|
2886
|
+
}
|
2887
|
+
|
2888
|
+
/*
|
2889
|
+
* call-seq:
|
2890
|
+
* conn.untrace() -> nil
|
2891
|
+
*
|
2892
|
+
* Disables the message tracing.
|
2893
|
+
*/
|
2894
|
+
static VALUE
|
2895
|
+
pgconn_untrace(VALUE self)
|
2896
|
+
{
|
2897
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2898
|
+
|
2899
|
+
PQuntrace(this->pgconn);
|
2900
|
+
rb_funcall(this->trace_stream, rb_intern("close"), 0);
|
2901
|
+
RB_OBJ_WRITE(self, &this->trace_stream, Qnil);
|
2902
|
+
return Qnil;
|
2903
|
+
}
|
2904
|
+
|
2905
|
+
|
2906
|
+
/*
|
2907
|
+
* Notice callback proxy function -- delegate the callback to the
|
2908
|
+
* currently-registered Ruby notice_receiver object.
|
2909
|
+
*/
|
2910
|
+
void
|
2911
|
+
notice_receiver_proxy(void *arg, const PGresult *pgresult)
|
2912
|
+
{
|
2913
|
+
VALUE self = (VALUE)arg;
|
2914
|
+
t_pg_connection *this = pg_get_connection( self );
|
2915
|
+
|
2916
|
+
if (this->notice_receiver != Qnil) {
|
2917
|
+
VALUE result = pg_new_result_autoclear( (PGresult *)pgresult, self );
|
2918
|
+
|
2919
|
+
rb_funcall(this->notice_receiver, rb_intern("call"), 1, result);
|
2920
|
+
pg_result_clear( result );
|
2921
|
+
}
|
2922
|
+
return;
|
2923
|
+
}
|
2924
|
+
|
2925
|
+
/*
|
2926
|
+
* call-seq:
|
2927
|
+
* conn.set_notice_receiver {|result| ... } -> Proc
|
2928
|
+
*
|
2929
|
+
* Notice and warning messages generated by the server are not returned
|
2930
|
+
* by the query execution functions, since they do not imply failure of
|
2931
|
+
* the query. Instead they are passed to a notice handling function, and
|
2932
|
+
* execution continues normally after the handler returns. The default
|
2933
|
+
* notice handling function prints the message on <tt>stderr</tt>, but the
|
2934
|
+
* application can override this behavior by supplying its own handling
|
2935
|
+
* function.
|
2936
|
+
*
|
2937
|
+
* For historical reasons, there are two levels of notice handling, called the
|
2938
|
+
* notice receiver and notice processor. The default behavior is for the notice
|
2939
|
+
* receiver to format the notice and pass a string to the notice processor for
|
2940
|
+
* printing. However, an application that chooses to provide its own notice
|
2941
|
+
* receiver will typically ignore the notice processor layer and just do all
|
2942
|
+
* the work in the notice receiver.
|
2943
|
+
*
|
2944
|
+
* This function takes a new block to act as the handler, which should
|
2945
|
+
* accept a single parameter that will be a PG::Result object, and returns
|
2946
|
+
* the Proc object previously set, or +nil+ if it was previously the default.
|
2947
|
+
*
|
2948
|
+
* If you pass no arguments, it will reset the handler to the default.
|
2949
|
+
*
|
2950
|
+
* *Note:* The +result+ passed to the block should not be used outside
|
2951
|
+
* of the block, since the corresponding C object could be freed after the
|
2952
|
+
* block finishes.
|
2953
|
+
*/
|
2954
|
+
static VALUE
|
2955
|
+
pgconn_set_notice_receiver(VALUE self)
|
2956
|
+
{
|
2957
|
+
VALUE proc, old_proc;
|
2958
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
2959
|
+
|
2960
|
+
rb_check_frozen(self);
|
2961
|
+
/* If default_notice_receiver is unset, assume that the current
|
2962
|
+
* notice receiver is the default, and save it to a global variable.
|
2963
|
+
* This should not be a problem because the default receiver is
|
2964
|
+
* always the same, so won't vary among connections.
|
2965
|
+
*/
|
2966
|
+
if(this->default_notice_receiver == NULL)
|
2967
|
+
this->default_notice_receiver = PQsetNoticeReceiver(this->pgconn, NULL, NULL);
|
2968
|
+
|
2969
|
+
old_proc = this->notice_receiver;
|
2970
|
+
if( rb_block_given_p() ) {
|
2971
|
+
proc = rb_block_proc();
|
2972
|
+
PQsetNoticeReceiver(this->pgconn, gvl_notice_receiver_proxy, (void *)self);
|
2973
|
+
} else {
|
2974
|
+
/* if no block is given, set back to default */
|
2975
|
+
proc = Qnil;
|
2976
|
+
PQsetNoticeReceiver(this->pgconn, this->default_notice_receiver, NULL);
|
2977
|
+
}
|
2978
|
+
|
2979
|
+
RB_OBJ_WRITE(self, &this->notice_receiver, proc);
|
2980
|
+
return old_proc;
|
2981
|
+
}
|
2982
|
+
|
2983
|
+
|
2984
|
+
/*
|
2985
|
+
* Notice callback proxy function -- delegate the callback to the
|
2986
|
+
* currently-registered Ruby notice_processor object.
|
2987
|
+
*/
|
2988
|
+
void
|
2989
|
+
notice_processor_proxy(void *arg, const char *message)
|
2990
|
+
{
|
2991
|
+
VALUE self = (VALUE)arg;
|
2992
|
+
t_pg_connection *this = pg_get_connection( self );
|
2993
|
+
|
2994
|
+
if (this->notice_processor != Qnil) {
|
2995
|
+
VALUE message_str = rb_str_new2(message);
|
2996
|
+
PG_ENCODING_SET_NOCHECK( message_str, this->enc_idx );
|
2997
|
+
rb_funcall(this->notice_processor, rb_intern("call"), 1, message_str);
|
2998
|
+
}
|
2999
|
+
return;
|
3000
|
+
}
|
3001
|
+
|
3002
|
+
/*
|
3003
|
+
* call-seq:
|
3004
|
+
* conn.set_notice_processor {|message| ... } -> Proc
|
3005
|
+
*
|
3006
|
+
* See #set_notice_receiver for the description of what this and the
|
3007
|
+
* notice_processor methods do.
|
3008
|
+
*
|
3009
|
+
* This function takes a new block to act as the notice processor and returns
|
3010
|
+
* the Proc object previously set, or +nil+ if it was previously the default.
|
3011
|
+
* The block should accept a single String object.
|
3012
|
+
*
|
3013
|
+
* If you pass no arguments, it will reset the handler to the default.
|
3014
|
+
*/
|
3015
|
+
static VALUE
|
3016
|
+
pgconn_set_notice_processor(VALUE self)
|
3017
|
+
{
|
3018
|
+
VALUE proc, old_proc;
|
3019
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
3020
|
+
|
3021
|
+
rb_check_frozen(self);
|
3022
|
+
/* If default_notice_processor is unset, assume that the current
|
3023
|
+
* notice processor is the default, and save it to a global variable.
|
3024
|
+
* This should not be a problem because the default processor is
|
3025
|
+
* always the same, so won't vary among connections.
|
3026
|
+
*/
|
3027
|
+
if(this->default_notice_processor == NULL)
|
3028
|
+
this->default_notice_processor = PQsetNoticeProcessor(this->pgconn, NULL, NULL);
|
3029
|
+
|
3030
|
+
old_proc = this->notice_processor;
|
3031
|
+
if( rb_block_given_p() ) {
|
3032
|
+
proc = rb_block_proc();
|
3033
|
+
PQsetNoticeProcessor(this->pgconn, gvl_notice_processor_proxy, (void *)self);
|
3034
|
+
} else {
|
3035
|
+
/* if no block is given, set back to default */
|
3036
|
+
proc = Qnil;
|
3037
|
+
PQsetNoticeProcessor(this->pgconn, this->default_notice_processor, NULL);
|
3038
|
+
}
|
3039
|
+
|
3040
|
+
RB_OBJ_WRITE(self, &this->notice_processor, proc);
|
3041
|
+
return old_proc;
|
3042
|
+
}
|
3043
|
+
|
3044
|
+
|
3045
|
+
/*
|
3046
|
+
* call-seq:
|
3047
|
+
* conn.get_client_encoding() -> String
|
3048
|
+
*
|
3049
|
+
* Returns the client encoding as a String.
|
3050
|
+
*/
|
3051
|
+
static VALUE
|
3052
|
+
pgconn_get_client_encoding(VALUE self)
|
3053
|
+
{
|
3054
|
+
char *encoding = (char *)pg_encoding_to_char(PQclientEncoding(pg_get_pgconn(self)));
|
3055
|
+
return rb_str_new2(encoding);
|
3056
|
+
}
|
3057
|
+
|
3058
|
+
|
3059
|
+
/*
|
3060
|
+
* call-seq:
|
3061
|
+
* conn.sync_set_client_encoding( encoding )
|
3062
|
+
*
|
3063
|
+
* This function has the same behavior as #async_set_client_encoding, but is implemented using the synchronous command processing API of libpq.
|
3064
|
+
* See #async_exec for the differences between the two API variants.
|
3065
|
+
* It's not recommended to use explicit sync or async variants but #set_client_encoding instead, unless you have a good reason to do so.
|
3066
|
+
*/
|
3067
|
+
static VALUE
|
3068
|
+
pgconn_sync_set_client_encoding(VALUE self, VALUE str)
|
3069
|
+
{
|
3070
|
+
PGconn *conn = pg_get_pgconn( self );
|
3071
|
+
|
3072
|
+
rb_check_frozen(self);
|
3073
|
+
Check_Type(str, T_STRING);
|
3074
|
+
|
3075
|
+
if ( (gvl_PQsetClientEncoding(conn, StringValueCStr(str))) == -1 )
|
3076
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3077
|
+
|
3078
|
+
pgconn_set_internal_encoding_index( self );
|
3079
|
+
|
3080
|
+
return Qnil;
|
3081
|
+
}
|
3082
|
+
|
3083
|
+
|
3084
|
+
/*
|
3085
|
+
* call-seq:
|
3086
|
+
* conn.quote_ident( str ) -> String
|
3087
|
+
* conn.quote_ident( array ) -> String
|
3088
|
+
* PG::Connection.quote_ident( str ) -> String
|
3089
|
+
* PG::Connection.quote_ident( array ) -> String
|
3090
|
+
*
|
3091
|
+
* Returns a string that is safe for inclusion in a SQL query as an
|
3092
|
+
* identifier. Note: this is not a quote function for values, but for
|
3093
|
+
* identifiers.
|
3094
|
+
*
|
3095
|
+
* For example, in a typical SQL query: <tt>SELECT FOO FROM MYTABLE</tt>
|
3096
|
+
* The identifier <tt>FOO</tt> is folded to lower case, so it actually
|
3097
|
+
* means <tt>foo</tt>. If you really want to access the case-sensitive
|
3098
|
+
* field name <tt>FOO</tt>, use this function like
|
3099
|
+
* <tt>conn.quote_ident('FOO')</tt>, which will return <tt>"FOO"</tt>
|
3100
|
+
* (with double-quotes). PostgreSQL will see the double-quotes, and
|
3101
|
+
* it will not fold to lower case.
|
3102
|
+
*
|
3103
|
+
* Similarly, this function also protects against special characters,
|
3104
|
+
* and other things that might allow SQL injection if the identifier
|
3105
|
+
* comes from an untrusted source.
|
3106
|
+
*
|
3107
|
+
* If the parameter is an Array, then all it's values are separately quoted
|
3108
|
+
* and then joined by a "." character. This can be used for identifiers in
|
3109
|
+
* the form "schema"."table"."column" .
|
3110
|
+
*
|
3111
|
+
* This method is functional identical to the encoder PG::TextEncoder::Identifier .
|
3112
|
+
*
|
3113
|
+
* If the instance method form is used and the input string character encoding
|
3114
|
+
* is different to the connection encoding, then the string is converted to this
|
3115
|
+
* encoding, so that the returned string is always encoded as PG::Connection#internal_encoding .
|
3116
|
+
*
|
3117
|
+
* In the singleton form (PG::Connection.quote_ident) the character encoding
|
3118
|
+
* of the result string is set to the character encoding of the input string.
|
3119
|
+
*/
|
3120
|
+
static VALUE
|
3121
|
+
pgconn_s_quote_ident(VALUE self, VALUE str_or_array)
|
3122
|
+
{
|
3123
|
+
VALUE ret;
|
3124
|
+
int enc_idx;
|
3125
|
+
|
3126
|
+
if( rb_obj_is_kind_of(self, rb_cPGconn) ){
|
3127
|
+
enc_idx = pg_get_connection(self)->enc_idx;
|
3128
|
+
}else{
|
3129
|
+
enc_idx = RB_TYPE_P(str_or_array, T_STRING) ? ENCODING_GET( str_or_array ) : rb_ascii8bit_encindex();
|
3130
|
+
}
|
3131
|
+
pg_text_enc_identifier(NULL, str_or_array, NULL, &ret, enc_idx);
|
3132
|
+
|
3133
|
+
return ret;
|
3134
|
+
}
|
3135
|
+
|
3136
|
+
|
3137
|
+
static void *
|
3138
|
+
get_result_readable(PGconn *conn)
|
3139
|
+
{
|
3140
|
+
return gvl_PQisBusy(conn) ? NULL : (void*)1;
|
3141
|
+
}
|
3142
|
+
|
3143
|
+
|
3144
|
+
/*
|
3145
|
+
* call-seq:
|
3146
|
+
* conn.block( [ timeout ] ) -> Boolean
|
3147
|
+
*
|
3148
|
+
* Blocks until the server is no longer busy, or until the
|
3149
|
+
* optional _timeout_ is reached, whichever comes first.
|
3150
|
+
* _timeout_ is measured in seconds and can be fractional.
|
3151
|
+
*
|
3152
|
+
* Returns +false+ if _timeout_ is reached, +true+ otherwise.
|
3153
|
+
*
|
3154
|
+
* If +true+ is returned, +conn.is_busy+ will return +false+
|
3155
|
+
* and +conn.get_result+ will not block.
|
3156
|
+
*/
|
3157
|
+
VALUE
|
3158
|
+
pgconn_block( int argc, VALUE *argv, VALUE self ) {
|
3159
|
+
struct timeval timeout;
|
3160
|
+
struct timeval *ptimeout = NULL;
|
3161
|
+
VALUE timeout_in;
|
3162
|
+
double timeout_sec;
|
3163
|
+
void *ret;
|
3164
|
+
|
3165
|
+
if ( rb_scan_args(argc, argv, "01", &timeout_in) == 1 ) {
|
3166
|
+
timeout_sec = NUM2DBL( timeout_in );
|
3167
|
+
timeout.tv_sec = (time_t)timeout_sec;
|
3168
|
+
timeout.tv_usec = (suseconds_t)((timeout_sec - (long)timeout_sec) * 1e6);
|
3169
|
+
ptimeout = &timeout;
|
3170
|
+
}
|
3171
|
+
|
3172
|
+
ret = wait_socket_readable( self, ptimeout, get_result_readable);
|
3173
|
+
|
3174
|
+
if( !ret )
|
3175
|
+
return Qfalse;
|
3176
|
+
|
3177
|
+
return Qtrue;
|
3178
|
+
}
|
3179
|
+
|
3180
|
+
|
3181
|
+
/*
|
3182
|
+
* call-seq:
|
3183
|
+
* conn.sync_get_last_result( ) -> PG::Result
|
3184
|
+
*
|
3185
|
+
* This function has the same behavior as #async_get_last_result, but is implemented using the synchronous command processing API of libpq.
|
3186
|
+
* See #async_exec for the differences between the two API variants.
|
3187
|
+
* It's not recommended to use explicit sync or async variants but #get_last_result instead, unless you have a good reason to do so.
|
3188
|
+
*/
|
3189
|
+
static VALUE
|
3190
|
+
pgconn_sync_get_last_result(VALUE self)
|
3191
|
+
{
|
3192
|
+
PGconn *conn = pg_get_pgconn(self);
|
3193
|
+
VALUE rb_pgresult = Qnil;
|
3194
|
+
PGresult *cur, *prev;
|
3195
|
+
|
3196
|
+
|
3197
|
+
cur = prev = NULL;
|
3198
|
+
while ((cur = gvl_PQgetResult(conn)) != NULL) {
|
3199
|
+
int status;
|
3200
|
+
|
3201
|
+
if (prev) PQclear(prev);
|
3202
|
+
prev = cur;
|
3203
|
+
|
3204
|
+
status = PQresultStatus(cur);
|
3205
|
+
if (status == PGRES_COPY_OUT || status == PGRES_COPY_IN || status == PGRES_COPY_BOTH)
|
3206
|
+
break;
|
3207
|
+
}
|
3208
|
+
|
3209
|
+
if (prev) {
|
3210
|
+
rb_pgresult = pg_new_result( prev, self );
|
3211
|
+
pg_result_check(rb_pgresult);
|
3212
|
+
}
|
3213
|
+
|
3214
|
+
return rb_pgresult;
|
3215
|
+
}
|
3216
|
+
|
3217
|
+
/*
|
3218
|
+
* call-seq:
|
3219
|
+
* conn.get_last_result( ) -> PG::Result
|
3220
|
+
*
|
3221
|
+
* This function retrieves all available results
|
3222
|
+
* on the current connection (from previously issued
|
3223
|
+
* asynchronous commands like +send_query()+) and
|
3224
|
+
* returns the last non-NULL result, or +nil+ if no
|
3225
|
+
* results are available.
|
3226
|
+
*
|
3227
|
+
* If the last result contains a bad result_status, an
|
3228
|
+
* appropriate exception is raised.
|
3229
|
+
*
|
3230
|
+
* This function is similar to #get_result
|
3231
|
+
* except that it is designed to get one and only
|
3232
|
+
* one result and that it checks the result state.
|
3233
|
+
*/
|
3234
|
+
static VALUE
|
3235
|
+
pgconn_async_get_last_result(VALUE self)
|
3236
|
+
{
|
3237
|
+
PGconn *conn = pg_get_pgconn(self);
|
3238
|
+
VALUE rb_pgresult = Qnil;
|
3239
|
+
PGresult *cur, *prev;
|
3240
|
+
|
3241
|
+
cur = prev = NULL;
|
3242
|
+
for(;;) {
|
3243
|
+
int status;
|
3244
|
+
|
3245
|
+
/* Wait for input before reading each result.
|
3246
|
+
* That way we support the ruby-3.x IO scheduler and don't block other ruby threads.
|
3247
|
+
*/
|
3248
|
+
wait_socket_readable(self, NULL, get_result_readable);
|
3249
|
+
|
3250
|
+
cur = gvl_PQgetResult(conn);
|
3251
|
+
if (cur == NULL)
|
3252
|
+
break;
|
3253
|
+
|
3254
|
+
if (prev) PQclear(prev);
|
3255
|
+
prev = cur;
|
3256
|
+
|
3257
|
+
status = PQresultStatus(cur);
|
3258
|
+
if (status == PGRES_COPY_OUT || status == PGRES_COPY_IN || status == PGRES_COPY_BOTH)
|
3259
|
+
break;
|
3260
|
+
}
|
3261
|
+
|
3262
|
+
if (prev) {
|
3263
|
+
rb_pgresult = pg_new_result( prev, self );
|
3264
|
+
pg_result_check(rb_pgresult);
|
3265
|
+
}
|
3266
|
+
|
3267
|
+
return rb_pgresult;
|
3268
|
+
}
|
3269
|
+
|
3270
|
+
/*
|
3271
|
+
* call-seq:
|
3272
|
+
* conn.discard_results()
|
3273
|
+
*
|
3274
|
+
* Silently discard any prior query result that application didn't eat.
|
3275
|
+
* This is internally used prior to Connection#exec and sibling methods.
|
3276
|
+
* It doesn't raise an exception on connection errors, but returns +false+ instead.
|
3277
|
+
*
|
3278
|
+
* Returns:
|
3279
|
+
* * +nil+ when the connection is already idle
|
3280
|
+
* * +true+ when some results have been discarded
|
3281
|
+
* * +false+ when a failure occurred and the connection was closed
|
3282
|
+
*
|
3283
|
+
*/
|
3284
|
+
static VALUE
|
3285
|
+
pgconn_discard_results(VALUE self)
|
3286
|
+
{
|
3287
|
+
PGconn *conn = pg_get_pgconn(self);
|
3288
|
+
VALUE socket_io;
|
3289
|
+
|
3290
|
+
switch( PQtransactionStatus(conn) ) {
|
3291
|
+
case PQTRANS_IDLE:
|
3292
|
+
case PQTRANS_INTRANS:
|
3293
|
+
case PQTRANS_INERROR:
|
3294
|
+
return Qnil;
|
3295
|
+
default:;
|
3296
|
+
}
|
3297
|
+
|
3298
|
+
socket_io = pgconn_socket_io(self);
|
3299
|
+
|
3300
|
+
for(;;) {
|
3301
|
+
PGresult *cur;
|
3302
|
+
int status;
|
3303
|
+
|
3304
|
+
/* pgconn_block() raises an exception in case of errors.
|
3305
|
+
* To avoid this call pg_rb_io_wait() and PQconsumeInput() without rb_raise().
|
3306
|
+
*/
|
3307
|
+
while( gvl_PQisBusy(conn) ){
|
3308
|
+
int events;
|
3309
|
+
|
3310
|
+
switch( PQflush(conn) ) {
|
3311
|
+
case 1:
|
3312
|
+
events = RB_NUM2INT(pg_rb_io_wait(socket_io, RB_INT2NUM(PG_RUBY_IO_READABLE | PG_RUBY_IO_WRITABLE), Qnil));
|
3313
|
+
if (events & PG_RUBY_IO_READABLE){
|
3314
|
+
if ( PQconsumeInput(conn) == 0 ) goto error;
|
3315
|
+
}
|
3316
|
+
break;
|
3317
|
+
case 0:
|
3318
|
+
pg_rb_io_wait(socket_io, RB_INT2NUM(PG_RUBY_IO_READABLE), Qnil);
|
3319
|
+
if ( PQconsumeInput(conn) == 0 ) goto error;
|
3320
|
+
break;
|
3321
|
+
default:
|
3322
|
+
goto error;
|
3323
|
+
}
|
3324
|
+
}
|
3325
|
+
|
3326
|
+
cur = gvl_PQgetResult(conn);
|
3327
|
+
if( cur == NULL) break;
|
3328
|
+
|
3329
|
+
status = PQresultStatus(cur);
|
3330
|
+
PQclear(cur);
|
3331
|
+
if (status == PGRES_COPY_IN){
|
3332
|
+
while( gvl_PQputCopyEnd(conn, "COPY terminated by new query or discard_results") == 0 ){
|
3333
|
+
pgconn_async_flush(self);
|
3334
|
+
}
|
3335
|
+
}
|
3336
|
+
if (status == PGRES_COPY_OUT){
|
3337
|
+
for(;;) {
|
3338
|
+
char *buffer = NULL;
|
3339
|
+
int st = gvl_PQgetCopyData(conn, &buffer, 1);
|
3340
|
+
if( st == 0 ) {
|
3341
|
+
/* would block -> wait for readable data */
|
3342
|
+
pg_rb_io_wait(socket_io, RB_INT2NUM(PG_RUBY_IO_READABLE), Qnil);
|
3343
|
+
if ( PQconsumeInput(conn) == 0 ) goto error;
|
3344
|
+
} else if( st > 0 ) {
|
3345
|
+
/* some data retrieved -> discard it */
|
3346
|
+
PQfreemem(buffer);
|
3347
|
+
} else {
|
3348
|
+
/* no more data */
|
3349
|
+
break;
|
3350
|
+
}
|
3351
|
+
}
|
3352
|
+
}
|
3353
|
+
}
|
3354
|
+
|
3355
|
+
return Qtrue;
|
3356
|
+
|
3357
|
+
error:
|
3358
|
+
pgconn_close_socket_io(self);
|
3359
|
+
return Qfalse;
|
3360
|
+
}
|
3361
|
+
|
3362
|
+
/*
|
3363
|
+
* call-seq:
|
3364
|
+
* conn.exec(sql) -> PG::Result
|
3365
|
+
* conn.exec(sql) {|pg_result| block }
|
3366
|
+
*
|
3367
|
+
* Sends SQL query request specified by _sql_ to PostgreSQL.
|
3368
|
+
* On success, it returns a PG::Result instance with all result rows and columns.
|
3369
|
+
* On failure, it raises a PG::Error.
|
3370
|
+
*
|
3371
|
+
* For backward compatibility, if you pass more than one parameter to this method,
|
3372
|
+
* it will call #exec_params for you. New code should explicitly use #exec_params if
|
3373
|
+
* argument placeholders are used.
|
3374
|
+
*
|
3375
|
+
* If the optional code block is given, it will be passed <i>result</i> as an argument,
|
3376
|
+
* and the PG::Result object will automatically be cleared when the block terminates.
|
3377
|
+
* In this instance, <code>conn.exec</code> returns the value of the block.
|
3378
|
+
*
|
3379
|
+
* #exec is an alias for #async_exec which is almost identical to #sync_exec .
|
3380
|
+
* #sync_exec is implemented on the simpler synchronous command processing API of libpq, whereas
|
3381
|
+
* #async_exec is implemented on the asynchronous API and on ruby's IO mechanisms.
|
3382
|
+
* Only #async_exec is compatible to <tt>Fiber.scheduler</tt> based asynchronous IO processing introduced in ruby-3.0.
|
3383
|
+
* Both methods ensure that other threads can process while waiting for the server to
|
3384
|
+
* complete the request, but #sync_exec blocks all signals to be processed until the query is finished.
|
3385
|
+
* This is most notably visible by a delayed reaction to Control+C.
|
3386
|
+
* It's not recommended to use explicit sync or async variants but #exec instead, unless you have a good reason to do so.
|
3387
|
+
*
|
3388
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQEXEC].
|
3389
|
+
*/
|
3390
|
+
static VALUE
|
3391
|
+
pgconn_async_exec(int argc, VALUE *argv, VALUE self)
|
3392
|
+
{
|
3393
|
+
VALUE rb_pgresult = Qnil;
|
3394
|
+
|
3395
|
+
pgconn_discard_results( self );
|
3396
|
+
pgconn_send_query( argc, argv, self );
|
3397
|
+
rb_pgresult = pgconn_async_get_last_result( self );
|
3398
|
+
|
3399
|
+
if ( rb_block_given_p() ) {
|
3400
|
+
return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
|
3401
|
+
}
|
3402
|
+
return rb_pgresult;
|
3403
|
+
}
|
3404
|
+
|
3405
|
+
|
3406
|
+
/*
|
3407
|
+
* call-seq:
|
3408
|
+
* conn.exec_params(sql, params [, result_format [, type_map ]] ) -> nil
|
3409
|
+
* conn.exec_params(sql, params [, result_format [, type_map ]] ) {|pg_result| block }
|
3410
|
+
*
|
3411
|
+
* Sends SQL query request specified by +sql+ to PostgreSQL using placeholders
|
3412
|
+
* for parameters.
|
3413
|
+
*
|
3414
|
+
* Returns a PG::Result instance on success. On failure, it raises a PG::Error.
|
3415
|
+
*
|
3416
|
+
* +params+ is an array of the bind parameters for the SQL query.
|
3417
|
+
* Each element of the +params+ array may be either:
|
3418
|
+
* a hash of the form:
|
3419
|
+
* {:value => String (value of bind parameter)
|
3420
|
+
* :type => Integer (oid of type of bind parameter)
|
3421
|
+
* :format => Integer (0 for text, 1 for binary)
|
3422
|
+
* }
|
3423
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
3424
|
+
* { :value => <string value>, :type => 0, :format => 0 }
|
3425
|
+
*
|
3426
|
+
* PostgreSQL bind parameters are represented as $1, $2, $3, etc.,
|
3427
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
3428
|
+
* to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
|
3429
|
+
*
|
3430
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
3431
|
+
* Instead of specifying type oids, it's recommended to simply add
|
3432
|
+
* explicit casts in the query to ensure that the right type is used.
|
3433
|
+
*
|
3434
|
+
* For example: "SELECT $1::int"
|
3435
|
+
*
|
3436
|
+
* The optional +result_format+ should be 0 for text results, 1
|
3437
|
+
* for binary.
|
3438
|
+
*
|
3439
|
+
* +type_map+ can be a PG::TypeMap derivation (such as PG::BasicTypeMapForQueries).
|
3440
|
+
* This will type cast the params from various Ruby types before transmission
|
3441
|
+
* based on the encoders defined by the type map. When a type encoder is used
|
3442
|
+
* the format and oid of a given bind parameter are retrieved from the encoder
|
3443
|
+
* instead out of the hash form described above.
|
3444
|
+
*
|
3445
|
+
* If the optional code block is given, it will be passed <i>result</i> as an argument,
|
3446
|
+
* and the PG::Result object will automatically be cleared when the block terminates.
|
3447
|
+
* In this instance, <code>conn.exec</code> returns the value of the block.
|
3448
|
+
*
|
3449
|
+
* The primary advantage of #exec_params over #exec is that parameter values can be separated from the command string, thus avoiding the need for tedious and error-prone quoting and escaping.
|
3450
|
+
* Unlike #exec, #exec_params allows at most one SQL command in the given string.
|
3451
|
+
* (There can be semicolons in it, but not more than one nonempty command.)
|
3452
|
+
* This is a limitation of the underlying protocol, but has some usefulness as an extra defense against SQL-injection attacks.
|
3453
|
+
*
|
3454
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQEXECPARAMS].
|
3455
|
+
*/
|
3456
|
+
static VALUE
|
3457
|
+
pgconn_async_exec_params(int argc, VALUE *argv, VALUE self)
|
3458
|
+
{
|
3459
|
+
VALUE rb_pgresult = Qnil;
|
3460
|
+
|
3461
|
+
pgconn_discard_results( self );
|
3462
|
+
/* If called with no or nil parameters, use PQsendQuery for compatibility */
|
3463
|
+
if ( argc == 1 || (argc >= 2 && argc <= 4 && NIL_P(argv[1]) )) {
|
3464
|
+
pg_deprecated(3, ("forwarding async_exec_params to async_exec is deprecated"));
|
3465
|
+
pgconn_send_query( argc, argv, self );
|
3466
|
+
} else {
|
3467
|
+
pgconn_send_query_params( argc, argv, self );
|
3468
|
+
}
|
3469
|
+
rb_pgresult = pgconn_async_get_last_result( self );
|
3470
|
+
|
3471
|
+
if ( rb_block_given_p() ) {
|
3472
|
+
return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
|
3473
|
+
}
|
3474
|
+
return rb_pgresult;
|
3475
|
+
}
|
3476
|
+
|
3477
|
+
|
3478
|
+
/*
|
3479
|
+
* call-seq:
|
3480
|
+
* conn.prepare(stmt_name, sql [, param_types ] ) -> PG::Result
|
3481
|
+
*
|
3482
|
+
* Prepares statement _sql_ with name _name_ to be executed later.
|
3483
|
+
* Returns a PG::Result instance on success.
|
3484
|
+
* On failure, it raises a PG::Error.
|
3485
|
+
*
|
3486
|
+
* +param_types+ is an optional parameter to specify the Oids of the
|
3487
|
+
* types of the parameters.
|
3488
|
+
*
|
3489
|
+
* If the types are not specified, they will be inferred by PostgreSQL.
|
3490
|
+
* Instead of specifying type oids, it's recommended to simply add
|
3491
|
+
* explicit casts in the query to ensure that the right type is used.
|
3492
|
+
*
|
3493
|
+
* For example: "SELECT $1::int"
|
3494
|
+
*
|
3495
|
+
* PostgreSQL bind parameters are represented as $1, $2, $3, etc.,
|
3496
|
+
* inside the SQL query.
|
3497
|
+
*
|
3498
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQPREPARE].
|
3499
|
+
*/
|
3500
|
+
static VALUE
|
3501
|
+
pgconn_async_prepare(int argc, VALUE *argv, VALUE self)
|
3502
|
+
{
|
3503
|
+
VALUE rb_pgresult = Qnil;
|
3504
|
+
|
3505
|
+
pgconn_discard_results( self );
|
3506
|
+
pgconn_send_prepare( argc, argv, self );
|
3507
|
+
rb_pgresult = pgconn_async_get_last_result( self );
|
3508
|
+
|
3509
|
+
if ( rb_block_given_p() ) {
|
3510
|
+
return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
|
3511
|
+
}
|
3512
|
+
return rb_pgresult;
|
3513
|
+
}
|
3514
|
+
|
3515
|
+
|
3516
|
+
/*
|
3517
|
+
* call-seq:
|
3518
|
+
* conn.exec_prepared(statement_name [, params, result_format[, type_map]] ) -> PG::Result
|
3519
|
+
* conn.exec_prepared(statement_name [, params, result_format[, type_map]] ) {|pg_result| block }
|
3520
|
+
*
|
3521
|
+
* Execute prepared named statement specified by _statement_name_.
|
3522
|
+
* Returns a PG::Result instance on success.
|
3523
|
+
* On failure, it raises a PG::Error.
|
3524
|
+
*
|
3525
|
+
* +params+ is an array of the optional bind parameters for the
|
3526
|
+
* SQL query. Each element of the +params+ array may be either:
|
3527
|
+
* a hash of the form:
|
3528
|
+
* {:value => String (value of bind parameter)
|
3529
|
+
* :format => Integer (0 for text, 1 for binary)
|
3530
|
+
* }
|
3531
|
+
* or, it may be a String. If it is a string, that is equivalent to the hash:
|
3532
|
+
* { :value => <string value>, :format => 0 }
|
3533
|
+
*
|
3534
|
+
* PostgreSQL bind parameters are represented as $1, $2, $3, etc.,
|
3535
|
+
* inside the SQL query. The 0th element of the +params+ array is bound
|
3536
|
+
* to $1, the 1st element is bound to $2, etc. +nil+ is treated as +NULL+.
|
3537
|
+
*
|
3538
|
+
* The optional +result_format+ should be 0 for text results, 1
|
3539
|
+
* for binary.
|
3540
|
+
*
|
3541
|
+
* +type_map+ can be a PG::TypeMap derivation (such as PG::BasicTypeMapForQueries).
|
3542
|
+
* This will type cast the params from various Ruby types before transmission
|
3543
|
+
* based on the encoders defined by the type map. When a type encoder is used
|
3544
|
+
* the format and oid of a given bind parameter are retrieved from the encoder
|
3545
|
+
* instead out of the hash form described above.
|
3546
|
+
*
|
3547
|
+
* If the optional code block is given, it will be passed <i>result</i> as an argument,
|
3548
|
+
* and the PG::Result object will automatically be cleared when the block terminates.
|
3549
|
+
* In this instance, <code>conn.exec_prepared</code> returns the value of the block.
|
3550
|
+
*
|
3551
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQEXECPREPARED].
|
3552
|
+
*/
|
3553
|
+
static VALUE
|
3554
|
+
pgconn_async_exec_prepared(int argc, VALUE *argv, VALUE self)
|
3555
|
+
{
|
3556
|
+
VALUE rb_pgresult = Qnil;
|
3557
|
+
|
3558
|
+
pgconn_discard_results( self );
|
3559
|
+
pgconn_send_query_prepared( argc, argv, self );
|
3560
|
+
rb_pgresult = pgconn_async_get_last_result( self );
|
3561
|
+
|
3562
|
+
if ( rb_block_given_p() ) {
|
3563
|
+
return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
|
3564
|
+
}
|
3565
|
+
return rb_pgresult;
|
3566
|
+
}
|
3567
|
+
|
3568
|
+
static VALUE
|
3569
|
+
pgconn_async_describe_close_prepared_potral(VALUE self, VALUE name, VALUE
|
3570
|
+
(*func)(VALUE, VALUE))
|
3571
|
+
{
|
3572
|
+
VALUE rb_pgresult = Qnil;
|
3573
|
+
|
3574
|
+
pgconn_discard_results( self );
|
3575
|
+
func( self, name );
|
3576
|
+
rb_pgresult = pgconn_async_get_last_result( self );
|
3577
|
+
|
3578
|
+
if ( rb_block_given_p() ) {
|
3579
|
+
return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
|
3580
|
+
}
|
3581
|
+
return rb_pgresult;
|
3582
|
+
}
|
3583
|
+
|
3584
|
+
/*
|
3585
|
+
* call-seq:
|
3586
|
+
* conn.describe_portal( portal_name ) -> PG::Result
|
3587
|
+
*
|
3588
|
+
* Retrieve information about the portal _portal_name_.
|
3589
|
+
*
|
3590
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQDESCRIBEPORTAL].
|
3591
|
+
*/
|
3592
|
+
static VALUE
|
3593
|
+
pgconn_async_describe_portal(VALUE self, VALUE portal)
|
3594
|
+
{
|
3595
|
+
return pgconn_async_describe_close_prepared_potral(self, portal, pgconn_send_describe_portal);
|
3596
|
+
}
|
3597
|
+
|
3598
|
+
|
3599
|
+
/*
|
3600
|
+
* call-seq:
|
3601
|
+
* conn.describe_prepared( statement_name ) -> PG::Result
|
3602
|
+
*
|
3603
|
+
* Retrieve information about the prepared statement _statement_name_.
|
3604
|
+
*
|
3605
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQDESCRIBEPREPARED].
|
3606
|
+
*/
|
3607
|
+
static VALUE
|
3608
|
+
pgconn_async_describe_prepared(VALUE self, VALUE stmt_name)
|
3609
|
+
{
|
3610
|
+
return pgconn_async_describe_close_prepared_potral(self, stmt_name, pgconn_send_describe_prepared);
|
3611
|
+
}
|
3612
|
+
|
3613
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
3614
|
+
/*
|
3615
|
+
* call-seq:
|
3616
|
+
* conn.close_prepared( statement_name ) -> PG::Result
|
3617
|
+
*
|
3618
|
+
* Submits a request to close the specified prepared statement, and waits for completion.
|
3619
|
+
* close_prepared allows an application to close a previously prepared statement.
|
3620
|
+
* Closing a statement releases all of its associated resources on the server and allows its name to be reused.
|
3621
|
+
* It's the same as using the +DEALLOCATE+ SQL statement, but on a lower protocol level.
|
3622
|
+
*
|
3623
|
+
* +statement_name+ can be "" or +nil+ to reference the unnamed statement.
|
3624
|
+
* It is fine if no statement exists with this name, in that case the operation is a no-op.
|
3625
|
+
* On success, a PG::Result with status PGRES_COMMAND_OK is returned.
|
3626
|
+
*
|
3627
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQCLOSEPREPARED].
|
3628
|
+
*
|
3629
|
+
* Available since PostgreSQL-17.
|
3630
|
+
*/
|
3631
|
+
static VALUE
|
3632
|
+
pgconn_async_close_prepared(VALUE self, VALUE stmt_name)
|
3633
|
+
{
|
3634
|
+
return pgconn_async_describe_close_prepared_potral(self, stmt_name, pgconn_send_close_prepared);
|
3635
|
+
}
|
3636
|
+
|
3637
|
+
/*
|
3638
|
+
* call-seq:
|
3639
|
+
* conn.close_portal( portal_name ) -> PG::Result
|
3640
|
+
*
|
3641
|
+
* Submits a request to close the specified portal, and waits for completion.
|
3642
|
+
*
|
3643
|
+
* close_portal allows an application to trigger a close of a previously created portal.
|
3644
|
+
* Closing a portal releases all of its associated resources on the server and allows its name to be reused.
|
3645
|
+
* (pg does not provide any direct access to portals, but you can use this function to close a cursor created with a DECLARE CURSOR SQL command.)
|
3646
|
+
*
|
3647
|
+
* +portal_name+ can be "" or +nil+ to reference the unnamed portal.
|
3648
|
+
* It is fine if no portal exists with this name, in that case the operation is a no-op.
|
3649
|
+
* On success, a PG::Result with status PGRES_COMMAND_OK is returned.
|
3650
|
+
*
|
3651
|
+
* See also corresponding {libpq function}[https://www.postgresql.org/docs/current/libpq-exec.html#LIBPQ-PQCLOSEPORTAL].
|
3652
|
+
*
|
3653
|
+
* Available since PostgreSQL-17.
|
3654
|
+
*/
|
3655
|
+
static VALUE
|
3656
|
+
pgconn_async_close_portal(VALUE self, VALUE portal)
|
3657
|
+
{
|
3658
|
+
return pgconn_async_describe_close_prepared_potral(self, portal, pgconn_send_close_portal);
|
3659
|
+
}
|
3660
|
+
#endif
|
3661
|
+
|
3662
|
+
/*
|
3663
|
+
* call-seq:
|
3664
|
+
* conn.ssl_in_use? -> Boolean
|
3665
|
+
*
|
3666
|
+
* Returns +true+ if the connection uses SSL/TLS, +false+ if not.
|
3667
|
+
*
|
3668
|
+
*/
|
3669
|
+
static VALUE
|
3670
|
+
pgconn_ssl_in_use(VALUE self)
|
3671
|
+
{
|
3672
|
+
return PQsslInUse(pg_get_pgconn(self)) ? Qtrue : Qfalse;
|
3673
|
+
}
|
3674
|
+
|
3675
|
+
|
3676
|
+
/*
|
3677
|
+
* call-seq:
|
3678
|
+
* conn.ssl_attribute(attribute_name) -> String
|
3679
|
+
*
|
3680
|
+
* Returns SSL-related information about the connection.
|
3681
|
+
*
|
3682
|
+
* The list of available attributes varies depending on the SSL library being used,
|
3683
|
+
* and the type of connection. If an attribute is not available, returns nil.
|
3684
|
+
*
|
3685
|
+
* The following attributes are commonly available:
|
3686
|
+
*
|
3687
|
+
* [+library+]
|
3688
|
+
* Name of the SSL implementation in use. (Currently, only "OpenSSL" is implemented)
|
3689
|
+
* [+protocol+]
|
3690
|
+
* SSL/TLS version in use. Common values are "SSLv2", "SSLv3", "TLSv1", "TLSv1.1" and "TLSv1.2", but an implementation may return other strings if some other protocol is used.
|
3691
|
+
* [+key_bits+]
|
3692
|
+
* Number of key bits used by the encryption algorithm.
|
3693
|
+
* [+cipher+]
|
3694
|
+
* A short name of the ciphersuite used, e.g. "DHE-RSA-DES-CBC3-SHA". The names are specific to each SSL implementation.
|
3695
|
+
* [+compression+]
|
3696
|
+
* If SSL compression is in use, returns the name of the compression algorithm, or "on" if compression is used but the algorithm is not known. If compression is not in use, returns "off".
|
3697
|
+
*
|
3698
|
+
*
|
3699
|
+
* See also #ssl_attribute_names and the {corresponding libpq function}[https://www.postgresql.org/docs/current/libpq-status.html#LIBPQ-PQSSLATTRIBUTE].
|
3700
|
+
*
|
3701
|
+
*/
|
3702
|
+
static VALUE
|
3703
|
+
pgconn_ssl_attribute(VALUE self, VALUE attribute_name)
|
3704
|
+
{
|
3705
|
+
const char *p_attr;
|
3706
|
+
|
3707
|
+
p_attr = PQsslAttribute(pg_get_pgconn(self), StringValueCStr(attribute_name));
|
3708
|
+
return p_attr ? rb_str_new_cstr(p_attr) : Qnil;
|
3709
|
+
}
|
3710
|
+
|
3711
|
+
/*
|
3712
|
+
* call-seq:
|
3713
|
+
* conn.ssl_attribute_names -> Array<String>
|
3714
|
+
*
|
3715
|
+
* Return an array of SSL attribute names available.
|
3716
|
+
*
|
3717
|
+
* See also #ssl_attribute
|
3718
|
+
*
|
3719
|
+
*/
|
3720
|
+
static VALUE
|
3721
|
+
pgconn_ssl_attribute_names(VALUE self)
|
3722
|
+
{
|
3723
|
+
int i;
|
3724
|
+
const char * const * p_list = PQsslAttributeNames(pg_get_pgconn(self));
|
3725
|
+
VALUE ary = rb_ary_new();
|
3726
|
+
|
3727
|
+
for ( i = 0; p_list[i]; i++ ) {
|
3728
|
+
rb_ary_push( ary, rb_str_new_cstr( p_list[i] ));
|
3729
|
+
}
|
3730
|
+
return ary;
|
3731
|
+
}
|
3732
|
+
|
3733
|
+
|
3734
|
+
|
3735
|
+
#ifdef HAVE_PQENTERPIPELINEMODE
|
3736
|
+
/*
|
3737
|
+
* call-seq:
|
3738
|
+
* conn.pipeline_status -> Integer
|
3739
|
+
*
|
3740
|
+
* Returns the current pipeline mode status of the libpq connection.
|
3741
|
+
*
|
3742
|
+
* PQpipelineStatus can return one of the following values:
|
3743
|
+
*
|
3744
|
+
* * PQ_PIPELINE_ON - The libpq connection is in pipeline mode.
|
3745
|
+
* * PQ_PIPELINE_OFF - The libpq connection is not in pipeline mode.
|
3746
|
+
* * PQ_PIPELINE_ABORTED - The libpq connection is in pipeline mode and an error occurred while processing the current pipeline.
|
3747
|
+
* The aborted flag is cleared when PQgetResult returns a result of type PGRES_PIPELINE_SYNC.
|
3748
|
+
*
|
3749
|
+
* Available since PostgreSQL-14
|
3750
|
+
*/
|
3751
|
+
static VALUE
|
3752
|
+
pgconn_pipeline_status(VALUE self)
|
3753
|
+
{
|
3754
|
+
int res = PQpipelineStatus(pg_get_pgconn(self));
|
3755
|
+
return INT2FIX(res);
|
3756
|
+
}
|
3757
|
+
|
3758
|
+
|
3759
|
+
/*
|
3760
|
+
* call-seq:
|
3761
|
+
* conn.enter_pipeline_mode -> nil
|
3762
|
+
*
|
3763
|
+
* Causes a connection to enter pipeline mode if it is currently idle or already in pipeline mode.
|
3764
|
+
*
|
3765
|
+
* Raises PG::Error and has no effect if the connection is not currently idle, i.e., it has a result ready, or it is waiting for more input from the server, etc.
|
3766
|
+
* This function does not actually send anything to the server, it just changes the libpq connection state.
|
3767
|
+
*
|
3768
|
+
* See the {PostgreSQL documentation}[https://www.postgresql.org/docs/17/libpq-pipeline-mode.html#LIBPQ-PIPELINE-MODE].
|
3769
|
+
*
|
3770
|
+
* Available since PostgreSQL-14
|
3771
|
+
*/
|
3772
|
+
static VALUE
|
3773
|
+
pgconn_enter_pipeline_mode(VALUE self)
|
3774
|
+
{
|
3775
|
+
PGconn *conn = pg_get_pgconn(self);
|
3776
|
+
int res = PQenterPipelineMode(conn);
|
3777
|
+
if( res != 1 )
|
3778
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3779
|
+
|
3780
|
+
return Qnil;
|
3781
|
+
}
|
3782
|
+
|
3783
|
+
/*
|
3784
|
+
* call-seq:
|
3785
|
+
* conn.exit_pipeline_mode -> nil
|
3786
|
+
*
|
3787
|
+
* Causes a connection to exit pipeline mode if it is currently in pipeline mode with an empty queue and no pending results.
|
3788
|
+
*
|
3789
|
+
* Takes no action if not in pipeline mode.
|
3790
|
+
* Raises PG::Error if the current statement isn't finished processing, or PQgetResult has not been called to collect results from all previously sent query.
|
3791
|
+
*
|
3792
|
+
* Available since PostgreSQL-14
|
3793
|
+
*/
|
3794
|
+
static VALUE
|
3795
|
+
pgconn_exit_pipeline_mode(VALUE self)
|
3796
|
+
{
|
3797
|
+
PGconn *conn = pg_get_pgconn(self);
|
3798
|
+
int res = PQexitPipelineMode(conn);
|
3799
|
+
if( res != 1 )
|
3800
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3801
|
+
|
3802
|
+
return Qnil;
|
3803
|
+
}
|
3804
|
+
|
3805
|
+
|
3806
|
+
/*
|
3807
|
+
* call-seq:
|
3808
|
+
* conn.sync_pipeline_sync -> nil
|
3809
|
+
*
|
3810
|
+
* This function has the same behavior as #async_pipeline_sync, but is implemented using the synchronous command processing API of libpq.
|
3811
|
+
* See #async_exec for the differences between the two API variants.
|
3812
|
+
* It's not recommended to use explicit sync or async variants but #pipeline_sync instead, unless you have a good reason to do so.
|
3813
|
+
*
|
3814
|
+
* Available since PostgreSQL-14
|
3815
|
+
*/
|
3816
|
+
static VALUE
|
3817
|
+
pgconn_sync_pipeline_sync(VALUE self)
|
3818
|
+
{
|
3819
|
+
PGconn *conn = pg_get_pgconn(self);
|
3820
|
+
int res = gvl_PQpipelineSync(conn);
|
3821
|
+
if( res != 1 )
|
3822
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3823
|
+
|
3824
|
+
return Qnil;
|
3825
|
+
}
|
3826
|
+
|
3827
|
+
|
3828
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
3829
|
+
/*
|
3830
|
+
* call-seq:
|
3831
|
+
* conn.send_pipeline_sync -> nil
|
3832
|
+
*
|
3833
|
+
* Marks a synchronization point in a pipeline by sending a sync message without flushing the send buffer.
|
3834
|
+
*
|
3835
|
+
* This serves as the delimiter of an implicit transaction and an error recovery point.
|
3836
|
+
* Raises PG::Error if the connection is not in pipeline mode or sending a sync message failed.
|
3837
|
+
* Note that the message is not itself flushed to the server automatically; use flush if necessary.
|
3838
|
+
*
|
3839
|
+
* Available since PostgreSQL-17
|
3840
|
+
*/
|
3841
|
+
static VALUE
|
3842
|
+
pgconn_send_pipeline_sync(VALUE self)
|
3843
|
+
{
|
3844
|
+
PGconn *conn = pg_get_pgconn(self);
|
3845
|
+
int res = gvl_PQsendPipelineSync(conn);
|
3846
|
+
if( res != 1 )
|
3847
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3848
|
+
|
3849
|
+
return Qnil;
|
3850
|
+
}
|
3851
|
+
#endif
|
3852
|
+
|
3853
|
+
|
3854
|
+
/*
|
3855
|
+
* call-seq:
|
3856
|
+
* conn.send_flush_request -> nil
|
3857
|
+
*
|
3858
|
+
* Sends a request for the server to flush its output buffer.
|
3859
|
+
*
|
3860
|
+
* The server flushes its output buffer automatically as a result of Connection#pipeline_sync being called, or on any request when not in pipeline mode.
|
3861
|
+
* This function is useful to cause the server to flush its output buffer in pipeline mode without establishing a synchronization point.
|
3862
|
+
* Note that the request is not itself flushed to the server automatically; use Connection#flush if necessary.
|
3863
|
+
*
|
3864
|
+
* Available since PostgreSQL-14
|
3865
|
+
*/
|
3866
|
+
static VALUE
|
3867
|
+
pgconn_send_flush_request(VALUE self)
|
3868
|
+
{
|
3869
|
+
PGconn *conn = pg_get_pgconn(self);
|
3870
|
+
int res = PQsendFlushRequest(conn);
|
3871
|
+
if( res != 1 )
|
3872
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3873
|
+
|
3874
|
+
return Qnil;
|
3875
|
+
}
|
3876
|
+
|
3877
|
+
#endif
|
3878
|
+
|
3879
|
+
/**************************************************************************
|
3880
|
+
* LARGE OBJECT SUPPORT
|
3881
|
+
**************************************************************************/
|
3882
|
+
|
3883
|
+
#define BLOCKING_BEGIN(conn) do { \
|
3884
|
+
int old_nonblocking = PQisnonblocking(conn); \
|
3885
|
+
PQsetnonblocking(conn, 0);
|
3886
|
+
|
3887
|
+
#define BLOCKING_END(th) \
|
3888
|
+
PQsetnonblocking(conn, old_nonblocking); \
|
3889
|
+
} while(0);
|
3890
|
+
|
3891
|
+
/*
|
3892
|
+
* call-seq:
|
3893
|
+
* conn.lo_creat( [mode] ) -> Integer
|
3894
|
+
*
|
3895
|
+
* Creates a large object with mode _mode_. Returns a large object Oid.
|
3896
|
+
* On failure, it raises PG::Error.
|
3897
|
+
*/
|
3898
|
+
static VALUE
|
3899
|
+
pgconn_locreat(int argc, VALUE *argv, VALUE self)
|
3900
|
+
{
|
3901
|
+
Oid lo_oid;
|
3902
|
+
int mode;
|
3903
|
+
VALUE nmode;
|
3904
|
+
PGconn *conn = pg_get_pgconn(self);
|
3905
|
+
|
3906
|
+
if (rb_scan_args(argc, argv, "01", &nmode) == 0)
|
3907
|
+
mode = INV_READ;
|
3908
|
+
else
|
3909
|
+
mode = NUM2INT(nmode);
|
3910
|
+
|
3911
|
+
BLOCKING_BEGIN(conn)
|
3912
|
+
lo_oid = lo_creat(conn, mode);
|
3913
|
+
BLOCKING_END(conn)
|
3914
|
+
|
3915
|
+
if (lo_oid == 0)
|
3916
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_creat failed");
|
3917
|
+
|
3918
|
+
return UINT2NUM(lo_oid);
|
3919
|
+
}
|
3920
|
+
|
3921
|
+
/*
|
3922
|
+
* call-seq:
|
3923
|
+
* conn.lo_create( oid ) -> Integer
|
3924
|
+
*
|
3925
|
+
* Creates a large object with oid _oid_. Returns the large object Oid.
|
3926
|
+
* On failure, it raises PG::Error.
|
3927
|
+
*/
|
3928
|
+
static VALUE
|
3929
|
+
pgconn_locreate(VALUE self, VALUE in_lo_oid)
|
3930
|
+
{
|
3931
|
+
Oid ret, lo_oid;
|
3932
|
+
PGconn *conn = pg_get_pgconn(self);
|
3933
|
+
lo_oid = NUM2UINT(in_lo_oid);
|
3934
|
+
|
3935
|
+
ret = lo_create(conn, lo_oid);
|
3936
|
+
if (ret == InvalidOid)
|
3937
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_create failed");
|
3938
|
+
|
3939
|
+
return UINT2NUM(ret);
|
3940
|
+
}
|
3941
|
+
|
3942
|
+
/*
|
3943
|
+
* call-seq:
|
3944
|
+
* conn.lo_import(file) -> Integer
|
3945
|
+
*
|
3946
|
+
* Import a file to a large object. Returns a large object Oid.
|
3947
|
+
*
|
3948
|
+
* On failure, it raises a PG::Error.
|
3949
|
+
*/
|
3950
|
+
static VALUE
|
3951
|
+
pgconn_loimport(VALUE self, VALUE filename)
|
3952
|
+
{
|
3953
|
+
Oid lo_oid;
|
3954
|
+
|
3955
|
+
PGconn *conn = pg_get_pgconn(self);
|
3956
|
+
|
3957
|
+
Check_Type(filename, T_STRING);
|
3958
|
+
|
3959
|
+
BLOCKING_BEGIN(conn)
|
3960
|
+
lo_oid = lo_import(conn, StringValueCStr(filename));
|
3961
|
+
BLOCKING_END(conn)
|
3962
|
+
|
3963
|
+
if (lo_oid == 0) {
|
3964
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3965
|
+
}
|
3966
|
+
return UINT2NUM(lo_oid);
|
3967
|
+
}
|
3968
|
+
|
3969
|
+
/*
|
3970
|
+
* call-seq:
|
3971
|
+
* conn.lo_export( oid, file ) -> nil
|
3972
|
+
*
|
3973
|
+
* Saves a large object of _oid_ to a _file_.
|
3974
|
+
*/
|
3975
|
+
static VALUE
|
3976
|
+
pgconn_loexport(VALUE self, VALUE lo_oid, VALUE filename)
|
3977
|
+
{
|
3978
|
+
PGconn *conn = pg_get_pgconn(self);
|
3979
|
+
Oid oid;
|
3980
|
+
int ret;
|
3981
|
+
Check_Type(filename, T_STRING);
|
3982
|
+
|
3983
|
+
oid = NUM2UINT(lo_oid);
|
3984
|
+
|
3985
|
+
BLOCKING_BEGIN(conn)
|
3986
|
+
ret = lo_export(conn, oid, StringValueCStr(filename));
|
3987
|
+
BLOCKING_END(conn)
|
3988
|
+
|
3989
|
+
if (ret < 0) {
|
3990
|
+
pg_raise_conn_error( rb_ePGerror, self, "%s", PQerrorMessage(conn));
|
3991
|
+
}
|
3992
|
+
return Qnil;
|
3993
|
+
}
|
3994
|
+
|
3995
|
+
/*
|
3996
|
+
* call-seq:
|
3997
|
+
* conn.lo_open( oid, [mode] ) -> Integer
|
3998
|
+
*
|
3999
|
+
* Open a large object of _oid_. Returns a large object descriptor
|
4000
|
+
* instance on success. The _mode_ argument specifies the mode for
|
4001
|
+
* the opened large object,which is either +INV_READ+, or +INV_WRITE+.
|
4002
|
+
*
|
4003
|
+
* If _mode_ is omitted, the default is +INV_READ+.
|
4004
|
+
*/
|
4005
|
+
static VALUE
|
4006
|
+
pgconn_loopen(int argc, VALUE *argv, VALUE self)
|
4007
|
+
{
|
4008
|
+
Oid lo_oid;
|
4009
|
+
int fd, mode;
|
4010
|
+
VALUE nmode, selfid;
|
4011
|
+
PGconn *conn = pg_get_pgconn(self);
|
4012
|
+
|
4013
|
+
rb_scan_args(argc, argv, "11", &selfid, &nmode);
|
4014
|
+
lo_oid = NUM2UINT(selfid);
|
4015
|
+
if(NIL_P(nmode))
|
4016
|
+
mode = INV_READ;
|
4017
|
+
else
|
4018
|
+
mode = NUM2INT(nmode);
|
4019
|
+
|
4020
|
+
BLOCKING_BEGIN(conn)
|
4021
|
+
fd = lo_open(conn, lo_oid, mode);
|
4022
|
+
BLOCKING_END(conn)
|
4023
|
+
|
4024
|
+
if(fd < 0) {
|
4025
|
+
pg_raise_conn_error( rb_ePGerror, self, "can't open large object: %s", PQerrorMessage(conn));
|
4026
|
+
}
|
4027
|
+
return INT2FIX(fd);
|
4028
|
+
}
|
4029
|
+
|
4030
|
+
/*
|
4031
|
+
* call-seq:
|
4032
|
+
* conn.lo_write( lo_desc, buffer ) -> Integer
|
4033
|
+
*
|
4034
|
+
* Writes the string _buffer_ to the large object _lo_desc_.
|
4035
|
+
* Returns the number of bytes written.
|
4036
|
+
*/
|
4037
|
+
static VALUE
|
4038
|
+
pgconn_lowrite(VALUE self, VALUE in_lo_desc, VALUE buffer)
|
4039
|
+
{
|
4040
|
+
int n;
|
4041
|
+
PGconn *conn = pg_get_pgconn(self);
|
4042
|
+
int fd = NUM2INT(in_lo_desc);
|
4043
|
+
|
4044
|
+
Check_Type(buffer, T_STRING);
|
4045
|
+
|
4046
|
+
if( RSTRING_LEN(buffer) < 0) {
|
4047
|
+
pg_raise_conn_error( rb_ePGerror, self, "write buffer zero string");
|
4048
|
+
}
|
4049
|
+
BLOCKING_BEGIN(conn)
|
4050
|
+
n = lo_write(conn, fd, StringValuePtr(buffer),
|
4051
|
+
RSTRING_LEN(buffer));
|
4052
|
+
BLOCKING_END(conn)
|
4053
|
+
|
4054
|
+
if(n < 0) {
|
4055
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_write failed: %s", PQerrorMessage(conn));
|
4056
|
+
}
|
4057
|
+
|
4058
|
+
return INT2FIX(n);
|
4059
|
+
}
|
4060
|
+
|
4061
|
+
/*
|
4062
|
+
* call-seq:
|
4063
|
+
* conn.lo_read( lo_desc, len ) -> String
|
4064
|
+
*
|
4065
|
+
* Attempts to read _len_ bytes from large object _lo_desc_,
|
4066
|
+
* returns resulting data.
|
4067
|
+
*/
|
4068
|
+
static VALUE
|
4069
|
+
pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
|
4070
|
+
{
|
4071
|
+
int ret;
|
4072
|
+
PGconn *conn = pg_get_pgconn(self);
|
4073
|
+
int len = NUM2INT(in_len);
|
4074
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
4075
|
+
VALUE str;
|
4076
|
+
char *buffer;
|
4077
|
+
|
4078
|
+
if (len < 0)
|
4079
|
+
pg_raise_conn_error( rb_ePGerror, self, "negative length %d given", len);
|
4080
|
+
|
4081
|
+
buffer = ALLOC_N(char, len);
|
4082
|
+
|
4083
|
+
BLOCKING_BEGIN(conn)
|
4084
|
+
ret = lo_read(conn, lo_desc, buffer, len);
|
4085
|
+
BLOCKING_END(conn)
|
4086
|
+
|
4087
|
+
if(ret < 0)
|
4088
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_read failed");
|
4089
|
+
|
4090
|
+
if(ret == 0) {
|
4091
|
+
xfree(buffer);
|
4092
|
+
return Qnil;
|
4093
|
+
}
|
4094
|
+
|
4095
|
+
str = rb_str_new(buffer, ret);
|
4096
|
+
xfree(buffer);
|
4097
|
+
|
4098
|
+
return str;
|
4099
|
+
}
|
4100
|
+
|
4101
|
+
|
4102
|
+
/*
|
4103
|
+
* call-seq:
|
4104
|
+
* conn.lo_lseek( lo_desc, offset, whence ) -> Integer
|
4105
|
+
*
|
4106
|
+
* Move the large object pointer _lo_desc_ to offset _offset_.
|
4107
|
+
* Valid values for _whence_ are +SEEK_SET+, +SEEK_CUR+, and +SEEK_END+.
|
4108
|
+
* (Or 0, 1, or 2.)
|
4109
|
+
*/
|
4110
|
+
static VALUE
|
4111
|
+
pgconn_lolseek(VALUE self, VALUE in_lo_desc, VALUE offset, VALUE whence)
|
4112
|
+
{
|
4113
|
+
PGconn *conn = pg_get_pgconn(self);
|
4114
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
4115
|
+
int ret;
|
4116
|
+
|
4117
|
+
BLOCKING_BEGIN(conn)
|
4118
|
+
ret = lo_lseek(conn, lo_desc, NUM2INT(offset), NUM2INT(whence));
|
4119
|
+
BLOCKING_END(conn)
|
4120
|
+
|
4121
|
+
if(ret < 0) {
|
4122
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_lseek failed");
|
4123
|
+
}
|
4124
|
+
|
4125
|
+
return INT2FIX(ret);
|
4126
|
+
}
|
4127
|
+
|
4128
|
+
/*
|
4129
|
+
* call-seq:
|
4130
|
+
* conn.lo_tell( lo_desc ) -> Integer
|
4131
|
+
*
|
4132
|
+
* Returns the current position of the large object _lo_desc_.
|
4133
|
+
*/
|
4134
|
+
static VALUE
|
4135
|
+
pgconn_lotell(VALUE self, VALUE in_lo_desc)
|
4136
|
+
{
|
4137
|
+
int position;
|
4138
|
+
PGconn *conn = pg_get_pgconn(self);
|
4139
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
4140
|
+
|
4141
|
+
BLOCKING_BEGIN(conn)
|
4142
|
+
position = lo_tell(conn, lo_desc);
|
4143
|
+
BLOCKING_END(conn)
|
4144
|
+
|
4145
|
+
if(position < 0)
|
4146
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_tell failed");
|
4147
|
+
|
4148
|
+
return INT2FIX(position);
|
4149
|
+
}
|
4150
|
+
|
4151
|
+
/*
|
4152
|
+
* call-seq:
|
4153
|
+
* conn.lo_truncate( lo_desc, len ) -> nil
|
4154
|
+
*
|
4155
|
+
* Truncates the large object _lo_desc_ to size _len_.
|
4156
|
+
*/
|
4157
|
+
static VALUE
|
4158
|
+
pgconn_lotruncate(VALUE self, VALUE in_lo_desc, VALUE in_len)
|
4159
|
+
{
|
4160
|
+
PGconn *conn = pg_get_pgconn(self);
|
4161
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
4162
|
+
size_t len = NUM2INT(in_len);
|
4163
|
+
int ret;
|
4164
|
+
|
4165
|
+
BLOCKING_BEGIN(conn)
|
4166
|
+
ret = lo_truncate(conn,lo_desc,len);
|
4167
|
+
BLOCKING_END(conn)
|
4168
|
+
|
4169
|
+
if(ret < 0)
|
4170
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_truncate failed");
|
4171
|
+
|
4172
|
+
return Qnil;
|
4173
|
+
}
|
4174
|
+
|
4175
|
+
/*
|
4176
|
+
* call-seq:
|
4177
|
+
* conn.lo_close( lo_desc ) -> nil
|
4178
|
+
*
|
4179
|
+
* Closes the postgres large object of _lo_desc_.
|
4180
|
+
*/
|
4181
|
+
static VALUE
|
4182
|
+
pgconn_loclose(VALUE self, VALUE in_lo_desc)
|
4183
|
+
{
|
4184
|
+
PGconn *conn = pg_get_pgconn(self);
|
4185
|
+
int lo_desc = NUM2INT(in_lo_desc);
|
4186
|
+
int ret;
|
4187
|
+
|
4188
|
+
BLOCKING_BEGIN(conn)
|
4189
|
+
ret = lo_close(conn,lo_desc);
|
4190
|
+
BLOCKING_END(conn)
|
4191
|
+
|
4192
|
+
if(ret < 0)
|
4193
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_close failed");
|
4194
|
+
|
4195
|
+
return Qnil;
|
4196
|
+
}
|
4197
|
+
|
4198
|
+
/*
|
4199
|
+
* call-seq:
|
4200
|
+
* conn.lo_unlink( oid ) -> nil
|
4201
|
+
*
|
4202
|
+
* Unlinks (deletes) the postgres large object of _oid_.
|
4203
|
+
*/
|
4204
|
+
static VALUE
|
4205
|
+
pgconn_lounlink(VALUE self, VALUE in_oid)
|
4206
|
+
{
|
4207
|
+
PGconn *conn = pg_get_pgconn(self);
|
4208
|
+
Oid oid = NUM2UINT(in_oid);
|
4209
|
+
int ret;
|
4210
|
+
|
4211
|
+
BLOCKING_BEGIN(conn)
|
4212
|
+
ret = lo_unlink(conn,oid);
|
4213
|
+
BLOCKING_END(conn)
|
4214
|
+
|
4215
|
+
if(ret < 0)
|
4216
|
+
pg_raise_conn_error( rb_ePGerror, self, "lo_unlink failed");
|
4217
|
+
|
4218
|
+
return Qnil;
|
4219
|
+
}
|
4220
|
+
|
4221
|
+
|
4222
|
+
static void
|
4223
|
+
pgconn_set_internal_encoding_index( VALUE self )
|
4224
|
+
{
|
4225
|
+
int enc_idx;
|
4226
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
4227
|
+
rb_encoding *enc = pg_conn_enc_get( this->pgconn );
|
4228
|
+
enc_idx = rb_enc_to_index(enc);
|
4229
|
+
if( enc_idx >= (1<<(PG_ENC_IDX_BITS-1)) ) rb_raise(rb_eArgError, "unsupported encoding index %d", enc_idx);
|
4230
|
+
this->enc_idx = enc_idx;
|
4231
|
+
}
|
4232
|
+
|
4233
|
+
/*
|
4234
|
+
* call-seq:
|
4235
|
+
* conn.internal_encoding -> Encoding
|
4236
|
+
*
|
4237
|
+
* defined in Ruby 1.9 or later.
|
4238
|
+
*
|
4239
|
+
* Returns:
|
4240
|
+
* * an Encoding - client_encoding of the connection as a Ruby Encoding object.
|
4241
|
+
* * nil - the client_encoding is 'SQL_ASCII'
|
4242
|
+
*/
|
4243
|
+
static VALUE
|
4244
|
+
pgconn_internal_encoding(VALUE self)
|
4245
|
+
{
|
4246
|
+
PGconn *conn = pg_get_pgconn( self );
|
4247
|
+
rb_encoding *enc = pg_conn_enc_get( conn );
|
4248
|
+
|
4249
|
+
if ( enc ) {
|
4250
|
+
return rb_enc_from_encoding( enc );
|
4251
|
+
} else {
|
4252
|
+
return Qnil;
|
4253
|
+
}
|
4254
|
+
}
|
4255
|
+
|
4256
|
+
static VALUE pgconn_external_encoding(VALUE self);
|
4257
|
+
|
4258
|
+
/*
|
4259
|
+
* call-seq:
|
4260
|
+
* conn.internal_encoding = value
|
4261
|
+
*
|
4262
|
+
* A wrapper of #set_client_encoding.
|
4263
|
+
* defined in Ruby 1.9 or later.
|
4264
|
+
*
|
4265
|
+
* +value+ can be one of:
|
4266
|
+
* * an Encoding
|
4267
|
+
* * a String - a name of Encoding
|
4268
|
+
* * +nil+ - sets the client_encoding to SQL_ASCII.
|
4269
|
+
*/
|
4270
|
+
static VALUE
|
4271
|
+
pgconn_internal_encoding_set(VALUE self, VALUE enc)
|
4272
|
+
{
|
4273
|
+
rb_check_frozen(self);
|
4274
|
+
if (NIL_P(enc)) {
|
4275
|
+
pgconn_sync_set_client_encoding( self, rb_usascii_str_new_cstr("SQL_ASCII") );
|
4276
|
+
return enc;
|
4277
|
+
}
|
4278
|
+
else if ( TYPE(enc) == T_STRING && strcasecmp("JOHAB", StringValueCStr(enc)) == 0 ) {
|
4279
|
+
pgconn_sync_set_client_encoding(self, rb_usascii_str_new_cstr("JOHAB"));
|
4280
|
+
return enc;
|
4281
|
+
}
|
4282
|
+
else {
|
4283
|
+
rb_encoding *rbenc = rb_to_encoding( enc );
|
4284
|
+
const char *name = pg_get_rb_encoding_as_pg_encoding( rbenc );
|
4285
|
+
|
4286
|
+
if ( gvl_PQsetClientEncoding(pg_get_pgconn( self ), name) == -1 ) {
|
4287
|
+
VALUE server_encoding = pgconn_external_encoding( self );
|
4288
|
+
rb_raise( rb_eEncCompatError, "incompatible character encodings: %s and %s",
|
4289
|
+
rb_enc_name(rb_to_encoding(server_encoding)), name );
|
4290
|
+
}
|
4291
|
+
pgconn_set_internal_encoding_index( self );
|
4292
|
+
return enc;
|
4293
|
+
}
|
4294
|
+
}
|
4295
|
+
|
4296
|
+
|
4297
|
+
|
4298
|
+
/*
|
4299
|
+
* call-seq:
|
4300
|
+
* conn.external_encoding() -> Encoding
|
4301
|
+
*
|
4302
|
+
* Return the +server_encoding+ of the connected database as a Ruby Encoding object.
|
4303
|
+
* The <tt>SQL_ASCII</tt> encoding is mapped to to <tt>ASCII_8BIT</tt>.
|
4304
|
+
*/
|
4305
|
+
static VALUE
|
4306
|
+
pgconn_external_encoding(VALUE self)
|
4307
|
+
{
|
4308
|
+
t_pg_connection *this = pg_get_connection_safe( self );
|
4309
|
+
rb_encoding *enc = NULL;
|
4310
|
+
const char *pg_encname = NULL;
|
4311
|
+
|
4312
|
+
pg_encname = PQparameterStatus( this->pgconn, "server_encoding" );
|
4313
|
+
enc = pg_get_pg_encname_as_rb_encoding( pg_encname );
|
4314
|
+
return rb_enc_from_encoding( enc );
|
4315
|
+
}
|
4316
|
+
|
4317
|
+
/*
|
4318
|
+
* call-seq:
|
4319
|
+
* conn.set_client_encoding( encoding )
|
4320
|
+
*
|
4321
|
+
* Sets the client encoding to the _encoding_ String.
|
4322
|
+
*/
|
4323
|
+
static VALUE
|
4324
|
+
pgconn_async_set_client_encoding(VALUE self, VALUE encname)
|
4325
|
+
{
|
4326
|
+
VALUE query_format, query;
|
4327
|
+
|
4328
|
+
rb_check_frozen(self);
|
4329
|
+
Check_Type(encname, T_STRING);
|
4330
|
+
query_format = rb_str_new_cstr("set client_encoding to '%s'");
|
4331
|
+
query = rb_funcall(query_format, rb_intern("%"), 1, encname);
|
4332
|
+
|
4333
|
+
pgconn_async_exec(1, &query, self);
|
4334
|
+
pgconn_set_internal_encoding_index( self );
|
4335
|
+
|
4336
|
+
return Qnil;
|
4337
|
+
}
|
4338
|
+
|
4339
|
+
static VALUE
|
4340
|
+
pgconn_set_client_encoding_async1( VALUE args )
|
4341
|
+
{
|
4342
|
+
VALUE self = ((VALUE*)args)[0];
|
4343
|
+
VALUE encname = ((VALUE*)args)[1];
|
4344
|
+
pgconn_async_set_client_encoding(self, encname);
|
4345
|
+
return 0;
|
4346
|
+
}
|
4347
|
+
|
4348
|
+
|
4349
|
+
static VALUE
|
4350
|
+
pgconn_set_client_encoding_async2( VALUE arg, VALUE ex )
|
4351
|
+
{
|
4352
|
+
UNUSED(arg);
|
4353
|
+
UNUSED(ex);
|
4354
|
+
return 1;
|
4355
|
+
}
|
4356
|
+
|
4357
|
+
|
4358
|
+
static VALUE
|
4359
|
+
pgconn_set_client_encoding_async( VALUE self, VALUE encname )
|
4360
|
+
{
|
4361
|
+
VALUE args[] = { self, encname };
|
4362
|
+
return rb_rescue(pgconn_set_client_encoding_async1, (VALUE)&args, pgconn_set_client_encoding_async2, Qnil);
|
4363
|
+
}
|
4364
|
+
|
4365
|
+
|
4366
|
+
/*
|
4367
|
+
* call-seq:
|
4368
|
+
* conn.set_default_encoding() -> Encoding
|
4369
|
+
*
|
4370
|
+
* If Ruby has its Encoding.default_internal set, set PostgreSQL's client_encoding
|
4371
|
+
* to match. Returns the new Encoding, or +nil+ if the default internal encoding
|
4372
|
+
* wasn't set.
|
4373
|
+
*/
|
4374
|
+
static VALUE
|
4375
|
+
pgconn_set_default_encoding( VALUE self )
|
4376
|
+
{
|
4377
|
+
PGconn *conn = pg_get_pgconn( self );
|
4378
|
+
rb_encoding *rb_enc;
|
4379
|
+
|
4380
|
+
rb_check_frozen(self);
|
4381
|
+
if (( rb_enc = rb_default_internal_encoding() )) {
|
4382
|
+
rb_encoding * conn_encoding = pg_conn_enc_get( conn );
|
4383
|
+
|
4384
|
+
/* Don't set the server encoding, if it's unnecessary.
|
4385
|
+
* This is important for connection proxies, who disallow configuration settings.
|
4386
|
+
*/
|
4387
|
+
if ( conn_encoding != rb_enc ) {
|
4388
|
+
const char *encname = pg_get_rb_encoding_as_pg_encoding( rb_enc );
|
4389
|
+
if ( pgconn_set_client_encoding_async(self, rb_str_new_cstr(encname)) != 0 )
|
4390
|
+
rb_warning( "Failed to set the default_internal encoding to %s: '%s'",
|
4391
|
+
encname, PQerrorMessage(conn) );
|
4392
|
+
}
|
4393
|
+
pgconn_set_internal_encoding_index( self );
|
4394
|
+
return rb_enc_from_encoding( rb_enc );
|
4395
|
+
} else {
|
4396
|
+
pgconn_set_internal_encoding_index( self );
|
4397
|
+
return Qnil;
|
4398
|
+
}
|
4399
|
+
}
|
4400
|
+
|
4401
|
+
|
4402
|
+
/*
|
4403
|
+
* call-seq:
|
4404
|
+
* res.type_map_for_queries = typemap
|
4405
|
+
*
|
4406
|
+
* Set the default TypeMap that is used for type casts of query bind parameters.
|
4407
|
+
*
|
4408
|
+
* +typemap+ must be a kind of PG::TypeMap .
|
4409
|
+
*
|
4410
|
+
*/
|
4411
|
+
static VALUE
|
4412
|
+
pgconn_type_map_for_queries_set(VALUE self, VALUE typemap)
|
4413
|
+
{
|
4414
|
+
t_pg_connection *this = pg_get_connection( self );
|
4415
|
+
t_typemap *tm;
|
4416
|
+
UNUSED(tm);
|
4417
|
+
|
4418
|
+
rb_check_frozen(self);
|
4419
|
+
/* Check type of method param */
|
4420
|
+
TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, tm);
|
4421
|
+
|
4422
|
+
RB_OBJ_WRITE(self, &this->type_map_for_queries, typemap);
|
4423
|
+
|
4424
|
+
return typemap;
|
4425
|
+
}
|
4426
|
+
|
4427
|
+
/*
|
4428
|
+
* call-seq:
|
4429
|
+
* res.type_map_for_queries -> TypeMap
|
4430
|
+
*
|
4431
|
+
* Returns the default TypeMap that is currently set for type casts of query
|
4432
|
+
* bind parameters.
|
4433
|
+
*
|
4434
|
+
*/
|
4435
|
+
static VALUE
|
4436
|
+
pgconn_type_map_for_queries_get(VALUE self)
|
4437
|
+
{
|
4438
|
+
t_pg_connection *this = pg_get_connection( self );
|
4439
|
+
|
4440
|
+
return this->type_map_for_queries;
|
4441
|
+
}
|
4442
|
+
|
4443
|
+
/*
|
4444
|
+
* call-seq:
|
4445
|
+
* res.type_map_for_results = typemap
|
4446
|
+
*
|
4447
|
+
* Set the default TypeMap that is used for type casts of result values.
|
4448
|
+
*
|
4449
|
+
* +typemap+ must be a kind of PG::TypeMap .
|
4450
|
+
*
|
4451
|
+
*/
|
4452
|
+
static VALUE
|
4453
|
+
pgconn_type_map_for_results_set(VALUE self, VALUE typemap)
|
4454
|
+
{
|
4455
|
+
t_pg_connection *this = pg_get_connection( self );
|
4456
|
+
t_typemap *tm;
|
4457
|
+
UNUSED(tm);
|
4458
|
+
|
4459
|
+
rb_check_frozen(self);
|
4460
|
+
TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, tm);
|
4461
|
+
RB_OBJ_WRITE(self, &this->type_map_for_results, typemap);
|
4462
|
+
|
4463
|
+
return typemap;
|
4464
|
+
}
|
4465
|
+
|
4466
|
+
/*
|
4467
|
+
* call-seq:
|
4468
|
+
* res.type_map_for_results -> TypeMap
|
4469
|
+
*
|
4470
|
+
* Returns the default TypeMap that is currently set for type casts of result values.
|
4471
|
+
*
|
4472
|
+
*/
|
4473
|
+
static VALUE
|
4474
|
+
pgconn_type_map_for_results_get(VALUE self)
|
4475
|
+
{
|
4476
|
+
t_pg_connection *this = pg_get_connection( self );
|
4477
|
+
|
4478
|
+
return this->type_map_for_results;
|
4479
|
+
}
|
4480
|
+
|
4481
|
+
|
4482
|
+
/*
|
4483
|
+
* call-seq:
|
4484
|
+
* res.encoder_for_put_copy_data = encoder
|
4485
|
+
*
|
4486
|
+
* Set the default coder that is used for type casting of parameters
|
4487
|
+
* to #put_copy_data .
|
4488
|
+
*
|
4489
|
+
* +encoder+ can be:
|
4490
|
+
* * a kind of PG::Coder
|
4491
|
+
* * +nil+ - disable type encoding, data must be a String.
|
4492
|
+
*
|
4493
|
+
*/
|
4494
|
+
static VALUE
|
4495
|
+
pgconn_encoder_for_put_copy_data_set(VALUE self, VALUE encoder)
|
4496
|
+
{
|
4497
|
+
t_pg_connection *this = pg_get_connection( self );
|
4498
|
+
|
4499
|
+
rb_check_frozen(self);
|
4500
|
+
if( encoder != Qnil ){
|
4501
|
+
t_pg_coder *co;
|
4502
|
+
UNUSED(co);
|
4503
|
+
/* Check argument type */
|
4504
|
+
TypedData_Get_Struct(encoder, t_pg_coder, &pg_coder_type, co);
|
4505
|
+
}
|
4506
|
+
RB_OBJ_WRITE(self, &this->encoder_for_put_copy_data, encoder);
|
4507
|
+
|
4508
|
+
return encoder;
|
4509
|
+
}
|
4510
|
+
|
4511
|
+
/*
|
4512
|
+
* call-seq:
|
4513
|
+
* res.encoder_for_put_copy_data -> PG::Coder
|
4514
|
+
*
|
4515
|
+
* Returns the default coder object that is currently set for type casting of parameters
|
4516
|
+
* to #put_copy_data .
|
4517
|
+
*
|
4518
|
+
* Returns either:
|
4519
|
+
* * a kind of PG::Coder
|
4520
|
+
* * +nil+ - type encoding is disabled, data must be a String.
|
4521
|
+
*
|
4522
|
+
*/
|
4523
|
+
static VALUE
|
4524
|
+
pgconn_encoder_for_put_copy_data_get(VALUE self)
|
4525
|
+
{
|
4526
|
+
t_pg_connection *this = pg_get_connection( self );
|
4527
|
+
|
4528
|
+
return this->encoder_for_put_copy_data;
|
4529
|
+
}
|
4530
|
+
|
4531
|
+
/*
|
4532
|
+
* call-seq:
|
4533
|
+
* res.decoder_for_get_copy_data = decoder
|
4534
|
+
*
|
4535
|
+
* Set the default coder that is used for type casting of received data
|
4536
|
+
* by #get_copy_data .
|
4537
|
+
*
|
4538
|
+
* +decoder+ can be:
|
4539
|
+
* * a kind of PG::Coder
|
4540
|
+
* * +nil+ - disable type decoding, returned data will be a String.
|
4541
|
+
*
|
4542
|
+
*/
|
4543
|
+
static VALUE
|
4544
|
+
pgconn_decoder_for_get_copy_data_set(VALUE self, VALUE decoder)
|
4545
|
+
{
|
4546
|
+
t_pg_connection *this = pg_get_connection( self );
|
4547
|
+
|
4548
|
+
rb_check_frozen(self);
|
4549
|
+
if( decoder != Qnil ){
|
4550
|
+
t_pg_coder *co;
|
4551
|
+
UNUSED(co);
|
4552
|
+
/* Check argument type */
|
4553
|
+
TypedData_Get_Struct(decoder, t_pg_coder, &pg_coder_type, co);
|
4554
|
+
}
|
4555
|
+
RB_OBJ_WRITE(self, &this->decoder_for_get_copy_data, decoder);
|
4556
|
+
|
4557
|
+
return decoder;
|
4558
|
+
}
|
4559
|
+
|
4560
|
+
/*
|
4561
|
+
* call-seq:
|
4562
|
+
* res.decoder_for_get_copy_data -> PG::Coder
|
4563
|
+
*
|
4564
|
+
* Returns the default coder object that is currently set for type casting of received
|
4565
|
+
* data by #get_copy_data .
|
4566
|
+
*
|
4567
|
+
* Returns either:
|
4568
|
+
* * a kind of PG::Coder
|
4569
|
+
* * +nil+ - type encoding is disabled, returned data will be a String.
|
4570
|
+
*
|
4571
|
+
*/
|
4572
|
+
static VALUE
|
4573
|
+
pgconn_decoder_for_get_copy_data_get(VALUE self)
|
4574
|
+
{
|
4575
|
+
t_pg_connection *this = pg_get_connection( self );
|
4576
|
+
|
4577
|
+
return this->decoder_for_get_copy_data;
|
4578
|
+
}
|
4579
|
+
|
4580
|
+
/*
|
4581
|
+
* call-seq:
|
4582
|
+
* conn.field_name_type = Symbol
|
4583
|
+
*
|
4584
|
+
* Set default type of field names of results retrieved by this connection.
|
4585
|
+
* It can be set to one of:
|
4586
|
+
* * +:string+ to use String based field names
|
4587
|
+
* * +:symbol+ to use Symbol based field names
|
4588
|
+
*
|
4589
|
+
* The default is +:string+ .
|
4590
|
+
*
|
4591
|
+
* Settings the type of field names affects only future results.
|
4592
|
+
*
|
4593
|
+
* See further description at PG::Result#field_name_type=
|
4594
|
+
*
|
4595
|
+
*/
|
4596
|
+
static VALUE
|
4597
|
+
pgconn_field_name_type_set(VALUE self, VALUE sym)
|
4598
|
+
{
|
4599
|
+
t_pg_connection *this = pg_get_connection( self );
|
4600
|
+
|
4601
|
+
rb_check_frozen(self);
|
4602
|
+
this->flags &= ~PG_RESULT_FIELD_NAMES_MASK;
|
4603
|
+
if( sym == sym_symbol ) this->flags |= PG_RESULT_FIELD_NAMES_SYMBOL;
|
4604
|
+
else if ( sym == sym_static_symbol ) this->flags |= PG_RESULT_FIELD_NAMES_STATIC_SYMBOL;
|
4605
|
+
else if ( sym == sym_string );
|
4606
|
+
else rb_raise(rb_eArgError, "invalid argument %+"PRIsVALUE, sym);
|
4607
|
+
|
4608
|
+
return sym;
|
4609
|
+
}
|
4610
|
+
|
4611
|
+
/*
|
4612
|
+
* call-seq:
|
4613
|
+
* conn.field_name_type -> Symbol
|
4614
|
+
*
|
4615
|
+
* Get type of field names.
|
4616
|
+
*
|
4617
|
+
* See description at #field_name_type=
|
4618
|
+
*/
|
4619
|
+
static VALUE
|
4620
|
+
pgconn_field_name_type_get(VALUE self)
|
4621
|
+
{
|
4622
|
+
t_pg_connection *this = pg_get_connection( self );
|
4623
|
+
|
4624
|
+
if( this->flags & PG_RESULT_FIELD_NAMES_SYMBOL ){
|
4625
|
+
return sym_symbol;
|
4626
|
+
} else if( this->flags & PG_RESULT_FIELD_NAMES_STATIC_SYMBOL ){
|
4627
|
+
return sym_static_symbol;
|
4628
|
+
} else {
|
4629
|
+
return sym_string;
|
4630
|
+
}
|
4631
|
+
}
|
4632
|
+
|
4633
|
+
|
4634
|
+
/*
|
4635
|
+
* Document-class: PG::Connection
|
4636
|
+
*/
|
4637
|
+
void
|
4638
|
+
init_pg_connection(void)
|
4639
|
+
{
|
4640
|
+
s_id_encode = rb_intern("encode");
|
4641
|
+
s_id_autoclose_set = rb_intern("autoclose=");
|
4642
|
+
sym_type = ID2SYM(rb_intern("type"));
|
4643
|
+
sym_format = ID2SYM(rb_intern("format"));
|
4644
|
+
sym_value = ID2SYM(rb_intern("value"));
|
4645
|
+
sym_string = ID2SYM(rb_intern("string"));
|
4646
|
+
sym_symbol = ID2SYM(rb_intern("symbol"));
|
4647
|
+
sym_static_symbol = ID2SYM(rb_intern("static_symbol"));
|
4648
|
+
|
4649
|
+
rb_cPGconn = rb_define_class_under( rb_mPG, "Connection", rb_cObject );
|
4650
|
+
/* Help rdoc to known the Constants module */
|
4651
|
+
/* rb_mPGconstants = rb_define_module_under( rb_mPG, "Constants" ); */
|
4652
|
+
rb_include_module(rb_cPGconn, rb_mPGconstants);
|
4653
|
+
|
4654
|
+
/****** PG::Connection CLASS METHODS ******/
|
4655
|
+
rb_define_alloc_func( rb_cPGconn, pgconn_s_allocate );
|
4656
|
+
|
4657
|
+
rb_define_singleton_method(rb_cPGconn, "escape_string", pgconn_s_escape, 1);
|
4658
|
+
SINGLETON_ALIAS(rb_cPGconn, "escape", "escape_string");
|
4659
|
+
rb_define_singleton_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
|
4660
|
+
rb_define_singleton_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
|
4661
|
+
rb_define_singleton_method(rb_cPGconn, "encrypt_password", pgconn_s_encrypt_password, 2);
|
4662
|
+
rb_define_singleton_method(rb_cPGconn, "quote_ident", pgconn_s_quote_ident, 1);
|
4663
|
+
rb_define_singleton_method(rb_cPGconn, "connect_start", pgconn_s_connect_start, -1);
|
4664
|
+
rb_define_singleton_method(rb_cPGconn, "conndefaults", pgconn_s_conndefaults, 0);
|
4665
|
+
rb_define_singleton_method(rb_cPGconn, "conninfo_parse", pgconn_s_conninfo_parse, 1);
|
4666
|
+
rb_define_singleton_method(rb_cPGconn, "sync_ping", pgconn_s_sync_ping, -1);
|
4667
|
+
rb_define_singleton_method(rb_cPGconn, "sync_connect", pgconn_s_sync_connect, -1);
|
4668
|
+
|
4669
|
+
/****** PG::Connection INSTANCE METHODS: Connection Control ******/
|
4670
|
+
rb_define_method(rb_cPGconn, "connect_poll", pgconn_connect_poll, 0);
|
4671
|
+
rb_define_method(rb_cPGconn, "finish", pgconn_finish, 0);
|
4672
|
+
rb_define_method(rb_cPGconn, "finished?", pgconn_finished_p, 0);
|
4673
|
+
rb_define_method(rb_cPGconn, "sync_reset", pgconn_sync_reset, 0);
|
4674
|
+
rb_define_method(rb_cPGconn, "reset_start", pgconn_reset_start, 0);
|
4675
|
+
rb_define_private_method(rb_cPGconn, "reset_start2", pgconn_reset_start2, 1);
|
4676
|
+
rb_define_method(rb_cPGconn, "reset_poll", pgconn_reset_poll, 0);
|
4677
|
+
rb_define_alias(rb_cPGconn, "close", "finish");
|
4678
|
+
|
4679
|
+
/****** PG::Connection INSTANCE METHODS: Connection Status ******/
|
4680
|
+
rb_define_method(rb_cPGconn, "db", pgconn_db, 0);
|
4681
|
+
rb_define_method(rb_cPGconn, "user", pgconn_user, 0);
|
4682
|
+
rb_define_method(rb_cPGconn, "pass", pgconn_pass, 0);
|
4683
|
+
rb_define_method(rb_cPGconn, "host", pgconn_host, 0);
|
4684
|
+
#if defined(HAVE_PQRESULTMEMORYSIZE)
|
4685
|
+
rb_define_method(rb_cPGconn, "hostaddr", pgconn_hostaddr, 0);
|
4686
|
+
#endif
|
4687
|
+
rb_define_method(rb_cPGconn, "port", pgconn_port, 0);
|
4688
|
+
rb_define_method(rb_cPGconn, "tty", pgconn_tty, 0);
|
4689
|
+
rb_define_method(rb_cPGconn, "conninfo", pgconn_conninfo, 0);
|
4690
|
+
rb_define_method(rb_cPGconn, "options", pgconn_options, 0);
|
4691
|
+
rb_define_method(rb_cPGconn, "status", pgconn_status, 0);
|
4692
|
+
rb_define_method(rb_cPGconn, "transaction_status", pgconn_transaction_status, 0);
|
4693
|
+
rb_define_method(rb_cPGconn, "parameter_status", pgconn_parameter_status, 1);
|
4694
|
+
rb_define_method(rb_cPGconn, "protocol_version", pgconn_protocol_version, 0);
|
4695
|
+
rb_define_method(rb_cPGconn, "server_version", pgconn_server_version, 0);
|
4696
|
+
rb_define_method(rb_cPGconn, "error_message", pgconn_error_message, 0);
|
4697
|
+
rb_define_method(rb_cPGconn, "socket", pgconn_socket, 0);
|
4698
|
+
rb_define_method(rb_cPGconn, "socket_io", pgconn_socket_io, 0);
|
4699
|
+
rb_define_method(rb_cPGconn, "backend_pid", pgconn_backend_pid, 0);
|
4700
|
+
#ifndef HAVE_PQSETCHUNKEDROWSMODE
|
4701
|
+
rb_define_method(rb_cPGconn, "backend_key", pgconn_backend_key, 0);
|
4702
|
+
#endif
|
4703
|
+
rb_define_method(rb_cPGconn, "connection_needs_password", pgconn_connection_needs_password, 0);
|
4704
|
+
rb_define_method(rb_cPGconn, "connection_used_password", pgconn_connection_used_password, 0);
|
4705
|
+
/* rb_define_method(rb_cPGconn, "getssl", pgconn_getssl, 0); */
|
4706
|
+
|
4707
|
+
/****** PG::Connection INSTANCE METHODS: Command Execution ******/
|
4708
|
+
rb_define_method(rb_cPGconn, "sync_exec", pgconn_sync_exec, -1);
|
4709
|
+
rb_define_method(rb_cPGconn, "sync_exec_params", pgconn_sync_exec_params, -1);
|
4710
|
+
rb_define_method(rb_cPGconn, "sync_prepare", pgconn_sync_prepare, -1);
|
4711
|
+
rb_define_method(rb_cPGconn, "sync_exec_prepared", pgconn_sync_exec_prepared, -1);
|
4712
|
+
rb_define_method(rb_cPGconn, "sync_describe_prepared", pgconn_sync_describe_prepared, 1);
|
4713
|
+
rb_define_method(rb_cPGconn, "sync_describe_portal", pgconn_sync_describe_portal, 1);
|
4714
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
4715
|
+
rb_define_method(rb_cPGconn, "sync_close_prepared", pgconn_sync_close_prepared, 1);
|
4716
|
+
rb_define_method(rb_cPGconn, "sync_close_portal", pgconn_sync_close_portal, 1);
|
4717
|
+
#endif
|
4718
|
+
|
4719
|
+
rb_define_method(rb_cPGconn, "exec", pgconn_async_exec, -1);
|
4720
|
+
rb_define_method(rb_cPGconn, "exec_params", pgconn_async_exec_params, -1);
|
4721
|
+
rb_define_method(rb_cPGconn, "prepare", pgconn_async_prepare, -1);
|
4722
|
+
rb_define_method(rb_cPGconn, "exec_prepared", pgconn_async_exec_prepared, -1);
|
4723
|
+
rb_define_method(rb_cPGconn, "describe_prepared", pgconn_async_describe_prepared, 1);
|
4724
|
+
rb_define_method(rb_cPGconn, "describe_portal", pgconn_async_describe_portal, 1);
|
4725
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
4726
|
+
rb_define_method(rb_cPGconn, "close_prepared", pgconn_async_close_prepared, 1);
|
4727
|
+
rb_define_method(rb_cPGconn, "close_portal", pgconn_async_close_portal, 1);
|
4728
|
+
#endif
|
4729
|
+
|
4730
|
+
rb_define_alias(rb_cPGconn, "async_exec", "exec");
|
4731
|
+
rb_define_alias(rb_cPGconn, "async_query", "async_exec");
|
4732
|
+
rb_define_alias(rb_cPGconn, "async_exec_params", "exec_params");
|
4733
|
+
rb_define_alias(rb_cPGconn, "async_prepare", "prepare");
|
4734
|
+
rb_define_alias(rb_cPGconn, "async_exec_prepared", "exec_prepared");
|
4735
|
+
rb_define_alias(rb_cPGconn, "async_describe_prepared", "describe_prepared");
|
4736
|
+
rb_define_alias(rb_cPGconn, "async_describe_portal", "describe_portal");
|
4737
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
4738
|
+
rb_define_alias(rb_cPGconn, "async_close_prepared", "close_prepared");
|
4739
|
+
rb_define_alias(rb_cPGconn, "async_close_portal", "close_portal");
|
4740
|
+
#endif
|
4741
|
+
|
4742
|
+
rb_define_method(rb_cPGconn, "make_empty_pgresult", pgconn_make_empty_pgresult, 1);
|
4743
|
+
rb_define_method(rb_cPGconn, "escape_string", pgconn_s_escape, 1);
|
4744
|
+
rb_define_alias(rb_cPGconn, "escape", "escape_string");
|
4745
|
+
rb_define_method(rb_cPGconn, "escape_literal", pgconn_escape_literal, 1);
|
4746
|
+
rb_define_method(rb_cPGconn, "escape_identifier", pgconn_escape_identifier, 1);
|
4747
|
+
rb_define_method(rb_cPGconn, "escape_bytea", pgconn_s_escape_bytea, 1);
|
4748
|
+
rb_define_method(rb_cPGconn, "unescape_bytea", pgconn_s_unescape_bytea, 1);
|
4749
|
+
rb_define_method(rb_cPGconn, "set_single_row_mode", pgconn_set_single_row_mode, 0);
|
4750
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
4751
|
+
rb_define_method(rb_cPGconn, "set_chunked_rows_mode", pgconn_set_chunked_rows_mode, 1);
|
4752
|
+
#endif
|
4753
|
+
|
4754
|
+
/****** PG::Connection INSTANCE METHODS: Asynchronous Command Processing ******/
|
4755
|
+
rb_define_method(rb_cPGconn, "send_query", pgconn_send_query, -1);
|
4756
|
+
rb_define_method(rb_cPGconn, "send_query_params", pgconn_send_query_params, -1);
|
4757
|
+
rb_define_method(rb_cPGconn, "send_prepare", pgconn_send_prepare, -1);
|
4758
|
+
rb_define_method(rb_cPGconn, "send_query_prepared", pgconn_send_query_prepared, -1);
|
4759
|
+
rb_define_method(rb_cPGconn, "send_describe_prepared", pgconn_send_describe_prepared, 1);
|
4760
|
+
rb_define_method(rb_cPGconn, "send_describe_portal", pgconn_send_describe_portal, 1);
|
4761
|
+
rb_define_method(rb_cPGconn, "sync_get_result", pgconn_sync_get_result, 0);
|
4762
|
+
rb_define_method(rb_cPGconn, "consume_input", pgconn_consume_input, 0);
|
4763
|
+
rb_define_method(rb_cPGconn, "is_busy", pgconn_is_busy, 0);
|
4764
|
+
rb_define_method(rb_cPGconn, "sync_setnonblocking", pgconn_sync_setnonblocking, 1);
|
4765
|
+
rb_define_method(rb_cPGconn, "sync_isnonblocking", pgconn_sync_isnonblocking, 0);
|
4766
|
+
rb_define_method(rb_cPGconn, "sync_flush", pgconn_sync_flush, 0);
|
4767
|
+
rb_define_method(rb_cPGconn, "flush", pgconn_async_flush, 0);
|
4768
|
+
rb_define_alias(rb_cPGconn, "async_flush", "flush");
|
4769
|
+
rb_define_method(rb_cPGconn, "discard_results", pgconn_discard_results, 0);
|
4770
|
+
|
4771
|
+
/****** PG::Connection INSTANCE METHODS: Cancelling Queries in Progress ******/
|
4772
|
+
#ifndef HAVE_PQSETCHUNKEDROWSMODE
|
4773
|
+
rb_define_method(rb_cPGconn, "sync_cancel", pgconn_sync_cancel, 0);
|
4774
|
+
#endif
|
4775
|
+
|
4776
|
+
/****** PG::Connection INSTANCE METHODS: NOTIFY ******/
|
4777
|
+
rb_define_method(rb_cPGconn, "notifies", pgconn_notifies, 0);
|
4778
|
+
|
4779
|
+
/****** PG::Connection INSTANCE METHODS: COPY ******/
|
4780
|
+
rb_define_method(rb_cPGconn, "sync_put_copy_data", pgconn_sync_put_copy_data, -1);
|
4781
|
+
rb_define_method(rb_cPGconn, "sync_put_copy_end", pgconn_sync_put_copy_end, -1);
|
4782
|
+
rb_define_method(rb_cPGconn, "sync_get_copy_data", pgconn_sync_get_copy_data, -1);
|
4783
|
+
|
4784
|
+
/****** PG::Connection INSTANCE METHODS: Control Functions ******/
|
4785
|
+
rb_define_method(rb_cPGconn, "set_error_verbosity", pgconn_set_error_verbosity, 1);
|
4786
|
+
rb_define_method(rb_cPGconn, "set_error_context_visibility", pgconn_set_error_context_visibility, 1 );
|
4787
|
+
rb_define_method(rb_cPGconn, "trace", pgconn_trace, 1);
|
4788
|
+
rb_define_method(rb_cPGconn, "untrace", pgconn_untrace, 0);
|
4789
|
+
|
4790
|
+
/****** PG::Connection INSTANCE METHODS: Notice Processing ******/
|
4791
|
+
rb_define_method(rb_cPGconn, "set_notice_receiver", pgconn_set_notice_receiver, 0);
|
4792
|
+
rb_define_method(rb_cPGconn, "set_notice_processor", pgconn_set_notice_processor, 0);
|
4793
|
+
|
4794
|
+
/****** PG::Connection INSTANCE METHODS: Other ******/
|
4795
|
+
rb_define_method(rb_cPGconn, "get_client_encoding", pgconn_get_client_encoding, 0);
|
4796
|
+
rb_define_method(rb_cPGconn, "sync_set_client_encoding", pgconn_sync_set_client_encoding, 1);
|
4797
|
+
rb_define_method(rb_cPGconn, "set_client_encoding", pgconn_async_set_client_encoding, 1);
|
4798
|
+
rb_define_alias(rb_cPGconn, "async_set_client_encoding", "set_client_encoding");
|
4799
|
+
rb_define_alias(rb_cPGconn, "client_encoding=", "set_client_encoding");
|
4800
|
+
rb_define_method(rb_cPGconn, "block", pgconn_block, -1);
|
4801
|
+
rb_define_private_method(rb_cPGconn, "flush_data=", pgconn_flush_data_set, 1);
|
4802
|
+
rb_define_method(rb_cPGconn, "wait_for_notify", pgconn_wait_for_notify, -1);
|
4803
|
+
rb_define_alias(rb_cPGconn, "notifies_wait", "wait_for_notify");
|
4804
|
+
rb_define_method(rb_cPGconn, "quote_ident", pgconn_s_quote_ident, 1);
|
4805
|
+
rb_define_method(rb_cPGconn, "sync_get_last_result", pgconn_sync_get_last_result, 0);
|
4806
|
+
rb_define_method(rb_cPGconn, "get_last_result", pgconn_async_get_last_result, 0);
|
4807
|
+
rb_define_alias(rb_cPGconn, "async_get_last_result", "get_last_result");
|
4808
|
+
rb_define_method(rb_cPGconn, "sync_encrypt_password", pgconn_sync_encrypt_password, -1);
|
4809
|
+
|
4810
|
+
rb_define_method(rb_cPGconn, "ssl_in_use?", pgconn_ssl_in_use, 0);
|
4811
|
+
rb_define_method(rb_cPGconn, "ssl_attribute", pgconn_ssl_attribute, 1);
|
4812
|
+
rb_define_method(rb_cPGconn, "ssl_attribute_names", pgconn_ssl_attribute_names, 0);
|
4813
|
+
|
4814
|
+
#ifdef HAVE_PQENTERPIPELINEMODE
|
4815
|
+
rb_define_method(rb_cPGconn, "pipeline_status", pgconn_pipeline_status, 0);
|
4816
|
+
rb_define_method(rb_cPGconn, "enter_pipeline_mode", pgconn_enter_pipeline_mode, 0);
|
4817
|
+
rb_define_method(rb_cPGconn, "exit_pipeline_mode", pgconn_exit_pipeline_mode, 0);
|
4818
|
+
rb_define_method(rb_cPGconn, "sync_pipeline_sync", pgconn_sync_pipeline_sync, 0);
|
4819
|
+
rb_define_method(rb_cPGconn, "send_flush_request", pgconn_send_flush_request, 0);
|
4820
|
+
#ifdef HAVE_PQSETCHUNKEDROWSMODE
|
4821
|
+
rb_define_method(rb_cPGconn, "send_pipeline_sync", pgconn_send_pipeline_sync, 0);
|
4822
|
+
#endif
|
4823
|
+
#endif
|
4824
|
+
|
4825
|
+
/****** PG::Connection INSTANCE METHODS: Large Object Support ******/
|
4826
|
+
rb_define_method(rb_cPGconn, "lo_creat", pgconn_locreat, -1);
|
4827
|
+
rb_define_alias(rb_cPGconn, "locreat", "lo_creat");
|
4828
|
+
rb_define_method(rb_cPGconn, "lo_create", pgconn_locreate, 1);
|
4829
|
+
rb_define_alias(rb_cPGconn, "locreate", "lo_create");
|
4830
|
+
rb_define_method(rb_cPGconn, "lo_import", pgconn_loimport, 1);
|
4831
|
+
rb_define_alias(rb_cPGconn, "loimport", "lo_import");
|
4832
|
+
rb_define_method(rb_cPGconn, "lo_export", pgconn_loexport, 2);
|
4833
|
+
rb_define_alias(rb_cPGconn, "loexport", "lo_export");
|
4834
|
+
rb_define_method(rb_cPGconn, "lo_open", pgconn_loopen, -1);
|
4835
|
+
rb_define_alias(rb_cPGconn, "loopen", "lo_open");
|
4836
|
+
rb_define_method(rb_cPGconn, "lo_write",pgconn_lowrite, 2);
|
4837
|
+
rb_define_alias(rb_cPGconn, "lowrite", "lo_write");
|
4838
|
+
rb_define_method(rb_cPGconn, "lo_read",pgconn_loread, 2);
|
4839
|
+
rb_define_alias(rb_cPGconn, "loread", "lo_read");
|
4840
|
+
rb_define_method(rb_cPGconn, "lo_lseek",pgconn_lolseek, 3);
|
4841
|
+
rb_define_alias(rb_cPGconn, "lolseek", "lo_lseek");
|
4842
|
+
rb_define_alias(rb_cPGconn, "lo_seek", "lo_lseek");
|
4843
|
+
rb_define_alias(rb_cPGconn, "loseek", "lo_lseek");
|
4844
|
+
rb_define_method(rb_cPGconn, "lo_tell",pgconn_lotell, 1);
|
4845
|
+
rb_define_alias(rb_cPGconn, "lotell", "lo_tell");
|
4846
|
+
rb_define_method(rb_cPGconn, "lo_truncate", pgconn_lotruncate, 2);
|
4847
|
+
rb_define_alias(rb_cPGconn, "lotruncate", "lo_truncate");
|
4848
|
+
rb_define_method(rb_cPGconn, "lo_close",pgconn_loclose, 1);
|
4849
|
+
rb_define_alias(rb_cPGconn, "loclose", "lo_close");
|
4850
|
+
rb_define_method(rb_cPGconn, "lo_unlink", pgconn_lounlink, 1);
|
4851
|
+
rb_define_alias(rb_cPGconn, "lounlink", "lo_unlink");
|
4852
|
+
|
4853
|
+
rb_define_method(rb_cPGconn, "internal_encoding", pgconn_internal_encoding, 0);
|
4854
|
+
rb_define_method(rb_cPGconn, "internal_encoding=", pgconn_internal_encoding_set, 1);
|
4855
|
+
rb_define_method(rb_cPGconn, "external_encoding", pgconn_external_encoding, 0);
|
4856
|
+
rb_define_method(rb_cPGconn, "set_default_encoding", pgconn_set_default_encoding, 0);
|
4857
|
+
|
4858
|
+
rb_define_method(rb_cPGconn, "type_map_for_queries=", pgconn_type_map_for_queries_set, 1);
|
4859
|
+
rb_define_method(rb_cPGconn, "type_map_for_queries", pgconn_type_map_for_queries_get, 0);
|
4860
|
+
rb_define_method(rb_cPGconn, "type_map_for_results=", pgconn_type_map_for_results_set, 1);
|
4861
|
+
rb_define_method(rb_cPGconn, "type_map_for_results", pgconn_type_map_for_results_get, 0);
|
4862
|
+
rb_define_method(rb_cPGconn, "encoder_for_put_copy_data=", pgconn_encoder_for_put_copy_data_set, 1);
|
4863
|
+
rb_define_method(rb_cPGconn, "encoder_for_put_copy_data", pgconn_encoder_for_put_copy_data_get, 0);
|
4864
|
+
rb_define_method(rb_cPGconn, "decoder_for_get_copy_data=", pgconn_decoder_for_get_copy_data_set, 1);
|
4865
|
+
rb_define_method(rb_cPGconn, "decoder_for_get_copy_data", pgconn_decoder_for_get_copy_data_get, 0);
|
4866
|
+
|
4867
|
+
rb_define_method(rb_cPGconn, "field_name_type=", pgconn_field_name_type_set, 1 );
|
4868
|
+
rb_define_method(rb_cPGconn, "field_name_type", pgconn_field_name_type_get, 0 );
|
4869
|
+
}
|