|
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 $queryMaker = null; |
|
17
|
|
|
private ?PDOStatement $statement = null; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(PDO $db) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->db = $db; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function select($table, array $columns = ['*']): self |
|
25
|
|
|
{ |
|
26
|
|
|
$this->queryMaker = QueryMaker::select($table, $columns); |
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function delete($table): self |
|
31
|
|
|
{ |
|
32
|
|
|
$this->queryMaker = QueryMaker::delete($table); |
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function update($table, array $values): self |
|
37
|
|
|
{ |
|
38
|
|
|
$this->queryMaker = QueryMaker::update($table, $values); |
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function insert($table, array $values): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->queryMaker = QueryMaker::insert($table, $values); |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function where($key, $value): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->queryMaker->where($key, $value); |
|
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function and($key, $value): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->queryMaker->and($key, $value); |
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function orderBy($key, $order = 'ASC'): self |
|
61
|
|
|
{ |
|
62
|
|
|
if ($order !== 'DESC' && $order !== 'ASC') { |
|
63
|
|
|
throw new InvalidArgumentException('Invalid order value'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->queryMaker->orderBy($key, $order); |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function limit(int $limit): self |
|
71
|
|
|
{ |
|
72
|
|
|
$this->queryMaker->limit($limit); |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function offset(int $offset): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->queryMaker->offset($offset); |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function or($key, $value): self |
|
83
|
|
|
{ |
|
84
|
|
|
$this->queryMaker->or($key, $value); |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function between($key, $before, $after): self |
|
89
|
|
|
{ |
|
90
|
|
|
$this->queryMaker->between($key, $before, $after); |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function lastInsertId(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->db->lastInsertId(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function rowCount(): int |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->statement->rowCount(); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function execute(): bool |
|
105
|
|
|
{ |
|
106
|
|
|
$this->statement = $this->db->prepare($this->queryMaker->getStatement()); |
|
107
|
|
|
return $this->statement->execute($this->queryMaker->getParams()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function fetch(): array |
|
111
|
|
|
{ |
|
112
|
|
|
$this->execute(); |
|
113
|
|
|
$toReturn = $this->statement->fetch(PDO::FETCH_ASSOC); |
|
114
|
|
|
return $toReturn === false ? [] : $toReturn; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function fetchAll(): array |
|
118
|
|
|
{ |
|
119
|
|
|
$this->execute(); |
|
120
|
|
|
|
|
121
|
|
|
$toReturn = $this->statement->fetchAll(PDO::FETCH_ASSOC); |
|
122
|
|
|
return $toReturn === false ? [] : $toReturn; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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.