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: MultiColumns, Record, SingleColumn

Instance Method Summary collapse

Instance Method Details

#decode_value(type, s) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simple/sql/decoder.rb', line 21

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, mode = nil, into: nil) ⇒ Object



5
6
7
8
9
10
# File 'lib/simple/sql/decoder.rb', line 5

def new(result, mode = nil, into: nil)
  if mode == :record        then Record.new(result, into: into)
  elsif result.nfields == 1 then SingleColumn.new(result)
  else                      MultiColumns.new(result)
  end
end

#parse_timestamp(s) ⇒ Object



12
13
14
15
16
# File 'lib/simple/sql/decoder.rb', line 12

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