Class: PG::BasicTypeMapForQueries

Inherits:
TypeMapByClass show all
Includes:
PG::BasicTypeRegistry::Checker
Defined in:
lib/pg/basic_type_map_for_queries.rb

Overview

Simple set of rules for type casting common Ruby types to PostgreSQL.

OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the PostgreSQL's pg_type table in PG::BasicTypeMapForQueries.new .

Query params are type casted based on the class of the given value.

Higher level libraries will most likely not make use of this class, but use their own derivation of PG::TypeMapByClass or another set of rules to choose suitable encoders and decoders for the values to be sent.

Example:

conn = PG::Connection.new
# Assign a default ruleset for type casts of input and output values.
conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn)
# Execute a query. The Integer param value is typecasted internally by PG::BinaryEncoder::Int8.
# The format of the parameter is set to 0 (text) and the OID of this parameter is set to 20 (int8).
res = conn.exec_params( "SELECT $1", [5] )

Defined Under Namespace

Classes: BinaryData, UndefinedEncoder

Constant Summary

Constants included from PG::BasicTypeRegistry::Checker

PG::BasicTypeRegistry::Checker::ValidDirections, PG::BasicTypeRegistry::Checker::ValidFormats

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TypeMapByClass

#[], #[]=, #coders

Methods included from TypeMap::DefaultTypeMappable

#default_type_map, #default_type_map=, #with_default_type_map

Constructor Details

#initialize(connection_or_coder_maps, registry: nil, if_undefined: nil) ⇒ BasicTypeMapForQueries

Create a new type map for query submission

Options:

  • registry: Custom type registry, nil for default global registry

  • if_undefined: Optional Proc object which is called, if no type for an parameter class is not defined in the registry.


50
51
52
53
54
55
56
57
58
# File 'lib/pg/basic_type_map_for_queries.rb', line 50

def initialize(connection_or_coder_maps, registry: nil, if_undefined: nil)
	@coder_maps = build_coder_maps(connection_or_coder_maps, registry: registry)
	@array_encoders_by_klass = array_encoders_by_klass
	@encode_array_as = :array
	@if_undefined = if_undefined || proc { |oid_name, format|
		raise UndefinedEncoder, "no encoder defined for type #{oid_name.inspect} format #{format}"
	}
	init_encoders
end

Instance Attribute Details

#encode_array_asObject

Returns the value of attribute encode_array_as.


86
87
88
# File 'lib/pg/basic_type_map_for_queries.rb', line 86

def encode_array_as
  @encode_array_as
end