Skip to content

Commit 0761e33

Browse files
committed
Let's simplify this by using string for dates
I rarely use this class and if I do I will just use it manually
1 parent 5d20c20 commit 0761e33

19 files changed

Lines changed: 74 additions & 235 deletions

File tree

examples/minimal/src/Sqlc/MySQL/Book.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(
1717
// UUID
1818
public string $title,
1919
public int $yr,
20-
public \DateTimeImmutable $available,
20+
public string $available,
2121
public string $tags,
2222
)
2323
{}

examples/minimal/src/Sqlc/MySQL/Queries.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ public function bookByTitleYear(string $uuidToBin, int $yr): array;
2626

2727
public function createAuthor(string $name): int|string;
2828

29-
public function createBook(
30-
int $authorId,
31-
string $isbn,
32-
string $bookType,
33-
string $uuidToBin,
34-
int $yr,
35-
\DateTimeImmutable $available,
36-
string $tags): int|string;
29+
public function createBook(int $authorId, string $isbn, string $bookType, string $uuidToBin, int $yr, string $available, string $tags): int|string;
3730

3831
public function deleteAuthorBeforeYear(int $yr, int $authorId): void;
3932

@@ -43,16 +36,9 @@ public function getAuthor(int $authorId): ?Author;
4336

4437
public function getBook(int $bookId): ?Book;
4538

46-
public function updateBook(
47-
string $title,
48-
string $tags,
49-
int $bookId): void;
39+
public function updateBook(string $title, string $tags, int $bookId): void;
5040

51-
public function updateBookISBN(
52-
string $title,
53-
string $tags,
54-
string $isbn,
55-
int $bookId): void;
41+
public function updateBookISBN(string $title, string $tags, string $isbn, int $bookId): void;
5642

5743
}
5844

examples/minimal/src/Sqlc/MySQL/QueriesImpl.php

Lines changed: 24 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

examples/minimal/src/Sqlc/SQLite/Book.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(
1616
public string $bookType,
1717
public string $title,
1818
public int $yr,
19-
public \DateTimeImmutable $available,
19+
public string $available,
2020
public string $tags,
2121
)
2222
{}

examples/minimal/src/Sqlc/SQLite/Queries.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ public function bookByTitleYear(mixed $uuidToBin, int $yr): array;
2626

2727
public function createAuthor(string $name): int|string;
2828

29-
public function createBook(
30-
int $authorId,
31-
string $isbn,
32-
string $bookType,
33-
mixed $uuidToBin,
34-
int $yr,
35-
\DateTimeImmutable $available,
36-
string $tags): int|string;
29+
public function createBook(int $authorId, string $isbn, string $bookType, mixed $uuidToBin, int $yr, string $available, string $tags): int|string;
3730

3831
public function deleteAuthorBeforeYear(int $yr, int $authorId): void;
3932

@@ -48,16 +41,9 @@ public function getBook(int $bookId): ?Book;
4841
*/
4942
public function listAuthors(): array;
5043

51-
public function updateBook(
52-
string $title,
53-
string $tags,
54-
int $bookId): void;
44+
public function updateBook(string $title, string $tags, int $bookId): void;
5545

56-
public function updateBookISBN(
57-
string $title,
58-
string $tags,
59-
string $isbn,
60-
int $bookId): void;
46+
public function updateBookISBN(string $title, string $tags, string $isbn, int $bookId): void;
6147

6248
}
6349

0 commit comments

Comments
 (0)