pg 1.6.0.rc1-x86_64-linux

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +4 -0
  3. data/BSDL +22 -0
  4. data/Contributors.rdoc +46 -0
  5. data/Gemfile +23 -0
  6. data/History.md +958 -0
  7. data/LICENSE +56 -0
  8. data/Manifest.txt +72 -0
  9. data/POSTGRES +23 -0
  10. data/README-OS_X.rdoc +68 -0
  11. data/README-Windows.rdoc +56 -0
  12. data/README.ja.md +300 -0
  13. data/README.md +286 -0
  14. data/Rakefile +161 -0
  15. data/certs/ged.pem +24 -0
  16. data/certs/kanis@comcard.de.pem +20 -0
  17. data/certs/larskanis-2022.pem +26 -0
  18. data/certs/larskanis-2023.pem +24 -0
  19. data/certs/larskanis-2024.pem +24 -0
  20. data/ext/errorcodes.def +1043 -0
  21. data/ext/errorcodes.rb +45 -0
  22. data/ext/errorcodes.txt +494 -0
  23. data/ext/extconf.rb +282 -0
  24. data/ext/gvl_wrappers.c +32 -0
  25. data/ext/gvl_wrappers.h +297 -0
  26. data/ext/pg.c +703 -0
  27. data/ext/pg.h +390 -0
  28. data/ext/pg_binary_decoder.c +460 -0
  29. data/ext/pg_binary_encoder.c +583 -0
  30. data/ext/pg_cancel_connection.c +360 -0
  31. data/ext/pg_coder.c +622 -0
  32. data/ext/pg_connection.c +4869 -0
  33. data/ext/pg_copy_coder.c +921 -0
  34. data/ext/pg_errors.c +95 -0
  35. data/ext/pg_record_coder.c +522 -0
  36. data/ext/pg_result.c +1764 -0
  37. data/ext/pg_text_decoder.c +1008 -0
  38. data/ext/pg_text_encoder.c +833 -0
  39. data/ext/pg_tuple.c +572 -0
  40. data/ext/pg_type_map.c +200 -0
  41. data/ext/pg_type_map_all_strings.c +130 -0
  42. data/ext/pg_type_map_by_class.c +271 -0
  43. data/ext/pg_type_map_by_column.c +355 -0
  44. data/ext/pg_type_map_by_mri_type.c +313 -0
  45. data/ext/pg_type_map_by_oid.c +388 -0
  46. data/ext/pg_type_map_in_ruby.c +333 -0
  47. data/ext/pg_util.c +149 -0
  48. data/ext/pg_util.h +65 -0
  49. data/ext/vc/pg.sln +26 -0
  50. data/ext/vc/pg_18/pg.vcproj +216 -0
  51. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  52. data/lib/2.7/pg_ext.so +0 -0
  53. data/lib/3.0/pg_ext.so +0 -0
  54. data/lib/3.1/pg_ext.so +0 -0
  55. data/lib/3.2/pg_ext.so +0 -0
  56. data/lib/3.3/pg_ext.so +0 -0
  57. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  58. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  59. data/lib/pg/basic_type_map_for_results.rb +104 -0
  60. data/lib/pg/basic_type_registry.rb +311 -0
  61. data/lib/pg/binary_decoder/date.rb +9 -0
  62. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  63. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  64. data/lib/pg/cancel_connection.rb +30 -0
  65. data/lib/pg/coder.rb +106 -0
  66. data/lib/pg/connection.rb +1027 -0
  67. data/lib/pg/exceptions.rb +31 -0
  68. data/lib/pg/result.rb +43 -0
  69. data/lib/pg/text_decoder/date.rb +21 -0
  70. data/lib/pg/text_decoder/inet.rb +9 -0
  71. data/lib/pg/text_decoder/json.rb +17 -0
  72. data/lib/pg/text_decoder/numeric.rb +9 -0
  73. data/lib/pg/text_decoder/timestamp.rb +30 -0
  74. data/lib/pg/text_encoder/date.rb +13 -0
  75. data/lib/pg/text_encoder/inet.rb +31 -0
  76. data/lib/pg/text_encoder/json.rb +17 -0
  77. data/lib/pg/text_encoder/numeric.rb +9 -0
  78. data/lib/pg/text_encoder/timestamp.rb +24 -0
  79. data/lib/pg/tuple.rb +30 -0
  80. data/lib/pg/type_map_by_column.rb +16 -0
  81. data/lib/pg/version.rb +4 -0
  82. data/lib/pg.rb +144 -0
  83. data/misc/openssl-pg-segfault.rb +31 -0
  84. data/misc/postgres/History.txt +9 -0
  85. data/misc/postgres/Manifest.txt +5 -0
  86. data/misc/postgres/README.txt +21 -0
  87. data/misc/postgres/Rakefile +21 -0
  88. data/misc/postgres/lib/postgres.rb +16 -0
  89. data/misc/ruby-pg/History.txt +9 -0
  90. data/misc/ruby-pg/Manifest.txt +5 -0
  91. data/misc/ruby-pg/README.txt +21 -0
  92. data/misc/ruby-pg/Rakefile +21 -0
  93. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  94. data/pg.gemspec +36 -0
  95. data/ports/x86_64-linux/lib/libpq-ruby-pg.so.1 +0 -0
  96. data/rakelib/task_extension.rb +46 -0
  97. data/sample/array_insert.rb +20 -0
  98. data/sample/async_api.rb +102 -0
  99. data/sample/async_copyto.rb +39 -0
  100. data/sample/async_mixed.rb +56 -0
  101. data/sample/check_conn.rb +21 -0
  102. data/sample/copydata.rb +71 -0
  103. data/sample/copyfrom.rb +81 -0
  104. data/sample/copyto.rb +19 -0
  105. data/sample/cursor.rb +21 -0
  106. data/sample/disk_usage_report.rb +177 -0
  107. data/sample/issue-119.rb +94 -0
  108. data/sample/losample.rb +69 -0
  109. data/sample/minimal-testcase.rb +17 -0
  110. data/sample/notify_wait.rb +72 -0
  111. data/sample/pg_statistics.rb +285 -0
  112. data/sample/replication_monitor.rb +222 -0
  113. data/sample/test_binary_values.rb +33 -0
  114. data/sample/wal_shipper.rb +434 -0
  115. data/sample/warehouse_partitions.rb +311 -0
  116. data.tar.gz.sig +0 -0
  117. metadata +252 -0
  118. metadata.gz.sig +0 -0
data/ext/pg_result.c ADDED
@@ -0,0 +1,1764 @@
1
+ /*
2
+ * pg_result.c - PG::Result class extension
3
+ * $Id$
4
+ *
5
+ */
6
+
7
+ #include "pg.h"
8
+
9
+ VALUE rb_cPGresult;
10
+ static VALUE sym_symbol, sym_string, sym_static_symbol;
11
+
12
+ static VALUE pgresult_type_map_set( VALUE, VALUE );
13
+ static t_pg_result *pgresult_get_this( VALUE );
14
+ static t_pg_result *pgresult_get_this_safe( VALUE );
15
+
16
+ #if defined(HAVE_PQRESULTMEMORYSIZE)
17
+
18
+ static ssize_t
19
+ pgresult_approx_size(const PGresult *result)
20
+ {
21
+ return PQresultMemorySize(result);
22
+ }
23
+
24
+ #else
25
+
26
+ #define PGRESULT_DATA_BLOCKSIZE 2048
27
+ typedef struct pgresAttValue
28
+ {
29
+ int len; /* length in bytes of the value */
30
+ char *value; /* actual value, plus terminating zero byte */
31
+ } PGresAttValue;
32
+
33
+
34
+ static int
35
+ count_leading_zero_bits(unsigned int x)
36
+ {
37
+ #if defined(__GNUC__) || defined(__clang__)
38
+ return __builtin_clz(x);
39
+ #elif defined(_MSC_VER)
40
+ DWORD r = 0;
41
+ _BitScanForward(&r, x);
42
+ return (int)r;
43
+ #else
44
+ unsigned int a;
45
+ for(a=0; a < sizeof(unsigned int) * 8; a++){
46
+ if( x & (1 << (sizeof(unsigned int) * 8 - 1))) return a;
47
+ x <<= 1;
48
+ }
49
+ return a;
50
+ #endif
51
+ }
52
+
53
+ static ssize_t
54
+ pgresult_approx_size(const PGresult *result)
55
+ {
56
+ int num_fields = PQnfields(result);
57
+ ssize_t size = 0;
58
+
59
+ if( num_fields > 0 ){
60
+ int num_tuples = PQntuples(result);
61
+
62
+ if( num_tuples > 0 ){
63
+ int pos;
64
+
65
+ /* This is a simple heuristic to determine the number of sample fields and subsequently to approximate the memory size taken by all field values of the result set.
66
+ * Since scanning of all field values is would have a severe performance impact, only a small subset of fields is retrieved and the result is extrapolated to the whole result set.
67
+ * The given algorithm has no real scientific background, but is made for speed and typical table layouts.
68
+ */
69
+ int num_samples =
70
+ (num_fields < 9 ? num_fields : 39 - count_leading_zero_bits(num_fields-8)) *
71
+ (num_tuples < 8 ? 1 : 30 - count_leading_zero_bits(num_tuples));
72
+
73
+ /* start with scanning very last fields, since they are most probably in the cache */
74
+ for( pos = 0; pos < (num_samples+1)/2; pos++ ){
75
+ size += PQgetlength(result, num_tuples - 1 - (pos / num_fields), num_fields - 1 - (pos % num_fields));
76
+ }
77
+ /* scan the very first fields */
78
+ for( pos = 0; pos < num_samples/2; pos++ ){
79
+ size += PQgetlength(result, pos / num_fields, pos % num_fields);
80
+ }
81
+ /* extrapolate sample size to whole result set */
82
+ size = size * num_tuples * num_fields / num_samples;
83
+ }
84
+
85
+ /* count metadata */
86
+ size += num_fields * (
87
+ sizeof(PGresAttDesc) + /* column description */
88
+ num_tuples * (
89
+ sizeof(PGresAttValue) + 1 /* ptr, len and zero termination of each value */
90
+ )
91
+ );
92
+
93
+ /* Account free space due to libpq's default block size */
94
+ size = (size + PGRESULT_DATA_BLOCKSIZE - 1) / PGRESULT_DATA_BLOCKSIZE * PGRESULT_DATA_BLOCKSIZE;
95
+
96
+ /* count tuple pointers */
97
+ size += sizeof(void*) * ((num_tuples + 128 - 1) / 128 * 128);
98
+ }
99
+
100
+ size += 216; /* add PGresult size */
101
+
102
+ return size;
103
+ }
104
+ #endif
105
+
106
+ /*
107
+ * GC Mark function
108
+ */
109
+ static void
110
+ pgresult_gc_mark( void *_this )
111
+ {
112
+ t_pg_result *this = (t_pg_result *)_this;
113
+ int i;
114
+
115
+ rb_gc_mark_movable( this->connection );
116
+ rb_gc_mark_movable( this->typemap );
117
+ rb_gc_mark_movable( this->tuple_hash );
118
+ rb_gc_mark_movable( this->field_map );
119
+
120
+ for( i=0; i < this->nfields; i++ ){
121
+ rb_gc_mark_movable( this->fnames[i] );
122
+ }
123
+ }
124
+
125
+ static void
126
+ pgresult_gc_compact( void *_this )
127
+ {
128
+ t_pg_result *this = (t_pg_result *)_this;
129
+ int i;
130
+
131
+ pg_gc_location( this->connection );
132
+ pg_gc_location( this->typemap );
133
+ pg_gc_location( this->tuple_hash );
134
+ pg_gc_location( this->field_map );
135
+
136
+ for( i=0; i < this->nfields; i++ ){
137
+ pg_gc_location( this->fnames[i] );
138
+ }
139
+ }
140
+
141
+ /*
142
+ * GC Free function
143
+ */
144
+ static void
145
+ pgresult_clear( void *_this )
146
+ {
147
+ t_pg_result *this = (t_pg_result *)_this;
148
+ if( this->pgresult && !this->autoclear ){
149
+ PQclear(this->pgresult);
150
+ rb_gc_adjust_memory_usage(-this->result_size);
151
+ }
152
+ this->result_size = 0;
153
+ this->nfields = -1;
154
+ this->pgresult = NULL;
155
+ }
156
+
157
+ static void
158
+ pgresult_gc_free( void *_this )
159
+ {
160
+ t_pg_result *this = (t_pg_result *)_this;
161
+ pgresult_clear( this );
162
+ xfree(this);
163
+ }
164
+
165
+ static size_t
166
+ pgresult_memsize( const void *_this )
167
+ {
168
+ const t_pg_result *this = (const t_pg_result *)_this;
169
+ /* Ideally the memory 'this' is pointing to should be taken into account as well.
170
+ * However we don't want to store two memory sizes in t_pg_result just for reporting by ObjectSpace.memsize_of.
171
+ */
172
+ return this->result_size;
173
+ }
174
+
175
+ static const rb_data_type_t pgresult_type = {
176
+ "PG::Result",
177
+ {
178
+ pgresult_gc_mark,
179
+ pgresult_gc_free,
180
+ pgresult_memsize,
181
+ pgresult_gc_compact,
182
+ },
183
+ 0, 0,
184
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
185
+ };
186
+
187
+ /* Needed by sequel_pg gem, do not delete */
188
+ int pg_get_result_enc_idx(VALUE self)
189
+ {
190
+ return pgresult_get_this(self)->enc_idx;
191
+ }
192
+
193
+ /*
194
+ * Global functions
195
+ */
196
+
197
+ /*
198
+ * Result constructor
199
+ */
200
+ static VALUE
201
+ pg_new_result2(PGresult *result, VALUE rb_pgconn)
202
+ {
203
+ int nfields = result ? PQnfields(result) : 0;
204
+ VALUE self;
205
+ t_pg_result *this;
206
+
207
+ this = (t_pg_result *)xmalloc(sizeof(*this) + sizeof(*this->fnames) * nfields);
208
+ this->pgresult = result;
209
+ /* Initialize connection and typemap prior to any object allocations,
210
+ * to make sure valid objects are marked. */
211
+ this->connection = rb_pgconn;
212
+ this->typemap = pg_typemap_all_strings;
213
+ this->p_typemap = RTYPEDDATA_DATA( this->typemap );
214
+ this->nfields = -1;
215
+ this->tuple_hash = Qnil;
216
+ this->field_map = Qnil;
217
+ this->flags = 0;
218
+ self = TypedData_Wrap_Struct(rb_cPGresult, &pgresult_type, this);
219
+
220
+ if( result ){
221
+ t_pg_connection *p_conn = pg_get_connection(rb_pgconn);
222
+ VALUE typemap = p_conn->type_map_for_results;
223
+ /* Type check is done when assigned to PG::Connection. */
224
+ t_typemap *p_typemap = RTYPEDDATA_DATA(typemap);
225
+
226
+ this->enc_idx = p_conn->enc_idx;
227
+ typemap = p_typemap->funcs.fit_to_result( typemap, self );
228
+ RB_OBJ_WRITE(self, &this->typemap, typemap);
229
+ this->p_typemap = RTYPEDDATA_DATA( this->typemap );
230
+ this->flags = p_conn->flags;
231
+ } else {
232
+ this->enc_idx = rb_locale_encindex();
233
+ }
234
+
235
+ return self;
236
+ }
237
+
238
+ VALUE
239
+ pg_new_result(PGresult *result, VALUE rb_pgconn)
240
+ {
241
+ VALUE self = pg_new_result2(result, rb_pgconn);
242
+ t_pg_result *this = pgresult_get_this(self);
243
+
244
+ this->autoclear = 0;
245
+
246
+ /* Estimate size of underlying pgresult memory storage and account to ruby GC.
247
+ * There's no need to adjust the GC for xmalloc'ed memory, but libpq is using libc malloc() ruby doesn't know about.
248
+ */
249
+ /* TODO: If someday most systems provide PQresultMemorySize(), it's questionable to store result_size in t_pg_result in addition to the value already stored in PGresult.
250
+ * For now the memory savings don't justify the ifdefs necessary to support both cases.
251
+ */
252
+ this->result_size = pgresult_approx_size(result);
253
+
254
+ rb_gc_adjust_memory_usage(this->result_size);
255
+
256
+ return self;
257
+ }
258
+
259
+ static VALUE
260
+ pg_copy_result(t_pg_result *this)
261
+ {
262
+ int nfields = this->nfields == -1 ? (this->pgresult ? PQnfields(this->pgresult) : 0) : this->nfields;
263
+ size_t len = sizeof(*this) + sizeof(*this->fnames) * nfields;
264
+ t_pg_result *copy;
265
+
266
+ copy = (t_pg_result *)xmalloc(len);
267
+ memcpy(copy, this, len);
268
+ this->result_size = 0;
269
+
270
+ return TypedData_Wrap_Struct(rb_cPGresult, &pgresult_type, copy);
271
+ }
272
+
273
+ VALUE
274
+ pg_new_result_autoclear(PGresult *result, VALUE rb_pgconn)
275
+ {
276
+ VALUE self = pg_new_result2(result, rb_pgconn);
277
+ t_pg_result *this = pgresult_get_this(self);
278
+
279
+ /* Autocleared results are freed implicit instead of by PQclear().
280
+ * So it's not very useful to be accounted by ruby GC.
281
+ */
282
+ this->result_size = 0;
283
+ this->autoclear = 1;
284
+ return self;
285
+ }
286
+
287
+ /*
288
+ * call-seq:
289
+ * res.check -> nil
290
+ *
291
+ * Raises appropriate exception if PG::Result is in a bad state, which is:
292
+ * * +PGRES_BAD_RESPONSE+
293
+ * * +PGRES_FATAL_ERROR+
294
+ * * +PGRES_NONFATAL_ERROR+
295
+ * * +PGRES_PIPELINE_ABORTED+
296
+ */
297
+ VALUE
298
+ pg_result_check( VALUE self )
299
+ {
300
+ t_pg_result *this = pgresult_get_this(self);
301
+ VALUE error, exception, klass;
302
+ char * sqlstate;
303
+
304
+ if(this->pgresult == NULL)
305
+ {
306
+ PGconn *conn = pg_get_pgconn(this->connection);
307
+ error = rb_str_new2( PQerrorMessage(conn) );
308
+ }
309
+ else
310
+ {
311
+ switch (PQresultStatus(this->pgresult))
312
+ {
313
+ case PGRES_TUPLES_OK:
314
+ case PGRES_COPY_OUT:
315
+ case PGRES_COPY_IN:
316
+ case PGRES_COPY_BOTH:
317
+ case PGRES_SINGLE_TUPLE:
318
+ case PGRES_EMPTY_QUERY:
319
+ case PGRES_COMMAND_OK:
320
+ #ifdef HAVE_PQENTERPIPELINEMODE
321
+ case PGRES_PIPELINE_SYNC:
322
+ #endif
323
+ #ifdef HAVE_PQSETCHUNKEDROWSMODE
324
+ case PGRES_TUPLES_CHUNK:
325
+ #endif
326
+ return self;
327
+ case PGRES_BAD_RESPONSE:
328
+ case PGRES_FATAL_ERROR:
329
+ case PGRES_NONFATAL_ERROR:
330
+ #ifdef HAVE_PQENTERPIPELINEMODE
331
+ case PGRES_PIPELINE_ABORTED:
332
+ #endif
333
+ error = rb_str_new2( PQresultErrorMessage(this->pgresult) );
334
+ break;
335
+ default:
336
+ error = rb_str_new2( "internal error : unknown result status." );
337
+ }
338
+ }
339
+
340
+ PG_ENCODING_SET_NOCHECK( error, this->enc_idx );
341
+
342
+ sqlstate = PQresultErrorField( this->pgresult, PG_DIAG_SQLSTATE );
343
+ klass = lookup_error_class( sqlstate );
344
+ exception = rb_exc_new3( klass, error );
345
+ rb_iv_set( exception, "@connection", this->connection );
346
+ rb_iv_set( exception, "@result", this->pgresult ? self : Qnil );
347
+ rb_exc_raise( exception );
348
+
349
+ /* Not reached */
350
+ return self;
351
+ }
352
+
353
+
354
+ /*
355
+ * :TODO: This shouldn't be a global function, but it needs to be as long as pg_new_result
356
+ * doesn't handle blocks, check results, etc. Once connection and result are disentangled
357
+ * a bit more, I can make this a static pgresult_clear() again.
358
+ */
359
+
360
+ /*
361
+ * call-seq:
362
+ * res.clear() -> nil
363
+ *
364
+ * Clears the PG::Result object as the result of a query.
365
+ * This frees all underlying memory consumed by the result object.
366
+ * Afterwards access to result methods raises PG::Error "result has been cleared".
367
+ *
368
+ * Explicit calling #clear can lead to better memory performance, but is not generally necessary.
369
+ * Special care must be taken when PG::Tuple objects are used.
370
+ * In this case #clear must not be called unless all PG::Tuple objects of this result are fully materialized.
371
+ *
372
+ * If PG::Result#autoclear? is +true+ then the result is only marked as cleared but clearing the underlying C struct will happen when the callback returns.
373
+ *
374
+ */
375
+ VALUE
376
+ pg_result_clear(VALUE self)
377
+ {
378
+ t_pg_result *this = pgresult_get_this(self);
379
+ rb_check_frozen(self);
380
+ pgresult_clear( this );
381
+ return Qnil;
382
+ }
383
+
384
+ /*
385
+ * call-seq:
386
+ * res.freeze
387
+ *
388
+ * Freeze the PG::Result object and unlink the result from the related PG::Connection.
389
+ *
390
+ * A frozen PG::Result object doesn't allow any streaming and it can't be cleared.
391
+ * It also denies setting a type_map or field_name_type.
392
+ *
393
+ */
394
+ static VALUE
395
+ pg_result_freeze(VALUE self)
396
+ {
397
+ t_pg_result *this = pgresult_get_this(self);
398
+
399
+ RB_OBJ_WRITE(self, &this->connection, Qnil);
400
+ return rb_call_super(0, NULL);
401
+ }
402
+
403
+ /*
404
+ * call-seq:
405
+ * res.cleared? -> boolean
406
+ *
407
+ * Returns +true+ if the backend result memory has been freed.
408
+ */
409
+ static VALUE
410
+ pgresult_cleared_p( VALUE self )
411
+ {
412
+ t_pg_result *this = pgresult_get_this(self);
413
+ return this->pgresult ? Qfalse : Qtrue;
414
+ }
415
+
416
+ /*
417
+ * call-seq:
418
+ * res.autoclear? -> boolean
419
+ *
420
+ * Returns +true+ if the underlying C struct will be cleared at the end of a callback.
421
+ * This applies only to Result objects received by the block to PG::Connection#set_notice_receiver .
422
+ *
423
+ * All other Result objects are automatically cleared by the GC when the object is no longer in use or manually by PG::Result#clear .
424
+ *
425
+ */
426
+ static VALUE
427
+ pgresult_autoclear_p( VALUE self )
428
+ {
429
+ t_pg_result *this = pgresult_get_this(self);
430
+ return this->autoclear ? Qtrue : Qfalse;
431
+ }
432
+
433
+ /*
434
+ * DATA pointer functions
435
+ */
436
+
437
+ /*
438
+ * Fetch the PG::Result object data pointer and check it's
439
+ * PGresult data pointer for sanity.
440
+ */
441
+ static t_pg_result *
442
+ pgresult_get_this_safe( VALUE self )
443
+ {
444
+ t_pg_result *this = pgresult_get_this(self);
445
+
446
+ if (this->pgresult == NULL) rb_raise(rb_ePGerror, "result has been cleared");
447
+ return this;
448
+ }
449
+
450
+ /*
451
+ * Fetch the PGresult pointer for the result object and check validity
452
+ *
453
+ * Note: This function is used externally by the sequel_pg gem,
454
+ * so do changes carefully.
455
+ *
456
+ */
457
+ PGresult*
458
+ pgresult_get(VALUE self)
459
+ {
460
+ t_pg_result *this = pgresult_get_this(self);
461
+
462
+ if (this->pgresult == NULL) rb_raise(rb_ePGerror, "result has been cleared");
463
+ return this->pgresult;
464
+ }
465
+
466
+ static VALUE pg_cstr_to_sym(char *cstr, unsigned int flags, int enc_idx)
467
+ {
468
+ VALUE fname;
469
+ #ifdef TRUFFLERUBY
470
+ if( flags & (PG_RESULT_FIELD_NAMES_SYMBOL | PG_RESULT_FIELD_NAMES_STATIC_SYMBOL) ){
471
+ #else
472
+ if( flags & PG_RESULT_FIELD_NAMES_SYMBOL ){
473
+ rb_encoding *enc = rb_enc_from_index(enc_idx);
474
+ fname = rb_check_symbol_cstr(cstr, strlen(cstr), enc);
475
+ if( fname == Qnil ){
476
+ fname = rb_str_new2(cstr);
477
+ PG_ENCODING_SET_NOCHECK(fname, enc_idx);
478
+ fname = rb_str_intern(fname);
479
+ }
480
+ } else if( flags & PG_RESULT_FIELD_NAMES_STATIC_SYMBOL ){
481
+ #endif
482
+ rb_encoding *enc = rb_enc_from_index(enc_idx);
483
+ fname = ID2SYM(rb_intern3(cstr, strlen(cstr), enc));
484
+ } else {
485
+ fname = rb_str_new2(cstr);
486
+ PG_ENCODING_SET_NOCHECK(fname, enc_idx);
487
+ fname = rb_obj_freeze(fname);
488
+ }
489
+ return fname;
490
+ }
491
+
492
+ static void pgresult_init_fnames(VALUE self)
493
+ {
494
+ t_pg_result *this = pgresult_get_this_safe(self);
495
+
496
+ if( this->nfields == -1 ){
497
+ int i;
498
+ int nfields = PQnfields(this->pgresult);
499
+
500
+ for( i=0; i<nfields; i++ ){
501
+ char *cfname = PQfname(this->pgresult, i);
502
+ VALUE fname = pg_cstr_to_sym(cfname, this->flags, this->enc_idx);
503
+ RB_OBJ_WRITE(self, &this->fnames[i], fname);
504
+ this->nfields = i + 1;
505
+ }
506
+ this->nfields = nfields;
507
+ }
508
+ }
509
+
510
+ /********************************************************************
511
+ *
512
+ * Document-class: PG::Result
513
+ *
514
+ * The class to represent the query result tuples (rows).
515
+ * An instance of this class is created as the result of every query.
516
+ * All result rows and columns are stored in a memory block attached to the PG::Result object.
517
+ * Whenever a value is accessed it is casted to a Ruby object by the assigned #type_map .
518
+ *
519
+ * Since pg-1.1 the amount of memory in use by a PG::Result object is estimated and passed to ruby's garbage collector.
520
+ * You can invoke the #clear method to force deallocation of memory of the instance when finished with the result for better memory performance.
521
+ *
522
+ * Example:
523
+ * require 'pg'
524
+ * conn = PG.connect(:dbname => 'test')
525
+ * res = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
526
+ * res.getvalue(0,0) # '1'
527
+ * res[0]['b'] # '2'
528
+ * res[0]['c'] # nil
529
+ *
530
+ */
531
+
532
+ /**************************************************************************
533
+ * PG::Result INSTANCE METHODS
534
+ **************************************************************************/
535
+
536
+ /*
537
+ * call-seq:
538
+ * res.result_status() -> Integer
539
+ *
540
+ * Returns the status of the query. The status value is one of:
541
+ * * +PGRES_EMPTY_QUERY+
542
+ * * +PGRES_COMMAND_OK+
543
+ * * +PGRES_TUPLES_OK+
544
+ * * +PGRES_COPY_OUT+
545
+ * * +PGRES_COPY_IN+
546
+ * * +PGRES_BAD_RESPONSE+
547
+ * * +PGRES_NONFATAL_ERROR+
548
+ * * +PGRES_FATAL_ERROR+
549
+ * * +PGRES_COPY_BOTH+
550
+ * * +PGRES_SINGLE_TUPLE+
551
+ * * +PGRES_TUPLES_CHUNK+
552
+ * * +PGRES_PIPELINE_SYNC+
553
+ * * +PGRES_PIPELINE_ABORTED+
554
+ *
555
+ * Use <tt>res.res_status</tt> to retrieve the string representation.
556
+ */
557
+ static VALUE
558
+ pgresult_result_status(VALUE self)
559
+ {
560
+ return INT2FIX(PQresultStatus(pgresult_get(self)));
561
+ }
562
+
563
+ /*
564
+ * call-seq:
565
+ * PG::Result.res_status( status ) -> String
566
+ *
567
+ * Returns the string representation of +status+.
568
+ *
569
+ */
570
+ static VALUE
571
+ pgresult_s_res_status(VALUE self, VALUE status)
572
+ {
573
+ return rb_utf8_str_new_cstr(PQresStatus(NUM2INT(status)));
574
+ }
575
+
576
+ /*
577
+ * call-seq:
578
+ * res.res_status -> String
579
+ * res.res_status( status ) -> String
580
+ *
581
+ * Returns the string representation of the status of the result or of the provided +status+.
582
+ *
583
+ */
584
+ static VALUE
585
+ pgresult_res_status(int argc, VALUE *argv, VALUE self)
586
+ {
587
+ t_pg_result *this = pgresult_get_this_safe(self);
588
+ VALUE ret;
589
+
590
+ if( argc == 0 ){
591
+ ret = rb_str_new2(PQresStatus(PQresultStatus(this->pgresult)));
592
+ }else if( argc == 1 ){
593
+ ret = rb_str_new2(PQresStatus(NUM2INT(argv[0])));
594
+ }else{
595
+ rb_raise(rb_eArgError, "only 0 or 1 arguments expected");
596
+ }
597
+ PG_ENCODING_SET_NOCHECK(ret, this->enc_idx);
598
+ return ret;
599
+ }
600
+
601
+ /*
602
+ * call-seq:
603
+ * res.error_message() -> String
604
+ *
605
+ * Returns the error message of the command as a string.
606
+ */
607
+ static VALUE
608
+ pgresult_error_message(VALUE self)
609
+ {
610
+ t_pg_result *this = pgresult_get_this_safe(self);
611
+ VALUE ret = rb_str_new2(PQresultErrorMessage(this->pgresult));
612
+ PG_ENCODING_SET_NOCHECK(ret, this->enc_idx);
613
+ return ret;
614
+ }
615
+
616
+ /*
617
+ * call-seq:
618
+ * res.verbose_error_message( verbosity, show_context ) -> String
619
+ *
620
+ * Returns a reformatted version of the error message associated with a PGresult object.
621
+ *
622
+ */
623
+ static VALUE
624
+ pgresult_verbose_error_message(VALUE self, VALUE verbosity, VALUE show_context)
625
+ {
626
+ t_pg_result *this = pgresult_get_this_safe(self);
627
+ VALUE ret;
628
+ char *c_str;
629
+
630
+ c_str = PQresultVerboseErrorMessage(this->pgresult, NUM2INT(verbosity), NUM2INT(show_context));
631
+ if(!c_str)
632
+ rb_raise(rb_eNoMemError, "insufficient memory to format error message");
633
+
634
+ ret = rb_str_new2(c_str);
635
+ PQfreemem(c_str);
636
+ PG_ENCODING_SET_NOCHECK(ret, this->enc_idx);
637
+
638
+ return ret;
639
+ }
640
+
641
+ /*
642
+ * call-seq:
643
+ * res.error_field(fieldcode) -> String
644
+ *
645
+ * Returns the individual field of an error.
646
+ *
647
+ * +fieldcode+ is one of:
648
+ * * +PG_DIAG_SEVERITY+
649
+ * * +PG_DIAG_SQLSTATE+
650
+ * * +PG_DIAG_MESSAGE_PRIMARY+
651
+ * * +PG_DIAG_MESSAGE_DETAIL+
652
+ * * +PG_DIAG_MESSAGE_HINT+
653
+ * * +PG_DIAG_STATEMENT_POSITION+
654
+ * * +PG_DIAG_INTERNAL_POSITION+
655
+ * * +PG_DIAG_INTERNAL_QUERY+
656
+ * * +PG_DIAG_CONTEXT+
657
+ * * +PG_DIAG_SOURCE_FILE+
658
+ * * +PG_DIAG_SOURCE_LINE+
659
+ * * +PG_DIAG_SOURCE_FUNCTION+
660
+ *
661
+ * An example:
662
+ *
663
+ * begin
664
+ * conn.exec( "SELECT * FROM nonexistent_table" )
665
+ * rescue PG::Error => err
666
+ * p [
667
+ * err.result.error_field( PG::Result::PG_DIAG_SEVERITY ),
668
+ * err.result.error_field( PG::Result::PG_DIAG_SQLSTATE ),
669
+ * err.result.error_field( PG::Result::PG_DIAG_MESSAGE_PRIMARY ),
670
+ * err.result.error_field( PG::Result::PG_DIAG_MESSAGE_DETAIL ),
671
+ * err.result.error_field( PG::Result::PG_DIAG_MESSAGE_HINT ),
672
+ * err.result.error_field( PG::Result::PG_DIAG_STATEMENT_POSITION ),
673
+ * err.result.error_field( PG::Result::PG_DIAG_INTERNAL_POSITION ),
674
+ * err.result.error_field( PG::Result::PG_DIAG_INTERNAL_QUERY ),
675
+ * err.result.error_field( PG::Result::PG_DIAG_CONTEXT ),
676
+ * err.result.error_field( PG::Result::PG_DIAG_SOURCE_FILE ),
677
+ * err.result.error_field( PG::Result::PG_DIAG_SOURCE_LINE ),
678
+ * err.result.error_field( PG::Result::PG_DIAG_SOURCE_FUNCTION ),
679
+ * ]
680
+ * end
681
+ *
682
+ * Outputs:
683
+ *
684
+ * ["ERROR", "42P01", "relation \"nonexistent_table\" does not exist", nil, nil,
685
+ * "15", nil, nil, nil, "path/to/parse_relation.c", "857", "parserOpenTable"]
686
+ */
687
+ static VALUE
688
+ pgresult_error_field(VALUE self, VALUE field)
689
+ {
690
+ t_pg_result *this = pgresult_get_this_safe(self);
691
+ int fieldcode = NUM2INT( field );
692
+ char * fieldstr = PQresultErrorField( this->pgresult, fieldcode );
693
+ VALUE ret = Qnil;
694
+
695
+ if ( fieldstr ) {
696
+ ret = rb_str_new2( fieldstr );
697
+ PG_ENCODING_SET_NOCHECK( ret, this->enc_idx );
698
+ }
699
+
700
+ return ret;
701
+ }
702
+
703
+ /*
704
+ * call-seq:
705
+ * res.ntuples() -> Integer
706
+ *
707
+ * Returns the number of tuples in the query result.
708
+ */
709
+ static VALUE
710
+ pgresult_ntuples(VALUE self)
711
+ {
712
+ return INT2FIX(PQntuples(pgresult_get(self)));
713
+ }
714
+
715
+ static VALUE
716
+ pgresult_ntuples_for_enum(VALUE self, VALUE args, VALUE eobj)
717
+ {
718
+ return pgresult_ntuples(self);
719
+ }
720
+
721
+ /*
722
+ * call-seq:
723
+ * res.nfields() -> Integer
724
+ *
725
+ * Returns the number of columns in the query result.
726
+ */
727
+ static VALUE
728
+ pgresult_nfields(VALUE self)
729
+ {
730
+ return INT2NUM(PQnfields(pgresult_get(self)));
731
+ }
732
+
733
+ /*
734
+ * call-seq:
735
+ * res.binary_tuples() -> Integer
736
+ *
737
+ * Returns 1 if the PGresult contains binary data and 0 if it contains text data.
738
+ *
739
+ * This function is deprecated (except for its use in connection with COPY), because it is possible for a single PGresult to contain text data in some columns and binary data in others.
740
+ * Result#fformat is preferred. binary_tuples returns 1 only if all columns of the result are binary (format 1).
741
+ */
742
+ static VALUE
743
+ pgresult_binary_tuples(VALUE self)
744
+ {
745
+ return INT2NUM(PQbinaryTuples(pgresult_get(self)));
746
+ }
747
+
748
+ /*
749
+ * call-seq:
750
+ * res.fname( index ) -> String or Symbol
751
+ *
752
+ * Returns the name of the column corresponding to _index_.
753
+ * Depending on #field_name_type= it's a String or Symbol.
754
+ *
755
+ */
756
+ static VALUE
757
+ pgresult_fname(VALUE self, VALUE index)
758
+ {
759
+ t_pg_result *this = pgresult_get_this_safe(self);
760
+ int i = NUM2INT(index);
761
+ char *cfname;
762
+
763
+ if (i < 0 || i >= PQnfields(this->pgresult)) {
764
+ rb_raise(rb_eArgError,"invalid field number %d", i);
765
+ }
766
+
767
+ cfname = PQfname(this->pgresult, i);
768
+ return pg_cstr_to_sym(cfname, this->flags, this->enc_idx);
769
+ }
770
+
771
+ /*
772
+ * call-seq:
773
+ * res.fnumber( name ) -> Integer
774
+ *
775
+ * Returns the index of the field specified by the string +name+.
776
+ * The given +name+ is treated like an identifier in an SQL command, that is,
777
+ * it is downcased unless double-quoted. For example, given a query result
778
+ * generated from the SQL command:
779
+ *
780
+ * result = conn.exec( %{SELECT 1 AS FOO, 2 AS "BAR"} )
781
+ *
782
+ * we would have the results:
783
+ *
784
+ * result.fname( 0 ) # => "foo"
785
+ * result.fname( 1 ) # => "BAR"
786
+ * result.fnumber( "FOO" ) # => 0
787
+ * result.fnumber( "foo" ) # => 0
788
+ * result.fnumber( "BAR" ) # => ArgumentError
789
+ * result.fnumber( %{"BAR"} ) # => 1
790
+ *
791
+ * Raises an ArgumentError if the specified +name+ isn't one of the field names;
792
+ * raises a TypeError if +name+ is not a String.
793
+ */
794
+ static VALUE
795
+ pgresult_fnumber(VALUE self, VALUE name)
796
+ {
797
+ int n;
798
+
799
+ Check_Type(name, T_STRING);
800
+
801
+ n = PQfnumber(pgresult_get(self), StringValueCStr(name));
802
+ if (n == -1) {
803
+ rb_raise(rb_eArgError,"Unknown field: %s", StringValueCStr(name));
804
+ }
805
+ return INT2FIX(n);
806
+ }
807
+
808
+ /*
809
+ * call-seq:
810
+ * res.ftable( column_number ) -> Integer
811
+ *
812
+ * Returns the Oid of the table from which the column _column_number_
813
+ * was fetched.
814
+ *
815
+ * Raises ArgumentError if _column_number_ is out of range or if
816
+ * the Oid is undefined for that column.
817
+ */
818
+ static VALUE
819
+ pgresult_ftable(VALUE self, VALUE column_number)
820
+ {
821
+ Oid n ;
822
+ int col_number = NUM2INT(column_number);
823
+ PGresult *pgresult = pgresult_get(self);
824
+
825
+ if( col_number < 0 || col_number >= PQnfields(pgresult))
826
+ rb_raise(rb_eArgError,"Invalid column index: %d", col_number);
827
+
828
+ n = PQftable(pgresult, col_number);
829
+ return UINT2NUM(n);
830
+ }
831
+
832
+ /*
833
+ * call-seq:
834
+ * res.ftablecol( column_number ) -> Integer
835
+ *
836
+ * Returns the column number (within its table) of the table from
837
+ * which the column _column_number_ is made up.
838
+ *
839
+ * Raises ArgumentError if _column_number_ is out of range or if
840
+ * the column number from its table is undefined for that column.
841
+ */
842
+ static VALUE
843
+ pgresult_ftablecol(VALUE self, VALUE column_number)
844
+ {
845
+ int col_number = NUM2INT(column_number);
846
+ PGresult *pgresult = pgresult_get(self);
847
+
848
+ int n;
849
+
850
+ if( col_number < 0 || col_number >= PQnfields(pgresult))
851
+ rb_raise(rb_eArgError,"Invalid column index: %d", col_number);
852
+
853
+ n = PQftablecol(pgresult, col_number);
854
+ return INT2FIX(n);
855
+ }
856
+
857
+ /*
858
+ * call-seq:
859
+ * res.fformat( column_number ) -> Integer
860
+ *
861
+ * Returns the format (0 for text, 1 for binary) of column
862
+ * _column_number_.
863
+ *
864
+ * Raises ArgumentError if _column_number_ is out of range.
865
+ */
866
+ static VALUE
867
+ pgresult_fformat(VALUE self, VALUE column_number)
868
+ {
869
+ PGresult *result = pgresult_get(self);
870
+ int fnumber = NUM2INT(column_number);
871
+ if (fnumber < 0 || fnumber >= PQnfields(result)) {
872
+ rb_raise(rb_eArgError, "Column number is out of range: %d",
873
+ fnumber);
874
+ }
875
+ return INT2FIX(PQfformat(result, fnumber));
876
+ }
877
+
878
+ /*
879
+ * call-seq:
880
+ * res.ftype( column_number ) -> Integer
881
+ *
882
+ * Returns the data type associated with _column_number_.
883
+ *
884
+ * The integer returned is the internal +OID+ number (in PostgreSQL)
885
+ * of the type. To get a human-readable value for the type, use the
886
+ * returned OID and the field's #fmod value with the format_type() SQL
887
+ * function:
888
+ *
889
+ * # Get the type of the second column of the result 'res'
890
+ * typename = conn.
891
+ * exec( "SELECT format_type($1,$2)", [res.ftype(1), res.fmod(1)] ).
892
+ * getvalue( 0, 0 )
893
+ *
894
+ * Raises an ArgumentError if _column_number_ is out of range.
895
+ */
896
+ static VALUE
897
+ pgresult_ftype(VALUE self, VALUE index)
898
+ {
899
+ PGresult* result = pgresult_get(self);
900
+ int i = NUM2INT(index);
901
+ if (i < 0 || i >= PQnfields(result)) {
902
+ rb_raise(rb_eArgError, "invalid field number %d", i);
903
+ }
904
+ return UINT2NUM(PQftype(result, i));
905
+ }
906
+
907
+ /*
908
+ * call-seq:
909
+ * res.fmod( column_number )
910
+ *
911
+ * Returns the type modifier associated with column _column_number_. See
912
+ * the #ftype method for an example of how to use this.
913
+ *
914
+ * Raises an ArgumentError if _column_number_ is out of range.
915
+ */
916
+ static VALUE
917
+ pgresult_fmod(VALUE self, VALUE column_number)
918
+ {
919
+ PGresult *result = pgresult_get(self);
920
+ int fnumber = NUM2INT(column_number);
921
+ int modifier;
922
+ if (fnumber < 0 || fnumber >= PQnfields(result)) {
923
+ rb_raise(rb_eArgError, "Column number is out of range: %d",
924
+ fnumber);
925
+ }
926
+ modifier = PQfmod(result,fnumber);
927
+
928
+ return INT2NUM(modifier);
929
+ }
930
+
931
+ /*
932
+ * call-seq:
933
+ * res.fsize( index )
934
+ *
935
+ * Returns the size of the field type in bytes. Returns <tt>-1</tt> if the field is variable sized.
936
+ *
937
+ * res = conn.exec("SELECT myInt, myVarChar50 FROM foo")
938
+ * res.size(0) => 4
939
+ * res.size(1) => -1
940
+ */
941
+ static VALUE
942
+ pgresult_fsize(VALUE self, VALUE index)
943
+ {
944
+ PGresult *result;
945
+ int i = NUM2INT(index);
946
+
947
+ result = pgresult_get(self);
948
+ if (i < 0 || i >= PQnfields(result)) {
949
+ rb_raise(rb_eArgError,"invalid field number %d", i);
950
+ }
951
+ return INT2NUM(PQfsize(result, i));
952
+ }
953
+
954
+
955
+ /*
956
+ * call-seq:
957
+ * res.getvalue( tup_num, field_num )
958
+ *
959
+ * Returns the value in tuple number _tup_num_, field _field_num_,
960
+ * or +nil+ if the field is +NULL+.
961
+ */
962
+ static VALUE
963
+ pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num)
964
+ {
965
+ t_pg_result *this = pgresult_get_this_safe(self);
966
+ int i = NUM2INT(tup_num);
967
+ int j = NUM2INT(field_num);
968
+
969
+ if(i < 0 || i >= PQntuples(this->pgresult)) {
970
+ rb_raise(rb_eArgError,"invalid tuple number %d", i);
971
+ }
972
+ if(j < 0 || j >= PQnfields(this->pgresult)) {
973
+ rb_raise(rb_eArgError,"invalid field number %d", j);
974
+ }
975
+ return this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, i, j);
976
+ }
977
+
978
+ /*
979
+ * call-seq:
980
+ * res.getisnull(tuple_position, field_position) -> boolean
981
+ *
982
+ * Returns +true+ if the specified value is +nil+; +false+ otherwise.
983
+ */
984
+ static VALUE
985
+ pgresult_getisnull(VALUE self, VALUE tup_num, VALUE field_num)
986
+ {
987
+ PGresult *result;
988
+ int i = NUM2INT(tup_num);
989
+ int j = NUM2INT(field_num);
990
+
991
+ result = pgresult_get(self);
992
+ if (i < 0 || i >= PQntuples(result)) {
993
+ rb_raise(rb_eArgError,"invalid tuple number %d", i);
994
+ }
995
+ if (j < 0 || j >= PQnfields(result)) {
996
+ rb_raise(rb_eArgError,"invalid field number %d", j);
997
+ }
998
+ return PQgetisnull(result, i, j) ? Qtrue : Qfalse;
999
+ }
1000
+
1001
+ /*
1002
+ * call-seq:
1003
+ * res.getlength( tup_num, field_num ) -> Integer
1004
+ *
1005
+ * Returns the (String) length of the field in bytes.
1006
+ *
1007
+ * Equivalent to <tt>res.value(<i>tup_num</i>,<i>field_num</i>).length</tt>.
1008
+ */
1009
+ static VALUE
1010
+ pgresult_getlength(VALUE self, VALUE tup_num, VALUE field_num)
1011
+ {
1012
+ PGresult *result;
1013
+ int i = NUM2INT(tup_num);
1014
+ int j = NUM2INT(field_num);
1015
+
1016
+ result = pgresult_get(self);
1017
+ if (i < 0 || i >= PQntuples(result)) {
1018
+ rb_raise(rb_eArgError,"invalid tuple number %d", i);
1019
+ }
1020
+ if (j < 0 || j >= PQnfields(result)) {
1021
+ rb_raise(rb_eArgError,"invalid field number %d", j);
1022
+ }
1023
+ return INT2FIX(PQgetlength(result, i, j));
1024
+ }
1025
+
1026
+ /*
1027
+ * call-seq:
1028
+ * res.nparams() -> Integer
1029
+ *
1030
+ * Returns the number of parameters of a prepared statement.
1031
+ * Only useful for the result returned by conn.describePrepared
1032
+ */
1033
+ static VALUE
1034
+ pgresult_nparams(VALUE self)
1035
+ {
1036
+ PGresult *result;
1037
+
1038
+ result = pgresult_get(self);
1039
+ return INT2FIX(PQnparams(result));
1040
+ }
1041
+
1042
+ /*
1043
+ * call-seq:
1044
+ * res.paramtype( param_number ) -> Oid
1045
+ *
1046
+ * Returns the Oid of the data type of parameter _param_number_.
1047
+ * Only useful for the result returned by conn.describePrepared
1048
+ */
1049
+ static VALUE
1050
+ pgresult_paramtype(VALUE self, VALUE param_number)
1051
+ {
1052
+ PGresult *result;
1053
+
1054
+ result = pgresult_get(self);
1055
+ return UINT2NUM(PQparamtype(result,NUM2INT(param_number)));
1056
+ }
1057
+
1058
+ /*
1059
+ * call-seq:
1060
+ * res.cmd_status() -> String
1061
+ *
1062
+ * Returns the status string of the last query command.
1063
+ */
1064
+ static VALUE
1065
+ pgresult_cmd_status(VALUE self)
1066
+ {
1067
+ t_pg_result *this = pgresult_get_this_safe(self);
1068
+ VALUE ret = rb_str_new2(PQcmdStatus(this->pgresult));
1069
+ PG_ENCODING_SET_NOCHECK(ret, this->enc_idx);
1070
+ return ret;
1071
+ }
1072
+
1073
+ /*
1074
+ * call-seq:
1075
+ * res.cmd_tuples() -> Integer
1076
+ *
1077
+ * Returns the number of tuples (rows) affected by the SQL command.
1078
+ *
1079
+ * If the SQL command that generated the PG::Result was not one of:
1080
+ *
1081
+ * * <tt>SELECT</tt>
1082
+ * * <tt>CREATE TABLE AS</tt>
1083
+ * * <tt>INSERT</tt>
1084
+ * * <tt>UPDATE</tt>
1085
+ * * <tt>DELETE</tt>
1086
+ * * <tt>MOVE</tt>
1087
+ * * <tt>FETCH</tt>
1088
+ * * <tt>COPY</tt>
1089
+ * * an +EXECUTE+ of a prepared query that contains an +INSERT+, +UPDATE+, or +DELETE+ statement
1090
+ *
1091
+ * or if no tuples were affected, <tt>0</tt> is returned.
1092
+ */
1093
+ static VALUE
1094
+ pgresult_cmd_tuples(VALUE self)
1095
+ {
1096
+ long n;
1097
+ n = strtol(PQcmdTuples(pgresult_get(self)),NULL, 10);
1098
+ return LONG2NUM(n);
1099
+ }
1100
+
1101
+ /*
1102
+ * call-seq:
1103
+ * res.oid_value() -> Integer
1104
+ *
1105
+ * Returns the +oid+ of the inserted row if applicable,
1106
+ * otherwise +nil+.
1107
+ */
1108
+ static VALUE
1109
+ pgresult_oid_value(VALUE self)
1110
+ {
1111
+ Oid n = PQoidValue(pgresult_get(self));
1112
+ if (n == InvalidOid)
1113
+ return Qnil;
1114
+ else
1115
+ return UINT2NUM(n);
1116
+ }
1117
+
1118
+ /* Utility methods not in libpq */
1119
+
1120
+ /*
1121
+ * call-seq:
1122
+ * res[ n ] -> Hash
1123
+ *
1124
+ * Returns tuple _n_ as a hash.
1125
+ */
1126
+ static VALUE
1127
+ pgresult_aref(VALUE self, VALUE index)
1128
+ {
1129
+ t_pg_result *this = pgresult_get_this_safe(self);
1130
+ int tuple_num = NUM2INT(index);
1131
+ int field_num;
1132
+ int num_tuples = PQntuples(this->pgresult);
1133
+ VALUE tuple;
1134
+
1135
+ if( this->nfields == -1 )
1136
+ pgresult_init_fnames( self );
1137
+
1138
+ if ( tuple_num < 0 || tuple_num >= num_tuples )
1139
+ rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num );
1140
+
1141
+ /* We reuse the Hash of the previous output for larger row counts.
1142
+ * This is somewhat faster than populating an empty Hash object. */
1143
+ tuple = NIL_P(this->tuple_hash) ? rb_hash_new() : this->tuple_hash;
1144
+ for ( field_num = 0; field_num < this->nfields; field_num++ ) {
1145
+ VALUE val = this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, tuple_num, field_num);
1146
+ rb_hash_aset( tuple, this->fnames[field_num], val );
1147
+ }
1148
+ /* Store a copy of the filled hash for use at the next row. */
1149
+ if( num_tuples > 10 )
1150
+ RB_OBJ_WRITE(self, &this->tuple_hash, rb_hash_dup(tuple));
1151
+
1152
+ return tuple;
1153
+ }
1154
+
1155
+ /*
1156
+ * call-seq:
1157
+ * res.each_row { |row| ... }
1158
+ *
1159
+ * Yields each row of the result. The row is a list of column values.
1160
+ */
1161
+ static VALUE
1162
+ pgresult_each_row(VALUE self)
1163
+ {
1164
+ t_pg_result *this;
1165
+ int row;
1166
+ int field;
1167
+ int num_rows;
1168
+ int num_fields;
1169
+
1170
+ RETURN_SIZED_ENUMERATOR(self, 0, NULL, pgresult_ntuples_for_enum);
1171
+
1172
+ this = pgresult_get_this_safe(self);
1173
+ num_rows = PQntuples(this->pgresult);
1174
+ num_fields = PQnfields(this->pgresult);
1175
+
1176
+ for ( row = 0; row < num_rows; row++ ) {
1177
+ PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, num_fields, PG_MAX_COLUMNS)
1178
+
1179
+ /* populate the row */
1180
+ for ( field = 0; field < num_fields; field++ ) {
1181
+ row_values[field] = this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, row, field);
1182
+ }
1183
+ rb_yield( rb_ary_new4( num_fields, row_values ));
1184
+ }
1185
+
1186
+ return Qnil;
1187
+ }
1188
+
1189
+ /*
1190
+ * call-seq:
1191
+ * res.values -> Array
1192
+ *
1193
+ * Returns all tuples as an array of arrays.
1194
+ */
1195
+ static VALUE
1196
+ pgresult_values(VALUE self)
1197
+ {
1198
+ t_pg_result *this = pgresult_get_this_safe(self);
1199
+ int row;
1200
+ int field;
1201
+ int num_rows = PQntuples(this->pgresult);
1202
+ int num_fields = PQnfields(this->pgresult);
1203
+ VALUE results = rb_ary_new2( num_rows );
1204
+
1205
+ for ( row = 0; row < num_rows; row++ ) {
1206
+ PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, num_fields, PG_MAX_COLUMNS)
1207
+
1208
+ /* populate the row */
1209
+ for ( field = 0; field < num_fields; field++ ) {
1210
+ row_values[field] = this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, row, field);
1211
+ }
1212
+ rb_ary_store( results, row, rb_ary_new4( num_fields, row_values ) );
1213
+ }
1214
+
1215
+ return results;
1216
+ }
1217
+
1218
+ /*
1219
+ * Make a Ruby array out of the encoded values from the specified
1220
+ * column in the given result.
1221
+ */
1222
+ static VALUE
1223
+ make_column_result_array( VALUE self, int col )
1224
+ {
1225
+ t_pg_result *this = pgresult_get_this_safe(self);
1226
+ int rows = PQntuples( this->pgresult );
1227
+ int i;
1228
+ VALUE results = rb_ary_new2( rows );
1229
+
1230
+ if ( col >= PQnfields(this->pgresult) )
1231
+ rb_raise( rb_eIndexError, "no column %d in result", col );
1232
+
1233
+ for ( i=0; i < rows; i++ ) {
1234
+ VALUE val = this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, i, col);
1235
+ rb_ary_store( results, i, val );
1236
+ }
1237
+
1238
+ return results;
1239
+ }
1240
+
1241
+
1242
+ /*
1243
+ * call-seq:
1244
+ * res.column_values( n ) -> array
1245
+ *
1246
+ * Returns an Array of the values from the nth column of each
1247
+ * tuple in the result.
1248
+ *
1249
+ */
1250
+ static VALUE
1251
+ pgresult_column_values(VALUE self, VALUE index)
1252
+ {
1253
+ int col = NUM2INT( index );
1254
+ return make_column_result_array( self, col );
1255
+ }
1256
+
1257
+
1258
+ /*
1259
+ * call-seq:
1260
+ * res.field_values( field ) -> array
1261
+ *
1262
+ * Returns an Array of the values from the given _field_ of each tuple in the result.
1263
+ *
1264
+ */
1265
+ static VALUE
1266
+ pgresult_field_values( VALUE self, VALUE field )
1267
+ {
1268
+ PGresult *result = pgresult_get( self );
1269
+ const char *fieldname;
1270
+ int fnum;
1271
+
1272
+ if( RB_TYPE_P(field, T_SYMBOL) ) field = rb_sym_to_s( field );
1273
+ fieldname = StringValueCStr( field );
1274
+ fnum = PQfnumber( result, fieldname );
1275
+
1276
+ if ( fnum < 0 )
1277
+ rb_raise( rb_eIndexError, "no such field '%s' in result", fieldname );
1278
+
1279
+ return make_column_result_array( self, fnum );
1280
+ }
1281
+
1282
+
1283
+ /*
1284
+ * call-seq:
1285
+ * res.tuple_values( n ) -> array
1286
+ *
1287
+ * Returns an Array of the field values from the nth row of the result.
1288
+ *
1289
+ */
1290
+ static VALUE
1291
+ pgresult_tuple_values(VALUE self, VALUE index)
1292
+ {
1293
+ int tuple_num = NUM2INT( index );
1294
+ t_pg_result *this;
1295
+ int field;
1296
+ int num_tuples;
1297
+ int num_fields;
1298
+
1299
+ this = pgresult_get_this_safe(self);
1300
+ num_tuples = PQntuples(this->pgresult);
1301
+ num_fields = PQnfields(this->pgresult);
1302
+
1303
+ if ( tuple_num < 0 || tuple_num >= num_tuples )
1304
+ rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num );
1305
+
1306
+ {
1307
+ PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, num_fields, PG_MAX_COLUMNS)
1308
+
1309
+ /* populate the row */
1310
+ for ( field = 0; field < num_fields; field++ ) {
1311
+ row_values[field] = this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, tuple_num, field);
1312
+ }
1313
+ return rb_ary_new4( num_fields, row_values );
1314
+ }
1315
+ }
1316
+
1317
+ static void ensure_init_for_tuple(VALUE self)
1318
+ {
1319
+ t_pg_result *this = pgresult_get_this_safe(self);
1320
+
1321
+ if( this->field_map == Qnil ){
1322
+ int i;
1323
+ VALUE field_map = rb_hash_new();
1324
+
1325
+ if( this->nfields == -1 )
1326
+ pgresult_init_fnames( self );
1327
+
1328
+ for( i = 0; i < this->nfields; i++ ){
1329
+ rb_hash_aset(field_map, this->fnames[i], INT2FIX(i));
1330
+ }
1331
+ rb_obj_freeze(field_map);
1332
+ RB_OBJ_WRITE(self, &this->field_map, field_map);
1333
+ }
1334
+ }
1335
+
1336
+ /*
1337
+ * call-seq:
1338
+ * res.tuple( n ) -> PG::Tuple
1339
+ *
1340
+ * Returns a PG::Tuple from the nth row of the result.
1341
+ *
1342
+ */
1343
+ static VALUE
1344
+ pgresult_tuple(VALUE self, VALUE index)
1345
+ {
1346
+ int tuple_num = NUM2INT( index );
1347
+ t_pg_result *this;
1348
+ int num_tuples;
1349
+
1350
+ this = pgresult_get_this_safe(self);
1351
+ num_tuples = PQntuples(this->pgresult);
1352
+
1353
+ if ( tuple_num < 0 || tuple_num >= num_tuples )
1354
+ rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num );
1355
+
1356
+ ensure_init_for_tuple(self);
1357
+
1358
+ return pg_tuple_new(self, tuple_num);
1359
+ }
1360
+
1361
+
1362
+ /*
1363
+ * call-seq:
1364
+ * res.each{ |tuple| ... }
1365
+ *
1366
+ * Invokes block for each tuple in the result set.
1367
+ */
1368
+ static VALUE
1369
+ pgresult_each(VALUE self)
1370
+ {
1371
+ PGresult *result;
1372
+ int tuple_num;
1373
+
1374
+ RETURN_SIZED_ENUMERATOR(self, 0, NULL, pgresult_ntuples_for_enum);
1375
+
1376
+ result = pgresult_get(self);
1377
+
1378
+ for(tuple_num = 0; tuple_num < PQntuples(result); tuple_num++) {
1379
+ rb_yield(pgresult_aref(self, INT2NUM(tuple_num)));
1380
+ }
1381
+ return self;
1382
+ }
1383
+
1384
+ /*
1385
+ * call-seq:
1386
+ * res.fields() -> Array
1387
+ *
1388
+ * Depending on #field_name_type= returns an array of strings or symbols representing the names of the fields in the result.
1389
+ */
1390
+ static VALUE
1391
+ pgresult_fields(VALUE self)
1392
+ {
1393
+ t_pg_result *this = pgresult_get_this_safe(self);
1394
+
1395
+ if( this->nfields == -1 )
1396
+ pgresult_init_fnames( self );
1397
+
1398
+ return rb_ary_new4( this->nfields, this->fnames );
1399
+ }
1400
+
1401
+ /*
1402
+ * call-seq:
1403
+ * res.type_map = typemap
1404
+ *
1405
+ * Set the TypeMap that is used for type casts of result values to ruby objects.
1406
+ *
1407
+ * All value retrieval methods will respect the type map and will do the
1408
+ * type casts from PostgreSQL's wire format to Ruby objects on the fly,
1409
+ * according to the rules and decoders defined in the given typemap.
1410
+ *
1411
+ * +typemap+ must be a kind of PG::TypeMap .
1412
+ *
1413
+ */
1414
+ static VALUE
1415
+ pgresult_type_map_set(VALUE self, VALUE typemap)
1416
+ {
1417
+ t_pg_result *this = pgresult_get_this(self);
1418
+ t_typemap *p_typemap;
1419
+
1420
+ rb_check_frozen(self);
1421
+ /* Check type of method param */
1422
+ TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, p_typemap);
1423
+
1424
+ typemap = p_typemap->funcs.fit_to_result( typemap, self );
1425
+ RB_OBJ_WRITE(self, &this->typemap, typemap);
1426
+ this->p_typemap = RTYPEDDATA_DATA( typemap );
1427
+
1428
+ return typemap;
1429
+ }
1430
+
1431
+ /*
1432
+ * call-seq:
1433
+ * res.type_map -> value
1434
+ *
1435
+ * Returns the TypeMap that is currently set for type casts of result values to ruby objects.
1436
+ *
1437
+ */
1438
+ static VALUE
1439
+ pgresult_type_map_get(VALUE self)
1440
+ {
1441
+ t_pg_result *this = pgresult_get_this(self);
1442
+
1443
+ return this->typemap;
1444
+ }
1445
+
1446
+
1447
+ static int
1448
+ yield_hash(VALUE self, int ntuples, int nfields, void *data)
1449
+ {
1450
+ int tuple_num;
1451
+ UNUSED(nfields);
1452
+
1453
+ for(tuple_num = 0; tuple_num < ntuples; tuple_num++) {
1454
+ rb_yield(pgresult_aref(self, INT2NUM(tuple_num)));
1455
+ }
1456
+
1457
+ return 1; /* clear the result */
1458
+ }
1459
+
1460
+ static int
1461
+ yield_array(VALUE self, int ntuples, int nfields, void *data)
1462
+ {
1463
+ int row;
1464
+ t_pg_result *this = pgresult_get_this(self);
1465
+
1466
+ for ( row = 0; row < ntuples; row++ ) {
1467
+ PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, nfields, PG_MAX_COLUMNS)
1468
+ int field;
1469
+
1470
+ /* populate the row */
1471
+ for ( field = 0; field < nfields; field++ ) {
1472
+ row_values[field] = this->p_typemap->funcs.typecast_result_value(this->p_typemap, self, row, field);
1473
+ }
1474
+ rb_yield( rb_ary_new4( nfields, row_values ));
1475
+ }
1476
+
1477
+ return 1; /* clear the result */
1478
+ }
1479
+
1480
+ static int
1481
+ yield_tuple(VALUE self, int ntuples, int nfields, void *data)
1482
+ {
1483
+ int tuple_num;
1484
+ t_pg_result *this = pgresult_get_this(self);
1485
+ VALUE copy;
1486
+ UNUSED(nfields);
1487
+
1488
+ /* make a copy of the base result, that is bound to the PG::Tuple */
1489
+ copy = pg_copy_result(this);
1490
+ /* The copy is now owner of the PGresult and is responsible to PQclear it.
1491
+ * We clear the pgresult here, so that it's not double freed on error within yield. */
1492
+ this->pgresult = NULL;
1493
+
1494
+ for(tuple_num = 0; tuple_num < ntuples; tuple_num++) {
1495
+ VALUE tuple = pgresult_tuple(copy, INT2FIX(tuple_num));
1496
+ rb_yield( tuple );
1497
+ }
1498
+ return 0; /* don't clear the result */
1499
+ }
1500
+
1501
+ /* Non-static, and data pointer for use by sequel_pg */
1502
+ VALUE
1503
+ pgresult_stream_any(VALUE self, int (*yielder)(VALUE, int, int, void*), void* data)
1504
+ {
1505
+ t_pg_result *this;
1506
+ int nfields, nfields2;
1507
+ PGconn *pgconn;
1508
+ PGresult *pgresult;
1509
+
1510
+ rb_check_frozen(self);
1511
+ RETURN_ENUMERATOR(self, 0, NULL);
1512
+
1513
+ this = pgresult_get_this_safe(self);
1514
+ pgconn = pg_get_pgconn(this->connection);
1515
+ pgresult = this->pgresult;
1516
+ nfields = PQnfields(pgresult);
1517
+
1518
+ for(;;){
1519
+ int ntuples = PQntuples(pgresult);
1520
+
1521
+ switch( PQresultStatus(pgresult) ){
1522
+ case PGRES_TUPLES_OK:
1523
+ case PGRES_COMMAND_OK:
1524
+ if( ntuples == 0 )
1525
+ return self;
1526
+ rb_raise( rb_eInvalidResultStatus, "PG::Result is not in single row mode");
1527
+ case PGRES_SINGLE_TUPLE:
1528
+ #ifdef HAVE_PQSETCHUNKEDROWSMODE
1529
+ case PGRES_TUPLES_CHUNK:
1530
+ #endif
1531
+ break;
1532
+ default:
1533
+ pg_result_check( self );
1534
+ }
1535
+
1536
+ nfields2 = PQnfields(pgresult);
1537
+ if( nfields != nfields2 ){
1538
+ pgresult_clear( this );
1539
+ rb_raise( rb_eInvalidChangeOfResultFields, "number of fields changed in single row mode from %d to %d - this is a sign for intersection with another query", nfields, nfields2);
1540
+ }
1541
+
1542
+ if( yielder( self, ntuples, nfields, data ) ){
1543
+ pgresult_clear( this );
1544
+ }
1545
+
1546
+ if( gvl_PQisBusy(pgconn) ){
1547
+ /* wait for input (without blocking) before reading each result */
1548
+ pgconn_block( 0, NULL, this->connection );
1549
+ }
1550
+
1551
+ pgresult = gvl_PQgetResult(pgconn);
1552
+ if( pgresult == NULL )
1553
+ rb_raise( rb_eNoResultError, "no result received - possibly an intersection with another query");
1554
+
1555
+ this->pgresult = pgresult;
1556
+ }
1557
+
1558
+ /* never reached */
1559
+ return self;
1560
+ }
1561
+
1562
+
1563
+ /*
1564
+ * call-seq:
1565
+ * res.stream_each{ |tuple| ... }
1566
+ *
1567
+ * Invokes block for each tuple in the result set in single row mode.
1568
+ *
1569
+ * This is a convenience method for retrieving all result tuples
1570
+ * as they are transferred. It is an alternative to repeated calls of
1571
+ * PG::Connection#get_result , but given that it avoids the overhead of
1572
+ * wrapping each row into a dedicated result object, it delivers data in nearly
1573
+ * the same speed as with ordinary results.
1574
+ *
1575
+ * The base result must be in status PGRES_SINGLE_TUPLE or PGRES_TUPLES_CHUNK.
1576
+ * It iterates over all tuples until the status changes to PGRES_TUPLES_OK.
1577
+ * A PG::Error is raised for any errors from the server.
1578
+ *
1579
+ * Row description data does not change while the iteration. All value retrieval
1580
+ * methods refer to only the current row. Result#ntuples returns +1+ while
1581
+ * the iteration and +0+ after all tuples were yielded.
1582
+ *
1583
+ * Example:
1584
+ * conn.send_query( "first SQL query; second SQL query" )
1585
+ * conn.set_single_row_mode
1586
+ * conn.get_result.stream_each do |row|
1587
+ * # do something with each received row of the first query
1588
+ * end
1589
+ * conn.get_result.stream_each do |row|
1590
+ * # do something with each received row of the second query
1591
+ * end
1592
+ * conn.get_result # => nil (no more results)
1593
+ */
1594
+ static VALUE
1595
+ pgresult_stream_each(VALUE self)
1596
+ {
1597
+ return pgresult_stream_any(self, yield_hash, NULL);
1598
+ }
1599
+
1600
+ /*
1601
+ * call-seq:
1602
+ * res.stream_each_row { |row| ... }
1603
+ *
1604
+ * Yields each row of the result set in single row mode.
1605
+ * The row is a list of column values.
1606
+ *
1607
+ * This method works equally to #stream_each , but yields an Array of
1608
+ * values.
1609
+ */
1610
+ static VALUE
1611
+ pgresult_stream_each_row(VALUE self)
1612
+ {
1613
+ return pgresult_stream_any(self, yield_array, NULL);
1614
+ }
1615
+
1616
+ /*
1617
+ * call-seq:
1618
+ * res.stream_each_tuple { |tuple| ... }
1619
+ *
1620
+ * Yields each row of the result set in single row mode.
1621
+ *
1622
+ * This method works equally to #stream_each , but yields a PG::Tuple object.
1623
+ */
1624
+ static VALUE
1625
+ pgresult_stream_each_tuple(VALUE self)
1626
+ {
1627
+ /* allocate VALUEs that are shared between all streamed tuples */
1628
+ ensure_init_for_tuple(self);
1629
+
1630
+ return pgresult_stream_any(self, yield_tuple, NULL);
1631
+ }
1632
+
1633
+ /*
1634
+ * call-seq:
1635
+ * res.field_name_type = Symbol
1636
+ *
1637
+ * Set type of field names specific to this result.
1638
+ * It can be set to one of:
1639
+ * * +:string+ to use String based field names
1640
+ * * +:symbol+ to use Symbol based field names
1641
+ * * +:static_symbol+ to use pinned Symbol (can not be garbage collected) - Don't use this, it will probably be removed in future.
1642
+ *
1643
+ * The default is retrieved from PG::Connection#field_name_type , which defaults to +:string+ .
1644
+ *
1645
+ * This setting affects several result methods:
1646
+ * * keys of Hash returned by #[] , #each and #stream_each
1647
+ * * #fields
1648
+ * * #fname
1649
+ * * field names used by #tuple and #stream_each_tuple
1650
+ *
1651
+ * The type of field names can only be changed before any of the affected methods have been called.
1652
+ *
1653
+ */
1654
+ static VALUE
1655
+ pgresult_field_name_type_set(VALUE self, VALUE sym)
1656
+ {
1657
+ t_pg_result *this = pgresult_get_this(self);
1658
+
1659
+ rb_check_frozen(self);
1660
+ if( this->nfields != -1 ) rb_raise(rb_eArgError, "field names are already materialized");
1661
+
1662
+ this->flags &= ~PG_RESULT_FIELD_NAMES_MASK;
1663
+ if( sym == sym_symbol ) this->flags |= PG_RESULT_FIELD_NAMES_SYMBOL;
1664
+ else if ( sym == sym_static_symbol ) this->flags |= PG_RESULT_FIELD_NAMES_STATIC_SYMBOL;
1665
+ else if ( sym == sym_string );
1666
+ else rb_raise(rb_eArgError, "invalid argument %+"PRIsVALUE, sym);
1667
+
1668
+ return sym;
1669
+ }
1670
+
1671
+ /*
1672
+ * call-seq:
1673
+ * res.field_name_type -> Symbol
1674
+ *
1675
+ * Get type of field names.
1676
+ *
1677
+ * See description at #field_name_type=
1678
+ */
1679
+ static VALUE
1680
+ pgresult_field_name_type_get(VALUE self)
1681
+ {
1682
+ t_pg_result *this = pgresult_get_this(self);
1683
+ if( this->flags & PG_RESULT_FIELD_NAMES_SYMBOL ){
1684
+ return sym_symbol;
1685
+ } else if( this->flags & PG_RESULT_FIELD_NAMES_STATIC_SYMBOL ){
1686
+ return sym_static_symbol;
1687
+ } else {
1688
+ return sym_string;
1689
+ }
1690
+ }
1691
+
1692
+ void
1693
+ init_pg_result(void)
1694
+ {
1695
+ sym_string = ID2SYM(rb_intern("string"));
1696
+ sym_symbol = ID2SYM(rb_intern("symbol"));
1697
+ sym_static_symbol = ID2SYM(rb_intern("static_symbol"));
1698
+
1699
+ rb_cPGresult = rb_define_class_under( rb_mPG, "Result", rb_cObject );
1700
+ rb_undef_alloc_func(rb_cPGresult);
1701
+ rb_include_module(rb_cPGresult, rb_mEnumerable);
1702
+ rb_include_module(rb_cPGresult, rb_mPGconstants);
1703
+
1704
+ /****** PG::Result INSTANCE METHODS: libpq ******/
1705
+ rb_define_method(rb_cPGresult, "result_status", pgresult_result_status, 0);
1706
+ rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, -1);
1707
+ rb_define_singleton_method(rb_cPGresult, "res_status", pgresult_s_res_status, 1);
1708
+ rb_define_method(rb_cPGresult, "error_message", pgresult_error_message, 0);
1709
+ rb_define_alias( rb_cPGresult, "result_error_message", "error_message");
1710
+ rb_define_method(rb_cPGresult, "verbose_error_message", pgresult_verbose_error_message, 2);
1711
+ rb_define_alias( rb_cPGresult, "result_verbose_error_message", "verbose_error_message");
1712
+ rb_define_method(rb_cPGresult, "error_field", pgresult_error_field, 1);
1713
+ rb_define_alias( rb_cPGresult, "result_error_field", "error_field" );
1714
+ rb_define_method(rb_cPGresult, "clear", pg_result_clear, 0);
1715
+ rb_define_method(rb_cPGresult, "freeze", pg_result_freeze, 0 );
1716
+ rb_define_method(rb_cPGresult, "check", pg_result_check, 0);
1717
+ rb_define_alias (rb_cPGresult, "check_result", "check");
1718
+ rb_define_method(rb_cPGresult, "ntuples", pgresult_ntuples, 0);
1719
+ rb_define_alias(rb_cPGresult, "num_tuples", "ntuples");
1720
+ rb_define_method(rb_cPGresult, "nfields", pgresult_nfields, 0);
1721
+ rb_define_alias(rb_cPGresult, "num_fields", "nfields");
1722
+ rb_define_method(rb_cPGresult, "binary_tuples", pgresult_binary_tuples, 0);
1723
+ rb_define_method(rb_cPGresult, "fname", pgresult_fname, 1);
1724
+ rb_define_method(rb_cPGresult, "fnumber", pgresult_fnumber, 1);
1725
+ rb_define_method(rb_cPGresult, "ftable", pgresult_ftable, 1);
1726
+ rb_define_method(rb_cPGresult, "ftablecol", pgresult_ftablecol, 1);
1727
+ rb_define_method(rb_cPGresult, "fformat", pgresult_fformat, 1);
1728
+ rb_define_method(rb_cPGresult, "ftype", pgresult_ftype, 1);
1729
+ rb_define_method(rb_cPGresult, "fmod", pgresult_fmod, 1);
1730
+ rb_define_method(rb_cPGresult, "fsize", pgresult_fsize, 1);
1731
+ rb_define_method(rb_cPGresult, "getvalue", pgresult_getvalue, 2);
1732
+ rb_define_method(rb_cPGresult, "getisnull", pgresult_getisnull, 2);
1733
+ rb_define_method(rb_cPGresult, "getlength", pgresult_getlength, 2);
1734
+ rb_define_method(rb_cPGresult, "nparams", pgresult_nparams, 0);
1735
+ rb_define_method(rb_cPGresult, "paramtype", pgresult_paramtype, 1);
1736
+ rb_define_method(rb_cPGresult, "cmd_status", pgresult_cmd_status, 0);
1737
+ rb_define_method(rb_cPGresult, "cmd_tuples", pgresult_cmd_tuples, 0);
1738
+ rb_define_alias(rb_cPGresult, "cmdtuples", "cmd_tuples");
1739
+ rb_define_method(rb_cPGresult, "oid_value", pgresult_oid_value, 0);
1740
+
1741
+ /****** PG::Result INSTANCE METHODS: other ******/
1742
+ rb_define_method(rb_cPGresult, "[]", pgresult_aref, 1);
1743
+ rb_define_method(rb_cPGresult, "each", pgresult_each, 0);
1744
+ rb_define_method(rb_cPGresult, "fields", pgresult_fields, 0);
1745
+ rb_define_method(rb_cPGresult, "each_row", pgresult_each_row, 0);
1746
+ rb_define_method(rb_cPGresult, "values", pgresult_values, 0);
1747
+ rb_define_method(rb_cPGresult, "column_values", pgresult_column_values, 1);
1748
+ rb_define_method(rb_cPGresult, "field_values", pgresult_field_values, 1);
1749
+ rb_define_method(rb_cPGresult, "tuple_values", pgresult_tuple_values, 1);
1750
+ rb_define_method(rb_cPGresult, "tuple", pgresult_tuple, 1);
1751
+ rb_define_method(rb_cPGresult, "cleared?", pgresult_cleared_p, 0);
1752
+ rb_define_method(rb_cPGresult, "autoclear?", pgresult_autoclear_p, 0);
1753
+
1754
+ rb_define_method(rb_cPGresult, "type_map=", pgresult_type_map_set, 1);
1755
+ rb_define_method(rb_cPGresult, "type_map", pgresult_type_map_get, 0);
1756
+
1757
+ /****** PG::Result INSTANCE METHODS: streaming ******/
1758
+ rb_define_method(rb_cPGresult, "stream_each", pgresult_stream_each, 0);
1759
+ rb_define_method(rb_cPGresult, "stream_each_row", pgresult_stream_each_row, 0);
1760
+ rb_define_method(rb_cPGresult, "stream_each_tuple", pgresult_stream_each_tuple, 0);
1761
+
1762
+ rb_define_method(rb_cPGresult, "field_name_type=", pgresult_field_name_type_set, 1 );
1763
+ rb_define_method(rb_cPGresult, "field_name_type", pgresult_field_name_type_get, 0 );
1764
+ }