yugabytedb-ysql 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.appveyor.yml +42 -0
  3. data/.gems +6 -0
  4. data/.gemtest +0 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +143 -0
  7. data/.gitignore +24 -0
  8. data/.hgsigs +34 -0
  9. data/.hgtags +41 -0
  10. data/.irbrc +23 -0
  11. data/.pryrc +23 -0
  12. data/.tm_properties +21 -0
  13. data/.travis.yml +49 -0
  14. data/BSDL +22 -0
  15. data/Contributors.rdoc +46 -0
  16. data/Gemfile +18 -0
  17. data/History.md +901 -0
  18. data/LICENSE +56 -0
  19. data/Manifest.txt +73 -0
  20. data/POSTGRES +23 -0
  21. data/README-OS_X.rdoc +68 -0
  22. data/README-Windows.rdoc +56 -0
  23. data/README.ja.md +302 -0
  24. data/README.md +381 -0
  25. data/Rakefile +118 -0
  26. data/Rakefile.cross +299 -0
  27. data/certs/ged.pem +24 -0
  28. data/certs/kanis@comcard.de.pem +20 -0
  29. data/certs/larskanis-2022.pem +26 -0
  30. data/certs/larskanis-2023.pem +24 -0
  31. data/certs/larskanis-2024.pem +24 -0
  32. data/ext/errorcodes.def +1044 -0
  33. data/ext/errorcodes.rb +45 -0
  34. data/ext/errorcodes.txt +497 -0
  35. data/ext/extconf.rb +174 -0
  36. data/ext/gvl_wrappers.c +21 -0
  37. data/ext/gvl_wrappers.h +264 -0
  38. data/ext/pg.c +692 -0
  39. data/ext/pg.h +392 -0
  40. data/ext/pg_binary_decoder.c +308 -0
  41. data/ext/pg_binary_encoder.c +387 -0
  42. data/ext/pg_coder.c +624 -0
  43. data/ext/pg_connection.c +4681 -0
  44. data/ext/pg_copy_coder.c +917 -0
  45. data/ext/pg_errors.c +95 -0
  46. data/ext/pg_record_coder.c +522 -0
  47. data/ext/pg_result.c +1766 -0
  48. data/ext/pg_text_decoder.c +1005 -0
  49. data/ext/pg_text_encoder.c +827 -0
  50. data/ext/pg_tuple.c +572 -0
  51. data/ext/pg_type_map.c +200 -0
  52. data/ext/pg_type_map_all_strings.c +130 -0
  53. data/ext/pg_type_map_by_class.c +271 -0
  54. data/ext/pg_type_map_by_column.c +355 -0
  55. data/ext/pg_type_map_by_mri_type.c +313 -0
  56. data/ext/pg_type_map_by_oid.c +388 -0
  57. data/ext/pg_type_map_in_ruby.c +333 -0
  58. data/ext/pg_util.c +149 -0
  59. data/ext/pg_util.h +65 -0
  60. data/ext/vc/pg.sln +26 -0
  61. data/ext/vc/pg_18/pg.vcproj +216 -0
  62. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  63. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  64. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  65. data/lib/pg/basic_type_map_for_results.rb +104 -0
  66. data/lib/pg/basic_type_registry.rb +303 -0
  67. data/lib/pg/binary_decoder/date.rb +9 -0
  68. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  69. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  70. data/lib/pg/coder.rb +106 -0
  71. data/lib/pg/connection.rb +997 -0
  72. data/lib/pg/exceptions.rb +25 -0
  73. data/lib/pg/load_balance_service.rb +406 -0
  74. data/lib/pg/result.rb +43 -0
  75. data/lib/pg/text_decoder/date.rb +18 -0
  76. data/lib/pg/text_decoder/inet.rb +9 -0
  77. data/lib/pg/text_decoder/json.rb +14 -0
  78. data/lib/pg/text_decoder/numeric.rb +9 -0
  79. data/lib/pg/text_decoder/timestamp.rb +30 -0
  80. data/lib/pg/text_encoder/date.rb +12 -0
  81. data/lib/pg/text_encoder/inet.rb +28 -0
  82. data/lib/pg/text_encoder/json.rb +14 -0
  83. data/lib/pg/text_encoder/numeric.rb +9 -0
  84. data/lib/pg/text_encoder/timestamp.rb +24 -0
  85. data/lib/pg/tuple.rb +30 -0
  86. data/lib/pg/type_map_by_column.rb +16 -0
  87. data/lib/pg/version.rb +5 -0
  88. data/lib/ysql.rb +130 -0
  89. data/misc/openssl-pg-segfault.rb +31 -0
  90. data/misc/postgres/History.txt +9 -0
  91. data/misc/postgres/Manifest.txt +5 -0
  92. data/misc/postgres/README.txt +21 -0
  93. data/misc/postgres/Rakefile +21 -0
  94. data/misc/postgres/lib/postgres.rb +16 -0
  95. data/misc/ruby-pg/History.txt +9 -0
  96. data/misc/ruby-pg/Manifest.txt +5 -0
  97. data/misc/ruby-pg/README.txt +21 -0
  98. data/misc/ruby-pg/Rakefile +21 -0
  99. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  100. data/rakelib/task_extension.rb +46 -0
  101. data/sample/array_insert.rb +20 -0
  102. data/sample/async_api.rb +102 -0
  103. data/sample/async_copyto.rb +39 -0
  104. data/sample/async_mixed.rb +56 -0
  105. data/sample/check_conn.rb +21 -0
  106. data/sample/copydata.rb +71 -0
  107. data/sample/copyfrom.rb +81 -0
  108. data/sample/copyto.rb +19 -0
  109. data/sample/cursor.rb +21 -0
  110. data/sample/disk_usage_report.rb +177 -0
  111. data/sample/issue-119.rb +94 -0
  112. data/sample/losample.rb +69 -0
  113. data/sample/minimal-testcase.rb +17 -0
  114. data/sample/notify_wait.rb +72 -0
  115. data/sample/pg_statistics.rb +285 -0
  116. data/sample/replication_monitor.rb +222 -0
  117. data/sample/test_binary_values.rb +33 -0
  118. data/sample/wal_shipper.rb +434 -0
  119. data/sample/warehouse_partitions.rb +311 -0
  120. data/yugabytedb-ysql.gemspec +33 -0
  121. metadata +232 -0
data/ext/pg.h ADDED
@@ -0,0 +1,392 @@
1
+ #ifndef __pg_h
2
+ #define __pg_h
3
+
4
+ #ifdef RUBY_EXTCONF_H
5
+ # include RUBY_EXTCONF_H
6
+ #endif
7
+
8
+ /* System headers */
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <sys/types.h>
12
+ #if !defined(_WIN32)
13
+ # include <sys/time.h>
14
+ # include <sys/socket.h>
15
+ #endif
16
+ #if defined(HAVE_UNISTD_H) && !defined(_WIN32)
17
+ # include <unistd.h>
18
+ #endif /* HAVE_UNISTD_H */
19
+
20
+ /* Ruby headers */
21
+ #include "ruby.h"
22
+ #include "ruby/st.h"
23
+ #include "ruby/encoding.h"
24
+
25
+ #define PG_ENCODING_SET_NOCHECK(obj,i) \
26
+ do { \
27
+ if ((i) < ENCODING_INLINE_MAX) \
28
+ ENCODING_SET_INLINED((obj), (i)); \
29
+ else \
30
+ rb_enc_set_index((obj), (i)); \
31
+ } while(0)
32
+
33
+ #include "ruby/io.h"
34
+
35
+ #ifndef timeradd
36
+ #define timeradd(a, b, result) \
37
+ do { \
38
+ (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
39
+ (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
40
+ if ((result)->tv_usec >= 1000000L) { \
41
+ ++(result)->tv_sec; \
42
+ (result)->tv_usec -= 1000000L; \
43
+ } \
44
+ } while (0)
45
+ #endif
46
+
47
+ #ifndef timersub
48
+ #define timersub(a, b, result) \
49
+ do { \
50
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
51
+ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
52
+ if ((result)->tv_usec < 0) { \
53
+ --(result)->tv_sec; \
54
+ (result)->tv_usec += 1000000L; \
55
+ } \
56
+ } while (0)
57
+ #endif
58
+
59
+ /* PostgreSQL headers */
60
+ #include "pg_config.h"
61
+ #include "libpq-fe.h"
62
+ #include "libpq/libpq-fs.h" /* large-object interface */
63
+ #include "pg_config_manual.h"
64
+
65
+ #if defined(_WIN32)
66
+ # include <fcntl.h>
67
+ typedef long suseconds_t;
68
+ #endif
69
+
70
+ #if defined(HAVE_VARIABLE_LENGTH_ARRAYS)
71
+ #define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) type name[(len)];
72
+ #else
73
+ #define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) \
74
+ type name[(maxlen)] = {(len)>(maxlen) ? (rb_raise(rb_eArgError, "Number of " #name " (%d) exceeds allowed maximum of " #maxlen, (len) ), (type)1) : (type)0};
75
+
76
+ #define PG_MAX_COLUMNS 4000
77
+ #endif
78
+
79
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
80
+ #define pg_compact_callback(x) (x)
81
+ #define pg_gc_location(x) x = rb_gc_location(x)
82
+ #else
83
+ #define rb_gc_mark_movable(x) rb_gc_mark(x)
84
+ #define pg_compact_callback(x) {(x)}
85
+ #define pg_gc_location(x) UNUSED(x)
86
+ #endif
87
+
88
+ /* For compatibility with ruby < 3.0 */
89
+ #ifndef RUBY_TYPED_FROZEN_SHAREABLE
90
+ #define PG_RUBY_TYPED_FROZEN_SHAREABLE 0
91
+ #else
92
+ #define PG_RUBY_TYPED_FROZEN_SHAREABLE RUBY_TYPED_FROZEN_SHAREABLE
93
+ #endif
94
+ #define PG_ENC_IDX_BITS 28
95
+
96
+ /* The data behind each PG::Connection object */
97
+ typedef struct {
98
+ PGconn *pgconn;
99
+
100
+ /* Cached IO object for the socket descriptor */
101
+ VALUE socket_io;
102
+ /* function pointers of the original libpq notice receivers */
103
+ PQnoticeReceiver default_notice_receiver;
104
+ PQnoticeProcessor default_notice_processor;
105
+ /* Proc object that receives notices as PG::Result objects */
106
+ VALUE notice_receiver;
107
+ /* Proc object that receives notices as String objects */
108
+ VALUE notice_processor;
109
+ /* Kind of PG::TypeMap object for casting query params */
110
+ VALUE type_map_for_queries;
111
+ /* Kind of PG::TypeMap object for casting result values */
112
+ VALUE type_map_for_results;
113
+ /* IO object internally used for the trace stream */
114
+ VALUE trace_stream;
115
+ /* Kind of PG::Coder object for casting ruby values to COPY rows */
116
+ VALUE encoder_for_put_copy_data;
117
+ /* Kind of PG::Coder object for casting COPY rows to ruby values */
118
+ VALUE decoder_for_get_copy_data;
119
+ /* Ruby encoding index of the client/internal encoding */
120
+ int enc_idx : PG_ENC_IDX_BITS;
121
+ /* flags controlling Symbol/String field names */
122
+ unsigned int flags : 2;
123
+ /* enable automatic flushing of send data at the end of send_query calls */
124
+ unsigned int flush_data : 1;
125
+
126
+ #if defined(_WIN32)
127
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
128
+ int ruby_sd;
129
+ #endif
130
+ } t_pg_connection;
131
+
132
+ typedef struct pg_coder t_pg_coder;
133
+ typedef struct pg_typemap t_typemap;
134
+
135
+ /* The data behind each PG::Result object */
136
+ typedef struct {
137
+ PGresult *pgresult;
138
+
139
+ /* The connection object used to build this result */
140
+ VALUE connection;
141
+
142
+ /* The TypeMap used to type cast result values */
143
+ VALUE typemap;
144
+
145
+ /* Pointer to the typemap object data. This is assumed to be
146
+ * always valid.
147
+ */
148
+ t_typemap *p_typemap;
149
+
150
+ /* Ruby encoding index of the client/internal encoding */
151
+ int enc_idx : PG_ENC_IDX_BITS;
152
+
153
+ /* 0 = PGresult is cleared by PG::Result#clear or by the GC
154
+ * 1 = PGresult is cleared internally by libpq
155
+ */
156
+ unsigned int autoclear : 1;
157
+
158
+ /* flags controlling Symbol/String field names */
159
+ unsigned int flags : 2;
160
+
161
+ /* Number of fields in fnames[] .
162
+ * Set to -1 if fnames[] is not yet initialized.
163
+ */
164
+ int nfields;
165
+
166
+ /* Size of PGresult as published to ruby memory management. */
167
+ ssize_t result_size;
168
+
169
+ /* Prefilled tuple Hash with fnames[] as keys. */
170
+ VALUE tuple_hash;
171
+
172
+ /* Hash with fnames[] to field number mapping. */
173
+ VALUE field_map;
174
+
175
+ /* List of field names as frozen String or Symbol objects.
176
+ * Only valid if nfields != -1
177
+ */
178
+ VALUE fnames[0];
179
+ } t_pg_result;
180
+
181
+
182
+ typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
183
+ typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
184
+ typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
185
+ typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
186
+ typedef int (* t_pg_fit_to_copy_get)(VALUE);
187
+ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
188
+ typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
189
+ typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
190
+
191
+ #define PG_RESULT_FIELD_NAMES_MASK 0x03
192
+ #define PG_RESULT_FIELD_NAMES_SYMBOL 0x01
193
+ #define PG_RESULT_FIELD_NAMES_STATIC_SYMBOL 0x02
194
+
195
+ #define PG_CODER_TIMESTAMP_DB_UTC 0x0
196
+ #define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
197
+ #define PG_CODER_TIMESTAMP_APP_UTC 0x0
198
+ #define PG_CODER_TIMESTAMP_APP_LOCAL 0x2
199
+ #define PG_CODER_FORMAT_ERROR_MASK 0xc
200
+ #define PG_CODER_FORMAT_ERROR_TO_RAISE 0x4
201
+ #define PG_CODER_FORMAT_ERROR_TO_STRING 0x8
202
+ #define PG_CODER_FORMAT_ERROR_TO_PARTIAL 0xc
203
+
204
+ struct pg_coder {
205
+ t_pg_coder_enc_func enc_func;
206
+ t_pg_coder_dec_func dec_func;
207
+ VALUE coder_obj;
208
+ Oid oid;
209
+ int format;
210
+ /* OR-ed values out of PG_CODER_* */
211
+ int flags;
212
+ };
213
+
214
+ typedef struct {
215
+ t_pg_coder comp;
216
+ t_pg_coder *elem;
217
+ int needs_quotation;
218
+ char delimiter;
219
+ } t_pg_composite_coder;
220
+
221
+ struct pg_typemap {
222
+ struct pg_typemap_funcs {
223
+ t_pg_fit_to_result fit_to_result;
224
+ t_pg_fit_to_query fit_to_query;
225
+ t_pg_fit_to_copy_get fit_to_copy_get;
226
+ t_pg_typecast_result typecast_result_value;
227
+ t_pg_typecast_query_param typecast_query_param;
228
+ t_pg_typecast_copy_get typecast_copy_get;
229
+ } funcs;
230
+ VALUE default_typemap;
231
+ };
232
+
233
+ typedef struct {
234
+ t_typemap typemap;
235
+ int nfields;
236
+ struct pg_tmbc_converter {
237
+ t_pg_coder *cconv;
238
+ } convs[0];
239
+ } t_tmbc;
240
+
241
+ extern const rb_data_type_t pg_typemap_type;
242
+ extern const rb_data_type_t pg_coder_type;
243
+
244
+ #include "gvl_wrappers.h"
245
+
246
+ /***************************************************************************
247
+ * Globals
248
+ **************************************************************************/
249
+
250
+ extern int pg_skip_deprecation_warning;
251
+ extern VALUE rb_mPG;
252
+ extern VALUE rb_ePGerror;
253
+ extern VALUE rb_eServerError;
254
+ extern VALUE rb_eUnableToSend;
255
+ extern VALUE rb_eConnectionBad;
256
+ extern VALUE rb_eInvalidResultStatus;
257
+ extern VALUE rb_eNoResultError;
258
+ extern VALUE rb_eInvalidChangeOfResultFields;
259
+ extern VALUE rb_mPGconstants;
260
+ extern VALUE rb_cPGconn;
261
+ extern VALUE rb_cPGresult;
262
+ extern VALUE rb_cPGlbs;
263
+ extern VALUE rb_hErrors;
264
+ extern VALUE rb_cTypeMap;
265
+ extern VALUE rb_cTypeMapAllStrings;
266
+ extern VALUE rb_mDefaultTypeMappable;
267
+ extern VALUE rb_cPG_Coder;
268
+ extern VALUE rb_cPG_SimpleEncoder;
269
+ extern VALUE rb_cPG_SimpleDecoder;
270
+ extern VALUE rb_cPG_CompositeEncoder;
271
+ extern VALUE rb_cPG_CompositeDecoder;
272
+ extern VALUE rb_cPG_CopyCoder;
273
+ extern VALUE rb_cPG_CopyEncoder;
274
+ extern VALUE rb_cPG_CopyDecoder;
275
+ extern VALUE rb_mPG_TextEncoder;
276
+ extern VALUE rb_mPG_TextDecoder;
277
+ extern VALUE rb_mPG_BinaryEncoder;
278
+ extern VALUE rb_mPG_BinaryDecoder;
279
+ extern VALUE rb_mPG_BinaryFormatting;
280
+ extern const struct pg_typemap_funcs pg_tmbc_funcs;
281
+ extern const struct pg_typemap_funcs pg_typemap_funcs;
282
+
283
+ extern VALUE pg_typemap_all_strings;
284
+
285
+ /***************************************************************************
286
+ * MACROS
287
+ **************************************************************************/
288
+
289
+ #define UNUSED(x) ((void)(x))
290
+ #define SINGLETON_ALIAS(klass,new,old) rb_define_alias(rb_singleton_class((klass)),(new),(old))
291
+
292
+
293
+ /***************************************************************************
294
+ * PROTOTYPES
295
+ **************************************************************************/
296
+ void Init_pg_ext _(( void ));
297
+
298
+ void init_pg_connection _(( void ));
299
+ void init_pg_result _(( void ));
300
+ void init_pg_errors _(( void ));
301
+ void init_pg_type_map _(( void ));
302
+ void init_pg_type_map_all_strings _(( void ));
303
+ void init_pg_type_map_by_class _(( void ));
304
+ void init_pg_type_map_by_column _(( void ));
305
+ void init_pg_type_map_by_mri_type _(( void ));
306
+ void init_pg_type_map_by_oid _(( void ));
307
+ void init_pg_type_map_in_ruby _(( void ));
308
+ void init_pg_coder _(( void ));
309
+ void init_pg_copycoder _(( void ));
310
+ void init_pg_recordcoder _(( void ));
311
+ void init_pg_text_encoder _(( void ));
312
+ void init_pg_text_decoder _(( void ));
313
+ void init_pg_binary_encoder _(( void ));
314
+ void init_pg_binary_decoder _(( void ));
315
+ void init_pg_tuple _(( void ));
316
+ VALUE lookup_error_class _(( const char * ));
317
+ VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
318
+ VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
319
+ int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
320
+ int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
321
+ t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
322
+ t_pg_coder_dec_func pg_coder_dec_func _(( t_pg_coder*, int ));
323
+ VALUE pg_define_coder _(( const char *, void *, VALUE, VALUE ));
324
+ VALUE pg_obj_to_i _(( VALUE ));
325
+ VALUE pg_tmbc_allocate _(( void ));
326
+ void pg_coder_init_encoder _(( VALUE ));
327
+ void pg_coder_init_decoder _(( VALUE ));
328
+ void pg_coder_compact _(( void * ));
329
+ char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
330
+
331
+ #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
332
+ do { \
333
+ if( (curr_ptr) + (expand_len) >= (end_ptr) ) \
334
+ (curr_ptr) = pg_rb_str_ensure_capa( (str), (expand_len), (curr_ptr), &(end_ptr) ); \
335
+ } while(0);
336
+
337
+ #define PG_RB_STR_NEW( str, curr_ptr, end_ptr ) ( \
338
+ (str) = rb_str_new( NULL, 0 ), \
339
+ (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
340
+ )
341
+
342
+ VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
343
+ VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
344
+ int pg_typemap_fit_to_copy_get _(( VALUE ));
345
+ VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
346
+ t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
347
+ VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
348
+ void pg_typemap_mark _(( void * ));
349
+ size_t pg_typemap_memsize _(( const void * ));
350
+ void pg_typemap_compact _(( void * ));
351
+
352
+ PGconn *pg_get_pgconn _(( VALUE ));
353
+ t_pg_connection *pg_get_connection _(( VALUE ));
354
+ VALUE pgconn_block _(( int, VALUE *, VALUE ));
355
+
356
+ VALUE pg_new_result _(( PGresult *, VALUE ));
357
+ VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
358
+ PGresult* pgresult_get _(( VALUE ));
359
+ VALUE pg_result_check _(( VALUE ));
360
+ VALUE pg_result_clear _(( VALUE ));
361
+ VALUE pg_tuple_new _(( VALUE, int ));
362
+
363
+ /*
364
+ * Fetch the data pointer for the result object
365
+ */
366
+ static inline t_pg_result *
367
+ pgresult_get_this( VALUE self )
368
+ {
369
+ return RTYPEDDATA_DATA(self);
370
+ }
371
+
372
+
373
+ rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
374
+ const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
375
+ rb_encoding *pg_conn_enc_get _(( PGconn * ));
376
+
377
+ void notice_receiver_proxy(void *arg, const PGresult *result);
378
+ void notice_processor_proxy(void *arg, const char *message);
379
+
380
+ /* reports if `-W' specified and PG_SKIP_DEPRECATION_WARNING environment variable isn't set
381
+ *
382
+ * message_id identifies the warning, so that it's reported only once.
383
+ */
384
+ #define pg_deprecated(message_id, format_args) \
385
+ do { \
386
+ if( !(pg_skip_deprecation_warning & (1 << message_id)) ){ \
387
+ pg_skip_deprecation_warning |= 1 << message_id; \
388
+ rb_warning format_args; \
389
+ } \
390
+ } while(0);
391
+
392
+ #endif /* end __pg_h */
@@ -0,0 +1,308 @@
1
+ /*
2
+ * pg_column_map.c - PG::ColumnMap class extension
3
+ * $Id$
4
+ *
5
+ */
6
+
7
+ #include "ruby/version.h"
8
+ #include "pg.h"
9
+ #include "pg_util.h"
10
+ #ifdef HAVE_INTTYPES_H
11
+ #include <inttypes.h>
12
+ #endif
13
+
14
+ VALUE rb_mPG_BinaryDecoder;
15
+ static VALUE s_Date;
16
+ static ID s_id_new;
17
+
18
+
19
+ /*
20
+ * Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
21
+ *
22
+ * This is a decoder class for conversion of PostgreSQL binary +bool+ type
23
+ * to Ruby +true+ or +false+ objects.
24
+ *
25
+ */
26
+ static VALUE
27
+ pg_bin_dec_boolean(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
28
+ {
29
+ if (len < 1) {
30
+ rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
31
+ }
32
+ return *val == 0 ? Qfalse : Qtrue;
33
+ }
34
+
35
+ /*
36
+ * Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
37
+ *
38
+ * This is a decoder class for conversion of PostgreSQL binary +int2+, +int4+ and +int8+ types
39
+ * to Ruby Integer objects.
40
+ *
41
+ */
42
+ static VALUE
43
+ pg_bin_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
44
+ {
45
+ switch( len ){
46
+ case 2:
47
+ return INT2NUM(read_nbo16(val));
48
+ case 4:
49
+ return LONG2NUM(read_nbo32(val));
50
+ case 8:
51
+ return LL2NUM(read_nbo64(val));
52
+ default:
53
+ rb_raise( rb_eTypeError, "wrong data for binary integer converter in tuple %d field %d length %d", tuple, field, len);
54
+ }
55
+ }
56
+
57
+ /*
58
+ * Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
59
+ *
60
+ * This is a decoder class for conversion of PostgreSQL binary +float4+ and +float8+ types
61
+ * to Ruby Float objects.
62
+ *
63
+ */
64
+ static VALUE
65
+ pg_bin_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
66
+ {
67
+ union {
68
+ float f;
69
+ int32_t i;
70
+ } swap4;
71
+ union {
72
+ double f;
73
+ int64_t i;
74
+ } swap8;
75
+
76
+ switch( len ){
77
+ case 4:
78
+ swap4.i = read_nbo32(val);
79
+ return rb_float_new(swap4.f);
80
+ case 8:
81
+ swap8.i = read_nbo64(val);
82
+ return rb_float_new(swap8.f);
83
+ default:
84
+ rb_raise( rb_eTypeError, "wrong data for BinaryFloat converter in tuple %d field %d length %d", tuple, field, len);
85
+ }
86
+ }
87
+
88
+ /*
89
+ * Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
90
+ *
91
+ * This decoder class delivers the data received from the server as binary String object.
92
+ * It is therefore suitable for conversion of PostgreSQL +bytea+ data as well as any other
93
+ * data in binary format.
94
+ *
95
+ */
96
+ VALUE
97
+ pg_bin_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
98
+ {
99
+ VALUE ret;
100
+ ret = rb_str_new( val, len );
101
+ PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
102
+ return ret;
103
+ }
104
+
105
+ /*
106
+ * Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
107
+ *
108
+ * This is a decoder class for conversion of binary +bytea+ to base64 data.
109
+ *
110
+ */
111
+ static VALUE
112
+ pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
113
+ {
114
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
115
+ t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
116
+ int encoded_len = BASE64_ENCODED_SIZE(len);
117
+ /* create a buffer of the encoded length */
118
+ VALUE out_value = rb_str_new(NULL, encoded_len);
119
+
120
+ base64_encode( RSTRING_PTR(out_value), val, len );
121
+
122
+ /* Is it a pure String conversion? Then we can directly send out_value to the user. */
123
+ if( this->comp.format == 0 && dec_func == pg_text_dec_string ){
124
+ PG_ENCODING_SET_NOCHECK( out_value, enc_idx );
125
+ return out_value;
126
+ }
127
+ if( this->comp.format == 1 && dec_func == pg_bin_dec_bytea ){
128
+ PG_ENCODING_SET_NOCHECK( out_value, rb_ascii8bit_encindex() );
129
+ return out_value;
130
+ }
131
+ out_value = dec_func(this->elem, RSTRING_PTR(out_value), encoded_len, tuple, field, enc_idx);
132
+
133
+ return out_value;
134
+ }
135
+
136
+ #define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
137
+ #define PG_INT64_MAX 0x7FFFFFFFFFFFFFFFL
138
+
139
+ /*
140
+ * Document-class: PG::BinaryDecoder::Timestamp < PG::SimpleDecoder
141
+ *
142
+ * This is a decoder class for conversion of PostgreSQL binary timestamps
143
+ * to Ruby Time objects.
144
+ *
145
+ * The following flags can be used to specify timezone interpretation:
146
+ * * +PG::Coder::TIMESTAMP_DB_UTC+ : Interpret timestamp as UTC time (default)
147
+ * * +PG::Coder::TIMESTAMP_DB_LOCAL+ : Interpret timestamp as local time
148
+ * * +PG::Coder::TIMESTAMP_APP_UTC+ : Return timestamp as UTC time (default)
149
+ * * +PG::Coder::TIMESTAMP_APP_LOCAL+ : Return timestamp as local time
150
+ *
151
+ * Example:
152
+ * deco = PG::BinaryDecoder::Timestamp.new(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
153
+ * deco.decode("\0"*8) # => 2000-01-01 01:00:00 +0100
154
+ */
155
+ static VALUE
156
+ pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
157
+ {
158
+ int64_t timestamp;
159
+ int64_t sec;
160
+ int64_t nsec;
161
+ VALUE t;
162
+
163
+ if( len != sizeof(timestamp) ){
164
+ rb_raise( rb_eTypeError, "wrong data for timestamp converter in tuple %d field %d length %d", tuple, field, len);
165
+ }
166
+
167
+ timestamp = read_nbo64(val);
168
+
169
+ switch(timestamp){
170
+ case PG_INT64_MAX:
171
+ return rb_str_new2("infinity");
172
+ case PG_INT64_MIN:
173
+ return rb_str_new2("-infinity");
174
+ default:
175
+ /* PostgreSQL's timestamp is based on year 2000 and Ruby's time is based on 1970.
176
+ * Adjust the 30 years difference. */
177
+ sec = (timestamp / 1000000) + 10957L * 24L * 3600L;
178
+ nsec = (timestamp % 1000000) * 1000;
179
+
180
+ #if (RUBY_API_VERSION_MAJOR > 2 || (RUBY_API_VERSION_MAJOR == 2 && RUBY_API_VERSION_MINOR >= 3)) && defined(NEGATIVE_TIME_T) && defined(SIZEOF_TIME_T) && SIZEOF_TIME_T >= 8
181
+ /* Fast path for time conversion */
182
+ {
183
+ struct timespec ts = {sec, nsec};
184
+ t = rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
185
+ }
186
+ #else
187
+ t = rb_funcall(rb_cTime, rb_intern("at"), 2, LL2NUM(sec), LL2NUM(nsec / 1000));
188
+ if( !(conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL) ) {
189
+ t = rb_funcall(t, rb_intern("utc"), 0);
190
+ }
191
+ #endif
192
+ if( conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
193
+ /* interpret it as local time */
194
+ t = rb_funcall(t, rb_intern("-"), 1, rb_funcall(t, rb_intern("utc_offset"), 0));
195
+ }
196
+ return t;
197
+ }
198
+ }
199
+
200
+ #define PG_INT32_MIN (-0x7FFFFFFF-1)
201
+ #define PG_INT32_MAX (0x7FFFFFFF)
202
+ #define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */
203
+ #define MONTHS_PER_YEAR 12
204
+
205
+ /* taken from PostgreSQL sources at src/backend/utils/adt/datetime.c */
206
+ void
207
+ j2date(int jd, int *year, int *month, int *day)
208
+ {
209
+ unsigned int julian;
210
+ unsigned int quad;
211
+ unsigned int extra;
212
+ int y;
213
+
214
+ julian = jd;
215
+ julian += 32044;
216
+ quad = julian / 146097;
217
+ extra = (julian - quad * 146097) * 4 + 3;
218
+ julian += 60 + quad * 3 + extra / 146097;
219
+ quad = julian / 1461;
220
+ julian -= quad * 1461;
221
+ y = julian * 4 / 1461;
222
+ julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
223
+ + 123;
224
+ y += quad * 4;
225
+ *year = y - 4800;
226
+ quad = julian * 2141 / 65536;
227
+ *day = julian - 7834 * quad / 256;
228
+ *month = (quad + 10) % MONTHS_PER_YEAR + 1;
229
+ } /* j2date() */
230
+
231
+ /*
232
+ * Document-class: PG::BinaryDecoder::Date < PG::SimpleDecoder
233
+ *
234
+ * This is a decoder class for conversion of PostgreSQL binary date
235
+ * to Ruby Date objects.
236
+ */
237
+ static VALUE
238
+ pg_bin_dec_date(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
239
+ {
240
+ int year, month, day;
241
+ int date;
242
+
243
+ if (len != 4) {
244
+ rb_raise(rb_eTypeError, "unexpected date format != 4 bytes");
245
+ }
246
+
247
+ date = read_nbo32(val);
248
+ switch(date){
249
+ case PG_INT32_MAX:
250
+ return rb_str_new2("infinity");
251
+ case PG_INT32_MIN:
252
+ return rb_str_new2("-infinity");
253
+ default:
254
+ j2date(date + POSTGRES_EPOCH_JDATE, &year, &month, &day);
255
+
256
+ return rb_funcall(s_Date, s_id_new, 3, INT2NUM(year), INT2NUM(month), INT2NUM(day));
257
+ }
258
+ }
259
+
260
+ /* called per autoload when BinaryDecoder::Date is used */
261
+ static VALUE
262
+ init_pg_bin_decoder_date(VALUE rb_mPG_BinaryDecoder)
263
+ {
264
+ rb_require("date");
265
+ s_Date = rb_const_get(rb_cObject, rb_intern("Date"));
266
+ rb_gc_register_mark_object(s_Date);
267
+ s_id_new = rb_intern("new");
268
+
269
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Date", rb_cPG_SimpleDecoder ); */
270
+ pg_define_coder( "Date", pg_bin_dec_date, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
271
+
272
+ return Qnil;
273
+ }
274
+
275
+
276
+ /*
277
+ * Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
278
+ *
279
+ * This is a decoder class for conversion of PostgreSQL text output to
280
+ * to Ruby String object. The output value will have the character encoding
281
+ * set with PG::Connection#internal_encoding= .
282
+ *
283
+ */
284
+
285
+ void
286
+ init_pg_binary_decoder(void)
287
+ {
288
+ /* This module encapsulates all decoder classes with binary input format */
289
+ rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" );
290
+ rb_define_private_method(rb_singleton_class(rb_mPG_BinaryDecoder), "init_date", init_pg_bin_decoder_date, 0);
291
+
292
+ /* Make RDoc aware of the decoder classes... */
293
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
294
+ pg_define_coder( "Boolean", pg_bin_dec_boolean, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
295
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Integer", rb_cPG_SimpleDecoder ); */
296
+ pg_define_coder( "Integer", pg_bin_dec_integer, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
297
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Float", rb_cPG_SimpleDecoder ); */
298
+ pg_define_coder( "Float", pg_bin_dec_float, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
299
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "String", rb_cPG_SimpleDecoder ); */
300
+ pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
301
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
302
+ pg_define_coder( "Bytea", pg_bin_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
303
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
304
+ pg_define_coder( "Timestamp", pg_bin_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
305
+
306
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
307
+ pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
308
+ }