Class: Sequel::Postgres::HStore

Inherits:
Hash show all
Includes:
SQL::AliasMethods
Defined in:
lib/sequel/extensions/pg_hstore_ops.rb,
lib/sequel/extensions/pg_hstore.rb

Overview

:nocov:

Defined Under Namespace

Modules: DatabaseMethods Classes: Parser

Constant Summary collapse

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.

lambda{|h, k| h[k.to_s] unless k.is_a?(String)}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SQL::AliasMethods

#as

Methods inherited from Hash

#&, #case, #hstore, #pg_json, #pg_jsonb, #sql_expr, #sql_negate, #sql_or, #|, #~

Class Method Details

._load(args) ⇒ Object

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



203
204
205
# File 'lib/sequel/extensions/pg_hstore.rb', line 203

def self._load(args)
  new(Hash[Marshal.load(args)])
end

.parse(str) ⇒ Object

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



209
210
211
# File 'lib/sequel/extensions/pg_hstore.rb', line 209

def self.parse(str)
  new(Parser.new(str).parse)
end

Instance Method Details

#_dumpObject

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



235
236
237
# File 'lib/sequel/extensions/pg_hstore.rb', line 235

def _dump(*)
  Marshal.dump(to_a)
end

#fetch(key, *args, &block) ⇒ Object

Override to force the key argument to a string.



240
241
242
# File 'lib/sequel/extensions/pg_hstore.rb', line 240

def fetch(key, *args, &block)
  super(key.to_s, *args, &block)
end

#merge(hash, &block) ⇒ Object

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



246
247
248
# File 'lib/sequel/extensions/pg_hstore.rb', line 246

def merge(hash, &block)
  self.class.new(super(convert_hash(hash), &block))
end

#opObject

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



371
372
373
# File 'lib/sequel/extensions/pg_hstore_ops.rb', line 371

def op
  HStoreOp.new(self)
end

#sequel_auto_param_type(ds) ⇒ Object

Allow automatic parameterization.



284
285
286
# File 'lib/sequel/extensions/pg_hstore.rb', line 284

def sequel_auto_param_type(ds)
  "::hstore"
end

#sql_literal_append(ds, sql) ⇒ Object

Append a literalize version of the hstore to the sql.



254
255
256
257
# File 'lib/sequel/extensions/pg_hstore.rb', line 254

def sql_literal_append(ds, sql)
  ds.literal_append(sql, unquoted_literal)
  sql << '::hstore'
end

#unquoted_literalObject

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/sequel/extensions/pg_hstore.rb', line 262

def unquoted_literal
  str = String.new
  comma = false
  commas = ","
  quote = '"'
  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