Skip to content

Commit ff8fc34

Browse files
authored
[#513] Port Area DAO to use PDO.
Merge pull request #560 from Igalia/pdo-area
2 parents fbe35ce + 66486a9 commit ff8fc34

File tree

9 files changed

+52
-179
lines changed

9 files changed

+52
-179
lines changed

model/dao/AreaDAO/AreaDAO.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@
4040
*/
4141
abstract class AreaDAO extends BaseDAO{
4242

43-
/** Area DAO constructor.
44-
*
45-
* This is the base constructor of Area DAOs, and it just calls its parent's constructor.
46-
*
47-
* @throws {@link ConnectionErrorException}
48-
* @see BaseDAO::__construct()
49-
*/
50-
protected function __construct() {
51-
parent::__construct();
52-
}
53-
5443
/** Area retriever by id.
5544
*
5645
* This function retrieves the row from Area table with the id <var>$areaId</var> and creates an {@link AreaVO} with its data.
@@ -61,16 +50,6 @@ protected function __construct() {
6150
*/
6251
public abstract function getById($areaId);
6352

64-
/** Area retriever by name for PostgreSQL.
65-
*
66-
* This function retrieves the row from Area table with the name <var>$areaName</var> and creates an {@link AreaVO} with its data.
67-
*
68-
* @param string $areaName the name of the row we want to retrieve.
69-
* @return AreaVO a value object {@link AreaVO} with its properties set to the values from the row.
70-
* @throws {@link SQLQueryErrorException}
71-
*/
72-
public abstract function getByName($areaName);
73-
7453
/** Projects retriever by Area id.
7554
*
7655
* This function retrieves the rows from Project table that are assigned to the Area with

model/dao/AreaDAO/PostgreSQLAreaDAO.php

Lines changed: 40 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,7 @@ class PostgreSQLAreaDAO extends AreaDAO{
5353
* @see AreaDAO::__construct()
5454
*/
5555
function __construct() {
56-
parent::__construct();
57-
}
58-
59-
/** Area value object constructor for PostgreSQL.
60-
*
61-
* This function creates a new {@link AreaVO} with data retrieved from database.
62-
*
63-
* @param array $row an array with the Area values from a row.
64-
* @return AreaVO an {@link AreaVO} with its properties set to the values from <var>$row</var>.
65-
* @see AreaVO
66-
*/
67-
protected function setValues($row)
68-
{
69-
70-
$areaVO = new AreaVO();
71-
72-
$areaVO->setId($row['id']);
73-
$areaVO->setName($row['name']);
74-
75-
return $areaVO;
76-
}
77-
78-
/** Area retriever by name for PostgreSQL.
79-
*
80-
* This function retrieves the row from Area table with the name <var>$areaName</var> and creates an {@link AreaVO} with its data.
81-
*
82-
* @param string $areaName the name of the row we want to retrieve.
83-
* @return AreaVO a value object {@link AreaVO} with its properties set to the values from the row.
84-
* @throws {@link SQLQueryErrorException}
85-
*/
86-
public function getByName($areaName) {
87-
$sql = "SELECT * FROM area WHERE name=" . $areaName;
88-
$result = $this->execute($sql);
89-
return $result[0];
56+
parent::__construct();
9057
}
9158

9259
/** Area retriever by Id for PostgreSQL.
@@ -98,11 +65,13 @@ public function getByName($areaName) {
9865
* @throws {@link SQLQueryErrorException}
9966
*/
10067
public function getById($areaId) {
101-
if (!is_numeric($areaId))
102-
throw new SQLIncorrectTypeException($areaId);
103-
$sql = "SELECT * FROM area WHERE id=" . $areaId;
104-
$result = $this->execute($sql);
105-
return $result[0];
68+
if (!is_numeric($areaId))
69+
throw new SQLIncorrectTypeException($areaId);
70+
$result = $this->runSelectQuery(
71+
"SELECT * FROM area WHERE id=:areaId",
72+
[':areaId' => $areaId],
73+
'AreaVO');
74+
return $result[0] ?? NULL;
10675
}
10776

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

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

168-
if($areaVO->getId() >= 0) {
169-
$currareaVO = $this->getById($areaVO->getId());
170-
}
171-
172-
// If the query returned a row then update
173-
if(sizeof($currareaVO) > 0) {
174-
175-
$sql = "UPDATE area SET name=" . DBPostgres::checkStringNull($areaVO->getName()) . " WHERE id=".$areaVO->getId();
176-
177-
$res = pg_query($this->connect, $sql);
178-
179-
if ($res == NULL)
180-
if (strpos(pg_last_error(), "unique_area_name"))
181-
throw new SQLUniqueViolationException(pg_last_error());
182-
else throw new SQLQueryErrorException(pg_last_error());
183-
184-
$affectedRows = pg_affected_rows($res);
137+
$sql = "UPDATE area SET name=:name WHERE id=:id";
138+
try {
139+
$statement = $this->pdo->prepare($sql);
140+
$statement->bindValue(":name", $areaVO->getName(), PDO::PARAM_STR);
141+
$statement->bindValue(":id", $areaVO->getId(), PDO::PARAM_INT);
142+
$statement->execute();
143+
144+
$affectedRows = $statement->rowCount();
145+
} catch (PDOException $e) {
146+
error_log('Query failed: ' . $e->getMessage());
147+
throw new SQLQueryErrorException($e->getMessage());
185148
}
186149

187150
return $affectedRows;
@@ -198,18 +161,19 @@ public function update(AreaVO $areaVO) {
198161
public function create(AreaVO $areaVO) {
199162
$affectedRows = 0;
200163

201-
$sql = "INSERT INTO area (name) VALUES (" . DBPostgres::checkStringNull($areaVO->getName()) . ")";
202-
203-
$res = pg_query($this->connect, $sql);
204-
205-
if ($res == NULL)
206-
if (strpos(pg_last_error(), "unique_area_name"))
207-
throw new SQLUniqueViolationException(pg_last_error());
208-
else throw new SQLQueryErrorException(pg_last_error());
164+
$sql = "INSERT INTO area (name) VALUES (:name)";
165+
try {
166+
$statement = $this->pdo->prepare($sql);
167+
$statement->bindValue(":name", $areaVO->getName(), PDO::PARAM_STR);
168+
$statement->execute();
209169

210-
$areaVO->setId(DBPostgres::getId($this->connect, "area_id_seq"));
170+
$areaVO->setId($this->pdo->lastInsertId('area_id_seq'));
211171

212-
$affectedRows = pg_affected_rows($res);
172+
$affectedRows = $statement->rowCount();
173+
} catch (PDOException $e) {
174+
error_log('Query failed: ' . $e->getMessage());
175+
throw new SQLQueryErrorException($e->getMessage());
176+
}
213177

214178
return $affectedRows;
215179

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

229-
// Check for an area ID.
230-
if($areaVO->getId() >= 0) {
231-
$currareaVO = $this->getById($areaVO->getId());
232-
}
233-
234-
// Delete an area.
235-
if(sizeof($currareaVO) > 0) {
236-
$sql = "DELETE FROM area WHERE id=".$areaVO->getId();
193+
$sql = "DELETE FROM area WHERE id=:id";
194+
try {
195+
$statement = $this->pdo->prepare($sql);
196+
$statement->bindValue(":id", $areaVO->getId(), PDO::PARAM_INT);
197+
$statement->execute();
237198

238-
$res = pg_query($this->connect, $sql);
239-
240-
if ($res == NULL) throw new SQLQueryErrorException(pg_last_error());
241-
242-
$affectedRows = pg_affected_rows($res);
199+
$affectedRows = $statement->rowCount();
200+
} catch (PDOException $e) {
201+
error_log('Query failed: ' . $e->getMessage());
202+
throw new SQLQueryErrorException($e->getMessage());
243203
}
244204

245205
return $affectedRows;
246206
}
247207
}
248-
249-
250-
251-
252-
/*//Uncomment these lines in order to do a simple test of the Dao
253-
254-
255-
256-
$dao = new PostgreSQLareaDAO();
257-
258-
// We create a new area
259-
260-
$area = new areaVO();
261-
262-
$area->setName("Players");
263-
264-
$dao->create($area);
265-
266-
print ("New area Id is ". $area->getId() ."\n");
267-
268-
// We search for the new Id
269-
270-
$area = $dao->getById($area->getId());
271-
272-
print ("New area Id found is ". $area->getId() ."\n");
273-
274-
// We update the area with a differente name
275-
276-
$area->setName("Non-players");
277-
278-
$dao->update($area);
279-
280-
// We search for the new name
281-
282-
$area = $dao->getById($area->getId());
283-
284-
print ("New area name found is ". $area->getName() ."\n");
285-
286-
// We delete the new area
287-
288-
$dao->delete($area);*/

model/dao/BaseDAO.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,19 @@ protected function __construct() {
9797

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

107114
/** SQL retrieving data sentences performer.
108115
*

model/dao/CustomerDAO/PostgreSQLCustomerDAO.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ function __construct() {
5454
parent::__construct();
5555
}
5656

57-
/**
58-
* This method is declared to fulfill this class as non-abstract, but it should not be used.
59-
* PDO::FETCH_CLASS now takes care of transforming DB rows into VO objects.
60-
*/
61-
protected function setValues($row)
62-
{
63-
error_log("Unused CustomerDAO::setValues() called");
64-
}
65-
6657
/** Customer retriever by id.
6758
*
6859
* This function retrieves the row from Customer table with the id

model/dao/SectorDAO/PostgreSQLSectorDAO.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ function __construct() {
5454
parent::__construct();
5555
}
5656

57-
/**
58-
* This method is declared to fulfill this class as non-abstract, but it should not be used.
59-
* PDO::FETCH_CLASS now takes care of transforming DB rows into VO objects.
60-
*/
61-
protected function setValues($row)
62-
{
63-
error_log("Unused SectorDAO::setValues() called");
64-
}
65-
6657
/** Sector retriever by id for PostgreSQL.
6758
*
6859
* This function retrieves the row from Sector table with the id

model/dao/TemplateDAO/PostgreSQLTemplateDAO.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ function __construct() {
4848
parent::__construct();
4949
}
5050

51-
/**
52-
* This method is declared to fulfill TemplateVO as non-abstract, but it should not be used.
53-
* PDO::FETCH_CLASS now takes care of transforming DB rows into VO objects.
54-
*/
55-
protected function setValues($row) {
56-
error_log("Unused TemplateVO::setValues() called");
57-
}
58-
5951
/** Template retriever by id for PostgreSQL.
6052
*
6153
* This function retrieves the row from Template table with the id <var>$templateId</var> and creates a {@link TemplateVO} with its data.

web/services/createAreasService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@
116116

117117
}
118118

119-
120-
121-
if (!$string)
119+
if (!isset($string))
122120
{
123121

124122
$string = "<return service='createAreas'><ok>Operation Success!</ok><areas>";

web/services/deleteAreasService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@
116116

117117
}
118118

119-
120-
121-
if (!$string)
119+
if (!isset($string))
122120
$string = "<return service='deleteAreas'><ok>Operation Success!</ok></return>";
123121

124122
} while (false);

web/services/updateAreasService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@
125125

126126
}
127127

128-
129-
130-
if (!$string)
128+
if (!isset($string))
131129
{
132130

133131
$string = "<return service='updateAreas'><ok>Operation Success!</ok><areas>";

0 commit comments

Comments
 (0)