Class: JDBCHelper::Connection
- Inherits:
-
Object
- Object
- JDBCHelper::Connection
- Defined in:
- lib/jdbc-helper/connection.rb,
lib/jdbc-helper/connection/row.rb,
lib/jdbc-helper/connection/type_map.rb,
lib/jdbc-helper/connection/result_set.rb,
lib/jdbc-helper/connection/statement_pool.rb,
lib/jdbc-helper/connection/callable_statement.rb,
lib/jdbc-helper/connection/prepared_statement.rb,
lib/jdbc-helper/connection/parameterized_statement.rb
Overview
p_upd.close
Defined Under Namespace
Classes: CallableStatement, ParameterizedStatement, PreparedStatement, ResultSet, Row, StatementPool, Transaction
Constant Summary collapse
- RUBY_SQL_TYPE_MAP =
{ Fixnum => java.sql.Types::INTEGER, Bignum => java.sql.Types::BIGINT, String => java.sql.Types::VARCHAR, Float => java.sql.Types::DOUBLE, Time => java.sql.Types::TIMESTAMP }
- GETTER_MAP =
{ java.sql.Types::TINYINT => :getInt, java.sql.Types::SMALLINT => :getInt, java.sql.Types::INTEGER => :getInt, java.sql.Types::BIGINT => :getLong, java.sql.Types::CHAR => :getString, java.sql.Types::VARCHAR => :getString, java.sql.Types::LONGVARCHAR => :getString, (java.sql.Types::NCHAR rescue nil) => :getString, (java.sql.Types::NVARCHAR rescue nil) => :getString, (java.sql.Types::LONGNVARCHAR rescue nil) => :getString, # !! MySQL function returns VARBINARY type java.sql.Types::BINARY => :getBinaryStream, java.sql.Types::VARBINARY => :getBinaryStream, java.sql.Types::LONGVARBINARY => :getBinaryStream, java.sql.Types::REAL => :getDouble, java.sql.Types::FLOAT => :getFloat, java.sql.Types::DOUBLE => :getDouble, java.sql.Types::DATE => :getDate, java.sql.Types::TIME => :getTime, java.sql.Types::TIMESTAMP => :getTimestamp, java.sql.Types::BLOB => :getBlob, java.sql.Types::CLOB => :getString, (java.sql.Types::NCLOB rescue nil) => :getString, java.sql.Types::BOOLEAN => :getBoolean }
Instance Attribute Summary collapse
-
#driver ⇒ String
readonly
JDBC driver of the connection.
-
#fetch_size ⇒ Fixnum
Returns the fetch size of the connection.
-
#url ⇒ String
readonly
JDBC URL of the connection.
Instance Method Summary collapse
-
#add_batch(qstr) ⇒ NilClass
Adds a statement to be executed in batch Adds to the batch.
-
#clear_batch ⇒ NilClass
Clears the batched statements including prepared statements.
-
#clone ⇒ JDBCHelper::Connection
Creates another connection with the same parameters as this Connection.
-
#close ⇒ NilClass
Closes the connection.
-
#closed? ⇒ Boolean
Returns if this connection is closed or not.
-
#execute(qstr) ⇒ Fixnum|ResultSet
Executes an SQL and returns the count of the update rows or a ResultSet object depending on the type of the given statement.
-
#execute_batch ⇒ Fixnum
Executes batched statements including prepared statements.
-
#function(func_name) ⇒ JDBCHelper::FunctionWrapper
Returns a function wrapper for the given function name.
-
#initialize(args = {}) ⇒ Connection
constructor
Creates a database connection.
- #inspect ⇒ String
-
#jdbc_conn ⇒ Object
(also: #java_obj, #java)
Returns the underlying JDBC Connection object.
-
#prepare(qstr) ⇒ Object
Creates a prepared statement, which is also an encapsulation of Java PreparedStatement object.
-
#prepare_call(qstr) ⇒ Object
Creates a callable statement.
-
#prepared_statements ⇒ Array
Prepared statements currently opened for this connection.
-
#procedure(proc_name) ⇒ JDBCHelper::ProcedureWrapper
Returns a procedure wrapper for the given procedure name.
-
#query(qstr) {|JDBCHelper::Connection::Row| ... } ⇒ Array
(also: #enumerate)
Executes a select query.
-
#sequence(sequence_name) ⇒ JDBCHelper::SequenceWrapper
Returns a sequence wrapper for the given name.
-
#set_fetch_size(fsz) ⇒ NilClass
(also: #fetch_size=)
Gives the JDBC driver a hint of the number of rows to fetch from the database by a single interaction.
-
#table(table_name) ⇒ JDBCHelper::TableWrapper
(also: #[])
Returns a table wrapper for the given table name.
-
#transaction {|JDBCHelper::Connection::Transaction| ... } ⇒ Boolean
Executes the given code block as a transaction.
-
#update(qstr) ⇒ Fixnum
Executes an update and returns the count of the updated rows.
Constructor Details
#initialize(args = {}) ⇒ Connection
Creates a database connection.
-
‘args` hash must include :driver (or “driver”) and :url (or “url”)
-
and takes optional :user and :password tuples (or “user”, “password”)
-
You can also specify :timeout (or “timeout”) to override the default connection timeout (60 seconds)
Must be closed explicitly if not used. If a block is given, the connection is automatically closed after executing the block.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/jdbc-helper/connection.rb', line 138 def initialize(args = {}) # Subsequent deletes should not affect the input @args = args args = InsensitiveHash[ @args ] raise ArgumentError.new("driver not given") unless args.has_key? :driver raise ArgumentError.new("url not given") unless args.has_key? :url @driver = args.delete :driver @url = args.delete :url timeout = args.has_key?(:timeout) ? args.delete(:timeout) : Constants::DEFAULT_LOGIN_TIMEOUT if timeout if timeout.is_a?(Fixnum) == false || timeout <= 0 raise ArgumentError.new("Timeout must be a positive integer") end Java::java.sql.DriverManager.setLoginTimeout timeout end props = Java::java.util.Properties.new args.each do |k, v| props.setProperty(k.to_s, v.to_s) if v end @conn = case @driver when String # NameError will be thrown for invalid drivers Java::JavaClass.for_name @driver Java::java.sql.DriverManager.get_connection(@url, props) when Class @driver.new.connect(@url, props) else raise ArgumentError.new('Invalid type for :driver') end @spool = StatementPool.send :new, self @bstmt = nil @fetch_size = nil @pstmts = [] @table_wrappers = {} if block_given? begin yield self ensure close rescue nil end end end |
Instance Attribute Details
#driver ⇒ String (readonly)
JDBC driver of the connection
120 121 122 |
# File 'lib/jdbc-helper/connection.rb', line 120 def driver @driver end |
#fetch_size ⇒ Fixnum
Returns the fetch size of the connection. If not set, nil is returned.
381 382 383 |
# File 'lib/jdbc-helper/connection.rb', line 381 def fetch_size @fetch_size end |
#url ⇒ String (readonly)
JDBC URL of the connection
116 117 118 |
# File 'lib/jdbc-helper/connection.rb', line 116 def url @url end |
Instance Method Details
#add_batch(qstr) ⇒ NilClass
Adds a statement to be executed in batch Adds to the batch
324 325 326 327 328 329 |
# File 'lib/jdbc-helper/connection.rb', line 324 def add_batch(qstr) check_closed @bstmt ||= @spool.take @bstmt.add_batch qstr end |
#clear_batch ⇒ NilClass
Clears the batched statements including prepared statements.
353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/jdbc-helper/connection.rb', line 353 def clear_batch check_closed if @bstmt @bstmt.clear_batch @spool.give @bstmt @bstmt = nil end @pstmts.each do |stmt| stmt.clear_batch end end |
#clone ⇒ JDBCHelper::Connection
Creates another connection with the same parameters as this Connection.
191 192 193 194 195 |
# File 'lib/jdbc-helper/connection.rb', line 191 def clone nc = JDBCHelper::Connection.new @args nc.fetch_size = @fetch_size if @fetch_size nc end |
#close ⇒ NilClass
Closes the connection
385 386 387 388 389 390 |
# File 'lib/jdbc-helper/connection.rb', line 385 def close return if closed? @spool.close @conn.close @conn = @spool = nil end |
#closed? ⇒ Boolean
Returns if this connection is closed or not
394 395 396 |
# File 'lib/jdbc-helper/connection.rb', line 394 def closed? @conn.nil? end |
#execute(qstr) ⇒ Fixnum|ResultSet
Executes an SQL and returns the count of the update rows or a ResultSet object depending on the type of the given statement. If a ResultSet is returned, it must be enumerated or closed.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/jdbc-helper/connection.rb', line 255 def execute(qstr) check_closed stmt = @spool.take begin if stmt.execute(qstr) ResultSet.send(:new, stmt.getResultSet) { @spool.give stmt } else rset = stmt.getUpdateCount @spool.give stmt rset end rescue Exception => e @spool.give stmt raise end end |
#execute_batch ⇒ Fixnum
Executes batched statements including prepared statements. No effect when no statement is added
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/jdbc-helper/connection.rb', line 333 def execute_batch check_closed cnt = 0 if @bstmt cnt += @bstmt.execute_batch.inject(:+) || 0 @spool.give @bstmt @bstmt = nil end @pstmts.each do |pstmt| cnt += pstmt.execute_batch end cnt end |
#function(func_name) ⇒ JDBCHelper::FunctionWrapper
Returns a function wrapper for the given function name
421 422 423 |
# File 'lib/jdbc-helper/connection.rb', line 421 def function func_name JDBCHelper::FunctionWrapper.new self, func_name end |
#inspect ⇒ String
434 435 436 437 438 |
# File 'lib/jdbc-helper/connection.rb', line 434 def inspect InsensitiveHash[@args].merge({ :closed? => closed? }).tap { |c| c.delete(:password) }.inspect end |
#jdbc_conn ⇒ Object Also known as: java_obj, java
Returns the underlying JDBC Connection object. Only use this when you really need to access it directly.
124 125 126 |
# File 'lib/jdbc-helper/connection.rb', line 124 def jdbc_conn @conn end |
#prepare(qstr) ⇒ Object
Creates a prepared statement, which is also an encapsulation of Java PreparedStatement object
199 200 201 202 203 204 205 206 207 |
# File 'lib/jdbc-helper/connection.rb', line 199 def prepare(qstr) check_closed pstmt = PreparedStatement.send(:new, self, qstr, @conn.prepare_statement(qstr)) pstmt.set_fetch_size @fetch_size if @fetch_size @pstmts << pstmt pstmt end |
#prepare_call(qstr) ⇒ Object
Creates a callable statement.
216 217 218 219 220 |
# File 'lib/jdbc-helper/connection.rb', line 216 def prepare_call(qstr) check_closed CallableStatement.send(:new, self, qstr, @conn.prepare_call(qstr)) end |
#prepared_statements ⇒ Array
Returns Prepared statements currently opened for this connection.
210 211 212 |
# File 'lib/jdbc-helper/connection.rb', line 210 def prepared_statements @pstmts end |
#procedure(proc_name) ⇒ JDBCHelper::ProcedureWrapper
Returns a procedure wrapper for the given procedure name
429 430 431 |
# File 'lib/jdbc-helper/connection.rb', line 429 def procedure proc_name JDBCHelper::ProcedureWrapper.new self, proc_name end |
#query(qstr) {|JDBCHelper::Connection::Row| ... } ⇒ Array Also known as: enumerate
Executes a select query. When a code block is given, each row of the result is passed to the block one by one. If not given, ResultSet is returned, which can be used to enumerate through the result set. ResultSet is closed automatically when all the rows in the result set is consumed.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/jdbc-helper/connection.rb', line 298 def query(qstr, &blk) check_closed stmt = @spool.take begin rset = stmt.execute_query(qstr) rescue Exception => e @spool.give stmt raise end enum = ResultSet.send(:new, rset) { @spool.give stmt } if block_given? enum.each do |row| yield row end else enum end end |
#sequence(sequence_name) ⇒ JDBCHelper::SequenceWrapper
Returns a sequence wrapper for the given name
413 414 415 |
# File 'lib/jdbc-helper/connection.rb', line 413 def sequence sequence_name JDBCHelper::SequenceWrapper.new self, sequence_name end |
#set_fetch_size(fsz) ⇒ NilClass Also known as: fetch_size=
Gives the JDBC driver a hint of the number of rows to fetch from the database by a single interaction. This is only a hint. It may have no effect at all.
371 372 373 374 375 376 |
# File 'lib/jdbc-helper/connection.rb', line 371 def set_fetch_size(fsz) check_closed @fetch_size = fsz @spool.each { | stmt | stmt.set_fetch_size @fetch_size } end |
#table(table_name) ⇒ JDBCHelper::TableWrapper Also known as: []
Returns a table wrapper for the given table name
402 403 404 405 406 |
# File 'lib/jdbc-helper/connection.rb', line 402 def table table_name table = JDBCHelper::TableWrapper.new(self, table_name) table = table.fetch_size(@fetch_size) if @fetch_size @table_wrappers[table_name] ||= table end |
#transaction {|JDBCHelper::Connection::Transaction| ... } ⇒ Boolean
Executes the given code block as a transaction. Returns true if the transaction is committed. A transaction object is passed to the block, which only has commit and rollback methods. The execution breaks out of the code block when either of the methods is called.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/jdbc-helper/connection.rb', line 227 def transaction check_closed raise ArgumentError.new("Transaction block not given") unless block_given? tx = Transaction.send :new, @conn ac = @conn.get_auto_commit status = :unknown begin @conn.set_auto_commit false yield tx @conn.commit status = :committed rescue Transaction::Commit status = :committed rescue Transaction::Rollback status = :rolledback ensure @conn.rollback if status == :unknown && @conn.get_auto_commit == false @conn.set_auto_commit ac end status == :committed end |
#update(qstr) ⇒ Fixnum
Executes an update and returns the count of the updated rows.
276 277 278 279 280 281 282 |
# File 'lib/jdbc-helper/connection.rb', line 276 def update(qstr) check_closed @spool.with do | stmt | ret = stmt.execute_update(qstr) end end |