class Sequel::Postgres::HStore

Constants

COMMA
DEFAULT_PROC

Default proc used for all underlying HStore hashes, so that even if you grab the underlying hash, it will still convert non-string keys to strings during lookup.

ESCAPE_RE
ESCAPE_REPLACE
HSTORE_CAST
KV_SEP
NULL
QUOTE

Public Class Methods

_load(args) click to toggle source

Use custom marshal loading, since underlying hash uses a default proc.

# File lib/sequel/extensions/pg_hstore.rb, line 208
def self._load(args)
  new(Hash[Marshal.load(args)])
end
parse(str) click to toggle source

Parse the given string into an HStore, assuming the str is in PostgreSQL hstore output format.

# File lib/sequel/extensions/pg_hstore.rb, line 214
def self.parse(str)
  new(Parser.new(str).parse)
end

Public Instance Methods

_dump(*) click to toggle source

Use custom marshal dumping, since underlying hash uses a default proc.

# File lib/sequel/extensions/pg_hstore.rb, line 240
def _dump(*)
  Marshal.dump(to_a)
end
fetch(key, *args, &block) click to toggle source

Override to force the key argument to a string.

Calls superclass method
# File lib/sequel/extensions/pg_hstore.rb, line 245
def fetch(key, *args, &block)
  super(key.to_s, *args, &block)
end
merge(hash, &block) click to toggle source

Convert the input hash to string keys and values before merging, and return a new HStore instance with the merged hash.

Calls superclass method
# File lib/sequel/extensions/pg_hstore.rb, line 251
def merge(hash, &block)
  self.class.new(super(convert_hash(hash), &block))
end
op() click to toggle source

Wrap the receiver in an HStoreOp so you can easily use the PostgreSQL hstore functions and operators with it.

# File lib/sequel/extensions/pg_hstore_ops.rb, line 307
def op
  HStoreOp.new(self)
end
sql_literal_append(ds, sql) click to toggle source

Append a literalize version of the hstore to the sql.

# File lib/sequel/extensions/pg_hstore.rb, line 259
def sql_literal_append(ds, sql)
  ds.literal_append(sql, unquoted_literal)
  sql << HSTORE_CAST
end
unquoted_literal() click to toggle source

Return a string containing the unquoted, unstring-escaped literal version of the hstore. Separated out for use by the bound argument code.

# File lib/sequel/extensions/pg_hstore.rb, line 267
def unquoted_literal
  str = ''
  comma = false
  commas = COMMA
  quote = QUOTE
  kv_sep = KV_SEP
  null = NULL
  each do |k, v|
    str << commas if comma
    str << quote << escape_value(k) << quote
    str << kv_sep
    if v.nil?
      str << null
    else
      str << quote << escape_value(v) << quote
    end
    comma = true
  end
  str
end

Private Instance Methods

convert_hash(h) click to toggle source

Return a new hash based on the input hash with string keys and string or nil values.

# File lib/sequel/extensions/pg_hstore.rb, line 292
def convert_hash(h)
  hash = Hash.new(&DEFAULT_PROC)
  h.each{|k,v| hash[k.to_s] = convert_value(v)}
  hash
end
convert_value(v) click to toggle source

Return value v as a string unless it is already nil.

# File lib/sequel/extensions/pg_hstore.rb, line 299
def convert_value(v)
  v.to_s unless v.nil?
end
escape_value(k) click to toggle source

Escape key/value strings when literalizing to correctly handle backslash and quote characters.

# File lib/sequel/extensions/pg_hstore.rb, line 305
def escape_value(k)
  k.to_s.gsub(ESCAPE_RE, ESCAPE_REPLACE)
end