|
| 1 | +ruby-plsql |
| 2 | +========== |
| 3 | + |
| 4 | +Ruby API for calling Oracle PL/SQL procedures. |
| 5 | + |
| 6 | +DESCRIPTION |
| 7 | +----------- |
| 8 | + |
| 9 | +ruby-plsql gem provides simple Ruby API for calling Oracle PL/SQL procedures. It could be used both for accessing Oracle PL/SQL API procedures in legacy applications as well as it could be used to create PL/SQL unit tests using Ruby testing libraries. |
| 10 | + |
| 11 | +NUMBER, BINARY_INTEGER, PLS_INTEGER, VARCHAR2, NVARCHAR2, CHAR, NCHAR, DATE, TIMESTAMP, CLOB, BLOB, BOOLEAN, PL/SQL RECORD, TABLE, VARRAY, OBJECT and CURSOR types are supported for input and output parameters and return values of PL/SQL procedures and functions. |
| 12 | + |
| 13 | +ruby-plsql supports Ruby 1.8.7, 1.9.3 and JRuby 1.6.7 implementations. |
| 14 | + |
| 15 | +USAGE |
| 16 | +----- |
| 17 | + |
| 18 | +### Calling PL/SQL functions and procedures: |
| 19 | + |
| 20 | +```ruby |
| 21 | +require "rubygems" |
| 22 | +require "ruby-plsql" |
| 23 | + |
| 24 | +plsql.connection = OCI8.new("hr","hr","xe") |
| 25 | + |
| 26 | +plsql.test_uppercase('xxx') # => "XXX" |
| 27 | +plsql.test_uppercase(:p_string => 'xxx') # => "XXX" |
| 28 | +plsql.test_copy("abc", nil, nil) # => { :p_to => "abc", :p_to_double => "abcabc" } |
| 29 | +plsql.test_copy(:p_from => "abc", :p_to => nil, :p_to_double => nil) |
| 30 | + # => { :p_to => "abc", :p_to_double => "abcabc" } |
| 31 | +plsql.hr.test_uppercase('xxx') # => "XXX" |
| 32 | +plsql.test_package.test_uppercase('xxx') # => 'XXX' |
| 33 | + |
| 34 | +# PL/SQL records or object type parameters should be passed as Hash |
| 35 | +p_employee = { :employee_id => 1, :first_name => 'First', :last_name => 'Last', :hire_date => Time.local(2000,01,31) } |
| 36 | +plsql.test_full_name(p_employee) |
| 37 | + |
| 38 | +# TABLE or VARRAY parameters should be passed as Array |
| 39 | +plsql.test_sum([1,2,3,4]) |
| 40 | + |
| 41 | +# Nested objects or arrays are also supported |
| 42 | +p_employee = { :employee_id => 1, :first_name => 'First', :last_name => 'Last', :hire_date => Time.local(2000,01,31), |
| 43 | + :address => {:street => 'Street', :city => 'City', :country => 'Country'}, |
| 44 | + :phones => [{:type => 'mobile', :phone_number => '123456'}, {:type => 'fixed', :phone_number => '654321'}]} |
| 45 | +plsql.test_store_employee(p_employee) |
| 46 | + |
| 47 | +# Returned cursor can be fetched |
| 48 | +plsql.test_cursor do |cursor| |
| 49 | + cursor.fetch # => one row from cursor |
| 50 | + cursor.fetch_all # => all rows from cursor |
| 51 | +end |
| 52 | + |
| 53 | +plsql.connection.autocommit = false |
| 54 | +plsql.commit |
| 55 | +plsql.rollback |
| 56 | + |
| 57 | +plsql.logoff |
| 58 | +``` |
| 59 | + |
| 60 | +Look at RSpec tests under spec directory for more usage examples. |
| 61 | + |
| 62 | + |
| 63 | +### Table operations: |
| 64 | + |
| 65 | +ruby-plsql also provides simple API for select/insert/update/delete table operations (with Sequel-like syntax). This could be useful if ruby-plsql is used without ActiveRecord (e.g. for writing PL/SQL unit tests): |
| 66 | + |
| 67 | +```ruby |
| 68 | +# insert record in table |
| 69 | +employee = { :employee_id => 1, :first_name => 'First', :last_name => 'Last', :hire_date => Time.local(2000,01,31) } |
| 70 | +plsql.employees.insert employee # INSERT INTO employees VALUES (1, 'First', 'Last', ...) |
| 71 | + |
| 72 | +# insert many records |
| 73 | +employees = [employee1, employee2, ... ] # array of many Hashes |
| 74 | +plsql.employees.insert employees |
| 75 | + |
| 76 | +# insert many records as list of values |
| 77 | +plsql.employees.insert_values [:employee_id, :first_name, :last_name], |
| 78 | + [1, 'First 1', 'Last 1'], |
| 79 | + [2, 'First 2', 'Last 2'] |
| 80 | + |
| 81 | +# select one record |
| 82 | +plsql.employees.first # SELECT * FROM employees |
| 83 | + # fetch first row => {:employee_id => ..., :first_name => '...', ...} |
| 84 | +plsql.employees.first(:employee_id => 1) # SELECT * FROM employees WHERE employee_id = 1 |
| 85 | +plsql.employees.first("WHERE employee_id = 1") |
| 86 | +plsql.employees.first("WHERE employee_id = :employee_id", 1) |
| 87 | + |
| 88 | +# select many records |
| 89 | +plsql.employees.all # => [{...}, {...}, ...] |
| 90 | +plsql.employees.all(:order_by => :employee_id) |
| 91 | +plsql.employees.all("WHERE employee_id > :employee_id", 5) |
| 92 | + |
| 93 | +# count records |
| 94 | +plsql.employees.count # SELECT COUNT(*) FROM employees |
| 95 | +plsql.employees.count("WHERE employee_id > :employee_id", 5) |
| 96 | + |
| 97 | +# update records |
| 98 | +plsql.employees.update(:first_name => 'Second', :where => {:employee_id => 1}) |
| 99 | + # UPDATE employees SET first_name = 'Second' WHERE employee_id = 1 |
| 100 | + |
| 101 | +# delete records |
| 102 | +plsql.employees.delete(:employee_id => 1) # DELETE FROM employees WHERE employee_id = 1 |
| 103 | + |
| 104 | +# select from sequences |
| 105 | +plsql.employees_seq.nextval # SELECT employees_seq.NEXTVAL FROM dual |
| 106 | +plsql.employees_seq.currval # SELECT employees_seq.CURRVAL FROM dual |
| 107 | + |
| 108 | + |
| 109 | +### Usage with Rails: |
| 110 | + |
| 111 | +If using with Rails then include in initializer file: |
| 112 | + |
| 113 | +```ruby |
| 114 | +plsql.activerecord_class = ActiveRecord::Base |
| 115 | +``` |
| 116 | + |
| 117 | +and then you do not need to specify plsql.connection (this is also safer when ActiveRecord reestablishes connection to database). |
| 118 | + |
| 119 | +INSTALLATION |
| 120 | +------------ |
| 121 | + |
| 122 | +Install as gem with |
| 123 | + |
| 124 | + gem install ruby-plsql |
| 125 | + |
| 126 | +or include gem in Gemfile if using bundler. |
| 127 | + |
| 128 | +In addition install either ruby-oci8 (for MRI/YARV) or copy Oracle JDBC driver to $JRUBY_HOME/lib (for JRuby). |
| 129 | + |
| 130 | +If you are using MRI 1.8 or 1.9 Ruby implementation then you need to install ruby-oci8 gem (version 2.0.x or 2.1.x) |
| 131 | +as well as Oracle client, e.g. [Oracle Instant Client](http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html). |
| 132 | + |
| 133 | +If you are using JRuby then you need to download latest [Oracle JDBC driver](http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html) - either ojdbc6.jar for Java 6 or ojdbc5.jar for Java 5. And copy this file to one of these locations: |
| 134 | + |
| 135 | +* in `./lib` directory of Rails application and require it manually |
| 136 | +* in some directory which is in `PATH` |
| 137 | +* in `JRUBY_HOME/lib` directory |
| 138 | +* or include path to JDBC driver jar file in Java `CLASSPATH` |
| 139 | + |
| 140 | + |
| 141 | +LINKS |
| 142 | +----- |
| 143 | + |
| 144 | +* Source code: http://github.com/rsim/ruby-plsql |
| 145 | +* Bug reports / Feature requests: http://github.com/rsim/ruby-plsql/issues |
| 146 | +* Discuss at oracle_enhanced adapter group: http://groups.google.com/group/oracle-enhanced |
| 147 | + |
| 148 | +CONTRIBUTORS |
| 149 | +------------ |
| 150 | + |
| 151 | +* Raimonds Simanovskis |
| 152 | +* Edgars Beigarts |
| 153 | +* Oleh Mykytyuk |
| 154 | +* Wiehann Matthysen |
| 155 | +* Dayle Larson |
| 156 | +* Yasuo Honda |
| 157 | + |
| 158 | +LICENSE |
| 159 | +------- |
| 160 | + |
| 161 | +(The MIT License) |
| 162 | + |
| 163 | +Copyright (c) 2008-2012 Raimonds Simanovskis |
| 164 | + |
| 165 | +Permission is hereby granted, free of charge, to any person obtaining |
| 166 | +a copy of this software and associated documentation files (the |
| 167 | +'Software'), to deal in the Software without restriction, including |
| 168 | +without limitation the rights to use, copy, modify, merge, publish, |
| 169 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 170 | +permit persons to whom the Software is furnished to do so, subject to |
| 171 | +the following conditions: |
| 172 | + |
| 173 | +The above copyright notice and this permission notice shall be |
| 174 | +included in all copies or substantial portions of the Software. |
| 175 | + |
| 176 | +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| 177 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 178 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 179 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 180 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 181 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 182 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments