From 9b78fe861eb4ca608c75a888cc854a12f2e8d3db Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Tue, 6 Mar 2018 13:55:13 -0500 Subject: Bug 1443537 - Add utility functions for switching database handle to writeable or read-only mode --- Bugzilla/Util.pm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'Bugzilla') diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm index 91f06e650..7d85a4dfd 100644 --- a/Bugzilla/Util.pm +++ b/Bugzilla/Util.pm @@ -14,6 +14,7 @@ use warnings; use base qw(Exporter); @Bugzilla::Util::EXPORT = qw(trick_taint detaint_natural detaint_signed + with_writable_database with_readonly_database html_quote url_quote xml_quote css_class_quote html_light_quote i_am_cgi i_am_webservice correct_urlbase remote_ip @@ -44,6 +45,31 @@ use Encode qw(encode decode resolve_alias); use Encode::Guess; use POSIX qw(floor ceil); use Taint::Util qw(untaint); +use Try::Tiny; + +sub with_writable_database(&) { + my ($code) = @_; + my $dbh = Bugzilla->dbh_main; + local Bugzilla->request_cache->{dbh} = $dbh; + local Bugzilla->request_cache->{error_mode} = ERROR_MODE_DIE; + try { + $dbh->bz_start_transaction; + $code->(); + $dbh->bz_commit_transaction; + } catch { + $dbh->bz_rollback_transaction; + # re-throw + die $_; + }; +} + +sub with_readonly_database(&) { + my ($code) = @_; + local Bugzilla->request_cache->{dbh} = undef; + local Bugzilla->request_cache->{error_mode} = ERROR_MODE_DIE; + Bugzilla->switch_to_shadow_db(); + $code->(); +} sub trick_taint { untaint($_[0]); -- cgit v1.2.3-24-g4f1b