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