Class: PG::CompositeCoder

Inherits:
Coder
  • Object
show all
Defined in:
ext/pg_coder.c,
lib/pg/coder.rb,
ext/pg_coder.c

Overview

This is the base class for all type cast classes of PostgreSQL types, that are made up of some sub type.

See PG::TextEncoder::Array, PG::TextDecoder::Array, PG::BinaryEncoder::Array, PG::BinaryDecoder::Array, etc.

Direct Known Subclasses

CompositeDecoder, CompositeEncoder

Constant Summary

Constants inherited from Coder

PG::Coder::FORMAT_ERROR_MASK, PG::Coder::FORMAT_ERROR_TO_PARTIAL, PG::Coder::FORMAT_ERROR_TO_RAISE, PG::Coder::FORMAT_ERROR_TO_STRING, PG::Coder::TIMESTAMP_APP_LOCAL, PG::Coder::TIMESTAMP_APP_UTC, PG::Coder::TIMESTAMP_DB_LOCAL, PG::Coder::TIMESTAMP_DB_UTC

Instance Attribute Summary collapse

Attributes inherited from Coder

#name

Instance Method Summary collapse

Methods inherited from Coder

#==, #dup, #flags, #flags=, #format, #format=, #initialize, #inspect_short, #marshal_dump, #marshal_load, #oid, #oid=

Constructor Details

This class inherits a constructor from PG::Coder

Instance Attribute Details

#elements_typeObject (readonly)

Instance Method Details

#delimiterString

The character that separates values within the composite type.

Returns:

  • (String)


421
422
423
424
425
426
# File 'ext/pg_coder.c', line 421

static VALUE
pg_coder_delimiter_get(VALUE self)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
	return rb_str_new(&this->delimiter, 1);
}

#delimiter=(String) ⇒ Object

Specifies the character that separates values within the composite type. The default is a comma. This must be a single one-byte character. It is only used by text coders and ignored by binary coders.



403
404
405
406
407
408
409
410
411
412
413
# File 'ext/pg_coder.c', line 403

static VALUE
pg_coder_delimiter_set(VALUE self, VALUE delimiter)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
	rb_check_frozen(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;
}

#dimensionsInteger | nil

Get number of enforced array dimensions or nil if not set.

See #dimensions=

Returns:

  • (Integer | nil)


464
465
466
467
468
469
# File 'ext/pg_coder.c', line 464

static VALUE
pg_coder_dimensions_get(VALUE self)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
	return this->dimensions < 0 ? Qnil : INT2NUM(this->dimensions);
}

#dimensions=(Integer) ⇒ Object #dimensions=(nil) ⇒ Object

Set number of array dimensions to be encoded.

This property ensures, that this number of dimensions is always encoded. If less dimensions than this number are in the given value, an ArgumentError is raised. If more dimensions than this number are in the value, the Array value is passed to the next encoder.

Setting dimensions is especially useful, when a Record shall be encoded into an Array, since the Array encoder can not distinguish if the array shall be encoded as a higher dimension or as a record otherwise.

The default is nil.

See #dimensions



445
446
447
448
449
450
451
452
453
454
# File 'ext/pg_coder.c', line 445

static VALUE
pg_coder_dimensions_set(VALUE self, VALUE dimensions)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
	rb_check_frozen(self);
	if(!NIL_P(dimensions) && NUM2INT(dimensions) < 0)
		rb_raise( rb_eArgError, "dimensions must be nil or >= 0");
	this->dimensions = NIL_P(dimensions) ? -1 : NUM2INT(dimensions);
	return dimensions;
}

#elements_type=(coder) ⇒ Object (readonly)

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.



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'ext/pg_coder.c', line 480

static VALUE
pg_coder_elements_type_set(VALUE self, VALUE elem_type)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA( self );

	rb_check_frozen(self);
	if ( NIL_P(elem_type) ){
		this->elem = NULL;
	} else if ( rb_obj_is_kind_of(elem_type, rb_cPG_Coder) ){
		this->elem = RTYPEDDATA_DATA( 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;
}

#inspectObject



84
85
86
87
88
# File 'lib/pg/coder.rb', line 84

def inspect
	str = super
	str[-1,0] = " elements_type=#{elements_type.inspect} #{needs_quotation? ? 'needs' : 'no'} quotation#{dimensions && " #{dimensions} dimensions"}"
	str
end

#needs_quotation=(Boolean) ⇒ Object

Specifies whether the assigned #elements_type requires quotation marks to be transferred safely. Encoding with #needs_quotation=false is somewhat faster. It is only used by text coders and ignored by binary coders.

The default is true. This option is ignored for decoding of values.



371
372
373
374
375
376
377
378
# File 'ext/pg_coder.c', line 371

static VALUE
pg_coder_needs_quotation_set(VALUE self, VALUE needs_quotation)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
	rb_check_frozen(self);
	this->needs_quotation = RTEST(needs_quotation);
	return needs_quotation;
}

#needs_quotationBoolean

Specifies whether the assigned #elements_type requires quotation marks to be transferred safely.

Returns:

  • (Boolean)


387
388
389
390
391
392
# File 'ext/pg_coder.c', line 387

static VALUE
pg_coder_needs_quotation_get(VALUE self)
{
	t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
	return this->needs_quotation ? Qtrue : Qfalse;
}

#to_hObject



74
75
76
77
78
79
80
81
82
# File 'lib/pg/coder.rb', line 74

def to_h
	h = { **super,
		elements_type: elements_type,
		needs_quotation: needs_quotation?,
		delimiter: delimiter,
	}
	h[:dimensions] = dimensions if dimensions # Write only when set, for Marshal compat with pg<1.6
	h
end