Oracle adapters for Ruby ORMs

Post on 01-Sep-2014

6035 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation that I gave in Oracle OpenWorld 2009 unconference

Transcript

Oracle adaptersfor Ruby ORMs

+

Raimonds Simanovskis

TietoEnator Alise

@rsim github.com/rsim

What isRuby on

Rails?

Ruby isobject-oriented

dynamicprogramming language

simple from outside

powerful inside

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

Main principles

DRY - Don’t Repeat Yourself

Convention over Configuration

Opinionated software

Components

Active Record (Model)

Action Controller

Action View

Ruby platformsMRI 1.8.7

Ruby/YARV 1.9.1

JRuby

Rubinius IronRuby MacRuby

MagLev BlueRuby

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

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

ActiveRecordOracle Enhanced

Adapter

Ruby on Rails=> Oracle

gem install activerecord-oracle_enhanced-adapter

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

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

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)

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

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 :(

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

DataMapper

•Alternative Ruby ORM persistence framework

•Not just for relational databases

DataMapper Model

DataMapper differencesConditions

Identity MapEager Loading

Lazy Loading

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

DataMapper & OracleType mapping Composite primary keys

Legacy schemas

Bind variables CLOB and BLOB values

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

ruby-plsql gem

gem install ruby-plsql

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

puts plsql.test_uppercase('xxx')

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

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

Thanks!

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

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

top related