class DBI::DBD::Pg::Type::ByteA

ByteA is a special escaped form of binary data, suitable for inclusion in queries.

This class is an attempt to abstract that type so you do not have to concern yourself with the conversion issues.

Attributes

escaped[R]
original[R]

Public Class Methods

escape_bytea(str) click to toggle source

Class method to escape the data into ByteA format.

# File lib/dbd/pg/type.rb, line 48
def self.escape_bytea(str)
    self.new(str).escaped
end
new(obj) click to toggle source

Build a new ByteA object.

The data supplied is the unescaped binary data you wish to put in the database.

# File lib/dbd/pg/type.rb, line 24
def initialize(obj)
    @original = obj
    @escaped = escape_bytea(obj)
    @original.freeze
    @escaped.freeze
end
parse(obj) click to toggle source

Class method to unescape the ByteA data and present it as a string.

# File lib/dbd/pg/type.rb, line 55
def self.parse(obj)

    return nil if obj.nil?

    # FIXME there's a bug in the upstream 'pg' driver that does not
    # properly decode bytea, leaving in an extra slash for each decoded
    # character.
    #
    # Fix this for now, but beware that we'll have to unfix this as
    # soon as they fix their end.
    ret = PG::Connection.unescape_bytea(obj)

    # XXX
    # String#split does not properly create a full array if the the
    # string ENDS in the split regex, unless this oddball -1 argument is supplied.
    #
    # Another way of saying this:
    # if foo = "foo\\\\\" and foo.split(/\\\\/), the result will be
    # ["foo"]. You can add as many delimiters to the end of the string
    # as you'd like - the result is no different.
    #

    ret = ret.split(/\\\\/, -1).collect { |x| x.length > 0 ? x.gsub(/\\[0-7]{3}/) { |y| y[1..3].oct.chr } : "" }.join("\\")
    ret.gsub!(/''/, "'")
    return ret
end

Public Instance Methods

escape_bytea(str) click to toggle source

Escapes the supplied data. Has no effect on the object.

# File lib/dbd/pg/type.rb, line 34
def escape_bytea(str)
    PG::Connection.escape_bytea(str)
end
to_s() click to toggle source

Returns the original data.

# File lib/dbd/pg/type.rb, line 41
def to_s
    return @original.dup
end