From 79b6d7d2b27de8c95178b16dd62476611f4b5dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Mon, 17 Jan 2022 14:57:16 +0100 Subject: [PATCH 1/5] Remove redundant SectorDAO constructor. --- model/dao/SectorDAO/PostgreSQLSectorDAO.php | 2 +- model/dao/SectorDAO/SectorDAO.php | 11 ----------- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/model/dao/SectorDAO/PostgreSQLSectorDAO.php b/model/dao/SectorDAO/PostgreSQLSectorDAO.php index c665688a7..b2afa264b 100644 --- a/model/dao/SectorDAO/PostgreSQLSectorDAO.php +++ b/model/dao/SectorDAO/PostgreSQLSectorDAO.php @@ -51,7 +51,7 @@ class PostgreSQLSectorDAO extends SectorDAO{ * @see SectorDAO::__construct() */ function __construct() { - parent::__construct(); + parent::__construct(); } /** Sector value object constructor for PostgreSQL. diff --git a/model/dao/SectorDAO/SectorDAO.php b/model/dao/SectorDAO/SectorDAO.php index 53d6f32d5..80b523514 100644 --- a/model/dao/SectorDAO/SectorDAO.php +++ b/model/dao/SectorDAO/SectorDAO.php @@ -40,17 +40,6 @@ */ abstract class SectorDAO extends BaseDAO{ - /** Sector DAO constructor. - * - * This is the base constructor of Sector DAOs, and it just calls its parent's constructor. - * - * @throws {@link ConnectionErrorException} - * @see BaseDAO::__construct() - */ - protected function __construct() { - parent::__construct(); - } - /** Sector retriever by id. * * This function retrieves the row from Sector table with the id $sectorId and creates a {@link SectorVO} with its data. From c2e78a3806bd99851d08511ef820f9ba7434c393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Mon, 17 Jan 2022 15:08:38 +0100 Subject: [PATCH 2/5] Migrate SectorDAO::getById and getAll to PDO. We also remove the implementation of the method setValues() because it is not used any more, although we cannot remove the function yet. --- model/dao/SectorDAO/PostgreSQLSectorDAO.php | 37 +++++++++------------ 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/model/dao/SectorDAO/PostgreSQLSectorDAO.php b/model/dao/SectorDAO/PostgreSQLSectorDAO.php index b2afa264b..6d7faae27 100644 --- a/model/dao/SectorDAO/PostgreSQLSectorDAO.php +++ b/model/dao/SectorDAO/PostgreSQLSectorDAO.php @@ -54,40 +54,33 @@ function __construct() { parent::__construct(); } - /** Sector value object constructor for PostgreSQL. - * - * This function creates a new {@link SectorVO} with data retrieved from database. - * - * @param array $row an array with the Sector values from a row. - * @return SectorVO an {@link SectorVO} with its properties set to the values from $row. - * @see SectorVO + /** + * This method is declared to fulfill this class as non-abstract, but it should not be used. + * PDO::FETCH_CLASS now takes care of transforming DB rows into VO objects. */ protected function setValues($row) { - - $sectorVO = new SectorVO(); - - $sectorVO->setId($row['id']); - $sectorVO->setName($row['name']); - - return $sectorVO; - + error_log("Unused SectorDAO::setValues() called"); } /** Sector retriever by id for PostgreSQL. * - * This function retrieves the row from Sector table with the id $sectorId and creates a {@link SectorVO} with its data. + * This function retrieves the row from Sector table with the id + * $sectorId and creates a {@link SectorVO} with its data. * * @param int $sectorId the id of the row we want to retrieve. - * @return SectorVO a value object {@link SectorVO} with its properties set to the values from the row. + * @return SectorVO a value object {@link SectorVO} with its properties set + * to the values from the row, or NULL if no object was found for that id. * @throws {@link SQLQueryErrorException} */ public function getById($sectorId) { if (!is_numeric($sectorId)) - throw new SQLIncorrectTypeException($sectorId); - $sql = "SELECT * FROM sector WHERE id=" . $sectorId; - $result = $this->execute($sql); - return $result[0]; + throw new SQLIncorrectTypeException($customerId); + $result = $this->runSelectQuery( + "SELECT * FROM sector WHERE id=:sectorId", + [':sectorId' => $sectorId], + 'SectorVO'); + return $result[0] ?? NULL; } /** Customers retriever by Sector id for PostgreSQL. @@ -118,7 +111,7 @@ public function getCustomers($sectorId) { */ public function getAll() { $sql = "SELECT * FROM sector ORDER BY id ASC"; - return $this->execute($sql); + return $this->runSelectQuery($sql, array(), 'SectorVO'); } /** Sector updater for PostgreSQL. From 06c201382b18761cbaf852412fb1c640ef8160ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Mon, 17 Jan 2022 15:27:14 +0100 Subject: [PATCH 3/5] Migrate SectorDAO create/update/delete operations to PDO. We also remove unnecessary checks for the customer id in update and delete operations. --- model/dao/SectorDAO/PostgreSQLSectorDAO.php | 72 +++++++++------------ 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/model/dao/SectorDAO/PostgreSQLSectorDAO.php b/model/dao/SectorDAO/PostgreSQLSectorDAO.php index 6d7faae27..ec740f453 100644 --- a/model/dao/SectorDAO/PostgreSQLSectorDAO.php +++ b/model/dao/SectorDAO/PostgreSQLSectorDAO.php @@ -123,26 +123,19 @@ public function getAll() { * @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException} */ public function update(SectorVO $sectorVO) { - $affectedRows = 0; - if($sectorVO->getId() >= 0) { - $currsectorVO = $this->getById($sectorVO->getId()); - } - - // If the query returned a row then update - if(sizeof($currsectorVO) > 0) { - - $sql = "UPDATE sector SET name=" . DBPostgres::checkStringNull($sectorVO->getName()) . " WHERE id=".$sectorVO->getId(); - - $res = pg_query($this->connect, $sql); - - if ($res == NULL) - if (strpos(pg_last_error(), "unique_sector_name")) - throw new SQLUniqueViolationException(pg_last_error()); - else throw new SQLQueryErrorException(pg_last_error()); - - $affectedRows = pg_affected_rows($res); + $sql = "UPDATE sector SET name=:name WHERE id=:id"; + try { + $statement = $this->pdo->prepare($sql); + $statement->bindValue(":name", $sectorVO->getName(), PDO::PARAM_STR); + $statement->bindValue(":id", $sectorVO->getId(), PDO::PARAM_INT); + $statement->execute(); + + $affectedRows = $statement->rowCount(); + } catch (PDOException $e) { + error_log('Query failed: ' . $e->getMessage()); + throw new SQLQueryErrorException($e->getMessage()); } return $affectedRows; @@ -159,18 +152,19 @@ public function update(SectorVO $sectorVO) { public function create(SectorVO $sectorVO) { $affectedRows = 0; - $sql = "INSERT INTO sector (name) VALUES (" . DBPostgres::checkStringNull($sectorVO->getName()) . ")"; + $sql = "INSERT INTO sector (name) VALUES (:name)"; + try { + $statement = $this->pdo->prepare($sql); + $statement->bindValue(":name", $sectorVO->getName(), PDO::PARAM_STR); + $statement->execute(); - $res = pg_query($this->connect, $sql); + $sectorVO->setId($this->pdo->lastInsertId('sector_id_seq')); - if ($res == NULL) - if (strpos(pg_last_error(), "unique_sector_name")) - throw new SQLUniqueViolationException(pg_last_error()); - else throw new SQLQueryErrorException(pg_last_error()); - - $sectorVO->setId(DBPostgres::getId($this->connect, "sector_id_seq")); - - $affectedRows = pg_affected_rows($res); + $affectedRows = $statement->rowCount(); + } catch (PDOException $e) { + error_log('Query failed: ' . $e->getMessage()); + throw new SQLQueryErrorException($e->getMessage()); + } return $affectedRows; @@ -187,19 +181,17 @@ public function create(SectorVO $sectorVO) { public function delete(SectorVO $sectorVO) { $affectedRows = 0; - // Check for a sector ID. - if($sectorVO->getId() >= 0) { - $currsectorVO = $this->getById($sectorVO->getId()); - } + $sql = "DELETE FROM sector WHERE id=:id"; + try { + $statement = $this->pdo->prepare($sql); + $statement->bindValue(":id", $sectorVO->getId(), PDO::PARAM_INT); + $statement->execute(); - // Delete a sector. - if(sizeof($currsectorVO) > 0) { - $sql = "DELETE FROM sector WHERE id=".$sectorVO->getId(); - - $res = pg_query($this->connect, $sql); - if ($res == NULL) throw new SQLQueryErrorException(pg_last_error()); - $affectedRows = pg_affected_rows($res); - } + $affectedRows = $statement->rowCount(); + } catch (PDOException $e) { + error_log('Query failed: ' . $e->getMessage()); + throw new SQLQueryErrorException($e->getMessage()); + } return $affectedRows; } From d8b73be8a720c3547bf4143424bb750c833e8fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Mon, 17 Jan 2022 15:30:57 +0100 Subject: [PATCH 4/5] [#431] Fix 'undefined variable: string' in sector services. --- web/services/createSectorsService.php | 7 +------ web/services/deleteSectorsService.php | 7 +------ web/services/updateSectorsService.php | 7 +------ 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/web/services/createSectorsService.php b/web/services/createSectorsService.php index ec496f438..2df88c397 100644 --- a/web/services/createSectorsService.php +++ b/web/services/createSectorsService.php @@ -108,9 +108,6 @@ } while ($parser->read()); - //var_dump($createSectors); - - if (count($createSectors) >= 1) foreach((array)$createSectors as $createSector) { @@ -122,9 +119,7 @@ } - - - if (!$string) + if (!isset($string)) { $string = "Operation Success!"; diff --git a/web/services/deleteSectorsService.php b/web/services/deleteSectorsService.php index 955cbd738..1d877bfb2 100644 --- a/web/services/deleteSectorsService.php +++ b/web/services/deleteSectorsService.php @@ -108,9 +108,6 @@ } while ($parser->read()); - //var_dump($deleteSectors); - - if (count($deleteSectors) >= 1) foreach((array)$deleteSectors as $deleteSector) { @@ -122,9 +119,7 @@ } - - - if (!$string) + if (!isset($string)) $string = "Operation Success!"; } while (false); diff --git a/web/services/updateSectorsService.php b/web/services/updateSectorsService.php index 4452e4d15..a57cd6e53 100644 --- a/web/services/updateSectorsService.php +++ b/web/services/updateSectorsService.php @@ -117,9 +117,6 @@ } while ($parser->read()); - //var_dump($createSectors); - - if (count($updateSectors) >= 1) foreach((array)$updateSectors as $updateSector) { @@ -131,9 +128,7 @@ } - - - if (!$string) + if (!isset($string)) { $string = "Operation Success!"; From b8f9d25cc2207d3ab3d5ceaf665f983e09583078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacobo=20Aragunde=20P=C3=A9rez?= Date: Mon, 17 Jan 2022 15:35:20 +0100 Subject: [PATCH 5/5] Remove commented test lines in PostgreSQLSectorDAO. --- model/dao/SectorDAO/PostgreSQLSectorDAO.php | 41 --------------------- 1 file changed, 41 deletions(-) diff --git a/model/dao/SectorDAO/PostgreSQLSectorDAO.php b/model/dao/SectorDAO/PostgreSQLSectorDAO.php index ec740f453..9c9d7d328 100644 --- a/model/dao/SectorDAO/PostgreSQLSectorDAO.php +++ b/model/dao/SectorDAO/PostgreSQLSectorDAO.php @@ -196,44 +196,3 @@ public function delete(SectorVO $sectorVO) { return $affectedRows; } } - - - - -/*//Uncomment these lines in order to do a simple test of the Dao - - - -$dao = new PostgreSQLSectorDAO(); - -// We create a new sector - -$sector = new sectorVO(); - -$sector->setName("Telenet"); - -$dao->create($sector); - -print ("New sector Id is ". $sector->getId() ."\n"); - -// We search for the new Id - -$sector = $dao->getById($sector->getId()); - -print ("New sector Id found is ". $sector->getId() ."\n"); - -// We update the sector with a differente name - -$sector->setName("Intranet"); - -$dao->update($sector); - -// We search for the new name - -$sector = $dao->getById($sector->getId()); - -print ("New sector name found is ". $sector->getName() ."\n"); - -// We delete the new sector - -$dao->delete($sector);*/