class Sequel::Postgres::HStore
:nocov:
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 203 def self._load(args) 204 new(Hash[Marshal.load(args)]) 205 end
Parse the given string into an HStore
, assuming the str is in PostgreSQL hstore output format.
# File lib/sequel/extensions/pg_hstore.rb 209 def self.parse(str) 210 new(Parser.new(str).parse) 211 end
Public Instance Methods
Use custom marshal dumping, since underlying hash uses a default proc.
# File lib/sequel/extensions/pg_hstore.rb 235 def _dump(*) 236 Marshal.dump(to_a) 237 end
Override to force the key argument to a string.
# File lib/sequel/extensions/pg_hstore.rb 240 def fetch(key, *args, &block) 241 super(key.to_s, *args, &block) 242 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 246 def merge(hash, &block) 247 self.class.new(super(convert_hash(hash), &block)) 248 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 371 def op 372 HStoreOp.new(self) 373 end
Allow automatic parameterization.
# File lib/sequel/extensions/pg_hstore.rb 284 def sequel_auto_param_type(ds) 285 "::hstore" 286 end
Append a literalize version of the hstore to the sql.
# File lib/sequel/extensions/pg_hstore.rb 254 def sql_literal_append(ds, sql) 255 ds.literal_append(sql, unquoted_literal) 256 sql << '::hstore' 257 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 262 def unquoted_literal 263 str = String.new 264 comma = false 265 commas = "," 266 quote = '"' 267 kv_sep = "=>" 268 null = "NULL" 269 each do |k, v| 270 str << commas if comma 271 str << quote << escape_value(k) << quote 272 str << kv_sep 273 if v.nil? 274 str << null 275 else 276 str << quote << escape_value(v) << quote 277 end 278 comma = true 279 end 280 str 281 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 292 def convert_hash(h) 293 hash = Hash.new(&DEFAULT_PROC) 294 h.each{|k,v| hash[k.to_s] = convert_value(v)} 295 hash 296 end
Return value v as a string unless it is already nil.
# File lib/sequel/extensions/pg_hstore.rb 299 def convert_value(v) 300 v.to_s unless v.nil? 301 end
Escape key/value strings when literalizing to correctly handle backslash and quote characters.
# File lib/sequel/extensions/pg_hstore.rb 305 def escape_value(k) 306 k.to_s.gsub(/("|\\)/, '\\\\\1') 307 end