Sha256: b21702fcf029bb9fd40e5690c8b46d53392fb6c4cb167c7d5cadc151c419e0f7

Contents?: true

Size: 1.82 KB

Versions: 76

Compression:

Stored size: 1.82 KB

Contents

# -*- ruby -*-
# frozen_string_literal: true

require 'pg' unless defined?( PG )

# Simple set of rules for type casting common PostgreSQL types from Ruby
# 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::BasicTypeMapBasedOnResult.new .
#
# This class works equal to PG::BasicTypeMapForResults, but does not define decoders for
# the given result OIDs, but encoders. So it can be used to type cast field values based on
# the type OID retrieved by a separate SQL query.
#
# PG::TypeMapByOid#build_column_map(result) can be used to generate a result independent
# PG::TypeMapByColumn type map, which can subsequently be used to cast query bind parameters
# or #put_copy_data fields.
#
# Example:
#   conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[])" )
#
#   # Retrieve table OIDs per empty result set.
#   res = conn.exec( "SELECT * FROM copytable LIMIT 0" )
#   # Build a type map for common ruby to database type encoders.
#   btm = PG::BasicTypeMapBasedOnResult.new(conn)
#   # Build a PG::TypeMapByColumn with encoders suitable for copytable.
#   tm = btm.build_column_map( res )
#   row_encoder = PG::TextEncoder::CopyRow.new type_map: tm
#
#   conn.copy_data( "COPY copytable FROM STDIN", row_encoder ) do |res|
#     conn.put_copy_data ['a', 123, [5,4,3]]
#   end
# This inserts a single row into copytable with type casts from ruby to
# database types.
class PG::BasicTypeMapBasedOnResult < PG::TypeMapByOid
	include PG::BasicTypeRegistry::Checker

	def initialize(connection_or_coder_maps, registry: nil)
		@coder_maps = build_coder_maps(connection_or_coder_maps, registry: registry)

		# Populate TypeMapByOid hash with encoders
		@coder_maps.each_format(:encoder).flat_map{|f| f.coders }.each do |coder|
			add_coder(coder)
		end
	end
end

Version data entries

76 entries across 76 versions & 4 rubygems

Version Path
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_map_based_on_result.rb
cipherstash-pg-1.0.0.beta.1-x86_64-darwin-21 ./lib/pg/basic_type_map_based_on_result.rb
cipherstash-pg-1.0.0.beta.1-arm64-darwin-21 ./lib/pg/basic_type_map_based_on_result.rb
cipherstash-pg-1.0.0.beta.1-x86_64-linux ./lib/pg/basic_type_map_based_on_result.rb
cipherstash-pg-1.0.0.beta.1-x86_64-darwin-22 ./lib/pg/basic_type_map_based_on_result.rb
cipherstash-pg-1.0.0.beta.1-arm64-darwin-22 ./lib/pg/basic_type_map_based_on_result.rb
cipherstash-pg-1.0.0.beta.1-aarch64-linux ./lib/pg/basic_type_map_based_on_result.rb
pg-1.4.6-x86-mingw32 lib/pg/basic_type_map_based_on_result.rb
pg-1.4.6-x64-mingw32 lib/pg/basic_type_map_based_on_result.rb
pg-1.4.6-x64-mingw-ucrt lib/pg/basic_type_map_based_on_result.rb
pg-1.4.6 lib/pg/basic_type_map_based_on_result.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_map_based_on_result.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_map_based_on_result.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pg-1.4.5/lib/pg/basic_type_map_based_on_result.rb
pg-1.4.5-x86-mingw32 lib/pg/basic_type_map_based_on_result.rb
pg-1.4.5-x64-mingw-ucrt lib/pg/basic_type_map_based_on_result.rb
pg-1.4.5-x64-mingw32 lib/pg/basic_type_map_based_on_result.rb
pg-1.4.5 lib/pg/basic_type_map_based_on_result.rb
pg-1.4.4-x86-mingw32 lib/pg/basic_type_map_based_on_result.rb
pg-1.4.4-x64-mingw32 lib/pg/basic_type_map_based_on_result.rb