Repository navigation
Expand file tree
/
Copy pathDbServiceInterface.php
More file actions
119 lines (110 loc) · 4.35 KB
/
Copy pathDbServiceInterface.php
File metadata and controls
119 lines (110 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
declare(strict_types=1);
namespace SqlFirstDemo\Db;
use Generator;
use SqlFirstDemo\Result\RowSet;
/**
* The ONE thing application code injects to talk to the database.
*
* WHY AN INTERFACE (and a small one)
* ----------------------------------
* Everything that touches the database goes through these few methods. That gives you three
* things at once:
*
* 1. A single place for cross-cutting behaviour: connection-loss retry, slow-query logging,
* statement caching, wrapping driver errors in one exception type. See {@see PdoDbService}.
* 2. A seam for tests. {@see \SqlFirstDemo\Testing\FakeDbService} implements this same
* interface, keyed by SQL text, so a query class can be unit tested without a database and
* without a mocking library.
* 3. A surface small enough to read in one sitting. There is no query builder, no entity
* manager and no hidden lazy loading behind it. What you pass in is the SQL that runs.
*
* ONE INSTANCE == ONE CONNECTION
* ------------------------------
* There are deliberately no "database name" parameters here. An application that talks to two
* databases wires two instances (for example a read replica and a primary), and each query class
* is handed the one it needs.
*
* PARAMETERS ARE ALWAYS BOUND
* ---------------------------
* Every method takes the SQL and a separate array of named parameters (`:id` → `['id' => 7]`).
* There is no method that interpolates values into the SQL string, so there is no SQL-injection
* surface for values. Identifiers (table and column names) cannot be bound by PDO at all; those
* go through {@see Identifier} instead.
*/
interface DbServiceInterface
{
/**
* The safe-by-default read: rows wrapped in a typed {@see RowSet}.
*
* Prefer this to {@see self::query()} for anything you hydrate into an object. A RowSet makes
* you say how many rows you expect (`single()`, `firstOrNull()`, `all()`) instead of reaching
* for `$rows[0]` and hoping.
*
* @param array<string, scalar|null> $params named parameters, without the leading colon
*
* @throws QueryException
*/
public function queryRows(string $sql, array $params = []): RowSet;
/**
* The raw read: a list of associative arrays exactly as PDO returned them.
*
* Kept for the rare caller that genuinely wants untyped arrays (a CSV export, say). Everything
* else should use {@see self::queryRows()}.
*
* @param array<string, scalar|null> $params
*
* @return list<array<string, mixed>>
*
* @throws QueryException
*/
public function query(string $sql, array $params = []): array;
/**
* Stream a large result set one row at a time, in constant memory.
*
* Use it for exports and batch jobs over tables too big to hold in an array. The generator
* keeps the statement open until it is exhausted, so finish (or discard) it before running
* another query on the same connection.
*
* @param array<string, scalar|null> $params
*
* @return Generator<int, array<string, mixed>>
*
* @throws QueryException
*/
public function stream(string $sql, array $params = []): Generator;
/**
* Run any non-SELECT statement and return the number of affected rows.
*
* @param array<string, scalar|null> $params
*
* @throws QueryException
*/
public function execute(string $sql, array $params = []): int;
/**
* Run an INSERT and return the auto-increment id it generated.
*
* It refuses SQL that does not start with INSERT, because "last insert id" after an UPDATE is
* whatever the previous INSERT on this connection produced, which is a confusing bug to chase.
*
* @param array<string, scalar|null> $params
*
* @throws QueryException
*/
public function insert(string $sql, array $params = []): int;
/**
* Run `$action` inside a transaction: COMMIT if it returns, ROLLBACK if it throws.
*
* Only meaningful for transactional storage engines (InnoDB). On MyISAM the ROLLBACK silently
* does nothing, which gives you the appearance of atomicity with none of the protection.
*
* @template T
*
* @param callable(): T $action
*
* @return T
*
* @throws QueryException
*/
public function transactional(callable $action): mixed;
}