class Sequel::Postgres::HStore::Parser

  1. lib/sequel/extensions/pg_hstore.rb
Parent: HStore

Parser for PostgreSQL hstore output format.

Methods

Public Instance

  1. parse

Constants

KV_SEP_RE = /"\s*=>\s*/.freeze  
NULL_RE = /NULL/.freeze  
QUOTED_RE = /(\\"|[^"])*/.freeze  
QUOTE_RE = /"/.freeze  
REPLACE_RE = /\\(.)/.freeze  
REPLACE_WITH = '\1'.freeze  
SEP_RE = /,\s*/.freeze  

Public Instance methods

parse ()

Parse the output format that PostgreSQL uses for hstore columns. Note that this does not attempt to parse all input formats that PostgreSQL will accept. For instance, it expects all keys and non-NULL values to be quoted.

Return the resulting hash of objects. This can be called multiple times, it will cache the parsed hash on the first call and use it for subsequent calls.

[show source]
# File lib/sequel/extensions/pg_hstore.rb, line 112
def parse
  return @result if @result
  hash = {}
  while !eos?
    skip(QUOTE_RE)
    k = parse_quoted
    skip(KV_SEP_RE)
    if skip(QUOTE_RE)
      v = parse_quoted
      skip(QUOTE_RE)
    else
      scan(NULL_RE)
      v = nil
    end
    skip(SEP_RE)
    hash[k] = v
  end
  @result = hash
end