[Nitro] Storing binary data
Michael Neumann
mneumann at ntecs.de
Tue Jun 14 07:41:01 EDT 2005
Am Tuesday 14 June 2005 13:29 schrieb George Moschovitis:
> Hello Michael
>
> when recoding the psql driver I forgot to overide the default mapping to a
> blob column that was included in earlier versions of the Og-Psql adapter.
> The prefered way is BYTEA, and this will be fixed in the imminent 0.19.0
> release of Og.
Here's my patch/override to enable BYTEA's:
class Og::Binary; end # tag for binary properties
require 'og/adapters/psql'
class Og::PsqlAdapter
class << self
#
# Encodes a string as bytea value.
#
# for encoding rules see:
# http://www.postgresql.org/docs/7.4/static/datatype-binary.html
#
def encode_bytea(str)
str.gsub(/[\000-\037\047\134\177-\377]/) {|b|
"\\#{ b[0].to_s(8).rjust(3, '0') }" }
end
#
# Decodes a bytea encoded string.
#
# for decoding rules see:
# http://www.postgresql.org/docs/7.4/static/datatype-binary.html
#
def decode_bytea(str)
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
alias __old_read_prop read_prop
alias __old_write_prop write_prop
def read_prop(p, idx)
if p.klass.ancestors.include?(Og::Binary)
return "#{ self.class }.decode_bytea(res.getvalue(tuple, #{idx}))"
else
__old_read_prop(p, idx)
end
end
def write_prop(p)
if p.klass.ancestors.include?(Og::Binary)
return %|#\{@#{p.symbol} ?
"'#\{#{self.class}.escape(#{self.class}.encode_bytea(@#{p.symbol}))\}'" :
'NULL'\}|
else
__old_write_prop(p)
end
end
end
> Do you think than BLOB is better than BYTEA?
Blobs are actually a lot faster (and uses up less storage) for large data I
think, as they need not to be encoded and decoded. I'd like to have both ;-)
BYTEA is easier to handle than BLOBs, but if you implement BLOBs in a way
that they are transparent to the user (as I did in Ruby/DBI), I'd prefer that
way.
Regards,
Michael
More information about the Nitro-general
mailing list