I think I have a possible solution for Darcy's problem, I've encountered
the same issue with JRuby and Oracle using activerecord-jdbc-adapter 0.9. 
Apparently the sql statement is underneath executed as a PreparedStatement
in Java, but is doesn't contain te required bind variable for setting the
id (primary key) value which was retrieve from the sequence.

So in order to fix it, I did an override/patch.  I don't know if the is the
correct the way to do it, but at least now it works :)

# Bug fix for:
# ActiveRecord::StatementInvalid (ActiveRecord::ActiveRecordError:
ORA-01006: bind variable does not exist
# ActiveRecord::ActiveRecordError: Invalid column index
module ::JdbcSpec
  module Oracle
    def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name =
nil) #:nodoc:
        log("patched insert", name)
        if pk.nil? # Who called us? What does the sql look like? No idea!
          execute sql, name
        elsif id_value # Pre-assigned id
          execute sql, name
        else # Assume the sql contains a bind-variable for the id
          id_value = select_one("select #{sequence_name}.nextval id from
dual")['id'].to_i
          # Wrong original sql: INSERT INTO people (name) VALUES('Darcy')
          # -BEGIN- Fix for prepared statement
          sql.sub!(/ \(/," (#{pk}, ")
          sql.sub!(/ VALUES\(/," VALUES(?, ")
          # -END- Fix for prepared statement
          # Becomes correct prepared statement sql: INSERT INTO people (id,
name) VALUES(?, 'Darcy')
          log(sql, name) { 
            @connection.execute_id_insert(sql,id_value)
          }
        end
      id_value
    end
  end
end


I'm using:
 
Java 6
JRuby 1.0.3
Rails 2.0.2
activerecord-jdbc-adapter 0.7.1
 
When I try to persist an object to an Oracle table I get the following:
 
Loading development environment (Rails 2.0.2)
>> Person.find :all
>> => []
p = Person.new
>> => #<Person id: nil, name: nil>
p.name = 'Darcy'
=> "Darcy"
>> p.save
ActiveRecord::StatementInvalid: ActiveRecord::ActiveRecordError:
ORA-01006: bind variable does not exist
: INSERT INTO people (name) VALUES('Darcy')
        from
D:/jruby-1.0.3/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_rec
ord/transactions.rb:129:in `rollback_active_record_state!'
        from
D:/jruby-1.0.3/lib/ruby/gems/1.8/gems/activerecord-2.0.2-/lib/active_rec
ord/transactions.rb:108:in `save_with_transactions'
        from (irb):4:in `binding'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:150:in `eval_input'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:70:in `signal_status'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:147:in `eval_input'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:70:in
`each_top_level_statement'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:146:in `loop'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:146:in `catch'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:146:in `eval_input'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:70:in `start'
        from :1:in `catch'
        from D:/jruby-1.0.3/lib/ruby/1.8/irb.rb:69:in `start'
        from :1
>> p.id = 1
>> => 1
p.save
=> true
>> Person.find :all
=> [#<Person id: 1, name: "Darcy">]
>>
 
Is there anything else I need to do to work with Oracle?
 
thanks,
Darcy.

_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to