Fast Web Applications Development with Ruby on Rails on Oracle
Sep 01, 2014
FastWeb Applications
Developmentwith Ruby on Rails
on Oracle
github.com/rsim
Raimonds Simanovskis
What is Ruby on Rails?
Ruby isobject-oriented
dynamic
programming language
simple from outside
powerful insideYukihiro
Matsumotoor “Matz”
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
MVC Architecture
ActionController
ActiveRecord
ActionView
Browser
Request Router
Res
pons
e
Database
class CreatePosts < ActiveRecor::Migration def self.up create_table :posts do |t| t.string :title t.text :body t.timestamps end endend
Active Record (Model)CREATE TABLE posts ( id NUMBER(38) NOT NULL, title VARCHAR2(255), body CLOB, created_at DATE, updated_at DATE);CREATE SEQUENCE posts_seq;
class Post < ActiveRecord::Base # nothing here!endpost = Post.newpost.title = "First post"post.savepost = Post.find(1)puts post.name # output: "First post"
Action Controllerclass PostsController < ApplicationController def index @posts = Post.all end
def show @post = Post.find(params[:id]) end
def new @post = Post.new end
# ...end
Action View
<h1>Posts</h1><% @posts.each do |post| %> <h2><%= post.title %></h2> <h3>Created at <%= post.created_at %></h3> <p><%= post.body %></p><% end %>
Demo
Ruby platformsMRI 1.8.7
Ruby/YARV 1.9.2
JRuby
Rubinius IronRuby MacRuby
MagLev BlueRuby
Ruby => Oracle
Ruby application
ruby-oci8
Oracle [Instant] Client Oracle Database
SQL*Net
require 'oci8'OCI8.new('scott', 'tiger').exec('select * from emp') do |r| puts r.join(',')end
JRuby => Oracle
Ruby application
JRuby
JDBC driver Oracle Database
SQL*Net
require "java"java.sql.DriverManager.registerDriver( Java::oracle.jdbc.driver.OracleDriver.new)conn = java.sql.DriverManager.getConnection( 'jdbc:oracle:thin:orcl', 'hr', 'hr')statement = conn.createStatementstatus = statement.execute( "SELECT * FROM employees")rs = statement.getResultSet()
ActiveRecordOracle enhanced
adapter
Ruby on Rails=> Oracle
gem install activerecord-oracle_enhanced-adapter
database.ymldevelopment: adapter: oracle_enhanced database: orcl username: blog password: blog
Multi-platform support
Ruby 1.8.7 Ruby 1.9.2 JRuby
ruby-oci8 ruby-oci8 JDBC
oracle_enhanced adapter
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)
Legacy schemas
class Employee < ActiveRecord::Base set_table_name "hr_employees" set_primary_key "employee_id" set_sequence_name "hr_employee_s"
set_date_columns :hired_on, :birth_date_on set_datetime_columns :last_login_time
set_boolean_columns :manager, :active
ignore_table_columns :attribute1, :attribute2end
Alternative Ruby ORM
DataMapper
persistence framework
Not just for relational databases
DataMapper Modelclass Post include DataMapper::Resource property :id, Serial property :title, String, :length => 255, :nullable => false property :body, Text timestamps :atend
CREATE TABLE posts ( id NUMBER(38) NOT NULL PRIMARY KEY, title VARCHAR2(255) NOT NULL, created_at DATE, updated_at DATE);CREATE SEQUENCE posts_seq;CREATE TRIGGER posts_pkt...;post = Post.new
post.title = "First post"post.save
post = Post.get(1)puts post.name # output: "First post"
PL/SQL calls from Ruby(old way)
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.connect! "hr","hr","xe"
puts plsql.test_uppercase('xxx')
ruby-plsql gemplsql.connect! "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
ActiveRecordwith
PL/SQLCRUD
procedures
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
Java integration with JRuby
require "java"java.lang.System.current_time_millis # => java.lang.System.currentTimeMillis()import java.lang.SystemSystem.properties['java.runtime.version'] # => System.getProperties[...]System.err = System.out # => System.setErr(System.getOut)
import javax.swing.JFrameimport javax.swing.JButton
f = JFrame.new("Swing Demo")f.set_size 400, 300f.layout = java.awt.FlowLayout.newbutton = JButton.new("Hello world!")f.add(button)f.show
Full-text indexesadd_context_index :posts, [:title, :body, # specify aliases always with AS keyword "SELECT comments.author AS comment_author, " + "comments.body AS comment_body " + "FROM comments WHERE comments.post_id = :id" ], :name => 'post_and_comments_index', :index_column => :all_text, :index_column_trigger_on => [:updated_at, :comments_count], :sync => 'ON COMMIT'
Post.contains(:all_text, "hello")Post.contains(:all_text, "{first} within title")Post.contains(:all_text, "{first} AND {post}")
Deployment options
Apache Java app sever
Ruby mod_passengerApplication WAR file
JRuby Gems
ApplicationApplication
Application source
Why Rails on Oracle?Fast agile / iterative development
Flexible HTML/CSS/JavaScript front-end
Test-driven development support
Easy to put on top of legacy applications
Open-source with liberal license
Libraries for all new “cool” technologies
More information
http://blog.rayapps.com
http://github.com/rsim/oracle-enhanced
http://groups.google.com/group/oracle-enhanced
Related sessionsSession ID: S318545
Title: Develop a Ruby on Rails Web Application with Oracle Database 11g in One Hour
Schedule: Thursday, September 23, 12:30 | Hilton San Francisco, Franciscan A / B / C / D
Session ID: S319104
Title: PL/SQL Unit Testing Can Be Fun!
Schedule: Tuesday, September 21, 09:30 | Hotel Nikko, Bay View
JRuby meetupTuesday, September 21
@ Engine Yard500 3rd Street, Suite 510
5:30 - 7:00 Networking + beer + food7:00 - 8:00 Lightning talks