class Sequel::Postgres::HStore::Parser

Parser for PostgreSQL hstore output format.

Public Instance Methods

parse() click to toggle source

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.

    # File lib/sequel/extensions/pg_hstore.rb
102 def parse
103   return @result if @result
104   hash = {}
105   while !eos?
106     skip(/"/)
107     k = parse_quoted
108     skip(/"\s*=>\s*/)
109     if skip(/"/)
110       v = parse_quoted
111       skip(/"/)
112     else
113       scan(/NULL/)
114       v = nil
115     end
116     skip(/,\s*/)
117     hash[k] = v
118   end
119   @result = hash
120 end

Private Instance Methods

parse_quoted() click to toggle source

Parse and unescape a quoted key/value.

    # File lib/sequel/extensions/pg_hstore.rb
125 def parse_quoted
126   scan(/(\\"|[^"])*/).gsub(/\\(.)/, '\1')
127 end