Class: Sequel::IBMDB::Connection
Overview
Wraps an underlying connection to DB2 using IBM_DB, to provide a more rubyish API.
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#prepared_statements ⇒ Object
readonly
A hash with prepared statement name symbol keys, where each value is a two element array with an sql string and cached Statement value.
Instance Method Summary collapse
-
#autocommit ⇒ Object
Check whether the connection is in autocommit state or not.
-
#autocommit=(value) ⇒ Object
Turn autocommit on or off for the connection.
-
#close ⇒ Object
Close the connection, disconnecting from DB2.
-
#commit ⇒ Object
Commit the currently outstanding transaction on this connection.
-
#error_msg ⇒ Object
Return the related error message for the connection.
-
#error_sqlstate ⇒ Object
Return the related error message for the connection.
-
#execute(sql) ⇒ Object
Execute the given SQL on the database, and return a Statement instance holding the results.
-
#execute_prepared(ps_name, *values) ⇒ Object
Execute the related prepared statement on the database with the given arguments.
-
#initialize(connection_param) ⇒ Connection
constructor
Create the underlying IBM_DB connection.
-
#prepare(sql, ps_name) ⇒ Object
Prepare a statement with the given
sql
on the database, and cache the prepared statement value by name. -
#rollback ⇒ Object
Rollback the currently outstanding transaction on this connection.
Constructor Details
permalink #initialize(connection_param) ⇒ Connection
Create the underlying IBM_DB connection.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sequel/adapters/ibmdb.rb', line 42 def initialize(connection_param) @conn = if connection_param.class == String IBM_DB.connect(connection_param, '', '') else # connect using catalog IBM_DB.connect(*connection_param) end self.autocommit = true @prepared_statements = {} end |
Instance Attribute Details
Instance Method Details
permalink #autocommit ⇒ Object
Check whether the connection is in autocommit state or not.
54 55 56 |
# File 'lib/sequel/adapters/ibmdb.rb', line 54 def autocommit IBM_DB.autocommit(@conn) == 1 end |
permalink #autocommit=(value) ⇒ Object
Turn autocommit on or off for the connection.
59 60 61 |
# File 'lib/sequel/adapters/ibmdb.rb', line 59 def autocommit=(value) IBM_DB.autocommit(@conn, value ? IBM_DB::SQL_AUTOCOMMIT_ON : IBM_DB::SQL_AUTOCOMMIT_OFF) end |
permalink #close ⇒ Object
Close the connection, disconnecting from DB2.
64 65 66 |
# File 'lib/sequel/adapters/ibmdb.rb', line 64 def close IBM_DB.close(@conn) end |
permalink #commit ⇒ Object
Commit the currently outstanding transaction on this connection.
69 70 71 |
# File 'lib/sequel/adapters/ibmdb.rb', line 69 def commit IBM_DB.commit(@conn) end |
permalink #error_msg ⇒ Object
Return the related error message for the connection.
74 75 76 |
# File 'lib/sequel/adapters/ibmdb.rb', line 74 def error_msg IBM_DB.getErrormsg(@conn, IBM_DB::DB_CONN) end |
permalink #error_sqlstate ⇒ Object
Return the related error message for the connection.
79 80 81 |
# File 'lib/sequel/adapters/ibmdb.rb', line 79 def error_sqlstate IBM_DB.getErrorstate(@conn, IBM_DB::DB_CONN) end |
permalink #execute(sql) ⇒ Object
Execute the given SQL on the database, and return a Statement instance holding the results.
85 86 87 88 89 |
# File 'lib/sequel/adapters/ibmdb.rb', line 85 def execute(sql) stmt = IBM_DB.exec(@conn, sql) raise Error.new(error_msg, error_sqlstate) unless stmt Statement.new(stmt) end |
permalink #execute_prepared(ps_name, *values) ⇒ Object
Execute the related prepared statement on the database with the given arguments.
93 94 95 96 97 98 99 100 |
# File 'lib/sequel/adapters/ibmdb.rb', line 93 def execute_prepared(ps_name, *values) stmt = @prepared_statements[ps_name].last res = stmt.execute(*values) unless res raise Error.new("Error executing statement #{ps_name}: #{error_msg}", error_sqlstate) end stmt end |
permalink #prepare(sql, ps_name) ⇒ Object
Prepare a statement with the given sql
on the database, and cache the prepared statement value by name.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sequel/adapters/ibmdb.rb', line 104 def prepare(sql, ps_name) if stmt = IBM_DB.prepare(@conn, sql) ps_name = ps_name.to_sym stmt = Statement.new(stmt) @prepared_statements[ps_name] = [sql, stmt] else err = error_msg err = "Error preparing #{ps_name} with SQL: #{sql}" if error_msg.nil? || error_msg.empty? raise Error.new(err, error_sqlstate) end end |
permalink #rollback ⇒ Object
Rollback the currently outstanding transaction on this connection.
117 118 119 |
# File 'lib/sequel/adapters/ibmdb.rb', line 117 def rollback IBM_DB.rollback(@conn) end |