class Database
{
/**
* @access private
* @var string Username used to access the database.
*/
private $_s_username = '';
/**
* @access private
* @var string Password used to access the database.
*/
private $_s_password = '';
/**
* @access private
* @var string Identifies database to be used.
*/
private $_s_database = '';
/**
* @access private
* @var string Identifies server to connect to.
*/
private $_s_server = '';
/**
* @access private
* @var string Database type. Default is mssql.
*/
private $_s_type = '';
/**
* @access private
* @var resource Contains connection resource for database.
*/
private $_r_connection = null;
/**
* Constructor method that will default the database connection to host
* to either csdbbcdev001 or csdbbmsql001 (development or production)
* if any of the parameters are left blank (excluding type).
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @param string $username Sting that determines the LDAP server to connect to.
* @param string $password Sting that determines the LDAP server to connect to.
* @param string $database Sting that determines the LDAP server to connect to.
* @param string $server Sting that determines the LDAP server to connect to.
* @param string $type Determines database type. Can be fbsql, ibase, mssql, mysql, mysqli, oci8, pgsql, querysim, or sqlite.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
public function __construct($username = null, $password = null, $database = null, $server = null, $type = null)
{
try
{
if ((strlen($username) == 0)
|| (strlen($password) == 0)
|| (strlen($database) == 0)
|| (strlen($server) == 0))
{
$this->_s_username = 'web';
$this->_s_password = 'web';
$this->_s_database = 'case_coordination_center';
if (strtolower($_SERVER['COMPUTERNAME']) == 'csdbbcccw001') {$this->_s_server = 'csdbbmsql001';}
// else {$this->_s_server = 'csdbbcdev001';}
else {$this->_s_server = 'CSDBBCDEV001\SQLEXPRESS';}
}
else
{
$this->_s_username = $username;
$this->_s_password = $password;
$this->_s_database = $database;
$this->_s_server = $server;
}
if (strlen($type) == 0) {$this->_s_type = 'mssql';}
else {$this->_s_type = $type;}
}
catch(exception $error)
{
}
}
/**
* Destructor method that will disconnect the database and clear any residual objects from memory.
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
public function __destruct()
{
if ($this->_r_connection) {$this->_r_connection->disconnect();}
$this->_r_connection = null;
$this->_s_database = null;
$this->_s_password = null;
$this->_s_server = null;
$this->_s_type = null;
$this->_s_type = null;
$this->_s_username = null;
}
/**
* Method that is called internally when needed to connect to the database. Each method in the class will
* connect, perform its actions against the database, then disconnect again.
*
* @access private
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
private function connect()
{
$dsn = '';
try
{
$dsn = $this->_s_type . '://' . $this->_s_username . ':' . $this->_s_password . '@' . $this->_s_server . '/' . $this->_s_database;
$db =& MDB2::factory($dsn, array('debug' => 2,'result_buffering' => false));
if (PEAR::isError($db)) {throw new Exception($db->getMessage());}
else
{
$this->_r_connection = $db;
return true;
}
}
catch(exception $error)
{
return false;
}
}
/**
* Wrapper method that is called internally when needed to disconnect from the database. Each method in the
* class will connect, perform its actions against the database, then disconnect again.
*
* @access private
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @version 1
*/
private function disconnect()
{
$this->_r_connection->disconnect();
}
/**
* Method that is used to run a query.
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @return resource Recordset object.
* @version 1
*/
public function query($query_string)
{
$r_recordset = null;
try
{
$this->connect();
$r_recordset = $this->_r_connection->query($query_string);
if (PEAR::isError($r_recordset)) {throw new Exception($r_recordset->getDebugInfo());}
return $r_recordset;
}
catch (Exception $error)
{
return false;
}
$this->disconnect();
}
/**
* Method that is used to run DML, like inserts, updates, deletes.
*
* @access public
* @todo Complete exception handling functionality in try-catch blocks.
* @author Mike Bronner <michael.g.bronner@kp.org>
* @return resource Recordset object.
* @version 1
*/
public function execute($query_string)
{
$r_result = null;
try
{
$this->connect();
$r_result = $this->_r_connection->exec($query_string);
if (PEAR::isError($r_result)) {throw new Exception($r_result->getDebugInfo());}
return $r_result;
}
catch (Exception $error)
{
return false;
}
}
public function execute_stored_procedure($s_stored_procedure, $a_parameters, $a_datatypes = null)
{
$r_resultset = null;
try
{
$this->connect();
$this->_r_connection->loadModule("Function");
$r_resultset = $this->_r_connection->executeStoredProc($s_stored_procedure, $a_parameters, $a_datatypes, true, true);
if (MDB2::isError($r_resultset))
{
echo 'Standard Message: ' . $r_resultset->getMessage() . "<br />";
echo 'Standard Code: ' . $r_resultset->getCode() . "<br />";
echo 'DBMS/User Message: ' . $r_resultset->getUserInfo() . "<br />";
echo 'DBMS/Debug Message: ' . $r_resultset->getDebugInfo() . "<br />";
}
}
catch (exception $error)
{
return false;
}
return $r_resultset;
}
}