Class: PG::Coder

Inherits:
Object
  • 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 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.

Direct Known Subclasses

CompositeCoder, CopyCoder, RecordCoder, SimpleCoder

Defined Under Namespace

Modules: BinaryFormatting

Constant Summary collapse

TIMESTAMP_DB_UTC =

:Coder#flags=

define flags to be used with PG
TIMESTAMP_DB_LOCAL =
INT2NUM(PG_CODER_TIMESTAMP_DB_LOCAL)
TIMESTAMP_APP_UTC =
INT2NUM(PG_CODER_TIMESTAMP_APP_UTC)
TIMESTAMP_APP_LOCAL =
INT2NUM(PG_CODER_TIMESTAMP_APP_LOCAL)
FORMAT_ERROR_MASK =
INT2NUM(PG_CODER_FORMAT_ERROR_MASK)
FORMAT_ERROR_TO_RAISE =
INT2NUM(PG_CODER_FORMAT_ERROR_TO_RAISE)
FORMAT_ERROR_TO_STRING =
INT2NUM(PG_CODER_FORMAT_ERROR_TO_STRING)
FORMAT_ERROR_TO_PARTIAL =
INT2NUM(PG_CODER_FORMAT_ERROR_TO_PARTIAL)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil, **kwargs) ⇒ Coder

Create a new coder object based on the attribute Hash.



17
18
19
20
21
22
23
# 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

#nameObject

Name of the coder or the corresponding data type.

This accessor is only used in PG::Coder#inspect .

Instance Method Details

#==(v) ⇒ Object



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

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

#dupObject



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

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

#flagsInteger

Get current bitwise OR-ed coder flags.

Returns:

  • (Integer)


353
354
355
356
357
358
# 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) ⇒ Object

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

The default is 0.



338
339
340
341
342
343
344
345
# 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

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

Returns:

  • (Integer)


322
323
324
325
326
327
# 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) ⇒ Object

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

The default is 0.



306
307
308
309
310
311
312
313
# 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;
}

#inspectObject



51
52
53
54
55
56
57
58
# 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_shortObject



60
61
62
63
64
65
66
67
68
69
70
# 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_dumpObject



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

def marshal_dump
	Marshal.dump(to_h)
end

#marshal_load(str) ⇒ Object



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

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

#oidInteger

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

Returns:

  • (Integer)


290
291
292
293
294
295
# 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) ⇒ Object

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

The default is 0.



274
275
276
277
278
279
280
281
# 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;
}

#to_hObject

Returns coder attributes as Hash.



30
31
32
33
34
35
36
37
# File 'lib/pg/coder.rb', line 30

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