class Sequel::Postgres::PGArray::Parser

PostgreSQL array parser that handles PostgreSQL array output format. Note that does not handle all forms out input that PostgreSQL will accept, and it will not raise an error for all forms of invalid input.

Public Class Methods

new(source, converter=nil) click to toggle source

Set the source for the input, and any converter callable to call with objects to be created. For nested parsers the source may contain text after the end current parse, which will be ignored.

Calls superclass method
    # File lib/sequel/extensions/pg_array.rb
314 def initialize(source, converter=nil)
315   super(source)
316   @converter = converter 
317   @stack = [[]]
318   @encoding = string.encoding
319   @recorded = String.new.force_encoding(@encoding)
320 end

Public Instance Methods

new_entry(include_empty=false) click to toggle source

Take the buffer of recorded characters and add it to the array of entries, and use a new buffer for recorded characters.

    # File lib/sequel/extensions/pg_array.rb
324 def new_entry(include_empty=false)
325   if !@recorded.empty? || include_empty
326     entry = @recorded
327     if entry == 'NULL' && !include_empty
328       entry = nil
329     elsif @converter
330       entry = @converter.call(entry)
331     end
332     @stack.last.push(entry)
333     @recorded = String.new.force_encoding(@encoding)
334   end
335 end
parse() click to toggle source

Parse the input character by character, returning an array of parsed (and potentially converted) objects.

    # File lib/sequel/extensions/pg_array.rb
339 def parse
340   raise Sequel::Error, "invalid array, empty string" if eos?
341   raise Sequel::Error, "invalid array, doesn't start with {" unless scan(/((\[\d+:\d+\])+=)?\{/)
342 
343   while !eos?
344     char = scan(/[{}",]|[^{}",]+/)
345     if char == ','
346       # Comma outside quoted string indicates end of current entry
347       new_entry
348     elsif char == '"'
349       raise Sequel::Error, "invalid array, opening quote with existing recorded data" unless @recorded.empty?
350       while true
351         char = scan(/["\\]|[^"\\]+/)
352         if char == '\\'
353           @recorded << getch
354         elsif char == '"'
355           n = peek(1)
356           raise Sequel::Error, "invalid array, closing quote not followed by comma or closing brace" unless n == ',' || n == '}'
357           break
358         else
359           @recorded << char
360         end
361       end
362       new_entry(true)
363     elsif char == '{'
364       raise Sequel::Error, "invalid array, opening brace with existing recorded data" unless @recorded.empty?
365 
366       # Start of new array, add it to the stack
367       new = []
368       @stack.last << new
369       @stack << new
370     elsif char == '}'
371       # End of current array, add current entry to the current array
372       new_entry
373 
374       if @stack.length == 1
375         raise Sequel::Error, "array parsing finished without parsing entire string" unless eos?
376 
377         # Top level of array, parsing should be over.
378         # Pop current array off stack and return it as result
379         return @stack.pop
380       else
381         # Nested array, pop current array off stack
382         @stack.pop
383       end
384     else
385       # Add the character to the recorded character buffer.
386       @recorded << char
387     end
388   end
389 
390   raise Sequel::Error, "array parsing finished with array unclosed"
391 end