Class: PG::BasicTypeRegistry
- Includes:
- Checker
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb
Overview
This class defines the mapping between PostgreSQL types and encoder/decoder classes for PG::BasicTypeMapForResults, PG::BasicTypeMapForQueries and PG::BasicTypeMapBasedOnResult.
Additional types can be added like so:
require 'pg'
require 'ipaddr'
class InetDecoder < PG::SimpleDecoder
def decode(string, tuple=nil, field=nil)
IPAddr.new(string)
end
end
class InetEncoder < PG::SimpleEncoder
def encode(ip_addr)
ip_addr.to_s
end
end
conn = PG.connect
regi = PG::BasicTypeRegistry.new.register_default_types
regi.register_type(0, 'inet', InetEncoder, InetDecoder)
conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: regi)
Defined Under Namespace
Modules: Checker Classes: CoderMap, CoderMapsBundle
Constant Summary collapse
- DEFAULT_TYPE_REGISTRY =
PG::BasicTypeRegistry.new.register_default_types
Constants included from Checker
Checker::ValidDirections, Checker::ValidFormats
Instance Method Summary collapse
-
#alias_type(format, new, old) ⇒ Object
Alias the
old
type to thenew
type. -
#coders_for(format, direction) ⇒ Object
Retrieve a Hash of all en- or decoders for a given wire format.
-
#initialize ⇒ BasicTypeRegistry
constructor
A new instance of BasicTypeRegistry.
-
#register_coder(coder) ⇒ Object
Register an encoder or decoder instance for casting a PostgreSQL type.
-
#register_default_types ⇒ Object
(also: #define_default_types)
Populate the registry with all builtin types of ruby-pg.
-
#register_type(format, name, encoder_class, decoder_class) ⇒ Object
Register the given
encoder_class
and/ordecoder_class
for casting a PostgreSQL type.
Constructor Details
#initialize ⇒ BasicTypeRegistry
Returns a new instance of BasicTypeRegistry.
165 166 167 168 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb', line 165 def initialize # The key of these hashs maps to the `typname` column from the table pg_type. @coders_by_name = [] end |
Instance Method Details
#alias_type(format, new, old) ⇒ Object
Alias the old
type to the new
type.
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb', line 201 def alias_type(format, new, old) [:encoder, :decoder].each do |ende| enc = @coders_by_name[format][ende][old] if enc @coders_by_name[format][ende][new] = enc else @coders_by_name[format][ende].delete(new) end end self end |
#coders_for(format, direction) ⇒ Object
Retrieve a Hash of all en- or decoders for a given wire format. The hash key is the name as defined in table pg_type
. The hash value is the registered coder object.
173 174 175 176 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb', line 173 def coders_for(format, direction) check_format_and_direction(format, direction) @coders_by_name[format]&.[](direction) end |
#register_coder(coder) ⇒ Object
Register an encoder or decoder instance for casting a PostgreSQL type.
Coder#name must correspond to the typname
column in the pg_type
table. Coder#format can be 0 for text format and 1 for binary.
182 183 184 185 186 187 188 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb', line 182 def register_coder(coder) h = @coders_by_name[coder.format] ||= { encoder: {}, decoder: {} } name = coder.name || raise(ArgumentError, "name of #{coder.inspect} must be defined") h[:encoder][name] = coder if coder.respond_to?(:encode) h[:decoder][name] = coder if coder.respond_to?(:decode) self end |
#register_default_types ⇒ Object Also known as: define_default_types
Populate the registry with all builtin types of ruby-pg
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb', line 214 def register_default_types register_type 0, 'int2', PG::TextEncoder::Integer, PG::TextDecoder::Integer alias_type 0, 'int4', 'int2' alias_type 0, 'int8', 'int2' alias_type 0, 'oid', 'int2' register_type 0, 'numeric', PG::TextEncoder::Numeric, PG::TextDecoder::Numeric register_type 0, 'text', PG::TextEncoder::String, PG::TextDecoder::String alias_type 0, 'varchar', 'text' alias_type 0, 'char', 'text' alias_type 0, 'bpchar', 'text' alias_type 0, 'xml', 'text' alias_type 0, 'name', 'text' # FIXME: why are we keeping these types as strings? # alias_type 'tsvector', 'text' # alias_type 'interval', 'text' # alias_type 'macaddr', 'text' # alias_type 'uuid', 'text' # # register_type 'money', OID::Money.new # There is no PG::TextEncoder::Bytea, because it's simple and more efficient to send bytea-data # in binary format, either with PG::BinaryEncoder::Bytea or in Hash param format. register_type 0, 'bytea', nil, PG::TextDecoder::Bytea register_type 0, 'bool', PG::TextEncoder::Boolean, PG::TextDecoder::Boolean # register_type 'bit', OID::Bit.new # register_type 'varbit', OID::Bit.new register_type 0, 'float4', PG::TextEncoder::Float, PG::TextDecoder::Float alias_type 0, 'float8', 'float4' register_type 0, 'timestamp', PG::TextEncoder::TimestampWithoutTimeZone, PG::TextDecoder::TimestampWithoutTimeZone register_type 0, 'timestamptz', PG::TextEncoder::TimestampWithTimeZone, PG::TextDecoder::TimestampWithTimeZone register_type 0, 'date', PG::TextEncoder::Date, PG::TextDecoder::Date # register_type 'time', OID::Time.new # # register_type 'path', OID::Text.new # register_type 'point', OID::Point.new # register_type 'polygon', OID::Text.new # register_type 'circle', OID::Text.new # register_type 'hstore', OID::Hstore.new register_type 0, 'json', PG::TextEncoder::JSON, PG::TextDecoder::JSON alias_type 0, 'jsonb', 'json' # register_type 'citext', OID::Text.new # register_type 'ltree', OID::Text.new # register_type 0, 'inet', PG::TextEncoder::Inet, PG::TextDecoder::Inet alias_type 0, 'cidr', 'inet' register_type 1, 'int2', PG::BinaryEncoder::Int2, PG::BinaryDecoder::Integer register_type 1, 'int4', PG::BinaryEncoder::Int4, PG::BinaryDecoder::Integer register_type 1, 'int8', PG::BinaryEncoder::Int8, PG::BinaryDecoder::Integer alias_type 1, 'oid', 'int2' register_type 1, 'text', PG::BinaryEncoder::String, PG::BinaryDecoder::String alias_type 1, 'varchar', 'text' alias_type 1, 'char', 'text' alias_type 1, 'bpchar', 'text' alias_type 1, 'xml', 'text' alias_type 1, 'name', 'text' register_type 1, 'bytea', PG::BinaryEncoder::Bytea, PG::BinaryDecoder::Bytea register_type 1, 'bool', PG::BinaryEncoder::Boolean, PG::BinaryDecoder::Boolean register_type 1, 'float4', nil, PG::BinaryDecoder::Float register_type 1, 'float8', nil, PG::BinaryDecoder::Float register_type 1, 'timestamp', nil, PG::BinaryDecoder::TimestampUtc register_type 1, 'timestamptz', nil, PG::BinaryDecoder::TimestampUtcToLocal self end |
#register_type(format, name, encoder_class, decoder_class) ⇒ Object
Register the given encoder_class
and/or decoder_class
for casting a PostgreSQL type.
name
must correspond to the typname
column in the pg_type
table. format
can be 0 for text format and 1 for binary.
194 195 196 197 198 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_registry.rb', line 194 def register_type(format, name, encoder_class, decoder_class) register_coder(encoder_class.new(name: name, format: format)) if encoder_class register_coder(decoder_class.new(name: name, format: format)) if decoder_class self end |