oracle adapters for ruby orms

30
Oracle adapters for Ruby ORMs +

Upload: rsim

Post on 01-Sep-2014

6.046 views

Category:

Technology


0 download

DESCRIPTION

Presentation that I gave in Oracle OpenWorld 2009 unconference

TRANSCRIPT

Page 1: Oracle adapters for Ruby ORMs

Oracle adaptersfor Ruby ORMs

+

Page 2: Oracle adapters for Ruby ORMs

Raimonds Simanovskis

TietoEnator Alise

@rsim github.com/rsim

Page 3: Oracle adapters for Ruby ORMs

What isRuby on

Rails?

Page 4: Oracle adapters for Ruby ORMs

Ruby isobject-oriented

dynamicprogramming language

simple from outside

powerful inside

Page 5: Oracle adapters for Ruby ORMs

Ruby on RailsWeb applications development framework

Developed in Ruby

Extracted from 37signals Basecamp application

Open source software

Focused on developer productivity

Agile software development approach

Page 6: Oracle adapters for Ruby ORMs

Main principles

DRY - Don’t Repeat Yourself

Convention over Configuration

Opinionated software

Page 7: Oracle adapters for Ruby ORMs

Components

Page 8: Oracle adapters for Ruby ORMs

Active Record (Model)

Page 9: Oracle adapters for Ruby ORMs

Action Controller

Page 10: Oracle adapters for Ruby ORMs

Action View

Page 11: Oracle adapters for Ruby ORMs

Ruby platformsMRI 1.8.7

Ruby/YARV 1.9.1

JRuby

Rubinius IronRuby MacRuby

MagLev BlueRuby

Page 12: Oracle adapters for Ruby ORMs

Ruby => Oracle

Ruby application

ruby-oci8

require 'oci8'OCI8.new('scott', 'tiger').exec('select * from emp') do |r| puts r.join(',')end

Oracle [Instant] Client Oracle Database

SQL*Net

Page 13: Oracle adapters for Ruby ORMs

JRuby => Oracle

Ruby application

JRuby

require "java"sql = JavaSQL::DriverManager.getConnection(db, user, password)statement = sql.createStatementstatus = statement.execute "select * from parts;"rs = statement.getResultSet()

JDBC driver Oracle Database

SQL*Net

Page 14: Oracle adapters for Ruby ORMs

ActiveRecordOracle Enhanced

Adapter

Page 15: Oracle adapters for Ruby ORMs

Ruby on Rails=> Oracle

gem install activerecord-oracle_enhanced-adapter

database.ymldevelopment: adapter: oracle_enhanced database: XE username: blogdemo password: blogdemo

Page 16: Oracle adapters for Ruby ORMs

SQL bind variables

database.ymldevelopment: adapter: oracle_enhanced database: XE username: blogdemo password: blogdemo cursor_sharing: force

Employee.find(1)

SELECT * FROM employees WHERE (employees.id = 1)

SELECT * FROM employees WHERE (employees.id = :SYS_B_0)

ActiveRecord

Oracle optimizer

Page 17: Oracle adapters for Ruby ORMs

Oracle Data TypesRuby Rails Oracle

Fixnum :integer NUMBERFloat :float NUMBER

BigDecimal :decimal NUMBER, DECIMALTime :datetime DATETime :time DATEDate :date DATEString :string VARCHAR2String :text CLOBString :binary BLOB

True/FalseClass :boolean NUMBER(1), CHAR(1)

Page 18: Oracle adapters for Ruby ORMs

ActiveRecord Extensions

set_date_columnsset_datetime_columns

add_foreign_key

set_boolean_columnsemulate_booleans

add_synonym

emulate_integers_by_column_name

add_primary_key_trigger

ignore_table_columns set_create_methodset_update_method

table_commentcomment

set_delete_method

Page 19: Oracle adapters for Ruby ORMs

Issues & Limitations

Identifiers up to 30 characters

CLOB / BLOB usage in SQL statements

absence of LIMIT, OFFSET

slow Data Dictionary views

empty String == NULL

Mac OS X is not wellsupported by Oracle :(

Page 20: Oracle adapters for Ruby ORMs

Multi-platform support

MRI 1.8.6/1.8.7 Ruby/YARV 1.9.1

JRuby

ruby-oci8 1.x or 2.x

ruby-oci8 2.x JDBC

oracle_enhanced adapter

Page 21: Oracle adapters for Ruby ORMs

DataMapper

•Alternative Ruby ORM persistence framework

•Not just for relational databases

Page 22: Oracle adapters for Ruby ORMs

DataMapper Model

Page 23: Oracle adapters for Ruby ORMs

DataMapper differencesConditions

Identity MapEager Loading

Lazy Loading

Page 24: Oracle adapters for Ruby ORMs

DataMapper & Oracle

MRI 1.8.6/1.8.7 Ruby/YARV 1.9.1 JRuby

ruby-oci8 2.x ruby-oci8 2.x JDBC

DataMapper DataObjects adapter

DataObjects Oracle driver DO JDBC Oracle driver

DataMapper Oracle adapter

Page 25: Oracle adapters for Ruby ORMs

DataMapper & OracleType mapping Composite primary keys

Legacy schemas

Bind variables CLOB and BLOB values

Page 26: Oracle adapters for Ruby ORMs

PL/SQL calls from Ruby

require "oci8"conn = OCI8.new("hr","hr","xe")

cursor = conn.parse <<-EOSBEGIN :return := test_uppercase(:p_string);END;EOScursor.bind_param(':p_string',"xxx",String)cursor.bind_param(':return',nil,String,4000)cursor.execputs cursor[':return']cursor.close

Page 27: Oracle adapters for Ruby ORMs

ruby-plsql gem

gem install ruby-plsql

require "ruby-plsql"plsql.connection = OCI8.new("hr","hr","xe")

puts plsql.test_uppercase('xxx')

Page 28: Oracle adapters for Ruby ORMs

ruby-plsql gemplsql.connection = OCI8.new("hr","hr","xe")

plsql.test_uppercase('xxx') # => "XXX"plsql.test_uppercase(:p_string => 'xxx') # => "XXX"plsql.test_copy("abc", nil, nil) # => { :p_to => "abc", # :p_to_double => "abcabc" }plsql.test_copy(:p_from => "abc", :p_to => nil, :p_to_double => nil) # => { :p_to => "abc", # :p_to_double => "abcabc" }plsql.hr.test_uppercase('xxx') # => "XXX"plsql.test_package.test_uppercase('xxx') # => 'XXX'plsql.hr.test_package.test_uppercase('xxx') # => 'XXX'

plsql.logoff

Page 29: Oracle adapters for Ruby ORMs

PL/SQL CRUDprocedures in

legacy applications

(withActiveRecord)

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 )[:p_employee_id] end set_update_method do plsql.employees_pkg.update_employee( :p_employee_id => id, :p_first_name => first_name, :p_last_name => last_name ) end set_delete_method do plsql.employees_pkg.delete_employee( :p_employee_id => id ) endend

Page 30: Oracle adapters for Ruby ORMs

Thanks!

http://blog.rayapps.comhttp://github.com/rsim

http://groups.google.com/group/oracle-enhanced