@@ -51,43 +51,36 @@ class PostgreSQLSectorDAO extends SectorDAO{
51
51
* @see SectorDAO::__construct()
52
52
*/
53
53
function __construct () {
54
- parent ::__construct ();
54
+ parent ::__construct ();
55
55
}
56
56
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.
64
60
*/
65
61
protected function setValues ($ row )
66
62
{
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 " );
75
64
}
76
65
77
66
/** Sector retriever by id for PostgreSQL.
78
67
*
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.
80
70
*
81
71
* @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.
83
74
* @throws {@link SQLQueryErrorException}
84
75
*/
85
76
public function getById ($ sectorId ) {
86
77
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 ;
91
84
}
92
85
93
86
/** Sectors retriever for PostgreSQL.
@@ -100,7 +93,7 @@ public function getById($sectorId) {
100
93
*/
101
94
public function getAll () {
102
95
$ sql = "SELECT * FROM sector ORDER BY id ASC " ;
103
- return $ this ->execute ($ sql );
96
+ return $ this ->runSelectQuery ($ sql, array (), ' SectorVO ' );
104
97
}
105
98
106
99
/** Sector updater for PostgreSQL.
@@ -112,26 +105,19 @@ public function getAll() {
112
105
* @throws {@link SQLQueryErrorException}, {@link SQLUniqueViolationException}
113
106
*/
114
107
public function update (SectorVO $ sectorVO ) {
115
-
116
108
$ affectedRows = 0 ;
117
109
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 ());
135
121
}
136
122
137
123
return $ affectedRows ;
@@ -148,18 +134,19 @@ public function update(SectorVO $sectorVO) {
148
134
public function create (SectorVO $ sectorVO ) {
149
135
$ affectedRows = 0 ;
150
136
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 ();
154
142
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 ' ));
159
144
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
+ }
163
150
164
151
return $ affectedRows ;
165
152
@@ -176,61 +163,18 @@ public function create(SectorVO $sectorVO) {
176
163
public function delete (SectorVO $ sectorVO ) {
177
164
$ affectedRows = 0 ;
178
165
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 ();
183
171
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
+ }
192
177
193
178
return $ affectedRows ;
194
179
}
195
180
}
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);*/
0 commit comments