Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / DBConnect / Cache.pm
index b04a5b4..c133e8a 100644 (file)
@@ -35,14 +35,23 @@ sub reset {
   $dbh;
 }
 
+sub reset_all {
+  $_->{AutoCommit} || $_->rollback for values %cache;
+}
+
 sub clear {
   %cache = ();
 }
 
+sub disconnect_all_and_clear {
+  $_->disconnect for values %cache;
+  %cache = ();
+}
+
 sub _args2str {
   my (@args) = @_;
 
-  my ($dbconnect, $dbuser, $dbpasswd, $options) = @_;
+  my ($dbconnect, $dbuser, $dbpasswd, $options, $initial_sql) = @_;
   $dbconnect //= '';
   $dbuser    //= '';
   $dbpasswd  //= '';
@@ -52,7 +61,7 @@ sub _args2str {
     map { $_ => $options->{$_} }
     sort keys %$options;                  # deterministic order
 
-  join ';', apply { s/([;\\])/\\$1/g } $dbconnect, $dbuser, $dbpasswd, $options_str;
+  join ';', apply { $_ //= ''; s/([;\\])/\\$1/g } $dbconnect, $dbuser, $dbpasswd, $options_str, $initial_sql;
 }
 
 1;
@@ -67,26 +76,26 @@ SL::DBConnect::Cache - cached database handle pool
 
 =head1 SYNOPSIS
 
-  use SL::SBConnect::Cache;
+  use SL::DBConnect::Cache;
 
-  my $dbh = SL::DBConnct::Cache->get(@args);
-  SL::DBConnct::Cache->store($dbh, @args);
+  my $dbh = SL::DBConnect::Cache->get(@args);
+  SL::DBConnect::Cache->store($dbh, @args);
 
   # reset a cached handle
-  SL::DBConnct::Cache->reset($dbh);
+  SL::DBConnect::Cache->reset($dbh);
 
   # close a cached handle and forget it
-  SL::DBConnct::Cache->close($dbh);
+  SL::DBConnect::Cache->close($dbh);
 
-  SL::DBConnct::Cache->clear($dbh);
+  SL::DBConnect::Cache->clear($dbh);
 
 
 =head1 DESCRIPTION
 
-Implementes a managed cache for DB connection handles.
+Implements a managed cache for DB connection handles.
 
 The same would be possible with C<< DBI->connect_cached >>, but in that case,
-we would have no control ver the cache.
+we would have no control over the cache.
 
 =head1 METHODS
 
@@ -106,7 +115,7 @@ Rollback the connection specified by C<ARGS>.
 
 =item * C<clear>
 
-Emties the cache. If handles are not referenced otherwise, they will get
+Empties the cache. If handles are not referenced otherwise, they will get
 dropped and closed.
 
 =back