lib/pg_conn.rb in pg_conn-0.13.5 vs lib/pg_conn.rb in pg_conn-0.14.0
- old
+ new
@@ -173,10 +173,13 @@
else
raise Error, "Illegal number of parameters: #{args.size}"
end
if @pg_connection && !using_existing_connection
+ # Set a dummy notice processor to avoid warnings on stderr
+ @pg_connection.set_notice_processor { |message| ; } # Intentionally a nop
+
# Auto-convert to ruby types
type_map = PG::BasicTypeMapForResults.new(@pg_connection)
# Use String as default type. Kills 'Warning: no type cast defined for
# type "uuid" with oid 2950..' warnings
@@ -675,11 +678,13 @@
end
end
def rollback() raise Rollback end
- # True if a transaction is in progress
+ # True if a transaction is in progress. Note that this requires all
+ # transactions to be started using PgConn's transaction methods;
+ # transactions started using raw SQL are not registered
def transaction?() !@savepoints.nil? end
# Returns number of transaction or savepoint levels
def transactions() @savepoints ? 1 + @savepoints.size : 0 end
@@ -713,37 +718,46 @@
# Does a rollback and empties the stack. This should be called in response
# to PG::Error exceptions because the whole transaction stack is
# invalid and the server is in an invalid state
#
# It is not an error to call #cancel_transaction when no transaction is in
- # progress
+ # progress, the method always succeeds
def cancel_transaction
begin
pg_exec("rollback")
rescue PG::Error
end
@savepoints = nil
end
- # Execute block within a transaction and return the result of the block.
- # The transaction can be rolled back by raising a PgConn::Rollback
- # exception in which case #transaction returns nil. Note that the
- # transaction timestamp is set to the start of the first transaction even
- # if transactions are nested
+ # Start a transaction. If called with a block, the block is executed within
+ # a transaction that is auto-committed if the commit option is true (the
+ # default). #transaction returns the result of the block or nil if no block
+ # was given
+ #
+ # The transaction can be rolled back inside the block by raising a
+ # PgConn::Rollback exception in which case #transaction returns nil. Note
+ # that the transaction timestamp is set to the start of the first
+ # transaction even if transactions are nested
def transaction(commit: true, &block)
- result = nil
- begin
+ if block_given?
+ result = nil
+ begin
+ push_transaction
+ result = yield
+ rescue PgConn::Rollback
+ pop_transaction(commit: false)
+ return nil
+ rescue PG::Error
+ @savepoints = nil
+ raise
+ end
+ pop_transaction(commit: commit)
+ result
+ else
push_transaction
- result = yield
- rescue PgConn::Rollback
- pop_transaction(commit: false)
- return nil
- rescue PG::Error
- @savepoints = nil
- raise
+ nil
end
- pop_transaction(commit: commit)
- result
end
private
# Wrapper around PG::Connection.new that switches to the postgres user
# before connecting if the current user is the root user