Skip to content

Commit

Permalink
[#513] Port City DAO to use PDO.
Browse files Browse the repository at this point in the history
Merge pull request #606 from Igalia/pdo-city-dao
  • Loading branch information
jaragunde authored Jan 13, 2023
2 parents 48bccb4 + 88604ed commit b8ad544
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 218 deletions.
49 changes: 1 addition & 48 deletions model/dao/CityDAO/CityDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,7 @@
*
* @see DAOFactory::getCityDAO(), CityVO
*/
abstract class CityDAO extends BaseDAO{

/** City DAO constructor.
*
* This is the base constructor of City DAOs, and it just calls its parent's constructor.
*
* @throws {@link ConnectionErrorException}
* @see BaseDAO::__construct()
*/
protected function __construct() {
parent::__construct();
}

/** City retriever by id.
*
* This function retrieves the row from City table with the id <var>$cityId</var> and creates a {@link CityVO} with its data.
*
* @param int $cityId the id of the row we want to retrieve.
* @return CityVO a value object {@link CityVO} with its properties set to the values from the row.
* @throws {@link OperationErrorException}
*/
public abstract function getById($cityId);

/** City Histories retriever by City id.
*
* This function retrieves the rows from City History table that are assigned to the City with
* the id <var>$cityId</var> and creates a {@link CityHistoryVO} with data from each row.
*
* @param int $cityId the id of the City whose City Histories we want to retrieve.
* @return array an array with value objects {@link CityHistoryVO} with their properties set to the values from the rows
* and ordered ascendantly by their database internal identifier.
* @see CityHistoryDAO
* @throws {@link OperationErrorException}
*/
public abstract function getCityHistories($cityId);

/** Common Events retriever by City id.
*
* This function retrieves the rows from Common Event table that are assigned to the City with
* the id <var>$cityId</var> and creates a {@link CommonEventVO} with data from each row.
*
* @param int $cityId the id of the City whose Common Events we want to retrieve.
* @return array an array with value objects {@link CommonEventVO} with their properties set to the values from the rows
* and ordered ascendantly by their database internal identifier.
* @see CommonEventDAO
* @throws {@link OperationErrorException}
*/
public abstract function getCommonEvents($cityId);
abstract class CityDAO extends BaseDAO {

/** Cities retriever.
*
Expand Down
205 changes: 45 additions & 160 deletions model/dao/CityDAO/PostgreSQLCityDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/

include_once(PHPREPORT_ROOT . '/util/SQLIncorrectTypeException.php');
include_once(PHPREPORT_ROOT . '/util/SQLUniqueViolationException.php');
include_once(PHPREPORT_ROOT . '/util/DBPostgres.php');
include_once(PHPREPORT_ROOT . '/model/vo/CityVO.php');
include_once(PHPREPORT_ROOT . '/model/dao/CityDAO/CityDAO.php');
Expand All @@ -42,87 +43,19 @@
*
* @see CityDAO, CityVO
*/
class PostgreSQLCityDAO extends CityDAO{
class PostgreSQLCityDAO extends CityDAO {

/** City DAO for PostgreSQL constructor.
*
* This is the constructor of the implementation for PostgreSQL of {@link CityDAO}, and it just calls its parent's constructor.
* This constructor just calls its parent's constructor. It's necessary
* to overwrite the visibility of the BaseDAO constructor, which is set
* to `protected`.
*
* @throws {@link DBConnectionErrorException}
* @see CityDAO::__construct()
* @see BaseDAO::__construct()
*/
function __construct() {
parent::__construct();
}

/** City value object constructor for PostgreSQL.
*
* This function creates a new {@link CityVO} with data retrieved from database.
*
* @param array $row an array with the City values from a row.
* @return CityVO a {@link CityVO} with its properties set to the values from <var>$row</var>.
* @see CityVO
*/
protected function setValues($row)
{
$cityVO = new CityVO();

$cityVO->setId($row['id']);
$cityVO->setName($row['name']);

return $cityVO;
}

/** Cities retriever by id.
*
* This function retrieves the row from City table with the id <var>$cityId</var> and creates a {@link CityVO} with its data.
*
* @param int $cityId the id of the row we want to retrieve.
* @return CityVO a value object {@link CityVO} with its properties set to the values from the row.
* @throws {@link SQLQueryErrorException}
*/
public function getById($cityId) {
if (!is_numeric($cityId))
throw new SQLIncorrectTypeException($cityId);
$sql = "SELECT * FROM city WHERE id=" . $cityId;
$result = $this->execute($sql);
return $result[0];
}

/** City Histories retriever by City id.
*
* This function retrieves the rows from CityHistory table that are assigned to the City with
* the id <var>$cityId</var> and creates a {@link CityHistoryVO} with data from each row.
*
* @param int $cityId the id of the City whose City Histories we want to retrieve.
* @return array an array with value objects {@link CityHistoryVO} with their properties set to the values from the rows
* and ordered ascendantly by their database internal identifier.
* @see CityHistoryDAO
* @throws {@link SQLQueryErrorException}
*/
public function getCityHistories($cityId) {

$dao = DAOFactory::getCityHistoryDAO();
return $dao->getByCityId($cityId);

}

/** Common Events retriever by City id.
*
* This function retrieves the rows from Common Event table that are assigned to the City with
* the id <var>$cityId</var> and creates a {@link CommonEventVO} with data from each row.
*
* @param int $cityId the id of the City whose Common Events we want to retrieve.
* @return array an array with value objects {@link CommonEventVO} with their properties set to the values from the rows
* and ordered ascendantly by their database internal identifier.
* @see CommonEventDAO
* @throws {@link SQLQueryErrorException}
*/
public function getCommonEvents($cityId) {

$dao = DAOFactory::getCommonEventDAO();
return $dao->getByCityId($cityId);

parent::__construct();
}

/** City retriever.
Expand All @@ -134,8 +67,8 @@ public function getCommonEvents($cityId) {
* @throws {@link SQLQueryErrorException}
*/
public function getAll() {
$sql = "SELECT * FROM city ORDER BY id ASC";
return $this->execute($sql);
return $this->runSelectQuery(
"SELECT * FROM city ORDER BY id ASC", [], 'CityVO');
}

/** City updater.
Expand All @@ -147,25 +80,21 @@ public function getAll() {
* @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException}
*/
public function update(CityVO $cityVO) {
$affectedRows = 0;

if($cityVO->getId() >= 0) {
$currcityVO = $this->getById($cityVO->getId());
}

// If the query returned a row then update
if(sizeof($currcityVO) > 0) {

$sql = "UPDATE city SET name=" . DBPostgres::checkStringNull($cityVO->getName()) . " WHERE id=".$cityVO->getId();

$res = pg_query($this->connect, $sql);

if ($res == NULL)
if (strpos(pg_last_error(), "unique_city_name"))
throw new SQLUniqueViolationException(pg_last_error());
else throw new SQLQueryErrorException(pg_last_error());
$affectedRows = 0;

$affectedRows = pg_affected_rows($res);
$sql = "UPDATE city SET name=:name WHERE id=:id";
try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":name", $cityVO->getName(), PDO::PARAM_STR);
$statement->bindValue(":id", $cityVO->getId(), PDO::PARAM_INT);
$statement->execute();

$affectedRows = $statement->rowCount();
} catch (PDOException $e) {
error_log('Query failed: ' . $e->getMessage());
if (strpos($e->getMessage(), "unique_city_name"))
throw new SQLUniqueViolationException($e->getMessage());
else throw new SQLQueryErrorException($e->getMessage());
}

return $affectedRows;
Expand All @@ -180,24 +109,25 @@ public function update(CityVO $cityVO) {
* @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException}
*/
public function create(CityVO $cityVO) {

$affectedRows = 0;

$sql = "INSERT INTO city (name) VALUES (" . DBPostgres::checkStringNull($cityVO->getName()) . ")";

$res = pg_query($this->connect, $sql);
$sql = "INSERT INTO city (name) VALUES (:name)";
try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":name", $cityVO->getName(), PDO::PARAM_STR);
$statement->execute();

if ($res == NULL)
if (strpos(pg_last_error(), "unique_city_name"))
throw new SQLUniqueViolationException(pg_last_error());
else throw new SQLQueryErrorException(pg_last_error());
$cityVO->setId($this->pdo->lastInsertId('city_id_seq'));

$cityVO->setId(DBPostgres::getId($this->connect, "city_id_seq"));

$affectedRows = pg_affected_rows($res);
$affectedRows = $statement->rowCount();
} catch (PDOException $e) {
error_log('Query failed: ' . $e->getMessage());
if (strpos($e->getMessage(), "unique_city_name"))
throw new SQLUniqueViolationException($e->getMessage());
else throw new SQLQueryErrorException($e->getMessage());
}

return $affectedRows;

}

/** City deleter.
Expand All @@ -211,63 +141,18 @@ public function create(CityVO $cityVO) {
public function delete(CityVO $cityVO) {
$affectedRows = 0;

// Check for a city ID.
if($cityVO->getId() >= 0) {
$currcityVO = $this->getById($cityVO->getId());
}

// Delete a city.
if(sizeof($currcityVO) > 0) {
$sql = "DELETE FROM city WHERE id=".$cityVO->getId();

$res = pg_query($this->connect, $sql);

if ($res == NULL) throw new SQLQueryErrorException(pg_last_error());
$sql = "DELETE FROM city WHERE id=:id";
try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":id", $cityVO->getId(), PDO::PARAM_INT);
$statement->execute();

$affectedRows = pg_affected_rows($res);
$affectedRows = $statement->rowCount();
} catch (PDOException $e) {
error_log('Query failed: ' . $e->getMessage());
throw new SQLQueryErrorException($e->getMessage());
}

return $affectedRows;
}
}




/*//Uncomment these lines in order to do a simple test of the Dao
$dao = new PostgreSQLcityDAO();
// We create a new city
$city = new cityVO();
$city->setName("Shanghai");
$dao->create($city);
print ("New city Id is ". $city->getId() ."\n");
// We search for the new Id
$city = $dao->getById($city->getId());
print ("New city Id found is ". $city->getId() ."\n");
// We update the city with a differente name
$city->setName("Laos");
$dao->update($city);
// We search for the new name
$city = $dao->getById($city->getId());
print ("New city name found is ". $city->getName() ."\n");
// We delete the new city
$dao->delete($city);*/
4 changes: 1 addition & 3 deletions web/services/createCitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@

}



if (!$string)
if (!isset($string))
{

$string = "<return service='createCities'><ok>Operation Success!</ok><cities>";
Expand Down
4 changes: 1 addition & 3 deletions web/services/deleteCitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@

}



if (!$string)
if (!isset($string))
$string = "<return service='deleteCities'><ok>Operation Success!</ok></return>";

} while (false);
Expand Down
2 changes: 1 addition & 1 deletion web/services/getAllCitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
include_once(PHPREPORT_ROOT . '/model/facade/AdminFacade.php');
include_once(PHPREPORT_ROOT . '/model/vo/AreaVO.php');

$sid = $_GET['sid'];
$sid = $_GET['sid'] ?? NULL;

do {
/* We check authentication and authorization */
Expand Down
4 changes: 1 addition & 3 deletions web/services/updateCitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@

}



if (!$string)
if (!isset($string))
{

$string = "<return service='updateCities'><ok>Operation Success!</ok><cities>";
Expand Down

0 comments on commit b8ad544

Please sign in to comment.