@@ -53,40 +53,7 @@ class PostgreSQLAreaDAO extends AreaDAO{
53
53
* @see AreaDAO::__construct()
54
54
*/
55
55
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 ();
90
57
}
91
58
92
59
/** Area retriever by Id for PostgreSQL.
@@ -98,11 +65,13 @@ public function getByName($areaName) {
98
65
* @throws {@link SQLQueryErrorException}
99
66
*/
100
67
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 ;
106
75
}
107
76
108
77
/** Project retriever by Area id for PostgreSQL.
@@ -151,7 +120,7 @@ public function getAreaHistories($areaId) {
151
120
*/
152
121
public function getAll () {
153
122
$ sql = "SELECT * FROM area ORDER BY id ASC " ;
154
- return $ this ->execute ($ sql );
123
+ return $ this ->runSelectQuery ($ sql, array (), ' AreaVO ' );
155
124
}
156
125
157
126
/** Area updater for PostgreSQL.
@@ -165,23 +134,17 @@ public function getAll() {
165
134
public function update (AreaVO $ areaVO ) {
166
135
$ affectedRows = 0 ;
167
136
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 ());
185
148
}
186
149
187
150
return $ affectedRows ;
@@ -198,18 +161,19 @@ public function update(AreaVO $areaVO) {
198
161
public function create (AreaVO $ areaVO ) {
199
162
$ affectedRows = 0 ;
200
163
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 ();
209
169
210
- $ areaVO ->setId (DBPostgres:: getId ( $ this ->connect , " area_id_seq " ));
170
+ $ areaVO ->setId ($ this ->pdo -> lastInsertId ( ' area_id_seq ' ));
211
171
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
+ }
213
177
214
178
return $ affectedRows ;
215
179
@@ -226,63 +190,18 @@ public function create(AreaVO $areaVO) {
226
190
public function delete (AreaVO $ areaVO ) {
227
191
$ affectedRows = 0 ;
228
192
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 ();
237
198
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 ());
243
203
}
244
204
245
205
return $ affectedRows ;
246
206
}
247
207
}
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);*/
0 commit comments