GithubHelp home page GithubHelp logo

flash-gordon / rails-plsql Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 6.0 56 KB

Extension for ActiveRecord that provides convenient using some of Oracle PL/SQL features.

License: MIT License

Ruby 100.00%

rails-plsql's Introduction

Code Climate rails-plsql

Middleware between ActiveRecord and Oracle Database

Description

This gem is ActiveRecord extension for some Oracle Database specific features such as pipelined functions or PL/SQL procedures. It uses ruby-plsql and oracle enhanced adapter gems as dependencies for connection to Oracle and calling PL/SQL procedures and functions. It also adds basic logger to my fork of ruby-plsql gem.

Installation

Rails 3.2

Just put this line into your Gemfile

gem 'rails-plsql', '~> 0.1', github: 'flash-gordon/rails-plsql'

Gem tested with MRI 1.9.2, 1.9.3, 2.0.0 and JRuby 1.7.4. So if you use ruby-oci8 then add also

gem 'ruby-oci8', '~> 2.1.0'

And run

bundle install

to install all gems.

Other versions of Rails not tested.

Usage

Pipelined functions as tables in ActiveRecord models

Oracle pipelined functions could be used as data source instead of ordinary tables (or views).

If you have such PL/SQL function

CREATE OR REPLACE
PACKAGE users_pkg IS

  TYPE users_list IS TABLE OF USERS%ROWTYPE;

  FUNCTION find_users_by_name(
    p_name    USERS.NAME%TYPE)
  RETURN users_list
  PIPELINED;

END users_pkg;
/

CREATE OR REPLACE
PACKAGE BODY users_pkg IS

  FUNCTION find_users_by_name(
    p_name    USERS.NAME%TYPE)
  RETURN users_list
  PIPELINED
  IS
  BEGIN
    FOR l_user IN (
      SELECT *
      FROM   users
      WHERE  name = p_name)
    LOOP
      PIPE ROW(l_user);
    END LOOP;
  END FIND_USERS_BY_NAME;

END users_pkg;
/

So you can set this function in your model instead of table name

class User < ActiveRecord::Base
  include ActiveRecord::PLSQL::Pipelined

  self.pipelined_function = 'users_pkg.find_users_by_name'

  scope :alberts, where(p_name: 'Albert')
  scope :einsteins, where(surname: 'Einstein')
end

and use standard Rails scopes and finders

User.alberts
# [#<User id: #<BigDecimal:6fec77a4,'0.1E1',9(36)>, name: "Albert", surname: "Einstein">]
User.alberts.einsteins.first
# #<User id: #<BigDecimal:6fec77a4,'0.1E1',9(36)>, name: "Albert", surname: "Einstein">

User.all(conditions: {p_name: 'Max'})
# [#<User id: #<BigDecimal:6ee2c728,'0.3E1',9(36)>, name: "Max", surname: "Planck">]

Pipelined function arguments must be set via where condition (see p_name usage above). If not they will be set to NULL.

Oracle procedures and functions as methods of ActiveRecord objects

If you have some PL/SQL package related with AR model you could bind it to class.

CREATE OR REPLACE
PACKAGE users_pkg IS

  FUNCTION salute(
    p_name    IN VARCHAR2)
  RETURN VARCHAR2;

END users_pkg;
/

CREATE OR REPLACE
PACKAGE BODY users_pkg IS

  FUNCTION salute(
    p_name    IN VARCHAR2)
  RETURN VARCHAR2
  IS
  BEGIN
    RETURN 'Hello, ' || p_name || '!';
  END salute;

END users_pkg;
/
class User < ActiveRecord::Base
  include ActiveRecord::PLSQL::ProcedureMethods

  self.plsql_package = plsql.users_pkg
  procedure_method :salute
end

After that you can call procedure as method

einstein = User.find_by_name('Albert')
# Just pass arguments as array or hash
einstein.salute(p_name: einstein.name)  # 'Hello, Albert!'
einstein.salute([einstein.surname])     # 'Hello, Einstein!'

Support

Feel free to contact me at [email protected] or send a pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.