The system uses MySQL as the DBMS to store data regarding the fixed Deposit management process as well as some fundamental system information.
So a php script was needed to access and manipulate the database. There also was the need to manage transactions in the database so that the database will be consistent.
These two requirements were addressed by the DBAdapter class. It was developed to allow the system to access the database and to do transactions to the database in a centralized manner. In other words all other scripts that require database access will use this class to gain it.
This way allows better control of database management because of several reasons.
- Since the system is using one class to access the database DB access scripts won’t scatter all around the source code, thereby giving the programmer a much easier time when it comes to reconfigure the DB connection.
- This will also reduce the amount of code because of code reusability.
Actually DB access is achieved using 3 class files (DBAdapter, FileAdapter, XMLAdapter) and 1 XML file (dbconfig.xml).
Why so many classes?
Well, first of all let me explain how a connection is made to the database.
All credentials for accessing the database such as Host, User, Password and Database Name are stored in the dbconfig.xml.
So to get those values from the XML first you need to read the file (FileAdapter is used). Then you need to read each XML element (XMLAdapter is used) then you can make the connection (DBAdapter).
Then why can’t you write one class to do all these?
Well, you can. But then, what if you wanted to read a different kind of file to do something else? Then you will have to write code to open and read files all over again. When you create large systems you have to consider the big picture. That is why these functionalities are divided into several classes. Code reusability can be achieved. And that will definitely improve the clarity of the code.
The code looks something like this for accessing the database.
Note: Only the necessary methods of each class are given below.
class FileAdapter
{
/**
*Open the file
*/
function openFile($fileName, $accessMode)
{
$fp = fopen($fileName, $accessMode) or die("Error opening file");
return $fp;
}
/**
*Close the file
*/
function closeFile($pointer)
{
if($pointer)
{
fclose($pointer);
}
}
/**
*Read the file and store data in a variable
*/
function readFile($fileName)
{
$fp = $this -> openFile($fileName, "r"); //file mode “r” means read
$data;
if($fp)
{
while(!feof($fp))
{
$data .= fgets($fp, 4096); //get one line at a time
}
}
$this -> closeFile($fp);
return $data;
}
}
//imports___________________________
require_once('FileAdapter.php');
//_________________________________
class XMLAdapter
{
/**
*Open the xml file and get the content
*/
function getXMLContent($fileName)
{
if($fileName != "" && substr($fileName, -3) == "xml")
{
$FileAdapter = new FileAdapter;
$xml = $FileAdapter -> readFile($fileName);
return $xml;
}
else
{
return false;
}
}
/**
*Get the content into an array of XML tags
*/
function getXMLTagArray($fileName)
{
$FileAdapter = new FileAdapter;
$xml = $FileAdapter -> readFile($fileName);
//Create an XML parser
$xmlparser = xml_parser_create();
xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xmlparser, $xml, $arrValues);
xml_parser_free($xmlparser);
return $arrValues;
}
/**
*Get values in XML tags to array variables
*/
function getXMLData($fileName)
{
$arrTemp = $this -> getXMLTagArray($fileName);
$arrValues = array();
foreach($arrTemp as $arrSub)
{
if($arrSub['type'] == "complete")
{
$arrValues[] = $arrSub['value'];
}
}
return $arrValues;
}
}
//imports__________________________________
require_once('XMLAdapter.php');
//_______________________________________
class DBAdapter
{
/**
*Connect to the database
*/
function connect()
{
//Get connection info from the config XML
$XMLAdapter = new XMLAdapter;
$db = $XMLAdapter -> getXMLData($_SERVER['DOCUMENT_ROOT']."/FixedDeposit/config/dbconfig.xml");
$server = $db[0];
$user = $db[1];
$password = $db[2];
$dbName = $db[3];
//Establish connection
$connection = mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($dbName,$connection) or die(mysql_error());
return $connection;
}
/**
*Close connection
*/
function close($connection)
{
if($connection)
{
mysql_close($connection);
}
}
/**
*Start a new transaction
*/
function begin($connection)
{
mysql_query("BEGIN", $connection);
}
/**
*Commit a transaction
*/
function commit($connection)
{
mysql_query("COMMIT", $connection);
}
/**
*Rollback a transaction
*/
function rollback($connection, $terminate = false)
{
mysql_query("ROLLBACK", $connection);
if($terminate)
{
$this -> close($connection);
}
}
/**
*Run query without transaction control
*/
function runQueryDirect($query)
{
//connect
$connection = $this -> connect();
//run query
$result = mysql_query($query, $connection) or die(mysql_error());
//close connection
$this -> close($connection);
return $result;
}
/**
*Run query with transaction control
*/
function runQueryTrans($query)
{
//connect
$connection = $this -> connect();
//start the transaction
$this->begin($connection);
//run query
$result = mysql_query($query, $connection);
//check for success
if($result)
{
//commit
$this->commit($connection);
}
else
{
//rollback
$this->rollback($connection);
}
$this -> close($connection);
return $result;
}
}
No comments:
Post a Comment