lib/pg_conn.rb in pg_conn-0.7.3 vs lib/pg_conn.rb in pg_conn-0.8.0
- old
+ new
@@ -518,10 +518,23 @@
# TODO: Make sure the transaction stack is emptied on postgres errors
def exec(sql, commit: true, fail: true, silent: false)
transaction(commit: commit) { execute(sql, fail: fail, silent: silent) }
end
+ # Like #exec but returns true/false depending on if the command succeeded.
+ # There is not corresponding #exeucte? method because any failure rolls
+ # back the whole transaction stack. TODO: Check which exceptions that
+ # should be captured
+ def exec?(sql, commit: true, silent: true)
+ begin
+ exec(sql, commit: commit, fail: true, silent: silent)
+ rescue PG::Error
+ return false
+ end
+ return true
+ end
+
# Execute SQL statement(s) without a transaction block and return the
# number of affected records (if any). This used to call procedures that
# may manipulate transactions. The +sql+ argument can be a SQL command or
# an arbitrarily nested array of commands. The empty array is a NOP but the
# empty string is not. #execute pass Postgres exceptions to the caller
@@ -658,11 +671,11 @@
# pg_exec(string)
# pg_exec(array)
#
# Execute statement(s) on the server. If the argument is an array of
# commands, the commands are concatenated with ';' before being sent to the
- # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
+ # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
# #exec pass Postgres exceptions to the caller unless :fail is false
#
# FIXME: Error message prints the last statement but what if another
# statement failed?
#
@@ -681,9 +694,10 @@
last_stmt = arg
@pg_connection.exec(last_stmt)
else
stmts = arg.flatten.compact
return nil if stmts.empty?
+# stmts.unshift("set on_error_exit stop")
last_stmt = stmts.last
@pg_connection.exec(stmts.join(";\n"))
end
rescue PG::Error => ex