pg 1.0.0 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/ChangeLog +0 -6595
  5. data/History.rdoc +156 -0
  6. data/Manifest.txt +8 -2
  7. data/README-Windows.rdoc +4 -4
  8. data/README.ja.rdoc +1 -2
  9. data/README.rdoc +55 -9
  10. data/Rakefile +9 -7
  11. data/Rakefile.cross +58 -57
  12. data/ext/errorcodes.def +68 -0
  13. data/ext/errorcodes.rb +1 -1
  14. data/ext/errorcodes.txt +19 -2
  15. data/ext/extconf.rb +7 -5
  16. data/ext/pg.c +141 -98
  17. data/ext/pg.h +64 -21
  18. data/ext/pg_binary_decoder.c +82 -15
  19. data/ext/pg_binary_encoder.c +13 -12
  20. data/ext/pg_coder.c +73 -12
  21. data/ext/pg_connection.c +625 -346
  22. data/ext/pg_copy_coder.c +16 -8
  23. data/ext/pg_record_coder.c +491 -0
  24. data/ext/pg_result.c +571 -191
  25. data/ext/pg_text_decoder.c +606 -40
  26. data/ext/pg_text_encoder.c +185 -54
  27. data/ext/pg_tuple.c +549 -0
  28. data/ext/pg_type_map.c +1 -1
  29. data/ext/pg_type_map_all_strings.c +4 -4
  30. data/ext/pg_type_map_by_class.c +9 -4
  31. data/ext/pg_type_map_by_column.c +7 -6
  32. data/ext/pg_type_map_by_mri_type.c +1 -1
  33. data/ext/pg_type_map_by_oid.c +3 -2
  34. data/ext/pg_type_map_in_ruby.c +1 -1
  35. data/ext/{util.c → pg_util.c} +10 -10
  36. data/ext/{util.h → pg_util.h} +2 -2
  37. data/lib/pg.rb +8 -6
  38. data/lib/pg/basic_type_mapping.rb +121 -25
  39. data/lib/pg/binary_decoder.rb +23 -0
  40. data/lib/pg/coder.rb +23 -2
  41. data/lib/pg/connection.rb +22 -3
  42. data/lib/pg/constants.rb +2 -1
  43. data/lib/pg/exceptions.rb +2 -1
  44. data/lib/pg/result.rb +14 -2
  45. data/lib/pg/text_decoder.rb +21 -26
  46. data/lib/pg/text_encoder.rb +32 -8
  47. data/lib/pg/tuple.rb +30 -0
  48. data/lib/pg/type_map_by_column.rb +3 -2
  49. data/spec/helpers.rb +52 -20
  50. data/spec/pg/basic_type_mapping_spec.rb +362 -37
  51. data/spec/pg/connection_spec.rb +376 -146
  52. data/spec/pg/connection_sync_spec.rb +41 -0
  53. data/spec/pg/result_spec.rb +240 -15
  54. data/spec/pg/tuple_spec.rb +333 -0
  55. data/spec/pg/type_map_by_class_spec.rb +2 -2
  56. data/spec/pg/type_map_by_column_spec.rb +6 -2
  57. data/spec/pg/type_map_by_mri_type_spec.rb +1 -1
  58. data/spec/pg/type_map_by_oid_spec.rb +3 -3
  59. data/spec/pg/type_map_in_ruby_spec.rb +1 -1
  60. data/spec/pg/type_map_spec.rb +1 -1
  61. data/spec/pg/type_spec.rb +363 -17
  62. data/spec/pg_spec.rb +1 -1
  63. metadata +47 -47
  64. metadata.gz.sig +0 -0
data/ext/pg.h CHANGED
@@ -21,9 +21,6 @@
21
21
  #include "ruby/st.h"
22
22
  #include "ruby/encoding.h"
23
23
 
24
- /* exported by ruby-1.9.3+ but not declared */
25
- extern int rb_encdb_alias(const char *, const char *);
26
-
27
24
  #define PG_ENCODING_SET_NOCHECK(obj,i) \
28
25
  do { \
29
26
  if ((i) < ENCODING_INLINE_MAX) \
@@ -77,6 +74,12 @@ typedef long suseconds_t;
77
74
  #define PG_MAX_COLUMNS 4000
78
75
  #endif
79
76
 
77
+ #ifndef RARRAY_AREF
78
+ #define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
79
+ #endif
80
+
81
+ #define PG_ENC_IDX_BITS 28
82
+
80
83
  /* The data behind each PG::Connection object */
81
84
  typedef struct {
82
85
  PGconn *pgconn;
@@ -93,13 +96,19 @@ typedef struct {
93
96
  VALUE type_map_for_results;
94
97
  /* IO object internally used for the trace stream */
95
98
  VALUE trace_stream;
96
- /* Cached Encoding object */
97
- VALUE external_encoding;
98
99
  /* Kind of PG::Coder object for casting ruby values to COPY rows */
99
100
  VALUE encoder_for_put_copy_data;
100
101
  /* Kind of PG::Coder object for casting COPY rows to ruby values */
101
102
  VALUE decoder_for_get_copy_data;
103
+ /* Ruby encoding index of the client/internal encoding */
104
+ int enc_idx : PG_ENC_IDX_BITS;
105
+ /* flags controlling Symbol/String field names */
106
+ unsigned int flags : 2;
102
107
 
108
+ #if defined(_WIN32)
109
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
110
+ int ruby_sd;
111
+ #endif
103
112
  } t_pg_connection;
104
113
 
105
114
  typedef struct pg_coder t_pg_coder;
@@ -120,20 +129,32 @@ typedef struct {
120
129
  */
121
130
  t_typemap *p_typemap;
122
131
 
132
+ /* Ruby encoding index of the client/internal encoding */
133
+ int enc_idx : PG_ENC_IDX_BITS;
134
+
123
135
  /* 0 = PGresult is cleared by PG::Result#clear or by the GC
124
136
  * 1 = PGresult is cleared internally by libpq
125
137
  */
126
- int autoclear;
138
+ unsigned int autoclear : 1;
139
+
140
+ /* flags controlling Symbol/String field names */
141
+ unsigned int flags : 2;
127
142
 
128
143
  /* Number of fields in fnames[] .
129
144
  * Set to -1 if fnames[] is not yet initialized.
130
145
  */
131
146
  int nfields;
132
147
 
148
+ /* Size of PGresult as published to ruby memory management. */
149
+ ssize_t result_size;
150
+
133
151
  /* Prefilled tuple Hash with fnames[] as keys. */
134
152
  VALUE tuple_hash;
135
153
 
136
- /* List of field names as frozen String objects.
154
+ /* Hash with fnames[] to field number mapping. */
155
+ VALUE field_map;
156
+
157
+ /* List of field names as frozen String or Symbol objects.
137
158
  * Only valid if nfields != -1
138
159
  */
139
160
  VALUE fnames[0];
@@ -141,7 +162,7 @@ typedef struct {
141
162
 
142
163
 
143
164
  typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
144
- typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, char *, int, int, int, int);
165
+ typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
145
166
  typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
146
167
  typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
147
168
  typedef int (* t_pg_fit_to_copy_get)(VALUE);
@@ -149,12 +170,27 @@ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
149
170
  typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
150
171
  typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
151
172
 
173
+ #define PG_RESULT_FIELD_NAMES_MASK 0x03
174
+ #define PG_RESULT_FIELD_NAMES_SYMBOL 0x01
175
+ #define PG_RESULT_FIELD_NAMES_STATIC_SYMBOL 0x02
176
+
177
+ #define PG_CODER_TIMESTAMP_DB_UTC 0x0
178
+ #define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
179
+ #define PG_CODER_TIMESTAMP_APP_UTC 0x0
180
+ #define PG_CODER_TIMESTAMP_APP_LOCAL 0x2
181
+ #define PG_CODER_FORMAT_ERROR_MASK 0xc
182
+ #define PG_CODER_FORMAT_ERROR_TO_RAISE 0x4
183
+ #define PG_CODER_FORMAT_ERROR_TO_STRING 0x8
184
+ #define PG_CODER_FORMAT_ERROR_TO_PARTIAL 0xc
185
+
152
186
  struct pg_coder {
153
187
  t_pg_coder_enc_func enc_func;
154
188
  t_pg_coder_dec_func dec_func;
155
189
  VALUE coder_obj;
156
190
  Oid oid;
157
191
  int format;
192
+ /* OR-ed values out of PG_CODER_* */
193
+ int flags;
158
194
  };
159
195
 
160
196
  typedef struct {
@@ -191,6 +227,7 @@ typedef struct {
191
227
  * Globals
192
228
  **************************************************************************/
193
229
 
230
+ extern int pg_skip_deprecation_warning;
194
231
  extern VALUE rb_mPG;
195
232
  extern VALUE rb_ePGerror;
196
233
  extern VALUE rb_eServerError;
@@ -249,13 +286,15 @@ void init_pg_type_map_by_oid _(( void ));
249
286
  void init_pg_type_map_in_ruby _(( void ));
250
287
  void init_pg_coder _(( void ));
251
288
  void init_pg_copycoder _(( void ));
289
+ void init_pg_recordcoder _(( void ));
252
290
  void init_pg_text_encoder _(( void ));
253
291
  void init_pg_text_decoder _(( void ));
254
292
  void init_pg_binary_encoder _(( void ));
255
293
  void init_pg_binary_decoder _(( void ));
294
+ void init_pg_tuple _(( void ));
256
295
  VALUE lookup_error_class _(( const char * ));
257
- VALUE pg_bin_dec_bytea _(( t_pg_coder*, char *, int, int, int, int ));
258
- VALUE pg_text_dec_string _(( t_pg_coder*, char *, int, int, int, int ));
296
+ VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
297
+ VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
259
298
  int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
260
299
  int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
261
300
  t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
@@ -265,6 +304,7 @@ VALUE pg_obj_to_i _(( VALUE ));
265
304
  VALUE pg_tmbc_allocate _(( void ));
266
305
  void pg_coder_init_encoder _(( VALUE ));
267
306
  void pg_coder_init_decoder _(( VALUE ));
307
+ void pg_coder_mark _(( t_pg_coder * ));
268
308
  char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
269
309
 
270
310
  #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
@@ -278,11 +318,6 @@ char *pg_rb_str_ensure_capa _(( VALUE, long, char *,
278
318
  (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
279
319
  )
280
320
 
281
- #define PG_RB_TAINTED_STR_NEW( str, curr_ptr, end_ptr ) ( \
282
- (str) = rb_tainted_str_new( NULL, 0 ), \
283
- (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
284
- )
285
-
286
321
  VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
287
322
  VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
288
323
  int pg_typemap_fit_to_copy_get _(( VALUE ));
@@ -298,6 +333,7 @@ VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
298
333
  PGresult* pgresult_get _(( VALUE ));
299
334
  VALUE pg_result_check _(( VALUE ));
300
335
  VALUE pg_result_clear _(( VALUE ));
336
+ VALUE pg_tuple_new _(( VALUE, int ));
301
337
 
302
338
  /*
303
339
  * Fetch the data pointer for the result object
@@ -305,12 +341,7 @@ VALUE pg_result_clear _(( VALUE ));
305
341
  static inline t_pg_result *
306
342
  pgresult_get_this( VALUE self )
307
343
  {
308
- t_pg_result *this = DATA_PTR(self);
309
-
310
- if( this == NULL )
311
- rb_raise(rb_ePGerror, "result has been cleared");
312
-
313
- return this;
344
+ return RTYPEDDATA_DATA(self);
314
345
  }
315
346
 
316
347
 
@@ -322,4 +353,16 @@ rb_encoding *pg_conn_enc_get _(( PGconn * ));
322
353
  void notice_receiver_proxy(void *arg, const PGresult *result);
323
354
  void notice_processor_proxy(void *arg, const char *message);
324
355
 
356
+ /* reports if `-W' specified and PG_SKIP_DEPRECATION_WARNING environment variable isn't set
357
+ *
358
+ * message_id identifies the warning, so that it's reported only once.
359
+ */
360
+ #define pg_deprecated(message_id, format_args) \
361
+ do { \
362
+ if( !(pg_skip_deprecation_warning & (1 << message_id)) ){ \
363
+ pg_skip_deprecation_warning |= 1 << message_id; \
364
+ rb_warning format_args; \
365
+ } \
366
+ } while(0);
367
+
325
368
  #endif /* end __pg_h */
@@ -1,11 +1,12 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_binary_decoder.c,v fcf731d3dff7 2015/09/08 12:25:06 jfali $
3
+ * $Id$
4
4
  *
5
5
  */
6
6
 
7
+ #include "ruby/version.h"
7
8
  #include "pg.h"
8
- #include "util.h"
9
+ #include "pg_util.h"
9
10
  #ifdef HAVE_INTTYPES_H
10
11
  #include <inttypes.h>
11
12
  #endif
@@ -16,12 +17,12 @@ VALUE rb_mPG_BinaryDecoder;
16
17
  /*
17
18
  * Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
18
19
  *
19
- * This is a decoder class for conversion of PostgreSQL binary bool type
20
- * to Ruby true or false objects.
20
+ * This is a decoder class for conversion of PostgreSQL binary +bool+ type
21
+ * to Ruby +true+ or +false+ objects.
21
22
  *
22
23
  */
23
24
  static VALUE
24
- pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
25
+ pg_bin_dec_boolean(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
25
26
  {
26
27
  if (len < 1) {
27
28
  rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
@@ -32,12 +33,12 @@ pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, i
32
33
  /*
33
34
  * Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
34
35
  *
35
- * This is a decoder class for conversion of PostgreSQL binary int2, int4 and int8 types
36
+ * This is a decoder class for conversion of PostgreSQL binary +int2+, +int4+ and +int8+ types
36
37
  * to Ruby Integer objects.
37
38
  *
38
39
  */
39
40
  static VALUE
40
- pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
41
+ pg_bin_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
41
42
  {
42
43
  switch( len ){
43
44
  case 2:
@@ -54,12 +55,12 @@ pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, i
54
55
  /*
55
56
  * Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
56
57
  *
57
- * This is a decoder class for conversion of PostgreSQL binary float4 and float8 types
58
+ * This is a decoder class for conversion of PostgreSQL binary +float4+ and +float8+ types
58
59
  * to Ruby Float objects.
59
60
  *
60
61
  */
61
62
  static VALUE
62
- pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
63
+ pg_bin_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
63
64
  {
64
65
  union {
65
66
  float f;
@@ -86,15 +87,15 @@ pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int
86
87
  * Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
87
88
  *
88
89
  * This decoder class delivers the data received from the server as binary String object.
89
- * It is therefore suitable for conversion of PostgreSQL bytea data as well as any other
90
+ * It is therefore suitable for conversion of PostgreSQL +bytea+ data as well as any other
90
91
  * data in binary format.
91
92
  *
92
93
  */
93
94
  VALUE
94
- pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
95
+ pg_bin_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
95
96
  {
96
97
  VALUE ret;
97
- ret = rb_tainted_str_new( val, len );
98
+ ret = rb_str_new( val, len );
98
99
  PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
99
100
  return ret;
100
101
  }
@@ -102,17 +103,17 @@ pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int
102
103
  /*
103
104
  * Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
104
105
  *
105
- * This is a decoder class for conversion of binary (bytea) to base64 data.
106
+ * This is a decoder class for conversion of binary +bytea+ to base64 data.
106
107
  *
107
108
  */
108
109
  static VALUE
109
- pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
110
+ pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
110
111
  {
111
112
  t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
112
113
  t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
113
114
  int encoded_len = BASE64_ENCODED_SIZE(len);
114
115
  /* create a buffer of the encoded length */
115
- VALUE out_value = rb_tainted_str_new(NULL, encoded_len);
116
+ VALUE out_value = rb_str_new(NULL, encoded_len);
116
117
 
117
118
  base64_encode( RSTRING_PTR(out_value), val, len );
118
119
 
@@ -130,6 +131,70 @@ pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field,
130
131
  return out_value;
131
132
  }
132
133
 
134
+ #define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
135
+ #define PG_INT64_MAX 0x7FFFFFFFFFFFFFFFL
136
+
137
+ /*
138
+ * Document-class: PG::BinaryDecoder::Timestamp < PG::SimpleDecoder
139
+ *
140
+ * This is a decoder class for conversion of PostgreSQL binary timestamps
141
+ * to Ruby Time objects.
142
+ *
143
+ * The following flags can be used to specify timezone interpretation:
144
+ * * +PG::Coder::TIMESTAMP_DB_UTC+ : Interpret timestamp as UTC time (default)
145
+ * * +PG::Coder::TIMESTAMP_DB_LOCAL+ : Interpret timestamp as local time
146
+ * * +PG::Coder::TIMESTAMP_APP_UTC+ : Return timestamp as UTC time (default)
147
+ * * +PG::Coder::TIMESTAMP_APP_LOCAL+ : Return timestamp as local time
148
+ *
149
+ * Example:
150
+ * deco = PG::BinaryDecoder::Timestamp.new(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
151
+ * deco.decode("\0"*8) # => 2000-01-01 01:00:00 +0100
152
+ */
153
+ static VALUE
154
+ pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
155
+ {
156
+ int64_t timestamp;
157
+ int64_t sec;
158
+ int64_t nsec;
159
+ VALUE t;
160
+
161
+ if( len != sizeof(timestamp) ){
162
+ rb_raise( rb_eTypeError, "wrong data for timestamp converter in tuple %d field %d length %d", tuple, field, len);
163
+ }
164
+
165
+ timestamp = read_nbo64(val);
166
+
167
+ switch(timestamp){
168
+ case PG_INT64_MAX:
169
+ return rb_str_new2("infinity");
170
+ case PG_INT64_MIN:
171
+ return rb_str_new2("-infinity");
172
+ default:
173
+ /* PostgreSQL's timestamp is based on year 2000 and Ruby's time is based on 1970.
174
+ * Adjust the 30 years difference. */
175
+ sec = (timestamp / 1000000) + 10957L * 24L * 3600L;
176
+ nsec = (timestamp % 1000000) * 1000;
177
+
178
+ #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
179
+ /* Fast path for time conversion */
180
+ {
181
+ struct timespec ts = {sec, nsec};
182
+ t = rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
183
+ }
184
+ #else
185
+ t = rb_funcall(rb_cTime, rb_intern("at"), 2, LL2NUM(sec), LL2NUM(nsec / 1000));
186
+ if( !(conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL) ) {
187
+ t = rb_funcall(t, rb_intern("utc"), 0);
188
+ }
189
+ #endif
190
+ if( conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
191
+ /* interpret it as local time */
192
+ t = rb_funcall(t, rb_intern("-"), 1, rb_funcall(t, rb_intern("utc_offset"), 0));
193
+ }
194
+ return t;
195
+ }
196
+ }
197
+
133
198
  /*
134
199
  * Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
135
200
  *
@@ -156,6 +221,8 @@ init_pg_binary_decoder()
156
221
  pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
157
222
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
158
223
  pg_define_coder( "Bytea", pg_bin_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
224
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
225
+ pg_define_coder( "Timestamp", pg_bin_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
159
226
 
160
227
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
161
228
  pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
@@ -1,11 +1,11 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_binary_encoder.c,v e61a06f1f5ed 2015/12/25 21:14:21 lars $
3
+ * $Id$
4
4
  *
5
5
  */
6
6
 
7
7
  #include "pg.h"
8
- #include "util.h"
8
+ #include "pg_util.h"
9
9
  #ifdef HAVE_INTTYPES_H
10
10
  #include <inttypes.h>
11
11
  #endif
@@ -25,11 +25,12 @@ static int
25
25
  pg_bin_enc_boolean(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
26
26
  {
27
27
  char mybool;
28
- switch(value){
29
- case Qtrue : mybool = 1; break;
30
- case Qfalse : mybool = 0; break;
31
- default :
32
- rb_raise( rb_eTypeError, "wrong data for binary boolean converter" );
28
+ if (value == Qtrue) {
29
+ mybool = 1;
30
+ } else if (value == Qfalse) {
31
+ mybool = 0;
32
+ } else {
33
+ rb_raise( rb_eTypeError, "wrong data for binary boolean converter" );
33
34
  }
34
35
  if(out) *out = mybool;
35
36
  return 1;
@@ -38,7 +39,7 @@ pg_bin_enc_boolean(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate
38
39
  /*
39
40
  * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
40
41
  *
41
- * This is the encoder class for the PostgreSQL int2 type.
42
+ * This is the encoder class for the PostgreSQL +int2+ (alias +smallint+) type.
42
43
  *
43
44
  * Non-Number values are expected to have method +to_i+ defined.
44
45
  *
@@ -55,9 +56,9 @@ pg_bin_enc_int2(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, i
55
56
  }
56
57
 
57
58
  /*
58
- * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
59
+ * Document-class: PG::BinaryEncoder::Int4 < PG::SimpleEncoder
59
60
  *
60
- * This is the encoder class for the PostgreSQL int4 type.
61
+ * This is the encoder class for the PostgreSQL +int4+ (alias +integer+) type.
61
62
  *
62
63
  * Non-Number values are expected to have method +to_i+ defined.
63
64
  *
@@ -74,9 +75,9 @@ pg_bin_enc_int4(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, i
74
75
  }
75
76
 
76
77
  /*
77
- * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
78
+ * Document-class: PG::BinaryEncoder::Int8 < PG::SimpleEncoder
78
79
  *
79
- * This is the encoder class for the PostgreSQL int8 type.
80
+ * This is the encoder class for the PostgreSQL +int8+ (alias +bigint+) type.
80
81
  *
81
82
  * Non-Number values are expected to have method +to_i+ defined.
82
83
  *
@@ -38,6 +38,7 @@ pg_coder_init_encoder( VALUE self )
38
38
  this->coder_obj = self;
39
39
  this->oid = 0;
40
40
  this->format = 0;
41
+ this->flags = 0;
41
42
  rb_iv_set( self, "@name", Qnil );
42
43
  }
43
44
 
@@ -56,14 +57,27 @@ pg_coder_init_decoder( VALUE self )
56
57
  this->coder_obj = self;
57
58
  this->oid = 0;
58
59
  this->format = 0;
60
+ this->flags = 0;
59
61
  rb_iv_set( self, "@name", Qnil );
60
62
  }
61
63
 
64
+ void
65
+ pg_coder_mark(t_pg_coder *this)
66
+ {
67
+ rb_gc_mark(this->coder_obj);
68
+ }
69
+
70
+ static void
71
+ pg_composite_coder_mark(t_pg_composite_coder *this)
72
+ {
73
+ pg_coder_mark(&this->comp);
74
+ }
75
+
62
76
  static VALUE
63
77
  pg_simple_encoder_allocate( VALUE klass )
64
78
  {
65
79
  t_pg_coder *this;
66
- VALUE self = Data_Make_Struct( klass, t_pg_coder, NULL, -1, this );
80
+ VALUE self = Data_Make_Struct( klass, t_pg_coder, pg_coder_mark, -1, this );
67
81
  pg_coder_init_encoder( self );
68
82
  return self;
69
83
  }
@@ -72,7 +86,7 @@ static VALUE
72
86
  pg_composite_encoder_allocate( VALUE klass )
73
87
  {
74
88
  t_pg_composite_coder *this;
75
- VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, NULL, -1, this );
89
+ VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, pg_composite_coder_mark, -1, this );
76
90
  pg_coder_init_encoder( self );
77
91
  this->elem = NULL;
78
92
  this->needs_quotation = 1;
@@ -85,7 +99,7 @@ static VALUE
85
99
  pg_simple_decoder_allocate( VALUE klass )
86
100
  {
87
101
  t_pg_coder *this;
88
- VALUE self = Data_Make_Struct( klass, t_pg_coder, NULL, -1, this );
102
+ VALUE self = Data_Make_Struct( klass, t_pg_coder, pg_coder_mark, -1, this );
89
103
  pg_coder_init_decoder( self );
90
104
  return self;
91
105
  }
@@ -94,7 +108,7 @@ static VALUE
94
108
  pg_composite_decoder_allocate( VALUE klass )
95
109
  {
96
110
  t_pg_composite_coder *this;
97
- VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, NULL, -1, this );
111
+ VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, pg_composite_coder_mark, -1, this );
98
112
  pg_coder_init_decoder( self );
99
113
  this->elem = NULL;
100
114
  this->needs_quotation = 1;
@@ -143,7 +157,6 @@ pg_coder_encode(int argc, VALUE *argv, VALUE self)
143
157
 
144
158
  if( len == -1 ){
145
159
  /* The intermediate value is a String that can be used directly. */
146
- OBJ_INFECT(intermediate, value);
147
160
  return intermediate;
148
161
  }
149
162
 
@@ -155,7 +168,6 @@ pg_coder_encode(int argc, VALUE *argv, VALUE self)
155
168
  rb_obj_classname( self ), len, len2 );
156
169
  }
157
170
  rb_str_set_len( res, len2 );
158
- OBJ_INFECT(res, value);
159
171
 
160
172
  RB_GC_GUARD(intermediate);
161
173
 
@@ -192,13 +204,16 @@ pg_coder_decode(int argc, VALUE *argv, VALUE self)
192
204
  if( NIL_P(argv[0]) )
193
205
  return Qnil;
194
206
 
195
- val = StringValuePtr(argv[0]);
207
+ if( this->format == 0 ){
208
+ val = StringValueCStr(argv[0]);
209
+ }else{
210
+ val = StringValuePtr(argv[0]);
211
+ }
196
212
  if( !this->dec_func ){
197
213
  rb_raise(rb_eRuntimeError, "no decoder function defined");
198
214
  }
199
215
 
200
216
  res = this->dec_func(this, val, RSTRING_LEN(argv[0]), tuple, field, ENCODING_GET(argv[0]));
201
- OBJ_INFECT(res, argv[0]);
202
217
 
203
218
  return res;
204
219
  }
@@ -265,6 +280,36 @@ pg_coder_format_get(VALUE self)
265
280
  return INT2NUM(this->format);
266
281
  }
267
282
 
283
+ /*
284
+ * call-seq:
285
+ * coder.flags = Integer
286
+ *
287
+ * Set coder specific bitwise OR-ed flags.
288
+ * See the particular en- or decoder description for available flags.
289
+ *
290
+ * The default is +0+.
291
+ */
292
+ static VALUE
293
+ pg_coder_flags_set(VALUE self, VALUE flags)
294
+ {
295
+ t_pg_coder *this = DATA_PTR(self);
296
+ this->flags = NUM2INT(flags);
297
+ return flags;
298
+ }
299
+
300
+ /*
301
+ * call-seq:
302
+ * coder.flags -> Integer
303
+ *
304
+ * Get current bitwise OR-ed coder flags.
305
+ */
306
+ static VALUE
307
+ pg_coder_flags_get(VALUE self)
308
+ {
309
+ t_pg_coder *this = DATA_PTR(self);
310
+ return INT2NUM(this->flags);
311
+ }
312
+
268
313
  /*
269
314
  * call-seq:
270
315
  * coder.needs_quotation = Boolean
@@ -364,6 +409,11 @@ pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
364
409
  if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_BinaryDecoder )
365
410
  rb_include_module( coder_klass, rb_mPG_BinaryFormatting );
366
411
 
412
+ if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_TextEncoder )
413
+ rb_define_method( coder_klass, "encode", pg_coder_encode, -1 );
414
+ if( nsp==rb_mPG_BinaryDecoder || nsp==rb_mPG_TextDecoder )
415
+ rb_define_method( coder_klass, "decode", pg_coder_decode, -1 );
416
+
367
417
  rb_define_const( coder_klass, "CFUNC", cfunc_obj );
368
418
 
369
419
  RB_GC_GUARD(cfunc_obj);
@@ -403,14 +453,14 @@ pg_coder_enc_func(t_pg_coder *this)
403
453
  }
404
454
 
405
455
  static VALUE
406
- pg_text_dec_in_ruby(t_pg_coder *this, char *val, int len, int tuple, int field, int enc_idx)
456
+ pg_text_dec_in_ruby(t_pg_coder *this, const char *val, int len, int tuple, int field, int enc_idx)
407
457
  {
408
458
  VALUE string = pg_text_dec_string(this, val, len, tuple, field, enc_idx);
409
459
  return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
410
460
  }
411
461
 
412
462
  static VALUE
413
- pg_bin_dec_in_ruby(t_pg_coder *this, char *val, int len, int tuple, int field, int enc_idx)
463
+ pg_bin_dec_in_ruby(t_pg_coder *this, const char *val, int len, int tuple, int field, int enc_idx)
414
464
  {
415
465
  VALUE string = pg_bin_dec_bytea(this, val, len, tuple, field, enc_idx);
416
466
  return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
@@ -457,14 +507,25 @@ init_pg_coder()
457
507
  rb_define_method( rb_cPG_Coder, "oid", pg_coder_oid_get, 0 );
458
508
  rb_define_method( rb_cPG_Coder, "format=", pg_coder_format_set, 1 );
459
509
  rb_define_method( rb_cPG_Coder, "format", pg_coder_format_get, 0 );
510
+ rb_define_method( rb_cPG_Coder, "flags=", pg_coder_flags_set, 1 );
511
+ rb_define_method( rb_cPG_Coder, "flags", pg_coder_flags_get, 0 );
512
+
513
+ /* define flags to be used with PG::Coder#flags= */
514
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_DB_UTC", INT2NUM(PG_CODER_TIMESTAMP_DB_UTC));
515
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_DB_LOCAL", INT2NUM(PG_CODER_TIMESTAMP_DB_LOCAL));
516
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_APP_UTC", INT2NUM(PG_CODER_TIMESTAMP_APP_UTC));
517
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_APP_LOCAL", INT2NUM(PG_CODER_TIMESTAMP_APP_LOCAL));
518
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_MASK", INT2NUM(PG_CODER_FORMAT_ERROR_MASK));
519
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_RAISE", INT2NUM(PG_CODER_FORMAT_ERROR_TO_RAISE));
520
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_STRING", INT2NUM(PG_CODER_FORMAT_ERROR_TO_STRING));
521
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_PARTIAL", INT2NUM(PG_CODER_FORMAT_ERROR_TO_PARTIAL));
522
+
460
523
  /*
461
524
  * Name of the coder or the corresponding data type.
462
525
  *
463
526
  * This accessor is only used in PG::Coder#inspect .
464
527
  */
465
528
  rb_define_attr( rb_cPG_Coder, "name", 1, 1 );
466
- rb_define_method( rb_cPG_Coder, "encode", pg_coder_encode, -1 );
467
- rb_define_method( rb_cPG_Coder, "decode", pg_coder_decode, -1 );
468
529
 
469
530
  /* Document-class: PG::SimpleCoder < PG::Coder */
470
531
  rb_cPG_SimpleCoder = rb_define_class_under( rb_mPG, "SimpleCoder", rb_cPG_Coder );