#!/usr/bin/perl -Ilibs

use strict;
use warnings;

package API::DB;

use DBI;
use Try::Tiny;

# explicity disconnect a database handle, 'finish'ing all child handles
sub disconnect {
	my $dbh = shift;
	my @handles = grep $_->{Active}, grep defined, @{ $dbh->{ChildHandles} };
	try { $_->finish } for @handles;
	$dbh->disconnect;
}
###############################################################################

# disconnect all active database handles
sub disconnect_all {
	my $class = shift;
	my %drivers = DBI->installed_drivers;
	my @db_handles = grep defined, map @{ $_->{ChildHandles} || [] }, values %drivers;
	disconnect( $_ ) for @db_handles;
	return 1;
}

__PACKAGE__;
