40
40
*/
41
41
class PostgreSQLTemplateDAO extends TemplateDAO{
42
42
43
+ /** The connection to DB.
44
+ *
45
+ * PDO object with an open connection to the database, initialized in the
46
+ * class constructor.
47
+ *
48
+ * @var resource
49
+ * @see __construct()
50
+ */
51
+ protected PDO $ pdo ;
52
+
43
53
/** Template DAO for PostgreSQL constructor.
44
54
*
45
- * This is the constructor of the implementation for PostgreSQL of {@link TemplateDAO}, and it just calls its parent's constructor.
55
+ * This is the constructor of the implementation for PostgreSQL of
56
+ * {@link TemplateDAO}. It sets up everything for database connection, using
57
+ * the parameters read from <i>{@link config.php}</i> and saving the open
58
+ * connection in <var>{@link $pdo}</var>.
59
+ * Notice this DAO connects to the DB through PDO, unlike the rest of the
60
+ * application.
46
61
*
47
62
* @throws {@link DBConnectionErrorException}
48
- * @see TemplateDAO::__construct()
49
63
*/
50
64
function __construct () {
65
+ // Call parent to initialize non-PDO database access, while we don't
66
+ // migrate all the methods here.
51
67
parent ::__construct ();
68
+
69
+ // TODO: EXTRA_DB_CONNECTION_PARAMETERS used to expect pg_connect
70
+ // parameters, which were space-separated, but PDO requires semicolons
71
+ $ connectionString = sprintf ("pgsql:host=%s;port=%d;user=%s;dbname=%s;password=%s;%s " ,
72
+ ConfigurationParametersManager::getParameter ('DB_HOST ' ),
73
+ ConfigurationParametersManager::getParameter ('DB_PORT ' ),
74
+ ConfigurationParametersManager::getParameter ('DB_USER ' ),
75
+ ConfigurationParametersManager::getParameter ('DB_NAME ' ),
76
+ ConfigurationParametersManager::getParameter ('DB_PASSWORD ' ),
77
+ ConfigurationParametersManager::getParameter ('EXTRA_DB_CONNECTION_PARAMETERS ' ));
78
+
79
+ try {
80
+ $ this ->pdo = new PDO ($ connectionString );
81
+ $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
82
+ } catch (PDOException $ e ) {
83
+ error_log ('Connection failed: ' . $ e ->getMessage ());
84
+ throw new DBConnectionErrorException ($ connectionString );
85
+ }
52
86
}
53
87
54
- /** Template value object constructor for PostgreSQL.
55
- *
56
- * This function creates a new {@link TemplateVO} with data retrieved from database.
57
- *
58
- * @param array $row an array with the Task values from a row.
59
- * @return TemplateVO a {@link TemplateVO} with its properties set to the values from <var>$row</var>.
60
- * @see TemplateVO
88
+ /**
89
+ * This method is declared to fulfill TemplateVO as non-abstract, but it should not be used.
90
+ * PDO::FETCH_CLASS now takes care of transforming DB rows into VO objects.
61
91
*/
62
92
protected function setValues ($ row ) {
63
- $ templateVO = new TemplateVO ();
64
-
65
- $ templateVO ->setId ($ row ['id ' ]);
66
- $ templateVO ->setName ($ row ['name ' ]);
67
- $ templateVO ->setStory ($ row ['story ' ]);
68
- $ templateVO ->setStory ($ row ['story ' ]);
69
- if (strtolower ($ row ['telework ' ]) == "t " )
70
- $ templateVO ->setTelework (True );
71
- elseif (strtolower ($ row ['telework ' ]) == "f " )
72
- $ templateVO ->setTelework (False );
73
- if (strtolower ($ row ['onsite ' ]) == "t " )
74
- $ templateVO ->setOnsite (True );
75
- elseif (strtolower ($ row ['onsite ' ]) == "f " )
76
- $ templateVO ->setOnsite (False );
77
- $ templateVO ->setText ($ row ['text ' ]);
78
- $ templateVO ->setTtype ($ row ['ttype ' ]);
79
- $ templateVO ->setUserId ($ row ['usrid ' ]);
80
- $ templateVO ->setProjectId ($ row ['projectid ' ]);
81
- $ templateVO ->setTaskStoryId ($ row ['task_storyid ' ]);
82
- $ templateVO ->setInitTime ($ row ['init_time ' ]);
83
- $ templateVO ->setEndTime ($ row ['end_time ' ]);
84
-
85
- return $ templateVO ;
93
+ error_log ("Unused TemplateVO::setValues() called " );
94
+ }
95
+
96
+ protected function runSelectQuery (string $ statement , array $ data ) {
97
+ try {
98
+ $ statement = $ this ->pdo ->prepare ($ statement );
99
+ $ statement ->execute ($ data );
100
+ return $ statement ->fetchAll (PDO ::FETCH_CLASS , 'TemplateVO ' );
101
+ } catch (PDOException $ e ) {
102
+ error_log ('Query failed: ' . $ e ->getMessage ());
103
+ throw new SQLQueryErrorException ($ e ->getMessage ());
104
+ }
86
105
}
87
106
88
107
/** Template retriever by id for PostgreSQL.
@@ -97,8 +116,9 @@ protected function setValues($row) {
97
116
public function getById ($ templateId ) {
98
117
if (!is_numeric ($ templateId ))
99
118
throw new SQLIncorrectTypeException ($ templateId );
100
- $ sql = "SELECT * FROM template WHERE id= " .$ templateId ;
101
- $ result = $ this ->execute ($ sql );
119
+ $ result = $ this ->runSelectQuery (
120
+ "SELECT * FROM template WHERE id=:id " ,
121
+ [':id ' => $ templateId ]);
102
122
return $ result [0 ] ?? NULL ;
103
123
}
104
124
@@ -115,8 +135,9 @@ public function getById($templateId) {
115
135
public function getByUserId ($ userId ) {
116
136
if (!is_numeric ($ userId ))
117
137
throw new SQLIncorrectTypeException ($ userId );
118
- $ sql = "SELECT * FROM template WHERE usrid= $ userId " ;
119
- $ result = $ this ->execute ($ sql );
138
+ $ result = $ this ->runSelectQuery (
139
+ "SELECT * FROM template WHERE usrid=:usrid " ,
140
+ [':usrid ' => $ userId ]);
120
141
return $ result ;
121
142
}
122
143
@@ -132,30 +153,34 @@ public function getByUserId($userId) {
132
153
public function create (TemplateVO $ templateVO ) {
133
154
$ affectedRows = 0 ;
134
155
135
- $ sql = "INSERT INTO template (name, story, telework, onsite, text, ttype, usrid, projectid, init_time, end_time, task_storyid) VALUES( " .
136
- DBPostgres::checkStringNull ($ templateVO ->getName ()) . ", " .
137
- DBPostgres::checkStringNull ($ templateVO ->getStory ()) . ", " .
138
- DBPostgres::boolToString ($ templateVO ->isTelework ()) . ", " .
139
- DBPostgres::boolToString ($ templateVO ->isOnsite ()) . ", " .
140
- DBPostgres::checkStringNull ($ templateVO ->getText ()) . ", " .
141
- DBPostgres::checkStringNull ($ templateVO ->getTtype ()) . ", " .
142
- DBPostgres::checkNull ($ templateVO ->getUserId ()) . ", " .
143
- DBPostgres::checkNull ($ templateVO ->getProjectId ()) . ", " .
144
- DBPostgres::checkNull ($ templateVO ->getInitTime ()) . ", " .
145
- DBPostgres::checkNull ($ templateVO ->getEndTime ()) . ", " .
146
- DBPostgres::checkNull ($ templateVO ->getTaskStoryId ()) .") " ;
147
-
148
- $ res = pg_query ($ this ->connect , $ sql );
149
-
150
- if ($ res == NULL )
151
- throw new SQLQueryErrorException (pg_last_error ());
152
-
153
- $ templateVO ->setId (DBPostgres::getId ($ this ->connect , "template_id_seq " ));
154
-
155
- $ affectedRows = pg_affected_rows ($ res );
156
-
156
+ $ sql = "INSERT INTO template (name, story, telework, onsite, text, " .
157
+ "ttype, usrid, projectid, init_time, end_time, task_storyid) " .
158
+ "VALUES(:name, :story, :telework, :onsite, :text, :ttype, " .
159
+ ":usrid, :projectid, :init_time, :end_time, :task_storyid) " ;
160
+
161
+ try {
162
+ $ statement = $ this ->pdo ->prepare ($ sql );
163
+ $ statement ->bindValue (":name " , $ templateVO ->getName (), PDO ::PARAM_STR );
164
+ $ statement ->bindValue (":story " , $ templateVO ->getStory (), PDO ::PARAM_STR );
165
+ $ statement ->bindValue (":telework " , $ templateVO ->isTelework (), PDO ::PARAM_BOOL );
166
+ $ statement ->bindValue (":onsite " , $ templateVO ->isOnsite (), PDO ::PARAM_BOOL );
167
+ $ statement ->bindValue (":text " , $ templateVO ->getText (), PDO ::PARAM_STR );
168
+ $ statement ->bindValue (":ttype " , $ templateVO ->getTtype (), PDO ::PARAM_STR );
169
+ $ statement ->bindValue (":usrid " , $ templateVO ->getUserId (), PDO ::PARAM_INT );
170
+ $ statement ->bindValue (":projectid " , $ templateVO ->getProjectId (), PDO ::PARAM_INT );
171
+ $ statement ->bindValue (":init_time " , $ templateVO ->getInitTime (), PDO ::PARAM_INT );
172
+ $ statement ->bindValue (":end_time " , $ templateVO ->getEndTime (), PDO ::PARAM_INT );
173
+ $ statement ->bindValue (":task_storyid " , $ templateVO ->getTaskStoryId (), PDO ::PARAM_INT );
174
+ $ statement ->execute ();
175
+
176
+ $ templateVO ->setId ($ this ->pdo ->lastInsertId ('template_id_seq ' ));
177
+
178
+ $ affectedRows = $ statement ->rowCount ();
179
+ } catch (PDOException $ e ) {
180
+ error_log ('Query failed: ' . $ e ->getMessage ());
181
+ throw new SQLQueryErrorException ($ e ->getMessage ());
182
+ }
157
183
return $ affectedRows ;
158
-
159
184
}
160
185
161
186
/**
@@ -186,20 +211,16 @@ public function batchCreate($templates) {
186
211
public function delete (TemplateVO $ templateVO ) {
187
212
$ affectedRows = 0 ;
188
213
189
- // Check for a task ID.
190
- if ($ templateVO ->getId () >= 0 ) {
191
- $ currTaskVO = $ this ->getById ($ templateVO ->getId ());
192
- }
193
-
194
- // Otherwise delete a task.
195
- if ($ currTaskVO ) {
196
- $ sql = "DELETE FROM template WHERE id= " .$ currTaskVO ->getId ();
214
+ $ sql = "DELETE FROM template WHERE id=:id " ;
197
215
198
- $ res = pg_query ($ this ->connect , $ sql );
199
- if ($ res == NULL ) throw new SQLQueryErrorException (pg_last_error ());
200
- $ affectedRows = pg_affected_rows ($ res );
216
+ try {
217
+ $ statement = $ this ->pdo ->prepare ($ sql );
218
+ $ statement ->execute ([':id ' => $ templateVO ->getId ()]);
219
+ $ affectedRows = $ statement ->rowCount ();
220
+ } catch (PDOException $ e ) {
221
+ error_log ('Query failed: ' . $ e ->getMessage ());
222
+ throw new SQLQueryErrorException ($ e ->getMessage ());
201
223
}
202
-
203
224
return $ affectedRows ;
204
225
}
205
226
@@ -226,8 +247,6 @@ public function batchDelete($templates){
226
247
* @throws SQLQueryErrorException
227
248
*/
228
249
public function getUserTemplates ($ userId ) {
229
- $ sql = "SELECT * FROM template where usrid= $ userId " ;
230
-
231
- return $ this ->execute ($ sql );
250
+ return $ this ->getByUserId ($ userId );
232
251
}
233
- }
252
+ }
0 commit comments