Top Banner
Fast Web Applications Development with Ruby on Rails on Oracle
32

Fast Web Applications Development with Ruby on Rails on Oracle

Sep 01, 2014

Download

Technology

rsim

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fast Web Applications Development with Ruby on Rails on Oracle

FastWeb Applications

Developmentwith Ruby on Rails

on Oracle

Page 2: Fast Web Applications Development with Ruby on Rails on Oracle

github.com/rsim

Raimonds Simanovskis

Page 3: Fast Web Applications Development with Ruby on Rails on Oracle

What is Ruby on Rails?

Page 4: Fast Web Applications Development with Ruby on Rails on Oracle

Ruby isobject-oriented

dynamic

programming language

simple from outside

powerful insideYukihiro

Matsumotoor “Matz”

Page 5: Fast Web Applications Development with Ruby on Rails on Oracle

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: Fast Web Applications Development with Ruby on Rails on Oracle

Main principles

DRY - Don’t Repeat Yourself

Convention over Configuration

Opinionated software

Page 7: Fast Web Applications Development with Ruby on Rails on Oracle

MVC Architecture

ActionController

ActiveRecord

ActionView

Browser

Request Router

Res

pons

e

Database

Page 8: Fast Web Applications Development with Ruby on Rails on Oracle

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"

Page 9: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 10: Fast Web Applications Development with Ruby on Rails on Oracle

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 %>

Page 11: Fast Web Applications Development with Ruby on Rails on Oracle

Demo

Page 12: Fast Web Applications Development with Ruby on Rails on Oracle

Ruby platformsMRI 1.8.7

Ruby/YARV 1.9.2

JRuby

Rubinius IronRuby MacRuby

MagLev BlueRuby

Page 13: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 14: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 15: Fast Web Applications Development with Ruby on Rails on Oracle

ActiveRecordOracle enhanced

adapter

Page 16: Fast Web Applications Development with Ruby on Rails on Oracle

Ruby on Rails=> Oracle

gem install activerecord-oracle_enhanced-adapter

database.ymldevelopment: adapter: oracle_enhanced database: orcl username: blog password: blog

Page 17: Fast Web Applications Development with Ruby on Rails on Oracle

Multi-platform support

Ruby 1.8.7 Ruby 1.9.2 JRuby

ruby-oci8 ruby-oci8 JDBC

oracle_enhanced adapter

Page 18: Fast Web Applications Development with Ruby on Rails on Oracle

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 19: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 20: Fast Web Applications Development with Ruby on Rails on Oracle

Alternative Ruby ORM

DataMapper

persistence framework

Not just for relational databases

Page 21: Fast Web Applications Development with Ruby on Rails on Oracle

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"

Page 22: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 23: Fast Web Applications Development with Ruby on Rails on Oracle

ruby-plsql gem

gem install ruby-plsql

require "ruby-plsql"plsql.connect! "hr","hr","xe"

puts plsql.test_uppercase('xxx')

Page 24: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 25: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 26: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 27: Fast Web Applications Development with Ruby on Rails on Oracle

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}")

Page 28: Fast Web Applications Development with Ruby on Rails on Oracle

Deployment options

Apache Java app sever

Ruby mod_passengerApplication WAR file

JRuby Gems

ApplicationApplication

Application source

Page 29: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 30: Fast Web Applications Development with Ruby on Rails on Oracle

More information

http://blog.rayapps.com

http://github.com/rsim/oracle-enhanced

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

Page 31: Fast Web Applications Development with Ruby on Rails on Oracle

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

Page 32: Fast Web Applications Development with Ruby on Rails on Oracle

JRuby meetupTuesday, September 21

@ Engine Yard500 3rd Street, Suite 510

5:30 - 7:00 Networking + beer + food7:00 - 8:00 Lightning talks