File: pg_coder.c

package info (click to toggle)
ruby-pg 0.18.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,060 kB
  • ctags: 780
  • sloc: ansic: 6,205; ruby: 5,358; makefile: 16
file content (479 lines) | stat: -rw-r--r-- 12,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * pg_coder.c - PG::Coder class extension
 *
 */

#include "pg.h"

VALUE rb_cPG_Coder;
VALUE rb_cPG_SimpleCoder;
VALUE rb_cPG_SimpleEncoder;
VALUE rb_cPG_SimpleDecoder;
VALUE rb_cPG_CompositeCoder;
VALUE rb_cPG_CompositeEncoder;
VALUE rb_cPG_CompositeDecoder;
VALUE rb_mPG_BinaryFormatting;
static ID s_id_encode;
static ID s_id_decode;
static ID s_id_CFUNC;

static VALUE
pg_coder_allocate( VALUE klass )
{
	rb_raise( rb_eTypeError, "PG::Coder cannot be instantiated directly");
}

void
pg_coder_init_encoder( VALUE self )
{
	t_pg_coder *this = DATA_PTR( self );
	VALUE klass = rb_class_of(self);
	if( rb_const_defined( klass, s_id_CFUNC ) ){
		VALUE cfunc = rb_const_get( klass, s_id_CFUNC );
		this->enc_func = DATA_PTR(cfunc);
	} else {
		this->enc_func = NULL;
	}
	this->dec_func = NULL;
	this->coder_obj = self;
	this->oid = 0;
	this->format = 0;
	rb_iv_set( self, "@name", Qnil );
}

void
pg_coder_init_decoder( VALUE self )
{
	t_pg_coder *this = DATA_PTR( self );
	VALUE klass = rb_class_of(self);
	this->enc_func = NULL;
	if( rb_const_defined( klass, s_id_CFUNC ) ){
		VALUE cfunc = rb_const_get( klass, s_id_CFUNC );
		this->dec_func = DATA_PTR(cfunc);
	} else {
		this->dec_func = NULL;
	}
	this->coder_obj = self;
	this->oid = 0;
	this->format = 0;
	rb_iv_set( self, "@name", Qnil );
}

static VALUE
pg_simple_encoder_allocate( VALUE klass )
{
	t_pg_coder *this;
	VALUE self = Data_Make_Struct( klass, t_pg_coder, NULL, -1, this );
	pg_coder_init_encoder( self );
	return self;
}

static VALUE
pg_composite_encoder_allocate( VALUE klass )
{
	t_pg_composite_coder *this;
	VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, NULL, -1, this );
	pg_coder_init_encoder( self );
	this->elem = NULL;
	this->needs_quotation = 1;
	this->delimiter = ',';
	rb_iv_set( self, "@elements_type", Qnil );
	return self;
}

static VALUE
pg_simple_decoder_allocate( VALUE klass )
{
	t_pg_coder *this;
	VALUE self = Data_Make_Struct( klass, t_pg_coder, NULL, -1, this );
	pg_coder_init_decoder( self );
	return self;
}

static VALUE
pg_composite_decoder_allocate( VALUE klass )
{
	t_pg_composite_coder *this;
	VALUE self = Data_Make_Struct( klass, t_pg_composite_coder, NULL, -1, this );
	pg_coder_init_decoder( self );
	this->elem = NULL;
	this->needs_quotation = 1;
	this->delimiter = ',';
	rb_iv_set( self, "@elements_type", Qnil );
	return self;
}

/*
 * call-seq:
 *    coder.encode( value )
 *
 * Encodes the given Ruby object into string representation, without
 * sending data to/from the database server.
 *
 * A nil value is passed through.
 *
 */
static VALUE
pg_coder_encode(VALUE self, VALUE value)
{
	VALUE res;
	VALUE intermediate;
	int len, len2;
	t_pg_coder *this = DATA_PTR(self);

	if( NIL_P(value) )
		return Qnil;

	if( !this->enc_func ){
		rb_raise(rb_eRuntimeError, "no encoder function defined");
	}

	len = this->enc_func( this, value, NULL, &intermediate );

	if( len == -1 ){
		/* The intermediate value is a String that can be used directly. */
		OBJ_INFECT(intermediate, value);
		return intermediate;
	}

	res = rb_str_new(NULL, len);
	len2 = this->enc_func( this, value, RSTRING_PTR(res), &intermediate);
	if( len < len2 ){
		rb_bug("%s: result length of first encoder run (%i) is less than second run (%i)",
			rb_obj_classname( self ), len, len2 );
	}
	rb_str_set_len( res, len2 );
	OBJ_INFECT(res, value);

	RB_GC_GUARD(intermediate);

	return res;
}

/*
 * call-seq:
 *    coder.decode( string, tuple=nil, field=nil )
 *
 * Decodes the given string representation into a Ruby object, without
 * sending data to/from the database server.
 *
 * A nil value is passed through and non String values are expected to have
 * #to_str defined.
 *
 */
static VALUE
pg_coder_decode(int argc, VALUE *argv, VALUE self)
{
	char *val;
	VALUE tuple = -1;
	VALUE field = -1;
	VALUE res;
	t_pg_coder *this = DATA_PTR(self);

	if(argc < 1 || argc > 3){
		rb_raise(rb_eArgError, "wrong number of arguments (%i for 1..3)", argc);
	}else if(argc >= 3){
		tuple = NUM2INT(argv[1]);
		field = NUM2INT(argv[2]);
	}

	if( NIL_P(argv[0]) )
		return Qnil;

	val = StringValuePtr(argv[0]);
	if( !this->dec_func ){
		rb_raise(rb_eRuntimeError, "no decoder function defined");
	}

	res = this->dec_func(this, val, RSTRING_LEN(argv[0]), tuple, field, ENCODING_GET(argv[0]));
	OBJ_INFECT(res, argv[0]);

	return res;
}

/*
 * call-seq:
 *    coder.oid = Integer
 *
 * Specifies the type OID that is sent alongside with an encoded
 * query parameter value.
 *
 * The default is +0+.
 */
static VALUE
pg_coder_oid_set(VALUE self, VALUE oid)
{
	t_pg_coder *this = DATA_PTR(self);
	this->oid = NUM2UINT(oid);
	return oid;
}

/*
 * call-seq:
 *    coder.oid -> Integer
 *
 * The type OID that is sent alongside with an encoded
 * query parameter value.
 */
static VALUE
pg_coder_oid_get(VALUE self)
{
	t_pg_coder *this = DATA_PTR(self);
	return UINT2NUM(this->oid);
}

/*
 * call-seq:
 *    coder.format = Integer
 *
 * Specifies the format code that is sent alongside with an encoded
 * query parameter value.
 *
 * The default is +0+.
 */
static VALUE
pg_coder_format_set(VALUE self, VALUE format)
{
	t_pg_coder *this = DATA_PTR(self);
	this->format = NUM2INT(format);
	return format;
}

/*
 * call-seq:
 *    coder.format -> Integer
 *
 * The format code that is sent alongside with an encoded
 * query parameter value.
 */
static VALUE
pg_coder_format_get(VALUE self)
{
	t_pg_coder *this = DATA_PTR(self);
	return INT2NUM(this->format);
}

/*
 * call-seq:
 *    coder.needs_quotation = Boolean
 *
 * Specifies whether the assigned #elements_type requires quotation marks to
 * be transferred safely. Encoding with #needs_quotation=false is somewhat
 * faster.
 *
 * The default is +true+. This option is ignored for decoding of values.
 */
static VALUE
pg_coder_needs_quotation_set(VALUE self, VALUE needs_quotation)
{
	t_pg_composite_coder *this = DATA_PTR(self);
	this->needs_quotation = RTEST(needs_quotation);
	return needs_quotation;
}

/*
 * call-seq:
 *    coder.needs_quotation -> Boolean
 *
 * Specifies whether the assigned #elements_type requires quotation marks to
 * be transferred safely.
 */
static VALUE
pg_coder_needs_quotation_get(VALUE self)
{
	t_pg_composite_coder *this = DATA_PTR(self);
	return this->needs_quotation ? Qtrue : Qfalse;
}

/*
 * call-seq:
 *    coder.delimiter = String
 *
 * Specifies the character that separates values within the composite type.
 * The default is a comma.
 * This must be a single one-byte character.
 */
static VALUE
pg_coder_delimiter_set(VALUE self, VALUE delimiter)
{
	t_pg_composite_coder *this = DATA_PTR(self);
	StringValue(delimiter);
	if(RSTRING_LEN(delimiter) != 1)
		rb_raise( rb_eArgError, "delimiter size must be one byte");
	this->delimiter = *RSTRING_PTR(delimiter);
	return delimiter;
}

/*
 * call-seq:
 *    coder.delimiter -> String
 *
 * The character that separates values within the composite type.
 */
static VALUE
pg_coder_delimiter_get(VALUE self)
{
	t_pg_composite_coder *this = DATA_PTR(self);
	return rb_str_new(&this->delimiter, 1);
}

/*
 * call-seq:
 *    coder.elements_type = coder
 *
 * Specifies the PG::Coder object that is used to encode or decode
 * the single elementes of this composite type.
 *
 * If set to +nil+ all values are encoded and decoded as String objects.
 */
static VALUE
pg_coder_elements_type_set(VALUE self, VALUE elem_type)
{
	t_pg_composite_coder *this = DATA_PTR( self );

	if ( NIL_P(elem_type) ){
		this->elem = NULL;
	} else if ( rb_obj_is_kind_of(elem_type, rb_cPG_Coder) ){
		this->elem = DATA_PTR( elem_type );
	} else {
		rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::Coder)",
				rb_obj_classname( elem_type ) );
	}

	rb_iv_set( self, "@elements_type", elem_type );
	return elem_type;
}

void
pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
{
	VALUE cfunc_obj = Data_Wrap_Struct( rb_cObject, NULL, NULL, func );
	VALUE coder_klass = rb_define_class_under( nsp, name, base_klass );
	if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_BinaryDecoder )
		rb_include_module( coder_klass, rb_mPG_BinaryFormatting );

	rb_define_const( coder_klass, "CFUNC", cfunc_obj );

	RB_GC_GUARD(cfunc_obj);
}


static int
pg_text_enc_in_ruby(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
{
	*intermediate = rb_funcall( conv->coder_obj, s_id_encode, 1, value );
	StringValue( *intermediate );
	return -1;
}

t_pg_coder_enc_func
pg_coder_enc_func(t_pg_coder *this)
{
	if( this ){
		if( this->enc_func ){
			return this->enc_func;
		}else{
			return pg_text_enc_in_ruby;
		}
	}else{
		/* no element encoder defined -> use std to_str conversion */
		return pg_coder_enc_to_s;
	}
}

static VALUE
pg_text_dec_in_ruby(t_pg_coder *this, char *val, int len, int tuple, int field, int enc_idx)
{
	VALUE string = pg_text_dec_string(this, val, len, tuple, field, enc_idx);
	return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
}

static VALUE
pg_bin_dec_in_ruby(t_pg_coder *this, char *val, int len, int tuple, int field, int enc_idx)
{
	VALUE string = pg_bin_dec_bytea(this, val, len, tuple, field, enc_idx);
	return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
}

t_pg_coder_dec_func
pg_coder_dec_func(t_pg_coder *this, int binary)
{
	if( this ){
		if( this->dec_func ){
			return this->dec_func;
		}else{
			return binary ? pg_bin_dec_in_ruby : pg_text_dec_in_ruby;
		}
	}else{
		/* no element decoder defined -> use std String conversion */
		return binary ? pg_bin_dec_bytea : pg_text_dec_string;
	}
}


void
init_pg_coder()
{
	s_id_encode = rb_intern("encode");
	s_id_decode = rb_intern("decode");
	s_id_CFUNC = rb_intern("CFUNC");

	/* Document-class: PG::Coder < Object
	 *
	 * This is the base class for all type cast encoder and decoder classes.
	 *
	 * It can be used for implicit type casts by a PG::TypeMap or to
	 * convert single values to/from their string representation by #encode
	 * and #decode.
	 *
	 * Ruby +nil+ values are not handled by encoders, but are always transmitted
	 * as SQL +NULL+ value. Vice versa SQL +NULL+ values are not handled by decoders,
	 * but are always returned as a +nil+ value.
	 */
	rb_cPG_Coder = rb_define_class_under( rb_mPG, "Coder", rb_cObject );
	rb_define_alloc_func( rb_cPG_Coder, pg_coder_allocate );
	rb_define_method( rb_cPG_Coder, "oid=", pg_coder_oid_set, 1 );
	rb_define_method( rb_cPG_Coder, "oid", pg_coder_oid_get, 0 );
	rb_define_method( rb_cPG_Coder, "format=", pg_coder_format_set, 1 );
	rb_define_method( rb_cPG_Coder, "format", pg_coder_format_get, 0 );
	/*
	 * Name of the coder or the corresponding data type.
	 *
	 * This accessor is only used in PG::Coder#inspect .
	 */
	rb_define_attr(   rb_cPG_Coder, "name", 1, 1 );
	rb_define_method( rb_cPG_Coder, "encode", pg_coder_encode, 1 );
	rb_define_method( rb_cPG_Coder, "decode", pg_coder_decode, -1 );

	/* Document-class: PG::SimpleCoder < PG::Coder */
	rb_cPG_SimpleCoder = rb_define_class_under( rb_mPG, "SimpleCoder", rb_cPG_Coder );

	/* Document-class: PG::SimpleEncoder < PG::SimpleCoder */
	rb_cPG_SimpleEncoder = rb_define_class_under( rb_mPG, "SimpleEncoder", rb_cPG_SimpleCoder );
	rb_define_alloc_func( rb_cPG_SimpleEncoder, pg_simple_encoder_allocate );
	/* Document-class: PG::SimpleDecoder < PG::SimpleCoder */
	rb_cPG_SimpleDecoder = rb_define_class_under( rb_mPG, "SimpleDecoder", rb_cPG_SimpleCoder );
	rb_define_alloc_func( rb_cPG_SimpleDecoder, pg_simple_decoder_allocate );

	/* Document-class: PG::CompositeCoder < PG::Coder
	 *
	 * This is the base class for all type cast classes of PostgreSQL types,
	 * that are made up of some sub type.
	 */
	rb_cPG_CompositeCoder = rb_define_class_under( rb_mPG, "CompositeCoder", rb_cPG_Coder );
	rb_define_method( rb_cPG_CompositeCoder, "elements_type=", pg_coder_elements_type_set, 1 );
	rb_define_attr( rb_cPG_CompositeCoder, "elements_type", 1, 0 );
	rb_define_method( rb_cPG_CompositeCoder, "needs_quotation=", pg_coder_needs_quotation_set, 1 );
	rb_define_method( rb_cPG_CompositeCoder, "needs_quotation?", pg_coder_needs_quotation_get, 0 );
	rb_define_method( rb_cPG_CompositeCoder, "delimiter=", pg_coder_delimiter_set, 1 );
	rb_define_method( rb_cPG_CompositeCoder, "delimiter", pg_coder_delimiter_get, 0 );

	/* Document-class: PG::CompositeEncoder < PG::CompositeCoder */
	rb_cPG_CompositeEncoder = rb_define_class_under( rb_mPG, "CompositeEncoder", rb_cPG_CompositeCoder );
	rb_define_alloc_func( rb_cPG_CompositeEncoder, pg_composite_encoder_allocate );
	/* Document-class: PG::CompositeDecoder < PG::CompositeCoder */
	rb_cPG_CompositeDecoder = rb_define_class_under( rb_mPG, "CompositeDecoder", rb_cPG_CompositeCoder );
	rb_define_alloc_func( rb_cPG_CompositeDecoder, pg_composite_decoder_allocate );

	rb_mPG_BinaryFormatting = rb_define_module_under( rb_cPG_Coder, "BinaryFormatting");
}