[Nitro] Sqlite bug fix patch for og 0.18.1

Ghislain Mary nospam at lunacymaze.org
Wed Jun 1 17:09:08 EDT 2005

I found a bug, still in the sqlite store. Here's a little program that 
shows the wrong behaviour:

require 'og'

# Enables debug.
$DBG = true

# A class representing a blog Entry.
class Entry

   property :title, String
   property :body, String

   has_many :comments, Comment

   def initialize(body = nil)
     @body = body

   def to_s


# A class representing a Comment to a blog Entry.
class Comment

   property :body, String

   belongs_to Entry

   def initialize(body = nil)
     @body = body

   def to_s


# Initializes Og.
manager = Og.setup(
   :destroy  => true,   # Destroy tables created from earlier runs.
   :store    => 'sqlite',
   :name     => 'ogtest'

# Create an Entry.
e1 = Entry.create('Body1')
p e1

# Test the creation of a Comment.
c1 = Comment.create('Comment1')
p c1

# Attach a Comment to an Entry.
c1.entry = e1

The problem here is that when created, the Entry has an oid of 0 whereas 
in the database the oid for this row is 1. So the attachement of a 
Comment to the Entry has no effect because refering an unknown Entry.
Here is a patch incorporating the last one, fixing this problem and also 
enabling the printing of queries when debug is on:

diff -ru og-0.18.1/lib/og/store/sqlite.rb 
--- og-0.18.1/lib/og/store/sqlite.rb    Fri May 20 13:52:50 2005
+++ og-0.18.1-modified/lib/og/store/sqlite.rb   Wed Jun  1 23:00:06 2005
@@ -14,6 +14,7 @@

  class SQLite3::ResultSet
         alias_method :blank?, :eof?
+       alias_method :fields, :columns

         def each_row
                 each do |row|
@@ -96,6 +97,10 @@
                 @conn.rollback if @transaction_nesting < 1

+        def last_insert_rowid
+                conn.query("SELECT last_insert_rowid()").first_value.to_i
+        end

         def create_table(klass)
@@ -182,8 +187,8 @@
                 klass.class_eval %{
                         def og_insert(store)
klass.advices, :pre) if klass.respond_to?(:advices)}
-                               store.conn.query("#{sql}").close
-                               @#{pk} = store.conn.last_insert_row_id
+                               store.query("#{sql}").close
+                               @#{pk} = store.last_insert_rowid
klass.advices, :post) if klass.respond_to?(:advices)}


More information about the Nitro-general mailing list