1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace midorikocak\nanodb; |
||||
6 | |||||
7 | use InvalidArgumentException; |
||||
8 | use midorikocak\querymaker\QueryInterface; |
||||
9 | use midorikocak\querymaker\QueryMaker; |
||||
10 | use PDO; |
||||
11 | use PDOStatement; |
||||
12 | |||||
13 | class Database implements DatabaseInterface |
||||
14 | { |
||||
15 | private PDO $db; |
||||
16 | private ?QueryInterface $query = null; |
||||
17 | private ?PDOStatement $statement = null; |
||||
18 | |||||
19 | 22 | public function __construct(PDO $db, ?QueryInterface $query = null) |
|||
20 | { |
||||
21 | 22 | $this->db = $db; |
|||
22 | 22 | $this->query = $query ?? new QueryMaker(); |
|||
23 | 22 | } |
|||
24 | |||||
25 | 1 | public function query(QueryInterface $query): self |
|||
26 | { |
||||
27 | 1 | $this->reset(); |
|||
28 | 1 | $this->query = $query; |
|||
29 | 1 | return $this; |
|||
30 | } |
||||
31 | |||||
32 | 20 | public function select($table, array $columns = ['*']): self |
|||
33 | { |
||||
34 | 20 | $this->reset(); |
|||
35 | 20 | $this->query->select($table, $columns); |
|||
0 ignored issues
–
show
|
|||||
36 | 20 | return $this; |
|||
37 | } |
||||
38 | |||||
39 | public function count($table): self |
||||
40 | { |
||||
41 | $this->reset(); |
||||
42 | $this->query->count($table); |
||||
43 | return $this; |
||||
44 | } |
||||
45 | |||||
46 | 3 | public function delete($table): self |
|||
47 | { |
||||
48 | 3 | $this->reset(); |
|||
49 | 3 | $this->query->delete($table); |
|||
50 | 3 | return $this; |
|||
51 | } |
||||
52 | |||||
53 | 5 | public function update($table, array $values): self |
|||
54 | { |
||||
55 | 5 | $this->reset(); |
|||
56 | 5 | $this->query->update($table, $values); |
|||
57 | 5 | return $this; |
|||
58 | } |
||||
59 | |||||
60 | 6 | public function insert($table, array $values): self |
|||
61 | { |
||||
62 | 6 | $this->reset(); |
|||
63 | 6 | $this->query->insert($table, $values); |
|||
64 | 6 | return $this; |
|||
65 | } |
||||
66 | |||||
67 | 15 | public function where($key, $value, string $operator = '='): self |
|||
68 | { |
||||
69 | 15 | $this->query->where($key, $value, $operator); |
|||
70 | 15 | return $this; |
|||
71 | } |
||||
72 | |||||
73 | public function and($key, $value, string $operator = '='): self |
||||
74 | { |
||||
75 | $this->query->and($key, $value, $operator); |
||||
76 | return $this; |
||||
77 | } |
||||
78 | |||||
79 | 1 | public function orderBy($key, $order = 'ASC'): self |
|||
80 | { |
||||
81 | 1 | if ($order !== 'DESC' && $order !== 'ASC') { |
|||
82 | throw new InvalidArgumentException('Invalid order value'); |
||||
83 | } |
||||
84 | |||||
85 | 1 | $this->query->orderBy($key, $order); |
|||
86 | 1 | return $this; |
|||
87 | } |
||||
88 | |||||
89 | 1 | public function limit(int $limit): self |
|||
90 | { |
||||
91 | 1 | $this->query->limit($limit); |
|||
92 | 1 | return $this; |
|||
93 | } |
||||
94 | |||||
95 | 1 | public function offset(int $offset): self |
|||
96 | { |
||||
97 | 1 | $this->query->offset($offset); |
|||
98 | 1 | return $this; |
|||
99 | } |
||||
100 | |||||
101 | public function or($key, $value, string $operator = '='): self |
||||
102 | { |
||||
103 | $this->query->or($key, $value, $operator); |
||||
104 | return $this; |
||||
105 | } |
||||
106 | |||||
107 | public function between($key, $before, $after): self |
||||
108 | { |
||||
109 | $this->query->between($key, $before, $after); |
||||
110 | return $this; |
||||
111 | } |
||||
112 | |||||
113 | 10 | public function lastInsertId(): string |
|||
114 | { |
||||
115 | 10 | return $this->db->lastInsertId(); |
|||
116 | } |
||||
117 | |||||
118 | 4 | public function rowCount(): int |
|||
119 | { |
||||
120 | 4 | return $this->statement->rowCount(); |
|||
0 ignored issues
–
show
The method
rowCount() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
121 | } |
||||
122 | |||||
123 | 22 | public function execute(): bool |
|||
124 | { |
||||
125 | 22 | $this->statement = $this->db->prepare($this->query->getStatement()); |
|||
126 | 22 | return $this->statement->execute($this->query->getParams()); |
|||
127 | } |
||||
128 | |||||
129 | 17 | public function fetch(): array |
|||
130 | { |
||||
131 | 17 | $this->execute(); |
|||
132 | 17 | $toReturn = $this->statement->fetch(PDO::FETCH_ASSOC); |
|||
133 | 17 | return $toReturn === false ? [] : $toReturn; |
|||
134 | } |
||||
135 | |||||
136 | 5 | public function fetchAll(): array |
|||
137 | { |
||||
138 | 5 | $this->execute(); |
|||
139 | |||||
140 | 5 | $toReturn = $this->statement->fetchAll(PDO::FETCH_ASSOC); |
|||
141 | 5 | return $toReturn === false ? [] : $toReturn; |
|||
142 | } |
||||
143 | |||||
144 | 22 | private function reset() |
|||
145 | { |
||||
146 | 22 | $this->statement = null; |
|||
147 | 22 | } |
|||
148 | } |
||||
149 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.