#!perl

# Simple test program using DBD::mysqlPP to connect to a MySQL server and print connected OK to std out if
#it succeeds.
#
#You will need a MySQL database to which you have access and edit the $dsnm $dbuser & $dbpass variables
#below accordingly
#
#

use DBI;

#MySQL parameters defined once here and used globally in each DB function
$dsn = "DBI:mysqlPP:database=DBNAME;host=MYSQL.SERVER.DOMAIN";
$dbuser = "DBUSERNAME";
$dbpass = "DBPLAINTXT-PASSWD";

&connectAndStart();
print("connected OK\n");
#run some query here
&rollback();


sub connectAndStart()
{
	$dbh= DBI->connect($dsn,$dbuser,$dbpass,{RaiseError => 1});
	if (!defined($dbh))
	{
		print("connectAndStart: unable to connect to database\n");
		exit 1;	
	}
		
	$sth=$dbh->prepare("BEGIN");
	if($sth->execute())
	{
		$sth->finish();
	}
	else
	{
		print("connectAndStart: connected but unable to BEGIN transaction\n");
		exit 1;	
	}
	
}

sub commit()
{
		$sth=$dbh->prepare("COMMIT");
		if ($sth->execute())
		{
			if (!($sth->finish()))
			{
				print("commit:  unable to COMMIT transaction (A)\n");
				exit 1;	
			}
			if (!($dbh->disconnect()))
			{
				print("commit:  unable to COMMIT transaction (B)\n");
				exit 1;	
			}
		}
		else
		{
			print("commit:  unable to COMMIT transaction (C)\n");
			exit 1;	
		}
		
}

sub rollback()
{
		$sth=$dbh->prepare("ROLLBACK");
		if ($sth->execute())
		{
			if (!($sth->finish()))
			{
				print("rolback:  unable to ROLLBACK transaction (A)\n");
				exit 1;	
			}
			if (!($dbh->disconnect()))
			{
				print("rollback:  unable to ROLLBACK transaction (B)\n");
				exit 1;	
			}
		}
		else
		{
			print("rollback:  unable to ROLLBACK transaction (C)\n");
			exit 1;	
		}
}
