Module: Simple::SQL::Helpers::Decoder

Extended by:
PgArrayParser, Decoder
Included in:
Decoder
Defined in:
lib/simple/sql/helpers/decoder.rb,
lib/simple/sql/helpers/decoder.rb

Overview

private

Defined Under Namespace

Modules: HStore Classes: HashRecord, MultiColumns, SingleColumn

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(result, into:, column_info:) ⇒ Object



70
71
72
73
74
75
# File 'lib/simple/sql/helpers/decoder.rb', line 70

def self.new(result, into:, column_info:)
  if into == Hash           then HashRecord.new(column_info)
  elsif result.nfields == 1 then SingleColumn.new(column_info)
  else                           MultiColumns.new(column_info)
  end
end

Instance Method Details

#decode_value(type, s) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Naming/UncommunicativeMethodParamName



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple/sql/helpers/decoder.rb', line 10

def decode_value(type, s)
  case type
  when :unknown                       then s
  when :"character varying"           then s
  when :integer                       then Integer(s)
  when :bigint                        then Integer(s)
  when :numeric                       then Float(s)
  when :"double precision"            then Float(s)
  when :'integer[]'                   then s.scan(/-?\d+/).map { |part| Integer(part) }
  when :"character varying[]"         then parse_pg_array(s)
  when :"text[]"                      then parse_pg_array(s)
  when :"timestamp without time zone" then ::Time.parse(s)
  when :"timestamp with time zone"    then ::Time.parse(s)
  when :hstore                        then HStore.parse(s)
  when :json                          then ::JSON.parse(s)
  when :jsonb                         then ::JSON.parse(s)
  when :boolean                       then s == "t" || s == true
  else
    # unknown value, we just return the string here.
    # STDERR.puts "unknown type: #{type.inspect}"
    s
  end
end