Module: Simple::SQL::Decoder

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

Overview

private

Defined Under Namespace

Modules: HStore Classes: HashRecord, MultiColumns, Record, SingleColumn, StructRecord

Instance Method Summary collapse

Instance Method Details

#decode_value(type, s) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple/sql/decoder.rb', line 25

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 parse_timestamp(s)
  when :hstore                        then HStore.parse(s)
  when :json                          then ::JSON.parse(s)
  when :jsonb                         then ::JSON.parse(s)
  when :boolean                       then s == "t"
  else
    # unknown value, we just return the string here.
    # STDERR.puts "unknown type: #{type.inspect}"
    s
  end
end

#new(result, into:) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/simple/sql/decoder.rb', line 7

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

#parse_timestamp(s) ⇒ Object



16
17
18
19
20
# File 'lib/simple/sql/decoder.rb', line 16

def parse_timestamp(s)
  r = ::Time.parse(s)
  return r if r.utc_offset == 0
  Time.gm(r.year, r.mon, r.day, r.hour, r.min, r.sec)
end