Created
January 14, 2011 00:12
-
-
Save enebo/778895 to your computer and use it in GitHub Desktop.
Shows how you can leverage some of Java's Java integration features. Original code from http://theblasfrompas.blogspot.com/2011/01/retrieving-dbmsoutput-from-plsql-in.html.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'C:/jdev/jdcbdrivers/11.2/ojdbc6.jar' | |
require 'lib/jdbc_connection' | |
require 'lib/dbms_output' | |
# v--- java_import is better than import if you end up using rake for testing | |
# v--- java,org,edu,javax can all be supplied bare-word instead of string form | |
java_import java.sql.CallableStatement | |
java_import java.sql.Connection | |
java_import java.sql.SQLException | |
java_import java.sql.Types | |
# v--- but note we cannot bareword here because of package name 'oracle' | |
java_import 'oracle.jdbc.OracleDriver' | |
PLSQL_BLOCK = <<EOF | |
begin | |
for i in 1..10 loop | |
dbms_output.put_line('Hello JRuby at position '||i); | |
end loop; | |
end; | |
EOF | |
SHOW_STATEMENT = <<EOF | |
declare | |
l_line varchar2(255); | |
l_done number; | |
l_buffer long; | |
begin | |
loop | |
exit when length(l_buffer)+255 > :maxbytes OR l_done = 1; | |
dbms_output.get_line( l_line, l_done ); | |
l_buffer := l_buffer || l_line || chr(10); | |
end loop; | |
:done := l_done; | |
:buffer := l_buffer; | |
end; | |
EOF | |
class OracleConnection | |
def initialize(user, passwd, url) | |
@user, @passwd, @url = user, passwd, url | |
# v--- we can inline full package names if we want | |
java.sql.DriverManager.registerDriver OracleDriver.new # load driver class | |
# v--- We can use camel-case like getConnection | |
@connection = DriverManager.getConnection url, user, passwd | |
# v--- Java method turned into snake-cased setter | |
# ... setAutoCommit can also be 'set_auto_commit' or 'auto_commit =' | |
@connection.auto_commit = false | |
end | |
# add getters and setters for all attrributes we wish to expose | |
attr_reader :user, :passwd, :url, :connection | |
def prepare_call(call) | |
@connection.prepare_call call | |
end | |
def create_statement() | |
@connection.create_statement | |
end | |
def close | |
@connection.close unless @connection | |
end | |
def to_s | |
"OracleConnection [user=#{@user}, passwd=#{@passwd}, " + | |
"url=#{@url}]" | |
end | |
def self.create(user, passwd, url) | |
conn = new(user, passwd, url) | |
yield conn | |
rescue | |
puts "\n** Error occured **\n" | |
puts "Failed executing Oracle JDBC DBMS_OUTPUT demo from JRuby ", $!, "\n" | |
ensure | |
conn.close | |
end | |
end | |
class DbmsOutput | |
def initialize(conn) | |
@enable_stmt = conn.prepare_call "begin dbms_output.enable(:1); end;" | |
@disable_stmt = conn.prepare_call "begin dbms_output.disable; end;" | |
@show_stmt = conn.prepare_call SHOW_STATEMENT | |
end | |
def enable (size) | |
@enable_stmt.set_int 1, size | |
@enable_stmt.execute_update | |
end | |
def disable | |
@disable_stmt.execute_update | |
end | |
def show | |
output = "" | |
@show_stmt.register_out_parameter 2, 4 | |
@show_stmt.register_out_parameter 3, 12 | |
loop do | |
@show_stmt.set_int 1, 32000 | |
@show_stmt.execute_update | |
output += "#{@show_stmt.get_string(3)}\n" | |
break if @show_stmt.get_int(2).to_i == 1 | |
end | |
output | |
end | |
def close_all | |
@enable_stmt.close | |
@disable_stmt.close | |
@show_stmt.close | |
end | |
def self.create(connection) | |
dbms_output = new(connection) | |
yield dbms_output | |
ensure | |
dbms_output.close_all | |
end | |
end | |
user = "scott" | |
passwd = "tiger" | |
url = "jdbc:oracle:thin:@beast.au.oracle.com:1524/linux11gr2" | |
print "Run at #{Time.now} using JRuby #{RUBY_VERSION}\n\n" | |
OracleConnection.create(user, passwd, url) do |conn| | |
DbmsOutput.create(conn) do |dbms_output| | |
dbms_output.enable 1000000 | |
conn.create_statement.execute(PLSQL_BLOCK) | |
puts "** Output from PLSQL Block as follows **" | |
puts dbms_output.show | |
end | |
end | |
print "Ended at #{Time.now}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment