Module: Postgres::Conversion
- Included in:
- TC_Conversion
- Defined in:
- lib/postgres-pr/typeconv/conv.rb,
lib/postgres-pr/typeconv/array.rb,
lib/postgres-pr/typeconv/bytea.rb
Defined Under Namespace
Classes: ConversionError
Instance Method Summary collapse
- #decode_array(str, delim = ',', &conv_proc) ⇒ Object
-
#decode_bytea(str) ⇒ Object
Decodes a bytea encoded string.
-
#encode_bytea(str) ⇒ Object
Encodes a string as bytea value.
Instance Method Details
#decode_array(str, delim = ',', &conv_proc) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/postgres-pr/typeconv/array.rb', line 5 def decode_array(str, delim=',', &conv_proc) delim = Regexp.escape(delim) buf = StringScanner.new(str) return parse_arr(buf, delim, &conv_proc) ensure raise ConversionError, "end of string expected (#{buf.rest})" unless buf.empty? end |
#decode_bytea(str) ⇒ Object
Decodes a bytea encoded string.
For decoding rules see:
http://www.postgresql.org/docs/7.4/static/datatype-binary.html
Supports both the historical escape format and the new 9.0+ hex format.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/postgres-pr/typeconv/bytea.rb', line 19 def decode_bytea(str) if str =~ /\A\\x/ # PostgreSQL 9.0+ bytea hex format str[2..-1].gsub(/(..)/){|s| s.to_i(16).chr} else # Historical PostgreSQL bytea escape format str.gsub(/\\(\\|'|[0-3][0-7][0-7])/) {|s| if s.size == 2 then s[1,1] else s[1,3].oct.chr end } end end |
#encode_bytea(str) ⇒ Object
Encodes a string as bytea value.
For encoding rules see:
http://www.postgresql.org/docs/7.4/static/datatype-binary.html
The historical bytea escape format is always used, for maximum compatibility.
8 9 10 11 |
# File 'lib/postgres-pr/typeconv/bytea.rb', line 8 def encode_bytea(str) # each_byte used instead of [] for 1.9 compatibility str.gsub(/[\000-\037\047\134\177-\377]/n){|b| "\\#{sprintf('%o', b.each_byte{|x| break x}).rjust(3, '0')}"} end |