Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using Ruby on Rails with legacy Oracle databases


Published on

Published in: Technology, Business
  • Be the first to comment

Using Ruby on Rails with legacy Oracle databases

  1. 1. Using Ruby on Rails with legacy Oracle databases Raimonds Simanovskis
  2. 2. Ruby => Oracle require 'oci8' Ruby application'scott', 'tiger').exec( 'select * from emp') do |r| puts r.join(',') end ruby-oci8 Oracle Instant Oracle Database Client SQL*Net
  3. 3. JRuby => Oracle require quot;javaquot; sql = Ruby application JavaSQL::DriverManager.getConnec tion(db, user, password) statement = sql.createStatement status = statement.execute quot;select * from parts;quot; JRuby rs = statement.getResultSet() JDBC driver Oracle Database SQL*Net
  4. 4. ActiveRecord Oracle Enhanced Adapter
  5. 5. Ruby on Rails 2.x => Oracle gem install activerecord-oracle_enhanced-adapter database.yml development: adapter: oracle_enhanced database: XE username: blogdemo password: blogdemo encoding: utf8
  6. 6. SQL bind variables database.yml Employee.find(1) development: ActiveRecord adapter: oracle_enhanced SELECT * FROM employees database: XE WHERE ( = 1) username: blogdemo password: blogdemo Oracle optimizer encoding: utf8 SELECT * FROM employees WHERE cursor_sharing: similar ( = :SYS_B_0)
  7. 7. Oracle Data Types Ruby Rails Oracle Fixnum :integer NUMBER Float :float NUMBER BigDecimal :decimal NUMBER, DECIMAL Time :datetime DATE Time :time DATE Date :date DATE String :string VARCHAR2 String :text CLOB True/FalseClass :boolean NUMBER(1), CHAR(1)
  8. 8. Workarounds ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter. emulate_dates_by_column_name = true hire_date # Oracle DATE => Ruby Date hire_time # Oracle DATE => Ruby Time set_date_columns :started_on, :finished_on set_datetime_columns :start_date_and_time, :end_date_and_time ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter. emulate_booleans_from_strings = true active_flag # Oracle CHAR(1) ‘Y’/’N’ => true/false set_boolean_columns :some_boolean_column ignore_table_columns :attribute1, :attribute2
  9. 9. Calling PL/SQL from Ruby require quot;oci8quot; conn =;hrquot;,quot;hrquot;,quot;xequot;) cursor = conn.parse <<-EOS BEGIN :return := test_uppercase(:p_string); END; EOS cursor.bind_param(':p_string',quot;xxxquot;,String) cursor.bind_param(':return',nil,String,4000) cursor.exec puts cursor[':return'] cursor.close
  10. 10. ruby-plsql gem gem install ruby-plsql require quot;ruby-plsqlquot; plsql.connection =;hrquot;,quot;hrquot;,quot;xequot;) puts plsql.test_uppercase('xxx')
  11. 11. ruby-plsql gem plsql.connection =;hrquot;,quot;hrquot;,quot;xequot;) plsql.test_uppercase('xxx') # => quot;XXXquot; plsql.test_uppercase(:p_string => 'xxx') # => quot;XXXquot; plsql.test_copy(quot;abcquot;, nil, nil) # => { :p_to => quot;abcquot;, # :p_to_double => quot;abcabcquot; } plsql.test_copy(:p_from => quot;abcquot;, :p_to => nil, :p_to_double => nil) # => { :p_to => quot;abcquot;, # :p_to_double => quot;abcabcquot; }'xxx') # => quot;XXXquot; plsql.test_package.test_uppercase('xxx') # => 'XXX''xxx') # => 'XXX' plsql.logoff
  12. 12. class Employee < ActiveRecord::Base set_create_method do plsql.employees_pkg.create_employee( :p_first_name => first_name, :p_last_name => last_name, :p_employee_id => nil Custom )[:p_employee_id] end set_update_method do CUD plsql.employees_pkg.update_employee( :p_employee_id => id, :p_first_name => first_name, methods ) end :p_last_name => last_name set_delete_method do plsql.employees_pkg.delete_employee( :p_employee_id => id ) end end
  13. 13. Other features • composite_primary_keys support • NLS_DATE_FORMAT independent • TIMESTAMP columns support • Faster schema dump
  14. 14. DEMO see code at
  15. 15. Ruby on Rails application Controller classes (incoming request HTTP request processing) Browser HTML result Model classes View templates (business logic) (HTML, Javascript) Ruby on SQL update data selects through API SQL Rails on Oracle Oracle E-Business Suite database new schema for web application custom tables synonym for for web views EBS application synonym for packages APPS schema XXX schema custom views for data read data XXX tables access update data custom or standard API packages
  16. 16. HTTPS Application server Apache Typical HTTP HAproxy balancer application HTTP HTTP HTTP server Mongrel Mongrel ... Mongrel TCP TCP TCP processes SQL SQL SQL Memcached Oracle DB
  17. 17. Thank You!