Class: RDBI::Driver::JDBC::Database
- Inherits:
-
RDBI::Database
- Object
- RDBI::Database
- RDBI::Driver::JDBC::Database
- Defined in:
- lib/caruby/rdbi/driver/jdbc.rb
Instance Attribute Summary collapse
-
#handle ⇒ Object
Returns the value of attribute handle.
Instance Method Summary collapse
- #commit ⇒ Object
- #disconnect ⇒ Object
-
#initialize(*args) ⇒ Database
constructor
A new instance of Database.
- #new_statement(query) ⇒ Object
- #ping ⇒ Object
- #quote(item) ⇒ Object
- #rollback ⇒ Object
- #schema ⇒ Object
- #table_schema(table_name) ⇒ Object
- #transaction(&block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Database
Returns a new instance of Database.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 47 def initialize(*args) super *args database = @connect_args[:database] || @connect_args[:dbname] || @connect_args[:db] username = @connect_args[:username] || @connect_args[:user] password = @connect_args[:password] || @connect_args[:pass] # the driver class driver_class = @connect_args[:driver_class] raise DatabaseError.new('Missing JDBC driver class') unless driver_class clazz = java.lang.Class.forName(driver_class, true, JRuby.runtime.jruby_class_loader) java.sql.DriverManager.registerDriver(clazz.newInstance) @handle = java.sql.DriverManager.getConnection( "#{database}", username, password ) self.database_name = @handle.getCatalog end |
Instance Attribute Details
#handle ⇒ Object
Returns the value of attribute handle.
45 46 47 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 45 def handle @handle end |
Instance Method Details
#commit ⇒ Object
90 91 92 93 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 90 def commit @handle.commit if @handle.getAutoCommit == false super end |
#disconnect ⇒ Object
70 71 72 73 74 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 70 def disconnect @handle.rollback if @handle.getAutoCommit == false @handle.close super end |
#new_statement(query) ⇒ Object
95 96 97 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 95 def new_statement(query) Statement.new(query, self) end |
#ping ⇒ Object
114 115 116 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 114 def ping !@handle.isClosed end |
#quote(item) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 118 def quote(item) case item when Numeric item.to_s when TrueClass "1" when FalseClass "0" when NilClass "NULL" else "'#{item.to_s}'" end end |
#rollback ⇒ Object
85 86 87 88 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 85 def rollback @handle.rollback if @handle.getAutoCommit == false super end |
#schema ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 105 def schema rs = @handle.getMetaData.getTables(nil, nil, nil, nil) tables = [] while rs.next tables << table_schema(rs.getString(3)) end tables end |
#table_schema(table_name) ⇒ Object
99 100 101 102 103 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 99 def table_schema(table_name) new_statement( "SELECT * FROM #{table_name} WHERE 1=2" ).new_execution[1] end |
#transaction(&block) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/caruby/rdbi/driver/jdbc.rb', line 76 def transaction(&block) raise RDBI::TransactionError, "Already in transaction" if in_transaction? @handle.setAutoCommit false @handle.setSavepoint super @handle.commit @handle.setAutoCommit true end |