123456789_123456789_123456789_123456789_123456789_

Class: PG::Coder

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Subclasses:
CompositeCoder, CompositeDecoder, CompositeEncoder, CopyCoder, CopyDecoder, CopyEncoder, RecordCoder, RecordDecoder, RecordEncoder, SimpleCoder, SimpleDecoder, SimpleEncoder, BinaryDecoder::Boolean, BinaryDecoder::Bytea, BinaryDecoder::CopyRow, BinaryDecoder::Date, BinaryDecoder::Float, BinaryDecoder::Integer, BinaryDecoder::String, BinaryDecoder::Timestamp, BinaryDecoder::TimestampLocal, BinaryDecoder::TimestampUtc, BinaryDecoder::TimestampUtcToLocal, BinaryDecoder::ToBase64, BinaryEncoder::Boolean, BinaryEncoder::CopyRow, BinaryEncoder::Date, BinaryEncoder::Float4, BinaryEncoder::Float8, BinaryEncoder::FromBase64, BinaryEncoder::Int2, BinaryEncoder::Int4, BinaryEncoder::Int8, BinaryEncoder::Timestamp, BinaryEncoder::TimestampLocal, BinaryEncoder::TimestampUtc, TextDecoder::Array, TextDecoder::Boolean, TextDecoder::Bytea, TextDecoder::CopyRow, TextDecoder::Date, TextDecoder::Float, TextDecoder::FromBase64, TextDecoder::Identifier, TextDecoder::Inet, TextDecoder::Integer, TextDecoder::JSON, TextDecoder::Numeric, TextDecoder::Record, TextDecoder::String, TextDecoder::Timestamp, TextDecoder::TimestampLocal, TextDecoder::TimestampUtc, TextDecoder::TimestampUtcToLocal, TextEncoder::Array, TextEncoder::Boolean, TextEncoder::Bytea, TextEncoder::CopyRow, TextEncoder::Date, TextEncoder::Float, TextEncoder::Identifier, TextEncoder::Inet, TextEncoder::Integer, TextEncoder::JSON, TextEncoder::Numeric, TextEncoder::QuotedLiteral, TextEncoder::Record, TextEncoder::String, TextEncoder::TimestampUtc, TextEncoder::TimestampWithTimeZone, TextEncoder::TimestampWithoutTimeZone, TextEncoder::ToBase64
Inherits: Object
Defined in: ext/pg_coder.c,
ext/pg_coder.c,
lib/pg/coder.rb

Overview

This is the base class for all type cast encoder and decoder classes.

It can be used for implicit type casts by a 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.

Constant Summary

Class Method Summary

Instance Attribute Summary

  • #flags ⇒ Integer rw

    Get current bitwise OR-ed coder flags.

  • #flags=(Integer) rw

    Set coder specific bitwise OR-ed flags.

  • #format ⇒ Integer rw

    The format code that is sent alongside with an encoded query parameter value.

  • #format=(Integer) rw

    Specifies the format code that is sent alongside with an encoded query parameter value.

  • #name rw

    Name of the coder or the corresponding data type.

  • #oid ⇒ Integer rw

    The type OID that is sent alongside with an encoded query parameter value.

  • #oid=(Integer) rw

    Specifies the type OID that is sent alongside with an encoded query parameter value.

Instance Method Summary

Constructor Details

.new(hash = nil, **kwargs) ⇒ Coder

Create a new coder object based on the attribute Hash.

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 17

def initialize(hash=nil, **kwargs)
	warn("PG::Coder.new(hash) is deprecated. Please use keyword arguments instead! Called from #{caller.first}", category: :deprecated) if hash

	(hash || kwargs).each do |key, val|
		send("#{key}=", val)
	end
end

Instance Attribute Details

#flagsInteger (rw)

Get current bitwise OR-ed coder flags.

[ GitHub ]

  
# File 'ext/pg_coder.c', line 353

static VALUE
pg_coder_flags_get(VALUE self)
{
	t_pg_coder *this = RTYPEDDATA_DATA(self);
	return INT2NUM(this->flags);
}

#flags=(Integer) (rw)

Set coder specific bitwise OR-ed flags. See the particular en- or decoder description for available flags.

The default is 0.

[ GitHub ]

  
# File 'ext/pg_coder.c', line 338

static VALUE
pg_coder_flags_set(VALUE self, VALUE flags)
{
	t_pg_coder *this = RTYPEDDATA_DATA(self);
	rb_check_frozen(self);
	this->flags = NUM2INT(flags);
	return flags;
}

#formatInteger (rw)

The format code that is sent alongside with an encoded query parameter value.

[ GitHub ]

  
# File 'ext/pg_coder.c', line 322

static VALUE
pg_coder_format_get(VALUE self)
{
	t_pg_coder *this = RTYPEDDATA_DATA(self);
	return INT2NUM(this->format);
}

#format=(Integer) (rw)

Specifies the format code that is sent alongside with an encoded query parameter value.

The default is 0.

[ GitHub ]

  
# File 'ext/pg_coder.c', line 306

static VALUE
pg_coder_format_set(VALUE self, VALUE format)
{
	t_pg_coder *this = RTYPEDDATA_DATA(self);
	rb_check_frozen(self);
	this->format = NUM2INT(format);
	return format;
}

#name (rw)

Name of the coder or the corresponding data type.

This accessor is only used in #inspect .

[ GitHub ]

#oidInteger (rw)

The type OID that is sent alongside with an encoded query parameter value.

[ GitHub ]

  
# File 'ext/pg_coder.c', line 290

static VALUE
pg_coder_oid_get(VALUE self)
{
	t_pg_coder *this = RTYPEDDATA_DATA(self);
	return UINT2NUM(this->oid);
}

#oid=(Integer) (rw)

Specifies the type OID that is sent alongside with an encoded query parameter value.

The default is 0.

[ GitHub ]

  
# File 'ext/pg_coder.c', line 274

static VALUE
pg_coder_oid_set(VALUE self, VALUE oid)
{
	t_pg_coder *this = RTYPEDDATA_DATA(self);
	rb_check_frozen(self);
	this->oid = NUM2UINT(oid);
	return oid;
}

Instance Method Details

#==(v)

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 39

def ==(v)
	self.class == v.class && to_h == v.to_h
end

#dup

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 25

def dup
	self.class.new(**to_h)
end

#inspect

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 51

def inspect
	str = self.to_s
	oid_str = " oid=#{oid}" unless oid==0
	format_str = " format=#{format}" unless format==0
	name_str = " #{name.inspect}" if name
	str[-1,0] = "#{name_str} #{oid_str}#{format_str}"
	str
end

#inspect_short

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 60

def inspect_short
	str = case format
		when 0 then "T"
		when 1 then "B"
		else format.to_s
	end
	str += "E" if respond_to?(:encode)
	str += "D" if respond_to?(:decode)

	"#{name || self.class.name}:#{str}"
end

#marshal_dump

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 43

def marshal_dump
	Marshal.dump(to_h)
end

#marshal_load(str)

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 47

def marshal_load(str)
	initialize(**Marshal.load(str))
end

#to_h

Returns coder attributes as Hash.

[ GitHub ]

  
# File 'lib/pg/coder.rb', line 30

def to_h
	{
		oid: oid,
		format: format,
		flags: flags,
		name: name,
	}
end