class Sequel::Postgres::PGRow::Splitter

  1. lib/sequel/extensions/pg_row.rb
Parent: PGRow

This parser-like class splits the PostgreSQL row-valued/composite type output string format into an array of strings. Note this class makes no attempt to handle all input formats that PostgreSQL will accept, it only handles the output format that PostgreSQL uses.

Methods

Public Instance

  1. parse

Constants

CLOSE_PAREN = /\)/.freeze  
OPEN_PAREN = /\(/.freeze  
QUOTED_RE = /(\\.|""|[^"])*/.freeze  
QUOTE_RE = /"/.freeze  
QUOTE_SEP_RE = /"[,)]/.freeze  
REPLACE_RE = /\\(.)|"(")/.freeze  
REPLACE_WITH = '\1\2'.freeze  
SEP_RE = /[,)]/.freeze  
UNQUOTED_RE = /[^,)]*/.freeze  

Public Instance methods

parse ()

Split the stored string into an array of strings, handling the different types of quoting.

[show source]
# File lib/sequel/extensions/pg_row.rb, line 232
def parse
  return @result if @result
  values = []
  skip(OPEN_PAREN)
  if skip(CLOSE_PAREN)
    values << nil
  else
    until eos?
      if skip(QUOTE_RE)
        values << scan(QUOTED_RE).gsub(REPLACE_RE, REPLACE_WITH)
        skip(QUOTE_SEP_RE)
      else
        v = scan(UNQUOTED_RE)
        values << (v unless v.empty?)
        skip(SEP_RE)
      end
    end
  end
  values
end