@@ -141,13 +141,10 @@ public function bookByTags(string $tags): array
141141 {
142142 $ stmt = $ this ->pdo ->prepare (bookByTags);
143143 $ stmt ->execute ([$ tags ]);
144- $ results = $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
145- /**
146- * @var BookByTagsRow[]
147- */
144+ $ results = $ stmt ->fetchAll (\PDO ::FETCH_NUM );
148145 $ ret = [];
149146 foreach ($ results as $ row ) {
150- $ ret [] = new BookByTagsRow ($ row [" book_id " ], $ row [" title " ], $ row [" name " ], $ row [" isbn " ], $ row [" tags " ]);
147+ $ ret [] = new BookByTagsRow ($ row [0 ], $ row [1 ], $ row [2 ], $ row [3 ], $ row [4 ]);
151148 }
152149 return $ ret ;
153150 }
@@ -160,13 +157,10 @@ public function bookByTagsMultiple(array $tags): array
160157 {
161158 $ stmt = $ this ->pdo ->prepare (bookByTagsMultiple);
162159 $ stmt ->execute ([$ tags ]);
163- $ results = $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
164- /**
165- * @var BookByTagsMultipleRow[]
166- */
160+ $ results = $ stmt ->fetchAll (\PDO ::FETCH_NUM );
167161 $ ret = [];
168162 foreach ($ results as $ row ) {
169- $ ret [] = new BookByTagsMultipleRow ($ row [" book_id " ], $ row [" title " ], $ row [" name " ], $ row [" isbn " ], $ row [" tags " ]);
163+ $ ret [] = new BookByTagsMultipleRow ($ row [0 ], $ row [1 ], $ row [2 ], $ row [3 ], $ row [4 ]);
170164 }
171165 return $ ret ;
172166 }
@@ -178,15 +172,11 @@ public function bookByTagsMultiple(array $tags): array
178172 public function bookByTitleYear (string $ uuidToBin , int $ yr ): array
179173 {
180174 $ stmt = $ this ->pdo ->prepare (bookByTitleYear);
181- $ stmt ->execute ([$ uuidToBin ,
182- $ yr ]);
183- $ results = $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
184- /**
185- * @var Book[]
186- */
175+ $ stmt ->execute ([$ uuidToBin , $ yr ]);
176+ $ results = $ stmt ->fetchAll (\PDO ::FETCH_NUM );
187177 $ ret = [];
188178 foreach ($ results as $ row ) {
189- $ ret [] = new Book ($ row [" book_id " ], $ row [" author_id " ], $ row [" isbn " ], $ row [" book_type " ], $ row [" title " ], $ row [" yr " ], $ row [" available " ] == null ? null : new \ DateTimeImmutable ( $ row [ " available " ]) , $ row [" tags " ]);
179+ $ ret [] = new Book ($ row [0 ], $ row [1 ], $ row [2 ], $ row [3 ], $ row [4 ], $ row [5 ], $ row [6 ] , $ row [7 ]);
190180 }
191181 return $ ret ;
192182 }
@@ -203,22 +193,9 @@ public function createAuthor(string $name): int|string {
203193 /**
204194 * @throws \Exception
205195 */
206- public function createBook (
207- int $ authorId ,
208- string $ isbn ,
209- string $ bookType ,
210- string $ uuidToBin ,
211- int $ yr ,
212- \DateTimeImmutable $ available ,
213- string $ tags ): int |string {
196+ public function createBook (int $ authorId , string $ isbn , string $ bookType , string $ uuidToBin , int $ yr , string $ available , string $ tags ): int |string {
214197 $ stmt = $ this ->pdo ->prepare (createBook);
215- $ stmt ->execute ([$ authorId ,
216- $ isbn ,
217- $ bookType ,
218- $ uuidToBin ,
219- $ yr ,
220- $ available ,
221- $ tags ]);
198+ $ stmt ->execute ([$ authorId , $ isbn , $ bookType , $ uuidToBin , $ yr , $ available , $ tags ]);
222199 return $ this ->pdo ->lastInsertId ();
223200 }
224201
@@ -228,8 +205,7 @@ public function createBook(
228205 public function deleteAuthorBeforeYear (int $ yr , int $ authorId ): void
229206 {
230207 $ stmt = $ this ->pdo ->prepare (deleteAuthorBeforeYear);
231- $ stmt ->execute ([$ yr ,
232- $ authorId ]);
208+ $ stmt ->execute ([$ yr , $ authorId ]);
233209 }
234210
235211 /**
@@ -249,17 +225,14 @@ public function getAuthor(int $authorId): ?Author
249225 {
250226 $ stmt = $ this ->pdo ->prepare (getAuthor);
251227 $ stmt ->execute ([$ authorId ]);
252- $ results = $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
253- /**
254- * @var Author[]
255- */
228+ $ results = $ stmt ->fetchAll (\PDO ::FETCH_NUM );
256229 $ ret = [];
257230 if (count ($ results ) != 1 ){
258- throw new \Exception ("NOT 1 ROW RETURNED " );
259- }
260- foreach ($ results as $ row ) {
261- $ ret [] = new Author ($ row ["author_id " ], $ row ["name " ]);
231+ throw new \Exception ('Expected exactly 1 row, but got ' . count ($ results ));
262232 }
233+
234+ $ row = $ results [0 ];
235+ $ ret [] = new Author ($ row [0 ], $ row [1 ]);
263236 return $ ret [0 ];
264237 }
265238
@@ -271,48 +244,33 @@ public function getBook(int $bookId): ?Book
271244 {
272245 $ stmt = $ this ->pdo ->prepare (getBook);
273246 $ stmt ->execute ([$ bookId ]);
274- $ results = $ stmt ->fetchAll (\PDO ::FETCH_ASSOC );
275- /**
276- * @var Book[]
277- */
247+ $ results = $ stmt ->fetchAll (\PDO ::FETCH_NUM );
278248 $ ret = [];
279249 if (count ($ results ) != 1 ){
280- throw new \Exception ("NOT 1 ROW RETURNED " );
281- }
282- foreach ($ results as $ row ) {
283- $ ret [] = new Book ($ row ["book_id " ], $ row ["author_id " ], $ row ["isbn " ], $ row ["book_type " ], $ row ["title " ], $ row ["yr " ], $ row ["available " ] == null ? null : new \DateTimeImmutable ($ row ["available " ]), $ row ["tags " ]);
250+ throw new \Exception ('Expected exactly 1 row, but got ' . count ($ results ));
284251 }
252+
253+ $ row = $ results [0 ];
254+ $ ret [] = new Book ($ row [0 ], $ row [1 ], $ row [2 ], $ row [3 ], $ row [4 ], $ row [5 ], $ row [6 ], $ row [7 ]);
285255 return $ ret [0 ];
286256 }
287257
288258 /**
289259 * @throws \Exception
290260 */
291- public function updateBook (
292- string $ title ,
293- string $ tags ,
294- int $ bookId ): void
261+ public function updateBook (string $ title , string $ tags , int $ bookId ): void
295262 {
296263 $ stmt = $ this ->pdo ->prepare (updateBook);
297- $ stmt ->execute ([$ title ,
298- $ tags ,
299- $ bookId ]);
264+ $ stmt ->execute ([$ title , $ tags , $ bookId ]);
300265 }
301266
302267 /**
303268 * @throws \Exception
304269 */
305- public function updateBookISBN (
306- string $ title ,
307- string $ tags ,
308- string $ isbn ,
309- int $ bookId ): void
270+ public function updateBookISBN (string $ title , string $ tags , string $ isbn , int $ bookId ): void
310271 {
311272 $ stmt = $ this ->pdo ->prepare (updateBookISBN);
312- $ stmt ->execute ([$ title ,
313- $ tags ,
314- $ isbn ,
315- $ bookId ]);
273+ $ stmt ->execute ([$ title , $ tags , $ isbn , $ bookId ]);
316274 }
317275
318276}
0 commit comments