class Sequel::Postgres::HStore
Constants
- 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.
Public Class Methods
Use custom marshal loading, since underlying hash uses a default proc.
# File lib/sequel/extensions/pg_hstore.rb 197 def self._load(args) 198 new(Hash[Marshal.load(args)]) 199 end
Parse the given string into an HStore
, assuming the str is in PostgreSQL hstore output format.
# File lib/sequel/extensions/pg_hstore.rb 203 def self.parse(str) 204 new(Parser.new(str).parse) 205 end
Public Instance Methods
Use custom marshal dumping, since underlying hash uses a default proc.
# File lib/sequel/extensions/pg_hstore.rb 229 def _dump(*) 230 Marshal.dump(to_a) 231 end
Override to force the key argument to a string.
# File lib/sequel/extensions/pg_hstore.rb 234 def fetch(key, *args, &block) 235 super(key.to_s, *args, &block) 236 end
Convert the input hash to string keys and values before merging, and return a new HStore
instance with the merged hash.
# File lib/sequel/extensions/pg_hstore.rb 240 def merge(hash, &block) 241 self.class.new(super(convert_hash(hash), &block)) 242 end
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 319 def op 320 HStoreOp.new(self) 321 end
Append a literalize version of the hstore to the sql.
# File lib/sequel/extensions/pg_hstore.rb 248 def sql_literal_append(ds, sql) 249 ds.literal_append(sql, unquoted_literal) 250 sql << '::hstore' 251 end
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 256 def unquoted_literal 257 str = String.new 258 comma = false 259 commas = "," 260 quote = '"' 261 kv_sep = "=>" 262 null = "NULL" 263 each do |k, v| 264 str << commas if comma 265 str << quote << escape_value(k) << quote 266 str << kv_sep 267 if v.nil? 268 str << null 269 else 270 str << quote << escape_value(v) << quote 271 end 272 comma = true 273 end 274 str 275 end
Private Instance Methods
Return a new hash based on the input hash with string keys and string or nil values.
# File lib/sequel/extensions/pg_hstore.rb 281 def convert_hash(h) 282 hash = Hash.new(&DEFAULT_PROC) 283 h.each{|k,v| hash[k.to_s] = convert_value(v)} 284 hash 285 end
Return value v as a string unless it is already nil.
# File lib/sequel/extensions/pg_hstore.rb 288 def convert_value(v) 289 v.to_s unless v.nil? 290 end
Escape key/value strings when literalizing to correctly handle backslash and quote characters.
# File lib/sequel/extensions/pg_hstore.rb 294 def escape_value(k) 295 k.to_s.gsub(/("|\\)/, '\\\\\1') 296 end