class PG::BasicTypeMapForQueries
res = conn.exec_params( “SELECT $1”, [5] )
# The format of the parameter is set to 1 (binary) and the OID of this parameter is set to 20 (int8).
# Execute a query. The Integer param value is typecasted internally by PG::BinaryEncoder::Int8.
conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn)
# Assign a default ruleset for type casts of input and output values.
conn = PG::Connection.new
Example:
encoders and decoders for the values to be sent.
own derivation of PG::TypeMapByClass or another set of rules to choose suitable
Higher level libraries will most likely not make use of this class, but use their
Query params are type casted based on the class of the given value.
PostgreSQL’s pg_type table in PG::BasicTypeMapForQueries.new .
OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
Simple set of rules for type casting common Ruby types to PostgreSQL.
def array_encoders_by_klass
def array_encoders_by_klass DEFAULT_ARRAY_TYPE_MAP.inject({}) do |h, (klass, (format, name))| h[klass] = coder_by_name(format, :encoder, name) h end end
def coder_by_name(format, direction, name)
def coder_by_name(format, direction, name) check_format_and_direction(format, direction) @coder_maps[format][direction].coder_by_name(name) end
def get_array_type(value)
def get_array_type(value) elem = value while elem.kind_of?(Array) elem = elem.first end @array_encoders_by_klass[elem.class] || elem.class.ancestors.lazy.map{|ancestor| @array_encoders_by_klass[ancestor] }.find{|a| a } || @anyarray_encoder end
def initialize(connection)
def initialize(connection) @coder_maps = build_coder_maps(connection) populate_encoder_list @array_encoders_by_klass = array_encoders_by_klass @anyarray_encoder = coder_by_name(0, :encoder, '_any') end
def populate_encoder_list
def populate_encoder_list DEFAULT_TYPE_MAP.each do |klass, selector| if Array === selector format, name, oid_name = selector coder = coder_by_name(format, :encoder, name).dup if oid_name coder.oid = coder_by_name(format, :encoder, oid_name).oid else coder.oid = 0 end self[klass] = coder else self[klass] = selector end end end