I was asked to write a simple but well written class as an example of my work for a potential job. I had a script that updated a number of MySQL databases with a list of SQL of statements which I decided would be a good thing to write properly to show them.
The class is shown below. Please let me know what you think as I would really like some professional feed back before I send it off.
One thing I will note is that I have not yet sub-classed Exception. This is already in the list to be done.
Thanks!
Code: Select all
<?php
class dbUpdater {
private $dbListLocation;
private $commandListLocation;
private $dbList;
private $commandList;
public function __construct($DBLocation, $commandLocation) {
// Set the lists to the given locations
$this->setLists($DBLocation, $commandLocation);
}
/**
* SetLists()
*
* Sets the location of the database and command lists
* If either of the locations is not send as a string an exception is thrown
*
* @access public
* @param string $DBLocation
* @param string $commandLocation
*
*/
public function setLists($DBLocation, $commandLocation) {
if(is_string($DBLocation) && is_string($commandLocation)) {
// Set location Vars for the database and command list
$this->dbListLocation = $DBLocation;
$this->commandListLocation = $commandLocation;
// Set the database and command lists
$this->createLists();
}
else {
throw new Exception( "Class dbUpdater - Both List arguments must be of type string!");
}
}
/**
* CreateLists()
*
* Creates an array of databases to update and an array of commands to issue
* based on the current dbList and commandList respectively.
* If either of the files do not exist, and exception is thrown.
*
* @access private
*
*/
private function createLists() {
if(file_exists($this->dbListLocation)) {
// Pick up list of datbase names
$this->dbList = file($this->dbListLocation);
}
else {
throw new Exception("Class dbUpdater - Database list file ({$this->dbListLocation}) does not exist.");
}
if(file_exists($this->commandListLocation)) {
// Pick up list of commands to issue on each DB
$this->commandList = file($this->commandListLocation);
}
else {
throw new Exception("Class dbUpdater - Database list file ({$this->commandListLocation}) does not exist.");
}
}
/**
* updateDBs()
*
* Issue the commands stored in $commandList on the databases stored in $dbList
* The method will thrown an exception if the it cannot connect to the speicifed database
* or if an SQL command cannot be issued to the specified database
*
* @access public
* @param resource $db
*
*/
public function updateDBs($dbc) {
if(!is_resource($dbc)) {
throw new Exception("Class dbUpdater - Database resource is not valid");
}
foreach($this->dbList as $database) {
// Connect to database
if(!empty($database)) {
$database = trim($database);
$con = mysqli_select_db($dbc, $database);
if(!$con) {
throw new Exception("Class dbUpdater - Unable to connect to database '$database'");
}
// Issue SQL
foreach($this->commandList as $query) {
if(!empty($query)) {
$result = mysqli_query($dbc, $query);
if(!$result) {
throw new Exception("Class dbUpdater - Unable to execute query ($query) on database '$database'");
}
}
}
}
}
}
/**
* getDBListLocation()
*
* Returns the location of the database list as a string
*
* @access public
*
*/
public function getDBListLocation() {
return $this->dbListLocation;
}
/**
* getCommandListLocation()
*
* Returns the location of the command list as a string
*
* @access public
*
*/
public function getCommandListLocation() {
return $this->commandListLocation;
}
/**
* getDBList()
*
* Returns the database list as an array
*
* @access public
*
*/
public function getDBList() {
return $this->dbList;
}
/**
* getCommandList()
*
* Returns the SQL command list as an array
*
* @access public
*
*/
public function getCommandList() {
return $this->commandList;
}
}
?>pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: