Skip to content

Commit

Permalink
[#513] Port Area DAO to use PDO.
Browse files Browse the repository at this point in the history
Merge pull request #560 from Igalia/pdo-area
  • Loading branch information
jaragunde authored Feb 9, 2022
2 parents fbe35ce + 66486a9 commit ff8fc34
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 179 deletions.
21 changes: 0 additions & 21 deletions model/dao/AreaDAO/AreaDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@
*/
abstract class AreaDAO extends BaseDAO{

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

/** Area retriever by id.
*
* This function retrieves the row from Area table with the id <var>$areaId</var> and creates an {@link AreaVO} with its data.
Expand All @@ -61,16 +50,6 @@ protected function __construct() {
*/
public abstract function getById($areaId);

/** Area retriever by name for PostgreSQL.
*
* This function retrieves the row from Area table with the name <var>$areaName</var> and creates an {@link AreaVO} with its data.
*
* @param string $areaName the name of the row we want to retrieve.
* @return AreaVO a value object {@link AreaVO} with its properties set to the values from the row.
* @throws {@link SQLQueryErrorException}
*/
public abstract function getByName($areaName);

/** Projects retriever by Area id.
*
* This function retrieves the rows from Project table that are assigned to the Area with
Expand Down
161 changes: 40 additions & 121 deletions model/dao/AreaDAO/PostgreSQLAreaDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,7 @@ class PostgreSQLAreaDAO extends AreaDAO{
* @see AreaDAO::__construct()
*/
function __construct() {
parent::__construct();
}

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

$areaVO = new AreaVO();

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

return $areaVO;
}

/** Area retriever by name for PostgreSQL.
*
* This function retrieves the row from Area table with the name <var>$areaName</var> and creates an {@link AreaVO} with its data.
*
* @param string $areaName the name of the row we want to retrieve.
* @return AreaVO a value object {@link AreaVO} with its properties set to the values from the row.
* @throws {@link SQLQueryErrorException}
*/
public function getByName($areaName) {
$sql = "SELECT * FROM area WHERE name=" . $areaName;
$result = $this->execute($sql);
return $result[0];
parent::__construct();
}

/** Area retriever by Id for PostgreSQL.
Expand All @@ -98,11 +65,13 @@ public function getByName($areaName) {
* @throws {@link SQLQueryErrorException}
*/
public function getById($areaId) {
if (!is_numeric($areaId))
throw new SQLIncorrectTypeException($areaId);
$sql = "SELECT * FROM area WHERE id=" . $areaId;
$result = $this->execute($sql);
return $result[0];
if (!is_numeric($areaId))
throw new SQLIncorrectTypeException($areaId);
$result = $this->runSelectQuery(
"SELECT * FROM area WHERE id=:areaId",
[':areaId' => $areaId],
'AreaVO');
return $result[0] ?? NULL;
}

/** Project retriever by Area id for PostgreSQL.
Expand Down Expand Up @@ -151,7 +120,7 @@ public function getAreaHistories($areaId) {
*/
public function getAll() {
$sql = "SELECT * FROM area ORDER BY id ASC";
return $this->execute($sql);
return $this->runSelectQuery($sql, array(), 'AreaVO');
}

/** Area updater for PostgreSQL.
Expand All @@ -165,23 +134,17 @@ public function getAll() {
public function update(AreaVO $areaVO) {
$affectedRows = 0;

if($areaVO->getId() >= 0) {
$currareaVO = $this->getById($areaVO->getId());
}

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

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

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

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

$affectedRows = pg_affected_rows($res);
$sql = "UPDATE area SET name=:name WHERE id=:id";
try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":name", $areaVO->getName(), PDO::PARAM_STR);
$statement->bindValue(":id", $areaVO->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;
Expand All @@ -198,18 +161,19 @@ public function update(AreaVO $areaVO) {
public function create(AreaVO $areaVO) {
$affectedRows = 0;

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

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

if ($res == NULL)
if (strpos(pg_last_error(), "unique_area_name"))
throw new SQLUniqueViolationException(pg_last_error());
else throw new SQLQueryErrorException(pg_last_error());
$sql = "INSERT INTO area (name) VALUES (:name)";
try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":name", $areaVO->getName(), PDO::PARAM_STR);
$statement->execute();

$areaVO->setId(DBPostgres::getId($this->connect, "area_id_seq"));
$areaVO->setId($this->pdo->lastInsertId('area_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;

Expand All @@ -226,63 +190,18 @@ public function create(AreaVO $areaVO) {
public function delete(AreaVO $areaVO) {
$affectedRows = 0;

// Check for an area ID.
if($areaVO->getId() >= 0) {
$currareaVO = $this->getById($areaVO->getId());
}

// Delete an area.
if(sizeof($currareaVO) > 0) {
$sql = "DELETE FROM area WHERE id=".$areaVO->getId();
$sql = "DELETE FROM area WHERE id=:id";
try {
$statement = $this->pdo->prepare($sql);
$statement->bindValue(":id", $areaVO->getId(), PDO::PARAM_INT);
$statement->execute();

$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;
}
}




/*//Uncomment these lines in order to do a simple test of the Dao
$dao = new PostgreSQLareaDAO();
// We create a new area
$area = new areaVO();
$area->setName("Players");
$dao->create($area);
print ("New area Id is ". $area->getId() ."\n");
// We search for the new Id
$area = $dao->getById($area->getId());
print ("New area Id found is ". $area->getId() ."\n");
// We update the area with a differente name
$area->setName("Non-players");
$dao->update($area);
// We search for the new name
$area = $dao->getById($area->getId());
print ("New area name found is ". $area->getName() ."\n");
// We delete the new area
$dao->delete($area);*/
11 changes: 9 additions & 2 deletions model/dao/BaseDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ protected function __construct() {

/** Value object constructor.
*
* This is the function that DAOs will use to create new value objects with data retrieved from database.
* This is the function that DAOs will use to create new value objects with data retrieved
* from database.
*
* A default implementation is provided so PDO-based DAOs don't have to add a placeholder
* function, but it is expected to be overriden in DAOs that don't use the new API yet.
*
* @param array $row an array with the values from a row.
* @return mixed a value object with its properties set to the values from <var>$row</var>.
*/
abstract protected function setValues($row);
protected function setValues($row)
{
error_log("Placeholder BaseDAO::setValues() called");
}

/** SQL retrieving data sentences performer.
*
Expand Down
9 changes: 0 additions & 9 deletions model/dao/CustomerDAO/PostgreSQLCustomerDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ function __construct() {
parent::__construct();
}

/**
* 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)
{
error_log("Unused CustomerDAO::setValues() called");
}

/** Customer retriever by id.
*
* This function retrieves the row from Customer table with the id
Expand Down
9 changes: 0 additions & 9 deletions model/dao/SectorDAO/PostgreSQLSectorDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ function __construct() {
parent::__construct();
}

/**
* 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)
{
error_log("Unused SectorDAO::setValues() called");
}

/** Sector retriever by id for PostgreSQL.
*
* This function retrieves the row from Sector table with the id
Expand Down
8 changes: 0 additions & 8 deletions model/dao/TemplateDAO/PostgreSQLTemplateDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ function __construct() {
parent::__construct();
}

/**
* This method is declared to fulfill TemplateVO 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) {
error_log("Unused TemplateVO::setValues() called");
}

/** Template retriever by id for PostgreSQL.
*
* This function retrieves the row from Template table with the id <var>$templateId</var> and creates a {@link TemplateVO} with its data.
Expand Down
4 changes: 1 addition & 3 deletions web/services/createAreasService.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='createAreas'><ok>Operation Success!</ok><areas>";
Expand Down
4 changes: 1 addition & 3 deletions web/services/deleteAreasService.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='deleteAreas'><ok>Operation Success!</ok></return>";

} while (false);
Expand Down
4 changes: 1 addition & 3 deletions web/services/updateAreasService.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='updateAreas'><ok>Operation Success!</ok><areas>";
Expand Down

0 comments on commit ff8fc34

Please sign in to comment.