diff --git a/model/dao/CityDAO/CityDAO.php b/model/dao/CityDAO/CityDAO.php
index 5a532509f..4083aee38 100644
--- a/model/dao/CityDAO/CityDAO.php
+++ b/model/dao/CityDAO/CityDAO.php
@@ -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 $cityId 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 $cityId 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 $cityId 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.
*
diff --git a/model/dao/CityDAO/PostgreSQLCityDAO.php b/model/dao/CityDAO/PostgreSQLCityDAO.php
index 526a9cd78..9a706af65 100644
--- a/model/dao/CityDAO/PostgreSQLCityDAO.php
+++ b/model/dao/CityDAO/PostgreSQLCityDAO.php
@@ -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');
@@ -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 $row.
- * @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 $cityId 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 $cityId 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 $cityId 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.
@@ -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.
@@ -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;
@@ -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.
@@ -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);*/
diff --git a/web/services/createCitiesService.php b/web/services/createCitiesService.php
index 3fc66091f..639cdedc9 100644
--- a/web/services/createCitiesService.php
+++ b/web/services/createCitiesService.php
@@ -115,9 +115,7 @@
}
-
-
- if (!$string)
+ if (!isset($string))
{
$string = "Operation Success!";
diff --git a/web/services/deleteCitiesService.php b/web/services/deleteCitiesService.php
index ae406c9cf..886b17a39 100644
--- a/web/services/deleteCitiesService.php
+++ b/web/services/deleteCitiesService.php
@@ -116,9 +116,7 @@
}
-
-
- if (!$string)
+ if (!isset($string))
$string = "Operation Success!";
} while (false);
diff --git a/web/services/getAllCitiesService.php b/web/services/getAllCitiesService.php
index a021c9c4e..f53412799 100644
--- a/web/services/getAllCitiesService.php
+++ b/web/services/getAllCitiesService.php
@@ -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 */
diff --git a/web/services/updateCitiesService.php b/web/services/updateCitiesService.php
index 76670e68b..f6cb95cda 100644
--- a/web/services/updateCitiesService.php
+++ b/web/services/updateCitiesService.php
@@ -125,9 +125,7 @@
}
-
-
- if (!$string)
+ if (!isset($string))
{
$string = "Operation Success!";