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
108 def parse
109   return @result if @result
110   hash = {}
111   while !eos?
112     skip(/"/)
113     k = parse_quoted
114     skip(/"\s*=>\s*/)
115     if skip(/"/)
116       v = parse_quoted
117       skip(/"/)
118     else
119       scan(/NULL/)
120       v = nil
121     end
122     skip(/,\s*/)
123     hash[k] = v
124   end
125   @result = hash
126 end

Private Instance Methods

parse_quoted() click to toggle source

Parse and unescape a quoted key/value.

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