<?php
/**
* A class to generate data access objects from database tables.
*
* @author Christian Velin, christian.velin@conjurer.org
* @version 0.2
* @since 0.1 Added phpdoc tags to each generated variable and method.
* @package DbCodeBuilder
*
* Copyright (C) 2007 Christian Velin
*
* This program is free software; distributed under the artistic license.
*/
class DAOFormatter
{
/**
* The String variable to store all the generated code.
*
* @var String
*/
private $output;
public function __construct($tables, $columns)
{
$this -> generate($tables, $columns);
}
/**
* Master method to produce workable DAO's to work with VO's.
*
* @param Array $tables array of the tables in the db.
* @param Array $columns recursive array containing all column information.
*/
private function generate($tables, $columns)
{
for ($i = 0; $i < sizeOf($tables); $i++)
{
$this->output .= '============ ' . ucfirst($tables[$i]) . '.DAO.php ============<br><br>';
$this->output .= 'require_once(\'' . ucfirst($tables[$i]) . '.VO.php\');<br>';
$this->output .= '/**<br>';
$this->output .= ' *<br>';
$this->output .= ' */<br>';
$this->output .= 'class ' . ucfirst($tables[$i]) . 'DAO<br>';
$this->output .= '{<br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @var unknown_type<br>';
$this->output .= ' */<br>';
$this->output .= ' private $link;<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param unknown_type $link<br>';
$this->output .= ' */<br>';
$this->output .= ' public function __construct($link)<br>';
$this->output .= ' {<br>';
$this->output .= ' $this -> link = $link;<br>';
$this->output .= ' }<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param unknown_type $vo<br>';
$this->output .= ' */<br>';
$this->output .= ' public function save($vo)<br>';
$this->output .= ' {<br>';
$this->output .= ' if ($vo->get' . ucfirst($columns[$i][0]['Field']) . '() == 0)<br>';
$this->output .= ' {<br>';
$this->output .= ' $this->insert($vo);<br>';
$this->output .= ' }<br>';
$this->output .= ' else<br>';
$this->output .= ' {<br>';
$this->output .= ' $this->update($vo);<br>';
$this->output .= ' }<br>';
$this->output .= ' }<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param Integer $id<br>';
$this->output .= ' * @return unknown_type<br>';
$this->output .= ' */<br>';
$this->output .= ' public function get($id)<br>';
$this->output .= ' {<br>';
$this->output .= ' $sql = \'SELECT * FROM ' . $tables[$i] . ' WHERE ' . $columns[$i][0]['Field'] . ' = \' . $id;<br>';
$this->output .= ' $query = mysql_query($sql, $this->link);<br>';
$this->output .= ' $result = mysql_fetch_assoc($query);<br>';
$this->output .= ' $vo = new ' . ucfirst($tables[$i]) . 'VO();<br>';
$this->output .= ' $this->getFromResult($vo, $result);<br>';
$this->output .= ' return $vo;<br>';
$this->output .= ' }<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param unknown_type $vo<br>';
$this->output .= ' */<br>';
$this->output .= ' public function delete($vo)<br>';
$this->output .= ' {<br>';
$this->output .= ' $sql = \'DELETE FROM ' . $tables[$i] . ' WHERE ' . $columns[$i][0]['Field'] . ' = \' . $vo->get' . ucfirst($columns[$i][0]['Field']) . '();<br>';
$this->output .= ' $result = mysql_query($sql, $this->link);<br>';
$this->output .= ' $vo->set' . ucfirst($columns[$i][0]['Field']) . '(0);<br>';
$this->output .= ' }<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param unknown_type $vo<br>';
$this->output .= ' * @param unknown_type $result<br>';
$this->output .= ' */<br>';
$this->output .= ' private function getFromResult($vo, $result)<br>';
$this->output .= ' {<br>';
$this->output .= $this->generateUpdateVO($i, $columns);
$this->output .= ' }<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param unknown_type $vo<br>';
$this->output .= ' */<br>';
$this->output .= ' private function update($vo)<br>';
$this->output .= ' {<br>';
$this->output .= $this->generateUpdateQuery($i, $tables, $columns);
$this->output .= ' mysql_query($sql, $this->link) or die (mysql_error());<br>';
$this->output .= ' }<br><br><br><br>';
$this->output .= ' /**<br>';
$this->output .= ' * Enter description here...<br>';
$this->output .= ' *<br>';
$this->output .= ' * @param unknown_type $vo<br>';
$this->output .= ' */<br>';
$this->output .= ' private function insert($vo)<br>';
$this->output .= ' {<br>';
$this->output .= $this->generateInsertQuery($i, $tables, $columns);
$this->output .= ' mysql_query($sql, $this->link) or die (mysql_error());<br>';
$this->output .= ' $vo->setId(mysql_insert_id());<br>';
$this->output .= ' }<br>';
$this->output .= '}<br>';
$this->output .= '============ ' . ucfirst($tables[$i]) . '.DAO.php ============<br><br>';
}
}
/**
* Method to generate code needed to update the corresponding VO class.
*
* @param Integer $i keeps track of which table for which we are constructing a DAO.
* @param Array $columns recursive array containing all column information.
* @return String the generated code for updating a VO class.
*/
private function generateUpdateVO($i, $columns)
{
$method = '';
for ($j = 0; $j < sizeOf($columns[$i]); $j++)
{
$method .= '$vo->set' . ucfirst($columns[$i][$j]['Field']) . '($result[\'' . $columns[$i][$j]['Field'] . '\']);<br>';
}
return $method;
}
/**
* Method to generate the update query used to update the db from a VO class.
*
* @param Integer $i keeps track of which table for which we are constructing a DAO.
* @param Array $tables array of the tables in the db.
* @param Array $columns recursive array containing all column information.
* @return String the generated update query.
*/
private function generateUpdateQuery($i, $tables, $columns)
{
$query = '$sql = \'UPDATE ' . $tables[$i] . ' SET ';
$size = sizeOf($columns[0])-1;
for ($j = 0; $j < $size; $j++)
{
$query .= $columns[$i][$j]['Field'] .' = \\\'\'.$vo->get' . ucfirst($columns[$i][$j]['Field']) . '().\'\\\', ';
}
$query .= $columns[$i][$size]['Field'] .' = \\\'\'.$vo->get' . ucfirst($columns[$i][$size]['Field']) . '().\'\\\'';
$query .= ' WHERE ' . $columns[$i][0]['Field'] . ' = \\\'\'.$vo->get' . ucfirst($columns[$i][0]['Field']) . '().\'\\\'\';<br>';
return $query;
}
/**
* Method to generate the insert query used to create a new row in the db from a VO class.
*
* @param Integer $i keeps track of which table for which we are constructing a DAO.
* @param Array $tables array of the tables in the db.
* @param Array $columns recursive array containing all column information.
* @return String the generated insert query.
*/
private function generateInsertQuery($i, $tables, $columns)
{
$query = '$sql = \'INSERT INTO ' . $tables[$i] . ' (';
$size = sizeOf($columns[0])-1;
for ($j = 1; $j < $size; $j++)
{
$query .= $columns[$i][$j]['Field'] . ', ';
}
$query .= $columns[$i][$size]['Field'] . ') VALUES (';
for ($j = 1; $j < $size; $j++)
{
$query .= '\\\'\'.$vo->get' . ucfirst($columns[$i][$j]['Field']) . '().\'\\\', ';
}
$query .= '\\\'\'.$vo->get' . ucfirst($columns[$i][$size]['Field']) . '().\'\\\')\';<br>';
return $query;
}
/**
* Method to get the generated code.
*
* @return String the generated DAO(s).
*/
public function getOutput()
{
return $this->output;
}
}
?>
|