Module: Java::Jdbc
- Defined in:
- lib/jdbc.rb
Class Method Summary collapse
- .auto_close(object, &block) ⇒ Object
- .get_connection(url, user, password) ⇒ Object
- .get_rows(conn, tablename) ⇒ Object
- .get_tables(conn) ⇒ Object
-
.load_driver(driver_class) ⇒ Object
this is apparently not necessary in Java 1.6+ it implements some sort of automatic driver loading.
- .with_connection(context, &block) ⇒ Object
- .with_resultset(object, &block) ⇒ Object
-
.with_statement(object, &block) ⇒ Object
TODO: determine if it’s possible to just alias Module class methods alias :with_statement :auto_close alias :with_resultset :auto_close.
Class Method Details
.auto_close(object, &block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/jdbc.rb', line 15 def Jdbc.auto_close(object, &block) begin block.call object ensure object.close end end |
.get_connection(url, user, password) ⇒ Object
11 12 13 |
# File 'lib/jdbc.rb', line 11 def Jdbc.get_connection(url, user, password) return java.sql.DriverManager.getConnection(url, user, password) end |
.get_rows(conn, tablename) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jdbc.rb', line 38 def Jdbc.get_rows(conn, tablename) auto_close(conn.createStatement) do |stmt| rs = stmt.executeQuery("select * from " + tablename) auto_close(rs) do |rs| = rs.getMetaData column_names = [] 1.upto(.getColumnCount) do |i| column_names << .getColumnName(i) end while rs.next do column_values = [] column_names.each_with_index do |name, i| column_values << rs.getString(i + 1) end yield column_names, column_values end end end end |
.get_tables(conn) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/jdbc.rb', line 29 def Jdbc.get_tables(conn) rs = conn.getMetaData.getTables(nil, nil, nil, [ "TABLE" ].to_java(:String)) auto_close(rs) do |rs| while rs.next do yield rs.getString("TABLE_NAME") end end end |
.load_driver(driver_class) ⇒ Object
this is apparently not necessary in Java 1.6+ it implements some sort of automatic driver loading
7 8 9 |
# File 'lib/jdbc.rb', line 7 def Jdbc.load_driver(driver_class) Java::JavaClass.for_name(driver_class) end |
.with_connection(context, &block) ⇒ Object
23 24 25 26 27 |
# File 'lib/jdbc.rb', line 23 def Jdbc.with_connection(context, &block) load_driver(context[:driver_class]) if context.has_key?(:driver_class) conn = get_connection(context[:url], context[:user], context[:password]) auto_close(conn, &block) end |
.with_resultset(object, &block) ⇒ Object
64 65 66 |
# File 'lib/jdbc.rb', line 64 def Jdbc.with_resultset(object, &block) auto_close(object, &block) end |
.with_statement(object, &block) ⇒ Object
TODO: determine if it’s possible to just alias Module class methods
alias :with_statement :auto_close alias :with_resultset :auto_close
61 62 63 |
# File 'lib/jdbc.rb', line 61 def Jdbc.with_statement(object, &block) auto_close(object, &block) end |