Skip to content

Commit edb994c

Browse files
authored
[#513] Port Sector DAO to use PDO.
Merge pull request #553 from Igalia/pdo-sector
2 parents edd6c61 + b8f9d25 commit edb994c

File tree

5 files changed

+51
-133
lines changed

5 files changed

+51
-133
lines changed

model/dao/SectorDAO/PostgreSQLSectorDAO.php

+48-104
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,36 @@ class PostgreSQLSectorDAO extends SectorDAO{
5151
* @see SectorDAO::__construct()
5252
*/
5353
function __construct() {
54-
parent::__construct();
54+
parent::__construct();
5555
}
5656

57-
/** Sector value object constructor for PostgreSQL.
58-
*
59-
* This function creates a new {@link SectorVO} with data retrieved from database.
60-
*
61-
* @param array $row an array with the Sector values from a row.
62-
* @return SectorVO an {@link SectorVO} with its properties set to the values from <var>$row</var>.
63-
* @see SectorVO
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.
6460
*/
6561
protected function setValues($row)
6662
{
67-
68-
$sectorVO = new SectorVO();
69-
70-
$sectorVO->setId($row['id']);
71-
$sectorVO->setName($row['name']);
72-
73-
return $sectorVO;
74-
63+
error_log("Unused SectorDAO::setValues() called");
7564
}
7665

7766
/** Sector retriever by id for PostgreSQL.
7867
*
79-
* This function retrieves the row from Sector table with the id <var>$sectorId</var> and creates a {@link SectorVO} with its data.
68+
* This function retrieves the row from Sector table with the id
69+
* <var>$sectorId</var> and creates a {@link SectorVO} with its data.
8070
*
8171
* @param int $sectorId the id of the row we want to retrieve.
82-
* @return SectorVO a value object {@link SectorVO} with its properties set to the values from the row.
72+
* @return SectorVO a value object {@link SectorVO} with its properties set
73+
* to the values from the row, or NULL if no object was found for that id.
8374
* @throws {@link SQLQueryErrorException}
8475
*/
8576
public function getById($sectorId) {
8677
if (!is_numeric($sectorId))
87-
throw new SQLIncorrectTypeException($sectorId);
88-
$sql = "SELECT * FROM sector WHERE id=" . $sectorId;
89-
$result = $this->execute($sql);
90-
return $result[0];
78+
throw new SQLIncorrectTypeException($customerId);
79+
$result = $this->runSelectQuery(
80+
"SELECT * FROM sector WHERE id=:sectorId",
81+
[':sectorId' => $sectorId],
82+
'SectorVO');
83+
return $result[0] ?? NULL;
9184
}
9285

9386
/** Sectors retriever for PostgreSQL.
@@ -100,7 +93,7 @@ public function getById($sectorId) {
10093
*/
10194
public function getAll() {
10295
$sql = "SELECT * FROM sector ORDER BY id ASC";
103-
return $this->execute($sql);
96+
return $this->runSelectQuery($sql, array(), 'SectorVO');
10497
}
10598

10699
/** Sector updater for PostgreSQL.
@@ -112,26 +105,19 @@ public function getAll() {
112105
* @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException}
113106
*/
114107
public function update(SectorVO $sectorVO) {
115-
116108
$affectedRows = 0;
117109

118-
if($sectorVO->getId() >= 0) {
119-
$currsectorVO = $this->getById($sectorVO->getId());
120-
}
121-
122-
// If the query returned a row then update
123-
if(sizeof($currsectorVO) > 0) {
124-
125-
$sql = "UPDATE sector SET name=" . DBPostgres::checkStringNull($sectorVO->getName()) . " WHERE id=".$sectorVO->getId();
126-
127-
$res = pg_query($this->connect, $sql);
128-
129-
if ($res == NULL)
130-
if (strpos(pg_last_error(), "unique_sector_name"))
131-
throw new SQLUniqueViolationException(pg_last_error());
132-
else throw new SQLQueryErrorException(pg_last_error());
133-
134-
$affectedRows = pg_affected_rows($res);
110+
$sql = "UPDATE sector SET name=:name WHERE id=:id";
111+
try {
112+
$statement = $this->pdo->prepare($sql);
113+
$statement->bindValue(":name", $sectorVO->getName(), PDO::PARAM_STR);
114+
$statement->bindValue(":id", $sectorVO->getId(), PDO::PARAM_INT);
115+
$statement->execute();
116+
117+
$affectedRows = $statement->rowCount();
118+
} catch (PDOException $e) {
119+
error_log('Query failed: ' . $e->getMessage());
120+
throw new SQLQueryErrorException($e->getMessage());
135121
}
136122

137123
return $affectedRows;
@@ -148,18 +134,19 @@ public function update(SectorVO $sectorVO) {
148134
public function create(SectorVO $sectorVO) {
149135
$affectedRows = 0;
150136

151-
$sql = "INSERT INTO sector (name) VALUES (" . DBPostgres::checkStringNull($sectorVO->getName()) . ")";
152-
153-
$res = pg_query($this->connect, $sql);
137+
$sql = "INSERT INTO sector (name) VALUES (:name)";
138+
try {
139+
$statement = $this->pdo->prepare($sql);
140+
$statement->bindValue(":name", $sectorVO->getName(), PDO::PARAM_STR);
141+
$statement->execute();
154142

155-
if ($res == NULL)
156-
if (strpos(pg_last_error(), "unique_sector_name"))
157-
throw new SQLUniqueViolationException(pg_last_error());
158-
else throw new SQLQueryErrorException(pg_last_error());
143+
$sectorVO->setId($this->pdo->lastInsertId('sector_id_seq'));
159144

160-
$sectorVO->setId(DBPostgres::getId($this->connect, "sector_id_seq"));
161-
162-
$affectedRows = pg_affected_rows($res);
145+
$affectedRows = $statement->rowCount();
146+
} catch (PDOException $e) {
147+
error_log('Query failed: ' . $e->getMessage());
148+
throw new SQLQueryErrorException($e->getMessage());
149+
}
163150

164151
return $affectedRows;
165152

@@ -176,61 +163,18 @@ public function create(SectorVO $sectorVO) {
176163
public function delete(SectorVO $sectorVO) {
177164
$affectedRows = 0;
178165

179-
// Check for a sector ID.
180-
if($sectorVO->getId() >= 0) {
181-
$currsectorVO = $this->getById($sectorVO->getId());
182-
}
166+
$sql = "DELETE FROM sector WHERE id=:id";
167+
try {
168+
$statement = $this->pdo->prepare($sql);
169+
$statement->bindValue(":id", $sectorVO->getId(), PDO::PARAM_INT);
170+
$statement->execute();
183171

184-
// Delete a sector.
185-
if(sizeof($currsectorVO) > 0) {
186-
$sql = "DELETE FROM sector WHERE id=".$sectorVO->getId();
187-
188-
$res = pg_query($this->connect, $sql);
189-
if ($res == NULL) throw new SQLQueryErrorException(pg_last_error());
190-
$affectedRows = pg_affected_rows($res);
191-
}
172+
$affectedRows = $statement->rowCount();
173+
} catch (PDOException $e) {
174+
error_log('Query failed: ' . $e->getMessage());
175+
throw new SQLQueryErrorException($e->getMessage());
176+
}
192177

193178
return $affectedRows;
194179
}
195180
}
196-
197-
198-
199-
200-
/*//Uncomment these lines in order to do a simple test of the Dao
201-
202-
203-
204-
$dao = new PostgreSQLSectorDAO();
205-
206-
// We create a new sector
207-
208-
$sector = new sectorVO();
209-
210-
$sector->setName("Telenet");
211-
212-
$dao->create($sector);
213-
214-
print ("New sector Id is ". $sector->getId() ."\n");
215-
216-
// We search for the new Id
217-
218-
$sector = $dao->getById($sector->getId());
219-
220-
print ("New sector Id found is ". $sector->getId() ."\n");
221-
222-
// We update the sector with a differente name
223-
224-
$sector->setName("Intranet");
225-
226-
$dao->update($sector);
227-
228-
// We search for the new name
229-
230-
$sector = $dao->getById($sector->getId());
231-
232-
print ("New sector name found is ". $sector->getName() ."\n");
233-
234-
// We delete the new sector
235-
236-
$dao->delete($sector);*/

model/dao/SectorDAO/SectorDAO.php

-11
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@
4040
*/
4141
abstract class SectorDAO extends BaseDAO{
4242

43-
/** Sector DAO constructor.
44-
*
45-
* This is the base constructor of Sector 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
/** Sector retriever by id.
5544
*
5645
* This function retrieves the row from Sector table with the id <var>$sectorId</var> and creates a {@link SectorVO} with its data.

web/services/createSectorsService.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@
108108

109109
} while ($parser->read());
110110

111-
//var_dump($createSectors);
112-
113-
114111
if (count($createSectors) >= 1)
115112
foreach((array)$createSectors as $createSector)
116113
{
@@ -122,9 +119,7 @@
122119

123120
}
124121

125-
126-
127-
if (!$string)
122+
if (!isset($string))
128123
{
129124

130125
$string = "<return service='createSectors'><ok>Operation Success!</ok><sectors>";

web/services/deleteSectorsService.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@
108108

109109
} while ($parser->read());
110110

111-
//var_dump($deleteSectors);
112-
113-
114111
if (count($deleteSectors) >= 1)
115112
foreach((array)$deleteSectors as $deleteSector)
116113
{
@@ -122,9 +119,7 @@
122119

123120
}
124121

125-
126-
127-
if (!$string)
122+
if (!isset($string))
128123
$string = "<return service='deleteSectors'><ok>Operation Success!</ok></return>";
129124

130125
} while (false);

web/services/updateSectorsService.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@
117117

118118
} while ($parser->read());
119119

120-
//var_dump($createSectors);
121-
122-
123120
if (count($updateSectors) >= 1)
124121
foreach((array)$updateSectors as $updateSector)
125122
{
@@ -131,9 +128,7 @@
131128

132129
}
133130

134-
135-
136-
if (!$string)
131+
if (!isset($string))
137132
{
138133

139134
$string = "<return service='updateSectors'><ok>Operation Success!</ok><sectors>";

0 commit comments

Comments
 (0)