Module: PgHstore
- Defined in:
- lib/pg_hstore.rb
Constant Summary collapse
- SINGLE_QUOTE =
"'"- E_SINGLE_QUOTE =
"E'"- DOUBLE_QUOTE =
'"'- HASHROCKET =
'=>'- COMMA =
','- SLASH =
'\\'- ESCAPED_CHAR =
/\\(.)/- ESCAPED_SINGLE_QUOTE =
'\\\''- ESCAPED_DOUBLE_QUOTE =
'\\"'- ESCAPED_SLASH =
'\\\\'- QUOTED_LITERAL =
/"[^"\\]*(?:\\.[^"\\]*)*"/- UNQUOTED_LITERAL =
/[^\s=,][^\s=,\\]*(?:\\.[^\s=,\\]*|=[^,>])*/- LITERAL =
/(#{QUOTED_LITERAL}|#{UNQUOTED_LITERAL})/- PAIR =
/#{LITERAL}\s*=>\s*#{LITERAL}/- NULL =
/\ANULL\z/i
Class Method Summary collapse
-
.dump(hash, raw_string = false) ⇒ Object
Serialize a hash to be sent to PostgreSQL as an hstore value.
-
.load(hstore, symbolize_keys = false) ⇒ Object
(also: parse)
STRING KEYS BY DEFAULT set symbolize_keys = true if you want symbol keys thanks to https://github.com/engageis/activerecord-postgres-hstore for regexps!.
Class Method Details
.dump(hash, raw_string = false) ⇒ Object
Serialize a hash to be sent to PostgreSQL as an hstore value.
By default, returns a (Postgre)SQL string constant suitable for interpolating directly into a query. With raw_string = true, returns the plain string value suitable for use as a bind variable.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pg_hstore.rb', line 38 def PgHstore.dump(hash, raw_string = false) # Per http://www.postgresql.org/docs/9.2/static/hstore.html : # # The text representation of an hstore, used for input and # output, includes zero or more 'key => value' pairs separated # by commas. [...] Whitespace between pairs or around the => # sign is ignored. Double-quote keys and values [... see # escape_nonnull_for_hstore ...] # # A value (but not a key) can be an SQL NULL. Double-quote the # NULL to treat it as the ordinary string "NULL". hstore = hash.map do |k, v| hstore_k = escape_nonnull_for_hstore(k) hstore_v = (v.nil?) ? "NULL" : escape_nonnull_for_hstore(v) [hstore_k, hstore_v].join(HASHROCKET) end.join(COMMA) raw_string ? hstore : as_postgresql_string_constant(hstore) end |
.load(hstore, symbolize_keys = false) ⇒ Object Also known as: parse
STRING KEYS BY DEFAULT set symbolize_keys = true if you want symbol keys thanks to https://github.com/engageis/activerecord-postgres-hstore for regexps!
23 24 25 26 27 28 29 30 31 |
# File 'lib/pg_hstore.rb', line 23 def PgHstore.load(hstore, symbolize_keys = false) hstore.scan(PAIR).inject({}) do |memo, (k, v)| k = unescape unquote(k, DOUBLE_QUOTE) k = k.to_sym if symbolize_keys v = (v =~ NULL) ? nil : unescape(unquote(v, DOUBLE_QUOTE)) memo[k] = v memo end end |