lib/og/store/psql.rb in og-0.18.1 vs lib/og/store/psql.rb in og-0.19.0

- old
+ new

@@ -41,10 +41,31 @@ def escape(str) return nil unless str return PGconn.escape(str) end + + # TODO, mneumann: + # + # 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. + + def blob(val) + val.gsub(/[\000-\037\047\134\177-\377]/) do |b| + "\\#{ b[0].to_s(8).rjust(3, '0') }" + end + end + + def parse_blob(val) + val.gsub(/\\(\\|'|[0-3][0-7][0-7])/) do |s| + if s.size == 2 then s[1,1] else s[1,3].oct.chr end + end + end + end # A Store that persists objects into a PostgreSQL database. # To read documentation about the methods, consult the documentation # for SqlStore and Store. @@ -120,10 +141,18 @@ @conn.exec(sql).clear rescue => ex handle_sql_exception(ex, sql) end + def sql_update(sql) + Logger.debug sql if $DBG + res = @conn.exec(sql) + changed = res.cmdtuples + res.clear + changed + end + private def create_table(klass) fields = fields_for_class(klass) @@ -212,10 +241,12 @@ return "#{self.class}.parse_timestamp(res.getvalue(row, #{col} + offset))" elsif p.klass.ancestors.include?(Date) return "#{self.class}.parse_date(res.getvalue(row, #{col} + offset))" elsif p.klass.ancestors.include?(TrueClass) return %|('t' == res.getvalue(row, #{col} + offset))| + elsif p.klass.ancestors.include?(Og::Blob) + return "#{self.class}.parse_blob(res.getvalue(row, #{col} + offset))" else return "YAML.load(res.getvalue(row, #{col} + offset))" end end @@ -224,10 +255,15 @@ #++ def eval_og_insert(klass) props = klass.properties values = props.collect { |p| write_prop(p) }.join(',') + + if klass.metadata.superclass or klass.metadata.subclasses + props << Property.new(:ogtype, String) + values << ", '#{klass}'" + end sql = "INSERT INTO #{klass::OGTABLE} (#{props.collect {|p| p.symbol.to_s}.join(',')}) VALUES (#{values})" klass.class_eval %{ def og_insert(store) @@ -242,5 +278,8 @@ end end end + +# * George Moschovitis <gm@navel.gr> +# * Michael Neumann <mneumann@ntecs.de>